xref: /freebsd/lib/libprocstat/zfs.c (revision ba3c1f5972d7b90feb6e6da47905ff2757e0fe57)
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  * $FreeBSD$
29  */
30 
31 #include <stdbool.h>
32 
33 #include <sys/param.h>
34 #define _KERNEL
35 #include <sys/mount.h>
36 #undef _KERNEL
37 #include <sys/queue.h>
38 #include <sys/stat.h>
39 #include <sys/sysctl.h>
40 #include <sys/time.h>
41 #include <sys/vnode.h>
42 
43 #include <netinet/in.h>
44 
45 #include <err.h>
46 #include <kvm.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 
50 #define ZFS
51 #include "libprocstat.h"
52 #include "common_kvm.h"
53 #include "zfs_defs.h"
54 
55 int
56 zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
57 {
58 
59 	struct mount mount, *mountptr;
60 	void *znodeptr;
61 	char *dataptr;
62 	size_t len;
63 	int size;
64 
65 	len = sizeof(size);
66 	if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) {
67 		warnx("error getting sysctl");
68 		return (1);
69 	}
70 	dataptr = malloc(size);
71 	if (dataptr == NULL) {
72 		warnx("error allocating memory for znode storage");
73 		return (1);
74 	}
75 
76 	if ((size_t)size < offsetof_z_id + sizeof(uint64_t) ||
77 	    (size_t)size < offsetof_z_mode + sizeof(mode_t) ||
78 	    (size_t)size < offsetof_z_size + sizeof(uint64_t)) {
79 		warnx("znode_t size is too small");
80 		goto bad;
81 	}
82 
83 	if ((size_t)size != sizeof_znode_t)
84 		warnx("znode_t size mismatch, data could be wrong");
85 
86 	/* Since we have problems including vnode.h, we'll use the wrappers. */
87 	znodeptr = getvnodedata(vp);
88 	if (!kvm_read_all(kd, (unsigned long)znodeptr, dataptr,
89 	    (size_t)size)) {
90 		warnx("can't read znode at %p", (void *)znodeptr);
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 = *(uint64_t *)(void *)(dataptr + offsetof_z_id);
107 	vn->vn_mode = *(mode_t *)(void *)(dataptr + offsetof_z_mode);
108 	vn->vn_size = *(uint64_t *)(void *)(dataptr + offsetof_z_size);
109 	free(dataptr);
110 	return (0);
111 bad:
112 	free(dataptr);
113 	return (1);
114 }
115