Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/fstyp fstyp: Add HAMMER1/2 support



details:   https://anonhg.NetBSD.org/src/rev/368087c46db7
branches:  trunk
changeset: 968025:368087c46db7
user:      tkusumi <tkusumi%NetBSD.org@localhost>
date:      Wed Jan 01 08:56:41 2020 +0000

description:
fstyp: Add HAMMER1/2 support

FreeBSD has recently imported HAMMER1/2 support from DragonFlyBSD,
so why not in NetBSD as well.

taken-from: DragonFlyBSD

diffstat:

 usr.sbin/fstyp/Makefile       |     4 +-
 usr.sbin/fstyp/fstyp.8        |    12 +-
 usr.sbin/fstyp/fstyp.c        |     8 +-
 usr.sbin/fstyp/fstyp.h        |    10 +-
 usr.sbin/fstyp/hammer.c       |   217 ++++++
 usr.sbin/fstyp/hammer2.c      |   319 +++++++++
 usr.sbin/fstyp/hammer2_disk.h |  1376 +++++++++++++++++++++++++++++++++++++++++
 usr.sbin/fstyp/hammer_disk.h  |  1092 ++++++++++++++++++++++++++++++++
 8 files changed, 3029 insertions(+), 9 deletions(-)

diffs (truncated from 3147 to 300 lines):

diff -r 2778485e1a1b -r 368087c46db7 usr.sbin/fstyp/Makefile
--- a/usr.sbin/fstyp/Makefile   Wed Jan 01 06:14:29 2020 +0000
+++ b/usr.sbin/fstyp/Makefile   Wed Jan 01 08:56:41 2020 +0000
@@ -1,9 +1,9 @@
-#      $NetBSD: Makefile,v 1.8 2019/12/27 11:15:06 tkusumi Exp $
+#      $NetBSD: Makefile,v 1.9 2020/01/01 08:56:41 tkusumi Exp $
 
 .include <bsd.own.mk>
 
 PROG=  fstyp
-SRCS=  apfs.c cd9660.c exfat.c ext2fs.c fstyp.c hfsplus.c msdosfs.c ntfs.c ufs.c
+SRCS=  apfs.c cd9660.c exfat.c ext2fs.c fstyp.c hammer.c hammer2.c hfsplus.c msdosfs.c ntfs.c ufs.c
 
 .if (${MKZFS} != "no")
 SRCS+= zfs.c
diff -r 2778485e1a1b -r 368087c46db7 usr.sbin/fstyp/fstyp.8
--- a/usr.sbin/fstyp/fstyp.8    Wed Jan 01 06:14:29 2020 +0000
+++ b/usr.sbin/fstyp/fstyp.8    Wed Jan 01 08:56:41 2020 +0000
@@ -1,4 +1,4 @@
-.\" $NetBSD: fstyp.8,v 1.4 2019/11/21 15:24:17 tkusumi Exp $
+.\" $NetBSD: fstyp.8,v 1.5 2020/01/01 08:56:41 tkusumi Exp $
 .\"
 .\" Copyright (c) 2017 The NetBSD Foundation, Inc.
 .\" Copyright (c) 2016 The DragonFly Project
@@ -34,7 +34,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd November 19, 2019
+.Dd January 1, 2020
 .Dt FSTYP 8
 .Os
 .Sh NAME
@@ -48,7 +48,7 @@
 The
 .Nm
 utility is used to determine the file system type on a given device.
-It can recognize ISO-9660, exFAT, Ext2, FAT, NTFS, and UFS file systems.
+It can recognize ISO-9660, exFAT, Ext2, FAT, NTFS, UFS, HAMMER, and HAMMER2 file systems.
 When the
 .Fl u
 flag is specified,
@@ -74,9 +74,15 @@
 .It
 ufs
 .It
+hammer
+.It
+hammer2
+.It
 zfs
 .El
 .Pp
+Note that HAMMER filesystem consists of more than one volumes requires a path in blkdevs format.
+.Pp
 Because
 .Nm
 is built specifically to detect file system types, it differs from
diff -r 2778485e1a1b -r 368087c46db7 usr.sbin/fstyp/fstyp.c
--- a/usr.sbin/fstyp/fstyp.c    Wed Jan 01 06:14:29 2020 +0000
+++ b/usr.sbin/fstyp/fstyp.c    Wed Jan 01 08:56:41 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fstyp.c,v 1.7 2019/12/28 08:22:30 tkusumi Exp $        */
+/*     $NetBSD: fstyp.c,v 1.8 2020/01/01 08:56:41 tkusumi Exp $        */
 
 /*-
  * Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  *
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: fstyp.c,v 1.7 2019/12/28 08:22:30 tkusumi Exp $");
+__RCSID("$NetBSD: fstyp.c,v 1.8 2020/01/01 08:56:41 tkusumi Exp $");
 
 #include <sys/disklabel.h>
 #include <sys/dkio.h>
@@ -55,7 +55,7 @@
 
 #include "fstyp.h"
 
-#define        LABEL_LEN       256
+#define        LABEL_LEN       512
 
 bool show_label = false;
 
@@ -76,6 +76,8 @@
        { "msdosfs", &fstyp_msdosfs, false, NULL },
        { "ntfs", &fstyp_ntfs, false, NTFS_ENC },
        { "ufs", &fstyp_ufs, false, NULL },
+       { "hammer", &fstyp_hammer, false, NULL },
+       { "hammer2", &fstyp_hammer2, false, NULL },
 #ifdef HAVE_ZFS
        { "zfs", &fstyp_zfs, true, NULL },
 #endif
diff -r 2778485e1a1b -r 368087c46db7 usr.sbin/fstyp/fstyp.h
--- a/usr.sbin/fstyp/fstyp.h    Wed Jan 01 06:14:29 2020 +0000
+++ b/usr.sbin/fstyp/fstyp.h    Wed Jan 01 08:56:41 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fstyp.h,v 1.6 2019/12/28 08:22:30 tkusumi Exp $        */
+/*     $NetBSD: fstyp.h,v 1.7 2020/01/01 08:56:41 tkusumi Exp $        */
 
 /*-
  * Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -41,6 +41,9 @@
 
 #include <stdbool.h>
 
+/* Undefine this on FreeBSD and NetBSD. */
+//#define HAS_DEVPATH
+
 /* The spec doesn't seem to permit UTF-16 surrogates; definitely LE. */
 #define        EXFAT_ENC       "UCS-2LE"
 /*
@@ -63,8 +66,13 @@
 int    fstyp_msdosfs(FILE *, char *, size_t);
 int    fstyp_ntfs(FILE *, char *, size_t);
 int    fstyp_ufs(FILE *, char *, size_t);
+int    fstyp_hammer(FILE *, char *, size_t);
+int    fstyp_hammer2(FILE *, char *, size_t);
 #ifdef HAVE_ZFS
 int    fstyp_zfs(FILE *, char *, size_t);
 #endif
 
+int    fsvtyp_hammer(const char *blkdevs, char *label, size_t size);
+int    fsvtyp_hammer_partial(const char *blkdevs, char *label, size_t size);
+
 #endif /* !FSTYP_H */
diff -r 2778485e1a1b -r 368087c46db7 usr.sbin/fstyp/hammer.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/fstyp/hammer.c   Wed Jan 01 08:56:41 2020 +0000
@@ -0,0 +1,217 @@
+/*        $NetBSD: hammer.c,v 1.1 2020/01/01 08:56:41 tkusumi Exp $      */
+
+/*-
+ * Copyright (c) 2016-2019 The DragonFly Project
+ * Copyright (c) 2016-2019 Tomohiro Kusumi <tkusumi%netbsd.org@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: hammer.c,v 1.1 2020/01/01 08:56:41 tkusumi Exp $");
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <err.h>
+#include <assert.h>
+
+#include "fstyp.h"
+#include "hammer_disk.h"
+
+static hammer_volume_ondisk_t
+read_ondisk(FILE *fp)
+{
+       hammer_volume_ondisk_t ondisk;
+
+       ondisk = read_buf(fp, 0, sizeof(*ondisk));
+       if (ondisk == NULL)
+               err(1, "failed to read ondisk");
+
+       return (ondisk);
+}
+
+static int
+test_ondisk(const hammer_volume_ondisk_t ondisk)
+{
+       static int count = 0;
+       static hammer_uuid_t fsid, fstype;
+       static char label[64];
+
+       if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME &&
+           ondisk->vol_signature != HAMMER_FSBUF_VOLUME_REV)
+               return (1);
+       if (ondisk->vol_rootvol != HAMMER_ROOT_VOLNO)
+               return (2);
+       if (ondisk->vol_no < 0 || ondisk->vol_no > HAMMER_MAX_VOLUMES - 1)
+               return (3);
+       if (ondisk->vol_count < 1 || ondisk->vol_count > HAMMER_MAX_VOLUMES)
+               return (4);
+
+       if (count == 0) {
+               count = ondisk->vol_count;
+               assert(count != 0);
+               memcpy(&fsid, &ondisk->vol_fsid, sizeof(fsid));
+               memcpy(&fstype, &ondisk->vol_fstype, sizeof(fstype));
+               strncpy(label, ondisk->vol_label, sizeof(label));
+       } else {
+               if (ondisk->vol_count != count)
+                       return (5);
+               if (memcmp(&ondisk->vol_fsid, &fsid, sizeof(fsid)))
+                       return (6);
+               if (memcmp(&ondisk->vol_fstype, &fstype, sizeof(fstype)))
+                       return (7);
+               if (strncmp(ondisk->vol_label, label, sizeof(label)))
+                       return (8);
+       }
+
+       return (0);
+}
+
+int
+fstyp_hammer(FILE *fp, char *label, size_t size)
+{
+       hammer_volume_ondisk_t ondisk;
+       int error = 1;
+#ifdef HAS_DEVPATH
+       const char *p;
+#endif
+       ondisk = read_ondisk(fp);
+       if (ondisk->vol_no != HAMMER_ROOT_VOLNO)
+               goto done;
+       if (ondisk->vol_count != 1)
+               goto done;
+       if (test_ondisk(ondisk))
+               goto done;
+
+       /*
+        * fstyp_function in DragonFly takes an additional devpath argument
+        * which doesn't exist in FreeBSD and NetBSD.
+        */
+#ifdef HAS_DEVPATH
+       /* Add device name to help support multiple autofs -media mounts. */
+       p = strrchr(devpath, '/');
+       if (p) {
+               p++;
+               if (*p == 0)
+                       strlcpy(label, ondisk->vol_label, size);
+               else
+                       snprintf(label, size, "%s_%s", ondisk->vol_label, p);
+       } else
+               snprintf(label, size, "%s_%s", ondisk->vol_label, devpath);
+#else
+       strlcpy(label, ondisk->vol_label, size);
+#endif
+       error = 0;
+done:
+       free(ondisk);
+       return (error);
+}
+
+static int
+test_volume(const char *volpath)
+{
+       hammer_volume_ondisk_t ondisk;
+       FILE *fp;
+       int volno = -1;
+
+       if ((fp = fopen(volpath, "r")) == NULL)
+               err(1, "failed to open %s", volpath);
+
+       ondisk = read_ondisk(fp);
+       fclose(fp);
+       if (test_ondisk(ondisk))
+               goto done;
+
+       volno = ondisk->vol_no;
+done:
+       free(ondisk);
+       return (volno);
+}
+
+static int
+__fsvtyp_hammer(const char *blkdevs, char *label, size_t size, int partial)
+{
+       hammer_volume_ondisk_t ondisk;
+       FILE *fp;
+       char *dup, *p, *volpath, x[HAMMER_MAX_VOLUMES];
+       int i, volno, error = 1;
+
+       memset(x, 0, sizeof(x));
+       dup = strdup(blkdevs);
+       p = dup;
+
+       volpath = NULL;
+       while (p) {
+               volpath = p;
+               if ((p = strchr(p, ':')) != NULL)
+                       *p++ = '\0';
+               if ((volno = test_volume(volpath)) == -1)
+                       break;



Home | Main Index | Thread Index | Old Index