From ab6e54a2c2a6804b40a40732116d057c6401a75e Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 28 Feb 2018 11:18:44 +0100
Subject: [PATCH] WIP fstat hammer2 support.

---
 sys/vfs/hammer2/hammer2.h |  4 ++-
 usr.bin/fstat/Makefile    |  2 +-
 usr.bin/fstat/fstat.c     |  4 +++
 usr.bin/fstat/fstat.h     |  1 +
 usr.bin/fstat/hammer2.c   | 75 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 usr.bin/fstat/hammer2.c

diff --git a/sys/vfs/hammer2/hammer2.h b/sys/vfs/hammer2/hammer2.h
index d9ff3ee1ca..9165b830c2 100644
--- a/sys/vfs/hammer2/hammer2.h
+++ b/sys/vfs/hammer2/hammer2.h
@@ -1214,13 +1214,15 @@ TAILQ_HEAD(hammer2_pfslist, hammer2_pfs);
 /*
  * Misc
  */
-#if defined(_KERNEL)
+#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
 
 MALLOC_DECLARE(M_HAMMER2);
 
 #define VTOI(vp)	((hammer2_inode_t *)(vp)->v_data)
 #define ITOV(ip)	((ip)->vp)
+#endif
 
+#if defined(_KERNEL)
 /*
  * Currently locked chains retain the locked buffer cache buffer for
  * indirect blocks, and indirect blocks can be one of two sizes.  The
diff --git a/usr.bin/fstat/Makefile b/usr.bin/fstat/Makefile
index b1f1162413..0276fe9001 100644
--- a/usr.bin/fstat/Makefile
+++ b/usr.bin/fstat/Makefile
@@ -2,7 +2,7 @@
 # $FreeBSD: src/usr.bin/fstat/Makefile,v 1.4.2.1 2000/07/02 10:20:24 ps Exp $
 
 PROG=	fstat
-SRCS=	cd9660.c ext2fs.c fstat.c hammer.c msdosfs.c ntfs.c tmpfs.c
+SRCS=	cd9660.c ext2fs.c fstat.c hammer.c hammer2.c msdosfs.c ntfs.c tmpfs.c
 CFLAGS+=-I${.CURDIR}/../../sys
 DPADD=	${LIBKVM}
 LDADD=	-lkvm
diff --git a/usr.bin/fstat/fstat.c b/usr.bin/fstat/fstat.c
index 876811dc76..6bc6637d0b 100644
--- a/usr.bin/fstat/fstat.c
+++ b/usr.bin/fstat/fstat.c
@@ -471,6 +471,10 @@ vtrans(struct vnode *vp, struct nchandle *ncr, int i, int flag, off_t off)
 			if (!hammer_filestat(&vn, &fst))
 				badtype = "error";
 			break;
+		case VT_HAMMER2:
+			if (!hammer2_filestat(&vn, &fst))
+				badtype = "error";
+			break;
 		case VT_TMPFS:
 			if (!tmpfs_filestat(&vn, &fst))
 				badtype = "error";
diff --git a/usr.bin/fstat/fstat.h b/usr.bin/fstat/fstat.h
index e5fd5df196..4c4b82e3a5 100644
--- a/usr.bin/fstat/fstat.h
+++ b/usr.bin/fstat/fstat.h
@@ -86,6 +86,7 @@ udev_t makeudev(int, int);
 /* Additional filesystem types */
 int ext2fs_filestat(struct vnode *, struct filestat *);
 int hammer_filestat(struct vnode *, struct filestat *);
+int hammer2_filestat(struct vnode *, struct filestat *);
 int isofs_filestat(struct vnode *, struct filestat *);
 int msdosfs_filestat(struct vnode *, struct filestat *);
 int ntfs_filestat(struct vnode *, struct filestat *);
diff --git a/usr.bin/fstat/hammer2.c b/usr.bin/fstat/hammer2.c
new file mode 100644
index 0000000000..0027f7f106
--- /dev/null
+++ b/usr.bin/fstat/hammer2.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2018 The DragonFly Project.  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.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
+ * COPYRIGHT HOLDERS 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/param.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+
+#include <kvm.h>
+#include <stdio.h>
+
+#define _KERNEL_STRUCTURES
+#include <sys/vnode.h>
+#include <sys/mount.h>
+#include <stddef.h>
+#include <vfs/hammer2/hammer2.h>
+#include <vfs/hammer2/hammer2_disk.h>
+#undef _KERNEL_STRUCTURES
+
+#include "fstat.h"
+
+int
+hammer2_filestat(struct vnode *vp, struct filestat *fsp)
+{
+	struct hammer2_inode ino;
+
+	if (!kread(VTOI(vp), &ino, sizeof(ino))) {
+		dprintf(stderr, "can't read hammer2_inode at %p for pid %d\n",
+		    (void *)VTOI(vp), Pid);
+		return 0;
+	}
+
+#if 0
+	fsp->fsid = pfsm.fsid_udev ^ (uint32_t)ino.obj_asof ^
+	    (uint32_t)(ino.obj_asof >> 32);
+#else /* XXX */
+	fsp->fsid = 8888888;
+#endif
+	fsp->mode = ino.meta.mode | mtrans(vp->v_type);
+#if 0
+	fsp->fileid = (long)ino.ino_leaf.base.obj_id;
+#else /* XXX */
+	fsp->fileid = 9999999;
+#endif
+	fsp->size = ino.meta.size;
+	fsp->rdev = dev2udev(vp->v_rdev);
+	return 1;
+}
-- 
2.15.1

