1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2007 Ulf Lilleengen 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #define _WANT_MOUNT 31 #include <sys/mount.h> 32 #include <sys/queue.h> 33 #include <sys/stat.h> 34 #include <sys/sysctl.h> 35 #include <sys/time.h> 36 #include <sys/vnode.h> 37 #define _WANT_ZNODE 38 #include <sys/zfs_context.h> 39 #include <sys/zfs_znode.h> 40 41 #include <netinet/in.h> 42 43 #include <err.h> 44 #include <kvm.h> 45 #include <stdbool.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 49 #define ZFS 50 #include "libprocstat.h" 51 #include "common_kvm.h" 52 53 int 54 zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) 55 { 56 57 struct mount mount, *mountptr; 58 znode_t *kznodeptr, *znode; 59 size_t len; 60 int size; 61 62 len = sizeof(size); 63 if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) { 64 warnx("error getting sysctl"); 65 return (1); 66 } 67 znode = malloc(size); 68 if (znode == NULL) { 69 warnx("error allocating memory for znode storage"); 70 return (1); 71 } 72 73 if ((size_t)size != sizeof(znode_t)) 74 warnx("znode_t size mismatch, data could be wrong"); 75 76 if ((size_t)size < offsetof(znode_t, z_id) + sizeof(znode->z_id) || 77 (size_t)size < offsetof(znode_t, z_mode) + sizeof(znode->z_mode) || 78 (size_t)size < offsetof(znode_t, z_size) + sizeof(znode->z_size)) { 79 warnx("znode_t size is too small"); 80 goto bad; 81 } 82 83 /* 84 * OpenZFS's libspl provides a dummy sys/vnode.h that shadows ours so 85 * struct vnode is an incomplete type. Use the wrapper until that is 86 * resolved. 87 */ 88 kznodeptr = getvnodedata(vp); 89 if (!kvm_read_all(kd, (unsigned long)kznodeptr, znode, (size_t)size)) { 90 warnx("can't read znode at %p", (void *)kznodeptr); 91 goto bad; 92 } 93 94 /* Get the mount pointer, and read from the address. */ 95 mountptr = getvnodemount(vp); 96 if (!kvm_read_all(kd, (unsigned long)mountptr, &mount, sizeof(mount))) { 97 warnx("can't read mount at %p", (void *)mountptr); 98 goto bad; 99 } 100 101 /* 102 * XXX Assume that this is a znode, but it can be a special node 103 * under .zfs/. 104 */ 105 vn->vn_fsid = mount.mnt_stat.f_fsid.val[0]; 106 vn->vn_fileid = znode->z_id; 107 vn->vn_mode = znode->z_mode; 108 vn->vn_size = znode->z_size; 109 return (0); 110 bad: 111 return (1); 112 } 113