xref: /netbsd-src/external/cddl/osnet/dist/lib/libzfs/common/libzfs_compat.c (revision ba2539a9805a0544ff82c0003cc02fe1eee5603d)
1 /*
2  * CDDL HEADER SART
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 
22 /*
23  * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
24  */
25 
26 #include <err.h>
27 
28 #include "libzfs_compat.h"
29 
30 int zfs_ioctl_version = ZFS_IOCVER_UNDEF;
31 static int zfs_spa_version = -1;
32 
33 /*
34  * Get zfs_ioctl_version
35  */
36 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 	if (sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0) < 0)
44 		err(1, "sysctl vfs.zfs.version.ioctl failed");
45 
46 	return (ver);
47 }
48 
49 /*
50  * Get the SPA version
51  */
52 static int
get_zfs_spa_version(void)53 get_zfs_spa_version(void)
54 {
55 	size_t ver_size;
56 	int ver = 0;
57 
58 	ver_size = sizeof(ver);
59 	if (sysctlbyname("vfs.zfs.version.spa", &ver, &ver_size, NULL, 0) < 0)
60 		err(1, "sysctl vfs.zfs.version.spa failed");
61 
62 	return (ver);
63 }
64 
65 /*
66  * This is FreeBSD version of ioctl, because Solaris' ioctl() updates
67  * zc_nvlist_dst_size even if an error is returned, on FreeBSD if an
68  * error is returned zc_nvlist_dst_size won't be updated.
69  */
70 int
zcmd_ioctl(int fd,int request,zfs_cmd_t * zc)71 zcmd_ioctl(int fd, int request, zfs_cmd_t *zc)
72 {
73 	size_t oldsize;
74 	int ret, cflag = ZFS_CMD_COMPAT_NONE;
75 
76 	if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
77 		zfs_ioctl_version = get_zfs_ioctl_version();
78 
79 	if (zfs_ioctl_version >= ZFS_IOCVER_DEADMAN) {
80 		switch (zfs_ioctl_version) {
81 		case ZFS_IOCVER_RESUME:
82 			cflag = ZFS_CMD_COMPAT_RESUME;
83 			break;
84 		case ZFS_IOCVER_EDBP:
85 			cflag = ZFS_CMD_COMPAT_EDBP;
86 			break;
87 		case ZFS_IOCVER_ZCMD:
88 			cflag = ZFS_CMD_COMPAT_ZCMD;
89 			break;
90 		case ZFS_IOCVER_LZC:
91 			cflag = ZFS_CMD_COMPAT_LZC;
92 			break;
93 		case ZFS_IOCVER_DEADMAN:
94 			cflag = ZFS_CMD_COMPAT_DEADMAN;
95 			break;
96 		}
97 	} else {
98 		/*
99 		 * If vfs.zfs.version.ioctl is not defined, assume we have v28
100 		 * compatible binaries and use vfs.zfs.version.spa to test for v15
101 		 */
102 		cflag = ZFS_CMD_COMPAT_V28;
103 
104 		if (zfs_spa_version < 0)
105 			zfs_spa_version = get_zfs_spa_version();
106 
107 		if (zfs_spa_version == SPA_VERSION_15 ||
108 		    zfs_spa_version == SPA_VERSION_14 ||
109 		    zfs_spa_version == SPA_VERSION_13)
110 			cflag = ZFS_CMD_COMPAT_V15;
111 	}
112 
113 	oldsize = zc->zc_nvlist_dst_size;
114 	ret = zcmd_ioctl_compat(fd, request, zc, cflag);
115 
116 	if (ret == 0 && oldsize < zc->zc_nvlist_dst_size) {
117 		ret = -1;
118 		errno = ENOMEM;
119 	}
120 
121 	return (ret);
122 }
123