xref: /src/sys/contrib/openzfs/lib/libzfs_core/os/freebsd/libzfs_core_ioctl.c (revision b1c1ee4429fcca8f69873a8be66184e68e1b19d7)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/sysctl.h>
25 #include <sys/zfs_ioctl.h>
26 #include <os/freebsd/zfs/sys/zfs_ioctl_compat.h>
27 #include <err.h>
28 #include <libzfs_core.h>
29 #include "libzfs_core_impl.h"
30 
31 int zfs_ioctl_version = ZFS_IOCVER_UNDEF;
32 
33 /*
34  * Get zfs_ioctl_version
35  */
36 static int
get_zfs_ioctl_version(void)37 get_zfs_ioctl_version(void)
38 {
39 	size_t ver_size;
40 	int ver = ZFS_IOCVER_NONE;
41 
42 	ver_size = sizeof (ver);
43 	(void) sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0);
44 
45 	return (ver);
46 }
47 
48 static int
zcmd_ioctl_compat(int fd,int request,zfs_cmd_t * zc,const int cflag)49 zcmd_ioctl_compat(int fd, int request, zfs_cmd_t *zc, const int cflag)
50 {
51 	int ret;
52 #ifdef ZFS_LEGACY_SUPPORT
53 	int newrequest;
54 	void *zc_c = NULL;
55 #endif
56 	unsigned long ncmd;
57 	zfs_iocparm_t zp;
58 
59 	switch (cflag) {
60 	case ZFS_CMD_COMPAT_NONE:
61 		ncmd = _IOWR('Z', request, zfs_iocparm_t);
62 		zp.zfs_cmd = (uint64_t)(uintptr_t)zc;
63 		zp.zfs_cmd_size = sizeof (zfs_cmd_t);
64 		zp.zfs_ioctl_version = ZFS_IOCVER_OZFS;
65 		break;
66 #ifdef ZFS_LEGACY_SUPPORT
67 	case ZFS_CMD_COMPAT_LEGACY:
68 		newrequest = zfs_ioctl_ozfs_to_legacy(request);
69 		ncmd = _IOWR('Z', newrequest, zfs_iocparm_t);
70 		zc_c = malloc(sizeof (zfs_cmd_legacy_t));
71 		zfs_cmd_ozfs_to_legacy(zc, zc_c);
72 		zp.zfs_cmd = (uint64_t)(uintptr_t)zc_c;
73 		zp.zfs_cmd_size = sizeof (zfs_cmd_legacy_t);
74 		zp.zfs_ioctl_version = ZFS_IOCVER_LEGACY;
75 		break;
76 #endif
77 	default:
78 		abort();
79 		return (EINVAL);
80 	}
81 
82 	ret = ioctl(fd, ncmd, &zp);
83 	if (ret) {
84 #ifdef ZFS_LEGACY_SUPPORT
85 		if (zc_c)
86 			free(zc_c);
87 #endif
88 		return (ret);
89 	}
90 #ifdef ZFS_LEGACY_SUPPORT
91 	if (zc_c) {
92 		zfs_cmd_legacy_to_ozfs(zc_c, zc);
93 		free(zc_c);
94 	}
95 #endif
96 	return (ret);
97 }
98 
99 /*
100  * This is FreeBSD version of ioctl, because Solaris' ioctl() updates
101  * zc_nvlist_dst_size even if an error is returned, on FreeBSD if an
102  * error is returned zc_nvlist_dst_size won't be updated.
103  */
104 int
lzc_ioctl_fd_os(int fd,unsigned long request,zfs_cmd_t * zc)105 lzc_ioctl_fd_os(int fd, unsigned long request, zfs_cmd_t *zc)
106 {
107 	size_t oldsize;
108 	int ret, cflag = ZFS_CMD_COMPAT_NONE;
109 
110 	if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
111 		zfs_ioctl_version = get_zfs_ioctl_version();
112 
113 	switch (zfs_ioctl_version) {
114 #ifdef ZFS_LEGACY_SUPPORT
115 		case ZFS_IOCVER_LEGACY:
116 			cflag = ZFS_CMD_COMPAT_LEGACY;
117 			break;
118 #endif
119 		case ZFS_IOCVER_OZFS:
120 			cflag = ZFS_CMD_COMPAT_NONE;
121 			break;
122 		default:
123 			errx(1, "unrecognized zfs ioctl version %d",
124 			    zfs_ioctl_version);
125 	}
126 
127 	oldsize = zc->zc_nvlist_dst_size;
128 	ret = zcmd_ioctl_compat(fd, request, zc, cflag);
129 
130 	if (ret == 0 && oldsize < zc->zc_nvlist_dst_size) {
131 		ret = -1;
132 		errno = ENOMEM;
133 	}
134 
135 	return (ret);
136 }
137