Add linux mlslabel support
[zfs.git] / lib / libzfs / libzfs_dataset.c
index a3f5a7d..baf289b 100644 (file)
@@ -20,8 +20,7 @@
  */
 
 /*
- * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
- * Use is subject to license terms.
+ * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  */
 
 #include <ctype.h>
 #include <grp.h>
 #include <stddef.h>
 #include <ucred.h>
+#ifdef HAVE_IDMAP
 #include <idmap.h>
 #include <aclutils.h>
 #include <directory.h>
+#endif /* HAVE_IDMAP */
 
 #include <sys/dnode.h>
 #include <sys/spa.h>
@@ -73,60 +74,19 @@ zfs_type_to_name(zfs_type_t type)
                return (dgettext(TEXT_DOMAIN, "snapshot"));
        case ZFS_TYPE_VOLUME:
                return (dgettext(TEXT_DOMAIN, "volume"));
+       default:
+               break;
        }
 
        return (NULL);
 }
 
 /*
- * Given a path and mask of ZFS types, return a string describing this dataset.
- * This is used when we fail to open a dataset and we cannot get an exact type.
- * We guess what the type would have been based on the path and the mask of
- * acceptable types.
- */
-static const char *
-path_to_str(const char *path, int types)
-{
-       /*
-        * When given a single type, always report the exact type.
-        */
-       if (types == ZFS_TYPE_SNAPSHOT)
-               return (dgettext(TEXT_DOMAIN, "snapshot"));
-       if (types == ZFS_TYPE_FILESYSTEM)
-               return (dgettext(TEXT_DOMAIN, "filesystem"));
-       if (types == ZFS_TYPE_VOLUME)
-               return (dgettext(TEXT_DOMAIN, "volume"));
-
-       /*
-        * The user is requesting more than one type of dataset.  If this is the
-        * case, consult the path itself.  If we're looking for a snapshot, and
-        * a '@' is found, then report it as "snapshot".  Otherwise, remove the
-        * snapshot attribute and try again.
-        */
-       if (types & ZFS_TYPE_SNAPSHOT) {
-               if (strchr(path, '@') != NULL)
-                       return (dgettext(TEXT_DOMAIN, "snapshot"));
-               return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
-       }
-
-       /*
-        * The user has requested either filesystems or volumes.
-        * We have no way of knowing a priori what type this would be, so always
-        * report it as "filesystem" or "volume", our two primitive types.
-        */
-       if (types & ZFS_TYPE_FILESYSTEM)
-               return (dgettext(TEXT_DOMAIN, "filesystem"));
-
-       assert(types & ZFS_TYPE_VOLUME);
-       return (dgettext(TEXT_DOMAIN, "volume"));
-}
-
-/*
  * Validate a ZFS path.  This is used even before trying to open the dataset, to
  * provide a more meaningful error message.  We call zfs_error_aux() to
  * explain exactly why the name was not valid.
  */
-static int
+int
 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
     boolean_t modifying)
 {
@@ -181,6 +141,8 @@ zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
                                zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
                                    "reserved disk name"));
                                break;
+                       default:
+                               break;
                        }
                }
 
@@ -345,7 +307,7 @@ get_recvd_props_ioctl(zfs_handle_t *zhp)
 {
        libzfs_handle_t *hdl = zhp->zfs_hdl;
        nvlist_t *recvdprops;
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int err;
 
        if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
@@ -408,7 +370,7 @@ static int
 get_stats(zfs_handle_t *zhp)
 {
        int rc = 0;
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
 
        if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
                return (-1);
@@ -468,7 +430,7 @@ make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
 zfs_handle_t *
 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
 
        zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
 
@@ -620,7 +582,7 @@ libzfs_mnttab_fini(libzfs_handle_t *hdl)
        void *cookie = NULL;
        mnttab_node_t *mtn;
 
-       while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
+       while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
                free(mtn->mtn_mt.mnt_special);
                free(mtn->mtn_mt.mnt_mountp);
                free(mtn->mtn_mt.mnt_fstype);
@@ -692,7 +654,7 @@ libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
        mnttab_node_t *ret;
 
        find.mtn_mt.mnt_special = (char *)fsname;
-       if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
+       if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
                avl_remove(&hdl->libzfs_mnttab_cache, ret);
                free(ret->mtn_mt.mnt_special);
                free(ret->mtn_mt.mnt_mountp);
@@ -942,6 +904,7 @@ zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
 
                case ZFS_PROP_MLSLABEL:
                {
+#ifdef HAVE_MLSLABEL
                        /*
                         * Verify the mlslabel string and convert to
                         * internal hex label string.
@@ -991,7 +954,12 @@ badlabel:
                        (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
                        m_label_free(new_sl);   /* OK if null */
                        goto error;
-
+#else
+                       zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
+                           "mlslabels are unsupported"));
+                       (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
+                       goto error;
+#endif /* HAVE_MLSLABEL */
                }
 
                case ZFS_PROP_MOUNTPOINT:
@@ -1016,6 +984,8 @@ badlabel:
                                            "component of '%s' is too long"),
                                            propname);
                                        break;
+                               default:
+                                       break;
                                }
                                (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
                                goto error;
@@ -1140,6 +1110,8 @@ badlabel:
                case ZFS_PROP_NORMALIZE:
                        chosen_normal = (int)intval;
                        break;
+               default:
+                       break;
                }
 
                /*
@@ -1188,6 +1160,8 @@ badlabel:
                                        goto error;
                                }
                                break;
+                       default:
+                               break;
                        }
                }
        }
@@ -1212,39 +1186,46 @@ badlabel:
                (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
                goto error;
        }
+       return (ret);
+
+error:
+       nvlist_free(ret);
+       return (NULL);
+}
+
+int
+zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
+{
+       uint64_t old_volsize;
+       uint64_t new_volsize;
+       uint64_t old_reservation;
+       uint64_t new_reservation;
+       zfs_prop_t resv_prop;
 
        /*
         * If this is an existing volume, and someone is setting the volsize,
         * make sure that it matches the reservation, or add it if necessary.
         */
-       if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
-           nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
-           &intval) == 0) {
-               uint64_t old_volsize = zfs_prop_get_int(zhp,
-                   ZFS_PROP_VOLSIZE);
-               uint64_t old_reservation;
-               uint64_t new_reservation;
-               zfs_prop_t resv_prop;
-
-               if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
-                       goto error;
-               old_reservation = zfs_prop_get_int(zhp, resv_prop);
-
-               if (old_volsize == old_reservation &&
-                   nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
-                   &new_reservation) != 0) {
-                       if (nvlist_add_uint64(ret,
-                           zfs_prop_to_name(resv_prop), intval) != 0) {
-                               (void) no_memory(hdl);
-                               goto error;
-                       }
-               }
+       old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
+       if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
+               return (-1);
+       old_reservation = zfs_prop_get_int(zhp, resv_prop);
+       if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
+           old_reservation) || nvlist_lookup_uint64(nvl,
+           zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
+               return (0);
        }
-       return (ret);
-
-error:
-       nvlist_free(ret);
-       return (NULL);
+       if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
+           &new_volsize) != 0)
+               return (-1);
+       new_reservation = zvol_volsize_to_reservation(new_volsize,
+           zhp->zfs_props);
+       if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
+           new_reservation) != 0) {
+               (void) no_memory(zhp->zfs_hdl);
+               return (-1);
+       }
+       return (1);
 }
 
 void
@@ -1337,7 +1318,7 @@ zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
 int
 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int ret = -1;
        prop_changelist_t *cl = NULL;
        char errbuf[1024];
@@ -1346,6 +1327,7 @@ zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
        zfs_prop_t prop;
        boolean_t do_prefix;
        uint64_t idx;
+       int added_resv = 0;
 
        (void) snprintf(errbuf, sizeof (errbuf),
            dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
@@ -1366,6 +1348,11 @@ zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
 
        prop = zfs_name_to_prop(propname);
 
+       if (prop == ZFS_PROP_VOLSIZE) {
+               if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
+                       goto error;
+       }
+
        if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
                goto error;
 
@@ -1400,6 +1387,22 @@ zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
 
        if (ret != 0) {
                zfs_setprop_error(hdl, prop, errno, errbuf);
+               if (added_resv && errno == ENOSPC) {
+                       /* clean up the volsize property we tried to set */
+                       uint64_t old_volsize = zfs_prop_get_int(zhp,
+                           ZFS_PROP_VOLSIZE);
+                       nvlist_free(nvl);
+                       zcmd_free_nvlists(&zc);
+                       if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
+                               goto error;
+                       if (nvlist_add_uint64(nvl,
+                           zfs_prop_to_name(ZFS_PROP_VOLSIZE),
+                           old_volsize) != 0)
+                               goto error;
+                       if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
+                               goto error;
+                       (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
+               }
        } else {
                if (do_prefix)
                        ret = changelist_postfix(cl);
@@ -1427,7 +1430,7 @@ error:
 int
 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int ret;
        prop_changelist_t *cl;
        libzfs_handle_t *hdl = zhp->zfs_hdl;
@@ -1474,7 +1477,7 @@ zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
                return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
 
        /*
-        * Normalize the name, to get rid of shorthand abbrevations.
+        * Normalize the name, to get rid of shorthand abbreviations.
         */
        propname = zfs_prop_to_name(prop);
        (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
@@ -1602,7 +1605,7 @@ static int
 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
     char **source, uint64_t *val)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        nvlist_t *zplprops = NULL;
        struct mnttab mnt;
        char *mntopt_on = NULL;
@@ -1646,6 +1649,8 @@ get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
                mntopt_on = MNTOPT_NBMAND;
                mntopt_off = MNTOPT_NONBMAND;
                break;
+       default:
+               break;
        }
 
        /*
@@ -1878,7 +1883,7 @@ zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
                            localtime_r(&time, &t) == NULL ||
                            strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
                            &t) == 0)
-                               (void) snprintf(propbuf, proplen, "%llu", val);
+                               (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
                }
                break;
 
@@ -2042,6 +2047,7 @@ zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
 
        case ZFS_PROP_MLSLABEL:
                {
+#ifdef HAVE_MLSLABEL
                        m_label_t *new_sl = NULL;
                        char *ascii = NULL;     /* human readable label */
 
@@ -2075,6 +2081,10 @@ zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
 
                        (void) strlcpy(propbuf, ascii, proplen);
                        free(ascii);
+#else
+                       (void) strlcpy(propbuf,
+                           getprop_string(zhp, prop, &source), proplen);
+#endif /* HAVE_MLSLABEL */
                }
                break;
 
@@ -2169,18 +2179,16 @@ zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
        return (0);
 }
 
+#ifdef HAVE_IDMAP
 static int
 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
     char **domainp, idmap_rid_t *ridp)
 {
-       idmap_handle_t *idmap_hdl = NULL;
        idmap_get_handle_t *get_hdl = NULL;
        idmap_stat status;
        int err = EINVAL;
 
-       if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
-               goto out;
-       if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
+       if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
                goto out;
 
        if (isuser) {
@@ -2199,10 +2207,9 @@ idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
 out:
        if (get_hdl)
                idmap_get_destroy(get_hdl);
-       if (idmap_hdl)
-               (void) idmap_fini(idmap_hdl);
        return (err);
 }
+#endif /* HAVE_IDMAP */
 
 /*
  * convert the propname into parameters needed by kernel
@@ -2236,6 +2243,7 @@ userquota_propname_decode(const char *propname, boolean_t zoned,
        cp = strchr(propname, '@') + 1;
 
        if (strchr(cp, '@')) {
+#ifdef HAVE_IDMAP
                /*
                 * It's a SID name (eg "user@domain") that needs to be
                 * turned into S-1-domainID-RID.
@@ -2258,6 +2266,9 @@ userquota_propname_decode(const char *propname, boolean_t zoned,
                        return (ENOENT);
                cp = numericsid;
                /* will be further decoded below */
+#else
+               return (ENOSYS);
+#endif /* HAVE_IDMAP */
        }
 
        if (strncmp(cp, "S-1-", 4) == 0) {
@@ -2296,6 +2307,7 @@ userquota_propname_decode(const char *propname, boolean_t zoned,
                        *ridp = gr->gr_gid;
                }
        } else {
+#ifdef HAVE_IDMAP
                /* It's a user/group ID (eg "12345"). */
                uid_t id = strtoul(cp, &end, 10);
                idmap_rid_t rid;
@@ -2313,6 +2325,9 @@ userquota_propname_decode(const char *propname, boolean_t zoned,
                } else {
                        *ridp = id;
                }
+#else
+               return (ENOSYS);
+#endif /* HAVE_IDMAP */
        }
 
        ASSERT3P(numericsid, ==, NULL);
@@ -2324,7 +2339,7 @@ zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
     uint64_t *propvalue, zfs_userquota_prop_t *typep)
 {
        int err;
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
 
        (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
 
@@ -2368,7 +2383,8 @@ zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
                return (err);
 
        if (literal) {
-               (void) snprintf(propbuf, proplen, "%llu", propvalue);
+               (void) snprintf(propbuf, proplen, "%llu",
+                              (u_longlong_t)propvalue);
        } else if (propvalue == 0 &&
            (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
                (void) strlcpy(propbuf, "none", proplen);
@@ -2442,7 +2458,7 @@ top:
 int
 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        zfs_handle_t *nzhp;
        int ret;
 
@@ -2478,7 +2494,7 @@ zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
 int
 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        zfs_handle_t *nzhp;
        int ret;
 
@@ -2569,7 +2585,7 @@ static int
 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
     boolean_t accept_ancestor, int *prefixlen)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char parent[ZFS_MAXNAMELEN];
        char *slash;
        zfs_handle_t *zhp;
@@ -2698,7 +2714,7 @@ create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
         * up to the prefixlen-long one.
         */
        for (cp = target + prefixlen + 1;
-           cp = strchr(cp, '/'); *cp = '/', cp++) {
+           (cp = strchr(cp, '/')); *cp = '/', cp++) {
                char *logstr;
 
                *cp = '\0';
@@ -2755,7 +2771,7 @@ zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
 {
        int prefix;
        char *path_copy;
-       int rc;
+       int rc = 0;
 
        if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
                return (-1);
@@ -2777,7 +2793,7 @@ int
 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
     nvlist_t *props)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int ret;
        uint64_t size = 0;
        uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
@@ -2928,7 +2944,7 @@ zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
 int
 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
 
        (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
 
@@ -2989,7 +3005,7 @@ zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
 int
 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int ret;
        struct destroydata dd = { 0 };
 
@@ -3034,7 +3050,7 @@ zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
 int
 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char parent[ZFS_MAXNAMELEN];
        int ret;
        char errbuf[1024];
@@ -3124,7 +3140,7 @@ int
 zfs_promote(zfs_handle_t *zhp)
 {
        libzfs_handle_t *hdl = zhp->zfs_hdl;
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char parent[MAXPATHLEN];
        int ret;
        char errbuf[1024];
@@ -3178,7 +3194,7 @@ zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
        const char *delim;
        char parent[ZFS_MAXNAMELEN];
        zfs_handle_t *zhp;
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int ret;
        char errbuf[1024];
 
@@ -3310,10 +3326,10 @@ zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
 {
        rollback_data_t cb = { 0 };
        int err;
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        boolean_t restore_resv = 0;
-       uint64_t old_volsize, new_volsize;
-       zfs_prop_t resv_prop;
+       uint64_t old_volsize = 0, new_volsize;
+       zfs_prop_t resv_prop = { 0 };
 
        assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
            zhp->zfs_type == ZFS_TYPE_VOLUME);
@@ -3426,7 +3442,7 @@ int
 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
 {
        int ret;
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        char *delim;
        prop_changelist_t *cl = NULL;
        zfs_handle_t *zhrp = NULL;
@@ -3543,6 +3559,7 @@ zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
                            "child dataset with inherited mountpoint is used "
                            "in a non-global zone"));
                        (void) zfs_error(hdl, EZFS_ZONED, errbuf);
+                       ret = -1;
                        goto error;
                }
 
@@ -3732,7 +3749,7 @@ zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
     char *resource, void *export, void *sharetab,
     int sharemax, zfs_share_op_t operation)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int error;
 
        (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
@@ -3782,7 +3799,7 @@ static int
 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        nvlist_t *nvlist = NULL;
        int error;
 
@@ -3793,7 +3810,7 @@ zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
        if (cmd == ZFS_SMB_ACL_RENAME) {
                if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
                        (void) no_memory(hdl);
-                       return (NULL);
+                       return (-1);
                }
        }
 
@@ -3864,7 +3881,7 @@ int
 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
     zfs_userspace_cb_t func, void *arg)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        int error;
        zfs_useracct_t buf[100];
 
@@ -3898,11 +3915,14 @@ zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
 
 int
 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
-    boolean_t recursive, boolean_t temphold, boolean_t enoent_ok)
+    boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
+    int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        libzfs_handle_t *hdl = zhp->zfs_hdl;
 
+       ASSERT(!recursive || dsobj == 0);
+
        (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
        (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
        if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
@@ -3910,6 +3930,9 @@ zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
                return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
        zc.zc_cookie = recursive;
        zc.zc_temphold = temphold;
+       zc.zc_cleanup_fd = cleanup_fd;
+       zc.zc_sendobj = dsobj;
+       zc.zc_createtxg = createtxg;
 
        if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
                char errbuf[ZFS_MAXNAMELEN+32];
@@ -3939,7 +3962,7 @@ zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
                        return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
                case ENOENT:
                        if (enoent_ok)
-                               return (0);
+                               return (ENOENT);
                        /* FALLTHROUGH */
                default:
                        return (zfs_standard_error_fmt(hdl, errno, errbuf));
@@ -3949,107 +3972,11 @@ zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
        return (0);
 }
 
-struct hold_range_arg {
-       zfs_handle_t    *origin;
-       const char      *fromsnap;
-       const char      *tosnap;
-       char            lastsnapheld[ZFS_MAXNAMELEN];
-       const char      *tag;
-       boolean_t       temphold;
-       boolean_t       seento;
-       boolean_t       seenfrom;
-       boolean_t       holding;
-       boolean_t       recursive;
-       snapfilter_cb_t *filter_cb;
-       void            *filter_cb_arg;
-};
-
-static int
-zfs_hold_range_one(zfs_handle_t *zhp, void *arg)
-{
-       struct hold_range_arg *hra = arg;
-       const char *thissnap;
-       int error;
-
-       thissnap = strchr(zfs_get_name(zhp), '@') + 1;
-
-       if (hra->fromsnap && !hra->seenfrom &&
-           strcmp(hra->fromsnap, thissnap) == 0)
-               hra->seenfrom = B_TRUE;
-
-       /* snap is older or newer than the desired range, ignore it */
-       if (hra->seento || !hra->seenfrom) {
-               zfs_close(zhp);
-               return (0);
-       }
-
-       if (!hra->seento && strcmp(hra->tosnap, thissnap) == 0)
-               hra->seento = B_TRUE;
-
-       if (hra->filter_cb != NULL &&
-           hra->filter_cb(zhp, hra->filter_cb_arg) == B_FALSE) {
-               zfs_close(zhp);
-               return (0);
-       }
-
-       if (hra->holding) {
-               /* We could be racing with destroy, so ignore ENOENT. */
-               error = zfs_hold(hra->origin, thissnap, hra->tag,
-                   hra->recursive, hra->temphold, B_TRUE);
-               if (error == 0) {
-                       (void) strlcpy(hra->lastsnapheld, zfs_get_name(zhp),
-                           sizeof (hra->lastsnapheld));
-               }
-       } else {
-               error = zfs_release(hra->origin, thissnap, hra->tag,
-                   hra->recursive);
-       }
-
-       zfs_close(zhp);
-       return (error);
-}
-
-/*
- * Add a user hold on the set of snapshots starting with fromsnap up to
- * and including tosnap. If we're unable to to acquire a particular hold,
- * undo any holds up to that point.
- */
-int
-zfs_hold_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
-    const char *tag, boolean_t recursive, boolean_t temphold,
-    snapfilter_cb_t filter_cb, void *cbarg)
-{
-       struct hold_range_arg arg = { 0 };
-       int error;
-
-       arg.origin = zhp;
-       arg.fromsnap = fromsnap;
-       arg.tosnap = tosnap;
-       arg.tag = tag;
-       arg.temphold = temphold;
-       arg.holding = B_TRUE;
-       arg.recursive = recursive;
-       arg.seenfrom = (fromsnap == NULL);
-       arg.filter_cb = filter_cb;
-       arg.filter_cb_arg = cbarg;
-
-       error = zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg);
-
-       /*
-        * Make sure we either hold the entire range or none.
-        */
-       if (error && arg.lastsnapheld[0] != '\0') {
-               (void) zfs_release_range(zhp, fromsnap,
-                   (const char *)arg.lastsnapheld, tag, recursive);
-       }
-       return (error);
-}
-
 int
 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
     boolean_t recursive)
 {
-       zfs_cmd_t zc = { 0 };
+       zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
        libzfs_handle_t *hdl = zhp->zfs_hdl;
 
        (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
@@ -4086,26 +4013,6 @@ zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
        return (0);
 }
 
-/*
- * Release a user hold from the set of snapshots starting with fromsnap
- * up to and including tosnap.
- */
-int
-zfs_release_range(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
-    const char *tag, boolean_t recursive)
-{
-       struct hold_range_arg arg = { 0 };
-
-       arg.origin = zhp;
-       arg.fromsnap = fromsnap;
-       arg.tosnap = tosnap;
-       arg.tag = tag;
-       arg.recursive = recursive;
-       arg.seenfrom = (fromsnap == NULL);
-
-       return (zfs_iter_snapshots_sorted(zhp, zfs_hold_range_one, &arg));
-}
-
 uint64_t
 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
 {