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
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.
28 #include <sys/param.h>
29 #include <sys/mount.h>
31 #include <ufs/ufs/dinode.h>
32 #include <ufs/ffs/fs.h>
43 static void find_inum(char *path);
44 static void usage(void);
45 static int compare_function(const char *, const struct stat *,
47 static int find_snapshot(struct statfs *sfs);
50 static int cont_search;
51 static uint32_t inode;
54 main(int argc, char **argv)
58 struct statfs *mntbuf;
59 int all = 0, ch, done = 0, fscount, n;
61 while ((ch = getopt(argc, argv, "adv")) != -1) {
67 /* continue to search when matching inode is found
68 * this feature is not documented */
82 if ((all == 0 && argc != 1) || (all == 1 && argc > 0))
86 char resolved[PATH_MAX];
90 * mount(8) use realpath(3) before mounting file system,
91 * so let's do the same with the given path.
93 if (realpath(path, resolved) == NULL || /* can create full path */
94 stat(resolved, &st) == -1 || /* is it stat'able */
95 !S_ISDIR(st.st_mode)) { /* is it a directory */
101 fscount = getmntinfo(&mntbuf, MNT_WAIT);
102 for (n = 0; n < fscount; n++) {
103 if (!strncmp(mntbuf[n].f_fstypename, "ufs", 3)) {
104 if (all || strcmp(path, mntbuf[n].f_mntonname) == 0) {
105 find_snapshot(&mntbuf[n]);
118 find_snapshot(struct statfs *sfs)
121 int j, snapcount = 0;
123 if (ufs_disk_fillout(&disk, sfs->f_mntfromname) == -1)
124 perror("ufs_disk_fillout");
127 printf("%s mounted on %s\n", disk.d_name, disk.d_fs.fs_fsmnt);
129 for (j = 0; j < FSMAXSNAP; j++) {
130 if (disk.d_fs.fs_snapinum[j]) {
131 inode = disk.d_fs.fs_snapinum[j];
132 find_inum(sfs->f_mntonname);
137 if (!snapcount && verbose)
138 printf("\tno snapshots found\n");
144 compare_function(const char *path, const struct stat *st, int flags,
145 struct FTW * ftwv __unused)
148 if (flags == FTW_F && st->st_ino == inode) {
150 printf("\tsnapshot ");
153 printf(" (inode %ju)", (uintmax_t)st->st_ino);
163 find_inum(char *path)
167 ret = nftw(path, compare_function, 1, FTW_PHYS|FTW_MOUNT);
168 if (ret != EEXIST && ret != 0) {
178 printf("usage: snapinfo [-v] -a\n");
179 printf(" snapinfo [-v] mountpoint\n");