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 2012 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
43 #include <sys/mntent.h>
44 #include <sys/mount.h>
51 #include <sys/dnode.h>
57 #include "zfs_namecheck.h"
59 #include "libzfs_impl.h"
60 #include "zfs_deleg.h"
62 static int userquota_propname_decode(const char *propname, boolean_t zoned,
63 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
66 * Given a single type (not a mask of types), return the type in a human
70 zfs_type_to_name(zfs_type_t type)
73 case ZFS_TYPE_FILESYSTEM:
74 return (dgettext(TEXT_DOMAIN, "filesystem"));
75 case ZFS_TYPE_SNAPSHOT:
76 return (dgettext(TEXT_DOMAIN, "snapshot"));
78 return (dgettext(TEXT_DOMAIN, "volume"));
85 * Given a path and mask of ZFS types, return a string describing this dataset.
86 * This is used when we fail to open a dataset and we cannot get an exact type.
87 * We guess what the type would have been based on the path and the mask of
91 path_to_str(const char *path, int types)
94 * When given a single type, always report the exact type.
96 if (types == ZFS_TYPE_SNAPSHOT)
97 return (dgettext(TEXT_DOMAIN, "snapshot"));
98 if (types == ZFS_TYPE_FILESYSTEM)
99 return (dgettext(TEXT_DOMAIN, "filesystem"));
100 if (types == ZFS_TYPE_VOLUME)
101 return (dgettext(TEXT_DOMAIN, "volume"));
104 * The user is requesting more than one type of dataset. If this is the
105 * case, consult the path itself. If we're looking for a snapshot, and
106 * a '@' is found, then report it as "snapshot". Otherwise, remove the
107 * snapshot attribute and try again.
109 if (types & ZFS_TYPE_SNAPSHOT) {
110 if (strchr(path, '@') != NULL)
111 return (dgettext(TEXT_DOMAIN, "snapshot"));
112 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
116 * The user has requested either filesystems or volumes.
117 * We have no way of knowing a priori what type this would be, so always
118 * report it as "filesystem" or "volume", our two primitive types.
120 if (types & ZFS_TYPE_FILESYSTEM)
121 return (dgettext(TEXT_DOMAIN, "filesystem"));
123 assert(types & ZFS_TYPE_VOLUME);
124 return (dgettext(TEXT_DOMAIN, "volume"));
128 * Validate a ZFS path. This is used even before trying to open the dataset, to
129 * provide a more meaningful error message. We call zfs_error_aux() to
130 * explain exactly why the name was not valid.
133 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
139 (void) zfs_prop_get_table();
140 if (dataset_namecheck(path, &why, &what) != 0) {
143 case NAME_ERR_TOOLONG:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "name is too long"));
148 case NAME_ERR_LEADING_SLASH:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "leading slash in name"));
153 case NAME_ERR_EMPTY_COMPONENT:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "empty component in name"));
158 case NAME_ERR_TRAILING_SLASH:
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "trailing slash in name"));
163 case NAME_ERR_INVALCHAR:
165 dgettext(TEXT_DOMAIN, "invalid character "
166 "'%c' in name"), what);
169 case NAME_ERR_MULTIPLE_AT:
170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 "multiple '@' delimiters in name"));
174 case NAME_ERR_NOLETTER:
175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176 "pool doesn't begin with a letter"));
179 case NAME_ERR_RESERVED:
180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
181 "name is reserved"));
184 case NAME_ERR_DISKLIKE:
185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
186 "reserved disk name"));
194 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
197 "snapshot delimiter '@' in filesystem name"));
201 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
204 "missing '@' delimiter in snapshot name"));
208 if (modifying && strchr(path, '%') != NULL) {
210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
211 "invalid character %c in name"), '%');
219 zfs_name_valid(const char *name, zfs_type_t type)
221 if (type == ZFS_TYPE_POOL)
222 return (zpool_name_valid(NULL, B_FALSE, name));
223 return (zfs_validate_name(NULL, name, type, B_FALSE));
227 * This function takes the raw DSL properties, and filters out the user-defined
228 * properties into a separate nvlist.
231 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
233 libzfs_handle_t *hdl = zhp->zfs_hdl;
238 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
239 (void) no_memory(hdl);
244 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
245 if (!zfs_prop_user(nvpair_name(elem)))
248 verify(nvpair_value_nvlist(elem, &propval) == 0);
249 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
251 (void) no_memory(hdl);
259 static zpool_handle_t *
260 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
262 libzfs_handle_t *hdl = zhp->zfs_hdl;
265 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
266 if (hdl->libzfs_pool_handles != NULL)
267 zph->zpool_next = hdl->libzfs_pool_handles;
268 hdl->libzfs_pool_handles = zph;
273 static zpool_handle_t *
274 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
276 libzfs_handle_t *hdl = zhp->zfs_hdl;
277 zpool_handle_t *zph = hdl->libzfs_pool_handles;
279 while ((zph != NULL) &&
280 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
281 zph = zph->zpool_next;
286 * Returns a handle to the pool that contains the provided dataset.
287 * If a handle to that pool already exists then that handle is returned.
288 * Otherwise, a new handle is created and added to the list of handles.
290 static zpool_handle_t *
291 zpool_handle(zfs_handle_t *zhp)
297 len = strcspn(zhp->zfs_name, "/@") + 1;
298 pool_name = zfs_alloc(zhp->zfs_hdl, len);
299 (void) strlcpy(pool_name, zhp->zfs_name, len);
301 zph = zpool_find_handle(zhp, pool_name, len);
303 zph = zpool_add_handle(zhp, pool_name);
310 zpool_free_handles(libzfs_handle_t *hdl)
312 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
314 while (zph != NULL) {
315 next = zph->zpool_next;
319 hdl->libzfs_pool_handles = NULL;
323 * Utility function to gather stats (objset and zpl) for the given object.
326 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
328 libzfs_handle_t *hdl = zhp->zfs_hdl;
330 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
332 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
333 if (errno == ENOMEM) {
334 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
345 * Utility function to get the received properties of the given object.
348 get_recvd_props_ioctl(zfs_handle_t *zhp)
350 libzfs_handle_t *hdl = zhp->zfs_hdl;
351 nvlist_t *recvdprops;
352 zfs_cmd_t zc = { 0 };
355 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
358 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
360 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
361 if (errno == ENOMEM) {
362 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
366 zcmd_free_nvlists(&zc);
371 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
372 zcmd_free_nvlists(&zc);
376 nvlist_free(zhp->zfs_recvd_props);
377 zhp->zfs_recvd_props = recvdprops;
383 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
385 nvlist_t *allprops, *userprops;
387 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
389 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
394 * XXX Why do we store the user props separately, in addition to
395 * storing them in zfs_props?
397 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
398 nvlist_free(allprops);
402 nvlist_free(zhp->zfs_props);
403 nvlist_free(zhp->zfs_user_props);
405 zhp->zfs_props = allprops;
406 zhp->zfs_user_props = userprops;
412 get_stats(zfs_handle_t *zhp)
415 zfs_cmd_t zc = { 0 };
417 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
419 if (get_stats_ioctl(zhp, &zc) != 0)
421 else if (put_stats_zhdl(zhp, &zc) != 0)
423 zcmd_free_nvlists(&zc);
428 * Refresh the properties currently stored in the handle.
431 zfs_refresh_properties(zfs_handle_t *zhp)
433 (void) get_stats(zhp);
437 * Makes a handle from the given dataset name. Used by zfs_open() and
438 * zfs_iter_* to create child handles on the fly.
441 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
443 if (put_stats_zhdl(zhp, zc) != 0)
447 * We've managed to open the dataset and gather statistics. Determine
448 * the high-level type.
450 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
451 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
452 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
453 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
457 if (zhp->zfs_dmustats.dds_is_snapshot)
458 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
459 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
460 zhp->zfs_type = ZFS_TYPE_VOLUME;
461 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
462 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
464 abort(); /* we should never see any other types */
466 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
473 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
475 zfs_cmd_t zc = { 0 };
477 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
483 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
484 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
488 if (get_stats_ioctl(zhp, &zc) == -1) {
489 zcmd_free_nvlists(&zc);
493 if (make_dataset_handle_common(zhp, &zc) == -1) {
497 zcmd_free_nvlists(&zc);
502 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
504 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
510 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
511 if (make_dataset_handle_common(zhp, zc) == -1) {
519 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
521 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
526 zhp->zfs_hdl = pzhp->zfs_hdl;
527 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
528 zhp->zfs_head_type = pzhp->zfs_type;
529 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
530 zhp->zpool_hdl = zpool_handle(zhp);
535 zfs_handle_dup(zfs_handle_t *zhp_orig)
537 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
542 zhp->zfs_hdl = zhp_orig->zfs_hdl;
543 zhp->zpool_hdl = zhp_orig->zpool_hdl;
544 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
545 sizeof (zhp->zfs_name));
546 zhp->zfs_type = zhp_orig->zfs_type;
547 zhp->zfs_head_type = zhp_orig->zfs_head_type;
548 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
549 if (zhp_orig->zfs_props != NULL) {
550 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
551 (void) no_memory(zhp->zfs_hdl);
556 if (zhp_orig->zfs_user_props != NULL) {
557 if (nvlist_dup(zhp_orig->zfs_user_props,
558 &zhp->zfs_user_props, 0) != 0) {
559 (void) no_memory(zhp->zfs_hdl);
564 if (zhp_orig->zfs_recvd_props != NULL) {
565 if (nvlist_dup(zhp_orig->zfs_recvd_props,
566 &zhp->zfs_recvd_props, 0)) {
567 (void) no_memory(zhp->zfs_hdl);
572 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
573 if (zhp_orig->zfs_mntopts != NULL) {
574 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
575 zhp_orig->zfs_mntopts);
577 zhp->zfs_props_table = zhp_orig->zfs_props_table;
582 * Opens the given snapshot, filesystem, or volume. The 'types'
583 * argument is a mask of acceptable types. The function will print an
584 * appropriate error message and return NULL if it can't be opened.
587 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
592 (void) snprintf(errbuf, sizeof (errbuf),
593 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
596 * Validate the name before we even try to open it.
598 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
600 "invalid dataset name"));
601 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
606 * Try to get stats for the dataset, which will tell us if it exists.
609 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
610 (void) zfs_standard_error(hdl, errno, errbuf);
615 char *at = strchr(path, '@');
620 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
621 (void) zfs_standard_error(hdl, errno, errbuf);
626 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
627 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
630 if (!(types & zhp->zfs_type)) {
631 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
640 * Release a ZFS handle. Nothing to do but free the associated memory.
643 zfs_close(zfs_handle_t *zhp)
645 if (zhp->zfs_mntopts)
646 free(zhp->zfs_mntopts);
647 nvlist_free(zhp->zfs_props);
648 nvlist_free(zhp->zfs_user_props);
649 nvlist_free(zhp->zfs_recvd_props);
653 typedef struct mnttab_node {
654 struct mnttab mtn_mt;
659 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
661 const mnttab_node_t *mtn1 = arg1;
662 const mnttab_node_t *mtn2 = arg2;
665 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
669 return (rv > 0 ? 1 : -1);
673 libzfs_mnttab_init(libzfs_handle_t *hdl)
675 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
676 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
677 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
681 libzfs_mnttab_update(libzfs_handle_t *hdl)
685 rewind(hdl->libzfs_mnttab);
686 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
689 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
691 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
692 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
693 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
694 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
695 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
696 avl_add(&hdl->libzfs_mnttab_cache, mtn);
701 libzfs_mnttab_fini(libzfs_handle_t *hdl)
706 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
707 free(mtn->mtn_mt.mnt_special);
708 free(mtn->mtn_mt.mnt_mountp);
709 free(mtn->mtn_mt.mnt_fstype);
710 free(mtn->mtn_mt.mnt_mntopts);
713 avl_destroy(&hdl->libzfs_mnttab_cache);
717 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
719 hdl->libzfs_mnttab_enable = enable;
723 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
724 struct mnttab *entry)
729 if (!hdl->libzfs_mnttab_enable) {
730 struct mnttab srch = { 0 };
732 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
733 libzfs_mnttab_fini(hdl);
734 rewind(hdl->libzfs_mnttab);
735 srch.mnt_special = (char *)fsname;
736 srch.mnt_fstype = MNTTYPE_ZFS;
737 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
743 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
744 libzfs_mnttab_update(hdl);
746 find.mtn_mt.mnt_special = (char *)fsname;
747 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
749 *entry = mtn->mtn_mt;
756 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
757 const char *mountp, const char *mntopts)
761 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
763 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
764 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
765 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
766 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
767 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
768 avl_add(&hdl->libzfs_mnttab_cache, mtn);
772 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
777 find.mtn_mt.mnt_special = (char *)fsname;
778 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
779 avl_remove(&hdl->libzfs_mnttab_cache, ret);
780 free(ret->mtn_mt.mnt_special);
781 free(ret->mtn_mt.mnt_mountp);
782 free(ret->mtn_mt.mnt_fstype);
783 free(ret->mtn_mt.mnt_mntopts);
789 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
791 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
793 if (zpool_handle == NULL)
796 *spa_version = zpool_get_prop_int(zpool_handle,
797 ZPOOL_PROP_VERSION, NULL);
802 * The choice of reservation property depends on the SPA version.
805 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
809 if (zfs_spa_version(zhp, &spa_version) < 0)
812 if (spa_version >= SPA_VERSION_REFRESERVATION)
813 *resv_prop = ZFS_PROP_REFRESERVATION;
815 *resv_prop = ZFS_PROP_RESERVATION;
821 * Given an nvlist of properties to set, validates that they are correct, and
822 * parses any numeric properties (index, boolean, etc) if they are specified as
826 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
827 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
834 int chosen_normal = -1;
837 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
838 (void) no_memory(hdl);
843 * Make sure this property is valid and applies to this type.
847 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
848 const char *propname = nvpair_name(elem);
850 prop = zfs_name_to_prop(propname);
851 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
853 * This is a user property: make sure it's a
854 * string, and that it's less than ZAP_MAXNAMELEN.
856 if (nvpair_type(elem) != DATA_TYPE_STRING) {
857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
858 "'%s' must be a string"), propname);
859 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
863 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
865 "property name '%s' is too long"),
867 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
871 (void) nvpair_value_string(elem, &strval);
872 if (nvlist_add_string(ret, propname, strval) != 0) {
873 (void) no_memory(hdl);
880 * Currently, only user properties can be modified on
883 if (type == ZFS_TYPE_SNAPSHOT) {
884 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
885 "this property can not be modified for snapshots"));
886 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
890 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
891 zfs_userquota_prop_t uqtype;
892 char newpropname[128];
897 if (userquota_propname_decode(propname, zoned,
898 &uqtype, domain, sizeof (domain), &rid) != 0) {
900 dgettext(TEXT_DOMAIN,
901 "'%s' has an invalid user/group name"),
903 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
907 if (uqtype != ZFS_PROP_USERQUOTA &&
908 uqtype != ZFS_PROP_GROUPQUOTA) {
910 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
912 (void) zfs_error(hdl, EZFS_PROPREADONLY,
917 if (nvpair_type(elem) == DATA_TYPE_STRING) {
918 (void) nvpair_value_string(elem, &strval);
919 if (strcmp(strval, "none") == 0) {
921 } else if (zfs_nicestrtonum(hdl,
922 strval, &intval) != 0) {
923 (void) zfs_error(hdl,
924 EZFS_BADPROP, errbuf);
927 } else if (nvpair_type(elem) ==
929 (void) nvpair_value_uint64(elem, &intval);
931 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
932 "use 'none' to disable "
933 "userquota/groupquota"));
937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
938 "'%s' must be a number"), propname);
939 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
944 * Encode the prop name as
945 * userquota@<hex-rid>-domain, to make it easy
946 * for the kernel to decode.
948 (void) snprintf(newpropname, sizeof (newpropname),
949 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
950 (longlong_t)rid, domain);
954 if (nvlist_add_uint64_array(ret, newpropname,
956 (void) no_memory(hdl);
960 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
961 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
968 if (prop == ZPROP_INVAL) {
969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
970 "invalid property '%s'"), propname);
971 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
975 if (!zfs_prop_valid_for_type(prop, type)) {
977 dgettext(TEXT_DOMAIN, "'%s' does not "
978 "apply to datasets of this type"), propname);
979 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
983 if (zfs_prop_readonly(prop) &&
984 (!zfs_prop_setonce(prop) || zhp != NULL)) {
986 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
988 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
992 if (zprop_parse_value(hdl, elem, prop, type, ret,
993 &strval, &intval, errbuf) != 0)
997 * Perform some additional checks for specific properties.
1000 case ZFS_PROP_VERSION:
1006 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1007 if (intval < version) {
1008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1009 "Can not downgrade; already at version %u"),
1011 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1017 case ZFS_PROP_RECORDSIZE:
1018 case ZFS_PROP_VOLBLOCKSIZE:
1019 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
1020 if (intval < SPA_MINBLOCKSIZE ||
1021 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
1022 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1023 "'%s' must be power of 2 from %u "
1024 "to %uk"), propname,
1025 (uint_t)SPA_MINBLOCKSIZE,
1026 (uint_t)SPA_MAXBLOCKSIZE >> 10);
1027 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1032 case ZFS_PROP_MLSLABEL:
1036 * Verify the mlslabel string and convert to
1037 * internal hex label string.
1041 char *hex = NULL; /* internal label string */
1043 /* Default value is already OK. */
1044 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1047 /* Verify the label can be converted to binary form */
1048 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1049 (str_to_label(strval, &new_sl, MAC_LABEL,
1050 L_NO_CORRECTION, NULL) == -1)) {
1054 /* Now translate to hex internal label string */
1055 if (label_to_str(new_sl, &hex, M_INTERNAL,
1061 m_label_free(new_sl);
1063 /* If string is already in internal form, we're done. */
1064 if (strcmp(strval, hex) == 0) {
1069 /* Replace the label string with the internal form. */
1070 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1072 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1080 "invalid mlslabel '%s'"), strval);
1081 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1082 m_label_free(new_sl); /* OK if null */
1084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1085 "mlslabel is not supported on FreeBSD"));
1086 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1092 case ZFS_PROP_MOUNTPOINT:
1094 namecheck_err_t why;
1096 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1097 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1100 if (mountpoint_namecheck(strval, &why)) {
1102 case NAME_ERR_LEADING_SLASH:
1104 dgettext(TEXT_DOMAIN,
1105 "'%s' must be an absolute path, "
1106 "'none', or 'legacy'"), propname);
1108 case NAME_ERR_TOOLONG:
1110 dgettext(TEXT_DOMAIN,
1111 "component of '%s' is too long"),
1115 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1122 case ZFS_PROP_SHARESMB:
1123 case ZFS_PROP_SHARENFS:
1125 * For the mountpoint and sharenfs or sharesmb
1126 * properties, check if it can be set in a
1127 * global/non-global zone based on
1128 * the zoned property value:
1130 * global zone non-global zone
1131 * --------------------------------------------------
1132 * zoned=on mountpoint (no) mountpoint (yes)
1133 * sharenfs (no) sharenfs (no)
1134 * sharesmb (no) sharesmb (no)
1136 * zoned=off mountpoint (yes) N/A
1141 if (getzoneid() == GLOBAL_ZONEID) {
1142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1143 "'%s' cannot be set on "
1144 "dataset in a non-global zone"),
1146 (void) zfs_error(hdl, EZFS_ZONED,
1149 } else if (prop == ZFS_PROP_SHARENFS ||
1150 prop == ZFS_PROP_SHARESMB) {
1151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1152 "'%s' cannot be set in "
1153 "a non-global zone"), propname);
1154 (void) zfs_error(hdl, EZFS_ZONED,
1158 } else if (getzoneid() != GLOBAL_ZONEID) {
1160 * If zoned property is 'off', this must be in
1161 * a global zone. If not, something is wrong.
1163 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1164 "'%s' cannot be set while dataset "
1165 "'zoned' property is set"), propname);
1166 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1171 * At this point, it is legitimate to set the
1172 * property. Now we want to make sure that the
1173 * property value is valid if it is sharenfs.
1175 if ((prop == ZFS_PROP_SHARENFS ||
1176 prop == ZFS_PROP_SHARESMB) &&
1177 strcmp(strval, "on") != 0 &&
1178 strcmp(strval, "off") != 0) {
1179 zfs_share_proto_t proto;
1181 if (prop == ZFS_PROP_SHARESMB)
1187 * Must be an valid sharing protocol
1188 * option string so init the libshare
1189 * in order to enable the parser and
1190 * then parse the options. We use the
1191 * control API since we don't care about
1192 * the current configuration and don't
1193 * want the overhead of loading it
1194 * until we actually do something.
1197 if (zfs_init_libshare(hdl,
1198 SA_INIT_CONTROL_API) != SA_OK) {
1200 * An error occurred so we can't do
1203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1204 "'%s' cannot be set: problem "
1205 "in share initialization"),
1207 (void) zfs_error(hdl, EZFS_BADPROP,
1212 if (zfs_parse_options(strval, proto) != SA_OK) {
1214 * There was an error in parsing so
1215 * deal with it by issuing an error
1216 * message and leaving after
1217 * uninitializing the the libshare
1220 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1221 "'%s' cannot be set to invalid "
1222 "options"), propname);
1223 (void) zfs_error(hdl, EZFS_BADPROP,
1225 zfs_uninit_libshare(hdl);
1228 zfs_uninit_libshare(hdl);
1232 case ZFS_PROP_UTF8ONLY:
1233 chosen_utf = (int)intval;
1235 case ZFS_PROP_NORMALIZE:
1236 chosen_normal = (int)intval;
1241 * For changes to existing volumes, we have some additional
1242 * checks to enforce.
1244 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1245 uint64_t volsize = zfs_prop_get_int(zhp,
1247 uint64_t blocksize = zfs_prop_get_int(zhp,
1248 ZFS_PROP_VOLBLOCKSIZE);
1252 case ZFS_PROP_RESERVATION:
1253 case ZFS_PROP_REFRESERVATION:
1254 if (intval > volsize) {
1255 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1256 "'%s' is greater than current "
1257 "volume size"), propname);
1258 (void) zfs_error(hdl, EZFS_BADPROP,
1264 case ZFS_PROP_VOLSIZE:
1265 if (intval % blocksize != 0) {
1266 zfs_nicenum(blocksize, buf,
1268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1269 "'%s' must be a multiple of "
1270 "volume block size (%s)"),
1272 (void) zfs_error(hdl, EZFS_BADPROP,
1278 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1279 "'%s' cannot be zero"),
1281 (void) zfs_error(hdl, EZFS_BADPROP,
1291 * If normalization was chosen, but no UTF8 choice was made,
1292 * enforce rejection of non-UTF8 names.
1294 * If normalization was chosen, but rejecting non-UTF8 names
1295 * was explicitly not chosen, it is an error.
1297 if (chosen_normal > 0 && chosen_utf < 0) {
1298 if (nvlist_add_uint64(ret,
1299 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1300 (void) no_memory(hdl);
1303 } else if (chosen_normal > 0 && chosen_utf == 0) {
1304 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1305 "'%s' must be set 'on' if normalization chosen"),
1306 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1307 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1318 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1320 uint64_t old_volsize;
1321 uint64_t new_volsize;
1322 uint64_t old_reservation;
1323 uint64_t new_reservation;
1324 zfs_prop_t resv_prop;
1327 * If this is an existing volume, and someone is setting the volsize,
1328 * make sure that it matches the reservation, or add it if necessary.
1330 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1331 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1333 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1334 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1335 old_reservation) || nvlist_lookup_uint64(nvl,
1336 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1339 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1342 new_reservation = zvol_volsize_to_reservation(new_volsize,
1344 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1345 new_reservation) != 0) {
1346 (void) no_memory(zhp->zfs_hdl);
1353 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1360 * For quotas and reservations, ENOSPC indicates
1361 * something different; setting a quota or reservation
1362 * doesn't use any disk space.
1365 case ZFS_PROP_QUOTA:
1366 case ZFS_PROP_REFQUOTA:
1367 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1368 "size is less than current used or "
1370 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1373 case ZFS_PROP_RESERVATION:
1374 case ZFS_PROP_REFRESERVATION:
1375 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1376 "size is greater than available space"));
1377 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1381 (void) zfs_standard_error(hdl, err, errbuf);
1387 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1391 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1396 "pool and or dataset must be upgraded to set this "
1397 "property or value"));
1398 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1402 if (prop == ZFS_PROP_COMPRESSION) {
1403 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1404 "property setting is not allowed on "
1405 "bootable datasets"));
1406 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1408 (void) zfs_standard_error(hdl, err, errbuf);
1413 if (prop == ZPROP_INVAL) {
1414 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1416 (void) zfs_standard_error(hdl, err, errbuf);
1422 * This platform can't address a volume this big.
1425 if (prop == ZFS_PROP_VOLSIZE) {
1426 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1432 (void) zfs_standard_error(hdl, err, errbuf);
1437 * Given a property name and value, set the property for the given dataset.
1440 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1442 zfs_cmd_t zc = { 0 };
1444 prop_changelist_t *cl = NULL;
1446 libzfs_handle_t *hdl = zhp->zfs_hdl;
1447 nvlist_t *nvl = NULL, *realprops;
1449 boolean_t do_prefix = B_TRUE;
1452 (void) snprintf(errbuf, sizeof (errbuf),
1453 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1456 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1457 nvlist_add_string(nvl, propname, propval) != 0) {
1458 (void) no_memory(hdl);
1462 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1463 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1469 prop = zfs_name_to_prop(propname);
1471 /* We don't support those properties on FreeBSD. */
1473 case ZFS_PROP_DEVICES:
1474 case ZFS_PROP_ISCSIOPTIONS:
1475 case ZFS_PROP_XATTR:
1476 case ZFS_PROP_VSCAN:
1477 case ZFS_PROP_NBMAND:
1478 case ZFS_PROP_MLSLABEL:
1479 (void) snprintf(errbuf, sizeof (errbuf),
1480 "property '%s' not supported on FreeBSD", propname);
1481 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1485 if (prop == ZFS_PROP_VOLSIZE) {
1486 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1490 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1493 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1495 "child dataset with inherited mountpoint is used "
1496 "in a non-global zone"));
1497 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1502 * We don't want to unmount & remount the dataset when changing
1503 * its canmount property to 'on' or 'noauto'. We only use
1504 * the changelist logic to unmount when setting canmount=off.
1506 if (prop == ZFS_PROP_CANMOUNT) {
1508 int err = zprop_string_to_index(prop, propval, &idx,
1510 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1511 do_prefix = B_FALSE;
1514 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1518 * Execute the corresponding ioctl() to set this property.
1520 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1522 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1525 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1528 zfs_setprop_error(hdl, prop, errno, errbuf);
1529 if (added_resv && errno == ENOSPC) {
1530 /* clean up the volsize property we tried to set */
1531 uint64_t old_volsize = zfs_prop_get_int(zhp,
1534 zcmd_free_nvlists(&zc);
1535 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1537 if (nvlist_add_uint64(nvl,
1538 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1541 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1543 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1547 ret = changelist_postfix(cl);
1550 * Refresh the statistics so the new property value
1554 (void) get_stats(zhp);
1559 zcmd_free_nvlists(&zc);
1561 changelist_free(cl);
1566 * Given a property, inherit the value from the parent dataset, or if received
1567 * is TRUE, revert to the received value, if any.
1570 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1572 zfs_cmd_t zc = { 0 };
1574 prop_changelist_t *cl;
1575 libzfs_handle_t *hdl = zhp->zfs_hdl;
1579 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1580 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1582 zc.zc_cookie = received;
1583 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1585 * For user properties, the amount of work we have to do is very
1586 * small, so just do it here.
1588 if (!zfs_prop_user(propname)) {
1589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1590 "invalid property"));
1591 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1594 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1595 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1597 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1598 return (zfs_standard_error(hdl, errno, errbuf));
1604 * Verify that this property is inheritable.
1606 if (zfs_prop_readonly(prop))
1607 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1609 if (!zfs_prop_inheritable(prop) && !received)
1610 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1613 * Check to see if the value applies to this type
1615 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1616 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1619 * Normalize the name, to get rid of shorthand abbreviations.
1621 propname = zfs_prop_to_name(prop);
1622 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1623 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1625 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1626 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1628 "dataset is used in a non-global zone"));
1629 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1633 * Determine datasets which will be affected by this change, if any.
1635 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1638 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1640 "child dataset with inherited mountpoint is used "
1641 "in a non-global zone"));
1642 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1646 if ((ret = changelist_prefix(cl)) != 0)
1649 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1650 return (zfs_standard_error(hdl, errno, errbuf));
1653 if ((ret = changelist_postfix(cl)) != 0)
1657 * Refresh the statistics so the new property is reflected.
1659 (void) get_stats(zhp);
1663 changelist_free(cl);
1668 * True DSL properties are stored in an nvlist. The following two functions
1669 * extract them appropriately.
1672 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1678 if (nvlist_lookup_nvlist(zhp->zfs_props,
1679 zfs_prop_to_name(prop), &nv) == 0) {
1680 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1681 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1683 verify(!zhp->zfs_props_table ||
1684 zhp->zfs_props_table[prop] == B_TRUE);
1685 value = zfs_prop_default_numeric(prop);
1693 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1699 if (nvlist_lookup_nvlist(zhp->zfs_props,
1700 zfs_prop_to_name(prop), &nv) == 0) {
1701 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1702 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1704 verify(!zhp->zfs_props_table ||
1705 zhp->zfs_props_table[prop] == B_TRUE);
1706 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1715 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1717 return (zhp->zfs_props == zhp->zfs_recvd_props);
1721 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1723 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1724 zhp->zfs_props = zhp->zfs_recvd_props;
1728 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1730 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1735 * Internal function for getting a numeric property. Both zfs_prop_get() and
1736 * zfs_prop_get_int() are built using this interface.
1738 * Certain properties can be overridden using 'mount -o'. In this case, scan
1739 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1740 * If they differ from the on-disk values, report the current values and mark
1741 * the source "temporary".
1744 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1745 char **source, uint64_t *val)
1747 zfs_cmd_t zc = { 0 };
1748 nvlist_t *zplprops = NULL;
1750 char *mntopt_on = NULL;
1751 char *mntopt_off = NULL;
1752 boolean_t received = zfs_is_recvd_props_mode(zhp);
1757 case ZFS_PROP_ATIME:
1758 mntopt_on = MNTOPT_ATIME;
1759 mntopt_off = MNTOPT_NOATIME;
1762 case ZFS_PROP_DEVICES:
1763 mntopt_on = MNTOPT_DEVICES;
1764 mntopt_off = MNTOPT_NODEVICES;
1768 mntopt_on = MNTOPT_EXEC;
1769 mntopt_off = MNTOPT_NOEXEC;
1772 case ZFS_PROP_READONLY:
1773 mntopt_on = MNTOPT_RO;
1774 mntopt_off = MNTOPT_RW;
1777 case ZFS_PROP_SETUID:
1778 mntopt_on = MNTOPT_SETUID;
1779 mntopt_off = MNTOPT_NOSETUID;
1782 case ZFS_PROP_XATTR:
1783 mntopt_on = MNTOPT_XATTR;
1784 mntopt_off = MNTOPT_NOXATTR;
1787 case ZFS_PROP_NBMAND:
1788 mntopt_on = MNTOPT_NBMAND;
1789 mntopt_off = MNTOPT_NONBMAND;
1794 * Because looking up the mount options is potentially expensive
1795 * (iterating over all of /etc/mnttab), we defer its calculation until
1796 * we're looking up a property which requires its presence.
1798 if (!zhp->zfs_mntcheck &&
1799 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1800 libzfs_handle_t *hdl = zhp->zfs_hdl;
1801 struct mnttab entry;
1803 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1804 zhp->zfs_mntopts = zfs_strdup(hdl,
1806 if (zhp->zfs_mntopts == NULL)
1810 zhp->zfs_mntcheck = B_TRUE;
1813 if (zhp->zfs_mntopts == NULL)
1814 mnt.mnt_mntopts = "";
1816 mnt.mnt_mntopts = zhp->zfs_mntopts;
1819 case ZFS_PROP_ATIME:
1820 case ZFS_PROP_DEVICES:
1822 case ZFS_PROP_READONLY:
1823 case ZFS_PROP_SETUID:
1824 case ZFS_PROP_XATTR:
1825 case ZFS_PROP_NBMAND:
1826 *val = getprop_uint64(zhp, prop, source);
1831 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1834 *src = ZPROP_SRC_TEMPORARY;
1835 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1838 *src = ZPROP_SRC_TEMPORARY;
1842 case ZFS_PROP_CANMOUNT:
1843 case ZFS_PROP_VOLSIZE:
1844 case ZFS_PROP_QUOTA:
1845 case ZFS_PROP_REFQUOTA:
1846 case ZFS_PROP_RESERVATION:
1847 case ZFS_PROP_REFRESERVATION:
1848 *val = getprop_uint64(zhp, prop, source);
1850 if (*source == NULL) {
1851 /* not default, must be local */
1852 *source = zhp->zfs_name;
1856 case ZFS_PROP_MOUNTED:
1857 *val = (zhp->zfs_mntopts != NULL);
1860 case ZFS_PROP_NUMCLONES:
1861 *val = zhp->zfs_dmustats.dds_num_clones;
1864 case ZFS_PROP_VERSION:
1865 case ZFS_PROP_NORMALIZE:
1866 case ZFS_PROP_UTF8ONLY:
1868 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1869 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1871 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1872 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1873 zcmd_free_nvlists(&zc);
1876 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1877 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1879 zcmd_free_nvlists(&zc);
1883 nvlist_free(zplprops);
1884 zcmd_free_nvlists(&zc);
1888 switch (zfs_prop_get_type(prop)) {
1889 case PROP_TYPE_NUMBER:
1890 case PROP_TYPE_INDEX:
1891 *val = getprop_uint64(zhp, prop, source);
1893 * If we tried to use a default value for a
1894 * readonly property, it means that it was not
1897 if (zfs_prop_readonly(prop) &&
1898 *source != NULL && (*source)[0] == '\0') {
1903 case PROP_TYPE_STRING:
1905 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1906 "cannot get non-numeric property"));
1907 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1908 dgettext(TEXT_DOMAIN, "internal error")));
1916 * Calculate the source type, given the raw source string.
1919 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1920 char *statbuf, size_t statlen)
1922 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1925 if (source == NULL) {
1926 *srctype = ZPROP_SRC_NONE;
1927 } else if (source[0] == '\0') {
1928 *srctype = ZPROP_SRC_DEFAULT;
1929 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1930 *srctype = ZPROP_SRC_RECEIVED;
1932 if (strcmp(source, zhp->zfs_name) == 0) {
1933 *srctype = ZPROP_SRC_LOCAL;
1935 (void) strlcpy(statbuf, source, statlen);
1936 *srctype = ZPROP_SRC_INHERITED;
1943 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1944 size_t proplen, boolean_t literal)
1949 if (zhp->zfs_recvd_props == NULL)
1950 if (get_recvd_props_ioctl(zhp) != 0)
1953 prop = zfs_name_to_prop(propname);
1955 if (prop != ZPROP_INVAL) {
1957 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1959 zfs_set_recvd_props_mode(zhp, &cookie);
1960 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1961 NULL, NULL, 0, literal);
1962 zfs_unset_recvd_props_mode(zhp, &cookie);
1966 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1967 propname, &propval) != 0)
1969 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1971 (void) strlcpy(propbuf, recvdval, proplen);
1974 return (err == 0 ? 0 : -1);
1978 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1983 value = zfs_get_clones_nvl(zhp);
1988 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1989 pair = nvlist_next_nvpair(value, pair)) {
1990 if (propbuf[0] != '\0')
1991 (void) strlcat(propbuf, ",", proplen);
1992 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1998 struct get_clones_arg {
2002 char buf[ZFS_MAXNAMELEN];
2006 get_clones_cb(zfs_handle_t *zhp, void *arg)
2008 struct get_clones_arg *gca = arg;
2010 if (gca->numclones == 0) {
2015 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2016 NULL, NULL, 0, B_TRUE) != 0)
2018 if (strcmp(gca->buf, gca->origin) == 0) {
2019 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2024 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2030 zfs_get_clones_nvl(zfs_handle_t *zhp)
2032 nvlist_t *nv, *value;
2034 if (nvlist_lookup_nvlist(zhp->zfs_props,
2035 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2036 struct get_clones_arg gca;
2039 * if this is a snapshot, then the kernel wasn't able
2040 * to get the clones. Do it by slowly iterating.
2042 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2044 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2046 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2051 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2053 gca.origin = zhp->zfs_name;
2055 if (gca.numclones != 0) {
2057 char pool[ZFS_MAXNAMELEN];
2060 /* get the pool name */
2061 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2062 (void) strsep(&cp, "/@");
2063 root = zfs_open(zhp->zfs_hdl, pool,
2064 ZFS_TYPE_FILESYSTEM);
2066 (void) get_clones_cb(root, &gca);
2069 if (gca.numclones != 0 ||
2070 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2071 nvlist_add_nvlist(zhp->zfs_props,
2072 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2079 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2080 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2083 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2089 * Retrieve a property from the given object. If 'literal' is specified, then
2090 * numbers are left as exact values. Otherwise, numbers are converted to a
2091 * human-readable form.
2093 * Returns 0 on success, or -1 on error.
2096 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2097 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2099 char *source = NULL;
2103 boolean_t received = zfs_is_recvd_props_mode(zhp);
2106 * Check to see if this property applies to our object
2108 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2111 if (received && zfs_prop_readonly(prop))
2115 *src = ZPROP_SRC_NONE;
2118 case ZFS_PROP_CREATION:
2120 * 'creation' is a time_t stored in the statistics. We convert
2121 * this into a string unless 'literal' is specified.
2124 val = getprop_uint64(zhp, prop, &source);
2125 time_t time = (time_t)val;
2129 localtime_r(&time, &t) == NULL ||
2130 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2132 (void) snprintf(propbuf, proplen, "%llu", val);
2136 case ZFS_PROP_MOUNTPOINT:
2138 * Getting the precise mountpoint can be tricky.
2140 * - for 'none' or 'legacy', return those values.
2141 * - for inherited mountpoints, we want to take everything
2142 * after our ancestor and append it to the inherited value.
2144 * If the pool has an alternate root, we want to prepend that
2145 * root to any values we return.
2148 str = getprop_string(zhp, prop, &source);
2150 if (str[0] == '/') {
2151 char buf[MAXPATHLEN];
2153 const char *relpath;
2156 * If we inherit the mountpoint, even from a dataset
2157 * with a received value, the source will be the path of
2158 * the dataset we inherit from. If source is
2159 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2162 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2165 relpath = zhp->zfs_name + strlen(source);
2166 if (relpath[0] == '/')
2170 if ((zpool_get_prop(zhp->zpool_hdl,
2171 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2172 (strcmp(root, "-") == 0))
2175 * Special case an alternate root of '/'. This will
2176 * avoid having multiple leading slashes in the
2179 if (strcmp(root, "/") == 0)
2183 * If the mountpoint is '/' then skip over this
2184 * if we are obtaining either an alternate root or
2185 * an inherited mountpoint.
2187 if (str[1] == '\0' && (root[0] != '\0' ||
2188 relpath[0] != '\0'))
2191 if (relpath[0] == '\0')
2192 (void) snprintf(propbuf, proplen, "%s%s",
2195 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2196 root, str, relpath[0] == '@' ? "" : "/",
2199 /* 'legacy' or 'none' */
2200 (void) strlcpy(propbuf, str, proplen);
2205 case ZFS_PROP_ORIGIN:
2206 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2209 * If there is no parent at all, return failure to indicate that
2210 * it doesn't apply to this dataset.
2212 if (propbuf[0] == '\0')
2216 case ZFS_PROP_CLONES:
2217 if (get_clones_string(zhp, propbuf, proplen) != 0)
2221 case ZFS_PROP_QUOTA:
2222 case ZFS_PROP_REFQUOTA:
2223 case ZFS_PROP_RESERVATION:
2224 case ZFS_PROP_REFRESERVATION:
2226 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2230 * If quota or reservation is 0, we translate this into 'none'
2231 * (unless literal is set), and indicate that it's the default
2232 * value. Otherwise, we print the number nicely and indicate
2233 * that its set locally.
2237 (void) strlcpy(propbuf, "0", proplen);
2239 (void) strlcpy(propbuf, "none", proplen);
2242 (void) snprintf(propbuf, proplen, "%llu",
2245 zfs_nicenum(val, propbuf, proplen);
2249 case ZFS_PROP_REFRATIO:
2250 case ZFS_PROP_COMPRESSRATIO:
2251 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2253 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2254 (u_longlong_t)(val / 100),
2255 (u_longlong_t)(val % 100));
2259 switch (zhp->zfs_type) {
2260 case ZFS_TYPE_FILESYSTEM:
2263 case ZFS_TYPE_VOLUME:
2266 case ZFS_TYPE_SNAPSHOT:
2272 (void) snprintf(propbuf, proplen, "%s", str);
2275 case ZFS_PROP_MOUNTED:
2277 * The 'mounted' property is a pseudo-property that described
2278 * whether the filesystem is currently mounted. Even though
2279 * it's a boolean value, the typical values of "on" and "off"
2280 * don't make sense, so we translate to "yes" and "no".
2282 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2283 src, &source, &val) != 0)
2286 (void) strlcpy(propbuf, "yes", proplen);
2288 (void) strlcpy(propbuf, "no", proplen);
2293 * The 'name' property is a pseudo-property derived from the
2294 * dataset name. It is presented as a real property to simplify
2297 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2300 case ZFS_PROP_MLSLABEL:
2303 m_label_t *new_sl = NULL;
2304 char *ascii = NULL; /* human readable label */
2306 (void) strlcpy(propbuf,
2307 getprop_string(zhp, prop, &source), proplen);
2309 if (literal || (strcasecmp(propbuf,
2310 ZFS_MLSLABEL_DEFAULT) == 0))
2314 * Try to translate the internal hex string to
2315 * human-readable output. If there are any
2316 * problems just use the hex string.
2319 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2320 L_NO_CORRECTION, NULL) == -1) {
2321 m_label_free(new_sl);
2325 if (label_to_str(new_sl, &ascii, M_LABEL,
2329 m_label_free(new_sl);
2332 m_label_free(new_sl);
2334 (void) strlcpy(propbuf, ascii, proplen);
2344 * GUIDs are stored as numbers, but they are identifiers.
2345 * We don't want them to be pretty printed, because pretty
2346 * printing mangles the ID into a truncated and useless value.
2348 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2350 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2354 switch (zfs_prop_get_type(prop)) {
2355 case PROP_TYPE_NUMBER:
2356 if (get_numeric_property(zhp, prop, src,
2357 &source, &val) != 0)
2360 (void) snprintf(propbuf, proplen, "%llu",
2363 zfs_nicenum(val, propbuf, proplen);
2366 case PROP_TYPE_STRING:
2367 (void) strlcpy(propbuf,
2368 getprop_string(zhp, prop, &source), proplen);
2371 case PROP_TYPE_INDEX:
2372 if (get_numeric_property(zhp, prop, src,
2373 &source, &val) != 0)
2375 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2377 (void) strlcpy(propbuf, strval, proplen);
2385 get_source(zhp, src, source, statbuf, statlen);
2391 * Utility function to get the given numeric property. Does no validation that
2392 * the given property is the appropriate type; should only be used with
2393 * hard-coded property types.
2396 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2401 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2407 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2411 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2412 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2416 * Similar to zfs_prop_get(), but returns the value as an integer.
2419 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2420 zprop_source_t *src, char *statbuf, size_t statlen)
2425 * Check to see if this property applies to our object
2427 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2428 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2429 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2430 zfs_prop_to_name(prop)));
2434 *src = ZPROP_SRC_NONE;
2436 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2439 get_source(zhp, src, source, statbuf, statlen);
2445 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2446 char **domainp, idmap_rid_t *ridp)
2449 idmap_get_handle_t *get_hdl = NULL;
2453 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2457 err = idmap_get_sidbyuid(get_hdl, id,
2458 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2460 err = idmap_get_sidbygid(get_hdl, id,
2461 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2463 if (err == IDMAP_SUCCESS &&
2464 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2465 status == IDMAP_SUCCESS)
2471 idmap_get_destroy(get_hdl);
2474 assert(!"invalid code path");
2479 * convert the propname into parameters needed by kernel
2480 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2481 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2484 userquota_propname_decode(const char *propname, boolean_t zoned,
2485 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2487 zfs_userquota_prop_t type;
2489 char *numericsid = NULL;
2494 /* Figure out the property type ({user|group}{quota|space}) */
2495 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2496 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2497 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2500 if (type == ZFS_NUM_USERQUOTA_PROPS)
2504 isuser = (type == ZFS_PROP_USERQUOTA ||
2505 type == ZFS_PROP_USERUSED);
2507 cp = strchr(propname, '@') + 1;
2509 if (strchr(cp, '@')) {
2512 * It's a SID name (eg "user@domain") that needs to be
2513 * turned into S-1-domainID-RID.
2515 directory_error_t e;
2516 if (zoned && getzoneid() == GLOBAL_ZONEID)
2519 e = directory_sid_from_user_name(NULL,
2522 e = directory_sid_from_group_name(NULL,
2526 directory_error_free(e);
2529 if (numericsid == NULL)
2532 /* will be further decoded below */
2538 if (strncmp(cp, "S-1-", 4) == 0) {
2539 /* It's a numeric SID (eg "S-1-234-567-89") */
2540 (void) strlcpy(domain, cp, domainlen);
2541 cp = strrchr(domain, '-');
2546 *ridp = strtoull(cp, &end, 10);
2551 if (errno != 0 || *end != '\0')
2553 } else if (!isdigit(*cp)) {
2555 * It's a user/group name (eg "user") that needs to be
2556 * turned into a uid/gid
2558 if (zoned && getzoneid() == GLOBAL_ZONEID)
2574 /* It's a user/group ID (eg "12345"). */
2575 uid_t id = strtoul(cp, &end, 10);
2582 /* It's an ephemeral ID. */
2583 if (idmap_id_to_numeric_domain_rid(id, isuser,
2584 &mapdomain, &rid) != 0)
2586 (void) strlcpy(domain, mapdomain, domainlen);
2593 ASSERT3P(numericsid, ==, NULL);
2598 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2599 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2602 zfs_cmd_t zc = { 0 };
2604 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2606 err = userquota_propname_decode(propname,
2607 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2608 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2609 zc.zc_objset_type = *typep;
2613 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2617 *propvalue = zc.zc_cookie;
2622 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2623 uint64_t *propvalue)
2625 zfs_userquota_prop_t type;
2627 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2632 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2633 char *propbuf, int proplen, boolean_t literal)
2637 zfs_userquota_prop_t type;
2639 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2646 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2647 } else if (propvalue == 0 &&
2648 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2649 (void) strlcpy(propbuf, "none", proplen);
2651 zfs_nicenum(propvalue, propbuf, proplen);
2657 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2658 uint64_t *propvalue)
2661 zfs_cmd_t zc = { 0 };
2662 const char *snapname;
2664 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2666 snapname = strchr(propname, '@') + 1;
2667 if (strchr(snapname, '@')) {
2668 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2670 /* snapname is the short name, append it to zhp's fsname */
2673 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2674 sizeof (zc.zc_value));
2675 cp = strchr(zc.zc_value, '@');
2678 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2679 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2682 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2686 *propvalue = zc.zc_cookie;
2691 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2692 char *propbuf, int proplen, boolean_t literal)
2697 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2703 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2705 zfs_nicenum(propvalue, propbuf, proplen);
2711 * Returns the name of the given zfs handle.
2714 zfs_get_name(const zfs_handle_t *zhp)
2716 return (zhp->zfs_name);
2720 * Returns the type of the given zfs handle.
2723 zfs_get_type(const zfs_handle_t *zhp)
2725 return (zhp->zfs_type);
2729 * Is one dataset name a child dataset of another?
2731 * Needs to handle these cases:
2732 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2733 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2734 * Descendant? No. No. No. Yes.
2737 is_descendant(const char *ds1, const char *ds2)
2739 size_t d1len = strlen(ds1);
2741 /* ds2 can't be a descendant if it's smaller */
2742 if (strlen(ds2) < d1len)
2745 /* otherwise, compare strings and verify that there's a '/' char */
2746 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2750 * Given a complete name, return just the portion that refers to the parent.
2751 * Will return -1 if there is no parent (path is just the name of the
2755 parent_name(const char *path, char *buf, size_t buflen)
2759 (void) strlcpy(buf, path, buflen);
2761 if ((slashp = strrchr(buf, '/')) == NULL)
2769 * If accept_ancestor is false, then check to make sure that the given path has
2770 * a parent, and that it exists. If accept_ancestor is true, then find the
2771 * closest existing ancestor for the given path. In prefixlen return the
2772 * length of already existing prefix of the given path. We also fetch the
2773 * 'zoned' property, which is used to validate property settings when creating
2777 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2778 boolean_t accept_ancestor, int *prefixlen)
2780 zfs_cmd_t zc = { 0 };
2781 char parent[ZFS_MAXNAMELEN];
2787 (void) snprintf(errbuf, sizeof (errbuf),
2788 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2790 /* get parent, and check to see if this is just a pool */
2791 if (parent_name(path, parent, sizeof (parent)) != 0) {
2792 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2793 "missing dataset name"));
2794 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2797 /* check to see if the pool exists */
2798 if ((slash = strchr(parent, '/')) == NULL)
2799 slash = parent + strlen(parent);
2800 (void) strncpy(zc.zc_name, parent, slash - parent);
2801 zc.zc_name[slash - parent] = '\0';
2802 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2804 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2805 "no such pool '%s'"), zc.zc_name);
2806 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2809 /* check to see if the parent dataset exists */
2810 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2811 if (errno == ENOENT && accept_ancestor) {
2813 * Go deeper to find an ancestor, give up on top level.
2815 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2816 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2817 "no such pool '%s'"), zc.zc_name);
2818 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2820 } else if (errno == ENOENT) {
2821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2822 "parent does not exist"));
2823 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2825 return (zfs_standard_error(hdl, errno, errbuf));
2828 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2832 /* we are in a non-global zone, but parent is in the global zone */
2833 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2834 (void) zfs_standard_error(hdl, EPERM, errbuf);
2839 /* make sure parent is a filesystem */
2840 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2842 "parent is not a filesystem"));
2843 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2849 if (prefixlen != NULL)
2850 *prefixlen = strlen(parent);
2855 * Finds whether the dataset of the given type(s) exists.
2858 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2862 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2866 * Try to get stats for the dataset, which will tell us if it exists.
2868 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2869 int ds_type = zhp->zfs_type;
2872 if (types & ds_type)
2879 * Given a path to 'target', create all the ancestors between
2880 * the prefixlen portion of the path, and the target itself.
2881 * Fail if the initial prefixlen-ancestor does not already exist.
2884 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2890 /* make sure prefix exists */
2891 cp = target + prefixlen;
2893 assert(strchr(cp, '/') == NULL);
2894 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2897 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2905 * Attempt to create, mount, and share any ancestor filesystems,
2906 * up to the prefixlen-long one.
2908 for (cp = target + prefixlen + 1;
2909 cp = strchr(cp, '/'); *cp = '/', cp++) {
2913 h = make_dataset_handle(hdl, target);
2915 /* it already exists, nothing to do here */
2920 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2922 opname = dgettext(TEXT_DOMAIN, "create");
2926 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2928 opname = dgettext(TEXT_DOMAIN, "open");
2932 if (zfs_mount(h, NULL, 0) != 0) {
2933 opname = dgettext(TEXT_DOMAIN, "mount");
2937 if (zfs_share(h) != 0) {
2938 opname = dgettext(TEXT_DOMAIN, "share");
2948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2949 "failed to %s ancestor '%s'"), opname, target);
2954 * Creates non-existing ancestors of the given path.
2957 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2963 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2966 if ((path_copy = strdup(path)) != NULL) {
2967 rc = create_parents(hdl, path_copy, prefix);
2970 if (path_copy == NULL || rc != 0)
2977 * Create a new filesystem or volume.
2980 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2985 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2988 dmu_objset_type_t ost;
2990 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2991 "cannot create '%s'"), path);
2993 /* validate the path, taking care to note the extended error message */
2994 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2995 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2997 /* validate parents exist */
2998 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3002 * The failure modes when creating a dataset of a different type over
3003 * one that already exists is a little strange. In particular, if you
3004 * try to create a dataset on top of an existing dataset, the ioctl()
3005 * will return ENOENT, not EEXIST. To prevent this from happening, we
3006 * first try to see if the dataset exists.
3008 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3010 "dataset already exists"));
3011 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3014 if (type == ZFS_TYPE_VOLUME)
3019 if (props && (props = zfs_valid_proplist(hdl, type, props,
3020 zoned, NULL, errbuf)) == 0)
3023 if (type == ZFS_TYPE_VOLUME) {
3025 * If we are creating a volume, the size and block size must
3026 * satisfy a few restraints. First, the blocksize must be a
3027 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3028 * volsize must be a multiple of the block size, and cannot be
3031 if (props == NULL || nvlist_lookup_uint64(props,
3032 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3034 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3035 "missing volume size"));
3036 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3039 if ((ret = nvlist_lookup_uint64(props,
3040 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3041 &blocksize)) != 0) {
3042 if (ret == ENOENT) {
3043 blocksize = zfs_prop_default_numeric(
3044 ZFS_PROP_VOLBLOCKSIZE);
3047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3048 "missing volume block size"));
3049 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3055 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3056 "volume size cannot be zero"));
3057 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3060 if (size % blocksize != 0) {
3062 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3063 "volume size must be a multiple of volume block "
3065 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3069 /* create the dataset */
3070 ret = lzc_create(path, ost, props);
3073 /* check for failure */
3075 char parent[ZFS_MAXNAMELEN];
3076 (void) parent_name(path, parent, sizeof (parent));
3080 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3081 "no such parent '%s'"), parent);
3082 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3086 "parent '%s' is not a filesystem"), parent);
3087 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3091 "volume block size must be power of 2 from "
3093 (uint_t)SPA_MINBLOCKSIZE,
3094 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3096 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3100 "pool must be upgraded to set this "
3101 "property or value"));
3102 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3106 * This platform can't address a volume this big.
3108 if (type == ZFS_TYPE_VOLUME)
3109 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3114 return (zfs_standard_error(hdl, errno, errbuf));
3122 * Destroys the given dataset. The caller must make sure that the filesystem
3123 * isn't mounted, and that there are no active dependents. If the file system
3124 * does not exist this function does nothing.
3127 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3129 zfs_cmd_t zc = { 0 };
3131 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3133 if (ZFS_IS_VOLUME(zhp)) {
3134 zc.zc_objset_type = DMU_OST_ZVOL;
3136 zc.zc_objset_type = DMU_OST_ZFS;
3139 zc.zc_defer_destroy = defer;
3140 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3142 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3143 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3147 remove_mountpoint(zhp);
3152 struct destroydata {
3154 const char *snapname;
3158 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3160 struct destroydata *dd = arg;
3162 char name[ZFS_MAXNAMELEN];
3165 (void) snprintf(name, sizeof (name),
3166 "%s@%s", zhp->zfs_name, dd->snapname);
3168 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3170 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3174 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3180 * Destroys all snapshots with the given name in zhp & descendants.
3183 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3186 struct destroydata dd = { 0 };
3188 dd.snapname = snapname;
3189 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3190 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3192 if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3193 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3194 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3195 zhp->zfs_name, snapname);
3197 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3199 nvlist_free(dd.nvl);
3204 * Destroys all the snapshots named in the nvlist.
3207 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3212 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3217 if (nvlist_next_nvpair(errlist, NULL) == NULL) {
3219 (void) snprintf(errbuf, sizeof (errbuf),
3220 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3222 ret = zfs_standard_error(hdl, ret, errbuf);
3224 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3225 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3227 (void) snprintf(errbuf, sizeof (errbuf),
3228 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3231 switch (fnvpair_value_int32(pair)) {
3234 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3235 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3238 ret = zfs_standard_error(hdl, errno, errbuf);
3247 * Clones the given dataset. The target must be of the same type as the source.
3250 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3252 char parent[ZFS_MAXNAMELEN];
3255 libzfs_handle_t *hdl = zhp->zfs_hdl;
3258 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3260 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3261 "cannot create '%s'"), target);
3263 /* validate the target/clone name */
3264 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3265 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3267 /* validate parents exist */
3268 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3271 (void) parent_name(target, parent, sizeof (parent));
3277 if (ZFS_IS_VOLUME(zhp)) {
3278 type = ZFS_TYPE_VOLUME;
3280 type = ZFS_TYPE_FILESYSTEM;
3282 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3283 zhp, errbuf)) == NULL)
3287 ret = lzc_clone(target, zhp->zfs_name, props);
3295 * The parent doesn't exist. We should have caught this
3296 * above, but there may a race condition that has since
3297 * destroyed the parent.
3299 * At this point, we don't know whether it's the source
3300 * that doesn't exist anymore, or whether the target
3301 * dataset doesn't exist.
3303 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3304 "no such parent '%s'"), parent);
3305 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3308 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3309 "source and target pools differ"));
3310 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3314 return (zfs_standard_error(zhp->zfs_hdl, errno,
3323 * Promotes the given clone fs to be the clone parent.
3326 zfs_promote(zfs_handle_t *zhp)
3328 libzfs_handle_t *hdl = zhp->zfs_hdl;
3329 zfs_cmd_t zc = { 0 };
3330 char parent[MAXPATHLEN];
3334 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3335 "cannot promote '%s'"), zhp->zfs_name);
3337 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3339 "snapshots can not be promoted"));
3340 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3343 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3344 if (parent[0] == '\0') {
3345 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3346 "not a cloned filesystem"));
3347 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3350 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3351 sizeof (zc.zc_value));
3352 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3353 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3356 int save_errno = errno;
3358 switch (save_errno) {
3360 /* There is a conflicting snapshot name. */
3361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3362 "conflicting snapshot '%s' from parent '%s'"),
3363 zc.zc_string, parent);
3364 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3367 return (zfs_standard_error(hdl, save_errno, errbuf));
3373 typedef struct snapdata {
3375 const char *sd_snapname;
3379 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3381 snapdata_t *sd = arg;
3382 char name[ZFS_MAXNAMELEN];
3385 (void) snprintf(name, sizeof (name),
3386 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3388 fnvlist_add_boolean(sd->sd_nvl, name);
3390 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3396 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3400 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3407 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3408 "cannot create snapshots "));
3411 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3412 const char *snapname = nvpair_name(elem);
3414 /* validate the target name */
3415 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3417 (void) snprintf(errbuf, sizeof (errbuf),
3418 dgettext(TEXT_DOMAIN,
3419 "cannot create snapshot '%s'"), snapname);
3420 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3424 if (props != NULL &&
3425 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3426 props, B_FALSE, NULL, errbuf)) == NULL) {
3430 ret = lzc_snapshot(snaps, props, &errors);
3433 boolean_t printed = B_FALSE;
3434 for (elem = nvlist_next_nvpair(errors, NULL);
3436 elem = nvlist_next_nvpair(errors, elem)) {
3437 (void) snprintf(errbuf, sizeof (errbuf),
3438 dgettext(TEXT_DOMAIN,
3439 "cannot create snapshot '%s'"), nvpair_name(elem));
3440 (void) zfs_standard_error(hdl,
3441 fnvpair_value_int32(elem), errbuf);
3447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3448 "multiple snapshots of same "
3450 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3454 (void) zfs_standard_error(hdl, ret, errbuf);
3460 nvlist_free(errors);
3465 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3469 snapdata_t sd = { 0 };
3470 char fsname[ZFS_MAXNAMELEN];
3475 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3476 "cannot snapshot %s"), path);
3478 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3479 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3481 (void) strlcpy(fsname, path, sizeof (fsname));
3482 cp = strchr(fsname, '@');
3484 sd.sd_snapname = cp + 1;
3486 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3487 ZFS_TYPE_VOLUME)) == NULL) {
3491 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3493 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3495 fnvlist_add_boolean(sd.sd_nvl, path);
3498 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3499 nvlist_free(sd.sd_nvl);
3505 * Destroy any more recent snapshots. We invoke this callback on any dependents
3506 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3507 * is a dependent and we should just destroy it without checking the transaction
3510 typedef struct rollback_data {
3511 const char *cb_target; /* the snapshot */
3512 uint64_t cb_create; /* creation time reference */
3514 boolean_t cb_dependent;
3519 rollback_destroy(zfs_handle_t *zhp, void *data)
3521 rollback_data_t *cbp = data;
3523 if (!cbp->cb_dependent) {
3524 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3525 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3526 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3529 cbp->cb_dependent = B_TRUE;
3530 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3531 rollback_destroy, cbp);
3532 cbp->cb_dependent = B_FALSE;
3534 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3537 /* We must destroy this clone; first unmount it */
3538 prop_changelist_t *clp;
3540 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3541 cbp->cb_force ? MS_FORCE: 0);
3542 if (clp == NULL || changelist_prefix(clp) != 0) {
3543 cbp->cb_error = B_TRUE;
3547 if (zfs_destroy(zhp, B_FALSE) != 0)
3548 cbp->cb_error = B_TRUE;
3550 changelist_remove(clp, zhp->zfs_name);
3551 (void) changelist_postfix(clp);
3552 changelist_free(clp);
3560 * Given a dataset, rollback to a specific snapshot, discarding any
3561 * data changes since then and making it the active dataset.
3563 * Any snapshots more recent than the target are destroyed, along with
3567 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3569 rollback_data_t cb = { 0 };
3571 zfs_cmd_t zc = { 0 };
3572 boolean_t restore_resv = 0;
3573 uint64_t old_volsize, new_volsize;
3574 zfs_prop_t resv_prop;
3576 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3577 zhp->zfs_type == ZFS_TYPE_VOLUME);
3580 * Destroy all recent snapshots and their dependents.
3582 cb.cb_force = force;
3583 cb.cb_target = snap->zfs_name;
3584 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3585 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3591 * Now that we have verified that the snapshot is the latest,
3592 * rollback to the given snapshot.
3595 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3596 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3598 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3600 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3603 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3605 if (ZFS_IS_VOLUME(zhp))
3606 zc.zc_objset_type = DMU_OST_ZVOL;
3608 zc.zc_objset_type = DMU_OST_ZFS;
3611 * We rely on zfs_iter_children() to verify that there are no
3612 * newer snapshots for the given dataset. Therefore, we can
3613 * simply pass the name on to the ioctl() call. There is still
3614 * an unlikely race condition where the user has taken a
3615 * snapshot since we verified that this was the most recent.
3618 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3619 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3620 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3626 * For volumes, if the pre-rollback volsize matched the pre-
3627 * rollback reservation and the volsize has changed then set
3628 * the reservation property to the post-rollback volsize.
3629 * Make a new handle since the rollback closed the dataset.
3631 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3632 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3634 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3635 if (old_volsize != new_volsize)
3636 err = zfs_prop_set_int(zhp, resv_prop,
3645 * Renames the given dataset.
3648 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
3649 renameflags_t flags)
3652 zfs_cmd_t zc = { 0 };
3654 prop_changelist_t *cl = NULL;
3655 zfs_handle_t *zhrp = NULL;
3656 char *parentname = NULL;
3657 char parent[ZFS_MAXNAMELEN];
3658 char property[ZFS_MAXPROPLEN];
3659 libzfs_handle_t *hdl = zhp->zfs_hdl;
3662 /* if we have the same exact name, just return success */
3663 if (strcmp(zhp->zfs_name, target) == 0)
3666 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3667 "cannot rename to '%s'"), target);
3669 if (source != NULL) {
3671 * This is recursive snapshots rename, put snapshot name
3672 * (that might not exist) into zfs_name.
3674 assert(flags.recurse);
3676 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
3677 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
3678 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
3682 * Make sure the target name is valid
3684 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3685 if ((strchr(target, '@') == NULL) ||
3688 * Snapshot target name is abbreviated,
3689 * reconstruct full dataset name
3691 (void) strlcpy(parent, zhp->zfs_name,
3693 delim = strchr(parent, '@');
3694 if (strchr(target, '@') == NULL)
3698 (void) strlcat(parent, target, sizeof (parent));
3702 * Make sure we're renaming within the same dataset.
3704 delim = strchr(target, '@');
3705 if (strncmp(zhp->zfs_name, target, delim - target)
3706 != 0 || zhp->zfs_name[delim - target] != '@') {
3707 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3708 "snapshots must be part of same "
3710 return (zfs_error(hdl, EZFS_CROSSTARGET,
3714 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3715 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3717 if (flags.recurse) {
3718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3719 "recursive rename must be a snapshot"));
3720 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3723 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3724 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3726 /* validate parents */
3727 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3730 /* make sure we're in the same pool */
3731 verify((delim = strchr(target, '/')) != NULL);
3732 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3733 zhp->zfs_name[delim - target] != '/') {
3734 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3735 "datasets must be within same pool"));
3736 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3739 /* new name cannot be a child of the current dataset name */
3740 if (is_descendant(zhp->zfs_name, target)) {
3741 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3742 "New dataset name cannot be a descendant of "
3743 "current dataset name"));
3744 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3748 (void) snprintf(errbuf, sizeof (errbuf),
3749 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3751 if (getzoneid() == GLOBAL_ZONEID &&
3752 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3754 "dataset is used in a non-global zone"));
3755 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3759 * Avoid unmounting file systems with mountpoint property set to
3760 * 'legacy' or 'none' even if -u option is not given.
3762 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3763 !flags.recurse && !flags.nounmount &&
3764 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
3765 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
3766 (strcmp(property, "legacy") == 0 ||
3767 strcmp(property, "none") == 0)) {
3768 flags.nounmount = B_TRUE;
3771 if (flags.recurse) {
3773 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3774 if (parentname == NULL) {
3778 delim = strchr(parentname, '@');
3780 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3787 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
3788 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
3789 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
3793 if (changelist_haszonedchild(cl)) {
3794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3795 "child dataset with inherited mountpoint is used "
3796 "in a non-global zone"));
3797 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3801 if ((ret = changelist_prefix(cl)) != 0)
3805 if (ZFS_IS_VOLUME(zhp))
3806 zc.zc_objset_type = DMU_OST_ZVOL;
3808 zc.zc_objset_type = DMU_OST_ZFS;
3810 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3811 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3813 zc.zc_cookie = flags.recurse ? 1 : 0;
3814 if (flags.nounmount)
3817 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3819 * if it was recursive, the one that actually failed will
3822 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3823 "cannot rename '%s'"), zc.zc_name);
3825 if (flags.recurse && errno == EEXIST) {
3826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3827 "a child dataset already has a snapshot "
3828 "with the new name"));
3829 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3831 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3835 * On failure, we still want to remount any filesystems that
3836 * were previously mounted, so we don't alter the system state.
3839 (void) changelist_postfix(cl);
3841 if (!flags.recurse) {
3842 changelist_rename(cl, zfs_get_name(zhp), target);
3843 ret = changelist_postfix(cl);
3855 changelist_free(cl);
3861 zfs_get_user_props(zfs_handle_t *zhp)
3863 return (zhp->zfs_user_props);
3867 zfs_get_recvd_props(zfs_handle_t *zhp)
3869 if (zhp->zfs_recvd_props == NULL)
3870 if (get_recvd_props_ioctl(zhp) != 0)
3872 return (zhp->zfs_recvd_props);
3876 * This function is used by 'zfs list' to determine the exact set of columns to
3877 * display, and their maximum widths. This does two main things:
3879 * - If this is a list of all properties, then expand the list to include
3880 * all native properties, and set a flag so that for each dataset we look
3881 * for new unique user properties and add them to the list.
3883 * - For non fixed-width properties, keep track of the maximum width seen
3884 * so that we can size the column appropriately. If the user has
3885 * requested received property values, we also need to compute the width
3886 * of the RECEIVED column.
3889 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3891 libzfs_handle_t *hdl = zhp->zfs_hdl;
3892 zprop_list_t *entry;
3893 zprop_list_t **last, **start;
3894 nvlist_t *userprops, *propval;
3897 char buf[ZFS_MAXPROPLEN];
3899 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3902 userprops = zfs_get_user_props(zhp);
3905 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3907 * Go through and add any user properties as necessary. We
3908 * start by incrementing our list pointer to the first
3909 * non-native property.
3912 while (*start != NULL) {
3913 if ((*start)->pl_prop == ZPROP_INVAL)
3915 start = &(*start)->pl_next;
3919 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3921 * See if we've already found this property in our list.
3923 for (last = start; *last != NULL;
3924 last = &(*last)->pl_next) {
3925 if (strcmp((*last)->pl_user_prop,
3926 nvpair_name(elem)) == 0)
3930 if (*last == NULL) {
3931 if ((entry = zfs_alloc(hdl,
3932 sizeof (zprop_list_t))) == NULL ||
3933 ((entry->pl_user_prop = zfs_strdup(hdl,
3934 nvpair_name(elem)))) == NULL) {
3939 entry->pl_prop = ZPROP_INVAL;
3940 entry->pl_width = strlen(nvpair_name(elem));
3941 entry->pl_all = B_TRUE;
3948 * Now go through and check the width of any non-fixed columns
3950 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3951 if (entry->pl_fixed)
3954 if (entry->pl_prop != ZPROP_INVAL) {
3955 if (zfs_prop_get(zhp, entry->pl_prop,
3956 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3957 if (strlen(buf) > entry->pl_width)
3958 entry->pl_width = strlen(buf);
3960 if (received && zfs_prop_get_recvd(zhp,
3961 zfs_prop_to_name(entry->pl_prop),
3962 buf, sizeof (buf), B_FALSE) == 0)
3963 if (strlen(buf) > entry->pl_recvd_width)
3964 entry->pl_recvd_width = strlen(buf);
3966 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3968 verify(nvlist_lookup_string(propval,
3969 ZPROP_VALUE, &strval) == 0);
3970 if (strlen(strval) > entry->pl_width)
3971 entry->pl_width = strlen(strval);
3973 if (received && zfs_prop_get_recvd(zhp,
3974 entry->pl_user_prop,
3975 buf, sizeof (buf), B_FALSE) == 0)
3976 if (strlen(buf) > entry->pl_recvd_width)
3977 entry->pl_recvd_width = strlen(buf);
3985 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3986 char *resource, void *export, void *sharetab,
3987 int sharemax, zfs_share_op_t operation)
3989 zfs_cmd_t zc = { 0 };
3992 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3993 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3995 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3996 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3997 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3998 zc.zc_share.z_sharetype = operation;
3999 zc.zc_share.z_sharemax = sharemax;
4000 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4005 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4010 * Keep a reference to the props-table against which we prune the
4013 zhp->zfs_props_table = props;
4015 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4018 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4019 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4022 * User properties will result in ZPROP_INVAL, and since we
4023 * only know how to prune standard ZFS properties, we always
4024 * leave these in the list. This can also happen if we
4025 * encounter an unknown DSL property (when running older
4026 * software, for example).
4028 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4029 (void) nvlist_remove(zhp->zfs_props,
4030 nvpair_name(curr), nvpair_type(curr));
4037 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4038 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4040 zfs_cmd_t zc = { 0 };
4041 nvlist_t *nvlist = NULL;
4044 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4045 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4046 zc.zc_cookie = (uint64_t)cmd;
4048 if (cmd == ZFS_SMB_ACL_RENAME) {
4049 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4050 (void) no_memory(hdl);
4056 case ZFS_SMB_ACL_ADD:
4057 case ZFS_SMB_ACL_REMOVE:
4058 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4060 case ZFS_SMB_ACL_RENAME:
4061 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4063 (void) no_memory(hdl);
4066 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4068 (void) no_memory(hdl);
4071 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4072 nvlist_free(nvlist);
4076 case ZFS_SMB_ACL_PURGE:
4081 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4083 nvlist_free(nvlist);
4088 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4089 char *path, char *resource)
4091 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4096 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4097 char *path, char *resource)
4099 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4104 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4106 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4111 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4112 char *oldname, char *newname)
4114 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4120 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4121 zfs_userspace_cb_t func, void *arg)
4123 zfs_cmd_t zc = { 0 };
4124 zfs_useracct_t buf[100];
4125 libzfs_handle_t *hdl = zhp->zfs_hdl;
4128 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4130 zc.zc_objset_type = type;
4131 zc.zc_nvlist_dst = (uintptr_t)buf;
4134 zfs_useracct_t *zua = buf;
4136 zc.zc_nvlist_dst_size = sizeof (buf);
4137 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4140 (void) snprintf(errbuf, sizeof (errbuf),
4141 dgettext(TEXT_DOMAIN,
4142 "cannot get used/quota for %s"), zc.zc_name);
4143 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4145 if (zc.zc_nvlist_dst_size == 0)
4148 while (zc.zc_nvlist_dst_size > 0) {
4149 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4150 zua->zu_space)) != 0)
4153 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4162 const char *snapname;
4164 boolean_t recursive;
4168 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4170 struct holdarg *ha = arg;
4172 char name[ZFS_MAXNAMELEN];
4175 (void) snprintf(name, sizeof (name),
4176 "%s@%s", zhp->zfs_name, ha->snapname);
4178 szhp = make_dataset_handle(zhp->zfs_hdl, name);
4180 fnvlist_add_string(ha->nvl, name, ha->tag);
4185 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4191 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4192 boolean_t recursive, boolean_t enoent_ok, int cleanup_fd)
4197 libzfs_handle_t *hdl = zhp->zfs_hdl;
4201 ha.nvl = fnvlist_alloc();
4202 ha.snapname = snapname;
4204 ha.recursive = recursive;
4205 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4207 if (nvlist_next_nvpair(ha.nvl, NULL) == NULL) {
4208 fnvlist_free(ha.nvl);
4211 (void) snprintf(errbuf, sizeof (errbuf),
4212 dgettext(TEXT_DOMAIN,
4213 "cannot hold snapshot '%s@%s'"),
4214 zhp->zfs_name, snapname);
4215 (void) zfs_standard_error(hdl, ret, errbuf);
4220 ret = lzc_hold(ha.nvl, cleanup_fd, &errors);
4221 fnvlist_free(ha.nvl);
4226 if (nvlist_next_nvpair(errors, NULL) == NULL) {
4227 /* no hold-specific errors */
4228 (void) snprintf(errbuf, sizeof (errbuf),
4229 dgettext(TEXT_DOMAIN, "cannot hold"));
4232 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4233 "pool must be upgraded"));
4234 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4237 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4240 (void) zfs_standard_error(hdl, ret, errbuf);
4244 for (elem = nvlist_next_nvpair(errors, NULL);
4246 elem = nvlist_next_nvpair(errors, elem)) {
4247 (void) snprintf(errbuf, sizeof (errbuf),
4248 dgettext(TEXT_DOMAIN,
4249 "cannot hold snapshot '%s'"), nvpair_name(elem));
4250 switch (fnvpair_value_int32(elem)) {
4253 * Temporary tags wind up having the ds object id
4254 * prepended. So even if we passed the length check
4255 * above, it's still possible for the tag to wind
4256 * up being slightly too long.
4258 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4261 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4264 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4271 (void) zfs_standard_error(hdl,
4272 fnvpair_value_int32(elem), errbuf);
4276 fnvlist_free(errors);
4282 const char *snapname;
4284 boolean_t recursive;
4288 zfs_release_one(zfs_handle_t *zhp, void *arg)
4290 struct holdarg *ha = arg;
4292 char name[ZFS_MAXNAMELEN];
4295 (void) snprintf(name, sizeof (name),
4296 "%s@%s", zhp->zfs_name, ha->snapname);
4298 szhp = make_dataset_handle(zhp->zfs_hdl, name);
4300 nvlist_t *holds = fnvlist_alloc();
4301 fnvlist_add_boolean(holds, ha->tag);
4302 fnvlist_add_nvlist(ha->nvl, name, holds);
4307 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4313 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4314 boolean_t recursive)
4320 libzfs_handle_t *hdl = zhp->zfs_hdl;
4323 ha.nvl = fnvlist_alloc();
4324 ha.snapname = snapname;
4326 ha.recursive = recursive;
4327 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4329 if (nvlist_next_nvpair(ha.nvl, NULL) == NULL) {
4330 fnvlist_free(ha.nvl);
4332 (void) snprintf(errbuf, sizeof (errbuf),
4333 dgettext(TEXT_DOMAIN,
4334 "cannot release hold from snapshot '%s@%s'"),
4335 zhp->zfs_name, snapname);
4336 (void) zfs_standard_error(hdl, ret, errbuf);
4340 ret = lzc_release(ha.nvl, &errors);
4341 fnvlist_free(ha.nvl);
4346 if (nvlist_next_nvpair(errors, NULL) == NULL) {
4347 /* no hold-specific errors */
4348 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4353 "pool must be upgraded"));
4354 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4357 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4361 for (elem = nvlist_next_nvpair(errors, NULL);
4363 elem = nvlist_next_nvpair(errors, elem)) {
4364 (void) snprintf(errbuf, sizeof (errbuf),
4365 dgettext(TEXT_DOMAIN,
4366 "cannot release hold from snapshot '%s'"),
4368 switch (fnvpair_value_int32(elem)) {
4370 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4373 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4376 (void) zfs_standard_error_fmt(hdl,
4377 fnvpair_value_int32(elem), errbuf);
4381 fnvlist_free(errors);
4386 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4388 zfs_cmd_t zc = { 0 };
4389 libzfs_handle_t *hdl = zhp->zfs_hdl;
4395 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4396 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4400 nvbuf = malloc(nvsz);
4401 if (nvbuf == NULL) {
4402 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4406 zc.zc_nvlist_dst_size = nvsz;
4407 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4409 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4411 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4412 (void) snprintf(errbuf, sizeof (errbuf),
4413 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4418 nvsz = zc.zc_nvlist_dst_size;
4422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4423 "pool must be upgraded"));
4424 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4427 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4430 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4433 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4438 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4440 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4441 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4443 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4453 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4455 zfs_cmd_t zc = { 0 };
4456 libzfs_handle_t *hdl = zhp->zfs_hdl;
4462 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4463 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4465 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4468 nvbuf = malloc(nvsz);
4470 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4473 zc.zc_nvlist_src_size = nvsz;
4474 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4475 zc.zc_perm_action = un;
4477 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4479 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4480 (void) snprintf(errbuf, sizeof (errbuf),
4481 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4486 "pool must be upgraded"));
4487 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4490 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4493 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4496 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4507 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4512 err = lzc_get_holds(zhp->zfs_name, nvl);
4515 libzfs_handle_t *hdl = zhp->zfs_hdl;
4517 (void) snprintf(errbuf, sizeof (errbuf),
4518 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4523 "pool must be upgraded"));
4524 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4527 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4530 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4533 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4542 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4545 uint64_t nblocks, volblocksize;
4549 if (nvlist_lookup_string(props,
4550 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4551 ncopies = atoi(strval);
4554 if (nvlist_lookup_uint64(props,
4555 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4556 &volblocksize) != 0)
4557 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4558 nblocks = volsize/volblocksize;
4559 /* start with metadnode L0-L6 */
4561 /* calculate number of indirects */
4562 while (nblocks > 1) {
4563 nblocks += DNODES_PER_LEVEL - 1;
4564 nblocks /= DNODES_PER_LEVEL;
4567 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4570 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4571 * compressed, but in practice they compress down to about
4574 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4580 * Attach/detach the given filesystem to/from the given jail.
4583 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4585 libzfs_handle_t *hdl = zhp->zfs_hdl;
4586 zfs_cmd_t zc = { 0 };
4592 (void) snprintf(errbuf, sizeof (errbuf),
4593 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4595 (void) snprintf(errbuf, sizeof (errbuf),
4596 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4599 switch (zhp->zfs_type) {
4600 case ZFS_TYPE_VOLUME:
4601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4602 "volumes can not be jailed"));
4603 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4604 case ZFS_TYPE_SNAPSHOT:
4605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4606 "snapshots can not be jailed"));
4607 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4609 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4611 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4612 zc.zc_objset_type = DMU_OST_ZFS;
4613 zc.zc_jailid = jailid;
4615 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4616 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4617 zfs_standard_error(hdl, errno, errbuf);