xref: /onnv-gate/usr/src/cmd/fs.d/nfs/nfsstat/nfsstat.c (revision 10265:b988146c84e5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53406Sth199096  * Common Development and Distribution License (the "License").
63406Sth199096  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213406Sth199096 
220Sstevel@tonic-gate /* LINTLIBRARY */
230Sstevel@tonic-gate /* PROTOLIB1 */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*10265SKrishnendu.Sadhukhan@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * nfsstat: Network File System statistics
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <unistd.h>
380Sstevel@tonic-gate #include <stdarg.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <fcntl.h>
420Sstevel@tonic-gate #include <kvm.h>
430Sstevel@tonic-gate #include <kstat.h>
440Sstevel@tonic-gate #include <sys/param.h>
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <sys/t_lock.h>
470Sstevel@tonic-gate #include <sys/tiuser.h>
480Sstevel@tonic-gate #include <sys/statvfs.h>
490Sstevel@tonic-gate #include <sys/mntent.h>
500Sstevel@tonic-gate #include <sys/mnttab.h>
510Sstevel@tonic-gate #include <sys/sysmacros.h>
520Sstevel@tonic-gate #include <sys/mkdev.h>
530Sstevel@tonic-gate #include <rpc/types.h>
540Sstevel@tonic-gate #include <rpc/xdr.h>
550Sstevel@tonic-gate #include <rpc/auth.h>
560Sstevel@tonic-gate #include <rpc/clnt.h>
570Sstevel@tonic-gate #include <nfs/nfs.h>
580Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
590Sstevel@tonic-gate #include <nfs/nfs_sec.h>
600Sstevel@tonic-gate #include <inttypes.h>
610Sstevel@tonic-gate #include <signal.h>
620Sstevel@tonic-gate #include <time.h>
630Sstevel@tonic-gate #include <sys/time.h>
640Sstevel@tonic-gate #include <strings.h>
650Sstevel@tonic-gate #include <ctype.h>
66*10265SKrishnendu.Sadhukhan@Sun.COM #include <locale.h>
670Sstevel@tonic-gate 
68*10265SKrishnendu.Sadhukhan@Sun.COM #include "statcommon.h"
690Sstevel@tonic-gate 
700Sstevel@tonic-gate static kstat_ctl_t *kc = NULL;		/* libkstat cookie */
710Sstevel@tonic-gate static kstat_t *rpc_clts_client_kstat, *rpc_clts_server_kstat;
720Sstevel@tonic-gate static kstat_t *rpc_cots_client_kstat, *rpc_cots_server_kstat;
730Sstevel@tonic-gate static kstat_t *rpc_rdma_client_kstat, *rpc_rdma_server_kstat;
740Sstevel@tonic-gate static kstat_t *nfs_client_kstat, *nfs_server_v2_kstat, *nfs_server_v3_kstat;
750Sstevel@tonic-gate static kstat_t *nfs4_client_kstat, *nfs_server_v4_kstat;
760Sstevel@tonic-gate static kstat_t *rfsproccnt_v2_kstat, *rfsproccnt_v3_kstat, *rfsproccnt_v4_kstat;
770Sstevel@tonic-gate static kstat_t *rfsreqcnt_v2_kstat, *rfsreqcnt_v3_kstat, *rfsreqcnt_v4_kstat;
780Sstevel@tonic-gate static kstat_t *aclproccnt_v2_kstat, *aclproccnt_v3_kstat;
790Sstevel@tonic-gate static kstat_t *aclreqcnt_v2_kstat, *aclreqcnt_v3_kstat;
800Sstevel@tonic-gate static kstat_t *ksum_kstat;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static void handle_sig(int);
830Sstevel@tonic-gate static int getstats_rpc(void);
840Sstevel@tonic-gate static int getstats_nfs(void);
850Sstevel@tonic-gate static int getstats_rfsproc(int);
860Sstevel@tonic-gate static int getstats_rfsreq(int);
870Sstevel@tonic-gate static int getstats_aclproc(void);
880Sstevel@tonic-gate static int getstats_aclreq(void);
890Sstevel@tonic-gate static void putstats(void);
900Sstevel@tonic-gate static void setup(void);
910Sstevel@tonic-gate static void cr_print(int);
920Sstevel@tonic-gate static void sr_print(int);
930Sstevel@tonic-gate static void cn_print(int, int);
940Sstevel@tonic-gate static void sn_print(int, int);
950Sstevel@tonic-gate static void ca_print(int, int);
960Sstevel@tonic-gate static void sa_print(int, int);
970Sstevel@tonic-gate static void req_print(kstat_t *, kstat_t *, int, int, int);
980Sstevel@tonic-gate static void req_print_v4(kstat_t *, kstat_t *, int, int);
990Sstevel@tonic-gate static void stat_print(const char *, kstat_t *, kstat_t *, int, int);
100*10265SKrishnendu.Sadhukhan@Sun.COM static void nfsstat_kstat_sum(kstat_t *, kstat_t *, kstat_t *);
1010Sstevel@tonic-gate static void stats_timer(int);
1020Sstevel@tonic-gate static void safe_zalloc(void **, uint_t, int);
1030Sstevel@tonic-gate static int safe_strtoi(char const *, char *);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 
106*10265SKrishnendu.Sadhukhan@Sun.COM static void nfsstat_kstat_copy(kstat_t *, kstat_t *, int);
1070Sstevel@tonic-gate static kid_t safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
1080Sstevel@tonic-gate static kid_t safe_kstat_write(kstat_ctl_t *, kstat_t *, void *);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate static void usage(void);
1110Sstevel@tonic-gate static void mi_print(void);
1120Sstevel@tonic-gate static int ignore(char *);
1130Sstevel@tonic-gate static int interval;		/* interval between stats */
1140Sstevel@tonic-gate static int count;		/* number of iterations the stat is printed */
1150Sstevel@tonic-gate #define	MAX_COLUMNS	80
1160Sstevel@tonic-gate #define	MAX_PATHS	50	/* max paths that can be taken by -m */
1170Sstevel@tonic-gate 
1185302Sth199096 /*
1195302Sth199096  * MI4_MIRRORMOUNT is canonically defined in nfs4_clnt.h, but we cannot
1205302Sth199096  * include that file here.
1215302Sth199096  */
1225302Sth199096 #define	MI4_MIRRORMOUNT 0x4000
1235302Sth199096 #define	NFS_V4		4
1245302Sth199096 
1250Sstevel@tonic-gate static int req_width(kstat_t *, int);
1260Sstevel@tonic-gate static int stat_width(kstat_t *, int);
1270Sstevel@tonic-gate static char *path [MAX_PATHS] = {NULL};  /* array to store the multiple paths */
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * Struct holds the previous kstat values so
1310Sstevel@tonic-gate  * we can compute deltas when using the -i flag
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate typedef struct old_kstat
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	kstat_t kst;
1360Sstevel@tonic-gate 	int tot;
1370Sstevel@tonic-gate } old_kstat_t;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static old_kstat_t old_rpc_clts_client_kstat, old_rpc_clts_server_kstat;
1400Sstevel@tonic-gate static old_kstat_t old_rpc_cots_client_kstat, old_rpc_cots_server_kstat;
1410Sstevel@tonic-gate static old_kstat_t old_rpc_rdma_client_kstat, old_rpc_rdma_server_kstat;
1420Sstevel@tonic-gate static old_kstat_t old_nfs_client_kstat, old_nfs_server_v2_kstat;
1430Sstevel@tonic-gate static old_kstat_t old_nfs_server_v3_kstat, old_ksum_kstat;
1440Sstevel@tonic-gate static old_kstat_t old_nfs4_client_kstat, old_nfs_server_v4_kstat;
1450Sstevel@tonic-gate static old_kstat_t old_rfsproccnt_v2_kstat, old_rfsproccnt_v3_kstat;
1460Sstevel@tonic-gate static old_kstat_t old_rfsproccnt_v4_kstat, old_rfsreqcnt_v2_kstat;
1470Sstevel@tonic-gate static old_kstat_t old_rfsreqcnt_v3_kstat, old_rfsreqcnt_v4_kstat;
1480Sstevel@tonic-gate static old_kstat_t old_aclproccnt_v2_kstat, old_aclproccnt_v3_kstat;
1490Sstevel@tonic-gate static old_kstat_t old_aclreqcnt_v2_kstat, old_aclreqcnt_v3_kstat;
1500Sstevel@tonic-gate 
151*10265SKrishnendu.Sadhukhan@Sun.COM static uint_t timestamp_fmt = NODATE;
1520Sstevel@tonic-gate 
153*10265SKrishnendu.Sadhukhan@Sun.COM #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
154*10265SKrishnendu.Sadhukhan@Sun.COM #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
155*10265SKrishnendu.Sadhukhan@Sun.COM #endif
1560Sstevel@tonic-gate 
157249Sjwahlig int
1580Sstevel@tonic-gate main(int argc, char *argv[])
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int c, go_forever, j;
1610Sstevel@tonic-gate 	int cflag = 0;		/* client stats */
1620Sstevel@tonic-gate 	int sflag = 0;		/* server stats */
1630Sstevel@tonic-gate 	int nflag = 0;		/* nfs stats */
1640Sstevel@tonic-gate 	int rflag = 0;		/* rpc stats */
1650Sstevel@tonic-gate 	int mflag = 0;		/* mount table stats */
1660Sstevel@tonic-gate 	int aflag = 0;		/* print acl statistics */
1670Sstevel@tonic-gate 	int vflag = 0;		/* version specified, 0 specifies all */
1680Sstevel@tonic-gate 	int zflag = 0;		/* zero stats after printing */
1690Sstevel@tonic-gate 	char *split_line = "*******************************************"
1705302Sth199096 	    "*************************************";
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	interval = 0;
1730Sstevel@tonic-gate 	count = 0;
1740Sstevel@tonic-gate 	go_forever = 0;
1750Sstevel@tonic-gate 
176*10265SKrishnendu.Sadhukhan@Sun.COM 	(void) setlocale(LC_ALL, "");
177*10265SKrishnendu.Sadhukhan@Sun.COM 	(void) textdomain(TEXT_DOMAIN);
178*10265SKrishnendu.Sadhukhan@Sun.COM 
179*10265SKrishnendu.Sadhukhan@Sun.COM 	while ((c = getopt(argc, argv, "cnrsmzav:T:")) != EOF) {
1800Sstevel@tonic-gate 		switch (c) {
1810Sstevel@tonic-gate 		case 'c':
1820Sstevel@tonic-gate 			cflag++;
1830Sstevel@tonic-gate 			break;
1840Sstevel@tonic-gate 		case 'n':
1850Sstevel@tonic-gate 			nflag++;
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 		case 'r':
1880Sstevel@tonic-gate 			rflag++;
1890Sstevel@tonic-gate 			break;
1900Sstevel@tonic-gate 		case 's':
1910Sstevel@tonic-gate 			sflag++;
1920Sstevel@tonic-gate 			break;
1930Sstevel@tonic-gate 		case 'm':
1940Sstevel@tonic-gate 			mflag++;
1950Sstevel@tonic-gate 			break;
1960Sstevel@tonic-gate 		case 'z':
1970Sstevel@tonic-gate 			if (geteuid())
1980Sstevel@tonic-gate 				fail(0, "Must be root for z flag\n");
1990Sstevel@tonic-gate 			zflag++;
2000Sstevel@tonic-gate 			break;
2010Sstevel@tonic-gate 		case 'a':
2020Sstevel@tonic-gate 			aflag++;
2030Sstevel@tonic-gate 			break;
2040Sstevel@tonic-gate 		case 'v':
2050Sstevel@tonic-gate 			vflag = atoi(optarg);
2060Sstevel@tonic-gate 			if ((vflag < 2) || (vflag > 4))
2070Sstevel@tonic-gate 				fail(0, "Invalid version number\n");
2080Sstevel@tonic-gate 			break;
209*10265SKrishnendu.Sadhukhan@Sun.COM 		case 'T':
210*10265SKrishnendu.Sadhukhan@Sun.COM 			if (optarg) {
211*10265SKrishnendu.Sadhukhan@Sun.COM 				if (*optarg == 'u')
212*10265SKrishnendu.Sadhukhan@Sun.COM 					timestamp_fmt = UDATE;
213*10265SKrishnendu.Sadhukhan@Sun.COM 				else if (*optarg == 'd')
214*10265SKrishnendu.Sadhukhan@Sun.COM 					timestamp_fmt = DDATE;
215*10265SKrishnendu.Sadhukhan@Sun.COM 				else
216*10265SKrishnendu.Sadhukhan@Sun.COM 					usage();
217*10265SKrishnendu.Sadhukhan@Sun.COM 			} else {
218*10265SKrishnendu.Sadhukhan@Sun.COM 				usage();
219*10265SKrishnendu.Sadhukhan@Sun.COM 			}
220*10265SKrishnendu.Sadhukhan@Sun.COM 			break;
2210Sstevel@tonic-gate 		case '?':
2220Sstevel@tonic-gate 		default:
2230Sstevel@tonic-gate 			usage();
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	if (((argc - optind) > 0) && !mflag) {
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
2300Sstevel@tonic-gate 		if (interval < 1)
2310Sstevel@tonic-gate 			fail(0, "invalid interval\n");
2320Sstevel@tonic-gate 		optind++;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		if ((argc - optind) > 0) {
2350Sstevel@tonic-gate 			count = safe_strtoi(argv[optind], "invalid count");
2360Sstevel@tonic-gate 			if ((count <= 0) || (count == NULL))
2370Sstevel@tonic-gate 				fail(0, "invalid count\n");
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 		optind++;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 		if ((argc - optind) > 0)
2420Sstevel@tonic-gate 			usage();
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		/*
2450Sstevel@tonic-gate 		 * no count number was set, so we will loop infinitely
2460Sstevel@tonic-gate 		 * at interval specified
2470Sstevel@tonic-gate 		 */
2480Sstevel@tonic-gate 		if (!count)
2490Sstevel@tonic-gate 			go_forever = 1;
2500Sstevel@tonic-gate 		stats_timer(interval);
2510Sstevel@tonic-gate 	} else if (mflag) {
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 		if (cflag || rflag || sflag || zflag || nflag || aflag || vflag)
2545302Sth199096 			fail(0,
2555302Sth199096 			    "The -m flag may not be used with any other flags");
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		for (j = 0; (argc - optind > 0) && (j < (MAX_PATHS - 1)); j++) {
2580Sstevel@tonic-gate 			path[j] =  argv[optind];
2590Sstevel@tonic-gate 			if (*path[j] != '/')
2600Sstevel@tonic-gate 				fail(0, "Please fully qualify your pathname "
2610Sstevel@tonic-gate 				    "with a leading '/'");
2620Sstevel@tonic-gate 			optind++;
2630Sstevel@tonic-gate 		}
2640Sstevel@tonic-gate 		path[j] = NULL;
2650Sstevel@tonic-gate 		if (argc - optind > 0)
2660Sstevel@tonic-gate 			fprintf(stderr, "Only the first 50 paths "
2675302Sth199096 			    "will be searched for\n");
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	setup();
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	do {
2730Sstevel@tonic-gate 		if (mflag) {
2740Sstevel@tonic-gate 			mi_print();
2750Sstevel@tonic-gate 		} else {
276*10265SKrishnendu.Sadhukhan@Sun.COM 			if (timestamp_fmt != NODATE)
277*10265SKrishnendu.Sadhukhan@Sun.COM 				print_timestamp(timestamp_fmt);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 			if (sflag &&
2800Sstevel@tonic-gate 			    (rpc_clts_server_kstat == NULL ||
2810Sstevel@tonic-gate 			    nfs_server_v4_kstat == NULL)) {
2820Sstevel@tonic-gate 				fprintf(stderr,
2830Sstevel@tonic-gate 				    "nfsstat: kernel is not configured with "
2840Sstevel@tonic-gate 				    "the server nfs and rpc code.\n");
2850Sstevel@tonic-gate 			}
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 			/* if s and nothing else, all 3 prints are called */
2880Sstevel@tonic-gate 			if (sflag || (!sflag && !cflag)) {
2890Sstevel@tonic-gate 				if (rflag || (!rflag && !nflag && !aflag))
2900Sstevel@tonic-gate 					sr_print(zflag);
2910Sstevel@tonic-gate 				if (nflag || (!rflag && !nflag && !aflag))
2920Sstevel@tonic-gate 					sn_print(zflag, vflag);
2930Sstevel@tonic-gate 				if (aflag || (!rflag && !nflag && !aflag))
2940Sstevel@tonic-gate 					sa_print(zflag, vflag);
2950Sstevel@tonic-gate 			}
2960Sstevel@tonic-gate 			if (cflag &&
2970Sstevel@tonic-gate 			    (rpc_clts_client_kstat == NULL ||
2980Sstevel@tonic-gate 			    nfs_client_kstat == NULL)) {
2990Sstevel@tonic-gate 				fprintf(stderr,
3005302Sth199096 				    "nfsstat: kernel is not configured with"
3015302Sth199096 				    " the client nfs and rpc code.\n");
3020Sstevel@tonic-gate 			}
3030Sstevel@tonic-gate 			if (cflag || (!sflag && !cflag)) {
3040Sstevel@tonic-gate 				if (rflag || (!rflag && !nflag && !aflag))
3050Sstevel@tonic-gate 					cr_print(zflag);
3060Sstevel@tonic-gate 				if (nflag || (!rflag && !nflag && !aflag))
3070Sstevel@tonic-gate 					cn_print(zflag, vflag);
3080Sstevel@tonic-gate 				if (aflag || (!rflag && !nflag && !aflag))
3090Sstevel@tonic-gate 					ca_print(zflag, vflag);
3100Sstevel@tonic-gate 			}
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 		if (zflag)
3140Sstevel@tonic-gate 			putstats();
3150Sstevel@tonic-gate 		if (interval)
3160Sstevel@tonic-gate 			printf("%s\n", split_line);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		if (interval > 0)
3190Sstevel@tonic-gate 			(void) pause();
3200Sstevel@tonic-gate 	} while ((--count > 0) || go_forever);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	kstat_close(kc);
3230Sstevel@tonic-gate 	free(ksum_kstat);
3240Sstevel@tonic-gate 	return (0);
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate static int
3290Sstevel@tonic-gate getstats_rpc(void)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate 	int field_width = 0;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	if (rpc_clts_client_kstat != NULL) {
3340Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_clts_client_kstat, NULL);
3350Sstevel@tonic-gate 		field_width = stat_width(rpc_clts_client_kstat, field_width);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	if (rpc_cots_client_kstat != NULL) {
3390Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_cots_client_kstat, NULL);
3400Sstevel@tonic-gate 		field_width = stat_width(rpc_cots_client_kstat, field_width);
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (rpc_rdma_client_kstat != NULL) {
3440Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_rdma_client_kstat, NULL);
3450Sstevel@tonic-gate 		field_width = stat_width(rpc_rdma_client_kstat, field_width);
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (rpc_clts_server_kstat != NULL) {
3490Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_clts_server_kstat, NULL);
3500Sstevel@tonic-gate 		field_width =  stat_width(rpc_clts_server_kstat, field_width);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	if (rpc_cots_server_kstat != NULL) {
3530Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_cots_server_kstat, NULL);
3540Sstevel@tonic-gate 		field_width = stat_width(rpc_cots_server_kstat, field_width);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 	if (rpc_rdma_server_kstat != NULL) {
3570Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_rdma_server_kstat, NULL);
3580Sstevel@tonic-gate 		field_width = stat_width(rpc_rdma_server_kstat, field_width);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	return (field_width);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate static int
3640Sstevel@tonic-gate getstats_nfs(void)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	int field_width = 0;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (nfs_client_kstat != NULL) {
3690Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_client_kstat, NULL);
3700Sstevel@tonic-gate 		field_width = stat_width(nfs_client_kstat, field_width);
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate 	if (nfs4_client_kstat != NULL) {
3730Sstevel@tonic-gate 		safe_kstat_read(kc, nfs4_client_kstat, NULL);
3740Sstevel@tonic-gate 		field_width = stat_width(nfs4_client_kstat, field_width);
3750Sstevel@tonic-gate 	}
3760Sstevel@tonic-gate 	if (nfs_server_v2_kstat != NULL) {
3770Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_server_v2_kstat, NULL);
3780Sstevel@tonic-gate 		field_width = stat_width(nfs_server_v2_kstat, field_width);
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 	if (nfs_server_v3_kstat != NULL) {
3810Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_server_v3_kstat, NULL);
3820Sstevel@tonic-gate 		field_width = stat_width(nfs_server_v3_kstat, field_width);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	if (nfs_server_v4_kstat != NULL) {
3850Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_server_v4_kstat, NULL);
3860Sstevel@tonic-gate 		field_width = stat_width(nfs_server_v4_kstat, field_width);
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 	return (field_width);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate static int
3920Sstevel@tonic-gate getstats_rfsproc(int ver)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	int field_width = 0;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if ((ver == 2) && (rfsproccnt_v2_kstat != NULL)) {
3970Sstevel@tonic-gate 		safe_kstat_read(kc, rfsproccnt_v2_kstat, NULL);
3980Sstevel@tonic-gate 		field_width = req_width(rfsproccnt_v2_kstat, field_width);
3990Sstevel@tonic-gate 	}
4000Sstevel@tonic-gate 	if ((ver == 3) && (rfsproccnt_v3_kstat != NULL)) {
4010Sstevel@tonic-gate 		safe_kstat_read(kc, rfsproccnt_v3_kstat, NULL);
4020Sstevel@tonic-gate 		field_width = req_width(rfsproccnt_v3_kstat, field_width);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 	if ((ver == 4) && (rfsproccnt_v4_kstat != NULL)) {
4050Sstevel@tonic-gate 		safe_kstat_read(kc, rfsproccnt_v4_kstat, NULL);
4060Sstevel@tonic-gate 		field_width = req_width(rfsproccnt_v4_kstat, field_width);
4070Sstevel@tonic-gate 	}
4080Sstevel@tonic-gate 	return (field_width);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate static int
4120Sstevel@tonic-gate getstats_rfsreq(int ver)
4130Sstevel@tonic-gate {
4140Sstevel@tonic-gate 	int field_width = 0;
4150Sstevel@tonic-gate 	if ((ver == 2) && (rfsreqcnt_v2_kstat != NULL)) {
4160Sstevel@tonic-gate 		safe_kstat_read(kc, rfsreqcnt_v2_kstat, NULL);
4170Sstevel@tonic-gate 		field_width = req_width(rfsreqcnt_v2_kstat, field_width);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 	if ((ver == 3) && (rfsreqcnt_v3_kstat != NULL)) {
4200Sstevel@tonic-gate 		safe_kstat_read(kc, rfsreqcnt_v3_kstat, NULL);
4210Sstevel@tonic-gate 		field_width = req_width(rfsreqcnt_v3_kstat,  field_width);
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 	if ((ver == 4) && (rfsreqcnt_v4_kstat != NULL)) {
4240Sstevel@tonic-gate 		safe_kstat_read(kc, rfsreqcnt_v4_kstat, NULL);
4250Sstevel@tonic-gate 		field_width = req_width(rfsreqcnt_v4_kstat, field_width);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	return (field_width);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate static int
4310Sstevel@tonic-gate getstats_aclproc(void)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate 	int field_width = 0;
4340Sstevel@tonic-gate 	if (aclproccnt_v2_kstat != NULL) {
4350Sstevel@tonic-gate 		safe_kstat_read(kc, aclproccnt_v2_kstat, NULL);
4360Sstevel@tonic-gate 		field_width = req_width(aclproccnt_v2_kstat, field_width);
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	if (aclproccnt_v3_kstat != NULL) {
4390Sstevel@tonic-gate 		safe_kstat_read(kc, aclproccnt_v3_kstat, NULL);
4400Sstevel@tonic-gate 		field_width = req_width(aclproccnt_v3_kstat, field_width);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 	return (field_width);
4430Sstevel@tonic-gate }
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate static int
4460Sstevel@tonic-gate getstats_aclreq(void)
4470Sstevel@tonic-gate {
4480Sstevel@tonic-gate 	int field_width = 0;
4490Sstevel@tonic-gate 	if (aclreqcnt_v2_kstat != NULL) {
4500Sstevel@tonic-gate 		safe_kstat_read(kc, aclreqcnt_v2_kstat, NULL);
4510Sstevel@tonic-gate 		field_width = req_width(aclreqcnt_v2_kstat, field_width);
4520Sstevel@tonic-gate 	}
4530Sstevel@tonic-gate 	if (aclreqcnt_v3_kstat != NULL) {
4540Sstevel@tonic-gate 		safe_kstat_read(kc, aclreqcnt_v3_kstat, NULL);
4550Sstevel@tonic-gate 		field_width = req_width(aclreqcnt_v3_kstat, field_width);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	return (field_width);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate static void
4610Sstevel@tonic-gate putstats(void)
4620Sstevel@tonic-gate {
4630Sstevel@tonic-gate 	if (rpc_clts_client_kstat != NULL)
4640Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_clts_client_kstat, NULL);
4650Sstevel@tonic-gate 	if (rpc_cots_client_kstat != NULL)
4660Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_cots_client_kstat, NULL);
4670Sstevel@tonic-gate 	if (rpc_rdma_client_kstat != NULL)
4680Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_rdma_client_kstat, NULL);
4690Sstevel@tonic-gate 	if (nfs_client_kstat != NULL)
4700Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_client_kstat, NULL);
4710Sstevel@tonic-gate 	if (nfs4_client_kstat != NULL)
4720Sstevel@tonic-gate 		safe_kstat_write(kc, nfs4_client_kstat, NULL);
4730Sstevel@tonic-gate 	if (rpc_clts_server_kstat != NULL)
4740Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_clts_server_kstat, NULL);
4750Sstevel@tonic-gate 	if (rpc_cots_server_kstat != NULL)
4760Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_cots_server_kstat, NULL);
4770Sstevel@tonic-gate 	if (rpc_rdma_server_kstat != NULL)
4780Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_rdma_server_kstat, NULL);
4790Sstevel@tonic-gate 	if (nfs_server_v2_kstat != NULL)
4800Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_server_v2_kstat, NULL);
4810Sstevel@tonic-gate 	if (nfs_server_v3_kstat != NULL)
4820Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_server_v3_kstat, NULL);
4830Sstevel@tonic-gate 	if (nfs_server_v4_kstat != NULL)
4840Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_server_v4_kstat, NULL);
4850Sstevel@tonic-gate 	if (rfsproccnt_v2_kstat != NULL)
4860Sstevel@tonic-gate 		safe_kstat_write(kc, rfsproccnt_v2_kstat, NULL);
4870Sstevel@tonic-gate 	if (rfsproccnt_v3_kstat != NULL)
4880Sstevel@tonic-gate 		safe_kstat_write(kc, rfsproccnt_v3_kstat, NULL);
4890Sstevel@tonic-gate 	if (rfsproccnt_v4_kstat != NULL)
4900Sstevel@tonic-gate 		safe_kstat_write(kc, rfsproccnt_v4_kstat, NULL);
4910Sstevel@tonic-gate 	if (rfsreqcnt_v2_kstat != NULL)
4920Sstevel@tonic-gate 		safe_kstat_write(kc, rfsreqcnt_v2_kstat, NULL);
4930Sstevel@tonic-gate 	if (rfsreqcnt_v3_kstat != NULL)
4940Sstevel@tonic-gate 		safe_kstat_write(kc, rfsreqcnt_v3_kstat, NULL);
4950Sstevel@tonic-gate 	if (rfsreqcnt_v4_kstat != NULL)
4960Sstevel@tonic-gate 		safe_kstat_write(kc, rfsreqcnt_v4_kstat, NULL);
4970Sstevel@tonic-gate 	if (aclproccnt_v2_kstat != NULL)
4980Sstevel@tonic-gate 		safe_kstat_write(kc, aclproccnt_v2_kstat, NULL);
4990Sstevel@tonic-gate 	if (aclproccnt_v3_kstat != NULL)
5000Sstevel@tonic-gate 		safe_kstat_write(kc, aclproccnt_v3_kstat, NULL);
5010Sstevel@tonic-gate 	if (aclreqcnt_v2_kstat != NULL)
5020Sstevel@tonic-gate 		safe_kstat_write(kc, aclreqcnt_v2_kstat, NULL);
5030Sstevel@tonic-gate 	if (aclreqcnt_v3_kstat != NULL)
5040Sstevel@tonic-gate 		safe_kstat_write(kc, aclreqcnt_v3_kstat, NULL);
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate static void
5080Sstevel@tonic-gate setup(void)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
5110Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/* malloc space for our temporary kstat */
5140Sstevel@tonic-gate 	ksum_kstat = malloc(sizeof (kstat_t));
5150Sstevel@tonic-gate 	rpc_clts_client_kstat = kstat_lookup(kc, "unix", 0, "rpc_clts_client");
5160Sstevel@tonic-gate 	rpc_clts_server_kstat = kstat_lookup(kc, "unix", 0, "rpc_clts_server");
5170Sstevel@tonic-gate 	rpc_cots_client_kstat = kstat_lookup(kc, "unix", 0, "rpc_cots_client");
5180Sstevel@tonic-gate 	rpc_cots_server_kstat = kstat_lookup(kc, "unix", 0, "rpc_cots_server");
5190Sstevel@tonic-gate 	rpc_rdma_client_kstat = kstat_lookup(kc, "unix", 0, "rpc_rdma_client");
5200Sstevel@tonic-gate 	rpc_rdma_server_kstat = kstat_lookup(kc, "unix", 0, "rpc_rdma_server");
5210Sstevel@tonic-gate 	nfs_client_kstat = kstat_lookup(kc, "nfs", 0, "nfs_client");
5220Sstevel@tonic-gate 	nfs4_client_kstat = kstat_lookup(kc, "nfs", 0, "nfs4_client");
5230Sstevel@tonic-gate 	nfs_server_v2_kstat = kstat_lookup(kc, "nfs", 2, "nfs_server");
5240Sstevel@tonic-gate 	nfs_server_v3_kstat = kstat_lookup(kc, "nfs", 3, "nfs_server");
5250Sstevel@tonic-gate 	nfs_server_v4_kstat = kstat_lookup(kc, "nfs", 4, "nfs_server");
5260Sstevel@tonic-gate 	rfsproccnt_v2_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v2");
5270Sstevel@tonic-gate 	rfsproccnt_v3_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v3");
5280Sstevel@tonic-gate 	rfsproccnt_v4_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v4");
5290Sstevel@tonic-gate 	rfsreqcnt_v2_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v2");
5300Sstevel@tonic-gate 	rfsreqcnt_v3_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v3");
5310Sstevel@tonic-gate 	rfsreqcnt_v4_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v4");
5320Sstevel@tonic-gate 	aclproccnt_v2_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclproccnt_v2");
5330Sstevel@tonic-gate 	aclproccnt_v3_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclproccnt_v3");
5340Sstevel@tonic-gate 	aclreqcnt_v2_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclreqcnt_v2");
5350Sstevel@tonic-gate 	aclreqcnt_v3_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclreqcnt_v3");
5360Sstevel@tonic-gate 	if (rpc_clts_client_kstat == NULL && rpc_cots_server_kstat == NULL &&
5370Sstevel@tonic-gate 	    rfsproccnt_v2_kstat == NULL && rfsreqcnt_v3_kstat == NULL)
5380Sstevel@tonic-gate 		fail(0, "Multiple kstat lookups failed."
5393406Sth199096 		    "Your kernel module may not be loaded\n");
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate static int
5430Sstevel@tonic-gate req_width(kstat_t *req, int field_width)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	int i, nreq, per, len;
5460Sstevel@tonic-gate 	char fixlen[128];
5470Sstevel@tonic-gate 	kstat_named_t *knp;
5480Sstevel@tonic-gate 	uint64_t tot;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	tot = 0;
5510Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(req);
5520Sstevel@tonic-gate 	for (i = 0; i < req->ks_ndata; i++)
5530Sstevel@tonic-gate 		tot += knp[i].value.ui64;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	knp = kstat_data_lookup(req, "null");
5560Sstevel@tonic-gate 	nreq = req->ks_ndata - (knp - KSTAT_NAMED_PTR(req));
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
5590Sstevel@tonic-gate 		len = strlen(knp[i].name) + 1;
5600Sstevel@tonic-gate 		if (field_width < len)
5610Sstevel@tonic-gate 			field_width = len;
5620Sstevel@tonic-gate 		if (tot)
5630Sstevel@tonic-gate 			per = (int)(knp[i].value.ui64 * 100 / tot);
5640Sstevel@tonic-gate 		else
5650Sstevel@tonic-gate 			per = 0;
5660Sstevel@tonic-gate 		(void) sprintf(fixlen, "%" PRIu64 " %d%%",
5675302Sth199096 		    knp[i].value.ui64, per);
5680Sstevel@tonic-gate 		len = strlen(fixlen) + 1;
5690Sstevel@tonic-gate 		if (field_width < len)
5700Sstevel@tonic-gate 			field_width = len;
5710Sstevel@tonic-gate 	}
5720Sstevel@tonic-gate 	return (field_width);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate static int
5760Sstevel@tonic-gate stat_width(kstat_t *req, int field_width)
5770Sstevel@tonic-gate {
5780Sstevel@tonic-gate 	int i, nreq, len;
5790Sstevel@tonic-gate 	char fixlen[128];
5800Sstevel@tonic-gate 	kstat_named_t *knp;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(req);
5830Sstevel@tonic-gate 	nreq = req->ks_ndata;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
5860Sstevel@tonic-gate 		len = strlen(knp[i].name) + 1;
5870Sstevel@tonic-gate 		if (field_width < len)
5880Sstevel@tonic-gate 			field_width = len;
5890Sstevel@tonic-gate 		(void) sprintf(fixlen, "%" PRIu64, knp[i].value.ui64);
5900Sstevel@tonic-gate 		len = strlen(fixlen) + 1;
5910Sstevel@tonic-gate 		if (field_width < len)
5920Sstevel@tonic-gate 			field_width = len;
5930Sstevel@tonic-gate 	}
5940Sstevel@tonic-gate 	return (field_width);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate static void
5980Sstevel@tonic-gate cr_print(int zflag)
5990Sstevel@tonic-gate {
6000Sstevel@tonic-gate 	int field_width;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	field_width = getstats_rpc();
6033406Sth199096 	if (field_width == 0)
6043406Sth199096 		return;
6053406Sth199096 
6060Sstevel@tonic-gate 	stat_print("\nClient rpc:\nConnection oriented:",
6075302Sth199096 	    rpc_cots_client_kstat,
6085302Sth199096 	    &old_rpc_cots_client_kstat.kst, field_width, zflag);
6090Sstevel@tonic-gate 	stat_print("Connectionless:", rpc_clts_client_kstat,
6105302Sth199096 	    &old_rpc_clts_client_kstat.kst, field_width, zflag);
6110Sstevel@tonic-gate 	stat_print("RDMA based:", rpc_rdma_client_kstat,
6125302Sth199096 	    &old_rpc_rdma_client_kstat.kst, field_width, zflag);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate static void
6160Sstevel@tonic-gate sr_print(int zflag)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate 	int field_width;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	field_width = getstats_rpc();
6213406Sth199096 	if (field_width == 0)
6223406Sth199096 		return;
6233406Sth199096 
6240Sstevel@tonic-gate 	stat_print("\nServer rpc:\nConnection oriented:", rpc_cots_server_kstat,
6255302Sth199096 	    &old_rpc_cots_server_kstat.kst, field_width, zflag);
6260Sstevel@tonic-gate 	stat_print("Connectionless:", rpc_clts_server_kstat,
6275302Sth199096 	    &old_rpc_clts_server_kstat.kst, field_width, zflag);
6280Sstevel@tonic-gate 	stat_print("RDMA based:", rpc_rdma_server_kstat,
6295302Sth199096 	    &old_rpc_rdma_server_kstat.kst, field_width, zflag);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate static void
6330Sstevel@tonic-gate cn_print(int zflag, int vflag)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	int field_width;
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	field_width = getstats_nfs();
6383406Sth199096 	if (field_width == 0)
6393406Sth199096 		return;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	if (vflag == 0) {
642*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_sum(nfs_client_kstat, nfs4_client_kstat,
643*10265SKrishnendu.Sadhukhan@Sun.COM 		    ksum_kstat);
6440Sstevel@tonic-gate 		stat_print("\nClient nfs:", ksum_kstat, &old_ksum_kstat.kst,
6455302Sth199096 		    field_width, zflag);
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (vflag == 2 || vflag == 3) {
6490Sstevel@tonic-gate 		stat_print("\nClient nfs:", nfs_client_kstat,
6505302Sth199096 		    &old_nfs_client_kstat.kst, field_width, zflag);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	if (vflag == 4) {
6540Sstevel@tonic-gate 		stat_print("\nClient nfs:", nfs4_client_kstat,
6555302Sth199096 		    &old_nfs4_client_kstat.kst, field_width, zflag);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
6590Sstevel@tonic-gate 		field_width = getstats_rfsreq(2);
6600Sstevel@tonic-gate 		req_print(rfsreqcnt_v2_kstat, &old_rfsreqcnt_v2_kstat.kst,
6615302Sth199096 		    2, field_width, zflag);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
6650Sstevel@tonic-gate 		field_width = getstats_rfsreq(3);
6660Sstevel@tonic-gate 		req_print(rfsreqcnt_v3_kstat, &old_rfsreqcnt_v3_kstat.kst, 3,
6675302Sth199096 		    field_width, zflag);
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (vflag == 4 || vflag == 0) {
6710Sstevel@tonic-gate 		field_width = getstats_rfsreq(4);
6720Sstevel@tonic-gate 		req_print_v4(rfsreqcnt_v4_kstat, &old_rfsreqcnt_v4_kstat.kst,
6735302Sth199096 		    field_width, zflag);
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate }
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate static void
6780Sstevel@tonic-gate sn_print(int zflag, int vflag)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate 	int  field_width;
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	field_width = getstats_nfs();
6833406Sth199096 	if (field_width == 0)
6843406Sth199096 		return;
6853406Sth199096 
6860Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
6870Sstevel@tonic-gate 		stat_print("\nServer NFSv2:", nfs_server_v2_kstat,
6885302Sth199096 		    &old_nfs_server_v2_kstat.kst, field_width, zflag);
6890Sstevel@tonic-gate 	}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
6920Sstevel@tonic-gate 		stat_print("\nServer NFSv3:", nfs_server_v3_kstat,
6935302Sth199096 		    &old_nfs_server_v3_kstat.kst, field_width, zflag);
6940Sstevel@tonic-gate 	}
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	if (vflag == 4 || vflag == 0) {
6970Sstevel@tonic-gate 		stat_print("\nServer NFSv4:", nfs_server_v4_kstat,
6985302Sth199096 		    &old_nfs_server_v4_kstat.kst, field_width, zflag);
6990Sstevel@tonic-gate 	}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
7020Sstevel@tonic-gate 		field_width = getstats_rfsproc(2);
7030Sstevel@tonic-gate 		req_print(rfsproccnt_v2_kstat, &old_rfsproccnt_v2_kstat.kst,
7045302Sth199096 		    2, field_width, zflag);
7050Sstevel@tonic-gate 	}
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
7080Sstevel@tonic-gate 		field_width = getstats_rfsproc(3);
7090Sstevel@tonic-gate 		req_print(rfsproccnt_v3_kstat, &old_rfsproccnt_v3_kstat.kst,
7105302Sth199096 		    3, field_width, zflag);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (vflag == 4 || vflag == 0) {
7140Sstevel@tonic-gate 		field_width = getstats_rfsproc(4);
7150Sstevel@tonic-gate 		req_print_v4(rfsproccnt_v4_kstat, &old_rfsproccnt_v4_kstat.kst,
7165302Sth199096 		    field_width, zflag);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate static void
7210Sstevel@tonic-gate ca_print(int zflag, int vflag)
7220Sstevel@tonic-gate {
7230Sstevel@tonic-gate 	int  field_width;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	field_width = getstats_aclreq();
7263406Sth199096 	if (field_width == 0)
7273406Sth199096 		return;
7283406Sth199096 
7290Sstevel@tonic-gate 	printf("\nClient nfs_acl:\n");
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
7320Sstevel@tonic-gate 		req_print(aclreqcnt_v2_kstat, &old_aclreqcnt_v2_kstat.kst, 2,
7335302Sth199096 		    field_width, zflag);
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
7370Sstevel@tonic-gate 		req_print(aclreqcnt_v3_kstat, &old_aclreqcnt_v3_kstat.kst,
7385302Sth199096 		    3, field_width, zflag);
7390Sstevel@tonic-gate 	}
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate static void
7430Sstevel@tonic-gate sa_print(int zflag, int vflag)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	int  field_width;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	field_width = getstats_aclproc();
7483406Sth199096 	if (field_width == 0)
7493406Sth199096 		return;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	printf("\nServer nfs_acl:\n");
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
7540Sstevel@tonic-gate 		req_print(aclproccnt_v2_kstat, &old_aclproccnt_v2_kstat.kst,
7555302Sth199096 		    2, field_width, zflag);
7560Sstevel@tonic-gate 	}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
7590Sstevel@tonic-gate 		req_print(aclproccnt_v3_kstat, &old_aclproccnt_v3_kstat.kst,
7605302Sth199096 		    3, field_width, zflag);
7610Sstevel@tonic-gate 	}
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate #define	MIN(a, b)	((a) < (b) ? (a) : (b))
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate static void
7670Sstevel@tonic-gate req_print(kstat_t *req, kstat_t *req_old, int ver, int field_width,
7685302Sth199096     int zflag)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	int i, j, nreq, per, ncolumns;
7710Sstevel@tonic-gate 	uint64_t tot, old_tot;
7720Sstevel@tonic-gate 	char fixlen[128];
7730Sstevel@tonic-gate 	kstat_named_t *knp;
7740Sstevel@tonic-gate 	kstat_named_t *kptr;
7750Sstevel@tonic-gate 	kstat_named_t *knp_old;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	if (req == NULL)
7780Sstevel@tonic-gate 		return;
7790Sstevel@tonic-gate 
7803406Sth199096 	if (field_width == 0)
7813406Sth199096 		return;
7823406Sth199096 
7830Sstevel@tonic-gate 	ncolumns = (MAX_COLUMNS -1)/field_width;
7840Sstevel@tonic-gate 	knp = kstat_data_lookup(req, "null");
7850Sstevel@tonic-gate 	knp_old = KSTAT_NAMED_PTR(req_old);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	kptr = KSTAT_NAMED_PTR(req);
7880Sstevel@tonic-gate 	nreq = req->ks_ndata - (knp - KSTAT_NAMED_PTR(req));
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	tot = 0;
7910Sstevel@tonic-gate 	old_tot = 0;
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (knp_old == NULL) {
7940Sstevel@tonic-gate 		old_tot = 0;
7950Sstevel@tonic-gate 	}
7960Sstevel@tonic-gate 
7970Sstevel@tonic-gate 	for (i = 0; i < req->ks_ndata; i++)
7980Sstevel@tonic-gate 		tot += kptr[i].value.ui64;
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if (interval && knp_old != NULL) {
8010Sstevel@tonic-gate 		for (i = 0; i < req_old->ks_ndata; i++)
8020Sstevel@tonic-gate 			old_tot += knp_old[i].value.ui64;
8030Sstevel@tonic-gate 		tot -= old_tot;
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	printf("Version %d: (%" PRIu64 " calls)\n", ver, tot);
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	for (i = 0; i < nreq; i += ncolumns) {
8090Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
8100Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
8110Sstevel@tonic-gate 		}
8120Sstevel@tonic-gate 		printf("\n");
8130Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
8140Sstevel@tonic-gate 			if (tot && interval && knp_old != NULL)
8150Sstevel@tonic-gate 				per = (int)((knp[j].value.ui64 -
8160Sstevel@tonic-gate 				    knp_old[j].value.ui64) * 100 / tot);
8170Sstevel@tonic-gate 			else if (tot)
8180Sstevel@tonic-gate 				per = (int)(knp[j].value.ui64 * 100 / tot);
8190Sstevel@tonic-gate 			else
8200Sstevel@tonic-gate 				per = 0;
8210Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " %d%% ",
8225302Sth199096 			    ((interval && knp_old != NULL) ?
8235302Sth199096 			    (knp[j].value.ui64 - knp_old[j].value.ui64)
8245302Sth199096 			    : knp[j].value.ui64), per);
8250Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
8260Sstevel@tonic-gate 		}
8270Sstevel@tonic-gate 		printf("\n");
8285998Sgt29601 	}
8295998Sgt29601 	if (zflag) {
8305998Sgt29601 		for (i = 0; i < req->ks_ndata; i++)
8315998Sgt29601 			knp[i].value.ui64 = 0;
8320Sstevel@tonic-gate 	}
8335998Sgt29601 	if (knp_old != NULL)
834*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_copy(req, req_old, 1);
8355998Sgt29601 	else
836*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_copy(req, req_old, 0);
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate  * Separate version of the req_print() to deal with V4 and its use of
8410Sstevel@tonic-gate  * procedures and operations.  It looks odd to have the counts for
8420Sstevel@tonic-gate  * both of those lumped into the same set of statistics so this
8430Sstevel@tonic-gate  * function (copy of req_print() does the separation and titles).
8440Sstevel@tonic-gate  */
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate #define	COUNT	2
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate static void
8490Sstevel@tonic-gate req_print_v4(kstat_t *req, kstat_t *req_old, int field_width, int zflag)
8500Sstevel@tonic-gate {
8510Sstevel@tonic-gate 	int i, j, nreq, per, ncolumns;
8520Sstevel@tonic-gate 	uint64_t tot, tot_ops, old_tot, old_tot_ops;
8530Sstevel@tonic-gate 	char fixlen[128];
8540Sstevel@tonic-gate 	kstat_named_t *kptr;
8550Sstevel@tonic-gate 	kstat_named_t *knp;
8560Sstevel@tonic-gate 	kstat_named_t *kptr_old;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	if (req == NULL)
8590Sstevel@tonic-gate 		return;
8600Sstevel@tonic-gate 
8613406Sth199096 	if (field_width == 0)
8623406Sth199096 		return;
8633406Sth199096 
8640Sstevel@tonic-gate 	ncolumns = (MAX_COLUMNS)/field_width;
8650Sstevel@tonic-gate 	kptr = KSTAT_NAMED_PTR(req);
8660Sstevel@tonic-gate 	kptr_old = KSTAT_NAMED_PTR(req_old);
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	if (kptr_old == NULL) {
8690Sstevel@tonic-gate 		old_tot_ops = 0;
8700Sstevel@tonic-gate 		old_tot = 0;
8710Sstevel@tonic-gate 	} else {
8720Sstevel@tonic-gate 		old_tot =  kptr_old[0].value.ui64 + kptr_old[1].value.ui64;
8730Sstevel@tonic-gate 		for (i = 2, old_tot_ops = 0; i < req_old->ks_ndata; i++)
8740Sstevel@tonic-gate 			old_tot_ops += kptr_old[i].value.ui64;
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	/* Count the number of operations sent */
8780Sstevel@tonic-gate 	for (i = 2, tot_ops = 0; i < req->ks_ndata; i++)
8790Sstevel@tonic-gate 		tot_ops += kptr[i].value.ui64;
8800Sstevel@tonic-gate 	/* For v4 NULL/COMPOUND are the only procedures */
8810Sstevel@tonic-gate 	tot = kptr[0].value.ui64 + kptr[1].value.ui64;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (interval) {
8840Sstevel@tonic-gate 		tot -= old_tot;
8850Sstevel@tonic-gate 		tot_ops -= old_tot_ops;
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	printf("Version 4: (%" PRIu64 " calls)\n", tot);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	knp = kstat_data_lookup(req, "null");
8910Sstevel@tonic-gate 	nreq = req->ks_ndata - (knp - KSTAT_NAMED_PTR(req));
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	for (i = 0; i < COUNT; i += ncolumns) {
8940Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, 2); j++) {
8950Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 		printf("\n");
8980Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, 2); j++) {
8990Sstevel@tonic-gate 			if (tot && interval && kptr_old != NULL)
9000Sstevel@tonic-gate 				per = (int)((knp[j].value.ui64 -
9010Sstevel@tonic-gate 				    kptr_old[j].value.ui64) * 100 / tot);
9020Sstevel@tonic-gate 			else if (tot)
9030Sstevel@tonic-gate 				per = (int)(knp[j].value.ui64 * 100 / tot);
9040Sstevel@tonic-gate 			else
9050Sstevel@tonic-gate 				per = 0;
9060Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " %d%% ",
9075302Sth199096 			    ((interval && kptr_old != NULL) ?
9085302Sth199096 			    (knp[j].value.ui64 - kptr_old[j].value.ui64)
9095302Sth199096 			    : knp[j].value.ui64), per);
9100Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 		printf("\n");
9130Sstevel@tonic-gate 	}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	printf("Version 4: (%" PRIu64 " operations)\n", tot_ops);
9160Sstevel@tonic-gate 	for (i = 2; i < nreq; i += ncolumns) {
9170Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
9180Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 		printf("\n");
9210Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
9220Sstevel@tonic-gate 			if (tot_ops && interval && kptr_old != NULL)
9230Sstevel@tonic-gate 				per = (int)((knp[j].value.ui64 -
9240Sstevel@tonic-gate 				    kptr_old[j].value.ui64) * 100 / tot_ops);
9250Sstevel@tonic-gate 			else if (tot_ops)
9260Sstevel@tonic-gate 				per = (int)(knp[j].value.ui64 * 100 / tot_ops);
9270Sstevel@tonic-gate 			else
9280Sstevel@tonic-gate 				per = 0;
9290Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " %d%% ",
9305302Sth199096 			    ((interval && kptr_old != NULL) ?
9315302Sth199096 			    (knp[j].value.ui64 - kptr_old[j].value.ui64)
9325302Sth199096 			    : knp[j].value.ui64), per);
9330Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
9340Sstevel@tonic-gate 		}
9350Sstevel@tonic-gate 		printf("\n");
9360Sstevel@tonic-gate 	}
9370Sstevel@tonic-gate 	if (zflag) {
9380Sstevel@tonic-gate 		for (i = 0; i < req->ks_ndata; i++)
9390Sstevel@tonic-gate 			kptr[i].value.ui64 = 0;
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate 	if (kptr_old != NULL)
942*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_copy(req, req_old, 1);
9430Sstevel@tonic-gate 	else
944*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_copy(req, req_old, 0);
9450Sstevel@tonic-gate }
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate static void
9480Sstevel@tonic-gate stat_print(const char *title_string, kstat_t *req, kstat_t  *req_old,
9495302Sth199096     int field_width, int zflag)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate 	int i, j, nreq, ncolumns;
9520Sstevel@tonic-gate 	char fixlen[128];
9530Sstevel@tonic-gate 	kstat_named_t *knp;
9540Sstevel@tonic-gate 	kstat_named_t *knp_old;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	if (req == NULL)
9570Sstevel@tonic-gate 		return;
9580Sstevel@tonic-gate 
9593406Sth199096 	if (field_width == 0)
9603406Sth199096 		return;
9613406Sth199096 
9620Sstevel@tonic-gate 	printf("%s\n", title_string);
9630Sstevel@tonic-gate 	ncolumns = (MAX_COLUMNS -1)/field_width;
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	/* MEANS knp =  (kstat_named_t *)req->ks_data */
9660Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(req);
9670Sstevel@tonic-gate 	nreq = req->ks_ndata;
9680Sstevel@tonic-gate 	knp_old = KSTAT_NAMED_PTR(req_old);
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	for (i = 0; i < nreq; i += ncolumns) {
9710Sstevel@tonic-gate 		/* prints out the titles of the columns */
9720Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
9730Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 		printf("\n");
9760Sstevel@tonic-gate 		/* prints out the stat numbers */
9770Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
9780Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " ",
9795302Sth199096 			    (interval && knp_old != NULL) ?
9805302Sth199096 			    (knp[j].value.ui64 - knp_old[j].value.ui64)
9815302Sth199096 			    : knp[j].value.ui64);
9820Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
9830Sstevel@tonic-gate 		}
9840Sstevel@tonic-gate 		printf("\n");
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	}
9870Sstevel@tonic-gate 	if (zflag) {
9880Sstevel@tonic-gate 		for (i = 0; i < req->ks_ndata; i++)
9890Sstevel@tonic-gate 			knp[i].value.ui64 = 0;
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	if (knp_old != NULL)
993*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_copy(req, req_old, 1);
9940Sstevel@tonic-gate 	else
995*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_copy(req, req_old, 0);
9960Sstevel@tonic-gate }
9973406Sth199096 
9980Sstevel@tonic-gate static void
999*10265SKrishnendu.Sadhukhan@Sun.COM nfsstat_kstat_sum(kstat_t *kstat1, kstat_t *kstat2, kstat_t *sum)
10000Sstevel@tonic-gate {
10010Sstevel@tonic-gate 	int i;
10020Sstevel@tonic-gate 	kstat_named_t *knp1, *knp2, *knpsum;
10030Sstevel@tonic-gate 	if (kstat1 == NULL || kstat2 == NULL)
10040Sstevel@tonic-gate 		return;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	knp1 = KSTAT_NAMED_PTR(kstat1);
10070Sstevel@tonic-gate 	knp2 = KSTAT_NAMED_PTR(kstat2);
10080Sstevel@tonic-gate 	if (sum->ks_data == NULL)
1009*10265SKrishnendu.Sadhukhan@Sun.COM 		nfsstat_kstat_copy(kstat1, sum, 0);
10100Sstevel@tonic-gate 	knpsum = KSTAT_NAMED_PTR(sum);
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	for (i = 0; i < (kstat1->ks_ndata); i++)
10130Sstevel@tonic-gate 		knpsum[i].value.ui64 =  knp1[i].value.ui64 + knp2[i].value.ui64;
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate /*
10170Sstevel@tonic-gate  * my_dir and my_path could be pointers
10180Sstevel@tonic-gate  */
10190Sstevel@tonic-gate struct myrec {
10200Sstevel@tonic-gate 	ulong_t my_fsid;
10210Sstevel@tonic-gate 	char my_dir[MAXPATHLEN];
10220Sstevel@tonic-gate 	char *my_path;
10230Sstevel@tonic-gate 	char *ig_path;
10240Sstevel@tonic-gate 	struct myrec *next;
10250Sstevel@tonic-gate };
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate  * Print the mount table info
10290Sstevel@tonic-gate  */
10300Sstevel@tonic-gate static void
10310Sstevel@tonic-gate mi_print(void)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate 	FILE *mt;
10340Sstevel@tonic-gate 	struct extmnttab m;
10350Sstevel@tonic-gate 	struct myrec *list, *mrp, *pmrp;
10360Sstevel@tonic-gate 	char *flavor;
10370Sstevel@tonic-gate 	int ignored = 0;
10380Sstevel@tonic-gate 	seconfig_t nfs_sec;
10390Sstevel@tonic-gate 	kstat_t *ksp;
10400Sstevel@tonic-gate 	struct mntinfo_kstat mik;
10410Sstevel@tonic-gate 	int transport_flag = 0;
10420Sstevel@tonic-gate 	int path_count;
10430Sstevel@tonic-gate 	int found;
10440Sstevel@tonic-gate 	char *timer_name[] = {
10450Sstevel@tonic-gate 		"Lookups",
10460Sstevel@tonic-gate 		"Reads",
10470Sstevel@tonic-gate 		"Writes",
10480Sstevel@tonic-gate 		"All"
10490Sstevel@tonic-gate 	};
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	mt = fopen(MNTTAB, "r");
10520Sstevel@tonic-gate 	if (mt == NULL) {
10530Sstevel@tonic-gate 		perror(MNTTAB);
10540Sstevel@tonic-gate 		exit(0);
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	list = NULL;
10580Sstevel@tonic-gate 	resetmnttab(mt);
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	while (getextmntent(mt, &m, sizeof (struct extmnttab)) == 0) {
10610Sstevel@tonic-gate 		/* ignore non "nfs" and save the "ignore" entries */
10620Sstevel@tonic-gate 		if (strcmp(m.mnt_fstype, MNTTYPE_NFS) != 0)
10630Sstevel@tonic-gate 			continue;
10640Sstevel@tonic-gate 		/*
10650Sstevel@tonic-gate 		 * Check to see here if user gave a path(s) to
10660Sstevel@tonic-gate 		 * only show the mount point they wanted
10670Sstevel@tonic-gate 		 * Iterate through the list of paths the user gave and see
10685302Sth199096 		 * if any of them match our current nfs mount
10690Sstevel@tonic-gate 		 */
10700Sstevel@tonic-gate 		if (path[0] != NULL) {
10710Sstevel@tonic-gate 			found = 0;
10720Sstevel@tonic-gate 			for (path_count = 0; path[path_count] != NULL;
10730Sstevel@tonic-gate 			    path_count++) {
10740Sstevel@tonic-gate 				if (strcmp(path[path_count], m.mnt_mountp)
10750Sstevel@tonic-gate 				    == 0) {
10760Sstevel@tonic-gate 					found = 1;
10770Sstevel@tonic-gate 					break;
10780Sstevel@tonic-gate 				}
10790Sstevel@tonic-gate 			}
10800Sstevel@tonic-gate 			if (!found)
10810Sstevel@tonic-gate 				continue;
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 		if ((mrp = malloc(sizeof (struct myrec))) == 0) {
10850Sstevel@tonic-gate 			fprintf(stderr, "nfsstat: not enough memory\n");
10860Sstevel@tonic-gate 			exit(1);
10870Sstevel@tonic-gate 		}
10880Sstevel@tonic-gate 		mrp->my_fsid = makedev(m.mnt_major, m.mnt_minor);
10890Sstevel@tonic-gate 		if (ignore(m.mnt_mntopts)) {
10900Sstevel@tonic-gate 			/*
10910Sstevel@tonic-gate 			 * ignored entries cannot be ignored for this
10920Sstevel@tonic-gate 			 * option. We have to display the info for this
10930Sstevel@tonic-gate 			 * nfs mount. The ignore is an indication
10940Sstevel@tonic-gate 			 * that the actual mount point is different and
10950Sstevel@tonic-gate 			 * something is in between the nfs mount.
10960Sstevel@tonic-gate 			 * So save the mount point now
10970Sstevel@tonic-gate 			 */
10980Sstevel@tonic-gate 			if ((mrp->ig_path = malloc(
10995302Sth199096 			    strlen(m.mnt_mountp) + 1)) == 0) {
11000Sstevel@tonic-gate 				fprintf(stderr, "nfsstat: not enough memory\n");
11010Sstevel@tonic-gate 				exit(1);
11020Sstevel@tonic-gate 			}
11030Sstevel@tonic-gate 			(void) strcpy(mrp->ig_path, m.mnt_mountp);
11040Sstevel@tonic-gate 			ignored++;
11050Sstevel@tonic-gate 		} else {
11060Sstevel@tonic-gate 			mrp->ig_path = 0;
11070Sstevel@tonic-gate 			(void) strcpy(mrp->my_dir, m.mnt_mountp);
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 		if ((mrp->my_path = strdup(m.mnt_special)) == NULL) {
11100Sstevel@tonic-gate 			fprintf(stderr, "nfsstat: not enough memory\n");
11110Sstevel@tonic-gate 			exit(1);
11120Sstevel@tonic-gate 		}
11130Sstevel@tonic-gate 		mrp->next = list;
11140Sstevel@tonic-gate 		list = mrp;
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	/*
11180Sstevel@tonic-gate 	 * If something got ignored, go to the beginning of the mnttab
11190Sstevel@tonic-gate 	 * and look for the cachefs entries since they are the one
11200Sstevel@tonic-gate 	 * causing this. The mount point saved for the ignored entries
11210Sstevel@tonic-gate 	 * is matched against the special to get the actual mount point.
11220Sstevel@tonic-gate 	 * We are interested in the acutal mount point so that the output
11230Sstevel@tonic-gate 	 * look nice too.
11240Sstevel@tonic-gate 	 */
11250Sstevel@tonic-gate 	if (ignored) {
11260Sstevel@tonic-gate 		rewind(mt);
11270Sstevel@tonic-gate 		resetmnttab(mt);
11280Sstevel@tonic-gate 		while (getextmntent(mt, &m, sizeof (struct extmnttab)) == 0) {
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 			/* ignore non "cachefs" */
11310Sstevel@tonic-gate 			if (strcmp(m.mnt_fstype, MNTTYPE_CACHEFS) != 0)
11320Sstevel@tonic-gate 				continue;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 			for (mrp = list; mrp; mrp = mrp->next) {
11350Sstevel@tonic-gate 				if (mrp->ig_path == 0)
11360Sstevel@tonic-gate 					continue;
11370Sstevel@tonic-gate 				if (strcmp(mrp->ig_path, m.mnt_special) == 0) {
11380Sstevel@tonic-gate 					mrp->ig_path = 0;
11390Sstevel@tonic-gate 					(void) strcpy(mrp->my_dir,
11405302Sth199096 					    m.mnt_mountp);
11410Sstevel@tonic-gate 				}
11420Sstevel@tonic-gate 			}
11430Sstevel@tonic-gate 		}
11440Sstevel@tonic-gate 		/*
11450Sstevel@tonic-gate 		 * Now ignored entries which do not have
11460Sstevel@tonic-gate 		 * the my_dir initialized are really ignored; This never
11470Sstevel@tonic-gate 		 * happens unless the mnttab is corrupted.
11480Sstevel@tonic-gate 		 */
11490Sstevel@tonic-gate 		for (pmrp = 0, mrp = list; mrp; mrp = mrp->next) {
11500Sstevel@tonic-gate 			if (mrp->ig_path == 0)
11510Sstevel@tonic-gate 				pmrp = mrp;
11520Sstevel@tonic-gate 			else if (pmrp)
11530Sstevel@tonic-gate 				pmrp->next = mrp->next;
11540Sstevel@tonic-gate 			else
11550Sstevel@tonic-gate 				list = mrp->next;
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	(void) fclose(mt);
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
11630Sstevel@tonic-gate 		int i;
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_RAW)
11660Sstevel@tonic-gate 			continue;
11670Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "nfs") != 0)
11680Sstevel@tonic-gate 			continue;
11690Sstevel@tonic-gate 		if (strcmp(ksp->ks_name, "mntinfo") != 0)
11700Sstevel@tonic-gate 			continue;
11710Sstevel@tonic-gate 
11720Sstevel@tonic-gate 		for (mrp = list; mrp; mrp = mrp->next) {
11730Sstevel@tonic-gate 			if ((mrp->my_fsid & MAXMIN) == ksp->ks_instance)
11740Sstevel@tonic-gate 				break;
11750Sstevel@tonic-gate 		}
11760Sstevel@tonic-gate 		if (mrp == 0)
11770Sstevel@tonic-gate 			continue;
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 		if (safe_kstat_read(kc, ksp, &mik) == -1)
11800Sstevel@tonic-gate 			continue;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 		printf("%s from %s\n", mrp->my_dir, mrp->my_path);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 		/*
11850Sstevel@tonic-gate 		 * for printing rdma transport and provider string.
11860Sstevel@tonic-gate 		 * This way we avoid modifying the kernel mntinfo_kstat
11870Sstevel@tonic-gate 		 * struct for protofmly.
11880Sstevel@tonic-gate 		 */
11890Sstevel@tonic-gate 		if (strcmp(mik.mik_proto, "ibtf") == 0) {
11900Sstevel@tonic-gate 			printf(" Flags:		vers=%u,proto=rdma",
11910Sstevel@tonic-gate 			    mik.mik_vers);
11920Sstevel@tonic-gate 			transport_flag = 1;
11930Sstevel@tonic-gate 		} else {
11940Sstevel@tonic-gate 			printf(" Flags:		vers=%u,proto=%s",
11950Sstevel@tonic-gate 			    mik.mik_vers, mik.mik_proto);
11960Sstevel@tonic-gate 			transport_flag = 0;
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 		/*
12000Sstevel@tonic-gate 		 *  get the secmode name from /etc/nfssec.conf.
12010Sstevel@tonic-gate 		 */
12020Sstevel@tonic-gate 		if (!nfs_getseconfig_bynumber(mik.mik_secmod, &nfs_sec)) {
12030Sstevel@tonic-gate 			flavor = nfs_sec.sc_name;
12040Sstevel@tonic-gate 		} else
12050Sstevel@tonic-gate 			flavor = NULL;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 		if (flavor != NULL)
12080Sstevel@tonic-gate 			printf(",sec=%s", flavor);
12090Sstevel@tonic-gate 		else
12100Sstevel@tonic-gate 			printf(",sec#=%d", mik.mik_secmod);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 		printf(",%s", (mik.mik_flags & MI_HARD) ? "hard" : "soft");
12130Sstevel@tonic-gate 		if (mik.mik_flags & MI_PRINTED)
12140Sstevel@tonic-gate 			printf(",printed");
12150Sstevel@tonic-gate 		printf(",%s", (mik.mik_flags & MI_INT) ? "intr" : "nointr");
12160Sstevel@tonic-gate 		if (mik.mik_flags & MI_DOWN)
12170Sstevel@tonic-gate 			printf(",down");
12180Sstevel@tonic-gate 		if (mik.mik_flags & MI_NOAC)
12190Sstevel@tonic-gate 			printf(",noac");
12200Sstevel@tonic-gate 		if (mik.mik_flags & MI_NOCTO)
12210Sstevel@tonic-gate 			printf(",nocto");
12220Sstevel@tonic-gate 		if (mik.mik_flags & MI_DYNAMIC)
12230Sstevel@tonic-gate 			printf(",dynamic");
12240Sstevel@tonic-gate 		if (mik.mik_flags & MI_LLOCK)
12250Sstevel@tonic-gate 			printf(",llock");
12260Sstevel@tonic-gate 		if (mik.mik_flags & MI_GRPID)
12270Sstevel@tonic-gate 			printf(",grpid");
12280Sstevel@tonic-gate 		if (mik.mik_flags & MI_RPCTIMESYNC)
12290Sstevel@tonic-gate 			printf(",rpctimesync");
12300Sstevel@tonic-gate 		if (mik.mik_flags & MI_LINK)
12310Sstevel@tonic-gate 			printf(",link");
12320Sstevel@tonic-gate 		if (mik.mik_flags & MI_SYMLINK)
12330Sstevel@tonic-gate 			printf(",symlink");
12345897Sth199096 		if (mik.mik_vers < NFS_V4 && mik.mik_flags & MI_READDIRONLY)
12350Sstevel@tonic-gate 			printf(",readdironly");
12360Sstevel@tonic-gate 		if (mik.mik_flags & MI_ACL)
12370Sstevel@tonic-gate 			printf(",acl");
12380Sstevel@tonic-gate 
12395302Sth199096 		if (mik.mik_vers >= NFS_V4) {
12405302Sth199096 			if (mik.mik_flags & MI4_MIRRORMOUNT)
12415302Sth199096 				printf(",mirrormount");
12425302Sth199096 		}
12435302Sth199096 
12440Sstevel@tonic-gate 		printf(",rsize=%d,wsize=%d,retrans=%d,timeo=%d",
12455302Sth199096 		    mik.mik_curread, mik.mik_curwrite, mik.mik_retrans,
12465302Sth199096 		    mik.mik_timeo);
12470Sstevel@tonic-gate 		printf("\n");
12480Sstevel@tonic-gate 		printf(" Attr cache:	acregmin=%d,acregmax=%d"
12495302Sth199096 		    ",acdirmin=%d,acdirmax=%d\n", mik.mik_acregmin,
12505302Sth199096 		    mik.mik_acregmax, mik.mik_acdirmin, mik.mik_acdirmax);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		if (transport_flag) {
12530Sstevel@tonic-gate 			printf(" Transport:	proto=rdma, plugin=%s\n",
12540Sstevel@tonic-gate 			    mik.mik_proto);
12550Sstevel@tonic-gate 		}
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate #define	srtt_to_ms(x) x, (x * 2 + x / 2)
12580Sstevel@tonic-gate #define	dev_to_ms(x) x, (x * 5)
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		for (i = 0; i < NFS_CALLTYPES + 1; i++) {
12610Sstevel@tonic-gate 			int j;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 			j = (i == NFS_CALLTYPES ? i - 1 : i);
12640Sstevel@tonic-gate 			if (mik.mik_timers[j].srtt ||
12650Sstevel@tonic-gate 			    mik.mik_timers[j].rtxcur) {
12665302Sth199096 				printf(" %s:     srtt=%d (%dms), "
12675302Sth199096 				    "dev=%d (%dms), cur=%u (%ums)\n",
12685302Sth199096 				    timer_name[i],
12695302Sth199096 				    srtt_to_ms(mik.mik_timers[i].srtt),
12705302Sth199096 				    dev_to_ms(mik.mik_timers[i].deviate),
12715302Sth199096 				    mik.mik_timers[i].rtxcur,
12725302Sth199096 				    mik.mik_timers[i].rtxcur * 20);
12730Sstevel@tonic-gate 			}
12740Sstevel@tonic-gate 		}
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		if (strchr(mrp->my_path, ','))
12770Sstevel@tonic-gate 			printf(
12780Sstevel@tonic-gate 			    " Failover:	noresponse=%d,failover=%d,"
12790Sstevel@tonic-gate 			    "remap=%d,currserver=%s\n",
12800Sstevel@tonic-gate 			    mik.mik_noresponse, mik.mik_failover,
12810Sstevel@tonic-gate 			    mik.mik_remap, mik.mik_curserver);
12820Sstevel@tonic-gate 		printf("\n");
12830Sstevel@tonic-gate 	}
12840Sstevel@tonic-gate }
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate static char *mntopts[] = { MNTOPT_IGNORE, MNTOPT_DEV, NULL };
12870Sstevel@tonic-gate #define	IGNORE  0
12880Sstevel@tonic-gate #define	DEV	1
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate /*
12910Sstevel@tonic-gate  * Return 1 if "ignore" appears in the options string
12920Sstevel@tonic-gate  */
12930Sstevel@tonic-gate static int
12940Sstevel@tonic-gate ignore(char *opts)
12950Sstevel@tonic-gate {
12960Sstevel@tonic-gate 	char *value;
12970Sstevel@tonic-gate 	char *s;
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	if (opts == NULL)
13000Sstevel@tonic-gate 		return (0);
13010Sstevel@tonic-gate 	s = strdup(opts);
13020Sstevel@tonic-gate 	if (s == NULL)
13030Sstevel@tonic-gate 		return (0);
13040Sstevel@tonic-gate 	opts = s;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	while (*opts != '\0') {
13070Sstevel@tonic-gate 		if (getsubopt(&opts, mntopts, &value) == IGNORE) {
13080Sstevel@tonic-gate 			free(s);
13090Sstevel@tonic-gate 			return (1);
13100Sstevel@tonic-gate 		}
13110Sstevel@tonic-gate 	}
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	free(s);
13140Sstevel@tonic-gate 	return (0);
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate void
13180Sstevel@tonic-gate usage(void)
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	fprintf(stderr, "Usage: nfsstat [-cnrsza [-v version] "
1321*10265SKrishnendu.Sadhukhan@Sun.COM 	    "[-T d|u] [interval [count]]\n");
13220Sstevel@tonic-gate 	fprintf(stderr, "Usage: nfsstat -m [pathname..]\n");
13230Sstevel@tonic-gate 	exit(1);
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate 
1326*10265SKrishnendu.Sadhukhan@Sun.COM void
13270Sstevel@tonic-gate fail(int do_perror, char *message, ...)
13280Sstevel@tonic-gate {
13290Sstevel@tonic-gate 	va_list args;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	va_start(args, message);
13320Sstevel@tonic-gate 	fprintf(stderr, "nfsstat: ");
13330Sstevel@tonic-gate 	vfprintf(stderr, message, args);
13340Sstevel@tonic-gate 	va_end(args);
13350Sstevel@tonic-gate 	if (do_perror)
13360Sstevel@tonic-gate 		fprintf(stderr, ": %s", strerror(errno));
13370Sstevel@tonic-gate 	fprintf(stderr, "\n");
13380Sstevel@tonic-gate 	exit(1);
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate kid_t
13420Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (kstat_chain_id == -1)
13470Sstevel@tonic-gate 		fail(1, "kstat_read(%x, '%s') failed", kc, ksp->ks_name);
13480Sstevel@tonic-gate 	return (kstat_chain_id);
13490Sstevel@tonic-gate }
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate kid_t
13520Sstevel@tonic-gate safe_kstat_write(kstat_ctl_t *kc, kstat_t *ksp, void *data)
13530Sstevel@tonic-gate {
13540Sstevel@tonic-gate 	kid_t kstat_chain_id = 0;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	if (ksp->ks_data != NULL) {
13570Sstevel@tonic-gate 		kstat_chain_id = kstat_write(kc, ksp, data);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 		if (kstat_chain_id == -1)
13600Sstevel@tonic-gate 			fail(1, "kstat_write(%x, '%s') failed", kc,
13610Sstevel@tonic-gate 			    ksp->ks_name);
13620Sstevel@tonic-gate 	}
13630Sstevel@tonic-gate 	return (kstat_chain_id);
13640Sstevel@tonic-gate }
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate void
13670Sstevel@tonic-gate stats_timer(int interval)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate 	timer_t t_id;
13700Sstevel@tonic-gate 	itimerspec_t time_struct;
13710Sstevel@tonic-gate 	struct sigevent sig_struct;
13720Sstevel@tonic-gate 	struct sigaction act;
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	bzero(&sig_struct, sizeof (struct sigevent));
13750Sstevel@tonic-gate 	bzero(&act, sizeof (struct sigaction));
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/* Create timer */
13780Sstevel@tonic-gate 	sig_struct.sigev_notify = SIGEV_SIGNAL;
13790Sstevel@tonic-gate 	sig_struct.sigev_signo = SIGUSR1;
13800Sstevel@tonic-gate 	sig_struct.sigev_value.sival_int = 0;
13810Sstevel@tonic-gate 
13820Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) {
13830Sstevel@tonic-gate 		fail(1, "Timer creation failed");
13840Sstevel@tonic-gate 	}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	act.sa_handler = handle_sig;
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	if (sigaction(SIGUSR1, &act, NULL) != 0) {
13890Sstevel@tonic-gate 		fail(1, "Could not set up signal handler");
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	time_struct.it_value.tv_sec = interval;
13930Sstevel@tonic-gate 	time_struct.it_value.tv_nsec = 0;
13940Sstevel@tonic-gate 	time_struct.it_interval.tv_sec = interval;
13950Sstevel@tonic-gate 	time_struct.it_interval.tv_nsec = 0;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	/* Arm timer */
13980Sstevel@tonic-gate 	if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) {
13990Sstevel@tonic-gate 		fail(1, "Setting timer failed");
14000Sstevel@tonic-gate 	}
14010Sstevel@tonic-gate }
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate void
14040Sstevel@tonic-gate handle_sig(int x)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate static void
1409*10265SKrishnendu.Sadhukhan@Sun.COM nfsstat_kstat_copy(kstat_t *src, kstat_t *dst, int fr)
14100Sstevel@tonic-gate {
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	if (fr)
14130Sstevel@tonic-gate 		free(dst->ks_data);
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	*dst = *src;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	if (src->ks_data != NULL) {
14180Sstevel@tonic-gate 		safe_zalloc(&dst->ks_data, src->ks_data_size, 0);
14190Sstevel@tonic-gate 		(void) memcpy(dst->ks_data, src->ks_data, src->ks_data_size);
14200Sstevel@tonic-gate 	} else {
14210Sstevel@tonic-gate 		dst->ks_data = NULL;
14220Sstevel@tonic-gate 		dst->ks_data_size = 0;
14230Sstevel@tonic-gate 	}
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate /*
14270Sstevel@tonic-gate  * "Safe" allocators - if we return we're guaranteed
14280Sstevel@tonic-gate  * to have the desired space. We exit via fail
14290Sstevel@tonic-gate  * if we can't get the space.
14300Sstevel@tonic-gate  */
14310Sstevel@tonic-gate void
14320Sstevel@tonic-gate safe_zalloc(void **ptr, uint_t size, int free_first)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	if (*ptr == NULL)
14350Sstevel@tonic-gate 		fail(1, "invalid pointer");
14360Sstevel@tonic-gate 	if (free_first && *ptr != NULL)
14370Sstevel@tonic-gate 		free(*ptr);
14380Sstevel@tonic-gate 	if ((*ptr = (void *)malloc(size)) == NULL)
14390Sstevel@tonic-gate 		fail(1, "malloc failed");
14400Sstevel@tonic-gate 	(void) memset(*ptr, 0, size);
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate static int
14440Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
14450Sstevel@tonic-gate {
14460Sstevel@tonic-gate 	char *end;
14470Sstevel@tonic-gate 	long tmp;
14480Sstevel@tonic-gate 	errno = 0;
14490Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
14500Sstevel@tonic-gate 	if (*end != '\0' || errno)
14510Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
14520Sstevel@tonic-gate 	return ((int)tmp);
14530Sstevel@tonic-gate }
1454