]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_compat.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / cddl / contrib / opensolaris / lib / libzfs / common / libzfs_compat.c
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 "libzfs_compat.h"
27
28 int zfs_ioctl_version = ZFS_IOCVER_UNDEF;
29 static int zfs_spa_version = -1;
30
31 /*
32  * Get zfs_ioctl_version
33  */
34 int
35 get_zfs_ioctl_version(void)
36 {
37         size_t ver_size;
38         int ver = ZFS_IOCVER_NONE;
39
40         ver_size = sizeof(ver);
41         sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0);
42
43         return (ver);
44 }
45
46 /*
47  * Get the SPA version
48  */
49 static int
50 get_zfs_spa_version(void)
51 {
52         size_t ver_size;
53         int ver = 0;
54
55         ver_size = sizeof(ver);
56         sysctlbyname("vfs.zfs.version.spa", &ver, &ver_size, NULL, 0);
57
58         return (ver);
59 }
60
61 /*
62  * This is FreeBSD version of ioctl, because Solaris' ioctl() updates
63  * zc_nvlist_dst_size even if an error is returned, on FreeBSD if an
64  * error is returned zc_nvlist_dst_size won't be updated.
65  */
66 int
67 zcmd_ioctl(int fd, int request, zfs_cmd_t *zc)
68 {
69         size_t oldsize;
70         int ret, cflag = ZFS_CMD_COMPAT_NONE;
71
72         if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
73                 zfs_ioctl_version = get_zfs_ioctl_version();
74
75         if (zfs_ioctl_version >= ZFS_IOCVER_DEADMAN) {
76                 switch (zfs_ioctl_version) {
77                 case ZFS_IOCVER_ZCMD:
78                         cflag = ZFS_CMD_COMPAT_ZCMD;
79                         break;
80                 case ZFS_IOCVER_LZC:
81                         cflag = ZFS_CMD_COMPAT_LZC;
82                         break;
83                 case ZFS_IOCVER_DEADMAN:
84                         cflag = ZFS_CMD_COMPAT_DEADMAN;
85                         break;
86                 }
87         } else {
88                 /*
89                  * If vfs.zfs.version.ioctl is not defined, assume we have v28
90                  * compatible binaries and use vfs.zfs.version.spa to test for v15
91                  */
92                 cflag = ZFS_CMD_COMPAT_V28;
93
94                 if (zfs_spa_version < 0)
95                         zfs_spa_version = get_zfs_spa_version();
96
97                 if (zfs_spa_version == SPA_VERSION_15 ||
98                     zfs_spa_version == SPA_VERSION_14 ||
99                     zfs_spa_version == SPA_VERSION_13)
100                         cflag = ZFS_CMD_COMPAT_V15;
101         }
102
103         oldsize = zc->zc_nvlist_dst_size;
104         ret = zcmd_ioctl_compat(fd, request, zc, cflag);
105
106         if (ret == 0 && oldsize < zc->zc_nvlist_dst_size) {
107                 ret = -1;
108                 errno = ENOMEM;
109         }
110
111         return (ret);
112 }