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