xref: /netbsd-src/usr.bin/fstat/zfs.c (revision 77228a74f8fc64ff1f17329ad3377e8abb2154b0)
1 /*	$NetBSD: zfs.c,v 1.1 2022/06/19 11:31:19 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Simon Burge.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: zfs.c,v 1.1 2022/06/19 11:31:19 simonb Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/vnode.h>
38 #define __EXPOSE_MOUNT
39 #include <sys/mount.h>
40 
41 #include "zfs_znode.h"
42 
43 #include <kvm.h>
44 #include <err.h>
45 #include "fstat.h"
46 
47 int
zfs_filestat(struct vnode * vp,struct filestat * fsp)48 zfs_filestat(struct vnode *vp, struct filestat *fsp)
49 {
50 	znode_t inode;
51 	struct mount mt;
52 
53 	if (!KVM_READ(VTOZ(vp), &inode, sizeof (inode))) {
54 		dprintf("can't read inode at %p for pid %d", VTOZ(vp), Pid);
55 		return 0;
56 	}
57 	if (!KVM_READ(vp->v_mount, &mt, sizeof(mt))) {
58 		dprintf("can't read mount at %p for pid %d",
59 		    vp->v_mount, Pid);
60 		return 0;
61 	}
62 	/*
63 	 * XXX: fsid should be something like
64 	 *    ((struct zfsvfs *)vp->v_mount->mnt_data)->
65 	 *    z_os->os_ds_dataset->ds_fsid_guid
66 	 * but ZFS headers are pretty much impossible to include
67 	 * from userland.
68 	 */
69 	fsp->fsid = mt.mnt_stat.f_fsidx.__fsid_val[0];
70 	fsp->fileid = inode.z_id;
71 	fsp->mode = inode.z_mode;
72 	fsp->size = inode.z_size;
73 	fsp->rdev = 0;
74 	return 1;
75 }
76