libzfs_compat.h revision 247882
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#ifndef	_LIBZFS_COMPAT_H
27#define	_LIBZFS_COMPAT_H
28
29#include <zfs_ioctl_compat.h>
30
31#ifdef	__cplusplus
32extern "C" {
33#endif
34
35static int zfs_kernel_version = 0;
36static int zfs_ioctl_version = 0;
37
38/*
39 * This is FreeBSD version of ioctl, because Solaris' ioctl() updates
40 * zc_nvlist_dst_size even if an error is returned, on FreeBSD if an
41 * error is returned zc_nvlist_dst_size won't be updated.
42 */
43static __inline int
44zcmd_ioctl(int fd, int request, zfs_cmd_t *zc)
45{
46	unsigned long cmd;
47	size_t oldsize, zfs_kernel_version_size, zfs_ioctl_version_size;
48	int version, ret, cflag = ZFS_CMD_COMPAT_NONE;
49
50	cmd = _IOWR('Z', request, struct zfs_cmd);
51
52	zfs_ioctl_version_size = sizeof(zfs_ioctl_version);
53	if (zfs_ioctl_version == 0) {
54		sysctlbyname("vfs.zfs.version.ioctl", &zfs_ioctl_version,
55		    &zfs_ioctl_version_size, NULL, 0);
56	}
57
58	/*
59	 * If vfs.zfs.version.ioctl is not defined, assume we have v28
60	 * compatible binaries and use vfs.zfs.version.spa to test for v15
61	 */
62	if (zfs_ioctl_version < ZFS_IOCVER_DEADMAN) {
63		cflag = ZFS_CMD_COMPAT_V28;
64		zfs_kernel_version_size = sizeof(zfs_kernel_version);
65
66		if (zfs_kernel_version == 0) {
67			sysctlbyname("vfs.zfs.version.spa",
68			    &zfs_kernel_version,
69			    &zfs_kernel_version_size, NULL, 0);
70		}
71
72		if (zfs_kernel_version == SPA_VERSION_15 ||
73		    zfs_kernel_version == SPA_VERSION_14 ||
74		    zfs_kernel_version == SPA_VERSION_13)
75			cflag = ZFS_CMD_COMPAT_V15;
76	}
77
78	oldsize = zc->zc_nvlist_dst_size;
79	ret = zcmd_ioctl_compat(fd, cmd, zc, cflag);
80
81	if (ret == 0 && oldsize < zc->zc_nvlist_dst_size) {
82		ret = -1;
83		errno = ENOMEM;
84	}
85
86	return (ret);
87}
88#define	ioctl(fd, cmd, zc)	zcmd_ioctl((fd), (cmd), (zc))
89
90#ifdef	__cplusplus
91}
92#endif
93
94#endif	/* _LIBZFS_COMPAT_H */
95