Module Name:    src
Committed By:   simonb
Date:           Sun Jun 19 11:31:19 UTC 2022

Modified Files:
        src/usr.bin/fstat: Makefile fstat.c fstat.h
Added Files:
        src/usr.bin/fstat: zfs.c zfs_znode.h

Log Message:
Add basic ZFS support to fstat(1).
XXX: Use a local zfs_znode.h for a znode_t definition because the ZFS
kernel code makes it too hard to include headers from userland.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/usr.bin/fstat/Makefile
cvs rdiff -u -r1.114 -r1.115 src/usr.bin/fstat/fstat.c
cvs rdiff -u -r1.10 -r1.11 src/usr.bin/fstat/fstat.h
cvs rdiff -u -r0 -r1.1 src/usr.bin/fstat/zfs.c src/usr.bin/fstat/zfs_znode.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/fstat/Makefile
diff -u src/usr.bin/fstat/Makefile:1.26 src/usr.bin/fstat/Makefile:1.27
--- src/usr.bin/fstat/Makefile:1.26	Sun Oct 13 07:28:15 2019
+++ src/usr.bin/fstat/Makefile	Sun Jun 19 11:31:19 2022
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.26 2019/10/13 07:28:15 mrg Exp $
+#	$NetBSD: Makefile,v 1.27 2022/06/19 11:31:19 simonb Exp $
 #	from: @(#)Makefile	8.1 (Berkeley) 6/6/93
 
 .include <bsd.own.mk>
@@ -6,6 +6,9 @@
 USE_FORT?= yes	# setuid
 PROG=	fstat
 SRCS=	fstat.c isofs.c ntfs.c ptyfs.c tmpfs.c misc.c
+.if (${MKZFS} != "no")
+SRCS+=	zfs.c
+.endif
 DPADD=	${LIBKVM}
 LDADD=	-lkvm
 BINGRP=	kmem
@@ -16,6 +19,9 @@ CPPFLAGS+= -I${NETBSDSRCDIR}/sys
 .if (${USE_INET6} != "no")
 CPPFLAGS+=-DINET6
 .endif
+.if (${MKZFS} != "no")
+CPPFLAGS+=-DHAVE_ZFS
+.endif
 CPPFLAGS+=-D_KMEMUSER
 
 LDADD+=-lutil

Index: src/usr.bin/fstat/fstat.c
diff -u src/usr.bin/fstat/fstat.c:1.114 src/usr.bin/fstat/fstat.c:1.115
--- src/usr.bin/fstat/fstat.c:1.114	Wed Aug 26 23:08:29 2020
+++ src/usr.bin/fstat/fstat.c	Sun Jun 19 11:31:19 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: fstat.c,v 1.114 2020/08/26 23:08:29 christos Exp $	*/
+/*	$NetBSD: fstat.c,v 1.115 2022/06/19 11:31:19 simonb Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1993
@@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
 #else
-__RCSID("$NetBSD: fstat.c,v 1.114 2020/08/26 23:08:29 christos Exp $");
+__RCSID("$NetBSD: fstat.c,v 1.115 2022/06/19 11:31:19 simonb Exp $");
 #endif
 #endif /* not lint */
 
@@ -611,6 +611,12 @@ vfilestat(struct vnode *vp, struct files
 			if (!tmpfs_filestat(vp, fsp))
 				badtype = "error";
 			break;
+#ifdef HAVE_ZFS
+		case VT_ZFS:
+			if (!zfs_filestat(vp, fsp))
+				badtype = "error";
+			break;
+#endif
 		case VT_NULL:
 		case VT_OVERLAY:
 		case VT_UMAP:

Index: src/usr.bin/fstat/fstat.h
diff -u src/usr.bin/fstat/fstat.h:1.10 src/usr.bin/fstat/fstat.h:1.11
--- src/usr.bin/fstat/fstat.h:1.10	Fri Sep  6 17:08:22 2019
+++ src/usr.bin/fstat/fstat.h	Sun Jun 19 11:31:19 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: fstat.h,v 1.10 2019/09/06 17:08:22 christos Exp $	*/
+/*	$NetBSD: fstat.h,v 1.11 2022/06/19 11:31:19 simonb Exp $	*/
 /*-
  * Copyright (c) 1988, 1993
  *	The Regents of the University of California.  All rights reserved.
@@ -60,4 +60,5 @@ int	isofs_filestat(struct vnode *, struc
 int	ntfs_filestat(struct vnode *, struct filestat *);
 int	ptyfs_filestat(struct vnode *, struct filestat *);
 int	tmpfs_filestat(struct vnode *, struct filestat *);
+int	zfs_filestat(struct vnode *, struct filestat *);
 void	oprint(struct file *, const char *);

Added files:

Index: src/usr.bin/fstat/zfs.c
diff -u /dev/null src/usr.bin/fstat/zfs.c:1.1
--- /dev/null	Sun Jun 19 11:31:19 2022
+++ src/usr.bin/fstat/zfs.c	Sun Jun 19 11:31:19 2022
@@ -0,0 +1,75 @@
+/*	$NetBSD: zfs.c,v 1.1 2022/06/19 11:31:19 simonb Exp $	*/
+
+/*-
+ * Copyright (c) 2022 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Simon Burge.
+ *
+ * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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>
+__RCSID("$NetBSD: zfs.c,v 1.1 2022/06/19 11:31:19 simonb Exp $");
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/vnode.h>
+#define __EXPOSE_MOUNT
+#include <sys/mount.h>
+
+#include "zfs_znode.h"
+
+#include <kvm.h>
+#include <err.h>
+#include "fstat.h"
+
+int
+zfs_filestat(struct vnode *vp, struct filestat *fsp)
+{
+	znode_t inode;
+	struct mount mt;
+
+	if (!KVM_READ(VTOZ(vp), &inode, sizeof (inode))) {
+		dprintf("can't read inode at %p for pid %d", VTOZ(vp), Pid);
+		return 0;
+	}
+	if (!KVM_READ(vp->v_mount, &mt, sizeof(mt))) {
+		dprintf("can't read mount at %p for pid %d",
+		    vp->v_mount, Pid);
+		return 0;
+	}
+	/*
+	 * XXX: fsid should be something like
+	 *    ((struct zfsvfs *)vp->v_mount->mnt_data)->
+	 *    z_os->os_ds_dataset->ds_fsid_guid
+	 * but ZFS headers are pretty much impossible to include
+	 * from userland.
+	 */
+	fsp->fsid = mt.mnt_stat.f_fsidx.__fsid_val[0];
+	fsp->fileid = inode.z_id;
+	fsp->mode = inode.z_mode;
+	fsp->size = inode.z_size;
+	fsp->rdev = 0;
+	return 1;
+}
Index: src/usr.bin/fstat/zfs_znode.h
diff -u /dev/null src/usr.bin/fstat/zfs_znode.h:1.1
--- /dev/null	Sun Jun 19 11:31:19 2022
+++ src/usr.bin/fstat/zfs_znode.h	Sun Jun 19 11:31:19 2022
@@ -0,0 +1,103 @@
+/*	$NetBSD: zfs_znode.h,v 1.1 2022/06/19 11:31:19 simonb Exp $	*/
+
+/*
+ * XXX From: external/cddl/osnet/dist/uts/common/fs/zfs/sys/zfs_znode.h
+ *
+ * Grotty hack to define a "simple" znode_t so we don't need to
+ * try to include the real zfs_znode.h which isn't easily possible
+ * from userland due to all sorts of kernel only defines and structs
+ * being required.
+ */
+
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
+ * Copyright (c) 2014 Integros [integros.com]
+ */
+
+
+#include <miscfs/genfs/genfs_node.h>
+
+#define	VTOZ(VP)	((znode_t *)(VP)->v_data)
+
+#define	ulong_t		unsigned long
+#define	uint_t		unsigned int
+#define	zfs_acl_t	void
+
+struct avl_node;
+typedef struct sa_handle sa_handle_t;
+typedef int boolean_t;
+
+typedef struct avl_tree {
+	struct avl_node	*avl_root;
+	int		(*avl_compar)(const void *, const void *);
+	size_t		avl_offset;
+	ulong_t		avl_numnodes;
+	size_t		avl_size;
+} avl_tree_t;
+
+typedef struct list_node {
+	struct list_node *list_next;
+	struct list_node *list_prev;
+} list_node_t;
+
+typedef struct znode {
+#ifdef __NetBSD__
+	struct genfs_node z_gnode;
+#endif
+	struct zfsvfs	*z_zfsvfs;
+	vnode_t		*z_vnode;
+	uint64_t	z_id;		/* object ID for this znode */
+#ifdef illumos
+	kmutex_t	z_lock;		/* znode modification lock */
+	krwlock_t	z_parent_lock;	/* parent lock for directories */
+	krwlock_t	z_name_lock;	/* "master" lock for dirent locks */
+	zfs_dirlock_t	*z_dirlocks;	/* directory entry lock list */
+#endif
+	kmutex_t	z_range_lock;	/* protects changes to z_range_avl */
+	avl_tree_t	z_range_avl;	/* avl tree of file range locks */
+	uint8_t		z_unlinked;	/* file has been unlinked */
+	uint8_t		z_atime_dirty;	/* atime needs to be synced */
+	uint8_t		z_zn_prefetch;	/* Prefetch znodes? */
+	uint8_t		z_moved;	/* Has this znode been moved? */
+	uint_t		z_blksz;	/* block size in bytes */
+	uint_t		z_seq;		/* modification sequence number */
+	uint64_t	z_mapcnt;	/* number of pages mapped to file */
+	uint64_t	z_gen;		/* generation (cached) */
+	uint64_t	z_size;		/* file size (cached) */
+	uint64_t	z_atime[2];	/* atime (cached) */
+	uint64_t	z_links;	/* file links (cached) */
+	uint64_t	z_pflags;	/* pflags (cached) */
+	uint64_t	z_uid;		/* uid fuid (cached) */
+	uint64_t	z_gid;		/* gid fuid (cached) */
+	mode_t		z_mode;		/* mode (cached) */
+	uint32_t	z_sync_cnt;	/* synchronous open count */
+	kmutex_t	z_acl_lock;	/* acl data lock */
+	zfs_acl_t	*z_acl_cached;	/* cached acl */
+	list_node_t	z_link_node;	/* all znodes in fs link */
+	sa_handle_t	*z_sa_hdl;	/* handle to sa data */
+	boolean_t	z_is_sa;	/* are we native sa? */
+#ifdef __NetBSD__
+	struct lockf	*z_lockf;	/* head of byte-level lock list */
+#endif
+} znode_t;

Reply via email to