1221807Sstas/*- 2221807Sstas * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org> 3221807Sstas * Copyright (c) 1988, 1993 4221807Sstas * The Regents of the University of California. All rights reserved. 5221807Sstas * 6221807Sstas * Redistribution and use in source and binary forms, with or without 7221807Sstas * modification, are permitted provided that the following conditions 8221807Sstas * are met: 9221807Sstas * 1. Redistributions of source code must retain the above copyright 10221807Sstas * notice, this list of conditions and the following disclaimer. 11221807Sstas * 2. Redistributions in binary form must reproduce the above copyright 12221807Sstas * notice, this list of conditions and the following disclaimer in the 13221807Sstas * documentation and/or other materials provided with the distribution. 14221807Sstas * 3. All advertising materials mentioning features or use of this software 15221807Sstas * must display the following acknowledgement: 16221807Sstas * This product includes software developed by the University of 17221807Sstas * California, Berkeley and its contributors. 18221807Sstas * 4. Neither the name of the University nor the names of its contributors 19221807Sstas * may be used to endorse or promote products derived from this software 20221807Sstas * without specific prior written permission. 21221807Sstas * 22221807Sstas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23221807Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24221807Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25221807Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26221807Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27221807Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28221807Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29221807Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30221807Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31221807Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32221807Sstas * SUCH DAMAGE. 33221807Sstas */ 34221807Sstas 35221807Sstas#include <sys/cdefs.h> 36221807Sstas__FBSDID("$FreeBSD$"); 37221807Sstas 38221807Sstas#include <sys/param.h> 39221807Sstas#include <sys/user.h> 40221807Sstas#include <sys/stat.h> 41221807Sstas#include <sys/vnode.h> 42221807Sstas#include <sys/conf.h> 43221807Sstas#define _KERNEL 44221807Sstas#include <sys/pipe.h> 45221807Sstas#include <sys/mount.h> 46221807Sstas#include <ufs/ufs/quota.h> 47221807Sstas#include <ufs/ufs/inode.h> 48221807Sstas#include <fs/devfs/devfs.h> 49221807Sstas#include <fs/devfs/devfs_int.h> 50221807Sstas#undef _KERNEL 51221807Sstas#include <nfs/nfsproto.h> 52221807Sstas#include <nfsclient/nfs.h> 53221807Sstas#include <nfsclient/nfsnode.h> 54221807Sstas 55221807Sstas#include <assert.h> 56221807Sstas#include <err.h> 57221807Sstas#include <kvm.h> 58221807Sstas#include <stddef.h> 59221807Sstas#include <string.h> 60221807Sstas 61221807Sstas#include <libprocstat.h> 62221807Sstas#include "common_kvm.h" 63221807Sstas 64221807Sstasint 65221807Sstaskvm_read_all(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) 66221807Sstas{ 67221807Sstas ssize_t error; 68221807Sstas 69221807Sstas if (nbytes >= SSIZE_MAX) 70221807Sstas return (0); 71221807Sstas error = kvm_read(kd, addr, buf, nbytes); 72221807Sstas return (error == (ssize_t)(nbytes)); 73221807Sstas} 74221807Sstas 75221807Sstasint 76221807Sstaskdevtoname(kvm_t *kd, struct cdev *dev, char *buf) 77221807Sstas{ 78221807Sstas struct cdev si; 79221807Sstas 80221807Sstas assert(buf); 81221807Sstas if (!kvm_read_all(kd, (unsigned long)dev, &si, sizeof(si))) 82221807Sstas return (1); 83231384Sed strlcpy(buf, si.si_name, SPECNAMELEN + 1); 84221807Sstas return (0); 85221807Sstas} 86221807Sstas 87221807Sstasint 88221807Sstasufs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) 89221807Sstas{ 90221807Sstas struct inode inode; 91221807Sstas 92221807Sstas if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &inode, sizeof(inode))) { 93221807Sstas warnx("can't read inode at %p", (void *)VTOI(vp)); 94221807Sstas return (1); 95221807Sstas } 96221807Sstas /* 97221807Sstas * The st_dev from stat(2) is a dev_t. These kernel structures 98221807Sstas * contain cdev pointers. We need to convert to dev_t to make 99221807Sstas * comparisons 100221807Sstas */ 101221807Sstas vn->vn_fsid = dev2udev(kd, inode.i_dev); 102235602Sgleb vn->vn_fileid = inode.i_number; 103221807Sstas vn->vn_mode = (mode_t)inode.i_mode; 104235602Sgleb vn->vn_size = inode.i_size; 105221807Sstas return (0); 106221807Sstas} 107221807Sstas 108221807Sstasint 109221807Sstasdevfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) 110221807Sstas{ 111221807Sstas struct devfs_dirent devfs_dirent; 112221807Sstas struct mount mount; 113221807Sstas 114221807Sstas if (!kvm_read_all(kd, (unsigned long)getvnodedata(vp), &devfs_dirent, 115221807Sstas sizeof(devfs_dirent))) { 116221807Sstas warnx("can't read devfs_dirent at %p", 117221807Sstas (void *)vp->v_data); 118221807Sstas return (1); 119221807Sstas } 120221807Sstas if (!kvm_read_all(kd, (unsigned long)getvnodemount(vp), &mount, 121221807Sstas sizeof(mount))) { 122221807Sstas warnx("can't read mount at %p", 123221807Sstas (void *)getvnodemount(vp)); 124221807Sstas return (1); 125221807Sstas } 126221807Sstas vn->vn_fsid = mount.mnt_stat.f_fsid.val[0]; 127221807Sstas vn->vn_fileid = devfs_dirent.de_inode; 128221807Sstas vn->vn_mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; 129221807Sstas vn->vn_size = 0; 130221807Sstas return (0); 131221807Sstas} 132221807Sstas 133221807Sstasint 134221807Sstasnfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn) 135221807Sstas{ 136221807Sstas struct nfsnode nfsnode; 137221807Sstas mode_t mode; 138221807Sstas 139221807Sstas if (!kvm_read_all(kd, (unsigned long)VTONFS(vp), &nfsnode, 140221807Sstas sizeof(nfsnode))) { 141221807Sstas warnx("can't read nfsnode at %p", 142221807Sstas (void *)VTONFS(vp)); 143221807Sstas return (1); 144221807Sstas } 145221807Sstas vn->vn_fsid = nfsnode.n_vattr.va_fsid; 146221807Sstas vn->vn_fileid = nfsnode.n_vattr.va_fileid; 147221807Sstas vn->vn_size = nfsnode.n_size; 148221807Sstas mode = (mode_t)nfsnode.n_vattr.va_mode; 149221807Sstas switch (vp->v_type) { 150221807Sstas case VREG: 151221807Sstas mode |= S_IFREG; 152221807Sstas break; 153221807Sstas case VDIR: 154221807Sstas mode |= S_IFDIR; 155221807Sstas break; 156221807Sstas case VBLK: 157221807Sstas mode |= S_IFBLK; 158221807Sstas break; 159221807Sstas case VCHR: 160221807Sstas mode |= S_IFCHR; 161221807Sstas break; 162221807Sstas case VLNK: 163221807Sstas mode |= S_IFLNK; 164221807Sstas break; 165221807Sstas case VSOCK: 166221807Sstas mode |= S_IFSOCK; 167221807Sstas break; 168221807Sstas case VFIFO: 169221807Sstas mode |= S_IFIFO; 170221807Sstas break; 171221807Sstas default: 172221807Sstas break; 173221807Sstas }; 174221807Sstas vn->vn_mode = mode; 175221807Sstas return (0); 176221807Sstas} 177221807Sstas 178221807Sstas/* 179221807Sstas * Read the cdev structure in the kernel in order to work out the 180221807Sstas * associated dev_t 181221807Sstas */ 182221807Sstasdev_t 183221807Sstasdev2udev(kvm_t *kd, struct cdev *dev) 184221807Sstas{ 185221807Sstas struct cdev_priv priv; 186221807Sstas 187221807Sstas assert(kd); 188221807Sstas if (kvm_read_all(kd, (unsigned long)cdev2priv(dev), &priv, 189221807Sstas sizeof(priv))) { 190221807Sstas return ((dev_t)priv.cdp_inode); 191221807Sstas } else { 192221807Sstas warnx("can't convert cdev *%p to a dev_t\n", dev); 193221807Sstas return (-1); 194221807Sstas } 195221807Sstas} 196221807Sstas 197221807Sstasvoid * 198221807Sstasgetvnodedata(struct vnode *vp) 199221807Sstas{ 200221807Sstas return (vp->v_data); 201221807Sstas} 202221807Sstas 203221807Sstasstruct mount * 204221807Sstasgetvnodemount(struct vnode *vp) 205221807Sstas{ 206221807Sstas return (vp->v_mount); 207221807Sstas} 208