xref: /freebsd/lib/libprocstat/zfs.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
1 /*-
2  * Copyright (c) 2007 Ulf Lilleengen
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #define _KERNEL
31 #include <sys/mount.h>
32 #include <sys/taskqueue.h>
33 #undef _KERNEL
34 #include <sys/sysctl.h>
35 
36 #undef lbolt
37 #undef lbolt64
38 #undef gethrestime_sec
39 #include <sys/zfs_context.h>
40 #include <sys/spa.h>
41 #include <sys/spa_impl.h>
42 #include <sys/dmu.h>
43 #include <sys/zap.h>
44 #include <sys/fs/zfs.h>
45 #include <sys/zfs_znode.h>
46 #include <sys/zfs_sa.h>
47 
48 #include <netinet/in.h>
49 
50 #include <err.h>
51 #include <kvm.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 
55 #define ZFS
56 #include "libprocstat.h"
57 #include "common_kvm.h"
58 
59 /*
60  * Offset calculations that are used to get data from znode without having the
61  * definition.
62  */
63 #define LOCATION_ZID (2 * sizeof(void *))
64 #define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *) + sizeof(struct task)))
65 
66 int
67 zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
68 {
69 
70 	znode_phys_t zphys;
71 	struct mount mount, *mountptr;
72 	uint64_t *zid;
73 	void *znodeptr, *vnodeptr;
74 	char *dataptr;
75 	void *zphys_addr;
76 	size_t len;
77 	int size;
78 
79 	len = sizeof(size);
80 	if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) {
81 		warnx("error getting sysctl");
82 		return (1);
83 	}
84 	znodeptr = malloc(size);
85 	if (znodeptr == NULL) {
86 		warnx("error allocating memory for znode storage");
87 		return (1);
88 	}
89 	/* Since we have problems including vnode.h, we'll use the wrappers. */
90 	vnodeptr = getvnodedata(vp);
91 	if (!kvm_read_all(kd, (unsigned long)vnodeptr, znodeptr,
92 	    (size_t)size)) {
93 		warnx("can't read znode at %p", (void *)vnodeptr);
94 		goto bad;
95 	}
96 
97 	/*
98 	 * z_id field is stored in the third pointer. We therefore skip the two
99 	 * first bytes.
100 	 *
101 	 * Pointer to the z_phys structure is the next last pointer. Therefore
102 	 * go back two bytes from the end.
103 	 */
104 	dataptr = znodeptr;
105 	zid = (uint64_t *)(dataptr + LOCATION_ZID);
106 	zphys_addr = *(void **)(dataptr + LOCATION_ZPHYS(size));
107 
108 	if (!kvm_read_all(kd, (unsigned long)zphys_addr, &zphys,
109 	    sizeof(zphys))) {
110 		warnx("can't read znode_phys at %p", zphys_addr);
111 		goto bad;
112 	}
113 
114 	/* Get the mount pointer, and read from the address. */
115 	mountptr = getvnodemount(vp);
116 	if (!kvm_read_all(kd, (unsigned long)mountptr, &mount, sizeof(mount))) {
117 		warnx("can't read mount at %p", (void *)mountptr);
118 		goto bad;
119 	}
120 	vn->vn_fsid = mount.mnt_stat.f_fsid.val[0];
121 	vn->vn_fileid = *zid;
122 	/*
123 	 * XXX: Shows up wrong in output, but UFS has this error too. Could
124 	 * be that we're casting mode-variables from 64-bit to 8-bit or simply
125 	 * error in the mode-to-string function.
126 	 */
127 	vn->vn_mode = (mode_t)zphys.zp_mode;
128 	vn->vn_size = (u_long)zphys.zp_size;
129 	free(znodeptr);
130 	return (0);
131 bad:
132 	free(znodeptr);
133 	return (1);
134 }
135