1248461Smm/* 2248461Smm * CDDL HEADER SART 3248461Smm * 4248461Smm * The contents of this file are subject to the terms of the 5248461Smm * Common Development and Distribution License (the "License"). 6248461Smm * You may not use this file except in compliance with the License. 7248461Smm * 8248461Smm * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9248461Smm * or http://www.opensolaris.org/os/licensing. 10248461Smm * See the License for the specific language governing permissions 11248461Smm * and limitations under the License. 12248461Smm * 13248461Smm * When distributing Covered Code, include this CDDL HEADER in each 14248461Smm * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15248461Smm * If applicable, add the following below this CDDL HEADER, with the 16248461Smm * fields enclosed by brackets "[]" replaced with your own identifying 17248461Smm * information: Portions Copyright [yyyy] [name of copyright owner] 18248461Smm * 19248461Smm * CDDL HEADER END 20248461Smm */ 21248461Smm 22248461Smm/* 23248461Smm * Copyright (c) 2013 Martin Matuska <mm@FreeBSD.org>. All rights reserved. 24248461Smm */ 25248461Smm 26248461Smm#include "libzfs_compat.h" 27248461Smm 28248461Smmint zfs_ioctl_version = ZFS_IOCVER_UNDEF; 29248461Smmstatic int zfs_spa_version = -1; 30248461Smm 31248461Smm/* 32248461Smm * Get zfs_ioctl_version 33248461Smm */ 34248461Smmint 35248461Smmget_zfs_ioctl_version(void) 36248461Smm{ 37248461Smm size_t ver_size; 38248461Smm int ver = ZFS_IOCVER_NONE; 39248461Smm 40248461Smm ver_size = sizeof(ver); 41248461Smm sysctlbyname("vfs.zfs.version.ioctl", &ver, &ver_size, NULL, 0); 42248461Smm 43248461Smm return (ver); 44248461Smm} 45248461Smm 46248461Smm/* 47248461Smm * Get the SPA version 48248461Smm */ 49248461Smmstatic int 50248461Smmget_zfs_spa_version(void) 51248461Smm{ 52248461Smm size_t ver_size; 53248461Smm int ver = 0; 54248461Smm 55248461Smm ver_size = sizeof(ver); 56248461Smm sysctlbyname("vfs.zfs.version.spa", &ver, &ver_size, NULL, 0); 57248461Smm 58248461Smm return (ver); 59248461Smm} 60248461Smm 61248461Smm/* 62248461Smm * This is FreeBSD version of ioctl, because Solaris' ioctl() updates 63248461Smm * zc_nvlist_dst_size even if an error is returned, on FreeBSD if an 64248461Smm * error is returned zc_nvlist_dst_size won't be updated. 65248461Smm */ 66248461Smmint 67248461Smmzcmd_ioctl(int fd, int request, zfs_cmd_t *zc) 68248461Smm{ 69248461Smm size_t oldsize; 70248461Smm int ret, cflag = ZFS_CMD_COMPAT_NONE; 71248461Smm 72248461Smm if (zfs_ioctl_version == ZFS_IOCVER_UNDEF) 73248461Smm zfs_ioctl_version = get_zfs_ioctl_version(); 74248461Smm 75249643Smm if (zfs_ioctl_version == ZFS_IOCVER_LZC) 76249643Smm cflag = ZFS_CMD_COMPAT_LZC; 77249643Smm else if (zfs_ioctl_version == ZFS_IOCVER_DEADMAN) 78248461Smm cflag = ZFS_CMD_COMPAT_DEADMAN; 79248461Smm 80248461Smm /* 81248461Smm * If vfs.zfs.version.ioctl is not defined, assume we have v28 82248461Smm * compatible binaries and use vfs.zfs.version.spa to test for v15 83248461Smm */ 84248461Smm if (zfs_ioctl_version < ZFS_IOCVER_DEADMAN) { 85248461Smm cflag = ZFS_CMD_COMPAT_V28; 86248461Smm 87248461Smm if (zfs_spa_version < 0) 88248461Smm zfs_spa_version = get_zfs_spa_version(); 89248461Smm 90248461Smm if (zfs_spa_version == SPA_VERSION_15 || 91248461Smm zfs_spa_version == SPA_VERSION_14 || 92248461Smm zfs_spa_version == SPA_VERSION_13) 93248461Smm cflag = ZFS_CMD_COMPAT_V15; 94248461Smm } 95248461Smm 96248461Smm oldsize = zc->zc_nvlist_dst_size; 97248461Smm ret = zcmd_ioctl_compat(fd, request, zc, cflag); 98248461Smm 99248461Smm if (ret == 0 && oldsize < zc->zc_nvlist_dst_size) { 100248461Smm ret = -1; 101248461Smm errno = ENOMEM; 102248461Smm } 103248461Smm 104248461Smm return (ret); 105248461Smm} 106