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.
39 #include <sys/mntent.h>
40 #include <sys/mount.h>
53 #include "zfs_namecheck.h"
55 #include "libzfs_impl.h"
56 #include "zfs_deleg.h"
58 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
59 static int userquota_propname_decode(const char *propname, boolean_t zoned,
60 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
63 * Given a single type (not a mask of types), return the type in a human
67 zfs_type_to_name(zfs_type_t type)
70 case ZFS_TYPE_FILESYSTEM:
71 return (dgettext(TEXT_DOMAIN, "filesystem"));
72 case ZFS_TYPE_SNAPSHOT:
73 return (dgettext(TEXT_DOMAIN, "snapshot"));
75 return (dgettext(TEXT_DOMAIN, "volume"));
82 * Given a path and mask of ZFS types, return a string describing this dataset.
83 * This is used when we fail to open a dataset and we cannot get an exact type.
84 * We guess what the type would have been based on the path and the mask of
88 path_to_str(const char *path, int types)
91 * When given a single type, always report the exact type.
93 if (types == ZFS_TYPE_SNAPSHOT)
94 return (dgettext(TEXT_DOMAIN, "snapshot"));
95 if (types == ZFS_TYPE_FILESYSTEM)
96 return (dgettext(TEXT_DOMAIN, "filesystem"));
97 if (types == ZFS_TYPE_VOLUME)
98 return (dgettext(TEXT_DOMAIN, "volume"));
101 * The user is requesting more than one type of dataset. If this is the
102 * case, consult the path itself. If we're looking for a snapshot, and
103 * a '@' is found, then report it as "snapshot". Otherwise, remove the
104 * snapshot attribute and try again.
106 if (types & ZFS_TYPE_SNAPSHOT) {
107 if (strchr(path, '@') != NULL)
108 return (dgettext(TEXT_DOMAIN, "snapshot"));
109 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
113 * The user has requested either filesystems or volumes.
114 * We have no way of knowing a priori what type this would be, so always
115 * report it as "filesystem" or "volume", our two primitive types.
117 if (types & ZFS_TYPE_FILESYSTEM)
118 return (dgettext(TEXT_DOMAIN, "filesystem"));
120 assert(types & ZFS_TYPE_VOLUME);
121 return (dgettext(TEXT_DOMAIN, "volume"));
125 * Validate a ZFS path. This is used even before trying to open the dataset, to
126 * provide a more meaningful error message. We call zfs_error_aux() to
127 * explain exactly why the name was not valid.
130 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
136 if (dataset_namecheck(path, &why, &what) != 0) {
139 case NAME_ERR_TOOLONG:
140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141 "name is too long"));
144 case NAME_ERR_LEADING_SLASH:
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "leading slash in name"));
149 case NAME_ERR_EMPTY_COMPONENT:
150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151 "empty component in name"));
154 case NAME_ERR_TRAILING_SLASH:
155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156 "trailing slash in name"));
159 case NAME_ERR_INVALCHAR:
161 dgettext(TEXT_DOMAIN, "invalid character "
162 "'%c' in name"), what);
165 case NAME_ERR_MULTIPLE_AT:
166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
167 "multiple '@' delimiters in name"));
170 case NAME_ERR_NOLETTER:
171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172 "pool doesn't begin with a letter"));
175 case NAME_ERR_RESERVED:
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "name is reserved"));
180 case NAME_ERR_DISKLIKE:
181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
182 "reserved disk name"));
190 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193 "snapshot delimiter '@' in filesystem name"));
197 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200 "missing '@' delimiter in snapshot name"));
204 if (modifying && strchr(path, '%') != NULL) {
206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
207 "invalid character %c in name"), '%');
215 zfs_name_valid(const char *name, zfs_type_t type)
217 if (type == ZFS_TYPE_POOL)
218 return (zpool_name_valid(NULL, B_FALSE, name));
219 return (zfs_validate_name(NULL, name, type, B_FALSE));
223 * This function takes the raw DSL properties, and filters out the user-defined
224 * properties into a separate nvlist.
227 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
229 libzfs_handle_t *hdl = zhp->zfs_hdl;
234 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
235 (void) no_memory(hdl);
240 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
241 if (!zfs_prop_user(nvpair_name(elem)))
244 verify(nvpair_value_nvlist(elem, &propval) == 0);
245 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
247 (void) no_memory(hdl);
255 static zpool_handle_t *
256 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
258 libzfs_handle_t *hdl = zhp->zfs_hdl;
261 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
262 if (hdl->libzfs_pool_handles != NULL)
263 zph->zpool_next = hdl->libzfs_pool_handles;
264 hdl->libzfs_pool_handles = zph;
269 static zpool_handle_t *
270 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
272 libzfs_handle_t *hdl = zhp->zfs_hdl;
273 zpool_handle_t *zph = hdl->libzfs_pool_handles;
275 while ((zph != NULL) &&
276 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
277 zph = zph->zpool_next;
282 * Returns a handle to the pool that contains the provided dataset.
283 * If a handle to that pool already exists then that handle is returned.
284 * Otherwise, a new handle is created and added to the list of handles.
286 static zpool_handle_t *
287 zpool_handle(zfs_handle_t *zhp)
293 len = strcspn(zhp->zfs_name, "/@") + 1;
294 pool_name = zfs_alloc(zhp->zfs_hdl, len);
295 (void) strlcpy(pool_name, zhp->zfs_name, len);
297 zph = zpool_find_handle(zhp, pool_name, len);
299 zph = zpool_add_handle(zhp, pool_name);
306 zpool_free_handles(libzfs_handle_t *hdl)
308 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
310 while (zph != NULL) {
311 next = zph->zpool_next;
315 hdl->libzfs_pool_handles = NULL;
319 * Utility function to gather stats (objset and zpl) for the given object.
321 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
323 libzfs_handle_t *hdl = zhp->zfs_hdl;
325 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
327 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
328 if (errno == ENOMEM) {
329 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
340 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
342 nvlist_t *allprops, *userprops;
344 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
346 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
351 * XXX Why do we store the user props separately, in addition to
352 * storing them in zfs_props?
354 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
355 nvlist_free(allprops);
359 nvlist_free(zhp->zfs_props);
360 nvlist_free(zhp->zfs_user_props);
362 zhp->zfs_props = allprops;
363 zhp->zfs_user_props = userprops;
369 get_stats(zfs_handle_t *zhp)
372 zfs_cmd_t zc = { 0 };
374 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
376 if (get_stats_ioctl(zhp, &zc) != 0)
378 else if (put_stats_zhdl(zhp, &zc) != 0)
380 zcmd_free_nvlists(&zc);
385 * Refresh the properties currently stored in the handle.
388 zfs_refresh_properties(zfs_handle_t *zhp)
390 (void) get_stats(zhp);
394 * Makes a handle from the given dataset name. Used by zfs_open() and
395 * zfs_iter_* to create child handles on the fly.
398 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
401 libzfs_handle_t *hdl = zhp->zfs_hdl;
404 * Preserve history log string.
405 * any changes performed here will be
406 * logged as an internal event.
408 logstr = zhp->zfs_hdl->libzfs_log_str;
409 zhp->zfs_hdl->libzfs_log_str = NULL;
412 if (put_stats_zhdl(zhp, zc) != 0) {
413 zhp->zfs_hdl->libzfs_log_str = logstr;
418 if (zhp->zfs_dmustats.dds_inconsistent) {
419 zfs_cmd_t zc2 = { 0 };
422 * If it is dds_inconsistent, then we've caught it in
423 * the middle of a 'zfs receive' or 'zfs destroy', and
424 * it is inconsistent from the ZPL's point of view, so
425 * can't be mounted. However, it could also be that we
426 * have crashed in the middle of one of those
427 * operations, in which case we need to get rid of the
428 * inconsistent state. We do that by either rolling
429 * back to the previous snapshot (which will fail if
430 * there is none), or destroying the filesystem. Note
431 * that if we are still in the middle of an active
432 * 'receive' or 'destroy', then the rollback and destroy
433 * will fail with EBUSY and we will drive on as usual.
436 (void) strlcpy(zc2.zc_name, zhp->zfs_name,
437 sizeof (zc2.zc_name));
439 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
440 (void) zvol_remove_link(hdl, zhp->zfs_name);
441 zc2.zc_objset_type = DMU_OST_ZVOL;
443 zc2.zc_objset_type = DMU_OST_ZFS;
447 * If we can successfully destroy it, pretend that it
450 if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) {
451 zhp->zfs_hdl->libzfs_log_str = logstr;
455 /* If we can successfully roll it back, reset the stats */
456 if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) {
457 if (get_stats_ioctl(zhp, zc) != 0) {
458 zhp->zfs_hdl->libzfs_log_str = logstr;
466 * We've managed to open the dataset and gather statistics. Determine
467 * the high-level type.
469 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
470 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
471 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
472 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
476 if (zhp->zfs_dmustats.dds_is_snapshot)
477 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
478 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
479 zhp->zfs_type = ZFS_TYPE_VOLUME;
480 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
481 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
483 abort(); /* we should never see any other types */
485 zhp->zfs_hdl->libzfs_log_str = logstr;
486 zhp->zpool_hdl = zpool_handle(zhp);
491 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
493 zfs_cmd_t zc = { 0 };
495 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
501 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
502 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
506 if (get_stats_ioctl(zhp, &zc) == -1) {
507 zcmd_free_nvlists(&zc);
511 if (make_dataset_handle_common(zhp, &zc) == -1) {
515 zcmd_free_nvlists(&zc);
519 static zfs_handle_t *
520 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
522 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
528 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
529 if (make_dataset_handle_common(zhp, zc) == -1) {
537 * Opens the given snapshot, filesystem, or volume. The 'types'
538 * argument is a mask of acceptable types. The function will print an
539 * appropriate error message and return NULL if it can't be opened.
542 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
547 (void) snprintf(errbuf, sizeof (errbuf),
548 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
551 * Validate the name before we even try to open it.
553 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "invalid dataset name"));
556 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
561 * Try to get stats for the dataset, which will tell us if it exists.
564 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
565 (void) zfs_standard_error(hdl, errno, errbuf);
569 if (!(types & zhp->zfs_type)) {
570 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
579 * Release a ZFS handle. Nothing to do but free the associated memory.
582 zfs_close(zfs_handle_t *zhp)
584 if (zhp->zfs_mntopts)
585 free(zhp->zfs_mntopts);
586 nvlist_free(zhp->zfs_props);
587 nvlist_free(zhp->zfs_user_props);
591 typedef struct mnttab_node {
592 struct mnttab mtn_mt;
597 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
599 const mnttab_node_t *mtn1 = arg1;
600 const mnttab_node_t *mtn2 = arg2;
603 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
607 return (rv > 0 ? 1 : -1);
611 libzfs_mnttab_init(libzfs_handle_t *hdl)
613 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
614 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
615 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
619 libzfs_mnttab_update(libzfs_handle_t *hdl)
623 rewind(hdl->libzfs_mnttab);
624 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
627 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
629 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
630 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
631 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
632 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
633 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
634 avl_add(&hdl->libzfs_mnttab_cache, mtn);
639 libzfs_mnttab_fini(libzfs_handle_t *hdl)
644 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
645 free(mtn->mtn_mt.mnt_special);
646 free(mtn->mtn_mt.mnt_mountp);
647 free(mtn->mtn_mt.mnt_fstype);
648 free(mtn->mtn_mt.mnt_mntopts);
651 avl_destroy(&hdl->libzfs_mnttab_cache);
655 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
657 hdl->libzfs_mnttab_enable = enable;
661 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
662 struct mnttab *entry)
667 if (!hdl->libzfs_mnttab_enable) {
668 struct mnttab srch = { 0 };
670 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
671 libzfs_mnttab_fini(hdl);
672 rewind(hdl->libzfs_mnttab);
673 srch.mnt_special = (char *)fsname;
674 srch.mnt_fstype = MNTTYPE_ZFS;
675 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
681 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
682 libzfs_mnttab_update(hdl);
684 find.mtn_mt.mnt_special = (char *)fsname;
685 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
687 *entry = mtn->mtn_mt;
694 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
695 const char *mountp, const char *mntopts)
699 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
701 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
702 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
703 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
704 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
705 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
706 avl_add(&hdl->libzfs_mnttab_cache, mtn);
710 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
715 find.mtn_mt.mnt_special = (char *)fsname;
716 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
717 avl_remove(&hdl->libzfs_mnttab_cache, ret);
718 free(ret->mtn_mt.mnt_special);
719 free(ret->mtn_mt.mnt_mountp);
720 free(ret->mtn_mt.mnt_fstype);
721 free(ret->mtn_mt.mnt_mntopts);
727 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
729 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
731 if (zpool_handle == NULL)
734 *spa_version = zpool_get_prop_int(zpool_handle,
735 ZPOOL_PROP_VERSION, NULL);
740 * The choice of reservation property depends on the SPA version.
743 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
747 if (zfs_spa_version(zhp, &spa_version) < 0)
750 if (spa_version >= SPA_VERSION_REFRESERVATION)
751 *resv_prop = ZFS_PROP_REFRESERVATION;
753 *resv_prop = ZFS_PROP_RESERVATION;
759 * Given an nvlist of properties to set, validates that they are correct, and
760 * parses any numeric properties (index, boolean, etc) if they are specified as
764 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
765 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
772 int chosen_normal = -1;
775 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
776 (void) no_memory(hdl);
781 * Make sure this property is valid and applies to this type.
785 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
786 const char *propname = nvpair_name(elem);
788 prop = zfs_name_to_prop(propname);
789 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
791 * This is a user property: make sure it's a
792 * string, and that it's less than ZAP_MAXNAMELEN.
794 if (nvpair_type(elem) != DATA_TYPE_STRING) {
795 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
796 "'%s' must be a string"), propname);
797 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
801 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
803 "property name '%s' is too long"),
805 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
809 (void) nvpair_value_string(elem, &strval);
810 if (nvlist_add_string(ret, propname, strval) != 0) {
811 (void) no_memory(hdl);
818 * Currently, only user properties can be modified on
821 if (type == ZFS_TYPE_SNAPSHOT) {
822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
823 "this property can not be modified for snapshots"));
824 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
828 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
829 zfs_userquota_prop_t uqtype;
830 char newpropname[128];
835 if (userquota_propname_decode(propname, zoned,
836 &uqtype, domain, sizeof (domain), &rid) != 0) {
838 dgettext(TEXT_DOMAIN,
839 "'%s' has an invalid user/group name"),
841 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
845 if (uqtype != ZFS_PROP_USERQUOTA &&
846 uqtype != ZFS_PROP_GROUPQUOTA) {
848 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
850 (void) zfs_error(hdl, EZFS_PROPREADONLY,
855 if (nvpair_type(elem) == DATA_TYPE_STRING) {
856 (void) nvpair_value_string(elem, &strval);
857 if (strcmp(strval, "none") == 0) {
859 } else if (zfs_nicestrtonum(hdl,
860 strval, &intval) != 0) {
861 (void) zfs_error(hdl,
862 EZFS_BADPROP, errbuf);
865 } else if (nvpair_type(elem) ==
867 (void) nvpair_value_uint64(elem, &intval);
869 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
870 "use 'none' to disable "
871 "userquota/groupquota"));
875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
876 "'%s' must be a number"), propname);
877 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
881 (void) snprintf(newpropname, sizeof (newpropname),
882 "%s%s", zfs_userquota_prop_prefixes[uqtype],
887 if (nvlist_add_uint64_array(ret, newpropname,
889 (void) no_memory(hdl);
895 if (prop == ZPROP_INVAL) {
896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
897 "invalid property '%s'"), propname);
898 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
902 if (!zfs_prop_valid_for_type(prop, type)) {
904 dgettext(TEXT_DOMAIN, "'%s' does not "
905 "apply to datasets of this type"), propname);
906 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
910 if (zfs_prop_readonly(prop) &&
911 (!zfs_prop_setonce(prop) || zhp != NULL)) {
913 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
915 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
919 if (zprop_parse_value(hdl, elem, prop, type, ret,
920 &strval, &intval, errbuf) != 0)
924 * Perform some additional checks for specific properties.
927 case ZFS_PROP_VERSION:
933 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
934 if (intval < version) {
935 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
936 "Can not downgrade; already at version %u"),
938 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
944 case ZFS_PROP_RECORDSIZE:
945 case ZFS_PROP_VOLBLOCKSIZE:
946 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
947 if (intval < SPA_MINBLOCKSIZE ||
948 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
949 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
950 "'%s' must be power of 2 from %u "
952 (uint_t)SPA_MINBLOCKSIZE,
953 (uint_t)SPA_MAXBLOCKSIZE >> 10);
954 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
959 case ZFS_PROP_SHAREISCSI:
960 if (strcmp(strval, "off") != 0 &&
961 strcmp(strval, "on") != 0 &&
962 strcmp(strval, "type=disk") != 0) {
963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964 "'%s' must be 'on', 'off', or 'type=disk'"),
966 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
972 case ZFS_PROP_MOUNTPOINT:
976 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
977 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
980 if (mountpoint_namecheck(strval, &why)) {
982 case NAME_ERR_LEADING_SLASH:
984 dgettext(TEXT_DOMAIN,
985 "'%s' must be an absolute path, "
986 "'none', or 'legacy'"), propname);
988 case NAME_ERR_TOOLONG:
990 dgettext(TEXT_DOMAIN,
991 "component of '%s' is too long"),
995 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1002 case ZFS_PROP_SHARESMB:
1003 case ZFS_PROP_SHARENFS:
1005 * For the mountpoint and sharenfs or sharesmb
1006 * properties, check if it can be set in a
1007 * global/non-global zone based on
1008 * the zoned property value:
1010 * global zone non-global zone
1011 * --------------------------------------------------
1012 * zoned=on mountpoint (no) mountpoint (yes)
1013 * sharenfs (no) sharenfs (no)
1014 * sharesmb (no) sharesmb (no)
1016 * zoned=off mountpoint (yes) N/A
1021 if (getzoneid() == GLOBAL_ZONEID) {
1022 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1023 "'%s' cannot be set on "
1024 "dataset in a non-global zone"),
1026 (void) zfs_error(hdl, EZFS_ZONED,
1029 } else if (prop == ZFS_PROP_SHARENFS ||
1030 prop == ZFS_PROP_SHARESMB) {
1031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1032 "'%s' cannot be set in "
1033 "a non-global zone"), propname);
1034 (void) zfs_error(hdl, EZFS_ZONED,
1038 } else if (getzoneid() != GLOBAL_ZONEID) {
1040 * If zoned property is 'off', this must be in
1041 * a global zone. If not, something is wrong.
1043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1044 "'%s' cannot be set while dataset "
1045 "'zoned' property is set"), propname);
1046 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1051 * At this point, it is legitimate to set the
1052 * property. Now we want to make sure that the
1053 * property value is valid if it is sharenfs.
1055 if ((prop == ZFS_PROP_SHARENFS ||
1056 prop == ZFS_PROP_SHARESMB) &&
1057 strcmp(strval, "on") != 0 &&
1058 strcmp(strval, "off") != 0) {
1059 zfs_share_proto_t proto;
1061 if (prop == ZFS_PROP_SHARESMB)
1067 * Must be an valid sharing protocol
1068 * option string so init the libshare
1069 * in order to enable the parser and
1070 * then parse the options. We use the
1071 * control API since we don't care about
1072 * the current configuration and don't
1073 * want the overhead of loading it
1074 * until we actually do something.
1077 if (zfs_init_libshare(hdl,
1078 SA_INIT_CONTROL_API) != SA_OK) {
1080 * An error occurred so we can't do
1083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1084 "'%s' cannot be set: problem "
1085 "in share initialization"),
1087 (void) zfs_error(hdl, EZFS_BADPROP,
1092 if (zfs_parse_options(strval, proto) != SA_OK) {
1094 * There was an error in parsing so
1095 * deal with it by issuing an error
1096 * message and leaving after
1097 * uninitializing the the libshare
1100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1101 "'%s' cannot be set to invalid "
1102 "options"), propname);
1103 (void) zfs_error(hdl, EZFS_BADPROP,
1105 zfs_uninit_libshare(hdl);
1108 zfs_uninit_libshare(hdl);
1112 case ZFS_PROP_UTF8ONLY:
1113 chosen_utf = (int)intval;
1115 case ZFS_PROP_NORMALIZE:
1116 chosen_normal = (int)intval;
1121 * For changes to existing volumes, we have some additional
1122 * checks to enforce.
1124 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1125 uint64_t volsize = zfs_prop_get_int(zhp,
1127 uint64_t blocksize = zfs_prop_get_int(zhp,
1128 ZFS_PROP_VOLBLOCKSIZE);
1132 case ZFS_PROP_RESERVATION:
1133 case ZFS_PROP_REFRESERVATION:
1134 if (intval > volsize) {
1135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136 "'%s' is greater than current "
1137 "volume size"), propname);
1138 (void) zfs_error(hdl, EZFS_BADPROP,
1144 case ZFS_PROP_VOLSIZE:
1145 if (intval % blocksize != 0) {
1146 zfs_nicenum(blocksize, buf,
1148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 "'%s' must be a multiple of "
1150 "volume block size (%s)"),
1152 (void) zfs_error(hdl, EZFS_BADPROP,
1158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1159 "'%s' cannot be zero"),
1161 (void) zfs_error(hdl, EZFS_BADPROP,
1171 * If normalization was chosen, but no UTF8 choice was made,
1172 * enforce rejection of non-UTF8 names.
1174 * If normalization was chosen, but rejecting non-UTF8 names
1175 * was explicitly not chosen, it is an error.
1177 if (chosen_normal > 0 && chosen_utf < 0) {
1178 if (nvlist_add_uint64(ret,
1179 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1180 (void) no_memory(hdl);
1183 } else if (chosen_normal > 0 && chosen_utf == 0) {
1184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1185 "'%s' must be set 'on' if normalization chosen"),
1186 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1187 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1192 * If this is an existing volume, and someone is setting the volsize,
1193 * make sure that it matches the reservation, or add it if necessary.
1195 if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
1196 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1198 uint64_t old_volsize = zfs_prop_get_int(zhp,
1200 uint64_t old_reservation;
1201 uint64_t new_reservation;
1202 zfs_prop_t resv_prop;
1204 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1206 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1208 if (old_volsize == old_reservation &&
1209 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
1210 &new_reservation) != 0) {
1211 if (nvlist_add_uint64(ret,
1212 zfs_prop_to_name(resv_prop), intval) != 0) {
1213 (void) no_memory(hdl);
1226 * Given a property name and value, set the property for the given dataset.
1229 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1231 zfs_cmd_t zc = { 0 };
1233 prop_changelist_t *cl = NULL;
1235 libzfs_handle_t *hdl = zhp->zfs_hdl;
1236 nvlist_t *nvl = NULL, *realprops;
1238 boolean_t do_prefix;
1241 (void) snprintf(errbuf, sizeof (errbuf),
1242 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1245 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1246 nvlist_add_string(nvl, propname, propval) != 0) {
1247 (void) no_memory(hdl);
1251 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1252 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1258 prop = zfs_name_to_prop(propname);
1260 /* We don't support those properties on FreeBSD. */
1262 case ZFS_PROP_DEVICES:
1263 case ZFS_PROP_SHAREISCSI:
1264 case ZFS_PROP_ISCSIOPTIONS:
1265 case ZFS_PROP_XATTR:
1266 case ZFS_PROP_VSCAN:
1267 case ZFS_PROP_NBMAND:
1268 (void) snprintf(errbuf, sizeof (errbuf),
1269 "property '%s' not supported on FreeBSD", propname);
1270 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1274 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1277 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1278 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1279 "child dataset with inherited mountpoint is used "
1280 "in a non-global zone"));
1281 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1286 * If the dataset's canmount property is being set to noauto,
1287 * then we want to prevent unmounting & remounting it.
1289 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1290 (zprop_string_to_index(prop, propval, &idx,
1291 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1293 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1297 * Execute the corresponding ioctl() to set this property.
1299 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1301 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1304 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1311 * For quotas and reservations, ENOSPC indicates
1312 * something different; setting a quota or reservation
1313 * doesn't use any disk space.
1316 case ZFS_PROP_QUOTA:
1317 case ZFS_PROP_REFQUOTA:
1318 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1319 "size is less than current used or "
1321 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1324 case ZFS_PROP_RESERVATION:
1325 case ZFS_PROP_REFRESERVATION:
1326 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1327 "size is greater than available space"));
1328 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1332 (void) zfs_standard_error(hdl, errno, errbuf);
1338 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1339 (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
1341 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1345 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1350 "pool and or dataset must be upgraded to set this "
1351 "property or value"));
1352 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1356 if (prop == ZFS_PROP_COMPRESSION) {
1357 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1358 "property setting is not allowed on "
1359 "bootable datasets"));
1360 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1362 (void) zfs_standard_error(hdl, errno, errbuf);
1368 * This platform can't address a volume this big.
1371 if (prop == ZFS_PROP_VOLSIZE) {
1372 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1378 (void) zfs_standard_error(hdl, errno, errbuf);
1382 ret = changelist_postfix(cl);
1385 * Refresh the statistics so the new property value
1389 (void) get_stats(zhp);
1394 zcmd_free_nvlists(&zc);
1396 changelist_free(cl);
1401 * Given a property, inherit the value from the parent dataset.
1404 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1406 zfs_cmd_t zc = { 0 };
1408 prop_changelist_t *cl;
1409 libzfs_handle_t *hdl = zhp->zfs_hdl;
1413 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1414 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1416 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1418 * For user properties, the amount of work we have to do is very
1419 * small, so just do it here.
1421 if (!zfs_prop_user(propname)) {
1422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1423 "invalid property"));
1424 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1427 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1428 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1430 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1431 return (zfs_standard_error(hdl, errno, errbuf));
1437 * Verify that this property is inheritable.
1439 if (zfs_prop_readonly(prop))
1440 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1442 if (!zfs_prop_inheritable(prop))
1443 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1446 * Check to see if the value applies to this type
1448 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1449 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1452 * Normalize the name, to get rid of shorthand abbrevations.
1454 propname = zfs_prop_to_name(prop);
1455 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1456 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1458 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1459 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1461 "dataset is used in a non-global zone"));
1462 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1466 * Determine datasets which will be affected by this change, if any.
1468 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1471 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1473 "child dataset with inherited mountpoint is used "
1474 "in a non-global zone"));
1475 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1479 if ((ret = changelist_prefix(cl)) != 0)
1482 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1483 return (zfs_standard_error(hdl, errno, errbuf));
1486 if ((ret = changelist_postfix(cl)) != 0)
1490 * Refresh the statistics so the new property is reflected.
1492 (void) get_stats(zhp);
1496 changelist_free(cl);
1501 * True DSL properties are stored in an nvlist. The following two functions
1502 * extract them appropriately.
1505 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1511 if (nvlist_lookup_nvlist(zhp->zfs_props,
1512 zfs_prop_to_name(prop), &nv) == 0) {
1513 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1514 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1516 verify(!zhp->zfs_props_table ||
1517 zhp->zfs_props_table[prop] == B_TRUE);
1518 value = zfs_prop_default_numeric(prop);
1526 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1532 if (nvlist_lookup_nvlist(zhp->zfs_props,
1533 zfs_prop_to_name(prop), &nv) == 0) {
1534 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1535 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1537 verify(!zhp->zfs_props_table ||
1538 zhp->zfs_props_table[prop] == B_TRUE);
1539 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1548 * Internal function for getting a numeric property. Both zfs_prop_get() and
1549 * zfs_prop_get_int() are built using this interface.
1551 * Certain properties can be overridden using 'mount -o'. In this case, scan
1552 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1553 * If they differ from the on-disk values, report the current values and mark
1554 * the source "temporary".
1557 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1558 char **source, uint64_t *val)
1560 zfs_cmd_t zc = { 0 };
1561 nvlist_t *zplprops = NULL;
1563 char *mntopt_on = NULL;
1564 char *mntopt_off = NULL;
1569 case ZFS_PROP_ATIME:
1570 mntopt_on = MNTOPT_ATIME;
1571 mntopt_off = MNTOPT_NOATIME;
1574 case ZFS_PROP_DEVICES:
1575 mntopt_on = MNTOPT_DEVICES;
1576 mntopt_off = MNTOPT_NODEVICES;
1580 mntopt_on = MNTOPT_EXEC;
1581 mntopt_off = MNTOPT_NOEXEC;
1584 case ZFS_PROP_READONLY:
1585 mntopt_on = MNTOPT_RO;
1586 mntopt_off = MNTOPT_RW;
1589 case ZFS_PROP_SETUID:
1590 mntopt_on = MNTOPT_SETUID;
1591 mntopt_off = MNTOPT_NOSETUID;
1594 case ZFS_PROP_XATTR:
1595 mntopt_on = MNTOPT_XATTR;
1596 mntopt_off = MNTOPT_NOXATTR;
1599 case ZFS_PROP_NBMAND:
1600 mntopt_on = MNTOPT_NBMAND;
1601 mntopt_off = MNTOPT_NONBMAND;
1606 * Because looking up the mount options is potentially expensive
1607 * (iterating over all of /etc/mnttab), we defer its calculation until
1608 * we're looking up a property which requires its presence.
1610 if (!zhp->zfs_mntcheck &&
1611 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1612 libzfs_handle_t *hdl = zhp->zfs_hdl;
1613 struct mnttab entry;
1615 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1616 zhp->zfs_mntopts = zfs_strdup(hdl,
1618 if (zhp->zfs_mntopts == NULL)
1622 zhp->zfs_mntcheck = B_TRUE;
1625 if (zhp->zfs_mntopts == NULL)
1626 mnt.mnt_mntopts = "";
1628 mnt.mnt_mntopts = zhp->zfs_mntopts;
1631 case ZFS_PROP_ATIME:
1632 case ZFS_PROP_DEVICES:
1634 case ZFS_PROP_READONLY:
1635 case ZFS_PROP_SETUID:
1636 case ZFS_PROP_XATTR:
1637 case ZFS_PROP_NBMAND:
1638 *val = getprop_uint64(zhp, prop, source);
1640 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1643 *src = ZPROP_SRC_TEMPORARY;
1644 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1647 *src = ZPROP_SRC_TEMPORARY;
1651 case ZFS_PROP_CANMOUNT:
1652 *val = getprop_uint64(zhp, prop, source);
1653 if (*val != ZFS_CANMOUNT_ON)
1654 *source = zhp->zfs_name;
1656 *source = ""; /* default */
1659 case ZFS_PROP_QUOTA:
1660 case ZFS_PROP_REFQUOTA:
1661 case ZFS_PROP_RESERVATION:
1662 case ZFS_PROP_REFRESERVATION:
1663 *val = getprop_uint64(zhp, prop, source);
1665 *source = ""; /* default */
1667 *source = zhp->zfs_name;
1670 case ZFS_PROP_MOUNTED:
1671 *val = (zhp->zfs_mntopts != NULL);
1674 case ZFS_PROP_NUMCLONES:
1675 *val = zhp->zfs_dmustats.dds_num_clones;
1678 case ZFS_PROP_VERSION:
1679 case ZFS_PROP_NORMALIZE:
1680 case ZFS_PROP_UTF8ONLY:
1682 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1683 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1685 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1686 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1687 zcmd_free_nvlists(&zc);
1688 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1689 "unable to get %s property"),
1690 zfs_prop_to_name(prop));
1691 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
1692 dgettext(TEXT_DOMAIN, "internal error")));
1694 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1695 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1697 zcmd_free_nvlists(&zc);
1698 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1699 "unable to get %s property"),
1700 zfs_prop_to_name(prop));
1701 return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
1702 dgettext(TEXT_DOMAIN, "internal error")));
1705 nvlist_free(zplprops);
1706 zcmd_free_nvlists(&zc);
1710 switch (zfs_prop_get_type(prop)) {
1711 case PROP_TYPE_NUMBER:
1712 case PROP_TYPE_INDEX:
1713 *val = getprop_uint64(zhp, prop, source);
1715 * If we tried to use a default value for a
1716 * readonly property, it means that it was not
1717 * present; return an error.
1719 if (zfs_prop_readonly(prop) &&
1720 *source && (*source)[0] == '\0') {
1725 case PROP_TYPE_STRING:
1727 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1728 "cannot get non-numeric property"));
1729 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1730 dgettext(TEXT_DOMAIN, "internal error")));
1738 * Calculate the source type, given the raw source string.
1741 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1742 char *statbuf, size_t statlen)
1744 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1747 if (source == NULL) {
1748 *srctype = ZPROP_SRC_NONE;
1749 } else if (source[0] == '\0') {
1750 *srctype = ZPROP_SRC_DEFAULT;
1752 if (strcmp(source, zhp->zfs_name) == 0) {
1753 *srctype = ZPROP_SRC_LOCAL;
1755 (void) strlcpy(statbuf, source, statlen);
1756 *srctype = ZPROP_SRC_INHERITED;
1763 * Retrieve a property from the given object. If 'literal' is specified, then
1764 * numbers are left as exact values. Otherwise, numbers are converted to a
1765 * human-readable form.
1767 * Returns 0 on success, or -1 on error.
1770 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1771 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1773 char *source = NULL;
1779 * Check to see if this property applies to our object
1781 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1785 *src = ZPROP_SRC_NONE;
1788 case ZFS_PROP_CREATION:
1790 * 'creation' is a time_t stored in the statistics. We convert
1791 * this into a string unless 'literal' is specified.
1794 val = getprop_uint64(zhp, prop, &source);
1795 time_t time = (time_t)val;
1799 localtime_r(&time, &t) == NULL ||
1800 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1802 (void) snprintf(propbuf, proplen, "%llu", val);
1806 case ZFS_PROP_MOUNTPOINT:
1808 * Getting the precise mountpoint can be tricky.
1810 * - for 'none' or 'legacy', return those values.
1811 * - for inherited mountpoints, we want to take everything
1812 * after our ancestor and append it to the inherited value.
1814 * If the pool has an alternate root, we want to prepend that
1815 * root to any values we return.
1818 str = getprop_string(zhp, prop, &source);
1820 if (str[0] == '/') {
1821 char buf[MAXPATHLEN];
1823 const char *relpath = zhp->zfs_name + strlen(source);
1825 if (relpath[0] == '/')
1828 if ((zpool_get_prop(zhp->zpool_hdl,
1829 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1830 (strcmp(root, "-") == 0))
1833 * Special case an alternate root of '/'. This will
1834 * avoid having multiple leading slashes in the
1837 if (strcmp(root, "/") == 0)
1841 * If the mountpoint is '/' then skip over this
1842 * if we are obtaining either an alternate root or
1843 * an inherited mountpoint.
1845 if (str[1] == '\0' && (root[0] != '\0' ||
1846 relpath[0] != '\0'))
1849 if (relpath[0] == '\0')
1850 (void) snprintf(propbuf, proplen, "%s%s",
1853 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1854 root, str, relpath[0] == '@' ? "" : "/",
1857 /* 'legacy' or 'none' */
1858 (void) strlcpy(propbuf, str, proplen);
1863 case ZFS_PROP_ORIGIN:
1864 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1867 * If there is no parent at all, return failure to indicate that
1868 * it doesn't apply to this dataset.
1870 if (propbuf[0] == '\0')
1874 case ZFS_PROP_QUOTA:
1875 case ZFS_PROP_REFQUOTA:
1876 case ZFS_PROP_RESERVATION:
1877 case ZFS_PROP_REFRESERVATION:
1879 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1883 * If quota or reservation is 0, we translate this into 'none'
1884 * (unless literal is set), and indicate that it's the default
1885 * value. Otherwise, we print the number nicely and indicate
1886 * that its set locally.
1890 (void) strlcpy(propbuf, "0", proplen);
1892 (void) strlcpy(propbuf, "none", proplen);
1895 (void) snprintf(propbuf, proplen, "%llu",
1898 zfs_nicenum(val, propbuf, proplen);
1902 case ZFS_PROP_COMPRESSRATIO:
1903 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1905 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
1906 val / 100, (longlong_t)val % 100);
1910 switch (zhp->zfs_type) {
1911 case ZFS_TYPE_FILESYSTEM:
1914 case ZFS_TYPE_VOLUME:
1917 case ZFS_TYPE_SNAPSHOT:
1923 (void) snprintf(propbuf, proplen, "%s", str);
1926 case ZFS_PROP_MOUNTED:
1928 * The 'mounted' property is a pseudo-property that described
1929 * whether the filesystem is currently mounted. Even though
1930 * it's a boolean value, the typical values of "on" and "off"
1931 * don't make sense, so we translate to "yes" and "no".
1933 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
1934 src, &source, &val) != 0)
1937 (void) strlcpy(propbuf, "yes", proplen);
1939 (void) strlcpy(propbuf, "no", proplen);
1944 * The 'name' property is a pseudo-property derived from the
1945 * dataset name. It is presented as a real property to simplify
1948 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
1952 switch (zfs_prop_get_type(prop)) {
1953 case PROP_TYPE_NUMBER:
1954 if (get_numeric_property(zhp, prop, src,
1955 &source, &val) != 0)
1958 (void) snprintf(propbuf, proplen, "%llu",
1961 zfs_nicenum(val, propbuf, proplen);
1964 case PROP_TYPE_STRING:
1965 (void) strlcpy(propbuf,
1966 getprop_string(zhp, prop, &source), proplen);
1969 case PROP_TYPE_INDEX:
1970 if (get_numeric_property(zhp, prop, src,
1971 &source, &val) != 0)
1973 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
1975 (void) strlcpy(propbuf, strval, proplen);
1983 get_source(zhp, src, source, statbuf, statlen);
1989 * Utility function to get the given numeric property. Does no validation that
1990 * the given property is the appropriate type; should only be used with
1991 * hard-coded property types.
1994 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1999 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2005 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2009 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2010 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2014 * Similar to zfs_prop_get(), but returns the value as an integer.
2017 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2018 zprop_source_t *src, char *statbuf, size_t statlen)
2023 * Check to see if this property applies to our object
2025 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2026 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2027 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2028 zfs_prop_to_name(prop)));
2032 *src = ZPROP_SRC_NONE;
2034 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2037 get_source(zhp, src, source, statbuf, statlen);
2043 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2044 char **domainp, idmap_rid_t *ridp)
2047 idmap_handle_t *idmap_hdl = NULL;
2048 idmap_get_handle_t *get_hdl = NULL;
2052 if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
2054 if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
2058 err = idmap_get_sidbyuid(get_hdl, id,
2059 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2061 err = idmap_get_sidbygid(get_hdl, id,
2062 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2064 if (err == IDMAP_SUCCESS &&
2065 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2066 status == IDMAP_SUCCESS)
2072 idmap_get_destroy(get_hdl);
2074 (void) idmap_fini(idmap_hdl);
2077 assert(!"invalid code path");
2082 /* Check if a string contains only digits */
2084 string_is_digits(char *cp)
2088 for(i = 0; i < strlen(cp); i++)
2097 * convert the propname into parameters needed by kernel
2098 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2099 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2102 userquota_propname_decode(const char *propname, boolean_t zoned,
2103 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2105 zfs_userquota_prop_t type;
2107 char *numericsid = NULL;
2112 /* Figure out the property type ({user|group}{quota|space}) */
2113 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2114 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2115 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2118 if (type == ZFS_NUM_USERQUOTA_PROPS)
2122 isuser = (type == ZFS_PROP_USERQUOTA ||
2123 type == ZFS_PROP_USERUSED);
2125 cp = strchr(propname, '@') + 1;
2127 if (strchr(cp, '@')) {
2130 * It's a SID name (eg "user@domain") that needs to be
2131 * turned into S-1-domainID-RID.
2133 directory_error_t e;
2135 if (zoned && getzoneid() == GLOBAL_ZONEID)
2138 e = directory_sid_from_user_name(NULL,
2141 e = directory_sid_from_group_name(NULL,
2145 directory_error_free(e);
2148 if (numericsid == NULL)
2151 /* will be further decoded below */
2157 if (strncmp(cp, "S-1-", 4) == 0) {
2158 /* It's a numeric SID (eg "S-1-234-567-89") */
2159 (void) strlcpy(domain, cp, domainlen);
2160 cp = strrchr(domain, '-');
2165 *ridp = strtoull(cp, &end, 10);
2170 if (errno != 0 || *end != '\0')
2173 } else if (!isdigit(*cp)) {
2176 * In FreeBSD user and group names can begin with a digit so treat
2177 * as a uid/gid if string contains digits only
2179 } else if (!string_is_digits(cp)) {
2182 * It's a user/group name (eg "user") that needs to be
2183 * turned into a uid/gid
2185 if (zoned && getzoneid() == GLOBAL_ZONEID)
2201 /* It's a user/group ID (eg "12345"). */
2202 uid_t id = strtoul(cp, &end, 10);
2209 /* It's an ephemeral ID. */
2210 if (idmap_id_to_numeric_domain_rid(id, isuser,
2211 &mapdomain, &rid) != 0)
2213 (void) strlcpy(domain, mapdomain, domainlen);
2220 ASSERT3P(numericsid, ==, NULL);
2225 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2226 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2229 zfs_cmd_t zc = { 0 };
2231 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2233 err = userquota_propname_decode(propname,
2234 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2235 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2236 zc.zc_objset_type = *typep;
2240 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2244 *propvalue = zc.zc_cookie;
2249 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2250 uint64_t *propvalue)
2252 zfs_userquota_prop_t type;
2254 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2259 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2260 char *propbuf, int proplen, boolean_t literal)
2264 zfs_userquota_prop_t type;
2266 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2273 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2274 } else if (propvalue == 0 &&
2275 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2276 (void) strlcpy(propbuf, "none", proplen);
2278 zfs_nicenum(propvalue, propbuf, proplen);
2284 * Returns the name of the given zfs handle.
2287 zfs_get_name(const zfs_handle_t *zhp)
2289 return (zhp->zfs_name);
2293 * Returns the type of the given zfs handle.
2296 zfs_get_type(const zfs_handle_t *zhp)
2298 return (zhp->zfs_type);
2302 zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
2305 uint64_t orig_cookie;
2307 orig_cookie = zc->zc_cookie;
2309 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2310 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2313 * FreeBSD compatibility with pre-v15 kernel module.
2314 * Ignore private dataset names.
2316 if (strchr(zc->zc_name, '$') != NULL)
2322 /* expand nvlist memory and try again */
2323 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2324 zcmd_free_nvlists(zc);
2327 zc->zc_cookie = orig_cookie;
2330 * An errno value of ESRCH indicates normal completion.
2331 * If ENOENT is returned, then the underlying dataset
2332 * has been removed since we obtained the handle.
2339 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2340 dgettext(TEXT_DOMAIN,
2341 "cannot iterate filesystems"));
2349 * Iterate over all child filesystems
2352 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2354 zfs_cmd_t zc = { 0 };
2358 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2361 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2364 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2368 * FreeBSD compatibility with pre-v15 kernel module.
2369 * Ignore private dataset names.
2371 if (strchr(zc.zc_name, '$') != NULL)
2375 * Silently ignore errors, as the only plausible explanation is
2376 * that the pool has since been removed.
2378 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2383 if ((ret = func(nzhp, data)) != 0) {
2384 zcmd_free_nvlists(&zc);
2388 zcmd_free_nvlists(&zc);
2389 return ((ret < 0) ? ret : 0);
2393 * Iterate over all snapshots
2396 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2398 zfs_cmd_t zc = { 0 };
2402 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2405 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2407 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2411 * FreeBSD compatibility with pre-v15 kernel module.
2412 * Ignore private dataset names.
2414 if (strchr(zc.zc_name, '$') != NULL)
2417 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2422 if ((ret = func(nzhp, data)) != 0) {
2423 zcmd_free_nvlists(&zc);
2427 zcmd_free_nvlists(&zc);
2428 return ((ret < 0) ? ret : 0);
2432 * Iterate over all children, snapshots and filesystems
2435 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2439 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2442 return (zfs_iter_snapshots(zhp, func, data));
2446 * Given a complete name, return just the portion that refers to the parent.
2447 * Can return NULL if this is a pool.
2450 parent_name(const char *path, char *buf, size_t buflen)
2454 if ((loc = strrchr(path, '/')) == NULL)
2457 (void) strncpy(buf, path, MIN(buflen, loc - path));
2458 buf[loc - path] = '\0';
2464 * If accept_ancestor is false, then check to make sure that the given path has
2465 * a parent, and that it exists. If accept_ancestor is true, then find the
2466 * closest existing ancestor for the given path. In prefixlen return the
2467 * length of already existing prefix of the given path. We also fetch the
2468 * 'zoned' property, which is used to validate property settings when creating
2472 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2473 boolean_t accept_ancestor, int *prefixlen)
2475 zfs_cmd_t zc = { 0 };
2476 char parent[ZFS_MAXNAMELEN];
2481 (void) snprintf(errbuf, sizeof (errbuf),
2482 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2484 /* get parent, and check to see if this is just a pool */
2485 if (parent_name(path, parent, sizeof (parent)) != 0) {
2486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2487 "missing dataset name"));
2488 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2491 /* check to see if the pool exists */
2492 if ((slash = strchr(parent, '/')) == NULL)
2493 slash = parent + strlen(parent);
2494 (void) strncpy(zc.zc_name, parent, slash - parent);
2495 zc.zc_name[slash - parent] = '\0';
2496 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2499 "no such pool '%s'"), zc.zc_name);
2500 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2503 /* check to see if the parent dataset exists */
2504 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2505 if (errno == ENOENT && accept_ancestor) {
2507 * Go deeper to find an ancestor, give up on top level.
2509 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2511 "no such pool '%s'"), zc.zc_name);
2512 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2514 } else if (errno == ENOENT) {
2515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2516 "parent does not exist"));
2517 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2519 return (zfs_standard_error(hdl, errno, errbuf));
2522 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2523 /* we are in a non-global zone, but parent is in the global zone */
2524 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
2525 (void) zfs_standard_error(hdl, EPERM, errbuf);
2530 /* make sure parent is a filesystem */
2531 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2533 "parent is not a filesystem"));
2534 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2540 if (prefixlen != NULL)
2541 *prefixlen = strlen(parent);
2546 * Finds whether the dataset of the given type(s) exists.
2549 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2553 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2557 * Try to get stats for the dataset, which will tell us if it exists.
2559 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2560 int ds_type = zhp->zfs_type;
2563 if (types & ds_type)
2570 * Given a path to 'target', create all the ancestors between
2571 * the prefixlen portion of the path, and the target itself.
2572 * Fail if the initial prefixlen-ancestor does not already exist.
2575 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2581 /* make sure prefix exists */
2582 cp = target + prefixlen;
2584 assert(strchr(cp, '/') == NULL);
2585 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2588 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2596 * Attempt to create, mount, and share any ancestor filesystems,
2597 * up to the prefixlen-long one.
2599 for (cp = target + prefixlen + 1;
2600 cp = strchr(cp, '/'); *cp = '/', cp++) {
2605 h = make_dataset_handle(hdl, target);
2607 /* it already exists, nothing to do here */
2612 logstr = hdl->libzfs_log_str;
2613 hdl->libzfs_log_str = NULL;
2614 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2616 hdl->libzfs_log_str = logstr;
2617 opname = dgettext(TEXT_DOMAIN, "create");
2621 hdl->libzfs_log_str = logstr;
2622 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2624 opname = dgettext(TEXT_DOMAIN, "open");
2628 if (zfs_mount(h, NULL, 0) != 0) {
2629 opname = dgettext(TEXT_DOMAIN, "mount");
2633 if (zfs_share(h) != 0) {
2634 opname = dgettext(TEXT_DOMAIN, "share");
2644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2645 "failed to %s ancestor '%s'"), opname, target);
2650 * Creates non-existing ancestors of the given path.
2653 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2660 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
2663 if ((path_copy = strdup(path)) != NULL) {
2664 rc = create_parents(hdl, path_copy, prefix);
2667 if (path_copy == NULL || rc != 0)
2674 * Create a new filesystem or volume.
2677 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2680 zfs_cmd_t zc = { 0 };
2683 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2687 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2688 "cannot create '%s'"), path);
2690 /* validate the path, taking care to note the extended error message */
2691 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2692 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2694 /* validate parents exist */
2695 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2699 * The failure modes when creating a dataset of a different type over
2700 * one that already exists is a little strange. In particular, if you
2701 * try to create a dataset on top of an existing dataset, the ioctl()
2702 * will return ENOENT, not EEXIST. To prevent this from happening, we
2703 * first try to see if the dataset exists.
2705 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2706 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2707 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2708 "dataset already exists"));
2709 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2712 if (type == ZFS_TYPE_VOLUME)
2713 zc.zc_objset_type = DMU_OST_ZVOL;
2715 zc.zc_objset_type = DMU_OST_ZFS;
2717 if (props && (props = zfs_valid_proplist(hdl, type, props,
2718 zoned, NULL, errbuf)) == 0)
2721 if (type == ZFS_TYPE_VOLUME) {
2723 * If we are creating a volume, the size and block size must
2724 * satisfy a few restraints. First, the blocksize must be a
2725 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2726 * volsize must be a multiple of the block size, and cannot be
2729 if (props == NULL || nvlist_lookup_uint64(props,
2730 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2732 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2733 "missing volume size"));
2734 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2737 if ((ret = nvlist_lookup_uint64(props,
2738 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2739 &blocksize)) != 0) {
2740 if (ret == ENOENT) {
2741 blocksize = zfs_prop_default_numeric(
2742 ZFS_PROP_VOLBLOCKSIZE);
2745 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2746 "missing volume block size"));
2747 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2754 "volume size cannot be zero"));
2755 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2758 if (size % blocksize != 0) {
2760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2761 "volume size must be a multiple of volume block "
2763 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2767 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2771 /* create the dataset */
2772 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2774 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2775 ret = zvol_create_link(hdl, path);
2777 (void) zfs_standard_error(hdl, errno,
2778 dgettext(TEXT_DOMAIN,
2779 "Volume successfully created, but device links "
2780 "were not created"));
2781 zcmd_free_nvlists(&zc);
2786 zcmd_free_nvlists(&zc);
2788 /* check for failure */
2790 char parent[ZFS_MAXNAMELEN];
2791 (void) parent_name(path, parent, sizeof (parent));
2795 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2796 "no such parent '%s'"), parent);
2797 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2801 "parent '%s' is not a filesystem"), parent);
2802 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2806 "volume block size must be power of 2 from "
2808 (uint_t)SPA_MINBLOCKSIZE,
2809 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2811 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2815 "pool must be upgraded to set this "
2816 "property or value"));
2817 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2821 * This platform can't address a volume this big.
2823 if (type == ZFS_TYPE_VOLUME)
2824 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2829 return (zfs_standard_error(hdl, errno, errbuf));
2837 * Destroys the given dataset. The caller must make sure that the filesystem
2838 * isn't mounted, and that there are no active dependents.
2841 zfs_destroy(zfs_handle_t *zhp)
2843 zfs_cmd_t zc = { 0 };
2845 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2847 if (ZFS_IS_VOLUME(zhp)) {
2849 * If user doesn't have permissions to unshare volume, then
2850 * abort the request. This would only happen for a
2851 * non-privileged user.
2853 if (zfs_unshare_iscsi(zhp) != 0) {
2857 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2860 zc.zc_objset_type = DMU_OST_ZVOL;
2862 zc.zc_objset_type = DMU_OST_ZFS;
2865 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2866 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2867 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2871 remove_mountpoint(zhp);
2876 struct destroydata {
2883 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
2885 struct destroydata *dd = arg;
2887 char name[ZFS_MAXNAMELEN];
2888 boolean_t closezhp = dd->closezhp;
2891 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
2892 (void) strlcat(name, "@", sizeof (name));
2893 (void) strlcat(name, dd->snapname, sizeof (name));
2895 szhp = make_dataset_handle(zhp->zfs_hdl, name);
2897 dd->gotone = B_TRUE;
2901 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
2902 (void) zvol_remove_link(zhp->zfs_hdl, name);
2904 * NB: this is simply a best-effort. We don't want to
2905 * return an error, because then we wouldn't visit all
2910 dd->closezhp = B_TRUE;
2911 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
2918 * Destroys all snapshots with the given name in zhp & descendants.
2921 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
2923 zfs_cmd_t zc = { 0 };
2925 struct destroydata dd = { 0 };
2927 dd.snapname = snapname;
2928 (void) zfs_remove_link_cb(zhp, &dd);
2931 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
2932 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
2933 zhp->zfs_name, snapname));
2936 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2937 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2939 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
2943 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2944 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
2948 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2949 "snapshot is cloned"));
2950 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
2953 return (zfs_standard_error(zhp->zfs_hdl, errno,
2962 * Clones the given dataset. The target must be of the same type as the source.
2965 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2967 zfs_cmd_t zc = { 0 };
2968 char parent[ZFS_MAXNAMELEN];
2971 libzfs_handle_t *hdl = zhp->zfs_hdl;
2975 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2977 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2978 "cannot create '%s'"), target);
2980 /* validate the target name */
2981 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
2982 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2984 /* validate parents exist */
2985 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2988 (void) parent_name(target, parent, sizeof (parent));
2991 if (ZFS_IS_VOLUME(zhp)) {
2992 zc.zc_objset_type = DMU_OST_ZVOL;
2993 type = ZFS_TYPE_VOLUME;
2995 zc.zc_objset_type = DMU_OST_ZFS;
2996 type = ZFS_TYPE_FILESYSTEM;
3000 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3001 zhp, errbuf)) == NULL)
3004 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3012 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3013 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3014 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3016 zcmd_free_nvlists(&zc);
3023 * The parent doesn't exist. We should have caught this
3024 * above, but there may a race condition that has since
3025 * destroyed the parent.
3027 * At this point, we don't know whether it's the source
3028 * that doesn't exist anymore, or whether the target
3029 * dataset doesn't exist.
3031 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3032 "no such parent '%s'"), parent);
3033 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3036 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3037 "source and target pools differ"));
3038 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3042 return (zfs_standard_error(zhp->zfs_hdl, errno,
3045 } else if (ZFS_IS_VOLUME(zhp)) {
3046 ret = zvol_create_link(zhp->zfs_hdl, target);
3052 typedef struct promote_data {
3053 char cb_mountpoint[MAXPATHLEN];
3054 const char *cb_target;
3055 const char *cb_errbuf;
3056 uint64_t cb_pivot_txg;
3060 promote_snap_cb(zfs_handle_t *zhp, void *data)
3062 promote_data_t *pd = data;
3064 char snapname[MAXPATHLEN];
3067 /* We don't care about snapshots after the pivot point */
3068 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3073 /* Remove the device link if it's a zvol. */
3074 if (ZFS_IS_VOLUME(zhp))
3075 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3077 /* Check for conflicting names */
3078 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3079 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3080 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3083 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3084 "snapshot name '%s' from origin \n"
3085 "conflicts with '%s' from target"),
3086 zhp->zfs_name, snapname);
3087 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3094 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3096 promote_data_t *pd = data;
3098 /* We don't care about snapshots after the pivot point */
3099 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3100 /* Create the device link if it's a zvol. */
3101 if (ZFS_IS_VOLUME(zhp))
3102 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3110 * Promotes the given clone fs to be the clone parent.
3113 zfs_promote(zfs_handle_t *zhp)
3115 libzfs_handle_t *hdl = zhp->zfs_hdl;
3116 zfs_cmd_t zc = { 0 };
3117 char parent[MAXPATHLEN];
3124 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3125 "cannot promote '%s'"), zhp->zfs_name);
3127 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3129 "snapshots can not be promoted"));
3130 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3133 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3134 if (parent[0] == '\0') {
3135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3136 "not a cloned filesystem"));
3137 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3139 cp = strchr(parent, '@');
3142 /* Walk the snapshots we will be moving */
3143 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3146 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3148 pd.cb_target = zhp->zfs_name;
3149 pd.cb_errbuf = errbuf;
3150 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3153 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3154 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3155 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3161 /* issue the ioctl */
3162 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3163 sizeof (zc.zc_value));
3164 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3165 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3168 int save_errno = errno;
3170 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3173 switch (save_errno) {
3176 * There is a conflicting snapshot name. We
3177 * should have caught this above, but they could
3178 * have renamed something in the mean time.
3180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3181 "conflicting snapshot name from parent '%s'"),
3183 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3186 return (zfs_standard_error(hdl, save_errno, errbuf));
3189 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3197 const char *cd_snapname;
3202 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3204 struct createdata *cd = arg;
3207 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3208 char name[MAXPATHLEN];
3210 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3211 (void) strlcat(name, "@", sizeof (name));
3212 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3213 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3216 * NB: this is simply a best-effort. We don't want to
3217 * return an error, because then we wouldn't visit all
3222 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3230 * Takes a snapshot of the given dataset.
3233 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3237 char parent[ZFS_MAXNAMELEN];
3239 zfs_cmd_t zc = { 0 };
3243 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3244 "cannot snapshot '%s'"), path);
3246 /* validate the target name */
3247 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3248 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3251 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3252 props, B_FALSE, NULL, errbuf)) == NULL)
3255 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3263 /* make sure the parent exists and is of the appropriate type */
3264 delim = strchr(path, '@');
3265 (void) strncpy(parent, path, delim - path);
3266 parent[delim - path] = '\0';
3268 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3269 ZFS_TYPE_VOLUME)) == NULL) {
3270 zcmd_free_nvlists(&zc);
3274 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3275 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3276 if (ZFS_IS_VOLUME(zhp))
3277 zc.zc_objset_type = DMU_OST_ZVOL;
3279 zc.zc_objset_type = DMU_OST_ZFS;
3280 zc.zc_cookie = recursive;
3281 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3283 zcmd_free_nvlists(&zc);
3286 * if it was recursive, the one that actually failed will be in
3290 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3291 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3293 if (ret == 0 && recursive) {
3294 struct createdata cd;
3296 cd.cd_snapname = delim + 1;
3297 cd.cd_ifexists = B_FALSE;
3298 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3300 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3301 ret = zvol_create_link(zhp->zfs_hdl, path);
3303 (void) zfs_standard_error(hdl, errno,
3304 dgettext(TEXT_DOMAIN,
3305 "Volume successfully snapshotted, but device links "
3306 "were not created"));
3313 (void) zfs_standard_error(hdl, errno, errbuf);
3321 * Destroy any more recent snapshots. We invoke this callback on any dependents
3322 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3323 * is a dependent and we should just destroy it without checking the transaction
3326 typedef struct rollback_data {
3327 const char *cb_target; /* the snapshot */
3328 uint64_t cb_create; /* creation time reference */
3330 boolean_t cb_dependent;
3335 rollback_destroy(zfs_handle_t *zhp, void *data)
3337 rollback_data_t *cbp = data;
3339 if (!cbp->cb_dependent) {
3340 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3341 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3342 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3346 cbp->cb_dependent = B_TRUE;
3347 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3348 rollback_destroy, cbp);
3349 cbp->cb_dependent = B_FALSE;
3351 logstr = zhp->zfs_hdl->libzfs_log_str;
3352 zhp->zfs_hdl->libzfs_log_str = NULL;
3353 cbp->cb_error |= zfs_destroy(zhp);
3354 zhp->zfs_hdl->libzfs_log_str = logstr;
3357 /* We must destroy this clone; first unmount it */
3358 prop_changelist_t *clp;
3360 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3361 cbp->cb_force ? MS_FORCE: 0);
3362 if (clp == NULL || changelist_prefix(clp) != 0) {
3363 cbp->cb_error = B_TRUE;
3367 if (zfs_destroy(zhp) != 0)
3368 cbp->cb_error = B_TRUE;
3370 changelist_remove(clp, zhp->zfs_name);
3371 (void) changelist_postfix(clp);
3372 changelist_free(clp);
3380 * Given a dataset, rollback to a specific snapshot, discarding any
3381 * data changes since then and making it the active dataset.
3383 * Any snapshots more recent than the target are destroyed, along with
3387 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3389 rollback_data_t cb = { 0 };
3391 zfs_cmd_t zc = { 0 };
3392 boolean_t restore_resv = 0;
3393 uint64_t old_volsize, new_volsize;
3394 zfs_prop_t resv_prop;
3396 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3397 zhp->zfs_type == ZFS_TYPE_VOLUME);
3400 * Destroy all recent snapshots and its dependends.
3402 cb.cb_force = force;
3403 cb.cb_target = snap->zfs_name;
3404 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3405 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3411 * Now that we have verified that the snapshot is the latest,
3412 * rollback to the given snapshot.
3415 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3416 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3418 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3420 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3422 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3425 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3427 if (ZFS_IS_VOLUME(zhp))
3428 zc.zc_objset_type = DMU_OST_ZVOL;
3430 zc.zc_objset_type = DMU_OST_ZFS;
3433 * We rely on zfs_iter_children() to verify that there are no
3434 * newer snapshots for the given dataset. Therefore, we can
3435 * simply pass the name on to the ioctl() call. There is still
3436 * an unlikely race condition where the user has taken a
3437 * snapshot since we verified that this was the most recent.
3440 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3441 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3442 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3448 * For volumes, if the pre-rollback volsize matched the pre-
3449 * rollback reservation and the volsize has changed then set
3450 * the reservation property to the post-rollback volsize.
3451 * Make a new handle since the rollback closed the dataset.
3453 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3454 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3455 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
3460 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3461 if (old_volsize != new_volsize)
3462 err = zfs_prop_set_int(zhp, resv_prop,
3471 * Iterate over all dependents for a given dataset. This includes both
3472 * hierarchical dependents (children) and data dependents (snapshots and
3473 * clones). The bulk of the processing occurs in get_dependents() in
3477 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3478 zfs_iter_f func, void *data)
3483 zfs_handle_t *child;
3486 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3487 &dependents, &count) != 0)
3490 for (i = 0; i < count; i++) {
3491 if ((child = make_dataset_handle(zhp->zfs_hdl,
3492 dependents[i])) == NULL)
3495 if ((ret = func(child, data)) != 0)
3499 for (i = 0; i < count; i++)
3500 free(dependents[i]);
3507 * Renames the given dataset.
3510 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3513 zfs_cmd_t zc = { 0 };
3515 prop_changelist_t *cl = NULL;
3516 zfs_handle_t *zhrp = NULL;
3517 char *parentname = NULL;
3518 char parent[ZFS_MAXNAMELEN];
3519 libzfs_handle_t *hdl = zhp->zfs_hdl;
3522 /* if we have the same exact name, just return success */
3523 if (strcmp(zhp->zfs_name, target) == 0)
3526 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3527 "cannot rename to '%s'"), target);
3530 * Make sure the target name is valid
3532 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3533 if ((strchr(target, '@') == NULL) ||
3536 * Snapshot target name is abbreviated,
3537 * reconstruct full dataset name
3539 (void) strlcpy(parent, zhp->zfs_name,
3541 delim = strchr(parent, '@');
3542 if (strchr(target, '@') == NULL)
3546 (void) strlcat(parent, target, sizeof (parent));
3550 * Make sure we're renaming within the same dataset.
3552 delim = strchr(target, '@');
3553 if (strncmp(zhp->zfs_name, target, delim - target)
3554 != 0 || zhp->zfs_name[delim - target] != '@') {
3555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3556 "snapshots must be part of same "
3558 return (zfs_error(hdl, EZFS_CROSSTARGET,
3562 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3563 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3566 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3567 "recursive rename must be a snapshot"));
3568 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3571 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3572 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3575 /* validate parents */
3576 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3579 (void) parent_name(target, parent, sizeof (parent));
3581 /* make sure we're in the same pool */
3582 verify((delim = strchr(target, '/')) != NULL);
3583 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3584 zhp->zfs_name[delim - target] != '/') {
3585 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3586 "datasets must be within same pool"));
3587 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3590 /* new name cannot be a child of the current dataset name */
3591 if (strncmp(parent, zhp->zfs_name,
3592 strlen(zhp->zfs_name)) == 0) {
3593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3594 "New dataset name cannot be a descendent of "
3595 "current dataset name"));
3596 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3600 (void) snprintf(errbuf, sizeof (errbuf),
3601 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3603 if (getzoneid() == GLOBAL_ZONEID &&
3604 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3606 "dataset is used in a non-global zone"));
3607 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3611 struct destroydata dd;
3613 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3614 if (parentname == NULL) {
3618 delim = strchr(parentname, '@');
3620 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3626 dd.snapname = delim + 1;
3627 dd.gotone = B_FALSE;
3628 dd.closezhp = B_TRUE;
3630 /* We remove any zvol links prior to renaming them */
3631 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
3636 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3639 if (changelist_haszonedchild(cl)) {
3640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3641 "child dataset with inherited mountpoint is used "
3642 "in a non-global zone"));
3643 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3647 if ((ret = changelist_prefix(cl)) != 0)
3651 if (ZFS_IS_VOLUME(zhp))
3652 zc.zc_objset_type = DMU_OST_ZVOL;
3654 zc.zc_objset_type = DMU_OST_ZFS;
3656 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3657 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3659 zc.zc_cookie = recursive;
3661 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3663 * if it was recursive, the one that actually failed will
3666 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3667 "cannot rename '%s'"), zc.zc_name);
3669 if (recursive && errno == EEXIST) {
3670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3671 "a child dataset already has a snapshot "
3672 "with the new name"));
3673 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3675 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3679 * On failure, we still want to remount any filesystems that
3680 * were previously mounted, so we don't alter the system state.
3683 struct createdata cd;
3685 /* only create links for datasets that had existed */
3686 cd.cd_snapname = delim + 1;
3687 cd.cd_ifexists = B_TRUE;
3688 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3691 (void) changelist_postfix(cl);
3695 struct createdata cd;
3697 /* only create links for datasets that had existed */
3698 cd.cd_snapname = strchr(target, '@') + 1;
3699 cd.cd_ifexists = B_TRUE;
3700 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3703 changelist_rename(cl, zfs_get_name(zhp), target);
3704 ret = changelist_postfix(cl);
3716 changelist_free(cl);
3722 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3723 * poke devfsadm to create the /dev link, and then wait for the link to appear.
3726 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3728 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3732 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3734 zfs_cmd_t zc = { 0 };
3736 di_devlink_handle_t dhdl;
3737 priv_set_t *priv_effective;
3741 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3744 * Issue the appropriate ioctl.
3746 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3750 * Silently ignore the case where the link already
3751 * exists. This allows 'zfs volinit' to be run multiple
3752 * times without errors.
3758 * Dataset does not exist in the kernel. If we
3759 * don't care (see zfs_rename), then ignore the
3769 return (zfs_standard_error_fmt(hdl, errno,
3770 dgettext(TEXT_DOMAIN, "cannot create device links "
3771 "for '%s'"), dataset));
3777 * If privileged call devfsadm and wait for the links to
3779 * Otherwise, print out an informational message.
3782 priv_effective = priv_allocset();
3783 (void) getppriv(PRIV_EFFECTIVE, priv_effective);
3784 privileged = (priv_isfullset(priv_effective) == B_TRUE);
3785 priv_freeset(priv_effective);
3788 if ((dhdl = di_devlink_init(ZFS_DRIVER,
3789 DI_MAKE_LINK)) == NULL) {
3790 zfs_error_aux(hdl, strerror(errno));
3791 (void) zfs_error_fmt(hdl, errno,
3792 dgettext(TEXT_DOMAIN, "cannot create device links "
3793 "for '%s'"), dataset);
3794 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
3797 (void) di_devlink_fini(&dhdl);
3800 char pathname[MAXPATHLEN];
3801 struct stat64 statbuf;
3807 * This is the poor mans way of waiting for the link
3808 * to show up. If after 10 seconds we still don't
3809 * have it, then print out a message.
3811 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
3814 for (i = 0; i != MAX_WAIT; i++) {
3815 if (stat64(pathname, &statbuf) == 0)
3820 (void) printf(gettext("%s may not be immediately "
3821 "available\n"), pathname);
3829 * Remove a minor node for the given zvol and the associated /dev links.
3832 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3834 zfs_cmd_t zc = { 0 };
3836 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3838 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3842 * Silently ignore the case where the link no longer
3843 * exists, so that 'zfs volfini' can be run multiple
3844 * times without errors.
3849 return (zfs_standard_error_fmt(hdl, errno,
3850 dgettext(TEXT_DOMAIN, "cannot remove device "
3851 "links for '%s'"), dataset));
3859 zfs_get_user_props(zfs_handle_t *zhp)
3861 return (zhp->zfs_user_props);
3865 * This function is used by 'zfs list' to determine the exact set of columns to
3866 * display, and their maximum widths. This does two main things:
3868 * - If this is a list of all properties, then expand the list to include
3869 * all native properties, and set a flag so that for each dataset we look
3870 * for new unique user properties and add them to the list.
3872 * - For non fixed-width properties, keep track of the maximum width seen
3873 * so that we can size the column appropriately.
3876 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
3878 libzfs_handle_t *hdl = zhp->zfs_hdl;
3879 zprop_list_t *entry;
3880 zprop_list_t **last, **start;
3881 nvlist_t *userprops, *propval;
3884 char buf[ZFS_MAXPROPLEN];
3886 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3889 userprops = zfs_get_user_props(zhp);
3892 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3894 * Go through and add any user properties as necessary. We
3895 * start by incrementing our list pointer to the first
3896 * non-native property.
3899 while (*start != NULL) {
3900 if ((*start)->pl_prop == ZPROP_INVAL)
3902 start = &(*start)->pl_next;
3906 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3908 * See if we've already found this property in our list.
3910 for (last = start; *last != NULL;
3911 last = &(*last)->pl_next) {
3912 if (strcmp((*last)->pl_user_prop,
3913 nvpair_name(elem)) == 0)
3917 if (*last == NULL) {
3918 if ((entry = zfs_alloc(hdl,
3919 sizeof (zprop_list_t))) == NULL ||
3920 ((entry->pl_user_prop = zfs_strdup(hdl,
3921 nvpair_name(elem)))) == NULL) {
3926 entry->pl_prop = ZPROP_INVAL;
3927 entry->pl_width = strlen(nvpair_name(elem));
3928 entry->pl_all = B_TRUE;
3935 * Now go through and check the width of any non-fixed columns
3937 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3938 if (entry->pl_fixed)
3941 if (entry->pl_prop != ZPROP_INVAL) {
3942 if (zfs_prop_get(zhp, entry->pl_prop,
3943 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3944 if (strlen(buf) > entry->pl_width)
3945 entry->pl_width = strlen(buf);
3947 } else if (nvlist_lookup_nvlist(userprops,
3948 entry->pl_user_prop, &propval) == 0) {
3949 verify(nvlist_lookup_string(propval,
3950 ZPROP_VALUE, &strval) == 0);
3951 if (strlen(strval) > entry->pl_width)
3952 entry->pl_width = strlen(strval);
3961 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
3963 zfs_cmd_t zc = { 0 };
3967 const gid_t *groups;
3971 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
3972 return (no_memory(hdl));
3974 uid = ucred_geteuid(cred);
3975 gid = ucred_getegid(cred);
3976 group_cnt = ucred_getgroups(cred, &groups);
3978 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
3981 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
3986 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
3991 if (nvlist_add_uint32_array(nvp,
3992 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
3996 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3998 if (zcmd_write_src_nvlist(hdl, &zc, nvp))
4001 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
4008 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4009 char *resource, void *export, void *sharetab,
4010 int sharemax, zfs_share_op_t operation)
4012 zfs_cmd_t zc = { 0 };
4015 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4016 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4018 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4019 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4020 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4021 zc.zc_share.z_sharetype = operation;
4022 zc.zc_share.z_sharemax = sharemax;
4023 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4028 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4033 * Keep a reference to the props-table against which we prune the
4036 zhp->zfs_props_table = props;
4038 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4041 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4042 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4045 * We leave user:props in the nvlist, so there will be
4046 * some ZPROP_INVAL. To be extra safe, don't prune
4049 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4050 (void) nvlist_remove(zhp->zfs_props,
4051 nvpair_name(curr), nvpair_type(curr));
4058 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4059 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4061 zfs_cmd_t zc = { 0 };
4062 nvlist_t *nvlist = NULL;
4065 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4066 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4067 zc.zc_cookie = (uint64_t)cmd;
4069 if (cmd == ZFS_SMB_ACL_RENAME) {
4070 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4071 (void) no_memory(hdl);
4077 case ZFS_SMB_ACL_ADD:
4078 case ZFS_SMB_ACL_REMOVE:
4079 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4081 case ZFS_SMB_ACL_RENAME:
4082 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4084 (void) no_memory(hdl);
4087 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4089 (void) no_memory(hdl);
4092 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4093 nvlist_free(nvlist);
4097 case ZFS_SMB_ACL_PURGE:
4102 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4104 nvlist_free(nvlist);
4109 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4110 char *path, char *resource)
4112 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4117 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4118 char *path, char *resource)
4120 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4125 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4127 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4132 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4133 char *oldname, char *newname)
4135 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4141 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4142 zfs_userspace_cb_t func, void *arg)
4144 zfs_cmd_t zc = { 0 };
4146 zfs_useracct_t buf[100];
4148 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4150 zc.zc_objset_type = type;
4151 zc.zc_nvlist_dst = (uintptr_t)buf;
4155 zfs_useracct_t *zua = buf;
4157 zc.zc_nvlist_dst_size = sizeof (buf);
4158 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4159 ZFS_IOC_USERSPACE_MANY, &zc);
4160 if (error || zc.zc_nvlist_dst_size == 0)
4163 while (zc.zc_nvlist_dst_size > 0) {
4164 error = func(arg, zua->zu_domain, zua->zu_rid,
4169 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4177 * Attach/detach the given filesystem to/from the given jail.
4180 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4182 libzfs_handle_t *hdl = zhp->zfs_hdl;
4183 zfs_cmd_t zc = { 0 };
4188 (void) snprintf(errbuf, sizeof (errbuf),
4189 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4191 (void) snprintf(errbuf, sizeof (errbuf),
4192 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4195 switch (zhp->zfs_type) {
4196 case ZFS_TYPE_VOLUME:
4197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4198 "volumes can not be jailed"));
4199 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4200 case ZFS_TYPE_SNAPSHOT:
4201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4202 "snapshots can not be jailed"));
4203 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4205 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4207 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4208 zc.zc_objset_type = DMU_OST_ZFS;
4209 zc.zc_jailid = jailid;
4211 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4212 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4213 zfs_standard_error(hdl, errno, errbuf);