Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / usr.sbin / snapinfo / snapinfo.c
blob55b54a546b267670c0872ea91af560d72a05ae9e
1 /*-
2 * Copyright (c) 2005 Mark Santcroos <marks@freebsd.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * $FreeBSD$
28 #include <sys/param.h>
29 #include <sys/mount.h>
31 #include <ufs/ufs/dinode.h>
32 #include <ufs/ffs/fs.h>
34 #include <errno.h>
35 #include <ftw.h>
36 #include <libufs.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
42 void find_inum(char *path);
43 void usage(void);
44 int compare_function(const char *, const struct stat *, int, struct FTW *);
45 int find_snapshot(struct statfs *sfs);
47 int verbose;
48 int cont_search;
49 uint32_t inode;
51 int
52 main(int argc, char **argv)
54 char *path;
55 struct stat st;
56 struct statfs *mntbuf;
57 int all = 0, ch, done = 0, fscount, n;
59 while ((ch = getopt(argc, argv, "adv")) != -1) {
60 switch (ch) {
61 case 'a':
62 all++;
63 break;
64 case 'd':
65 /* continue to search when matching inode is found
66 * this feature is not documented */
67 cont_search++;
68 break;
69 case 'v':
70 verbose++;
71 break;
72 default:
73 usage();
77 argc -= optind;
78 argv += optind;
80 if ((all == 0 && argc != 1) || (all == 1 && argc > 0))
81 usage();
83 if (!all) {
84 char resolved[PATH_MAX];
86 path = *argv;
88 * mount(8) use realpath(3) before mounting file system,
89 * so let's do the same with the given path.
91 if (realpath(path, resolved) == NULL || /* can create full path */
92 stat(resolved, &st) == -1 || /* is it stat'able */
93 !S_ISDIR(st.st_mode)) { /* is it a directory */
94 usage();
96 path = resolved;
99 fscount = getmntinfo(&mntbuf, MNT_WAIT);
100 for (n = 0; n < fscount; n++) {
101 if (!strncmp(mntbuf[n].f_fstypename, "ufs", 3)) {
102 if (all || strcmp(path, mntbuf[n].f_mntonname) == 0) {
103 find_snapshot(&mntbuf[n]);
104 done++;
109 if (done == 0)
110 usage();
112 return (0);
116 find_snapshot(struct statfs *sfs)
118 struct uufsd disk;
119 int j, snapcount = 0;
121 if (ufs_disk_fillout(&disk, sfs->f_mntfromname) == -1)
122 perror("ufs_disk_fillout");
124 if (verbose)
125 printf("%s mounted on %s\n", disk.d_name, disk.d_fs.fs_fsmnt);
127 for (j = 0; j < FSMAXSNAP; j++) {
128 if (disk.d_fs.fs_snapinum[j]) {
129 inode = disk.d_fs.fs_snapinum[j];
130 find_inum(sfs->f_mntonname);
131 snapcount++;
135 if (!snapcount && verbose)
136 printf("\tno snapshots found\n");
138 return 0;
142 compare_function(const char *path, const struct stat *st, int flags,
143 struct FTW * ftwv)
146 if (flags == FTW_F && st->st_ino == inode) {
147 if (verbose)
148 printf("\tsnapshot ");
149 printf("%s", path);
150 if (verbose)
151 printf(" (inode %d)", st->st_ino);
152 printf("\n");
153 if (!cont_search)
154 return (EEXIST);
157 return (0);
160 void
161 find_inum(char *path)
163 int ret;
165 ret = nftw(path, compare_function, 1, FTW_PHYS|FTW_MOUNT);
166 if (ret != EEXIST && ret != 0) {
167 perror("ftw");
168 exit(ret);
172 void
173 usage(void)
176 printf("usage: snapinfo [-v] -a\n");
177 printf(" snapinfo [-v] mountpoint\n");
178 exit(1);