xref: /titanic_44/usr/src/cmd/stat/fsstat/fsstat.c (revision cee8668251d5ec44fd1c6d6ddeb9c1d1821a57d2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <kstat.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <sys/types.h>
36 #include <time.h>
37 #include <langinfo.h>
38 #include <sys/time.h>
39 #include <sys/uio.h>
40 #include <sys/vnode.h>
41 #include <sys/vfs.h>
42 #include <sys/statvfs.h>
43 #include <sys/fstyp.h>
44 #include <sys/fsid.h>
45 #include <sys/mnttab.h>
46 #include <values.h>
47 #include <poll.h>
48 #include <ctype.h>
49 #include <libintl.h>
50 #include <locale.h>
51 
52 /*
53  * For now, parsable output is turned off.  Once we gather feedback and
54  * stablize the output format, we'll turn it back on.  This prevents
55  * the situation where users build tools which depend on a specific
56  * format before we declare the output stable.
57  */
58 #define	PARSABLE_OUTPUT	0
59 
60 #if PARSABLE_OUTPUT
61 #define	OPTIONS	"FPT:afginv"
62 #else
63 #define	OPTIONS	"FT:afginv"
64 #endif
65 
66 /* Time stamp values */
67 #define	NODATE	0	/* Default:  No time stamp */
68 #define	DDATE	1	/* Standard date format */
69 #define	UDATE	2	/* Internal representation of Unix time */
70 
71 #define	RETRY_DELAY	250	/* Timeout for poll() */
72 #define	HEADERLINES	12	/* Number of lines between display headers */
73 
74 #define	LBUFSZ		64	/* Generic size for local buffer */
75 
76 /*
77  * The following are used for the nicenum() function
78  */
79 #define	KILO_VAL	1024
80 #define	ONE_INDEX	3
81 
82 #define	NENTITY_INIT	1	/* Initial number of entities to allocate */
83 
84 /*
85  * We need to have a mechanism for an old/previous and new/current vopstat
86  * structure.  We only need two per entity and we can swap between them.
87  */
88 #define	VS_SIZE	2	/* Size of vopstat array */
89 #define	CUR_INDEX	(vs_i)
90 #define	PREV_INDEX	((vs_i == 0) ? 1 : 0)	/* Opposite of CUR_INDEX */
91 #define	BUMP_INDEX()	vs_i = ((vs_i == 0) ? 1 : 0)
92 
93 /*
94  * An "entity" is anything we're collecting statistics on, it could
95  * be a mountpoint or an FS-type.
96  * e_name is the name of the entity (e.g. mount point or FS-type)
97  * e_ksname is the name of the associated kstat
98  * e_vs is an array of vopstats.  This is used to keep track of "previous"
99  * and "current" vopstats.
100  */
101 typedef struct entity {
102 	char		*e_name;		/* name of entity */
103 	vopstats_t	*e_vs;			/* Array of vopstats */
104 	ulong_t		e_fsid;			/* fsid for ENTYPE_MNTPT only */
105 	int		e_type;			/* type of entity */
106 	char		e_ksname[KSTAT_STRLEN];	/* kstat name */
107 } entity_t;
108 
109 /* Types of entities (e_type) */
110 #define	ENTYPE_UNKNOWN	0	/* UNKNOWN must be zero since we calloc() */
111 #define	ENTYPE_FSTYPE	1
112 #define	ENTYPE_MNTPT	2
113 
114 /* If more sub-one units are added, make sure to adjust ONE_INDEX above */
115 static char units[] = "num KMGTPE";
116 
117 static char	*cmdname;	/* name of this command */
118 
119 static int	vs_i = 0;	/* Index of current vs[] slot */
120 
121 static void
122 usage()
123 {
124 	(void) fprintf(stderr, gettext(
125 	    "Usage: %s [-a|f|i|n|v] [-T d|u] {-F | {fstype | fspath}...} "
126 	    "[interval [count]]\n"), cmdname);
127 	exit(2);
128 }
129 
130 /*
131  * Given a 64-bit number and a starting unit (e.g., n - nanoseconds),
132  * convert the number to a 5-character representation including any
133  * decimal point and single-character unit.  Put that representation
134  * into the array "buf" (which had better be big enough).
135  */
136 char *
137 nicenum(uint64_t num, char unit, char *buf)
138 {
139 	uint64_t n = num;
140 	int unit_index;
141 	int index;
142 	char u;
143 
144 	/* If the user passed in a NUL/zero unit, use the blank value for 1 */
145 	if (unit == '\0')
146 		unit = ' ';
147 
148 	unit_index = 0;
149 	while (units[unit_index] != unit) {
150 		unit_index++;
151 		if (unit_index > sizeof (units) - 1) {
152 			(void) sprintf(buf, "??");
153 			return (buf);
154 		}
155 	}
156 
157 	index = 0;
158 	while (n >= KILO_VAL) {
159 		n = (n + (KILO_VAL / 2)) / KILO_VAL; /* Round up or down */
160 		index++;
161 		unit_index++;
162 	}
163 
164 	if (unit_index >= sizeof (units) - 1) {
165 		(void) sprintf(buf, "??");
166 		return (buf);
167 	}
168 
169 	u = units[unit_index];
170 
171 	if (unit_index == ONE_INDEX) {
172 		(void) sprintf(buf, "%llu", (u_longlong_t)n);
173 	} else if (n < 10 && (num & (num - 1)) != 0) {
174 		(void) sprintf(buf, "%.2f%c",
175 		    (double)num / (1ULL << 10 * index), u);
176 	} else if (n < 100 && (num & (num - 1)) != 0) {
177 		(void) sprintf(buf, "%.1f%c",
178 		    (double)num / (1ULL << 10 * index), u);
179 	} else {
180 		(void) sprintf(buf, "%llu%c", (u_longlong_t)n, u);
181 	}
182 
183 	return (buf);
184 }
185 
186 
187 #define	RAWVAL(ptr, member) ((ptr)->member.value.ui64)
188 #define	DELTA(member)	\
189 	(newvsp->member.value.ui64 - (oldvsp ? oldvsp->member.value.ui64 : 0))
190 
191 #define	PRINTSTAT(isnice, nicestring, rawstring, rawval, unit, buf)	\
192 	(isnice) ?	 						\
193 		(void) printf((nicestring), nicenum(rawval, unit, buf))	\
194 	:								\
195 		(void) printf((rawstring), (rawval))
196 
197 /* Values for display flag */
198 #define	DISP_HEADER	0x1
199 #define	DISP_RAW	0x2
200 
201 /*
202  * The policy for dealing with multiple flags is dealt with here.
203  * Currently, if we are displaying raw output, then don't allow
204  * headers to be printed.
205  */
206 int
207 dispflag_policy(int printhdr, int dispflag)
208 {
209 	/* If we're not displaying raw output, then allow headers to print */
210 	if ((dispflag & DISP_RAW) == 0) {
211 		if (printhdr) {
212 			dispflag |= DISP_HEADER;
213 		}
214 	}
215 
216 	return (dispflag);
217 }
218 
219 static void
220 dflt_display(char *name, vopstats_t *oldvsp, vopstats_t *newvsp, int dispflag)
221 {
222 	int		niceflag = ((dispflag & DISP_RAW) == 0);
223 	longlong_t	nnewfile;
224 	longlong_t	nnamerm;
225 	longlong_t	nnamechg;
226 	longlong_t	nattrret;
227 	longlong_t	nattrchg;
228 	longlong_t	nlookup;
229 	longlong_t	nreaddir;
230 	longlong_t	ndataread;
231 	longlong_t	ndatawrite;
232 	longlong_t	readthruput;
233 	longlong_t	writethruput;
234 	char		buf[LBUFSZ];
235 
236 	nnewfile = DELTA(ncreate) + DELTA(nmkdir) + DELTA(nsymlink);
237 	nnamerm = DELTA(nremove) + DELTA(nrmdir);
238 	nnamechg = DELTA(nrename) + DELTA(nlink) + DELTA(nsymlink);
239 	nattrret = DELTA(ngetattr) + DELTA(naccess) +
240 				DELTA(ngetsecattr) + DELTA(nfid);
241 	nattrchg = DELTA(nsetattr) + DELTA(nsetsecattr) + DELTA(nspace);
242 	nlookup = DELTA(nlookup);
243 	nreaddir = DELTA(nreaddir);
244 	ndataread = DELTA(nread);
245 	ndatawrite = DELTA(nwrite);
246 	readthruput = DELTA(read_bytes);
247 	writethruput = DELTA(write_bytes);
248 
249 	if (dispflag & DISP_HEADER) {
250 		(void) printf(gettext(
251 " new  name   name  attr  attr lookup rddir  read read  write write\n"
252 " file remov  chng   get   set    ops   ops   ops bytes   ops bytes\n"));
253 	}
254 
255 	PRINTSTAT(niceflag, "%5s ", "%lld:", nnewfile, ' ', buf);
256 	PRINTSTAT(niceflag, "%5s ", "%lld:", nnamerm, ' ', buf);
257 	PRINTSTAT(niceflag, "%5s ", "%lld:", nnamechg, ' ', buf);
258 	PRINTSTAT(niceflag, "%5s ", "%lld:", nattrret, ' ', buf);
259 	PRINTSTAT(niceflag, "%5s ", "%lld:", nattrchg, ' ', buf);
260 	PRINTSTAT(niceflag, " %5s ", "%lld:", nlookup, ' ', buf);
261 	PRINTSTAT(niceflag, "%5s ", "%lld:", nreaddir, ' ', buf);
262 	PRINTSTAT(niceflag, "%5s ", "%lld:", ndataread, ' ', buf);
263 	PRINTSTAT(niceflag, "%5s ", "%lld:", readthruput, ' ', buf);
264 	PRINTSTAT(niceflag, "%5s ", "%lld:", ndatawrite, ' ', buf);
265 	PRINTSTAT(niceflag, "%5s ", "%lld:", writethruput, ' ', buf);
266 	(void) printf("%s\n", name);
267 }
268 
269 static void
270 io_display(char *name, vopstats_t *oldvsp, vopstats_t *newvsp, int dispflag)
271 {
272 	int		niceflag = ((dispflag & DISP_RAW) == 0);
273 	char		buf[LBUFSZ];
274 
275 	if (dispflag & DISP_HEADER) {
276 		(void) printf(gettext(
277 " read read  write write rddir rddir rwlock rwulock\n"
278 "  ops bytes   ops bytes   ops bytes    ops     ops\n"));
279 	}
280 
281 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nread), ' ', buf);
282 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(read_bytes), ' ', buf);
283 
284 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nwrite), ' ', buf);
285 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(write_bytes), ' ', buf);
286 
287 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nreaddir), ' ', buf);
288 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(readdir_bytes), ' ', buf);
289 
290 	PRINTSTAT(niceflag, " %5s   ", "%lld:", DELTA(nrwlock), ' ', buf);
291 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nrwunlock), ' ', buf);
292 
293 	(void) printf("%s\n", name);
294 }
295 
296 static void
297 vm_display(char *name, vopstats_t *oldvsp, vopstats_t *newvsp, int dispflag)
298 {
299 	int		niceflag = ((dispflag & DISP_RAW) == 0);
300 	char		buf[LBUFSZ];
301 
302 	if (dispflag & DISP_HEADER) {
303 		(void) printf(
304 		    gettext("  map addmap delmap getpag putpag pagio\n"));
305 	}
306 
307 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nmap), ' ', buf);
308 	PRINTSTAT(niceflag, " %5s ", "%lld:", DELTA(naddmap), ' ', buf);
309 	PRINTSTAT(niceflag, " %5s ", "%lld:", DELTA(ndelmap), ' ', buf);
310 	PRINTSTAT(niceflag, " %5s ", "%lld:", DELTA(ngetpage), ' ', buf);
311 	PRINTSTAT(niceflag, " %5s ", "%lld:", DELTA(nputpage), ' ', buf);
312 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(npageio), ' ', buf);
313 	(void) printf("%s\n", name);
314 }
315 
316 static void
317 attr_display(char *name, vopstats_t *oldvsp, vopstats_t *newvsp, int dispflag)
318 {
319 	int		niceflag = ((dispflag & DISP_RAW) == 0);
320 	char		buf[LBUFSZ];
321 
322 	if (dispflag & DISP_HEADER) {
323 		(void) printf(gettext("getattr setattr getsec  setsec\n"));
324 	}
325 
326 	PRINTSTAT(niceflag, " %5s ", "%lld:", DELTA(ngetattr), ' ', buf);
327 	PRINTSTAT(niceflag, "  %5s ", "%lld:", DELTA(nsetattr), ' ', buf);
328 	PRINTSTAT(niceflag, "  %5s ", "%lld:", DELTA(ngetsecattr), ' ', buf);
329 	PRINTSTAT(niceflag, "  %5s ", "%lld:", DELTA(nsetsecattr), ' ', buf);
330 
331 	(void) printf("%s\n", name);
332 }
333 
334 static void
335 naming_display(char *name, vopstats_t *oldvsp, vopstats_t *newvsp, int dispflag)
336 {
337 	int		niceflag = ((dispflag & DISP_RAW) == 0);
338 	char		buf[LBUFSZ];
339 
340 	if (dispflag & DISP_HEADER) {
341 		(void) printf(gettext(
342 	"lookup creat remov  link renam mkdir rmdir rddir symlnk rdlnk\n"));
343 	}
344 
345 	PRINTSTAT(niceflag, "%5s  ", "%lld:", DELTA(nlookup), ' ', buf);
346 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(ncreate), ' ', buf);
347 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nremove), ' ', buf);
348 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nlink), ' ', buf);
349 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nrename), ' ', buf);
350 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nmkdir), ' ', buf);
351 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nrmdir), ' ', buf);
352 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nreaddir), ' ', buf);
353 	PRINTSTAT(niceflag, " %5s ", "%lld:", DELTA(nsymlink), ' ', buf);
354 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(nreadlink), ' ', buf);
355 	(void) printf("%s\n", name);
356 }
357 
358 
359 #define	PRINT_VOPSTAT_CMN(niceflag, vop)				\
360 	if (niceflag)							\
361 		(void) printf("%10s ", #vop);				\
362 	PRINTSTAT(niceflag, "%5s ", "%lld:", DELTA(n##vop), ' ', buf);
363 
364 #define	PRINT_VOPSTAT(niceflag, vop) 					\
365 	PRINT_VOPSTAT_CMN(niceflag, vop);				\
366 	if (niceflag)							\
367 		(void) printf("\n");
368 
369 #define	PRINT_VOPSTAT_IO(niceflag, vop)					\
370 	PRINT_VOPSTAT_CMN(niceflag, vop);				\
371 	PRINTSTAT(niceflag, " %5s\n", "%lld:",				\
372 		DELTA(vop##_bytes), ' ', buf);
373 
374 static void
375 vop_display(char *name, vopstats_t *oldvsp, vopstats_t *newvsp, int dispflag)
376 {
377 	int		niceflag = ((dispflag & DISP_RAW) == 0);
378 	char		buf[LBUFSZ];
379 
380 	if (niceflag) {
381 		(void) printf("%s\n", name);
382 		(void) printf(gettext(" operation  #ops  bytes\n"));
383 	}
384 
385 	PRINT_VOPSTAT(niceflag, open);
386 	PRINT_VOPSTAT(niceflag, close);
387 	PRINT_VOPSTAT_IO(niceflag, read);
388 	PRINT_VOPSTAT_IO(niceflag, write);
389 	PRINT_VOPSTAT(niceflag, ioctl);
390 	PRINT_VOPSTAT(niceflag, setfl);
391 	PRINT_VOPSTAT(niceflag, getattr);
392 	PRINT_VOPSTAT(niceflag, setattr);
393 	PRINT_VOPSTAT(niceflag, access);
394 	PRINT_VOPSTAT(niceflag, lookup);
395 	PRINT_VOPSTAT(niceflag, create);
396 	PRINT_VOPSTAT(niceflag, remove);
397 	PRINT_VOPSTAT(niceflag, link);
398 	PRINT_VOPSTAT(niceflag, rename);
399 	PRINT_VOPSTAT(niceflag, mkdir);
400 	PRINT_VOPSTAT(niceflag, rmdir);
401 	PRINT_VOPSTAT_IO(niceflag, readdir);
402 	PRINT_VOPSTAT(niceflag, symlink);
403 	PRINT_VOPSTAT(niceflag, readlink);
404 	PRINT_VOPSTAT(niceflag, fsync);
405 	PRINT_VOPSTAT(niceflag, inactive);
406 	PRINT_VOPSTAT(niceflag, fid);
407 	PRINT_VOPSTAT(niceflag, rwlock);
408 	PRINT_VOPSTAT(niceflag, rwunlock);
409 	PRINT_VOPSTAT(niceflag, seek);
410 	PRINT_VOPSTAT(niceflag, cmp);
411 	PRINT_VOPSTAT(niceflag, frlock);
412 	PRINT_VOPSTAT(niceflag, space);
413 	PRINT_VOPSTAT(niceflag, realvp);
414 	PRINT_VOPSTAT(niceflag, getpage);
415 	PRINT_VOPSTAT(niceflag, putpage);
416 	PRINT_VOPSTAT(niceflag, map);
417 	PRINT_VOPSTAT(niceflag, addmap);
418 	PRINT_VOPSTAT(niceflag, delmap);
419 	PRINT_VOPSTAT(niceflag, poll);
420 	PRINT_VOPSTAT(niceflag, dump);
421 	PRINT_VOPSTAT(niceflag, pathconf);
422 	PRINT_VOPSTAT(niceflag, pageio);
423 	PRINT_VOPSTAT(niceflag, dumpctl);
424 	PRINT_VOPSTAT(niceflag, dispose);
425 	PRINT_VOPSTAT(niceflag, getsecattr);
426 	PRINT_VOPSTAT(niceflag, setsecattr);
427 	PRINT_VOPSTAT(niceflag, shrlock);
428 	PRINT_VOPSTAT(niceflag, vnevent);
429 
430 	if (niceflag) {
431 		/* Make it easier on the eyes */
432 		(void) printf("\n");
433 	} else {
434 		(void) printf("%s\n", name);
435 	}
436 }
437 
438 
439 /*
440  * Retrieve the vopstats.  If kspp (pointer to kstat_t pointer) is non-NULL,
441  * then pass it back to the caller.
442  *
443  * Returns 0 on success, non-zero on failure.
444  */
445 int
446 get_vopstats(kstat_ctl_t *kc, char *ksname, vopstats_t *vsp, kstat_t **kspp)
447 {
448 	kstat_t		*ksp;
449 
450 	if (ksname == NULL || *ksname == 0)
451 		return (1);
452 
453 	errno = 0;
454 	/* wait for a possibly up-to-date chain */
455 	while (kstat_chain_update(kc) == -1) {
456 		if (errno == EAGAIN) {
457 			errno = 0;
458 			(void) poll(NULL, 0, RETRY_DELAY);
459 			continue;
460 		}
461 		perror(gettext("kstat_chain_update"));
462 		exit(1);
463 	}
464 
465 	if ((ksp = kstat_lookup(kc, NULL, -1, ksname)) == NULL) {
466 		return (1);
467 	}
468 
469 	if (kstat_read(kc, ksp, vsp) == -1) {
470 		return (1);
471 	}
472 
473 	if (kspp)
474 		*kspp = ksp;
475 
476 	return (0);
477 }
478 
479 /*
480  * Given a file system type name, determine if it's part of the
481  * exception list of file systems that are not to be displayed.
482  */
483 int
484 is_exception(char *fsname)
485 {
486 	char **xlp;	/* Pointer into the exception list */
487 
488 	static char *exception_list[] = {
489 		"specfs",
490 		"fifofs",
491 		"fd",
492 		"swapfs",
493 		"ctfs",
494 		"objfs",
495 		"nfsdyn",
496 		NULL
497 	};
498 
499 	for (xlp = &exception_list[0]; *xlp != NULL; xlp++) {
500 		if (strcmp(fsname, *xlp) == 0)
501 			return (1);
502 	}
503 
504 	return (0);
505 }
506 
507 /*
508  * Plain and simple, build an array of names for fstypes
509  * Returns 0, if it encounters a problem.
510  */
511 int
512 build_fstype_list(char ***fstypep)
513 {
514 	int	i;
515 	int	nfstype;
516 	char	buf[FSTYPSZ + 1];
517 
518 	if ((nfstype = sysfs(GETNFSTYP)) < 0) {
519 		perror(gettext("sysfs(GETNFSTYP)"));
520 		return (0);
521 	}
522 
523 	if ((*fstypep = calloc(nfstype, sizeof (char *))) == NULL) {
524 		perror(gettext("calloc on fstypes"));
525 		return (0);
526 	}
527 
528 	for (i = 1; i < nfstype; i++) {
529 		if (sysfs(GETFSTYP, i, buf) < 0) {
530 			perror(gettext("sysfs(GETFSTYP)"));
531 			return (0);
532 		}
533 
534 		if (buf[0] == 0)
535 			continue;
536 
537 		/* If this is part of the exception list, move on */
538 		if (is_exception(buf))
539 			continue;
540 
541 		if (((*fstypep)[i] = strdup(buf)) == NULL) {
542 			perror(gettext("strdup() of fstype name"));
543 			return (0);
544 		}
545 	}
546 
547 	return (i);
548 }
549 
550 /*
551  * After we're done with getopts(), process the rest of the
552  * operands.  We have three cases and this is the priority:
553  *
554  * 1) [ operand... ] interval count
555  * 2) [ operand... ] interval
556  * 3) [ operand... ]
557  *
558  * The trick is that any of the operands might start with a number or even
559  * be made up exclusively of numbers (and we have to handle negative numbers
560  * in case a user/script gets out of line).  If we find two operands at the
561  * end of the list then we claim case 1.  If we find only one operand at the
562  * end made up only of number, then we claim case 2.  Otherwise, case 3.
563  * BTW, argc, argv don't change.
564  */
565 int
566 parse_operands(
567 	int		argc,
568 	char		**argv,
569 	int		optind,
570 	long		*interval,
571 	long		*count,
572 	entity_t	**entityp)	/* Array of stat-able entities */
573 {
574 	int	nentities = 0;	/* Number of entities found */
575 	int	out_of_range;	/* Set if 2nd-to-last operand out-of-range */
576 
577 	if (argc == optind)
578 		return (nentities);	/* None found, returns 0 */
579 	/*
580 	 * We know exactly what the maximum number of entities is going
581 	 * to be:  argc - optind
582 	 */
583 	if ((*entityp = calloc((argc - optind), sizeof (entity_t))) == NULL) {
584 		perror(gettext("calloc"));
585 		return (-1);
586 	}
587 
588 	for (/* void */; argc > optind; optind++) {
589 		char	*endptr;
590 
591 		/* If we have more than two operands left to process */
592 		if ((argc - optind) > 2) {
593 			(*entityp)[nentities++].e_name = strdup(argv[optind]);
594 			continue;
595 		}
596 
597 		/* If we're here, then we only have one or two operands left */
598 		errno = 0;
599 		out_of_range = 0;
600 		*interval = strtol(argv[optind], &endptr, 10);
601 		if (*endptr && !isdigit((int)*endptr)) {
602 			/* Operand was not a number */
603 			(*entityp)[nentities++].e_name = strdup(argv[optind]);
604 			continue;
605 		} else if (errno == ERANGE || *interval <= 0 ||
606 							*interval > MAXLONG) {
607 			/* Operand was a number, just out of range */
608 			out_of_range++;
609 		}
610 
611 		/*
612 		 * The last operand we saw was a number.  If it happened to
613 		 * be the last operand, then it is the interval...
614 		 */
615 		if ((argc - optind) == 1) {
616 			/* ...but we need to check the range. */
617 			if (out_of_range) {
618 				(void) fprintf(stderr, gettext(
619 				    "interval must be between 1 and "
620 				    "%ld (inclusive)\n"), MAXLONG);
621 				return (-1);
622 			} else {
623 				/*
624 				 * The value of the interval is valid. Set
625 				 * count to something really big so it goes
626 				 * virtually forever.
627 				 */
628 				*count = MAXLONG;
629 				break;
630 			}
631 		}
632 
633 		/*
634 		 * At this point, we *might* have the interval, but if the
635 		 * next operand isn't a number, then we don't have either
636 		 * the interval nor the count.  Both must be set to the
637 		 * defaults.  In that case, both the current and the previous
638 		 * operands are stat-able entities.
639 		 */
640 		errno = 0;
641 		*count = strtol(argv[optind + 1], &endptr, 10);
642 		if (*endptr && !isdigit((int)*endptr)) {
643 			/*
644 			 * Faked out!  The last operand wasn't a number so
645 			 * the current and previous operands should be
646 			 * stat-able entities. We also need to reset interval.
647 			 */
648 			*interval = 0;
649 			(*entityp)[nentities++].e_name = strdup(argv[optind++]);
650 			(*entityp)[nentities++].e_name = strdup(argv[optind++]);
651 		} else if (out_of_range || errno == ERANGE || *count <= 0) {
652 			(void) fprintf(stderr, gettext(
653 			    "Both interval and count must be between 1 "
654 			    "and %ld (inclusive)\n"), MAXLONG);
655 			return (-1);
656 		}
657 		break;	/* Done! */
658 	}
659 	return (nentities);
660 }
661 
662 /*
663  * set_mntpt() looks at the entity's name (e_name) and finds its
664  * mountpoint.  To do this, we need to build a list of mountpoints
665  * from /etc/mnttab.  We only need to do this once and we don't do it
666  * if we don't need to look at any mountpoints.
667  * Returns 0 on success, non-zero if it couldn't find a mount-point.
668  */
669 int
670 set_mntpt(entity_t *ep)
671 {
672 	static struct mnt {
673 		struct mnt	*m_next;
674 		char		*m_mntpt;
675 		ulong_t		m_fsid;	/* From statvfs(), set only as needed */
676 	} *mnt_list = NULL;	/* Linked list of mount-points */
677 	struct mnt *mntp;
678 	struct statvfs statvfsbuf;
679 	char *original_name = ep->e_name;
680 	char path[PATH_MAX];
681 
682 	if (original_name == NULL)		/* Shouldn't happen */
683 		return (1);
684 
685 	/* We only set up mnt_list the first time this is called */
686 	if (mnt_list == NULL) {
687 		FILE *fp;
688 		struct mnttab mnttab;
689 
690 		if ((fp = fopen(MNTTAB, "r")) == NULL) {
691 			perror(MNTTAB);
692 			return (1);
693 		}
694 		resetmnttab(fp);
695 		/*
696 		 * We insert at the front of the list so that when we
697 		 * search entries we'll have the last mounted entries
698 		 * first in the list so that we can match the longest
699 		 * mountpoint.
700 		 */
701 		while (getmntent(fp, &mnttab) == 0) {
702 			if ((mntp = malloc(sizeof (*mntp))) == NULL) {
703 				perror(gettext("Can't create mount list"));
704 				return (1);
705 			}
706 			mntp->m_mntpt = strdup(mnttab.mnt_mountp);
707 			mntp->m_next = mnt_list;
708 			mnt_list = mntp;
709 		}
710 		(void) fclose(fp);
711 	}
712 
713 	if (realpath(original_name, path) == NULL) {
714 		perror(original_name);
715 		return (1);
716 	}
717 
718 	/*
719 	 * Now that we have the path, walk through the mnt_list and
720 	 * look for the first (best) match.
721 	 */
722 	for (mntp = mnt_list; mntp; mntp = mntp->m_next) {
723 		if (strncmp(path, mntp->m_mntpt, strlen(mntp->m_mntpt)) == 0) {
724 			if (mntp->m_fsid == 0) {
725 				if (statvfs(mntp->m_mntpt, &statvfsbuf)) {
726 					/* Can't statvfs so no match */
727 					continue;
728 				} else {
729 					mntp->m_fsid = statvfsbuf.f_fsid;
730 				}
731 			}
732 
733 			if (ep->e_fsid != mntp->m_fsid) {
734 				/* No match - Move on */
735 				continue;
736 			}
737 
738 			break;
739 		}
740 	}
741 
742 	if (mntp == NULL) {
743 		(void) fprintf(stderr, gettext(
744 		    "Can't find mount point for %s\n"), path);
745 		return (1);
746 	}
747 
748 	ep->e_name = strdup(mntp->m_mntpt);
749 	free(original_name);
750 	return (0);
751 }
752 
753 /*
754  * We have an array of entities that are potentially stat-able.  Using
755  * the name (e_name) of the entity, attempt to construct a ksname suitable
756  * for use by kstat_lookup(3kstat) and fill it into the e_ksname member.
757  *
758  * We check the e_name against the list of file system types.  If there is
759  * no match then test to see if the path is valid.  If the path is valid,
760  * then determine the mountpoint.
761  */
762 void
763 set_ksnames(entity_t *entities, int nentities, char **fstypes, int nfstypes)
764 {
765 	int		i, j;
766 	struct statvfs statvfsbuf;
767 
768 	for (i = 0; i < nentities; i++) {
769 		entity_t	*ep = &entities[i];
770 
771 		/* Check the name against the list of fstypes */
772 		for (j = 1; j < nfstypes; j++) {
773 			if (fstypes[j] && ep->e_name &&
774 			    strcmp(ep->e_name, fstypes[j]) == 0) {
775 				/* It's a file system type */
776 				ep->e_type = ENTYPE_FSTYPE;
777 				(void) snprintf(ep->e_ksname,
778 						KSTAT_STRLEN, "%s%s",
779 						VOPSTATS_STR, ep->e_name);
780 				/* Now allocate the vopstats array */
781 				ep->e_vs = calloc(VS_SIZE, sizeof (vopstats_t));
782 				if (entities[i].e_vs == NULL) {
783 					perror(gettext("calloc() vopstats"));
784 					exit(1);
785 				}
786 				break;
787 			}
788 		}
789 		if (j < nfstypes)	/* Found it! */
790 			continue;
791 
792 		/*
793 		 * If the entity in the exception list of fstypes, then
794 		 * null out the entry so it isn't displayed and move along.
795 		 */
796 		if (is_exception(ep->e_name)) {
797 			ep->e_ksname[0] = 0;
798 			continue;
799 		}
800 
801 		/* If we didn't find it, see if it's a path */
802 		if (ep->e_name == NULL || statvfs(ep->e_name, &statvfsbuf)) {
803 			/* Error - Make sure the entry is nulled out */
804 			ep->e_ksname[0] = 0;
805 			continue;
806 		}
807 		(void) snprintf(ep->e_ksname, KSTAT_STRLEN, "%s%lx",
808 		    VOPSTATS_STR, statvfsbuf.f_fsid);
809 		ep->e_fsid = statvfsbuf.f_fsid;
810 		if (set_mntpt(ep)) {
811 			(void) fprintf(stderr,
812 			    gettext("Can't determine type of \"%s\"\n"),
813 			    ep->e_name ? ep->e_name : gettext("<NULL>"));
814 		} else {
815 			ep->e_type = ENTYPE_MNTPT;
816 		}
817 
818 		/* Now allocate the vopstats array */
819 		ep->e_vs = calloc(VS_SIZE, sizeof (vopstats_t));
820 		if (entities[i].e_vs == NULL) {
821 			perror(gettext("Can't calloc vopstats"));
822 			exit(1);
823 		}
824 	}
825 }
826 
827 void
828 print_time(int type)
829 {
830 	time_t	t;
831 	static char *fmt = NULL;	/* Time format */
832 
833 	/* We only need to retrieve this once per invocation */
834 	if (fmt == NULL) {
835 		fmt = nl_langinfo(_DATE_FMT);
836 	}
837 
838 	if (time(&t) != -1) {
839 		if (type == UDATE) {
840 			(void) printf("%ld\n", t);
841 		} else if (type == DDATE) {
842 			char	dstr[64];
843 			int	len;
844 
845 			len = strftime(dstr, sizeof (dstr), fmt, localtime(&t));
846 			if (len > 0) {
847 				(void) printf("%s\n", dstr);
848 			}
849 		}
850 	}
851 }
852 
853 /*
854  * The idea is that 'dspfunc' should only be modified from the default
855  * once since the display options are mutually exclusive.  If 'dspfunc'
856  * only contains the default display function, then all is good and we
857  * can set it to the new display function.  Otherwise, bail.
858  */
859 void
860 set_dispfunc(
861 	void (**dspfunc)(char *, vopstats_t *, vopstats_t *, int),
862 	void (*newfunc)(char *, vopstats_t *, vopstats_t *, int))
863 {
864 	if (*dspfunc != dflt_display) {
865 		(void) fprintf(stderr, gettext(
866 		"%s: Display options -{a|f|i|n|v} are mutually exclusive\n"),
867 		    cmdname);
868 		usage();
869 	}
870 	*dspfunc = newfunc;
871 }
872 
873 int
874 main(int argc, char *argv[])
875 {
876 	int		c;
877 	int		i, j;		/* Generic counters */
878 	int		nentities_found;
879 	int		linesout;	/* Keeps track of lines printed */
880 	int		printhdr = 0;	/* Print a header?  0 = no, 1 = yes */
881 	int		nfstypes;	/* Number of fstypes */
882 	int		dispflag = 0;	/* Flags for display control */
883 	int		timestamp = NODATE;	/* Default: no time stamp */
884 	long		count = 0;	/* Number of iterations for display */
885 	long		interval = 0;
886 	boolean_t	fstypes_only = B_FALSE;	/* Display fstypes only */
887 	char		**fstypes;	/* Array of names of all fstypes */
888 	int		nentities;	/* Number of stat-able entities */
889 	entity_t	*entities;	/* Array of stat-able entities */
890 	kstat_ctl_t	*kc;
891 	void (*dfunc)(char *, vopstats_t *, vopstats_t *, int) = dflt_display;
892 
893 	extern int	optind;
894 
895 	(void) setlocale(LC_ALL, "");
896 #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
897 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
898 #endif
899 	(void) textdomain(TEXT_DOMAIN);
900 
901 	cmdname = argv[0];
902 	while ((c = getopt(argc, argv, OPTIONS)) != EOF) {
903 		switch (c) {
904 
905 		default:
906 			usage();
907 			break;
908 
909 		case 'F':	/* Only display available FStypes */
910 			fstypes_only = B_TRUE;
911 			break;
912 
913 #if PARSABLE_OUTPUT
914 		case 'P':	/* Parsable output */
915 			dispflag |= DISP_RAW;
916 			break;
917 #endif /* PARSABLE_OUTPUT */
918 
919 		case 'T':	/* Timestamp */
920 			if (optarg) {
921 				if (strcmp(optarg, "u") == 0) {
922 					timestamp = UDATE;
923 				} else if (strcmp(optarg, "d") == 0) {
924 					timestamp = DDATE;
925 				}
926 			}
927 
928 			/* If it was never set properly... */
929 			if (timestamp == NODATE) {
930 				(void) fprintf(stderr, gettext(
931 				"%s: -T option requires either 'u' or 'd'\n"),
932 					cmdname);
933 				usage();
934 			}
935 			break;
936 
937 		case 'a':
938 			set_dispfunc(&dfunc, attr_display);
939 			break;
940 
941 		case 'f':
942 			set_dispfunc(&dfunc, vop_display);
943 			break;
944 
945 		case 'i':
946 			set_dispfunc(&dfunc, io_display);
947 			break;
948 
949 		case 'n':
950 			set_dispfunc(&dfunc, naming_display);
951 			break;
952 
953 		case 'v':
954 			set_dispfunc(&dfunc, vm_display);
955 			break;
956 		}
957 	}
958 
959 #if PARSABLE_OUTPUT
960 	if ((dispflag & DISP_RAW) && (timestamp != NODATE)) {
961 		(void) fprintf(stderr, gettext(
962 			"-P and -T options are mutually exclusive\n"));
963 		usage();
964 	}
965 #endif /* PARSABLE_OUTPUT */
966 
967 	/* Gather the list of filesystem types */
968 	if ((nfstypes = build_fstype_list(&fstypes)) == 0) {
969 		(void) fprintf(stderr,
970 		    gettext("Can't build list of fstypes\n"));
971 		exit(1);
972 	}
973 
974 	nentities = parse_operands(
975 	    argc, argv, optind, &interval, &count, &entities);
976 
977 	if (nentities == -1)	/* Set of operands didn't parse properly  */
978 		usage();
979 
980 	if ((nentities == 0) && (fstypes_only == B_FALSE)) {
981 		(void) fprintf(stderr, gettext(
982 		    "Must specify -F or at least one fstype or mount point\n"));
983 		usage();
984 	}
985 
986 	if ((nentities > 0) && (fstypes_only == B_TRUE)) {
987 		(void) fprintf(stderr, gettext(
988 		    "Cannot use -F with fstypes or mount points\n"));
989 		usage();
990 	}
991 
992 	/*
993 	 * If we had no operands (except for interval/count) and we
994 	 * requested FStypes only (-F), then fill in the entities[]
995 	 * array with all available fstypes.
996 	 */
997 	if ((nentities == 0) && (fstypes_only == B_TRUE)) {
998 		if ((entities = calloc(nfstypes, sizeof (entity_t))) == NULL) {
999 			(void) fprintf(stderr,
1000 			    gettext("Can't calloc fstype stats\n"));
1001 			exit(1);
1002 		}
1003 
1004 		for (i = 1; i < nfstypes; i++) {
1005 			if (fstypes[i]) {
1006 				entities[nentities].e_name = strdup(fstypes[i]);
1007 				nentities++;
1008 			}
1009 		}
1010 	}
1011 
1012 	set_ksnames(entities, nentities, fstypes, nfstypes);
1013 
1014 	if ((kc = kstat_open()) == NULL) {
1015 		perror(gettext("kstat_open"));
1016 		exit(1);
1017 	}
1018 
1019 	/*
1020 	 * The following loop walks through the entities[] list to "prime
1021 	 * the pump"
1022 	 */
1023 	for (j = 0, linesout = 0; j < nentities; j++) {
1024 		entity_t *ent = &entities[j];
1025 		vopstats_t *vsp = &ent->e_vs[CUR_INDEX];
1026 		kstat_t *ksp = NULL;
1027 
1028 		if (get_vopstats(kc, ent->e_ksname, vsp, &ksp) == 0) {
1029 			(*dfunc)(ent->e_name, NULL, vsp,
1030 			    dispflag_policy(linesout == 0, dispflag));
1031 			linesout++;
1032 		} else {
1033 			/*
1034 			 * If we can't find it the first time through, then
1035 			 * get rid of it.
1036 			 */
1037 			entities[j].e_ksname[0] = 0;
1038 
1039 			/*
1040 			 * If we're only displaying FStypes (-F) then don't
1041 			 * complain about any file systems that might not
1042 			 * be loaded.  Otherwise, let the user know that
1043 			 * he chose poorly.
1044 			 */
1045 			if (fstypes_only == B_FALSE) {
1046 				(void) fprintf(stderr, gettext(
1047 				    "No statistics available for %s\n"),
1048 				    entities[j].e_name);
1049 			}
1050 		}
1051 	}
1052 
1053 	BUMP_INDEX();	/* Swap the previous/current indices */
1054 	for (i = 1; i <= count; i++) {
1055 		/*
1056 		 * No telling how many lines will be printed in any interval.
1057 		 * There should be a minimum of HEADERLINES between any
1058 		 * header.  If we exceed that, no big deal.
1059 		 */
1060 		if (linesout > HEADERLINES) {
1061 			linesout = 0;
1062 			printhdr = 1;
1063 		}
1064 		(void) poll(NULL, 0, interval*1000);
1065 
1066 		if (timestamp) {
1067 			print_time(timestamp);
1068 			linesout++;
1069 		}
1070 
1071 		for (j = 0, nentities_found = 0; j < nentities; j++) {
1072 			entity_t *ent = &entities[j];
1073 
1074 			/*
1075 			 * If this entry has been cleared, don't attempt
1076 			 * to process it.
1077 			 */
1078 			if (ent->e_ksname[0] == 0) {
1079 				continue;
1080 			}
1081 
1082 			if (get_vopstats(kc, ent->e_ksname,
1083 			    &ent->e_vs[CUR_INDEX], NULL) == 0) {
1084 				(*dfunc)(ent->e_name, &ent->e_vs[PREV_INDEX],
1085 				    &ent->e_vs[CUR_INDEX],
1086 				    dispflag_policy(printhdr, dispflag));
1087 				linesout++;
1088 				nentities_found++;
1089 			} else {
1090 				if (ent->e_type == ENTYPE_MNTPT) {
1091 					(void) printf(gettext(
1092 					    "<<mount point no longer "
1093 					    "available: %s>>\n"), ent->e_name);
1094 				} else if (ent->e_type == ENTYPE_FSTYPE) {
1095 					(void) printf(gettext(
1096 					    "<<file system module no longer "
1097 					    "loaded: %s>>\n"), ent->e_name);
1098 				} else {
1099 					(void) printf(gettext(
1100 					    "<<%s no longer available>>\n"),
1101 					    ent->e_name);
1102 				}
1103 				/* Disable this so it doesn't print again */
1104 				ent->e_ksname[0] = 0;
1105 			}
1106 			printhdr = 0;	/* Always shut this off */
1107 		}
1108 		BUMP_INDEX();	/* Bump the previous/current indices */
1109 
1110 		/*
1111 		 * If the entities we were observing are no longer there
1112 		 * (file system modules unloaded, file systems unmounted)
1113 		 * then we're done.
1114 		 */
1115 		if (nentities_found == 0)
1116 			break;
1117 	}
1118 
1119 	return (0);
1120 }
1121