1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org> 5 * Copyright (c) 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/param.h> 38 #include <sys/user.h> 39 #include <sys/stat.h> 40 #include <sys/vnode.h> 41 #include <sys/conf.h> 42 #include <sys/pipe.h> 43 #define _WANT_MOUNT 44 #include <sys/mount.h> 45 #include <ufs/ufs/quota.h> 46 #include <ufs/ufs/inode.h> 47 #include <ufs/ufs/extattr.h> 48 #include <ufs/ufs/ufsmount.h> 49 #include <fs/devfs/devfs.h> 50 #include <fs/devfs/devfs_int.h> 51 #include <nfs/nfsproto.h> 52 #include <nfsclient/nfs.h> 53 #include <nfsclient/nfsnode.h> 54 55 #include <assert.h> 56 #include <err.h> 57 #include <kvm.h> 58 #include <stddef.h> 59 #include <string.h> 60 61 #include <libprocstat.h> 62 #include "common_kvm.h" 63 64 int 65 kvm_read_all(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) 66 { 67 ssize_t error; 68 69 if (nbytes >= SSIZE_MAX) 70 return (0); 71 error = kvm_read(kd, addr, buf, nbytes); 72 return (error == (ssize_t)(nbytes)); 73 } 74 75 int 76 kdevtoname(kvm_t *kd, struct cdev *dev, char *buf) 77 { 78 struct cdev si; 79 80 assert(buf); 81 if (!kvm_read_all(kd, (unsigned long)dev, &si, sizeof(si))) 82 return (1); 83 strlcpy(buf, si.si_name, SPECNAMELEN + 1); 84 return (0); 85 } 86 87 int 88 ufs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) 89 { 90 struct inode inode; 91 struct ufsmount um; 92 93 if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &inode, sizeof(inode))) { 94 warnx("can't read inode at %p", (void *)VTOI(vp)); 95 return (1); 96 } 97 if (!kvm_read_all(kd, (unsigned long)inode.i_ump, &um, sizeof(um))) { 98 warnx("can't read ufsmount at %p", (void *)inode.i_ump); 99 return (1); 100 } 101 /* 102 * The st_dev from stat(2) is a dev_t. These kernel structures 103 * contain cdev pointers. We need to convert to dev_t to make 104 * comparisons 105 */ 106 vn->vn_fsid = dev2udev(kd, um.um_dev); 107 vn->vn_fileid = inode.i_number; 108 vn->vn_mode = (mode_t)inode.i_mode; 109 vn->vn_size = inode.i_size; 110 return (0); 111 } 112 113 int 114 devfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) 115 { 116 struct devfs_dirent devfs_dirent; 117 struct mount mount; 118 119 if (!kvm_read_all(kd, (unsigned long)getvnodedata(vp), &devfs_dirent, 120 sizeof(devfs_dirent))) { 121 warnx("can't read devfs_dirent at %p", 122 (void *)vp->v_data); 123 return (1); 124 } 125 if (!kvm_read_all(kd, (unsigned long)getvnodemount(vp), &mount, 126 sizeof(mount))) { 127 warnx("can't read mount at %p", 128 (void *)getvnodemount(vp)); 129 return (1); 130 } 131 vn->vn_fsid = mount.mnt_stat.f_fsid.val[0]; 132 vn->vn_fileid = devfs_dirent.de_inode; 133 vn->vn_mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; 134 vn->vn_size = 0; 135 return (0); 136 } 137 138 int 139 nfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) 140 { 141 struct nfsnode nfsnode; 142 mode_t mode; 143 144 if (!kvm_read_all(kd, (unsigned long)VTONFS(vp), &nfsnode, 145 sizeof(nfsnode))) { 146 warnx("can't read nfsnode at %p", 147 (void *)VTONFS(vp)); 148 return (1); 149 } 150 vn->vn_fsid = nfsnode.n_vattr.va_fsid; 151 vn->vn_fileid = nfsnode.n_vattr.va_fileid; 152 vn->vn_size = nfsnode.n_size; 153 mode = (mode_t)nfsnode.n_vattr.va_mode; 154 switch (vp->v_type) { 155 case VREG: 156 mode |= S_IFREG; 157 break; 158 case VDIR: 159 mode |= S_IFDIR; 160 break; 161 case VBLK: 162 mode |= S_IFBLK; 163 break; 164 case VCHR: 165 mode |= S_IFCHR; 166 break; 167 case VLNK: 168 mode |= S_IFLNK; 169 break; 170 case VSOCK: 171 mode |= S_IFSOCK; 172 break; 173 case VFIFO: 174 mode |= S_IFIFO; 175 break; 176 default: 177 break; 178 }; 179 vn->vn_mode = mode; 180 return (0); 181 } 182 183 /* 184 * Read the cdev structure in the kernel in order to work out the 185 * associated dev_t 186 */ 187 dev_t 188 dev2udev(kvm_t *kd, struct cdev *dev) 189 { 190 struct cdev_priv priv; 191 192 assert(kd); 193 if (kvm_read_all(kd, (unsigned long)cdev2priv(dev), &priv, 194 sizeof(priv))) { 195 return ((dev_t)priv.cdp_inode); 196 } else { 197 warnx("can't convert cdev *%p to a dev_t\n", dev); 198 return (-1); 199 } 200 } 201 202 void * 203 getvnodedata(struct vnode *vp) 204 { 205 return (vp->v_data); 206 } 207 208 struct mount * 209 getvnodemount(struct vnode *vp) 210 { 211 return (vp->v_mount); 212 } 213