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.
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.
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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
30 #include <libdevinfo.h>
40 #include <sys/mntent.h>
41 #include <sys/mount.h>
55 #include "zfs_namecheck.h"
57 #include "libzfs_impl.h"
58 #include "zfs_deleg.h"
60 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
61 static int userquota_propname_decode(const char *propname, boolean_t zoned,
62 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
65 * Given a single type (not a mask of types), return the type in a human
69 zfs_type_to_name(zfs_type_t type)
72 case ZFS_TYPE_FILESYSTEM:
73 return (dgettext(TEXT_DOMAIN, "filesystem"));
74 case ZFS_TYPE_SNAPSHOT:
75 return (dgettext(TEXT_DOMAIN, "snapshot"));
77 return (dgettext(TEXT_DOMAIN, "volume"));
84 * Given a path and mask of ZFS types, return a string describing this dataset.
85 * This is used when we fail to open a dataset and we cannot get an exact type.
86 * We guess what the type would have been based on the path and the mask of
90 path_to_str(const char *path, int types)
93 * When given a single type, always report the exact type.
95 if (types == ZFS_TYPE_SNAPSHOT)
96 return (dgettext(TEXT_DOMAIN, "snapshot"));
97 if (types == ZFS_TYPE_FILESYSTEM)
98 return (dgettext(TEXT_DOMAIN, "filesystem"));
99 if (types == ZFS_TYPE_VOLUME)
100 return (dgettext(TEXT_DOMAIN, "volume"));
103 * The user is requesting more than one type of dataset. If this is the
104 * case, consult the path itself. If we're looking for a snapshot, and
105 * a '@' is found, then report it as "snapshot". Otherwise, remove the
106 * snapshot attribute and try again.
108 if (types & ZFS_TYPE_SNAPSHOT) {
109 if (strchr(path, '@') != NULL)
110 return (dgettext(TEXT_DOMAIN, "snapshot"));
111 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
115 * The user has requested either filesystems or volumes.
116 * We have no way of knowing a priori what type this would be, so always
117 * report it as "filesystem" or "volume", our two primitive types.
119 if (types & ZFS_TYPE_FILESYSTEM)
120 return (dgettext(TEXT_DOMAIN, "filesystem"));
122 assert(types & ZFS_TYPE_VOLUME);
123 return (dgettext(TEXT_DOMAIN, "volume"));
127 * Validate a ZFS path. This is used even before trying to open the dataset, to
128 * provide a more meaningful error message. We call zfs_error_aux() to
129 * explain exactly why the name was not valid.
132 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
138 if (dataset_namecheck(path, &why, &what) != 0) {
141 case NAME_ERR_TOOLONG:
142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 "name is too long"));
146 case NAME_ERR_LEADING_SLASH:
147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148 "leading slash in name"));
151 case NAME_ERR_EMPTY_COMPONENT:
152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
153 "empty component in name"));
156 case NAME_ERR_TRAILING_SLASH:
157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158 "trailing slash in name"));
161 case NAME_ERR_INVALCHAR:
163 dgettext(TEXT_DOMAIN, "invalid character "
164 "'%c' in name"), what);
167 case NAME_ERR_MULTIPLE_AT:
168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169 "multiple '@' delimiters in name"));
172 case NAME_ERR_NOLETTER:
173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174 "pool doesn't begin with a letter"));
177 case NAME_ERR_RESERVED:
178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
179 "name is reserved"));
182 case NAME_ERR_DISKLIKE:
183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
184 "reserved disk name"));
192 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
194 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
195 "snapshot delimiter '@' in filesystem name"));
199 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
202 "missing '@' delimiter in snapshot name"));
206 if (modifying && strchr(path, '%') != NULL) {
208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
209 "invalid character %c in name"), '%');
217 zfs_name_valid(const char *name, zfs_type_t type)
219 if (type == ZFS_TYPE_POOL)
220 return (zpool_name_valid(NULL, B_FALSE, name));
221 return (zfs_validate_name(NULL, name, type, B_FALSE));
225 * This function takes the raw DSL properties, and filters out the user-defined
226 * properties into a separate nvlist.
229 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
231 libzfs_handle_t *hdl = zhp->zfs_hdl;
236 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
237 (void) no_memory(hdl);
242 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
243 if (!zfs_prop_user(nvpair_name(elem)))
246 verify(nvpair_value_nvlist(elem, &propval) == 0);
247 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
249 (void) no_memory(hdl);
257 static zpool_handle_t *
258 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
260 libzfs_handle_t *hdl = zhp->zfs_hdl;
263 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
264 if (hdl->libzfs_pool_handles != NULL)
265 zph->zpool_next = hdl->libzfs_pool_handles;
266 hdl->libzfs_pool_handles = zph;
271 static zpool_handle_t *
272 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
274 libzfs_handle_t *hdl = zhp->zfs_hdl;
275 zpool_handle_t *zph = hdl->libzfs_pool_handles;
277 while ((zph != NULL) &&
278 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
279 zph = zph->zpool_next;
284 * Returns a handle to the pool that contains the provided dataset.
285 * If a handle to that pool already exists then that handle is returned.
286 * Otherwise, a new handle is created and added to the list of handles.
288 static zpool_handle_t *
289 zpool_handle(zfs_handle_t *zhp)
295 len = strcspn(zhp->zfs_name, "/@") + 1;
296 pool_name = zfs_alloc(zhp->zfs_hdl, len);
297 (void) strlcpy(pool_name, zhp->zfs_name, len);
299 zph = zpool_find_handle(zhp, pool_name, len);
301 zph = zpool_add_handle(zhp, pool_name);
308 zpool_free_handles(libzfs_handle_t *hdl)
310 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
312 while (zph != NULL) {
313 next = zph->zpool_next;
317 hdl->libzfs_pool_handles = NULL;
321 * Utility function to gather stats (objset and zpl) for the given object.
324 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
326 libzfs_handle_t *hdl = zhp->zfs_hdl;
328 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
330 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
331 if (errno == ENOMEM) {
332 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
343 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
345 nvlist_t *allprops, *userprops;
347 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
349 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
354 * XXX Why do we store the user props separately, in addition to
355 * storing them in zfs_props?
357 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
358 nvlist_free(allprops);
362 nvlist_free(zhp->zfs_props);
363 nvlist_free(zhp->zfs_user_props);
365 zhp->zfs_props = allprops;
366 zhp->zfs_user_props = userprops;
372 get_stats(zfs_handle_t *zhp)
375 zfs_cmd_t zc = { 0 };
377 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
379 if (get_stats_ioctl(zhp, &zc) != 0)
381 else if (put_stats_zhdl(zhp, &zc) != 0)
383 zcmd_free_nvlists(&zc);
388 * Refresh the properties currently stored in the handle.
391 zfs_refresh_properties(zfs_handle_t *zhp)
393 (void) get_stats(zhp);
397 * Makes a handle from the given dataset name. Used by zfs_open() and
398 * zfs_iter_* to create child handles on the fly.
401 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
404 libzfs_handle_t *hdl = zhp->zfs_hdl;
407 * Preserve history log string.
408 * any changes performed here will be
409 * logged as an internal event.
411 logstr = zhp->zfs_hdl->libzfs_log_str;
412 zhp->zfs_hdl->libzfs_log_str = NULL;
415 if (put_stats_zhdl(zhp, zc) != 0) {
416 zhp->zfs_hdl->libzfs_log_str = logstr;
421 if (zhp->zfs_dmustats.dds_inconsistent) {
422 zfs_cmd_t zc2 = { 0 };
425 * If it is dds_inconsistent, then we've caught it in
426 * the middle of a 'zfs receive' or 'zfs destroy', and
427 * it is inconsistent from the ZPL's point of view, so
428 * can't be mounted. However, it could also be that we
429 * have crashed in the middle of one of those
430 * operations, in which case we need to get rid of the
431 * inconsistent state. We do that by either rolling
432 * back to the previous snapshot (which will fail if
433 * there is none), or destroying the filesystem. Note
434 * that if we are still in the middle of an active
435 * 'receive' or 'destroy', then the rollback and destroy
436 * will fail with EBUSY and we will drive on as usual.
439 (void) strlcpy(zc2.zc_name, zhp->zfs_name,
440 sizeof (zc2.zc_name));
442 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
443 (void) zvol_remove_link(hdl, zhp->zfs_name);
444 zc2.zc_objset_type = DMU_OST_ZVOL;
446 zc2.zc_objset_type = DMU_OST_ZFS;
450 * If we can successfully destroy it, pretend that it
453 if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) {
454 zhp->zfs_hdl->libzfs_log_str = logstr;
458 /* If we can successfully roll it back, reset the stats */
459 if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) {
460 if (get_stats_ioctl(zhp, zc) != 0) {
461 zhp->zfs_hdl->libzfs_log_str = logstr;
469 * We've managed to open the dataset and gather statistics. Determine
470 * the high-level type.
472 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
473 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
474 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
475 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
479 if (zhp->zfs_dmustats.dds_is_snapshot)
480 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
481 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
482 zhp->zfs_type = ZFS_TYPE_VOLUME;
483 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
484 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
486 abort(); /* we should never see any other types */
488 zhp->zfs_hdl->libzfs_log_str = logstr;
489 zhp->zpool_hdl = zpool_handle(zhp);
494 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
496 zfs_cmd_t zc = { 0 };
498 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
504 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
505 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
509 if (get_stats_ioctl(zhp, &zc) == -1) {
510 zcmd_free_nvlists(&zc);
514 if (make_dataset_handle_common(zhp, &zc) == -1) {
518 zcmd_free_nvlists(&zc);
522 static zfs_handle_t *
523 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
525 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
531 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
532 if (make_dataset_handle_common(zhp, zc) == -1) {
540 * Opens the given snapshot, filesystem, or volume. The 'types'
541 * argument is a mask of acceptable types. The function will print an
542 * appropriate error message and return NULL if it can't be opened.
545 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
550 (void) snprintf(errbuf, sizeof (errbuf),
551 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
554 * Validate the name before we even try to open it.
556 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
558 "invalid dataset name"));
559 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
564 * Try to get stats for the dataset, which will tell us if it exists.
567 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
568 (void) zfs_standard_error(hdl, errno, errbuf);
572 if (!(types & zhp->zfs_type)) {
573 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
582 * Release a ZFS handle. Nothing to do but free the associated memory.
585 zfs_close(zfs_handle_t *zhp)
587 if (zhp->zfs_mntopts)
588 free(zhp->zfs_mntopts);
589 nvlist_free(zhp->zfs_props);
590 nvlist_free(zhp->zfs_user_props);
594 typedef struct mnttab_node {
595 struct mnttab mtn_mt;
600 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
602 const mnttab_node_t *mtn1 = arg1;
603 const mnttab_node_t *mtn2 = arg2;
606 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
610 return (rv > 0 ? 1 : -1);
614 libzfs_mnttab_init(libzfs_handle_t *hdl)
616 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
617 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
618 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
622 libzfs_mnttab_update(libzfs_handle_t *hdl)
626 rewind(hdl->libzfs_mnttab);
627 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
630 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
632 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
633 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
634 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
635 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
636 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
637 avl_add(&hdl->libzfs_mnttab_cache, mtn);
642 libzfs_mnttab_fini(libzfs_handle_t *hdl)
647 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
648 free(mtn->mtn_mt.mnt_special);
649 free(mtn->mtn_mt.mnt_mountp);
650 free(mtn->mtn_mt.mnt_fstype);
651 free(mtn->mtn_mt.mnt_mntopts);
654 avl_destroy(&hdl->libzfs_mnttab_cache);
658 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
660 hdl->libzfs_mnttab_enable = enable;
664 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
665 struct mnttab *entry)
670 if (!hdl->libzfs_mnttab_enable) {
671 struct mnttab srch = { 0 };
673 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
674 libzfs_mnttab_fini(hdl);
675 rewind(hdl->libzfs_mnttab);
676 srch.mnt_special = (char *)fsname;
677 srch.mnt_fstype = MNTTYPE_ZFS;
678 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
684 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
685 libzfs_mnttab_update(hdl);
687 find.mtn_mt.mnt_special = (char *)fsname;
688 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
690 *entry = mtn->mtn_mt;
697 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
698 const char *mountp, const char *mntopts)
702 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
704 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
705 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
706 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
707 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
708 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
709 avl_add(&hdl->libzfs_mnttab_cache, mtn);
713 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
718 find.mtn_mt.mnt_special = (char *)fsname;
719 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
720 avl_remove(&hdl->libzfs_mnttab_cache, ret);
721 free(ret->mtn_mt.mnt_special);
722 free(ret->mtn_mt.mnt_mountp);
723 free(ret->mtn_mt.mnt_fstype);
724 free(ret->mtn_mt.mnt_mntopts);
730 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
732 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
734 if (zpool_handle == NULL)
737 *spa_version = zpool_get_prop_int(zpool_handle,
738 ZPOOL_PROP_VERSION, NULL);
743 * The choice of reservation property depends on the SPA version.
746 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
750 if (zfs_spa_version(zhp, &spa_version) < 0)
753 if (spa_version >= SPA_VERSION_REFRESERVATION)
754 *resv_prop = ZFS_PROP_REFRESERVATION;
756 *resv_prop = ZFS_PROP_RESERVATION;
762 * Given an nvlist of properties to set, validates that they are correct, and
763 * parses any numeric properties (index, boolean, etc) if they are specified as
767 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
768 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
775 int chosen_normal = -1;
778 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
779 (void) no_memory(hdl);
784 * Make sure this property is valid and applies to this type.
788 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
789 const char *propname = nvpair_name(elem);
791 prop = zfs_name_to_prop(propname);
792 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
794 * This is a user property: make sure it's a
795 * string, and that it's less than ZAP_MAXNAMELEN.
797 if (nvpair_type(elem) != DATA_TYPE_STRING) {
798 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
799 "'%s' must be a string"), propname);
800 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
804 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
806 "property name '%s' is too long"),
808 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
812 (void) nvpair_value_string(elem, &strval);
813 if (nvlist_add_string(ret, propname, strval) != 0) {
814 (void) no_memory(hdl);
821 * Currently, only user properties can be modified on
824 if (type == ZFS_TYPE_SNAPSHOT) {
825 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
826 "this property can not be modified for snapshots"));
827 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
831 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
832 zfs_userquota_prop_t uqtype;
833 char newpropname[128];
838 if (userquota_propname_decode(propname, zoned,
839 &uqtype, domain, sizeof (domain), &rid) != 0) {
841 dgettext(TEXT_DOMAIN,
842 "'%s' has an invalid user/group name"),
844 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
848 if (uqtype != ZFS_PROP_USERQUOTA &&
849 uqtype != ZFS_PROP_GROUPQUOTA) {
851 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
853 (void) zfs_error(hdl, EZFS_PROPREADONLY,
858 if (nvpair_type(elem) == DATA_TYPE_STRING) {
859 (void) nvpair_value_string(elem, &strval);
860 if (strcmp(strval, "none") == 0) {
862 } else if (zfs_nicestrtonum(hdl,
863 strval, &intval) != 0) {
864 (void) zfs_error(hdl,
865 EZFS_BADPROP, errbuf);
868 } else if (nvpair_type(elem) ==
870 (void) nvpair_value_uint64(elem, &intval);
872 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
873 "use 'none' to disable "
874 "userquota/groupquota"));
878 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
879 "'%s' must be a number"), propname);
880 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
884 (void) snprintf(newpropname, sizeof (newpropname),
885 "%s%s", zfs_userquota_prop_prefixes[uqtype],
890 if (nvlist_add_uint64_array(ret, newpropname,
892 (void) no_memory(hdl);
898 if (prop == ZPROP_INVAL) {
899 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
900 "invalid property '%s'"), propname);
901 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
905 if (!zfs_prop_valid_for_type(prop, type)) {
907 dgettext(TEXT_DOMAIN, "'%s' does not "
908 "apply to datasets of this type"), propname);
909 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
913 if (zfs_prop_readonly(prop) &&
914 (!zfs_prop_setonce(prop) || zhp != NULL)) {
916 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
918 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
922 if (zprop_parse_value(hdl, elem, prop, type, ret,
923 &strval, &intval, errbuf) != 0)
927 * Perform some additional checks for specific properties.
930 case ZFS_PROP_VERSION:
936 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
937 if (intval < version) {
938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
939 "Can not downgrade; already at version %u"),
941 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
947 case ZFS_PROP_RECORDSIZE:
948 case ZFS_PROP_VOLBLOCKSIZE:
949 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
950 if (intval < SPA_MINBLOCKSIZE ||
951 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953 "'%s' must be power of 2 from %u "
955 (uint_t)SPA_MINBLOCKSIZE,
956 (uint_t)SPA_MAXBLOCKSIZE >> 10);
957 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
962 case ZFS_PROP_SHAREISCSI:
963 if (strcmp(strval, "off") != 0 &&
964 strcmp(strval, "on") != 0 &&
965 strcmp(strval, "type=disk") != 0) {
966 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
967 "'%s' must be 'on', 'off', or 'type=disk'"),
969 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
975 case ZFS_PROP_MOUNTPOINT:
979 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
980 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
983 if (mountpoint_namecheck(strval, &why)) {
985 case NAME_ERR_LEADING_SLASH:
987 dgettext(TEXT_DOMAIN,
988 "'%s' must be an absolute path, "
989 "'none', or 'legacy'"), propname);
991 case NAME_ERR_TOOLONG:
993 dgettext(TEXT_DOMAIN,
994 "component of '%s' is too long"),
998 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1005 case ZFS_PROP_SHARESMB:
1006 case ZFS_PROP_SHARENFS:
1008 * For the mountpoint and sharenfs or sharesmb
1009 * properties, check if it can be set in a
1010 * global/non-global zone based on
1011 * the zoned property value:
1013 * global zone non-global zone
1014 * --------------------------------------------------
1015 * zoned=on mountpoint (no) mountpoint (yes)
1016 * sharenfs (no) sharenfs (no)
1017 * sharesmb (no) sharesmb (no)
1019 * zoned=off mountpoint (yes) N/A
1024 if (getzoneid() == GLOBAL_ZONEID) {
1025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1026 "'%s' cannot be set on "
1027 "dataset in a non-global zone"),
1029 (void) zfs_error(hdl, EZFS_ZONED,
1032 } else if (prop == ZFS_PROP_SHARENFS ||
1033 prop == ZFS_PROP_SHARESMB) {
1034 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1035 "'%s' cannot be set in "
1036 "a non-global zone"), propname);
1037 (void) zfs_error(hdl, EZFS_ZONED,
1041 } else if (getzoneid() != GLOBAL_ZONEID) {
1043 * If zoned property is 'off', this must be in
1044 * a global zone. If not, something is wrong.
1046 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1047 "'%s' cannot be set while dataset "
1048 "'zoned' property is set"), propname);
1049 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1054 * At this point, it is legitimate to set the
1055 * property. Now we want to make sure that the
1056 * property value is valid if it is sharenfs.
1058 if ((prop == ZFS_PROP_SHARENFS ||
1059 prop == ZFS_PROP_SHARESMB) &&
1060 strcmp(strval, "on") != 0 &&
1061 strcmp(strval, "off") != 0) {
1062 zfs_share_proto_t proto;
1064 if (prop == ZFS_PROP_SHARESMB)
1070 * Must be an valid sharing protocol
1071 * option string so init the libshare
1072 * in order to enable the parser and
1073 * then parse the options. We use the
1074 * control API since we don't care about
1075 * the current configuration and don't
1076 * want the overhead of loading it
1077 * until we actually do something.
1080 if (zfs_init_libshare(hdl,
1081 SA_INIT_CONTROL_API) != SA_OK) {
1083 * An error occurred so we can't do
1086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1087 "'%s' cannot be set: problem "
1088 "in share initialization"),
1090 (void) zfs_error(hdl, EZFS_BADPROP,
1095 if (zfs_parse_options(strval, proto) != SA_OK) {
1097 * There was an error in parsing so
1098 * deal with it by issuing an error
1099 * message and leaving after
1100 * uninitializing the the libshare
1103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1104 "'%s' cannot be set to invalid "
1105 "options"), propname);
1106 (void) zfs_error(hdl, EZFS_BADPROP,
1108 zfs_uninit_libshare(hdl);
1111 zfs_uninit_libshare(hdl);
1115 case ZFS_PROP_UTF8ONLY:
1116 chosen_utf = (int)intval;
1118 case ZFS_PROP_NORMALIZE:
1119 chosen_normal = (int)intval;
1124 * For changes to existing volumes, we have some additional
1125 * checks to enforce.
1127 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1128 uint64_t volsize = zfs_prop_get_int(zhp,
1130 uint64_t blocksize = zfs_prop_get_int(zhp,
1131 ZFS_PROP_VOLBLOCKSIZE);
1135 case ZFS_PROP_RESERVATION:
1136 case ZFS_PROP_REFRESERVATION:
1137 if (intval > volsize) {
1138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1139 "'%s' is greater than current "
1140 "volume size"), propname);
1141 (void) zfs_error(hdl, EZFS_BADPROP,
1147 case ZFS_PROP_VOLSIZE:
1148 if (intval % blocksize != 0) {
1149 zfs_nicenum(blocksize, buf,
1151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1152 "'%s' must be a multiple of "
1153 "volume block size (%s)"),
1155 (void) zfs_error(hdl, EZFS_BADPROP,
1161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1162 "'%s' cannot be zero"),
1164 (void) zfs_error(hdl, EZFS_BADPROP,
1174 * If normalization was chosen, but no UTF8 choice was made,
1175 * enforce rejection of non-UTF8 names.
1177 * If normalization was chosen, but rejecting non-UTF8 names
1178 * was explicitly not chosen, it is an error.
1180 if (chosen_normal > 0 && chosen_utf < 0) {
1181 if (nvlist_add_uint64(ret,
1182 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1183 (void) no_memory(hdl);
1186 } else if (chosen_normal > 0 && chosen_utf == 0) {
1187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1188 "'%s' must be set 'on' if normalization chosen"),
1189 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1190 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1195 * If this is an existing volume, and someone is setting the volsize,
1196 * make sure that it matches the reservation, or add it if necessary.
1198 if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
1199 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1201 uint64_t old_volsize = zfs_prop_get_int(zhp,
1203 uint64_t old_reservation;
1204 uint64_t new_reservation;
1205 zfs_prop_t resv_prop;
1207 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1209 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1211 if (old_volsize == old_reservation &&
1212 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
1213 &new_reservation) != 0) {
1214 if (nvlist_add_uint64(ret,
1215 zfs_prop_to_name(resv_prop), intval) != 0) {
1216 (void) no_memory(hdl);
1229 * Given a property name and value, set the property for the given dataset.
1232 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1234 zfs_cmd_t zc = { 0 };
1236 prop_changelist_t *cl = NULL;
1238 libzfs_handle_t *hdl = zhp->zfs_hdl;
1239 nvlist_t *nvl = NULL, *realprops;
1241 boolean_t do_prefix;
1244 (void) snprintf(errbuf, sizeof (errbuf),
1245 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1248 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1249 nvlist_add_string(nvl, propname, propval) != 0) {
1250 (void) no_memory(hdl);
1254 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1255 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1261 prop = zfs_name_to_prop(propname);
1263 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1266 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1268 "child dataset with inherited mountpoint is used "
1269 "in a non-global zone"));
1270 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1275 * If the dataset's canmount property is being set to noauto,
1276 * then we want to prevent unmounting & remounting it.
1278 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1279 (zprop_string_to_index(prop, propval, &idx,
1280 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1282 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1286 * Execute the corresponding ioctl() to set this property.
1288 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1290 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1293 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1300 * For quotas and reservations, ENOSPC indicates
1301 * something different; setting a quota or reservation
1302 * doesn't use any disk space.
1305 case ZFS_PROP_QUOTA:
1306 case ZFS_PROP_REFQUOTA:
1307 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1308 "size is less than current used or "
1310 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1313 case ZFS_PROP_RESERVATION:
1314 case ZFS_PROP_REFRESERVATION:
1315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1316 "size is greater than available space"));
1317 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1321 (void) zfs_standard_error(hdl, errno, errbuf);
1327 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1328 (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
1330 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1334 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1339 "pool and or dataset must be upgraded to set this "
1340 "property or value"));
1341 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1345 if (prop == ZFS_PROP_COMPRESSION) {
1346 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1347 "property setting is not allowed on "
1348 "bootable datasets"));
1349 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1351 (void) zfs_standard_error(hdl, errno, errbuf);
1357 * This platform can't address a volume this big.
1360 if (prop == ZFS_PROP_VOLSIZE) {
1361 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1367 (void) zfs_standard_error(hdl, errno, errbuf);
1371 ret = changelist_postfix(cl);
1374 * Refresh the statistics so the new property value
1378 (void) get_stats(zhp);
1383 zcmd_free_nvlists(&zc);
1385 changelist_free(cl);
1390 * Given a property, inherit the value from the parent dataset.
1393 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1395 zfs_cmd_t zc = { 0 };
1397 prop_changelist_t *cl;
1398 libzfs_handle_t *hdl = zhp->zfs_hdl;
1402 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1403 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1405 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1407 * For user properties, the amount of work we have to do is very
1408 * small, so just do it here.
1410 if (!zfs_prop_user(propname)) {
1411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1412 "invalid property"));
1413 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1416 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1417 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1419 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1420 return (zfs_standard_error(hdl, errno, errbuf));
1426 * Verify that this property is inheritable.
1428 if (zfs_prop_readonly(prop))
1429 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1431 if (!zfs_prop_inheritable(prop))
1432 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1435 * Check to see if the value applies to this type
1437 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1438 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1441 * Normalize the name, to get rid of shorthand abbrevations.
1443 propname = zfs_prop_to_name(prop);
1444 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1445 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1447 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1448 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1450 "dataset is used in a non-global zone"));
1451 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1455 * Determine datasets which will be affected by this change, if any.
1457 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1460 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1462 "child dataset with inherited mountpoint is used "
1463 "in a non-global zone"));
1464 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1468 if ((ret = changelist_prefix(cl)) != 0)
1471 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1472 return (zfs_standard_error(hdl, errno, errbuf));
1475 if ((ret = changelist_postfix(cl)) != 0)
1479 * Refresh the statistics so the new property is reflected.
1481 (void) get_stats(zhp);
1485 changelist_free(cl);
1490 * True DSL properties are stored in an nvlist. The following two functions
1491 * extract them appropriately.
1494 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1500 if (nvlist_lookup_nvlist(zhp->zfs_props,
1501 zfs_prop_to_name(prop), &nv) == 0) {
1502 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1503 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1505 verify(!zhp->zfs_props_table ||
1506 zhp->zfs_props_table[prop] == B_TRUE);
1507 value = zfs_prop_default_numeric(prop);
1515 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1521 if (nvlist_lookup_nvlist(zhp->zfs_props,
1522 zfs_prop_to_name(prop), &nv) == 0) {
1523 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1524 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1526 verify(!zhp->zfs_props_table ||
1527 zhp->zfs_props_table[prop] == B_TRUE);
1528 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1537 * Internal function for getting a numeric property. Both zfs_prop_get() and
1538 * zfs_prop_get_int() are built using this interface.
1540 * Certain properties can be overridden using 'mount -o'. In this case, scan
1541 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1542 * If they differ from the on-disk values, report the current values and mark
1543 * the source "temporary".
1546 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1547 char **source, uint64_t *val)
1549 zfs_cmd_t zc = { 0 };
1550 nvlist_t *zplprops = NULL;
1552 char *mntopt_on = NULL;
1553 char *mntopt_off = NULL;
1558 case ZFS_PROP_ATIME:
1559 mntopt_on = MNTOPT_ATIME;
1560 mntopt_off = MNTOPT_NOATIME;
1563 case ZFS_PROP_DEVICES:
1564 mntopt_on = MNTOPT_DEVICES;
1565 mntopt_off = MNTOPT_NODEVICES;
1569 mntopt_on = MNTOPT_EXEC;
1570 mntopt_off = MNTOPT_NOEXEC;
1573 case ZFS_PROP_READONLY:
1574 mntopt_on = MNTOPT_RO;
1575 mntopt_off = MNTOPT_RW;
1578 case ZFS_PROP_SETUID:
1579 mntopt_on = MNTOPT_SETUID;
1580 mntopt_off = MNTOPT_NOSETUID;
1583 case ZFS_PROP_XATTR:
1584 mntopt_on = MNTOPT_XATTR;
1585 mntopt_off = MNTOPT_NOXATTR;
1588 case ZFS_PROP_NBMAND:
1589 mntopt_on = MNTOPT_NBMAND;
1590 mntopt_off = MNTOPT_NONBMAND;
1595 * Because looking up the mount options is potentially expensive
1596 * (iterating over all of /etc/mnttab), we defer its calculation until
1597 * we're looking up a property which requires its presence.
1599 if (!zhp->zfs_mntcheck &&
1600 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1601 libzfs_handle_t *hdl = zhp->zfs_hdl;
1602 struct mnttab entry;
1604 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1605 zhp->zfs_mntopts = zfs_strdup(hdl,
1607 if (zhp->zfs_mntopts == NULL)
1611 zhp->zfs_mntcheck = B_TRUE;
1614 if (zhp->zfs_mntopts == NULL)
1615 mnt.mnt_mntopts = "";
1617 mnt.mnt_mntopts = zhp->zfs_mntopts;
1620 case ZFS_PROP_ATIME:
1621 case ZFS_PROP_DEVICES:
1623 case ZFS_PROP_READONLY:
1624 case ZFS_PROP_SETUID:
1625 case ZFS_PROP_XATTR:
1626 case ZFS_PROP_NBMAND:
1627 *val = getprop_uint64(zhp, prop, source);
1629 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1632 *src = ZPROP_SRC_TEMPORARY;
1633 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1636 *src = ZPROP_SRC_TEMPORARY;
1640 case ZFS_PROP_CANMOUNT:
1641 *val = getprop_uint64(zhp, prop, source);
1642 if (*val != ZFS_CANMOUNT_ON)
1643 *source = zhp->zfs_name;
1645 *source = ""; /* default */
1648 case ZFS_PROP_QUOTA:
1649 case ZFS_PROP_REFQUOTA:
1650 case ZFS_PROP_RESERVATION:
1651 case ZFS_PROP_REFRESERVATION:
1652 *val = getprop_uint64(zhp, prop, source);
1654 *source = ""; /* default */
1656 *source = zhp->zfs_name;
1659 case ZFS_PROP_MOUNTED:
1660 *val = (zhp->zfs_mntopts != NULL);
1663 case ZFS_PROP_NUMCLONES:
1664 *val = zhp->zfs_dmustats.dds_num_clones;
1667 case ZFS_PROP_VERSION:
1668 case ZFS_PROP_NORMALIZE:
1669 case ZFS_PROP_UTF8ONLY:
1671 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1672 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1674 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1675 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1676 zcmd_free_nvlists(&zc);
1677 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1678 "unable to get %s property"),
1679 zfs_prop_to_name(prop));
1680 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
1681 dgettext(TEXT_DOMAIN, "internal error")));
1683 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1684 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1686 zcmd_free_nvlists(&zc);
1687 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1688 "unable to get %s property"),
1689 zfs_prop_to_name(prop));
1690 return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
1691 dgettext(TEXT_DOMAIN, "internal error")));
1694 nvlist_free(zplprops);
1695 zcmd_free_nvlists(&zc);
1699 switch (zfs_prop_get_type(prop)) {
1700 case PROP_TYPE_NUMBER:
1701 case PROP_TYPE_INDEX:
1702 *val = getprop_uint64(zhp, prop, source);
1704 * If we tried to use a default value for a
1705 * readonly property, it means that it was not
1706 * present; return an error.
1708 if (zfs_prop_readonly(prop) &&
1709 *source && (*source)[0] == '\0') {
1714 case PROP_TYPE_STRING:
1716 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1717 "cannot get non-numeric property"));
1718 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1719 dgettext(TEXT_DOMAIN, "internal error")));
1727 * Calculate the source type, given the raw source string.
1730 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1731 char *statbuf, size_t statlen)
1733 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1736 if (source == NULL) {
1737 *srctype = ZPROP_SRC_NONE;
1738 } else if (source[0] == '\0') {
1739 *srctype = ZPROP_SRC_DEFAULT;
1741 if (strcmp(source, zhp->zfs_name) == 0) {
1742 *srctype = ZPROP_SRC_LOCAL;
1744 (void) strlcpy(statbuf, source, statlen);
1745 *srctype = ZPROP_SRC_INHERITED;
1752 * Retrieve a property from the given object. If 'literal' is specified, then
1753 * numbers are left as exact values. Otherwise, numbers are converted to a
1754 * human-readable form.
1756 * Returns 0 on success, or -1 on error.
1759 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1760 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1762 char *source = NULL;
1768 * Check to see if this property applies to our object
1770 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1774 *src = ZPROP_SRC_NONE;
1777 case ZFS_PROP_CREATION:
1779 * 'creation' is a time_t stored in the statistics. We convert
1780 * this into a string unless 'literal' is specified.
1783 val = getprop_uint64(zhp, prop, &source);
1784 time_t time = (time_t)val;
1788 localtime_r(&time, &t) == NULL ||
1789 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1791 (void) snprintf(propbuf, proplen, "%llu", val);
1795 case ZFS_PROP_MOUNTPOINT:
1797 * Getting the precise mountpoint can be tricky.
1799 * - for 'none' or 'legacy', return those values.
1800 * - for inherited mountpoints, we want to take everything
1801 * after our ancestor and append it to the inherited value.
1803 * If the pool has an alternate root, we want to prepend that
1804 * root to any values we return.
1807 str = getprop_string(zhp, prop, &source);
1809 if (str[0] == '/') {
1810 char buf[MAXPATHLEN];
1812 const char *relpath = zhp->zfs_name + strlen(source);
1814 if (relpath[0] == '/')
1817 if ((zpool_get_prop(zhp->zpool_hdl,
1818 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1819 (strcmp(root, "-") == 0))
1822 * Special case an alternate root of '/'. This will
1823 * avoid having multiple leading slashes in the
1826 if (strcmp(root, "/") == 0)
1830 * If the mountpoint is '/' then skip over this
1831 * if we are obtaining either an alternate root or
1832 * an inherited mountpoint.
1834 if (str[1] == '\0' && (root[0] != '\0' ||
1835 relpath[0] != '\0'))
1838 if (relpath[0] == '\0')
1839 (void) snprintf(propbuf, proplen, "%s%s",
1842 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1843 root, str, relpath[0] == '@' ? "" : "/",
1846 /* 'legacy' or 'none' */
1847 (void) strlcpy(propbuf, str, proplen);
1852 case ZFS_PROP_ORIGIN:
1853 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1856 * If there is no parent at all, return failure to indicate that
1857 * it doesn't apply to this dataset.
1859 if (propbuf[0] == '\0')
1863 case ZFS_PROP_QUOTA:
1864 case ZFS_PROP_REFQUOTA:
1865 case ZFS_PROP_RESERVATION:
1866 case ZFS_PROP_REFRESERVATION:
1868 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1872 * If quota or reservation is 0, we translate this into 'none'
1873 * (unless literal is set), and indicate that it's the default
1874 * value. Otherwise, we print the number nicely and indicate
1875 * that its set locally.
1879 (void) strlcpy(propbuf, "0", proplen);
1881 (void) strlcpy(propbuf, "none", proplen);
1884 (void) snprintf(propbuf, proplen, "%llu",
1887 zfs_nicenum(val, propbuf, proplen);
1891 case ZFS_PROP_COMPRESSRATIO:
1892 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1894 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
1895 val / 100, (longlong_t)val % 100);
1899 switch (zhp->zfs_type) {
1900 case ZFS_TYPE_FILESYSTEM:
1903 case ZFS_TYPE_VOLUME:
1906 case ZFS_TYPE_SNAPSHOT:
1912 (void) snprintf(propbuf, proplen, "%s", str);
1915 case ZFS_PROP_MOUNTED:
1917 * The 'mounted' property is a pseudo-property that described
1918 * whether the filesystem is currently mounted. Even though
1919 * it's a boolean value, the typical values of "on" and "off"
1920 * don't make sense, so we translate to "yes" and "no".
1922 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
1923 src, &source, &val) != 0)
1926 (void) strlcpy(propbuf, "yes", proplen);
1928 (void) strlcpy(propbuf, "no", proplen);
1933 * The 'name' property is a pseudo-property derived from the
1934 * dataset name. It is presented as a real property to simplify
1937 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
1941 switch (zfs_prop_get_type(prop)) {
1942 case PROP_TYPE_NUMBER:
1943 if (get_numeric_property(zhp, prop, src,
1944 &source, &val) != 0)
1947 (void) snprintf(propbuf, proplen, "%llu",
1950 zfs_nicenum(val, propbuf, proplen);
1953 case PROP_TYPE_STRING:
1954 (void) strlcpy(propbuf,
1955 getprop_string(zhp, prop, &source), proplen);
1958 case PROP_TYPE_INDEX:
1959 if (get_numeric_property(zhp, prop, src,
1960 &source, &val) != 0)
1962 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
1964 (void) strlcpy(propbuf, strval, proplen);
1972 get_source(zhp, src, source, statbuf, statlen);
1978 * Utility function to get the given numeric property. Does no validation that
1979 * the given property is the appropriate type; should only be used with
1980 * hard-coded property types.
1983 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1988 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
1994 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
1998 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
1999 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2003 * Similar to zfs_prop_get(), but returns the value as an integer.
2006 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2007 zprop_source_t *src, char *statbuf, size_t statlen)
2012 * Check to see if this property applies to our object
2014 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2015 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2016 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2017 zfs_prop_to_name(prop)));
2021 *src = ZPROP_SRC_NONE;
2023 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2026 get_source(zhp, src, source, statbuf, statlen);
2032 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2033 char **domainp, idmap_rid_t *ridp)
2035 idmap_handle_t *idmap_hdl = NULL;
2036 idmap_get_handle_t *get_hdl = NULL;
2040 if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
2042 if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
2046 err = idmap_get_sidbyuid(get_hdl, id,
2047 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2049 err = idmap_get_sidbygid(get_hdl, id,
2050 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2052 if (err == IDMAP_SUCCESS &&
2053 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2054 status == IDMAP_SUCCESS)
2060 idmap_get_destroy(get_hdl);
2062 (void) idmap_fini(idmap_hdl);
2067 * convert the propname into parameters needed by kernel
2068 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2069 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2072 userquota_propname_decode(const char *propname, boolean_t zoned,
2073 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2075 zfs_userquota_prop_t type;
2081 /* Figure out the property type ({user|group}{quota|space}) */
2082 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2083 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2084 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2087 if (type == ZFS_NUM_USERQUOTA_PROPS)
2091 isuser = (type == ZFS_PROP_USERQUOTA ||
2092 type == ZFS_PROP_USERUSED);
2094 cp = strchr(propname, '@') + 1;
2096 if (strchr(cp, '@')) {
2098 * It's a SID name (eg "user@domain") that needs to be
2099 * turned into S-1-domainID-RID. There should be a
2100 * better way to do this, but for now just translate it
2101 * to the (possibly ephemeral) uid and then back to the
2102 * SID. This is like getsidname(noresolve=TRUE).
2108 if (zoned && getzoneid() == GLOBAL_ZONEID)
2110 if (sid_to_id(cp, isuser, &id) != 0)
2112 if (idmap_id_to_numeric_domain_rid(id, isuser,
2113 &mapdomain, &rid) != 0)
2115 (void) strlcpy(domain, mapdomain, domainlen);
2117 } else if (strncmp(cp, "S-1-", 4) == 0) {
2118 /* It's a numeric SID (eg "S-1-234-567-89") */
2119 (void) strcpy(domain, cp);
2120 cp = strrchr(domain, '-');
2125 *ridp = strtoull(cp, &end, 10);
2126 if (errno != 0 || *end != '\0')
2128 } else if (!isdigit(*cp)) {
2130 * It's a user/group name (eg "user") that needs to be
2131 * turned into a uid/gid
2133 if (zoned && getzoneid() == GLOBAL_ZONEID)
2149 /* It's a user/group ID (eg "12345"). */
2150 uid_t id = strtoul(cp, &end, 10);
2157 /* It's an ephemeral ID. */
2158 if (idmap_id_to_numeric_domain_rid(id, isuser,
2159 &mapdomain, &rid) != 0)
2161 (void) strcpy(domain, mapdomain);
2172 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2173 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2176 zfs_cmd_t zc = { 0 };
2178 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2180 err = userquota_propname_decode(propname,
2181 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2182 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2183 zc.zc_objset_type = *typep;
2187 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2191 *propvalue = zc.zc_cookie;
2196 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2197 uint64_t *propvalue)
2199 zfs_userquota_prop_t type;
2201 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2206 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2207 char *propbuf, int proplen, boolean_t literal)
2211 zfs_userquota_prop_t type;
2213 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2220 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2221 } else if (propvalue == 0 &&
2222 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2223 (void) strlcpy(propbuf, "none", proplen);
2225 zfs_nicenum(propvalue, propbuf, proplen);
2231 * Returns the name of the given zfs handle.
2234 zfs_get_name(const zfs_handle_t *zhp)
2236 return (zhp->zfs_name);
2240 * Returns the type of the given zfs handle.
2243 zfs_get_type(const zfs_handle_t *zhp)
2245 return (zhp->zfs_type);
2249 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2252 uint64_t orig_cookie;
2254 orig_cookie = zc->zc_cookie;
2256 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2257 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2262 /* expand nvlist memory and try again */
2263 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2264 zcmd_free_nvlists(zc);
2267 zc->zc_cookie = orig_cookie;
2270 * An errno value of ESRCH indicates normal completion.
2271 * If ENOENT is returned, then the underlying dataset
2272 * has been removed since we obtained the handle.
2279 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2280 dgettext(TEXT_DOMAIN,
2281 "cannot iterate filesystems"));
2289 * Iterate over all child filesystems
2292 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2294 zfs_cmd_t zc = { 0 };
2298 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2301 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2304 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2307 * Silently ignore errors, as the only plausible explanation is
2308 * that the pool has since been removed.
2310 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2315 if ((ret = func(nzhp, data)) != 0) {
2316 zcmd_free_nvlists(&zc);
2320 zcmd_free_nvlists(&zc);
2321 return ((ret < 0) ? ret : 0);
2325 * Iterate over all snapshots
2328 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2330 zfs_cmd_t zc = { 0 };
2334 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2337 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2339 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2342 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2347 if ((ret = func(nzhp, data)) != 0) {
2348 zcmd_free_nvlists(&zc);
2352 zcmd_free_nvlists(&zc);
2353 return ((ret < 0) ? ret : 0);
2357 * Iterate over all children, snapshots and filesystems
2360 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2364 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2367 return (zfs_iter_snapshots(zhp, func, data));
2371 * Given a complete name, return just the portion that refers to the parent.
2372 * Can return NULL if this is a pool.
2375 parent_name(const char *path, char *buf, size_t buflen)
2379 if ((loc = strrchr(path, '/')) == NULL)
2382 (void) strncpy(buf, path, MIN(buflen, loc - path));
2383 buf[loc - path] = '\0';
2389 * If accept_ancestor is false, then check to make sure that the given path has
2390 * a parent, and that it exists. If accept_ancestor is true, then find the
2391 * closest existing ancestor for the given path. In prefixlen return the
2392 * length of already existing prefix of the given path. We also fetch the
2393 * 'zoned' property, which is used to validate property settings when creating
2397 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2398 boolean_t accept_ancestor, int *prefixlen)
2400 zfs_cmd_t zc = { 0 };
2401 char parent[ZFS_MAXNAMELEN];
2406 (void) snprintf(errbuf, sizeof (errbuf),
2407 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2409 /* get parent, and check to see if this is just a pool */
2410 if (parent_name(path, parent, sizeof (parent)) != 0) {
2411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2412 "missing dataset name"));
2413 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2416 /* check to see if the pool exists */
2417 if ((slash = strchr(parent, '/')) == NULL)
2418 slash = parent + strlen(parent);
2419 (void) strncpy(zc.zc_name, parent, slash - parent);
2420 zc.zc_name[slash - parent] = '\0';
2421 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2423 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2424 "no such pool '%s'"), zc.zc_name);
2425 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2428 /* check to see if the parent dataset exists */
2429 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2430 if (errno == ENOENT && accept_ancestor) {
2432 * Go deeper to find an ancestor, give up on top level.
2434 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2436 "no such pool '%s'"), zc.zc_name);
2437 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2439 } else if (errno == ENOENT) {
2440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2441 "parent does not exist"));
2442 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2444 return (zfs_standard_error(hdl, errno, errbuf));
2447 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2448 /* we are in a non-global zone, but parent is in the global zone */
2449 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
2450 (void) zfs_standard_error(hdl, EPERM, errbuf);
2455 /* make sure parent is a filesystem */
2456 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2458 "parent is not a filesystem"));
2459 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2465 if (prefixlen != NULL)
2466 *prefixlen = strlen(parent);
2471 * Finds whether the dataset of the given type(s) exists.
2474 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2478 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2482 * Try to get stats for the dataset, which will tell us if it exists.
2484 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2485 int ds_type = zhp->zfs_type;
2488 if (types & ds_type)
2495 * Given a path to 'target', create all the ancestors between
2496 * the prefixlen portion of the path, and the target itself.
2497 * Fail if the initial prefixlen-ancestor does not already exist.
2500 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2506 /* make sure prefix exists */
2507 cp = target + prefixlen;
2509 assert(strchr(cp, '/') == NULL);
2510 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2513 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2521 * Attempt to create, mount, and share any ancestor filesystems,
2522 * up to the prefixlen-long one.
2524 for (cp = target + prefixlen + 1;
2525 cp = strchr(cp, '/'); *cp = '/', cp++) {
2530 h = make_dataset_handle(hdl, target);
2532 /* it already exists, nothing to do here */
2537 logstr = hdl->libzfs_log_str;
2538 hdl->libzfs_log_str = NULL;
2539 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2541 hdl->libzfs_log_str = logstr;
2542 opname = dgettext(TEXT_DOMAIN, "create");
2546 hdl->libzfs_log_str = logstr;
2547 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2549 opname = dgettext(TEXT_DOMAIN, "open");
2553 if (zfs_mount(h, NULL, 0) != 0) {
2554 opname = dgettext(TEXT_DOMAIN, "mount");
2558 if (zfs_share(h) != 0) {
2559 opname = dgettext(TEXT_DOMAIN, "share");
2569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2570 "failed to %s ancestor '%s'"), opname, target);
2575 * Creates non-existing ancestors of the given path.
2578 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2585 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
2588 if ((path_copy = strdup(path)) != NULL) {
2589 rc = create_parents(hdl, path_copy, prefix);
2592 if (path_copy == NULL || rc != 0)
2599 * Create a new filesystem or volume.
2602 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2605 zfs_cmd_t zc = { 0 };
2608 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2612 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2613 "cannot create '%s'"), path);
2615 /* validate the path, taking care to note the extended error message */
2616 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2617 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2619 /* validate parents exist */
2620 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2624 * The failure modes when creating a dataset of a different type over
2625 * one that already exists is a little strange. In particular, if you
2626 * try to create a dataset on top of an existing dataset, the ioctl()
2627 * will return ENOENT, not EEXIST. To prevent this from happening, we
2628 * first try to see if the dataset exists.
2630 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2631 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2633 "dataset already exists"));
2634 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2637 if (type == ZFS_TYPE_VOLUME)
2638 zc.zc_objset_type = DMU_OST_ZVOL;
2640 zc.zc_objset_type = DMU_OST_ZFS;
2642 if (props && (props = zfs_valid_proplist(hdl, type, props,
2643 zoned, NULL, errbuf)) == 0)
2646 if (type == ZFS_TYPE_VOLUME) {
2648 * If we are creating a volume, the size and block size must
2649 * satisfy a few restraints. First, the blocksize must be a
2650 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2651 * volsize must be a multiple of the block size, and cannot be
2654 if (props == NULL || nvlist_lookup_uint64(props,
2655 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2658 "missing volume size"));
2659 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2662 if ((ret = nvlist_lookup_uint64(props,
2663 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2664 &blocksize)) != 0) {
2665 if (ret == ENOENT) {
2666 blocksize = zfs_prop_default_numeric(
2667 ZFS_PROP_VOLBLOCKSIZE);
2670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2671 "missing volume block size"));
2672 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2679 "volume size cannot be zero"));
2680 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2683 if (size % blocksize != 0) {
2685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2686 "volume size must be a multiple of volume block "
2688 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2692 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2696 /* create the dataset */
2697 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2699 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2700 ret = zvol_create_link(hdl, path);
2702 (void) zfs_standard_error(hdl, errno,
2703 dgettext(TEXT_DOMAIN,
2704 "Volume successfully created, but device links "
2705 "were not created"));
2706 zcmd_free_nvlists(&zc);
2711 zcmd_free_nvlists(&zc);
2713 /* check for failure */
2715 char parent[ZFS_MAXNAMELEN];
2716 (void) parent_name(path, parent, sizeof (parent));
2720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2721 "no such parent '%s'"), parent);
2722 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2726 "parent '%s' is not a filesystem"), parent);
2727 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2730 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2731 "volume block size must be power of 2 from "
2733 (uint_t)SPA_MINBLOCKSIZE,
2734 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2736 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2740 "pool must be upgraded to set this "
2741 "property or value"));
2742 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2746 * This platform can't address a volume this big.
2748 if (type == ZFS_TYPE_VOLUME)
2749 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2754 return (zfs_standard_error(hdl, errno, errbuf));
2762 * Destroys the given dataset. The caller must make sure that the filesystem
2763 * isn't mounted, and that there are no active dependents.
2766 zfs_destroy(zfs_handle_t *zhp)
2768 zfs_cmd_t zc = { 0 };
2770 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2772 if (ZFS_IS_VOLUME(zhp)) {
2774 * If user doesn't have permissions to unshare volume, then
2775 * abort the request. This would only happen for a
2776 * non-privileged user.
2778 if (zfs_unshare_iscsi(zhp) != 0) {
2782 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2785 zc.zc_objset_type = DMU_OST_ZVOL;
2787 zc.zc_objset_type = DMU_OST_ZFS;
2790 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2791 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2792 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2796 remove_mountpoint(zhp);
2801 struct destroydata {
2808 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
2810 struct destroydata *dd = arg;
2812 char name[ZFS_MAXNAMELEN];
2813 boolean_t closezhp = dd->closezhp;
2816 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
2817 (void) strlcat(name, "@", sizeof (name));
2818 (void) strlcat(name, dd->snapname, sizeof (name));
2820 szhp = make_dataset_handle(zhp->zfs_hdl, name);
2822 dd->gotone = B_TRUE;
2826 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
2827 (void) zvol_remove_link(zhp->zfs_hdl, name);
2829 * NB: this is simply a best-effort. We don't want to
2830 * return an error, because then we wouldn't visit all
2835 dd->closezhp = B_TRUE;
2836 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
2843 * Destroys all snapshots with the given name in zhp & descendants.
2846 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
2848 zfs_cmd_t zc = { 0 };
2850 struct destroydata dd = { 0 };
2852 dd.snapname = snapname;
2853 (void) zfs_remove_link_cb(zhp, &dd);
2856 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
2857 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
2858 zhp->zfs_name, snapname));
2861 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2862 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2864 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
2868 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2869 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
2873 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2874 "snapshot is cloned"));
2875 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
2878 return (zfs_standard_error(zhp->zfs_hdl, errno,
2887 * Clones the given dataset. The target must be of the same type as the source.
2890 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2892 zfs_cmd_t zc = { 0 };
2893 char parent[ZFS_MAXNAMELEN];
2896 libzfs_handle_t *hdl = zhp->zfs_hdl;
2900 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2902 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2903 "cannot create '%s'"), target);
2905 /* validate the target name */
2906 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
2907 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2909 /* validate parents exist */
2910 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2913 (void) parent_name(target, parent, sizeof (parent));
2916 if (ZFS_IS_VOLUME(zhp)) {
2917 zc.zc_objset_type = DMU_OST_ZVOL;
2918 type = ZFS_TYPE_VOLUME;
2920 zc.zc_objset_type = DMU_OST_ZFS;
2921 type = ZFS_TYPE_FILESYSTEM;
2925 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
2926 zhp, errbuf)) == NULL)
2929 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
2937 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
2938 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
2939 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
2941 zcmd_free_nvlists(&zc);
2948 * The parent doesn't exist. We should have caught this
2949 * above, but there may a race condition that has since
2950 * destroyed the parent.
2952 * At this point, we don't know whether it's the source
2953 * that doesn't exist anymore, or whether the target
2954 * dataset doesn't exist.
2956 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2957 "no such parent '%s'"), parent);
2958 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2961 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2962 "source and target pools differ"));
2963 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
2967 return (zfs_standard_error(zhp->zfs_hdl, errno,
2970 } else if (ZFS_IS_VOLUME(zhp)) {
2971 ret = zvol_create_link(zhp->zfs_hdl, target);
2977 typedef struct promote_data {
2978 char cb_mountpoint[MAXPATHLEN];
2979 const char *cb_target;
2980 const char *cb_errbuf;
2981 uint64_t cb_pivot_txg;
2985 promote_snap_cb(zfs_handle_t *zhp, void *data)
2987 promote_data_t *pd = data;
2989 char snapname[MAXPATHLEN];
2992 /* We don't care about snapshots after the pivot point */
2993 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
2998 /* Remove the device link if it's a zvol. */
2999 if (ZFS_IS_VOLUME(zhp))
3000 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3002 /* Check for conflicting names */
3003 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3004 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3005 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3008 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3009 "snapshot name '%s' from origin \n"
3010 "conflicts with '%s' from target"),
3011 zhp->zfs_name, snapname);
3012 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3019 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3021 promote_data_t *pd = data;
3023 /* We don't care about snapshots after the pivot point */
3024 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3025 /* Create the device link if it's a zvol. */
3026 if (ZFS_IS_VOLUME(zhp))
3027 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3035 * Promotes the given clone fs to be the clone parent.
3038 zfs_promote(zfs_handle_t *zhp)
3040 libzfs_handle_t *hdl = zhp->zfs_hdl;
3041 zfs_cmd_t zc = { 0 };
3042 char parent[MAXPATHLEN];
3049 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3050 "cannot promote '%s'"), zhp->zfs_name);
3052 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3054 "snapshots can not be promoted"));
3055 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3058 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3059 if (parent[0] == '\0') {
3060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3061 "not a cloned filesystem"));
3062 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3064 cp = strchr(parent, '@');
3067 /* Walk the snapshots we will be moving */
3068 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3071 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3073 pd.cb_target = zhp->zfs_name;
3074 pd.cb_errbuf = errbuf;
3075 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3078 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3079 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3080 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3086 /* issue the ioctl */
3087 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3088 sizeof (zc.zc_value));
3089 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3090 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3093 int save_errno = errno;
3095 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3098 switch (save_errno) {
3101 * There is a conflicting snapshot name. We
3102 * should have caught this above, but they could
3103 * have renamed something in the mean time.
3105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3106 "conflicting snapshot name from parent '%s'"),
3108 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3111 return (zfs_standard_error(hdl, save_errno, errbuf));
3114 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3122 const char *cd_snapname;
3127 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3129 struct createdata *cd = arg;
3132 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3133 char name[MAXPATHLEN];
3135 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3136 (void) strlcat(name, "@", sizeof (name));
3137 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3138 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3141 * NB: this is simply a best-effort. We don't want to
3142 * return an error, because then we wouldn't visit all
3147 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3155 * Takes a snapshot of the given dataset.
3158 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3162 char parent[ZFS_MAXNAMELEN];
3164 zfs_cmd_t zc = { 0 };
3168 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3169 "cannot snapshot '%s'"), path);
3171 /* validate the target name */
3172 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3173 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3176 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3177 props, B_FALSE, NULL, errbuf)) == NULL)
3180 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3188 /* make sure the parent exists and is of the appropriate type */
3189 delim = strchr(path, '@');
3190 (void) strncpy(parent, path, delim - path);
3191 parent[delim - path] = '\0';
3193 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3194 ZFS_TYPE_VOLUME)) == NULL) {
3195 zcmd_free_nvlists(&zc);
3199 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3200 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3201 if (ZFS_IS_VOLUME(zhp))
3202 zc.zc_objset_type = DMU_OST_ZVOL;
3204 zc.zc_objset_type = DMU_OST_ZFS;
3205 zc.zc_cookie = recursive;
3206 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3208 zcmd_free_nvlists(&zc);
3211 * if it was recursive, the one that actually failed will be in
3215 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3216 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3218 if (ret == 0 && recursive) {
3219 struct createdata cd;
3221 cd.cd_snapname = delim + 1;
3222 cd.cd_ifexists = B_FALSE;
3223 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3225 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3226 ret = zvol_create_link(zhp->zfs_hdl, path);
3228 (void) zfs_standard_error(hdl, errno,
3229 dgettext(TEXT_DOMAIN,
3230 "Volume successfully snapshotted, but device links "
3231 "were not created"));
3238 (void) zfs_standard_error(hdl, errno, errbuf);
3246 * Destroy any more recent snapshots. We invoke this callback on any dependents
3247 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3248 * is a dependent and we should just destroy it without checking the transaction
3251 typedef struct rollback_data {
3252 const char *cb_target; /* the snapshot */
3253 uint64_t cb_create; /* creation time reference */
3255 boolean_t cb_dependent;
3260 rollback_destroy(zfs_handle_t *zhp, void *data)
3262 rollback_data_t *cbp = data;
3264 if (!cbp->cb_dependent) {
3265 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3266 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3267 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3271 cbp->cb_dependent = B_TRUE;
3272 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3273 rollback_destroy, cbp);
3274 cbp->cb_dependent = B_FALSE;
3276 logstr = zhp->zfs_hdl->libzfs_log_str;
3277 zhp->zfs_hdl->libzfs_log_str = NULL;
3278 cbp->cb_error |= zfs_destroy(zhp);
3279 zhp->zfs_hdl->libzfs_log_str = logstr;
3282 /* We must destroy this clone; first unmount it */
3283 prop_changelist_t *clp;
3285 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3286 cbp->cb_force ? MS_FORCE: 0);
3287 if (clp == NULL || changelist_prefix(clp) != 0) {
3288 cbp->cb_error = B_TRUE;
3292 if (zfs_destroy(zhp) != 0)
3293 cbp->cb_error = B_TRUE;
3295 changelist_remove(clp, zhp->zfs_name);
3296 (void) changelist_postfix(clp);
3297 changelist_free(clp);
3305 * Given a dataset, rollback to a specific snapshot, discarding any
3306 * data changes since then and making it the active dataset.
3308 * Any snapshots more recent than the target are destroyed, along with
3312 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3314 rollback_data_t cb = { 0 };
3316 zfs_cmd_t zc = { 0 };
3317 boolean_t restore_resv = 0;
3318 uint64_t old_volsize, new_volsize;
3319 zfs_prop_t resv_prop;
3321 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3322 zhp->zfs_type == ZFS_TYPE_VOLUME);
3325 * Destroy all recent snapshots and its dependends.
3327 cb.cb_force = force;
3328 cb.cb_target = snap->zfs_name;
3329 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3330 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3336 * Now that we have verified that the snapshot is the latest,
3337 * rollback to the given snapshot.
3340 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3341 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3343 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3345 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3347 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3350 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3352 if (ZFS_IS_VOLUME(zhp))
3353 zc.zc_objset_type = DMU_OST_ZVOL;
3355 zc.zc_objset_type = DMU_OST_ZFS;
3358 * We rely on zfs_iter_children() to verify that there are no
3359 * newer snapshots for the given dataset. Therefore, we can
3360 * simply pass the name on to the ioctl() call. There is still
3361 * an unlikely race condition where the user has taken a
3362 * snapshot since we verified that this was the most recent.
3365 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3366 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3367 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3373 * For volumes, if the pre-rollback volsize matched the pre-
3374 * rollback reservation and the volsize has changed then set
3375 * the reservation property to the post-rollback volsize.
3376 * Make a new handle since the rollback closed the dataset.
3378 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3379 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3380 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
3385 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3386 if (old_volsize != new_volsize)
3387 err = zfs_prop_set_int(zhp, resv_prop,
3396 * Iterate over all dependents for a given dataset. This includes both
3397 * hierarchical dependents (children) and data dependents (snapshots and
3398 * clones). The bulk of the processing occurs in get_dependents() in
3402 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3403 zfs_iter_f func, void *data)
3408 zfs_handle_t *child;
3411 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3412 &dependents, &count) != 0)
3415 for (i = 0; i < count; i++) {
3416 if ((child = make_dataset_handle(zhp->zfs_hdl,
3417 dependents[i])) == NULL)
3420 if ((ret = func(child, data)) != 0)
3424 for (i = 0; i < count; i++)
3425 free(dependents[i]);
3432 * Renames the given dataset.
3435 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3438 zfs_cmd_t zc = { 0 };
3440 prop_changelist_t *cl = NULL;
3441 zfs_handle_t *zhrp = NULL;
3442 char *parentname = NULL;
3443 char parent[ZFS_MAXNAMELEN];
3444 libzfs_handle_t *hdl = zhp->zfs_hdl;
3447 /* if we have the same exact name, just return success */
3448 if (strcmp(zhp->zfs_name, target) == 0)
3451 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3452 "cannot rename to '%s'"), target);
3455 * Make sure the target name is valid
3457 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3458 if ((strchr(target, '@') == NULL) ||
3461 * Snapshot target name is abbreviated,
3462 * reconstruct full dataset name
3464 (void) strlcpy(parent, zhp->zfs_name,
3466 delim = strchr(parent, '@');
3467 if (strchr(target, '@') == NULL)
3471 (void) strlcat(parent, target, sizeof (parent));
3475 * Make sure we're renaming within the same dataset.
3477 delim = strchr(target, '@');
3478 if (strncmp(zhp->zfs_name, target, delim - target)
3479 != 0 || zhp->zfs_name[delim - target] != '@') {
3480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3481 "snapshots must be part of same "
3483 return (zfs_error(hdl, EZFS_CROSSTARGET,
3487 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3488 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3492 "recursive rename must be a snapshot"));
3493 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3496 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3497 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3500 /* validate parents */
3501 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3504 (void) parent_name(target, parent, sizeof (parent));
3506 /* make sure we're in the same pool */
3507 verify((delim = strchr(target, '/')) != NULL);
3508 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3509 zhp->zfs_name[delim - target] != '/') {
3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3511 "datasets must be within same pool"));
3512 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3515 /* new name cannot be a child of the current dataset name */
3516 if (strncmp(parent, zhp->zfs_name,
3517 strlen(zhp->zfs_name)) == 0) {
3518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3519 "New dataset name cannot be a descendent of "
3520 "current dataset name"));
3521 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3525 (void) snprintf(errbuf, sizeof (errbuf),
3526 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3528 if (getzoneid() == GLOBAL_ZONEID &&
3529 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3531 "dataset is used in a non-global zone"));
3532 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3536 struct destroydata dd;
3538 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3539 if (parentname == NULL) {
3543 delim = strchr(parentname, '@');
3545 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3551 dd.snapname = delim + 1;
3552 dd.gotone = B_FALSE;
3553 dd.closezhp = B_TRUE;
3555 /* We remove any zvol links prior to renaming them */
3556 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
3561 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3564 if (changelist_haszonedchild(cl)) {
3565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3566 "child dataset with inherited mountpoint is used "
3567 "in a non-global zone"));
3568 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3572 if ((ret = changelist_prefix(cl)) != 0)
3576 if (ZFS_IS_VOLUME(zhp))
3577 zc.zc_objset_type = DMU_OST_ZVOL;
3579 zc.zc_objset_type = DMU_OST_ZFS;
3581 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3582 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3584 zc.zc_cookie = recursive;
3586 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3588 * if it was recursive, the one that actually failed will
3591 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3592 "cannot rename '%s'"), zc.zc_name);
3594 if (recursive && errno == EEXIST) {
3595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3596 "a child dataset already has a snapshot "
3597 "with the new name"));
3598 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3600 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3604 * On failure, we still want to remount any filesystems that
3605 * were previously mounted, so we don't alter the system state.
3608 struct createdata cd;
3610 /* only create links for datasets that had existed */
3611 cd.cd_snapname = delim + 1;
3612 cd.cd_ifexists = B_TRUE;
3613 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3616 (void) changelist_postfix(cl);
3620 struct createdata cd;
3622 /* only create links for datasets that had existed */
3623 cd.cd_snapname = strchr(target, '@') + 1;
3624 cd.cd_ifexists = B_TRUE;
3625 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3628 changelist_rename(cl, zfs_get_name(zhp), target);
3629 ret = changelist_postfix(cl);
3641 changelist_free(cl);
3647 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3648 * poke devfsadm to create the /dev link, and then wait for the link to appear.
3651 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3653 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3657 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3659 zfs_cmd_t zc = { 0 };
3660 di_devlink_handle_t dhdl;
3661 priv_set_t *priv_effective;
3664 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3667 * Issue the appropriate ioctl.
3669 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3673 * Silently ignore the case where the link already
3674 * exists. This allows 'zfs volinit' to be run multiple
3675 * times without errors.
3681 * Dataset does not exist in the kernel. If we
3682 * don't care (see zfs_rename), then ignore the
3692 return (zfs_standard_error_fmt(hdl, errno,
3693 dgettext(TEXT_DOMAIN, "cannot create device links "
3694 "for '%s'"), dataset));
3699 * If privileged call devfsadm and wait for the links to
3701 * Otherwise, print out an informational message.
3704 priv_effective = priv_allocset();
3705 (void) getppriv(PRIV_EFFECTIVE, priv_effective);
3706 privileged = (priv_isfullset(priv_effective) == B_TRUE);
3707 priv_freeset(priv_effective);
3710 if ((dhdl = di_devlink_init(ZFS_DRIVER,
3711 DI_MAKE_LINK)) == NULL) {
3712 zfs_error_aux(hdl, strerror(errno));
3713 (void) zfs_error_fmt(hdl, errno,
3714 dgettext(TEXT_DOMAIN, "cannot create device links "
3715 "for '%s'"), dataset);
3716 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
3719 (void) di_devlink_fini(&dhdl);
3722 char pathname[MAXPATHLEN];
3723 struct stat64 statbuf;
3729 * This is the poor mans way of waiting for the link
3730 * to show up. If after 10 seconds we still don't
3731 * have it, then print out a message.
3733 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
3736 for (i = 0; i != MAX_WAIT; i++) {
3737 if (stat64(pathname, &statbuf) == 0)
3742 (void) printf(gettext("%s may not be immediately "
3743 "available\n"), pathname);
3750 * Remove a minor node for the given zvol and the associated /dev links.
3753 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3755 zfs_cmd_t zc = { 0 };
3757 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3759 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3763 * Silently ignore the case where the link no longer
3764 * exists, so that 'zfs volfini' can be run multiple
3765 * times without errors.
3770 return (zfs_standard_error_fmt(hdl, errno,
3771 dgettext(TEXT_DOMAIN, "cannot remove device "
3772 "links for '%s'"), dataset));
3780 zfs_get_user_props(zfs_handle_t *zhp)
3782 return (zhp->zfs_user_props);
3786 * This function is used by 'zfs list' to determine the exact set of columns to
3787 * display, and their maximum widths. This does two main things:
3789 * - If this is a list of all properties, then expand the list to include
3790 * all native properties, and set a flag so that for each dataset we look
3791 * for new unique user properties and add them to the list.
3793 * - For non fixed-width properties, keep track of the maximum width seen
3794 * so that we can size the column appropriately.
3797 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
3799 libzfs_handle_t *hdl = zhp->zfs_hdl;
3800 zprop_list_t *entry;
3801 zprop_list_t **last, **start;
3802 nvlist_t *userprops, *propval;
3805 char buf[ZFS_MAXPROPLEN];
3807 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3810 userprops = zfs_get_user_props(zhp);
3813 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3815 * Go through and add any user properties as necessary. We
3816 * start by incrementing our list pointer to the first
3817 * non-native property.
3820 while (*start != NULL) {
3821 if ((*start)->pl_prop == ZPROP_INVAL)
3823 start = &(*start)->pl_next;
3827 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3829 * See if we've already found this property in our list.
3831 for (last = start; *last != NULL;
3832 last = &(*last)->pl_next) {
3833 if (strcmp((*last)->pl_user_prop,
3834 nvpair_name(elem)) == 0)
3838 if (*last == NULL) {
3839 if ((entry = zfs_alloc(hdl,
3840 sizeof (zprop_list_t))) == NULL ||
3841 ((entry->pl_user_prop = zfs_strdup(hdl,
3842 nvpair_name(elem)))) == NULL) {
3847 entry->pl_prop = ZPROP_INVAL;
3848 entry->pl_width = strlen(nvpair_name(elem));
3849 entry->pl_all = B_TRUE;
3856 * Now go through and check the width of any non-fixed columns
3858 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3859 if (entry->pl_fixed)
3862 if (entry->pl_prop != ZPROP_INVAL) {
3863 if (zfs_prop_get(zhp, entry->pl_prop,
3864 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3865 if (strlen(buf) > entry->pl_width)
3866 entry->pl_width = strlen(buf);
3868 } else if (nvlist_lookup_nvlist(userprops,
3869 entry->pl_user_prop, &propval) == 0) {
3870 verify(nvlist_lookup_string(propval,
3871 ZPROP_VALUE, &strval) == 0);
3872 if (strlen(strval) > entry->pl_width)
3873 entry->pl_width = strlen(strval);
3881 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
3883 zfs_cmd_t zc = { 0 };
3887 const gid_t *groups;
3891 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
3892 return (no_memory(hdl));
3894 uid = ucred_geteuid(cred);
3895 gid = ucred_getegid(cred);
3896 group_cnt = ucred_getgroups(cred, &groups);
3898 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
3901 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
3906 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
3911 if (nvlist_add_uint32_array(nvp,
3912 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
3916 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3918 if (zcmd_write_src_nvlist(hdl, &zc, nvp))
3921 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
3927 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3928 char *resource, void *export, void *sharetab,
3929 int sharemax, zfs_share_op_t operation)
3931 zfs_cmd_t zc = { 0 };
3934 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3935 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3937 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3938 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3939 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3940 zc.zc_share.z_sharetype = operation;
3941 zc.zc_share.z_sharemax = sharemax;
3942 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3947 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3952 * Keep a reference to the props-table against which we prune the
3955 zhp->zfs_props_table = props;
3957 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3960 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3961 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3964 * We leave user:props in the nvlist, so there will be
3965 * some ZPROP_INVAL. To be extra safe, don't prune
3968 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3969 (void) nvlist_remove(zhp->zfs_props,
3970 nvpair_name(curr), nvpair_type(curr));
3976 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3977 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3979 zfs_cmd_t zc = { 0 };
3980 nvlist_t *nvlist = NULL;
3983 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3984 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3985 zc.zc_cookie = (uint64_t)cmd;
3987 if (cmd == ZFS_SMB_ACL_RENAME) {
3988 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3989 (void) no_memory(hdl);
3995 case ZFS_SMB_ACL_ADD:
3996 case ZFS_SMB_ACL_REMOVE:
3997 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3999 case ZFS_SMB_ACL_RENAME:
4000 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4002 (void) no_memory(hdl);
4005 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4007 (void) no_memory(hdl);
4010 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4011 nvlist_free(nvlist);
4015 case ZFS_SMB_ACL_PURGE:
4020 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4022 nvlist_free(nvlist);
4027 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4028 char *path, char *resource)
4030 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4035 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4036 char *path, char *resource)
4038 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4043 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4045 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4050 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4051 char *oldname, char *newname)
4053 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4058 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4059 zfs_userspace_cb_t func, void *arg)
4061 zfs_cmd_t zc = { 0 };
4063 zfs_useracct_t buf[100];
4065 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4067 zc.zc_objset_type = type;
4068 zc.zc_nvlist_dst = (uintptr_t)buf;
4072 zfs_useracct_t *zua = buf;
4074 zc.zc_nvlist_dst_size = sizeof (buf);
4075 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4076 ZFS_IOC_USERSPACE_MANY, &zc);
4077 if (error || zc.zc_nvlist_dst_size == 0)
4080 while (zc.zc_nvlist_dst_size > 0) {
4081 error = func(arg, zua->zu_domain, zua->zu_rid,
4086 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);