xref: /onnv-gate/usr/src/cmd/fs.d/nfs/nfsstat/nfsstat.c (revision 5302:eec6aeacde6e)
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 /*
263406Sth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * nfsstat: Network File System statistics
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <stdarg.h>
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate #include <errno.h>
430Sstevel@tonic-gate #include <fcntl.h>
440Sstevel@tonic-gate #include <kvm.h>
450Sstevel@tonic-gate #include <kstat.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/t_lock.h>
490Sstevel@tonic-gate #include <sys/tiuser.h>
500Sstevel@tonic-gate #include <sys/statvfs.h>
510Sstevel@tonic-gate #include <sys/mntent.h>
520Sstevel@tonic-gate #include <sys/mnttab.h>
530Sstevel@tonic-gate #include <sys/sysmacros.h>
540Sstevel@tonic-gate #include <sys/mkdev.h>
550Sstevel@tonic-gate #include <rpc/types.h>
560Sstevel@tonic-gate #include <rpc/xdr.h>
570Sstevel@tonic-gate #include <rpc/auth.h>
580Sstevel@tonic-gate #include <rpc/clnt.h>
590Sstevel@tonic-gate #include <nfs/nfs.h>
600Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
610Sstevel@tonic-gate #include <nfs/nfs_sec.h>
620Sstevel@tonic-gate #include <inttypes.h>
630Sstevel@tonic-gate #include <signal.h>
640Sstevel@tonic-gate #include <time.h>
650Sstevel@tonic-gate #include <sys/time.h>
660Sstevel@tonic-gate #include <strings.h>
670Sstevel@tonic-gate #include <ctype.h>
680Sstevel@tonic-gate 
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);
1000Sstevel@tonic-gate static void 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 
1060Sstevel@tonic-gate static void kstat_copy(kstat_t *, kstat_t *, int);
1070Sstevel@tonic-gate static void fail(int, char *, ...);
1080Sstevel@tonic-gate static kid_t safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
1090Sstevel@tonic-gate static kid_t safe_kstat_write(kstat_ctl_t *, kstat_t *, void *);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static void usage(void);
1120Sstevel@tonic-gate static void mi_print(void);
1130Sstevel@tonic-gate static int ignore(char *);
1140Sstevel@tonic-gate static int interval;		/* interval between stats */
1150Sstevel@tonic-gate static int count;		/* number of iterations the stat is printed */
1160Sstevel@tonic-gate #define	MAX_COLUMNS	80
1170Sstevel@tonic-gate #define	MAX_PATHS	50	/* max paths that can be taken by -m */
1180Sstevel@tonic-gate 
119*5302Sth199096 /*
120*5302Sth199096  * MI4_MIRRORMOUNT is canonically defined in nfs4_clnt.h, but we cannot
121*5302Sth199096  * include that file here.
122*5302Sth199096  */
123*5302Sth199096 #define	MI4_MIRRORMOUNT 0x4000
124*5302Sth199096 #define	NFS_V4		4
125*5302Sth199096 
1260Sstevel@tonic-gate static int req_width(kstat_t *, int);
1270Sstevel@tonic-gate static int stat_width(kstat_t *, int);
1280Sstevel@tonic-gate static char *path [MAX_PATHS] = {NULL};  /* array to store the multiple paths */
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * Struct holds the previous kstat values so
1320Sstevel@tonic-gate  * we can compute deltas when using the -i flag
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate typedef struct old_kstat
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate 	kstat_t kst;
1370Sstevel@tonic-gate 	int tot;
1380Sstevel@tonic-gate } old_kstat_t;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate static old_kstat_t old_rpc_clts_client_kstat, old_rpc_clts_server_kstat;
1410Sstevel@tonic-gate static old_kstat_t old_rpc_cots_client_kstat, old_rpc_cots_server_kstat;
1420Sstevel@tonic-gate static old_kstat_t old_rpc_rdma_client_kstat, old_rpc_rdma_server_kstat;
1430Sstevel@tonic-gate static old_kstat_t old_nfs_client_kstat, old_nfs_server_v2_kstat;
1440Sstevel@tonic-gate static old_kstat_t old_nfs_server_v3_kstat, old_ksum_kstat;
1450Sstevel@tonic-gate static old_kstat_t old_nfs4_client_kstat, old_nfs_server_v4_kstat;
1460Sstevel@tonic-gate static old_kstat_t old_rfsproccnt_v2_kstat, old_rfsproccnt_v3_kstat;
1470Sstevel@tonic-gate static old_kstat_t old_rfsproccnt_v4_kstat, old_rfsreqcnt_v2_kstat;
1480Sstevel@tonic-gate static old_kstat_t old_rfsreqcnt_v3_kstat, old_rfsreqcnt_v4_kstat;
1490Sstevel@tonic-gate static old_kstat_t old_aclproccnt_v2_kstat, old_aclproccnt_v3_kstat;
1500Sstevel@tonic-gate static old_kstat_t old_aclreqcnt_v2_kstat, old_aclreqcnt_v3_kstat;
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 
154249Sjwahlig int
1550Sstevel@tonic-gate main(int argc, char *argv[])
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	int c, go_forever, j;
1580Sstevel@tonic-gate 	int cflag = 0;		/* client stats */
1590Sstevel@tonic-gate 	int sflag = 0;		/* server stats */
1600Sstevel@tonic-gate 	int nflag = 0;		/* nfs stats */
1610Sstevel@tonic-gate 	int rflag = 0;		/* rpc stats */
1620Sstevel@tonic-gate 	int mflag = 0;		/* mount table stats */
1630Sstevel@tonic-gate 	int aflag = 0;		/* print acl statistics */
1640Sstevel@tonic-gate 	int vflag = 0;		/* version specified, 0 specifies all */
1650Sstevel@tonic-gate 	int zflag = 0;		/* zero stats after printing */
1660Sstevel@tonic-gate 	char *split_line = "*******************************************"
167*5302Sth199096 	    "*************************************";
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	interval = 0;
1700Sstevel@tonic-gate 	count = 0;
1710Sstevel@tonic-gate 	go_forever = 0;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "cnrsmzav:")) != EOF) {
1740Sstevel@tonic-gate 		switch (c) {
1750Sstevel@tonic-gate 		case 'c':
1760Sstevel@tonic-gate 			cflag++;
1770Sstevel@tonic-gate 			break;
1780Sstevel@tonic-gate 		case 'n':
1790Sstevel@tonic-gate 			nflag++;
1800Sstevel@tonic-gate 			break;
1810Sstevel@tonic-gate 		case 'r':
1820Sstevel@tonic-gate 			rflag++;
1830Sstevel@tonic-gate 			break;
1840Sstevel@tonic-gate 		case 's':
1850Sstevel@tonic-gate 			sflag++;
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 		case 'm':
1880Sstevel@tonic-gate 			mflag++;
1890Sstevel@tonic-gate 			break;
1900Sstevel@tonic-gate 		case 'z':
1910Sstevel@tonic-gate 			if (geteuid())
1920Sstevel@tonic-gate 				fail(0, "Must be root for z flag\n");
1930Sstevel@tonic-gate 			zflag++;
1940Sstevel@tonic-gate 			break;
1950Sstevel@tonic-gate 		case 'a':
1960Sstevel@tonic-gate 			aflag++;
1970Sstevel@tonic-gate 			break;
1980Sstevel@tonic-gate 		case 'v':
1990Sstevel@tonic-gate 			vflag = atoi(optarg);
2000Sstevel@tonic-gate 			if ((vflag < 2) || (vflag > 4))
2010Sstevel@tonic-gate 				fail(0, "Invalid version number\n");
2020Sstevel@tonic-gate 			break;
2030Sstevel@tonic-gate 		case '?':
2040Sstevel@tonic-gate 		default:
2050Sstevel@tonic-gate 			usage();
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	if (((argc - optind) > 0) && !mflag) {
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		interval = safe_strtoi(argv[optind], "invalid interval");
2120Sstevel@tonic-gate 		if (interval < 1)
2130Sstevel@tonic-gate 			fail(0, "invalid interval\n");
2140Sstevel@tonic-gate 		optind++;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		if ((argc - optind) > 0) {
2170Sstevel@tonic-gate 			count = safe_strtoi(argv[optind], "invalid count");
2180Sstevel@tonic-gate 			if ((count <= 0) || (count == NULL))
2190Sstevel@tonic-gate 				fail(0, "invalid count\n");
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 		optind++;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		if ((argc - optind) > 0)
2240Sstevel@tonic-gate 			usage();
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		/*
2270Sstevel@tonic-gate 		 * no count number was set, so we will loop infinitely
2280Sstevel@tonic-gate 		 * at interval specified
2290Sstevel@tonic-gate 		 */
2300Sstevel@tonic-gate 		if (!count)
2310Sstevel@tonic-gate 			go_forever = 1;
2320Sstevel@tonic-gate 		stats_timer(interval);
2330Sstevel@tonic-gate 	} else if (mflag) {
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 		if (cflag || rflag || sflag || zflag || nflag || aflag || vflag)
236*5302Sth199096 			fail(0,
237*5302Sth199096 			    "The -m flag may not be used with any other flags");
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		for (j = 0; (argc - optind > 0) && (j < (MAX_PATHS - 1)); j++) {
2400Sstevel@tonic-gate 			path[j] =  argv[optind];
2410Sstevel@tonic-gate 			if (*path[j] != '/')
2420Sstevel@tonic-gate 				fail(0, "Please fully qualify your pathname "
2430Sstevel@tonic-gate 				    "with a leading '/'");
2440Sstevel@tonic-gate 			optind++;
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 		path[j] = NULL;
2470Sstevel@tonic-gate 		if (argc - optind > 0)
2480Sstevel@tonic-gate 			fprintf(stderr, "Only the first 50 paths "
249*5302Sth199096 			    "will be searched for\n");
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	setup();
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	do {
2550Sstevel@tonic-gate 		if (mflag) {
2560Sstevel@tonic-gate 			mi_print();
2570Sstevel@tonic-gate 		} else {
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 			if (sflag &&
2600Sstevel@tonic-gate 			    (rpc_clts_server_kstat == NULL ||
2610Sstevel@tonic-gate 			    nfs_server_v4_kstat == NULL)) {
2620Sstevel@tonic-gate 				fprintf(stderr,
2630Sstevel@tonic-gate 				    "nfsstat: kernel is not configured with "
2640Sstevel@tonic-gate 				    "the server nfs and rpc code.\n");
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 			/* if s and nothing else, all 3 prints are called */
2680Sstevel@tonic-gate 			if (sflag || (!sflag && !cflag)) {
2690Sstevel@tonic-gate 				if (rflag || (!rflag && !nflag && !aflag))
2700Sstevel@tonic-gate 					sr_print(zflag);
2710Sstevel@tonic-gate 				if (nflag || (!rflag && !nflag && !aflag))
2720Sstevel@tonic-gate 					sn_print(zflag, vflag);
2730Sstevel@tonic-gate 				if (aflag || (!rflag && !nflag && !aflag))
2740Sstevel@tonic-gate 					sa_print(zflag, vflag);
2750Sstevel@tonic-gate 			}
2760Sstevel@tonic-gate 			if (cflag &&
2770Sstevel@tonic-gate 			    (rpc_clts_client_kstat == NULL ||
2780Sstevel@tonic-gate 			    nfs_client_kstat == NULL)) {
2790Sstevel@tonic-gate 				fprintf(stderr,
280*5302Sth199096 				    "nfsstat: kernel is not configured with"
281*5302Sth199096 				    " the client nfs and rpc code.\n");
2820Sstevel@tonic-gate 			}
2830Sstevel@tonic-gate 			if (cflag || (!sflag && !cflag)) {
2840Sstevel@tonic-gate 				if (rflag || (!rflag && !nflag && !aflag))
2850Sstevel@tonic-gate 					cr_print(zflag);
2860Sstevel@tonic-gate 				if (nflag || (!rflag && !nflag && !aflag))
2870Sstevel@tonic-gate 					cn_print(zflag, vflag);
2880Sstevel@tonic-gate 				if (aflag || (!rflag && !nflag && !aflag))
2890Sstevel@tonic-gate 					ca_print(zflag, vflag);
2900Sstevel@tonic-gate 			}
2910Sstevel@tonic-gate 		}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 		if (zflag)
2940Sstevel@tonic-gate 			putstats();
2950Sstevel@tonic-gate 		if (interval)
2960Sstevel@tonic-gate 			printf("%s\n", split_line);
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 		if (interval > 0)
2990Sstevel@tonic-gate 			(void) pause();
3000Sstevel@tonic-gate 	} while ((--count > 0) || go_forever);
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	kstat_close(kc);
3030Sstevel@tonic-gate 	free(ksum_kstat);
3040Sstevel@tonic-gate 	return (0);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate static int
3090Sstevel@tonic-gate getstats_rpc(void)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	int field_width = 0;
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	if (rpc_clts_client_kstat != NULL) {
3140Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_clts_client_kstat, NULL);
3150Sstevel@tonic-gate 		field_width = stat_width(rpc_clts_client_kstat, field_width);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	if (rpc_cots_client_kstat != NULL) {
3190Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_cots_client_kstat, NULL);
3200Sstevel@tonic-gate 		field_width = stat_width(rpc_cots_client_kstat, field_width);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	if (rpc_rdma_client_kstat != NULL) {
3240Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_rdma_client_kstat, NULL);
3250Sstevel@tonic-gate 		field_width = stat_width(rpc_rdma_client_kstat, field_width);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	if (rpc_clts_server_kstat != NULL) {
3290Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_clts_server_kstat, NULL);
3300Sstevel@tonic-gate 		field_width =  stat_width(rpc_clts_server_kstat, field_width);
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate 	if (rpc_cots_server_kstat != NULL) {
3330Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_cots_server_kstat, NULL);
3340Sstevel@tonic-gate 		field_width = stat_width(rpc_cots_server_kstat, field_width);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 	if (rpc_rdma_server_kstat != NULL) {
3370Sstevel@tonic-gate 		safe_kstat_read(kc, rpc_rdma_server_kstat, NULL);
3380Sstevel@tonic-gate 		field_width = stat_width(rpc_rdma_server_kstat, field_width);
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 	return (field_width);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate static int
3440Sstevel@tonic-gate getstats_nfs(void)
3450Sstevel@tonic-gate {
3460Sstevel@tonic-gate 	int field_width = 0;
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if (nfs_client_kstat != NULL) {
3490Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_client_kstat, NULL);
3500Sstevel@tonic-gate 		field_width = stat_width(nfs_client_kstat, field_width);
3510Sstevel@tonic-gate 	}
3520Sstevel@tonic-gate 	if (nfs4_client_kstat != NULL) {
3530Sstevel@tonic-gate 		safe_kstat_read(kc, nfs4_client_kstat, NULL);
3540Sstevel@tonic-gate 		field_width = stat_width(nfs4_client_kstat, field_width);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate 	if (nfs_server_v2_kstat != NULL) {
3570Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_server_v2_kstat, NULL);
3580Sstevel@tonic-gate 		field_width = stat_width(nfs_server_v2_kstat, field_width);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 	if (nfs_server_v3_kstat != NULL) {
3610Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_server_v3_kstat, NULL);
3620Sstevel@tonic-gate 		field_width = stat_width(nfs_server_v3_kstat, field_width);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	if (nfs_server_v4_kstat != NULL) {
3650Sstevel@tonic-gate 		safe_kstat_read(kc, nfs_server_v4_kstat, NULL);
3660Sstevel@tonic-gate 		field_width = stat_width(nfs_server_v4_kstat, field_width);
3670Sstevel@tonic-gate 	}
3680Sstevel@tonic-gate 	return (field_width);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate static int
3720Sstevel@tonic-gate getstats_rfsproc(int ver)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	int field_width = 0;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if ((ver == 2) && (rfsproccnt_v2_kstat != NULL)) {
3770Sstevel@tonic-gate 		safe_kstat_read(kc, rfsproccnt_v2_kstat, NULL);
3780Sstevel@tonic-gate 		field_width = req_width(rfsproccnt_v2_kstat, field_width);
3790Sstevel@tonic-gate 	}
3800Sstevel@tonic-gate 	if ((ver == 3) && (rfsproccnt_v3_kstat != NULL)) {
3810Sstevel@tonic-gate 		safe_kstat_read(kc, rfsproccnt_v3_kstat, NULL);
3820Sstevel@tonic-gate 		field_width = req_width(rfsproccnt_v3_kstat, field_width);
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	if ((ver == 4) && (rfsproccnt_v4_kstat != NULL)) {
3850Sstevel@tonic-gate 		safe_kstat_read(kc, rfsproccnt_v4_kstat, NULL);
3860Sstevel@tonic-gate 		field_width = req_width(rfsproccnt_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_rfsreq(int ver)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	int field_width = 0;
3950Sstevel@tonic-gate 	if ((ver == 2) && (rfsreqcnt_v2_kstat != NULL)) {
3960Sstevel@tonic-gate 		safe_kstat_read(kc, rfsreqcnt_v2_kstat, NULL);
3970Sstevel@tonic-gate 		field_width = req_width(rfsreqcnt_v2_kstat, field_width);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 	if ((ver == 3) && (rfsreqcnt_v3_kstat != NULL)) {
4000Sstevel@tonic-gate 		safe_kstat_read(kc, rfsreqcnt_v3_kstat, NULL);
4010Sstevel@tonic-gate 		field_width = req_width(rfsreqcnt_v3_kstat,  field_width);
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 	if ((ver == 4) && (rfsreqcnt_v4_kstat != NULL)) {
4040Sstevel@tonic-gate 		safe_kstat_read(kc, rfsreqcnt_v4_kstat, NULL);
4050Sstevel@tonic-gate 		field_width = req_width(rfsreqcnt_v4_kstat, field_width);
4060Sstevel@tonic-gate 	}
4070Sstevel@tonic-gate 	return (field_width);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate static int
4110Sstevel@tonic-gate getstats_aclproc(void)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	int field_width = 0;
4140Sstevel@tonic-gate 	if (aclproccnt_v2_kstat != NULL) {
4150Sstevel@tonic-gate 		safe_kstat_read(kc, aclproccnt_v2_kstat, NULL);
4160Sstevel@tonic-gate 		field_width = req_width(aclproccnt_v2_kstat, field_width);
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	if (aclproccnt_v3_kstat != NULL) {
4190Sstevel@tonic-gate 		safe_kstat_read(kc, aclproccnt_v3_kstat, NULL);
4200Sstevel@tonic-gate 		field_width = req_width(aclproccnt_v3_kstat, field_width);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 	return (field_width);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate static int
4260Sstevel@tonic-gate getstats_aclreq(void)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	int field_width = 0;
4290Sstevel@tonic-gate 	if (aclreqcnt_v2_kstat != NULL) {
4300Sstevel@tonic-gate 		safe_kstat_read(kc, aclreqcnt_v2_kstat, NULL);
4310Sstevel@tonic-gate 		field_width = req_width(aclreqcnt_v2_kstat, field_width);
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 	if (aclreqcnt_v3_kstat != NULL) {
4340Sstevel@tonic-gate 		safe_kstat_read(kc, aclreqcnt_v3_kstat, NULL);
4350Sstevel@tonic-gate 		field_width = req_width(aclreqcnt_v3_kstat, field_width);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 	return (field_width);
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate static void
4410Sstevel@tonic-gate putstats(void)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	if (rpc_clts_client_kstat != NULL)
4440Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_clts_client_kstat, NULL);
4450Sstevel@tonic-gate 	if (rpc_cots_client_kstat != NULL)
4460Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_cots_client_kstat, NULL);
4470Sstevel@tonic-gate 	if (rpc_rdma_client_kstat != NULL)
4480Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_rdma_client_kstat, NULL);
4490Sstevel@tonic-gate 	if (nfs_client_kstat != NULL)
4500Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_client_kstat, NULL);
4510Sstevel@tonic-gate 	if (nfs4_client_kstat != NULL)
4520Sstevel@tonic-gate 		safe_kstat_write(kc, nfs4_client_kstat, NULL);
4530Sstevel@tonic-gate 	if (rpc_clts_server_kstat != NULL)
4540Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_clts_server_kstat, NULL);
4550Sstevel@tonic-gate 	if (rpc_cots_server_kstat != NULL)
4560Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_cots_server_kstat, NULL);
4570Sstevel@tonic-gate 	if (rpc_rdma_server_kstat != NULL)
4580Sstevel@tonic-gate 		safe_kstat_write(kc, rpc_rdma_server_kstat, NULL);
4590Sstevel@tonic-gate 	if (nfs_server_v2_kstat != NULL)
4600Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_server_v2_kstat, NULL);
4610Sstevel@tonic-gate 	if (nfs_server_v3_kstat != NULL)
4620Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_server_v3_kstat, NULL);
4630Sstevel@tonic-gate 	if (nfs_server_v4_kstat != NULL)
4640Sstevel@tonic-gate 		safe_kstat_write(kc, nfs_server_v4_kstat, NULL);
4650Sstevel@tonic-gate 	if (rfsproccnt_v2_kstat != NULL)
4660Sstevel@tonic-gate 		safe_kstat_write(kc, rfsproccnt_v2_kstat, NULL);
4670Sstevel@tonic-gate 	if (rfsproccnt_v3_kstat != NULL)
4680Sstevel@tonic-gate 		safe_kstat_write(kc, rfsproccnt_v3_kstat, NULL);
4690Sstevel@tonic-gate 	if (rfsproccnt_v4_kstat != NULL)
4700Sstevel@tonic-gate 		safe_kstat_write(kc, rfsproccnt_v4_kstat, NULL);
4710Sstevel@tonic-gate 	if (rfsreqcnt_v2_kstat != NULL)
4720Sstevel@tonic-gate 		safe_kstat_write(kc, rfsreqcnt_v2_kstat, NULL);
4730Sstevel@tonic-gate 	if (rfsreqcnt_v3_kstat != NULL)
4740Sstevel@tonic-gate 		safe_kstat_write(kc, rfsreqcnt_v3_kstat, NULL);
4750Sstevel@tonic-gate 	if (rfsreqcnt_v4_kstat != NULL)
4760Sstevel@tonic-gate 		safe_kstat_write(kc, rfsreqcnt_v4_kstat, NULL);
4770Sstevel@tonic-gate 	if (aclproccnt_v2_kstat != NULL)
4780Sstevel@tonic-gate 		safe_kstat_write(kc, aclproccnt_v2_kstat, NULL);
4790Sstevel@tonic-gate 	if (aclproccnt_v3_kstat != NULL)
4800Sstevel@tonic-gate 		safe_kstat_write(kc, aclproccnt_v3_kstat, NULL);
4810Sstevel@tonic-gate 	if (aclreqcnt_v2_kstat != NULL)
4820Sstevel@tonic-gate 		safe_kstat_write(kc, aclreqcnt_v2_kstat, NULL);
4830Sstevel@tonic-gate 	if (aclreqcnt_v3_kstat != NULL)
4840Sstevel@tonic-gate 		safe_kstat_write(kc, aclreqcnt_v3_kstat, NULL);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate static void
4880Sstevel@tonic-gate setup(void)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
4910Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	/* malloc space for our temporary kstat */
4940Sstevel@tonic-gate 	ksum_kstat = malloc(sizeof (kstat_t));
4950Sstevel@tonic-gate 	rpc_clts_client_kstat = kstat_lookup(kc, "unix", 0, "rpc_clts_client");
4960Sstevel@tonic-gate 	rpc_clts_server_kstat = kstat_lookup(kc, "unix", 0, "rpc_clts_server");
4970Sstevel@tonic-gate 	rpc_cots_client_kstat = kstat_lookup(kc, "unix", 0, "rpc_cots_client");
4980Sstevel@tonic-gate 	rpc_cots_server_kstat = kstat_lookup(kc, "unix", 0, "rpc_cots_server");
4990Sstevel@tonic-gate 	rpc_rdma_client_kstat = kstat_lookup(kc, "unix", 0, "rpc_rdma_client");
5000Sstevel@tonic-gate 	rpc_rdma_server_kstat = kstat_lookup(kc, "unix", 0, "rpc_rdma_server");
5010Sstevel@tonic-gate 	nfs_client_kstat = kstat_lookup(kc, "nfs", 0, "nfs_client");
5020Sstevel@tonic-gate 	nfs4_client_kstat = kstat_lookup(kc, "nfs", 0, "nfs4_client");
5030Sstevel@tonic-gate 	nfs_server_v2_kstat = kstat_lookup(kc, "nfs", 2, "nfs_server");
5040Sstevel@tonic-gate 	nfs_server_v3_kstat = kstat_lookup(kc, "nfs", 3, "nfs_server");
5050Sstevel@tonic-gate 	nfs_server_v4_kstat = kstat_lookup(kc, "nfs", 4, "nfs_server");
5060Sstevel@tonic-gate 	rfsproccnt_v2_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v2");
5070Sstevel@tonic-gate 	rfsproccnt_v3_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v3");
5080Sstevel@tonic-gate 	rfsproccnt_v4_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v4");
5090Sstevel@tonic-gate 	rfsreqcnt_v2_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v2");
5100Sstevel@tonic-gate 	rfsreqcnt_v3_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v3");
5110Sstevel@tonic-gate 	rfsreqcnt_v4_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v4");
5120Sstevel@tonic-gate 	aclproccnt_v2_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclproccnt_v2");
5130Sstevel@tonic-gate 	aclproccnt_v3_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclproccnt_v3");
5140Sstevel@tonic-gate 	aclreqcnt_v2_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclreqcnt_v2");
5150Sstevel@tonic-gate 	aclreqcnt_v3_kstat = kstat_lookup(kc, "nfs_acl", 0, "aclreqcnt_v3");
5160Sstevel@tonic-gate 	if (rpc_clts_client_kstat == NULL && rpc_cots_server_kstat == NULL &&
5170Sstevel@tonic-gate 	    rfsproccnt_v2_kstat == NULL && rfsreqcnt_v3_kstat == NULL)
5180Sstevel@tonic-gate 		fail(0, "Multiple kstat lookups failed."
5193406Sth199096 		    "Your kernel module may not be loaded\n");
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate static int
5230Sstevel@tonic-gate req_width(kstat_t *req, int field_width)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate 	int i, nreq, per, len;
5260Sstevel@tonic-gate 	char fixlen[128];
5270Sstevel@tonic-gate 	kstat_named_t *knp;
5280Sstevel@tonic-gate 	uint64_t tot;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	tot = 0;
5310Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(req);
5320Sstevel@tonic-gate 	for (i = 0; i < req->ks_ndata; i++)
5330Sstevel@tonic-gate 		tot += knp[i].value.ui64;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	knp = kstat_data_lookup(req, "null");
5360Sstevel@tonic-gate 	nreq = req->ks_ndata - (knp - KSTAT_NAMED_PTR(req));
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
5390Sstevel@tonic-gate 		len = strlen(knp[i].name) + 1;
5400Sstevel@tonic-gate 		if (field_width < len)
5410Sstevel@tonic-gate 			field_width = len;
5420Sstevel@tonic-gate 		if (tot)
5430Sstevel@tonic-gate 			per = (int)(knp[i].value.ui64 * 100 / tot);
5440Sstevel@tonic-gate 		else
5450Sstevel@tonic-gate 			per = 0;
5460Sstevel@tonic-gate 		(void) sprintf(fixlen, "%" PRIu64 " %d%%",
547*5302Sth199096 		    knp[i].value.ui64, per);
5480Sstevel@tonic-gate 		len = strlen(fixlen) + 1;
5490Sstevel@tonic-gate 		if (field_width < len)
5500Sstevel@tonic-gate 			field_width = len;
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	return (field_width);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate static int
5560Sstevel@tonic-gate stat_width(kstat_t *req, int field_width)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate 	int i, nreq, len;
5590Sstevel@tonic-gate 	char fixlen[128];
5600Sstevel@tonic-gate 	kstat_named_t *knp;
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(req);
5630Sstevel@tonic-gate 	nreq = req->ks_ndata;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	for (i = 0; i < nreq; i++) {
5660Sstevel@tonic-gate 		len = strlen(knp[i].name) + 1;
5670Sstevel@tonic-gate 		if (field_width < len)
5680Sstevel@tonic-gate 			field_width = len;
5690Sstevel@tonic-gate 		(void) sprintf(fixlen, "%" PRIu64, knp[i].value.ui64);
5700Sstevel@tonic-gate 		len = strlen(fixlen) + 1;
5710Sstevel@tonic-gate 		if (field_width < len)
5720Sstevel@tonic-gate 			field_width = len;
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 	return (field_width);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate static void
5780Sstevel@tonic-gate cr_print(int zflag)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate 	int field_width;
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 	field_width = getstats_rpc();
5833406Sth199096 	if (field_width == 0)
5843406Sth199096 		return;
5853406Sth199096 
5860Sstevel@tonic-gate 	stat_print("\nClient rpc:\nConnection oriented:",
587*5302Sth199096 	    rpc_cots_client_kstat,
588*5302Sth199096 	    &old_rpc_cots_client_kstat.kst, field_width, zflag);
5890Sstevel@tonic-gate 	stat_print("Connectionless:", rpc_clts_client_kstat,
590*5302Sth199096 	    &old_rpc_clts_client_kstat.kst, field_width, zflag);
5910Sstevel@tonic-gate 	stat_print("RDMA based:", rpc_rdma_client_kstat,
592*5302Sth199096 	    &old_rpc_rdma_client_kstat.kst, field_width, zflag);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate static void
5960Sstevel@tonic-gate sr_print(int zflag)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate 	int field_width;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	field_width = getstats_rpc();
6013406Sth199096 	if (field_width == 0)
6023406Sth199096 		return;
6033406Sth199096 
6040Sstevel@tonic-gate 	stat_print("\nServer rpc:\nConnection oriented:", rpc_cots_server_kstat,
605*5302Sth199096 	    &old_rpc_cots_server_kstat.kst, field_width, zflag);
6060Sstevel@tonic-gate 	stat_print("Connectionless:", rpc_clts_server_kstat,
607*5302Sth199096 	    &old_rpc_clts_server_kstat.kst, field_width, zflag);
6080Sstevel@tonic-gate 	stat_print("RDMA based:", rpc_rdma_server_kstat,
609*5302Sth199096 	    &old_rpc_rdma_server_kstat.kst, field_width, zflag);
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate static void
6130Sstevel@tonic-gate cn_print(int zflag, int vflag)
6140Sstevel@tonic-gate {
6150Sstevel@tonic-gate 	int field_width;
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate 	field_width = getstats_nfs();
6183406Sth199096 	if (field_width == 0)
6193406Sth199096 		return;
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	if (vflag == 0) {
6220Sstevel@tonic-gate 		kstat_sum(nfs_client_kstat, nfs4_client_kstat, ksum_kstat);
6230Sstevel@tonic-gate 		stat_print("\nClient nfs:", ksum_kstat, &old_ksum_kstat.kst,
624*5302Sth199096 		    field_width, zflag);
6250Sstevel@tonic-gate 	}
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (vflag == 2 || vflag == 3) {
6280Sstevel@tonic-gate 		stat_print("\nClient nfs:", nfs_client_kstat,
629*5302Sth199096 		    &old_nfs_client_kstat.kst, field_width, zflag);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if (vflag == 4) {
6330Sstevel@tonic-gate 		stat_print("\nClient nfs:", nfs4_client_kstat,
634*5302Sth199096 		    &old_nfs4_client_kstat.kst, field_width, zflag);
6350Sstevel@tonic-gate 	}
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
6380Sstevel@tonic-gate 		field_width = getstats_rfsreq(2);
6390Sstevel@tonic-gate 		req_print(rfsreqcnt_v2_kstat, &old_rfsreqcnt_v2_kstat.kst,
640*5302Sth199096 		    2, field_width, zflag);
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
6440Sstevel@tonic-gate 		field_width = getstats_rfsreq(3);
6450Sstevel@tonic-gate 		req_print(rfsreqcnt_v3_kstat, &old_rfsreqcnt_v3_kstat.kst, 3,
646*5302Sth199096 		    field_width, zflag);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	if (vflag == 4 || vflag == 0) {
6500Sstevel@tonic-gate 		field_width = getstats_rfsreq(4);
6510Sstevel@tonic-gate 		req_print_v4(rfsreqcnt_v4_kstat, &old_rfsreqcnt_v4_kstat.kst,
652*5302Sth199096 		    field_width, zflag);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate static void
6570Sstevel@tonic-gate sn_print(int zflag, int vflag)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	int  field_width;
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	field_width = getstats_nfs();
6623406Sth199096 	if (field_width == 0)
6633406Sth199096 		return;
6643406Sth199096 
6650Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
6660Sstevel@tonic-gate 		stat_print("\nServer NFSv2:", nfs_server_v2_kstat,
667*5302Sth199096 		    &old_nfs_server_v2_kstat.kst, field_width, zflag);
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
6710Sstevel@tonic-gate 		stat_print("\nServer NFSv3:", nfs_server_v3_kstat,
672*5302Sth199096 		    &old_nfs_server_v3_kstat.kst, field_width, zflag);
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	if (vflag == 4 || vflag == 0) {
6760Sstevel@tonic-gate 		stat_print("\nServer NFSv4:", nfs_server_v4_kstat,
677*5302Sth199096 		    &old_nfs_server_v4_kstat.kst, field_width, zflag);
6780Sstevel@tonic-gate 	}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
6810Sstevel@tonic-gate 		field_width = getstats_rfsproc(2);
6820Sstevel@tonic-gate 		req_print(rfsproccnt_v2_kstat, &old_rfsproccnt_v2_kstat.kst,
683*5302Sth199096 		    2, field_width, zflag);
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 
6860Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
6870Sstevel@tonic-gate 		field_width = getstats_rfsproc(3);
6880Sstevel@tonic-gate 		req_print(rfsproccnt_v3_kstat, &old_rfsproccnt_v3_kstat.kst,
689*5302Sth199096 		    3, field_width, zflag);
6900Sstevel@tonic-gate 	}
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate 	if (vflag == 4 || vflag == 0) {
6930Sstevel@tonic-gate 		field_width = getstats_rfsproc(4);
6940Sstevel@tonic-gate 		req_print_v4(rfsproccnt_v4_kstat, &old_rfsproccnt_v4_kstat.kst,
695*5302Sth199096 		    field_width, zflag);
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate static void
7000Sstevel@tonic-gate ca_print(int zflag, int vflag)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	int  field_width;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	field_width = getstats_aclreq();
7053406Sth199096 	if (field_width == 0)
7063406Sth199096 		return;
7073406Sth199096 
7080Sstevel@tonic-gate 	printf("\nClient nfs_acl:\n");
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
7110Sstevel@tonic-gate 		req_print(aclreqcnt_v2_kstat, &old_aclreqcnt_v2_kstat.kst, 2,
712*5302Sth199096 		    field_width, zflag);
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
7160Sstevel@tonic-gate 		req_print(aclreqcnt_v3_kstat, &old_aclreqcnt_v3_kstat.kst,
717*5302Sth199096 		    3, field_width, zflag);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate static void
7220Sstevel@tonic-gate sa_print(int zflag, int vflag)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate 	int  field_width;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	field_width = getstats_aclproc();
7273406Sth199096 	if (field_width == 0)
7283406Sth199096 		return;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	printf("\nServer nfs_acl:\n");
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	if (vflag == 2 || vflag == 0) {
7330Sstevel@tonic-gate 		req_print(aclproccnt_v2_kstat, &old_aclproccnt_v2_kstat.kst,
734*5302Sth199096 		    2, field_width, zflag);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (vflag == 3 || vflag == 0) {
7380Sstevel@tonic-gate 		req_print(aclproccnt_v3_kstat, &old_aclproccnt_v3_kstat.kst,
739*5302Sth199096 		    3, field_width, zflag);
7400Sstevel@tonic-gate 	}
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate #define	MIN(a, b)	((a) < (b) ? (a) : (b))
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate static void
7460Sstevel@tonic-gate req_print(kstat_t *req, kstat_t *req_old, int ver, int field_width,
747*5302Sth199096     int zflag)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	int i, j, nreq, per, ncolumns;
7500Sstevel@tonic-gate 	uint64_t tot, old_tot;
7510Sstevel@tonic-gate 	char fixlen[128];
7520Sstevel@tonic-gate 	kstat_named_t *knp;
7530Sstevel@tonic-gate 	kstat_named_t *kptr;
7540Sstevel@tonic-gate 	kstat_named_t *knp_old;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	if (req == NULL)
7570Sstevel@tonic-gate 		return;
7580Sstevel@tonic-gate 
7593406Sth199096 	if (field_width == 0)
7603406Sth199096 		return;
7613406Sth199096 
7620Sstevel@tonic-gate 	ncolumns = (MAX_COLUMNS -1)/field_width;
7630Sstevel@tonic-gate 	knp = kstat_data_lookup(req, "null");
7640Sstevel@tonic-gate 	knp_old = KSTAT_NAMED_PTR(req_old);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	kptr = KSTAT_NAMED_PTR(req);
7670Sstevel@tonic-gate 	nreq = req->ks_ndata - (knp - KSTAT_NAMED_PTR(req));
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	tot = 0;
7700Sstevel@tonic-gate 	old_tot = 0;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	if (knp_old == NULL) {
7730Sstevel@tonic-gate 		old_tot = 0;
7740Sstevel@tonic-gate 	}
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	for (i = 0; i < req->ks_ndata; i++)
7770Sstevel@tonic-gate 		tot += kptr[i].value.ui64;
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	if (interval && knp_old != NULL) {
7800Sstevel@tonic-gate 		for (i = 0; i < req_old->ks_ndata; i++)
7810Sstevel@tonic-gate 			old_tot += knp_old[i].value.ui64;
7820Sstevel@tonic-gate 		tot -= old_tot;
7830Sstevel@tonic-gate 	}
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	printf("Version %d: (%" PRIu64 " calls)\n", ver, tot);
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	for (i = 0; i < nreq; i += ncolumns) {
7880Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
7890Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
7900Sstevel@tonic-gate 		}
7910Sstevel@tonic-gate 		printf("\n");
7920Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
7930Sstevel@tonic-gate 			if (tot && interval && knp_old != NULL)
7940Sstevel@tonic-gate 				per = (int)((knp[j].value.ui64 -
7950Sstevel@tonic-gate 				    knp_old[j].value.ui64) * 100 / tot);
7960Sstevel@tonic-gate 			else if (tot)
7970Sstevel@tonic-gate 				per = (int)(knp[j].value.ui64 * 100 / tot);
7980Sstevel@tonic-gate 			else
7990Sstevel@tonic-gate 				per = 0;
8000Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " %d%% ",
801*5302Sth199096 			    ((interval && knp_old != NULL) ?
802*5302Sth199096 			    (knp[j].value.ui64 - knp_old[j].value.ui64)
803*5302Sth199096 			    : knp[j].value.ui64), per);
8040Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 		if (zflag) {
8070Sstevel@tonic-gate 			for (i = 0; i < req->ks_ndata; i++)
8080Sstevel@tonic-gate 				knp[i].value.ui64 = 0;
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 		printf("\n");
8110Sstevel@tonic-gate 		if (knp_old != NULL)
8120Sstevel@tonic-gate 			kstat_copy(req, req_old, 1);
8130Sstevel@tonic-gate 		else
8140Sstevel@tonic-gate 			kstat_copy(req, req_old, 0);
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	}
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate  * Separate version of the req_print() to deal with V4 and its use of
8210Sstevel@tonic-gate  * procedures and operations.  It looks odd to have the counts for
8220Sstevel@tonic-gate  * both of those lumped into the same set of statistics so this
8230Sstevel@tonic-gate  * function (copy of req_print() does the separation and titles).
8240Sstevel@tonic-gate  */
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate #define	COUNT	2
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate static void
8290Sstevel@tonic-gate req_print_v4(kstat_t *req, kstat_t *req_old, int field_width, int zflag)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	int i, j, nreq, per, ncolumns;
8320Sstevel@tonic-gate 	uint64_t tot, tot_ops, old_tot, old_tot_ops;
8330Sstevel@tonic-gate 	char fixlen[128];
8340Sstevel@tonic-gate 	kstat_named_t *kptr;
8350Sstevel@tonic-gate 	kstat_named_t *knp;
8360Sstevel@tonic-gate 	kstat_named_t *kptr_old;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	if (req == NULL)
8390Sstevel@tonic-gate 		return;
8400Sstevel@tonic-gate 
8413406Sth199096 	if (field_width == 0)
8423406Sth199096 		return;
8433406Sth199096 
8440Sstevel@tonic-gate 	ncolumns = (MAX_COLUMNS)/field_width;
8450Sstevel@tonic-gate 	kptr = KSTAT_NAMED_PTR(req);
8460Sstevel@tonic-gate 	kptr_old = KSTAT_NAMED_PTR(req_old);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	if (kptr_old == NULL) {
8490Sstevel@tonic-gate 		old_tot_ops = 0;
8500Sstevel@tonic-gate 		old_tot = 0;
8510Sstevel@tonic-gate 	} else {
8520Sstevel@tonic-gate 		old_tot =  kptr_old[0].value.ui64 + kptr_old[1].value.ui64;
8530Sstevel@tonic-gate 		for (i = 2, old_tot_ops = 0; i < req_old->ks_ndata; i++)
8540Sstevel@tonic-gate 			old_tot_ops += kptr_old[i].value.ui64;
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/* Count the number of operations sent */
8580Sstevel@tonic-gate 	for (i = 2, tot_ops = 0; i < req->ks_ndata; i++)
8590Sstevel@tonic-gate 		tot_ops += kptr[i].value.ui64;
8600Sstevel@tonic-gate 	/* For v4 NULL/COMPOUND are the only procedures */
8610Sstevel@tonic-gate 	tot = kptr[0].value.ui64 + kptr[1].value.ui64;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if (interval) {
8640Sstevel@tonic-gate 		tot -= old_tot;
8650Sstevel@tonic-gate 		tot_ops -= old_tot_ops;
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	printf("Version 4: (%" PRIu64 " calls)\n", tot);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	knp = kstat_data_lookup(req, "null");
8710Sstevel@tonic-gate 	nreq = req->ks_ndata - (knp - KSTAT_NAMED_PTR(req));
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	for (i = 0; i < COUNT; i += ncolumns) {
8740Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, 2); j++) {
8750Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
8760Sstevel@tonic-gate 		}
8770Sstevel@tonic-gate 		printf("\n");
8780Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, 2); j++) {
8790Sstevel@tonic-gate 			if (tot && interval && kptr_old != NULL)
8800Sstevel@tonic-gate 				per = (int)((knp[j].value.ui64 -
8810Sstevel@tonic-gate 				    kptr_old[j].value.ui64) * 100 / tot);
8820Sstevel@tonic-gate 			else if (tot)
8830Sstevel@tonic-gate 				per = (int)(knp[j].value.ui64 * 100 / tot);
8840Sstevel@tonic-gate 			else
8850Sstevel@tonic-gate 				per = 0;
8860Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " %d%% ",
887*5302Sth199096 			    ((interval && kptr_old != NULL) ?
888*5302Sth199096 			    (knp[j].value.ui64 - kptr_old[j].value.ui64)
889*5302Sth199096 			    : knp[j].value.ui64), per);
8900Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 		printf("\n");
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	printf("Version 4: (%" PRIu64 " operations)\n", tot_ops);
8960Sstevel@tonic-gate 	for (i = 2; i < nreq; i += ncolumns) {
8970Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
8980Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 		printf("\n");
9010Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
9020Sstevel@tonic-gate 			if (tot_ops && interval && kptr_old != NULL)
9030Sstevel@tonic-gate 				per = (int)((knp[j].value.ui64 -
9040Sstevel@tonic-gate 				    kptr_old[j].value.ui64) * 100 / tot_ops);
9050Sstevel@tonic-gate 			else if (tot_ops)
9060Sstevel@tonic-gate 				per = (int)(knp[j].value.ui64 * 100 / tot_ops);
9070Sstevel@tonic-gate 			else
9080Sstevel@tonic-gate 				per = 0;
9090Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " %d%% ",
910*5302Sth199096 			    ((interval && kptr_old != NULL) ?
911*5302Sth199096 			    (knp[j].value.ui64 - kptr_old[j].value.ui64)
912*5302Sth199096 			    : knp[j].value.ui64), per);
9130Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 		printf("\n");
9160Sstevel@tonic-gate 	}
9170Sstevel@tonic-gate 	if (zflag) {
9180Sstevel@tonic-gate 		for (i = 0; i < req->ks_ndata; i++)
9190Sstevel@tonic-gate 			kptr[i].value.ui64 = 0;
9200Sstevel@tonic-gate 	}
9210Sstevel@tonic-gate 	if (kptr_old != NULL)
9220Sstevel@tonic-gate 		kstat_copy(req, req_old, 1);
9230Sstevel@tonic-gate 	else
9240Sstevel@tonic-gate 		kstat_copy(req, req_old, 0);
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate static void
9280Sstevel@tonic-gate stat_print(const char *title_string, kstat_t *req, kstat_t  *req_old,
929*5302Sth199096     int field_width, int zflag)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate 	int i, j, nreq, ncolumns;
9320Sstevel@tonic-gate 	char fixlen[128];
9330Sstevel@tonic-gate 	kstat_named_t *knp;
9340Sstevel@tonic-gate 	kstat_named_t *knp_old;
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	if (req == NULL)
9370Sstevel@tonic-gate 		return;
9380Sstevel@tonic-gate 
9393406Sth199096 	if (field_width == 0)
9403406Sth199096 		return;
9413406Sth199096 
9420Sstevel@tonic-gate 	printf("%s\n", title_string);
9430Sstevel@tonic-gate 	ncolumns = (MAX_COLUMNS -1)/field_width;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	/* MEANS knp =  (kstat_named_t *)req->ks_data */
9460Sstevel@tonic-gate 	knp = KSTAT_NAMED_PTR(req);
9470Sstevel@tonic-gate 	nreq = req->ks_ndata;
9480Sstevel@tonic-gate 	knp_old = KSTAT_NAMED_PTR(req_old);
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	for (i = 0; i < nreq; i += ncolumns) {
9510Sstevel@tonic-gate 		/* prints out the titles of the columns */
9520Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
9530Sstevel@tonic-gate 			printf("%-*s", field_width, knp[j].name);
9540Sstevel@tonic-gate 		}
9550Sstevel@tonic-gate 		printf("\n");
9560Sstevel@tonic-gate 		/* prints out the stat numbers */
9570Sstevel@tonic-gate 		for (j = i; j < MIN(i + ncolumns, nreq); j++) {
9580Sstevel@tonic-gate 			(void) sprintf(fixlen, "%" PRIu64 " ",
959*5302Sth199096 			    (interval && knp_old != NULL) ?
960*5302Sth199096 			    (knp[j].value.ui64 - knp_old[j].value.ui64)
961*5302Sth199096 			    : knp[j].value.ui64);
9620Sstevel@tonic-gate 			printf("%-*s", field_width, fixlen);
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 		printf("\n");
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	}
9670Sstevel@tonic-gate 	if (zflag) {
9680Sstevel@tonic-gate 		for (i = 0; i < req->ks_ndata; i++)
9690Sstevel@tonic-gate 			knp[i].value.ui64 = 0;
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 
9720Sstevel@tonic-gate 	if (knp_old != NULL)
9730Sstevel@tonic-gate 		kstat_copy(req, req_old, 1);
9740Sstevel@tonic-gate 	else
9750Sstevel@tonic-gate 		kstat_copy(req, req_old, 0);
9760Sstevel@tonic-gate }
9773406Sth199096 
9780Sstevel@tonic-gate static void
9790Sstevel@tonic-gate kstat_sum(kstat_t *kstat1, kstat_t *kstat2, kstat_t *sum)
9800Sstevel@tonic-gate {
9810Sstevel@tonic-gate 	int i;
9820Sstevel@tonic-gate 	kstat_named_t *knp1, *knp2, *knpsum;
9830Sstevel@tonic-gate 	if (kstat1 == NULL || kstat2 == NULL)
9840Sstevel@tonic-gate 		return;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	knp1 = KSTAT_NAMED_PTR(kstat1);
9870Sstevel@tonic-gate 	knp2 = KSTAT_NAMED_PTR(kstat2);
9880Sstevel@tonic-gate 	if (sum->ks_data == NULL)
9890Sstevel@tonic-gate 		kstat_copy(kstat1, sum, 0);
9900Sstevel@tonic-gate 	knpsum = KSTAT_NAMED_PTR(sum);
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	for (i = 0; i < (kstat1->ks_ndata); i++)
9930Sstevel@tonic-gate 		knpsum[i].value.ui64 =  knp1[i].value.ui64 + knp2[i].value.ui64;
9940Sstevel@tonic-gate }
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate /*
9970Sstevel@tonic-gate  * my_dir and my_path could be pointers
9980Sstevel@tonic-gate  */
9990Sstevel@tonic-gate struct myrec {
10000Sstevel@tonic-gate 	ulong_t my_fsid;
10010Sstevel@tonic-gate 	char my_dir[MAXPATHLEN];
10020Sstevel@tonic-gate 	char *my_path;
10030Sstevel@tonic-gate 	char *ig_path;
10040Sstevel@tonic-gate 	struct myrec *next;
10050Sstevel@tonic-gate };
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate /*
10080Sstevel@tonic-gate  * Print the mount table info
10090Sstevel@tonic-gate  */
10100Sstevel@tonic-gate static void
10110Sstevel@tonic-gate mi_print(void)
10120Sstevel@tonic-gate {
10130Sstevel@tonic-gate 	FILE *mt;
10140Sstevel@tonic-gate 	struct extmnttab m;
10150Sstevel@tonic-gate 	struct myrec *list, *mrp, *pmrp;
10160Sstevel@tonic-gate 	char *flavor;
10170Sstevel@tonic-gate 	int ignored = 0;
10180Sstevel@tonic-gate 	seconfig_t nfs_sec;
10190Sstevel@tonic-gate 	kstat_t *ksp;
10200Sstevel@tonic-gate 	struct mntinfo_kstat mik;
10210Sstevel@tonic-gate 	int transport_flag = 0;
10220Sstevel@tonic-gate 	int path_count;
10230Sstevel@tonic-gate 	int found;
10240Sstevel@tonic-gate 	char *timer_name[] = {
10250Sstevel@tonic-gate 		"Lookups",
10260Sstevel@tonic-gate 		"Reads",
10270Sstevel@tonic-gate 		"Writes",
10280Sstevel@tonic-gate 		"All"
10290Sstevel@tonic-gate 	};
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	mt = fopen(MNTTAB, "r");
10320Sstevel@tonic-gate 	if (mt == NULL) {
10330Sstevel@tonic-gate 		perror(MNTTAB);
10340Sstevel@tonic-gate 		exit(0);
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	list = NULL;
10380Sstevel@tonic-gate 	resetmnttab(mt);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	while (getextmntent(mt, &m, sizeof (struct extmnttab)) == 0) {
10410Sstevel@tonic-gate 		/* ignore non "nfs" and save the "ignore" entries */
10420Sstevel@tonic-gate 		if (strcmp(m.mnt_fstype, MNTTYPE_NFS) != 0)
10430Sstevel@tonic-gate 			continue;
10440Sstevel@tonic-gate 		/*
10450Sstevel@tonic-gate 		 * Check to see here if user gave a path(s) to
10460Sstevel@tonic-gate 		 * only show the mount point they wanted
10470Sstevel@tonic-gate 		 * Iterate through the list of paths the user gave and see
1048*5302Sth199096 		 * if any of them match our current nfs mount
10490Sstevel@tonic-gate 		 */
10500Sstevel@tonic-gate 		if (path[0] != NULL) {
10510Sstevel@tonic-gate 			found = 0;
10520Sstevel@tonic-gate 			for (path_count = 0; path[path_count] != NULL;
10530Sstevel@tonic-gate 			    path_count++) {
10540Sstevel@tonic-gate 				if (strcmp(path[path_count], m.mnt_mountp)
10550Sstevel@tonic-gate 				    == 0) {
10560Sstevel@tonic-gate 					found = 1;
10570Sstevel@tonic-gate 					break;
10580Sstevel@tonic-gate 				}
10590Sstevel@tonic-gate 			}
10600Sstevel@tonic-gate 			if (!found)
10610Sstevel@tonic-gate 				continue;
10620Sstevel@tonic-gate 		}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 		if ((mrp = malloc(sizeof (struct myrec))) == 0) {
10650Sstevel@tonic-gate 			fprintf(stderr, "nfsstat: not enough memory\n");
10660Sstevel@tonic-gate 			exit(1);
10670Sstevel@tonic-gate 		}
10680Sstevel@tonic-gate 		mrp->my_fsid = makedev(m.mnt_major, m.mnt_minor);
10690Sstevel@tonic-gate 		if (ignore(m.mnt_mntopts)) {
10700Sstevel@tonic-gate 			/*
10710Sstevel@tonic-gate 			 * ignored entries cannot be ignored for this
10720Sstevel@tonic-gate 			 * option. We have to display the info for this
10730Sstevel@tonic-gate 			 * nfs mount. The ignore is an indication
10740Sstevel@tonic-gate 			 * that the actual mount point is different and
10750Sstevel@tonic-gate 			 * something is in between the nfs mount.
10760Sstevel@tonic-gate 			 * So save the mount point now
10770Sstevel@tonic-gate 			 */
10780Sstevel@tonic-gate 			if ((mrp->ig_path = malloc(
1079*5302Sth199096 			    strlen(m.mnt_mountp) + 1)) == 0) {
10800Sstevel@tonic-gate 				fprintf(stderr, "nfsstat: not enough memory\n");
10810Sstevel@tonic-gate 				exit(1);
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 			(void) strcpy(mrp->ig_path, m.mnt_mountp);
10840Sstevel@tonic-gate 			ignored++;
10850Sstevel@tonic-gate 		} else {
10860Sstevel@tonic-gate 			mrp->ig_path = 0;
10870Sstevel@tonic-gate 			(void) strcpy(mrp->my_dir, m.mnt_mountp);
10880Sstevel@tonic-gate 		}
10890Sstevel@tonic-gate 		if ((mrp->my_path = strdup(m.mnt_special)) == NULL) {
10900Sstevel@tonic-gate 			fprintf(stderr, "nfsstat: not enough memory\n");
10910Sstevel@tonic-gate 			exit(1);
10920Sstevel@tonic-gate 		}
10930Sstevel@tonic-gate 		mrp->next = list;
10940Sstevel@tonic-gate 		list = mrp;
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/*
10980Sstevel@tonic-gate 	 * If something got ignored, go to the beginning of the mnttab
10990Sstevel@tonic-gate 	 * and look for the cachefs entries since they are the one
11000Sstevel@tonic-gate 	 * causing this. The mount point saved for the ignored entries
11010Sstevel@tonic-gate 	 * is matched against the special to get the actual mount point.
11020Sstevel@tonic-gate 	 * We are interested in the acutal mount point so that the output
11030Sstevel@tonic-gate 	 * look nice too.
11040Sstevel@tonic-gate 	 */
11050Sstevel@tonic-gate 	if (ignored) {
11060Sstevel@tonic-gate 		rewind(mt);
11070Sstevel@tonic-gate 		resetmnttab(mt);
11080Sstevel@tonic-gate 		while (getextmntent(mt, &m, sizeof (struct extmnttab)) == 0) {
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 			/* ignore non "cachefs" */
11110Sstevel@tonic-gate 			if (strcmp(m.mnt_fstype, MNTTYPE_CACHEFS) != 0)
11120Sstevel@tonic-gate 				continue;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 			for (mrp = list; mrp; mrp = mrp->next) {
11150Sstevel@tonic-gate 				if (mrp->ig_path == 0)
11160Sstevel@tonic-gate 					continue;
11170Sstevel@tonic-gate 				if (strcmp(mrp->ig_path, m.mnt_special) == 0) {
11180Sstevel@tonic-gate 					mrp->ig_path = 0;
11190Sstevel@tonic-gate 					(void) strcpy(mrp->my_dir,
1120*5302Sth199096 					    m.mnt_mountp);
11210Sstevel@tonic-gate 				}
11220Sstevel@tonic-gate 			}
11230Sstevel@tonic-gate 		}
11240Sstevel@tonic-gate 		/*
11250Sstevel@tonic-gate 		 * Now ignored entries which do not have
11260Sstevel@tonic-gate 		 * the my_dir initialized are really ignored; This never
11270Sstevel@tonic-gate 		 * happens unless the mnttab is corrupted.
11280Sstevel@tonic-gate 		 */
11290Sstevel@tonic-gate 		for (pmrp = 0, mrp = list; mrp; mrp = mrp->next) {
11300Sstevel@tonic-gate 			if (mrp->ig_path == 0)
11310Sstevel@tonic-gate 				pmrp = mrp;
11320Sstevel@tonic-gate 			else if (pmrp)
11330Sstevel@tonic-gate 				pmrp->next = mrp->next;
11340Sstevel@tonic-gate 			else
11350Sstevel@tonic-gate 				list = mrp->next;
11360Sstevel@tonic-gate 		}
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	(void) fclose(mt);
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
11430Sstevel@tonic-gate 		int i;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_RAW)
11460Sstevel@tonic-gate 			continue;
11470Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "nfs") != 0)
11480Sstevel@tonic-gate 			continue;
11490Sstevel@tonic-gate 		if (strcmp(ksp->ks_name, "mntinfo") != 0)
11500Sstevel@tonic-gate 			continue;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 		for (mrp = list; mrp; mrp = mrp->next) {
11530Sstevel@tonic-gate 			if ((mrp->my_fsid & MAXMIN) == ksp->ks_instance)
11540Sstevel@tonic-gate 				break;
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 		if (mrp == 0)
11570Sstevel@tonic-gate 			continue;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		if (safe_kstat_read(kc, ksp, &mik) == -1)
11600Sstevel@tonic-gate 			continue;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 		printf("%s from %s\n", mrp->my_dir, mrp->my_path);
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 		/*
11650Sstevel@tonic-gate 		 * for printing rdma transport and provider string.
11660Sstevel@tonic-gate 		 * This way we avoid modifying the kernel mntinfo_kstat
11670Sstevel@tonic-gate 		 * struct for protofmly.
11680Sstevel@tonic-gate 		 */
11690Sstevel@tonic-gate 		if (strcmp(mik.mik_proto, "ibtf") == 0) {
11700Sstevel@tonic-gate 			printf(" Flags:		vers=%u,proto=rdma",
11710Sstevel@tonic-gate 			    mik.mik_vers);
11720Sstevel@tonic-gate 			transport_flag = 1;
11730Sstevel@tonic-gate 		} else {
11740Sstevel@tonic-gate 			printf(" Flags:		vers=%u,proto=%s",
11750Sstevel@tonic-gate 			    mik.mik_vers, mik.mik_proto);
11760Sstevel@tonic-gate 			transport_flag = 0;
11770Sstevel@tonic-gate 		}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 		/*
11800Sstevel@tonic-gate 		 *  get the secmode name from /etc/nfssec.conf.
11810Sstevel@tonic-gate 		 */
11820Sstevel@tonic-gate 		if (!nfs_getseconfig_bynumber(mik.mik_secmod, &nfs_sec)) {
11830Sstevel@tonic-gate 			flavor = nfs_sec.sc_name;
11840Sstevel@tonic-gate 		} else
11850Sstevel@tonic-gate 			flavor = NULL;
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 		if (flavor != NULL)
11880Sstevel@tonic-gate 			printf(",sec=%s", flavor);
11890Sstevel@tonic-gate 		else
11900Sstevel@tonic-gate 			printf(",sec#=%d", mik.mik_secmod);
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 		printf(",%s", (mik.mik_flags & MI_HARD) ? "hard" : "soft");
11930Sstevel@tonic-gate 		if (mik.mik_flags & MI_PRINTED)
11940Sstevel@tonic-gate 			printf(",printed");
11950Sstevel@tonic-gate 		printf(",%s", (mik.mik_flags & MI_INT) ? "intr" : "nointr");
11960Sstevel@tonic-gate 		if (mik.mik_flags & MI_DOWN)
11970Sstevel@tonic-gate 			printf(",down");
11980Sstevel@tonic-gate 		if (mik.mik_flags & MI_NOAC)
11990Sstevel@tonic-gate 			printf(",noac");
12000Sstevel@tonic-gate 		if (mik.mik_flags & MI_NOCTO)
12010Sstevel@tonic-gate 			printf(",nocto");
12020Sstevel@tonic-gate 		if (mik.mik_flags & MI_DYNAMIC)
12030Sstevel@tonic-gate 			printf(",dynamic");
12040Sstevel@tonic-gate 		if (mik.mik_flags & MI_LLOCK)
12050Sstevel@tonic-gate 			printf(",llock");
12060Sstevel@tonic-gate 		if (mik.mik_flags & MI_GRPID)
12070Sstevel@tonic-gate 			printf(",grpid");
12080Sstevel@tonic-gate 		if (mik.mik_flags & MI_RPCTIMESYNC)
12090Sstevel@tonic-gate 			printf(",rpctimesync");
12100Sstevel@tonic-gate 		if (mik.mik_flags & MI_LINK)
12110Sstevel@tonic-gate 			printf(",link");
12120Sstevel@tonic-gate 		if (mik.mik_flags & MI_SYMLINK)
12130Sstevel@tonic-gate 			printf(",symlink");
12140Sstevel@tonic-gate 		if (mik.mik_flags & MI_READDIRONLY)
12150Sstevel@tonic-gate 			printf(",readdironly");
12160Sstevel@tonic-gate 		if (mik.mik_flags & MI_ACL)
12170Sstevel@tonic-gate 			printf(",acl");
12180Sstevel@tonic-gate 
1219*5302Sth199096 		if (mik.mik_vers >= NFS_V4) {
1220*5302Sth199096 			if (mik.mik_flags & MI4_MIRRORMOUNT)
1221*5302Sth199096 				printf(",mirrormount");
1222*5302Sth199096 		}
1223*5302Sth199096 
12240Sstevel@tonic-gate 		printf(",rsize=%d,wsize=%d,retrans=%d,timeo=%d",
1225*5302Sth199096 		    mik.mik_curread, mik.mik_curwrite, mik.mik_retrans,
1226*5302Sth199096 		    mik.mik_timeo);
12270Sstevel@tonic-gate 		printf("\n");
12280Sstevel@tonic-gate 		printf(" Attr cache:	acregmin=%d,acregmax=%d"
1229*5302Sth199096 		    ",acdirmin=%d,acdirmax=%d\n", mik.mik_acregmin,
1230*5302Sth199096 		    mik.mik_acregmax, mik.mik_acdirmin, mik.mik_acdirmax);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 		if (transport_flag) {
12330Sstevel@tonic-gate 			printf(" Transport:	proto=rdma, plugin=%s\n",
12340Sstevel@tonic-gate 			    mik.mik_proto);
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate #define	srtt_to_ms(x) x, (x * 2 + x / 2)
12380Sstevel@tonic-gate #define	dev_to_ms(x) x, (x * 5)
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 		for (i = 0; i < NFS_CALLTYPES + 1; i++) {
12410Sstevel@tonic-gate 			int j;
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 			j = (i == NFS_CALLTYPES ? i - 1 : i);
12440Sstevel@tonic-gate 			if (mik.mik_timers[j].srtt ||
12450Sstevel@tonic-gate 			    mik.mik_timers[j].rtxcur) {
1246*5302Sth199096 				printf(" %s:     srtt=%d (%dms), "
1247*5302Sth199096 				    "dev=%d (%dms), cur=%u (%ums)\n",
1248*5302Sth199096 				    timer_name[i],
1249*5302Sth199096 				    srtt_to_ms(mik.mik_timers[i].srtt),
1250*5302Sth199096 				    dev_to_ms(mik.mik_timers[i].deviate),
1251*5302Sth199096 				    mik.mik_timers[i].rtxcur,
1252*5302Sth199096 				    mik.mik_timers[i].rtxcur * 20);
12530Sstevel@tonic-gate 			}
12540Sstevel@tonic-gate 		}
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 		if (strchr(mrp->my_path, ','))
12570Sstevel@tonic-gate 			printf(
12580Sstevel@tonic-gate 			    " Failover:	noresponse=%d,failover=%d,"
12590Sstevel@tonic-gate 			    "remap=%d,currserver=%s\n",
12600Sstevel@tonic-gate 			    mik.mik_noresponse, mik.mik_failover,
12610Sstevel@tonic-gate 			    mik.mik_remap, mik.mik_curserver);
12620Sstevel@tonic-gate 		printf("\n");
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate 
12660Sstevel@tonic-gate static char *mntopts[] = { MNTOPT_IGNORE, MNTOPT_DEV, NULL };
12670Sstevel@tonic-gate #define	IGNORE  0
12680Sstevel@tonic-gate #define	DEV	1
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate /*
12710Sstevel@tonic-gate  * Return 1 if "ignore" appears in the options string
12720Sstevel@tonic-gate  */
12730Sstevel@tonic-gate static int
12740Sstevel@tonic-gate ignore(char *opts)
12750Sstevel@tonic-gate {
12760Sstevel@tonic-gate 	char *value;
12770Sstevel@tonic-gate 	char *s;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	if (opts == NULL)
12800Sstevel@tonic-gate 		return (0);
12810Sstevel@tonic-gate 	s = strdup(opts);
12820Sstevel@tonic-gate 	if (s == NULL)
12830Sstevel@tonic-gate 		return (0);
12840Sstevel@tonic-gate 	opts = s;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	while (*opts != '\0') {
12870Sstevel@tonic-gate 		if (getsubopt(&opts, mntopts, &value) == IGNORE) {
12880Sstevel@tonic-gate 			free(s);
12890Sstevel@tonic-gate 			return (1);
12900Sstevel@tonic-gate 		}
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	free(s);
12940Sstevel@tonic-gate 	return (0);
12950Sstevel@tonic-gate }
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate void
12980Sstevel@tonic-gate usage(void)
12990Sstevel@tonic-gate {
13000Sstevel@tonic-gate 	fprintf(stderr, "Usage: nfsstat [-cnrsza [-v version] "
13010Sstevel@tonic-gate 	    "[interval [count]]\n");
13020Sstevel@tonic-gate 	fprintf(stderr, "Usage: nfsstat -m [pathname..]\n");
13030Sstevel@tonic-gate 	exit(1);
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate static void
13070Sstevel@tonic-gate fail(int do_perror, char *message, ...)
13080Sstevel@tonic-gate {
13090Sstevel@tonic-gate 	va_list args;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	va_start(args, message);
13120Sstevel@tonic-gate 	fprintf(stderr, "nfsstat: ");
13130Sstevel@tonic-gate 	vfprintf(stderr, message, args);
13140Sstevel@tonic-gate 	va_end(args);
13150Sstevel@tonic-gate 	if (do_perror)
13160Sstevel@tonic-gate 		fprintf(stderr, ": %s", strerror(errno));
13170Sstevel@tonic-gate 	fprintf(stderr, "\n");
13180Sstevel@tonic-gate 	exit(1);
13190Sstevel@tonic-gate }
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate kid_t
13220Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
13230Sstevel@tonic-gate {
13240Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	if (kstat_chain_id == -1)
13270Sstevel@tonic-gate 		fail(1, "kstat_read(%x, '%s') failed", kc, ksp->ks_name);
13280Sstevel@tonic-gate 	return (kstat_chain_id);
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate kid_t
13320Sstevel@tonic-gate safe_kstat_write(kstat_ctl_t *kc, kstat_t *ksp, void *data)
13330Sstevel@tonic-gate {
13340Sstevel@tonic-gate 	kid_t kstat_chain_id = 0;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	if (ksp->ks_data != NULL) {
13370Sstevel@tonic-gate 		kstat_chain_id = kstat_write(kc, ksp, data);
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		if (kstat_chain_id == -1)
13400Sstevel@tonic-gate 			fail(1, "kstat_write(%x, '%s') failed", kc,
13410Sstevel@tonic-gate 			    ksp->ks_name);
13420Sstevel@tonic-gate 	}
13430Sstevel@tonic-gate 	return (kstat_chain_id);
13440Sstevel@tonic-gate }
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate void
13470Sstevel@tonic-gate stats_timer(int interval)
13480Sstevel@tonic-gate {
13490Sstevel@tonic-gate 	timer_t t_id;
13500Sstevel@tonic-gate 	itimerspec_t time_struct;
13510Sstevel@tonic-gate 	struct sigevent sig_struct;
13520Sstevel@tonic-gate 	struct sigaction act;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	bzero(&sig_struct, sizeof (struct sigevent));
13550Sstevel@tonic-gate 	bzero(&act, sizeof (struct sigaction));
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	/* Create timer */
13580Sstevel@tonic-gate 	sig_struct.sigev_notify = SIGEV_SIGNAL;
13590Sstevel@tonic-gate 	sig_struct.sigev_signo = SIGUSR1;
13600Sstevel@tonic-gate 	sig_struct.sigev_value.sival_int = 0;
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	if (timer_create(CLOCK_REALTIME, &sig_struct, &t_id) != 0) {
13630Sstevel@tonic-gate 		fail(1, "Timer creation failed");
13640Sstevel@tonic-gate 	}
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	act.sa_handler = handle_sig;
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	if (sigaction(SIGUSR1, &act, NULL) != 0) {
13690Sstevel@tonic-gate 		fail(1, "Could not set up signal handler");
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	time_struct.it_value.tv_sec = interval;
13730Sstevel@tonic-gate 	time_struct.it_value.tv_nsec = 0;
13740Sstevel@tonic-gate 	time_struct.it_interval.tv_sec = interval;
13750Sstevel@tonic-gate 	time_struct.it_interval.tv_nsec = 0;
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	/* Arm timer */
13780Sstevel@tonic-gate 	if ((timer_settime(t_id, 0, &time_struct, NULL)) != 0) {
13790Sstevel@tonic-gate 		fail(1, "Setting timer failed");
13800Sstevel@tonic-gate 	}
13810Sstevel@tonic-gate }
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate void
13840Sstevel@tonic-gate handle_sig(int x)
13850Sstevel@tonic-gate {
13860Sstevel@tonic-gate }
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate static void
13890Sstevel@tonic-gate kstat_copy(kstat_t *src, kstat_t *dst, int fr)
13900Sstevel@tonic-gate {
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate 	if (fr)
13930Sstevel@tonic-gate 		free(dst->ks_data);
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 	*dst = *src;
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	if (src->ks_data != NULL) {
13980Sstevel@tonic-gate 		safe_zalloc(&dst->ks_data, src->ks_data_size, 0);
13990Sstevel@tonic-gate 		(void) memcpy(dst->ks_data, src->ks_data, src->ks_data_size);
14000Sstevel@tonic-gate 	} else {
14010Sstevel@tonic-gate 		dst->ks_data = NULL;
14020Sstevel@tonic-gate 		dst->ks_data_size = 0;
14030Sstevel@tonic-gate 	}
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate /*
14070Sstevel@tonic-gate  * "Safe" allocators - if we return we're guaranteed
14080Sstevel@tonic-gate  * to have the desired space. We exit via fail
14090Sstevel@tonic-gate  * if we can't get the space.
14100Sstevel@tonic-gate  */
14110Sstevel@tonic-gate void
14120Sstevel@tonic-gate safe_zalloc(void **ptr, uint_t size, int free_first)
14130Sstevel@tonic-gate {
14140Sstevel@tonic-gate 	if (*ptr == NULL)
14150Sstevel@tonic-gate 		fail(1, "invalid pointer");
14160Sstevel@tonic-gate 	if (free_first && *ptr != NULL)
14170Sstevel@tonic-gate 		free(*ptr);
14180Sstevel@tonic-gate 	if ((*ptr = (void *)malloc(size)) == NULL)
14190Sstevel@tonic-gate 		fail(1, "malloc failed");
14200Sstevel@tonic-gate 	(void) memset(*ptr, 0, size);
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate static int
14240Sstevel@tonic-gate safe_strtoi(char const *val, char *errmsg)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	char *end;
14270Sstevel@tonic-gate 	long tmp;
14280Sstevel@tonic-gate 	errno = 0;
14290Sstevel@tonic-gate 	tmp = strtol(val, &end, 10);
14300Sstevel@tonic-gate 	if (*end != '\0' || errno)
14310Sstevel@tonic-gate 		fail(0, "%s %s", errmsg, val);
14320Sstevel@tonic-gate 	return ((int)tmp);
14330Sstevel@tonic-gate }
1434