2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Ulf Lilleengen
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
33 #include <sys/mount.h>
34 #include <sys/taskqueue.h>
36 #include <sys/sysctl.h>
40 #undef gethrestime_sec
41 #include <sys/zfs_context.h>
43 #include <sys/spa_impl.h>
46 #include <sys/fs/zfs.h>
47 #include <sys/zfs_znode.h>
48 #include <sys/zfs_sa.h>
50 #include <netinet/in.h>
58 #include "libprocstat.h"
59 #include "common_kvm.h"
62 * Offset calculations that are used to get data from znode without having the
65 #define LOCATION_ZID (2 * sizeof(void *))
66 #define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *) + sizeof(struct task)))
69 zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
73 struct mount mount, *mountptr;
75 void *znodeptr, *vnodeptr;
82 if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) {
83 warnx("error getting sysctl");
86 znodeptr = malloc(size);
87 if (znodeptr == NULL) {
88 warnx("error allocating memory for znode storage");
91 /* Since we have problems including vnode.h, we'll use the wrappers. */
92 vnodeptr = getvnodedata(vp);
93 if (!kvm_read_all(kd, (unsigned long)vnodeptr, znodeptr,
95 warnx("can't read znode at %p", (void *)vnodeptr);
100 * z_id field is stored in the third pointer. We therefore skip the two
103 * Pointer to the z_phys structure is the next last pointer. Therefore
104 * go back two bytes from the end.
107 zid = (uint64_t *)(dataptr + LOCATION_ZID);
108 zphys_addr = *(void **)(dataptr + LOCATION_ZPHYS(size));
110 if (!kvm_read_all(kd, (unsigned long)zphys_addr, &zphys,
112 warnx("can't read znode_phys at %p", zphys_addr);
116 /* Get the mount pointer, and read from the address. */
117 mountptr = getvnodemount(vp);
118 if (!kvm_read_all(kd, (unsigned long)mountptr, &mount, sizeof(mount))) {
119 warnx("can't read mount at %p", (void *)mountptr);
122 vn->vn_fsid = mount.mnt_stat.f_fsid.val[0];
123 vn->vn_fileid = *zid;
125 * XXX: Shows up wrong in output, but UFS has this error too. Could
126 * be that we're casting mode-variables from 64-bit to 8-bit or simply
127 * error in the mode-to-string function.
129 vn->vn_mode = (mode_t)zphys.zp_mode;
130 vn->vn_size = (u_long)zphys.zp_size;