]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libprocstat/zfs.c
Make linux_ptrace() use linux_msg() instead of printf().
[FreeBSD/FreeBSD.git] / lib / libprocstat / zfs.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
29  */
30
31 #include <sys/param.h>
32 #define _KERNEL
33 #include <sys/mount.h>
34 #include <sys/taskqueue.h>
35 #undef _KERNEL
36 #include <sys/sysctl.h>
37
38 #undef lbolt
39 #undef lbolt64
40 #undef gethrestime_sec
41 #include <sys/zfs_context.h>
42 #include <sys/spa.h>
43 #include <sys/spa_impl.h>
44 #include <sys/dmu.h>
45 #include <sys/zap.h>
46 #include <sys/fs/zfs.h>
47 #include <sys/zfs_znode.h>
48 #include <sys/zfs_sa.h>
49
50 #include <netinet/in.h>
51
52 #include <err.h>
53 #include <kvm.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56
57 #define ZFS
58 #include "libprocstat.h"
59 #include "common_kvm.h"
60
61 /* 
62  * Offset calculations that are used to get data from znode without having the
63  * definition.
64  */
65 #define LOCATION_ZID (2 * sizeof(void *))
66 #define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *) + sizeof(struct task)))
67
68 int
69 zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
70 {
71
72         znode_phys_t zphys;
73         struct mount mount, *mountptr;
74         uint64_t *zid;
75         void *znodeptr, *vnodeptr;
76         char *dataptr;
77         void *zphys_addr;
78         size_t len;
79         int size;
80
81         len = sizeof(size);
82         if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) {
83                 warnx("error getting sysctl");
84                 return (1);
85         }
86         znodeptr = malloc(size);
87         if (znodeptr == NULL) {
88                 warnx("error allocating memory for znode storage");
89                 return (1);
90         }
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,
94             (size_t)size)) {
95                 warnx("can't read znode at %p", (void *)vnodeptr);
96                 goto bad;
97         }
98
99         /* 
100          * z_id field is stored in the third pointer. We therefore skip the two
101          * first bytes. 
102          *
103          * Pointer to the z_phys structure is the next last pointer. Therefore
104          * go back two bytes from the end.
105          */
106         dataptr = znodeptr;
107         zid = (uint64_t *)(dataptr + LOCATION_ZID);
108         zphys_addr = *(void **)(dataptr + LOCATION_ZPHYS(size));
109
110         if (!kvm_read_all(kd, (unsigned long)zphys_addr, &zphys,
111             sizeof(zphys))) {
112                 warnx("can't read znode_phys at %p", zphys_addr);
113                 goto bad;
114         }
115
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);
120                 goto bad;
121         }
122         vn->vn_fsid = mount.mnt_stat.f_fsid.val[0];
123         vn->vn_fileid = *zid;
124         /*
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.
128          */
129         vn->vn_mode = (mode_t)zphys.zp_mode;
130         vn->vn_size = (u_long)zphys.zp_size;
131         free(znodeptr);
132         return (0);
133 bad:
134         free(znodeptr);
135         return (1);
136 }