17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
23*47644099Sgt29601 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <libintl.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
367c478bd9Sstevel@tonic-gate #include <kstat.h>
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #include <sys/fs/cachefs_log.h>
397c478bd9Sstevel@tonic-gate #include "stats.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate void usage(char *);
427c478bd9Sstevel@tonic-gate void pr_err(char *, ...);
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate static int hflag = 0;
457c478bd9Sstevel@tonic-gate static char *fpath = NULL;
467c478bd9Sstevel@tonic-gate static int vflag = 0;
477c478bd9Sstevel@tonic-gate char *prog;
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate static void log_show(char *, char *);
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)527c478bd9Sstevel@tonic-gate main(int argc, char **argv)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate int rc = 0, c;
557c478bd9Sstevel@tonic-gate int errflg = 0;
567c478bd9Sstevel@tonic-gate stats_cookie_t *fs = NULL;
577c478bd9Sstevel@tonic-gate char *logfile;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
607c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
617c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
627c478bd9Sstevel@tonic-gate #endif /* TEXT_DOMAIN */
637c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate if (prog = strrchr(argv[0], '/'))
667c478bd9Sstevel@tonic-gate ++prog;
677c478bd9Sstevel@tonic-gate else
687c478bd9Sstevel@tonic-gate prog = argv[0];
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "hf:v")) != EOF)
717c478bd9Sstevel@tonic-gate switch (c) {
727c478bd9Sstevel@tonic-gate case 'h':
737c478bd9Sstevel@tonic-gate if (fpath != NULL)
747c478bd9Sstevel@tonic-gate ++errflg;
757c478bd9Sstevel@tonic-gate else
767c478bd9Sstevel@tonic-gate ++hflag;
777c478bd9Sstevel@tonic-gate break;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate case 'f':
807c478bd9Sstevel@tonic-gate if (hflag)
817c478bd9Sstevel@tonic-gate ++errflg;
827c478bd9Sstevel@tonic-gate else
837c478bd9Sstevel@tonic-gate fpath = optarg;
847c478bd9Sstevel@tonic-gate break;
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate case 'v':
877c478bd9Sstevel@tonic-gate ++vflag;
887c478bd9Sstevel@tonic-gate break;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate case '?':
917c478bd9Sstevel@tonic-gate default:
927c478bd9Sstevel@tonic-gate ++errflg;
937c478bd9Sstevel@tonic-gate break;
947c478bd9Sstevel@tonic-gate }
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate if ((errflg) || (optind != (argc - 1))) {
977c478bd9Sstevel@tonic-gate usage(NULL);
987c478bd9Sstevel@tonic-gate rc = -1;
997c478bd9Sstevel@tonic-gate goto out;
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate fs = stats_create_mountpath(argv[optind], prog);
1037c478bd9Sstevel@tonic-gate if (fs == NULL) {
1047c478bd9Sstevel@tonic-gate pr_err(gettext("Cannot initialize cachefs library\n"));
1057c478bd9Sstevel@tonic-gate rc = 1;
1067c478bd9Sstevel@tonic-gate goto out;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate if (! stats_good(fs)) {
1107c478bd9Sstevel@tonic-gate pr_err(stats_errorstr(fs));
1117c478bd9Sstevel@tonic-gate rc = stats_errno(fs);
1127c478bd9Sstevel@tonic-gate goto out;
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate if ((logfile = stats_log_kernel_getname(fs)) == NULL) {
1167c478bd9Sstevel@tonic-gate pr_err(stats_errorstr(fs));
1177c478bd9Sstevel@tonic-gate rc = stats_errno(fs);
1187c478bd9Sstevel@tonic-gate goto out;
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate if ((logfile[0] == '\0') && (hflag) && (! vflag)) {
1217c478bd9Sstevel@tonic-gate log_show(argv[optind], logfile);
1227c478bd9Sstevel@tonic-gate goto out;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate if (fpath != NULL) {
1267c478bd9Sstevel@tonic-gate if ((stats_log_kernel_setname(fs, fpath) != 0) ||
1277c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_MOUNT, 1) != 0) ||
1287c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_UMOUNT, 1) != 0) ||
1297c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_REMOVE, 1) != 0) ||
1307c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_RMDIR, 1) != 0) ||
1317c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_TRUNCATE, 1) != 0) ||
1327c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_CREATE, 1) != 0) ||
1337c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_MKDIR, 1) != 0) ||
1347c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_RENAME, 1) != 0) ||
1357c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_SYMLINK, 1) != 0) ||
1367c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_UALLOC, 1) != 0) ||
1377c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_CSYMLINK, 1) != 0) ||
1387c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_FILLDIR, 1) != 0) ||
1397c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_MDCREATE, 1) != 0) ||
1407c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_NOCACHE, 1) != 0) ||
1417c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_CALLOC, 1) != 0) ||
1427c478bd9Sstevel@tonic-gate (stats_log_which(fs, CACHEFS_LOG_RFDIR, 1) != 0)) {
1437c478bd9Sstevel@tonic-gate pr_err(stats_errorstr(fs));
1447c478bd9Sstevel@tonic-gate rc = stats_errno(fs);
1457c478bd9Sstevel@tonic-gate goto out;
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate } else if (hflag) {
1487c478bd9Sstevel@tonic-gate if (stats_log_kernel_setname(fs, NULL) != 0) {
1497c478bd9Sstevel@tonic-gate pr_err(stats_errorstr(fs));
1507c478bd9Sstevel@tonic-gate rc = stats_errno(fs);
1517c478bd9Sstevel@tonic-gate goto out;
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate if ((logfile = stats_log_kernel_getname(fs)) == NULL) {
1567c478bd9Sstevel@tonic-gate pr_err(stats_errorstr(fs));
1577c478bd9Sstevel@tonic-gate rc = stats_errno(fs);
1587c478bd9Sstevel@tonic-gate goto out;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate
1617c478bd9Sstevel@tonic-gate log_show(argv[optind], logfile);
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * if they're changing state, inform them of other filesystems
1657c478bd9Sstevel@tonic-gate * that they're changing state for by way of sharing the
1667c478bd9Sstevel@tonic-gate * cache.
1677c478bd9Sstevel@tonic-gate *
1687c478bd9Sstevel@tonic-gate * or, if they're verbose (-v flag), tell them about the
1697c478bd9Sstevel@tonic-gate * others.
1707c478bd9Sstevel@tonic-gate */
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate if (((fpath) || (hflag) || (vflag)) && (! stats_inerror(fs))) {
1737c478bd9Sstevel@tonic-gate cachefs_kstat_key_t *k, *origk;
1747c478bd9Sstevel@tonic-gate stats_cookie_t *sc;
1757c478bd9Sstevel@tonic-gate int before = 0;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate origk = stats_getkey(fs);
1787c478bd9Sstevel@tonic-gate sc = stats_create_unbound(prog);
1797c478bd9Sstevel@tonic-gate if (sc == NULL) {
1807c478bd9Sstevel@tonic-gate pr_err(gettext("Cannot create stats object"));
1817c478bd9Sstevel@tonic-gate rc = 1;
1827c478bd9Sstevel@tonic-gate goto out;
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate while ((k = stats_next(sc)) != NULL) {
1867c478bd9Sstevel@tonic-gate if (! k->ks_mounted) {
1877c478bd9Sstevel@tonic-gate free(k);
1887c478bd9Sstevel@tonic-gate continue;
1897c478bd9Sstevel@tonic-gate }
190*47644099Sgt29601 if (strcmp((char *)(uintptr_t)origk->ks_cachedir,
191*47644099Sgt29601 (char *)(uintptr_t)k->ks_cachedir) != 0) {
1927c478bd9Sstevel@tonic-gate free(k);
1937c478bd9Sstevel@tonic-gate continue;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate if (origk->ks_id == k->ks_id) {
1967c478bd9Sstevel@tonic-gate free(k);
1977c478bd9Sstevel@tonic-gate continue;
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate if (! before)
2007c478bd9Sstevel@tonic-gate printf("\n");
2017c478bd9Sstevel@tonic-gate before = 1;
202*47644099Sgt29601 log_show((char *)(uintptr_t)k->ks_mountpoint, logfile);
2037c478bd9Sstevel@tonic-gate free(k);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate free(origk);
2067c478bd9Sstevel@tonic-gate stats_destroy(sc);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate if (stats_inerror(fs)) {
2107c478bd9Sstevel@tonic-gate pr_err(stats_errorstr(fs));
2117c478bd9Sstevel@tonic-gate rc = stats_errno(fs);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate out:
2157c478bd9Sstevel@tonic-gate stats_destroy(fs);
2167c478bd9Sstevel@tonic-gate return (rc);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate static void
log_show(char * mount,char * logfile)2207c478bd9Sstevel@tonic-gate log_show(char *mount, char *logfile)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate if (logfile[0] == '\0')
2237c478bd9Sstevel@tonic-gate logfile = gettext("not logged");
2247c478bd9Sstevel@tonic-gate printf("%s: %s\n", logfile, mount);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate *
2297c478bd9Sstevel@tonic-gate * usage
2307c478bd9Sstevel@tonic-gate *
2317c478bd9Sstevel@tonic-gate * Description:
2327c478bd9Sstevel@tonic-gate * Prints a short usage message.
2337c478bd9Sstevel@tonic-gate * Arguments:
2347c478bd9Sstevel@tonic-gate * msgp message to include with the usage message
2357c478bd9Sstevel@tonic-gate * Returns:
2367c478bd9Sstevel@tonic-gate * Preconditions:
2377c478bd9Sstevel@tonic-gate */
2387c478bd9Sstevel@tonic-gate
2397c478bd9Sstevel@tonic-gate void
usage(char * msgp)2407c478bd9Sstevel@tonic-gate usage(char *msgp)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate if (msgp) {
2437c478bd9Sstevel@tonic-gate pr_err("%s", msgp);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate fprintf(stderr,
2477c478bd9Sstevel@tonic-gate gettext("Usage: "
2487c478bd9Sstevel@tonic-gate "cachefslog [ -v ] [-h | -f <logfile>] mountpoint\n"));
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate *
2537c478bd9Sstevel@tonic-gate * pr_err
2547c478bd9Sstevel@tonic-gate *
2557c478bd9Sstevel@tonic-gate * Description:
2567c478bd9Sstevel@tonic-gate * Prints an error message to stderr.
2577c478bd9Sstevel@tonic-gate * Arguments:
2587c478bd9Sstevel@tonic-gate * fmt printf style format
2597c478bd9Sstevel@tonic-gate * ... arguments for fmt
2607c478bd9Sstevel@tonic-gate * Returns:
2617c478bd9Sstevel@tonic-gate * Preconditions:
2627c478bd9Sstevel@tonic-gate * precond(fmt)
2637c478bd9Sstevel@tonic-gate */
2647c478bd9Sstevel@tonic-gate
2657c478bd9Sstevel@tonic-gate void
pr_err(char * fmt,...)2667c478bd9Sstevel@tonic-gate pr_err(char *fmt, ...)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate va_list ap;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate va_start(ap, fmt);
2717c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("cachefslog: "));
2727c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, fmt, ap);
2737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n");
2747c478bd9Sstevel@tonic-gate va_end(ap);
2757c478bd9Sstevel@tonic-gate }
276