xref: /titanic_54/usr/src/uts/common/io/kstat.c (revision 3c8ab7ef4c92d599fef219e45c08fe01e0fbe880)
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.
24*3c8ab7efSHans 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
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
1097c478bd9Sstevel@tonic-gate 	 * var-size kstat, we don't know the size until after the update
1107c478bd9Sstevel@tonic-gate 	 * routine is called, so we can't do this optimization.)
1117c478bd9Sstevel@tonic-gate 	 * The allocator relies on this behavior to prevent recursive
1127c478bd9Sstevel@tonic-gate 	 * mutex_enter in its (fixed-size) kstat update routine.
1137c478bd9Sstevel@tonic-gate 	 * It's a zalloc to prevent unintentional exposure of random
1147c478bd9Sstevel@tonic-gate 	 * juicy morsels of (old) kernel data.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	if (!(ksp->ks_flags & KSTAT_FLAG_VAR_SIZE)) {
1177c478bd9Sstevel@tonic-gate 		kbufsize = ksp->ks_data_size;
1187c478bd9Sstevel@tonic-gate 		kbuf = kmem_zalloc(kbufsize + 1, KM_NOSLEEP);
1197c478bd9Sstevel@tonic-gate 		if (kbuf == NULL) {
1207c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
1217c478bd9Sstevel@tonic-gate 			return (EAGAIN);
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	KSTAT_ENTER(ksp);
1257c478bd9Sstevel@tonic-gate 	if ((error = KSTAT_UPDATE(ksp, KSTAT_READ)) != 0) {
1267c478bd9Sstevel@tonic-gate 		KSTAT_EXIT(ksp);
1277c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
1287c478bd9Sstevel@tonic-gate 		if (kbuf != NULL)
1297c478bd9Sstevel@tonic-gate 			kmem_free(kbuf, kbufsize + 1);
1307c478bd9Sstevel@tonic-gate 		return (error);
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	kbufsize = ksp->ks_data_size;
1347c478bd9Sstevel@tonic-gate 	ubufsize = user_kstat.ks_data_size;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (ubufsize < kbufsize) {
1377c478bd9Sstevel@tonic-gate 		error = ENOMEM;
1387c478bd9Sstevel@tonic-gate 	} else {
1397c478bd9Sstevel@tonic-gate 		if (kbuf == NULL)
1407c478bd9Sstevel@tonic-gate 			kbuf = kmem_zalloc(kbufsize + 1, KM_NOSLEEP);
1417c478bd9Sstevel@tonic-gate 		if (kbuf == NULL) {
1427c478bd9Sstevel@tonic-gate 			error = EAGAIN;
1437c478bd9Sstevel@tonic-gate 		} else {
1447c478bd9Sstevel@tonic-gate 			error = KSTAT_SNAPSHOT(ksp, kbuf, KSTAT_READ);
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * The following info must be returned to user level,
1507c478bd9Sstevel@tonic-gate 	 * even if the the update or snapshot failed.  This allows
1517c478bd9Sstevel@tonic-gate 	 * kstat readers to get a handle on variable-size kstats,
1527c478bd9Sstevel@tonic-gate 	 * detect dormant kstats, etc.
1537c478bd9Sstevel@tonic-gate 	 */
1547c478bd9Sstevel@tonic-gate 	user_kstat.ks_ndata	= ksp->ks_ndata;
1557c478bd9Sstevel@tonic-gate 	user_kstat.ks_data_size	= kbufsize;
1567c478bd9Sstevel@tonic-gate 	user_kstat.ks_flags	= ksp->ks_flags;
1577c478bd9Sstevel@tonic-gate 	user_kstat.ks_snaptime	= ksp->ks_snaptime;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	*rvalp = kstat_chain_id;
1607c478bd9Sstevel@tonic-gate 	KSTAT_EXIT(ksp);
1617c478bd9Sstevel@tonic-gate 	kstat_rele(ksp);
1627c478bd9Sstevel@tonic-gate 
163*3c8ab7efSHans Rosenfeld 	if (kbuf == NULL)
164*3c8ab7efSHans Rosenfeld 		goto out;
165*3c8ab7efSHans Rosenfeld 
1667c478bd9Sstevel@tonic-gate 	/*
1677c478bd9Sstevel@tonic-gate 	 * Copy the buffer containing the kstat back to userland.
1687c478bd9Sstevel@tonic-gate 	 */
1697c478bd9Sstevel@tonic-gate 	copysize = kbufsize;
170*3c8ab7efSHans Rosenfeld 
171*3c8ab7efSHans Rosenfeld 	switch (model) {
172*3c8ab7efSHans Rosenfeld 	int i;
1737c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
1747c478bd9Sstevel@tonic-gate 	kstat32_t *k32;
1757c478bd9Sstevel@tonic-gate 	kstat_t *k;
1767c478bd9Sstevel@tonic-gate 
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 				/*
185*3c8ab7efSHans Rosenfeld 				 * Named statistics have fields of type 'long'.
186*3c8ab7efSHans Rosenfeld 				 * For a 32-bit application looking at a 64-bit
187*3c8ab7efSHans Rosenfeld 				 * kernel, forcibly truncate these 64-bit
1887c478bd9Sstevel@tonic-gate 				 * quantities to 32-bit values.
1897c478bd9Sstevel@tonic-gate 				 */
1907c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_LONG:
191*3c8ab7efSHans Rosenfeld 					kn->value.i32 = (int32_t)kn->value.l;
192*3c8ab7efSHans Rosenfeld 					kn->data_type = KSTAT_DATA_INT32;
1937c478bd9Sstevel@tonic-gate 					break;
1947c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_ULONG:
195*3c8ab7efSHans Rosenfeld 					kn->value.ui32 = (uint32_t)kn->value.ul;
196*3c8ab7efSHans Rosenfeld 					kn->data_type = KSTAT_DATA_UINT32;
1977c478bd9Sstevel@tonic-gate 					break;
1987c478bd9Sstevel@tonic-gate 				/*
199*3c8ab7efSHans Rosenfeld 				 * Long strings must be massaged before being
200*3c8ab7efSHans Rosenfeld 				 * copied out to userland.  Do that here.
2017c478bd9Sstevel@tonic-gate 				 */
2027c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_STRING:
203*3c8ab7efSHans Rosenfeld 					if (KSTAT_NAMED_STR_PTR(kn) == NULL)
2047c478bd9Sstevel@tonic-gate 						break;
2057c478bd9Sstevel@tonic-gate 					/*
206*3c8ab7efSHans Rosenfeld 					 * The offsets within the buffers are
207*3c8ab7efSHans Rosenfeld 					 * the same, so add the offset to the
208*3c8ab7efSHans Rosenfeld 					 * beginning of the new buffer to fix
209*3c8ab7efSHans Rosenfeld 					 * the pointer.
2107c478bd9Sstevel@tonic-gate 					 */
2117c478bd9Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(kn) =
2127c478bd9Sstevel@tonic-gate 					    (char *)user_kstat.ks_data +
2137c478bd9Sstevel@tonic-gate 					    (KSTAT_NAMED_STR_PTR(kn) -
2147c478bd9Sstevel@tonic-gate 					    (char *)kbuf);
2157c478bd9Sstevel@tonic-gate 					/*
216*3c8ab7efSHans Rosenfeld 					 * Make sure the string pointer lies
217*3c8ab7efSHans Rosenfeld 					 * within the allocated buffer.
2187c478bd9Sstevel@tonic-gate 					 */
2197c478bd9Sstevel@tonic-gate 					ASSERT(KSTAT_NAMED_STR_PTR(kn) +
220*3c8ab7efSHans Rosenfeld 					    KSTAT_NAMED_STR_BUFLEN(kn) <=
221*3c8ab7efSHans Rosenfeld 					    ((char *)user_kstat.ks_data +
2227c478bd9Sstevel@tonic-gate 					    ubufsize));
223*3c8ab7efSHans Rosenfeld 					ASSERT(KSTAT_NAMED_STR_PTR(kn) >=
224*3c8ab7efSHans Rosenfeld 					    (char *)((kstat_named_t *)
2257c478bd9Sstevel@tonic-gate 					    user_kstat.ks_data +
2267c478bd9Sstevel@tonic-gate 					    user_kstat.ks_ndata));
2277c478bd9Sstevel@tonic-gate 					/*
2287c478bd9Sstevel@tonic-gate 					 * Cast 64-bit ptr to 32-bit.
2297c478bd9Sstevel@tonic-gate 					 */
230a1b5e537Sbmc 					kn->value.str.addr.ptr32 =
2317c478bd9Sstevel@tonic-gate 					    (caddr32_t)(uintptr_t)
2327c478bd9Sstevel@tonic-gate 					    KSTAT_NAMED_STR_PTR(kn);
2337c478bd9Sstevel@tonic-gate 					break;
2347c478bd9Sstevel@tonic-gate 				default:
2357c478bd9Sstevel@tonic-gate 					break;
2367c478bd9Sstevel@tonic-gate 				}
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		if (user_kstat.ks_kid != 0)
2407c478bd9Sstevel@tonic-gate 			break;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		/*
2437c478bd9Sstevel@tonic-gate 		 * This is the special case of the kstat header
2447c478bd9Sstevel@tonic-gate 		 * list for the entire system.  Reshape the
2457c478bd9Sstevel@tonic-gate 		 * array in place, then copy it out.
2467c478bd9Sstevel@tonic-gate 		 */
2477c478bd9Sstevel@tonic-gate 		k32 = kbuf;
2487c478bd9Sstevel@tonic-gate 		k = kbuf;
2497c478bd9Sstevel@tonic-gate 		for (i = 0; i < user_kstat.ks_ndata; k32++, k++, i++) {
2507c478bd9Sstevel@tonic-gate 			k32->ks_crtime		= k->ks_crtime;
2517c478bd9Sstevel@tonic-gate 			k32->ks_next		= 0;
2527c478bd9Sstevel@tonic-gate 			k32->ks_kid		= k->ks_kid;
2537c478bd9Sstevel@tonic-gate 			(void) strcpy(k32->ks_module, k->ks_module);
2547c478bd9Sstevel@tonic-gate 			k32->ks_resv		= k->ks_resv;
2557c478bd9Sstevel@tonic-gate 			k32->ks_instance	= k->ks_instance;
2567c478bd9Sstevel@tonic-gate 			(void) strcpy(k32->ks_name, k->ks_name);
2577c478bd9Sstevel@tonic-gate 			k32->ks_type		= k->ks_type;
2587c478bd9Sstevel@tonic-gate 			(void) strcpy(k32->ks_class, k->ks_class);
2597c478bd9Sstevel@tonic-gate 			k32->ks_flags		= k->ks_flags;
2607c478bd9Sstevel@tonic-gate 			k32->ks_data		= 0;
2617c478bd9Sstevel@tonic-gate 			k32->ks_ndata		= k->ks_ndata;
2627c478bd9Sstevel@tonic-gate 			if (k->ks_data_size > UINT32_MAX) {
2637c478bd9Sstevel@tonic-gate 				error = EOVERFLOW;
2647c478bd9Sstevel@tonic-gate 				break;
2657c478bd9Sstevel@tonic-gate 			}
2667c478bd9Sstevel@tonic-gate 			k32->ks_data_size = (size32_t)k->ks_data_size;
2677c478bd9Sstevel@tonic-gate 			k32->ks_snaptime	= k->ks_snaptime;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * XXX	In this case we copy less data than is
2727c478bd9Sstevel@tonic-gate 		 *	claimed in the header.
2737c478bd9Sstevel@tonic-gate 		 */
2747c478bd9Sstevel@tonic-gate 		copysize = user_kstat.ks_ndata * sizeof (kstat32_t);
2757c478bd9Sstevel@tonic-gate 		break;
2767c478bd9Sstevel@tonic-gate #endif	/* _MULTI_DATAMODEL */
2777c478bd9Sstevel@tonic-gate 	default:
2787c478bd9Sstevel@tonic-gate 	case DDI_MODEL_NONE:
2797c478bd9Sstevel@tonic-gate 		if (ksp->ks_type == KSTAT_TYPE_NAMED) {
2807c478bd9Sstevel@tonic-gate 			kstat_named_t *kn = kbuf;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 			for (i = 0; i < user_kstat.ks_ndata; kn++, i++)
2837c478bd9Sstevel@tonic-gate 				switch (kn->data_type) {
2847c478bd9Sstevel@tonic-gate #ifdef _LP64
2857c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_LONG:
2867c478bd9Sstevel@tonic-gate 					kn->data_type =
2877c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_INT64;
2887c478bd9Sstevel@tonic-gate 					break;
2897c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_ULONG:
2907c478bd9Sstevel@tonic-gate 					kn->data_type =
2917c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_UINT64;
2927c478bd9Sstevel@tonic-gate 					break;
2937c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
2947c478bd9Sstevel@tonic-gate 				case KSTAT_DATA_STRING:
295*3c8ab7efSHans Rosenfeld 					if (KSTAT_NAMED_STR_PTR(kn) == NULL)
2967c478bd9Sstevel@tonic-gate 						break;
2977c478bd9Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(kn) =
2987c478bd9Sstevel@tonic-gate 					    (char *)user_kstat.ks_data +
2997c478bd9Sstevel@tonic-gate 					    (KSTAT_NAMED_STR_PTR(kn) -
3007c478bd9Sstevel@tonic-gate 					    (char *)kbuf);
3017c478bd9Sstevel@tonic-gate 					ASSERT(KSTAT_NAMED_STR_PTR(kn) +
302*3c8ab7efSHans Rosenfeld 					    KSTAT_NAMED_STR_BUFLEN(kn) <=
303*3c8ab7efSHans Rosenfeld 					    ((char *)user_kstat.ks_data +
3047c478bd9Sstevel@tonic-gate 					    ubufsize));
305*3c8ab7efSHans Rosenfeld 					ASSERT(KSTAT_NAMED_STR_PTR(kn) >=
306*3c8ab7efSHans Rosenfeld 					    (char *)((kstat_named_t *)
3077c478bd9Sstevel@tonic-gate 					    user_kstat.ks_data +
3087c478bd9Sstevel@tonic-gate 					    user_kstat.ks_ndata));
3097c478bd9Sstevel@tonic-gate 					break;
3107c478bd9Sstevel@tonic-gate 				default:
3117c478bd9Sstevel@tonic-gate 					break;
3127c478bd9Sstevel@tonic-gate 				}
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 		break;
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (error == 0 &&
3187c478bd9Sstevel@tonic-gate 	    copyout(kbuf, user_kstat.ks_data, copysize))
3197c478bd9Sstevel@tonic-gate 		error = EFAULT;
3207c478bd9Sstevel@tonic-gate 	kmem_free(kbuf, kbufsize + 1);
3217c478bd9Sstevel@tonic-gate 
322*3c8ab7efSHans Rosenfeld out:
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * We have modified the ks_ndata, ks_data_size, ks_flags, and
3257c478bd9Sstevel@tonic-gate 	 * ks_snaptime fields of the user kstat; now copy it back to userland.
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 	switch (model) {
3287c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
3297c478bd9Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
3307c478bd9Sstevel@tonic-gate 		if (kbufsize > UINT32_MAX) {
3317c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
3327c478bd9Sstevel@tonic-gate 			break;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 		user_kstat32.ks_ndata		= user_kstat.ks_ndata;
3357c478bd9Sstevel@tonic-gate 		user_kstat32.ks_data_size	= (size32_t)kbufsize;
3367c478bd9Sstevel@tonic-gate 		user_kstat32.ks_flags		= user_kstat.ks_flags;
3377c478bd9Sstevel@tonic-gate 		user_kstat32.ks_snaptime	= user_kstat.ks_snaptime;
3387c478bd9Sstevel@tonic-gate 		if (copyout(&user_kstat32, user_ksp, sizeof (kstat32_t)) &&
3397c478bd9Sstevel@tonic-gate 		    error == 0)
3407c478bd9Sstevel@tonic-gate 			error = EFAULT;
3417c478bd9Sstevel@tonic-gate 		break;
3427c478bd9Sstevel@tonic-gate #endif
3437c478bd9Sstevel@tonic-gate 	default:
3447c478bd9Sstevel@tonic-gate 	case DDI_MODEL_NONE:
3457c478bd9Sstevel@tonic-gate 		if (copyout(&user_kstat, user_ksp, sizeof (kstat_t)) &&
3467c478bd9Sstevel@tonic-gate 		    error == 0)
3477c478bd9Sstevel@tonic-gate 			error = EFAULT;
3487c478bd9Sstevel@tonic-gate 		break;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	return (error);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate static int
3557c478bd9Sstevel@tonic-gate write_kstat_data(int *rvalp, void *user_ksp, int flag, cred_t *cred)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	kstat_t user_kstat, *ksp;
3587c478bd9Sstevel@tonic-gate 	void *buf = NULL;
3597c478bd9Sstevel@tonic-gate 	size_t bufsize;
3607c478bd9Sstevel@tonic-gate 	int error = 0;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	if (secpolicy_sys_config(cred, B_FALSE) != 0)
3637c478bd9Sstevel@tonic-gate 		return (EPERM);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	switch (ddi_model_convert_from(flag & FMODELS)) {
3667c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
3677c478bd9Sstevel@tonic-gate 		kstat32_t user_kstat32;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	case DDI_MODEL_ILP32:
3707c478bd9Sstevel@tonic-gate 		if (copyin(user_ksp, &user_kstat32, sizeof (kstat32_t)))
3717c478bd9Sstevel@tonic-gate 			return (EFAULT);
3727c478bd9Sstevel@tonic-gate 		/*
3737c478bd9Sstevel@tonic-gate 		 * These are the only fields we actually look at.
3747c478bd9Sstevel@tonic-gate 		 */
3757c478bd9Sstevel@tonic-gate 		user_kstat.ks_kid = user_kstat32.ks_kid;
3767c478bd9Sstevel@tonic-gate 		user_kstat.ks_data = (void *)(uintptr_t)user_kstat32.ks_data;
3777c478bd9Sstevel@tonic-gate 		user_kstat.ks_data_size = (size_t)user_kstat32.ks_data_size;
3787c478bd9Sstevel@tonic-gate 		user_kstat.ks_ndata = user_kstat32.ks_ndata;
3797c478bd9Sstevel@tonic-gate 		break;
3807c478bd9Sstevel@tonic-gate #endif
3817c478bd9Sstevel@tonic-gate 	default:
3827c478bd9Sstevel@tonic-gate 	case DDI_MODEL_NONE:
3837c478bd9Sstevel@tonic-gate 		if (copyin(user_ksp, &user_kstat, sizeof (kstat_t)))
3847c478bd9Sstevel@tonic-gate 			return (EFAULT);
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	bufsize = user_kstat.ks_data_size;
3887c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(bufsize + 1, KM_NOSLEEP);
3897c478bd9Sstevel@tonic-gate 	if (buf == NULL)
3907c478bd9Sstevel@tonic-gate 		return (EAGAIN);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	if (copyin(user_kstat.ks_data, buf, bufsize)) {
3937c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
3947c478bd9Sstevel@tonic-gate 		return (EFAULT);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	ksp = kstat_hold_bykid(user_kstat.ks_kid, getzoneid());
3987c478bd9Sstevel@tonic-gate 	if (ksp == NULL) {
3997c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4007c478bd9Sstevel@tonic-gate 		return (ENXIO);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 	if (ksp->ks_flags & KSTAT_FLAG_INVALID) {
4037c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
4047c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4057c478bd9Sstevel@tonic-gate 		return (EAGAIN);
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	if (!(ksp->ks_flags & KSTAT_FLAG_WRITABLE)) {
4087c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
4097c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4107c478bd9Sstevel@tonic-gate 		return (EACCES);
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	/*
414*3c8ab7efSHans Rosenfeld 	 * With KSTAT_FLAG_VAR_SIZE, one must call the kstat's update callback
4157c478bd9Sstevel@tonic-gate 	 * routine to ensure ks_data_size is up to date.
4167c478bd9Sstevel@tonic-gate 	 * In this case it makes sense to do it anyhow, as it will be shortly
4177c478bd9Sstevel@tonic-gate 	 * followed by a KSTAT_SNAPSHOT().
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	KSTAT_ENTER(ksp);
4207c478bd9Sstevel@tonic-gate 	error = KSTAT_UPDATE(ksp, KSTAT_READ);
4217c478bd9Sstevel@tonic-gate 	if (error || user_kstat.ks_data_size != ksp->ks_data_size ||
4227c478bd9Sstevel@tonic-gate 	    user_kstat.ks_ndata != ksp->ks_ndata) {
4237c478bd9Sstevel@tonic-gate 		KSTAT_EXIT(ksp);
4247c478bd9Sstevel@tonic-gate 		kstat_rele(ksp);
4257c478bd9Sstevel@tonic-gate 		kmem_free(buf, bufsize + 1);
4267c478bd9Sstevel@tonic-gate 		return (error ? error : EINVAL);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * We have to ensure that we don't accidentally change the type of
4317c478bd9Sstevel@tonic-gate 	 * existing kstat_named statistics when writing over them.
4327c478bd9Sstevel@tonic-gate 	 * Since read_kstat_data() modifies some of the types on their way
4337c478bd9Sstevel@tonic-gate 	 * out, we need to be sure to handle these types seperately.
4347c478bd9Sstevel@tonic-gate 	 */
4357c478bd9Sstevel@tonic-gate 	if (ksp->ks_type == KSTAT_TYPE_NAMED) {
4367c478bd9Sstevel@tonic-gate 		void *kbuf;
4377c478bd9Sstevel@tonic-gate 		kstat_named_t *kold;
4387c478bd9Sstevel@tonic-gate 		kstat_named_t *knew = buf;
4397c478bd9Sstevel@tonic-gate 		int i;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
4427c478bd9Sstevel@tonic-gate 		int model = ddi_model_convert_from(flag & FMODELS);
4437c478bd9Sstevel@tonic-gate #endif
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		/*
4467c478bd9Sstevel@tonic-gate 		 * Since ksp->ks_data may be NULL, we need to take a snapshot
4477c478bd9Sstevel@tonic-gate 		 * of the published data to look at the types.
4487c478bd9Sstevel@tonic-gate 		 */
4497c478bd9Sstevel@tonic-gate 		kbuf = kmem_alloc(bufsize + 1, KM_NOSLEEP);
4507c478bd9Sstevel@tonic-gate 		if (kbuf == NULL) {
4517c478bd9Sstevel@tonic-gate 			KSTAT_EXIT(ksp);
4527c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
4537c478bd9Sstevel@tonic-gate 			kmem_free(buf, bufsize + 1);
4547c478bd9Sstevel@tonic-gate 			return (EAGAIN);
4557c478bd9Sstevel@tonic-gate 		}
4567c478bd9Sstevel@tonic-gate 		error = KSTAT_SNAPSHOT(ksp, kbuf, KSTAT_READ);
4577c478bd9Sstevel@tonic-gate 		if (error) {
4587c478bd9Sstevel@tonic-gate 			KSTAT_EXIT(ksp);
4597c478bd9Sstevel@tonic-gate 			kstat_rele(ksp);
4607c478bd9Sstevel@tonic-gate 			kmem_free(kbuf, bufsize + 1);
4617c478bd9Sstevel@tonic-gate 			kmem_free(buf, bufsize + 1);
4627c478bd9Sstevel@tonic-gate 			return (error);
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		kold = kbuf;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		/*
4677c478bd9Sstevel@tonic-gate 		 * read_kstat_data() changes the types of
4687c478bd9Sstevel@tonic-gate 		 * KSTAT_DATA_LONG / KSTAT_DATA_ULONG, so we need to
4697c478bd9Sstevel@tonic-gate 		 * make sure that these (modified) types are considered
4707c478bd9Sstevel@tonic-gate 		 * valid.
4717c478bd9Sstevel@tonic-gate 		 */
4727c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, kold++, knew++) {
4737c478bd9Sstevel@tonic-gate 			switch (kold->data_type) {
4747c478bd9Sstevel@tonic-gate #ifdef	_MULTI_DATAMODEL
4757c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_LONG:
4767c478bd9Sstevel@tonic-gate 				switch (model) {
4777c478bd9Sstevel@tonic-gate 				case DDI_MODEL_ILP32:
4787c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
4797c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_INT32) {
4807c478bd9Sstevel@tonic-gate 						knew->value.l =
4817c478bd9Sstevel@tonic-gate 						    (long)knew->value.i32;
4827c478bd9Sstevel@tonic-gate 						knew->data_type =
4837c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_LONG;
4847c478bd9Sstevel@tonic-gate 					}
4857c478bd9Sstevel@tonic-gate 					break;
4867c478bd9Sstevel@tonic-gate 				default:
4877c478bd9Sstevel@tonic-gate 				case DDI_MODEL_NONE:
4887c478bd9Sstevel@tonic-gate #ifdef _LP64
4897c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
4907c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_INT64) {
4917c478bd9Sstevel@tonic-gate 						knew->value.l =
4927c478bd9Sstevel@tonic-gate 						    (long)knew->value.i64;
4937c478bd9Sstevel@tonic-gate 						knew->data_type =
4947c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_LONG;
4957c478bd9Sstevel@tonic-gate 					}
4967c478bd9Sstevel@tonic-gate #endif /* _LP64 */
4977c478bd9Sstevel@tonic-gate 					break;
4987c478bd9Sstevel@tonic-gate 				}
4997c478bd9Sstevel@tonic-gate 				break;
5007c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_ULONG:
5017c478bd9Sstevel@tonic-gate 				switch (model) {
5027c478bd9Sstevel@tonic-gate 				case DDI_MODEL_ILP32:
5037c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
5047c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_UINT32) {
5057c478bd9Sstevel@tonic-gate 						knew->value.ul =
5067c478bd9Sstevel@tonic-gate 						    (ulong_t)knew->value.ui32;
5077c478bd9Sstevel@tonic-gate 						knew->data_type =
5087c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_ULONG;
5097c478bd9Sstevel@tonic-gate 					}
5107c478bd9Sstevel@tonic-gate 					break;
5117c478bd9Sstevel@tonic-gate 				default:
5127c478bd9Sstevel@tonic-gate 				case DDI_MODEL_NONE:
5137c478bd9Sstevel@tonic-gate #ifdef _LP64
5147c478bd9Sstevel@tonic-gate 					if (knew->data_type ==
5157c478bd9Sstevel@tonic-gate 					    KSTAT_DATA_UINT64) {
5167c478bd9Sstevel@tonic-gate 						knew->value.ul =
5177c478bd9Sstevel@tonic-gate 						    (ulong_t)knew->value.ui64;
5187c478bd9Sstevel@tonic-gate 						knew->data_type =
5197c478bd9Sstevel@tonic-gate 						    KSTAT_DATA_ULONG;
5207c478bd9Sstevel@tonic-gate 					}
5217c478bd9Sstevel@tonic-gate #endif /* _LP64 */
5227c478bd9Sstevel@tonic-gate 					break;
5237c478bd9Sstevel@tonic-gate 				}
5247c478bd9Sstevel@tonic-gate 				break;
5257c478bd9Sstevel@tonic-gate #endif /* _MULTI_DATAMODEL */
5267c478bd9Sstevel@tonic-gate 			case KSTAT_DATA_STRING:
5277c478bd9Sstevel@tonic-gate 				if (knew->data_type != KSTAT_DATA_STRING) {
5287c478bd9Sstevel@tonic-gate 					KSTAT_EXIT(ksp);
5297c478bd9Sstevel@tonic-gate 					kstat_rele(ksp);
5307c478bd9Sstevel@tonic-gate 					kmem_free(kbuf, bufsize + 1);
5317c478bd9Sstevel@tonic-gate 					kmem_free(buf, bufsize + 1);
5327c478bd9Sstevel@tonic-gate 					return (EINVAL);
5337c478bd9Sstevel@tonic-gate 				}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate #ifdef _MULTI_DATAMODEL
5367c478bd9Sstevel@tonic-gate 				if (model == DDI_MODEL_ILP32)
5377c478bd9Sstevel@tonic-gate 					KSTAT_NAMED_STR_PTR(knew) =
5387c478bd9Sstevel@tonic-gate 					    (char *)(uintptr_t)
539a1b5e537Sbmc 						knew->value.str.addr.ptr32;
5407c478bd9Sstevel@tonic-gate #endif
5417c478bd9Sstevel@tonic-gate 				/*
5427c478bd9Sstevel@tonic-gate 				 * Nothing special for NULL
5437c478bd9Sstevel@tonic-gate 				 */
5447c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knew) == NULL)
5457c478bd9Sstevel@tonic-gate 					break;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 				/*
5487c478bd9Sstevel@tonic-gate 				 * Check to see that the pointers all point
5497c478bd9Sstevel@tonic-gate 				 * to within the buffer and after the array
5507c478bd9Sstevel@tonic-gate 				 * of kstat_named_t's.
5517c478bd9Sstevel@tonic-gate 				 */
5527c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knew) <
5537c478bd9Sstevel@tonic-gate 				    (char *)
5547c478bd9Sstevel@tonic-gate 				    ((kstat_named_t *)user_kstat.ks_data +
5557c478bd9Sstevel@tonic-gate 				    ksp->ks_ndata)) {
5567c478bd9Sstevel@tonic-gate 					KSTAT_EXIT(ksp);
5577c478bd9Sstevel@tonic-gate 					kstat_rele(ksp);
5587c478bd9Sstevel@tonic-gate 					kmem_free(kbuf, bufsize + 1);
5597c478bd9Sstevel@tonic-gate 					kmem_free(buf, bufsize + 1);
5607c478bd9Sstevel@tonic-gate 					return (EINVAL);
5617c478bd9Sstevel@tonic-gate 				}
5627c478bd9Sstevel@tonic-gate 				if (KSTAT_NAMED_STR_PTR(knew) +
5637c478bd9Sstevel@tonic-gate 				    KSTAT_NAMED_STR_BUFLEN(knew) >
5647c478bd9Sstevel@tonic-gate 				    ((char *)user_kstat.ks_data +
5657c478bd9Sstevel@tonic-gate 				    ksp->ks_data_size)) {
5667c478bd9Sstevel@tonic-gate 					KSTAT_EXIT(ksp);
5677c478bd9Sstevel@tonic-gate 					kstat_rele(ksp);
5687c478bd9Sstevel@tonic-gate 					kmem_free(kbuf, bufsize + 1);
5697c478bd9Sstevel@tonic-gate 					kmem_free(buf, bufsize + 1);
5707c478bd9Sstevel@tonic-gate 					return (EINVAL);
5717c478bd9Sstevel@tonic-gate 				}
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 				/*
5747c478bd9Sstevel@tonic-gate 				 * Update the pointers within the buffer
5757c478bd9Sstevel@tonic-gate 				 */
5767c478bd9Sstevel@tonic-gate 				KSTAT_NAMED_STR_PTR(knew) =
5777c478bd9Sstevel@tonic-gate 				    (char *)buf +
5787c478bd9Sstevel@tonic-gate 				    (KSTAT_NAMED_STR_PTR(knew) -
5797c478bd9Sstevel@tonic-gate 				    (char *)user_kstat.ks_data);
5807c478bd9Sstevel@tonic-gate 				break;
5817c478bd9Sstevel@tonic-gate 			default:
5827c478bd9Sstevel@tonic-gate 				break;
5837c478bd9Sstevel@tonic-gate 			}
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 		kold = kbuf;
5877c478bd9Sstevel@tonic-gate 		knew = buf;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * Now make sure the types are what we expected them to be.
5917c478bd9Sstevel@tonic-gate 		 */
5927c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++, kold++, knew++)
5937c478bd9Sstevel@tonic-gate 			if (kold->data_type != knew->data_type) {
5947c478bd9Sstevel@tonic-gate 				KSTAT_EXIT(ksp);
5957c478bd9Sstevel@tonic-gate 				kstat_rele(ksp);
5967c478bd9Sstevel@tonic-gate 				kmem_free(kbuf, bufsize + 1);
5977c478bd9Sstevel@tonic-gate 				kmem_free(buf, bufsize + 1);
5987c478bd9Sstevel@tonic-gate 				return (EINVAL);
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		kmem_free(kbuf, bufsize + 1);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	error = KSTAT_SNAPSHOT(ksp, buf, KSTAT_WRITE);
6057c478bd9Sstevel@tonic-gate 	if (!error)
6067c478bd9Sstevel@tonic-gate 		error = KSTAT_UPDATE(ksp, KSTAT_WRITE);
6077c478bd9Sstevel@tonic-gate 	*rvalp = kstat_chain_id;
6087c478bd9Sstevel@tonic-gate 	KSTAT_EXIT(ksp);
6097c478bd9Sstevel@tonic-gate 	kstat_rele(ksp);
6107c478bd9Sstevel@tonic-gate 	kmem_free(buf, bufsize + 1);
6117c478bd9Sstevel@tonic-gate 	return (error);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6157c478bd9Sstevel@tonic-gate static int
6167c478bd9Sstevel@tonic-gate kstat_ioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cr, int *rvalp)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	int rc = 0;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	switch (cmd) {
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	case KSTAT_IOC_CHAIN_ID:
6237c478bd9Sstevel@tonic-gate 		*rvalp = kstat_chain_id;
6247c478bd9Sstevel@tonic-gate 		break;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	case KSTAT_IOC_READ:
6277c478bd9Sstevel@tonic-gate 		rc = read_kstat_data(rvalp, (void *)data, flag);
6287c478bd9Sstevel@tonic-gate 		break;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	case KSTAT_IOC_WRITE:
6317c478bd9Sstevel@tonic-gate 		rc = write_kstat_data(rvalp, (void *)data, flag, cr);
6327c478bd9Sstevel@tonic-gate 		break;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	default:
6357c478bd9Sstevel@tonic-gate 		/* invalid request */
6367c478bd9Sstevel@tonic-gate 		rc = EINVAL;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	return (rc);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate /* ARGSUSED */
6427c478bd9Sstevel@tonic-gate static int
6437c478bd9Sstevel@tonic-gate kstat_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
6447c478bd9Sstevel@tonic-gate 	void **result)
6457c478bd9Sstevel@tonic-gate {
6467c478bd9Sstevel@tonic-gate 	switch (infocmd) {
6477c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
6487c478bd9Sstevel@tonic-gate 		*result = kstat_devi;
6497c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6507c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
6517c478bd9Sstevel@tonic-gate 		*result = NULL;
6527c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	return (DDI_FAILURE);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static int
6587c478bd9Sstevel@tonic-gate kstat_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
6617c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if (ddi_create_minor_node(devi, "kstat", S_IFCHR,
6647c478bd9Sstevel@tonic-gate 	    0, DDI_PSEUDO, NULL) == DDI_FAILURE) {
6657c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
6667c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate 	kstat_devi = devi;
6697c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate static int
6737c478bd9Sstevel@tonic-gate kstat_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
6767c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
6797c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate static struct cb_ops kstat_cb_ops = {
6837c478bd9Sstevel@tonic-gate 	nulldev,		/* open */
6847c478bd9Sstevel@tonic-gate 	nulldev,		/* close */
6857c478bd9Sstevel@tonic-gate 	nodev,			/* strategy */
6867c478bd9Sstevel@tonic-gate 	nodev,			/* print */
6877c478bd9Sstevel@tonic-gate 	nodev,			/* dump */
6887c478bd9Sstevel@tonic-gate 	nodev,			/* read */
6897c478bd9Sstevel@tonic-gate 	nodev,			/* write */
6907c478bd9Sstevel@tonic-gate 	kstat_ioctl,		/* ioctl */
6917c478bd9Sstevel@tonic-gate 	nodev,			/* devmap */
6927c478bd9Sstevel@tonic-gate 	nodev,			/* mmap */
6937c478bd9Sstevel@tonic-gate 	nodev,			/* segmap */
6947c478bd9Sstevel@tonic-gate 	nochpoll,		/* poll */
6957c478bd9Sstevel@tonic-gate 	ddi_prop_op,		/* prop_op */
6967c478bd9Sstevel@tonic-gate 	0,			/* streamtab  */
6977c478bd9Sstevel@tonic-gate 	D_NEW | D_MP		/* Driver compatibility flag */
6987c478bd9Sstevel@tonic-gate };
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate static struct dev_ops kstat_ops = {
7017c478bd9Sstevel@tonic-gate 	DEVO_REV,		/* devo_rev, */
7027c478bd9Sstevel@tonic-gate 	0,			/* refcnt  */
7037c478bd9Sstevel@tonic-gate 	kstat_info,		/* get_dev_info */
7047c478bd9Sstevel@tonic-gate 	nulldev,		/* identify */
7057c478bd9Sstevel@tonic-gate 	nulldev,		/* probe */
7067c478bd9Sstevel@tonic-gate 	kstat_attach,		/* attach */
7077c478bd9Sstevel@tonic-gate 	kstat_detach,		/* detach */
7087c478bd9Sstevel@tonic-gate 	nodev,			/* reset */
7097c478bd9Sstevel@tonic-gate 	&kstat_cb_ops,		/* driver operations */
71019397407SSherry Moore 	(struct bus_ops *)0,	/* no bus operations */
71119397407SSherry Moore 	NULL,			/* power */
71219397407SSherry Moore 	ddi_quiesce_not_needed,		/* quiesce */
7137c478bd9Sstevel@tonic-gate };
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
71619397407SSherry Moore 	&mod_driverops, "kernel statistics driver", &kstat_ops,
7177c478bd9Sstevel@tonic-gate };
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
7207c478bd9Sstevel@tonic-gate 	MODREV_1, &modldrv, NULL
7217c478bd9Sstevel@tonic-gate };
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate int
7247c478bd9Sstevel@tonic-gate _init(void)
7257c478bd9Sstevel@tonic-gate {
7267c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate int
7307c478bd9Sstevel@tonic-gate _fini(void)
7317c478bd9Sstevel@tonic-gate {
7327c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
7337c478bd9Sstevel@tonic-gate }
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate int
7367c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
7377c478bd9Sstevel@tonic-gate {
7387c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
7397c478bd9Sstevel@tonic-gate }
740