1/*
2 * lsvfs - list loaded VFSes
3 * Garrett A. Wollman, September 1994
4 * This file is in the public domain.
5 *
6 */
7
8#include <sys/cdefs.h>
9__FBSDID("$FreeBSD$");
10
11#include <sys/param.h>
12#include <sys/mount.h>
13#include <sys/sysctl.h>
14
15#include <err.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#define FMT	"%-32.32s 0x%08x %5d  %s\n"
21#define HDRFMT	"%-32.32s %10s %5.5s  %s\n"
22#define DASHES	"-------------------------------- "	\
23		"---------- -----  ---------------\n"
24
25static struct flaglist {
26	int		flag;
27	const char	str[32]; /* must be longer than the longest one. */
28} fl[] = {
29	{ .flag = VFCF_STATIC, .str = "static", },
30	{ .flag = VFCF_NETWORK, .str = "network", },
31	{ .flag = VFCF_READONLY, .str = "read-only", },
32	{ .flag = VFCF_SYNTHETIC, .str = "synthetic", },
33	{ .flag = VFCF_LOOPBACK, .str = "loopback", },
34	{ .flag = VFCF_UNICODE, .str = "unicode", },
35	{ .flag = VFCF_JAIL, .str = "jail", },
36	{ .flag = VFCF_DELEGADMIN, .str = "delegated-administration", },
37};
38
39static const char *fmt_flags(int);
40
41int
42main(int argc, char **argv)
43{
44  int cnt, rv = 0, i;
45  struct xvfsconf vfc, *xvfsp;
46  size_t buflen;
47  argc--, argv++;
48
49  printf(HDRFMT, "Filesystem", "Num", "Refs", "Flags");
50  fputs(DASHES, stdout);
51
52  if(argc) {
53    for(; argc; argc--, argv++) {
54      if (getvfsbyname(*argv, &vfc) == 0) {
55        printf(FMT, vfc.vfc_name, vfc.vfc_typenum, vfc.vfc_refcount,
56	    fmt_flags(vfc.vfc_flags));
57      } else {
58	warnx("VFS %s unknown or not loaded", *argv);
59        rv++;
60      }
61    }
62  } else {
63    if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0)
64      err(1, "sysctl(vfs.conflist)");
65    xvfsp = malloc(buflen);
66    if (xvfsp == NULL)
67      errx(1, "malloc failed");
68    if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0)
69      err(1, "sysctl(vfs.conflist)");
70    cnt = buflen / sizeof(struct xvfsconf);
71
72    for (i = 0; i < cnt; i++) {
73      printf(FMT, xvfsp[i].vfc_name, xvfsp[i].vfc_typenum,
74	    xvfsp[i].vfc_refcount, fmt_flags(xvfsp[i].vfc_flags));
75    }
76    free(xvfsp);
77  }
78
79  return rv;
80}
81
82static const char *
83fmt_flags(int flags)
84{
85	static char buf[sizeof(struct flaglist) * sizeof(fl)];
86	int i;
87
88	buf[0] = '\0';
89	for (i = 0; i < (int)nitems(fl); i++)
90		if (flags & fl[i].flag) {
91			strlcat(buf, fl[i].str, sizeof(buf));
92			strlcat(buf, ", ", sizeof(buf));
93		}
94	if (buf[0] != '\0')
95		buf[strlen(buf) - 2] = '\0';
96	return (buf);
97}
98