libzfs_compat.h revision 248445
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_ioctl_version = -1;
36static int zfs_kernel_version = 0;
37
38/*
39 * Get zfs_ioctl_version
40 */
41static __inline int
42get_zfs_ioctl_version(void)
43{
44	size_t ver_size;
45	int ver = 0;
46
47	ver_size = sizeof(ver);
48	sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0);
49
50	return (ver);
51}
52
53/*
54 * This is FreeBSD version of ioctl, because Solaris' ioctl() updates
55 * zc_nvlist_dst_size even if an error is returned, on FreeBSD if an
56 * error is returned zc_nvlist_dst_size won't be updated.
57 */
58static __inline int
59zcmd_ioctl(int fd, int request, zfs_cmd_t *zc)
60{
61	size_t oldsize, zfs_kernel_version_size;
62	int version, ret, cflag = ZFS_CMD_COMPAT_NONE;
63
64	if (zfs_ioctl_version == -1)
65		zfs_ioctl_version = get_zfs_ioctl_version();
66
67	if (zfs_ioctl_version == ZFS_IOCVER_DEADMAN)
68		cflag = ZFS_CMD_COMPAT_DEADMAN;
69
70	/*
71	 * If vfs.zfs.version.ioctl is not defined, assume we have v28
72	 * compatible binaries and use vfs.zfs.version.spa to test for v15
73	 */
74	if (zfs_ioctl_version < ZFS_IOCVER_DEADMAN) {
75		cflag = ZFS_CMD_COMPAT_V28;
76		zfs_kernel_version_size = sizeof(zfs_kernel_version);
77
78		if (zfs_kernel_version == 0) {
79			sysctlbyname("vfs.zfs.version.spa",
80			    &zfs_kernel_version,
81			    &zfs_kernel_version_size, NULL, 0);
82		}
83
84		if (zfs_kernel_version == SPA_VERSION_15 ||
85		    zfs_kernel_version == SPA_VERSION_14 ||
86		    zfs_kernel_version == SPA_VERSION_13)
87			cflag = ZFS_CMD_COMPAT_V15;
88	}
89
90	oldsize = zc->zc_nvlist_dst_size;
91	ret = zcmd_ioctl_compat(fd, request, zc, cflag);
92
93	if (ret == 0 && oldsize < zc->zc_nvlist_dst_size) {
94		ret = -1;
95		errno = ENOMEM;
96	}
97
98	return (ret);
99}
100#define	ioctl(fd, ioc, zc)	zcmd_ioctl((fd), (ioc), (zc))
101
102#ifdef	__cplusplus
103}
104#endif
105
106#endif	/* _LIBZFS_COMPAT_H */
107