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