hammer - Migration to libhammer (step 1/many)
authorAntonio Huete Jimenez <tuxillo@quantumachine.net>
Sat, 5 Nov 2011 18:00:20 +0000 (19:00 +0100)
committerAntonio Huete Jimenez <tuxillo@quantumachine.net>
Sat, 5 Nov 2011 18:41:31 +0000 (19:41 +0100)
- Start using libhammer
- Migrate info directive

sbin/hammer/Makefile
sbin/hammer/cmd_blockmap.c
sbin/hammer/cmd_info.c
sbin/hammer/cmd_snapshot.c
sbin/hammer/hammer.h

index 90b329b..2a68ae5 100644 (file)
@@ -9,8 +9,8 @@ SRCS=   hammer.c ondisk.c blockmap.c cache.c misc.c cycle.c \
 MAN=   hammer.8
 
 CFLAGS+= -I${.CURDIR}/../../sys -DALIST_NO_DEBUG
-LDADD= -lm -lutil -lmd
-DPADD= ${LIBM} ${LIBUTIL} ${LIBMD}
+LDADD= -lm -lutil -lmd -lhammer
+DPADD= ${LIBM} ${LIBUTIL} ${LIBMD} ${LIBHAMMER}
 
 .PATH: ${.CURDIR}/../../sys/libkern
 SRCS+= crc32.c
index 342295d..86bc2cb 100644 (file)
@@ -43,9 +43,6 @@ typedef struct collect {
        struct hammer_blockmap_layer2 *layer2;
 } *collect_t;
 
-#define COLLECT_HSIZE  1024
-#define COLLECT_HMASK  (COLLECT_HSIZE - 1)
-
 collect_t CollectHash[COLLECT_HSIZE];
 
 static void dump_blockmap(const char *label, int zone);
index 6e69305..de8fdbc 100644 (file)
  * SUCH DAMAGE.
  *
  */
-#include "hammer.h"
+#include <libhammer.h>
 #include <libutil.h>
 
-void   show_info(char *path);
-char   *find_pfs_mount(int pfsid, uuid_t parentuuid, int ismaster);
-double percent(int64_t value, int64_t total);
-u_int32_t count_snapshots (u_int32_t version,
-    char *pfs_snapshots, char *mountedon, int *errorp);
+#include "hammer.h"
+
+void show_info(char *path);
+static double percent(int64_t value, int64_t total);
 
 void
 hammer_cmd_info(void)
@@ -71,73 +70,63 @@ hammer_cmd_info(void)
 void
 show_info(char *path)
 {
-       struct      hammer_pseudofs_data pfs_od;
-       struct      hammer_ioc_pseudofs_rw pfs;
+       libhammer_volinfo_t hvi;
+       libhammer_pfsinfo_t pi, pi_first;
        int64_t     usedbigblocks;
        int64_t     usedbytes, rsvbytes;
        int64_t     totalbytes, freebytes;
-       struct      hammer_ioc_info info;
-       int         fd, pfs_id, ismaster, error;
+       int         error;
        char        *fsid;
-       char        *mountedon;
        char        buf[6];
        u_int32_t   sc;
 
-       fsid = mountedon = NULL;
+       fsid = NULL;
        usedbigblocks = 0;
-       pfs_id = 0;           /* Include PFS#0 */
+
        usedbytes = totalbytes = rsvbytes = freebytes = 0;
        sc = error = 0;
 
-       bzero(&info, sizeof(struct hammer_ioc_info));
-
-       /* Try to get a file descriptor based on the path given */
-       fd = open(path, O_RDONLY);
-       if (fd < 0) {
-               perror("show_info");
-               exit(EXIT_FAILURE);
-       }
-
-       if ((ioctl(fd, HAMMERIOC_GET_INFO, &info)) < 0) {
-               perror("show_info");
+       hvi = libhammer_get_volinfo(path);
+       if (hvi == NULL) {
+               perror("libhammer_get_volinfo");
                exit(EXIT_FAILURE);
        }
 
        /* Find out the UUID strings */
-       uuid_to_string(&info.vol_fsid, &fsid, NULL);
+       uuid_to_string(&hvi->vol_fsid, &fsid, NULL);
 
        /* Volume information */
        fprintf(stdout, "Volume identification\n");
-       fprintf(stdout, "\tLabel               %s\n", info.vol_name);
-       fprintf(stdout, "\tNo. Volumes         %d\n", info.nvolumes);
+       fprintf(stdout, "\tLabel               %s\n", hvi->vol_name);
+       fprintf(stdout, "\tNo. Volumes         %d\n", hvi->nvolumes);
        fprintf(stdout, "\tFSID                %s\n", fsid);
-       fprintf(stdout, "\tHAMMER Version      %d\n", info.version);
+       fprintf(stdout, "\tHAMMER Version      %d\n", hvi->version);
 
        /* Big blocks information */
-       usedbigblocks = info.bigblocks - info.freebigblocks;
+       usedbigblocks = hvi->bigblocks - hvi->freebigblocks;
 
        fprintf(stdout, "Big block information\n");
-       fprintf(stdout, "\tTotal      %10jd\n", (intmax_t)info.bigblocks);
+       fprintf(stdout, "\tTotal      %10jd\n", (intmax_t)hvi->bigblocks);
        fprintf(stdout, "\tUsed       %10jd (%.2lf%%)\n"
                        "\tReserved   %10jd (%.2lf%%)\n"
                        "\tFree       %10jd (%.2lf%%)\n",
                        (intmax_t)usedbigblocks,
-                       percent(usedbigblocks, info.bigblocks),
-                       (intmax_t)info.rsvbigblocks,
-                       percent(info.rsvbigblocks, info.bigblocks),
-                       (intmax_t)(info.freebigblocks - info.rsvbigblocks),
-                       percent(info.freebigblocks - info.rsvbigblocks,
-                               info.bigblocks));
+                       percent(usedbigblocks, hvi->bigblocks),
+                       (intmax_t)hvi->rsvbigblocks,
+                       percent(hvi->rsvbigblocks, hvi->bigblocks),
+                       (intmax_t)(hvi->freebigblocks - hvi->rsvbigblocks),
+                       percent(hvi->freebigblocks - hvi->rsvbigblocks,
+                               hvi->bigblocks));
        fprintf(stdout, "Space information\n");
 
        /* Space information */
-       totalbytes = (info.bigblocks << HAMMER_LARGEBLOCK_BITS);
+       totalbytes = (hvi->bigblocks << HAMMER_LARGEBLOCK_BITS);
        usedbytes = (usedbigblocks << HAMMER_LARGEBLOCK_BITS);
-       rsvbytes = (info.rsvbigblocks << HAMMER_LARGEBLOCK_BITS);
-       freebytes = ((info.freebigblocks - info.rsvbigblocks)
+       rsvbytes = (hvi->rsvbigblocks << HAMMER_LARGEBLOCK_BITS);
+       freebytes = ((hvi->freebigblocks - hvi->rsvbigblocks)
            << HAMMER_LARGEBLOCK_BITS);
 
-       fprintf(stdout, "\tNo. Inodes %10jd\n", (intmax_t)info.inodes);
+       fprintf(stdout, "\tNo. Inodes %10jd\n", (intmax_t)hvi->inodes);
        humanize_number(buf, sizeof(buf)  - (totalbytes < 0 ? 0 : 1),
            totalbytes, "", HN_AUTOSCALE, HN_DECIMAL | HN_NOSPACE | HN_B);
        fprintf(stdout, "\tTotal size     %6s (%jd bytes)\n",
@@ -162,121 +151,30 @@ show_info(char *path)
        fprintf(stdout, "PFS information\n");
        fprintf(stdout, "\tPFS ID  Mode    Snaps  Mounted on\n");
 
-       while(pfs_id < HAMMER_MAX_PFS) {
-               bzero(&pfs, sizeof(pfs));
-               bzero(&pfs_od, sizeof(pfs_od));
-               pfs.pfs_id = pfs_id;
-               pfs.ondisk = &pfs_od;
-               pfs.bytes = sizeof(pfs_od);
-               pfs.version = HAMMER_IOC_PSEUDOFS_VERSION;
-               if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, &pfs) >= 0) {
-                       ismaster = (pfs_od.mirror_flags & HAMMER_PFSD_SLAVE)
-                           ? 0 : 1;
-                       if (pfs_id == 0)
-                               mountedon = strdup(path);
-                       else
-                               mountedon = find_pfs_mount(pfs_id,
-                                   info.vol_fsid, ismaster);
+       /* Iterate all the PFSs found */
+       pi_first = libhammer_get_first_pfs(hvi);
+       for (pi = pi_first; pi != NULL; pi = libhammer_get_next_pfs(pi)) {
+               fprintf(stdout, "\t%6d  %-6s",
+                   pi->pfs_id, (pi->ismaster ? "MASTER" : "SLAVE"));
 
-                       sc = count_snapshots(info.version, pfs_od.snapshots,
-                           mountedon, &error);
+               snprintf(buf, 6, "%d", pi->snapcount);
+               fprintf(stdout, " %6s  ", (pi->head.error && pi->snapcount == 0) ? "-" : buf);
 
-                       fprintf(stdout, "\t%6d  %-6s",
-                           pfs_id, (ismaster ? "MASTER" : "SLAVE"));
-
-                       snprintf(buf, 6, "%d", sc);
-                       fprintf(stdout, " %6s  ", (error && sc == 0) ? "-" : buf);
+               if (pi->mountedon)
+                       fprintf(stdout, "%s", pi->mountedon);
+               else
+                       fprintf(stdout, "not mounted");
 
-                       if (mountedon)
-                               fprintf(stdout, "%s", mountedon);
-                       else
-                               fprintf(stdout, "not mounted");
-                       fprintf(stdout, "\n");
-               }
-               pfs_id++;
+               fprintf(stdout, "\n");
        }
 
        free(fsid);
-       free(mountedon);
-}
-
-char *
-find_pfs_mount(int pfsid, uuid_t parentuuid, int ismaster)
-{
-       struct hammer_ioc_info hi;
-       struct statfs *mntbuf;
-       int mntsize;
-       int curmount;
-       int fd;
-       size_t  mntbufsize;
-       char *trailstr;
-       char *retval;
-
-       retval = NULL;
-
-       /* Do not continue if there are no mounted filesystems */
-       mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
-       if (mntsize <= 0)
-               return retval;
-
-       mntbufsize = (mntsize) * sizeof(struct statfs);
-       mntbuf = malloc(mntbufsize);
-       if (mntbuf == NULL) {
-               perror("show_info");
-               exit(EXIT_FAILURE);
-       }
-
-       mntsize = getfsstat(mntbuf, (long)mntbufsize, MNT_NOWAIT);
-       curmount = mntsize - 1;
 
-       asprintf(&trailstr, ":%05d", pfsid);
+       libhammer_free_volinfo(hvi);
 
-       /*
-        * Iterate all the mounted points looking for the PFS passed to
-        * this function.
-        */
-       while(curmount >= 0) {
-               /*
-                * We need to avoid that PFS belonging to other HAMMER
-                * filesystems are showed as mounted, so we compare
-                * against the FSID, which is presumable to be unique.
-                */
-               bzero(&hi, sizeof(hi));
-               if ((fd = open(mntbuf[curmount].f_mntfromname, O_RDONLY)) < 0) {
-                       curmount--;
-                       continue;
-               }
-
-               if ((ioctl(fd, HAMMERIOC_GET_INFO, &hi)) < 0) {
-                       curmount--;
-                       continue;
-               }
-
-               if (strstr(mntbuf[curmount].f_mntfromname, trailstr) != NULL &&
-                   (uuid_compare(&hi.vol_fsid, &parentuuid, NULL)) == 0) {
-                       if (ismaster) {
-                               if (strstr(mntbuf[curmount].f_mntfromname,
-                                   "@@-1") != NULL) {
-                                       retval =
-                                           strdup(mntbuf[curmount].f_mntonname);
-                                       break;
-                               }
-                       } else {
-                               if (strstr(mntbuf[curmount].f_mntfromname,
-                                   "@@0x") != NULL ) {
-                                       retval =
-                                           strdup(mntbuf[curmount].f_mntonname);
-                                       break;
-                               }
-                       }
-               }
-               curmount--;
-       }
-       free(trailstr);
-       return retval;
 }
 
-double
+static double
 percent(int64_t value, int64_t total)
 {
        /* Avoid divide-by-zero */
@@ -285,58 +183,3 @@ percent(int64_t value, int64_t total)
 
        return ((value * 100.0) / (double)total);
 }
-
-u_int32_t
-count_snapshots(u_int32_t version, char *pfs_snapshots, char *mountedon, int *errorp)
-{
-       struct hammer_ioc_snapshot snapinfo;
-       char *snapshots_path, *fpath;
-       struct dirent *den;
-       struct stat st;
-       DIR *dir;
-       u_int32_t snapshot_count = 0;
-       int fd;
-
-       bzero(&snapinfo, sizeof(struct hammer_ioc_snapshot));
-
-        fd = open(mountedon, O_RDONLY);
-        if (fd < 0) {
-               *errorp = errno;
-               return 0;
-        }
-
-       if (version < 3) {
-               /*
-                * old style: count the number of softlinks in the snapshots dir
-                */
-               if (pfs_snapshots[0])
-                       snapshots_path = pfs_snapshots;
-               else
-                       asprintf(&snapshots_path, "%s/snapshots", mountedon);
-               if ((dir = opendir(snapshots_path)) != NULL) {
-                       while ((den = readdir(dir)) != NULL) {
-                               if (den->d_name[0] == '.')
-                                       continue;
-                               asprintf(&fpath, "%s/%s", snapshots_path,
-                                   den->d_name);
-                               if (lstat(fpath, &st) == 0 &&
-                                   S_ISLNK(st.st_mode))
-                                       snapshot_count++;
-                               free(fpath);
-                       }
-                       closedir(dir);
-               }
-       } else {
-               /*
-                * new style: file system meta-data
-                */
-               do {
-                       if (ioctl(fd, HAMMERIOC_GET_SNAPSHOT, &snapinfo) < 0) {
-                               *errorp = errno;
-                               return 0;
-                       }
-                       snapshot_count += snapinfo.count;
-               } while (snapinfo.head.error == 0 && snapinfo.count);
-       }
-       return snapshot_count;
-}
index ea9cdce..2ca67a8 100644 (file)
@@ -523,7 +523,7 @@ snapshot_ls(const char *path)
         }
 
        ismaster = (pfs_od.mirror_flags & HAMMER_PFSD_SLAVE) ? 0 : 1;
-       mntpoint = find_pfs_mount(pfs.pfs_id, info.vol_fsid, ismaster);
+       mntpoint = libhammer_find_pfs_mount(pfs.pfs_id, info.vol_fsid, ismaster);
 
        /* Note the degenerate case of PFS #0 */
        printf("Snapshots on %s\tPFS #%d\n",
index 26d46ca..988e05d 100644 (file)
@@ -60,6 +60,8 @@
 #include "hammer_util.h"
 #include <vfs/hammer/hammer_ioctl.h>
 
+#include <libhammer.h>
+
 extern int RecurseOpt;
 extern int VerboseOpt;
 extern int QuietOpt;