2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Antonio Huete <tuxillo@quantumachine.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 void show_info(char *path);
39 char *find_pfs_mount(int pfsid, uuid_t parentuuid, int ismaster);
40 double percent(int64_t value, int64_t total);
41 u_int32_t count_snapshots(int fd, u_int32_t version, char *pfs_snapshots, char *mountedon);
46 struct statfs *stfsbuf;
47 int mntsize, i, first = 1;
51 mntsize = getmntinfo(&stfsbuf, MNT_NOWAIT);
53 for (i=0; i < mntsize; i++) {
54 fstype = stfsbuf[i].f_fstypename;
55 path = stfsbuf[i].f_mntonname;
56 if ((strcmp(fstype, "hammer")) == 0) {
60 fprintf(stdout, "\n");
65 fprintf(stdout, "No mounted filesystems found\n");
73 struct hammer_pseudofs_data pfs_od;
74 struct hammer_ioc_pseudofs_rw pfs;
75 int64_t usedbigblocks, bytes;
76 struct hammer_ioc_info info;
77 int fd, pfs_id, ismaster;
83 fsid = mountedon = NULL;
85 pfs_id = 0; /* Include PFS#0 */
89 bzero(&info, sizeof(struct hammer_ioc_info));
91 /* Try to get a file descriptor based on the path given */
92 fd = open(path, O_RDONLY);
98 if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
103 /* Find out the UUID strings */
104 uuid_to_string(&info.vol_fsid, &fsid, NULL);
106 /* Volume information */
107 fprintf(stdout, "Volume identification\n");
108 fprintf(stdout, "\tLabel %s\n", info.vol_name);
109 fprintf(stdout, "\tNo. Volumes %d\n", info.nvolumes);
110 fprintf(stdout, "\tFSID %s\n", fsid);
111 fprintf(stdout, "\tHAMMER Version %d\n", info.version);
113 /* Big blocks information */
114 usedbigblocks = info.bigblocks - info.freebigblocks;
116 fprintf(stdout, "Big block information\n");
117 fprintf(stdout, "\tTotal %10jd\n", (intmax_t)info.bigblocks);
118 fprintf(stdout, "\tUsed %10jd (%.2lf%%)\n"
119 "\tReserved %10jd (%.2lf%%)\n"
120 "\tFree %10jd (%.2lf%%)\n",
121 (intmax_t)usedbigblocks,
122 percent(usedbigblocks, info.bigblocks),
123 (intmax_t)info.rsvbigblocks,
124 percent(info.rsvbigblocks, info.bigblocks),
125 (intmax_t)(info.freebigblocks - info.rsvbigblocks),
126 percent(info.freebigblocks - info.rsvbigblocks,
128 fprintf(stdout, "Space information\n");
130 /* Space information */
131 fprintf(stdout, "\tNo. Inodes %10jd\n", (intmax_t)info.inodes);
132 bytes = (info.bigblocks << HAMMER_LARGEBLOCK_BITS);
133 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), bytes, "",
134 HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE | HN_B);
135 fprintf(stdout, "\tTotal size %6s (%jd bytes)\n",
136 buf, (intmax_t)bytes);
138 bytes = (usedbigblocks << HAMMER_LARGEBLOCK_BITS);
139 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), bytes, "",
140 HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE | HN_B);
141 fprintf(stdout, "\tUsed space %6s\n", buf);
143 bytes = (info.rsvbigblocks << HAMMER_LARGEBLOCK_BITS);
144 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), bytes, "",
145 HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE | HN_B);
146 fprintf(stdout, "\tReserved space %6s\n", buf);
148 bytes = ((info.freebigblocks - info.rsvbigblocks) << HAMMER_LARGEBLOCK_BITS);
149 humanize_number(buf, sizeof(buf) - (bytes < 0 ? 0 : 1), bytes, "",
150 HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE | HN_B);
151 fprintf(stdout, "\tFree space %6s\n", buf);
153 /* Pseudo-filesystem information */
154 fprintf(stdout, "PFS information\n");
155 fprintf(stdout, "\tPFS-Id Mode Snaps Mounted-on\n");
157 while(pfs_id < HAMMER_MAX_PFS) {
158 bzero(&pfs, sizeof(pfs));
159 bzero(&pfs_od, sizeof(pfs_od));
161 pfs.ondisk = &pfs_od;
162 pfs.bytes = sizeof(pfs_od);
163 pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
164 if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) >= 0) {
165 ismaster = (pfs_od.mirror_flags & HAMMER_PFSD_SLAVE) ? 0 : 1;
167 mountedon = strdup(path);
169 mountedon = find_pfs_mount(pfs_id, info.vol_fsid, ismaster);
171 sc = count_snapshots(fd, info.version, pfs_od.snapshots, mountedon);
173 fprintf(stdout, "\t%6d %-6s %6d ",
174 pfs_id, (ismaster ? "MASTER" : "SLAVE"), sc);
176 fprintf(stdout, "%s", mountedon);
178 fprintf(stdout, "not-mounted");
179 fprintf(stdout, "\n");
189 find_pfs_mount(int pfsid, uuid_t parentuuid, int ismaster)
191 struct hammer_ioc_info hi;
192 struct statfs *mntbuf;
202 /* Do not continue if there are no mounted filesystems */
203 mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
207 mntbufsize = (mntsize) * sizeof(struct statfs);
208 mntbuf = malloc(mntbufsize);
209 if (mntbuf == NULL) {
214 mntsize = getfsstat(mntbuf, (long)mntbufsize, MNT_NOWAIT);
215 curmount = mntsize - 1;
217 asprintf(&trailstr, ":%05d", pfsid);
220 * Iterate all the mounted points looking for the PFS passed to
223 while(curmount >= 0) {
225 * We need to avoid that PFS belonging to other HAMMER
226 * filesystems are showed as mounted, so we compare
227 * against the FSID, which is presumable to be unique.
229 bzero(&hi, sizeof(hi));
230 if ((fd = open(mntbuf[curmount].f_mntfromname, O_RDONLY)) < 0) {
235 if ((ioctl(fd, HAMMERIOC_GET_INFO, &hi)) < 0) {
240 if (strstr(mntbuf[curmount].f_mntfromname, trailstr) != NULL &&
241 (uuid_compare(&hi.vol_fsid, &parentuuid, NULL)) == 0) {
243 if (strstr(mntbuf[curmount].f_mntfromname, "@@-1") != NULL) {
244 retval = strdup(mntbuf[curmount].f_mntonname);
248 if (strstr(mntbuf[curmount].f_mntfromname, "@@0x") != NULL ) {
249 retval = strdup(mntbuf[curmount].f_mntonname);
261 percent(int64_t value, int64_t total)
263 /* Avoid divide-by-zero */
267 return ((value * 100.0) / (double)total);
271 count_snapshots(int fd, u_int32_t version, char *pfs_snapshots, char *mountedon)
273 struct hammer_ioc_snapshot snapinfo;
274 char *snapshots_path, *fpath;
278 u_int32_t snapshot_count = 0;
280 bzero(&snapinfo, sizeof(struct hammer_ioc_snapshot));
283 * old style: count the number of softlinks in the snapshots dir
285 if (pfs_snapshots[0])
286 snapshots_path = pfs_snapshots;
288 asprintf(&snapshots_path, "%s/snapshots", mountedon);
289 if ((dir = opendir(snapshots_path)) != NULL) {
290 while ((den = readdir(dir)) != NULL) {
291 if (den->d_name[0] == '.')
293 asprintf(&fpath, "%s/%s", snapshots_path, den->d_name);
294 if (lstat(fpath, &st) == 0 && S_ISLNK(st.st_mode))
302 * new style: file system meta-data
305 if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapinfo) < 0) {
306 perror("count_snapshots");
309 snapshot_count += snapinfo.count;
310 } while (snapinfo.head.error == 0 && snapinfo.count);
312 return snapshot_count;