xref: /freebsd/lib/libprocstat/zfs.c (revision 41c233dec6c15c543d3042c06833c1623456743c)
10daf62d9SStanislav Sedov /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
40daf62d9SStanislav Sedov  * Copyright (c) 2007 Ulf Lilleengen
50daf62d9SStanislav Sedov  * All rights reserved.
60daf62d9SStanislav Sedov  *
70daf62d9SStanislav Sedov  * Redistribution and use in source and binary forms, with or without
80daf62d9SStanislav Sedov  * modification, are permitted provided that the following conditions
90daf62d9SStanislav Sedov  * are met:
100daf62d9SStanislav Sedov  * 1. Redistributions of source code must retain the above copyright
110daf62d9SStanislav Sedov  *    notice, this list of conditions and the following disclaimer.
120daf62d9SStanislav Sedov  * 2. Redistributions in binary form must reproduce the above copyright
130daf62d9SStanislav Sedov  *    notice, this list of conditions and the following disclaimer in the
140daf62d9SStanislav Sedov  *    documentation and/or other materials provided with the distribution.
150daf62d9SStanislav Sedov  *
160daf62d9SStanislav Sedov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
170daf62d9SStanislav Sedov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180daf62d9SStanislav Sedov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190daf62d9SStanislav Sedov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200daf62d9SStanislav Sedov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210daf62d9SStanislav Sedov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220daf62d9SStanislav Sedov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230daf62d9SStanislav Sedov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240daf62d9SStanislav Sedov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250daf62d9SStanislav Sedov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260daf62d9SStanislav Sedov  * SUCH DAMAGE.
270daf62d9SStanislav Sedov  */
280daf62d9SStanislav Sedov 
29*41c233deSKyle Evans #include <sys/cdefs.h>	/* __RENAME */
30*41c233deSKyle Evans 
31a4b07a27SJason A. Harmening #include <stdbool.h>
32a4b07a27SJason A. Harmening 
330daf62d9SStanislav Sedov #include <sys/param.h>
340daf62d9SStanislav Sedov #define _KERNEL
350daf62d9SStanislav Sedov #include <sys/mount.h>
360daf62d9SStanislav Sedov #undef _KERNEL
378a719b0cSAndriy Gapon #include <sys/queue.h>
388a719b0cSAndriy Gapon #include <sys/stat.h>
390daf62d9SStanislav Sedov #include <sys/sysctl.h>
408a719b0cSAndriy Gapon #include <sys/time.h>
418a719b0cSAndriy Gapon #include <sys/vnode.h>
420daf62d9SStanislav Sedov 
430daf62d9SStanislav Sedov #include <netinet/in.h>
440daf62d9SStanislav Sedov 
450daf62d9SStanislav Sedov #include <err.h>
460daf62d9SStanislav Sedov #include <kvm.h>
470daf62d9SStanislav Sedov #include <stdio.h>
480daf62d9SStanislav Sedov #include <stdlib.h>
490daf62d9SStanislav Sedov 
500daf62d9SStanislav Sedov #define ZFS
510daf62d9SStanislav Sedov #include "libprocstat.h"
520daf62d9SStanislav Sedov #include "common_kvm.h"
538a719b0cSAndriy Gapon #include "zfs_defs.h"
540daf62d9SStanislav Sedov 
550daf62d9SStanislav Sedov int
zfs_filestat(kvm_t * kd,struct vnode * vp,struct vnstat * vn)560daf62d9SStanislav Sedov zfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
570daf62d9SStanislav Sedov {
580daf62d9SStanislav Sedov 
590daf62d9SStanislav Sedov 	struct mount mount, *mountptr;
608a719b0cSAndriy Gapon 	void *znodeptr;
610daf62d9SStanislav Sedov 	char *dataptr;
620daf62d9SStanislav Sedov 	size_t len;
630daf62d9SStanislav Sedov 	int size;
640daf62d9SStanislav Sedov 
650daf62d9SStanislav Sedov 	len = sizeof(size);
660daf62d9SStanislav Sedov 	if (sysctlbyname("debug.sizeof.znode", &size, &len, NULL, 0) == -1) {
670daf62d9SStanislav Sedov 		warnx("error getting sysctl");
680daf62d9SStanislav Sedov 		return (1);
690daf62d9SStanislav Sedov 	}
708a719b0cSAndriy Gapon 	dataptr = malloc(size);
718a719b0cSAndriy Gapon 	if (dataptr == NULL) {
720daf62d9SStanislav Sedov 		warnx("error allocating memory for znode storage");
730daf62d9SStanislav Sedov 		return (1);
740daf62d9SStanislav Sedov 	}
758a719b0cSAndriy Gapon 
768a719b0cSAndriy Gapon 	if ((size_t)size < offsetof_z_id + sizeof(uint64_t) ||
778a719b0cSAndriy Gapon 	    (size_t)size < offsetof_z_mode + sizeof(mode_t) ||
788a719b0cSAndriy Gapon 	    (size_t)size < offsetof_z_size + sizeof(uint64_t)) {
798a719b0cSAndriy Gapon 		warnx("znode_t size is too small");
800daf62d9SStanislav Sedov 		goto bad;
810daf62d9SStanislav Sedov 	}
820daf62d9SStanislav Sedov 
838a719b0cSAndriy Gapon 	if ((size_t)size != sizeof_znode_t)
848a719b0cSAndriy Gapon 		warnx("znode_t size mismatch, data could be wrong");
850daf62d9SStanislav Sedov 
868a719b0cSAndriy Gapon 	/* Since we have problems including vnode.h, we'll use the wrappers. */
878a719b0cSAndriy Gapon 	znodeptr = getvnodedata(vp);
888a719b0cSAndriy Gapon 	if (!kvm_read_all(kd, (unsigned long)znodeptr, dataptr,
898a719b0cSAndriy Gapon 	    (size_t)size)) {
908a719b0cSAndriy Gapon 		warnx("can't read znode at %p", (void *)znodeptr);
910daf62d9SStanislav Sedov 		goto bad;
920daf62d9SStanislav Sedov 	}
930daf62d9SStanislav Sedov 
940daf62d9SStanislav Sedov 	/* Get the mount pointer, and read from the address. */
950daf62d9SStanislav Sedov 	mountptr = getvnodemount(vp);
960daf62d9SStanislav Sedov 	if (!kvm_read_all(kd, (unsigned long)mountptr, &mount, sizeof(mount))) {
970daf62d9SStanislav Sedov 		warnx("can't read mount at %p", (void *)mountptr);
980daf62d9SStanislav Sedov 		goto bad;
990daf62d9SStanislav Sedov 	}
1008a719b0cSAndriy Gapon 
1010daf62d9SStanislav Sedov 	/*
1028a719b0cSAndriy Gapon 	 * XXX Assume that this is a znode, but it can be a special node
1038a719b0cSAndriy Gapon 	 * under .zfs/.
1040daf62d9SStanislav Sedov 	 */
1058a719b0cSAndriy Gapon 	vn->vn_fsid = mount.mnt_stat.f_fsid.val[0];
1068a719b0cSAndriy Gapon 	vn->vn_fileid = *(uint64_t *)(void *)(dataptr + offsetof_z_id);
1078a719b0cSAndriy Gapon 	vn->vn_mode = *(mode_t *)(void *)(dataptr + offsetof_z_mode);
1088a719b0cSAndriy Gapon 	vn->vn_size = *(uint64_t *)(void *)(dataptr + offsetof_z_size);
1098a719b0cSAndriy Gapon 	free(dataptr);
1100daf62d9SStanislav Sedov 	return (0);
1110daf62d9SStanislav Sedov bad:
1128a719b0cSAndriy Gapon 	free(dataptr);
1130daf62d9SStanislav Sedov 	return (1);
1140daf62d9SStanislav Sedov }
115