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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
47 #include <directory.h>
48 #endif /* HAVE_IDMAP */
50 #include <sys/dnode.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"));
86 * Validate a ZFS path. This is used even before trying to open the dataset, to
87 * provide a more meaningful error message. We call zfs_error_aux() to
88 * explain exactly why the name was not valid.
91 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
97 if (dataset_namecheck(path, &why, &what) != 0) {
100 case NAME_ERR_TOOLONG:
101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
102 "name is too long"));
105 case NAME_ERR_LEADING_SLASH:
106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107 "leading slash in name"));
110 case NAME_ERR_EMPTY_COMPONENT:
111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
112 "empty component in name"));
115 case NAME_ERR_TRAILING_SLASH:
116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
117 "trailing slash in name"));
120 case NAME_ERR_INVALCHAR:
122 dgettext(TEXT_DOMAIN, "invalid character "
123 "'%c' in name"), what);
126 case NAME_ERR_MULTIPLE_AT:
127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
128 "multiple '@' delimiters in name"));
131 case NAME_ERR_NOLETTER:
132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133 "pool doesn't begin with a letter"));
136 case NAME_ERR_RESERVED:
137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
138 "name is reserved"));
141 case NAME_ERR_DISKLIKE:
142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 "reserved disk name"));
153 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156 "snapshot delimiter '@' in filesystem name"));
160 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
163 "missing '@' delimiter in snapshot name"));
167 if (modifying && strchr(path, '%') != NULL) {
169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170 "invalid character %c in name"), '%');
178 zfs_name_valid(const char *name, zfs_type_t type)
180 if (type == ZFS_TYPE_POOL)
181 return (zpool_name_valid(NULL, B_FALSE, name));
182 return (zfs_validate_name(NULL, name, type, B_FALSE));
186 * This function takes the raw DSL properties, and filters out the user-defined
187 * properties into a separate nvlist.
190 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
192 libzfs_handle_t *hdl = zhp->zfs_hdl;
197 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
198 (void) no_memory(hdl);
203 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
204 if (!zfs_prop_user(nvpair_name(elem)))
207 verify(nvpair_value_nvlist(elem, &propval) == 0);
208 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
210 (void) no_memory(hdl);
218 static zpool_handle_t *
219 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
221 libzfs_handle_t *hdl = zhp->zfs_hdl;
224 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
225 if (hdl->libzfs_pool_handles != NULL)
226 zph->zpool_next = hdl->libzfs_pool_handles;
227 hdl->libzfs_pool_handles = zph;
232 static zpool_handle_t *
233 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
235 libzfs_handle_t *hdl = zhp->zfs_hdl;
236 zpool_handle_t *zph = hdl->libzfs_pool_handles;
238 while ((zph != NULL) &&
239 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
240 zph = zph->zpool_next;
245 * Returns a handle to the pool that contains the provided dataset.
246 * If a handle to that pool already exists then that handle is returned.
247 * Otherwise, a new handle is created and added to the list of handles.
249 static zpool_handle_t *
250 zpool_handle(zfs_handle_t *zhp)
256 len = strcspn(zhp->zfs_name, "/@") + 1;
257 pool_name = zfs_alloc(zhp->zfs_hdl, len);
258 (void) strlcpy(pool_name, zhp->zfs_name, len);
260 zph = zpool_find_handle(zhp, pool_name, len);
262 zph = zpool_add_handle(zhp, pool_name);
269 zpool_free_handles(libzfs_handle_t *hdl)
271 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
273 while (zph != NULL) {
274 next = zph->zpool_next;
278 hdl->libzfs_pool_handles = NULL;
282 * Utility function to gather stats (objset and zpl) for the given object.
285 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
287 libzfs_handle_t *hdl = zhp->zfs_hdl;
289 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
291 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
292 if (errno == ENOMEM) {
293 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
304 * Utility function to get the received properties of the given object.
307 get_recvd_props_ioctl(zfs_handle_t *zhp)
309 libzfs_handle_t *hdl = zhp->zfs_hdl;
310 nvlist_t *recvdprops;
311 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
314 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
317 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
319 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
320 if (errno == ENOMEM) {
321 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
325 zcmd_free_nvlists(&zc);
330 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
331 zcmd_free_nvlists(&zc);
335 nvlist_free(zhp->zfs_recvd_props);
336 zhp->zfs_recvd_props = recvdprops;
342 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
344 nvlist_t *allprops, *userprops;
346 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
348 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
353 * XXX Why do we store the user props separately, in addition to
354 * storing them in zfs_props?
356 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
357 nvlist_free(allprops);
361 nvlist_free(zhp->zfs_props);
362 nvlist_free(zhp->zfs_user_props);
364 zhp->zfs_props = allprops;
365 zhp->zfs_user_props = userprops;
371 get_stats(zfs_handle_t *zhp)
374 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
376 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
378 if (get_stats_ioctl(zhp, &zc) != 0)
380 else if (put_stats_zhdl(zhp, &zc) != 0)
382 zcmd_free_nvlists(&zc);
387 * Refresh the properties currently stored in the handle.
390 zfs_refresh_properties(zfs_handle_t *zhp)
392 (void) get_stats(zhp);
396 * Makes a handle from the given dataset name. Used by zfs_open() and
397 * zfs_iter_* to create child handles on the fly.
400 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
402 if (put_stats_zhdl(zhp, zc) != 0)
406 * We've managed to open the dataset and gather statistics. Determine
407 * the high-level type.
409 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
410 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
411 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
412 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
416 if (zhp->zfs_dmustats.dds_is_snapshot)
417 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
418 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
419 zhp->zfs_type = ZFS_TYPE_VOLUME;
420 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
421 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
423 abort(); /* we should never see any other types */
425 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
432 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
434 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
436 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
442 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
443 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
447 if (get_stats_ioctl(zhp, &zc) == -1) {
448 zcmd_free_nvlists(&zc);
452 if (make_dataset_handle_common(zhp, &zc) == -1) {
456 zcmd_free_nvlists(&zc);
460 static zfs_handle_t *
461 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
463 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
469 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
470 if (make_dataset_handle_common(zhp, zc) == -1) {
478 * Opens the given snapshot, filesystem, or volume. The 'types'
479 * argument is a mask of acceptable types. The function will print an
480 * appropriate error message and return NULL if it can't be opened.
483 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
488 (void) snprintf(errbuf, sizeof (errbuf),
489 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
492 * Validate the name before we even try to open it.
494 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
496 "invalid dataset name"));
497 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
502 * Try to get stats for the dataset, which will tell us if it exists.
505 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
506 (void) zfs_standard_error(hdl, errno, errbuf);
510 if (!(types & zhp->zfs_type)) {
511 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
520 * Release a ZFS handle. Nothing to do but free the associated memory.
523 zfs_close(zfs_handle_t *zhp)
525 if (zhp->zfs_mntopts)
526 free(zhp->zfs_mntopts);
527 nvlist_free(zhp->zfs_props);
528 nvlist_free(zhp->zfs_user_props);
529 nvlist_free(zhp->zfs_recvd_props);
533 typedef struct mnttab_node {
534 struct mnttab mtn_mt;
539 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
541 const mnttab_node_t *mtn1 = arg1;
542 const mnttab_node_t *mtn2 = arg2;
545 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
549 return (rv > 0 ? 1 : -1);
553 libzfs_mnttab_init(libzfs_handle_t *hdl)
555 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
556 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
557 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
561 libzfs_mnttab_update(libzfs_handle_t *hdl)
565 rewind(hdl->libzfs_mnttab);
566 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
569 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
571 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
572 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
573 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
574 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
575 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
576 avl_add(&hdl->libzfs_mnttab_cache, mtn);
581 libzfs_mnttab_fini(libzfs_handle_t *hdl)
586 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
587 free(mtn->mtn_mt.mnt_special);
588 free(mtn->mtn_mt.mnt_mountp);
589 free(mtn->mtn_mt.mnt_fstype);
590 free(mtn->mtn_mt.mnt_mntopts);
593 avl_destroy(&hdl->libzfs_mnttab_cache);
597 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
599 hdl->libzfs_mnttab_enable = enable;
603 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
604 struct mnttab *entry)
609 if (!hdl->libzfs_mnttab_enable) {
610 struct mnttab srch = { 0 };
612 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
613 libzfs_mnttab_fini(hdl);
614 rewind(hdl->libzfs_mnttab);
615 srch.mnt_special = (char *)fsname;
616 srch.mnt_fstype = MNTTYPE_ZFS;
617 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
623 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
624 libzfs_mnttab_update(hdl);
626 find.mtn_mt.mnt_special = (char *)fsname;
627 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
629 *entry = mtn->mtn_mt;
636 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
637 const char *mountp, const char *mntopts)
641 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
643 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
644 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
645 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
646 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
647 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
648 avl_add(&hdl->libzfs_mnttab_cache, mtn);
652 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
657 find.mtn_mt.mnt_special = (char *)fsname;
658 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
659 avl_remove(&hdl->libzfs_mnttab_cache, ret);
660 free(ret->mtn_mt.mnt_special);
661 free(ret->mtn_mt.mnt_mountp);
662 free(ret->mtn_mt.mnt_fstype);
663 free(ret->mtn_mt.mnt_mntopts);
669 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
671 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
673 if (zpool_handle == NULL)
676 *spa_version = zpool_get_prop_int(zpool_handle,
677 ZPOOL_PROP_VERSION, NULL);
682 * The choice of reservation property depends on the SPA version.
685 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
689 if (zfs_spa_version(zhp, &spa_version) < 0)
692 if (spa_version >= SPA_VERSION_REFRESERVATION)
693 *resv_prop = ZFS_PROP_REFRESERVATION;
695 *resv_prop = ZFS_PROP_RESERVATION;
701 * Given an nvlist of properties to set, validates that they are correct, and
702 * parses any numeric properties (index, boolean, etc) if they are specified as
706 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
707 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
714 int chosen_normal = -1;
717 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
718 (void) no_memory(hdl);
723 * Make sure this property is valid and applies to this type.
727 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
728 const char *propname = nvpair_name(elem);
730 prop = zfs_name_to_prop(propname);
731 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
733 * This is a user property: make sure it's a
734 * string, and that it's less than ZAP_MAXNAMELEN.
736 if (nvpair_type(elem) != DATA_TYPE_STRING) {
737 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
738 "'%s' must be a string"), propname);
739 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
743 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
745 "property name '%s' is too long"),
747 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
751 (void) nvpair_value_string(elem, &strval);
752 if (nvlist_add_string(ret, propname, strval) != 0) {
753 (void) no_memory(hdl);
760 * Currently, only user properties can be modified on
763 if (type == ZFS_TYPE_SNAPSHOT) {
764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
765 "this property can not be modified for snapshots"));
766 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
770 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
771 zfs_userquota_prop_t uqtype;
772 char newpropname[128];
777 if (userquota_propname_decode(propname, zoned,
778 &uqtype, domain, sizeof (domain), &rid) != 0) {
780 dgettext(TEXT_DOMAIN,
781 "'%s' has an invalid user/group name"),
783 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
787 if (uqtype != ZFS_PROP_USERQUOTA &&
788 uqtype != ZFS_PROP_GROUPQUOTA) {
790 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
792 (void) zfs_error(hdl, EZFS_PROPREADONLY,
797 if (nvpair_type(elem) == DATA_TYPE_STRING) {
798 (void) nvpair_value_string(elem, &strval);
799 if (strcmp(strval, "none") == 0) {
801 } else if (zfs_nicestrtonum(hdl,
802 strval, &intval) != 0) {
803 (void) zfs_error(hdl,
804 EZFS_BADPROP, errbuf);
807 } else if (nvpair_type(elem) ==
809 (void) nvpair_value_uint64(elem, &intval);
811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
812 "use 'none' to disable "
813 "userquota/groupquota"));
817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
818 "'%s' must be a number"), propname);
819 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
824 * Encode the prop name as
825 * userquota@<hex-rid>-domain, to make it easy
826 * for the kernel to decode.
828 (void) snprintf(newpropname, sizeof (newpropname),
829 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
830 (longlong_t)rid, domain);
834 if (nvlist_add_uint64_array(ret, newpropname,
836 (void) no_memory(hdl);
842 if (prop == ZPROP_INVAL) {
843 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
844 "invalid property '%s'"), propname);
845 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
849 if (!zfs_prop_valid_for_type(prop, type)) {
851 dgettext(TEXT_DOMAIN, "'%s' does not "
852 "apply to datasets of this type"), propname);
853 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
857 if (zfs_prop_readonly(prop) &&
858 (!zfs_prop_setonce(prop) || zhp != NULL)) {
860 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
862 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
866 if (zprop_parse_value(hdl, elem, prop, type, ret,
867 &strval, &intval, errbuf) != 0)
871 * Perform some additional checks for specific properties.
874 case ZFS_PROP_VERSION:
880 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
881 if (intval < version) {
882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
883 "Can not downgrade; already at version %u"),
885 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
891 case ZFS_PROP_RECORDSIZE:
892 case ZFS_PROP_VOLBLOCKSIZE:
893 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
894 if (intval < SPA_MINBLOCKSIZE ||
895 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
897 "'%s' must be power of 2 from %u "
899 (uint_t)SPA_MINBLOCKSIZE,
900 (uint_t)SPA_MAXBLOCKSIZE >> 10);
901 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
906 case ZFS_PROP_MLSLABEL:
910 * Verify the mlslabel string and convert to
911 * internal hex label string.
915 char *hex = NULL; /* internal label string */
917 /* Default value is already OK. */
918 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
921 /* Verify the label can be converted to binary form */
922 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
923 (str_to_label(strval, &new_sl, MAC_LABEL,
924 L_NO_CORRECTION, NULL) == -1)) {
928 /* Now translate to hex internal label string */
929 if (label_to_str(new_sl, &hex, M_INTERNAL,
935 m_label_free(new_sl);
937 /* If string is already in internal form, we're done. */
938 if (strcmp(strval, hex) == 0) {
943 /* Replace the label string with the internal form. */
944 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
946 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "invalid mlslabel '%s'"), strval);
955 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
956 m_label_free(new_sl); /* OK if null */
959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
960 "mlslabels are unsupported"));
961 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
963 #endif /* HAVE_MLSLABEL */
966 case ZFS_PROP_MOUNTPOINT:
970 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
971 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
974 if (mountpoint_namecheck(strval, &why)) {
976 case NAME_ERR_LEADING_SLASH:
978 dgettext(TEXT_DOMAIN,
979 "'%s' must be an absolute path, "
980 "'none', or 'legacy'"), propname);
982 case NAME_ERR_TOOLONG:
984 dgettext(TEXT_DOMAIN,
985 "component of '%s' is too long"),
991 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
999 case ZFS_PROP_SHARESMB:
1000 case ZFS_PROP_SHARENFS:
1002 * For the mountpoint and sharenfs or sharesmb
1003 * properties, check if it can be set in a
1004 * global/non-global zone based on
1005 * the zoned property value:
1007 * global zone non-global zone
1008 * --------------------------------------------------
1009 * zoned=on mountpoint (no) mountpoint (yes)
1010 * sharenfs (no) sharenfs (no)
1011 * sharesmb (no) sharesmb (no)
1013 * zoned=off mountpoint (yes) N/A
1018 if (getzoneid() == GLOBAL_ZONEID) {
1019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1020 "'%s' cannot be set on "
1021 "dataset in a non-global zone"),
1023 (void) zfs_error(hdl, EZFS_ZONED,
1026 } else if (prop == ZFS_PROP_SHARENFS ||
1027 prop == ZFS_PROP_SHARESMB) {
1028 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1029 "'%s' cannot be set in "
1030 "a non-global zone"), propname);
1031 (void) zfs_error(hdl, EZFS_ZONED,
1035 } else if (getzoneid() != GLOBAL_ZONEID) {
1037 * If zoned property is 'off', this must be in
1038 * a global zone. If not, something is wrong.
1040 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1041 "'%s' cannot be set while dataset "
1042 "'zoned' property is set"), propname);
1043 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1048 * At this point, it is legitimate to set the
1049 * property. Now we want to make sure that the
1050 * property value is valid if it is sharenfs.
1052 if ((prop == ZFS_PROP_SHARENFS ||
1053 prop == ZFS_PROP_SHARESMB) &&
1054 strcmp(strval, "on") != 0 &&
1055 strcmp(strval, "off") != 0) {
1056 zfs_share_proto_t proto;
1058 if (prop == ZFS_PROP_SHARESMB)
1064 * Must be an valid sharing protocol
1065 * option string so init the libshare
1066 * in order to enable the parser and
1067 * then parse the options. We use the
1068 * control API since we don't care about
1069 * the current configuration and don't
1070 * want the overhead of loading it
1071 * until we actually do something.
1074 if (zfs_init_libshare(hdl,
1075 SA_INIT_CONTROL_API) != SA_OK) {
1077 * An error occurred so we can't do
1080 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1081 "'%s' cannot be set: problem "
1082 "in share initialization"),
1084 (void) zfs_error(hdl, EZFS_BADPROP,
1089 if (zfs_parse_options(strval, proto) != SA_OK) {
1091 * There was an error in parsing so
1092 * deal with it by issuing an error
1093 * message and leaving after
1094 * uninitializing the the libshare
1097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1098 "'%s' cannot be set to invalid "
1099 "options"), propname);
1100 (void) zfs_error(hdl, EZFS_BADPROP,
1102 zfs_uninit_libshare(hdl);
1105 zfs_uninit_libshare(hdl);
1109 #endif /* HAVE_ZPL */
1110 case ZFS_PROP_UTF8ONLY:
1111 chosen_utf = (int)intval;
1113 case ZFS_PROP_NORMALIZE:
1114 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,
1173 * If normalization was chosen, but no UTF8 choice was made,
1174 * enforce rejection of non-UTF8 names.
1176 * If normalization was chosen, but rejecting non-UTF8 names
1177 * was explicitly not chosen, it is an error.
1179 if (chosen_normal > 0 && chosen_utf < 0) {
1180 if (nvlist_add_uint64(ret,
1181 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1182 (void) no_memory(hdl);
1185 } else if (chosen_normal > 0 && chosen_utf == 0) {
1186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1187 "'%s' must be set 'on' if normalization chosen"),
1188 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1189 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1200 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1202 uint64_t old_volsize;
1203 uint64_t new_volsize;
1204 uint64_t old_reservation;
1205 uint64_t new_reservation;
1206 zfs_prop_t resv_prop;
1209 * If this is an existing volume, and someone is setting the volsize,
1210 * make sure that it matches the reservation, or add it if necessary.
1212 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1213 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1215 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1216 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1217 old_reservation) || nvlist_lookup_uint64(nvl,
1218 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1221 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1224 new_reservation = zvol_volsize_to_reservation(new_volsize,
1226 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1227 new_reservation) != 0) {
1228 (void) no_memory(zhp->zfs_hdl);
1235 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1242 * For quotas and reservations, ENOSPC indicates
1243 * something different; setting a quota or reservation
1244 * doesn't use any disk space.
1247 case ZFS_PROP_QUOTA:
1248 case ZFS_PROP_REFQUOTA:
1249 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1250 "size is less than current used or "
1252 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1255 case ZFS_PROP_RESERVATION:
1256 case ZFS_PROP_REFRESERVATION:
1257 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1258 "size is greater than available space"));
1259 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1263 (void) zfs_standard_error(hdl, err, errbuf);
1269 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1273 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1277 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1278 "pool and or dataset must be upgraded to set this "
1279 "property or value"));
1280 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1284 if (prop == ZFS_PROP_COMPRESSION) {
1285 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1286 "property setting is not allowed on "
1287 "bootable datasets"));
1288 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1290 (void) zfs_standard_error(hdl, err, errbuf);
1295 if (prop == ZPROP_INVAL) {
1296 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1298 (void) zfs_standard_error(hdl, err, errbuf);
1304 * This platform can't address a volume this big.
1307 if (prop == ZFS_PROP_VOLSIZE) {
1308 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1314 (void) zfs_standard_error(hdl, err, errbuf);
1319 * Given a property name and value, set the property for the given dataset.
1322 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1324 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1326 prop_changelist_t *cl = NULL;
1328 libzfs_handle_t *hdl = zhp->zfs_hdl;
1329 nvlist_t *nvl = NULL, *realprops;
1331 boolean_t do_prefix;
1335 (void) snprintf(errbuf, sizeof (errbuf),
1336 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1339 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1340 nvlist_add_string(nvl, propname, propval) != 0) {
1341 (void) no_memory(hdl);
1345 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1346 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1352 prop = zfs_name_to_prop(propname);
1354 if (prop == ZFS_PROP_VOLSIZE) {
1355 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1359 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1362 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1363 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1364 "child dataset with inherited mountpoint is used "
1365 "in a non-global zone"));
1366 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1371 * If the dataset's canmount property is being set to noauto,
1372 * then we want to prevent unmounting & remounting it.
1374 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1375 (zprop_string_to_index(prop, propval, &idx,
1376 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1378 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1382 * Execute the corresponding ioctl() to set this property.
1384 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1386 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1389 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1392 zfs_setprop_error(hdl, prop, errno, errbuf);
1393 if (added_resv && errno == ENOSPC) {
1394 /* clean up the volsize property we tried to set */
1395 uint64_t old_volsize = zfs_prop_get_int(zhp,
1398 zcmd_free_nvlists(&zc);
1399 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1401 if (nvlist_add_uint64(nvl,
1402 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1405 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1407 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1411 ret = changelist_postfix(cl);
1414 * Refresh the statistics so the new property value
1418 (void) get_stats(zhp);
1423 zcmd_free_nvlists(&zc);
1425 changelist_free(cl);
1430 * Given a property, inherit the value from the parent dataset, or if received
1431 * is TRUE, revert to the received value, if any.
1434 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1436 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1438 prop_changelist_t *cl;
1439 libzfs_handle_t *hdl = zhp->zfs_hdl;
1443 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1444 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1446 zc.zc_cookie = received;
1447 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1449 * For user properties, the amount of work we have to do is very
1450 * small, so just do it here.
1452 if (!zfs_prop_user(propname)) {
1453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1454 "invalid property"));
1455 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1458 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1459 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1461 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1462 return (zfs_standard_error(hdl, errno, errbuf));
1468 * Verify that this property is inheritable.
1470 if (zfs_prop_readonly(prop))
1471 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1473 if (!zfs_prop_inheritable(prop) && !received)
1474 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1477 * Check to see if the value applies to this type
1479 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1480 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1483 * Normalize the name, to get rid of shorthand abbreviations.
1485 propname = zfs_prop_to_name(prop);
1486 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1487 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1489 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1490 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1492 "dataset is used in a non-global zone"));
1493 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1497 * Determine datasets which will be affected by this change, if any.
1499 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1502 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1504 "child dataset with inherited mountpoint is used "
1505 "in a non-global zone"));
1506 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1510 if ((ret = changelist_prefix(cl)) != 0)
1513 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1514 return (zfs_standard_error(hdl, errno, errbuf));
1517 if ((ret = changelist_postfix(cl)) != 0)
1521 * Refresh the statistics so the new property is reflected.
1523 (void) get_stats(zhp);
1527 changelist_free(cl);
1532 * True DSL properties are stored in an nvlist. The following two functions
1533 * extract them appropriately.
1536 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1542 if (nvlist_lookup_nvlist(zhp->zfs_props,
1543 zfs_prop_to_name(prop), &nv) == 0) {
1544 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1545 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1547 verify(!zhp->zfs_props_table ||
1548 zhp->zfs_props_table[prop] == B_TRUE);
1549 value = zfs_prop_default_numeric(prop);
1557 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1563 if (nvlist_lookup_nvlist(zhp->zfs_props,
1564 zfs_prop_to_name(prop), &nv) == 0) {
1565 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1566 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1568 verify(!zhp->zfs_props_table ||
1569 zhp->zfs_props_table[prop] == B_TRUE);
1570 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1579 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1581 return (zhp->zfs_props == zhp->zfs_recvd_props);
1585 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1587 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1588 zhp->zfs_props = zhp->zfs_recvd_props;
1592 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1594 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1599 * Internal function for getting a numeric property. Both zfs_prop_get() and
1600 * zfs_prop_get_int() are built using this interface.
1602 * Certain properties can be overridden using 'mount -o'. In this case, scan
1603 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1604 * If they differ from the on-disk values, report the current values and mark
1605 * the source "temporary".
1608 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1609 char **source, uint64_t *val)
1611 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1612 nvlist_t *zplprops = NULL;
1614 char *mntopt_on = NULL;
1615 char *mntopt_off = NULL;
1616 boolean_t received = zfs_is_recvd_props_mode(zhp);
1621 case ZFS_PROP_ATIME:
1622 mntopt_on = MNTOPT_ATIME;
1623 mntopt_off = MNTOPT_NOATIME;
1626 case ZFS_PROP_DEVICES:
1627 mntopt_on = MNTOPT_DEVICES;
1628 mntopt_off = MNTOPT_NODEVICES;
1632 mntopt_on = MNTOPT_EXEC;
1633 mntopt_off = MNTOPT_NOEXEC;
1636 case ZFS_PROP_READONLY:
1637 mntopt_on = MNTOPT_RO;
1638 mntopt_off = MNTOPT_RW;
1641 case ZFS_PROP_SETUID:
1642 mntopt_on = MNTOPT_SETUID;
1643 mntopt_off = MNTOPT_NOSETUID;
1646 case ZFS_PROP_XATTR:
1647 mntopt_on = MNTOPT_XATTR;
1648 mntopt_off = MNTOPT_NOXATTR;
1651 case ZFS_PROP_NBMAND:
1652 mntopt_on = MNTOPT_NBMAND;
1653 mntopt_off = MNTOPT_NONBMAND;
1660 * Because looking up the mount options is potentially expensive
1661 * (iterating over all of /etc/mnttab), we defer its calculation until
1662 * we're looking up a property which requires its presence.
1664 if (!zhp->zfs_mntcheck &&
1665 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1666 libzfs_handle_t *hdl = zhp->zfs_hdl;
1667 struct mnttab entry;
1669 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1670 zhp->zfs_mntopts = zfs_strdup(hdl,
1672 if (zhp->zfs_mntopts == NULL)
1676 zhp->zfs_mntcheck = B_TRUE;
1679 if (zhp->zfs_mntopts == NULL)
1680 mnt.mnt_mntopts = "";
1682 mnt.mnt_mntopts = zhp->zfs_mntopts;
1685 case ZFS_PROP_ATIME:
1686 case ZFS_PROP_DEVICES:
1688 case ZFS_PROP_READONLY:
1689 case ZFS_PROP_SETUID:
1690 case ZFS_PROP_XATTR:
1691 case ZFS_PROP_NBMAND:
1692 *val = getprop_uint64(zhp, prop, source);
1697 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1700 *src = ZPROP_SRC_TEMPORARY;
1701 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1704 *src = ZPROP_SRC_TEMPORARY;
1708 case ZFS_PROP_CANMOUNT:
1709 case ZFS_PROP_VOLSIZE:
1710 case ZFS_PROP_QUOTA:
1711 case ZFS_PROP_REFQUOTA:
1712 case ZFS_PROP_RESERVATION:
1713 case ZFS_PROP_REFRESERVATION:
1714 *val = getprop_uint64(zhp, prop, source);
1716 if (*source == NULL) {
1717 /* not default, must be local */
1718 *source = zhp->zfs_name;
1722 case ZFS_PROP_MOUNTED:
1723 *val = (zhp->zfs_mntopts != NULL);
1726 case ZFS_PROP_NUMCLONES:
1727 *val = zhp->zfs_dmustats.dds_num_clones;
1730 case ZFS_PROP_VERSION:
1731 case ZFS_PROP_NORMALIZE:
1732 case ZFS_PROP_UTF8ONLY:
1734 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1735 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1737 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1738 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1739 zcmd_free_nvlists(&zc);
1742 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1743 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1745 zcmd_free_nvlists(&zc);
1749 nvlist_free(zplprops);
1750 zcmd_free_nvlists(&zc);
1754 switch (zfs_prop_get_type(prop)) {
1755 case PROP_TYPE_NUMBER:
1756 case PROP_TYPE_INDEX:
1757 *val = getprop_uint64(zhp, prop, source);
1759 * If we tried to use a default value for a
1760 * readonly property, it means that it was not
1763 if (zfs_prop_readonly(prop) &&
1764 *source != NULL && (*source)[0] == '\0') {
1769 case PROP_TYPE_STRING:
1771 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1772 "cannot get non-numeric property"));
1773 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1774 dgettext(TEXT_DOMAIN, "internal error")));
1782 * Calculate the source type, given the raw source string.
1785 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1786 char *statbuf, size_t statlen)
1788 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1791 if (source == NULL) {
1792 *srctype = ZPROP_SRC_NONE;
1793 } else if (source[0] == '\0') {
1794 *srctype = ZPROP_SRC_DEFAULT;
1795 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1796 *srctype = ZPROP_SRC_RECEIVED;
1798 if (strcmp(source, zhp->zfs_name) == 0) {
1799 *srctype = ZPROP_SRC_LOCAL;
1801 (void) strlcpy(statbuf, source, statlen);
1802 *srctype = ZPROP_SRC_INHERITED;
1809 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1810 size_t proplen, boolean_t literal)
1815 if (zhp->zfs_recvd_props == NULL)
1816 if (get_recvd_props_ioctl(zhp) != 0)
1819 prop = zfs_name_to_prop(propname);
1821 if (prop != ZPROP_INVAL) {
1823 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1825 zfs_set_recvd_props_mode(zhp, &cookie);
1826 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1827 NULL, NULL, 0, literal);
1828 zfs_unset_recvd_props_mode(zhp, &cookie);
1829 } else if (zfs_prop_userquota(propname)) {
1834 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1835 propname, &propval) != 0)
1837 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1839 (void) strlcpy(propbuf, recvdval, proplen);
1842 return (err == 0 ? 0 : -1);
1846 * Retrieve a property from the given object. If 'literal' is specified, then
1847 * numbers are left as exact values. Otherwise, numbers are converted to a
1848 * human-readable form.
1850 * Returns 0 on success, or -1 on error.
1853 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1854 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1856 char *source = NULL;
1860 boolean_t received = zfs_is_recvd_props_mode(zhp);
1863 * Check to see if this property applies to our object
1865 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1868 if (received && zfs_prop_readonly(prop))
1872 *src = ZPROP_SRC_NONE;
1875 case ZFS_PROP_CREATION:
1877 * 'creation' is a time_t stored in the statistics. We convert
1878 * this into a string unless 'literal' is specified.
1881 val = getprop_uint64(zhp, prop, &source);
1882 time_t time = (time_t)val;
1886 localtime_r(&time, &t) == NULL ||
1887 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1889 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
1893 case ZFS_PROP_MOUNTPOINT:
1895 * Getting the precise mountpoint can be tricky.
1897 * - for 'none' or 'legacy', return those values.
1898 * - for inherited mountpoints, we want to take everything
1899 * after our ancestor and append it to the inherited value.
1901 * If the pool has an alternate root, we want to prepend that
1902 * root to any values we return.
1905 str = getprop_string(zhp, prop, &source);
1907 if (str[0] == '/') {
1908 char buf[MAXPATHLEN];
1910 const char *relpath;
1913 * If we inherit the mountpoint, even from a dataset
1914 * with a received value, the source will be the path of
1915 * the dataset we inherit from. If source is
1916 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1919 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1922 relpath = zhp->zfs_name + strlen(source);
1923 if (relpath[0] == '/')
1927 if ((zpool_get_prop(zhp->zpool_hdl,
1928 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1929 (strcmp(root, "-") == 0))
1932 * Special case an alternate root of '/'. This will
1933 * avoid having multiple leading slashes in the
1936 if (strcmp(root, "/") == 0)
1940 * If the mountpoint is '/' then skip over this
1941 * if we are obtaining either an alternate root or
1942 * an inherited mountpoint.
1944 if (str[1] == '\0' && (root[0] != '\0' ||
1945 relpath[0] != '\0'))
1948 if (relpath[0] == '\0')
1949 (void) snprintf(propbuf, proplen, "%s%s",
1952 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1953 root, str, relpath[0] == '@' ? "" : "/",
1956 /* 'legacy' or 'none' */
1957 (void) strlcpy(propbuf, str, proplen);
1962 case ZFS_PROP_ORIGIN:
1963 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1966 * If there is no parent at all, return failure to indicate that
1967 * it doesn't apply to this dataset.
1969 if (propbuf[0] == '\0')
1973 case ZFS_PROP_QUOTA:
1974 case ZFS_PROP_REFQUOTA:
1975 case ZFS_PROP_RESERVATION:
1976 case ZFS_PROP_REFRESERVATION:
1978 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1982 * If quota or reservation is 0, we translate this into 'none'
1983 * (unless literal is set), and indicate that it's the default
1984 * value. Otherwise, we print the number nicely and indicate
1985 * that its set locally.
1989 (void) strlcpy(propbuf, "0", proplen);
1991 (void) strlcpy(propbuf, "none", proplen);
1994 (void) snprintf(propbuf, proplen, "%llu",
1997 zfs_nicenum(val, propbuf, proplen);
2001 case ZFS_PROP_COMPRESSRATIO:
2002 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2004 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2005 (u_longlong_t)(val / 100),
2006 (u_longlong_t)(val % 100));
2010 switch (zhp->zfs_type) {
2011 case ZFS_TYPE_FILESYSTEM:
2014 case ZFS_TYPE_VOLUME:
2017 case ZFS_TYPE_SNAPSHOT:
2023 (void) snprintf(propbuf, proplen, "%s", str);
2026 case ZFS_PROP_MOUNTED:
2028 * The 'mounted' property is a pseudo-property that described
2029 * whether the filesystem is currently mounted. Even though
2030 * it's a boolean value, the typical values of "on" and "off"
2031 * don't make sense, so we translate to "yes" and "no".
2033 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2034 src, &source, &val) != 0)
2037 (void) strlcpy(propbuf, "yes", proplen);
2039 (void) strlcpy(propbuf, "no", proplen);
2044 * The 'name' property is a pseudo-property derived from the
2045 * dataset name. It is presented as a real property to simplify
2048 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2051 case ZFS_PROP_MLSLABEL:
2053 #ifdef HAVE_MLSLABEL
2054 m_label_t *new_sl = NULL;
2055 char *ascii = NULL; /* human readable label */
2057 (void) strlcpy(propbuf,
2058 getprop_string(zhp, prop, &source), proplen);
2060 if (literal || (strcasecmp(propbuf,
2061 ZFS_MLSLABEL_DEFAULT) == 0))
2065 * Try to translate the internal hex string to
2066 * human-readable output. If there are any
2067 * problems just use the hex string.
2070 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2071 L_NO_CORRECTION, NULL) == -1) {
2072 m_label_free(new_sl);
2076 if (label_to_str(new_sl, &ascii, M_LABEL,
2080 m_label_free(new_sl);
2083 m_label_free(new_sl);
2085 (void) strlcpy(propbuf, ascii, proplen);
2088 (void) strlcpy(propbuf,
2089 getprop_string(zhp, prop, &source), proplen);
2090 #endif /* HAVE_MLSLABEL */
2095 switch (zfs_prop_get_type(prop)) {
2096 case PROP_TYPE_NUMBER:
2097 if (get_numeric_property(zhp, prop, src,
2098 &source, &val) != 0)
2101 (void) snprintf(propbuf, proplen, "%llu",
2104 zfs_nicenum(val, propbuf, proplen);
2107 case PROP_TYPE_STRING:
2108 (void) strlcpy(propbuf,
2109 getprop_string(zhp, prop, &source), proplen);
2112 case PROP_TYPE_INDEX:
2113 if (get_numeric_property(zhp, prop, src,
2114 &source, &val) != 0)
2116 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2118 (void) strlcpy(propbuf, strval, proplen);
2126 get_source(zhp, src, source, statbuf, statlen);
2132 * Utility function to get the given numeric property. Does no validation that
2133 * the given property is the appropriate type; should only be used with
2134 * hard-coded property types.
2137 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2142 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2148 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2152 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2153 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2157 * Similar to zfs_prop_get(), but returns the value as an integer.
2160 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2161 zprop_source_t *src, char *statbuf, size_t statlen)
2166 * Check to see if this property applies to our object
2168 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2169 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2170 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2171 zfs_prop_to_name(prop)));
2175 *src = ZPROP_SRC_NONE;
2177 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2180 get_source(zhp, src, source, statbuf, statlen);
2187 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2188 char **domainp, idmap_rid_t *ridp)
2190 idmap_get_handle_t *get_hdl = NULL;
2194 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2198 err = idmap_get_sidbyuid(get_hdl, id,
2199 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2201 err = idmap_get_sidbygid(get_hdl, id,
2202 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2204 if (err == IDMAP_SUCCESS &&
2205 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2206 status == IDMAP_SUCCESS)
2212 idmap_get_destroy(get_hdl);
2215 #endif /* HAVE_IDMAP */
2218 * convert the propname into parameters needed by kernel
2219 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2220 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2223 userquota_propname_decode(const char *propname, boolean_t zoned,
2224 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2226 zfs_userquota_prop_t type;
2228 char *numericsid = NULL;
2233 /* Figure out the property type ({user|group}{quota|space}) */
2234 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2235 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2236 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2239 if (type == ZFS_NUM_USERQUOTA_PROPS)
2243 isuser = (type == ZFS_PROP_USERQUOTA ||
2244 type == ZFS_PROP_USERUSED);
2246 cp = strchr(propname, '@') + 1;
2248 if (strchr(cp, '@')) {
2251 * It's a SID name (eg "user@domain") that needs to be
2252 * turned into S-1-domainID-RID.
2254 directory_error_t e;
2255 if (zoned && getzoneid() == GLOBAL_ZONEID)
2258 e = directory_sid_from_user_name(NULL,
2261 e = directory_sid_from_group_name(NULL,
2265 directory_error_free(e);
2268 if (numericsid == NULL)
2271 /* will be further decoded below */
2274 #endif /* HAVE_IDMAP */
2277 if (strncmp(cp, "S-1-", 4) == 0) {
2278 /* It's a numeric SID (eg "S-1-234-567-89") */
2279 (void) strlcpy(domain, cp, domainlen);
2280 cp = strrchr(domain, '-');
2285 *ridp = strtoull(cp, &end, 10);
2290 if (errno != 0 || *end != '\0')
2292 } else if (!isdigit(*cp)) {
2294 * It's a user/group name (eg "user") that needs to be
2295 * turned into a uid/gid
2297 if (zoned && getzoneid() == GLOBAL_ZONEID)
2314 /* It's a user/group ID (eg "12345"). */
2315 uid_t id = strtoul(cp, &end, 10);
2322 /* It's an ephemeral ID. */
2323 if (idmap_id_to_numeric_domain_rid(id, isuser,
2324 &mapdomain, &rid) != 0)
2326 (void) strlcpy(domain, mapdomain, domainlen);
2333 #endif /* HAVE_IDMAP */
2336 ASSERT3P(numericsid, ==, NULL);
2341 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2342 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2345 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2347 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2349 err = userquota_propname_decode(propname,
2350 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2351 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2352 zc.zc_objset_type = *typep;
2356 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2360 *propvalue = zc.zc_cookie;
2365 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2366 uint64_t *propvalue)
2368 zfs_userquota_prop_t type;
2370 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2375 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2376 char *propbuf, int proplen, boolean_t literal)
2380 zfs_userquota_prop_t type;
2382 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2389 (void) snprintf(propbuf, proplen, "%llu",
2390 (u_longlong_t)propvalue);
2391 } else if (propvalue == 0 &&
2392 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2393 (void) strlcpy(propbuf, "none", proplen);
2395 zfs_nicenum(propvalue, propbuf, proplen);
2401 * Returns the name of the given zfs handle.
2404 zfs_get_name(const zfs_handle_t *zhp)
2406 return (zhp->zfs_name);
2410 * Returns the type of the given zfs handle.
2413 zfs_get_type(const zfs_handle_t *zhp)
2415 return (zhp->zfs_type);
2419 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2422 uint64_t orig_cookie;
2424 orig_cookie = zc->zc_cookie;
2426 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2427 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2432 /* expand nvlist memory and try again */
2433 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2434 zcmd_free_nvlists(zc);
2437 zc->zc_cookie = orig_cookie;
2440 * An errno value of ESRCH indicates normal completion.
2441 * If ENOENT is returned, then the underlying dataset
2442 * has been removed since we obtained the handle.
2449 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2450 dgettext(TEXT_DOMAIN,
2451 "cannot iterate filesystems"));
2459 * Iterate over all child filesystems
2462 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2464 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2468 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2471 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2474 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2477 * Silently ignore errors, as the only plausible explanation is
2478 * that the pool has since been removed.
2480 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2485 if ((ret = func(nzhp, data)) != 0) {
2486 zcmd_free_nvlists(&zc);
2490 zcmd_free_nvlists(&zc);
2491 return ((ret < 0) ? ret : 0);
2495 * Iterate over all snapshots
2498 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2500 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2504 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2507 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2509 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2512 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2517 if ((ret = func(nzhp, data)) != 0) {
2518 zcmd_free_nvlists(&zc);
2522 zcmd_free_nvlists(&zc);
2523 return ((ret < 0) ? ret : 0);
2527 * Iterate over all children, snapshots and filesystems
2530 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2534 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2537 return (zfs_iter_snapshots(zhp, func, data));
2541 * Is one dataset name a child dataset of another?
2543 * Needs to handle these cases:
2544 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2545 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2546 * Descendant? No. No. No. Yes.
2549 is_descendant(const char *ds1, const char *ds2)
2551 size_t d1len = strlen(ds1);
2553 /* ds2 can't be a descendant if it's smaller */
2554 if (strlen(ds2) < d1len)
2557 /* otherwise, compare strings and verify that there's a '/' char */
2558 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2562 * Given a complete name, return just the portion that refers to the parent.
2563 * Can return NULL if this is a pool.
2566 parent_name(const char *path, char *buf, size_t buflen)
2570 if ((loc = strrchr(path, '/')) == NULL)
2573 (void) strncpy(buf, path, MIN(buflen, loc - path));
2574 buf[loc - path] = '\0';
2580 * If accept_ancestor is false, then check to make sure that the given path has
2581 * a parent, and that it exists. If accept_ancestor is true, then find the
2582 * closest existing ancestor for the given path. In prefixlen return the
2583 * length of already existing prefix of the given path. We also fetch the
2584 * 'zoned' property, which is used to validate property settings when creating
2588 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2589 boolean_t accept_ancestor, int *prefixlen)
2591 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2592 char parent[ZFS_MAXNAMELEN];
2598 (void) snprintf(errbuf, sizeof (errbuf),
2599 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2601 /* get parent, and check to see if this is just a pool */
2602 if (parent_name(path, parent, sizeof (parent)) != 0) {
2603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2604 "missing dataset name"));
2605 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2608 /* check to see if the pool exists */
2609 if ((slash = strchr(parent, '/')) == NULL)
2610 slash = parent + strlen(parent);
2611 (void) strncpy(zc.zc_name, parent, slash - parent);
2612 zc.zc_name[slash - parent] = '\0';
2613 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2616 "no such pool '%s'"), zc.zc_name);
2617 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2620 /* check to see if the parent dataset exists */
2621 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2622 if (errno == ENOENT && accept_ancestor) {
2624 * Go deeper to find an ancestor, give up on top level.
2626 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2628 "no such pool '%s'"), zc.zc_name);
2629 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2631 } else if (errno == ENOENT) {
2632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2633 "parent does not exist"));
2634 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2636 return (zfs_standard_error(hdl, errno, errbuf));
2639 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2643 /* we are in a non-global zone, but parent is in the global zone */
2644 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2645 (void) zfs_standard_error(hdl, EPERM, errbuf);
2650 /* make sure parent is a filesystem */
2651 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2653 "parent is not a filesystem"));
2654 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2660 if (prefixlen != NULL)
2661 *prefixlen = strlen(parent);
2666 * Finds whether the dataset of the given type(s) exists.
2669 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2673 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2677 * Try to get stats for the dataset, which will tell us if it exists.
2679 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2680 int ds_type = zhp->zfs_type;
2683 if (types & ds_type)
2690 * Given a path to 'target', create all the ancestors between
2691 * the prefixlen portion of the path, and the target itself.
2692 * Fail if the initial prefixlen-ancestor does not already exist.
2695 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2701 /* make sure prefix exists */
2702 cp = target + prefixlen;
2704 assert(strchr(cp, '/') == NULL);
2705 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2708 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2716 * Attempt to create, mount, and share any ancestor filesystems,
2717 * up to the prefixlen-long one.
2719 for (cp = target + prefixlen + 1;
2720 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2725 h = make_dataset_handle(hdl, target);
2727 /* it already exists, nothing to do here */
2732 logstr = hdl->libzfs_log_str;
2733 hdl->libzfs_log_str = NULL;
2734 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2736 hdl->libzfs_log_str = logstr;
2737 opname = dgettext(TEXT_DOMAIN, "create");
2741 hdl->libzfs_log_str = logstr;
2742 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2744 opname = dgettext(TEXT_DOMAIN, "open");
2749 if (zfs_mount(h, NULL, 0) != 0) {
2750 opname = dgettext(TEXT_DOMAIN, "mount");
2754 if (zfs_share(h) != 0) {
2755 opname = dgettext(TEXT_DOMAIN, "share");
2758 #endif /* HAVE_ZPL */
2766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2767 "failed to %s ancestor '%s'"), opname, target);
2772 * Creates non-existing ancestors of the given path.
2775 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2781 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2784 if ((path_copy = strdup(path)) != NULL) {
2785 rc = create_parents(hdl, path_copy, prefix);
2788 if (path_copy == NULL || rc != 0)
2795 * Create a new filesystem or volume.
2798 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2801 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2804 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2808 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2809 "cannot create '%s'"), path);
2811 /* validate the path, taking care to note the extended error message */
2812 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2813 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2815 /* validate parents exist */
2816 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2820 * The failure modes when creating a dataset of a different type over
2821 * one that already exists is a little strange. In particular, if you
2822 * try to create a dataset on top of an existing dataset, the ioctl()
2823 * will return ENOENT, not EEXIST. To prevent this from happening, we
2824 * first try to see if the dataset exists.
2826 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2827 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2829 "dataset already exists"));
2830 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2833 if (type == ZFS_TYPE_VOLUME)
2834 zc.zc_objset_type = DMU_OST_ZVOL;
2836 zc.zc_objset_type = DMU_OST_ZFS;
2838 if (props && (props = zfs_valid_proplist(hdl, type, props,
2839 zoned, NULL, errbuf)) == 0)
2842 if (type == ZFS_TYPE_VOLUME) {
2844 * If we are creating a volume, the size and block size must
2845 * satisfy a few restraints. First, the blocksize must be a
2846 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2847 * volsize must be a multiple of the block size, and cannot be
2850 if (props == NULL || nvlist_lookup_uint64(props,
2851 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2853 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2854 "missing volume size"));
2855 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2858 if ((ret = nvlist_lookup_uint64(props,
2859 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2860 &blocksize)) != 0) {
2861 if (ret == ENOENT) {
2862 blocksize = zfs_prop_default_numeric(
2863 ZFS_PROP_VOLBLOCKSIZE);
2866 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2867 "missing volume block size"));
2868 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2875 "volume size cannot be zero"));
2876 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2879 if (size % blocksize != 0) {
2881 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2882 "volume size must be a multiple of volume block "
2884 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2888 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2892 /* create the dataset */
2893 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2895 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2896 ret = zvol_create_link(hdl, path);
2898 (void) zfs_standard_error(hdl, errno,
2899 dgettext(TEXT_DOMAIN,
2900 "Volume successfully created, but device links "
2901 "were not created"));
2902 zcmd_free_nvlists(&zc);
2907 zcmd_free_nvlists(&zc);
2909 /* check for failure */
2911 char parent[ZFS_MAXNAMELEN];
2912 (void) parent_name(path, parent, sizeof (parent));
2916 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2917 "no such parent '%s'"), parent);
2918 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2921 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2922 "parent '%s' is not a filesystem"), parent);
2923 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2926 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2927 "volume block size must be power of 2 from "
2929 (uint_t)SPA_MINBLOCKSIZE,
2930 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2932 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2935 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2936 "pool must be upgraded to set this "
2937 "property or value"));
2938 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2942 * This platform can't address a volume this big.
2944 if (type == ZFS_TYPE_VOLUME)
2945 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2950 return (zfs_standard_error(hdl, errno, errbuf));
2958 * Destroys the given dataset. The caller must make sure that the filesystem
2959 * isn't mounted, and that there are no active dependents.
2962 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2964 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2966 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2968 if (ZFS_IS_VOLUME(zhp)) {
2969 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2972 zc.zc_objset_type = DMU_OST_ZVOL;
2974 zc.zc_objset_type = DMU_OST_ZFS;
2977 zc.zc_defer_destroy = defer;
2978 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2979 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2980 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2984 remove_mountpoint(zhp);
2989 struct destroydata {
2996 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
2998 struct destroydata *dd = arg;
3000 char name[ZFS_MAXNAMELEN];
3001 boolean_t closezhp = dd->closezhp;
3004 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3005 (void) strlcat(name, "@", sizeof (name));
3006 (void) strlcat(name, dd->snapname, sizeof (name));
3008 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3010 dd->gotone = B_TRUE;
3014 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3015 (void) zvol_remove_link(zhp->zfs_hdl, name);
3017 * NB: this is simply a best-effort. We don't want to
3018 * return an error, because then we wouldn't visit all
3023 dd->closezhp = B_TRUE;
3024 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
3031 * Destroys all snapshots with the given name in zhp & descendants.
3034 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3036 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3038 struct destroydata dd = { 0 };
3040 dd.snapname = snapname;
3041 (void) zfs_check_snap_cb(zhp, &dd);
3044 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3045 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3046 zhp->zfs_name, snapname));
3049 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3050 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3051 zc.zc_defer_destroy = defer;
3053 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3057 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3058 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3062 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3063 "snapshot is cloned"));
3064 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3067 return (zfs_standard_error(zhp->zfs_hdl, errno,
3076 * Clones the given dataset. The target must be of the same type as the source.
3079 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3081 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3082 char parent[ZFS_MAXNAMELEN];
3085 libzfs_handle_t *hdl = zhp->zfs_hdl;
3089 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3091 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3092 "cannot create '%s'"), target);
3094 /* validate the target name */
3095 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3096 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3098 /* validate parents exist */
3099 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3102 (void) parent_name(target, parent, sizeof (parent));
3105 if (ZFS_IS_VOLUME(zhp)) {
3106 zc.zc_objset_type = DMU_OST_ZVOL;
3107 type = ZFS_TYPE_VOLUME;
3109 zc.zc_objset_type = DMU_OST_ZFS;
3110 type = ZFS_TYPE_FILESYSTEM;
3114 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3115 zhp, errbuf)) == NULL)
3118 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3126 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3127 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3128 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3130 zcmd_free_nvlists(&zc);
3137 * The parent doesn't exist. We should have caught this
3138 * above, but there may a race condition that has since
3139 * destroyed the parent.
3141 * At this point, we don't know whether it's the source
3142 * that doesn't exist anymore, or whether the target
3143 * dataset doesn't exist.
3145 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3146 "no such parent '%s'"), parent);
3147 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3150 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3151 "source and target pools differ"));
3152 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3156 return (zfs_standard_error(zhp->zfs_hdl, errno,
3159 } else if (ZFS_IS_VOLUME(zhp)) {
3160 ret = zvol_create_link(zhp->zfs_hdl, target);
3166 typedef struct promote_data {
3167 char cb_mountpoint[MAXPATHLEN];
3168 const char *cb_target;
3169 const char *cb_errbuf;
3170 uint64_t cb_pivot_txg;
3174 promote_snap_cb(zfs_handle_t *zhp, void *data)
3176 promote_data_t *pd = data;
3178 char snapname[MAXPATHLEN];
3181 /* We don't care about snapshots after the pivot point */
3182 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3187 /* Remove the device link if it's a zvol. */
3188 if (ZFS_IS_VOLUME(zhp))
3189 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3191 /* Check for conflicting names */
3192 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3193 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3194 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3197 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3198 "snapshot name '%s' from origin \n"
3199 "conflicts with '%s' from target"),
3200 zhp->zfs_name, snapname);
3201 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3208 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3210 promote_data_t *pd = data;
3212 /* We don't care about snapshots after the pivot point */
3213 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3214 /* Create the device link if it's a zvol. */
3215 if (ZFS_IS_VOLUME(zhp))
3216 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3224 * Promotes the given clone fs to be the clone parent.
3227 zfs_promote(zfs_handle_t *zhp)
3229 libzfs_handle_t *hdl = zhp->zfs_hdl;
3230 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3231 char parent[MAXPATHLEN];
3238 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3239 "cannot promote '%s'"), zhp->zfs_name);
3241 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3242 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3243 "snapshots can not be promoted"));
3244 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3247 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3248 if (parent[0] == '\0') {
3249 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3250 "not a cloned filesystem"));
3251 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3253 cp = strchr(parent, '@');
3256 /* Walk the snapshots we will be moving */
3257 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3260 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3262 pd.cb_target = zhp->zfs_name;
3263 pd.cb_errbuf = errbuf;
3264 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3267 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3268 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3269 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3275 /* issue the ioctl */
3276 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3277 sizeof (zc.zc_value));
3278 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3279 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3282 int save_errno = errno;
3284 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3287 switch (save_errno) {
3290 * There is a conflicting snapshot name. We
3291 * should have caught this above, but they could
3292 * have renamed something in the mean time.
3294 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3295 "conflicting snapshot '%s' from parent '%s'"),
3296 zc.zc_string, parent);
3297 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3300 return (zfs_standard_error(hdl, save_errno, errbuf));
3303 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3311 const char *cd_snapname;
3316 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3318 struct createdata *cd = arg;
3321 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3322 char name[MAXPATHLEN];
3324 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3325 (void) strlcat(name, "@", sizeof (name));
3326 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3327 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3330 * NB: this is simply a best-effort. We don't want to
3331 * return an error, because then we wouldn't visit all
3336 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3344 * Takes a snapshot of the given dataset.
3347 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3351 char parent[ZFS_MAXNAMELEN];
3353 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3357 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3358 "cannot snapshot '%s'"), path);
3360 /* validate the target name */
3361 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3362 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3365 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3366 props, B_FALSE, NULL, errbuf)) == NULL)
3369 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3377 /* make sure the parent exists and is of the appropriate type */
3378 delim = strchr(path, '@');
3379 (void) strncpy(parent, path, delim - path);
3380 parent[delim - path] = '\0';
3382 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3383 ZFS_TYPE_VOLUME)) == NULL) {
3384 zcmd_free_nvlists(&zc);
3388 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3389 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3390 if (ZFS_IS_VOLUME(zhp))
3391 zc.zc_objset_type = DMU_OST_ZVOL;
3393 zc.zc_objset_type = DMU_OST_ZFS;
3394 zc.zc_cookie = recursive;
3395 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3397 zcmd_free_nvlists(&zc);
3400 * if it was recursive, the one that actually failed will be in
3404 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3405 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3407 if (ret == 0 && recursive) {
3408 struct createdata cd;
3410 cd.cd_snapname = delim + 1;
3411 cd.cd_ifexists = B_FALSE;
3412 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3414 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3415 ret = zvol_create_link(zhp->zfs_hdl, path);
3417 (void) zfs_standard_error(hdl, errno,
3418 dgettext(TEXT_DOMAIN,
3419 "Volume successfully snapshotted, but device links "
3420 "were not created"));
3427 (void) zfs_standard_error(hdl, errno, errbuf);
3435 * Destroy any more recent snapshots. We invoke this callback on any dependents
3436 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3437 * is a dependent and we should just destroy it without checking the transaction
3440 typedef struct rollback_data {
3441 const char *cb_target; /* the snapshot */
3442 uint64_t cb_create; /* creation time reference */
3444 boolean_t cb_dependent;
3449 rollback_destroy(zfs_handle_t *zhp, void *data)
3451 rollback_data_t *cbp = data;
3453 if (!cbp->cb_dependent) {
3454 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3455 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3456 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3460 cbp->cb_dependent = B_TRUE;
3461 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3462 rollback_destroy, cbp);
3463 cbp->cb_dependent = B_FALSE;
3465 logstr = zhp->zfs_hdl->libzfs_log_str;
3466 zhp->zfs_hdl->libzfs_log_str = NULL;
3467 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3468 zhp->zfs_hdl->libzfs_log_str = logstr;
3471 /* We must destroy this clone; first unmount it */
3472 prop_changelist_t *clp;
3474 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3475 cbp->cb_force ? MS_FORCE: 0);
3476 if (clp == NULL || changelist_prefix(clp) != 0) {
3477 cbp->cb_error = B_TRUE;
3481 if (zfs_destroy(zhp, B_FALSE) != 0)
3482 cbp->cb_error = B_TRUE;
3484 changelist_remove(clp, zhp->zfs_name);
3485 (void) changelist_postfix(clp);
3486 changelist_free(clp);
3494 * Given a dataset, rollback to a specific snapshot, discarding any
3495 * data changes since then and making it the active dataset.
3497 * Any snapshots more recent than the target are destroyed, along with
3501 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3503 rollback_data_t cb = { 0 };
3505 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3506 boolean_t restore_resv = 0;
3507 uint64_t old_volsize = 0, new_volsize;
3508 zfs_prop_t resv_prop = { 0 };
3510 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3511 zhp->zfs_type == ZFS_TYPE_VOLUME);
3514 * Destroy all recent snapshots and its dependends.
3516 cb.cb_force = force;
3517 cb.cb_target = snap->zfs_name;
3518 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3519 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3525 * Now that we have verified that the snapshot is the latest,
3526 * rollback to the given snapshot.
3529 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3530 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3532 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3534 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3536 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3539 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3541 if (ZFS_IS_VOLUME(zhp))
3542 zc.zc_objset_type = DMU_OST_ZVOL;
3544 zc.zc_objset_type = DMU_OST_ZFS;
3547 * We rely on zfs_iter_children() to verify that there are no
3548 * newer snapshots for the given dataset. Therefore, we can
3549 * simply pass the name on to the ioctl() call. There is still
3550 * an unlikely race condition where the user has taken a
3551 * snapshot since we verified that this was the most recent.
3554 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3555 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3556 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3562 * For volumes, if the pre-rollback volsize matched the pre-
3563 * rollback reservation and the volsize has changed then set
3564 * the reservation property to the post-rollback volsize.
3565 * Make a new handle since the rollback closed the dataset.
3567 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3568 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3569 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3574 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3575 if (old_volsize != new_volsize)
3576 err = zfs_prop_set_int(zhp, resv_prop,
3585 * Iterate over all dependents for a given dataset. This includes both
3586 * hierarchical dependents (children) and data dependents (snapshots and
3587 * clones). The bulk of the processing occurs in get_dependents() in
3591 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3592 zfs_iter_f func, void *data)
3597 zfs_handle_t *child;
3600 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3601 &dependents, &count) != 0)
3604 for (i = 0; i < count; i++) {
3605 if ((child = make_dataset_handle(zhp->zfs_hdl,
3606 dependents[i])) == NULL)
3609 if ((ret = func(child, data)) != 0)
3613 for (i = 0; i < count; i++)
3614 free(dependents[i]);
3621 * Renames the given dataset.
3624 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3627 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3629 prop_changelist_t *cl = NULL;
3630 zfs_handle_t *zhrp = NULL;
3631 char *parentname = NULL;
3632 char parent[ZFS_MAXNAMELEN];
3633 libzfs_handle_t *hdl = zhp->zfs_hdl;
3636 /* if we have the same exact name, just return success */
3637 if (strcmp(zhp->zfs_name, target) == 0)
3640 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3641 "cannot rename to '%s'"), target);
3644 * Make sure the target name is valid
3646 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3647 if ((strchr(target, '@') == NULL) ||
3650 * Snapshot target name is abbreviated,
3651 * reconstruct full dataset name
3653 (void) strlcpy(parent, zhp->zfs_name,
3655 delim = strchr(parent, '@');
3656 if (strchr(target, '@') == NULL)
3660 (void) strlcat(parent, target, sizeof (parent));
3664 * Make sure we're renaming within the same dataset.
3666 delim = strchr(target, '@');
3667 if (strncmp(zhp->zfs_name, target, delim - target)
3668 != 0 || zhp->zfs_name[delim - target] != '@') {
3669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3670 "snapshots must be part of same "
3672 return (zfs_error(hdl, EZFS_CROSSTARGET,
3676 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3677 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3681 "recursive rename must be a snapshot"));
3682 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3685 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3686 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3688 /* validate parents */
3689 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3692 /* make sure we're in the same pool */
3693 verify((delim = strchr(target, '/')) != NULL);
3694 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3695 zhp->zfs_name[delim - target] != '/') {
3696 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3697 "datasets must be within same pool"));
3698 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3701 /* new name cannot be a child of the current dataset name */
3702 if (is_descendant(zhp->zfs_name, target)) {
3703 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3704 "New dataset name cannot be a descendant of "
3705 "current dataset name"));
3706 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3710 (void) snprintf(errbuf, sizeof (errbuf),
3711 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3713 if (getzoneid() == GLOBAL_ZONEID &&
3714 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3715 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3716 "dataset is used in a non-global zone"));
3717 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3721 struct destroydata dd;
3723 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3724 if (parentname == NULL) {
3728 delim = strchr(parentname, '@');
3730 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3736 dd.snapname = delim + 1;
3737 dd.gotone = B_FALSE;
3738 dd.closezhp = B_TRUE;
3740 /* We remove any zvol links prior to renaming them */
3741 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3746 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3749 if (changelist_haszonedchild(cl)) {
3750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3751 "child dataset with inherited mountpoint is used "
3752 "in a non-global zone"));
3753 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3758 if ((ret = changelist_prefix(cl)) != 0)
3762 if (ZFS_IS_VOLUME(zhp))
3763 zc.zc_objset_type = DMU_OST_ZVOL;
3765 zc.zc_objset_type = DMU_OST_ZFS;
3767 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3768 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3770 zc.zc_cookie = recursive;
3772 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3774 * if it was recursive, the one that actually failed will
3777 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3778 "cannot rename '%s'"), zc.zc_name);
3780 if (recursive && errno == EEXIST) {
3781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3782 "a child dataset already has a snapshot "
3783 "with the new name"));
3784 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3786 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3790 * On failure, we still want to remount any filesystems that
3791 * were previously mounted, so we don't alter the system state.
3794 struct createdata cd;
3796 /* only create links for datasets that had existed */
3797 cd.cd_snapname = delim + 1;
3798 cd.cd_ifexists = B_TRUE;
3799 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3802 (void) changelist_postfix(cl);
3806 struct createdata cd;
3808 /* only create links for datasets that had existed */
3809 cd.cd_snapname = strchr(target, '@') + 1;
3810 cd.cd_ifexists = B_TRUE;
3811 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3814 changelist_rename(cl, zfs_get_name(zhp), target);
3815 ret = changelist_postfix(cl);
3827 changelist_free(cl);
3833 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3834 * and wait briefly for udev to create the /dev link.
3837 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3839 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3843 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3845 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3846 char path[MAXPATHLEN];
3849 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3852 * Issue the appropriate ioctl.
3854 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3858 * Silently ignore the case where the link already
3859 * exists. This allows 'zfs volinit' to be run multiple
3860 * times without errors.
3866 * Dataset does not exist in the kernel. If we
3867 * don't care (see zfs_rename), then ignore the
3877 return (zfs_standard_error_fmt(hdl, errno,
3878 dgettext(TEXT_DOMAIN, "cannot create device links "
3879 "for '%s'"), dataset));
3884 * Wait up to 10 seconds for udev to create the device.
3886 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
3887 error = zpool_label_disk_wait(path, 10000);
3889 (void) printf(gettext("%s may not be immediately "
3890 "available\n"), path);
3896 * Remove a minor node for the given zvol and the associated /dev links.
3899 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3901 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3903 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3905 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3909 * Silently ignore the case where the link no longer
3910 * exists, so that 'zfs volfini' can be run multiple
3911 * times without errors.
3916 return (zfs_standard_error_fmt(hdl, errno,
3917 dgettext(TEXT_DOMAIN, "cannot remove device "
3918 "links for '%s'"), dataset));
3926 zfs_get_user_props(zfs_handle_t *zhp)
3928 return (zhp->zfs_user_props);
3932 * This function is used by 'zfs list' to determine the exact set of columns to
3933 * display, and their maximum widths. This does two main things:
3935 * - If this is a list of all properties, then expand the list to include
3936 * all native properties, and set a flag so that for each dataset we look
3937 * for new unique user properties and add them to the list.
3939 * - For non fixed-width properties, keep track of the maximum width seen
3940 * so that we can size the column appropriately. If the user has
3941 * requested received property values, we also need to compute the width
3942 * of the RECEIVED column.
3945 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3947 libzfs_handle_t *hdl = zhp->zfs_hdl;
3948 zprop_list_t *entry;
3949 zprop_list_t **last, **start;
3950 nvlist_t *userprops, *propval;
3953 char buf[ZFS_MAXPROPLEN];
3955 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3958 userprops = zfs_get_user_props(zhp);
3961 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3963 * Go through and add any user properties as necessary. We
3964 * start by incrementing our list pointer to the first
3965 * non-native property.
3968 while (*start != NULL) {
3969 if ((*start)->pl_prop == ZPROP_INVAL)
3971 start = &(*start)->pl_next;
3975 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3977 * See if we've already found this property in our list.
3979 for (last = start; *last != NULL;
3980 last = &(*last)->pl_next) {
3981 if (strcmp((*last)->pl_user_prop,
3982 nvpair_name(elem)) == 0)
3986 if (*last == NULL) {
3987 if ((entry = zfs_alloc(hdl,
3988 sizeof (zprop_list_t))) == NULL ||
3989 ((entry->pl_user_prop = zfs_strdup(hdl,
3990 nvpair_name(elem)))) == NULL) {
3995 entry->pl_prop = ZPROP_INVAL;
3996 entry->pl_width = strlen(nvpair_name(elem));
3997 entry->pl_all = B_TRUE;
4004 * Now go through and check the width of any non-fixed columns
4006 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4007 if (entry->pl_fixed)
4010 if (entry->pl_prop != ZPROP_INVAL) {
4011 if (zfs_prop_get(zhp, entry->pl_prop,
4012 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4013 if (strlen(buf) > entry->pl_width)
4014 entry->pl_width = strlen(buf);
4016 if (received && zfs_prop_get_recvd(zhp,
4017 zfs_prop_to_name(entry->pl_prop),
4018 buf, sizeof (buf), B_FALSE) == 0)
4019 if (strlen(buf) > entry->pl_recvd_width)
4020 entry->pl_recvd_width = strlen(buf);
4022 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4024 verify(nvlist_lookup_string(propval,
4025 ZPROP_VALUE, &strval) == 0);
4026 if (strlen(strval) > entry->pl_width)
4027 entry->pl_width = strlen(strval);
4029 if (received && zfs_prop_get_recvd(zhp,
4030 entry->pl_user_prop,
4031 buf, sizeof (buf), B_FALSE) == 0)
4032 if (strlen(buf) > entry->pl_recvd_width)
4033 entry->pl_recvd_width = strlen(buf);
4042 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4043 char *resource, void *export, void *sharetab,
4044 int sharemax, zfs_share_op_t operation)
4046 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4049 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4050 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4052 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4053 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4054 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4055 zc.zc_share.z_sharetype = operation;
4056 zc.zc_share.z_sharemax = sharemax;
4057 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4060 #endif /* HAVE_ZPL */
4063 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4068 * Keep a reference to the props-table against which we prune the
4071 zhp->zfs_props_table = props;
4073 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4076 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4077 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4080 * User properties will result in ZPROP_INVAL, and since we
4081 * only know how to prune standard ZFS properties, we always
4082 * leave these in the list. This can also happen if we
4083 * encounter an unknown DSL property (when running older
4084 * software, for example).
4086 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4087 (void) nvlist_remove(zhp->zfs_props,
4088 nvpair_name(curr), nvpair_type(curr));
4094 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4095 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4097 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4098 nvlist_t *nvlist = NULL;
4101 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4102 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4103 zc.zc_cookie = (uint64_t)cmd;
4105 if (cmd == ZFS_SMB_ACL_RENAME) {
4106 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4107 (void) no_memory(hdl);
4113 case ZFS_SMB_ACL_ADD:
4114 case ZFS_SMB_ACL_REMOVE:
4115 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4117 case ZFS_SMB_ACL_RENAME:
4118 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4120 (void) no_memory(hdl);
4123 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4125 (void) no_memory(hdl);
4128 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4129 nvlist_free(nvlist);
4133 case ZFS_SMB_ACL_PURGE:
4138 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4140 nvlist_free(nvlist);
4145 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4146 char *path, char *resource)
4148 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4153 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4154 char *path, char *resource)
4156 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4161 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4163 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4168 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4169 char *oldname, char *newname)
4171 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4176 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4177 zfs_userspace_cb_t func, void *arg)
4179 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4181 zfs_useracct_t buf[100];
4183 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4185 zc.zc_objset_type = type;
4186 zc.zc_nvlist_dst = (uintptr_t)buf;
4190 zfs_useracct_t *zua = buf;
4192 zc.zc_nvlist_dst_size = sizeof (buf);
4193 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4194 ZFS_IOC_USERSPACE_MANY, &zc);
4195 if (error || zc.zc_nvlist_dst_size == 0)
4198 while (zc.zc_nvlist_dst_size > 0) {
4199 error = func(arg, zua->zu_domain, zua->zu_rid,
4204 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4212 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4213 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4214 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4216 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4217 libzfs_handle_t *hdl = zhp->zfs_hdl;
4219 ASSERT(!recursive || dsobj == 0);
4221 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4222 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4223 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4224 >= sizeof (zc.zc_string))
4225 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4226 zc.zc_cookie = recursive;
4227 zc.zc_temphold = temphold;
4228 zc.zc_cleanup_fd = cleanup_fd;
4229 zc.zc_sendobj = dsobj;
4230 zc.zc_createtxg = createtxg;
4232 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4233 char errbuf[ZFS_MAXNAMELEN+32];
4236 * if it was recursive, the one that actually failed will be in
4239 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4240 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4244 * Temporary tags wind up having the ds object id
4245 * prepended. So even if we passed the length check
4246 * above, it's still possible for the tag to wind
4247 * up being slightly too long.
4249 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4251 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4252 "pool must be upgraded"));
4253 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4255 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4257 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4263 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4271 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4272 boolean_t recursive)
4274 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4275 libzfs_handle_t *hdl = zhp->zfs_hdl;
4277 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4278 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4279 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4280 >= sizeof (zc.zc_string))
4281 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4282 zc.zc_cookie = recursive;
4284 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4285 char errbuf[ZFS_MAXNAMELEN+32];
4288 * if it was recursive, the one that actually failed will be in
4291 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4292 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4296 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4299 "pool must be upgraded"));
4300 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4302 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4304 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4312 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4315 uint64_t nblocks, volblocksize;
4319 if (nvlist_lookup_string(props,
4320 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4321 ncopies = atoi(strval);
4324 if (nvlist_lookup_uint64(props,
4325 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4326 &volblocksize) != 0)
4327 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4328 nblocks = volsize/volblocksize;
4329 /* start with metadnode L0-L6 */
4331 /* calculate number of indirects */
4332 while (nblocks > 1) {
4333 nblocks += DNODES_PER_LEVEL - 1;
4334 nblocks /= DNODES_PER_LEVEL;
4337 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4340 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4341 * compressed, but in practice they compress down to about
4344 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;