From c55024b789979f33c87d68c8ec5b9f260f035dc7 Mon Sep 17 00:00:00 2001 From: lulf Date: Mon, 5 Nov 2007 23:15:03 +0000 Subject: - Add ZFS-support to fstat(1). This allows ZFS-filsystems to be including in the open file-listing. It is added as a separate source file, so it can respect WITH_/WITHOUT_CDDL as compile-flags. - The warnlevel of the Makefile was decreased to quell solaris #pragma warnings. - Expect that fstat(1) doesn't work with kernel compiled with DEBUG_VFS_LOCKS/DEBUG_LOCKS for now. Approved by: pjd (mentor) --- usr.bin/fstat/Makefile | 9 ++++ usr.bin/fstat/fstat.c | 19 +++++++ usr.bin/fstat/fstat.h | 6 +++ usr.bin/fstat/zfs.c | 132 +++++++++++++++++++++++++++++++++++++++++++++ usr.bin/fstat/zfs/Makefile | 23 ++++++++ usr.bin/fstat/zfs/zfs.c | 132 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 321 insertions(+) create mode 100644 usr.bin/fstat/zfs.c create mode 100644 usr.bin/fstat/zfs/Makefile create mode 100644 usr.bin/fstat/zfs/zfs.c diff --git a/usr.bin/fstat/Makefile b/usr.bin/fstat/Makefile index 8ca57b9..baff3eb 100644 --- a/usr.bin/fstat/Makefile +++ b/usr.bin/fstat/Makefile @@ -1,6 +1,8 @@ # @(#)Makefile 8.1 (Berkeley) 6/6/93 # $FreeBSD$ +.include + PROG= fstat SRCS= cd9660.c fstat.c msdosfs.c DPADD= ${LIBKVM} @@ -11,4 +13,11 @@ WARNS?= 6 CFLAGS+=-D_KVM_VNODE +.if ${MK_CDDL} != "no" +CFLAGS+= -DZFS +OBJS+= zfs/zfs.o +SUBDIR= zfs +zfs/zfs.o: zfs +.endif + .include diff --git a/usr.bin/fstat/fstat.c b/usr.bin/fstat/fstat.c index 26c9ad4..bc6467a1 100644 --- a/usr.bin/fstat/fstat.c +++ b/usr.bin/fstat/fstat.c @@ -524,6 +524,11 @@ vtrans(struct vnode *vp, int i, int flag) } else if (!strcmp("isofs", tagstr)) { if (!isofs_filestat(&vn, &fst)) badtype = "error"; +#ifdef ZFS + } else if (!strcmp("zfs", tagstr)) { + if (!zfs_filestat(&vn, &fst)) + badtype = "error"; +#endif } else { static char unknown[32]; snprintf(unknown, sizeof unknown, "?(%s)", tagstr); @@ -938,6 +943,20 @@ getfname(const char *filename) return(1); } +#ifdef ZFS +void * +getvnodedata(struct vnode *vp) +{ + return (vp->v_data); +} + +struct mount * +getvnodemount(struct vnode *vp) +{ + return (vp->v_mount); +} +#endif + void usage(void) { diff --git a/usr.bin/fstat/fstat.h b/usr.bin/fstat/fstat.h index dda8bbf..4335110 100644 --- a/usr.bin/fstat/fstat.h +++ b/usr.bin/fstat/fstat.h @@ -71,4 +71,10 @@ dev_t dev2udev(struct cdev *dev); int isofs_filestat(struct vnode *vp, struct filestat *fsp); int msdosfs_filestat(struct vnode *vp, struct filestat *fsp); +#ifdef ZFS +int zfs_filestat(struct vnode *vp, struct filestat *fsp); +void *getvnodedata(struct vnode *vp); +struct mount *getvnodemount(struct vnode *vp); +#endif + #endif /* __FSTAT_H__ */ diff --git a/usr.bin/fstat/zfs.c b/usr.bin/fstat/zfs.c new file mode 100644 index 0000000..5cbf87a --- /dev/null +++ b/usr.bin/fstat/zfs.c @@ -0,0 +1,132 @@ +/*- + * Copyright (c) 2007 Ulf Lilleengen + * 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 AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#include +#define _KERNEL +#include +#undef _KERNEL +#include + +#undef lbolt +#undef lbolt64 +#undef gethrestime_sec +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#define ZFS +#undef dprintf +#include + +/* + * Offset calculations that are used to get data from znode without having the + * definition. + */ +#define LOCATION_ZID (2 * sizeof(void *)) +#define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *))) + +int +zfs_filestat(struct vnode *vp, struct filestat *fsp) +{ + + znode_phys_t zphys; + struct mount mount, *mountptr; + uint64_t *zid; + void *znodeptr, *vnodeptr; + char *dataptr; + int len, size, *zphys_addr; + + len = sizeof(size); + if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) { + dprintf(stderr, "error getting sysctl\n"); + return (0); + } + znodeptr = malloc(size); + if (znodeptr == NULL) { + dprintf(stderr, "error allocating memory for znode storage\n"); + return (0); + } + + /* Since we have problems including vnode.h, we'll use the wrappers. */ + vnodeptr = getvnodedata(vp); + if (!KVM_READ(vnodeptr, znodeptr, size)) { + dprintf(stderr, "can't read znode at %p for pid %d\n", + (void *)vnodeptr, Pid); + goto bad; + } + + /* + * z_id field is stored in the third pointer. We therefor skip the two + * first bytes. + * + * Pointer to the z_phys structure is the next last pointer. Therefore + * go back two bytes from the end. + */ + dataptr = znodeptr; + zid = (uint64_t *)(dataptr + LOCATION_ZID); + zphys_addr = (int *)(dataptr + LOCATION_ZPHYS(size)); + + if (!KVM_READ(*zphys_addr, &zphys, sizeof(zphys))) { + dprintf(stderr, "can't read znode_phys at %p for pid %d\n", + zphys_addr, Pid); + goto bad; + } + + /* Get the mount pointer, and read from the address. */ + mountptr = getvnodemount(vp); + if (!KVM_READ(mountptr, &mount, sizeof(mount))) { + dprintf(stderr, "can't read mount at %p for pid %d\n", + (void *)mountptr, Pid); + goto bad; + } + + fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0]; + fsp->fileid = *zid; + /* + * XXX: Shows up wrong in output, but UFS has this error too. Could + * be that we're casting mode-variables from 64-bit to 8-bit or simply + * error in the mode-to-string function. + */ + fsp->mode = (mode_t)zphys.zp_mode; + fsp->size = (u_long)zphys.zp_size; + fsp->rdev = (dev_t)zphys.zp_rdev; + free(znodeptr); + return (1); +bad: + free(znodeptr); + return (0); +} diff --git a/usr.bin/fstat/zfs/Makefile b/usr.bin/fstat/zfs/Makefile new file mode 100644 index 0000000..c8fcae3 --- /dev/null +++ b/usr.bin/fstat/zfs/Makefile @@ -0,0 +1,23 @@ +# $FreeBSD$ + +.PATH: ${.CURDIR}/.. + +SRCS= zfs.c +OBJS= zfs.o +WARNS?= 1 + +CFLAGS=-D_SOLARIS_C_SOURCE +CFLAGS+= -I${.CURDIR}/../../../sys/compat/opensolaris +CFLAGS+= -I${.CURDIR}/../../../compat/opensolaris/include +CFLAGS+= -I${.CURDIR}/../../../compat/opensolaris/lib/libumem +CFLAGS+= -I${.CURDIR}/../../../contrib/opensolaris/lib/libzpool/common +CFLAGS+= -I${.CURDIR}/../../../sys/contrib/opensolaris/uts/common/fs/zfs +CFLAGS+= -I${.CURDIR}/../../../sys/contrib/opensolaris/uts/common +CFLAGS+= -I${.CURDIR}/../../../sys/contrib/opensolaris/uts/common/sys +CFLAGS+= -I${.CURDIR}/../../../contrib/opensolaris/head +CFLAGS+= -I${.CURDIR}/../../../sys +CFLAGS+= -I${.CURDIR}/.. + +all: ${OBJS} +CLEANFILES= ${OBJS} +.include diff --git a/usr.bin/fstat/zfs/zfs.c b/usr.bin/fstat/zfs/zfs.c new file mode 100644 index 0000000..5cbf87a --- /dev/null +++ b/usr.bin/fstat/zfs/zfs.c @@ -0,0 +1,132 @@ +/*- + * Copyright (c) 2007 Ulf Lilleengen + * 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 AUTHOR 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 AUTHOR 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. + * + * $FreeBSD$ + */ + +#include +#define _KERNEL +#include +#undef _KERNEL +#include + +#undef lbolt +#undef lbolt64 +#undef gethrestime_sec +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#define ZFS +#undef dprintf +#include + +/* + * Offset calculations that are used to get data from znode without having the + * definition. + */ +#define LOCATION_ZID (2 * sizeof(void *)) +#define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *))) + +int +zfs_filestat(struct vnode *vp, struct filestat *fsp) +{ + + znode_phys_t zphys; + struct mount mount, *mountptr; + uint64_t *zid; + void *znodeptr, *vnodeptr; + char *dataptr; + int len, size, *zphys_addr; + + len = sizeof(size); + if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) { + dprintf(stderr, "error getting sysctl\n"); + return (0); + } + znodeptr = malloc(size); + if (znodeptr == NULL) { + dprintf(stderr, "error allocating memory for znode storage\n"); + return (0); + } + + /* Since we have problems including vnode.h, we'll use the wrappers. */ + vnodeptr = getvnodedata(vp); + if (!KVM_READ(vnodeptr, znodeptr, size)) { + dprintf(stderr, "can't read znode at %p for pid %d\n", + (void *)vnodeptr, Pid); + goto bad; + } + + /* + * z_id field is stored in the third pointer. We therefor skip the two + * first bytes. + * + * Pointer to the z_phys structure is the next last pointer. Therefore + * go back two bytes from the end. + */ + dataptr = znodeptr; + zid = (uint64_t *)(dataptr + LOCATION_ZID); + zphys_addr = (int *)(dataptr + LOCATION_ZPHYS(size)); + + if (!KVM_READ(*zphys_addr, &zphys, sizeof(zphys))) { + dprintf(stderr, "can't read znode_phys at %p for pid %d\n", + zphys_addr, Pid); + goto bad; + } + + /* Get the mount pointer, and read from the address. */ + mountptr = getvnodemount(vp); + if (!KVM_READ(mountptr, &mount, sizeof(mount))) { + dprintf(stderr, "can't read mount at %p for pid %d\n", + (void *)mountptr, Pid); + goto bad; + } + + fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0]; + fsp->fileid = *zid; + /* + * XXX: Shows up wrong in output, but UFS has this error too. Could + * be that we're casting mode-variables from 64-bit to 8-bit or simply + * error in the mode-to-string function. + */ + fsp->mode = (mode_t)zphys.zp_mode; + fsp->size = (u_long)zphys.zp_size; + fsp->rdev = (dev_t)zphys.zp_rdev; + free(znodeptr); + return (1); +bad: + free(znodeptr); + return (0); +} -- cgit v1.1