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.
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
46 #include <sys/dnode.h>
52 #include "zfs_namecheck.h"
54 #include "libzfs_impl.h"
55 #include "zfs_deleg.h"
57 static int userquota_propname_decode(const char *propname, boolean_t zoned,
58 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
61 * Given a single type (not a mask of types), return the type in a human
65 zfs_type_to_name(zfs_type_t type)
68 case ZFS_TYPE_FILESYSTEM:
69 return (dgettext(TEXT_DOMAIN, "filesystem"));
70 case ZFS_TYPE_SNAPSHOT:
71 return (dgettext(TEXT_DOMAIN, "snapshot"));
73 return (dgettext(TEXT_DOMAIN, "volume"));
80 * Given a path and mask of ZFS types, return a string describing this dataset.
81 * This is used when we fail to open a dataset and we cannot get an exact type.
82 * We guess what the type would have been based on the path and the mask of
86 path_to_str(const char *path, int types)
89 * When given a single type, always report the exact type.
91 if (types == ZFS_TYPE_SNAPSHOT)
92 return (dgettext(TEXT_DOMAIN, "snapshot"));
93 if (types == ZFS_TYPE_FILESYSTEM)
94 return (dgettext(TEXT_DOMAIN, "filesystem"));
95 if (types == ZFS_TYPE_VOLUME)
96 return (dgettext(TEXT_DOMAIN, "volume"));
99 * The user is requesting more than one type of dataset. If this is the
100 * case, consult the path itself. If we're looking for a snapshot, and
101 * a '@' is found, then report it as "snapshot". Otherwise, remove the
102 * snapshot attribute and try again.
104 if (types & ZFS_TYPE_SNAPSHOT) {
105 if (strchr(path, '@') != NULL)
106 return (dgettext(TEXT_DOMAIN, "snapshot"));
107 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
111 * The user has requested either filesystems or volumes.
112 * We have no way of knowing a priori what type this would be, so always
113 * report it as "filesystem" or "volume", our two primitive types.
115 if (types & ZFS_TYPE_FILESYSTEM)
116 return (dgettext(TEXT_DOMAIN, "filesystem"));
118 assert(types & ZFS_TYPE_VOLUME);
119 return (dgettext(TEXT_DOMAIN, "volume"));
123 * Validate a ZFS path. This is used even before trying to open the dataset, to
124 * provide a more meaningful error message. We call zfs_error_aux() to
125 * explain exactly why the name was not valid.
128 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
134 (void) zfs_prop_get_table();
135 if (dataset_namecheck(path, &why, &what) != 0) {
138 case NAME_ERR_TOOLONG:
139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
140 "name is too long"));
143 case NAME_ERR_LEADING_SLASH:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "leading slash in name"));
148 case NAME_ERR_EMPTY_COMPONENT:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "empty component in name"));
153 case NAME_ERR_TRAILING_SLASH:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "trailing slash in name"));
158 case NAME_ERR_INVALCHAR:
160 dgettext(TEXT_DOMAIN, "invalid character "
161 "'%c' in name"), what);
164 case NAME_ERR_MULTIPLE_AT:
165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
166 "multiple '@' delimiters in name"));
169 case NAME_ERR_NOLETTER:
170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 "pool doesn't begin with a letter"));
174 case NAME_ERR_RESERVED:
175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176 "name is reserved"));
179 case NAME_ERR_DISKLIKE:
180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
181 "reserved disk name"));
189 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
192 "snapshot delimiter '@' in filesystem name"));
196 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
199 "missing '@' delimiter in snapshot name"));
203 if (modifying && strchr(path, '%') != NULL) {
205 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
206 "invalid character %c in name"), '%');
214 zfs_name_valid(const char *name, zfs_type_t type)
216 if (type == ZFS_TYPE_POOL)
217 return (zpool_name_valid(NULL, B_FALSE, name));
218 return (zfs_validate_name(NULL, name, type, B_FALSE));
222 * This function takes the raw DSL properties, and filters out the user-defined
223 * properties into a separate nvlist.
226 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
228 libzfs_handle_t *hdl = zhp->zfs_hdl;
233 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
234 (void) no_memory(hdl);
239 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
240 if (!zfs_prop_user(nvpair_name(elem)))
243 verify(nvpair_value_nvlist(elem, &propval) == 0);
244 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
246 (void) no_memory(hdl);
254 static zpool_handle_t *
255 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
257 libzfs_handle_t *hdl = zhp->zfs_hdl;
260 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
261 if (hdl->libzfs_pool_handles != NULL)
262 zph->zpool_next = hdl->libzfs_pool_handles;
263 hdl->libzfs_pool_handles = zph;
268 static zpool_handle_t *
269 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
271 libzfs_handle_t *hdl = zhp->zfs_hdl;
272 zpool_handle_t *zph = hdl->libzfs_pool_handles;
274 while ((zph != NULL) &&
275 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
276 zph = zph->zpool_next;
281 * Returns a handle to the pool that contains the provided dataset.
282 * If a handle to that pool already exists then that handle is returned.
283 * Otherwise, a new handle is created and added to the list of handles.
285 static zpool_handle_t *
286 zpool_handle(zfs_handle_t *zhp)
292 len = strcspn(zhp->zfs_name, "/@") + 1;
293 pool_name = zfs_alloc(zhp->zfs_hdl, len);
294 (void) strlcpy(pool_name, zhp->zfs_name, len);
296 zph = zpool_find_handle(zhp, pool_name, len);
298 zph = zpool_add_handle(zhp, pool_name);
305 zpool_free_handles(libzfs_handle_t *hdl)
307 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
309 while (zph != NULL) {
310 next = zph->zpool_next;
314 hdl->libzfs_pool_handles = NULL;
318 * Utility function to gather stats (objset and zpl) for the given object.
321 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
323 libzfs_handle_t *hdl = zhp->zfs_hdl;
325 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
327 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
328 if (errno == ENOMEM) {
329 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
340 * Utility function to get the received properties of the given object.
343 get_recvd_props_ioctl(zfs_handle_t *zhp)
345 libzfs_handle_t *hdl = zhp->zfs_hdl;
346 nvlist_t *recvdprops;
347 zfs_cmd_t zc = { 0 };
350 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
353 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
355 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
356 if (errno == ENOMEM) {
357 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
361 zcmd_free_nvlists(&zc);
366 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
367 zcmd_free_nvlists(&zc);
371 nvlist_free(zhp->zfs_recvd_props);
372 zhp->zfs_recvd_props = recvdprops;
378 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
380 nvlist_t *allprops, *userprops;
382 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
384 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
389 * XXX Why do we store the user props separately, in addition to
390 * storing them in zfs_props?
392 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
393 nvlist_free(allprops);
397 nvlist_free(zhp->zfs_props);
398 nvlist_free(zhp->zfs_user_props);
400 zhp->zfs_props = allprops;
401 zhp->zfs_user_props = userprops;
407 get_stats(zfs_handle_t *zhp)
410 zfs_cmd_t zc = { 0 };
412 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
414 if (get_stats_ioctl(zhp, &zc) != 0)
416 else if (put_stats_zhdl(zhp, &zc) != 0)
418 zcmd_free_nvlists(&zc);
423 * Refresh the properties currently stored in the handle.
426 zfs_refresh_properties(zfs_handle_t *zhp)
428 (void) get_stats(zhp);
432 * Makes a handle from the given dataset name. Used by zfs_open() and
433 * zfs_iter_* to create child handles on the fly.
436 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
438 if (put_stats_zhdl(zhp, zc) != 0)
442 * We've managed to open the dataset and gather statistics. Determine
443 * the high-level type.
445 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
446 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
447 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
448 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
452 if (zhp->zfs_dmustats.dds_is_snapshot)
453 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
454 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
455 zhp->zfs_type = ZFS_TYPE_VOLUME;
456 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
457 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
459 abort(); /* we should never see any other types */
461 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
468 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
470 zfs_cmd_t zc = { 0 };
472 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
478 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
479 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
483 if (get_stats_ioctl(zhp, &zc) == -1) {
484 zcmd_free_nvlists(&zc);
488 if (make_dataset_handle_common(zhp, &zc) == -1) {
492 zcmd_free_nvlists(&zc);
496 static zfs_handle_t *
497 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
499 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
505 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
506 if (make_dataset_handle_common(zhp, zc) == -1) {
514 * Opens the given snapshot, filesystem, or volume. The 'types'
515 * argument is a mask of acceptable types. The function will print an
516 * appropriate error message and return NULL if it can't be opened.
519 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
524 (void) snprintf(errbuf, sizeof (errbuf),
525 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
528 * Validate the name before we even try to open it.
530 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
532 "invalid dataset name"));
533 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
538 * Try to get stats for the dataset, which will tell us if it exists.
541 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
542 (void) zfs_standard_error(hdl, errno, errbuf);
546 if (!(types & zhp->zfs_type)) {
547 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
556 * Release a ZFS handle. Nothing to do but free the associated memory.
559 zfs_close(zfs_handle_t *zhp)
561 if (zhp->zfs_mntopts)
562 free(zhp->zfs_mntopts);
563 nvlist_free(zhp->zfs_props);
564 nvlist_free(zhp->zfs_user_props);
565 nvlist_free(zhp->zfs_recvd_props);
569 typedef struct mnttab_node {
570 struct mnttab mtn_mt;
575 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
577 const mnttab_node_t *mtn1 = arg1;
578 const mnttab_node_t *mtn2 = arg2;
581 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
585 return (rv > 0 ? 1 : -1);
589 libzfs_mnttab_init(libzfs_handle_t *hdl)
591 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
592 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
593 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
597 libzfs_mnttab_update(libzfs_handle_t *hdl)
601 rewind(hdl->libzfs_mnttab);
602 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
605 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
607 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
608 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
609 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
610 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
611 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
612 avl_add(&hdl->libzfs_mnttab_cache, mtn);
617 libzfs_mnttab_fini(libzfs_handle_t *hdl)
622 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
623 free(mtn->mtn_mt.mnt_special);
624 free(mtn->mtn_mt.mnt_mountp);
625 free(mtn->mtn_mt.mnt_fstype);
626 free(mtn->mtn_mt.mnt_mntopts);
629 avl_destroy(&hdl->libzfs_mnttab_cache);
633 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
635 hdl->libzfs_mnttab_enable = enable;
639 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
640 struct mnttab *entry)
645 if (!hdl->libzfs_mnttab_enable) {
646 struct mnttab srch = { 0 };
648 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
649 libzfs_mnttab_fini(hdl);
650 rewind(hdl->libzfs_mnttab);
651 srch.mnt_special = (char *)fsname;
652 srch.mnt_fstype = MNTTYPE_ZFS;
653 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
659 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
660 libzfs_mnttab_update(hdl);
662 find.mtn_mt.mnt_special = (char *)fsname;
663 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
665 *entry = mtn->mtn_mt;
672 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
673 const char *mountp, const char *mntopts)
677 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
679 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
680 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
681 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
682 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
683 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
684 avl_add(&hdl->libzfs_mnttab_cache, mtn);
688 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
693 find.mtn_mt.mnt_special = (char *)fsname;
694 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
695 avl_remove(&hdl->libzfs_mnttab_cache, ret);
696 free(ret->mtn_mt.mnt_special);
697 free(ret->mtn_mt.mnt_mountp);
698 free(ret->mtn_mt.mnt_fstype);
699 free(ret->mtn_mt.mnt_mntopts);
705 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
707 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
709 if (zpool_handle == NULL)
712 *spa_version = zpool_get_prop_int(zpool_handle,
713 ZPOOL_PROP_VERSION, NULL);
718 * The choice of reservation property depends on the SPA version.
721 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
725 if (zfs_spa_version(zhp, &spa_version) < 0)
728 if (spa_version >= SPA_VERSION_REFRESERVATION)
729 *resv_prop = ZFS_PROP_REFRESERVATION;
731 *resv_prop = ZFS_PROP_RESERVATION;
737 * Given an nvlist of properties to set, validates that they are correct, and
738 * parses any numeric properties (index, boolean, etc) if they are specified as
742 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
743 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
750 int chosen_normal = -1;
753 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
754 (void) no_memory(hdl);
759 * Make sure this property is valid and applies to this type.
763 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
764 const char *propname = nvpair_name(elem);
766 prop = zfs_name_to_prop(propname);
767 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
769 * This is a user property: make sure it's a
770 * string, and that it's less than ZAP_MAXNAMELEN.
772 if (nvpair_type(elem) != DATA_TYPE_STRING) {
773 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
774 "'%s' must be a string"), propname);
775 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
779 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
781 "property name '%s' is too long"),
783 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
787 (void) nvpair_value_string(elem, &strval);
788 if (nvlist_add_string(ret, propname, strval) != 0) {
789 (void) no_memory(hdl);
796 * Currently, only user properties can be modified on
799 if (type == ZFS_TYPE_SNAPSHOT) {
800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
801 "this property can not be modified for snapshots"));
802 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
806 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
807 zfs_userquota_prop_t uqtype;
808 char newpropname[128];
813 if (userquota_propname_decode(propname, zoned,
814 &uqtype, domain, sizeof (domain), &rid) != 0) {
816 dgettext(TEXT_DOMAIN,
817 "'%s' has an invalid user/group name"),
819 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
823 if (uqtype != ZFS_PROP_USERQUOTA &&
824 uqtype != ZFS_PROP_GROUPQUOTA) {
826 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
828 (void) zfs_error(hdl, EZFS_PROPREADONLY,
833 if (nvpair_type(elem) == DATA_TYPE_STRING) {
834 (void) nvpair_value_string(elem, &strval);
835 if (strcmp(strval, "none") == 0) {
837 } else if (zfs_nicestrtonum(hdl,
838 strval, &intval) != 0) {
839 (void) zfs_error(hdl,
840 EZFS_BADPROP, errbuf);
843 } else if (nvpair_type(elem) ==
845 (void) nvpair_value_uint64(elem, &intval);
847 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
848 "use 'none' to disable "
849 "userquota/groupquota"));
853 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
854 "'%s' must be a number"), propname);
855 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
860 * Encode the prop name as
861 * userquota@<hex-rid>-domain, to make it easy
862 * for the kernel to decode.
864 (void) snprintf(newpropname, sizeof (newpropname),
865 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
866 (longlong_t)rid, domain);
870 if (nvlist_add_uint64_array(ret, newpropname,
872 (void) no_memory(hdl);
878 if (prop == ZPROP_INVAL) {
879 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
880 "invalid property '%s'"), propname);
881 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
885 if (!zfs_prop_valid_for_type(prop, type)) {
887 dgettext(TEXT_DOMAIN, "'%s' does not "
888 "apply to datasets of this type"), propname);
889 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
893 if (zfs_prop_readonly(prop) &&
894 (!zfs_prop_setonce(prop) || zhp != NULL)) {
896 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
898 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
902 if (zprop_parse_value(hdl, elem, prop, type, ret,
903 &strval, &intval, errbuf) != 0)
907 * Perform some additional checks for specific properties.
910 case ZFS_PROP_VERSION:
916 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
917 if (intval < version) {
918 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
919 "Can not downgrade; already at version %u"),
921 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
927 case ZFS_PROP_RECORDSIZE:
928 case ZFS_PROP_VOLBLOCKSIZE:
929 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
930 if (intval < SPA_MINBLOCKSIZE ||
931 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 "'%s' must be power of 2 from %u "
935 (uint_t)SPA_MINBLOCKSIZE,
936 (uint_t)SPA_MAXBLOCKSIZE >> 10);
937 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
942 case ZFS_PROP_MLSLABEL:
946 * Verify the mlslabel string and convert to
947 * internal hex label string.
951 char *hex = NULL; /* internal label string */
953 /* Default value is already OK. */
954 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
957 /* Verify the label can be converted to binary form */
958 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
959 (str_to_label(strval, &new_sl, MAC_LABEL,
960 L_NO_CORRECTION, NULL) == -1)) {
964 /* Now translate to hex internal label string */
965 if (label_to_str(new_sl, &hex, M_INTERNAL,
971 m_label_free(new_sl);
973 /* If string is already in internal form, we're done. */
974 if (strcmp(strval, hex) == 0) {
979 /* Replace the label string with the internal form. */
980 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
982 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
989 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990 "invalid mlslabel '%s'"), strval);
991 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
992 m_label_free(new_sl); /* OK if null */
994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
995 "mlslabel is not supported on FreeBSD"));
996 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1002 case ZFS_PROP_MOUNTPOINT:
1004 namecheck_err_t why;
1006 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1007 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1010 if (mountpoint_namecheck(strval, &why)) {
1012 case NAME_ERR_LEADING_SLASH:
1014 dgettext(TEXT_DOMAIN,
1015 "'%s' must be an absolute path, "
1016 "'none', or 'legacy'"), propname);
1018 case NAME_ERR_TOOLONG:
1020 dgettext(TEXT_DOMAIN,
1021 "component of '%s' is too long"),
1025 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1032 case ZFS_PROP_SHARESMB:
1033 case ZFS_PROP_SHARENFS:
1035 * For the mountpoint and sharenfs or sharesmb
1036 * properties, check if it can be set in a
1037 * global/non-global zone based on
1038 * the zoned property value:
1040 * global zone non-global zone
1041 * --------------------------------------------------
1042 * zoned=on mountpoint (no) mountpoint (yes)
1043 * sharenfs (no) sharenfs (no)
1044 * sharesmb (no) sharesmb (no)
1046 * zoned=off mountpoint (yes) N/A
1051 if (getzoneid() == GLOBAL_ZONEID) {
1052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1053 "'%s' cannot be set on "
1054 "dataset in a non-global zone"),
1056 (void) zfs_error(hdl, EZFS_ZONED,
1059 } else if (prop == ZFS_PROP_SHARENFS ||
1060 prop == ZFS_PROP_SHARESMB) {
1061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 "'%s' cannot be set in "
1063 "a non-global zone"), propname);
1064 (void) zfs_error(hdl, EZFS_ZONED,
1068 } else if (getzoneid() != GLOBAL_ZONEID) {
1070 * If zoned property is 'off', this must be in
1071 * a global zone. If not, something is wrong.
1073 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1074 "'%s' cannot be set while dataset "
1075 "'zoned' property is set"), propname);
1076 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1081 * At this point, it is legitimate to set the
1082 * property. Now we want to make sure that the
1083 * property value is valid if it is sharenfs.
1085 if ((prop == ZFS_PROP_SHARENFS ||
1086 prop == ZFS_PROP_SHARESMB) &&
1087 strcmp(strval, "on") != 0 &&
1088 strcmp(strval, "off") != 0) {
1089 zfs_share_proto_t proto;
1091 if (prop == ZFS_PROP_SHARESMB)
1097 * Must be an valid sharing protocol
1098 * option string so init the libshare
1099 * in order to enable the parser and
1100 * then parse the options. We use the
1101 * control API since we don't care about
1102 * the current configuration and don't
1103 * want the overhead of loading it
1104 * until we actually do something.
1107 if (zfs_init_libshare(hdl,
1108 SA_INIT_CONTROL_API) != SA_OK) {
1110 * An error occurred so we can't do
1113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1114 "'%s' cannot be set: problem "
1115 "in share initialization"),
1117 (void) zfs_error(hdl, EZFS_BADPROP,
1122 if (zfs_parse_options(strval, proto) != SA_OK) {
1124 * There was an error in parsing so
1125 * deal with it by issuing an error
1126 * message and leaving after
1127 * uninitializing the the libshare
1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1131 "'%s' cannot be set to invalid "
1132 "options"), propname);
1133 (void) zfs_error(hdl, EZFS_BADPROP,
1135 zfs_uninit_libshare(hdl);
1138 zfs_uninit_libshare(hdl);
1142 case ZFS_PROP_UTF8ONLY:
1143 chosen_utf = (int)intval;
1145 case ZFS_PROP_NORMALIZE:
1146 chosen_normal = (int)intval;
1151 * For changes to existing volumes, we have some additional
1152 * checks to enforce.
1154 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1155 uint64_t volsize = zfs_prop_get_int(zhp,
1157 uint64_t blocksize = zfs_prop_get_int(zhp,
1158 ZFS_PROP_VOLBLOCKSIZE);
1162 case ZFS_PROP_RESERVATION:
1163 case ZFS_PROP_REFRESERVATION:
1164 if (intval > volsize) {
1165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1166 "'%s' is greater than current "
1167 "volume size"), propname);
1168 (void) zfs_error(hdl, EZFS_BADPROP,
1174 case ZFS_PROP_VOLSIZE:
1175 if (intval % blocksize != 0) {
1176 zfs_nicenum(blocksize, buf,
1178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1179 "'%s' must be a multiple of "
1180 "volume block size (%s)"),
1182 (void) zfs_error(hdl, EZFS_BADPROP,
1188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1189 "'%s' cannot be zero"),
1191 (void) zfs_error(hdl, EZFS_BADPROP,
1201 * If normalization was chosen, but no UTF8 choice was made,
1202 * enforce rejection of non-UTF8 names.
1204 * If normalization was chosen, but rejecting non-UTF8 names
1205 * was explicitly not chosen, it is an error.
1207 if (chosen_normal > 0 && chosen_utf < 0) {
1208 if (nvlist_add_uint64(ret,
1209 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1210 (void) no_memory(hdl);
1213 } else if (chosen_normal > 0 && chosen_utf == 0) {
1214 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1215 "'%s' must be set 'on' if normalization chosen"),
1216 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1217 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1228 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1230 uint64_t old_volsize;
1231 uint64_t new_volsize;
1232 uint64_t old_reservation;
1233 uint64_t new_reservation;
1234 zfs_prop_t resv_prop;
1237 * If this is an existing volume, and someone is setting the volsize,
1238 * make sure that it matches the reservation, or add it if necessary.
1240 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1241 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1243 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1244 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1245 old_reservation) || nvlist_lookup_uint64(nvl,
1246 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1249 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1252 new_reservation = zvol_volsize_to_reservation(new_volsize,
1254 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1255 new_reservation) != 0) {
1256 (void) no_memory(zhp->zfs_hdl);
1263 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1270 * For quotas and reservations, ENOSPC indicates
1271 * something different; setting a quota or reservation
1272 * doesn't use any disk space.
1275 case ZFS_PROP_QUOTA:
1276 case ZFS_PROP_REFQUOTA:
1277 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1278 "size is less than current used or "
1280 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1283 case ZFS_PROP_RESERVATION:
1284 case ZFS_PROP_REFRESERVATION:
1285 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1286 "size is greater than available space"));
1287 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1291 (void) zfs_standard_error(hdl, err, errbuf);
1297 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1301 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1306 "pool and or dataset must be upgraded to set this "
1307 "property or value"));
1308 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1312 if (prop == ZFS_PROP_COMPRESSION) {
1313 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1314 "property setting is not allowed on "
1315 "bootable datasets"));
1316 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1318 (void) zfs_standard_error(hdl, err, errbuf);
1323 if (prop == ZPROP_INVAL) {
1324 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1326 (void) zfs_standard_error(hdl, err, errbuf);
1332 * This platform can't address a volume this big.
1335 if (prop == ZFS_PROP_VOLSIZE) {
1336 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1342 (void) zfs_standard_error(hdl, err, errbuf);
1347 * Given a property name and value, set the property for the given dataset.
1350 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1352 zfs_cmd_t zc = { 0 };
1354 prop_changelist_t *cl = NULL;
1356 libzfs_handle_t *hdl = zhp->zfs_hdl;
1357 nvlist_t *nvl = NULL, *realprops;
1359 boolean_t do_prefix;
1363 (void) snprintf(errbuf, sizeof (errbuf),
1364 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1367 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1368 nvlist_add_string(nvl, propname, propval) != 0) {
1369 (void) no_memory(hdl);
1373 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1374 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1380 prop = zfs_name_to_prop(propname);
1382 /* We don't support those properties on FreeBSD. */
1384 case ZFS_PROP_DEVICES:
1385 case ZFS_PROP_ISCSIOPTIONS:
1386 case ZFS_PROP_XATTR:
1387 case ZFS_PROP_VSCAN:
1388 case ZFS_PROP_NBMAND:
1389 case ZFS_PROP_MLSLABEL:
1390 (void) snprintf(errbuf, sizeof (errbuf),
1391 "property '%s' not supported on FreeBSD", propname);
1392 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1396 if (prop == ZFS_PROP_VOLSIZE) {
1397 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1401 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1404 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1406 "child dataset with inherited mountpoint is used "
1407 "in a non-global zone"));
1408 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1413 * If the dataset's canmount property is being set to noauto,
1414 * then we want to prevent unmounting & remounting it.
1416 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1417 (zprop_string_to_index(prop, propval, &idx,
1418 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1420 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1424 * Execute the corresponding ioctl() to set this property.
1426 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1428 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1431 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1434 zfs_setprop_error(hdl, prop, errno, errbuf);
1435 if (added_resv && errno == ENOSPC) {
1436 /* clean up the volsize property we tried to set */
1437 uint64_t old_volsize = zfs_prop_get_int(zhp,
1440 zcmd_free_nvlists(&zc);
1441 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1443 if (nvlist_add_uint64(nvl,
1444 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1447 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1449 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1453 ret = changelist_postfix(cl);
1456 * Refresh the statistics so the new property value
1460 (void) get_stats(zhp);
1465 zcmd_free_nvlists(&zc);
1467 changelist_free(cl);
1472 * Given a property, inherit the value from the parent dataset, or if received
1473 * is TRUE, revert to the received value, if any.
1476 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1478 zfs_cmd_t zc = { 0 };
1480 prop_changelist_t *cl;
1481 libzfs_handle_t *hdl = zhp->zfs_hdl;
1485 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1486 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1488 zc.zc_cookie = received;
1489 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1491 * For user properties, the amount of work we have to do is very
1492 * small, so just do it here.
1494 if (!zfs_prop_user(propname)) {
1495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1496 "invalid property"));
1497 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1500 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1501 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1503 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1504 return (zfs_standard_error(hdl, errno, errbuf));
1510 * Verify that this property is inheritable.
1512 if (zfs_prop_readonly(prop))
1513 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1515 if (!zfs_prop_inheritable(prop) && !received)
1516 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1519 * Check to see if the value applies to this type
1521 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1522 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1525 * Normalize the name, to get rid of shorthand abbreviations.
1527 propname = zfs_prop_to_name(prop);
1528 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1529 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1531 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1532 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1534 "dataset is used in a non-global zone"));
1535 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1539 * Determine datasets which will be affected by this change, if any.
1541 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1544 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1545 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1546 "child dataset with inherited mountpoint is used "
1547 "in a non-global zone"));
1548 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1552 if ((ret = changelist_prefix(cl)) != 0)
1555 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1556 return (zfs_standard_error(hdl, errno, errbuf));
1559 if ((ret = changelist_postfix(cl)) != 0)
1563 * Refresh the statistics so the new property is reflected.
1565 (void) get_stats(zhp);
1569 changelist_free(cl);
1574 * True DSL properties are stored in an nvlist. The following two functions
1575 * extract them appropriately.
1578 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1584 if (nvlist_lookup_nvlist(zhp->zfs_props,
1585 zfs_prop_to_name(prop), &nv) == 0) {
1586 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1587 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1589 verify(!zhp->zfs_props_table ||
1590 zhp->zfs_props_table[prop] == B_TRUE);
1591 value = zfs_prop_default_numeric(prop);
1599 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1605 if (nvlist_lookup_nvlist(zhp->zfs_props,
1606 zfs_prop_to_name(prop), &nv) == 0) {
1607 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1608 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1610 verify(!zhp->zfs_props_table ||
1611 zhp->zfs_props_table[prop] == B_TRUE);
1612 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1621 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1623 return (zhp->zfs_props == zhp->zfs_recvd_props);
1627 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1629 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1630 zhp->zfs_props = zhp->zfs_recvd_props;
1634 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1636 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1641 * Internal function for getting a numeric property. Both zfs_prop_get() and
1642 * zfs_prop_get_int() are built using this interface.
1644 * Certain properties can be overridden using 'mount -o'. In this case, scan
1645 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1646 * If they differ from the on-disk values, report the current values and mark
1647 * the source "temporary".
1650 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1651 char **source, uint64_t *val)
1653 zfs_cmd_t zc = { 0 };
1654 nvlist_t *zplprops = NULL;
1656 char *mntopt_on = NULL;
1657 char *mntopt_off = NULL;
1658 boolean_t received = zfs_is_recvd_props_mode(zhp);
1663 case ZFS_PROP_ATIME:
1664 mntopt_on = MNTOPT_ATIME;
1665 mntopt_off = MNTOPT_NOATIME;
1668 case ZFS_PROP_DEVICES:
1669 mntopt_on = MNTOPT_DEVICES;
1670 mntopt_off = MNTOPT_NODEVICES;
1674 mntopt_on = MNTOPT_EXEC;
1675 mntopt_off = MNTOPT_NOEXEC;
1678 case ZFS_PROP_READONLY:
1679 mntopt_on = MNTOPT_RO;
1680 mntopt_off = MNTOPT_RW;
1683 case ZFS_PROP_SETUID:
1684 mntopt_on = MNTOPT_SETUID;
1685 mntopt_off = MNTOPT_NOSETUID;
1688 case ZFS_PROP_XATTR:
1689 mntopt_on = MNTOPT_XATTR;
1690 mntopt_off = MNTOPT_NOXATTR;
1693 case ZFS_PROP_NBMAND:
1694 mntopt_on = MNTOPT_NBMAND;
1695 mntopt_off = MNTOPT_NONBMAND;
1700 * Because looking up the mount options is potentially expensive
1701 * (iterating over all of /etc/mnttab), we defer its calculation until
1702 * we're looking up a property which requires its presence.
1704 if (!zhp->zfs_mntcheck &&
1705 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1706 libzfs_handle_t *hdl = zhp->zfs_hdl;
1707 struct mnttab entry;
1709 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1710 zhp->zfs_mntopts = zfs_strdup(hdl,
1712 if (zhp->zfs_mntopts == NULL)
1716 zhp->zfs_mntcheck = B_TRUE;
1719 if (zhp->zfs_mntopts == NULL)
1720 mnt.mnt_mntopts = "";
1722 mnt.mnt_mntopts = zhp->zfs_mntopts;
1725 case ZFS_PROP_ATIME:
1726 case ZFS_PROP_DEVICES:
1728 case ZFS_PROP_READONLY:
1729 case ZFS_PROP_SETUID:
1730 case ZFS_PROP_XATTR:
1731 case ZFS_PROP_NBMAND:
1732 *val = getprop_uint64(zhp, prop, source);
1737 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1740 *src = ZPROP_SRC_TEMPORARY;
1741 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1744 *src = ZPROP_SRC_TEMPORARY;
1748 case ZFS_PROP_CANMOUNT:
1749 case ZFS_PROP_VOLSIZE:
1750 case ZFS_PROP_QUOTA:
1751 case ZFS_PROP_REFQUOTA:
1752 case ZFS_PROP_RESERVATION:
1753 case ZFS_PROP_REFRESERVATION:
1754 *val = getprop_uint64(zhp, prop, source);
1756 if (*source == NULL) {
1757 /* not default, must be local */
1758 *source = zhp->zfs_name;
1762 case ZFS_PROP_MOUNTED:
1763 *val = (zhp->zfs_mntopts != NULL);
1766 case ZFS_PROP_NUMCLONES:
1767 *val = zhp->zfs_dmustats.dds_num_clones;
1770 case ZFS_PROP_VERSION:
1771 case ZFS_PROP_NORMALIZE:
1772 case ZFS_PROP_UTF8ONLY:
1774 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1775 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1777 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1778 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1779 zcmd_free_nvlists(&zc);
1782 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1783 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1785 zcmd_free_nvlists(&zc);
1789 nvlist_free(zplprops);
1790 zcmd_free_nvlists(&zc);
1794 switch (zfs_prop_get_type(prop)) {
1795 case PROP_TYPE_NUMBER:
1796 case PROP_TYPE_INDEX:
1797 *val = getprop_uint64(zhp, prop, source);
1799 * If we tried to use a default value for a
1800 * readonly property, it means that it was not
1803 if (zfs_prop_readonly(prop) &&
1804 *source != NULL && (*source)[0] == '\0') {
1809 case PROP_TYPE_STRING:
1811 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1812 "cannot get non-numeric property"));
1813 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1814 dgettext(TEXT_DOMAIN, "internal error")));
1822 * Calculate the source type, given the raw source string.
1825 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1826 char *statbuf, size_t statlen)
1828 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1831 if (source == NULL) {
1832 *srctype = ZPROP_SRC_NONE;
1833 } else if (source[0] == '\0') {
1834 *srctype = ZPROP_SRC_DEFAULT;
1835 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1836 *srctype = ZPROP_SRC_RECEIVED;
1838 if (strcmp(source, zhp->zfs_name) == 0) {
1839 *srctype = ZPROP_SRC_LOCAL;
1841 (void) strlcpy(statbuf, source, statlen);
1842 *srctype = ZPROP_SRC_INHERITED;
1849 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1850 size_t proplen, boolean_t literal)
1855 if (zhp->zfs_recvd_props == NULL)
1856 if (get_recvd_props_ioctl(zhp) != 0)
1859 prop = zfs_name_to_prop(propname);
1861 if (prop != ZPROP_INVAL) {
1863 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1865 zfs_set_recvd_props_mode(zhp, &cookie);
1866 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1867 NULL, NULL, 0, literal);
1868 zfs_unset_recvd_props_mode(zhp, &cookie);
1869 } else if (zfs_prop_userquota(propname)) {
1874 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1875 propname, &propval) != 0)
1877 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1879 (void) strlcpy(propbuf, recvdval, proplen);
1882 return (err == 0 ? 0 : -1);
1886 * Retrieve a property from the given object. If 'literal' is specified, then
1887 * numbers are left as exact values. Otherwise, numbers are converted to a
1888 * human-readable form.
1890 * Returns 0 on success, or -1 on error.
1893 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1894 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1896 char *source = NULL;
1900 boolean_t received = zfs_is_recvd_props_mode(zhp);
1903 * Check to see if this property applies to our object
1905 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1908 if (received && zfs_prop_readonly(prop))
1912 *src = ZPROP_SRC_NONE;
1915 case ZFS_PROP_CREATION:
1917 * 'creation' is a time_t stored in the statistics. We convert
1918 * this into a string unless 'literal' is specified.
1921 val = getprop_uint64(zhp, prop, &source);
1922 time_t time = (time_t)val;
1926 localtime_r(&time, &t) == NULL ||
1927 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1929 (void) snprintf(propbuf, proplen, "%llu", val);
1933 case ZFS_PROP_MOUNTPOINT:
1935 * Getting the precise mountpoint can be tricky.
1937 * - for 'none' or 'legacy', return those values.
1938 * - for inherited mountpoints, we want to take everything
1939 * after our ancestor and append it to the inherited value.
1941 * If the pool has an alternate root, we want to prepend that
1942 * root to any values we return.
1945 str = getprop_string(zhp, prop, &source);
1947 if (str[0] == '/') {
1948 char buf[MAXPATHLEN];
1950 const char *relpath;
1953 * If we inherit the mountpoint, even from a dataset
1954 * with a received value, the source will be the path of
1955 * the dataset we inherit from. If source is
1956 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1959 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1962 relpath = zhp->zfs_name + strlen(source);
1963 if (relpath[0] == '/')
1967 if ((zpool_get_prop(zhp->zpool_hdl,
1968 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1969 (strcmp(root, "-") == 0))
1972 * Special case an alternate root of '/'. This will
1973 * avoid having multiple leading slashes in the
1976 if (strcmp(root, "/") == 0)
1980 * If the mountpoint is '/' then skip over this
1981 * if we are obtaining either an alternate root or
1982 * an inherited mountpoint.
1984 if (str[1] == '\0' && (root[0] != '\0' ||
1985 relpath[0] != '\0'))
1988 if (relpath[0] == '\0')
1989 (void) snprintf(propbuf, proplen, "%s%s",
1992 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1993 root, str, relpath[0] == '@' ? "" : "/",
1996 /* 'legacy' or 'none' */
1997 (void) strlcpy(propbuf, str, proplen);
2002 case ZFS_PROP_ORIGIN:
2003 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2006 * If there is no parent at all, return failure to indicate that
2007 * it doesn't apply to this dataset.
2009 if (propbuf[0] == '\0')
2013 case ZFS_PROP_QUOTA:
2014 case ZFS_PROP_REFQUOTA:
2015 case ZFS_PROP_RESERVATION:
2016 case ZFS_PROP_REFRESERVATION:
2018 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2022 * If quota or reservation is 0, we translate this into 'none'
2023 * (unless literal is set), and indicate that it's the default
2024 * value. Otherwise, we print the number nicely and indicate
2025 * that its set locally.
2029 (void) strlcpy(propbuf, "0", proplen);
2031 (void) strlcpy(propbuf, "none", proplen);
2034 (void) snprintf(propbuf, proplen, "%llu",
2037 zfs_nicenum(val, propbuf, proplen);
2041 case ZFS_PROP_COMPRESSRATIO:
2042 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2044 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2045 (u_longlong_t)(val / 100),
2046 (u_longlong_t)(val % 100));
2050 switch (zhp->zfs_type) {
2051 case ZFS_TYPE_FILESYSTEM:
2054 case ZFS_TYPE_VOLUME:
2057 case ZFS_TYPE_SNAPSHOT:
2063 (void) snprintf(propbuf, proplen, "%s", str);
2066 case ZFS_PROP_MOUNTED:
2068 * The 'mounted' property is a pseudo-property that described
2069 * whether the filesystem is currently mounted. Even though
2070 * it's a boolean value, the typical values of "on" and "off"
2071 * don't make sense, so we translate to "yes" and "no".
2073 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2074 src, &source, &val) != 0)
2077 (void) strlcpy(propbuf, "yes", proplen);
2079 (void) strlcpy(propbuf, "no", proplen);
2084 * The 'name' property is a pseudo-property derived from the
2085 * dataset name. It is presented as a real property to simplify
2088 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2091 case ZFS_PROP_MLSLABEL:
2094 m_label_t *new_sl = NULL;
2095 char *ascii = NULL; /* human readable label */
2097 (void) strlcpy(propbuf,
2098 getprop_string(zhp, prop, &source), proplen);
2100 if (literal || (strcasecmp(propbuf,
2101 ZFS_MLSLABEL_DEFAULT) == 0))
2105 * Try to translate the internal hex string to
2106 * human-readable output. If there are any
2107 * problems just use the hex string.
2110 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2111 L_NO_CORRECTION, NULL) == -1) {
2112 m_label_free(new_sl);
2116 if (label_to_str(new_sl, &ascii, M_LABEL,
2120 m_label_free(new_sl);
2123 m_label_free(new_sl);
2125 (void) strlcpy(propbuf, ascii, proplen);
2134 switch (zfs_prop_get_type(prop)) {
2135 case PROP_TYPE_NUMBER:
2136 if (get_numeric_property(zhp, prop, src,
2137 &source, &val) != 0)
2140 (void) snprintf(propbuf, proplen, "%llu",
2143 zfs_nicenum(val, propbuf, proplen);
2146 case PROP_TYPE_STRING:
2147 (void) strlcpy(propbuf,
2148 getprop_string(zhp, prop, &source), proplen);
2151 case PROP_TYPE_INDEX:
2152 if (get_numeric_property(zhp, prop, src,
2153 &source, &val) != 0)
2155 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2157 (void) strlcpy(propbuf, strval, proplen);
2165 get_source(zhp, src, source, statbuf, statlen);
2171 * Utility function to get the given numeric property. Does no validation that
2172 * the given property is the appropriate type; should only be used with
2173 * hard-coded property types.
2176 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2181 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2187 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2191 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2192 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2196 * Similar to zfs_prop_get(), but returns the value as an integer.
2199 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2200 zprop_source_t *src, char *statbuf, size_t statlen)
2205 * Check to see if this property applies to our object
2207 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2208 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2209 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2210 zfs_prop_to_name(prop)));
2214 *src = ZPROP_SRC_NONE;
2216 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2219 get_source(zhp, src, source, statbuf, statlen);
2225 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2226 char **domainp, idmap_rid_t *ridp)
2229 idmap_get_handle_t *get_hdl = NULL;
2233 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2237 err = idmap_get_sidbyuid(get_hdl, id,
2238 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2240 err = idmap_get_sidbygid(get_hdl, id,
2241 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2243 if (err == IDMAP_SUCCESS &&
2244 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2245 status == IDMAP_SUCCESS)
2251 idmap_get_destroy(get_hdl);
2254 assert(!"invalid code path");
2259 * convert the propname into parameters needed by kernel
2260 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2261 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2264 userquota_propname_decode(const char *propname, boolean_t zoned,
2265 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2267 zfs_userquota_prop_t type;
2269 char *numericsid = NULL;
2274 /* Figure out the property type ({user|group}{quota|space}) */
2275 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2276 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2277 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2280 if (type == ZFS_NUM_USERQUOTA_PROPS)
2284 isuser = (type == ZFS_PROP_USERQUOTA ||
2285 type == ZFS_PROP_USERUSED);
2287 cp = strchr(propname, '@') + 1;
2289 if (strchr(cp, '@')) {
2292 * It's a SID name (eg "user@domain") that needs to be
2293 * turned into S-1-domainID-RID.
2295 directory_error_t e;
2296 if (zoned && getzoneid() == GLOBAL_ZONEID)
2299 e = directory_sid_from_user_name(NULL,
2302 e = directory_sid_from_group_name(NULL,
2306 directory_error_free(e);
2309 if (numericsid == NULL)
2312 /* will be further decoded below */
2318 if (strncmp(cp, "S-1-", 4) == 0) {
2319 /* It's a numeric SID (eg "S-1-234-567-89") */
2320 (void) strlcpy(domain, cp, domainlen);
2321 cp = strrchr(domain, '-');
2326 *ridp = strtoull(cp, &end, 10);
2331 if (errno != 0 || *end != '\0')
2333 } else if (!isdigit(*cp)) {
2335 * It's a user/group name (eg "user") that needs to be
2336 * turned into a uid/gid
2338 if (zoned && getzoneid() == GLOBAL_ZONEID)
2354 /* It's a user/group ID (eg "12345"). */
2355 uid_t id = strtoul(cp, &end, 10);
2362 /* It's an ephemeral ID. */
2363 if (idmap_id_to_numeric_domain_rid(id, isuser,
2364 &mapdomain, &rid) != 0)
2366 (void) strlcpy(domain, mapdomain, domainlen);
2373 ASSERT3P(numericsid, ==, NULL);
2378 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2379 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2382 zfs_cmd_t zc = { 0 };
2384 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2386 err = userquota_propname_decode(propname,
2387 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2388 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2389 zc.zc_objset_type = *typep;
2393 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2397 *propvalue = zc.zc_cookie;
2402 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2403 uint64_t *propvalue)
2405 zfs_userquota_prop_t type;
2407 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2412 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2413 char *propbuf, int proplen, boolean_t literal)
2417 zfs_userquota_prop_t type;
2419 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2426 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2427 } else if (propvalue == 0 &&
2428 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2429 (void) strlcpy(propbuf, "none", proplen);
2431 zfs_nicenum(propvalue, propbuf, proplen);
2437 * Returns the name of the given zfs handle.
2440 zfs_get_name(const zfs_handle_t *zhp)
2442 return (zhp->zfs_name);
2446 * Returns the type of the given zfs handle.
2449 zfs_get_type(const zfs_handle_t *zhp)
2451 return (zhp->zfs_type);
2455 zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
2458 uint64_t orig_cookie;
2460 orig_cookie = zc->zc_cookie;
2462 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2463 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2468 /* expand nvlist memory and try again */
2469 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2470 zcmd_free_nvlists(zc);
2473 zc->zc_cookie = orig_cookie;
2476 * An errno value of ESRCH indicates normal completion.
2477 * If ENOENT is returned, then the underlying dataset
2478 * has been removed since we obtained the handle.
2485 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2486 dgettext(TEXT_DOMAIN,
2487 "cannot iterate filesystems"));
2495 * Iterate over all child filesystems
2498 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2500 zfs_cmd_t zc = { 0 };
2504 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2507 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2510 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2513 * Silently ignore errors, as the only plausible explanation is
2514 * that the pool has since been removed.
2516 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2521 if ((ret = func(nzhp, data)) != 0) {
2522 zcmd_free_nvlists(&zc);
2526 zcmd_free_nvlists(&zc);
2527 return ((ret < 0) ? ret : 0);
2531 * Iterate over all snapshots
2534 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2536 zfs_cmd_t zc = { 0 };
2540 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2543 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2545 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2548 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2553 if ((ret = func(nzhp, data)) != 0) {
2554 zcmd_free_nvlists(&zc);
2558 zcmd_free_nvlists(&zc);
2559 return ((ret < 0) ? ret : 0);
2563 * Iterate over all children, snapshots and filesystems
2566 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2570 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2573 return (zfs_iter_snapshots(zhp, func, data));
2577 * Is one dataset name a child dataset of another?
2579 * Needs to handle these cases:
2580 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2581 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2582 * Descendant? No. No. No. Yes.
2585 is_descendant(const char *ds1, const char *ds2)
2587 size_t d1len = strlen(ds1);
2589 /* ds2 can't be a descendant if it's smaller */
2590 if (strlen(ds2) < d1len)
2593 /* otherwise, compare strings and verify that there's a '/' char */
2594 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2598 * Given a complete name, return just the portion that refers to the parent.
2599 * Can return NULL if this is a pool.
2602 parent_name(const char *path, char *buf, size_t buflen)
2606 if ((loc = strrchr(path, '/')) == NULL)
2609 (void) strncpy(buf, path, MIN(buflen, loc - path));
2610 buf[loc - path] = '\0';
2616 * If accept_ancestor is false, then check to make sure that the given path has
2617 * a parent, and that it exists. If accept_ancestor is true, then find the
2618 * closest existing ancestor for the given path. In prefixlen return the
2619 * length of already existing prefix of the given path. We also fetch the
2620 * 'zoned' property, which is used to validate property settings when creating
2624 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2625 boolean_t accept_ancestor, int *prefixlen)
2627 zfs_cmd_t zc = { 0 };
2628 char parent[ZFS_MAXNAMELEN];
2634 (void) snprintf(errbuf, sizeof (errbuf),
2635 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2637 /* get parent, and check to see if this is just a pool */
2638 if (parent_name(path, parent, sizeof (parent)) != 0) {
2639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2640 "missing dataset name"));
2641 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2644 /* check to see if the pool exists */
2645 if ((slash = strchr(parent, '/')) == NULL)
2646 slash = parent + strlen(parent);
2647 (void) strncpy(zc.zc_name, parent, slash - parent);
2648 zc.zc_name[slash - parent] = '\0';
2649 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2652 "no such pool '%s'"), zc.zc_name);
2653 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2656 /* check to see if the parent dataset exists */
2657 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2658 if (errno == ENOENT && accept_ancestor) {
2660 * Go deeper to find an ancestor, give up on top level.
2662 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2664 "no such pool '%s'"), zc.zc_name);
2665 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2667 } else if (errno == ENOENT) {
2668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2669 "parent does not exist"));
2670 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2672 return (zfs_standard_error(hdl, errno, errbuf));
2675 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2679 /* we are in a non-global zone, but parent is in the global zone */
2680 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2681 (void) zfs_standard_error(hdl, EPERM, errbuf);
2686 /* make sure parent is a filesystem */
2687 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2689 "parent is not a filesystem"));
2690 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2696 if (prefixlen != NULL)
2697 *prefixlen = strlen(parent);
2702 * Finds whether the dataset of the given type(s) exists.
2705 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2709 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2713 * Try to get stats for the dataset, which will tell us if it exists.
2715 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2716 int ds_type = zhp->zfs_type;
2719 if (types & ds_type)
2726 * Given a path to 'target', create all the ancestors between
2727 * the prefixlen portion of the path, and the target itself.
2728 * Fail if the initial prefixlen-ancestor does not already exist.
2731 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2737 /* make sure prefix exists */
2738 cp = target + prefixlen;
2740 assert(strchr(cp, '/') == NULL);
2741 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2744 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2752 * Attempt to create, mount, and share any ancestor filesystems,
2753 * up to the prefixlen-long one.
2755 for (cp = target + prefixlen + 1;
2756 cp = strchr(cp, '/'); *cp = '/', cp++) {
2761 h = make_dataset_handle(hdl, target);
2763 /* it already exists, nothing to do here */
2768 logstr = hdl->libzfs_log_str;
2769 hdl->libzfs_log_str = NULL;
2770 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2772 hdl->libzfs_log_str = logstr;
2773 opname = dgettext(TEXT_DOMAIN, "create");
2777 hdl->libzfs_log_str = logstr;
2778 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2780 opname = dgettext(TEXT_DOMAIN, "open");
2784 if (zfs_mount(h, NULL, 0) != 0) {
2785 opname = dgettext(TEXT_DOMAIN, "mount");
2789 if (zfs_share(h) != 0) {
2790 opname = dgettext(TEXT_DOMAIN, "share");
2800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2801 "failed to %s ancestor '%s'"), opname, target);
2806 * Creates non-existing ancestors of the given path.
2809 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2815 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2818 if ((path_copy = strdup(path)) != NULL) {
2819 rc = create_parents(hdl, path_copy, prefix);
2822 if (path_copy == NULL || rc != 0)
2829 * Create a new filesystem or volume.
2832 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2835 zfs_cmd_t zc = { 0 };
2838 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2842 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2843 "cannot create '%s'"), path);
2845 /* validate the path, taking care to note the extended error message */
2846 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2847 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2849 /* validate parents exist */
2850 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2854 * The failure modes when creating a dataset of a different type over
2855 * one that already exists is a little strange. In particular, if you
2856 * try to create a dataset on top of an existing dataset, the ioctl()
2857 * will return ENOENT, not EEXIST. To prevent this from happening, we
2858 * first try to see if the dataset exists.
2860 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2861 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2862 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2863 "dataset already exists"));
2864 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2867 if (type == ZFS_TYPE_VOLUME)
2868 zc.zc_objset_type = DMU_OST_ZVOL;
2870 zc.zc_objset_type = DMU_OST_ZFS;
2872 if (props && (props = zfs_valid_proplist(hdl, type, props,
2873 zoned, NULL, errbuf)) == 0)
2876 if (type == ZFS_TYPE_VOLUME) {
2878 * If we are creating a volume, the size and block size must
2879 * satisfy a few restraints. First, the blocksize must be a
2880 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2881 * volsize must be a multiple of the block size, and cannot be
2884 if (props == NULL || nvlist_lookup_uint64(props,
2885 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2887 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2888 "missing volume size"));
2889 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2892 if ((ret = nvlist_lookup_uint64(props,
2893 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2894 &blocksize)) != 0) {
2895 if (ret == ENOENT) {
2896 blocksize = zfs_prop_default_numeric(
2897 ZFS_PROP_VOLBLOCKSIZE);
2900 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2901 "missing volume block size"));
2902 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2908 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2909 "volume size cannot be zero"));
2910 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2913 if (size % blocksize != 0) {
2915 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2916 "volume size must be a multiple of volume block "
2918 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2922 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2926 /* create the dataset */
2927 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2929 zcmd_free_nvlists(&zc);
2931 /* check for failure */
2933 char parent[ZFS_MAXNAMELEN];
2934 (void) parent_name(path, parent, sizeof (parent));
2938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2939 "no such parent '%s'"), parent);
2940 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2944 "parent '%s' is not a filesystem"), parent);
2945 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2949 "volume block size must be power of 2 from "
2951 (uint_t)SPA_MINBLOCKSIZE,
2952 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2954 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2958 "pool must be upgraded to set this "
2959 "property or value"));
2960 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2964 * This platform can't address a volume this big.
2966 if (type == ZFS_TYPE_VOLUME)
2967 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2972 return (zfs_standard_error(hdl, errno, errbuf));
2980 * Destroys the given dataset. The caller must make sure that the filesystem
2981 * isn't mounted, and that there are no active dependents.
2984 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2986 zfs_cmd_t zc = { 0 };
2988 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2990 if (ZFS_IS_VOLUME(zhp)) {
2991 zc.zc_objset_type = DMU_OST_ZVOL;
2993 zc.zc_objset_type = DMU_OST_ZFS;
2996 zc.zc_defer_destroy = defer;
2997 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2998 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2999 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3003 remove_mountpoint(zhp);
3008 struct destroydata {
3015 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3017 struct destroydata *dd = arg;
3019 char name[ZFS_MAXNAMELEN];
3020 boolean_t closezhp = dd->closezhp;
3023 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3024 (void) strlcat(name, "@", sizeof (name));
3025 (void) strlcat(name, dd->snapname, sizeof (name));
3027 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3029 dd->gotone = B_TRUE;
3033 dd->closezhp = B_TRUE;
3035 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
3042 * Destroys all snapshots with the given name in zhp & descendants.
3045 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3047 zfs_cmd_t zc = { 0 };
3049 struct destroydata dd = { 0 };
3051 dd.snapname = snapname;
3052 (void) zfs_check_snap_cb(zhp, &dd);
3055 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3056 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3057 zhp->zfs_name, snapname));
3060 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3061 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3062 zc.zc_defer_destroy = defer;
3064 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3068 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3069 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3073 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3074 "snapshot is cloned"));
3075 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3078 return (zfs_standard_error(zhp->zfs_hdl, errno,
3087 * Clones the given dataset. The target must be of the same type as the source.
3090 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3092 zfs_cmd_t zc = { 0 };
3093 char parent[ZFS_MAXNAMELEN];
3096 libzfs_handle_t *hdl = zhp->zfs_hdl;
3100 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3102 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3103 "cannot create '%s'"), target);
3105 /* validate the target name */
3106 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3107 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3109 /* validate parents exist */
3110 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3113 (void) parent_name(target, parent, sizeof (parent));
3116 if (ZFS_IS_VOLUME(zhp)) {
3117 zc.zc_objset_type = DMU_OST_ZVOL;
3118 type = ZFS_TYPE_VOLUME;
3120 zc.zc_objset_type = DMU_OST_ZFS;
3121 type = ZFS_TYPE_FILESYSTEM;
3125 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3126 zhp, errbuf)) == NULL)
3129 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3137 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3138 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3139 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3141 zcmd_free_nvlists(&zc);
3148 * The parent doesn't exist. We should have caught this
3149 * above, but there may a race condition that has since
3150 * destroyed the parent.
3152 * At this point, we don't know whether it's the source
3153 * that doesn't exist anymore, or whether the target
3154 * dataset doesn't exist.
3156 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3157 "no such parent '%s'"), parent);
3158 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3161 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3162 "source and target pools differ"));
3163 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3167 return (zfs_standard_error(zhp->zfs_hdl, errno,
3176 * Promotes the given clone fs to be the clone parent.
3179 zfs_promote(zfs_handle_t *zhp)
3181 libzfs_handle_t *hdl = zhp->zfs_hdl;
3182 zfs_cmd_t zc = { 0 };
3183 char parent[MAXPATHLEN];
3187 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3188 "cannot promote '%s'"), zhp->zfs_name);
3190 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3192 "snapshots can not be promoted"));
3193 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3196 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3197 if (parent[0] == '\0') {
3198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3199 "not a cloned filesystem"));
3200 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3203 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3204 sizeof (zc.zc_value));
3205 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3206 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3209 int save_errno = errno;
3211 switch (save_errno) {
3213 /* There is a conflicting snapshot name. */
3214 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3215 "conflicting snapshot '%s' from parent '%s'"),
3216 zc.zc_string, parent);
3217 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3220 return (zfs_standard_error(hdl, save_errno, errbuf));
3227 * Takes a snapshot of the given dataset.
3230 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3234 char parent[ZFS_MAXNAMELEN];
3236 zfs_cmd_t zc = { 0 };
3240 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3241 "cannot snapshot '%s'"), path);
3243 /* validate the target name */
3244 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3245 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3248 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3249 props, B_FALSE, NULL, errbuf)) == NULL)
3252 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3260 /* make sure the parent exists and is of the appropriate type */
3261 delim = strchr(path, '@');
3262 (void) strncpy(parent, path, delim - path);
3263 parent[delim - path] = '\0';
3265 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3266 ZFS_TYPE_VOLUME)) == NULL) {
3267 zcmd_free_nvlists(&zc);
3271 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3272 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3273 if (ZFS_IS_VOLUME(zhp))
3274 zc.zc_objset_type = DMU_OST_ZVOL;
3276 zc.zc_objset_type = DMU_OST_ZFS;
3277 zc.zc_cookie = recursive;
3278 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3280 zcmd_free_nvlists(&zc);
3283 * if it was recursive, the one that actually failed will be in
3287 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3288 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3289 (void) zfs_standard_error(hdl, errno, errbuf);
3298 * Destroy any more recent snapshots. We invoke this callback on any dependents
3299 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3300 * is a dependent and we should just destroy it without checking the transaction
3303 typedef struct rollback_data {
3304 const char *cb_target; /* the snapshot */
3305 uint64_t cb_create; /* creation time reference */
3307 boolean_t cb_dependent;
3312 rollback_destroy(zfs_handle_t *zhp, void *data)
3314 rollback_data_t *cbp = data;
3316 if (!cbp->cb_dependent) {
3317 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3318 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3319 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3323 cbp->cb_dependent = B_TRUE;
3324 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3325 rollback_destroy, cbp);
3326 cbp->cb_dependent = B_FALSE;
3328 logstr = zhp->zfs_hdl->libzfs_log_str;
3329 zhp->zfs_hdl->libzfs_log_str = NULL;
3330 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3331 zhp->zfs_hdl->libzfs_log_str = logstr;
3334 /* We must destroy this clone; first unmount it */
3335 prop_changelist_t *clp;
3337 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3338 cbp->cb_force ? MS_FORCE: 0);
3339 if (clp == NULL || changelist_prefix(clp) != 0) {
3340 cbp->cb_error = B_TRUE;
3344 if (zfs_destroy(zhp, B_FALSE) != 0)
3345 cbp->cb_error = B_TRUE;
3347 changelist_remove(clp, zhp->zfs_name);
3348 (void) changelist_postfix(clp);
3349 changelist_free(clp);
3357 * Given a dataset, rollback to a specific snapshot, discarding any
3358 * data changes since then and making it the active dataset.
3360 * Any snapshots more recent than the target are destroyed, along with
3364 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3366 rollback_data_t cb = { 0 };
3368 zfs_cmd_t zc = { 0 };
3369 boolean_t restore_resv = 0;
3370 uint64_t old_volsize, new_volsize;
3371 zfs_prop_t resv_prop;
3373 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3374 zhp->zfs_type == ZFS_TYPE_VOLUME);
3377 * Destroy all recent snapshots and its dependends.
3379 cb.cb_force = force;
3380 cb.cb_target = snap->zfs_name;
3381 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3382 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3388 * Now that we have verified that the snapshot is the latest,
3389 * rollback to the given snapshot.
3392 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3393 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3395 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3397 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3400 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3402 if (ZFS_IS_VOLUME(zhp))
3403 zc.zc_objset_type = DMU_OST_ZVOL;
3405 zc.zc_objset_type = DMU_OST_ZFS;
3408 * We rely on zfs_iter_children() to verify that there are no
3409 * newer snapshots for the given dataset. Therefore, we can
3410 * simply pass the name on to the ioctl() call. There is still
3411 * an unlikely race condition where the user has taken a
3412 * snapshot since we verified that this was the most recent.
3415 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3416 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3417 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3423 * For volumes, if the pre-rollback volsize matched the pre-
3424 * rollback reservation and the volsize has changed then set
3425 * the reservation property to the post-rollback volsize.
3426 * Make a new handle since the rollback closed the dataset.
3428 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3429 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3431 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3432 if (old_volsize != new_volsize)
3433 err = zfs_prop_set_int(zhp, resv_prop,
3442 * Iterate over all dependents for a given dataset. This includes both
3443 * hierarchical dependents (children) and data dependents (snapshots and
3444 * clones). The bulk of the processing occurs in get_dependents() in
3448 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3449 zfs_iter_f func, void *data)
3454 zfs_handle_t *child;
3457 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3458 &dependents, &count) != 0)
3461 for (i = 0; i < count; i++) {
3462 if ((child = make_dataset_handle(zhp->zfs_hdl,
3463 dependents[i])) == NULL)
3466 if ((ret = func(child, data)) != 0)
3470 for (i = 0; i < count; i++)
3471 free(dependents[i]);
3478 * Renames the given dataset.
3481 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3484 zfs_cmd_t zc = { 0 };
3486 prop_changelist_t *cl = NULL;
3487 zfs_handle_t *zhrp = NULL;
3488 char *parentname = NULL;
3489 char parent[ZFS_MAXNAMELEN];
3490 libzfs_handle_t *hdl = zhp->zfs_hdl;
3493 /* if we have the same exact name, just return success */
3494 if (strcmp(zhp->zfs_name, target) == 0)
3497 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3498 "cannot rename to '%s'"), target);
3501 * Make sure the target name is valid
3503 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3504 if ((strchr(target, '@') == NULL) ||
3507 * Snapshot target name is abbreviated,
3508 * reconstruct full dataset name
3510 (void) strlcpy(parent, zhp->zfs_name,
3512 delim = strchr(parent, '@');
3513 if (strchr(target, '@') == NULL)
3517 (void) strlcat(parent, target, sizeof (parent));
3521 * Make sure we're renaming within the same dataset.
3523 delim = strchr(target, '@');
3524 if (strncmp(zhp->zfs_name, target, delim - target)
3525 != 0 || zhp->zfs_name[delim - target] != '@') {
3526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3527 "snapshots must be part of same "
3529 return (zfs_error(hdl, EZFS_CROSSTARGET,
3533 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3534 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3538 "recursive rename must be a snapshot"));
3539 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3542 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3543 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3545 /* validate parents */
3546 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3549 /* make sure we're in the same pool */
3550 verify((delim = strchr(target, '/')) != NULL);
3551 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3552 zhp->zfs_name[delim - target] != '/') {
3553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3554 "datasets must be within same pool"));
3555 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3558 /* new name cannot be a child of the current dataset name */
3559 if (is_descendant(zhp->zfs_name, target)) {
3560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3561 "New dataset name cannot be a descendant of "
3562 "current dataset name"));
3563 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3567 (void) snprintf(errbuf, sizeof (errbuf),
3568 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3570 if (getzoneid() == GLOBAL_ZONEID &&
3571 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3573 "dataset is used in a non-global zone"));
3574 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3579 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3580 if (parentname == NULL) {
3584 delim = strchr(parentname, '@');
3586 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3593 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3596 if (changelist_haszonedchild(cl)) {
3597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3598 "child dataset with inherited mountpoint is used "
3599 "in a non-global zone"));
3600 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3604 if ((ret = changelist_prefix(cl)) != 0)
3608 if (ZFS_IS_VOLUME(zhp))
3609 zc.zc_objset_type = DMU_OST_ZVOL;
3611 zc.zc_objset_type = DMU_OST_ZFS;
3613 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3614 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3616 zc.zc_cookie = recursive;
3618 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3620 * if it was recursive, the one that actually failed will
3623 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3624 "cannot rename '%s'"), zc.zc_name);
3626 if (recursive && errno == EEXIST) {
3627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3628 "a child dataset already has a snapshot "
3629 "with the new name"));
3630 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3632 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3636 * On failure, we still want to remount any filesystems that
3637 * were previously mounted, so we don't alter the system state.
3640 (void) changelist_postfix(cl);
3643 changelist_rename(cl, zfs_get_name(zhp), target);
3644 ret = changelist_postfix(cl);
3656 changelist_free(cl);
3662 zfs_get_user_props(zfs_handle_t *zhp)
3664 return (zhp->zfs_user_props);
3668 zfs_get_recvd_props(zfs_handle_t *zhp)
3670 if (zhp->zfs_recvd_props == NULL)
3671 if (get_recvd_props_ioctl(zhp) != 0)
3673 return (zhp->zfs_recvd_props);
3677 * This function is used by 'zfs list' to determine the exact set of columns to
3678 * display, and their maximum widths. This does two main things:
3680 * - If this is a list of all properties, then expand the list to include
3681 * all native properties, and set a flag so that for each dataset we look
3682 * for new unique user properties and add them to the list.
3684 * - For non fixed-width properties, keep track of the maximum width seen
3685 * so that we can size the column appropriately. If the user has
3686 * requested received property values, we also need to compute the width
3687 * of the RECEIVED column.
3690 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3692 libzfs_handle_t *hdl = zhp->zfs_hdl;
3693 zprop_list_t *entry;
3694 zprop_list_t **last, **start;
3695 nvlist_t *userprops, *propval;
3698 char buf[ZFS_MAXPROPLEN];
3700 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3703 userprops = zfs_get_user_props(zhp);
3706 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3708 * Go through and add any user properties as necessary. We
3709 * start by incrementing our list pointer to the first
3710 * non-native property.
3713 while (*start != NULL) {
3714 if ((*start)->pl_prop == ZPROP_INVAL)
3716 start = &(*start)->pl_next;
3720 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3722 * See if we've already found this property in our list.
3724 for (last = start; *last != NULL;
3725 last = &(*last)->pl_next) {
3726 if (strcmp((*last)->pl_user_prop,
3727 nvpair_name(elem)) == 0)
3731 if (*last == NULL) {
3732 if ((entry = zfs_alloc(hdl,
3733 sizeof (zprop_list_t))) == NULL ||
3734 ((entry->pl_user_prop = zfs_strdup(hdl,
3735 nvpair_name(elem)))) == NULL) {
3740 entry->pl_prop = ZPROP_INVAL;
3741 entry->pl_width = strlen(nvpair_name(elem));
3742 entry->pl_all = B_TRUE;
3749 * Now go through and check the width of any non-fixed columns
3751 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3752 if (entry->pl_fixed)
3755 if (entry->pl_prop != ZPROP_INVAL) {
3756 if (zfs_prop_get(zhp, entry->pl_prop,
3757 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3758 if (strlen(buf) > entry->pl_width)
3759 entry->pl_width = strlen(buf);
3761 if (received && zfs_prop_get_recvd(zhp,
3762 zfs_prop_to_name(entry->pl_prop),
3763 buf, sizeof (buf), B_FALSE) == 0)
3764 if (strlen(buf) > entry->pl_recvd_width)
3765 entry->pl_recvd_width = strlen(buf);
3767 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3769 verify(nvlist_lookup_string(propval,
3770 ZPROP_VALUE, &strval) == 0);
3771 if (strlen(strval) > entry->pl_width)
3772 entry->pl_width = strlen(strval);
3774 if (received && zfs_prop_get_recvd(zhp,
3775 entry->pl_user_prop,
3776 buf, sizeof (buf), B_FALSE) == 0)
3777 if (strlen(buf) > entry->pl_recvd_width)
3778 entry->pl_recvd_width = strlen(buf);
3786 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3787 char *resource, void *export, void *sharetab,
3788 int sharemax, zfs_share_op_t operation)
3790 zfs_cmd_t zc = { 0 };
3793 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3794 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3796 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3797 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3798 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3799 zc.zc_share.z_sharetype = operation;
3800 zc.zc_share.z_sharemax = sharemax;
3801 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3806 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3811 * Keep a reference to the props-table against which we prune the
3814 zhp->zfs_props_table = props;
3816 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3819 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3820 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3823 * User properties will result in ZPROP_INVAL, and since we
3824 * only know how to prune standard ZFS properties, we always
3825 * leave these in the list. This can also happen if we
3826 * encounter an unknown DSL property (when running older
3827 * software, for example).
3829 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3830 (void) nvlist_remove(zhp->zfs_props,
3831 nvpair_name(curr), nvpair_type(curr));
3838 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3839 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3841 zfs_cmd_t zc = { 0 };
3842 nvlist_t *nvlist = NULL;
3845 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3846 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3847 zc.zc_cookie = (uint64_t)cmd;
3849 if (cmd == ZFS_SMB_ACL_RENAME) {
3850 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3851 (void) no_memory(hdl);
3857 case ZFS_SMB_ACL_ADD:
3858 case ZFS_SMB_ACL_REMOVE:
3859 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3861 case ZFS_SMB_ACL_RENAME:
3862 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3864 (void) no_memory(hdl);
3867 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3869 (void) no_memory(hdl);
3872 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3873 nvlist_free(nvlist);
3877 case ZFS_SMB_ACL_PURGE:
3882 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3884 nvlist_free(nvlist);
3889 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
3890 char *path, char *resource)
3892 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
3897 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
3898 char *path, char *resource)
3900 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
3905 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
3907 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
3912 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
3913 char *oldname, char *newname)
3915 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
3921 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
3922 zfs_userspace_cb_t func, void *arg)
3924 zfs_cmd_t zc = { 0 };
3926 zfs_useracct_t buf[100];
3928 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3930 zc.zc_objset_type = type;
3931 zc.zc_nvlist_dst = (uintptr_t)buf;
3935 zfs_useracct_t *zua = buf;
3937 zc.zc_nvlist_dst_size = sizeof (buf);
3938 error = ioctl(zhp->zfs_hdl->libzfs_fd,
3939 ZFS_IOC_USERSPACE_MANY, &zc);
3940 if (error || zc.zc_nvlist_dst_size == 0)
3943 while (zc.zc_nvlist_dst_size > 0) {
3944 error = func(arg, zua->zu_domain, zua->zu_rid,
3949 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
3957 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
3958 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
3959 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
3961 zfs_cmd_t zc = { 0 };
3962 libzfs_handle_t *hdl = zhp->zfs_hdl;
3964 ASSERT(!recursive || dsobj == 0);
3966 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3967 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3968 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3969 >= sizeof (zc.zc_string))
3970 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3971 zc.zc_cookie = recursive;
3972 zc.zc_temphold = temphold;
3973 zc.zc_cleanup_fd = cleanup_fd;
3974 zc.zc_sendobj = dsobj;
3975 zc.zc_createtxg = createtxg;
3977 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
3978 char errbuf[ZFS_MAXNAMELEN+32];
3981 * if it was recursive, the one that actually failed will be in
3984 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3985 "cannot hold '%s@%s'"), zc.zc_name, snapname);
3989 * Temporary tags wind up having the ds object id
3990 * prepended. So even if we passed the length check
3991 * above, it's still possible for the tag to wind
3992 * up being slightly too long.
3994 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
3996 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3997 "pool must be upgraded"));
3998 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4000 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4002 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4008 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4016 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4017 boolean_t recursive)
4019 zfs_cmd_t zc = { 0 };
4020 libzfs_handle_t *hdl = zhp->zfs_hdl;
4022 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4023 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4024 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4025 >= sizeof (zc.zc_string))
4026 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4027 zc.zc_cookie = recursive;
4029 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4030 char errbuf[ZFS_MAXNAMELEN+32];
4033 * if it was recursive, the one that actually failed will be in
4036 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4037 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4041 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4044 "pool must be upgraded"));
4045 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4047 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4049 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4057 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4059 zfs_cmd_t zc = { 0 };
4060 libzfs_handle_t *hdl = zhp->zfs_hdl;
4064 char errbuf[ZFS_MAXNAMELEN+32];
4066 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4067 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4071 nvbuf = malloc(nvsz);
4072 if (nvbuf == NULL) {
4073 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4077 zc.zc_nvlist_dst_size = nvsz;
4078 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4080 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4082 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) {
4083 (void) snprintf(errbuf, sizeof (errbuf),
4084 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4089 nvsz = zc.zc_nvlist_dst_size;
4093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4094 "pool must be upgraded"));
4095 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4098 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4101 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4104 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4109 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4111 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4112 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4114 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4124 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4126 zfs_cmd_t zc = { 0 };
4127 libzfs_handle_t *hdl = zhp->zfs_hdl;
4129 char errbuf[ZFS_MAXNAMELEN+32];
4133 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4134 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4136 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4139 nvbuf = malloc(nvsz);
4141 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4144 zc.zc_nvlist_src_size = nvsz;
4145 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4146 zc.zc_perm_action = un;
4148 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4150 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4151 (void) snprintf(errbuf, sizeof (errbuf),
4152 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4157 "pool must be upgraded"));
4158 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4161 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4164 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4167 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4178 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4180 zfs_cmd_t zc = { 0 };
4181 libzfs_handle_t *hdl = zhp->zfs_hdl;
4185 char errbuf[ZFS_MAXNAMELEN+32];
4187 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4191 nvbuf = malloc(nvsz);
4192 if (nvbuf == NULL) {
4193 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4197 zc.zc_nvlist_dst_size = nvsz;
4198 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4200 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4202 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4203 (void) snprintf(errbuf, sizeof (errbuf),
4204 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4209 nvsz = zc.zc_nvlist_dst_size;
4213 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4214 "pool must be upgraded"));
4215 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4218 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4221 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4224 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4229 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4231 (void) snprintf(errbuf, sizeof (errbuf),
4232 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4234 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4244 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4247 uint64_t nblocks, volblocksize;
4251 if (nvlist_lookup_string(props,
4252 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4253 ncopies = atoi(strval);
4256 if (nvlist_lookup_uint64(props,
4257 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4258 &volblocksize) != 0)
4259 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4260 nblocks = volsize/volblocksize;
4261 /* start with metadnode L0-L6 */
4263 /* calculate number of indirects */
4264 while (nblocks > 1) {
4265 nblocks += DNODES_PER_LEVEL - 1;
4266 nblocks /= DNODES_PER_LEVEL;
4269 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4272 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4273 * compressed, but in practice they compress down to about
4276 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4282 * Attach/detach the given filesystem to/from the given jail.
4285 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4287 libzfs_handle_t *hdl = zhp->zfs_hdl;
4288 zfs_cmd_t zc = { 0 };
4293 (void) snprintf(errbuf, sizeof (errbuf),
4294 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4296 (void) snprintf(errbuf, sizeof (errbuf),
4297 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4300 switch (zhp->zfs_type) {
4301 case ZFS_TYPE_VOLUME:
4302 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4303 "volumes can not be jailed"));
4304 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4305 case ZFS_TYPE_SNAPSHOT:
4306 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4307 "snapshots can not be jailed"));
4308 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4310 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4312 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4313 zc.zc_objset_type = DMU_OST_ZFS;
4314 zc.zc_jailid = jailid;
4316 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4317 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4318 zfs_standard_error(hdl, errno, errbuf);