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.
24 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
39 #include <sys/mntent.h>
40 #include <sys/mount.h>
47 #include <sys/dnode.h>
53 #include "zfs_namecheck.h"
55 #include "libzfs_impl.h"
56 #include "zfs_deleg.h"
58 static int userquota_propname_decode(const char *propname, boolean_t zoned,
59 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
62 * Given a single type (not a mask of types), return the type in a human
66 zfs_type_to_name(zfs_type_t type)
69 case ZFS_TYPE_FILESYSTEM:
70 return (dgettext(TEXT_DOMAIN, "filesystem"));
71 case ZFS_TYPE_SNAPSHOT:
72 return (dgettext(TEXT_DOMAIN, "snapshot"));
74 return (dgettext(TEXT_DOMAIN, "volume"));
81 * Given a path and mask of ZFS types, return a string describing this dataset.
82 * This is used when we fail to open a dataset and we cannot get an exact type.
83 * We guess what the type would have been based on the path and the mask of
87 path_to_str(const char *path, int types)
90 * When given a single type, always report the exact type.
92 if (types == ZFS_TYPE_SNAPSHOT)
93 return (dgettext(TEXT_DOMAIN, "snapshot"));
94 if (types == ZFS_TYPE_FILESYSTEM)
95 return (dgettext(TEXT_DOMAIN, "filesystem"));
96 if (types == ZFS_TYPE_VOLUME)
97 return (dgettext(TEXT_DOMAIN, "volume"));
100 * The user is requesting more than one type of dataset. If this is the
101 * case, consult the path itself. If we're looking for a snapshot, and
102 * a '@' is found, then report it as "snapshot". Otherwise, remove the
103 * snapshot attribute and try again.
105 if (types & ZFS_TYPE_SNAPSHOT) {
106 if (strchr(path, '@') != NULL)
107 return (dgettext(TEXT_DOMAIN, "snapshot"));
108 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
112 * The user has requested either filesystems or volumes.
113 * We have no way of knowing a priori what type this would be, so always
114 * report it as "filesystem" or "volume", our two primitive types.
116 if (types & ZFS_TYPE_FILESYSTEM)
117 return (dgettext(TEXT_DOMAIN, "filesystem"));
119 assert(types & ZFS_TYPE_VOLUME);
120 return (dgettext(TEXT_DOMAIN, "volume"));
124 * Validate a ZFS path. This is used even before trying to open the dataset, to
125 * provide a more meaningful error message. We call zfs_error_aux() to
126 * explain exactly why the name was not valid.
129 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
135 (void) zfs_prop_get_table();
136 if (dataset_namecheck(path, &why, &what) != 0) {
139 case NAME_ERR_TOOLONG:
140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141 "name is too long"));
144 case NAME_ERR_LEADING_SLASH:
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "leading slash in name"));
149 case NAME_ERR_EMPTY_COMPONENT:
150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151 "empty component in name"));
154 case NAME_ERR_TRAILING_SLASH:
155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156 "trailing slash in name"));
159 case NAME_ERR_INVALCHAR:
161 dgettext(TEXT_DOMAIN, "invalid character "
162 "'%c' in name"), what);
165 case NAME_ERR_MULTIPLE_AT:
166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
167 "multiple '@' delimiters in name"));
170 case NAME_ERR_NOLETTER:
171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172 "pool doesn't begin with a letter"));
175 case NAME_ERR_RESERVED:
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "name is reserved"));
180 case NAME_ERR_DISKLIKE:
181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
182 "reserved disk name"));
190 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193 "snapshot delimiter '@' in filesystem name"));
197 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200 "missing '@' delimiter in snapshot name"));
204 if (modifying && strchr(path, '%') != NULL) {
206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
207 "invalid character %c in name"), '%');
215 zfs_name_valid(const char *name, zfs_type_t type)
217 if (type == ZFS_TYPE_POOL)
218 return (zpool_name_valid(NULL, B_FALSE, name));
219 return (zfs_validate_name(NULL, name, type, B_FALSE));
223 * This function takes the raw DSL properties, and filters out the user-defined
224 * properties into a separate nvlist.
227 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
229 libzfs_handle_t *hdl = zhp->zfs_hdl;
234 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
235 (void) no_memory(hdl);
240 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
241 if (!zfs_prop_user(nvpair_name(elem)))
244 verify(nvpair_value_nvlist(elem, &propval) == 0);
245 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
247 (void) no_memory(hdl);
255 static zpool_handle_t *
256 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
258 libzfs_handle_t *hdl = zhp->zfs_hdl;
261 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
262 if (hdl->libzfs_pool_handles != NULL)
263 zph->zpool_next = hdl->libzfs_pool_handles;
264 hdl->libzfs_pool_handles = zph;
269 static zpool_handle_t *
270 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
272 libzfs_handle_t *hdl = zhp->zfs_hdl;
273 zpool_handle_t *zph = hdl->libzfs_pool_handles;
275 while ((zph != NULL) &&
276 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
277 zph = zph->zpool_next;
282 * Returns a handle to the pool that contains the provided dataset.
283 * If a handle to that pool already exists then that handle is returned.
284 * Otherwise, a new handle is created and added to the list of handles.
286 static zpool_handle_t *
287 zpool_handle(zfs_handle_t *zhp)
293 len = strcspn(zhp->zfs_name, "/@") + 1;
294 pool_name = zfs_alloc(zhp->zfs_hdl, len);
295 (void) strlcpy(pool_name, zhp->zfs_name, len);
297 zph = zpool_find_handle(zhp, pool_name, len);
299 zph = zpool_add_handle(zhp, pool_name);
306 zpool_free_handles(libzfs_handle_t *hdl)
308 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
310 while (zph != NULL) {
311 next = zph->zpool_next;
315 hdl->libzfs_pool_handles = NULL;
319 * Utility function to gather stats (objset and zpl) for the given object.
322 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
324 libzfs_handle_t *hdl = zhp->zfs_hdl;
326 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
328 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
329 if (errno == ENOMEM) {
330 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
341 * Utility function to get the received properties of the given object.
344 get_recvd_props_ioctl(zfs_handle_t *zhp)
346 libzfs_handle_t *hdl = zhp->zfs_hdl;
347 nvlist_t *recvdprops;
348 zfs_cmd_t zc = { 0 };
351 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
354 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
356 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
357 if (errno == ENOMEM) {
358 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
362 zcmd_free_nvlists(&zc);
367 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
368 zcmd_free_nvlists(&zc);
372 nvlist_free(zhp->zfs_recvd_props);
373 zhp->zfs_recvd_props = recvdprops;
379 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
381 nvlist_t *allprops, *userprops;
383 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
385 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
390 * XXX Why do we store the user props separately, in addition to
391 * storing them in zfs_props?
393 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
394 nvlist_free(allprops);
398 nvlist_free(zhp->zfs_props);
399 nvlist_free(zhp->zfs_user_props);
401 zhp->zfs_props = allprops;
402 zhp->zfs_user_props = userprops;
408 get_stats(zfs_handle_t *zhp)
411 zfs_cmd_t zc = { 0 };
413 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
415 if (get_stats_ioctl(zhp, &zc) != 0)
417 else if (put_stats_zhdl(zhp, &zc) != 0)
419 zcmd_free_nvlists(&zc);
424 * Refresh the properties currently stored in the handle.
427 zfs_refresh_properties(zfs_handle_t *zhp)
429 (void) get_stats(zhp);
433 * Makes a handle from the given dataset name. Used by zfs_open() and
434 * zfs_iter_* to create child handles on the fly.
437 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
439 if (put_stats_zhdl(zhp, zc) != 0)
443 * We've managed to open the dataset and gather statistics. Determine
444 * the high-level type.
446 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
447 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
448 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
449 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
453 if (zhp->zfs_dmustats.dds_is_snapshot)
454 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
455 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
456 zhp->zfs_type = ZFS_TYPE_VOLUME;
457 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
458 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
460 abort(); /* we should never see any other types */
462 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
469 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
471 zfs_cmd_t zc = { 0 };
473 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
479 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
480 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
484 if (get_stats_ioctl(zhp, &zc) == -1) {
485 zcmd_free_nvlists(&zc);
489 if (make_dataset_handle_common(zhp, &zc) == -1) {
493 zcmd_free_nvlists(&zc);
497 static zfs_handle_t *
498 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
500 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
506 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
507 if (make_dataset_handle_common(zhp, zc) == -1) {
515 * Opens the given snapshot, filesystem, or volume. The 'types'
516 * argument is a mask of acceptable types. The function will print an
517 * appropriate error message and return NULL if it can't be opened.
520 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
525 (void) snprintf(errbuf, sizeof (errbuf),
526 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
529 * Validate the name before we even try to open it.
531 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533 "invalid dataset name"));
534 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
539 * Try to get stats for the dataset, which will tell us if it exists.
542 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
543 (void) zfs_standard_error(hdl, errno, errbuf);
547 if (!(types & zhp->zfs_type)) {
548 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
557 * Release a ZFS handle. Nothing to do but free the associated memory.
560 zfs_close(zfs_handle_t *zhp)
562 if (zhp->zfs_mntopts)
563 free(zhp->zfs_mntopts);
564 nvlist_free(zhp->zfs_props);
565 nvlist_free(zhp->zfs_user_props);
566 nvlist_free(zhp->zfs_recvd_props);
570 typedef struct mnttab_node {
571 struct mnttab mtn_mt;
576 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
578 const mnttab_node_t *mtn1 = arg1;
579 const mnttab_node_t *mtn2 = arg2;
582 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
586 return (rv > 0 ? 1 : -1);
590 libzfs_mnttab_init(libzfs_handle_t *hdl)
592 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
593 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
594 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
598 libzfs_mnttab_update(libzfs_handle_t *hdl)
602 rewind(hdl->libzfs_mnttab);
603 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
606 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
608 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
609 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
610 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
611 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
612 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
613 avl_add(&hdl->libzfs_mnttab_cache, mtn);
618 libzfs_mnttab_fini(libzfs_handle_t *hdl)
623 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
624 free(mtn->mtn_mt.mnt_special);
625 free(mtn->mtn_mt.mnt_mountp);
626 free(mtn->mtn_mt.mnt_fstype);
627 free(mtn->mtn_mt.mnt_mntopts);
630 avl_destroy(&hdl->libzfs_mnttab_cache);
634 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
636 hdl->libzfs_mnttab_enable = enable;
640 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
641 struct mnttab *entry)
646 if (!hdl->libzfs_mnttab_enable) {
647 struct mnttab srch = { 0 };
649 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
650 libzfs_mnttab_fini(hdl);
651 rewind(hdl->libzfs_mnttab);
652 srch.mnt_special = (char *)fsname;
653 srch.mnt_fstype = MNTTYPE_ZFS;
654 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
660 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
661 libzfs_mnttab_update(hdl);
663 find.mtn_mt.mnt_special = (char *)fsname;
664 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
666 *entry = mtn->mtn_mt;
673 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
674 const char *mountp, const char *mntopts)
678 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
680 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
681 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
682 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
683 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
684 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
685 avl_add(&hdl->libzfs_mnttab_cache, mtn);
689 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
694 find.mtn_mt.mnt_special = (char *)fsname;
695 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
696 avl_remove(&hdl->libzfs_mnttab_cache, ret);
697 free(ret->mtn_mt.mnt_special);
698 free(ret->mtn_mt.mnt_mountp);
699 free(ret->mtn_mt.mnt_fstype);
700 free(ret->mtn_mt.mnt_mntopts);
706 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
708 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
710 if (zpool_handle == NULL)
713 *spa_version = zpool_get_prop_int(zpool_handle,
714 ZPOOL_PROP_VERSION, NULL);
719 * The choice of reservation property depends on the SPA version.
722 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
726 if (zfs_spa_version(zhp, &spa_version) < 0)
729 if (spa_version >= SPA_VERSION_REFRESERVATION)
730 *resv_prop = ZFS_PROP_REFRESERVATION;
732 *resv_prop = ZFS_PROP_RESERVATION;
738 * Given an nvlist of properties to set, validates that they are correct, and
739 * parses any numeric properties (index, boolean, etc) if they are specified as
743 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
744 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
751 int chosen_normal = -1;
754 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
755 (void) no_memory(hdl);
760 * Make sure this property is valid and applies to this type.
764 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
765 const char *propname = nvpair_name(elem);
767 prop = zfs_name_to_prop(propname);
768 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
770 * This is a user property: make sure it's a
771 * string, and that it's less than ZAP_MAXNAMELEN.
773 if (nvpair_type(elem) != DATA_TYPE_STRING) {
774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
775 "'%s' must be a string"), propname);
776 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
780 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
782 "property name '%s' is too long"),
784 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
788 (void) nvpair_value_string(elem, &strval);
789 if (nvlist_add_string(ret, propname, strval) != 0) {
790 (void) no_memory(hdl);
797 * Currently, only user properties can be modified on
800 if (type == ZFS_TYPE_SNAPSHOT) {
801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
802 "this property can not be modified for snapshots"));
803 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
807 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
808 zfs_userquota_prop_t uqtype;
809 char newpropname[128];
814 if (userquota_propname_decode(propname, zoned,
815 &uqtype, domain, sizeof (domain), &rid) != 0) {
817 dgettext(TEXT_DOMAIN,
818 "'%s' has an invalid user/group name"),
820 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
824 if (uqtype != ZFS_PROP_USERQUOTA &&
825 uqtype != ZFS_PROP_GROUPQUOTA) {
827 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
829 (void) zfs_error(hdl, EZFS_PROPREADONLY,
834 if (nvpair_type(elem) == DATA_TYPE_STRING) {
835 (void) nvpair_value_string(elem, &strval);
836 if (strcmp(strval, "none") == 0) {
838 } else if (zfs_nicestrtonum(hdl,
839 strval, &intval) != 0) {
840 (void) zfs_error(hdl,
841 EZFS_BADPROP, errbuf);
844 } else if (nvpair_type(elem) ==
846 (void) nvpair_value_uint64(elem, &intval);
848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
849 "use 'none' to disable "
850 "userquota/groupquota"));
854 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
855 "'%s' must be a number"), propname);
856 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
861 * Encode the prop name as
862 * userquota@<hex-rid>-domain, to make it easy
863 * for the kernel to decode.
865 (void) snprintf(newpropname, sizeof (newpropname),
866 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
867 (longlong_t)rid, domain);
871 if (nvlist_add_uint64_array(ret, newpropname,
873 (void) no_memory(hdl);
879 if (prop == ZPROP_INVAL) {
880 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
881 "invalid property '%s'"), propname);
882 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
886 if (!zfs_prop_valid_for_type(prop, type)) {
888 dgettext(TEXT_DOMAIN, "'%s' does not "
889 "apply to datasets of this type"), propname);
890 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
894 if (zfs_prop_readonly(prop) &&
895 (!zfs_prop_setonce(prop) || zhp != NULL)) {
897 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
899 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
903 if (zprop_parse_value(hdl, elem, prop, type, ret,
904 &strval, &intval, errbuf) != 0)
908 * Perform some additional checks for specific properties.
911 case ZFS_PROP_VERSION:
917 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
918 if (intval < version) {
919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
920 "Can not downgrade; already at version %u"),
922 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
928 case ZFS_PROP_RECORDSIZE:
929 case ZFS_PROP_VOLBLOCKSIZE:
930 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
931 if (intval < SPA_MINBLOCKSIZE ||
932 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
933 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
934 "'%s' must be power of 2 from %u "
936 (uint_t)SPA_MINBLOCKSIZE,
937 (uint_t)SPA_MAXBLOCKSIZE >> 10);
938 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
943 case ZFS_PROP_MLSLABEL:
947 * Verify the mlslabel string and convert to
948 * internal hex label string.
952 char *hex = NULL; /* internal label string */
954 /* Default value is already OK. */
955 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
958 /* Verify the label can be converted to binary form */
959 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
960 (str_to_label(strval, &new_sl, MAC_LABEL,
961 L_NO_CORRECTION, NULL) == -1)) {
965 /* Now translate to hex internal label string */
966 if (label_to_str(new_sl, &hex, M_INTERNAL,
972 m_label_free(new_sl);
974 /* If string is already in internal form, we're done. */
975 if (strcmp(strval, hex) == 0) {
980 /* Replace the label string with the internal form. */
981 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
983 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
991 "invalid mlslabel '%s'"), strval);
992 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
993 m_label_free(new_sl); /* OK if null */
995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
996 "mlslabel is not supported on FreeBSD"));
997 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1003 case ZFS_PROP_MOUNTPOINT:
1005 namecheck_err_t why;
1007 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1008 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1011 if (mountpoint_namecheck(strval, &why)) {
1013 case NAME_ERR_LEADING_SLASH:
1015 dgettext(TEXT_DOMAIN,
1016 "'%s' must be an absolute path, "
1017 "'none', or 'legacy'"), propname);
1019 case NAME_ERR_TOOLONG:
1021 dgettext(TEXT_DOMAIN,
1022 "component of '%s' is too long"),
1026 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1033 case ZFS_PROP_SHARESMB:
1034 case ZFS_PROP_SHARENFS:
1036 * For the mountpoint and sharenfs or sharesmb
1037 * properties, check if it can be set in a
1038 * global/non-global zone based on
1039 * the zoned property value:
1041 * global zone non-global zone
1042 * --------------------------------------------------
1043 * zoned=on mountpoint (no) mountpoint (yes)
1044 * sharenfs (no) sharenfs (no)
1045 * sharesmb (no) sharesmb (no)
1047 * zoned=off mountpoint (yes) N/A
1052 if (getzoneid() == GLOBAL_ZONEID) {
1053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1054 "'%s' cannot be set on "
1055 "dataset in a non-global zone"),
1057 (void) zfs_error(hdl, EZFS_ZONED,
1060 } else if (prop == ZFS_PROP_SHARENFS ||
1061 prop == ZFS_PROP_SHARESMB) {
1062 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1063 "'%s' cannot be set in "
1064 "a non-global zone"), propname);
1065 (void) zfs_error(hdl, EZFS_ZONED,
1069 } else if (getzoneid() != GLOBAL_ZONEID) {
1071 * If zoned property is 'off', this must be in
1072 * a global zone. If not, something is wrong.
1074 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1075 "'%s' cannot be set while dataset "
1076 "'zoned' property is set"), propname);
1077 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1082 * At this point, it is legitimate to set the
1083 * property. Now we want to make sure that the
1084 * property value is valid if it is sharenfs.
1086 if ((prop == ZFS_PROP_SHARENFS ||
1087 prop == ZFS_PROP_SHARESMB) &&
1088 strcmp(strval, "on") != 0 &&
1089 strcmp(strval, "off") != 0) {
1090 zfs_share_proto_t proto;
1092 if (prop == ZFS_PROP_SHARESMB)
1098 * Must be an valid sharing protocol
1099 * option string so init the libshare
1100 * in order to enable the parser and
1101 * then parse the options. We use the
1102 * control API since we don't care about
1103 * the current configuration and don't
1104 * want the overhead of loading it
1105 * until we actually do something.
1108 if (zfs_init_libshare(hdl,
1109 SA_INIT_CONTROL_API) != SA_OK) {
1111 * An error occurred so we can't do
1114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1115 "'%s' cannot be set: problem "
1116 "in share initialization"),
1118 (void) zfs_error(hdl, EZFS_BADPROP,
1123 if (zfs_parse_options(strval, proto) != SA_OK) {
1125 * There was an error in parsing so
1126 * deal with it by issuing an error
1127 * message and leaving after
1128 * uninitializing the the libshare
1131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1132 "'%s' cannot be set to invalid "
1133 "options"), propname);
1134 (void) zfs_error(hdl, EZFS_BADPROP,
1136 zfs_uninit_libshare(hdl);
1139 zfs_uninit_libshare(hdl);
1143 case ZFS_PROP_UTF8ONLY:
1144 chosen_utf = (int)intval;
1146 case ZFS_PROP_NORMALIZE:
1147 chosen_normal = (int)intval;
1152 * For changes to existing volumes, we have some additional
1153 * checks to enforce.
1155 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1156 uint64_t volsize = zfs_prop_get_int(zhp,
1158 uint64_t blocksize = zfs_prop_get_int(zhp,
1159 ZFS_PROP_VOLBLOCKSIZE);
1163 case ZFS_PROP_RESERVATION:
1164 case ZFS_PROP_REFRESERVATION:
1165 if (intval > volsize) {
1166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1167 "'%s' is greater than current "
1168 "volume size"), propname);
1169 (void) zfs_error(hdl, EZFS_BADPROP,
1175 case ZFS_PROP_VOLSIZE:
1176 if (intval % blocksize != 0) {
1177 zfs_nicenum(blocksize, buf,
1179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1180 "'%s' must be a multiple of "
1181 "volume block size (%s)"),
1183 (void) zfs_error(hdl, EZFS_BADPROP,
1189 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1190 "'%s' cannot be zero"),
1192 (void) zfs_error(hdl, EZFS_BADPROP,
1202 * If normalization was chosen, but no UTF8 choice was made,
1203 * enforce rejection of non-UTF8 names.
1205 * If normalization was chosen, but rejecting non-UTF8 names
1206 * was explicitly not chosen, it is an error.
1208 if (chosen_normal > 0 && chosen_utf < 0) {
1209 if (nvlist_add_uint64(ret,
1210 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1211 (void) no_memory(hdl);
1214 } else if (chosen_normal > 0 && chosen_utf == 0) {
1215 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1216 "'%s' must be set 'on' if normalization chosen"),
1217 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1218 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1229 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1231 uint64_t old_volsize;
1232 uint64_t new_volsize;
1233 uint64_t old_reservation;
1234 uint64_t new_reservation;
1235 zfs_prop_t resv_prop;
1238 * If this is an existing volume, and someone is setting the volsize,
1239 * make sure that it matches the reservation, or add it if necessary.
1241 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1242 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1244 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1245 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1246 old_reservation) || nvlist_lookup_uint64(nvl,
1247 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1250 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1253 new_reservation = zvol_volsize_to_reservation(new_volsize,
1255 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1256 new_reservation) != 0) {
1257 (void) no_memory(zhp->zfs_hdl);
1264 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1271 * For quotas and reservations, ENOSPC indicates
1272 * something different; setting a quota or reservation
1273 * doesn't use any disk space.
1276 case ZFS_PROP_QUOTA:
1277 case ZFS_PROP_REFQUOTA:
1278 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1279 "size is less than current used or "
1281 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1284 case ZFS_PROP_RESERVATION:
1285 case ZFS_PROP_REFRESERVATION:
1286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1287 "size is greater than available space"));
1288 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1292 (void) zfs_standard_error(hdl, err, errbuf);
1298 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1302 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1306 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1307 "pool and or dataset must be upgraded to set this "
1308 "property or value"));
1309 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1313 if (prop == ZFS_PROP_COMPRESSION) {
1314 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1315 "property setting is not allowed on "
1316 "bootable datasets"));
1317 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1319 (void) zfs_standard_error(hdl, err, errbuf);
1324 if (prop == ZPROP_INVAL) {
1325 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1327 (void) zfs_standard_error(hdl, err, errbuf);
1333 * This platform can't address a volume this big.
1336 if (prop == ZFS_PROP_VOLSIZE) {
1337 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1343 (void) zfs_standard_error(hdl, err, errbuf);
1348 * Given a property name and value, set the property for the given dataset.
1351 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1353 zfs_cmd_t zc = { 0 };
1355 prop_changelist_t *cl = NULL;
1357 libzfs_handle_t *hdl = zhp->zfs_hdl;
1358 nvlist_t *nvl = NULL, *realprops;
1360 boolean_t do_prefix;
1364 (void) snprintf(errbuf, sizeof (errbuf),
1365 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1368 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1369 nvlist_add_string(nvl, propname, propval) != 0) {
1370 (void) no_memory(hdl);
1374 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1375 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1381 prop = zfs_name_to_prop(propname);
1383 /* We don't support those properties on FreeBSD. */
1385 case ZFS_PROP_DEVICES:
1386 case ZFS_PROP_ISCSIOPTIONS:
1387 case ZFS_PROP_XATTR:
1388 case ZFS_PROP_VSCAN:
1389 case ZFS_PROP_NBMAND:
1390 case ZFS_PROP_MLSLABEL:
1391 (void) snprintf(errbuf, sizeof (errbuf),
1392 "property '%s' not supported on FreeBSD", propname);
1393 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1397 if (prop == ZFS_PROP_VOLSIZE) {
1398 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1402 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1405 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1407 "child dataset with inherited mountpoint is used "
1408 "in a non-global zone"));
1409 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1414 * If the dataset's canmount property is being set to noauto,
1415 * then we want to prevent unmounting & remounting it.
1417 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1418 (zprop_string_to_index(prop, propval, &idx,
1419 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1421 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1425 * Execute the corresponding ioctl() to set this property.
1427 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1429 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1432 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1435 zfs_setprop_error(hdl, prop, errno, errbuf);
1436 if (added_resv && errno == ENOSPC) {
1437 /* clean up the volsize property we tried to set */
1438 uint64_t old_volsize = zfs_prop_get_int(zhp,
1441 zcmd_free_nvlists(&zc);
1442 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1444 if (nvlist_add_uint64(nvl,
1445 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1448 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1450 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1454 ret = changelist_postfix(cl);
1457 * Refresh the statistics so the new property value
1461 (void) get_stats(zhp);
1466 zcmd_free_nvlists(&zc);
1468 changelist_free(cl);
1473 * Given a property, inherit the value from the parent dataset, or if received
1474 * is TRUE, revert to the received value, if any.
1477 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1479 zfs_cmd_t zc = { 0 };
1481 prop_changelist_t *cl;
1482 libzfs_handle_t *hdl = zhp->zfs_hdl;
1486 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1487 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1489 zc.zc_cookie = received;
1490 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1492 * For user properties, the amount of work we have to do is very
1493 * small, so just do it here.
1495 if (!zfs_prop_user(propname)) {
1496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1497 "invalid property"));
1498 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1501 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1502 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1504 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1505 return (zfs_standard_error(hdl, errno, errbuf));
1511 * Verify that this property is inheritable.
1513 if (zfs_prop_readonly(prop))
1514 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1516 if (!zfs_prop_inheritable(prop) && !received)
1517 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1520 * Check to see if the value applies to this type
1522 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1523 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1526 * Normalize the name, to get rid of shorthand abbreviations.
1528 propname = zfs_prop_to_name(prop);
1529 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1530 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1532 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1533 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1535 "dataset is used in a non-global zone"));
1536 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1540 * Determine datasets which will be affected by this change, if any.
1542 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1545 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1547 "child dataset with inherited mountpoint is used "
1548 "in a non-global zone"));
1549 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1553 if ((ret = changelist_prefix(cl)) != 0)
1556 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1557 return (zfs_standard_error(hdl, errno, errbuf));
1560 if ((ret = changelist_postfix(cl)) != 0)
1564 * Refresh the statistics so the new property is reflected.
1566 (void) get_stats(zhp);
1570 changelist_free(cl);
1575 * True DSL properties are stored in an nvlist. The following two functions
1576 * extract them appropriately.
1579 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1585 if (nvlist_lookup_nvlist(zhp->zfs_props,
1586 zfs_prop_to_name(prop), &nv) == 0) {
1587 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1588 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1590 verify(!zhp->zfs_props_table ||
1591 zhp->zfs_props_table[prop] == B_TRUE);
1592 value = zfs_prop_default_numeric(prop);
1600 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1606 if (nvlist_lookup_nvlist(zhp->zfs_props,
1607 zfs_prop_to_name(prop), &nv) == 0) {
1608 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1609 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1611 verify(!zhp->zfs_props_table ||
1612 zhp->zfs_props_table[prop] == B_TRUE);
1613 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1622 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1624 return (zhp->zfs_props == zhp->zfs_recvd_props);
1628 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1630 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1631 zhp->zfs_props = zhp->zfs_recvd_props;
1635 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1637 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1642 * Internal function for getting a numeric property. Both zfs_prop_get() and
1643 * zfs_prop_get_int() are built using this interface.
1645 * Certain properties can be overridden using 'mount -o'. In this case, scan
1646 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1647 * If they differ from the on-disk values, report the current values and mark
1648 * the source "temporary".
1651 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1652 char **source, uint64_t *val)
1654 zfs_cmd_t zc = { 0 };
1655 nvlist_t *zplprops = NULL;
1657 char *mntopt_on = NULL;
1658 char *mntopt_off = NULL;
1659 boolean_t received = zfs_is_recvd_props_mode(zhp);
1664 case ZFS_PROP_ATIME:
1665 mntopt_on = MNTOPT_ATIME;
1666 mntopt_off = MNTOPT_NOATIME;
1669 case ZFS_PROP_DEVICES:
1670 mntopt_on = MNTOPT_DEVICES;
1671 mntopt_off = MNTOPT_NODEVICES;
1675 mntopt_on = MNTOPT_EXEC;
1676 mntopt_off = MNTOPT_NOEXEC;
1679 case ZFS_PROP_READONLY:
1680 mntopt_on = MNTOPT_RO;
1681 mntopt_off = MNTOPT_RW;
1684 case ZFS_PROP_SETUID:
1685 mntopt_on = MNTOPT_SETUID;
1686 mntopt_off = MNTOPT_NOSETUID;
1689 case ZFS_PROP_XATTR:
1690 mntopt_on = MNTOPT_XATTR;
1691 mntopt_off = MNTOPT_NOXATTR;
1694 case ZFS_PROP_NBMAND:
1695 mntopt_on = MNTOPT_NBMAND;
1696 mntopt_off = MNTOPT_NONBMAND;
1701 * Because looking up the mount options is potentially expensive
1702 * (iterating over all of /etc/mnttab), we defer its calculation until
1703 * we're looking up a property which requires its presence.
1705 if (!zhp->zfs_mntcheck &&
1706 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1707 libzfs_handle_t *hdl = zhp->zfs_hdl;
1708 struct mnttab entry;
1710 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1711 zhp->zfs_mntopts = zfs_strdup(hdl,
1713 if (zhp->zfs_mntopts == NULL)
1717 zhp->zfs_mntcheck = B_TRUE;
1720 if (zhp->zfs_mntopts == NULL)
1721 mnt.mnt_mntopts = "";
1723 mnt.mnt_mntopts = zhp->zfs_mntopts;
1726 case ZFS_PROP_ATIME:
1727 case ZFS_PROP_DEVICES:
1729 case ZFS_PROP_READONLY:
1730 case ZFS_PROP_SETUID:
1731 case ZFS_PROP_XATTR:
1732 case ZFS_PROP_NBMAND:
1733 *val = getprop_uint64(zhp, prop, source);
1738 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1741 *src = ZPROP_SRC_TEMPORARY;
1742 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1745 *src = ZPROP_SRC_TEMPORARY;
1749 case ZFS_PROP_CANMOUNT:
1750 case ZFS_PROP_VOLSIZE:
1751 case ZFS_PROP_QUOTA:
1752 case ZFS_PROP_REFQUOTA:
1753 case ZFS_PROP_RESERVATION:
1754 case ZFS_PROP_REFRESERVATION:
1755 *val = getprop_uint64(zhp, prop, source);
1757 if (*source == NULL) {
1758 /* not default, must be local */
1759 *source = zhp->zfs_name;
1763 case ZFS_PROP_MOUNTED:
1764 *val = (zhp->zfs_mntopts != NULL);
1767 case ZFS_PROP_NUMCLONES:
1768 *val = zhp->zfs_dmustats.dds_num_clones;
1771 case ZFS_PROP_VERSION:
1772 case ZFS_PROP_NORMALIZE:
1773 case ZFS_PROP_UTF8ONLY:
1775 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1776 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1778 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1779 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1780 zcmd_free_nvlists(&zc);
1783 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1784 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1786 zcmd_free_nvlists(&zc);
1790 nvlist_free(zplprops);
1791 zcmd_free_nvlists(&zc);
1795 switch (zfs_prop_get_type(prop)) {
1796 case PROP_TYPE_NUMBER:
1797 case PROP_TYPE_INDEX:
1798 *val = getprop_uint64(zhp, prop, source);
1800 * If we tried to use a default value for a
1801 * readonly property, it means that it was not
1804 if (zfs_prop_readonly(prop) &&
1805 *source != NULL && (*source)[0] == '\0') {
1810 case PROP_TYPE_STRING:
1812 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1813 "cannot get non-numeric property"));
1814 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1815 dgettext(TEXT_DOMAIN, "internal error")));
1823 * Calculate the source type, given the raw source string.
1826 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1827 char *statbuf, size_t statlen)
1829 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1832 if (source == NULL) {
1833 *srctype = ZPROP_SRC_NONE;
1834 } else if (source[0] == '\0') {
1835 *srctype = ZPROP_SRC_DEFAULT;
1836 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1837 *srctype = ZPROP_SRC_RECEIVED;
1839 if (strcmp(source, zhp->zfs_name) == 0) {
1840 *srctype = ZPROP_SRC_LOCAL;
1842 (void) strlcpy(statbuf, source, statlen);
1843 *srctype = ZPROP_SRC_INHERITED;
1850 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1851 size_t proplen, boolean_t literal)
1856 if (zhp->zfs_recvd_props == NULL)
1857 if (get_recvd_props_ioctl(zhp) != 0)
1860 prop = zfs_name_to_prop(propname);
1862 if (prop != ZPROP_INVAL) {
1864 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1866 zfs_set_recvd_props_mode(zhp, &cookie);
1867 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1868 NULL, NULL, 0, literal);
1869 zfs_unset_recvd_props_mode(zhp, &cookie);
1870 } else if (zfs_prop_userquota(propname)) {
1875 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1876 propname, &propval) != 0)
1878 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1880 (void) strlcpy(propbuf, recvdval, proplen);
1883 return (err == 0 ? 0 : -1);
1887 * Retrieve a property from the given object. If 'literal' is specified, then
1888 * numbers are left as exact values. Otherwise, numbers are converted to a
1889 * human-readable form.
1891 * Returns 0 on success, or -1 on error.
1894 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1895 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1897 char *source = NULL;
1901 boolean_t received = zfs_is_recvd_props_mode(zhp);
1904 * Check to see if this property applies to our object
1906 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1909 if (received && zfs_prop_readonly(prop))
1913 *src = ZPROP_SRC_NONE;
1916 case ZFS_PROP_CREATION:
1918 * 'creation' is a time_t stored in the statistics. We convert
1919 * this into a string unless 'literal' is specified.
1922 val = getprop_uint64(zhp, prop, &source);
1923 time_t time = (time_t)val;
1927 localtime_r(&time, &t) == NULL ||
1928 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1930 (void) snprintf(propbuf, proplen, "%llu", val);
1934 case ZFS_PROP_MOUNTPOINT:
1936 * Getting the precise mountpoint can be tricky.
1938 * - for 'none' or 'legacy', return those values.
1939 * - for inherited mountpoints, we want to take everything
1940 * after our ancestor and append it to the inherited value.
1942 * If the pool has an alternate root, we want to prepend that
1943 * root to any values we return.
1946 str = getprop_string(zhp, prop, &source);
1948 if (str[0] == '/') {
1949 char buf[MAXPATHLEN];
1951 const char *relpath;
1954 * If we inherit the mountpoint, even from a dataset
1955 * with a received value, the source will be the path of
1956 * the dataset we inherit from. If source is
1957 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1960 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1963 relpath = zhp->zfs_name + strlen(source);
1964 if (relpath[0] == '/')
1968 if ((zpool_get_prop(zhp->zpool_hdl,
1969 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1970 (strcmp(root, "-") == 0))
1973 * Special case an alternate root of '/'. This will
1974 * avoid having multiple leading slashes in the
1977 if (strcmp(root, "/") == 0)
1981 * If the mountpoint is '/' then skip over this
1982 * if we are obtaining either an alternate root or
1983 * an inherited mountpoint.
1985 if (str[1] == '\0' && (root[0] != '\0' ||
1986 relpath[0] != '\0'))
1989 if (relpath[0] == '\0')
1990 (void) snprintf(propbuf, proplen, "%s%s",
1993 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1994 root, str, relpath[0] == '@' ? "" : "/",
1997 /* 'legacy' or 'none' */
1998 (void) strlcpy(propbuf, str, proplen);
2003 case ZFS_PROP_ORIGIN:
2004 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2007 * If there is no parent at all, return failure to indicate that
2008 * it doesn't apply to this dataset.
2010 if (propbuf[0] == '\0')
2014 case ZFS_PROP_QUOTA:
2015 case ZFS_PROP_REFQUOTA:
2016 case ZFS_PROP_RESERVATION:
2017 case ZFS_PROP_REFRESERVATION:
2019 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2023 * If quota or reservation is 0, we translate this into 'none'
2024 * (unless literal is set), and indicate that it's the default
2025 * value. Otherwise, we print the number nicely and indicate
2026 * that its set locally.
2030 (void) strlcpy(propbuf, "0", proplen);
2032 (void) strlcpy(propbuf, "none", proplen);
2035 (void) snprintf(propbuf, proplen, "%llu",
2038 zfs_nicenum(val, propbuf, proplen);
2042 case ZFS_PROP_REFRATIO:
2043 case ZFS_PROP_COMPRESSRATIO:
2044 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2046 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2047 (u_longlong_t)(val / 100),
2048 (u_longlong_t)(val % 100));
2052 switch (zhp->zfs_type) {
2053 case ZFS_TYPE_FILESYSTEM:
2056 case ZFS_TYPE_VOLUME:
2059 case ZFS_TYPE_SNAPSHOT:
2065 (void) snprintf(propbuf, proplen, "%s", str);
2068 case ZFS_PROP_MOUNTED:
2070 * The 'mounted' property is a pseudo-property that described
2071 * whether the filesystem is currently mounted. Even though
2072 * it's a boolean value, the typical values of "on" and "off"
2073 * don't make sense, so we translate to "yes" and "no".
2075 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2076 src, &source, &val) != 0)
2079 (void) strlcpy(propbuf, "yes", proplen);
2081 (void) strlcpy(propbuf, "no", proplen);
2086 * The 'name' property is a pseudo-property derived from the
2087 * dataset name. It is presented as a real property to simplify
2090 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2093 case ZFS_PROP_MLSLABEL:
2096 m_label_t *new_sl = NULL;
2097 char *ascii = NULL; /* human readable label */
2099 (void) strlcpy(propbuf,
2100 getprop_string(zhp, prop, &source), proplen);
2102 if (literal || (strcasecmp(propbuf,
2103 ZFS_MLSLABEL_DEFAULT) == 0))
2107 * Try to translate the internal hex string to
2108 * human-readable output. If there are any
2109 * problems just use the hex string.
2112 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2113 L_NO_CORRECTION, NULL) == -1) {
2114 m_label_free(new_sl);
2118 if (label_to_str(new_sl, &ascii, M_LABEL,
2122 m_label_free(new_sl);
2125 m_label_free(new_sl);
2127 (void) strlcpy(propbuf, ascii, proplen);
2136 switch (zfs_prop_get_type(prop)) {
2137 case PROP_TYPE_NUMBER:
2138 if (get_numeric_property(zhp, prop, src,
2139 &source, &val) != 0)
2142 (void) snprintf(propbuf, proplen, "%llu",
2145 zfs_nicenum(val, propbuf, proplen);
2148 case PROP_TYPE_STRING:
2149 (void) strlcpy(propbuf,
2150 getprop_string(zhp, prop, &source), proplen);
2153 case PROP_TYPE_INDEX:
2154 if (get_numeric_property(zhp, prop, src,
2155 &source, &val) != 0)
2157 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2159 (void) strlcpy(propbuf, strval, proplen);
2167 get_source(zhp, src, source, statbuf, statlen);
2173 * Utility function to get the given numeric property. Does no validation that
2174 * the given property is the appropriate type; should only be used with
2175 * hard-coded property types.
2178 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2183 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2189 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2193 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2194 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2198 * Similar to zfs_prop_get(), but returns the value as an integer.
2201 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2202 zprop_source_t *src, char *statbuf, size_t statlen)
2207 * Check to see if this property applies to our object
2209 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2210 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2211 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2212 zfs_prop_to_name(prop)));
2216 *src = ZPROP_SRC_NONE;
2218 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2221 get_source(zhp, src, source, statbuf, statlen);
2227 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2228 char **domainp, idmap_rid_t *ridp)
2231 idmap_get_handle_t *get_hdl = NULL;
2235 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2239 err = idmap_get_sidbyuid(get_hdl, id,
2240 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2242 err = idmap_get_sidbygid(get_hdl, id,
2243 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2245 if (err == IDMAP_SUCCESS &&
2246 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2247 status == IDMAP_SUCCESS)
2253 idmap_get_destroy(get_hdl);
2256 assert(!"invalid code path");
2261 * convert the propname into parameters needed by kernel
2262 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2263 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2266 userquota_propname_decode(const char *propname, boolean_t zoned,
2267 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2269 zfs_userquota_prop_t type;
2271 char *numericsid = NULL;
2276 /* Figure out the property type ({user|group}{quota|space}) */
2277 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2278 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2279 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2282 if (type == ZFS_NUM_USERQUOTA_PROPS)
2286 isuser = (type == ZFS_PROP_USERQUOTA ||
2287 type == ZFS_PROP_USERUSED);
2289 cp = strchr(propname, '@') + 1;
2291 if (strchr(cp, '@')) {
2294 * It's a SID name (eg "user@domain") that needs to be
2295 * turned into S-1-domainID-RID.
2297 directory_error_t e;
2298 if (zoned && getzoneid() == GLOBAL_ZONEID)
2301 e = directory_sid_from_user_name(NULL,
2304 e = directory_sid_from_group_name(NULL,
2308 directory_error_free(e);
2311 if (numericsid == NULL)
2314 /* will be further decoded below */
2320 if (strncmp(cp, "S-1-", 4) == 0) {
2321 /* It's a numeric SID (eg "S-1-234-567-89") */
2322 (void) strlcpy(domain, cp, domainlen);
2323 cp = strrchr(domain, '-');
2328 *ridp = strtoull(cp, &end, 10);
2333 if (errno != 0 || *end != '\0')
2335 } else if (!isdigit(*cp)) {
2337 * It's a user/group name (eg "user") that needs to be
2338 * turned into a uid/gid
2340 if (zoned && getzoneid() == GLOBAL_ZONEID)
2356 /* It's a user/group ID (eg "12345"). */
2357 uid_t id = strtoul(cp, &end, 10);
2364 /* It's an ephemeral ID. */
2365 if (idmap_id_to_numeric_domain_rid(id, isuser,
2366 &mapdomain, &rid) != 0)
2368 (void) strlcpy(domain, mapdomain, domainlen);
2375 ASSERT3P(numericsid, ==, NULL);
2380 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2381 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2384 zfs_cmd_t zc = { 0 };
2386 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2388 err = userquota_propname_decode(propname,
2389 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2390 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2391 zc.zc_objset_type = *typep;
2395 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2399 *propvalue = zc.zc_cookie;
2404 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2405 uint64_t *propvalue)
2407 zfs_userquota_prop_t type;
2409 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2414 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2415 char *propbuf, int proplen, boolean_t literal)
2419 zfs_userquota_prop_t type;
2421 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2428 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2429 } else if (propvalue == 0 &&
2430 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2431 (void) strlcpy(propbuf, "none", proplen);
2433 zfs_nicenum(propvalue, propbuf, proplen);
2439 * Returns the name of the given zfs handle.
2442 zfs_get_name(const zfs_handle_t *zhp)
2444 return (zhp->zfs_name);
2448 * Returns the type of the given zfs handle.
2451 zfs_get_type(const zfs_handle_t *zhp)
2453 return (zhp->zfs_type);
2457 zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
2460 uint64_t orig_cookie;
2462 orig_cookie = zc->zc_cookie;
2464 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2465 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2470 /* expand nvlist memory and try again */
2471 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2472 zcmd_free_nvlists(zc);
2475 zc->zc_cookie = orig_cookie;
2478 * An errno value of ESRCH indicates normal completion.
2479 * If ENOENT is returned, then the underlying dataset
2480 * has been removed since we obtained the handle.
2487 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2488 dgettext(TEXT_DOMAIN,
2489 "cannot iterate filesystems"));
2497 * Iterate over all child filesystems
2500 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2502 zfs_cmd_t zc = { 0 };
2506 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2509 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2512 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2515 * Silently ignore errors, as the only plausible explanation is
2516 * that the pool has since been removed.
2518 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2523 if ((ret = func(nzhp, data)) != 0) {
2524 zcmd_free_nvlists(&zc);
2528 zcmd_free_nvlists(&zc);
2529 return ((ret < 0) ? ret : 0);
2533 * Iterate over all snapshots
2536 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2538 zfs_cmd_t zc = { 0 };
2542 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2545 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2547 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2550 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2555 if ((ret = func(nzhp, data)) != 0) {
2556 zcmd_free_nvlists(&zc);
2560 zcmd_free_nvlists(&zc);
2561 return ((ret < 0) ? ret : 0);
2565 * Iterate over all children, snapshots and filesystems
2568 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2572 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2575 return (zfs_iter_snapshots(zhp, func, data));
2579 * Is one dataset name a child dataset of another?
2581 * Needs to handle these cases:
2582 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2583 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2584 * Descendant? No. No. No. Yes.
2587 is_descendant(const char *ds1, const char *ds2)
2589 size_t d1len = strlen(ds1);
2591 /* ds2 can't be a descendant if it's smaller */
2592 if (strlen(ds2) < d1len)
2595 /* otherwise, compare strings and verify that there's a '/' char */
2596 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2600 * Given a complete name, return just the portion that refers to the parent.
2601 * Can return NULL if this is a pool.
2604 parent_name(const char *path, char *buf, size_t buflen)
2608 if ((loc = strrchr(path, '/')) == NULL)
2611 (void) strncpy(buf, path, MIN(buflen, loc - path));
2612 buf[loc - path] = '\0';
2618 * If accept_ancestor is false, then check to make sure that the given path has
2619 * a parent, and that it exists. If accept_ancestor is true, then find the
2620 * closest existing ancestor for the given path. In prefixlen return the
2621 * length of already existing prefix of the given path. We also fetch the
2622 * 'zoned' property, which is used to validate property settings when creating
2626 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2627 boolean_t accept_ancestor, int *prefixlen)
2629 zfs_cmd_t zc = { 0 };
2630 char parent[ZFS_MAXNAMELEN];
2636 (void) snprintf(errbuf, sizeof (errbuf),
2637 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2639 /* get parent, and check to see if this is just a pool */
2640 if (parent_name(path, parent, sizeof (parent)) != 0) {
2641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2642 "missing dataset name"));
2643 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2646 /* check to see if the pool exists */
2647 if ((slash = strchr(parent, '/')) == NULL)
2648 slash = parent + strlen(parent);
2649 (void) strncpy(zc.zc_name, parent, slash - parent);
2650 zc.zc_name[slash - parent] = '\0';
2651 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2654 "no such pool '%s'"), zc.zc_name);
2655 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2658 /* check to see if the parent dataset exists */
2659 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2660 if (errno == ENOENT && accept_ancestor) {
2662 * Go deeper to find an ancestor, give up on top level.
2664 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2666 "no such pool '%s'"), zc.zc_name);
2667 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2669 } else if (errno == ENOENT) {
2670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2671 "parent does not exist"));
2672 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2674 return (zfs_standard_error(hdl, errno, errbuf));
2677 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2681 /* we are in a non-global zone, but parent is in the global zone */
2682 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2683 (void) zfs_standard_error(hdl, EPERM, errbuf);
2688 /* make sure parent is a filesystem */
2689 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2690 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2691 "parent is not a filesystem"));
2692 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2698 if (prefixlen != NULL)
2699 *prefixlen = strlen(parent);
2704 * Finds whether the dataset of the given type(s) exists.
2707 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2711 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2715 * Try to get stats for the dataset, which will tell us if it exists.
2717 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2718 int ds_type = zhp->zfs_type;
2721 if (types & ds_type)
2728 * Given a path to 'target', create all the ancestors between
2729 * the prefixlen portion of the path, and the target itself.
2730 * Fail if the initial prefixlen-ancestor does not already exist.
2733 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2739 /* make sure prefix exists */
2740 cp = target + prefixlen;
2742 assert(strchr(cp, '/') == NULL);
2743 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2746 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2754 * Attempt to create, mount, and share any ancestor filesystems,
2755 * up to the prefixlen-long one.
2757 for (cp = target + prefixlen + 1;
2758 cp = strchr(cp, '/'); *cp = '/', cp++) {
2763 h = make_dataset_handle(hdl, target);
2765 /* it already exists, nothing to do here */
2770 logstr = hdl->libzfs_log_str;
2771 hdl->libzfs_log_str = NULL;
2772 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2774 hdl->libzfs_log_str = logstr;
2775 opname = dgettext(TEXT_DOMAIN, "create");
2779 hdl->libzfs_log_str = logstr;
2780 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2782 opname = dgettext(TEXT_DOMAIN, "open");
2786 if (zfs_mount(h, NULL, 0) != 0) {
2787 opname = dgettext(TEXT_DOMAIN, "mount");
2791 if (zfs_share(h) != 0) {
2792 opname = dgettext(TEXT_DOMAIN, "share");
2802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2803 "failed to %s ancestor '%s'"), opname, target);
2808 * Creates non-existing ancestors of the given path.
2811 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2817 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2820 if ((path_copy = strdup(path)) != NULL) {
2821 rc = create_parents(hdl, path_copy, prefix);
2824 if (path_copy == NULL || rc != 0)
2831 * Create a new filesystem or volume.
2834 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2837 zfs_cmd_t zc = { 0 };
2840 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2844 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2845 "cannot create '%s'"), path);
2847 /* validate the path, taking care to note the extended error message */
2848 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2849 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2851 /* validate parents exist */
2852 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2856 * The failure modes when creating a dataset of a different type over
2857 * one that already exists is a little strange. In particular, if you
2858 * try to create a dataset on top of an existing dataset, the ioctl()
2859 * will return ENOENT, not EEXIST. To prevent this from happening, we
2860 * first try to see if the dataset exists.
2862 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2863 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2865 "dataset already exists"));
2866 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2869 if (type == ZFS_TYPE_VOLUME)
2870 zc.zc_objset_type = DMU_OST_ZVOL;
2872 zc.zc_objset_type = DMU_OST_ZFS;
2874 if (props && (props = zfs_valid_proplist(hdl, type, props,
2875 zoned, NULL, errbuf)) == 0)
2878 if (type == ZFS_TYPE_VOLUME) {
2880 * If we are creating a volume, the size and block size must
2881 * satisfy a few restraints. First, the blocksize must be a
2882 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2883 * volsize must be a multiple of the block size, and cannot be
2886 if (props == NULL || nvlist_lookup_uint64(props,
2887 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2889 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2890 "missing volume size"));
2891 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2894 if ((ret = nvlist_lookup_uint64(props,
2895 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2896 &blocksize)) != 0) {
2897 if (ret == ENOENT) {
2898 blocksize = zfs_prop_default_numeric(
2899 ZFS_PROP_VOLBLOCKSIZE);
2902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2903 "missing volume block size"));
2904 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2910 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2911 "volume size cannot be zero"));
2912 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2915 if (size % blocksize != 0) {
2917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2918 "volume size must be a multiple of volume block "
2920 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2924 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2928 /* create the dataset */
2929 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2931 zcmd_free_nvlists(&zc);
2933 /* check for failure */
2935 char parent[ZFS_MAXNAMELEN];
2936 (void) parent_name(path, parent, sizeof (parent));
2940 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2941 "no such parent '%s'"), parent);
2942 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2946 "parent '%s' is not a filesystem"), parent);
2947 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2950 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2951 "volume block size must be power of 2 from "
2953 (uint_t)SPA_MINBLOCKSIZE,
2954 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2956 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2960 "pool must be upgraded to set this "
2961 "property or value"));
2962 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2966 * This platform can't address a volume this big.
2968 if (type == ZFS_TYPE_VOLUME)
2969 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2974 return (zfs_standard_error(hdl, errno, errbuf));
2982 * Destroys the given dataset. The caller must make sure that the filesystem
2983 * isn't mounted, and that there are no active dependents.
2986 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2988 zfs_cmd_t zc = { 0 };
2990 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2992 if (ZFS_IS_VOLUME(zhp)) {
2993 zc.zc_objset_type = DMU_OST_ZVOL;
2995 zc.zc_objset_type = DMU_OST_ZFS;
2998 zc.zc_defer_destroy = defer;
2999 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3000 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3001 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3005 remove_mountpoint(zhp);
3010 struct destroydata {
3017 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3019 struct destroydata *dd = arg;
3021 char name[ZFS_MAXNAMELEN];
3022 boolean_t closezhp = dd->closezhp;
3025 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3026 (void) strlcat(name, "@", sizeof (name));
3027 (void) strlcat(name, dd->snapname, sizeof (name));
3029 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3031 dd->gotone = B_TRUE;
3035 dd->closezhp = B_TRUE;
3037 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
3044 * Destroys all snapshots with the given name in zhp & descendants.
3047 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3049 zfs_cmd_t zc = { 0 };
3051 struct destroydata dd = { 0 };
3053 dd.snapname = snapname;
3054 (void) zfs_check_snap_cb(zhp, &dd);
3057 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3058 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3059 zhp->zfs_name, snapname));
3062 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3063 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3064 zc.zc_defer_destroy = defer;
3066 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3070 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3071 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3075 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3076 "snapshot is cloned"));
3077 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3080 return (zfs_standard_error(zhp->zfs_hdl, errno,
3089 * Clones the given dataset. The target must be of the same type as the source.
3092 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3094 zfs_cmd_t zc = { 0 };
3095 char parent[ZFS_MAXNAMELEN];
3098 libzfs_handle_t *hdl = zhp->zfs_hdl;
3102 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3104 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3105 "cannot create '%s'"), target);
3107 /* validate the target name */
3108 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3109 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3111 /* validate parents exist */
3112 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3115 (void) parent_name(target, parent, sizeof (parent));
3118 if (ZFS_IS_VOLUME(zhp)) {
3119 zc.zc_objset_type = DMU_OST_ZVOL;
3120 type = ZFS_TYPE_VOLUME;
3122 zc.zc_objset_type = DMU_OST_ZFS;
3123 type = ZFS_TYPE_FILESYSTEM;
3127 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3128 zhp, errbuf)) == NULL)
3131 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3139 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3140 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3141 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3143 zcmd_free_nvlists(&zc);
3150 * The parent doesn't exist. We should have caught this
3151 * above, but there may a race condition that has since
3152 * destroyed the parent.
3154 * At this point, we don't know whether it's the source
3155 * that doesn't exist anymore, or whether the target
3156 * dataset doesn't exist.
3158 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3159 "no such parent '%s'"), parent);
3160 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3163 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3164 "source and target pools differ"));
3165 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3169 return (zfs_standard_error(zhp->zfs_hdl, errno,
3178 * Promotes the given clone fs to be the clone parent.
3181 zfs_promote(zfs_handle_t *zhp)
3183 libzfs_handle_t *hdl = zhp->zfs_hdl;
3184 zfs_cmd_t zc = { 0 };
3185 char parent[MAXPATHLEN];
3189 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3190 "cannot promote '%s'"), zhp->zfs_name);
3192 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3194 "snapshots can not be promoted"));
3195 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3198 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3199 if (parent[0] == '\0') {
3200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3201 "not a cloned filesystem"));
3202 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3205 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3206 sizeof (zc.zc_value));
3207 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3208 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3211 int save_errno = errno;
3213 switch (save_errno) {
3215 /* There is a conflicting snapshot name. */
3216 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3217 "conflicting snapshot '%s' from parent '%s'"),
3218 zc.zc_string, parent);
3219 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3222 return (zfs_standard_error(hdl, save_errno, errbuf));
3229 * Takes a snapshot of the given dataset.
3232 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3236 char parent[ZFS_MAXNAMELEN];
3238 zfs_cmd_t zc = { 0 };
3242 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3243 "cannot snapshot '%s'"), path);
3245 /* validate the target name */
3246 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3247 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3250 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3251 props, B_FALSE, NULL, errbuf)) == NULL)
3254 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3262 /* make sure the parent exists and is of the appropriate type */
3263 delim = strchr(path, '@');
3264 (void) strncpy(parent, path, delim - path);
3265 parent[delim - path] = '\0';
3267 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3268 ZFS_TYPE_VOLUME)) == NULL) {
3269 zcmd_free_nvlists(&zc);
3273 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3274 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3275 if (ZFS_IS_VOLUME(zhp))
3276 zc.zc_objset_type = DMU_OST_ZVOL;
3278 zc.zc_objset_type = DMU_OST_ZFS;
3279 zc.zc_cookie = recursive;
3280 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3282 zcmd_free_nvlists(&zc);
3285 * if it was recursive, the one that actually failed will be in
3289 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3290 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3291 (void) zfs_standard_error(hdl, errno, errbuf);
3300 * Destroy any more recent snapshots. We invoke this callback on any dependents
3301 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3302 * is a dependent and we should just destroy it without checking the transaction
3305 typedef struct rollback_data {
3306 const char *cb_target; /* the snapshot */
3307 uint64_t cb_create; /* creation time reference */
3309 boolean_t cb_dependent;
3314 rollback_destroy(zfs_handle_t *zhp, void *data)
3316 rollback_data_t *cbp = data;
3318 if (!cbp->cb_dependent) {
3319 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3320 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3321 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3325 cbp->cb_dependent = B_TRUE;
3326 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3327 rollback_destroy, cbp);
3328 cbp->cb_dependent = B_FALSE;
3330 logstr = zhp->zfs_hdl->libzfs_log_str;
3331 zhp->zfs_hdl->libzfs_log_str = NULL;
3332 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3333 zhp->zfs_hdl->libzfs_log_str = logstr;
3336 /* We must destroy this clone; first unmount it */
3337 prop_changelist_t *clp;
3339 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3340 cbp->cb_force ? MS_FORCE: 0);
3341 if (clp == NULL || changelist_prefix(clp) != 0) {
3342 cbp->cb_error = B_TRUE;
3346 if (zfs_destroy(zhp, B_FALSE) != 0)
3347 cbp->cb_error = B_TRUE;
3349 changelist_remove(clp, zhp->zfs_name);
3350 (void) changelist_postfix(clp);
3351 changelist_free(clp);
3359 * Given a dataset, rollback to a specific snapshot, discarding any
3360 * data changes since then and making it the active dataset.
3362 * Any snapshots more recent than the target are destroyed, along with
3366 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3368 rollback_data_t cb = { 0 };
3370 zfs_cmd_t zc = { 0 };
3371 boolean_t restore_resv = 0;
3372 uint64_t old_volsize, new_volsize;
3373 zfs_prop_t resv_prop;
3375 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3376 zhp->zfs_type == ZFS_TYPE_VOLUME);
3379 * Destroy all recent snapshots and its dependends.
3381 cb.cb_force = force;
3382 cb.cb_target = snap->zfs_name;
3383 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3384 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3390 * Now that we have verified that the snapshot is the latest,
3391 * rollback to the given snapshot.
3394 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3395 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3397 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3399 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3402 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3404 if (ZFS_IS_VOLUME(zhp))
3405 zc.zc_objset_type = DMU_OST_ZVOL;
3407 zc.zc_objset_type = DMU_OST_ZFS;
3410 * We rely on zfs_iter_children() to verify that there are no
3411 * newer snapshots for the given dataset. Therefore, we can
3412 * simply pass the name on to the ioctl() call. There is still
3413 * an unlikely race condition where the user has taken a
3414 * snapshot since we verified that this was the most recent.
3417 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3418 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3419 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3425 * For volumes, if the pre-rollback volsize matched the pre-
3426 * rollback reservation and the volsize has changed then set
3427 * the reservation property to the post-rollback volsize.
3428 * Make a new handle since the rollback closed the dataset.
3430 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3431 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3433 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3434 if (old_volsize != new_volsize)
3435 err = zfs_prop_set_int(zhp, resv_prop,
3444 * Iterate over all dependents for a given dataset. This includes both
3445 * hierarchical dependents (children) and data dependents (snapshots and
3446 * clones). The bulk of the processing occurs in get_dependents() in
3450 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3451 zfs_iter_f func, void *data)
3456 zfs_handle_t *child;
3459 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3460 &dependents, &count) != 0)
3463 for (i = 0; i < count; i++) {
3464 if ((child = make_dataset_handle(zhp->zfs_hdl,
3465 dependents[i])) == NULL)
3468 if ((ret = func(child, data)) != 0)
3472 for (i = 0; i < count; i++)
3473 free(dependents[i]);
3480 * Renames the given dataset.
3483 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3486 zfs_cmd_t zc = { 0 };
3488 prop_changelist_t *cl = NULL;
3489 zfs_handle_t *zhrp = NULL;
3490 char *parentname = NULL;
3491 char parent[ZFS_MAXNAMELEN];
3492 libzfs_handle_t *hdl = zhp->zfs_hdl;
3495 /* if we have the same exact name, just return success */
3496 if (strcmp(zhp->zfs_name, target) == 0)
3499 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3500 "cannot rename to '%s'"), target);
3503 * Make sure the target name is valid
3505 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3506 if ((strchr(target, '@') == NULL) ||
3509 * Snapshot target name is abbreviated,
3510 * reconstruct full dataset name
3512 (void) strlcpy(parent, zhp->zfs_name,
3514 delim = strchr(parent, '@');
3515 if (strchr(target, '@') == NULL)
3519 (void) strlcat(parent, target, sizeof (parent));
3523 * Make sure we're renaming within the same dataset.
3525 delim = strchr(target, '@');
3526 if (strncmp(zhp->zfs_name, target, delim - target)
3527 != 0 || zhp->zfs_name[delim - target] != '@') {
3528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3529 "snapshots must be part of same "
3531 return (zfs_error(hdl, EZFS_CROSSTARGET,
3535 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3536 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3540 "recursive rename must be a snapshot"));
3541 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3544 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3545 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3547 /* validate parents */
3548 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3551 /* make sure we're in the same pool */
3552 verify((delim = strchr(target, '/')) != NULL);
3553 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3554 zhp->zfs_name[delim - target] != '/') {
3555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3556 "datasets must be within same pool"));
3557 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3560 /* new name cannot be a child of the current dataset name */
3561 if (is_descendant(zhp->zfs_name, target)) {
3562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3563 "New dataset name cannot be a descendant of "
3564 "current dataset name"));
3565 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3569 (void) snprintf(errbuf, sizeof (errbuf),
3570 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3572 if (getzoneid() == GLOBAL_ZONEID &&
3573 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3574 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3575 "dataset is used in a non-global zone"));
3576 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3581 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3582 if (parentname == NULL) {
3586 delim = strchr(parentname, '@');
3588 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3595 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3598 if (changelist_haszonedchild(cl)) {
3599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3600 "child dataset with inherited mountpoint is used "
3601 "in a non-global zone"));
3602 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3606 if ((ret = changelist_prefix(cl)) != 0)
3610 if (ZFS_IS_VOLUME(zhp))
3611 zc.zc_objset_type = DMU_OST_ZVOL;
3613 zc.zc_objset_type = DMU_OST_ZFS;
3615 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3616 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3618 zc.zc_cookie = recursive;
3620 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3622 * if it was recursive, the one that actually failed will
3625 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3626 "cannot rename '%s'"), zc.zc_name);
3628 if (recursive && errno == EEXIST) {
3629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3630 "a child dataset already has a snapshot "
3631 "with the new name"));
3632 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3634 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3638 * On failure, we still want to remount any filesystems that
3639 * were previously mounted, so we don't alter the system state.
3642 (void) changelist_postfix(cl);
3645 changelist_rename(cl, zfs_get_name(zhp), target);
3646 ret = changelist_postfix(cl);
3658 changelist_free(cl);
3664 zfs_get_user_props(zfs_handle_t *zhp)
3666 return (zhp->zfs_user_props);
3670 zfs_get_recvd_props(zfs_handle_t *zhp)
3672 if (zhp->zfs_recvd_props == NULL)
3673 if (get_recvd_props_ioctl(zhp) != 0)
3675 return (zhp->zfs_recvd_props);
3679 * This function is used by 'zfs list' to determine the exact set of columns to
3680 * display, and their maximum widths. This does two main things:
3682 * - If this is a list of all properties, then expand the list to include
3683 * all native properties, and set a flag so that for each dataset we look
3684 * for new unique user properties and add them to the list.
3686 * - For non fixed-width properties, keep track of the maximum width seen
3687 * so that we can size the column appropriately. If the user has
3688 * requested received property values, we also need to compute the width
3689 * of the RECEIVED column.
3692 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3694 libzfs_handle_t *hdl = zhp->zfs_hdl;
3695 zprop_list_t *entry;
3696 zprop_list_t **last, **start;
3697 nvlist_t *userprops, *propval;
3700 char buf[ZFS_MAXPROPLEN];
3702 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3705 userprops = zfs_get_user_props(zhp);
3708 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3710 * Go through and add any user properties as necessary. We
3711 * start by incrementing our list pointer to the first
3712 * non-native property.
3715 while (*start != NULL) {
3716 if ((*start)->pl_prop == ZPROP_INVAL)
3718 start = &(*start)->pl_next;
3722 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3724 * See if we've already found this property in our list.
3726 for (last = start; *last != NULL;
3727 last = &(*last)->pl_next) {
3728 if (strcmp((*last)->pl_user_prop,
3729 nvpair_name(elem)) == 0)
3733 if (*last == NULL) {
3734 if ((entry = zfs_alloc(hdl,
3735 sizeof (zprop_list_t))) == NULL ||
3736 ((entry->pl_user_prop = zfs_strdup(hdl,
3737 nvpair_name(elem)))) == NULL) {
3742 entry->pl_prop = ZPROP_INVAL;
3743 entry->pl_width = strlen(nvpair_name(elem));
3744 entry->pl_all = B_TRUE;
3751 * Now go through and check the width of any non-fixed columns
3753 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3754 if (entry->pl_fixed)
3757 if (entry->pl_prop != ZPROP_INVAL) {
3758 if (zfs_prop_get(zhp, entry->pl_prop,
3759 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3760 if (strlen(buf) > entry->pl_width)
3761 entry->pl_width = strlen(buf);
3763 if (received && zfs_prop_get_recvd(zhp,
3764 zfs_prop_to_name(entry->pl_prop),
3765 buf, sizeof (buf), B_FALSE) == 0)
3766 if (strlen(buf) > entry->pl_recvd_width)
3767 entry->pl_recvd_width = strlen(buf);
3769 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3771 verify(nvlist_lookup_string(propval,
3772 ZPROP_VALUE, &strval) == 0);
3773 if (strlen(strval) > entry->pl_width)
3774 entry->pl_width = strlen(strval);
3776 if (received && zfs_prop_get_recvd(zhp,
3777 entry->pl_user_prop,
3778 buf, sizeof (buf), B_FALSE) == 0)
3779 if (strlen(buf) > entry->pl_recvd_width)
3780 entry->pl_recvd_width = strlen(buf);
3788 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3789 char *resource, void *export, void *sharetab,
3790 int sharemax, zfs_share_op_t operation)
3792 zfs_cmd_t zc = { 0 };
3795 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3796 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3798 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3799 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3800 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3801 zc.zc_share.z_sharetype = operation;
3802 zc.zc_share.z_sharemax = sharemax;
3803 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3808 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3813 * Keep a reference to the props-table against which we prune the
3816 zhp->zfs_props_table = props;
3818 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3821 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3822 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3825 * User properties will result in ZPROP_INVAL, and since we
3826 * only know how to prune standard ZFS properties, we always
3827 * leave these in the list. This can also happen if we
3828 * encounter an unknown DSL property (when running older
3829 * software, for example).
3831 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3832 (void) nvlist_remove(zhp->zfs_props,
3833 nvpair_name(curr), nvpair_type(curr));
3840 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3841 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3843 zfs_cmd_t zc = { 0 };
3844 nvlist_t *nvlist = NULL;
3847 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3848 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3849 zc.zc_cookie = (uint64_t)cmd;
3851 if (cmd == ZFS_SMB_ACL_RENAME) {
3852 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3853 (void) no_memory(hdl);
3859 case ZFS_SMB_ACL_ADD:
3860 case ZFS_SMB_ACL_REMOVE:
3861 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3863 case ZFS_SMB_ACL_RENAME:
3864 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3866 (void) no_memory(hdl);
3869 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3871 (void) no_memory(hdl);
3874 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3875 nvlist_free(nvlist);
3879 case ZFS_SMB_ACL_PURGE:
3884 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3886 nvlist_free(nvlist);
3891 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
3892 char *path, char *resource)
3894 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
3899 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
3900 char *path, char *resource)
3902 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
3907 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
3909 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
3914 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
3915 char *oldname, char *newname)
3917 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
3923 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
3924 zfs_userspace_cb_t func, void *arg)
3926 zfs_cmd_t zc = { 0 };
3928 zfs_useracct_t buf[100];
3930 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3932 zc.zc_objset_type = type;
3933 zc.zc_nvlist_dst = (uintptr_t)buf;
3937 zfs_useracct_t *zua = buf;
3939 zc.zc_nvlist_dst_size = sizeof (buf);
3940 error = ioctl(zhp->zfs_hdl->libzfs_fd,
3941 ZFS_IOC_USERSPACE_MANY, &zc);
3942 if (error || zc.zc_nvlist_dst_size == 0)
3945 while (zc.zc_nvlist_dst_size > 0) {
3946 error = func(arg, zua->zu_domain, zua->zu_rid,
3951 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
3959 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
3960 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
3961 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
3963 zfs_cmd_t zc = { 0 };
3964 libzfs_handle_t *hdl = zhp->zfs_hdl;
3966 ASSERT(!recursive || dsobj == 0);
3968 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3969 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3970 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3971 >= sizeof (zc.zc_string))
3972 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3973 zc.zc_cookie = recursive;
3974 zc.zc_temphold = temphold;
3975 zc.zc_cleanup_fd = cleanup_fd;
3976 zc.zc_sendobj = dsobj;
3977 zc.zc_createtxg = createtxg;
3979 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
3980 char errbuf[ZFS_MAXNAMELEN+32];
3983 * if it was recursive, the one that actually failed will be in
3986 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3987 "cannot hold '%s@%s'"), zc.zc_name, snapname);
3991 * Temporary tags wind up having the ds object id
3992 * prepended. So even if we passed the length check
3993 * above, it's still possible for the tag to wind
3994 * up being slightly too long.
3996 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
3998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3999 "pool must be upgraded"));
4000 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4002 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4004 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4010 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4018 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4019 boolean_t recursive)
4021 zfs_cmd_t zc = { 0 };
4022 libzfs_handle_t *hdl = zhp->zfs_hdl;
4024 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4025 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4026 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4027 >= sizeof (zc.zc_string))
4028 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4029 zc.zc_cookie = recursive;
4031 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4032 char errbuf[ZFS_MAXNAMELEN+32];
4035 * if it was recursive, the one that actually failed will be in
4038 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4039 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4043 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4045 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4046 "pool must be upgraded"));
4047 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4049 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4051 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4059 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4061 zfs_cmd_t zc = { 0 };
4062 libzfs_handle_t *hdl = zhp->zfs_hdl;
4066 char errbuf[ZFS_MAXNAMELEN+32];
4068 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4069 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4073 nvbuf = malloc(nvsz);
4074 if (nvbuf == NULL) {
4075 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4079 zc.zc_nvlist_dst_size = nvsz;
4080 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4082 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4084 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) {
4085 (void) snprintf(errbuf, sizeof (errbuf),
4086 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4091 nvsz = zc.zc_nvlist_dst_size;
4095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4096 "pool must be upgraded"));
4097 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4100 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4103 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4106 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4111 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4113 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4114 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4116 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4126 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4128 zfs_cmd_t zc = { 0 };
4129 libzfs_handle_t *hdl = zhp->zfs_hdl;
4131 char errbuf[ZFS_MAXNAMELEN+32];
4135 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4136 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4138 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4141 nvbuf = malloc(nvsz);
4143 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4146 zc.zc_nvlist_src_size = nvsz;
4147 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4148 zc.zc_perm_action = un;
4150 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4152 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4153 (void) snprintf(errbuf, sizeof (errbuf),
4154 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4159 "pool must be upgraded"));
4160 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4163 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4166 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4169 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4180 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4182 zfs_cmd_t zc = { 0 };
4183 libzfs_handle_t *hdl = zhp->zfs_hdl;
4187 char errbuf[ZFS_MAXNAMELEN+32];
4189 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4193 nvbuf = malloc(nvsz);
4194 if (nvbuf == NULL) {
4195 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4199 zc.zc_nvlist_dst_size = nvsz;
4200 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4202 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4204 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4205 (void) snprintf(errbuf, sizeof (errbuf),
4206 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4211 nvsz = zc.zc_nvlist_dst_size;
4215 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4216 "pool must be upgraded"));
4217 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4220 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4223 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4226 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4231 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4233 (void) snprintf(errbuf, sizeof (errbuf),
4234 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4236 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4246 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4249 uint64_t nblocks, volblocksize;
4253 if (nvlist_lookup_string(props,
4254 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4255 ncopies = atoi(strval);
4258 if (nvlist_lookup_uint64(props,
4259 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4260 &volblocksize) != 0)
4261 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4262 nblocks = volsize/volblocksize;
4263 /* start with metadnode L0-L6 */
4265 /* calculate number of indirects */
4266 while (nblocks > 1) {
4267 nblocks += DNODES_PER_LEVEL - 1;
4268 nblocks /= DNODES_PER_LEVEL;
4271 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4274 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4275 * compressed, but in practice they compress down to about
4278 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4284 * Attach/detach the given filesystem to/from the given jail.
4287 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4289 libzfs_handle_t *hdl = zhp->zfs_hdl;
4290 zfs_cmd_t zc = { 0 };
4296 (void) snprintf(errbuf, sizeof (errbuf),
4297 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4299 (void) snprintf(errbuf, sizeof (errbuf),
4300 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4303 switch (zhp->zfs_type) {
4304 case ZFS_TYPE_VOLUME:
4305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4306 "volumes can not be jailed"));
4307 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4308 case ZFS_TYPE_SNAPSHOT:
4309 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4310 "snapshots can not be jailed"));
4311 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4313 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4315 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4316 zc.zc_objset_type = DMU_OST_ZFS;
4317 zc.zc_jailid = jailid;
4319 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4320 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4321 zfs_standard_error(hdl, errno, errbuf);