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 (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27 * Copyright (c) 2013 Martin Matuska. All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
43 #include <sys/mntent.h>
44 #include <sys/mount.h>
53 #include <directory.h>
54 #endif /* HAVE_IDMAP */
56 #include <sys/dnode.h>
61 #include "zfs_namecheck.h"
63 #include "libzfs_impl.h"
64 #include "zfs_deleg.h"
66 static int userquota_propname_decode(const char *propname, boolean_t zoned,
67 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
70 * Given a single type (not a mask of types), return the type in a human
74 zfs_type_to_name(zfs_type_t type)
77 case ZFS_TYPE_FILESYSTEM:
78 return (dgettext(TEXT_DOMAIN, "filesystem"));
79 case ZFS_TYPE_SNAPSHOT:
80 return (dgettext(TEXT_DOMAIN, "snapshot"));
82 return (dgettext(TEXT_DOMAIN, "volume"));
91 * Validate a ZFS path. This is used even before trying to open the dataset, to
92 * provide a more meaningful error message. We call zfs_error_aux() to
93 * explain exactly why the name was not valid.
96 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
102 (void) zfs_prop_get_table();
103 if (dataset_namecheck(path, &why, &what) != 0) {
106 case NAME_ERR_TOOLONG:
107 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
108 "name is too long"));
111 case NAME_ERR_LEADING_SLASH:
112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
113 "leading slash in name"));
116 case NAME_ERR_EMPTY_COMPONENT:
117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 "empty component in name"));
121 case NAME_ERR_TRAILING_SLASH:
122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
123 "trailing slash in name"));
126 case NAME_ERR_INVALCHAR:
128 dgettext(TEXT_DOMAIN, "invalid character "
129 "'%c' in name"), what);
132 case NAME_ERR_MULTIPLE_AT:
133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
134 "multiple '@' delimiters in name"));
137 case NAME_ERR_NOLETTER:
138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139 "pool doesn't begin with a letter"));
142 case NAME_ERR_RESERVED:
143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
144 "name is reserved"));
147 case NAME_ERR_DISKLIKE:
148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
149 "reserved disk name"));
159 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
162 "snapshot delimiter '@' in filesystem name"));
166 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169 "missing '@' delimiter in snapshot name"));
173 if (modifying && strchr(path, '%') != NULL) {
175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176 "invalid character %c in name"), '%');
184 zfs_name_valid(const char *name, zfs_type_t type)
186 if (type == ZFS_TYPE_POOL)
187 return (zpool_name_valid(NULL, B_FALSE, name));
188 return (zfs_validate_name(NULL, name, type, B_FALSE));
192 * This function takes the raw DSL properties, and filters out the user-defined
193 * properties into a separate nvlist.
196 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
198 libzfs_handle_t *hdl = zhp->zfs_hdl;
203 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
204 (void) no_memory(hdl);
209 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
210 if (!zfs_prop_user(nvpair_name(elem)))
213 verify(nvpair_value_nvlist(elem, &propval) == 0);
214 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
216 (void) no_memory(hdl);
224 static zpool_handle_t *
225 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
227 libzfs_handle_t *hdl = zhp->zfs_hdl;
230 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
231 if (hdl->libzfs_pool_handles != NULL)
232 zph->zpool_next = hdl->libzfs_pool_handles;
233 hdl->libzfs_pool_handles = zph;
238 static zpool_handle_t *
239 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
241 libzfs_handle_t *hdl = zhp->zfs_hdl;
242 zpool_handle_t *zph = hdl->libzfs_pool_handles;
244 while ((zph != NULL) &&
245 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
246 zph = zph->zpool_next;
251 * Returns a handle to the pool that contains the provided dataset.
252 * If a handle to that pool already exists then that handle is returned.
253 * Otherwise, a new handle is created and added to the list of handles.
255 static zpool_handle_t *
256 zpool_handle(zfs_handle_t *zhp)
262 len = strcspn(zhp->zfs_name, "/@") + 1;
263 pool_name = zfs_alloc(zhp->zfs_hdl, len);
264 (void) strlcpy(pool_name, zhp->zfs_name, len);
266 zph = zpool_find_handle(zhp, pool_name, len);
268 zph = zpool_add_handle(zhp, pool_name);
275 zpool_free_handles(libzfs_handle_t *hdl)
277 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
279 while (zph != NULL) {
280 next = zph->zpool_next;
284 hdl->libzfs_pool_handles = NULL;
288 * Utility function to gather stats (objset and zpl) for the given object.
291 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
293 libzfs_handle_t *hdl = zhp->zfs_hdl;
295 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
297 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
298 if (errno == ENOMEM) {
299 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
310 * Utility function to get the received properties of the given object.
313 get_recvd_props_ioctl(zfs_handle_t *zhp)
315 libzfs_handle_t *hdl = zhp->zfs_hdl;
316 nvlist_t *recvdprops;
317 zfs_cmd_t zc = {"\0"};
320 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
323 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
325 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
326 if (errno == ENOMEM) {
327 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
331 zcmd_free_nvlists(&zc);
336 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
337 zcmd_free_nvlists(&zc);
341 nvlist_free(zhp->zfs_recvd_props);
342 zhp->zfs_recvd_props = recvdprops;
348 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
350 nvlist_t *allprops, *userprops;
352 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
354 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
359 * XXX Why do we store the user props separately, in addition to
360 * storing them in zfs_props?
362 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
363 nvlist_free(allprops);
367 nvlist_free(zhp->zfs_props);
368 nvlist_free(zhp->zfs_user_props);
370 zhp->zfs_props = allprops;
371 zhp->zfs_user_props = userprops;
377 get_stats(zfs_handle_t *zhp)
380 zfs_cmd_t zc = {"\0"};
382 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
384 if (get_stats_ioctl(zhp, &zc) != 0)
386 else if (put_stats_zhdl(zhp, &zc) != 0)
388 zcmd_free_nvlists(&zc);
393 * Refresh the properties currently stored in the handle.
396 zfs_refresh_properties(zfs_handle_t *zhp)
398 (void) get_stats(zhp);
402 * Makes a handle from the given dataset name. Used by zfs_open() and
403 * zfs_iter_* to create child handles on the fly.
406 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
408 if (put_stats_zhdl(zhp, zc) != 0)
412 * We've managed to open the dataset and gather statistics. Determine
413 * the high-level type.
415 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
416 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
417 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
418 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
419 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
424 if (zhp->zfs_dmustats.dds_is_snapshot)
425 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
426 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
427 zhp->zfs_type = ZFS_TYPE_VOLUME;
428 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
429 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
431 abort(); /* we should never see any other types */
433 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
440 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
442 zfs_cmd_t zc = {"\0"};
444 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
450 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
451 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
455 if (get_stats_ioctl(zhp, &zc) == -1) {
456 zcmd_free_nvlists(&zc);
460 if (make_dataset_handle_common(zhp, &zc) == -1) {
464 zcmd_free_nvlists(&zc);
469 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
471 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
477 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
478 if (make_dataset_handle_common(zhp, zc) == -1) {
486 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
488 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
493 zhp->zfs_hdl = pzhp->zfs_hdl;
494 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
495 zhp->zfs_head_type = pzhp->zfs_type;
496 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
497 zhp->zpool_hdl = zpool_handle(zhp);
503 zfs_handle_dup(zfs_handle_t *zhp_orig)
505 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
510 zhp->zfs_hdl = zhp_orig->zfs_hdl;
511 zhp->zpool_hdl = zhp_orig->zpool_hdl;
512 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
513 sizeof (zhp->zfs_name));
514 zhp->zfs_type = zhp_orig->zfs_type;
515 zhp->zfs_head_type = zhp_orig->zfs_head_type;
516 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
517 if (zhp_orig->zfs_props != NULL) {
518 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
519 (void) no_memory(zhp->zfs_hdl);
524 if (zhp_orig->zfs_user_props != NULL) {
525 if (nvlist_dup(zhp_orig->zfs_user_props,
526 &zhp->zfs_user_props, 0) != 0) {
527 (void) no_memory(zhp->zfs_hdl);
532 if (zhp_orig->zfs_recvd_props != NULL) {
533 if (nvlist_dup(zhp_orig->zfs_recvd_props,
534 &zhp->zfs_recvd_props, 0)) {
535 (void) no_memory(zhp->zfs_hdl);
540 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
541 if (zhp_orig->zfs_mntopts != NULL) {
542 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
543 zhp_orig->zfs_mntopts);
545 zhp->zfs_props_table = zhp_orig->zfs_props_table;
550 * Opens the given snapshot, filesystem, or volume. The 'types'
551 * argument is a mask of acceptable types. The function will print an
552 * appropriate error message and return NULL if it can't be opened.
555 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
560 (void) snprintf(errbuf, sizeof (errbuf),
561 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
564 * Validate the name before we even try to open it.
566 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
568 "invalid dataset name"));
569 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
574 * Try to get stats for the dataset, which will tell us if it exists.
577 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
578 (void) zfs_standard_error(hdl, errno, errbuf);
582 if (!(types & zhp->zfs_type)) {
583 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
592 * Release a ZFS handle. Nothing to do but free the associated memory.
595 zfs_close(zfs_handle_t *zhp)
597 if (zhp->zfs_mntopts)
598 free(zhp->zfs_mntopts);
599 nvlist_free(zhp->zfs_props);
600 nvlist_free(zhp->zfs_user_props);
601 nvlist_free(zhp->zfs_recvd_props);
605 typedef struct mnttab_node {
606 struct mnttab mtn_mt;
611 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
613 const mnttab_node_t *mtn1 = arg1;
614 const mnttab_node_t *mtn2 = arg2;
617 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
621 return (rv > 0 ? 1 : -1);
625 libzfs_mnttab_init(libzfs_handle_t *hdl)
627 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
628 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
629 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
633 libzfs_mnttab_update(libzfs_handle_t *hdl)
637 /* Reopen MNTTAB to prevent reading stale data from open file */
638 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
641 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
645 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
648 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
649 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
650 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
651 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
652 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
654 /* Exclude duplicate mounts */
655 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
656 free(mtn->mtn_mt.mnt_special);
657 free(mtn->mtn_mt.mnt_mountp);
658 free(mtn->mtn_mt.mnt_fstype);
659 free(mtn->mtn_mt.mnt_mntopts);
664 avl_add(&hdl->libzfs_mnttab_cache, mtn);
671 libzfs_mnttab_fini(libzfs_handle_t *hdl)
676 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
677 free(mtn->mtn_mt.mnt_special);
678 free(mtn->mtn_mt.mnt_mountp);
679 free(mtn->mtn_mt.mnt_fstype);
680 free(mtn->mtn_mt.mnt_mntopts);
683 avl_destroy(&hdl->libzfs_mnttab_cache);
687 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
689 hdl->libzfs_mnttab_enable = enable;
693 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
694 struct mnttab *entry)
700 if (!hdl->libzfs_mnttab_enable) {
701 struct mnttab srch = { 0 };
703 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
704 libzfs_mnttab_fini(hdl);
706 /* Reopen MNTTAB to prevent reading stale data from open file */
707 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
710 srch.mnt_special = (char *)fsname;
711 srch.mnt_fstype = MNTTYPE_ZFS;
712 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
718 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
719 if ((error = libzfs_mnttab_update(hdl)) != 0)
722 find.mtn_mt.mnt_special = (char *)fsname;
723 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
725 *entry = mtn->mtn_mt;
732 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
733 const char *mountp, const char *mntopts)
737 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
739 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
740 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
741 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
742 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
743 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
744 avl_add(&hdl->libzfs_mnttab_cache, mtn);
748 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
753 find.mtn_mt.mnt_special = (char *)fsname;
754 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
755 avl_remove(&hdl->libzfs_mnttab_cache, ret);
756 free(ret->mtn_mt.mnt_special);
757 free(ret->mtn_mt.mnt_mountp);
758 free(ret->mtn_mt.mnt_fstype);
759 free(ret->mtn_mt.mnt_mntopts);
765 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
767 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
769 if (zpool_handle == NULL)
772 *spa_version = zpool_get_prop_int(zpool_handle,
773 ZPOOL_PROP_VERSION, NULL);
778 * The choice of reservation property depends on the SPA version.
781 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
785 if (zfs_spa_version(zhp, &spa_version) < 0)
788 if (spa_version >= SPA_VERSION_REFRESERVATION)
789 *resv_prop = ZFS_PROP_REFRESERVATION;
791 *resv_prop = ZFS_PROP_RESERVATION;
797 * Given an nvlist of properties to set, validates that they are correct, and
798 * parses any numeric properties (index, boolean, etc) if they are specified as
802 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
803 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
810 int chosen_normal = -1;
813 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
814 (void) no_memory(hdl);
819 * Make sure this property is valid and applies to this type.
823 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
824 const char *propname = nvpair_name(elem);
826 prop = zfs_name_to_prop(propname);
827 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
829 * This is a user property: make sure it's a
830 * string, and that it's less than ZAP_MAXNAMELEN.
832 if (nvpair_type(elem) != DATA_TYPE_STRING) {
833 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
834 "'%s' must be a string"), propname);
835 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
839 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
841 "property name '%s' is too long"),
843 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
847 (void) nvpair_value_string(elem, &strval);
848 if (nvlist_add_string(ret, propname, strval) != 0) {
849 (void) no_memory(hdl);
856 * Currently, only user properties can be modified on
859 if (type == ZFS_TYPE_SNAPSHOT) {
860 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
861 "this property can not be modified for snapshots"));
862 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
866 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
867 zfs_userquota_prop_t uqtype;
868 char newpropname[128];
873 if (userquota_propname_decode(propname, zoned,
874 &uqtype, domain, sizeof (domain), &rid) != 0) {
876 dgettext(TEXT_DOMAIN,
877 "'%s' has an invalid user/group name"),
879 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
883 if (uqtype != ZFS_PROP_USERQUOTA &&
884 uqtype != ZFS_PROP_GROUPQUOTA) {
886 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
888 (void) zfs_error(hdl, EZFS_PROPREADONLY,
893 if (nvpair_type(elem) == DATA_TYPE_STRING) {
894 (void) nvpair_value_string(elem, &strval);
895 if (strcmp(strval, "none") == 0) {
897 } else if (zfs_nicestrtonum(hdl,
898 strval, &intval) != 0) {
899 (void) zfs_error(hdl,
900 EZFS_BADPROP, errbuf);
903 } else if (nvpair_type(elem) ==
905 (void) nvpair_value_uint64(elem, &intval);
907 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
908 "use 'none' to disable "
909 "userquota/groupquota"));
913 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
914 "'%s' must be a number"), propname);
915 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
920 * Encode the prop name as
921 * userquota@<hex-rid>-domain, to make it easy
922 * for the kernel to decode.
924 (void) snprintf(newpropname, sizeof (newpropname),
925 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
926 (longlong_t)rid, domain);
930 if (nvlist_add_uint64_array(ret, newpropname,
932 (void) no_memory(hdl);
936 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
944 if (prop == ZPROP_INVAL) {
945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
946 "invalid property '%s'"), propname);
947 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
951 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
953 dgettext(TEXT_DOMAIN, "'%s' does not "
954 "apply to datasets of this type"), propname);
955 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
959 if (zfs_prop_readonly(prop) &&
960 (!zfs_prop_setonce(prop) || zhp != NULL)) {
962 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
964 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
968 if (zprop_parse_value(hdl, elem, prop, type, ret,
969 &strval, &intval, errbuf) != 0)
973 * Perform some additional checks for specific properties.
976 case ZFS_PROP_VERSION:
982 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
983 if (intval < version) {
984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
985 "Can not downgrade; already at version %u"),
987 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
993 case ZFS_PROP_RECORDSIZE:
994 case ZFS_PROP_VOLBLOCKSIZE:
995 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
996 if (intval < SPA_MINBLOCKSIZE ||
997 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
999 "'%s' must be power of 2 from %u "
1000 "to %uk"), propname,
1001 (uint_t)SPA_MINBLOCKSIZE,
1002 (uint_t)SPA_MAXBLOCKSIZE >> 10);
1003 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1008 case ZFS_PROP_MLSLABEL:
1010 #ifdef HAVE_MLSLABEL
1012 * Verify the mlslabel string and convert to
1013 * internal hex label string.
1017 char *hex = NULL; /* internal label string */
1019 /* Default value is already OK. */
1020 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1023 /* Verify the label can be converted to binary form */
1024 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1025 (str_to_label(strval, &new_sl, MAC_LABEL,
1026 L_NO_CORRECTION, NULL) == -1)) {
1030 /* Now translate to hex internal label string */
1031 if (label_to_str(new_sl, &hex, M_INTERNAL,
1037 m_label_free(new_sl);
1039 /* If string is already in internal form, we're done. */
1040 if (strcmp(strval, hex) == 0) {
1045 /* Replace the label string with the internal form. */
1046 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1048 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1055 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1056 "invalid mlslabel '%s'"), strval);
1057 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1058 m_label_free(new_sl); /* OK if null */
1061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 "mlslabels are unsupported"));
1063 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1065 #endif /* HAVE_MLSLABEL */
1068 case ZFS_PROP_MOUNTPOINT:
1070 namecheck_err_t why;
1072 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1073 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1076 if (mountpoint_namecheck(strval, &why)) {
1078 case NAME_ERR_LEADING_SLASH:
1080 dgettext(TEXT_DOMAIN,
1081 "'%s' must be an absolute path, "
1082 "'none', or 'legacy'"), propname);
1084 case NAME_ERR_TOOLONG:
1086 dgettext(TEXT_DOMAIN,
1087 "component of '%s' is too long"),
1093 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1100 case ZFS_PROP_SHARESMB:
1101 case ZFS_PROP_SHARENFS:
1103 * For the mountpoint and sharenfs or sharesmb
1104 * properties, check if it can be set in a
1105 * global/non-global zone based on
1106 * the zoned property value:
1108 * global zone non-global zone
1109 * --------------------------------------------------
1110 * zoned=on mountpoint (no) mountpoint (yes)
1111 * sharenfs (no) sharenfs (no)
1112 * sharesmb (no) sharesmb (no)
1114 * zoned=off mountpoint (yes) N/A
1119 if (getzoneid() == GLOBAL_ZONEID) {
1120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1121 "'%s' cannot be set on "
1122 "dataset in a non-global zone"),
1124 (void) zfs_error(hdl, EZFS_ZONED,
1127 } else if (prop == ZFS_PROP_SHARENFS ||
1128 prop == ZFS_PROP_SHARESMB) {
1129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1130 "'%s' cannot be set in "
1131 "a non-global zone"), propname);
1132 (void) zfs_error(hdl, EZFS_ZONED,
1136 } else if (getzoneid() != GLOBAL_ZONEID) {
1138 * If zoned property is 'off', this must be in
1139 * a global zone. If not, something is wrong.
1141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1142 "'%s' cannot be set while dataset "
1143 "'zoned' property is set"), propname);
1144 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1149 * At this point, it is legitimate to set the
1150 * property. Now we want to make sure that the
1151 * property value is valid if it is sharenfs.
1153 if ((prop == ZFS_PROP_SHARENFS ||
1154 prop == ZFS_PROP_SHARESMB) &&
1155 strcmp(strval, "on") != 0 &&
1156 strcmp(strval, "off") != 0) {
1157 zfs_share_proto_t proto;
1159 if (prop == ZFS_PROP_SHARESMB)
1165 * Must be an valid sharing protocol
1166 * option string so init the libshare
1167 * in order to enable the parser and
1168 * then parse the options. We use the
1169 * control API since we don't care about
1170 * the current configuration and don't
1171 * want the overhead of loading it
1172 * until we actually do something.
1175 if (zfs_init_libshare(hdl,
1176 SA_INIT_CONTROL_API) != SA_OK) {
1178 * An error occurred so we can't do
1181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1182 "'%s' cannot be set: problem "
1183 "in share initialization"),
1185 (void) zfs_error(hdl, EZFS_BADPROP,
1190 if (zfs_parse_options(strval, proto) != SA_OK) {
1192 * There was an error in parsing so
1193 * deal with it by issuing an error
1194 * message and leaving after
1195 * uninitializing the the libshare
1198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1199 "'%s' cannot be set to invalid "
1200 "options"), propname);
1201 (void) zfs_error(hdl, EZFS_BADPROP,
1203 zfs_uninit_libshare(hdl);
1206 zfs_uninit_libshare(hdl);
1210 case ZFS_PROP_UTF8ONLY:
1211 chosen_utf = (int)intval;
1213 case ZFS_PROP_NORMALIZE:
1214 chosen_normal = (int)intval;
1221 * For changes to existing volumes, we have some additional
1222 * checks to enforce.
1224 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1225 uint64_t volsize = zfs_prop_get_int(zhp,
1227 uint64_t blocksize = zfs_prop_get_int(zhp,
1228 ZFS_PROP_VOLBLOCKSIZE);
1232 case ZFS_PROP_RESERVATION:
1233 case ZFS_PROP_REFRESERVATION:
1234 if (intval > volsize) {
1235 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1236 "'%s' is greater than current "
1237 "volume size"), propname);
1238 (void) zfs_error(hdl, EZFS_BADPROP,
1244 case ZFS_PROP_VOLSIZE:
1245 if (intval % blocksize != 0) {
1246 zfs_nicenum(blocksize, buf,
1248 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1249 "'%s' must be a multiple of "
1250 "volume block size (%s)"),
1252 (void) zfs_error(hdl, EZFS_BADPROP,
1258 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1259 "'%s' cannot be zero"),
1261 (void) zfs_error(hdl, EZFS_BADPROP,
1273 * If normalization was chosen, but no UTF8 choice was made,
1274 * enforce rejection of non-UTF8 names.
1276 * If normalization was chosen, but rejecting non-UTF8 names
1277 * was explicitly not chosen, it is an error.
1279 if (chosen_normal > 0 && chosen_utf < 0) {
1280 if (nvlist_add_uint64(ret,
1281 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1282 (void) no_memory(hdl);
1285 } else if (chosen_normal > 0 && chosen_utf == 0) {
1286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1287 "'%s' must be set 'on' if normalization chosen"),
1288 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1289 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1300 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1302 uint64_t old_volsize;
1303 uint64_t new_volsize;
1304 uint64_t old_reservation;
1305 uint64_t new_reservation;
1306 zfs_prop_t resv_prop;
1309 * If this is an existing volume, and someone is setting the volsize,
1310 * make sure that it matches the reservation, or add it if necessary.
1312 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1313 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1315 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1316 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1317 old_reservation) || nvlist_lookup_uint64(nvl,
1318 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1321 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1324 new_reservation = zvol_volsize_to_reservation(new_volsize,
1326 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1327 new_reservation) != 0) {
1328 (void) no_memory(zhp->zfs_hdl);
1335 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1342 * For quotas and reservations, ENOSPC indicates
1343 * something different; setting a quota or reservation
1344 * doesn't use any disk space.
1347 case ZFS_PROP_QUOTA:
1348 case ZFS_PROP_REFQUOTA:
1349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1350 "size is less than current used or "
1352 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1355 case ZFS_PROP_RESERVATION:
1356 case ZFS_PROP_REFRESERVATION:
1357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1358 "size is greater than available space"));
1359 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1363 (void) zfs_standard_error(hdl, err, errbuf);
1369 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1373 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1378 "pool and or dataset must be upgraded to set this "
1379 "property or value"));
1380 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1384 if (prop == ZFS_PROP_COMPRESSION) {
1385 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1386 "property setting is not allowed on "
1387 "bootable datasets"));
1388 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1390 (void) zfs_standard_error(hdl, err, errbuf);
1395 if (prop == ZPROP_INVAL) {
1396 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1398 (void) zfs_standard_error(hdl, err, errbuf);
1404 * This platform can't address a volume this big.
1407 if (prop == ZFS_PROP_VOLSIZE) {
1408 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1414 (void) zfs_standard_error(hdl, err, errbuf);
1419 zfs_is_namespace_prop(zfs_prop_t prop)
1423 case ZFS_PROP_ATIME:
1424 case ZFS_PROP_RELATIME:
1425 case ZFS_PROP_DEVICES:
1427 case ZFS_PROP_SETUID:
1428 case ZFS_PROP_READONLY:
1429 case ZFS_PROP_XATTR:
1430 case ZFS_PROP_NBMAND:
1439 * Given a property name and value, set the property for the given dataset.
1442 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1444 zfs_cmd_t zc = {"\0"};
1446 prop_changelist_t *cl = NULL;
1448 libzfs_handle_t *hdl = zhp->zfs_hdl;
1449 nvlist_t *nvl = NULL, *realprops;
1451 boolean_t do_prefix = B_TRUE;
1454 (void) snprintf(errbuf, sizeof (errbuf),
1455 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1458 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1459 nvlist_add_string(nvl, propname, propval) != 0) {
1460 (void) no_memory(hdl);
1464 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1465 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1471 prop = zfs_name_to_prop(propname);
1473 if (prop == ZFS_PROP_VOLSIZE) {
1474 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1478 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1481 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1483 "child dataset with inherited mountpoint is used "
1484 "in a non-global zone"));
1485 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1490 * We don't want to unmount & remount the dataset when changing
1491 * its canmount property to 'on' or 'noauto'. We only use
1492 * the changelist logic to unmount when setting canmount=off.
1494 if (prop == ZFS_PROP_CANMOUNT) {
1496 int err = zprop_string_to_index(prop, propval, &idx,
1498 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1499 do_prefix = B_FALSE;
1502 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1506 * Execute the corresponding ioctl() to set this property.
1508 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1510 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1513 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1516 zfs_setprop_error(hdl, prop, errno, errbuf);
1517 if (added_resv && errno == ENOSPC) {
1518 /* clean up the volsize property we tried to set */
1519 uint64_t old_volsize = zfs_prop_get_int(zhp,
1522 zcmd_free_nvlists(&zc);
1523 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1525 if (nvlist_add_uint64(nvl,
1526 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1529 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1531 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1535 ret = changelist_postfix(cl);
1539 * Refresh the statistics so the new property
1540 * value is reflected.
1542 (void) get_stats(zhp);
1545 * Remount the filesystem to propagate the change
1546 * if one of the options handled by the generic
1547 * Linux namespace layer has been modified.
1549 if (zfs_is_namespace_prop(prop) &&
1550 zfs_is_mounted(zhp, NULL))
1551 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1557 zcmd_free_nvlists(&zc);
1559 changelist_free(cl);
1564 * Given a property, inherit the value from the parent dataset, or if received
1565 * is TRUE, revert to the received value, if any.
1568 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1570 zfs_cmd_t zc = {"\0"};
1572 prop_changelist_t *cl;
1573 libzfs_handle_t *hdl = zhp->zfs_hdl;
1577 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1578 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1580 zc.zc_cookie = received;
1581 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1583 * For user properties, the amount of work we have to do is very
1584 * small, so just do it here.
1586 if (!zfs_prop_user(propname)) {
1587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1588 "invalid property"));
1589 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1592 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1593 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1595 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1596 return (zfs_standard_error(hdl, errno, errbuf));
1602 * Verify that this property is inheritable.
1604 if (zfs_prop_readonly(prop))
1605 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1607 if (!zfs_prop_inheritable(prop) && !received)
1608 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1611 * Check to see if the value applies to this type
1613 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
1614 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1617 * Normalize the name, to get rid of shorthand abbreviations.
1619 propname = zfs_prop_to_name(prop);
1620 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1621 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1623 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1624 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1626 "dataset is used in a non-global zone"));
1627 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1631 * Determine datasets which will be affected by this change, if any.
1633 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1636 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1638 "child dataset with inherited mountpoint is used "
1639 "in a non-global zone"));
1640 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1644 if ((ret = changelist_prefix(cl)) != 0)
1647 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1648 return (zfs_standard_error(hdl, errno, errbuf));
1651 if ((ret = changelist_postfix(cl)) != 0)
1655 * Refresh the statistics so the new property is reflected.
1657 (void) get_stats(zhp);
1660 * Remount the filesystem to propagate the change
1661 * if one of the options handled by the generic
1662 * Linux namespace layer has been modified.
1664 if (zfs_is_namespace_prop(prop) &&
1665 zfs_is_mounted(zhp, NULL))
1666 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1670 changelist_free(cl);
1675 * True DSL properties are stored in an nvlist. The following two functions
1676 * extract them appropriately.
1679 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1685 if (nvlist_lookup_nvlist(zhp->zfs_props,
1686 zfs_prop_to_name(prop), &nv) == 0) {
1687 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1688 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1690 verify(!zhp->zfs_props_table ||
1691 zhp->zfs_props_table[prop] == B_TRUE);
1692 value = zfs_prop_default_numeric(prop);
1700 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1706 if (nvlist_lookup_nvlist(zhp->zfs_props,
1707 zfs_prop_to_name(prop), &nv) == 0) {
1708 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1709 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1711 verify(!zhp->zfs_props_table ||
1712 zhp->zfs_props_table[prop] == B_TRUE);
1713 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1722 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1724 return (zhp->zfs_props == zhp->zfs_recvd_props);
1728 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1730 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1731 zhp->zfs_props = zhp->zfs_recvd_props;
1735 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1737 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1742 * Internal function for getting a numeric property. Both zfs_prop_get() and
1743 * zfs_prop_get_int() are built using this interface.
1745 * Certain properties can be overridden using 'mount -o'. In this case, scan
1746 * the contents of the /etc/mtab entry, searching for the appropriate options.
1747 * If they differ from the on-disk values, report the current values and mark
1748 * the source "temporary".
1751 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1752 char **source, uint64_t *val)
1754 zfs_cmd_t zc = {"\0"};
1755 nvlist_t *zplprops = NULL;
1757 char *mntopt_on = NULL;
1758 char *mntopt_off = NULL;
1759 boolean_t received = zfs_is_recvd_props_mode(zhp);
1764 * If the property is being fetched for a snapshot, check whether
1765 * the property is valid for the snapshot's head dataset type.
1767 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
1768 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
1769 *val = zfs_prop_default_numeric(prop);
1774 case ZFS_PROP_ATIME:
1775 mntopt_on = MNTOPT_ATIME;
1776 mntopt_off = MNTOPT_NOATIME;
1779 case ZFS_PROP_RELATIME:
1780 mntopt_on = MNTOPT_RELATIME;
1781 mntopt_off = MNTOPT_NORELATIME;
1784 case ZFS_PROP_DEVICES:
1785 mntopt_on = MNTOPT_DEVICES;
1786 mntopt_off = MNTOPT_NODEVICES;
1790 mntopt_on = MNTOPT_EXEC;
1791 mntopt_off = MNTOPT_NOEXEC;
1794 case ZFS_PROP_READONLY:
1795 mntopt_on = MNTOPT_RO;
1796 mntopt_off = MNTOPT_RW;
1799 case ZFS_PROP_SETUID:
1800 mntopt_on = MNTOPT_SETUID;
1801 mntopt_off = MNTOPT_NOSETUID;
1804 case ZFS_PROP_XATTR:
1805 mntopt_on = MNTOPT_XATTR;
1806 mntopt_off = MNTOPT_NOXATTR;
1809 case ZFS_PROP_NBMAND:
1810 mntopt_on = MNTOPT_NBMAND;
1811 mntopt_off = MNTOPT_NONBMAND;
1818 * Because looking up the mount options is potentially expensive
1819 * (iterating over all of /etc/mtab), we defer its calculation until
1820 * we're looking up a property which requires its presence.
1822 if (!zhp->zfs_mntcheck &&
1823 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1824 libzfs_handle_t *hdl = zhp->zfs_hdl;
1825 struct mnttab entry;
1827 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1828 zhp->zfs_mntopts = zfs_strdup(hdl,
1830 if (zhp->zfs_mntopts == NULL)
1834 zhp->zfs_mntcheck = B_TRUE;
1837 if (zhp->zfs_mntopts == NULL)
1838 mnt.mnt_mntopts = "";
1840 mnt.mnt_mntopts = zhp->zfs_mntopts;
1843 case ZFS_PROP_DEVICES:
1845 case ZFS_PROP_READONLY:
1846 case ZFS_PROP_SETUID:
1847 case ZFS_PROP_XATTR:
1848 case ZFS_PROP_NBMAND:
1849 *val = getprop_uint64(zhp, prop, source);
1854 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1857 *src = ZPROP_SRC_TEMPORARY;
1858 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1861 *src = ZPROP_SRC_TEMPORARY;
1865 case ZFS_PROP_ATIME:
1866 case ZFS_PROP_RELATIME:
1867 case ZFS_PROP_CANMOUNT:
1868 case ZFS_PROP_VOLSIZE:
1869 case ZFS_PROP_QUOTA:
1870 case ZFS_PROP_REFQUOTA:
1871 case ZFS_PROP_RESERVATION:
1872 case ZFS_PROP_REFRESERVATION:
1873 *val = getprop_uint64(zhp, prop, source);
1875 if (*source == NULL) {
1876 /* not default, must be local */
1877 *source = zhp->zfs_name;
1881 case ZFS_PROP_MOUNTED:
1882 *val = (zhp->zfs_mntopts != NULL);
1885 case ZFS_PROP_NUMCLONES:
1886 *val = zhp->zfs_dmustats.dds_num_clones;
1889 case ZFS_PROP_VERSION:
1890 case ZFS_PROP_NORMALIZE:
1891 case ZFS_PROP_UTF8ONLY:
1893 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1895 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1896 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1897 zcmd_free_nvlists(&zc);
1900 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1901 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1903 zcmd_free_nvlists(&zc);
1907 nvlist_free(zplprops);
1908 zcmd_free_nvlists(&zc);
1911 case ZFS_PROP_INCONSISTENT:
1912 *val = zhp->zfs_dmustats.dds_inconsistent;
1916 switch (zfs_prop_get_type(prop)) {
1917 case PROP_TYPE_NUMBER:
1918 case PROP_TYPE_INDEX:
1919 *val = getprop_uint64(zhp, prop, source);
1921 * If we tried to use a default value for a
1922 * readonly property, it means that it was not
1925 if (zfs_prop_readonly(prop) &&
1926 *source != NULL && (*source)[0] == '\0') {
1931 case PROP_TYPE_STRING:
1933 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1934 "cannot get non-numeric property"));
1935 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1936 dgettext(TEXT_DOMAIN, "internal error")));
1944 * Calculate the source type, given the raw source string.
1947 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1948 char *statbuf, size_t statlen)
1950 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1953 if (source == NULL) {
1954 *srctype = ZPROP_SRC_NONE;
1955 } else if (source[0] == '\0') {
1956 *srctype = ZPROP_SRC_DEFAULT;
1957 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1958 *srctype = ZPROP_SRC_RECEIVED;
1960 if (strcmp(source, zhp->zfs_name) == 0) {
1961 *srctype = ZPROP_SRC_LOCAL;
1963 (void) strlcpy(statbuf, source, statlen);
1964 *srctype = ZPROP_SRC_INHERITED;
1971 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1972 size_t proplen, boolean_t literal)
1977 if (zhp->zfs_recvd_props == NULL)
1978 if (get_recvd_props_ioctl(zhp) != 0)
1981 prop = zfs_name_to_prop(propname);
1983 if (prop != ZPROP_INVAL) {
1985 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1987 zfs_set_recvd_props_mode(zhp, &cookie);
1988 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1989 NULL, NULL, 0, literal);
1990 zfs_unset_recvd_props_mode(zhp, &cookie);
1994 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1995 propname, &propval) != 0)
1997 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1999 (void) strlcpy(propbuf, recvdval, proplen);
2002 return (err == 0 ? 0 : -1);
2006 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2011 value = zfs_get_clones_nvl(zhp);
2016 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2017 pair = nvlist_next_nvpair(value, pair)) {
2018 if (propbuf[0] != '\0')
2019 (void) strlcat(propbuf, ",", proplen);
2020 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2026 struct get_clones_arg {
2030 char buf[ZFS_MAXNAMELEN];
2034 get_clones_cb(zfs_handle_t *zhp, void *arg)
2036 struct get_clones_arg *gca = arg;
2038 if (gca->numclones == 0) {
2043 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2044 NULL, NULL, 0, B_TRUE) != 0)
2046 if (strcmp(gca->buf, gca->origin) == 0) {
2047 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2052 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2058 zfs_get_clones_nvl(zfs_handle_t *zhp)
2060 nvlist_t *nv, *value;
2062 if (nvlist_lookup_nvlist(zhp->zfs_props,
2063 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2064 struct get_clones_arg gca;
2067 * if this is a snapshot, then the kernel wasn't able
2068 * to get the clones. Do it by slowly iterating.
2070 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2072 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2074 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2079 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2081 gca.origin = zhp->zfs_name;
2083 if (gca.numclones != 0) {
2085 char pool[ZFS_MAXNAMELEN];
2088 /* get the pool name */
2089 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2090 (void) strsep(&cp, "/@");
2091 root = zfs_open(zhp->zfs_hdl, pool,
2092 ZFS_TYPE_FILESYSTEM);
2094 (void) get_clones_cb(root, &gca);
2097 if (gca.numclones != 0 ||
2098 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2099 nvlist_add_nvlist(zhp->zfs_props,
2100 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2107 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2108 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2111 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2117 * Retrieve a property from the given object. If 'literal' is specified, then
2118 * numbers are left as exact values. Otherwise, numbers are converted to a
2119 * human-readable form.
2121 * Returns 0 on success, or -1 on error.
2124 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2125 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2127 char *source = NULL;
2131 boolean_t received = zfs_is_recvd_props_mode(zhp);
2134 * Check to see if this property applies to our object
2136 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2139 if (received && zfs_prop_readonly(prop))
2143 *src = ZPROP_SRC_NONE;
2146 case ZFS_PROP_CREATION:
2148 * 'creation' is a time_t stored in the statistics. We convert
2149 * this into a string unless 'literal' is specified.
2152 val = getprop_uint64(zhp, prop, &source);
2153 time_t time = (time_t)val;
2157 localtime_r(&time, &t) == NULL ||
2158 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2160 (void) snprintf(propbuf, proplen, "%llu",
2161 (u_longlong_t) val);
2165 case ZFS_PROP_MOUNTPOINT:
2167 * Getting the precise mountpoint can be tricky.
2169 * - for 'none' or 'legacy', return those values.
2170 * - for inherited mountpoints, we want to take everything
2171 * after our ancestor and append it to the inherited value.
2173 * If the pool has an alternate root, we want to prepend that
2174 * root to any values we return.
2177 str = getprop_string(zhp, prop, &source);
2179 if (str[0] == '/') {
2180 char buf[MAXPATHLEN];
2182 const char *relpath;
2185 * If we inherit the mountpoint, even from a dataset
2186 * with a received value, the source will be the path of
2187 * the dataset we inherit from. If source is
2188 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2191 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2194 relpath = zhp->zfs_name + strlen(source);
2195 if (relpath[0] == '/')
2199 if ((zpool_get_prop(zhp->zpool_hdl,
2200 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2201 (strcmp(root, "-") == 0))
2204 * Special case an alternate root of '/'. This will
2205 * avoid having multiple leading slashes in the
2208 if (strcmp(root, "/") == 0)
2212 * If the mountpoint is '/' then skip over this
2213 * if we are obtaining either an alternate root or
2214 * an inherited mountpoint.
2216 if (str[1] == '\0' && (root[0] != '\0' ||
2217 relpath[0] != '\0'))
2220 if (relpath[0] == '\0')
2221 (void) snprintf(propbuf, proplen, "%s%s",
2224 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2225 root, str, relpath[0] == '@' ? "" : "/",
2228 /* 'legacy' or 'none' */
2229 (void) strlcpy(propbuf, str, proplen);
2234 case ZFS_PROP_ORIGIN:
2235 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2238 * If there is no parent at all, return failure to indicate that
2239 * it doesn't apply to this dataset.
2241 if (propbuf[0] == '\0')
2245 case ZFS_PROP_CLONES:
2246 if (get_clones_string(zhp, propbuf, proplen) != 0)
2250 case ZFS_PROP_QUOTA:
2251 case ZFS_PROP_REFQUOTA:
2252 case ZFS_PROP_RESERVATION:
2253 case ZFS_PROP_REFRESERVATION:
2255 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2259 * If quota or reservation is 0, we translate this into 'none'
2260 * (unless literal is set), and indicate that it's the default
2261 * value. Otherwise, we print the number nicely and indicate
2262 * that its set locally.
2266 (void) strlcpy(propbuf, "0", proplen);
2268 (void) strlcpy(propbuf, "none", proplen);
2271 (void) snprintf(propbuf, proplen, "%llu",
2274 zfs_nicenum(val, propbuf, proplen);
2278 case ZFS_PROP_REFRATIO:
2279 case ZFS_PROP_COMPRESSRATIO:
2280 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2282 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2283 (u_longlong_t)(val / 100),
2284 (u_longlong_t)(val % 100));
2288 switch (zhp->zfs_type) {
2289 case ZFS_TYPE_FILESYSTEM:
2292 case ZFS_TYPE_VOLUME:
2295 case ZFS_TYPE_SNAPSHOT:
2301 (void) snprintf(propbuf, proplen, "%s", str);
2304 case ZFS_PROP_MOUNTED:
2306 * The 'mounted' property is a pseudo-property that described
2307 * whether the filesystem is currently mounted. Even though
2308 * it's a boolean value, the typical values of "on" and "off"
2309 * don't make sense, so we translate to "yes" and "no".
2311 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2312 src, &source, &val) != 0)
2315 (void) strlcpy(propbuf, "yes", proplen);
2317 (void) strlcpy(propbuf, "no", proplen);
2322 * The 'name' property is a pseudo-property derived from the
2323 * dataset name. It is presented as a real property to simplify
2326 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2329 case ZFS_PROP_MLSLABEL:
2331 #ifdef HAVE_MLSLABEL
2332 m_label_t *new_sl = NULL;
2333 char *ascii = NULL; /* human readable label */
2335 (void) strlcpy(propbuf,
2336 getprop_string(zhp, prop, &source), proplen);
2338 if (literal || (strcasecmp(propbuf,
2339 ZFS_MLSLABEL_DEFAULT) == 0))
2343 * Try to translate the internal hex string to
2344 * human-readable output. If there are any
2345 * problems just use the hex string.
2348 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2349 L_NO_CORRECTION, NULL) == -1) {
2350 m_label_free(new_sl);
2354 if (label_to_str(new_sl, &ascii, M_LABEL,
2358 m_label_free(new_sl);
2361 m_label_free(new_sl);
2363 (void) strlcpy(propbuf, ascii, proplen);
2366 (void) strlcpy(propbuf,
2367 getprop_string(zhp, prop, &source), proplen);
2368 #endif /* HAVE_MLSLABEL */
2374 * GUIDs are stored as numbers, but they are identifiers.
2375 * We don't want them to be pretty printed, because pretty
2376 * printing mangles the ID into a truncated and useless value.
2378 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2380 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2384 switch (zfs_prop_get_type(prop)) {
2385 case PROP_TYPE_NUMBER:
2386 if (get_numeric_property(zhp, prop, src,
2387 &source, &val) != 0)
2390 (void) snprintf(propbuf, proplen, "%llu",
2393 zfs_nicenum(val, propbuf, proplen);
2396 case PROP_TYPE_STRING:
2397 (void) strlcpy(propbuf,
2398 getprop_string(zhp, prop, &source), proplen);
2401 case PROP_TYPE_INDEX:
2402 if (get_numeric_property(zhp, prop, src,
2403 &source, &val) != 0)
2405 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2407 (void) strlcpy(propbuf, strval, proplen);
2415 get_source(zhp, src, source, statbuf, statlen);
2421 * Utility function to get the given numeric property. Does no validation that
2422 * the given property is the appropriate type; should only be used with
2423 * hard-coded property types.
2426 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2431 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2437 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2441 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2442 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2446 * Similar to zfs_prop_get(), but returns the value as an integer.
2449 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2450 zprop_source_t *src, char *statbuf, size_t statlen)
2455 * Check to see if this property applies to our object
2457 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
2458 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2459 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2460 zfs_prop_to_name(prop)));
2464 *src = ZPROP_SRC_NONE;
2466 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2469 get_source(zhp, src, source, statbuf, statlen);
2476 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2477 char **domainp, idmap_rid_t *ridp)
2479 idmap_get_handle_t *get_hdl = NULL;
2483 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2487 err = idmap_get_sidbyuid(get_hdl, id,
2488 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2490 err = idmap_get_sidbygid(get_hdl, id,
2491 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2493 if (err == IDMAP_SUCCESS &&
2494 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2495 status == IDMAP_SUCCESS)
2501 idmap_get_destroy(get_hdl);
2504 #endif /* HAVE_IDMAP */
2507 * convert the propname into parameters needed by kernel
2508 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2509 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2510 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2511 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2514 userquota_propname_decode(const char *propname, boolean_t zoned,
2515 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2517 zfs_userquota_prop_t type;
2526 /* Figure out the property type ({user|group}{quota|space}) */
2527 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2528 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2529 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2532 if (type == ZFS_NUM_USERQUOTA_PROPS)
2536 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2537 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2539 cp = strchr(propname, '@') + 1;
2541 if (isuser && (pw = getpwnam(cp)) != NULL) {
2542 if (zoned && getzoneid() == GLOBAL_ZONEID)
2545 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2546 if (zoned && getzoneid() == GLOBAL_ZONEID)
2549 } else if (strchr(cp, '@')) {
2552 * It's a SID name (eg "user@domain") that needs to be
2553 * turned into S-1-domainID-RID.
2555 directory_error_t e;
2556 char *numericsid = NULL;
2559 if (zoned && getzoneid() == GLOBAL_ZONEID)
2562 e = directory_sid_from_user_name(NULL,
2565 e = directory_sid_from_group_name(NULL,
2569 directory_error_free(e);
2572 if (numericsid == NULL)
2575 (void) strlcpy(domain, cp, domainlen);
2576 cp = strrchr(domain, '-');
2581 *ridp = strtoull(cp, &end, 10);
2584 if (errno != 0 || *end != '\0')
2588 #endif /* HAVE_IDMAP */
2590 /* It's a user/group ID (eg "12345"). */
2593 id = strtoul(cp, &end, 10);
2598 /* It's an ephemeral ID. */
2602 if (idmap_id_to_numeric_domain_rid(id, isuser,
2603 &mapdomain, &rid) != 0)
2605 (void) strlcpy(domain, mapdomain, domainlen);
2609 #endif /* HAVE_IDMAP */
2619 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2620 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2623 zfs_cmd_t zc = {"\0"};
2625 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2627 err = userquota_propname_decode(propname,
2628 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2629 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2630 zc.zc_objset_type = *typep;
2634 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2638 *propvalue = zc.zc_cookie;
2643 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2644 uint64_t *propvalue)
2646 zfs_userquota_prop_t type;
2648 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2653 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2654 char *propbuf, int proplen, boolean_t literal)
2658 zfs_userquota_prop_t type;
2660 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2667 (void) snprintf(propbuf, proplen, "%llu",
2668 (u_longlong_t)propvalue);
2669 } else if (propvalue == 0 &&
2670 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2671 (void) strlcpy(propbuf, "none", proplen);
2673 zfs_nicenum(propvalue, propbuf, proplen);
2679 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2680 uint64_t *propvalue)
2683 zfs_cmd_t zc = {"\0"};
2684 const char *snapname;
2686 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2688 snapname = strchr(propname, '@') + 1;
2689 if (strchr(snapname, '@')) {
2690 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2692 /* snapname is the short name, append it to zhp's fsname */
2695 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2696 sizeof (zc.zc_value));
2697 cp = strchr(zc.zc_value, '@');
2700 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2701 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2704 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2708 *propvalue = zc.zc_cookie;
2713 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2714 char *propbuf, int proplen, boolean_t literal)
2719 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2725 (void) snprintf(propbuf, proplen, "%llu",
2726 (u_longlong_t)propvalue);
2728 zfs_nicenum(propvalue, propbuf, proplen);
2735 * Returns the name of the given zfs handle.
2738 zfs_get_name(const zfs_handle_t *zhp)
2740 return (zhp->zfs_name);
2744 * Returns the type of the given zfs handle.
2747 zfs_get_type(const zfs_handle_t *zhp)
2749 return (zhp->zfs_type);
2753 * Is one dataset name a child dataset of another?
2755 * Needs to handle these cases:
2756 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2757 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2758 * Descendant? No. No. No. Yes.
2761 is_descendant(const char *ds1, const char *ds2)
2763 size_t d1len = strlen(ds1);
2765 /* ds2 can't be a descendant if it's smaller */
2766 if (strlen(ds2) < d1len)
2769 /* otherwise, compare strings and verify that there's a '/' char */
2770 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2774 * Given a complete name, return just the portion that refers to the parent.
2775 * Will return -1 if there is no parent (path is just the name of the
2779 parent_name(const char *path, char *buf, size_t buflen)
2783 (void) strlcpy(buf, path, buflen);
2785 if ((slashp = strrchr(buf, '/')) == NULL)
2793 * If accept_ancestor is false, then check to make sure that the given path has
2794 * a parent, and that it exists. If accept_ancestor is true, then find the
2795 * closest existing ancestor for the given path. In prefixlen return the
2796 * length of already existing prefix of the given path. We also fetch the
2797 * 'zoned' property, which is used to validate property settings when creating
2801 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2802 boolean_t accept_ancestor, int *prefixlen)
2804 zfs_cmd_t zc = {"\0"};
2805 char parent[ZFS_MAXNAMELEN];
2811 (void) snprintf(errbuf, sizeof (errbuf),
2812 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2814 /* get parent, and check to see if this is just a pool */
2815 if (parent_name(path, parent, sizeof (parent)) != 0) {
2816 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2817 "missing dataset name"));
2818 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2821 /* check to see if the pool exists */
2822 if ((slash = strchr(parent, '/')) == NULL)
2823 slash = parent + strlen(parent);
2824 (void) strncpy(zc.zc_name, parent, slash - parent);
2825 zc.zc_name[slash - parent] = '\0';
2826 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2829 "no such pool '%s'"), zc.zc_name);
2830 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2833 /* check to see if the parent dataset exists */
2834 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2835 if (errno == ENOENT && accept_ancestor) {
2837 * Go deeper to find an ancestor, give up on top level.
2839 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2841 "no such pool '%s'"), zc.zc_name);
2842 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2844 } else if (errno == ENOENT) {
2845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2846 "parent does not exist"));
2847 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2849 return (zfs_standard_error(hdl, errno, errbuf));
2852 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2856 /* we are in a non-global zone, but parent is in the global zone */
2857 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2858 (void) zfs_standard_error(hdl, EPERM, errbuf);
2863 /* make sure parent is a filesystem */
2864 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2865 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2866 "parent is not a filesystem"));
2867 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2873 if (prefixlen != NULL)
2874 *prefixlen = strlen(parent);
2879 * Finds whether the dataset of the given type(s) exists.
2882 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2886 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2890 * Try to get stats for the dataset, which will tell us if it exists.
2892 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2893 int ds_type = zhp->zfs_type;
2896 if (types & ds_type)
2903 * Given a path to 'target', create all the ancestors between
2904 * the prefixlen portion of the path, and the target itself.
2905 * Fail if the initial prefixlen-ancestor does not already exist.
2908 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2914 /* make sure prefix exists */
2915 cp = target + prefixlen;
2917 assert(strchr(cp, '/') == NULL);
2918 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2921 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2929 * Attempt to create, mount, and share any ancestor filesystems,
2930 * up to the prefixlen-long one.
2932 for (cp = target + prefixlen + 1;
2933 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2937 h = make_dataset_handle(hdl, target);
2939 /* it already exists, nothing to do here */
2944 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2946 opname = dgettext(TEXT_DOMAIN, "create");
2950 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2952 opname = dgettext(TEXT_DOMAIN, "open");
2956 if (zfs_mount(h, NULL, 0) != 0) {
2957 opname = dgettext(TEXT_DOMAIN, "mount");
2961 if (zfs_share(h) != 0) {
2962 opname = dgettext(TEXT_DOMAIN, "share");
2972 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2973 "failed to %s ancestor '%s'"), opname, target);
2978 * Creates non-existing ancestors of the given path.
2981 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2987 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2990 if ((path_copy = strdup(path)) != NULL) {
2991 rc = create_parents(hdl, path_copy, prefix);
2994 if (path_copy == NULL || rc != 0)
3001 * Create a new filesystem or volume.
3004 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3009 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3012 dmu_objset_type_t ost;
3014 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3015 "cannot create '%s'"), path);
3017 /* validate the path, taking care to note the extended error message */
3018 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3019 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3021 /* validate parents exist */
3022 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3026 * The failure modes when creating a dataset of a different type over
3027 * one that already exists is a little strange. In particular, if you
3028 * try to create a dataset on top of an existing dataset, the ioctl()
3029 * will return ENOENT, not EEXIST. To prevent this from happening, we
3030 * first try to see if the dataset exists.
3032 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3033 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3034 "dataset already exists"));
3035 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3038 if (type == ZFS_TYPE_VOLUME)
3043 if (props && (props = zfs_valid_proplist(hdl, type, props,
3044 zoned, NULL, errbuf)) == 0)
3047 if (type == ZFS_TYPE_VOLUME) {
3049 * If we are creating a volume, the size and block size must
3050 * satisfy a few restraints. First, the blocksize must be a
3051 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3052 * volsize must be a multiple of the block size, and cannot be
3055 if (props == NULL || nvlist_lookup_uint64(props,
3056 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3058 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3059 "missing volume size"));
3060 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3063 if ((ret = nvlist_lookup_uint64(props,
3064 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3065 &blocksize)) != 0) {
3066 if (ret == ENOENT) {
3067 blocksize = zfs_prop_default_numeric(
3068 ZFS_PROP_VOLBLOCKSIZE);
3071 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3072 "missing volume block size"));
3073 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3080 "volume size cannot be zero"));
3081 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3084 if (size % blocksize != 0) {
3086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3087 "volume size must be a multiple of volume block "
3089 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3093 /* create the dataset */
3094 ret = lzc_create(path, ost, props);
3097 /* check for failure */
3099 char parent[ZFS_MAXNAMELEN];
3100 (void) parent_name(path, parent, sizeof (parent));
3104 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3105 "no such parent '%s'"), parent);
3106 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3110 "parent '%s' is not a filesystem"), parent);
3111 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3115 "volume block size must be power of 2 from "
3117 (uint_t)SPA_MINBLOCKSIZE,
3118 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3120 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3124 "pool must be upgraded to set this "
3125 "property or value"));
3126 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3130 * This platform can't address a volume this big.
3132 if (type == ZFS_TYPE_VOLUME)
3133 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3138 return (zfs_standard_error(hdl, errno, errbuf));
3146 * Destroys the given dataset. The caller must make sure that the filesystem
3147 * isn't mounted, and that there are no active dependents. If the file system
3148 * does not exist this function does nothing.
3151 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3153 zfs_cmd_t zc = {"\0"};
3155 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3157 if (ZFS_IS_VOLUME(zhp)) {
3158 zc.zc_objset_type = DMU_OST_ZVOL;
3160 zc.zc_objset_type = DMU_OST_ZFS;
3163 zc.zc_defer_destroy = defer;
3164 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3166 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3167 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3171 remove_mountpoint(zhp);
3176 struct destroydata {
3178 const char *snapname;
3182 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3184 struct destroydata *dd = arg;
3185 char name[ZFS_MAXNAMELEN];
3188 (void) snprintf(name, sizeof (name),
3189 "%s@%s", zhp->zfs_name, dd->snapname);
3191 if (lzc_exists(name))
3192 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3194 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3200 * Destroys all snapshots with the given name in zhp & descendants.
3203 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3206 struct destroydata dd = { 0 };
3208 dd.snapname = snapname;
3209 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3210 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3212 if (nvlist_empty(dd.nvl)) {
3213 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3214 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3215 zhp->zfs_name, snapname);
3217 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3219 nvlist_free(dd.nvl);
3224 * Destroys all the snapshots named in the nvlist.
3227 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3233 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3238 if (nvlist_empty(errlist)) {
3240 (void) snprintf(errbuf, sizeof (errbuf),
3241 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3243 ret = zfs_standard_error(hdl, ret, errbuf);
3245 for (pair = nvlist_next_nvpair(errlist, NULL);
3246 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3248 (void) snprintf(errbuf, sizeof (errbuf),
3249 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3252 switch (fnvpair_value_int32(pair)) {
3255 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3256 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3259 ret = zfs_standard_error(hdl, errno, errbuf);
3268 * Clones the given dataset. The target must be of the same type as the source.
3271 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3273 char parent[ZFS_MAXNAMELEN];
3276 libzfs_handle_t *hdl = zhp->zfs_hdl;
3279 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3281 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3282 "cannot create '%s'"), target);
3284 /* validate the target/clone name */
3285 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3286 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3288 /* validate parents exist */
3289 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3292 (void) parent_name(target, parent, sizeof (parent));
3298 if (ZFS_IS_VOLUME(zhp)) {
3299 type = ZFS_TYPE_VOLUME;
3301 type = ZFS_TYPE_FILESYSTEM;
3303 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3304 zhp, errbuf)) == NULL)
3308 ret = lzc_clone(target, zhp->zfs_name, props);
3316 * The parent doesn't exist. We should have caught this
3317 * above, but there may a race condition that has since
3318 * destroyed the parent.
3320 * At this point, we don't know whether it's the source
3321 * that doesn't exist anymore, or whether the target
3322 * dataset doesn't exist.
3324 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3325 "no such parent '%s'"), parent);
3326 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3329 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3330 "source and target pools differ"));
3331 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3335 return (zfs_standard_error(zhp->zfs_hdl, errno,
3344 * Promotes the given clone fs to be the clone parent.
3347 zfs_promote(zfs_handle_t *zhp)
3349 libzfs_handle_t *hdl = zhp->zfs_hdl;
3350 zfs_cmd_t zc = {"\0"};
3351 char parent[MAXPATHLEN];
3355 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3356 "cannot promote '%s'"), zhp->zfs_name);
3358 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3359 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3360 "snapshots can not be promoted"));
3361 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3364 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3365 if (parent[0] == '\0') {
3366 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3367 "not a cloned filesystem"));
3368 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3371 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3372 sizeof (zc.zc_value));
3373 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3374 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3377 int save_errno = errno;
3379 switch (save_errno) {
3381 /* There is a conflicting snapshot name. */
3382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3383 "conflicting snapshot '%s' from parent '%s'"),
3384 zc.zc_string, parent);
3385 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3388 return (zfs_standard_error(hdl, save_errno, errbuf));
3394 typedef struct snapdata {
3396 const char *sd_snapname;
3400 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3402 snapdata_t *sd = arg;
3403 char name[ZFS_MAXNAMELEN];
3406 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3407 (void) snprintf(name, sizeof (name),
3408 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3410 fnvlist_add_boolean(sd->sd_nvl, name);
3412 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3420 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3424 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3431 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3432 "cannot create snapshots "));
3435 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3436 const char *snapname = nvpair_name(elem);
3438 /* validate the target name */
3439 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3441 (void) snprintf(errbuf, sizeof (errbuf),
3442 dgettext(TEXT_DOMAIN,
3443 "cannot create snapshot '%s'"), snapname);
3444 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3448 if (props != NULL &&
3449 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3450 props, B_FALSE, NULL, errbuf)) == NULL) {
3454 ret = lzc_snapshot(snaps, props, &errors);
3457 boolean_t printed = B_FALSE;
3458 for (elem = nvlist_next_nvpair(errors, NULL);
3460 elem = nvlist_next_nvpair(errors, elem)) {
3461 (void) snprintf(errbuf, sizeof (errbuf),
3462 dgettext(TEXT_DOMAIN,
3463 "cannot create snapshot '%s'"), nvpair_name(elem));
3464 (void) zfs_standard_error(hdl,
3465 fnvpair_value_int32(elem), errbuf);
3471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3472 "multiple snapshots of same "
3474 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3478 (void) zfs_standard_error(hdl, ret, errbuf);
3484 nvlist_free(errors);
3489 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3493 snapdata_t sd = { 0 };
3494 char fsname[ZFS_MAXNAMELEN];
3499 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3500 "cannot snapshot %s"), path);
3502 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3503 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3505 (void) strlcpy(fsname, path, sizeof (fsname));
3506 cp = strchr(fsname, '@');
3508 sd.sd_snapname = cp + 1;
3510 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3511 ZFS_TYPE_VOLUME)) == NULL) {
3515 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3517 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3519 fnvlist_add_boolean(sd.sd_nvl, path);
3522 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3523 nvlist_free(sd.sd_nvl);
3529 * Destroy any more recent snapshots. We invoke this callback on any dependents
3530 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3531 * is a dependent and we should just destroy it without checking the transaction
3534 typedef struct rollback_data {
3535 const char *cb_target; /* the snapshot */
3536 uint64_t cb_create; /* creation time reference */
3538 boolean_t cb_dependent;
3543 rollback_destroy(zfs_handle_t *zhp, void *data)
3545 rollback_data_t *cbp = data;
3547 if (!cbp->cb_dependent) {
3548 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3549 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3550 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3553 cbp->cb_dependent = B_TRUE;
3554 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3555 rollback_destroy, cbp);
3556 cbp->cb_dependent = B_FALSE;
3558 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3561 /* We must destroy this clone; first unmount it */
3562 prop_changelist_t *clp;
3564 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3565 cbp->cb_force ? MS_FORCE: 0);
3566 if (clp == NULL || changelist_prefix(clp) != 0) {
3567 cbp->cb_error = B_TRUE;
3571 if (zfs_destroy(zhp, B_FALSE) != 0)
3572 cbp->cb_error = B_TRUE;
3574 changelist_remove(clp, zhp->zfs_name);
3575 (void) changelist_postfix(clp);
3576 changelist_free(clp);
3584 * Given a dataset, rollback to a specific snapshot, discarding any
3585 * data changes since then and making it the active dataset.
3587 * Any snapshots more recent than the target are destroyed, along with
3591 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3593 rollback_data_t cb = { 0 };
3595 boolean_t restore_resv = 0;
3596 uint64_t old_volsize = 0, new_volsize;
3597 zfs_prop_t resv_prop = { 0 };
3599 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3600 zhp->zfs_type == ZFS_TYPE_VOLUME);
3603 * Destroy all recent snapshots and their dependents.
3605 cb.cb_force = force;
3606 cb.cb_target = snap->zfs_name;
3607 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3608 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3614 * Now that we have verified that the snapshot is the latest,
3615 * rollback to the given snapshot.
3618 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3619 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3621 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3623 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3627 * We rely on zfs_iter_children() to verify that there are no
3628 * newer snapshots for the given dataset. Therefore, we can
3629 * simply pass the name on to the ioctl() call. There is still
3630 * an unlikely race condition where the user has taken a
3631 * snapshot since we verified that this was the most recent.
3633 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3635 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3636 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3642 * For volumes, if the pre-rollback volsize matched the pre-
3643 * rollback reservation and the volsize has changed then set
3644 * the reservation property to the post-rollback volsize.
3645 * Make a new handle since the rollback closed the dataset.
3647 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3648 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3650 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3651 if (old_volsize != new_volsize)
3652 err = zfs_prop_set_int(zhp, resv_prop,
3661 * Renames the given dataset.
3664 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3665 boolean_t force_unmount)
3668 zfs_cmd_t zc = {"\0"};
3670 prop_changelist_t *cl = NULL;
3671 zfs_handle_t *zhrp = NULL;
3672 char *parentname = NULL;
3673 char parent[ZFS_MAXNAMELEN];
3674 libzfs_handle_t *hdl = zhp->zfs_hdl;
3677 /* if we have the same exact name, just return success */
3678 if (strcmp(zhp->zfs_name, target) == 0)
3681 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3682 "cannot rename to '%s'"), target);
3685 * Make sure the target name is valid
3687 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3688 if ((strchr(target, '@') == NULL) ||
3691 * Snapshot target name is abbreviated,
3692 * reconstruct full dataset name
3694 (void) strlcpy(parent, zhp->zfs_name,
3696 delim = strchr(parent, '@');
3697 if (strchr(target, '@') == NULL)
3701 (void) strlcat(parent, target, sizeof (parent));
3705 * Make sure we're renaming within the same dataset.
3707 delim = strchr(target, '@');
3708 if (strncmp(zhp->zfs_name, target, delim - target)
3709 != 0 || zhp->zfs_name[delim - target] != '@') {
3710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3711 "snapshots must be part of same "
3713 return (zfs_error(hdl, EZFS_CROSSTARGET,
3717 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3718 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3721 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3722 "recursive rename must be a snapshot"));
3723 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3726 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3727 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3729 /* validate parents */
3730 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3733 /* make sure we're in the same pool */
3734 verify((delim = strchr(target, '/')) != NULL);
3735 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3736 zhp->zfs_name[delim - target] != '/') {
3737 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3738 "datasets must be within same pool"));
3739 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3742 /* new name cannot be a child of the current dataset name */
3743 if (is_descendant(zhp->zfs_name, target)) {
3744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3745 "New dataset name cannot be a descendant of "
3746 "current dataset name"));
3747 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3751 (void) snprintf(errbuf, sizeof (errbuf),
3752 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3754 if (getzoneid() == GLOBAL_ZONEID &&
3755 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3757 "dataset is used in a non-global zone"));
3758 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3763 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3764 if (parentname == NULL) {
3768 delim = strchr(parentname, '@');
3770 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3777 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3778 force_unmount ? MS_FORCE : 0)) == NULL)
3781 if (changelist_haszonedchild(cl)) {
3782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3783 "child dataset with inherited mountpoint is used "
3784 "in a non-global zone"));
3785 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3790 if ((ret = changelist_prefix(cl)) != 0)
3794 if (ZFS_IS_VOLUME(zhp))
3795 zc.zc_objset_type = DMU_OST_ZVOL;
3797 zc.zc_objset_type = DMU_OST_ZFS;
3799 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3800 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3802 zc.zc_cookie = recursive;
3804 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3806 * if it was recursive, the one that actually failed will
3809 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3810 "cannot rename '%s'"), zc.zc_name);
3812 if (recursive && errno == EEXIST) {
3813 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3814 "a child dataset already has a snapshot "
3815 "with the new name"));
3816 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3818 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3822 * On failure, we still want to remount any filesystems that
3823 * were previously mounted, so we don't alter the system state.
3826 (void) changelist_postfix(cl);
3829 changelist_rename(cl, zfs_get_name(zhp), target);
3830 ret = changelist_postfix(cl);
3842 changelist_free(cl);
3848 zfs_get_user_props(zfs_handle_t *zhp)
3850 return (zhp->zfs_user_props);
3854 * This function is used by 'zfs list' to determine the exact set of columns to
3855 * display, and their maximum widths. This does two main things:
3857 * - If this is a list of all properties, then expand the list to include
3858 * all native properties, and set a flag so that for each dataset we look
3859 * for new unique user properties and add them to the list.
3861 * - For non fixed-width properties, keep track of the maximum width seen
3862 * so that we can size the column appropriately. If the user has
3863 * requested received property values, we also need to compute the width
3864 * of the RECEIVED column.
3867 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
3870 libzfs_handle_t *hdl = zhp->zfs_hdl;
3871 zprop_list_t *entry;
3872 zprop_list_t **last, **start;
3873 nvlist_t *userprops, *propval;
3876 char buf[ZFS_MAXPROPLEN];
3878 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3881 userprops = zfs_get_user_props(zhp);
3884 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3886 * Go through and add any user properties as necessary. We
3887 * start by incrementing our list pointer to the first
3888 * non-native property.
3891 while (*start != NULL) {
3892 if ((*start)->pl_prop == ZPROP_INVAL)
3894 start = &(*start)->pl_next;
3898 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3900 * See if we've already found this property in our list.
3902 for (last = start; *last != NULL;
3903 last = &(*last)->pl_next) {
3904 if (strcmp((*last)->pl_user_prop,
3905 nvpair_name(elem)) == 0)
3909 if (*last == NULL) {
3910 if ((entry = zfs_alloc(hdl,
3911 sizeof (zprop_list_t))) == NULL ||
3912 ((entry->pl_user_prop = zfs_strdup(hdl,
3913 nvpair_name(elem)))) == NULL) {
3918 entry->pl_prop = ZPROP_INVAL;
3919 entry->pl_width = strlen(nvpair_name(elem));
3920 entry->pl_all = B_TRUE;
3927 * Now go through and check the width of any non-fixed columns
3929 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3930 if (entry->pl_fixed && !literal)
3933 if (entry->pl_prop != ZPROP_INVAL) {
3934 if (zfs_prop_get(zhp, entry->pl_prop,
3935 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
3936 if (strlen(buf) > entry->pl_width)
3937 entry->pl_width = strlen(buf);
3939 if (received && zfs_prop_get_recvd(zhp,
3940 zfs_prop_to_name(entry->pl_prop),
3941 buf, sizeof (buf), literal) == 0)
3942 if (strlen(buf) > entry->pl_recvd_width)
3943 entry->pl_recvd_width = strlen(buf);
3945 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3947 verify(nvlist_lookup_string(propval,
3948 ZPROP_VALUE, &strval) == 0);
3949 if (strlen(strval) > entry->pl_width)
3950 entry->pl_width = strlen(strval);
3952 if (received && zfs_prop_get_recvd(zhp,
3953 entry->pl_user_prop,
3954 buf, sizeof (buf), literal) == 0)
3955 if (strlen(buf) > entry->pl_recvd_width)
3956 entry->pl_recvd_width = strlen(buf);
3964 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3969 * Keep a reference to the props-table against which we prune the
3972 zhp->zfs_props_table = props;
3974 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3977 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3978 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3981 * User properties will result in ZPROP_INVAL, and since we
3982 * only know how to prune standard ZFS properties, we always
3983 * leave these in the list. This can also happen if we
3984 * encounter an unknown DSL property (when running older
3985 * software, for example).
3987 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3988 (void) nvlist_remove(zhp->zfs_props,
3989 nvpair_name(curr), nvpair_type(curr));
3995 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3996 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3998 zfs_cmd_t zc = {"\0"};
3999 nvlist_t *nvlist = NULL;
4002 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4003 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4004 zc.zc_cookie = (uint64_t)cmd;
4006 if (cmd == ZFS_SMB_ACL_RENAME) {
4007 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4008 (void) no_memory(hdl);
4014 case ZFS_SMB_ACL_ADD:
4015 case ZFS_SMB_ACL_REMOVE:
4016 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4018 case ZFS_SMB_ACL_RENAME:
4019 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4021 (void) no_memory(hdl);
4024 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4026 (void) no_memory(hdl);
4029 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4030 nvlist_free(nvlist);
4034 case ZFS_SMB_ACL_PURGE:
4039 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4041 nvlist_free(nvlist);
4046 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4047 char *path, char *resource)
4049 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4054 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4055 char *path, char *resource)
4057 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4062 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4064 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4069 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4070 char *oldname, char *newname)
4072 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4077 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4078 zfs_userspace_cb_t func, void *arg)
4080 zfs_cmd_t zc = {"\0"};
4081 zfs_useracct_t buf[100];
4082 libzfs_handle_t *hdl = zhp->zfs_hdl;
4085 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4087 zc.zc_objset_type = type;
4088 zc.zc_nvlist_dst = (uintptr_t)buf;
4091 zfs_useracct_t *zua = buf;
4093 zc.zc_nvlist_dst_size = sizeof (buf);
4094 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4095 char errbuf[ZFS_MAXNAMELEN + 32];
4097 (void) snprintf(errbuf, sizeof (errbuf),
4098 dgettext(TEXT_DOMAIN,
4099 "cannot get used/quota for %s"), zc.zc_name);
4100 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4102 if (zc.zc_nvlist_dst_size == 0)
4105 while (zc.zc_nvlist_dst_size > 0) {
4106 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4107 zua->zu_space)) != 0)
4110 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4119 const char *snapname;
4121 boolean_t recursive;
4126 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4128 struct holdarg *ha = arg;
4129 char name[ZFS_MAXNAMELEN];
4132 (void) snprintf(name, sizeof (name),
4133 "%s@%s", zhp->zfs_name, ha->snapname);
4135 if (lzc_exists(name))
4136 fnvlist_add_string(ha->nvl, name, ha->tag);
4139 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4145 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4146 boolean_t recursive, int cleanup_fd)
4151 ha.nvl = fnvlist_alloc();
4152 ha.snapname = snapname;
4154 ha.recursive = recursive;
4155 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4157 if (nvlist_empty(ha.nvl)) {
4160 fnvlist_free(ha.nvl);
4162 (void) snprintf(errbuf, sizeof (errbuf),
4163 dgettext(TEXT_DOMAIN,
4164 "cannot hold snapshot '%s@%s'"),
4165 zhp->zfs_name, snapname);
4166 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4170 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4171 fnvlist_free(ha.nvl);
4177 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4181 libzfs_handle_t *hdl = zhp->zfs_hdl;
4186 ret = lzc_hold(holds, cleanup_fd, &errors);
4189 /* There may be errors even in the success case. */
4190 fnvlist_free(errors);
4194 if (nvlist_empty(errors)) {
4195 /* no hold-specific errors */
4196 (void) snprintf(errbuf, sizeof (errbuf),
4197 dgettext(TEXT_DOMAIN, "cannot hold"));
4200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4201 "pool must be upgraded"));
4202 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4205 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4208 (void) zfs_standard_error(hdl, ret, errbuf);
4212 for (elem = nvlist_next_nvpair(errors, NULL);
4214 elem = nvlist_next_nvpair(errors, elem)) {
4215 (void) snprintf(errbuf, sizeof (errbuf),
4216 dgettext(TEXT_DOMAIN,
4217 "cannot hold snapshot '%s'"), nvpair_name(elem));
4218 switch (fnvpair_value_int32(elem)) {
4221 * Temporary tags wind up having the ds object id
4222 * prepended. So even if we passed the length check
4223 * above, it's still possible for the tag to wind
4224 * up being slightly too long.
4226 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4229 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4232 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4235 (void) zfs_standard_error(hdl,
4236 fnvpair_value_int32(elem), errbuf);
4240 fnvlist_free(errors);
4245 zfs_release_one(zfs_handle_t *zhp, void *arg)
4247 struct holdarg *ha = arg;
4248 char name[ZFS_MAXNAMELEN];
4250 nvlist_t *existing_holds;
4252 (void) snprintf(name, sizeof (name),
4253 "%s@%s", zhp->zfs_name, ha->snapname);
4255 if (lzc_get_holds(name, &existing_holds) != 0) {
4257 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4260 nvlist_t *torelease = fnvlist_alloc();
4261 fnvlist_add_boolean(torelease, ha->tag);
4262 fnvlist_add_nvlist(ha->nvl, name, torelease);
4263 fnvlist_free(torelease);
4267 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4273 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4274 boolean_t recursive)
4278 nvlist_t *errors = NULL;
4280 libzfs_handle_t *hdl = zhp->zfs_hdl;
4283 ha.nvl = fnvlist_alloc();
4284 ha.snapname = snapname;
4286 ha.recursive = recursive;
4288 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4290 if (nvlist_empty(ha.nvl)) {
4291 fnvlist_free(ha.nvl);
4293 (void) snprintf(errbuf, sizeof (errbuf),
4294 dgettext(TEXT_DOMAIN,
4295 "cannot release hold from snapshot '%s@%s'"),
4296 zhp->zfs_name, snapname);
4298 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4300 (void) zfs_standard_error(hdl, ret, errbuf);
4305 ret = lzc_release(ha.nvl, &errors);
4306 fnvlist_free(ha.nvl);
4309 /* There may be errors even in the success case. */
4310 fnvlist_free(errors);
4314 if (nvlist_empty(errors)) {
4315 /* no hold-specific errors */
4316 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4321 "pool must be upgraded"));
4322 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4325 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4329 for (elem = nvlist_next_nvpair(errors, NULL);
4331 elem = nvlist_next_nvpair(errors, elem)) {
4332 (void) snprintf(errbuf, sizeof (errbuf),
4333 dgettext(TEXT_DOMAIN,
4334 "cannot release hold from snapshot '%s'"),
4336 switch (fnvpair_value_int32(elem)) {
4338 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4341 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4344 (void) zfs_standard_error_fmt(hdl,
4345 fnvpair_value_int32(elem), errbuf);
4349 fnvlist_free(errors);
4354 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4356 zfs_cmd_t zc = {"\0"};
4357 libzfs_handle_t *hdl = zhp->zfs_hdl;
4363 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4364 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4368 nvbuf = malloc(nvsz);
4369 if (nvbuf == NULL) {
4370 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4374 zc.zc_nvlist_dst_size = nvsz;
4375 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4377 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4379 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4380 (void) snprintf(errbuf, sizeof (errbuf),
4381 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4386 nvsz = zc.zc_nvlist_dst_size;
4390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4391 "pool must be upgraded"));
4392 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4395 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4398 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4401 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4406 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4408 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4409 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4411 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4421 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4423 zfs_cmd_t zc = {"\0"};
4424 libzfs_handle_t *hdl = zhp->zfs_hdl;
4430 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4431 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4433 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4436 nvbuf = malloc(nvsz);
4438 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4441 zc.zc_nvlist_src_size = nvsz;
4442 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4443 zc.zc_perm_action = un;
4445 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4447 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4448 (void) snprintf(errbuf, sizeof (errbuf),
4449 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4454 "pool must be upgraded"));
4455 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4458 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4461 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4464 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4475 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4480 err = lzc_get_holds(zhp->zfs_name, nvl);
4483 libzfs_handle_t *hdl = zhp->zfs_hdl;
4485 (void) snprintf(errbuf, sizeof (errbuf),
4486 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4491 "pool must be upgraded"));
4492 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4495 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4498 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4501 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4510 * Convert the zvol's volume size to an appropriate reservation.
4511 * Note: If this routine is updated, it is necessary to update the ZFS test
4512 * suite's shell version in reservation.kshlib.
4515 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4518 uint64_t nblocks, volblocksize;
4522 if (nvlist_lookup_string(props,
4523 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4524 ncopies = atoi(strval);
4527 if (nvlist_lookup_uint64(props,
4528 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4529 &volblocksize) != 0)
4530 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4531 nblocks = volsize/volblocksize;
4532 /* start with metadnode L0-L6 */
4534 /* calculate number of indirects */
4535 while (nblocks > 1) {
4536 nblocks += DNODES_PER_LEVEL - 1;
4537 nblocks /= DNODES_PER_LEVEL;
4540 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4543 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4544 * compressed, but in practice they compress down to about
4547 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;