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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
  25  * Portions Copyright 2011 Martin Matuska
  26  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
  27  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  28  * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
  29  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
  30  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  31  * Copyright (c) 2013 Steven Hartland. All rights reserved.
  32  * Copyright (c) 2014 Integros [integros.com]
  33  * Copyright 2016 Toomas Soome <tsoome@me.com>
  34  * Copyright 2017 RackTop Systems.
  35  * Copyright (c) 2017 Datto Inc.
  36  */
  37 
  38 /*
  39  * ZFS ioctls.
  40  *
  41  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
  42  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
  43  *
  44  * There are two ways that we handle ioctls: the legacy way where almost
  45  * all of the logic is in the ioctl callback, and the new way where most
  46  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
  47  *
  48  * Non-legacy ioctls should be registered by calling
  49  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
  50  * from userland by lzc_ioctl().
  51  *
  52  * The registration arguments are as follows:
 
 
 139 #include <sys/types.h>
 140 #include <sys/param.h>
 141 #include <sys/errno.h>
 142 #include <sys/uio.h>
 143 #include <sys/buf.h>
 144 #include <sys/modctl.h>
 145 #include <sys/open.h>
 146 #include <sys/file.h>
 147 #include <sys/kmem.h>
 148 #include <sys/conf.h>
 149 #include <sys/cmn_err.h>
 150 #include <sys/stat.h>
 151 #include <sys/zfs_ioctl.h>
 152 #include <sys/zfs_vfsops.h>
 153 #include <sys/zfs_znode.h>
 154 #include <sys/zap.h>
 155 #include <sys/spa.h>
 156 #include <sys/spa_impl.h>
 157 #include <sys/vdev.h>
 158 #include <sys/priv_impl.h>
 159 #include <sys/dmu.h>
 160 #include <sys/dsl_dir.h>
 161 #include <sys/dsl_dataset.h>
 162 #include <sys/dsl_prop.h>
 163 #include <sys/dsl_deleg.h>
 164 #include <sys/dmu_objset.h>
 165 #include <sys/dmu_impl.h>
 166 #include <sys/dmu_tx.h>
 167 #include <sys/ddi.h>
 168 #include <sys/sunddi.h>
 169 #include <sys/sunldi.h>
 170 #include <sys/policy.h>
 171 #include <sys/zone.h>
 172 #include <sys/nvpair.h>
 173 #include <sys/pathname.h>
 174 #include <sys/mount.h>
 175 #include <sys/sdt.h>
 176 #include <sys/fs/zfs.h>
 177 #include <sys/zfs_ctldir.h>
 178 #include <sys/zfs_dir.h>
 179 #include <sys/zfs_onexit.h>
 180 #include <sys/zvol.h>
 181 #include <sys/dsl_scan.h>
 182 #include <sharefs/share.h>
 183 #include <sys/dmu_objset.h>
 184 #include <sys/dmu_send.h>
 185 #include <sys/dsl_destroy.h>
 186 #include <sys/dsl_bookmark.h>
 187 #include <sys/dsl_userhold.h>
 188 #include <sys/zfeature.h>
 189 #include <sys/zcp.h>
 190 #include <sys/zio_checksum.h>
 191 #include <sys/vdev_removal.h>
 192 
 193 #include "zfs_namecheck.h"
 194 #include "zfs_prop.h"
 195 #include "zfs_deleg.h"
 196 #include "zfs_comutil.h"
 197 
 198 #include "lua.h"
 199 #include "lauxlib.h"
 200 
 201 extern struct modlfs zfs_modlfs;
 202 
 203 extern void zfs_init(void);
 204 extern void zfs_fini(void);
 205 
 206 ldi_ident_t zfs_li = NULL;
 207 dev_info_t *zfs_dip;
 208 
 209 uint_t zfs_fsyncer_key;
 210 extern uint_t rrw_tsd_key;
 211 static uint_t zfs_allow_log_key;
 212 
 213 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
 214 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
 215 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
 216 
 
 
 240 /* This array is indexed by zfs_userquota_prop_t */
 241 static const char *userquota_perms[] = {
 242         ZFS_DELEG_PERM_USERUSED,
 243         ZFS_DELEG_PERM_USERQUOTA,
 244         ZFS_DELEG_PERM_GROUPUSED,
 245         ZFS_DELEG_PERM_GROUPQUOTA,
 246 };
 247 
 248 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
 249 static int zfs_check_settable(const char *name, nvpair_t *property,
 250     cred_t *cr);
 251 static int zfs_check_clearable(char *dataset, nvlist_t *props,
 252     nvlist_t **errors);
 253 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
 254     boolean_t *);
 255 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
 256 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
 257 
 258 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
 259 
 260 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
 261 void
 262 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
 263 {
 264         const char *newfile;
 265         char buf[512];
 266         va_list adx;
 267 
 268         /*
 269          * Get rid of annoying "../common/" prefix to filename.
 270          */
 271         newfile = strrchr(file, '/');
 272         if (newfile != NULL) {
 273                 newfile = newfile + 1; /* Get rid of leading / */
 274         } else {
 275                 newfile = file;
 276         }
 277 
 278         va_start(adx, fmt);
 279         (void) vsnprintf(buf, sizeof (buf), fmt, adx);
 
 755             NO_FOLLOW, NULL, &vp)) != 0)
 756                 return (error);
 757 
 758         /* Now make sure mntpnt and dataset are ZFS */
 759 
 760         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
 761             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
 762             zc->zc_name) != 0)) {
 763                 VN_RELE(vp);
 764                 return (SET_ERROR(EPERM));
 765         }
 766 
 767         VN_RELE(vp);
 768         return (dsl_deleg_access(zc->zc_name,
 769             ZFS_DELEG_PERM_SHARE, cr));
 770 }
 771 
 772 int
 773 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 774 {
 775         if (!INGLOBALZONE(curproc))
 776                 return (SET_ERROR(EPERM));
 777 
 778         if (secpolicy_nfs(cr) == 0) {
 779                 return (0);
 780         } else {
 781                 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 782         }
 783 }
 784 
 785 int
 786 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 787 {
 788         if (!INGLOBALZONE(curproc))
 789                 return (SET_ERROR(EPERM));
 790 
 791         if (secpolicy_smb(cr) == 0) {
 792                 return (0);
 793         } else {
 794                 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 795         }
 796 }
 797 
 798 static int
 799 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
 800 {
 801         char *cp;
 802 
 803         /*
 804          * Remove the @bla or /bla from the end of the name to get the parent.
 805          */
 806         (void) strncpy(parent, datasetname, parentsize);
 807         cp = strrchr(parent, '@');
 808         if (cp != NULL) {
 809                 cp[0] = '\0';
 810         } else {
 
1026             pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1027                 char *name = nvpair_name(pair);
1028                 char *hashp = strchr(name, '#');
1029 
1030                 if (hashp == NULL) {
1031                         error = SET_ERROR(EINVAL);
1032                         break;
1033                 }
1034                 *hashp = '\0';
1035                 error = zfs_secpolicy_write_perms(name,
1036                     ZFS_DELEG_PERM_BOOKMARK, cr);
1037                 *hashp = '#';
1038                 if (error != 0)
1039                         break;
1040         }
1041         return (error);
1042 }
1043 
1044 /* ARGSUSED */
1045 static int
1046 zfs_secpolicy_remap(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1047 {
1048         return (zfs_secpolicy_write_perms(zc->zc_name,
1049             ZFS_DELEG_PERM_REMAP, cr));
1050 }
1051 
1052 /* ARGSUSED */
1053 static int
1054 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1055 {
1056         nvpair_t *pair, *nextpair;
1057         int error = 0;
1058 
1059         for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1060             pair = nextpair) {
1061                 char *name = nvpair_name(pair);
1062                 char *hashp = strchr(name, '#');
1063                 nextpair = nvlist_next_nvpair(innvl, pair);
1064 
1065                 if (hashp == NULL) {
1066                         error = SET_ERROR(EINVAL);
1067                         break;
1068                 }
1069 
1070                 *hashp = '\0';
1071                 error = zfs_secpolicy_write_perms(name,
1072                     ZFS_DELEG_PERM_DESTROY, cr);
1073                 *hashp = '#';
 
1366 
1367                 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1368                 more_errors = nvlist_prev_nvpair(errors, NULL);
1369 
1370                 do {
1371                         nvpair_t *pair = nvlist_prev_nvpair(errors,
1372                             more_errors);
1373                         fnvlist_remove_nvpair(errors, pair);
1374                         n++;
1375                         size = fnvlist_size(errors);
1376                 } while (size > max);
1377 
1378                 fnvlist_remove_nvpair(errors, more_errors);
1379                 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1380                 ASSERT3U(fnvlist_size(errors), <=, max);
1381         }
1382 
1383         return (0);
1384 }
1385 
1386 static int
1387 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1388 {
1389         char *packed = NULL;
1390         int error = 0;
1391         size_t size;
1392 
1393         size = fnvlist_size(nvl);
1394 
1395         if (size > zc->zc_nvlist_dst_size) {
1396                 error = SET_ERROR(ENOMEM);
1397         } else {
1398                 packed = fnvlist_pack(nvl, &size);
1399                 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1400                     size, zc->zc_iflags) != 0)
1401                         error = SET_ERROR(EFAULT);
1402                 fnvlist_pack_free(packed, size);
1403         }
1404 
1405         zc->zc_nvlist_dst_size = size;
1406         zc->zc_nvlist_dst_filled = B_TRUE;
1407         return (error);
1408 }
1409 
1410 int
1411 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1412 {
1413         int error = 0;
1414         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1415                 return (SET_ERROR(EINVAL));
1416         }
1417 
1418         mutex_enter(&os->os_user_ptr_lock);
1419         *zfvp = dmu_objset_get_user(os);
1420         if (*zfvp) {
1421                 VFS_HOLD((*zfvp)->z_vfs);
1422         } else {
1423                 error = SET_ERROR(ESRCH);
1424         }
1425         mutex_exit(&os->os_user_ptr_lock);
1426         return (error);
1427 }
1428 
1429 int
 
1466                         rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1467                         return (SET_ERROR(EBUSY));
1468                 }
1469         }
1470         return (error);
1471 }
1472 
1473 static void
1474 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1475 {
1476         rrm_exit(&zfsvfs->z_teardown_lock, tag);
1477 
1478         if (zfsvfs->z_vfs) {
1479                 VFS_RELE(zfsvfs->z_vfs);
1480         } else {
1481                 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1482                 zfsvfs_free(zfsvfs);
1483         }
1484 }
1485 
1486 static int
1487 zfs_ioc_pool_create(zfs_cmd_t *zc)
1488 {
1489         int error;
1490         nvlist_t *config, *props = NULL;
1491         nvlist_t *rootprops = NULL;
1492         nvlist_t *zplprops = NULL;
1493 
1494         if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1495             zc->zc_iflags, &config))
1496                 return (error);
1497 
1498         if (zc->zc_nvlist_src_size != 0 && (error =
1499             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1500             zc->zc_iflags, &props))) {
1501                 nvlist_free(config);
1502                 return (error);
1503         }
1504 
1505         if (props) {
1506                 nvlist_t *nvl = NULL;
1507                 uint64_t version = SPA_VERSION;
1508 
1509                 (void) nvlist_lookup_uint64(props,
1510                     zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1511                 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1512                         error = SET_ERROR(EINVAL);
 
1521                                 return (error);
1522                         }
1523                         (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1524                 }
1525                 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1526                 error = zfs_fill_zplprops_root(version, rootprops,
1527                     zplprops, NULL);
1528                 if (error != 0)
1529                         goto pool_props_bad;
1530         }
1531 
1532         error = spa_create(zc->zc_name, config, props, zplprops);
1533 
1534         /*
1535          * Set the remaining root properties
1536          */
1537         if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1538             ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1539                 (void) spa_destroy(zc->zc_name);
1540 
1541 pool_props_bad:
1542         nvlist_free(rootprops);
1543         nvlist_free(zplprops);
1544         nvlist_free(config);
1545         nvlist_free(props);
1546 
1547         return (error);
1548 }
1549 
1550 static int
1551 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1552 {
1553         int error;
1554         zfs_log_history(zc);
1555         error = spa_destroy(zc->zc_name);
1556         if (error == 0)
1557                 zvol_remove_minors(zc->zc_name);
1558         return (error);
1559 }
1560 
1561 static int
1562 zfs_ioc_pool_import(zfs_cmd_t *zc)
1563 {
1564         nvlist_t *config, *props = NULL;
1565         uint64_t guid;
1566         int error;
1567 
1568         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1569             zc->zc_iflags, &config)) != 0)
1570                 return (error);
1571 
1572         if (zc->zc_nvlist_src_size != 0 && (error =
1573             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1574             zc->zc_iflags, &props))) {
1575                 nvlist_free(config);
1576                 return (error);
1577         }
1578 
1579         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1580             guid != zc->zc_guid)
1581                 error = SET_ERROR(EINVAL);
1582         else
1583                 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1584 
1585         if (zc->zc_nvlist_dst != 0) {
1586                 int err;
1587 
1588                 if ((err = put_nvlist(zc, config)) != 0)
1589                         error = err;
1590         }
1591 
1592         nvlist_free(config);
1593 
1594         nvlist_free(props);
1595 
1596         return (error);
1597 }
1598 
1599 static int
1600 zfs_ioc_pool_export(zfs_cmd_t *zc)
1601 {
1602         int error;
1603         boolean_t force = (boolean_t)zc->zc_cookie;
1604         boolean_t hardforce = (boolean_t)zc->zc_guid;
1605 
1606         zfs_log_history(zc);
1607         error = spa_export(zc->zc_name, NULL, force, hardforce);
1608         if (error == 0)
1609                 zvol_remove_minors(zc->zc_name);
1610         return (error);
1611 }
1612 
1613 static int
1614 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1615 {
1616         nvlist_t *configs;
1617         int error;
1618 
1619         if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1620                 return (SET_ERROR(EEXIST));
1621 
1622         error = put_nvlist(zc, configs);
1623 
1624         nvlist_free(configs);
1625 
1626         return (error);
1627 }
1628 
1629 /*
 
1702         int error;
1703 
1704         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1705                 return (error);
1706 
1707         if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1708                 return (SET_ERROR(EINVAL));
1709 
1710         if (zc->zc_flags == POOL_SCRUB_PAUSE)
1711                 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1712         else if (zc->zc_cookie == POOL_SCAN_NONE)
1713                 error = spa_scan_stop(spa);
1714         else
1715                 error = spa_scan(spa, zc->zc_cookie);
1716 
1717         spa_close(spa, FTAG);
1718 
1719         return (error);
1720 }
1721 
1722 static int
1723 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1724 {
1725         spa_t *spa;
1726         int error;
1727 
1728         error = spa_open(zc->zc_name, &spa, FTAG);
1729         if (error == 0) {
1730                 spa_freeze(spa);
1731                 spa_close(spa, FTAG);
1732         }
1733         return (error);
1734 }
1735 
1736 static int
1737 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1738 {
1739         spa_t *spa;
1740         int error;
1741 
1742         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
 
1853         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
1854                 return (error);
1855         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1856                 dmu_objset_rele(os, FTAG);
1857                 return (SET_ERROR(EINVAL));
1858         }
1859         error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
1860             sizeof (zc->zc_value));
1861         dmu_objset_rele(os, FTAG);
1862 
1863         return (error);
1864 }
1865 
1866 static int
1867 zfs_ioc_vdev_add(zfs_cmd_t *zc)
1868 {
1869         spa_t *spa;
1870         int error;
1871         nvlist_t *config, **l2cache, **spares;
1872         uint_t nl2cache = 0, nspares = 0;
1873 
1874         error = spa_open(zc->zc_name, &spa, FTAG);
1875         if (error != 0)
1876                 return (error);
1877 
1878         error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1879             zc->zc_iflags, &config);
1880         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
1881             &l2cache, &nl2cache);
1882 
1883         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
1884             &spares, &nspares);
1885 
1886         /*
1887          * A root pool with concatenated devices is not supported.
1888          * Thus, can not add a device to a root pool.
1889          *
1890          * Intent log device can not be added to a rootpool because
1891          * during mountroot, zil is replayed, a seperated log device
1892          * can not be accessed during the mountroot time.
1893          *
1894          * l2cache and spare devices are ok to be added to a rootpool.
1895          */
1896         if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
1897                 nvlist_free(config);
1898                 spa_close(spa, FTAG);
1899                 return (SET_ERROR(EDOM));
1900         }
1901 
1902         if (error == 0) {
1903                 error = spa_vdev_add(spa, config);
1904                 nvlist_free(config);
1905         }
1906         spa_close(spa, FTAG);
1907         return (error);
1908 }
1909 
1910 /*
1911  * inputs:
1912  * zc_name              name of the pool
1913  * zc_guid              guid of vdev to remove
1914  * zc_cookie            cancel removal
1915  */
1916 static int
1917 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
1918 {
1919         spa_t *spa;
1920         int error;
1921 
1922         error = spa_open(zc->zc_name, &spa, FTAG);
1923         if (error != 0)
1924                 return (error);
1925         if (zc->zc_cookie != 0) {
1926                 error = spa_vdev_remove_cancel(spa);
1927         } else {
1928                 error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
1929         }
1930         spa_close(spa, FTAG);
1931         return (error);
1932 }
1933 
1934 static int
1935 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
1936 {
1937         spa_t *spa;
1938         int error;
1939         vdev_state_t newstate = VDEV_STATE_UNKNOWN;
1940 
1941         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1942                 return (error);
1943         switch (zc->zc_cookie) {
1944         case VDEV_STATE_ONLINE:
1945                 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
1946                 break;
1947 
1948         case VDEV_STATE_OFFLINE:
1949                 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
1950                 break;
1951 
1952         case VDEV_STATE_FAULTED:
1953                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1954                     zc->zc_obj != VDEV_AUX_EXTERNAL)
1955                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1956 
1957                 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
1958                 break;
1959 
1960         case VDEV_STATE_DEGRADED:
1961                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
1962                     zc->zc_obj != VDEV_AUX_EXTERNAL)
1963                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
1964 
1965                 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
1966                 break;
1967 
1968         default:
1969                 error = SET_ERROR(EINVAL);
1970         }
1971         zc->zc_cookie = newstate;
1972         spa_close(spa, FTAG);
1973         return (error);
1974 }
1975 
1976 static int
1977 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
1978 {
1979         spa_t *spa;
1980         int replacing = zc->zc_cookie;
1981         nvlist_t *config;
1982         int error;
1983 
1984         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1985                 return (error);
1986 
1987         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1988             zc->zc_iflags, &config)) == 0) {
1989                 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
1990                 nvlist_free(config);
1991         }
1992 
1993         spa_close(spa, FTAG);
1994         return (error);
1995 }
1996 
1997 static int
1998 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
1999 {
2000         spa_t *spa;
2001         int error;
2002 
2003         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2004                 return (error);
2005 
2006         error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2007 
2008         spa_close(spa, FTAG);
2009         return (error);
2010 }
2011 
2012 static int
2013 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2014 {
2015         spa_t *spa;
2016         nvlist_t *config, *props = NULL;
2017         int error;
2018         boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2019 
2020         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2021                 return (error);
2022 
2023         if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2024             zc->zc_iflags, &config)) {
2025                 spa_close(spa, FTAG);
2026                 return (error);
2027         }
2028 
2029         if (zc->zc_nvlist_src_size != 0 && (error =
2030             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2031             zc->zc_iflags, &props))) {
2032                 spa_close(spa, FTAG);
2033                 nvlist_free(config);
2034                 return (error);
2035         }
2036 
2037         error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2038 
2039         spa_close(spa, FTAG);
2040 
2041         nvlist_free(config);
2042         nvlist_free(props);
2043 
2044         return (error);
2045 }
2046 
2047 static int
2048 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2049 {
2050         spa_t *spa;
2051         char *path = zc->zc_value;
2052         uint64_t guid = zc->zc_guid;
2053         int error;
2054 
2055         error = spa_open(zc->zc_name, &spa, FTAG);
2056         if (error != 0)
2057                 return (error);
2058 
2059         error = spa_vdev_setpath(spa, guid, path);
2060         spa_close(spa, FTAG);
2061         return (error);
2062 }
2063 
2064 static int
2065 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2066 {
2067         spa_t *spa;
 
2106                 error = put_nvlist(zc, nv);
2107                 nvlist_free(nv);
2108         }
2109 
2110         return (error);
2111 }
2112 
2113 /*
2114  * inputs:
2115  * zc_name              name of filesystem
2116  * zc_nvlist_dst_size   size of buffer for property nvlist
2117  *
2118  * outputs:
2119  * zc_objset_stats      stats
2120  * zc_nvlist_dst        property nvlist
2121  * zc_nvlist_dst_size   size of property nvlist
2122  */
2123 static int
2124 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2125 {
2126         objset_t *os;
2127         int error;
2128 
2129         error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2130         if (error == 0) {
2131                 error = zfs_ioc_objset_stats_impl(zc, os);
2132                 dmu_objset_rele(os, FTAG);
2133         }
2134 
2135         return (error);
2136 }
2137 
2138 /*
2139  * inputs:
2140  * zc_name              name of filesystem
2141  * zc_nvlist_dst_size   size of buffer for property nvlist
2142  *
2143  * outputs:
2144  * zc_nvlist_dst        received property nvlist
2145  * zc_nvlist_dst_size   size of received property nvlist
2146  *
 
2215          */
2216         if (zc->zc_nvlist_dst != NULL &&
2217             !zc->zc_objset_stats.dds_inconsistent &&
2218             dmu_objset_type(os) == DMU_OST_ZFS) {
2219                 nvlist_t *nv;
2220 
2221                 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2222                 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2223                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2224                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2225                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2226                         err = put_nvlist(zc, nv);
2227                 nvlist_free(nv);
2228         } else {
2229                 err = SET_ERROR(ENOENT);
2230         }
2231         dmu_objset_rele(os, FTAG);
2232         return (err);
2233 }
2234 
2235 static boolean_t
2236 dataset_name_hidden(const char *name)
2237 {
2238         /*
2239          * Skip over datasets that are not visible in this zone,
2240          * internal datasets (which have a $ in their name), and
2241          * temporary datasets (which have a % in their name).
2242          */
2243         if (strchr(name, '$') != NULL)
2244                 return (B_TRUE);
2245         if (strchr(name, '%') != NULL)
2246                 return (B_TRUE);
2247         if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL))
2248                 return (B_TRUE);
2249         return (B_FALSE);
2250 }
2251 
2252 /*
2253  * inputs:
2254  * zc_name              name of filesystem
2255  * zc_cookie            zap cursor
2256  * zc_nvlist_dst_size   size of buffer for property nvlist
2257  *
2258  * outputs:
2259  * zc_name              name of next filesystem
2260  * zc_cookie            zap cursor
2261  * zc_objset_stats      stats
2262  * zc_nvlist_dst        property nvlist
2263  * zc_nvlist_dst_size   size of property nvlist
2264  */
2265 static int
2266 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2267 {
2268         objset_t *os;
2269         int error;
2270         char *p;
2271         size_t orig_len = strlen(zc->zc_name);
 
2504                 err = -1;
2505         }
2506 
2507         return (err);
2508 }
2509 
2510 /*
2511  * This function is best effort. If it fails to set any of the given properties,
2512  * it continues to set as many as it can and returns the last error
2513  * encountered. If the caller provides a non-NULL errlist, it will be filled in
2514  * with the list of names of all the properties that failed along with the
2515  * corresponding error numbers.
2516  *
2517  * If every property is set successfully, zero is returned and errlist is not
2518  * modified.
2519  */
2520 int
2521 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2522     nvlist_t *errlist)
2523 {
2524         nvpair_t *pair;
2525         nvpair_t *propval;
2526         int rv = 0;
2527         uint64_t intval;
2528         char *strval;
2529         nvlist_t *genericnvl = fnvlist_alloc();
2530         nvlist_t *retrynvl = fnvlist_alloc();
2531 
2532 retry:
2533         pair = NULL;
2534         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2535                 const char *propname = nvpair_name(pair);
2536                 zfs_prop_t prop = zfs_name_to_prop(propname);
2537                 int err = 0;
2538 
2539                 /* decode the property value */
2540                 propval = pair;
2541                 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2542                         nvlist_t *attrs;
2543                         attrs = fnvpair_value_nvlist(pair);
2544                         if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2545                             &propval) != 0)
2546                                 err = SET_ERROR(EINVAL);
2547                 }
2548 
2549                 /* Validate value type */
2550                 if (err == 0 && prop == ZPROP_INVAL) {
2551                         if (zfs_prop_user(propname)) {
2552                                 if (nvpair_type(propval) != DATA_TYPE_STRING)
2553                                         err = SET_ERROR(EINVAL);
2554                         } else if (zfs_prop_userquota(propname)) {
2555                                 if (nvpair_type(propval) !=
2556                                     DATA_TYPE_UINT64_ARRAY)
2557                                         err = SET_ERROR(EINVAL);
2558                         } else {
 
2604                                  * This may be a spurious error caused by
2605                                  * receiving quota and reservation out of order.
2606                                  * Try again in a second pass.
2607                                  */
2608                                 err = nvlist_add_nvpair(retrynvl, pair);
2609                         }
2610                 }
2611 
2612                 if (err != 0) {
2613                         if (errlist != NULL)
2614                                 fnvlist_add_int32(errlist, propname, err);
2615                         rv = err;
2616                 }
2617         }
2618 
2619         if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2620                 nvl = retrynvl;
2621                 goto retry;
2622         }
2623 
2624         if (!nvlist_empty(genericnvl) &&
2625             dsl_props_set(dsname, source, genericnvl) != 0) {
2626                 /*
2627                  * If this fails, we still want to set as many properties as we
2628                  * can, so try setting them individually.
2629                  */
2630                 pair = NULL;
2631                 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2632                         const char *propname = nvpair_name(pair);
2633                         int err = 0;
2634 
2635                         propval = pair;
2636                         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2637                                 nvlist_t *attrs;
2638                                 attrs = fnvpair_value_nvlist(pair);
2639                                 propval = fnvlist_lookup_nvpair(attrs,
2640                                     ZPROP_VALUE);
2641                         }
2642 
2643                         if (nvpair_type(propval) == DATA_TYPE_STRING) {
 
2645                                 err = dsl_prop_set_string(dsname, propname,
2646                                     source, strval);
2647                         } else {
2648                                 intval = fnvpair_value_uint64(propval);
2649                                 err = dsl_prop_set_int(dsname, propname, source,
2650                                     intval);
2651                         }
2652 
2653                         if (err != 0) {
2654                                 if (errlist != NULL) {
2655                                         fnvlist_add_int32(errlist, propname,
2656                                             err);
2657                                 }
2658                                 rv = err;
2659                         }
2660                 }
2661         }
2662         nvlist_free(genericnvl);
2663         nvlist_free(retrynvl);
2664 
2665         return (rv);
2666 }
2667 
2668 /*
2669  * Check that all the properties are valid user properties.
2670  */
2671 static int
2672 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2673 {
2674         nvpair_t *pair = NULL;
2675         int error = 0;
2676 
2677         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2678                 const char *propname = nvpair_name(pair);
2679 
2680                 if (!zfs_prop_user(propname) ||
2681                     nvpair_type(pair) != DATA_TYPE_STRING)
2682                         return (SET_ERROR(EINVAL));
2683 
2684                 if (error = zfs_secpolicy_write_perms(fsname,
2685                     ZFS_DELEG_PERM_USERPROP, CRED()))
2686                         return (error);
2687 
2688                 if (strlen(propname) >= ZAP_MAXNAMELEN)
2689                         return (SET_ERROR(ENAMETOOLONG));
2690 
2691                 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2692                         return (E2BIG);
2693         }
2694         return (0);
2695 }
2696 
2697 static void
2698 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2699 {
2700         nvpair_t *pair;
2701 
2702         VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2703 
2704         pair = NULL;
2705         while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2706                 if (nvlist_exists(skipped, nvpair_name(pair)))
2707                         continue;
2708 
2709                 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2710         }
2711 }
2712 
2713 static int
2714 clear_received_props(const char *dsname, nvlist_t *props,
2715     nvlist_t *skipped)
2716 {
2717         int err = 0;
2718         nvlist_t *cleared_props = NULL;
2719         props_skip(props, skipped, &cleared_props);
2720         if (!nvlist_empty(cleared_props)) {
2721                 /*
2722                  * Acts on local properties until the dataset has received
2723                  * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2724                  */
2725                 zprop_source_t flags = (ZPROP_SRC_NONE |
2726                     (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2727                 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2728         }
2729         nvlist_free(cleared_props);
2730         return (err);
2731 }
2732 
2733 /*
2734  * inputs:
2735  * zc_name              name of filesystem
2736  * zc_value             name of property to set
2737  * zc_nvlist_src{_size} nvlist of properties to apply
2738  * zc_cookie            received properties flag
2739  *
2740  * outputs:
2741  * zc_nvlist_dst{_size} error for each unapplied received property
2742  */
2743 static int
2744 zfs_ioc_set_prop(zfs_cmd_t *zc)
2745 {
2746         nvlist_t *nvl;
2747         boolean_t received = zc->zc_cookie;
2748         zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2749             ZPROP_SRC_LOCAL);
2750         nvlist_t *errors;
2751         int error;
2752 
2753         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2754             zc->zc_iflags, &nvl)) != 0)
2755                 return (error);
2756 
2757         if (received) {
2758                 nvlist_t *origprops;
2759 
2760                 if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) {
2761                         (void) clear_received_props(zc->zc_name,
2762                             origprops, nvl);
2763                         nvlist_free(origprops);
2764                 }
2765 
2766                 error = dsl_prop_set_hasrecvd(zc->zc_name);
2767         }
2768 
2769         errors = fnvlist_alloc();
2770         if (error == 0)
2771                 error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors);
2772 
2773         if (zc->zc_nvlist_dst != NULL && errors != NULL) {
2774                 (void) put_nvlist(zc, errors);
2775         }
2776 
2777         nvlist_free(errors);
2778         nvlist_free(nvl);
2779         return (error);
2780 }
2781 
2782 /*
2783  * inputs:
2784  * zc_name              name of filesystem
2785  * zc_value             name of property to inherit
2786  * zc_cookie            revert to received value if TRUE
2787  *
2788  * outputs:             none
2789  */
2790 static int
2791 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
2792 {
 
2844                  * Only check this in the non-received case. We want to allow
2845                  * 'inherit -S' to revert non-inheritable properties like quota
2846                  * and reservation to the received or default values even though
2847                  * they are not considered inheritable.
2848                  */
2849                 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
2850                         return (SET_ERROR(EINVAL));
2851         }
2852 
2853         /* property name has been validated by zfs_secpolicy_inherit_prop() */
2854         return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
2855 }
2856 
2857 static int
2858 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
2859 {
2860         nvlist_t *props;
2861         spa_t *spa;
2862         int error;
2863         nvpair_t *pair;
2864 
2865         if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2866             zc->zc_iflags, &props))
2867                 return (error);
2868 
2869         /*
2870          * If the only property is the configfile, then just do a spa_lookup()
2871          * to handle the faulted case.
2872          */
2873         pair = nvlist_next_nvpair(props, NULL);
2874         if (pair != NULL && strcmp(nvpair_name(pair),
2875             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
2876             nvlist_next_nvpair(props, pair) == NULL) {
2877                 mutex_enter(&spa_namespace_lock);
2878                 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
2879                         spa_configfile_set(spa, props, B_FALSE);
2880                         spa_write_cachefile(spa, B_FALSE, B_TRUE);
2881                 }
2882                 mutex_exit(&spa_namespace_lock);
2883                 if (spa != NULL) {
2884                         nvlist_free(props);
2885                         return (0);
2886                 }
2887         }
2888 
2889         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2890                 nvlist_free(props);
2891                 return (error);
2892         }
2893 
2894         error = spa_prop_set(spa, props);
2895 
2896         nvlist_free(props);
2897         spa_close(spa, FTAG);
2898 
2899         return (error);
2900 }
2901 
2902 static int
2903 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
2904 {
2905         spa_t *spa;
2906         int error;
2907         nvlist_t *nvp = NULL;
2908 
2909         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
2910                 /*
2911                  * If the pool is faulted, there may be properties we can still
2912                  * get (such as altroot and cachefile), so attempt to get them
2913                  * anyway.
2914                  */
2915                 mutex_enter(&spa_namespace_lock);
 
2997                 error = put_nvlist(zc, nvp);
2998                 nvlist_free(nvp);
2999         }
3000 
3001         return (error);
3002 }
3003 
3004 /* ARGSUSED */
3005 static void
3006 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3007 {
3008         zfs_creat_t *zct = arg;
3009 
3010         zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3011 }
3012 
3013 #define ZFS_PROP_UNDEFINED      ((uint64_t)-1)
3014 
3015 /*
3016  * inputs:
3017  * os                   parent objset pointer (NULL if root fs)
3018  * fuids_ok             fuids allowed in this version of the spa?
3019  * sa_ok                SAs allowed in this version of the spa?
3020  * createprops          list of properties requested by creator
3021  *
3022  * outputs:
3023  * zplprops     values for the zplprops we attach to the master node object
3024  * is_ci        true if requested file system will be purely case-insensitive
3025  *
3026  * Determine the settings for utf8only, normalization and
3027  * casesensitivity.  Specific values may have been requested by the
3028  * creator and/or we can inherit values from the parent dataset.  If
3029  * the file system is of too early a vintage, a creator can not
3030  * request settings for these properties, even if the requested
3031  * setting is the default value.  We don't actually want to create dsl
3032  * properties for these, so remove them from the source nvlist after
3033  * processing.
3034  */
3035 static int
3036 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
 
3074          */
3075         if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3076             (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3077             (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3078             (zplver < ZPL_VERSION_NORMALIZATION &&
3079             (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3080             sense != ZFS_PROP_UNDEFINED)))
3081                 return (SET_ERROR(ENOTSUP));
3082 
3083         /*
3084          * Put the version in the zplprops
3085          */
3086         VERIFY(nvlist_add_uint64(zplprops,
3087             zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3088 
3089         if (norm == ZFS_PROP_UNDEFINED)
3090                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3091         VERIFY(nvlist_add_uint64(zplprops,
3092             zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3093 
3094         /*
3095          * If we're normalizing, names must always be valid UTF-8 strings.
3096          */
3097         if (norm)
3098                 u8 = 1;
3099         if (u8 == ZFS_PROP_UNDEFINED)
3100                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3101         VERIFY(nvlist_add_uint64(zplprops,
3102             zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3103 
3104         if (sense == ZFS_PROP_UNDEFINED)
3105                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3106         VERIFY(nvlist_add_uint64(zplprops,
3107             zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3108 
3109         if (is_ci)
3110                 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3111 
3112         return (0);
3113 }
 
 
3171 }
3172 
3173 /*
3174  * innvl: {
3175  *     "type" -> dmu_objset_type_t (int32)
3176  *     (optional) "props" -> { prop -> value }
3177  * }
3178  *
3179  * outnvl: propname -> error code (int32)
3180  */
3181 static int
3182 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3183 {
3184         int error = 0;
3185         zfs_creat_t zct = { 0 };
3186         nvlist_t *nvprops = NULL;
3187         void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3188         int32_t type32;
3189         dmu_objset_type_t type;
3190         boolean_t is_insensitive = B_FALSE;
3191 
3192         if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3193                 return (SET_ERROR(EINVAL));
3194         type = type32;
3195         (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3196 
3197         switch (type) {
3198         case DMU_OST_ZFS:
3199                 cbfunc = zfs_create_cb;
3200                 break;
3201 
3202         case DMU_OST_ZVOL:
3203                 cbfunc = zvol_create_cb;
3204                 break;
3205 
3206         default:
3207                 cbfunc = NULL;
3208                 break;
3209         }
3210         if (strchr(fsname, '@') ||
3211             strchr(fsname, '%'))
3212                 return (SET_ERROR(EINVAL));
3213 
3214         zct.zct_props = nvprops;
3215 
3216         if (cbfunc == NULL)
3217                 return (SET_ERROR(EINVAL));
3218 
3219         if (type == DMU_OST_ZVOL) {
3220                 uint64_t volsize, volblocksize;
3221 
3222                 if (nvprops == NULL)
3223                         return (SET_ERROR(EINVAL));
3224                 if (nvlist_lookup_uint64(nvprops,
3225                     zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3226                         return (SET_ERROR(EINVAL));
3227 
3228                 if ((error = nvlist_lookup_uint64(nvprops,
3229                     zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3230                     &volblocksize)) != 0 && error != ENOENT)
3231                         return (SET_ERROR(EINVAL));
3232 
3233                 if (error != 0)
3234                         volblocksize = zfs_prop_default_numeric(
3235                             ZFS_PROP_VOLBLOCKSIZE);
3236 
3237                 if ((error = zvol_check_volblocksize(
3238                     volblocksize)) != 0 ||
3239                     (error = zvol_check_volsize(volsize,
3240                     volblocksize)) != 0)
3241                         return (error);
3242         } else if (type == DMU_OST_ZFS) {
3243                 int error;
3244 
3245                 /*
3246                  * We have to have normalization and
3247                  * case-folding flags correct when we do the
3248                  * file system creation, so go figure them out
3249                  * now.
3250                  */
3251                 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3252                     NV_UNIQUE_NAME, KM_SLEEP) == 0);
3253                 error = zfs_fill_zplprops(fsname, nvprops,
3254                     zct.zct_zplprops, &is_insensitive);
3255                 if (error != 0) {
3256                         nvlist_free(zct.zct_zplprops);
3257                         return (error);
3258                 }
3259         }
3260 
3261         error = dmu_objset_create(fsname, type,
3262             is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3263         nvlist_free(zct.zct_zplprops);
3264 
3265         /*
3266          * It would be nice to do this atomically.
3267          */
3268         if (error == 0) {
3269                 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3270                     nvprops, outnvl);
3271                 if (error != 0)
3272                         (void) dsl_destroy_head(fsname);
3273         }
3274         return (error);
3275 }
3276 
3277 /*
3278  * innvl: {
3279  *     "origin" -> name of origin snapshot
3280  *     (optional) "props" -> { prop -> value }
3281  * }
3282  *
3283  * outnvl: propname -> error code (int32)
3284  */
3285 static int
3286 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3287 {
3288         int error = 0;
3289         nvlist_t *nvprops = NULL;
3290         char *origin_name;
3291 
3292         if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3293                 return (SET_ERROR(EINVAL));
3294         (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3295 
3296         if (strchr(fsname, '@') ||
3297             strchr(fsname, '%'))
3298                 return (SET_ERROR(EINVAL));
3299 
3300         if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3301                 return (SET_ERROR(EINVAL));
3302         error = dmu_objset_clone(fsname, origin_name);
3303         if (error != 0)
3304                 return (error);
3305 
3306         /*
3307          * It would be nice to do this atomically.
3308          */
3309         if (error == 0) {
3310                 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3311                     nvprops, outnvl);
3312                 if (error != 0)
3313                         (void) dsl_destroy_head(fsname);
3314         }
3315         return (error);
3316 }
3317 
3318 /* ARGSUSED */
3319 static int
3320 zfs_ioc_remap(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3321 {
3322         if (strchr(fsname, '@') ||
3323             strchr(fsname, '%'))
3324                 return (SET_ERROR(EINVAL));
3325 
3326         return (dmu_objset_remap_indirects(fsname));
3327 }
3328 
3329 /*
3330  * innvl: {
3331  *     "snaps" -> { snapshot1, snapshot2 }
3332  *     (optional) "props" -> { prop -> value (string) }
3333  * }
3334  *
3335  * outnvl: snapshot -> error code (int32)
3336  */
3337 static int
3338 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3339 {
3340         nvlist_t *snaps;
3341         nvlist_t *props = NULL;
3342         int error, poollen;
3343         nvpair_t *pair;
3344 
3345         (void) nvlist_lookup_nvlist(innvl, "props", &props);
3346         if ((error = zfs_check_userprops(poolname, props)) != 0)
3347                 return (error);
3348 
3349         if (!nvlist_empty(props) &&
3350             zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3351                 return (SET_ERROR(ENOTSUP));
3352 
3353         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3354                 return (SET_ERROR(EINVAL));
3355         poollen = strlen(poolname);
3356         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3357             pair = nvlist_next_nvpair(snaps, pair)) {
3358                 const char *name = nvpair_name(pair);
3359                 const char *cp = strchr(name, '@');
3360 
3361                 /*
3362                  * The snap name must contain an @, and the part after it must
3363                  * contain only valid characters.
3364                  */
3365                 if (cp == NULL ||
3366                     zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3367                         return (SET_ERROR(EINVAL));
3368 
3369                 /*
3370                  * The snap must be in the specified pool.
3371                  */
3372                 if (strncmp(name, poolname, poollen) != 0 ||
3373                     (name[poollen] != '/' && name[poollen] != '@'))
3374                         return (SET_ERROR(EXDEV));
3375 
3376                 /* This must be the only snap of this fs. */
3377                 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3378                     pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3379                         if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3380                             == 0) {
3381                                 return (SET_ERROR(EXDEV));
3382                         }
3383                 }
3384         }
3385 
3386         error = dsl_dataset_snapshot(snaps, props, outnvl);
3387         return (error);
3388 }
3389 
3390 /*
3391  * innvl: "message" -> string
3392  */
3393 /* ARGSUSED */
3394 static int
3395 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3396 {
3397         char *message;
3398         spa_t *spa;
3399         int error;
3400         char *poolname;
3401 
3402         /*
3403          * The poolname in the ioctl is not set, we get it from the TSD,
3404          * which was set at the end of the last successful ioctl that allows
3405          * logging.  The secpolicy func already checked that it is set.
3406          * Only one log ioctl is allowed after each successful ioctl, so
 
3484 zfs_destroy_unmount_origin(const char *fsname)
3485 {
3486         int error;
3487         objset_t *os;
3488         dsl_dataset_t *ds;
3489 
3490         error = dmu_objset_hold(fsname, FTAG, &os);
3491         if (error != 0)
3492                 return;
3493         ds = dmu_objset_ds(os);
3494         if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3495                 char originname[ZFS_MAX_DATASET_NAME_LEN];
3496                 dsl_dataset_name(ds->ds_prev, originname);
3497                 dmu_objset_rele(os, FTAG);
3498                 zfs_unmount_snap(originname);
3499         } else {
3500                 dmu_objset_rele(os, FTAG);
3501         }
3502 }
3503 
3504 /*
3505  * innvl: {
3506  *     "snaps" -> { snapshot1, snapshot2 }
3507  *     (optional boolean) "defer"
3508  * }
3509  *
3510  * outnvl: snapshot -> error code (int32)
3511  *
3512  */
3513 /* ARGSUSED */
3514 static int
3515 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3516 {
3517         nvlist_t *snaps;
3518         nvpair_t *pair;
3519         boolean_t defer;
3520 
3521         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3522                 return (SET_ERROR(EINVAL));
3523         defer = nvlist_exists(innvl, "defer");
3524 
3525         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3526             pair = nvlist_next_nvpair(snaps, pair)) {
3527                 zfs_unmount_snap(nvpair_name(pair));
3528         }
3529 
3530         return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl));
3531 }
3532 
3533 /*
3534  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
3535  * All bookmarks must be in the same pool.
3536  *
3537  * innvl: {
3538  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
3539  * }
3540  *
3541  * outnvl: bookmark -> error code (int32)
3542  *
3543  */
3544 /* ARGSUSED */
3545 static int
3546 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3547 {
3548         for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
3549             pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
3550                 char *snap_name;
 
3646                 memlimit = ZCP_DEFAULT_MEMLIMIT;
3647         }
3648         if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
3649                 return (EINVAL);
3650         }
3651 
3652         if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
3653                 return (EINVAL);
3654         if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
3655                 return (EINVAL);
3656 
3657         return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
3658             nvarg, outnvl));
3659 }
3660 
3661 /*
3662  * inputs:
3663  * zc_name              name of dataset to destroy
3664  * zc_objset_type       type of objset
3665  * zc_defer_destroy     mark for deferred destroy
3666  *
3667  * outputs:             none
3668  */
3669 static int
3670 zfs_ioc_destroy(zfs_cmd_t *zc)
3671 {
3672         int err;
3673 
3674         if (zc->zc_objset_type == DMU_OST_ZFS)
3675                 zfs_unmount_snap(zc->zc_name);
3676 
3677         if (strchr(zc->zc_name, '@'))
3678                 err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy);
3679         else
3680                 err = dsl_destroy_head(zc->zc_name);
3681         if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
3682                 (void) zvol_remove_minor(zc->zc_name);
3683         return (err);
3684 }
3685 
3686 /*
3687  * fsname is name of dataset to rollback (to most recent snapshot)
3688  *
3689  * innvl may contain name of expected target snapshot
3690  *
3691  * outnvl: "target" -> name of most recent snapshot
3692  * }
3693  */
3694 /* ARGSUSED */
3695 static int
3696 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3697 {
3698         zfsvfs_t *zfsvfs;
3699         char *target = NULL;
3700         int error;
3701 
3702         (void) nvlist_lookup_string(innvl, "target", &target);
3703         if (target != NULL) {
3704                 const char *cp = strchr(target, '@');
3705 
3706                 /*
3707                  * The snap name must contain an @, and the part after it must
3708                  * contain only valid characters.
3709                  */
3710                 if (cp == NULL ||
3711                     zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3712                         return (SET_ERROR(EINVAL));
3713         }
3714 
3715         if (getzfsvfs(fsname, &zfsvfs) == 0) {
3716                 dsl_dataset_t *ds;
3717 
3718                 ds = dmu_objset_ds(zfsvfs->z_os);
3719                 error = zfs_suspend_fs(zfsvfs);
3720                 if (error == 0) {
3721                         int resume_err;
3722 
3723                         error = dsl_dataset_rollback(fsname, target, zfsvfs,
3724                             outnvl);
3725                         resume_err = zfs_resume_fs(zfsvfs, ds);
3726                         error = error ? error : resume_err;
3727                 }
3728                 VFS_RELE(zfsvfs->z_vfs);
3729         } else {
3730                 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
3731         }
3732         return (error);
3733 }
3734 
3735 static int
3736 recursive_unmount(const char *fsname, void *arg)
3737 {
3738         const char *snapname = arg;
3739         char fullname[ZFS_MAX_DATASET_NAME_LEN];
3740 
3741         (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
3742         zfs_unmount_snap(fullname);
3743 
3744         return (0);
3745 }
3746 
3747 /*
3748  * inputs:
3749  * zc_name      old name of dataset
3750  * zc_value     new name of dataset
3751  * zc_cookie    recursive flag (only valid for snapshots)
3752  *
3753  * outputs:     none
3754  */
3755 static int
3756 zfs_ioc_rename(zfs_cmd_t *zc)
3757 {
3758         boolean_t recursive = zc->zc_cookie & 1;
3759         char *at;
3760 
3761         /* "zfs rename" from and to ...%recv datasets should both fail */
3762         zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
3763         zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
3764         if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
3765             dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
3766             strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%'))
3767                 return (SET_ERROR(EINVAL));
3768 
3769         at = strchr(zc->zc_name, '@');
3770         if (at != NULL) {
3771                 /* snaps must be in same fs */
3772                 int error;
3773 
3774                 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
3775                         return (SET_ERROR(EXDEV));
3776                 *at = '\0';
3777                 if (zc->zc_objset_type == DMU_OST_ZFS) {
3778                         error = dmu_objset_find(zc->zc_name,
3779                             recursive_unmount, at + 1,
3780                             recursive ? DS_FIND_CHILDREN : 0);
3781                         if (error != 0) {
3782                                 *at = '@';
3783                                 return (error);
3784                         }
3785                 }
3786                 error = dsl_dataset_rename_snapshot(zc->zc_name,
3787                     at + 1, strchr(zc->zc_value, '@') + 1, recursive);
3788                 *at = '@';
3789 
3790                 return (error);
3791         } else {
3792                 if (zc->zc_objset_type == DMU_OST_ZVOL)
3793                         (void) zvol_remove_minor(zc->zc_name);
3794                 return (dsl_dir_rename(zc->zc_name, zc->zc_value));
3795         }
3796 }
3797 
3798 static int
3799 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
3800 {
3801         const char *propname = nvpair_name(pair);
3802         boolean_t issnap = (strchr(dsname, '@') != NULL);
3803         zfs_prop_t prop = zfs_name_to_prop(propname);
3804         uint64_t intval;
3805         int err;
3806 
3807         if (prop == ZPROP_INVAL) {
3808                 if (zfs_prop_user(propname)) {
3809                         if (err = zfs_secpolicy_write_perms(dsname,
3810                             ZFS_DELEG_PERM_USERPROP, cr))
3811                                 return (err);
3812                         return (0);
3813                 }
3814 
3815                 if (!issnap && zfs_prop_userquota(propname)) {
 
3932                         }
3933                         spa_close(spa, FTAG);
3934                 }
3935                 break;
3936 
3937         case ZFS_PROP_SHARESMB:
3938                 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
3939                         return (SET_ERROR(ENOTSUP));
3940                 break;
3941 
3942         case ZFS_PROP_ACLINHERIT:
3943                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
3944                     nvpair_value_uint64(pair, &intval) == 0) {
3945                         if (intval == ZFS_ACL_PASSTHROUGH_X &&
3946                             zfs_earlier_version(dsname,
3947                             SPA_VERSION_PASSTHROUGH_X))
3948                                 return (SET_ERROR(ENOTSUP));
3949                 }
3950                 break;
3951 
3952         case ZFS_PROP_CHECKSUM:
3953         case ZFS_PROP_DEDUP:
3954         {
3955                 spa_feature_t feature;
3956                 spa_t *spa;
3957 
3958                 /* dedup feature version checks */
3959                 if (prop == ZFS_PROP_DEDUP &&
3960                     zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
3961                         return (SET_ERROR(ENOTSUP));
3962 
3963                 if (nvpair_value_uint64(pair, &intval) != 0)
3964                         return (SET_ERROR(EINVAL));
3965 
3966                 /* check prop value is enabled in features */
3967                 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
3968                 if (feature == SPA_FEATURE_NONE)
3969                         break;
3970 
3971                 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
 
4207                 }
4208                 if (delayable[i] != 0) {
4209                         tmp = nvlist_prev_nvpair(props, nvp);
4210                         VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4211                         VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4212                         nvp = tmp;
4213                 }
4214         }
4215 
4216         if (nvlist_empty(delayprops)) {
4217                 nvlist_free(delayprops);
4218                 delayprops = NULL;
4219         }
4220         return (delayprops);
4221 }
4222 
4223 #ifdef  DEBUG
4224 static boolean_t zfs_ioc_recv_inject_err;
4225 #endif
4226 
4227 /*
4228  * inputs:
4229  * zc_name              name of containing filesystem
4230  * zc_nvlist_src{_size} nvlist of properties to apply
4231  * zc_value             name of snapshot to create
4232  * zc_string            name of clone origin (if DRR_FLAG_CLONE)
4233  * zc_cookie            file descriptor to recv from
4234  * zc_begin_record      the BEGIN record of the stream (not byteswapped)
4235  * zc_guid              force flag
4236  * zc_cleanup_fd        cleanup-on-exit file descriptor
4237  * zc_action_handle     handle for this guid/ds mapping (or zero on first call)
4238  * zc_resumable         if data is incomplete assume sender will resume
4239  *
4240  * outputs:
4241  * zc_cookie            number of bytes read
4242  * zc_nvlist_dst{_size} error for each unapplied received property
4243  * zc_obj               zprop_errflags_t
4244  * zc_action_handle     handle for this guid/ds mapping
4245  */
4246 static int
4247 zfs_ioc_recv(zfs_cmd_t *zc)
4248 {
4249         file_t *fp;
4250         dmu_recv_cookie_t drc;
4251         boolean_t force = (boolean_t)zc->zc_guid;
4252         int fd;
4253         int error = 0;
4254         int props_error = 0;
4255         nvlist_t *errors;
4256         offset_t off;
4257         nvlist_t *props = NULL; /* sent properties */
4258         nvlist_t *origprops = NULL; /* existing properties */
4259         nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4260         char *origin = NULL;
4261         char *tosnap;
4262         char tofs[ZFS_MAX_DATASET_NAME_LEN];
4263         boolean_t first_recvd_props = B_FALSE;
4264 
4265         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4266             strchr(zc->zc_value, '@') == NULL ||
4267             strchr(zc->zc_value, '%'))
4268                 return (SET_ERROR(EINVAL));
4269 
4270         (void) strcpy(tofs, zc->zc_value);
4271         tosnap = strchr(tofs, '@');
4272         *tosnap++ = '\0';
4273 
4274         if (zc->zc_nvlist_src != NULL &&
4275             (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
4276             zc->zc_iflags, &props)) != 0)
4277                 return (error);
4278 
4279         fd = zc->zc_cookie;
4280         fp = getf(fd);
4281         if (fp == NULL) {
4282                 nvlist_free(props);
4283                 return (SET_ERROR(EBADF));
4284         }
4285 
4286         errors = fnvlist_alloc();
4287 
4288         if (zc->zc_string[0])
4289                 origin = zc->zc_string;
4290 
4291         error = dmu_recv_begin(tofs, tosnap,
4292             &zc->zc_begin_record, force, zc->zc_resumable, origin, &drc);
4293         if (error != 0)
4294                 goto out;
4295 
4296         /*
4297          * Set properties before we receive the stream so that they are applied
4298          * to the new data. Note that we must call dmu_recv_stream() if
4299          * dmu_recv_begin() succeeds.
4300          */
4301         if (props != NULL && !drc.drc_newfs) {
4302                 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4303                     SPA_VERSION_RECVD_PROPS &&
4304                     !dsl_prop_get_hasrecvd(tofs))
4305                         first_recvd_props = B_TRUE;
4306 
4307                 /*
4308                  * If new received properties are supplied, they are to
4309                  * completely replace the existing received properties, so stash
4310                  * away the existing ones.
4311                  */
4312                 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4313                         nvlist_t *errlist = NULL;
4314                         /*
4315                          * Don't bother writing a property if its value won't
4316                          * change (and avoid the unnecessary security checks).
4317                          *
4318                          * The first receive after SPA_VERSION_RECVD_PROPS is a
4319                          * special case where we blow away all local properties
4320                          * regardless.
4321                          */
4322                         if (!first_recvd_props)
4323                                 props_reduce(props, origprops);
4324                         if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4325                                 (void) nvlist_merge(errors, errlist, 0);
4326                         nvlist_free(errlist);
4327 
4328                         if (clear_received_props(tofs, origprops,
4329                             first_recvd_props ? NULL : props) != 0)
4330                                 zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4331                 } else {
4332                         zc->zc_obj |= ZPROP_ERR_NOCLEAR;
4333                 }
4334         }
4335 
4336         if (props != NULL) {
4337                 props_error = dsl_prop_set_hasrecvd(tofs);
4338 
4339                 if (props_error == 0) {
4340                         delayprops = extract_delay_props(props);
4341                         (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4342                             props, errors);
4343                 }
4344         }
4345 
4346         off = fp->f_offset;
4347         error = dmu_recv_stream(&drc, fp->f_vnode, &off, zc->zc_cleanup_fd,
4348             &zc->zc_action_handle);
4349 
4350         if (error == 0) {
4351                 zfsvfs_t *zfsvfs = NULL;
4352 
4353                 if (getzfsvfs(tofs, &zfsvfs) == 0) {
4354                         /* online recv */
4355                         dsl_dataset_t *ds;
4356                         int end_err;
4357 
4358                         ds = dmu_objset_ds(zfsvfs->z_os);
4359                         error = zfs_suspend_fs(zfsvfs);
4360                         /*
4361                          * If the suspend fails, then the recv_end will
4362                          * likely also fail, and clean up after itself.
4363                          */
4364                         end_err = dmu_recv_end(&drc, zfsvfs);
4365                         if (error == 0)
4366                                 error = zfs_resume_fs(zfsvfs, ds);
4367                         error = error ? error : end_err;
4368                         VFS_RELE(zfsvfs->z_vfs);
4369                 } else {
4370                         error = dmu_recv_end(&drc, NULL);
4371                 }
4372 
4373                 /* Set delayed properties now, after we're done receiving. */
4374                 if (delayprops != NULL && error == 0) {
4375                         (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4376                             delayprops, errors);
4377                 }
4378         }
4379 
4380         if (delayprops != NULL) {
4381                 /*
4382                  * Merge delayed props back in with initial props, in case
4383                  * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4384                  * we have to make sure clear_received_props() includes
4385                  * the delayed properties).
4386                  *
4387                  * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4388                  * using ASSERT() will be just like a VERIFY.
4389                  */
4390                 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4391                 nvlist_free(delayprops);
4392         }
4393 
4394         /*
4395          * Now that all props, initial and delayed, are set, report the prop
4396          * errors to the caller.
4397          */
4398         if (zc->zc_nvlist_dst_size != 0 &&
4399             (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
4400             put_nvlist(zc, errors) != 0)) {
4401                 /*
4402                  * Caller made zc->zc_nvlist_dst less than the minimum expected
4403                  * size or supplied an invalid address.
4404                  */
4405                 props_error = SET_ERROR(EINVAL);
4406         }
4407 
4408         zc->zc_cookie = off - fp->f_offset;
4409         if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4410                 fp->f_offset = off;
4411 
4412 #ifdef  DEBUG
4413         if (zfs_ioc_recv_inject_err) {
4414                 zfs_ioc_recv_inject_err = B_FALSE;
4415                 error = 1;
4416         }
4417 #endif
4418         /*
4419          * On error, restore the original props.
4420          */
4421         if (error != 0 && props != NULL && !drc.drc_newfs) {
4422                 if (clear_received_props(tofs, props, NULL) != 0) {
4423                         /*
4424                          * We failed to clear the received properties.
4425                          * Since we may have left a $recvd value on the
4426                          * system, we can't clear the $hasrecvd flag.
4427                          */
4428                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
4429                 } else if (first_recvd_props) {
4430                         dsl_prop_unset_hasrecvd(tofs);
4431                 }
4432 
4433                 if (origprops == NULL && !drc.drc_newfs) {
4434                         /* We failed to stash the original properties. */
4435                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
4436                 }
4437 
4438                 /*
4439                  * dsl_props_set() will not convert RECEIVED to LOCAL on or
4440                  * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
4441                  * explictly if we're restoring local properties cleared in the
4442                  * first new-style receive.
4443                  */
4444                 if (origprops != NULL &&
4445                     zfs_set_prop_nvlist(tofs, (first_recvd_props ?
4446                     ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
4447                     origprops, NULL) != 0) {
4448                         /*
4449                          * We stashed the original properties but failed to
4450                          * restore them.
4451                          */
4452                         zc->zc_obj |= ZPROP_ERR_NORESTORE;
4453                 }
4454         }
4455 out:
4456         nvlist_free(props);
4457         nvlist_free(origprops);
4458         nvlist_free(errors);
4459         releasef(fd);
4460 
4461         if (error == 0)
4462                 error = props_error;
4463 
4464         return (error);
4465 }
4466 
4467 /*
4468  * inputs:
4469  * zc_name      name of snapshot to send
4470  * zc_cookie    file descriptor to send stream to
4471  * zc_obj       fromorigin flag (mutually exclusive with zc_fromobj)
4472  * zc_sendobj   objsetid of snapshot to send
4473  * zc_fromobj   objsetid of incremental fromsnap (may be zero)
4474  * zc_guid      if set, estimate size of stream only.  zc_cookie is ignored.
4475  *              output size in zc_objset_type.
4476  * zc_flags     lzc_send_flags
4477  *
4478  * outputs:
4479  * zc_objset_type       estimated size, if zc_guid is set
4480  */
4481 static int
4482 zfs_ioc_send(zfs_cmd_t *zc)
4483 {
4484         int error;
4485         offset_t off;
4486         boolean_t estimate = (zc->zc_guid != 0);
4487         boolean_t embedok = (zc->zc_flags & 0x1);
4488         boolean_t large_block_ok = (zc->zc_flags & 0x2);
 
4525                 }
4526 
4527                 if (zc->zc_fromobj != 0) {
4528                         error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
4529                             FTAG, &fromsnap);
4530                         if (error != 0) {
4531                                 dsl_dataset_rele(tosnap, FTAG);
4532                                 dsl_pool_rele(dp, FTAG);
4533                                 return (error);
4534                         }
4535                 }
4536 
4537                 error = dmu_send_estimate(tosnap, fromsnap, compressok,
4538                     &zc->zc_objset_type);
4539 
4540                 if (fromsnap != NULL)
4541                         dsl_dataset_rele(fromsnap, FTAG);
4542                 dsl_dataset_rele(tosnap, FTAG);
4543                 dsl_pool_rele(dp, FTAG);
4544         } else {
4545                 file_t *fp = getf(zc->zc_cookie);
4546                 if (fp == NULL)
4547                         return (SET_ERROR(EBADF));
4548 
4549                 off = fp->f_offset;
4550                 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
4551                     zc->zc_fromobj, embedok, large_block_ok, compressok,
4552                     zc->zc_cookie, fp->f_vnode, &off);
4553 
4554                 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4555                         fp->f_offset = off;
4556                 releasef(zc->zc_cookie);
4557         }
4558         return (error);
4559 }
4560 
4561 /*
4562  * inputs:
4563  * zc_name      name of snapshot on which to report progress
4564  * zc_cookie    file descriptor of send stream
4565  *
4566  * outputs:
4567  * zc_cookie    number of bytes written in send stream thus far
4568  */
4569 static int
4570 zfs_ioc_send_progress(zfs_cmd_t *zc)
4571 {
4572         dsl_pool_t *dp;
4573         dsl_dataset_t *ds;
 
4768 
4769         (void) spa_vdev_state_exit(spa, NULL, 0);
4770         spa_close(spa, FTAG);
4771         return (0);
4772 }
4773 /*
4774  * inputs:
4775  * zc_name      name of filesystem
4776  *
4777  * outputs:
4778  * zc_string    name of conflicting snapshot, if there is one
4779  */
4780 static int
4781 zfs_ioc_promote(zfs_cmd_t *zc)
4782 {
4783         dsl_pool_t *dp;
4784         dsl_dataset_t *ds, *ods;
4785         char origin[ZFS_MAX_DATASET_NAME_LEN];
4786         char *cp;
4787         int error;
4788 
4789         zc->zc_name[sizeof (zc->zc_name) - 1] = '\0';
4790         if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 ||
4791             strchr(zc->zc_name, '%'))
4792                 return (SET_ERROR(EINVAL));
4793 
4794         error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
4795         if (error != 0)
4796                 return (error);
4797 
4798         error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
4799         if (error != 0) {
4800                 dsl_pool_rele(dp, FTAG);
4801                 return (error);
4802         }
4803 
4804         if (!dsl_dir_is_clone(ds->ds_dir)) {
4805                 dsl_dataset_rele(ds, FTAG);
4806                 dsl_pool_rele(dp, FTAG);
4807                 return (SET_ERROR(EINVAL));
4808         }
4809 
4810         error = dsl_dataset_hold_obj(dp,
4811             dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
4812         if (error != 0) {
4813                 dsl_dataset_rele(ds, FTAG);
4814                 dsl_pool_rele(dp, FTAG);
4815                 return (error);
4816         }
4817 
4818         dsl_dataset_name(ods, origin);
4819         dsl_dataset_rele(ods, FTAG);
4820         dsl_dataset_rele(ds, FTAG);
4821         dsl_pool_rele(dp, FTAG);
4822 
4823         /*
4824          * We don't need to unmount *all* the origin fs's snapshots, but
4825          * it's easier.
4826          */
4827         cp = strchr(origin, '@');
4828         if (cp)
4829                 *cp = '\0';
4830         (void) dmu_objset_find(origin,
4831             zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
4832         return (dsl_dataset_promote(zc->zc_name, zc->zc_string));
4833 }
4834 
4835 /*
4836  * Retrieve a single {user|group}{used|quota}@... property.
4837  *
4838  * inputs:
4839  * zc_name      name of filesystem
4840  * zc_objset_type zfs_userquota_prop_t
4841  * zc_value     domain name (eg. "S-1-234-567-89")
4842  * zc_guid      RID/UID/GID
4843  *
4844  * outputs:
4845  * zc_cookie    property value
4846  */
4847 static int
4848 zfs_ioc_userspace_one(zfs_cmd_t *zc)
4849 {
4850         zfsvfs_t *zfsvfs;
4851         int error;
4852 
 
5493         if (error == 0 && !old->ds_is_snapshot) {
5494                 dsl_dataset_rele(old, FTAG);
5495                 error = SET_ERROR(EINVAL);
5496         }
5497         if (error != 0) {
5498                 dsl_dataset_rele(new, FTAG);
5499                 dsl_pool_rele(dp, FTAG);
5500                 return (error);
5501         }
5502 
5503         error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
5504         dsl_dataset_rele(old, FTAG);
5505         dsl_dataset_rele(new, FTAG);
5506         dsl_pool_rele(dp, FTAG);
5507         fnvlist_add_uint64(outnvl, "used", used);
5508         fnvlist_add_uint64(outnvl, "compressed", comp);
5509         fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
5510         return (error);
5511 }
5512 
5513 /*
5514  * innvl: {
5515  *     "fd" -> file descriptor to write stream to (int32)
5516  *     (optional) "fromsnap" -> full snap name to send an incremental from
5517  *     (optional) "largeblockok" -> (value ignored)
5518  *         indicates that blocks > 128KB are permitted
5519  *     (optional) "embedok" -> (value ignored)
5520  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
5521  *     (optional) "compressok" -> (value ignored)
5522  *         presence indicates compressed DRR_WRITE records are permitted
5523  *     (optional) "resume_object" and "resume_offset" -> (uint64)
5524  *         if present, resume send stream from specified object and offset.
5525  * }
5526  *
5527  * outnvl is unused
5528  */
5529 /* ARGSUSED */
5530 static int
5531 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
5532 {
 
5643                          * bookmark
5644                          */
5645                         error = SET_ERROR(EINVAL);
5646                         goto out;
5647                 }
5648         } else {
5649                 /*
5650                  * If estimating the size of a full send, use dmu_send_estimate.
5651                  */
5652                 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
5653         }
5654 
5655         fnvlist_add_uint64(outnvl, "space", space);
5656 
5657 out:
5658         dsl_dataset_rele(tosnap, FTAG);
5659         dsl_pool_rele(dp, FTAG);
5660         return (error);
5661 }
5662 
5663 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
5664 
5665 static void
5666 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5667     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
5668     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
5669 {
5670         zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
5671 
5672         ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
5673         ASSERT3U(ioc, <, ZFS_IOC_LAST);
5674         ASSERT3P(vec->zvec_legacy_func, ==, NULL);
5675         ASSERT3P(vec->zvec_func, ==, NULL);
5676 
5677         vec->zvec_legacy_func = func;
5678         vec->zvec_secpolicy = secpolicy;
5679         vec->zvec_namecheck = namecheck;
5680         vec->zvec_allow_log = log_history;
5681         vec->zvec_pool_check = pool_check;
5682 }
 
5748 {
5749         zfs_ioctl_register_legacy(ioc, func, secpolicy,
5750             DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
5751 }
5752 
5753 static void
5754 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
5755 {
5756         zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
5757             zfs_secpolicy_read);
5758 }
5759 
5760 static void
5761 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
5762     zfs_secpolicy_func_t *secpolicy)
5763 {
5764         zfs_ioctl_register_legacy(ioc, func, secpolicy,
5765             DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
5766 }
5767 
5768 static void
5769 zfs_ioctl_init(void)
5770 {
5771         zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
5772             zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
5773             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5774 
5775         zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
5776             zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
5777             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
5778 
5779         zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
5780             zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
5781             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5782 
5783         zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
5784             zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
5785             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5786 
5787         zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
5788             zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
5789             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5790 
5791         zfs_ioctl_register("create", ZFS_IOC_CREATE,
5792             zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
5793             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5794 
5795         zfs_ioctl_register("clone", ZFS_IOC_CLONE,
5796             zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
5797             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5798 
5799         zfs_ioctl_register("remap", ZFS_IOC_REMAP,
5800             zfs_ioc_remap, zfs_secpolicy_remap, DATASET_NAME,
5801             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5802 
5803         zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
5804             zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
5805             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5806 
5807         zfs_ioctl_register("hold", ZFS_IOC_HOLD,
5808             zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
5809             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5810         zfs_ioctl_register("release", ZFS_IOC_RELEASE,
5811             zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
5812             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5813 
5814         zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
5815             zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
5816             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5817 
5818         zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
5819             zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
5820             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
5821 
5822         zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
5823             zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
5824             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5825 
5826         zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
5827             zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
5828             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
5829 
5830         zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
5831             zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
5832             POOL_NAME,
5833             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
5834 
5835         zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
5836             zfs_ioc_channel_program, zfs_secpolicy_config,
5837             POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
5838             B_TRUE);
5839 
5840         /* IOCTLS that use the legacy function signature */
5841 
5842         zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
5843             zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
5844 
5845         zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
5846             zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5847         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
5848             zfs_ioc_pool_scan);
5849         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
5850             zfs_ioc_pool_upgrade);
5851         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
5852             zfs_ioc_vdev_add);
5853         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
5854             zfs_ioc_vdev_remove);
5855         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
5856             zfs_ioc_vdev_set_state);
5857         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
5858             zfs_ioc_vdev_attach);
5859         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
5860             zfs_ioc_vdev_detach);
5861         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
5862             zfs_ioc_vdev_setpath);
5863         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
5864             zfs_ioc_vdev_setfru);
5865         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
5866             zfs_ioc_pool_set_props);
5867         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
5868             zfs_ioc_vdev_split);
5869         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
5870             zfs_ioc_pool_reguid);
5871 
5872         zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
5873             zfs_ioc_pool_configs, zfs_secpolicy_none);
5874         zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
5875             zfs_ioc_pool_tryimport, zfs_secpolicy_config);
5876         zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
5877             zfs_ioc_inject_fault, zfs_secpolicy_inject);
5878         zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
5879             zfs_ioc_clear_fault, zfs_secpolicy_inject);
5880         zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
 
5891             zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
5892 
5893         zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
5894             zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5895         zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
5896             zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
5897 
5898         zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
5899             zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
5900         zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
5901             zfs_ioc_dsobj_to_dsname,
5902             zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
5903         zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
5904             zfs_ioc_pool_get_history,
5905             zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
5906 
5907         zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
5908             zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
5909 
5910         zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
5911             zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY);
5912         zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
5913             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
5914 
5915         zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
5916             zfs_ioc_space_written);
5917         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
5918             zfs_ioc_objset_recvd_props);
5919         zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
5920             zfs_ioc_next_obj);
5921         zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
5922             zfs_ioc_get_fsacl);
5923         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
5924             zfs_ioc_objset_stats);
5925         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
5926             zfs_ioc_objset_zplprops);
5927         zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
5928             zfs_ioc_dataset_list_next);
5929         zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
5930             zfs_ioc_snapshot_list_next);
5931         zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
5932             zfs_ioc_send_progress);
5933 
5934         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
 
 
6386         spa_init(FREAD | FWRITE);
6387         zfs_init();
6388         zvol_init();
6389         zfs_ioctl_init();
6390 
6391         if ((error = mod_install(&modlinkage)) != 0) {
6392                 zvol_fini();
6393                 zfs_fini();
6394                 spa_fini();
6395                 return (error);
6396         }
6397 
6398         tsd_create(&zfs_fsyncer_key, NULL);
6399         tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
6400         tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
6401 
6402         error = ldi_ident_from_mod(&modlinkage, &zfs_li);
6403         ASSERT(error == 0);
6404         mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
6405 
6406         return (0);
6407 }
6408 
6409 int
6410 _fini(void)
6411 {
6412         int error;
6413 
6414         if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
6415                 return (SET_ERROR(EBUSY));
6416 
6417         if ((error = mod_remove(&modlinkage)) != 0)
6418                 return (error);
6419 
6420         zvol_fini();
6421         zfs_fini();
6422         spa_fini();
6423         if (zfs_nfsshare_inited)
6424                 (void) ddi_modclose(nfs_mod);
6425         if (zfs_smbshare_inited)
6426                 (void) ddi_modclose(smbsrv_mod);
6427         if (zfs_nfsshare_inited || zfs_smbshare_inited)
6428                 (void) ddi_modclose(sharefs_mod);
6429 
6430         tsd_destroy(&zfs_fsyncer_key);
6431         ldi_ident_release(zfs_li);
6432         zfs_li = NULL;
6433         mutex_destroy(&zfs_share_lock);
6434 
6435         return (error);
6436 }
6437 
6438 int
6439 _info(struct modinfo *modinfop)
6440 {
6441         return (mod_info(&modlinkage, modinfop));
6442 }
 | 
 
 
   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) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
  28  * Portions Copyright 2011 Martin Matuska
  29  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
  30  * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved.
  31  * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
  32  * Copyright (c) 2013 by Saso Kiselkov. All rights reserved.
  33  * Copyright (c) 2013 Steven Hartland. All rights reserved.
  34  * Copyright (c) 2014 Integros [integros.com]
  35  * Copyright 2018 Nexenta Systems, Inc.
  36  * Copyright 2016 Toomas Soome <tsoome@me.com>
  37  * Copyright 2017 RackTop Systems.
  38  * Copyright (c) 2017 Datto Inc.
  39  */
  40 
  41 /*
  42  * ZFS ioctls.
  43  *
  44  * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage
  45  * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool.
  46  *
  47  * There are two ways that we handle ioctls: the legacy way where almost
  48  * all of the logic is in the ioctl callback, and the new way where most
  49  * of the marshalling is handled in the common entry point, zfsdev_ioctl().
  50  *
  51  * Non-legacy ioctls should be registered by calling
  52  * zfs_ioctl_register() from zfs_ioctl_init().  The ioctl is invoked
  53  * from userland by lzc_ioctl().
  54  *
  55  * The registration arguments are as follows:
 
 
 142 #include <sys/types.h>
 143 #include <sys/param.h>
 144 #include <sys/errno.h>
 145 #include <sys/uio.h>
 146 #include <sys/buf.h>
 147 #include <sys/modctl.h>
 148 #include <sys/open.h>
 149 #include <sys/file.h>
 150 #include <sys/kmem.h>
 151 #include <sys/conf.h>
 152 #include <sys/cmn_err.h>
 153 #include <sys/stat.h>
 154 #include <sys/zfs_ioctl.h>
 155 #include <sys/zfs_vfsops.h>
 156 #include <sys/zfs_znode.h>
 157 #include <sys/zap.h>
 158 #include <sys/spa.h>
 159 #include <sys/spa_impl.h>
 160 #include <sys/vdev.h>
 161 #include <sys/priv_impl.h>
 162 #include <sys/autosnap.h>
 163 #include <sys/dmu.h>
 164 #include <sys/dsl_dir.h>
 165 #include <sys/dsl_dataset.h>
 166 #include <sys/dsl_prop.h>
 167 #include <sys/dsl_deleg.h>
 168 #include <sys/dsl_synctask.h>
 169 #include <sys/dmu_objset.h>
 170 #include <sys/dmu_impl.h>
 171 #include <sys/dmu_tx.h>
 172 #include <sys/ddi.h>
 173 #include <sys/sunddi.h>
 174 #include <sys/sunldi.h>
 175 #include <sys/policy.h>
 176 #include <sys/zone.h>
 177 #include <sys/nvpair.h>
 178 #include <sys/pathname.h>
 179 #include <sys/mount.h>
 180 #include <sys/sdt.h>
 181 #include <sys/fs/zfs.h>
 182 #include <sys/zfs_ctldir.h>
 183 #include <sys/zfs_dir.h>
 184 #include <sys/zfs_onexit.h>
 185 #include <sys/zvol.h>
 186 #include <sys/dsl_scan.h>
 187 #include <sharefs/share.h>
 188 #include <sys/dmu_objset.h>
 189 #include <sys/dmu_send.h>
 190 #include <sys/dsl_destroy.h>
 191 #include <sys/dsl_bookmark.h>
 192 #include <sys/dsl_userhold.h>
 193 #include <sys/zfeature.h>
 194 #include <sys/cos.h>
 195 #include <sys/cos_impl.h>
 196 #include <sys/zfeature.h>
 197 #include <sys/sysevent.h>
 198 #include <sys/sysevent_impl.h>
 199 #include <sys/zcp.h>
 200 #include <sys/zio_checksum.h>
 201 
 202 #include "zfs_namecheck.h"
 203 #include "zfs_prop.h"
 204 #include "zfs_deleg.h"
 205 #include "zfs_comutil.h"
 206 #include "zfs_errno.h"
 207 
 208 #include "lua.h"
 209 #include "lauxlib.h"
 210 
 211 extern struct modlfs zfs_modlfs;
 212 
 213 extern void zfs_init(void);
 214 extern void zfs_fini(void);
 215 
 216 ldi_ident_t zfs_li = NULL;
 217 dev_info_t *zfs_dip;
 218 
 219 uint_t zfs_fsyncer_key;
 220 extern uint_t rrw_tsd_key;
 221 static uint_t zfs_allow_log_key;
 222 
 223 typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *);
 224 typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *);
 225 typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *);
 226 
 
 
 250 /* This array is indexed by zfs_userquota_prop_t */
 251 static const char *userquota_perms[] = {
 252         ZFS_DELEG_PERM_USERUSED,
 253         ZFS_DELEG_PERM_USERQUOTA,
 254         ZFS_DELEG_PERM_GROUPUSED,
 255         ZFS_DELEG_PERM_GROUPQUOTA,
 256 };
 257 
 258 static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc);
 259 static int zfs_check_settable(const char *name, nvpair_t *property,
 260     cred_t *cr);
 261 static int zfs_check_clearable(char *dataset, nvlist_t *props,
 262     nvlist_t **errors);
 263 static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *,
 264     boolean_t *);
 265 int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *);
 266 static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp);
 267 
 268 static int zfs_prop_activate_feature(spa_t *spa, spa_feature_t feature);
 269 
 270 static int
 271 zfs_is_wormed_ds(dsl_dataset_t *ds)
 272 {
 273         char worminfo[13] = {0};
 274 
 275         if (dsl_prop_get_ds(ds, "nms:worm", 1, 12, &worminfo, NULL) == 0 &&
 276             worminfo[0] && strcmp(worminfo, "0") != 0 &&
 277             strcmp(worminfo, "off") != 0 && strcmp(worminfo, "-") != 0) {
 278                 return (1);
 279         }
 280         return (0);
 281 }
 282 
 283 static int
 284 zfs_is_wormed(const char *name)
 285 {
 286         char worminfo[13] = {0};
 287         char cname[MAXNAMELEN];
 288         char *end;
 289 
 290         (void) strlcpy(cname, name, MAXNAMELEN);
 291         end = strchr(cname, '@');
 292         if (end)
 293                 *end = 0;
 294 
 295         if (dsl_prop_get(cname, "nms:worm", 1, 12, &worminfo, NULL) == 0 &&
 296             worminfo[0] && strcmp(worminfo, "0") != 0 &&
 297             strcmp(worminfo, "off") != 0 && strcmp(worminfo, "-") != 0) {
 298                 return (1);
 299         }
 300         return (0);
 301 }
 302 
 303 /* _NOTE(PRINTFLIKE(4)) - this is printf-like, but lint is too whiney */
 304 void
 305 __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
 306 {
 307         const char *newfile;
 308         char buf[512];
 309         va_list adx;
 310 
 311         /*
 312          * Get rid of annoying "../common/" prefix to filename.
 313          */
 314         newfile = strrchr(file, '/');
 315         if (newfile != NULL) {
 316                 newfile = newfile + 1; /* Get rid of leading / */
 317         } else {
 318                 newfile = file;
 319         }
 320 
 321         va_start(adx, fmt);
 322         (void) vsnprintf(buf, sizeof (buf), fmt, adx);
 
 798             NO_FOLLOW, NULL, &vp)) != 0)
 799                 return (error);
 800 
 801         /* Now make sure mntpnt and dataset are ZFS */
 802 
 803         if (vp->v_vfsp->vfs_fstype != zfsfstype ||
 804             (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource),
 805             zc->zc_name) != 0)) {
 806                 VN_RELE(vp);
 807                 return (SET_ERROR(EPERM));
 808         }
 809 
 810         VN_RELE(vp);
 811         return (dsl_deleg_access(zc->zc_name,
 812             ZFS_DELEG_PERM_SHARE, cr));
 813 }
 814 
 815 int
 816 zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 817 {
 818         if (secpolicy_nfs(cr) == 0) {
 819                 return (0);
 820         } else {
 821                 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 822         }
 823 }
 824 
 825 int
 826 zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
 827 {
 828         if (secpolicy_smb(cr) == 0) {
 829                 return (0);
 830         } else {
 831                 return (zfs_secpolicy_deleg_share(zc, innvl, cr));
 832         }
 833 }
 834 
 835 static int
 836 zfs_get_parent(const char *datasetname, char *parent, int parentsize)
 837 {
 838         char *cp;
 839 
 840         /*
 841          * Remove the @bla or /bla from the end of the name to get the parent.
 842          */
 843         (void) strncpy(parent, datasetname, parentsize);
 844         cp = strrchr(parent, '@');
 845         if (cp != NULL) {
 846                 cp[0] = '\0';
 847         } else {
 
1063             pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
1064                 char *name = nvpair_name(pair);
1065                 char *hashp = strchr(name, '#');
1066 
1067                 if (hashp == NULL) {
1068                         error = SET_ERROR(EINVAL);
1069                         break;
1070                 }
1071                 *hashp = '\0';
1072                 error = zfs_secpolicy_write_perms(name,
1073                     ZFS_DELEG_PERM_BOOKMARK, cr);
1074                 *hashp = '#';
1075                 if (error != 0)
1076                         break;
1077         }
1078         return (error);
1079 }
1080 
1081 /* ARGSUSED */
1082 static int
1083 zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr)
1084 {
1085         nvpair_t *pair, *nextpair;
1086         int error = 0;
1087 
1088         for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL;
1089             pair = nextpair) {
1090                 char *name = nvpair_name(pair);
1091                 char *hashp = strchr(name, '#');
1092                 nextpair = nvlist_next_nvpair(innvl, pair);
1093 
1094                 if (hashp == NULL) {
1095                         error = SET_ERROR(EINVAL);
1096                         break;
1097                 }
1098 
1099                 *hashp = '\0';
1100                 error = zfs_secpolicy_write_perms(name,
1101                     ZFS_DELEG_PERM_DESTROY, cr);
1102                 *hashp = '#';
 
1395 
1396                 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0);
1397                 more_errors = nvlist_prev_nvpair(errors, NULL);
1398 
1399                 do {
1400                         nvpair_t *pair = nvlist_prev_nvpair(errors,
1401                             more_errors);
1402                         fnvlist_remove_nvpair(errors, pair);
1403                         n++;
1404                         size = fnvlist_size(errors);
1405                 } while (size > max);
1406 
1407                 fnvlist_remove_nvpair(errors, more_errors);
1408                 fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n);
1409                 ASSERT3U(fnvlist_size(errors), <=, max);
1410         }
1411 
1412         return (0);
1413 }
1414 
1415 /*
1416  * Callers will know whether there's anything to unpack based on ret non-0/errno
1417  * set to ENOMEM, but observers (e.g truss) need the message properly marked to
1418  * know if it should be unpacked and displayed. Don't marked as filled unless
1419  * completely successful. If there's a non-empty nvlist, set size to its nvl
1420  * size as resize hint.
1421  */
1422 static int
1423 put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl)
1424 {
1425         char *packed = NULL;
1426         int error = 0;
1427         size_t size;
1428 
1429         size = fnvlist_size(nvl);
1430 
1431         zc->zc_nvlist_dst_filled = B_FALSE;
1432         if (size > zc->zc_nvlist_dst_size) {
1433                 error = SET_ERROR(ENOMEM);
1434         } else {
1435                 packed = fnvlist_pack(nvl, &size);
1436                 if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst,
1437                     size, zc->zc_iflags) != 0)
1438                         error = SET_ERROR(EFAULT);
1439                 else
1440                         zc->zc_nvlist_dst_filled = B_TRUE;
1441                 fnvlist_pack_free(packed, size);
1442         }
1443 
1444         zc->zc_nvlist_dst_size = size;
1445         return (error);
1446 }
1447 
1448 static int
1449 getzfsvfs_from_ds(dsl_dataset_t *ds, zfsvfs_t **zfvp)
1450 {
1451         objset_t *os;
1452         int error;
1453         dsl_pool_t *dp;
1454 
1455         dp = ds->ds_dir->dd_pool;
1456         dsl_pool_config_enter(dp, FTAG);
1457 
1458         /*
1459          * IU:  we probably need to hold dataset here.
1460          *      For now let's assume we do.
1461          *      May need revision later.
1462          */
1463         dsl_dataset_long_hold(ds, FTAG);
1464         error = dmu_objset_from_ds(ds, &os);
1465         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1466                 dsl_dataset_long_rele(ds, FTAG);
1467                 dsl_pool_config_exit(dp, FTAG);
1468                 return (EINVAL);
1469         }
1470 
1471         mutex_enter(&os->os_user_ptr_lock);
1472         *zfvp = dmu_objset_get_user(os);
1473         if (*zfvp) {
1474                 VFS_HOLD((*zfvp)->z_vfs);
1475         } else {
1476                 error = ESRCH;
1477         }
1478         mutex_exit(&os->os_user_ptr_lock);
1479         dsl_dataset_long_rele(ds, FTAG);
1480         dsl_pool_config_exit(dp, FTAG);
1481         return (error);
1482 }
1483 
1484 int
1485 getzfsvfs_impl(objset_t *os, zfsvfs_t **zfvp)
1486 {
1487         int error = 0;
1488         if (dmu_objset_type(os) != DMU_OST_ZFS) {
1489                 return (SET_ERROR(EINVAL));
1490         }
1491 
1492         mutex_enter(&os->os_user_ptr_lock);
1493         *zfvp = dmu_objset_get_user(os);
1494         if (*zfvp) {
1495                 VFS_HOLD((*zfvp)->z_vfs);
1496         } else {
1497                 error = SET_ERROR(ESRCH);
1498         }
1499         mutex_exit(&os->os_user_ptr_lock);
1500         return (error);
1501 }
1502 
1503 int
 
1540                         rrm_exit(&(*zfvp)->z_teardown_lock, tag);
1541                         return (SET_ERROR(EBUSY));
1542                 }
1543         }
1544         return (error);
1545 }
1546 
1547 static void
1548 zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag)
1549 {
1550         rrm_exit(&zfsvfs->z_teardown_lock, tag);
1551 
1552         if (zfsvfs->z_vfs) {
1553                 VFS_RELE(zfsvfs->z_vfs);
1554         } else {
1555                 dmu_objset_disown(zfsvfs->z_os, zfsvfs);
1556                 zfsvfs_free(zfsvfs);
1557         }
1558 }
1559 
1560 
1561 /*
1562  * Publish events using GPEC subsystem
1563  */
1564 
1565 static evchan_t *zfs_channel = NULL;
1566 
1567 void
1568 zfs_event_post(const char *subclass, const char *operation, nvlist_t *ev_data)
1569 {
1570 
1571         if (zfs_channel == NULL)
1572                 goto out;
1573 
1574         fnvlist_add_string(ev_data, "operation", operation);
1575 
1576         (void) sysevent_evc_publish(zfs_channel, subclass, operation,
1577             "com.nexenta", "zfs-kernel", ev_data, EVCH_NOSLEEP);
1578 
1579 out:
1580         fnvlist_free(ev_data);
1581 }
1582 
1583 static int
1584 zfs_ioc_pool_create(zfs_cmd_t *zc)
1585 {
1586         int error;
1587         nvlist_t *config, *props = NULL;
1588         nvlist_t *rootprops = NULL;
1589         nvlist_t *zplprops = NULL;
1590         nvlist_t *event;
1591 
1592         if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1593             zc->zc_iflags, &config))
1594                 return (error);
1595 
1596         if (zc->zc_nvlist_src_size != 0 && (error =
1597             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1598             zc->zc_iflags, &props))) {
1599                 nvlist_free(config);
1600                 return (error);
1601         }
1602 
1603         if (props) {
1604                 nvlist_t *nvl = NULL;
1605                 uint64_t version = SPA_VERSION;
1606 
1607                 (void) nvlist_lookup_uint64(props,
1608                     zpool_prop_to_name(ZPOOL_PROP_VERSION), &version);
1609                 if (!SPA_VERSION_IS_SUPPORTED(version)) {
1610                         error = SET_ERROR(EINVAL);
 
1619                                 return (error);
1620                         }
1621                         (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS);
1622                 }
1623                 VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1624                 error = zfs_fill_zplprops_root(version, rootprops,
1625                     zplprops, NULL);
1626                 if (error != 0)
1627                         goto pool_props_bad;
1628         }
1629 
1630         error = spa_create(zc->zc_name, config, props, zplprops);
1631 
1632         /*
1633          * Set the remaining root properties
1634          */
1635         if (!error && (error = zfs_set_prop_nvlist(zc->zc_name,
1636             ZPROP_SRC_LOCAL, rootprops, NULL)) != 0)
1637                 (void) spa_destroy(zc->zc_name);
1638 
1639         if (error == 0) {
1640                 event = fnvlist_alloc();
1641                 fnvlist_add_string(event, "name", zc->zc_name);
1642                 fnvlist_add_nvlist(event, "config", config);
1643                 if (props != NULL)
1644                         fnvlist_add_nvlist(event, "props", props);
1645                 zfs_event_post(ZPOOL_EC_STATUS, "create", event);
1646         }
1647 
1648 pool_props_bad:
1649         nvlist_free(rootprops);
1650         nvlist_free(zplprops);
1651         nvlist_free(config);
1652         nvlist_free(props);
1653 
1654         return (error);
1655 }
1656 
1657 static int
1658 zfs_ioc_pool_destroy(zfs_cmd_t *zc)
1659 {
1660         int error;
1661         nvlist_t *event;
1662         zfs_log_history(zc);
1663         error = spa_destroy(zc->zc_name);
1664         if (error == 0) {
1665                 zvol_remove_minors(zc->zc_name);
1666                 event = fnvlist_alloc();
1667                 fnvlist_add_string(event, "pool", zc->zc_name);
1668                 zfs_event_post(ZPOOL_EC_STATUS, "destroy", event);
1669         }
1670         return (error);
1671 }
1672 
1673 static int
1674 zfs_ioc_pool_import(zfs_cmd_t *zc)
1675 {
1676         nvlist_t *config, *props = NULL;
1677         uint64_t guid;
1678         int error;
1679         nvlist_t *event;
1680 
1681         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
1682             zc->zc_iflags, &config)) != 0)
1683                 return (error);
1684 
1685         if (zc->zc_nvlist_src_size != 0 && (error =
1686             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
1687             zc->zc_iflags, &props))) {
1688                 nvlist_free(config);
1689                 return (error);
1690         }
1691 
1692         if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 ||
1693             guid != zc->zc_guid)
1694                 error = SET_ERROR(EINVAL);
1695         else
1696                 error = spa_import(zc->zc_name, config, props, zc->zc_cookie);
1697 
1698         if (error == 0) {
1699                 event = fnvlist_alloc();
1700                 fnvlist_add_string(event, "pool", zc->zc_name);
1701                 fnvlist_add_uint64(event, "guid", zc->zc_guid);
1702                 fnvlist_add_nvlist(event, "config", config);
1703                 if (props != NULL)
1704                         fnvlist_add_nvlist(event, "props", props);
1705                 zfs_event_post(ZPOOL_EC_STATUS, "import", event);
1706         }
1707 
1708         if (zc->zc_nvlist_dst != 0) {
1709                 int err;
1710 
1711                 if ((err = put_nvlist(zc, config)) != 0)
1712                         error = err;
1713         }
1714 
1715         nvlist_free(config);
1716 
1717         nvlist_free(props);
1718 
1719         return (error);
1720 }
1721 
1722 static int
1723 zfs_ioc_pool_export(zfs_cmd_t *zc)
1724 {
1725         int error;
1726         boolean_t force = (boolean_t)zc->zc_cookie;
1727         boolean_t hardforce = (boolean_t)zc->zc_guid;
1728         boolean_t saveconfig = (boolean_t)zc->zc_obj;
1729         nvlist_t *event;
1730 
1731         zfs_log_history(zc);
1732         error = spa_export(zc->zc_name, NULL, force, hardforce, saveconfig);
1733         if (error == 0) {
1734                 zvol_remove_minors(zc->zc_name);
1735                 event = fnvlist_alloc();
1736                 fnvlist_add_string(event, "pool", zc->zc_name);
1737                 zfs_event_post(ZPOOL_EC_STATUS, "export", event);
1738         }
1739         return (error);
1740 }
1741 
1742 static int
1743 zfs_ioc_pool_configs(zfs_cmd_t *zc)
1744 {
1745         nvlist_t *configs;
1746         int error;
1747 
1748         if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL)
1749                 return (SET_ERROR(EEXIST));
1750 
1751         error = put_nvlist(zc, configs);
1752 
1753         nvlist_free(configs);
1754 
1755         return (error);
1756 }
1757 
1758 /*
 
1831         int error;
1832 
1833         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1834                 return (error);
1835 
1836         if (zc->zc_flags >= POOL_SCRUB_FLAGS_END)
1837                 return (SET_ERROR(EINVAL));
1838 
1839         if (zc->zc_flags == POOL_SCRUB_PAUSE)
1840                 error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE);
1841         else if (zc->zc_cookie == POOL_SCAN_NONE)
1842                 error = spa_scan_stop(spa);
1843         else
1844                 error = spa_scan(spa, zc->zc_cookie);
1845 
1846         spa_close(spa, FTAG);
1847 
1848         return (error);
1849 }
1850 
1851 /*
1852  * inputs:
1853  * zc_name              name of the pool
1854  * zc_cookie            trim_cmd_info_t
1855  */
1856 static int
1857 zfs_ioc_pool_trim(zfs_cmd_t *zc)
1858 {
1859         spa_t *spa;
1860         int error;
1861         trim_cmd_info_t tci;
1862 
1863         if (ddi_copyin((void *)(uintptr_t)zc->zc_cookie, &tci,
1864             sizeof (tci), 0) == -1)
1865                 return (EFAULT);
1866 
1867         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
1868                 return (error);
1869 
1870         if (tci.tci_start) {
1871                 spa_man_trim(spa, tci.tci_rate);
1872         } else {
1873                 spa_man_trim_stop(spa);
1874         }
1875 
1876         spa_close(spa, FTAG);
1877 
1878         return (error);
1879 }
1880 
1881 static int
1882 zfs_ioc_pool_freeze(zfs_cmd_t *zc)
1883 {
1884         spa_t *spa;
1885         int error;
1886 
1887         error = spa_open(zc->zc_name, &spa, FTAG);
1888         if (error == 0) {
1889                 spa_freeze(spa);
1890                 spa_close(spa, FTAG);
1891         }
1892         return (error);
1893 }
1894 
1895 static int
1896 zfs_ioc_pool_upgrade(zfs_cmd_t *zc)
1897 {
1898         spa_t *spa;
1899         int error;
1900 
1901         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
 
2012         if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os)) != 0)
2013                 return (error);
2014         if (dmu_objset_type(os) != DMU_OST_ZFS) {
2015                 dmu_objset_rele(os, FTAG);
2016                 return (SET_ERROR(EINVAL));
2017         }
2018         error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value,
2019             sizeof (zc->zc_value));
2020         dmu_objset_rele(os, FTAG);
2021 
2022         return (error);
2023 }
2024 
2025 static int
2026 zfs_ioc_vdev_add(zfs_cmd_t *zc)
2027 {
2028         spa_t *spa;
2029         int error;
2030         nvlist_t *config, **l2cache, **spares;
2031         uint_t nl2cache = 0, nspares = 0;
2032         nvlist_t *event;
2033 
2034         error = spa_open(zc->zc_name, &spa, FTAG);
2035         if (error != 0)
2036                 return (error);
2037 
2038         error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2039             zc->zc_iflags, &config);
2040         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_L2CACHE,
2041             &l2cache, &nl2cache);
2042 
2043         (void) nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_SPARES,
2044             &spares, &nspares);
2045 
2046         /*
2047          * A root pool with concatenated devices is not supported.
2048          * Thus, can not add a device to a root pool.
2049          *
2050          * Intent log device can not be added to a rootpool because
2051          * during mountroot, zil is replayed, a seperated log device
2052          * can not be accessed during the mountroot time.
2053          *
2054          * l2cache and spare devices are ok to be added to a rootpool.
2055          */
2056         if (spa_bootfs(spa) != 0 && nl2cache == 0 && nspares == 0) {
2057                 nvlist_free(config);
2058                 spa_close(spa, FTAG);
2059                 return (SET_ERROR(EDOM));
2060         }
2061 
2062         if (error == 0) {
2063                 error = spa_vdev_add(spa, config);
2064                 if (error == 0) {
2065                         event = fnvlist_alloc();
2066                         fnvlist_add_string(event, "pool", zc->zc_name);
2067                         fnvlist_add_nvlist(event, "config", config);
2068                         zfs_event_post(ZPOOL_EC_STATUS, "add", event);
2069 
2070                 }
2071                 nvlist_free(config);
2072         }
2073         spa_close(spa, FTAG);
2074         return (error);
2075 }
2076 
2077 /*
2078  * inputs:
2079  * zc_name              name of the pool
2080  * zc_nvlist_conf       nvlist of devices to remove
2081  * zc_cookie            to stop the remove?
2082  */
2083 static int
2084 zfs_ioc_vdev_remove(zfs_cmd_t *zc)
2085 {
2086         spa_t *spa;
2087         int error;
2088         nvlist_t *event;
2089 
2090         error = spa_open(zc->zc_name, &spa, FTAG);
2091         if (error != 0)
2092                 return (error);
2093         error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE);
2094         if (error == 0) {
2095                 event = fnvlist_alloc();
2096                 fnvlist_add_string(event, "pool", zc->zc_name);
2097                 fnvlist_add_uint64(event, "guid", zc->zc_guid);
2098                 zfs_event_post(ZPOOL_EC_STATUS, "remove", event);
2099         }
2100 
2101         spa_close(spa, FTAG);
2102         return (error);
2103 }
2104 
2105 static int
2106 zfs_ioc_vdev_set_state(zfs_cmd_t *zc)
2107 {
2108         spa_t *spa;
2109         int error;
2110         vdev_state_t newstate = VDEV_STATE_UNKNOWN;
2111 
2112         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2113                 return (error);
2114         switch (zc->zc_cookie) {
2115         case VDEV_STATE_ONLINE:
2116                 error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate);
2117                 break;
2118 
2119         case VDEV_STATE_OFFLINE:
2120                 error = vdev_offline(spa, zc->zc_guid, zc->zc_obj);
2121                 break;
2122 
2123         case VDEV_STATE_FAULTED:
2124                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2125                     zc->zc_obj != VDEV_AUX_EXTERNAL &&
2126                     zc->zc_obj != VDEV_AUX_OPEN_FAILED)
2127                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2128 
2129                 error = vdev_fault(spa, zc->zc_guid, zc->zc_obj);
2130                 break;
2131 
2132         case VDEV_STATE_DEGRADED:
2133                 if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED &&
2134                     zc->zc_obj != VDEV_AUX_EXTERNAL)
2135                         zc->zc_obj = VDEV_AUX_ERR_EXCEEDED;
2136 
2137                 error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj);
2138                 break;
2139 
2140         default:
2141                 error = SET_ERROR(EINVAL);
2142         }
2143         zc->zc_cookie = newstate;
2144         spa_close(spa, FTAG);
2145         return (error);
2146 }
2147 
2148 static int
2149 zfs_ioc_vdev_attach(zfs_cmd_t *zc)
2150 {
2151         spa_t *spa;
2152         int replacing = zc->zc_cookie;
2153         nvlist_t *config;
2154         nvlist_t *event;
2155         int error;
2156 
2157         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2158                 return (error);
2159 
2160         if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2161             zc->zc_iflags, &config)) == 0) {
2162                 error = spa_vdev_attach(spa, zc->zc_guid, config, replacing);
2163                 if (error == 0) {
2164                         event = fnvlist_alloc();
2165                         fnvlist_add_string(event, "pool", zc->zc_name);
2166                         fnvlist_add_nvlist(event, "config", config);
2167                         fnvlist_add_int32(event, "replacing", replacing);
2168                         zfs_event_post(ZPOOL_EC_STATUS, "attach", event);
2169                 }
2170                 nvlist_free(config);
2171         }
2172 
2173         spa_close(spa, FTAG);
2174         return (error);
2175 }
2176 
2177 static int
2178 zfs_ioc_vdev_detach(zfs_cmd_t *zc)
2179 {
2180         spa_t *spa;
2181         int error;
2182         nvlist_t *event;
2183 
2184         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2185                 return (error);
2186 
2187         error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE);
2188         if (error == 0) {
2189                 event = fnvlist_alloc();
2190                 fnvlist_add_string(event, "pool", zc->zc_name);
2191                 fnvlist_add_uint64(event, "guid", zc->zc_guid);
2192                 zfs_event_post(ZPOOL_EC_STATUS, "detach", event);
2193         }
2194         spa_close(spa, FTAG);
2195         return (error);
2196 }
2197 
2198 static int
2199 zfs_ioc_vdev_split(zfs_cmd_t *zc)
2200 {
2201         spa_t *spa;
2202         nvlist_t *config, *props = NULL;
2203         int error;
2204         boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT);
2205 
2206         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
2207                 return (error);
2208 
2209         if (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size,
2210             zc->zc_iflags, &config)) {
2211                 spa_close(spa, FTAG);
2212                 return (error);
2213         }
2214 
2215         if (zc->zc_nvlist_src_size != 0 && (error =
2216             get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
2217             zc->zc_iflags, &props))) {
2218                 spa_close(spa, FTAG);
2219                 nvlist_free(config);
2220                 return (error);
2221         }
2222 
2223         error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp);
2224 
2225         spa_close(spa, FTAG);
2226 
2227         nvlist_free(config);
2228         nvlist_free(props);
2229 
2230         return (error);
2231 }
2232 
2233 static int
2234 zfs_ioc_vdev_setl2adddt(zfs_cmd_t *zc)
2235 {
2236         spa_t *spa;
2237         int error;
2238         uint64_t guid = zc->zc_guid;
2239         char *l2ad_ddt = zc->zc_value;
2240 
2241         error = spa_open(zc->zc_name, &spa, FTAG);
2242         if (error != 0)
2243                 return (error);
2244 
2245         error = spa_vdev_setl2adddt(spa, guid, l2ad_ddt);
2246         spa_close(spa, FTAG);
2247         return (error);
2248 }
2249 
2250 
2251 static int
2252 zfs_ioc_vdev_setpath(zfs_cmd_t *zc)
2253 {
2254         spa_t *spa;
2255         char *path = zc->zc_value;
2256         uint64_t guid = zc->zc_guid;
2257         int error;
2258 
2259         error = spa_open(zc->zc_name, &spa, FTAG);
2260         if (error != 0)
2261                 return (error);
2262 
2263         error = spa_vdev_setpath(spa, guid, path);
2264         spa_close(spa, FTAG);
2265         return (error);
2266 }
2267 
2268 static int
2269 zfs_ioc_vdev_setfru(zfs_cmd_t *zc)
2270 {
2271         spa_t *spa;
 
2310                 error = put_nvlist(zc, nv);
2311                 nvlist_free(nv);
2312         }
2313 
2314         return (error);
2315 }
2316 
2317 /*
2318  * inputs:
2319  * zc_name              name of filesystem
2320  * zc_nvlist_dst_size   size of buffer for property nvlist
2321  *
2322  * outputs:
2323  * zc_objset_stats      stats
2324  * zc_nvlist_dst        property nvlist
2325  * zc_nvlist_dst_size   size of property nvlist
2326  */
2327 static int
2328 zfs_ioc_objset_stats(zfs_cmd_t *zc)
2329 {
2330         objset_t *os = NULL;
2331         int error;
2332 
2333         error = dmu_objset_hold(zc->zc_name, FTAG, &os);
2334         if (error == 0) {
2335                 error = zfs_ioc_objset_stats_impl(zc, os);
2336                 dmu_objset_rele(os, FTAG);
2337         }
2338 
2339         return (error);
2340 }
2341 
2342 /*
2343  * inputs:
2344  * zc_name              name of filesystem
2345  * zc_nvlist_dst_size   size of buffer for property nvlist
2346  *
2347  * outputs:
2348  * zc_nvlist_dst        received property nvlist
2349  * zc_nvlist_dst_size   size of received property nvlist
2350  *
 
2419          */
2420         if (zc->zc_nvlist_dst != NULL &&
2421             !zc->zc_objset_stats.dds_inconsistent &&
2422             dmu_objset_type(os) == DMU_OST_ZFS) {
2423                 nvlist_t *nv;
2424 
2425                 VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2426                 if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 &&
2427                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 &&
2428                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 &&
2429                     (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0)
2430                         err = put_nvlist(zc, nv);
2431                 nvlist_free(nv);
2432         } else {
2433                 err = SET_ERROR(ENOENT);
2434         }
2435         dmu_objset_rele(os, FTAG);
2436         return (err);
2437 }
2438 
2439 /*
2440  * inputs:
2441  * zc_name              name of filesystem
2442  * zc_cookie            zap cursor
2443  * zc_nvlist_dst_size   size of buffer for property nvlist
2444  *
2445  * outputs:
2446  * zc_name              name of next filesystem
2447  * zc_cookie            zap cursor
2448  * zc_objset_stats      stats
2449  * zc_nvlist_dst        property nvlist
2450  * zc_nvlist_dst_size   size of property nvlist
2451  */
2452 static int
2453 zfs_ioc_dataset_list_next(zfs_cmd_t *zc)
2454 {
2455         objset_t *os;
2456         int error;
2457         char *p;
2458         size_t orig_len = strlen(zc->zc_name);
 
2691                 err = -1;
2692         }
2693 
2694         return (err);
2695 }
2696 
2697 /*
2698  * This function is best effort. If it fails to set any of the given properties,
2699  * it continues to set as many as it can and returns the last error
2700  * encountered. If the caller provides a non-NULL errlist, it will be filled in
2701  * with the list of names of all the properties that failed along with the
2702  * corresponding error numbers.
2703  *
2704  * If every property is set successfully, zero is returned and errlist is not
2705  * modified.
2706  */
2707 int
2708 zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl,
2709     nvlist_t *errlist)
2710 {
2711         spa_t *spa = NULL;
2712         nvpair_t *pair;
2713         nvpair_t *propval;
2714         int rv = 0;
2715         uint64_t intval;
2716         char *strval;
2717         nvlist_t *genericnvl = fnvlist_alloc();
2718         nvlist_t *retrynvl = fnvlist_alloc();
2719         zfsvfs_t *zfsvfs;
2720         boolean_t set_worm = B_FALSE;
2721         boolean_t set_wbc_mode = B_FALSE;
2722         boolean_t wbc_walk_locked = B_FALSE;
2723         boolean_t set_dedup = B_FALSE;
2724 
2725         if ((rv = spa_open(dsname, &spa, FTAG)) != 0)
2726                 return (rv);
2727 
2728 retry:
2729         pair = NULL;
2730         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2731                 const char *propname = nvpair_name(pair);
2732                 zfs_prop_t prop = zfs_name_to_prop(propname);
2733                 int err = 0;
2734 
2735                 if (!set_worm && (strcmp(propname, "nms:worm") == 0)) {
2736                         set_worm = B_TRUE;
2737                 }
2738 
2739                 /*
2740                  * If 'wbc_mode' is going to be changed, then we need to
2741                  * do some actions before 'set'
2742                  */
2743                 if (prop == ZFS_PROP_WBC_MODE)
2744                         set_wbc_mode = B_TRUE;
2745 
2746                 /*
2747                  *
2748                  */
2749                 if (prop == ZFS_PROP_DEDUP)
2750                         set_dedup = B_TRUE;
2751 
2752                 /* decode the property value */
2753                 propval = pair;
2754                 if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2755                         nvlist_t *attrs;
2756                         attrs = fnvpair_value_nvlist(pair);
2757                         if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE,
2758                             &propval) != 0)
2759                                 err = SET_ERROR(EINVAL);
2760                 }
2761 
2762                 /* Validate value type */
2763                 if (err == 0 && prop == ZPROP_INVAL) {
2764                         if (zfs_prop_user(propname)) {
2765                                 if (nvpair_type(propval) != DATA_TYPE_STRING)
2766                                         err = SET_ERROR(EINVAL);
2767                         } else if (zfs_prop_userquota(propname)) {
2768                                 if (nvpair_type(propval) !=
2769                                     DATA_TYPE_UINT64_ARRAY)
2770                                         err = SET_ERROR(EINVAL);
2771                         } else {
 
2817                                  * This may be a spurious error caused by
2818                                  * receiving quota and reservation out of order.
2819                                  * Try again in a second pass.
2820                                  */
2821                                 err = nvlist_add_nvpair(retrynvl, pair);
2822                         }
2823                 }
2824 
2825                 if (err != 0) {
2826                         if (errlist != NULL)
2827                                 fnvlist_add_int32(errlist, propname, err);
2828                         rv = err;
2829                 }
2830         }
2831 
2832         if (nvl != retrynvl && !nvlist_empty(retrynvl)) {
2833                 nvl = retrynvl;
2834                 goto retry;
2835         }
2836 
2837         /*
2838          * Deduplication and WBC cannot be used together
2839          * This code returns error also for case when
2840          * WBC is ON, DEDUP is off and a user tries
2841          * to do DEDUP=off, because in this case the code
2842          * will be more complex, but benefit is too small
2843          */
2844         if (set_wbc_mode && set_dedup) {
2845                 nvlist_free(genericnvl);
2846                 nvlist_free(retrynvl);
2847                 spa_close(spa, FTAG);
2848 
2849                 return (SET_ERROR(EKZFS_WBCCONFLICT));
2850         }
2851 
2852         /*
2853          * Additional actions before set wbc_mode:
2854          * - first need to try to lock WBC-walking, to stop migration and
2855          *   avoid the openning of new migration window
2856          * - second step (from sync-context): if migration window
2857          *   is active it will be purged, to correctly add/remove WBC-instance
2858          */
2859         if (set_wbc_mode && wbc_walk_lock(spa) == 0)
2860                 wbc_walk_locked = B_TRUE;
2861 
2862         if (!nvlist_empty(genericnvl) &&
2863             dsl_props_set(dsname, source, genericnvl) != 0) {
2864                 /*
2865                  * If this fails, we still want to set as many properties as we
2866                  * can, so try setting them individually.
2867                  */
2868                 pair = NULL;
2869                 while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) {
2870                         const char *propname = nvpair_name(pair);
2871                         int err = 0;
2872 
2873                         propval = pair;
2874                         if (nvpair_type(pair) == DATA_TYPE_NVLIST) {
2875                                 nvlist_t *attrs;
2876                                 attrs = fnvpair_value_nvlist(pair);
2877                                 propval = fnvlist_lookup_nvpair(attrs,
2878                                     ZPROP_VALUE);
2879                         }
2880 
2881                         if (nvpair_type(propval) == DATA_TYPE_STRING) {
 
2883                                 err = dsl_prop_set_string(dsname, propname,
2884                                     source, strval);
2885                         } else {
2886                                 intval = fnvpair_value_uint64(propval);
2887                                 err = dsl_prop_set_int(dsname, propname, source,
2888                                     intval);
2889                         }
2890 
2891                         if (err != 0) {
2892                                 if (errlist != NULL) {
2893                                         fnvlist_add_int32(errlist, propname,
2894                                             err);
2895                                 }
2896                                 rv = err;
2897                         }
2898                 }
2899         }
2900         nvlist_free(genericnvl);
2901         nvlist_free(retrynvl);
2902 
2903         if (wbc_walk_locked)
2904                 wbc_walk_unlock(spa);
2905 
2906         if (set_worm && getzfsvfs(dsname, &zfsvfs) == 0) {
2907                 if (zfs_is_wormed(dsname)) {
2908                         zfsvfs->z_isworm = B_TRUE;
2909                 } else {
2910                         zfsvfs->z_isworm = B_FALSE;
2911                 }
2912                 VFS_RELE(zfsvfs->z_vfs);
2913         }
2914 
2915         if (rv == 0)
2916                 autosnap_force_snap_by_name(dsname, NULL, B_FALSE);
2917 
2918         spa_close(spa, FTAG);
2919 
2920         return (rv);
2921 }
2922 
2923 /*
2924  * Check that all the properties are valid user properties.
2925  */
2926 static int
2927 zfs_check_userprops(const char *fsname, nvlist_t *nvl)
2928 {
2929         nvpair_t *pair = NULL;
2930         int error = 0;
2931 
2932         while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) {
2933                 const char *propname = nvpair_name(pair);
2934 
2935                 if (!zfs_prop_user(propname) ||
2936                     nvpair_type(pair) != DATA_TYPE_STRING)
2937                         return (SET_ERROR(EINVAL));
2938 
2939                 if (error = zfs_secpolicy_write_perms(fsname,
2940                     ZFS_DELEG_PERM_USERPROP, CRED()))
2941                         return (error);
2942 
2943                 if (strlen(propname) >= ZAP_MAXNAMELEN)
2944                         return (SET_ERROR(ENAMETOOLONG));
2945 
2946                 if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN)
2947                         return (SET_ERROR(E2BIG));
2948         }
2949         return (0);
2950 }
2951 
2952 static void
2953 props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops)
2954 {
2955         nvpair_t *pair;
2956 
2957         VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2958 
2959         pair = NULL;
2960         while ((pair = nvlist_next_nvpair(props, pair)) != NULL) {
2961                 if (nvlist_exists(skipped, nvpair_name(pair)))
2962                         continue;
2963 
2964                 VERIFY(nvlist_add_nvpair(*newprops, pair) == 0);
2965         }
2966 }
2967 
2968 static int
2969 clear_received_props(const char *dsname, nvlist_t *props,
2970     nvlist_t *skipped)
2971 {
2972         int err = 0;
2973         nvlist_t *cleared_props = NULL;
2974         props_skip(props, skipped, &cleared_props);
2975         if (!nvlist_empty(cleared_props)) {
2976                 /*
2977                  * Acts on local properties until the dataset has received
2978                  * properties at least once on or after SPA_VERSION_RECVD_PROPS.
2979                  */
2980                 zprop_source_t flags = (ZPROP_SRC_NONE |
2981                     (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0));
2982                 err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL);
2983         }
2984         nvlist_free(cleared_props);
2985         return (err);
2986 }
2987 
2988 int
2989 zfs_ioc_set_prop_impl(char *name, nvlist_t *props,
2990     boolean_t received, nvlist_t **out_errors)
2991 {
2992         int error = 0;
2993         nvlist_t *errors, *event;
2994         zprop_source_t source = (received ? ZPROP_SRC_RECEIVED :
2995             ZPROP_SRC_LOCAL);
2996 
2997         ASSERT(props != NULL);
2998 
2999         if (received) {
3000                 nvlist_t *origprops;
3001 
3002                 if (dsl_prop_get_received(name, &origprops) == 0) {
3003                         (void) clear_received_props(name, origprops, props);
3004                         nvlist_free(origprops);
3005                 }
3006 
3007                 error = dsl_prop_set_hasrecvd(name);
3008         }
3009 
3010         errors = fnvlist_alloc();
3011         if (error == 0)
3012                 error = zfs_set_prop_nvlist(name, source, props, errors);
3013 
3014         event = fnvlist_alloc();
3015         fnvlist_add_string(event, "fsname", name);
3016         fnvlist_add_nvlist(event, "properties", props);
3017         fnvlist_add_nvlist(event, "errors", errors);
3018         zfs_event_post(ZFS_EC_STATUS, "set", event);
3019 
3020         if (out_errors != NULL)
3021                 *out_errors = fnvlist_dup(errors);
3022 
3023         fnvlist_free(errors);
3024 
3025         return (error);
3026 }
3027 
3028 /*
3029  * XXX This functionality will be removed after integration of
3030  * functionality, that does the same via zfs-channel programm.
3031  * The zfs-channel programm implementation is being developed
3032  * by Delphix.
3033  *
3034  * This functions sets provided props for provided datasets
3035  * in one sync-round. There are some requirements:
3036  *  - all datasets should belong to the same pool
3037  *  - only user-properties
3038  *
3039  * This function does all or nothing.
3040  *
3041  * inputs:
3042  * zc_nvlist_src{_size} nvlist of datasets and properties to apply
3043  *
3044  * outputs:
3045  * zc_nvlist_dst{_size} error for each unapplied property
3046  */
3047 /* ARGSUSED */
3048 static int
3049 zfs_ioc_set_prop_mds(const char *pool_name, nvlist_t *dss_props,
3050     nvlist_t *outnvl)
3051 {
3052         int error = 0;
3053         spa_t *spa = NULL;
3054         nvpair_t *pair = NULL;
3055         size_t pool_name_len;
3056         size_t total_num_props = 0;
3057 
3058         ASSERT(dss_props != NULL);
3059 
3060         if (nvlist_empty(dss_props))
3061                 return (SET_ERROR(ENODATA));
3062 
3063         pool_name_len = strlen(pool_name);
3064         while ((pair = nvlist_next_nvpair(dss_props, pair)) != NULL) {
3065                 nvlist_t *props;
3066                 nvpair_t *prop_nvp = NULL;
3067                 const char *ds_name;
3068 
3069                 ds_name = nvpair_name(pair);
3070                 if (strncmp(pool_name, ds_name, pool_name_len) == 0) {
3071                         char c = ds_name[pool_name_len];
3072                         if (c != '\0' && c != '/' && c != '@')
3073                                 return (SET_ERROR(EXDEV));
3074                 }
3075 
3076                 if (nvpair_type(pair) != DATA_TYPE_NVLIST)
3077                         return (SET_ERROR(EINVAL));
3078 
3079                 props = fnvpair_value_nvlist(pair);
3080                 while ((prop_nvp = nvlist_next_nvpair(props,
3081                     prop_nvp)) != NULL) {
3082                         const char *propname = nvpair_name(prop_nvp);
3083                         /* Only user-props */
3084                         if (!zfs_prop_user(propname) ||
3085                             nvpair_type(prop_nvp) != DATA_TYPE_STRING)
3086                                 return (SET_ERROR(EINVAL));
3087 
3088                         /*
3089                          * We count the number to use it
3090                          * later to check for ENOSPC
3091                          */
3092                         total_num_props++;
3093                 }
3094         }
3095 
3096         if ((error = spa_open(pool_name, &spa, FTAG)) != 0)
3097                 return (error);
3098 
3099         error = dsl_props_set_mds(pool_name, dss_props, total_num_props);
3100         spa_close(spa, FTAG);
3101         if (error == 0) {
3102                 nvlist_t *event = fnvlist_alloc();
3103                 fnvlist_add_nvlist(event, "properties", dss_props);
3104                 zfs_event_post(ZFS_EC_STATUS, "set-mds", event);
3105         }
3106 
3107         return (error);
3108 }
3109 
3110 /*
3111  * inputs:
3112  * zc_name              name of filesystem
3113  * zc_value             name of property to set
3114  * zc_nvlist_src{_size} nvlist of properties to apply
3115  * zc_cookie            received properties flag
3116  *
3117  * outputs:
3118  * zc_nvlist_dst{_size} error for each unapplied received property
3119  */
3120 static int
3121 zfs_ioc_set_prop(zfs_cmd_t *zc)
3122 {
3123         nvlist_t *nvl;
3124         boolean_t received = zc->zc_cookie;
3125         nvlist_t *errors = NULL;
3126         int error;
3127 
3128         if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3129             zc->zc_iflags, &nvl)) != 0)
3130                 return (error);
3131 
3132         error = zfs_ioc_set_prop_impl(zc->zc_name, nvl, received, &errors);
3133 
3134         if (zc->zc_nvlist_dst != NULL && errors != NULL) {
3135                 (void) put_nvlist(zc, errors);
3136         }
3137 
3138         nvlist_free(errors);
3139         nvlist_free(nvl);
3140         return (error);
3141 }
3142 
3143 /*
3144  * inputs:
3145  * zc_name              name of filesystem
3146  * zc_value             name of property to inherit
3147  * zc_cookie            revert to received value if TRUE
3148  *
3149  * outputs:             none
3150  */
3151 static int
3152 zfs_ioc_inherit_prop(zfs_cmd_t *zc)
3153 {
 
3205                  * Only check this in the non-received case. We want to allow
3206                  * 'inherit -S' to revert non-inheritable properties like quota
3207                  * and reservation to the received or default values even though
3208                  * they are not considered inheritable.
3209                  */
3210                 if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop))
3211                         return (SET_ERROR(EINVAL));
3212         }
3213 
3214         /* property name has been validated by zfs_secpolicy_inherit_prop() */
3215         return (dsl_prop_inherit(zc->zc_name, zc->zc_value, source));
3216 }
3217 
3218 static int
3219 zfs_ioc_pool_set_props(zfs_cmd_t *zc)
3220 {
3221         nvlist_t *props;
3222         spa_t *spa;
3223         int error;
3224         nvpair_t *pair;
3225         nvlist_t *event;
3226         if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
3227             zc->zc_iflags, &props))
3228                 return (error);
3229 
3230         /*
3231          * If the only property is the configfile, then just do a spa_lookup()
3232          * to handle the faulted case.
3233          */
3234         pair = nvlist_next_nvpair(props, NULL);
3235         if (pair != NULL && strcmp(nvpair_name(pair),
3236             zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 &&
3237             nvlist_next_nvpair(props, pair) == NULL) {
3238                 mutex_enter(&spa_namespace_lock);
3239                 if ((spa = spa_lookup(zc->zc_name)) != NULL) {
3240                         spa_configfile_set(spa, props, B_FALSE);
3241                         spa_config_sync(spa, B_FALSE, B_TRUE);
3242                 }
3243                 mutex_exit(&spa_namespace_lock);
3244                 if (spa != NULL) {
3245                         nvlist_free(props);
3246                         return (0);
3247                 }
3248         }
3249 
3250         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3251                 nvlist_free(props);
3252                 return (error);
3253         }
3254 
3255         error = spa_prop_set(spa, props);
3256 
3257         if (error == 0) {
3258                 event = fnvlist_alloc();
3259                 fnvlist_add_string(event, "pool", zc->zc_name);
3260                 fnvlist_add_nvlist(event, "props", props);
3261                 zfs_event_post(ZPOOL_EC_STATUS, "set", event);
3262         }
3263 
3264         nvlist_free(props);
3265         spa_close(spa, FTAG);
3266 
3267         return (error);
3268 }
3269 
3270 static int
3271 zfs_ioc_pool_get_props(zfs_cmd_t *zc)
3272 {
3273         spa_t *spa;
3274         int error;
3275         nvlist_t *nvp = NULL;
3276 
3277         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
3278                 /*
3279                  * If the pool is faulted, there may be properties we can still
3280                  * get (such as altroot and cachefile), so attempt to get them
3281                  * anyway.
3282                  */
3283                 mutex_enter(&spa_namespace_lock);
 
3365                 error = put_nvlist(zc, nvp);
3366                 nvlist_free(nvp);
3367         }
3368 
3369         return (error);
3370 }
3371 
3372 /* ARGSUSED */
3373 static void
3374 zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx)
3375 {
3376         zfs_creat_t *zct = arg;
3377 
3378         zfs_create_fs(os, cr, zct->zct_zplprops, tx);
3379 }
3380 
3381 #define ZFS_PROP_UNDEFINED      ((uint64_t)-1)
3382 
3383 /*
3384  * inputs:
3385  * createprops          list of properties requested by creator
3386  * default_zplver       zpl version to use if unspecified in createprops
3387  * fuids_ok             fuids allowed in this version of the spa?
3388  * os                   parent objset pointer (NULL if root fs)
3389  * fuids_ok             fuids allowed in this version of the spa?
3390  * sa_ok                SAs allowed in this version of the spa?
3391  * createprops          list of properties requested by creator
3392  *
3393  * outputs:
3394  * zplprops     values for the zplprops we attach to the master node object
3395  * is_ci        true if requested file system will be purely case-insensitive
3396  *
3397  * Determine the settings for utf8only, normalization and
3398  * casesensitivity.  Specific values may have been requested by the
3399  * creator and/or we can inherit values from the parent dataset.  If
3400  * the file system is of too early a vintage, a creator can not
3401  * request settings for these properties, even if the requested
3402  * setting is the default value.  We don't actually want to create dsl
3403  * properties for these, so remove them from the source nvlist after
3404  * processing.
3405  */
3406 static int
3407 zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver,
 
3445          */
3446         if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) ||
3447             (zplver >= ZPL_VERSION_FUID && !fuids_ok) ||
3448             (zplver >= ZPL_VERSION_SA && !sa_ok) ||
3449             (zplver < ZPL_VERSION_NORMALIZATION &&
3450             (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED ||
3451             sense != ZFS_PROP_UNDEFINED)))
3452                 return (SET_ERROR(ENOTSUP));
3453 
3454         /*
3455          * Put the version in the zplprops
3456          */
3457         VERIFY(nvlist_add_uint64(zplprops,
3458             zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0);
3459 
3460         if (norm == ZFS_PROP_UNDEFINED)
3461                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm) == 0);
3462         VERIFY(nvlist_add_uint64(zplprops,
3463             zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0);
3464 
3465         if (os) {
3466                 if (zfs_is_wormed_ds(dmu_objset_ds(os)))
3467                         return (SET_ERROR(EPERM));
3468         }
3469 
3470         /*
3471          * If we're normalizing, names must always be valid UTF-8 strings.
3472          */
3473         if (norm)
3474                 u8 = 1;
3475         if (u8 == ZFS_PROP_UNDEFINED)
3476                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8) == 0);
3477         VERIFY(nvlist_add_uint64(zplprops,
3478             zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0);
3479 
3480         if (sense == ZFS_PROP_UNDEFINED)
3481                 VERIFY(zfs_get_zplprop(os, ZFS_PROP_CASE, &sense) == 0);
3482         VERIFY(nvlist_add_uint64(zplprops,
3483             zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0);
3484 
3485         if (is_ci)
3486                 *is_ci = (sense == ZFS_CASE_INSENSITIVE);
3487 
3488         return (0);
3489 }
 
3547 }
3548 
3549 /*
3550  * innvl: {
3551  *     "type" -> dmu_objset_type_t (int32)
3552  *     (optional) "props" -> { prop -> value }
3553  * }
3554  *
3555  * outnvl: propname -> error code (int32)
3556  */
3557 static int
3558 zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3559 {
3560         int error = 0;
3561         zfs_creat_t zct = { 0 };
3562         nvlist_t *nvprops = NULL;
3563         void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx);
3564         int32_t type32;
3565         dmu_objset_type_t type;
3566         boolean_t is_insensitive = B_FALSE;
3567         char parent[MAXNAMELEN];
3568         nvlist_t *event;
3569 
3570         if (nvlist_lookup_int32(innvl, "type", &type32) != 0)
3571                 return (SET_ERROR(EINVAL));
3572         type = type32;
3573         (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3574 
3575         switch (type) {
3576         case DMU_OST_ZFS:
3577                 cbfunc = zfs_create_cb;
3578                 break;
3579 
3580         case DMU_OST_ZVOL:
3581                 cbfunc = zvol_create_cb;
3582                 break;
3583 
3584         default:
3585                 cbfunc = NULL;
3586                 break;
3587         }
3588         if (strchr(fsname, '@') ||
3589             strchr(fsname, '%'))
3590                 return (SET_ERROR(EINVAL));
3591 
3592         zct.zct_props = nvprops;
3593 
3594         if (cbfunc == NULL)
3595                 return (SET_ERROR(EINVAL));
3596 
3597         if (zfs_get_parent(fsname, parent, MAXNAMELEN) == 0 &&
3598             zfs_is_wormed(parent)) {
3599                 return (SET_ERROR(EPERM));
3600         }
3601 
3602         if (type == DMU_OST_ZVOL) {
3603                 uint64_t volsize, volblocksize;
3604 
3605                 if (nvprops == NULL)
3606                         return (SET_ERROR(EINVAL));
3607                 if (nvlist_lookup_uint64(nvprops,
3608                     zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0)
3609                         return (SET_ERROR(EINVAL));
3610 
3611                 if ((error = nvlist_lookup_uint64(nvprops,
3612                     zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3613                     &volblocksize)) != 0 && error != ENOENT)
3614                         return (SET_ERROR(EINVAL));
3615 
3616                 if (error != 0)
3617                         volblocksize = zfs_prop_default_numeric(
3618                             ZFS_PROP_VOLBLOCKSIZE);
3619 
3620                 if ((error = zvol_check_volblocksize(
3621                     volblocksize)) != 0 ||
3622                     (error = zvol_check_volsize(volsize,
3623                     volblocksize)) != 0)
3624                         return (error);
3625         } else if (type == DMU_OST_ZFS) {
3626                 /*
3627                  * We have to have normalization and
3628                  * case-folding flags correct when we do the
3629                  * file system creation, so go figure them out
3630                  * now.
3631                  */
3632                 VERIFY(nvlist_alloc(&zct.zct_zplprops,
3633                     NV_UNIQUE_NAME, KM_SLEEP) == 0);
3634                 error = zfs_fill_zplprops(fsname, nvprops,
3635                     zct.zct_zplprops, &is_insensitive);
3636                 if (error != 0) {
3637                         nvlist_free(zct.zct_zplprops);
3638                         return (error);
3639                 }
3640         }
3641 
3642         error = dmu_objset_create(fsname, type,
3643             is_insensitive ? DS_FLAG_CI_DATASET : 0, cbfunc, &zct);
3644         nvlist_free(zct.zct_zplprops);
3645 
3646         /*
3647          * It would be nice to do this atomically.
3648          */
3649         if (error == 0) {
3650                 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3651                     nvprops, outnvl);
3652                 if (error != 0)
3653                         (void) dsl_destroy_head(fsname);
3654         }
3655 
3656         if (error == 0) {
3657                 event = fnvlist_alloc();
3658                 fnvlist_add_string(event, "fsname", fsname);
3659                 fnvlist_add_int32(event, "type", type);
3660                 if (nvprops != NULL)
3661                         fnvlist_add_nvlist(event, "properties", nvprops);
3662                 zfs_event_post(ZFS_EC_STATUS, "create", event);
3663         }
3664 
3665         return (error);
3666 }
3667 
3668 /*
3669  * innvl: {
3670  *     "origin" -> name of origin snapshot
3671  *     (optional) "props" -> { prop -> value }
3672  * }
3673  *
3674  * outnvl: propname -> error code (int32)
3675  */
3676 static int
3677 zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
3678 {
3679         int error = 0;
3680         nvlist_t *nvprops = NULL;
3681         char *origin_name, *origin_snap;
3682         nvlist_t *event;
3683 
3684         if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0)
3685                 return (SET_ERROR(EINVAL));
3686 
3687         origin_snap = strchr(origin_name, '@');
3688         if (!origin_snap)
3689                 return (SET_ERROR(EINVAL));
3690 
3691         if (autosnap_check_name(origin_snap))
3692                 return (SET_ERROR(EPERM));
3693 
3694         (void) nvlist_lookup_nvlist(innvl, "props", &nvprops);
3695 
3696         if (strchr(fsname, '@') ||
3697             strchr(fsname, '%'))
3698                 return (SET_ERROR(EINVAL));
3699 
3700         if (dataset_namecheck(origin_name, NULL, NULL) != 0)
3701                 return (SET_ERROR(EINVAL));
3702 
3703         error = dmu_objset_clone(fsname, origin_name);
3704         if (error != 0)
3705                 return (error);
3706 
3707         /*
3708          * It would be nice to do this atomically.
3709          */
3710         if (error == 0) {
3711                 error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL,
3712                     nvprops, outnvl);
3713                 if (error != 0)
3714                         (void) dsl_destroy_head(fsname);
3715         }
3716 
3717         if (error == 0) {
3718                 event = fnvlist_alloc();
3719                 fnvlist_add_string(event, "origin", origin_name);
3720                 fnvlist_add_string(event, "fsname", fsname);
3721                 if (nvprops != NULL)
3722                         fnvlist_add_nvlist(event, "properties", nvprops);
3723                 zfs_event_post(ZFS_EC_STATUS, "clone", event);
3724         }
3725 
3726         return (error);
3727 }
3728 
3729 /*
3730  * innvl: {
3731  *     "snaps" -> { snapshot1, snapshot2 }
3732  *     (optional) "props" -> { prop -> value (string) }
3733  * }
3734  *
3735  * outnvl: snapshot -> error code (int32)
3736  */
3737 static int
3738 zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3739 {
3740         nvlist_t *snaps;
3741         nvlist_t *props = NULL;
3742         int error, poollen;
3743         nvpair_t *pair;
3744         nvlist_t *event;
3745 
3746         (void) nvlist_lookup_nvlist(innvl, "props", &props);
3747         if ((error = zfs_check_userprops(poolname, props)) != 0)
3748                 return (error);
3749 
3750         if (!nvlist_empty(props) &&
3751             zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS))
3752                 return (SET_ERROR(ENOTSUP));
3753 
3754         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3755                 return (SET_ERROR(EINVAL));
3756         poollen = strlen(poolname);
3757         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3758             pair = nvlist_next_nvpair(snaps, pair)) {
3759                 const char *name = nvpair_name(pair);
3760                 const char *cp = strchr(name, '@');
3761 
3762                 /*
3763                  * The snap name must contain an @, and the part after it must
3764                  * contain only valid characters.
3765                  */
3766                 if (cp == NULL ||
3767                     zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
3768                         return (SET_ERROR(EINVAL));
3769 
3770                 if (autosnap_check_name(cp))
3771                         return (EINVAL);
3772 
3773                 /*
3774                  * The snap must be in the specified pool.
3775                  */
3776                 if (strncmp(name, poolname, poollen) != 0 ||
3777                     (name[poollen] != '/' && name[poollen] != '@'))
3778                         return (SET_ERROR(EXDEV));
3779 
3780                 /* This must be the only snap of this fs. */
3781                 for (nvpair_t *pair2 = nvlist_next_nvpair(snaps, pair);
3782                     pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) {
3783                         if (strncmp(name, nvpair_name(pair2), cp - name + 1)
3784                             == 0) {
3785                                 return (SET_ERROR(EXDEV));
3786                         }
3787                 }
3788         }
3789 
3790         error = dsl_dataset_snapshot(snaps, props, outnvl);
3791 
3792         event = fnvlist_alloc();
3793         fnvlist_add_nvlist(event, "snaps", snaps);
3794         fnvlist_add_nvlist(event, "errors", outnvl);
3795         fnvlist_add_string(event, "pool", poolname);
3796         zfs_event_post(ZFS_EC_STATUS, "snapshot", event);
3797 
3798         return (error);
3799 }
3800 
3801 /*
3802  * innvl: "message" -> string
3803  */
3804 /* ARGSUSED */
3805 static int
3806 zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl)
3807 {
3808         char *message;
3809         spa_t *spa;
3810         int error;
3811         char *poolname;
3812 
3813         /*
3814          * The poolname in the ioctl is not set, we get it from the TSD,
3815          * which was set at the end of the last successful ioctl that allows
3816          * logging.  The secpolicy func already checked that it is set.
3817          * Only one log ioctl is allowed after each successful ioctl, so
 
3895 zfs_destroy_unmount_origin(const char *fsname)
3896 {
3897         int error;
3898         objset_t *os;
3899         dsl_dataset_t *ds;
3900 
3901         error = dmu_objset_hold(fsname, FTAG, &os);
3902         if (error != 0)
3903                 return;
3904         ds = dmu_objset_ds(os);
3905         if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) {
3906                 char originname[ZFS_MAX_DATASET_NAME_LEN];
3907                 dsl_dataset_name(ds->ds_prev, originname);
3908                 dmu_objset_rele(os, FTAG);
3909                 zfs_unmount_snap(originname);
3910         } else {
3911                 dmu_objset_rele(os, FTAG);
3912         }
3913 }
3914 
3915 static int
3916 zfs_destroy_check_autosnap(spa_t *spa, const char *name)
3917 {
3918         const char *snap = strchr(name, '@');
3919 
3920         if (snap == NULL)
3921                 return (EINVAL);
3922 
3923         if (autosnap_check_name(snap)) {
3924                 int err = autosnap_check_for_destroy(
3925                     spa_get_autosnap(spa), name);
3926 
3927                 if (err != 0)
3928                         return (EBUSY);
3929         }
3930 
3931         return (0);
3932 }
3933 
3934 /*
3935  * innvl: {
3936  *     "snaps" -> { snapshot1, snapshot2 }
3937  *     (optional boolean) "defer"
3938  * }
3939  *
3940  * outnvl: snapshot -> error code (int32)
3941  *
3942  */
3943 /* ARGSUSED */
3944 static int
3945 zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
3946 {
3947         nvlist_t *snaps;
3948         nvpair_t *pair;
3949         boolean_t defer;
3950         int error = 0;
3951         nvlist_t *event;
3952         spa_t *spa;
3953 
3954         if (zfs_is_wormed(poolname))
3955                 return (SET_ERROR(EPERM));
3956 
3957         if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0)
3958                 return (SET_ERROR(EINVAL));
3959         defer = nvlist_exists(innvl, "defer");
3960 
3961         error = spa_open(poolname, &spa, FTAG);
3962         if (spa == NULL)
3963                 return (error);
3964 
3965         for (pair = nvlist_next_nvpair(snaps, NULL);
3966             pair != NULL; pair = nvlist_next_nvpair(snaps, pair)) {
3967                 error = zfs_destroy_check_autosnap(spa, nvpair_name(pair));
3968                 if (error)
3969                         fnvlist_add_int32(outnvl, nvpair_name(pair), error);
3970         }
3971 
3972         spa_close(spa, FTAG);
3973 
3974         if (error)
3975                 return (error);
3976 
3977         for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
3978             pair = nvlist_next_nvpair(snaps, pair)) {
3979                 zfs_unmount_snap(nvpair_name(pair));
3980         }
3981 
3982         error = dsl_destroy_snapshots_nvl(snaps, defer, outnvl);
3983 
3984         if (error == 0) {
3985                 event = fnvlist_alloc();
3986                 fnvlist_add_nvlist(event, "snaps", snaps);
3987                 fnvlist_add_nvlist(event, "errors", outnvl);
3988                 zfs_event_post(ZFS_EC_STATUS, "destroy_snaps", event);
3989         }
3990 
3991         return (error);
3992 }
3993 
3994 /*
3995  * Create bookmarks.  Bookmark names are of the form <fs>#<bmark>.
3996  * All bookmarks must be in the same pool.
3997  *
3998  * innvl: {
3999  *     bookmark1 -> snapshot1, bookmark2 -> snapshot2
4000  * }
4001  *
4002  * outnvl: bookmark -> error code (int32)
4003  *
4004  */
4005 /* ARGSUSED */
4006 static int
4007 zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
4008 {
4009         for (nvpair_t *pair = nvlist_next_nvpair(innvl, NULL);
4010             pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) {
4011                 char *snap_name;
 
4107                 memlimit = ZCP_DEFAULT_MEMLIMIT;
4108         }
4109         if (0 != nvlist_lookup_nvpair(innvl, ZCP_ARG_ARGLIST, &nvarg)) {
4110                 return (EINVAL);
4111         }
4112 
4113         if (instrlimit == 0 || instrlimit > zfs_lua_max_instrlimit)
4114                 return (EINVAL);
4115         if (memlimit == 0 || memlimit > zfs_lua_max_memlimit)
4116                 return (EINVAL);
4117 
4118         return (zcp_eval(poolname, program, sync_flag, instrlimit, memlimit,
4119             nvarg, outnvl));
4120 }
4121 
4122 /*
4123  * inputs:
4124  * zc_name              name of dataset to destroy
4125  * zc_objset_type       type of objset
4126  * zc_defer_destroy     mark for deferred destroy
4127  * zc_guid              if set, do atomical recursive destroy
4128  *
4129  * outputs:             none
4130  */
4131 static int
4132 zfs_ioc_destroy(zfs_cmd_t *zc)
4133 {
4134         int err;
4135         nvlist_t *event;
4136 
4137         if (zfs_is_wormed(zc->zc_name))
4138                 return (SET_ERROR(EPERM));
4139 
4140         if (zc->zc_objset_type == DMU_OST_ZFS)
4141                 zfs_unmount_snap(zc->zc_name);
4142 
4143         if (zc->zc_guid) {
4144                 spa_t *spa;
4145 
4146                 if ((err = spa_open(zc->zc_name, &spa, FTAG)) != 0)
4147                         return (err);
4148 
4149                 err = autosnap_lock(spa, RW_WRITER);
4150                 if (err == 0) {
4151                         err = wbc_walk_lock(spa);
4152                         if (err != 0)
4153                                 autosnap_unlock(spa);
4154                 }
4155 
4156                 if (err == 0) {
4157                         err = dsl_destroy_atomically(zc->zc_name,
4158                             zc->zc_defer_destroy);
4159                         wbc_walk_unlock(spa);
4160                         autosnap_unlock(spa);
4161                 }
4162 
4163                 spa_close(spa, FTAG);
4164         } else {
4165                 if (strchr(zc->zc_name, '@')) {
4166                         spa_t *spa = NULL;
4167 
4168                         err = spa_open(zc->zc_name, &spa, FTAG);
4169                         if (err != 0)
4170                                 return (err);
4171 
4172                         err = zfs_destroy_check_autosnap(spa, zc->zc_name);
4173                         if (err == 0) {
4174                                 err = dsl_destroy_snapshot(zc->zc_name,
4175                                     zc->zc_defer_destroy);
4176                         }
4177 
4178                         spa_close(spa, FTAG);
4179                 } else {
4180                         err = dsl_destroy_head(zc->zc_name);
4181                         if (err == EEXIST) {
4182                                 /*
4183                                  * It is possible that the given DS may have
4184                                  * hidden child (%recv) datasets - "leftovers"
4185                                  * resulting from the previously interrupted
4186                                  * 'zfs receive'.
4187                                  */
4188                                 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4189 
4190                                 if (snprintf(namebuf, sizeof (namebuf),
4191                                     "%s/%%recv", zc->zc_name) >=
4192                                     sizeof (namebuf))
4193                                         return (err);
4194 
4195                                 /* Try to remove the hidden child (%recv) */
4196                                 err = dsl_destroy_head(namebuf);
4197                                 if (err == 0) {
4198                                         /*
4199                                          * Now the given DS should not have
4200                                          * children, so we can try to remove
4201                                          * it again
4202                                          */
4203                                         err = dsl_destroy_head(zc->zc_name);
4204                                 } else if (err == ENOENT) {
4205                                         /*
4206                                          * The hidden child (%recv) does not
4207                                          * exist, so need to restore original
4208                                          * error
4209                                          */
4210                                         err = EEXIST;
4211                                 }
4212 
4213                         }
4214                 }
4215         }
4216         if (zc->zc_objset_type == DMU_OST_ZVOL && err == 0)
4217                 (void) zvol_remove_minor(zc->zc_name);
4218 
4219         if (err == 0) {
4220                 event = fnvlist_alloc();
4221                 fnvlist_add_string(event, "fsname", zc->zc_name);
4222                 fnvlist_add_int32(event, "type", zc->zc_objset_type);
4223                 zfs_event_post(ZFS_EC_STATUS, "destroy", event);
4224         }
4225 
4226         return (err);
4227 }
4228 
4229 /*
4230  * fsname is name of dataset to rollback (to most recent snapshot)
4231  *
4232  * innvl may contain name of expected target snapshot
4233  *
4234  * outnvl: "target" -> name of most recent snapshot
4235  * }
4236  */
4237 /* ARGSUSED */
4238 static int
4239 zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl)
4240 {
4241         zfsvfs_t *zfsvfs;
4242         char *target = NULL;
4243         int error;
4244         nvlist_t *event;
4245         int resume_err = 0;
4246 
4247         if (zfs_is_wormed(fsname))
4248                 return (SET_ERROR(EPERM));
4249 
4250         (void) nvlist_lookup_string(innvl, "target", &target);
4251         if (target != NULL) {
4252                 const char *cp = strchr(target, '@');
4253 
4254                 /*
4255                  * The snap name must contain an @, and the part after it must
4256                  * contain only valid characters.
4257                  */
4258                 if (cp == NULL ||
4259                     zfs_component_namecheck(cp + 1, NULL, NULL) != 0)
4260                         return (SET_ERROR(EINVAL));
4261         }
4262 
4263         if (getzfsvfs(fsname, &zfsvfs) == 0) {
4264                 dsl_dataset_t *ds;
4265 
4266                 ds = dmu_objset_ds(zfsvfs->z_os);
4267                 error = zfs_suspend_fs(zfsvfs);
4268                 if (error == 0) {
4269                         error = dsl_dataset_rollback(fsname, target, zfsvfs,
4270                             outnvl);
4271                         resume_err = zfs_resume_fs(zfsvfs, ds);
4272                 }
4273                 VFS_RELE(zfsvfs->z_vfs);
4274         } else {
4275                 error = dsl_dataset_rollback(fsname, target, NULL, outnvl);
4276         }
4277 
4278         if (error == 0) {
4279                 event = fnvlist_alloc();
4280                 fnvlist_add_string(event, "target", (target != NULL) ? target : "");
4281                 fnvlist_add_string(event, "fsname", fsname);
4282                 fnvlist_add_int32(event, "resume_err", resume_err);
4283                 zfs_event_post(ZFS_EC_STATUS, "rollback", event);
4284         }
4285 
4286         error = (error != 0) ? error : resume_err;
4287         return (error);
4288 }
4289 
4290 static int
4291 recursive_unmount(const char *fsname, void *arg)
4292 {
4293         const char *snapname = arg;
4294         char fullname[ZFS_MAX_DATASET_NAME_LEN];
4295 
4296         (void) snprintf(fullname, sizeof (fullname), "%s@%s", fsname, snapname);
4297         zfs_unmount_snap(fullname);
4298 
4299         return (0);
4300 }
4301 
4302 /*
4303  * inputs:
4304  * zc_name      old name of dataset
4305  * zc_value     new name of dataset
4306  * zc_cookie    recursive flag (only valid for snapshots)
4307  *
4308  * outputs:     none
4309  */
4310 static int
4311 zfs_ioc_rename(zfs_cmd_t *zc)
4312 {
4313         boolean_t recursive = zc->zc_cookie & 1;
4314         char *at;
4315         nvlist_t *event;
4316         int error;
4317 
4318         if (zfs_is_wormed(zc->zc_name))
4319                 return (SET_ERROR(EPERM));
4320 
4321         zc->zc_value[sizeof (zc->zc_value) - 1] = '\0';
4322         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
4323             strchr(zc->zc_value, '%'))
4324                 return (SET_ERROR(EINVAL));
4325 
4326         at = strchr(zc->zc_name, '@');
4327         if (at != NULL) {
4328                 /* snaps must be in same fs */
4329 
4330                 if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1))
4331                         return (SET_ERROR(EXDEV));
4332                 *at = '\0';
4333                 if (zc->zc_objset_type == DMU_OST_ZFS) {
4334                         error = dmu_objset_find(zc->zc_name,
4335                             recursive_unmount, at + 1,
4336                             recursive ? DS_FIND_CHILDREN : 0);
4337                         if (error != 0) {
4338                                 *at = '@';
4339                                 return (error);
4340                         }
4341                 }
4342                 error = dsl_dataset_rename_snapshot(zc->zc_name,
4343                     at + 1, strchr(zc->zc_value, '@') + 1, recursive);
4344                 *at = '@';
4345 
4346         } else {
4347                 if (zc->zc_objset_type == DMU_OST_ZVOL)
4348                         (void) zvol_remove_minor(zc->zc_name);
4349                 error = dsl_dir_rename(zc->zc_name, zc->zc_value);
4350         }
4351 
4352         if (error == 0) {
4353                 event = fnvlist_alloc();
4354                 fnvlist_add_string(event, "origin", zc->zc_name);
4355                 fnvlist_add_string(event, "fsname", zc->zc_value);
4356                 fnvlist_add_int32(event, "type", zc->zc_objset_type);
4357                 zfs_event_post(ZFS_EC_STATUS, "rename", event);
4358         }
4359 
4360         return (error);
4361 }
4362 
4363 static int
4364 zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr)
4365 {
4366         const char *propname = nvpair_name(pair);
4367         boolean_t issnap = (strchr(dsname, '@') != NULL);
4368         zfs_prop_t prop = zfs_name_to_prop(propname);
4369         uint64_t intval;
4370         int err;
4371 
4372         if (prop == ZPROP_INVAL) {
4373                 if (zfs_prop_user(propname)) {
4374                         if (err = zfs_secpolicy_write_perms(dsname,
4375                             ZFS_DELEG_PERM_USERPROP, cr))
4376                                 return (err);
4377                         return (0);
4378                 }
4379 
4380                 if (!issnap && zfs_prop_userquota(propname)) {
 
4497                         }
4498                         spa_close(spa, FTAG);
4499                 }
4500                 break;
4501 
4502         case ZFS_PROP_SHARESMB:
4503                 if (zpl_earlier_version(dsname, ZPL_VERSION_FUID))
4504                         return (SET_ERROR(ENOTSUP));
4505                 break;
4506 
4507         case ZFS_PROP_ACLINHERIT:
4508                 if (nvpair_type(pair) == DATA_TYPE_UINT64 &&
4509                     nvpair_value_uint64(pair, &intval) == 0) {
4510                         if (intval == ZFS_ACL_PASSTHROUGH_X &&
4511                             zfs_earlier_version(dsname,
4512                             SPA_VERSION_PASSTHROUGH_X))
4513                                 return (SET_ERROR(ENOTSUP));
4514                 }
4515                 break;
4516 
4517         case ZFS_PROP_WBC_MODE:
4518                 {
4519                         spa_t *spa;
4520                         boolean_t wbc_feature_enabled;
4521 
4522                         if ((err = spa_open(dsname, &spa, FTAG)) != 0)
4523                                 return (err);
4524 
4525                         wbc_feature_enabled =
4526                             spa_feature_is_enabled(spa, SPA_FEATURE_WBC);
4527                         spa_close(spa, FTAG);
4528 
4529                         /* WBC cannot be used without special-vdev */
4530                         if (!wbc_feature_enabled || !spa_has_special(spa))
4531                                 return (SET_ERROR(EKZFS_WBCNOTSUP));
4532 
4533                         /*
4534                          * We do not want to have races, because on
4535                          * import or after reboot WBC does registration
4536                          * asynchronously.
4537                          */
4538                         if (!spa->spa_wbc.wbc_ready_to_use)
4539                                 return (SET_ERROR(EBUSY));
4540                 }
4541                 break;
4542 
4543         case ZFS_PROP_CHECKSUM:
4544         case ZFS_PROP_DEDUP:
4545         {
4546                 spa_feature_t feature;
4547                 spa_t *spa;
4548 
4549                 /* dedup feature version checks */
4550                 if (prop == ZFS_PROP_DEDUP &&
4551                     zfs_earlier_version(dsname, SPA_VERSION_DEDUP))
4552                         return (SET_ERROR(ENOTSUP));
4553 
4554                 if (nvpair_value_uint64(pair, &intval) != 0)
4555                         return (SET_ERROR(EINVAL));
4556 
4557                 /* check prop value is enabled in features */
4558                 feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK);
4559                 if (feature == SPA_FEATURE_NONE)
4560                         break;
4561 
4562                 if ((err = spa_open(dsname, &spa, FTAG)) != 0)
 
4798                 }
4799                 if (delayable[i] != 0) {
4800                         tmp = nvlist_prev_nvpair(props, nvp);
4801                         VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0);
4802                         VERIFY(nvlist_remove_nvpair(props, nvp) == 0);
4803                         nvp = tmp;
4804                 }
4805         }
4806 
4807         if (nvlist_empty(delayprops)) {
4808                 nvlist_free(delayprops);
4809                 delayprops = NULL;
4810         }
4811         return (delayprops);
4812 }
4813 
4814 #ifdef  DEBUG
4815 static boolean_t zfs_ioc_recv_inject_err;
4816 #endif
4817 
4818 int
4819 dmu_recv_impl(int fd, char *tofs, char *tosnap, char *origin,
4820     dmu_replay_record_t *drr_begin, boolean_t is_resumable, nvlist_t *props,
4821     nvlist_t *errors, uint64_t *errf, int cfd, uint64_t *ahdl, uint64_t *sz,
4822     boolean_t force, dmu_krrp_task_t *krrp_task)
4823 {
4824         file_t *fp = getf(fd);
4825         dmu_recv_cookie_t drc;
4826         int error = 0;
4827         int props_error = 0;
4828         offset_t off;
4829         nvlist_t *origprops = NULL; /* existing properties */
4830         nvlist_t *delayprops = NULL; /* sent properties applied post-receive */
4831         boolean_t first_recvd_props = B_FALSE;
4832         nvlist_t *event;
4833         boolean_t force_cksum =
4834             !krrp_task || krrp_task->buffer_args.force_cksum;
4835 
4836         ASSERT(fp || krrp_task);
4837 
4838         error = dmu_recv_begin(tofs, tosnap,
4839             drr_begin, force, is_resumable, force_cksum, origin, &drc);
4840 
4841         if (error != 0)
4842                 goto out;
4843 
4844         drc.drc_krrp_task = krrp_task;
4845         /*
4846          * Set properties before we receive the stream so that they are applied
4847          * to the new data. Note that we must call dmu_recv_stream() if
4848          * dmu_recv_begin() succeeds.
4849          */
4850         if (props != NULL && !drc.drc_newfs) {
4851                 if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >=
4852                     SPA_VERSION_RECVD_PROPS &&
4853                     !dsl_prop_get_hasrecvd(tofs))
4854                         first_recvd_props = B_TRUE;
4855 
4856                 /*
4857                  * If new received properties are supplied, they are to
4858                  * completely replace the existing received properties, so stash
4859                  * away the existing ones.
4860                  */
4861                 if (dsl_prop_get_received(tofs, &origprops) == 0) {
4862                         nvlist_t *errlist = NULL;
4863                         /*
4864                          * Don't bother writing a property if its value won't
4865                          * change (and avoid the unnecessary security checks).
4866                          *
4867                          * The first receive after SPA_VERSION_RECVD_PROPS is a
4868                          * special case where we blow away all local properties
4869                          * regardless.
4870                          */
4871                         if (!first_recvd_props)
4872                                 props_reduce(props, origprops);
4873                         if (zfs_check_clearable(tofs, origprops, &errlist) != 0)
4874                                 (void) nvlist_merge(errors, errlist, 0);
4875                         nvlist_free(errlist);
4876 
4877                         if (clear_received_props(tofs, origprops,
4878                             first_recvd_props ? NULL : props) != 0)
4879                                 *errf |= ZPROP_ERR_NOCLEAR;
4880                 } else {
4881                         *errf |= ZPROP_ERR_NOCLEAR;
4882                 }
4883         }
4884 
4885         if (props != NULL) {
4886                 props_error = dsl_prop_set_hasrecvd(tofs);
4887 
4888                 if (props_error == 0) {
4889                         delayprops = extract_delay_props(props);
4890                         (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4891                             props, errors);
4892                 }
4893         }
4894 
4895         if (fp) {
4896                 off = fp->f_offset;
4897         } else {
4898                 off = 0;
4899         }
4900         error = dmu_recv_stream(&drc, fp ? fp->f_vnode : NULL,
4901             &off, cfd, ahdl, krrp_task);
4902 
4903         if (error == 0) {
4904                 zfsvfs_t *zfsvfs = NULL;
4905 
4906                 error = getzfsvfs(tofs, &zfsvfs);
4907                 if (error == 0) {
4908                         /* online recv */
4909                         dsl_dataset_t *ds;
4910                         int end_err;
4911 
4912                         ds = dmu_objset_ds(zfsvfs->z_os);
4913                         error = zfs_suspend_fs(zfsvfs);
4914                         /*
4915                          * If the suspend fails, then the recv_end will
4916                          * likely also fail, and clean up after itself.
4917                          */
4918                         end_err = dmu_recv_end(&drc, zfsvfs);
4919                         if (error == 0)
4920                                 error = zfs_resume_fs(zfsvfs, ds);
4921                         error = error ? error : end_err;
4922                         VFS_RELE(zfsvfs->z_vfs);
4923                 } else {
4924                         error = dmu_recv_end(&drc, NULL);
4925                 }
4926 
4927                 /* Set delayed properties now, after we're done receiving. */
4928                 if (delayprops != NULL && error == 0) {
4929                         (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED,
4930                             delayprops, errors);
4931                 }
4932         }
4933 
4934         if (delayprops != NULL) {
4935                 /*
4936                  * Merge delayed props back in with initial props, in case
4937                  * we're DEBUG and zfs_ioc_recv_inject_err is set (which means
4938                  * we have to make sure clear_received_props() includes
4939                  * the delayed properties).
4940                  *
4941                  * Since zfs_ioc_recv_inject_err is only in DEBUG kernels,
4942                  * using ASSERT() will be just like a VERIFY.
4943                  */
4944                 ASSERT(nvlist_merge(props, delayprops, 0) == 0);
4945                 nvlist_free(delayprops);
4946         }
4947 
4948         if (fp) {
4949                 *sz = off - fp->f_offset;
4950                 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
4951                         fp->f_offset = off;
4952         } else {
4953                 *sz = off;
4954         }
4955         if (error == 0) {
4956                 char val[MAXNAMELEN];
4957 
4958                 (void) strcpy(val, tofs);
4959                 (void) strcat(val, "@");
4960                 (void) strcat(val, tosnap);
4961 
4962                 event = fnvlist_alloc();
4963                 if (props != NULL)
4964                         fnvlist_add_nvlist(event, "props", props);
4965                 fnvlist_add_string(event, "origin", tofs);
4966                 fnvlist_add_string(event, "tosnap", val);
4967                 fnvlist_add_uint64(event, "bytes", *sz);
4968                 fnvlist_add_boolean_value(event, "newds", drc.drc_newfs);
4969                 zfs_event_post(ZFS_EC_STATUS, "recv", event);
4970         }
4971 
4972 #ifdef  DEBUG
4973         if (zfs_ioc_recv_inject_err) {
4974                 zfs_ioc_recv_inject_err = B_FALSE;
4975                 error = 1;
4976         }
4977 #endif
4978         /*
4979          * On error, restore the original props.
4980          */
4981         if (error != 0 && props != NULL && !drc.drc_newfs) {
4982                 if (clear_received_props(tofs, props, NULL) != 0) {
4983                         /*
4984                          * We failed to clear the received properties.
4985                          * Since we may have left a $recvd value on the
4986                          * system, we can't clear the $hasrecvd flag.
4987                          */
4988                         *errf |= ZPROP_ERR_NORESTORE;
4989                 } else if (first_recvd_props) {
4990                         dsl_prop_unset_hasrecvd(tofs);
4991                 }
4992 
4993                 if (origprops == NULL && !drc.drc_newfs) {
4994                         /* We failed to stash the original properties. */
4995                         *errf |= ZPROP_ERR_NORESTORE;
4996                 }
4997 
4998                 /*
4999                  * dsl_props_set() will not convert RECEIVED to LOCAL on or
5000                  * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL
5001                  * explictly if we're restoring local properties cleared in the
5002                  * first new-style receive.
5003                  */
5004                 if (origprops != NULL &&
5005                     zfs_set_prop_nvlist(tofs, (first_recvd_props ?
5006                     ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED),
5007                     origprops, NULL) != 0) {
5008                         /*
5009                          * We stashed the original properties but failed to
5010                          * restore them.
5011                          */
5012                         *errf |= ZPROP_ERR_NORESTORE;
5013                 }
5014         }
5015 out:
5016         nvlist_free(origprops);
5017         if (fp)
5018                 releasef(fd);
5019 
5020         if (error == 0)
5021                 error = props_error;
5022 
5023         return (error);
5024 }
5025 
5026 /*
5027  * inputs:
5028  * zc_name              name of containing filesystem
5029  * zc_nvlist_src{_size} nvlist of properties to apply
5030  * zc_value             name of snapshot to create
5031  * zc_string            name of clone origin (if DRR_FLAG_CLONE)
5032  * zc_cookie            file descriptor to recv from
5033  * zc_begin_record      the BEGIN record of the stream (not byteswapped)
5034  * zc_guid              force flag
5035  * zc_cleanup_fd        cleanup-on-exit file descriptor
5036  * zc_action_handle     handle for this guid/ds mapping (or zero on first call)
5037  * zc_resumable         if data is incomplete assume sender will resume
5038  *
5039  * outputs:
5040  * zc_cookie            number of bytes read
5041  * zc_nvlist_dst{_size} error for each unapplied received property
5042  * zc_obj               zprop_errflags_t
5043  * zc_action_handle     handle for this guid/ds mapping
5044  */
5045 static int
5046 zfs_ioc_recv(zfs_cmd_t *zc)
5047 {
5048         int fd = zc->zc_cookie;
5049         char tofs[ZFS_MAX_DATASET_NAME_LEN];
5050         char *tosnap;
5051         char *origin = NULL;
5052         nvlist_t *errors;
5053         nvlist_t *props = NULL; /* sent properties */
5054         boolean_t force = (boolean_t)zc->zc_guid;
5055         int err;
5056 
5057         if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 ||
5058             strchr(zc->zc_value, '@') == NULL ||
5059             strchr(zc->zc_value, '%'))
5060                 return (SET_ERROR(EINVAL));
5061 
5062         (void) strcpy(tofs, zc->zc_value);
5063         tosnap = strchr(tofs, '@');
5064         *tosnap++ = '\0';
5065 
5066         if (zc->zc_string[0])
5067                 origin = zc->zc_string;
5068 
5069         if (zc->zc_nvlist_src != NULL &&
5070             (err = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
5071             zc->zc_iflags, &props)) != 0)
5072                 return (err);
5073 
5074         errors = fnvlist_alloc();
5075 
5076         err = dmu_recv_impl(fd, tofs, tosnap, origin,
5077             &zc->zc_begin_record, zc->zc_resumable, props, errors, &zc->zc_obj,
5078             zc->zc_cleanup_fd, &zc->zc_action_handle, &zc->zc_cookie,
5079             force, NULL);
5080 
5081         /*
5082          * Now that all props, initial and delayed, are set, report the prop
5083          * errors to the caller.
5084          */
5085         if (zc->zc_nvlist_dst_size != 0 &&
5086             (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 ||
5087             put_nvlist(zc, errors) != 0)) {
5088                 /*
5089                  * Caller made zc->zc_nvlist_dst less than the minimum expected
5090                  * size or supplied an invalid address.
5091                  */
5092                 err = SET_ERROR(EINVAL);
5093         }
5094 
5095         nvlist_free(errors);
5096         nvlist_free(props);
5097         return (err);
5098 
5099 }
5100 
5101 /*
5102  * inputs:
5103  * zc_name      name of snapshot to send
5104  * zc_cookie    file descriptor to send stream to
5105  * zc_obj       fromorigin flag (mutually exclusive with zc_fromobj)
5106  * zc_sendobj   objsetid of snapshot to send
5107  * zc_fromobj   objsetid of incremental fromsnap (may be zero)
5108  * zc_guid      if set, estimate size of stream only.  zc_cookie is ignored.
5109  *              output size in zc_objset_type.
5110  * zc_flags     lzc_send_flags
5111  *
5112  * outputs:
5113  * zc_objset_type       estimated size, if zc_guid is set
5114  */
5115 static int
5116 zfs_ioc_send(zfs_cmd_t *zc)
5117 {
5118         int error;
5119         offset_t off;
5120         boolean_t estimate = (zc->zc_guid != 0);
5121         boolean_t embedok = (zc->zc_flags & 0x1);
5122         boolean_t large_block_ok = (zc->zc_flags & 0x2);
 
5159                 }
5160 
5161                 if (zc->zc_fromobj != 0) {
5162                         error = dsl_dataset_hold_obj(dp, zc->zc_fromobj,
5163                             FTAG, &fromsnap);
5164                         if (error != 0) {
5165                                 dsl_dataset_rele(tosnap, FTAG);
5166                                 dsl_pool_rele(dp, FTAG);
5167                                 return (error);
5168                         }
5169                 }
5170 
5171                 error = dmu_send_estimate(tosnap, fromsnap, compressok,
5172                     &zc->zc_objset_type);
5173 
5174                 if (fromsnap != NULL)
5175                         dsl_dataset_rele(fromsnap, FTAG);
5176                 dsl_dataset_rele(tosnap, FTAG);
5177                 dsl_pool_rele(dp, FTAG);
5178         } else {
5179                 offset_t off_starting;
5180                 file_t *fp = getf(zc->zc_cookie);
5181                 if (fp == NULL)
5182                         return (SET_ERROR(EBADF));
5183 
5184                 off_starting = off = fp->f_offset;
5185                 error = dmu_send_obj(zc->zc_name, zc->zc_sendobj,
5186                     zc->zc_fromobj, embedok, large_block_ok, compressok,
5187                     zc->zc_cookie, fp->f_vnode, &off, zc->zc_sendsize);
5188 
5189                 zc->zc_sendcounter = off - off_starting;
5190                 if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0)
5191                         fp->f_offset = off;
5192                 releasef(zc->zc_cookie);
5193         }
5194         return (error);
5195 }
5196 
5197 /*
5198  * inputs:
5199  * zc_name      name of snapshot on which to report progress
5200  * zc_cookie    file descriptor of send stream
5201  *
5202  * outputs:
5203  * zc_cookie    number of bytes written in send stream thus far
5204  */
5205 static int
5206 zfs_ioc_send_progress(zfs_cmd_t *zc)
5207 {
5208         dsl_pool_t *dp;
5209         dsl_dataset_t *ds;
 
5404 
5405         (void) spa_vdev_state_exit(spa, NULL, 0);
5406         spa_close(spa, FTAG);
5407         return (0);
5408 }
5409 /*
5410  * inputs:
5411  * zc_name      name of filesystem
5412  *
5413  * outputs:
5414  * zc_string    name of conflicting snapshot, if there is one
5415  */
5416 static int
5417 zfs_ioc_promote(zfs_cmd_t *zc)
5418 {
5419         dsl_pool_t *dp;
5420         dsl_dataset_t *ds, *ods;
5421         char origin[ZFS_MAX_DATASET_NAME_LEN];
5422         char *cp;
5423         int error;
5424         nvlist_t *event;
5425 
5426         error = dsl_pool_hold(zc->zc_name, FTAG, &dp);
5427         if (error != 0)
5428                 return (error);
5429 
5430         error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds);
5431         if (error != 0) {
5432                 dsl_pool_rele(dp, FTAG);
5433                 return (error);
5434         }
5435 
5436         if (!dsl_dir_is_clone(ds->ds_dir)) {
5437                 dsl_dataset_rele(ds, FTAG);
5438                 dsl_pool_rele(dp, FTAG);
5439                 return (SET_ERROR(EINVAL));
5440         }
5441 
5442         error = dsl_dataset_hold_obj(dp,
5443             dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods);
5444         if (error != 0) {
5445                 dsl_dataset_rele(ds, FTAG);
5446                 dsl_pool_rele(dp, FTAG);
5447                 return (error);
5448         }
5449 
5450         dsl_dataset_name(ods, origin);
5451         dsl_dataset_rele(ods, FTAG);
5452         dsl_dataset_rele(ds, FTAG);
5453         dsl_pool_rele(dp, FTAG);
5454 
5455         /*
5456          * We don't need to unmount *all* the origin fs's snapshots, but
5457          * it's easier.
5458          */
5459         cp = strchr(origin, '@');
5460         if (cp)
5461                 *cp = '\0';
5462         (void) dmu_objset_find(origin,
5463             zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS);
5464         error = dsl_dataset_promote(zc->zc_name, zc->zc_string);
5465 
5466         if (error == 0) {
5467                 event = fnvlist_alloc();
5468                 fnvlist_add_string(event, "fsname", zc->zc_name);
5469                 fnvlist_add_string(event, "origin", zc->zc_value);
5470                 zfs_event_post(ZFS_EC_STATUS, "promote", event);
5471         }
5472 
5473         return (error);
5474 }
5475 
5476 /*
5477  * Retrieve a single {user|group}{used|quota}@... property.
5478  *
5479  * inputs:
5480  * zc_name      name of filesystem
5481  * zc_objset_type zfs_userquota_prop_t
5482  * zc_value     domain name (eg. "S-1-234-567-89")
5483  * zc_guid      RID/UID/GID
5484  *
5485  * outputs:
5486  * zc_cookie    property value
5487  */
5488 static int
5489 zfs_ioc_userspace_one(zfs_cmd_t *zc)
5490 {
5491         zfsvfs_t *zfsvfs;
5492         int error;
5493 
 
6134         if (error == 0 && !old->ds_is_snapshot) {
6135                 dsl_dataset_rele(old, FTAG);
6136                 error = SET_ERROR(EINVAL);
6137         }
6138         if (error != 0) {
6139                 dsl_dataset_rele(new, FTAG);
6140                 dsl_pool_rele(dp, FTAG);
6141                 return (error);
6142         }
6143 
6144         error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp);
6145         dsl_dataset_rele(old, FTAG);
6146         dsl_dataset_rele(new, FTAG);
6147         dsl_pool_rele(dp, FTAG);
6148         fnvlist_add_uint64(outnvl, "used", used);
6149         fnvlist_add_uint64(outnvl, "compressed", comp);
6150         fnvlist_add_uint64(outnvl, "uncompressed", uncomp);
6151         return (error);
6152 }
6153 
6154 static int
6155 zfs_ioc_vdev_set_props(zfs_cmd_t *zc)
6156 {
6157         nvlist_t *props;
6158         spa_t *spa;
6159         int error;
6160         uint64_t vdev_guid = zc->zc_guid;
6161 
6162         if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6163             zc->zc_iflags, &props))
6164                 return (error);
6165 
6166         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
6167                 nvlist_free(props);
6168                 return (error);
6169         }
6170 
6171         error = spa_vdev_prop_set(spa, vdev_guid, props);
6172 
6173         nvlist_free(props);
6174         spa_close(spa, FTAG);
6175 
6176         return (error);
6177 }
6178 
6179 static int
6180 zfs_ioc_vdev_get_props(zfs_cmd_t *zc)
6181 {
6182         spa_t *spa;
6183         uint64_t vdev_guid = zc->zc_guid;
6184         nvlist_t *nvp = NULL;
6185         int error;
6186 
6187         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
6188                 /*
6189                  * If the pool is faulted, there may be properties we can still
6190                  * get (such as altroot and cachefile), so attempt to get them
6191                  * anyway.
6192                  */
6193                 mutex_enter(&spa_namespace_lock);
6194                 if ((spa = spa_lookup(zc->zc_name)) != NULL)
6195                         error = spa_vdev_prop_get(spa, vdev_guid, &nvp);
6196                 mutex_exit(&spa_namespace_lock);
6197         } else {
6198                 error = spa_vdev_prop_get(spa, vdev_guid, &nvp);
6199                 spa_close(spa, FTAG);
6200         }
6201 
6202         if (error == 0 && zc->zc_nvlist_dst != NULL)
6203                 error = put_nvlist(zc, nvp);
6204         else
6205                 error = EFAULT;
6206 
6207         nvlist_free(nvp);
6208         return (error);
6209 }
6210 
6211 static int
6212 zfs_ioc_cos_alloc(zfs_cmd_t *zc)
6213 {
6214         nvlist_t *props;
6215         spa_t *spa;
6216         int error;
6217 
6218         if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6219             zc->zc_iflags, &props))
6220                 return (error);
6221 
6222         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
6223                 nvlist_free(props);
6224                 return (error);
6225         }
6226 
6227         error = spa_alloc_cos(spa, zc->zc_string, 0);
6228         if (!error)
6229                 error = spa_cos_prop_set(spa, zc->zc_string, props);
6230 
6231         spa_close(spa, FTAG);
6232         nvlist_free(props);
6233 
6234         return (error);
6235 }
6236 
6237 static int
6238 zfs_ioc_cos_free(zfs_cmd_t *zc)
6239 {
6240         spa_t *spa;
6241         int error = 0;
6242 
6243         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
6244                 return (error);
6245 
6246         error = spa_free_cos(spa, zc->zc_string, zc->zc_cookie);
6247 
6248         spa_close(spa, FTAG);
6249 
6250         return (error);
6251 }
6252 
6253 static int
6254 zfs_ioc_cos_list(zfs_cmd_t *zc)
6255 {
6256         spa_t *spa;
6257         nvlist_t *nvl;
6258         int error = 0;
6259 
6260         VERIFY(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP) == 0);
6261 
6262         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
6263                 nvlist_free(nvl);
6264                 return (error);
6265         }
6266 
6267         error = spa_list_cos(spa, nvl);
6268 
6269         if (error == 0 && zc->zc_nvlist_dst != NULL)
6270                 error = put_nvlist(zc, nvl);
6271 
6272         spa_close(spa, FTAG);
6273         nvlist_free(nvl);
6274 
6275         return (error);
6276 }
6277 
6278 static int
6279 zfs_ioc_cos_set_props(zfs_cmd_t *zc)
6280 {
6281         nvlist_t *props;
6282         spa_t *spa;
6283         cos_t *cos;
6284         const char *cosname = NULL;
6285         int error = 0;
6286 
6287         if ((zc->zc_string == NULL || zc->zc_string[0] == '\0') &&
6288             zc->zc_guid == 0)
6289                 return (SET_ERROR(EINVAL));
6290 
6291         if (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size,
6292             zc->zc_iflags, &props))
6293                 return (error);
6294 
6295         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) {
6296                 nvlist_free(props);
6297                 return (error);
6298         }
6299 
6300         if (zc->zc_guid == 0) {
6301                 cosname = zc->zc_string;
6302         } else {
6303                 spa_cos_enter(spa);
6304                 cos = spa_lookup_cos_by_guid(spa, zc->zc_guid);
6305                 if (cos != NULL)
6306                         cosname = cos->cos_name;
6307                 else
6308                         error = SET_ERROR(ENOENT);
6309                 spa_cos_exit(spa);
6310         }
6311 
6312         if (error == 0)
6313                 error = spa_cos_prop_set(spa, cosname, props);
6314 
6315         spa_close(spa, FTAG);
6316         nvlist_free(props);
6317 
6318         return (error);
6319 }
6320 
6321 static int
6322 zfs_ioc_cos_get_props(zfs_cmd_t *zc)
6323 {
6324         spa_t *spa;
6325         cos_t *cos;
6326         nvlist_t *nvp = NULL;
6327         const char *cosname = NULL;
6328         int error = 0;
6329 
6330         if ((zc->zc_string == NULL || zc->zc_string[0] == '\0') &&
6331             zc->zc_guid == 0)
6332                 return (SET_ERROR(EINVAL));
6333 
6334         if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0)
6335                 return (error);
6336 
6337         if (zc->zc_guid == 0) {
6338                 cosname = zc->zc_string;
6339         } else {
6340                 spa_cos_enter(spa);
6341                 cos = spa_lookup_cos_by_guid(spa, zc->zc_guid);
6342                 if (cos != NULL)
6343                         cosname = cos->cos_name;
6344                 spa_cos_exit(spa);
6345         }
6346 
6347         if (error == 0)
6348                 error = spa_cos_prop_get(spa, cosname, &nvp);
6349 
6350         spa_close(spa, FTAG);
6351 
6352         if (error == 0 && zc->zc_nvlist_dst != NULL)
6353                 error = put_nvlist(zc, nvp);
6354         else
6355                 error = EFAULT;
6356 
6357         nvlist_free(nvp);
6358         return (error);
6359 }
6360 
6361 /*
6362  * innvl: {
6363  *     "fd" -> file descriptor to write stream to (int32)
6364  *     (optional) "fromsnap" -> full snap name to send an incremental from
6365  *     (optional) "largeblockok" -> (value ignored)
6366  *         indicates that blocks > 128KB are permitted
6367  *     (optional) "embedok" -> (value ignored)
6368  *         presence indicates DRR_WRITE_EMBEDDED records are permitted
6369  *     (optional) "compressok" -> (value ignored)
6370  *         presence indicates compressed DRR_WRITE records are permitted
6371  *     (optional) "resume_object" and "resume_offset" -> (uint64)
6372  *         if present, resume send stream from specified object and offset.
6373  * }
6374  *
6375  * outnvl is unused
6376  */
6377 /* ARGSUSED */
6378 static int
6379 zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl)
6380 {
 
6491                          * bookmark
6492                          */
6493                         error = SET_ERROR(EINVAL);
6494                         goto out;
6495                 }
6496         } else {
6497                 /*
6498                  * If estimating the size of a full send, use dmu_send_estimate.
6499                  */
6500                 error = dmu_send_estimate(tosnap, NULL, compressok, &space);
6501         }
6502 
6503         fnvlist_add_uint64(outnvl, "space", space);
6504 
6505 out:
6506         dsl_dataset_rele(tosnap, FTAG);
6507         dsl_pool_rele(dp, FTAG);
6508         return (error);
6509 }
6510 
6511 typedef struct dp_cursor_cb_arg {
6512         nvlist_t *outnvl;
6513         uint64_t offset;
6514         uint32_t count;
6515         uint32_t skip;
6516         boolean_t verbose;
6517         boolean_t snaps;
6518 } dp_cursor_cb_arg_t;
6519 
6520 /* ARGSUSED */
6521 int
6522 ds_cursor_cb(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg)
6523 {
6524         int error;
6525         char dsname[MAXNAMELEN];
6526         objset_t *osp;
6527 
6528         dp_cursor_cb_arg_t *cb = (dp_cursor_cb_arg_t *)arg;
6529 
6530         dsl_dataset_name(ds, dsname);
6531         nvlist_t *nv = fnvlist_alloc();
6532 
6533         fnvlist_add_uint64(nv, zfs_prop_to_name(ZFS_PROP_GUID),
6534             dsl_dataset_phys(ds)->ds_guid);
6535 
6536         if (cb->verbose) {
6537                 uint64_t refd, avail, uobjs, aobjs;
6538                 dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
6539 
6540                 fnvlist_add_uint64(nv, zfs_prop_to_name(ZFS_PROP_AVAILABLE),
6541                     avail);
6542                 fnvlist_add_uint64(nv, zfs_prop_to_name(ZFS_PROP_REFERENCED),
6543                     refd);
6544                 fnvlist_add_uint64(nv, zfs_prop_to_name(ZFS_PROP_USED),
6545                     dsl_dir_phys(ds->ds_dir)->dd_used_bytes);
6546         }
6547 
6548         error = dmu_objset_from_ds(ds, &osp);
6549 
6550         if (error == 0)
6551                 fnvlist_add_uint64(nv, zfs_prop_to_name(ZFS_PROP_TYPE),
6552                     dmu_objset_type(osp));
6553 
6554         fnvlist_add_nvlist(cb->outnvl, dsname, nv);
6555         nvlist_free(nv);
6556         return (error);
6557 }
6558 
6559 int
6560 dmu_objset_find_dp_cursor(dsl_pool_t *dp, uint64_t ddobj,
6561     int func(dsl_pool_t *, dsl_dataset_t *, void *), void *arg)
6562 {
6563         dsl_dir_t *dd;
6564         dsl_dataset_t *ds;
6565         zap_cursor_t zc;
6566         zap_attribute_t *attr;
6567         uint64_t thisobj;
6568         int error, i;
6569 
6570         dp_cursor_cb_arg_t *cb = (dp_cursor_cb_arg_t *)arg;
6571 
6572         ASSERT(dsl_pool_config_held(dp));
6573         error = dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd);
6574         thisobj = dsl_dir_phys(dd)->dd_head_dataset_obj;
6575 
6576         if (error != 0)
6577                 return (error);
6578 
6579         attr = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
6580 
6581         /* we are interrestd in filesytems and volumes */
6582         if (!cb->snaps) {
6583 
6584                 /* init the cursor at given offset */
6585                 zap_cursor_init_serialized(&zc, dp->dp_meta_objset,
6586                     dsl_dir_phys(dd)->dd_child_dir_zapobj, cb->offset);
6587 
6588 
6589                 for (i = 0; i < cb->skip; i++) {
6590                         zap_cursor_advance(&zc);
6591                         if ((zap_cursor_retrieve(&zc, attr) != 0)) {
6592                                 error = ENOENT;
6593                                 goto out;
6594                         }
6595                 }
6596 
6597                 for (i = 0; i < cb->count; i++) {
6598                         zap_cursor_advance(&zc);
6599                         if ((zap_cursor_retrieve(&zc, attr) != 0)) {
6600                                 error = ENOENT;
6601                                 goto out;
6602                         }
6603 
6604                         ASSERT3U(attr->za_integer_length, ==,
6605                             sizeof (uint64_t));
6606                         ASSERT3U(attr->za_num_integers, ==, 1);
6607                         /* recursivly walk objects skipping $MOS and $ORIGIN */
6608                         error = dmu_objset_find_dp(dp, attr->za_first_integer,
6609                             func, arg, 0);
6610                         if (error != 0)
6611                                 break;
6612                 }
6613         } else {
6614 
6615                 dsl_dataset_t *ds;
6616 
6617                 error = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds);
6618 
6619                 if (error == 0) {
6620 
6621                         dsl_dataset_rele(ds, FTAG);
6622                         zap_cursor_init_serialized(&zc, dp->dp_meta_objset,
6623                             dsl_dataset_phys(ds)->ds_snapnames_zapobj,
6624                             cb->offset);
6625 
6626                         for (i = 0; i < cb->skip; i++) {
6627                                 zap_cursor_advance(&zc);
6628                                         if ((zap_cursor_retrieve(&zc,
6629                                             attr) != 0)) {
6630                                         error = ENOENT;
6631                                         goto out;
6632                                 }
6633                         }
6634 
6635                         for (i = 0; i < cb->count; i++) {
6636                                 zap_cursor_advance(&zc);
6637                                 if ((zap_cursor_retrieve(&zc, attr) != 0)) {
6638                                         error = ENOENT;
6639                                         goto out;
6640 
6641                                 }
6642 
6643                                 ASSERT3U(attr->za_integer_length, ==,
6644                                     sizeof (uint64_t));
6645                                 ASSERT3U(attr->za_num_integers, ==, 1);
6646 
6647                                 error = dsl_dataset_hold_obj(dp,
6648                                     attr->za_first_integer, FTAG, &ds);
6649                                 if (error != 0)
6650                                         break;
6651                                 error = func(dp, ds, arg);
6652                                 dsl_dataset_rele(ds, FTAG);
6653                                 if (error != 0)
6654                                         break;
6655                         }
6656                 }
6657         }
6658 out:
6659         cb->offset = zap_cursor_serialize(&zc);
6660         zap_cursor_fini(&zc);
6661         dsl_dir_rele(dd, FTAG);
6662         kmem_free(attr, sizeof (zap_attribute_t));
6663 
6664         /* return self as the last dataset */
6665         if (error == ENOENT) {
6666                 if ((error = dsl_dataset_hold_obj(dp, thisobj, FTAG, &ds)) != 0)
6667                         return (error);
6668                 error = func(dp, ds, arg);
6669                 dsl_dataset_rele(ds, FTAG);
6670                 if (error)
6671                         return (error);
6672                 error = ENOENT;
6673         }
6674 
6675         return (error);
6676 }
6677 
6678 
6679 /*
6680  * We want to list all dataset under the given name. Optionally, we advance the
6681  * ZAP cursor "skip" times and retrieve "count" datasets. We return the offset
6682  * so the user can start the next invocation where he left off.
6683  */
6684 
6685 static int
6686 zfs_ioc_list_from_cursor(const char *name, nvlist_t *innvl, nvlist_t *outnvl)
6687 {
6688 
6689         dsl_pool_t *dp;
6690         dsl_dataset_t *ds;
6691 
6692         int error;
6693 
6694         dp_cursor_cb_arg_t cb_args;
6695 
6696         if ((strchr(name, '@') != NULL))
6697                 return (EINVAL);
6698 
6699         if ((error = dsl_pool_hold(name, FTAG, &dp)) != 0)
6700                 return (error);
6701 
6702         if ((error = dsl_dataset_hold(dp, name, FTAG, &ds)) != 0) {
6703                 dsl_pool_rele(dp, FTAG);
6704                 return (error);
6705         }
6706 
6707         (void) nvlist_lookup_uint32(innvl, "count", &cb_args.count);
6708         (void) nvlist_lookup_uint32(innvl, "skip", &cb_args.skip);
6709         (void) nvlist_lookup_uint64(innvl, "offset", &cb_args.offset);
6710         (void) nvlist_lookup_boolean_value(innvl, "verbose", &cb_args.verbose);
6711         (void) nvlist_lookup_boolean_value(innvl, "snaps", &cb_args.snaps);
6712 
6713         cb_args.outnvl = outnvl;
6714         error = dmu_objset_find_dp_cursor(dp, ds->ds_dir->dd_object,
6715             &ds_cursor_cb, &cb_args);
6716 
6717         fnvlist_add_uint64(outnvl, "offset", cb_args.offset);
6718         dsl_dataset_rele(ds, FTAG);
6719         dsl_pool_rele(dp, FTAG);
6720 
6721         return (error);
6722 }
6723 
6724 
6725 static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST];
6726 
6727 static void
6728 zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6729     zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck,
6730     boolean_t log_history, zfs_ioc_poolcheck_t pool_check)
6731 {
6732         zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST];
6733 
6734         ASSERT3U(ioc, >=, ZFS_IOC_FIRST);
6735         ASSERT3U(ioc, <, ZFS_IOC_LAST);
6736         ASSERT3P(vec->zvec_legacy_func, ==, NULL);
6737         ASSERT3P(vec->zvec_func, ==, NULL);
6738 
6739         vec->zvec_legacy_func = func;
6740         vec->zvec_secpolicy = secpolicy;
6741         vec->zvec_namecheck = namecheck;
6742         vec->zvec_allow_log = log_history;
6743         vec->zvec_pool_check = pool_check;
6744 }
 
6810 {
6811         zfs_ioctl_register_legacy(ioc, func, secpolicy,
6812             DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED);
6813 }
6814 
6815 static void
6816 zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func)
6817 {
6818         zfs_ioctl_register_dataset_read_secpolicy(ioc, func,
6819             zfs_secpolicy_read);
6820 }
6821 
6822 static void
6823 zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func,
6824     zfs_secpolicy_func_t *secpolicy)
6825 {
6826         zfs_ioctl_register_legacy(ioc, func, secpolicy,
6827             DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY);
6828 }
6829 
6830 /*
6831  * Appearing to take poolname as a parameter is a concession to the ioctl
6832  * handler. Leading underbar for generation idea nvpair exists only on output to
6833  * avoid pool name conflict.
6834  */
6835 /* ARGSUSED */
6836 static int
6837 zfs_ioc_pool_configs_nvl(const char *poolname, nvlist_t *innvl,
6838     nvlist_t *outnvl)
6839 {
6840         nvlist_t *configs;
6841         uint64_t generation;
6842 
6843         if (nvlist_lookup_uint64(innvl, "generation", &generation) != 0)
6844                 return (SET_ERROR(EINVAL));
6845 
6846         if ((configs = spa_all_configs(&generation)) == NULL)
6847                 return (SET_ERROR(EEXIST));
6848 
6849         fnvlist_merge(outnvl, configs);
6850         nvlist_free(configs);
6851         fnvlist_add_uint64(outnvl, "_generation", generation);
6852 
6853         return (0);
6854 }
6855 
6856 /*
6857  * Ask spa for pool statistics. If we get a non-NULL config but a non-zero
6858  * return from spa, we return EAGAIN to hint to callers that we've retrieved
6859  * a config for a faulted pool. We take no arguments but declare otherwise to
6860  * suit the ioctl handler's pattern. Similar considerations apply to outnvl as a
6861  * single pointer that has to be merged with config allocated or nulled by spa.
6862  */
6863 static int
6864 zfs_ioc_pool_stats_nvl(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl)
6865 {
6866         nvlist_t *config;
6867         int error;
6868         int ret = 0;
6869 
6870         ASSERT3P(innvl, ==, NULL);
6871         error = spa_get_stats(poolname, &config, NULL, 0);
6872         ASSERT3U(error, !=, EAGAIN);
6873 
6874         if (config != NULL) {
6875                 fnvlist_merge(outnvl, config);
6876                 nvlist_free(config);
6877                 if (error)
6878                         ret = SET_ERROR(EAGAIN);
6879         } else {
6880                 ret = error;
6881         }
6882 
6883         return (ret);
6884 }
6885 
6886 static nvlist_t *
6887 objset_stats2nv(dmu_objset_stats_t *stat)
6888 {
6889         nvlist_t *statlist  = fnvlist_alloc();
6890 
6891         fnvlist_add_uint64(statlist, "dds_num_clones", stat->dds_num_clones);
6892         fnvlist_add_uint64(statlist, "dds_creation_txg",
6893             stat->dds_creation_txg);
6894         fnvlist_add_uint64(statlist, "dds_guid", stat->dds_guid);
6895         fnvlist_add_uint8(statlist, "dds_type", (uint8_t)stat->dds_type);
6896         fnvlist_add_uint8(statlist, "dds_is_snapshot", stat->dds_is_snapshot);
6897         fnvlist_add_uint8(statlist, "dds_inconsistent",
6898             stat->dds_inconsistent);
6899         fnvlist_add_string(statlist, "dds_origin", stat->dds_origin);
6900 
6901         return (statlist);
6902 }
6903 
6904 /* Given an objset, retrieve stats and props by adding them to the output nvl */
6905 static int
6906 objset_render(objset_t *os, nvlist_t *outnvl)
6907 {
6908         int error = 0;
6909         nvlist_t *props = NULL, *statlist = NULL;
6910         dmu_objset_stats_t stats;
6911 
6912         dmu_objset_fast_stat(os, &stats);
6913 
6914         if ((error = dsl_prop_get_all(os, &props)) == 0) {
6915                 dmu_objset_stats(os, props);
6916                 /*
6917                  * NB: zvol_get_stats() will read the objset contents,
6918                  * which we aren't supposed to do with a
6919                  * DS_MODE_USER hold, because it could be
6920                  * inconsistent.  So this is a bit of a workaround...
6921                  * XXX reading with out owning
6922                  */
6923                 if (!stats.dds_inconsistent &&
6924                     dmu_objset_type(os) == DMU_OST_ZVOL) {
6925                         error = zvol_get_stats(os, props);
6926                         if (error == EIO)
6927                                 goto out;
6928                         VERIFY0(error);
6929                 }
6930                 fnvlist_add_nvlist(outnvl, "props", props);
6931                 statlist = objset_stats2nv(&stats);
6932                 fnvlist_add_nvlist(outnvl, "stats", statlist);
6933                 nvlist_free(statlist);
6934         }
6935 
6936 out:
6937         nvlist_free(props);
6938         return (error);
6939 }
6940 
6941 /*
6942  * Note: this IOC can be called internally by other IOCs as an existence
6943  * check against race conditions. Given a dataset name, return its stats
6944  * and props. Optionally we can verify type, which simplifies things for
6945  * callers that may not want to parse stats for themselves (and may discard
6946  * the outnvl in handlers).
6947  */
6948 static int
6949 zfs_ioc_objset_stats_nvl(const char *data, nvlist_t *innvl, nvlist_t *outnvl)
6950 {
6951         objset_t *os;
6952         int error;
6953         dmu_objset_type_t checktype = DMU_OST_ANY;
6954         boolean_t gettype = B_FALSE;
6955 
6956         if (innvl != NULL) {
6957                 if (nvlist_lookup_uint8(innvl, "type", (uint8_t *)&checktype)
6958                     == 0)
6959                         gettype = B_TRUE;
6960         }
6961         if ((error = dmu_objset_hold(data, FTAG, &os)) == 0) {
6962                 error = objset_render(os, outnvl);
6963                 dmu_objset_rele(os, FTAG);
6964 
6965                 if (error == 0) {
6966                         nvlist_t *statlist;
6967                         dmu_objset_type_t type;
6968                         statlist = fnvlist_lookup_nvlist(outnvl, "stats");
6969                         type = fnvlist_lookup_uint8_t(statlist, "dds_type");
6970                         if (checktype != DMU_OST_ANY && type != checktype) {
6971                                 error = EEXIST;
6972                                 fnvlist_remove(outnvl, "stats");
6973                                 fnvlist_remove(outnvl, "props");
6974                         }
6975                         if (gettype)
6976                                 fnvlist_add_uint8(outnvl, "type", type);
6977                 }
6978         }
6979 
6980         return (error);
6981 }
6982 
6983 /*
6984  * Given a dataset name and an innvl containing a DMU cursor offset, find the
6985  * next child dataset, and return its name, stats, and props and an updated
6986  * cursor.
6987  */
6988 static int
6989 zfs_ioc_dataset_list_next_nvl(const char *data, nvlist_t *innvl,
6990     nvlist_t *outnvl)
6991 {
6992         objset_t *os;
6993         int error;
6994         uint64_t off;
6995         char *p, *nextds;
6996         char name[MAXNAMELEN];
6997         size_t len;
6998         size_t orig_len = strlen(data);
6999 
7000         if (innvl == NULL ||
7001             nvlist_lookup_uint64(innvl, "offset", &off) != 0)
7002                 return (SET_ERROR(EINVAL));
7003 
7004         (void) strlcpy(name, data, sizeof (name));
7005 top:
7006         if (error = dmu_objset_hold(name, FTAG, &os)) {
7007                 if (error == ENOENT)
7008                         error = SET_ERROR(ESRCH);
7009                 return (error);
7010         }
7011 
7012         p = strrchr(name, '/');
7013         if (p == NULL || p[1] != '\0') {
7014                 if ((len = strlcat(name, "/", sizeof (name))) >= MAXNAMELEN) {
7015                         dmu_objset_rele(os, FTAG);
7016                         return (SET_ERROR(ESRCH));
7017                 }
7018         } else {
7019                 len = orig_len;
7020         }
7021         p = name + len;
7022 
7023         do {
7024                 error = dmu_dir_list_next(os, sizeof (name) - len, p, NULL,
7025                     &off);
7026                 if (error == ENOENT)
7027                         error = ESRCH;
7028         } while (error == 0 && dataset_name_hidden(name));
7029         dmu_objset_rele(os, FTAG);
7030 
7031         /*
7032          * If it's an internal dataset (ie. with a '$' in its name),
7033          * don't try to get stats for it, otherwise we'll return ENOENT.
7034          */
7035         if (error == 0 && strchr(name, '$') == NULL) {
7036                 error = zfs_ioc_objset_stats_nvl(name, NULL, outnvl);
7037                 if (error == ENOENT) {
7038                         /* We lost a race with destroy, get the next one. */
7039                         name[orig_len] = '\0';
7040                         goto top;
7041                 }
7042                 len = strlen(name) + 1;
7043                 nextds = kmem_alloc(len, KM_SLEEP);
7044                 (void) strlcpy(nextds, name, len);
7045                 fnvlist_add_string(outnvl, "nextds", (const char *)nextds);
7046                 fnvlist_add_uint64(outnvl, "offset", off);
7047         }
7048 
7049         return (error);
7050 }
7051 
7052 /*
7053  * Given a dataset name and a DMU cursor offset, find its next snapshot, and
7054  * return its name, props, and stats and an updated cursor offset.
7055  */
7056 static int
7057 zfs_ioc_snapshot_list_next_nvl(const char *data, nvlist_t *innvl,
7058     nvlist_t *outnvl)
7059 {
7060         objset_t *os;
7061         int error;
7062         uint64_t off, obj;
7063         char name[MAXNAMELEN], *nextsnap;
7064         size_t len;
7065 
7066         if (innvl == NULL ||
7067             nvlist_lookup_uint64(innvl, "offset", &off) != 0)
7068                 return (SET_ERROR(EINVAL));
7069 
7070         error = dmu_objset_hold(data, FTAG, &os);
7071         if (error != 0) {
7072                 return (error == ENOENT ? ESRCH : error);
7073         }
7074 
7075         /*
7076          * A dataset name of maximum length cannot have any snapshots,
7077          * so exit immediately.
7078          */
7079         (void) strlcpy(name, data, sizeof (name));
7080         if ((len = strlcat(name, "@", sizeof (name))) >= MAXNAMELEN) {
7081                 dmu_objset_rele(os, FTAG);
7082                 return (SET_ERROR(ESRCH));
7083         }
7084 
7085         /* Rest of name buffer is passed so snap ID can be appended. */
7086         error = dmu_snapshot_list_next(os, sizeof (name) - len, name + len,
7087             &obj, &off, NULL);
7088 
7089         if (error == 0) {
7090                 dsl_dataset_t *ds;
7091                 dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool;
7092 
7093                 error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
7094                 if (error == 0) {
7095                         objset_t *ossnap;
7096 
7097                         error = dmu_objset_from_ds(ds, &ossnap);
7098                         if (error == 0)
7099                                 error = objset_render(ossnap, outnvl);
7100                         dsl_dataset_rele(ds, FTAG);
7101                 }
7102         } else if (error == ENOENT) {
7103                 error = ESRCH;
7104         }
7105 
7106         dmu_objset_rele(os, FTAG);
7107 
7108         if (error == 0) {
7109                 len = strlen(name) + 1;
7110                 nextsnap = kmem_alloc(len, KM_SLEEP);
7111                 (void) strlcpy(nextsnap, name, len);
7112                 fnvlist_add_string(outnvl, "nextsnap", (const char *)nextsnap);
7113                 fnvlist_add_uint64(outnvl, "offset", off);
7114         }
7115         return (error);
7116 }
7117 
7118 static int
7119 zfs_ioc_pool_get_props_nvl(const char *poolname, nvlist_t *innvl,
7120     nvlist_t *outnvl)
7121 {
7122         spa_t *spa;
7123         int error;
7124         nvlist_t *props = NULL;
7125 
7126         ASSERT3P(innvl, ==, NULL);
7127         if ((error = spa_open(poolname, &spa, FTAG)) != 0) {
7128                 /*
7129                  * If the pool is faulted, there may be properties we can still
7130                  * get (such as altroot and cachefile), so attempt to get them
7131                  * anyway.
7132                  */
7133                 mutex_enter(&spa_namespace_lock);
7134                 if ((spa = spa_lookup(poolname)) != NULL)
7135                         error = spa_prop_get(spa, &props);
7136                 mutex_exit(&spa_namespace_lock);
7137         } else {
7138                 error = spa_prop_get(spa, &props);
7139                 spa_close(spa, FTAG);
7140         }
7141 
7142         if (props != NULL) {
7143                 fnvlist_merge(outnvl, props);
7144                 nvlist_free(props);
7145         } else {
7146                 ASSERT3S(error, !=, 0);
7147         }
7148 
7149         return (error);
7150 }
7151 
7152 /* ARGSUSED */
7153 static int
7154 zfs_ioc_check_krrp(const char *dataset, nvlist_t *innvl, nvlist_t *outnvl)
7155 {
7156         spa_t *spa;
7157         int err;
7158 
7159         /*
7160          * Here we use different way to open spa for the given pool,
7161          * because the pool maybe faulted
7162          */
7163 
7164         mutex_enter(&spa_namespace_lock);
7165         if ((spa = spa_lookup(dataset)) == NULL) {
7166                 mutex_exit(&spa_namespace_lock);
7167                 /* From KRRP side everything nice */
7168                 return (0);
7169         }
7170 
7171         spa_open_ref(spa, FTAG);
7172         mutex_exit(&spa_namespace_lock);
7173 
7174         err = autosnap_check_for_destroy(spa_get_autosnap(spa), dataset);
7175         if (err == 0)
7176                 err = ENOTSUP;
7177 
7178         mutex_enter(&spa_namespace_lock);
7179         spa_close(spa, FTAG);
7180         mutex_exit(&spa_namespace_lock);
7181 
7182         return (err != 0 ? SET_ERROR(err) : 0);
7183 }
7184 
7185 static void
7186 zfs_ioctl_init(void)
7187 {
7188         zfs_ioctl_register("bulk_list", ZFS_IOC_BULK_LIST,
7189             zfs_ioc_list_from_cursor, zfs_secpolicy_read,
7190             DATASET_NAME, POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
7191 
7192         zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT,
7193             zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME,
7194             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7195 
7196         zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY,
7197             zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME,
7198             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE);
7199 
7200         zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS,
7201             zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME,
7202             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
7203 
7204         zfs_ioctl_register("send", ZFS_IOC_SEND_NEW,
7205             zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME,
7206             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
7207 
7208         zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE,
7209             zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME,
7210             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
7211 
7212         zfs_ioctl_register("create", ZFS_IOC_CREATE,
7213             zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME,
7214             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7215 
7216         zfs_ioctl_register("clone", ZFS_IOC_CLONE,
7217             zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME,
7218             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7219 
7220         zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS,
7221             zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME,
7222             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7223 
7224         zfs_ioctl_register("check_krrp", ZFS_IOC_CHECK_KRRP,
7225             zfs_ioc_check_krrp, zfs_secpolicy_read, DATASET_NAME,
7226             POOL_CHECK_NONE, B_FALSE, B_FALSE);
7227 
7228         zfs_ioctl_register("pool_stats_nvl", ZFS_IOC_POOL_STATS_NVL,
7229             zfs_ioc_pool_stats_nvl, zfs_secpolicy_read, POOL_NAME,
7230             POOL_CHECK_NONE, B_FALSE, B_FALSE);
7231 
7232         zfs_ioctl_register("pool_configs_nvl", ZFS_IOC_POOL_CONFIGS_NVL,
7233             zfs_ioc_pool_configs_nvl, zfs_secpolicy_none, NO_NAME,
7234             POOL_CHECK_NONE, B_FALSE, B_FALSE);
7235 
7236         zfs_ioctl_register("pool_get_props_nvl", ZFS_IOC_POOL_GET_PROPS_NVL,
7237             zfs_ioc_pool_get_props_nvl, zfs_secpolicy_read, POOL_NAME,
7238             POOL_CHECK_NONE, B_FALSE, B_FALSE);
7239 
7240         zfs_ioctl_register("objset_stats_nvl", ZFS_IOC_OBJSET_STATS_NVL,
7241             zfs_ioc_objset_stats_nvl, zfs_secpolicy_read, DATASET_NAME,
7242             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
7243 
7244         zfs_ioctl_register("dataset_list_next_nvl",
7245             ZFS_IOC_DATASET_LIST_NEXT_NVL, zfs_ioc_dataset_list_next_nvl,
7246             zfs_secpolicy_read, DATASET_NAME, POOL_CHECK_SUSPENDED, B_FALSE,
7247             B_FALSE);
7248 
7249         zfs_ioctl_register("snapshot_list_next_nvl",
7250             ZFS_IOC_SNAPSHOT_LIST_NEXT_NVL, zfs_ioc_snapshot_list_next_nvl,
7251             zfs_secpolicy_read, DATASET_NAME, POOL_CHECK_SUSPENDED, B_FALSE,
7252             B_FALSE);
7253 
7254         zfs_ioctl_register("hold", ZFS_IOC_HOLD,
7255             zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME,
7256             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7257         zfs_ioctl_register("release", ZFS_IOC_RELEASE,
7258             zfs_ioc_release, zfs_secpolicy_release, POOL_NAME,
7259             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7260 
7261         zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS,
7262             zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME,
7263             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
7264 
7265         zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK,
7266             zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME,
7267             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE);
7268 
7269         zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK,
7270             zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME,
7271             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7272 
7273         zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS,
7274             zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME,
7275             POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE);
7276 
7277         zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS,
7278             zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks,
7279             POOL_NAME,
7280             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7281 
7282         zfs_ioctl_register("channel_program", ZFS_IOC_CHANNEL_PROGRAM,
7283             zfs_ioc_channel_program, zfs_secpolicy_config,
7284             POOL_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE,
7285             B_TRUE);
7286 
7287         zfs_ioctl_register("set_props_mds", ZFS_IOC_SET_PROPS_MDS,
7288             zfs_ioc_set_prop_mds, zfs_secpolicy_config,
7289             POOL_NAME,
7290             POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE);
7291 
7292         /* IOCTLS that use the legacy function signature */
7293 
7294         zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze,
7295             zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY);
7296 
7297         zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create,
7298             zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7299         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN,
7300             zfs_ioc_pool_scan);
7301         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_TRIM,
7302             zfs_ioc_pool_trim);
7303         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE,
7304             zfs_ioc_pool_upgrade);
7305         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD,
7306             zfs_ioc_vdev_add);
7307         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE,
7308             zfs_ioc_vdev_remove);
7309         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE,
7310             zfs_ioc_vdev_set_state);
7311         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH,
7312             zfs_ioc_vdev_attach);
7313         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH,
7314             zfs_ioc_vdev_detach);
7315         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETL2ADDDT,
7316             zfs_ioc_vdev_setl2adddt);
7317         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH,
7318             zfs_ioc_vdev_setpath);
7319         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU,
7320             zfs_ioc_vdev_setfru);
7321         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS,
7322             zfs_ioc_pool_set_props);
7323         zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT,
7324             zfs_ioc_vdev_split);
7325         zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID,
7326             zfs_ioc_pool_reguid);
7327 
7328         zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS,
7329             zfs_ioc_pool_configs, zfs_secpolicy_none);
7330         zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT,
7331             zfs_ioc_pool_tryimport, zfs_secpolicy_config);
7332         zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT,
7333             zfs_ioc_inject_fault, zfs_secpolicy_inject);
7334         zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT,
7335             zfs_ioc_clear_fault, zfs_secpolicy_inject);
7336         zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT,
 
7347             zfs_secpolicy_config, B_FALSE, POOL_CHECK_NONE);
7348 
7349         zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats,
7350             zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7351         zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props,
7352             zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE);
7353 
7354         zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log,
7355             zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED);
7356         zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME,
7357             zfs_ioc_dsobj_to_dsname,
7358             zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED);
7359         zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY,
7360             zfs_ioc_pool_get_history,
7361             zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED);
7362 
7363         zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import,
7364             zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7365 
7366         zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear,
7367             zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE);
7368         zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen,
7369             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7370         zfs_ioctl_register_pool(ZFS_IOC_VDEV_SET_PROPS, zfs_ioc_vdev_set_props,
7371             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7372         zfs_ioctl_register_pool(ZFS_IOC_VDEV_GET_PROPS, zfs_ioc_vdev_get_props,
7373             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7374         zfs_ioctl_register_pool(ZFS_IOC_COS_ALLOC, zfs_ioc_cos_alloc,
7375             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7376         zfs_ioctl_register_pool(ZFS_IOC_COS_FREE, zfs_ioc_cos_free,
7377             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7378         zfs_ioctl_register_pool(ZFS_IOC_COS_LIST, zfs_ioc_cos_list,
7379             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7380         zfs_ioctl_register_pool(ZFS_IOC_COS_SET_PROPS, zfs_ioc_cos_set_props,
7381             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7382         zfs_ioctl_register_pool(ZFS_IOC_COS_GET_PROPS, zfs_ioc_cos_get_props,
7383             zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED);
7384         zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN,
7385             zfs_ioc_space_written);
7386         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS,
7387             zfs_ioc_objset_recvd_props);
7388         zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ,
7389             zfs_ioc_next_obj);
7390         zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL,
7391             zfs_ioc_get_fsacl);
7392         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS,
7393             zfs_ioc_objset_stats);
7394         zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS,
7395             zfs_ioc_objset_zplprops);
7396         zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT,
7397             zfs_ioc_dataset_list_next);
7398         zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT,
7399             zfs_ioc_snapshot_list_next);
7400         zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS,
7401             zfs_ioc_send_progress);
7402 
7403         zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF,
 
 
7855         spa_init(FREAD | FWRITE);
7856         zfs_init();
7857         zvol_init();
7858         zfs_ioctl_init();
7859 
7860         if ((error = mod_install(&modlinkage)) != 0) {
7861                 zvol_fini();
7862                 zfs_fini();
7863                 spa_fini();
7864                 return (error);
7865         }
7866 
7867         tsd_create(&zfs_fsyncer_key, NULL);
7868         tsd_create(&rrw_tsd_key, rrw_tsd_destroy);
7869         tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy);
7870 
7871         error = ldi_ident_from_mod(&modlinkage, &zfs_li);
7872         ASSERT(error == 0);
7873         mutex_init(&zfs_share_lock, NULL, MUTEX_DEFAULT, NULL);
7874 
7875         if (sysevent_evc_bind(ZFS_EVENT_CHANNEL, &zfs_channel,
7876             EVCH_HOLD_PEND | EVCH_CREAT) != 0)
7877                 cmn_err(CE_NOTE, "Failed to bind to zfs event channel");
7878 
7879         return (0);
7880 }
7881 
7882 int
7883 _fini(void)
7884 {
7885         int error;
7886 
7887         if (spa_busy() || zfs_busy() || zvol_busy() || zio_injection_enabled)
7888                 return (SET_ERROR(EBUSY));
7889 
7890         if ((error = mod_remove(&modlinkage)) != 0)
7891                 return (error);
7892 
7893         zvol_fini();
7894         zfs_fini();
7895         spa_fini();
7896         if (zfs_nfsshare_inited)
7897                 (void) ddi_modclose(nfs_mod);
7898         if (zfs_smbshare_inited)
7899                 (void) ddi_modclose(smbsrv_mod);
7900         if (zfs_nfsshare_inited || zfs_smbshare_inited)
7901                 (void) ddi_modclose(sharefs_mod);
7902 
7903         tsd_destroy(&zfs_fsyncer_key);
7904         ldi_ident_release(zfs_li);
7905         zfs_li = NULL;
7906         mutex_destroy(&zfs_share_lock);
7907 
7908         if (zfs_channel)
7909                 (void) sysevent_evc_unbind(zfs_channel);
7910 
7911         return (error);
7912 }
7913 
7914 int
7915 _info(struct modinfo *modinfop)
7916 {
7917         return (mod_info(&modlinkage, modinfop));
7918 }
 |