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 2012 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
44 #include <sys/mntent.h>
45 #include <sys/mount.h>
52 #include <sys/dnode.h>
58 #include "zfs_namecheck.h"
60 #include "libzfs_impl.h"
61 #include "zfs_deleg.h"
63 static int userquota_propname_decode(const char *propname, boolean_t zoned,
64 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
67 * Given a single type (not a mask of types), return the type in a human
71 zfs_type_to_name(zfs_type_t type)
74 case ZFS_TYPE_FILESYSTEM:
75 return (dgettext(TEXT_DOMAIN, "filesystem"));
76 case ZFS_TYPE_SNAPSHOT:
77 return (dgettext(TEXT_DOMAIN, "snapshot"));
79 return (dgettext(TEXT_DOMAIN, "volume"));
86 * Given a path and mask of ZFS types, return a string describing this dataset.
87 * This is used when we fail to open a dataset and we cannot get an exact type.
88 * We guess what the type would have been based on the path and the mask of
92 path_to_str(const char *path, int types)
95 * When given a single type, always report the exact type.
97 if (types == ZFS_TYPE_SNAPSHOT)
98 return (dgettext(TEXT_DOMAIN, "snapshot"));
99 if (types == ZFS_TYPE_FILESYSTEM)
100 return (dgettext(TEXT_DOMAIN, "filesystem"));
101 if (types == ZFS_TYPE_VOLUME)
102 return (dgettext(TEXT_DOMAIN, "volume"));
105 * The user is requesting more than one type of dataset. If this is the
106 * case, consult the path itself. If we're looking for a snapshot, and
107 * a '@' is found, then report it as "snapshot". Otherwise, remove the
108 * snapshot attribute and try again.
110 if (types & ZFS_TYPE_SNAPSHOT) {
111 if (strchr(path, '@') != NULL)
112 return (dgettext(TEXT_DOMAIN, "snapshot"));
113 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
117 * The user has requested either filesystems or volumes.
118 * We have no way of knowing a priori what type this would be, so always
119 * report it as "filesystem" or "volume", our two primitive types.
121 if (types & ZFS_TYPE_FILESYSTEM)
122 return (dgettext(TEXT_DOMAIN, "filesystem"));
124 assert(types & ZFS_TYPE_VOLUME);
125 return (dgettext(TEXT_DOMAIN, "volume"));
129 * Validate a ZFS path. This is used even before trying to open the dataset, to
130 * provide a more meaningful error message. We call zfs_error_aux() to
131 * explain exactly why the name was not valid.
134 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
140 (void) zfs_prop_get_table();
141 if (dataset_namecheck(path, &why, &what) != 0) {
144 case NAME_ERR_TOOLONG:
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "name is too long"));
149 case NAME_ERR_LEADING_SLASH:
150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151 "leading slash in name"));
154 case NAME_ERR_EMPTY_COMPONENT:
155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156 "empty component in name"));
159 case NAME_ERR_TRAILING_SLASH:
160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
161 "trailing slash in name"));
164 case NAME_ERR_INVALCHAR:
166 dgettext(TEXT_DOMAIN, "invalid character "
167 "'%c' in name"), what);
170 case NAME_ERR_MULTIPLE_AT:
171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172 "multiple '@' delimiters in name"));
175 case NAME_ERR_NOLETTER:
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "pool doesn't begin with a letter"));
180 case NAME_ERR_RESERVED:
181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
182 "name is reserved"));
185 case NAME_ERR_DISKLIKE:
186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
187 "reserved disk name"));
195 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
198 "snapshot delimiter '@' in filesystem name"));
202 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205 "missing '@' delimiter in snapshot name"));
209 if (modifying && strchr(path, '%') != NULL) {
211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
212 "invalid character %c in name"), '%');
220 zfs_name_valid(const char *name, zfs_type_t type)
222 if (type == ZFS_TYPE_POOL)
223 return (zpool_name_valid(NULL, B_FALSE, name));
224 return (zfs_validate_name(NULL, name, type, B_FALSE));
228 * This function takes the raw DSL properties, and filters out the user-defined
229 * properties into a separate nvlist.
232 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
234 libzfs_handle_t *hdl = zhp->zfs_hdl;
239 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
240 (void) no_memory(hdl);
245 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
246 if (!zfs_prop_user(nvpair_name(elem)))
249 verify(nvpair_value_nvlist(elem, &propval) == 0);
250 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
252 (void) no_memory(hdl);
260 static zpool_handle_t *
261 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
263 libzfs_handle_t *hdl = zhp->zfs_hdl;
266 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
267 if (hdl->libzfs_pool_handles != NULL)
268 zph->zpool_next = hdl->libzfs_pool_handles;
269 hdl->libzfs_pool_handles = zph;
274 static zpool_handle_t *
275 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
277 libzfs_handle_t *hdl = zhp->zfs_hdl;
278 zpool_handle_t *zph = hdl->libzfs_pool_handles;
280 while ((zph != NULL) &&
281 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
282 zph = zph->zpool_next;
287 * Returns a handle to the pool that contains the provided dataset.
288 * If a handle to that pool already exists then that handle is returned.
289 * Otherwise, a new handle is created and added to the list of handles.
291 static zpool_handle_t *
292 zpool_handle(zfs_handle_t *zhp)
298 len = strcspn(zhp->zfs_name, "/@") + 1;
299 pool_name = zfs_alloc(zhp->zfs_hdl, len);
300 (void) strlcpy(pool_name, zhp->zfs_name, len);
302 zph = zpool_find_handle(zhp, pool_name, len);
304 zph = zpool_add_handle(zhp, pool_name);
311 zpool_free_handles(libzfs_handle_t *hdl)
313 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
315 while (zph != NULL) {
316 next = zph->zpool_next;
320 hdl->libzfs_pool_handles = NULL;
324 * Utility function to gather stats (objset and zpl) for the given object.
327 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
329 libzfs_handle_t *hdl = zhp->zfs_hdl;
331 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
333 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
334 if (errno == ENOMEM) {
335 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
346 * Utility function to get the received properties of the given object.
349 get_recvd_props_ioctl(zfs_handle_t *zhp)
351 libzfs_handle_t *hdl = zhp->zfs_hdl;
352 nvlist_t *recvdprops;
353 zfs_cmd_t zc = { 0 };
356 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
359 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
361 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
362 if (errno == ENOMEM) {
363 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
367 zcmd_free_nvlists(&zc);
372 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
373 zcmd_free_nvlists(&zc);
377 nvlist_free(zhp->zfs_recvd_props);
378 zhp->zfs_recvd_props = recvdprops;
384 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
386 nvlist_t *allprops, *userprops;
388 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
390 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
395 * XXX Why do we store the user props separately, in addition to
396 * storing them in zfs_props?
398 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
399 nvlist_free(allprops);
403 nvlist_free(zhp->zfs_props);
404 nvlist_free(zhp->zfs_user_props);
406 zhp->zfs_props = allprops;
407 zhp->zfs_user_props = userprops;
413 get_stats(zfs_handle_t *zhp)
416 zfs_cmd_t zc = { 0 };
418 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
420 if (get_stats_ioctl(zhp, &zc) != 0)
422 else if (put_stats_zhdl(zhp, &zc) != 0)
424 zcmd_free_nvlists(&zc);
429 * Refresh the properties currently stored in the handle.
432 zfs_refresh_properties(zfs_handle_t *zhp)
434 (void) get_stats(zhp);
438 * Makes a handle from the given dataset name. Used by zfs_open() and
439 * zfs_iter_* to create child handles on the fly.
442 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
444 if (put_stats_zhdl(zhp, zc) != 0)
448 * We've managed to open the dataset and gather statistics. Determine
449 * the high-level type.
451 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
452 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
453 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
454 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
458 if (zhp->zfs_dmustats.dds_is_snapshot)
459 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
460 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
461 zhp->zfs_type = ZFS_TYPE_VOLUME;
462 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
463 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
465 abort(); /* we should never see any other types */
467 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
474 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
476 zfs_cmd_t zc = { 0 };
478 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
484 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
485 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
489 if (get_stats_ioctl(zhp, &zc) == -1) {
490 zcmd_free_nvlists(&zc);
494 if (make_dataset_handle_common(zhp, &zc) == -1) {
498 zcmd_free_nvlists(&zc);
503 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
505 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
511 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
512 if (make_dataset_handle_common(zhp, zc) == -1) {
520 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
522 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
527 zhp->zfs_hdl = pzhp->zfs_hdl;
528 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
529 zhp->zfs_head_type = pzhp->zfs_type;
530 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
531 zhp->zpool_hdl = zpool_handle(zhp);
536 zfs_handle_dup(zfs_handle_t *zhp_orig)
538 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
543 zhp->zfs_hdl = zhp_orig->zfs_hdl;
544 zhp->zpool_hdl = zhp_orig->zpool_hdl;
545 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
546 sizeof (zhp->zfs_name));
547 zhp->zfs_type = zhp_orig->zfs_type;
548 zhp->zfs_head_type = zhp_orig->zfs_head_type;
549 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
550 if (zhp_orig->zfs_props != NULL) {
551 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
552 (void) no_memory(zhp->zfs_hdl);
557 if (zhp_orig->zfs_user_props != NULL) {
558 if (nvlist_dup(zhp_orig->zfs_user_props,
559 &zhp->zfs_user_props, 0) != 0) {
560 (void) no_memory(zhp->zfs_hdl);
565 if (zhp_orig->zfs_recvd_props != NULL) {
566 if (nvlist_dup(zhp_orig->zfs_recvd_props,
567 &zhp->zfs_recvd_props, 0)) {
568 (void) no_memory(zhp->zfs_hdl);
573 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
574 if (zhp_orig->zfs_mntopts != NULL) {
575 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
576 zhp_orig->zfs_mntopts);
578 zhp->zfs_props_table = zhp_orig->zfs_props_table;
583 * Opens the given snapshot, filesystem, or volume. The 'types'
584 * argument is a mask of acceptable types. The function will print an
585 * appropriate error message and return NULL if it can't be opened.
588 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
593 (void) snprintf(errbuf, sizeof (errbuf),
594 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
597 * Validate the name before we even try to open it.
599 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
600 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
601 "invalid dataset name"));
602 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
607 * Try to get stats for the dataset, which will tell us if it exists.
610 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
611 (void) zfs_standard_error(hdl, errno, errbuf);
616 char *at = strchr(path, '@');
621 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
622 (void) zfs_standard_error(hdl, errno, errbuf);
627 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
628 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
631 if (!(types & zhp->zfs_type)) {
632 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
641 * Release a ZFS handle. Nothing to do but free the associated memory.
644 zfs_close(zfs_handle_t *zhp)
646 if (zhp->zfs_mntopts)
647 free(zhp->zfs_mntopts);
648 nvlist_free(zhp->zfs_props);
649 nvlist_free(zhp->zfs_user_props);
650 nvlist_free(zhp->zfs_recvd_props);
654 typedef struct mnttab_node {
655 struct mnttab mtn_mt;
660 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
662 const mnttab_node_t *mtn1 = arg1;
663 const mnttab_node_t *mtn2 = arg2;
666 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
670 return (rv > 0 ? 1 : -1);
674 libzfs_mnttab_init(libzfs_handle_t *hdl)
676 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
677 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
678 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
682 libzfs_mnttab_update(libzfs_handle_t *hdl)
686 rewind(hdl->libzfs_mnttab);
687 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
690 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
692 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
693 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
694 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
695 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
696 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
697 avl_add(&hdl->libzfs_mnttab_cache, mtn);
702 libzfs_mnttab_fini(libzfs_handle_t *hdl)
707 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
708 free(mtn->mtn_mt.mnt_special);
709 free(mtn->mtn_mt.mnt_mountp);
710 free(mtn->mtn_mt.mnt_fstype);
711 free(mtn->mtn_mt.mnt_mntopts);
714 avl_destroy(&hdl->libzfs_mnttab_cache);
718 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
720 hdl->libzfs_mnttab_enable = enable;
724 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
725 struct mnttab *entry)
730 if (!hdl->libzfs_mnttab_enable) {
731 struct mnttab srch = { 0 };
733 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
734 libzfs_mnttab_fini(hdl);
735 rewind(hdl->libzfs_mnttab);
736 srch.mnt_special = (char *)fsname;
737 srch.mnt_fstype = MNTTYPE_ZFS;
738 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
744 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
745 libzfs_mnttab_update(hdl);
747 find.mtn_mt.mnt_special = (char *)fsname;
748 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
750 *entry = mtn->mtn_mt;
757 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
758 const char *mountp, const char *mntopts)
762 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
764 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
765 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
766 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
767 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
768 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
769 avl_add(&hdl->libzfs_mnttab_cache, mtn);
773 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
778 find.mtn_mt.mnt_special = (char *)fsname;
779 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
780 avl_remove(&hdl->libzfs_mnttab_cache, ret);
781 free(ret->mtn_mt.mnt_special);
782 free(ret->mtn_mt.mnt_mountp);
783 free(ret->mtn_mt.mnt_fstype);
784 free(ret->mtn_mt.mnt_mntopts);
790 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
792 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
794 if (zpool_handle == NULL)
797 *spa_version = zpool_get_prop_int(zpool_handle,
798 ZPOOL_PROP_VERSION, NULL);
803 * The choice of reservation property depends on the SPA version.
806 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
810 if (zfs_spa_version(zhp, &spa_version) < 0)
813 if (spa_version >= SPA_VERSION_REFRESERVATION)
814 *resv_prop = ZFS_PROP_REFRESERVATION;
816 *resv_prop = ZFS_PROP_RESERVATION;
822 * Given an nvlist of properties to set, validates that they are correct, and
823 * parses any numeric properties (index, boolean, etc) if they are specified as
827 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
828 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
835 int chosen_normal = -1;
838 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
839 (void) no_memory(hdl);
844 * Make sure this property is valid and applies to this type.
848 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
849 const char *propname = nvpair_name(elem);
851 prop = zfs_name_to_prop(propname);
852 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
854 * This is a user property: make sure it's a
855 * string, and that it's less than ZAP_MAXNAMELEN.
857 if (nvpair_type(elem) != DATA_TYPE_STRING) {
858 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
859 "'%s' must be a string"), propname);
860 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
864 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
865 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
866 "property name '%s' is too long"),
868 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
872 (void) nvpair_value_string(elem, &strval);
873 if (nvlist_add_string(ret, propname, strval) != 0) {
874 (void) no_memory(hdl);
881 * Currently, only user properties can be modified on
884 if (type == ZFS_TYPE_SNAPSHOT) {
885 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
886 "this property can not be modified for snapshots"));
887 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
891 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
892 zfs_userquota_prop_t uqtype;
893 char newpropname[128];
898 if (userquota_propname_decode(propname, zoned,
899 &uqtype, domain, sizeof (domain), &rid) != 0) {
901 dgettext(TEXT_DOMAIN,
902 "'%s' has an invalid user/group name"),
904 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
908 if (uqtype != ZFS_PROP_USERQUOTA &&
909 uqtype != ZFS_PROP_GROUPQUOTA) {
911 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
913 (void) zfs_error(hdl, EZFS_PROPREADONLY,
918 if (nvpair_type(elem) == DATA_TYPE_STRING) {
919 (void) nvpair_value_string(elem, &strval);
920 if (strcmp(strval, "none") == 0) {
922 } else if (zfs_nicestrtonum(hdl,
923 strval, &intval) != 0) {
924 (void) zfs_error(hdl,
925 EZFS_BADPROP, errbuf);
928 } else if (nvpair_type(elem) ==
930 (void) nvpair_value_uint64(elem, &intval);
932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 "use 'none' to disable "
934 "userquota/groupquota"));
938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
939 "'%s' must be a number"), propname);
940 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
945 * Encode the prop name as
946 * userquota@<hex-rid>-domain, to make it easy
947 * for the kernel to decode.
949 (void) snprintf(newpropname, sizeof (newpropname),
950 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
951 (longlong_t)rid, domain);
955 if (nvlist_add_uint64_array(ret, newpropname,
957 (void) no_memory(hdl);
961 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
962 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
965 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
969 if (prop == ZPROP_INVAL) {
970 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
971 "invalid property '%s'"), propname);
972 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
976 if (!zfs_prop_valid_for_type(prop, type)) {
978 dgettext(TEXT_DOMAIN, "'%s' does not "
979 "apply to datasets of this type"), propname);
980 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
984 if (zfs_prop_readonly(prop) &&
985 (!zfs_prop_setonce(prop) || zhp != NULL)) {
987 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
989 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
993 if (zprop_parse_value(hdl, elem, prop, type, ret,
994 &strval, &intval, errbuf) != 0)
998 * Perform some additional checks for specific properties.
1001 case ZFS_PROP_VERSION:
1007 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1008 if (intval < version) {
1009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1010 "Can not downgrade; already at version %u"),
1012 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1018 case ZFS_PROP_RECORDSIZE:
1019 case ZFS_PROP_VOLBLOCKSIZE:
1020 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
1021 if (intval < SPA_MINBLOCKSIZE ||
1022 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
1023 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1024 "'%s' must be power of 2 from %u "
1025 "to %uk"), propname,
1026 (uint_t)SPA_MINBLOCKSIZE,
1027 (uint_t)SPA_MAXBLOCKSIZE >> 10);
1028 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1033 case ZFS_PROP_MLSLABEL:
1037 * Verify the mlslabel string and convert to
1038 * internal hex label string.
1042 char *hex = NULL; /* internal label string */
1044 /* Default value is already OK. */
1045 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1048 /* Verify the label can be converted to binary form */
1049 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1050 (str_to_label(strval, &new_sl, MAC_LABEL,
1051 L_NO_CORRECTION, NULL) == -1)) {
1055 /* Now translate to hex internal label string */
1056 if (label_to_str(new_sl, &hex, M_INTERNAL,
1062 m_label_free(new_sl);
1064 /* If string is already in internal form, we're done. */
1065 if (strcmp(strval, hex) == 0) {
1070 /* Replace the label string with the internal form. */
1071 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1073 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1080 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1081 "invalid mlslabel '%s'"), strval);
1082 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1083 m_label_free(new_sl); /* OK if null */
1085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1086 "mlslabel is not supported on FreeBSD"));
1087 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1093 case ZFS_PROP_MOUNTPOINT:
1095 namecheck_err_t why;
1097 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1098 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1101 if (mountpoint_namecheck(strval, &why)) {
1103 case NAME_ERR_LEADING_SLASH:
1105 dgettext(TEXT_DOMAIN,
1106 "'%s' must be an absolute path, "
1107 "'none', or 'legacy'"), propname);
1109 case NAME_ERR_TOOLONG:
1111 dgettext(TEXT_DOMAIN,
1112 "component of '%s' is too long"),
1116 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1123 case ZFS_PROP_SHARESMB:
1124 case ZFS_PROP_SHARENFS:
1126 * For the mountpoint and sharenfs or sharesmb
1127 * properties, check if it can be set in a
1128 * global/non-global zone based on
1129 * the zoned property value:
1131 * global zone non-global zone
1132 * --------------------------------------------------
1133 * zoned=on mountpoint (no) mountpoint (yes)
1134 * sharenfs (no) sharenfs (no)
1135 * sharesmb (no) sharesmb (no)
1137 * zoned=off mountpoint (yes) N/A
1142 if (getzoneid() == GLOBAL_ZONEID) {
1143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1144 "'%s' cannot be set on "
1145 "dataset in a non-global zone"),
1147 (void) zfs_error(hdl, EZFS_ZONED,
1150 } else if (prop == ZFS_PROP_SHARENFS ||
1151 prop == ZFS_PROP_SHARESMB) {
1152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1153 "'%s' cannot be set in "
1154 "a non-global zone"), propname);
1155 (void) zfs_error(hdl, EZFS_ZONED,
1159 } else if (getzoneid() != GLOBAL_ZONEID) {
1161 * If zoned property is 'off', this must be in
1162 * a global zone. If not, something is wrong.
1164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1165 "'%s' cannot be set while dataset "
1166 "'zoned' property is set"), propname);
1167 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1172 * At this point, it is legitimate to set the
1173 * property. Now we want to make sure that the
1174 * property value is valid if it is sharenfs.
1176 if ((prop == ZFS_PROP_SHARENFS ||
1177 prop == ZFS_PROP_SHARESMB) &&
1178 strcmp(strval, "on") != 0 &&
1179 strcmp(strval, "off") != 0) {
1180 zfs_share_proto_t proto;
1182 if (prop == ZFS_PROP_SHARESMB)
1188 * Must be an valid sharing protocol
1189 * option string so init the libshare
1190 * in order to enable the parser and
1191 * then parse the options. We use the
1192 * control API since we don't care about
1193 * the current configuration and don't
1194 * want the overhead of loading it
1195 * until we actually do something.
1198 if (zfs_init_libshare(hdl,
1199 SA_INIT_CONTROL_API) != SA_OK) {
1201 * An error occurred so we can't do
1204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1205 "'%s' cannot be set: problem "
1206 "in share initialization"),
1208 (void) zfs_error(hdl, EZFS_BADPROP,
1213 if (zfs_parse_options(strval, proto) != SA_OK) {
1215 * There was an error in parsing so
1216 * deal with it by issuing an error
1217 * message and leaving after
1218 * uninitializing the the libshare
1221 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1222 "'%s' cannot be set to invalid "
1223 "options"), propname);
1224 (void) zfs_error(hdl, EZFS_BADPROP,
1226 zfs_uninit_libshare(hdl);
1229 zfs_uninit_libshare(hdl);
1233 case ZFS_PROP_UTF8ONLY:
1234 chosen_utf = (int)intval;
1236 case ZFS_PROP_NORMALIZE:
1237 chosen_normal = (int)intval;
1242 * For changes to existing volumes, we have some additional
1243 * checks to enforce.
1245 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1246 uint64_t volsize = zfs_prop_get_int(zhp,
1248 uint64_t blocksize = zfs_prop_get_int(zhp,
1249 ZFS_PROP_VOLBLOCKSIZE);
1253 case ZFS_PROP_RESERVATION:
1254 case ZFS_PROP_REFRESERVATION:
1255 if (intval > volsize) {
1256 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1257 "'%s' is greater than current "
1258 "volume size"), propname);
1259 (void) zfs_error(hdl, EZFS_BADPROP,
1265 case ZFS_PROP_VOLSIZE:
1266 if (intval % blocksize != 0) {
1267 zfs_nicenum(blocksize, buf,
1269 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1270 "'%s' must be a multiple of "
1271 "volume block size (%s)"),
1273 (void) zfs_error(hdl, EZFS_BADPROP,
1279 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1280 "'%s' cannot be zero"),
1282 (void) zfs_error(hdl, EZFS_BADPROP,
1292 * If normalization was chosen, but no UTF8 choice was made,
1293 * enforce rejection of non-UTF8 names.
1295 * If normalization was chosen, but rejecting non-UTF8 names
1296 * was explicitly not chosen, it is an error.
1298 if (chosen_normal > 0 && chosen_utf < 0) {
1299 if (nvlist_add_uint64(ret,
1300 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1301 (void) no_memory(hdl);
1304 } else if (chosen_normal > 0 && chosen_utf == 0) {
1305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1306 "'%s' must be set 'on' if normalization chosen"),
1307 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1308 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1319 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1321 uint64_t old_volsize;
1322 uint64_t new_volsize;
1323 uint64_t old_reservation;
1324 uint64_t new_reservation;
1325 zfs_prop_t resv_prop;
1328 * If this is an existing volume, and someone is setting the volsize,
1329 * make sure that it matches the reservation, or add it if necessary.
1331 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1332 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1334 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1335 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1336 old_reservation) || nvlist_lookup_uint64(nvl,
1337 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1340 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1343 new_reservation = zvol_volsize_to_reservation(new_volsize,
1345 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1346 new_reservation) != 0) {
1347 (void) no_memory(zhp->zfs_hdl);
1354 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1361 * For quotas and reservations, ENOSPC indicates
1362 * something different; setting a quota or reservation
1363 * doesn't use any disk space.
1366 case ZFS_PROP_QUOTA:
1367 case ZFS_PROP_REFQUOTA:
1368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1369 "size is less than current used or "
1371 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1374 case ZFS_PROP_RESERVATION:
1375 case ZFS_PROP_REFRESERVATION:
1376 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1377 "size is greater than available space"));
1378 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1382 (void) zfs_standard_error(hdl, err, errbuf);
1388 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1392 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1396 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1397 "pool and or dataset must be upgraded to set this "
1398 "property or value"));
1399 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1403 if (prop == ZFS_PROP_COMPRESSION) {
1404 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1405 "property setting is not allowed on "
1406 "bootable datasets"));
1407 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1409 (void) zfs_standard_error(hdl, err, errbuf);
1414 if (prop == ZPROP_INVAL) {
1415 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1417 (void) zfs_standard_error(hdl, err, errbuf);
1423 * This platform can't address a volume this big.
1426 if (prop == ZFS_PROP_VOLSIZE) {
1427 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1433 (void) zfs_standard_error(hdl, err, errbuf);
1438 * Given a property name and value, set the property for the given dataset.
1441 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1443 zfs_cmd_t zc = { 0 };
1445 prop_changelist_t *cl = NULL;
1447 libzfs_handle_t *hdl = zhp->zfs_hdl;
1448 nvlist_t *nvl = NULL, *realprops;
1450 boolean_t do_prefix = B_TRUE;
1453 (void) snprintf(errbuf, sizeof (errbuf),
1454 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1457 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1458 nvlist_add_string(nvl, propname, propval) != 0) {
1459 (void) no_memory(hdl);
1463 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1464 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1470 prop = zfs_name_to_prop(propname);
1472 /* We don't support those properties on FreeBSD. */
1474 case ZFS_PROP_DEVICES:
1475 case ZFS_PROP_ISCSIOPTIONS:
1476 case ZFS_PROP_XATTR:
1477 case ZFS_PROP_VSCAN:
1478 case ZFS_PROP_NBMAND:
1479 case ZFS_PROP_MLSLABEL:
1480 (void) snprintf(errbuf, sizeof (errbuf),
1481 "property '%s' not supported on FreeBSD", propname);
1482 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1486 if (prop == ZFS_PROP_VOLSIZE) {
1487 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1491 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1494 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1496 "child dataset with inherited mountpoint is used "
1497 "in a non-global zone"));
1498 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1503 * We don't want to unmount & remount the dataset when changing
1504 * its canmount property to 'on' or 'noauto'. We only use
1505 * the changelist logic to unmount when setting canmount=off.
1507 if (prop == ZFS_PROP_CANMOUNT) {
1509 int err = zprop_string_to_index(prop, propval, &idx,
1511 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1512 do_prefix = B_FALSE;
1515 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1519 * Execute the corresponding ioctl() to set this property.
1521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1523 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1526 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1529 zfs_setprop_error(hdl, prop, errno, errbuf);
1530 if (added_resv && errno == ENOSPC) {
1531 /* clean up the volsize property we tried to set */
1532 uint64_t old_volsize = zfs_prop_get_int(zhp,
1535 zcmd_free_nvlists(&zc);
1536 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1538 if (nvlist_add_uint64(nvl,
1539 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1542 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1544 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1548 ret = changelist_postfix(cl);
1551 * Refresh the statistics so the new property value
1555 (void) get_stats(zhp);
1560 zcmd_free_nvlists(&zc);
1562 changelist_free(cl);
1567 * Given a property, inherit the value from the parent dataset, or if received
1568 * is TRUE, revert to the received value, if any.
1571 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1573 zfs_cmd_t zc = { 0 };
1575 prop_changelist_t *cl;
1576 libzfs_handle_t *hdl = zhp->zfs_hdl;
1580 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1581 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1583 zc.zc_cookie = received;
1584 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1586 * For user properties, the amount of work we have to do is very
1587 * small, so just do it here.
1589 if (!zfs_prop_user(propname)) {
1590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1591 "invalid property"));
1592 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1595 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1596 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1598 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1599 return (zfs_standard_error(hdl, errno, errbuf));
1605 * Verify that this property is inheritable.
1607 if (zfs_prop_readonly(prop))
1608 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1610 if (!zfs_prop_inheritable(prop) && !received)
1611 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1614 * Check to see if the value applies to this type
1616 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1617 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1620 * Normalize the name, to get rid of shorthand abbreviations.
1622 propname = zfs_prop_to_name(prop);
1623 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1624 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1626 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1627 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1629 "dataset is used in a non-global zone"));
1630 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1634 * Determine datasets which will be affected by this change, if any.
1636 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1639 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1641 "child dataset with inherited mountpoint is used "
1642 "in a non-global zone"));
1643 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1647 if ((ret = changelist_prefix(cl)) != 0)
1650 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1651 return (zfs_standard_error(hdl, errno, errbuf));
1654 if ((ret = changelist_postfix(cl)) != 0)
1658 * Refresh the statistics so the new property is reflected.
1660 (void) get_stats(zhp);
1664 changelist_free(cl);
1669 * True DSL properties are stored in an nvlist. The following two functions
1670 * extract them appropriately.
1673 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1679 if (nvlist_lookup_nvlist(zhp->zfs_props,
1680 zfs_prop_to_name(prop), &nv) == 0) {
1681 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1682 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1684 verify(!zhp->zfs_props_table ||
1685 zhp->zfs_props_table[prop] == B_TRUE);
1686 value = zfs_prop_default_numeric(prop);
1694 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1700 if (nvlist_lookup_nvlist(zhp->zfs_props,
1701 zfs_prop_to_name(prop), &nv) == 0) {
1702 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1703 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1705 verify(!zhp->zfs_props_table ||
1706 zhp->zfs_props_table[prop] == B_TRUE);
1707 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1716 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1718 return (zhp->zfs_props == zhp->zfs_recvd_props);
1722 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1724 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1725 zhp->zfs_props = zhp->zfs_recvd_props;
1729 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1731 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1736 * Internal function for getting a numeric property. Both zfs_prop_get() and
1737 * zfs_prop_get_int() are built using this interface.
1739 * Certain properties can be overridden using 'mount -o'. In this case, scan
1740 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1741 * If they differ from the on-disk values, report the current values and mark
1742 * the source "temporary".
1745 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1746 char **source, uint64_t *val)
1748 zfs_cmd_t zc = { 0 };
1749 nvlist_t *zplprops = NULL;
1751 char *mntopt_on = NULL;
1752 char *mntopt_off = NULL;
1753 boolean_t received = zfs_is_recvd_props_mode(zhp);
1758 case ZFS_PROP_ATIME:
1759 mntopt_on = MNTOPT_ATIME;
1760 mntopt_off = MNTOPT_NOATIME;
1763 case ZFS_PROP_DEVICES:
1764 mntopt_on = MNTOPT_DEVICES;
1765 mntopt_off = MNTOPT_NODEVICES;
1769 mntopt_on = MNTOPT_EXEC;
1770 mntopt_off = MNTOPT_NOEXEC;
1773 case ZFS_PROP_READONLY:
1774 mntopt_on = MNTOPT_RO;
1775 mntopt_off = MNTOPT_RW;
1778 case ZFS_PROP_SETUID:
1779 mntopt_on = MNTOPT_SETUID;
1780 mntopt_off = MNTOPT_NOSETUID;
1783 case ZFS_PROP_XATTR:
1784 mntopt_on = MNTOPT_XATTR;
1785 mntopt_off = MNTOPT_NOXATTR;
1788 case ZFS_PROP_NBMAND:
1789 mntopt_on = MNTOPT_NBMAND;
1790 mntopt_off = MNTOPT_NONBMAND;
1795 * Because looking up the mount options is potentially expensive
1796 * (iterating over all of /etc/mnttab), we defer its calculation until
1797 * we're looking up a property which requires its presence.
1799 if (!zhp->zfs_mntcheck &&
1800 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1801 libzfs_handle_t *hdl = zhp->zfs_hdl;
1802 struct mnttab entry;
1804 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1805 zhp->zfs_mntopts = zfs_strdup(hdl,
1807 if (zhp->zfs_mntopts == NULL)
1811 zhp->zfs_mntcheck = B_TRUE;
1814 if (zhp->zfs_mntopts == NULL)
1815 mnt.mnt_mntopts = "";
1817 mnt.mnt_mntopts = zhp->zfs_mntopts;
1820 case ZFS_PROP_ATIME:
1821 case ZFS_PROP_DEVICES:
1823 case ZFS_PROP_READONLY:
1824 case ZFS_PROP_SETUID:
1825 case ZFS_PROP_XATTR:
1826 case ZFS_PROP_NBMAND:
1827 *val = getprop_uint64(zhp, prop, source);
1832 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1835 *src = ZPROP_SRC_TEMPORARY;
1836 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1839 *src = ZPROP_SRC_TEMPORARY;
1843 case ZFS_PROP_CANMOUNT:
1844 case ZFS_PROP_VOLSIZE:
1845 case ZFS_PROP_QUOTA:
1846 case ZFS_PROP_REFQUOTA:
1847 case ZFS_PROP_RESERVATION:
1848 case ZFS_PROP_REFRESERVATION:
1849 *val = getprop_uint64(zhp, prop, source);
1851 if (*source == NULL) {
1852 /* not default, must be local */
1853 *source = zhp->zfs_name;
1857 case ZFS_PROP_MOUNTED:
1858 *val = (zhp->zfs_mntopts != NULL);
1861 case ZFS_PROP_NUMCLONES:
1862 *val = zhp->zfs_dmustats.dds_num_clones;
1865 case ZFS_PROP_VERSION:
1866 case ZFS_PROP_NORMALIZE:
1867 case ZFS_PROP_UTF8ONLY:
1869 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1870 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1872 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1873 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1874 zcmd_free_nvlists(&zc);
1877 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1878 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1880 zcmd_free_nvlists(&zc);
1884 nvlist_free(zplprops);
1885 zcmd_free_nvlists(&zc);
1889 switch (zfs_prop_get_type(prop)) {
1890 case PROP_TYPE_NUMBER:
1891 case PROP_TYPE_INDEX:
1892 *val = getprop_uint64(zhp, prop, source);
1894 * If we tried to use a default value for a
1895 * readonly property, it means that it was not
1898 if (zfs_prop_readonly(prop) &&
1899 *source != NULL && (*source)[0] == '\0') {
1904 case PROP_TYPE_STRING:
1906 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1907 "cannot get non-numeric property"));
1908 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1909 dgettext(TEXT_DOMAIN, "internal error")));
1917 * Calculate the source type, given the raw source string.
1920 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1921 char *statbuf, size_t statlen)
1923 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1926 if (source == NULL) {
1927 *srctype = ZPROP_SRC_NONE;
1928 } else if (source[0] == '\0') {
1929 *srctype = ZPROP_SRC_DEFAULT;
1930 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1931 *srctype = ZPROP_SRC_RECEIVED;
1933 if (strcmp(source, zhp->zfs_name) == 0) {
1934 *srctype = ZPROP_SRC_LOCAL;
1936 (void) strlcpy(statbuf, source, statlen);
1937 *srctype = ZPROP_SRC_INHERITED;
1944 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1945 size_t proplen, boolean_t literal)
1950 if (zhp->zfs_recvd_props == NULL)
1951 if (get_recvd_props_ioctl(zhp) != 0)
1954 prop = zfs_name_to_prop(propname);
1956 if (prop != ZPROP_INVAL) {
1958 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1960 zfs_set_recvd_props_mode(zhp, &cookie);
1961 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1962 NULL, NULL, 0, literal);
1963 zfs_unset_recvd_props_mode(zhp, &cookie);
1967 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1968 propname, &propval) != 0)
1970 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1972 (void) strlcpy(propbuf, recvdval, proplen);
1975 return (err == 0 ? 0 : -1);
1979 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1984 value = zfs_get_clones_nvl(zhp);
1989 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1990 pair = nvlist_next_nvpair(value, pair)) {
1991 if (propbuf[0] != '\0')
1992 (void) strlcat(propbuf, ",", proplen);
1993 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1999 struct get_clones_arg {
2003 char buf[ZFS_MAXNAMELEN];
2007 get_clones_cb(zfs_handle_t *zhp, void *arg)
2009 struct get_clones_arg *gca = arg;
2011 if (gca->numclones == 0) {
2016 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2017 NULL, NULL, 0, B_TRUE) != 0)
2019 if (strcmp(gca->buf, gca->origin) == 0) {
2020 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2025 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2031 zfs_get_clones_nvl(zfs_handle_t *zhp)
2033 nvlist_t *nv, *value;
2035 if (nvlist_lookup_nvlist(zhp->zfs_props,
2036 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2037 struct get_clones_arg gca;
2040 * if this is a snapshot, then the kernel wasn't able
2041 * to get the clones. Do it by slowly iterating.
2043 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2045 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2047 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2052 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2054 gca.origin = zhp->zfs_name;
2056 if (gca.numclones != 0) {
2058 char pool[ZFS_MAXNAMELEN];
2061 /* get the pool name */
2062 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2063 (void) strsep(&cp, "/@");
2064 root = zfs_open(zhp->zfs_hdl, pool,
2065 ZFS_TYPE_FILESYSTEM);
2067 (void) get_clones_cb(root, &gca);
2070 if (gca.numclones != 0 ||
2071 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2072 nvlist_add_nvlist(zhp->zfs_props,
2073 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2080 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2081 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2084 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2090 * Retrieve a property from the given object. If 'literal' is specified, then
2091 * numbers are left as exact values. Otherwise, numbers are converted to a
2092 * human-readable form.
2094 * Returns 0 on success, or -1 on error.
2097 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2098 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2100 char *source = NULL;
2104 boolean_t received = zfs_is_recvd_props_mode(zhp);
2107 * Check to see if this property applies to our object
2109 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2112 if (received && zfs_prop_readonly(prop))
2116 *src = ZPROP_SRC_NONE;
2119 case ZFS_PROP_CREATION:
2121 * 'creation' is a time_t stored in the statistics. We convert
2122 * this into a string unless 'literal' is specified.
2125 val = getprop_uint64(zhp, prop, &source);
2126 time_t time = (time_t)val;
2130 localtime_r(&time, &t) == NULL ||
2131 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2133 (void) snprintf(propbuf, proplen, "%llu", val);
2137 case ZFS_PROP_MOUNTPOINT:
2139 * Getting the precise mountpoint can be tricky.
2141 * - for 'none' or 'legacy', return those values.
2142 * - for inherited mountpoints, we want to take everything
2143 * after our ancestor and append it to the inherited value.
2145 * If the pool has an alternate root, we want to prepend that
2146 * root to any values we return.
2149 str = getprop_string(zhp, prop, &source);
2151 if (str[0] == '/') {
2152 char buf[MAXPATHLEN];
2154 const char *relpath;
2157 * If we inherit the mountpoint, even from a dataset
2158 * with a received value, the source will be the path of
2159 * the dataset we inherit from. If source is
2160 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2163 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2166 relpath = zhp->zfs_name + strlen(source);
2167 if (relpath[0] == '/')
2171 if ((zpool_get_prop(zhp->zpool_hdl,
2172 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2173 (strcmp(root, "-") == 0))
2176 * Special case an alternate root of '/'. This will
2177 * avoid having multiple leading slashes in the
2180 if (strcmp(root, "/") == 0)
2184 * If the mountpoint is '/' then skip over this
2185 * if we are obtaining either an alternate root or
2186 * an inherited mountpoint.
2188 if (str[1] == '\0' && (root[0] != '\0' ||
2189 relpath[0] != '\0'))
2192 if (relpath[0] == '\0')
2193 (void) snprintf(propbuf, proplen, "%s%s",
2196 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2197 root, str, relpath[0] == '@' ? "" : "/",
2200 /* 'legacy' or 'none' */
2201 (void) strlcpy(propbuf, str, proplen);
2206 case ZFS_PROP_ORIGIN:
2207 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2210 * If there is no parent at all, return failure to indicate that
2211 * it doesn't apply to this dataset.
2213 if (propbuf[0] == '\0')
2217 case ZFS_PROP_CLONES:
2218 if (get_clones_string(zhp, propbuf, proplen) != 0)
2222 case ZFS_PROP_QUOTA:
2223 case ZFS_PROP_REFQUOTA:
2224 case ZFS_PROP_RESERVATION:
2225 case ZFS_PROP_REFRESERVATION:
2227 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2231 * If quota or reservation is 0, we translate this into 'none'
2232 * (unless literal is set), and indicate that it's the default
2233 * value. Otherwise, we print the number nicely and indicate
2234 * that its set locally.
2238 (void) strlcpy(propbuf, "0", proplen);
2240 (void) strlcpy(propbuf, "none", proplen);
2243 (void) snprintf(propbuf, proplen, "%llu",
2246 zfs_nicenum(val, propbuf, proplen);
2250 case ZFS_PROP_REFRATIO:
2251 case ZFS_PROP_COMPRESSRATIO:
2252 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2254 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2255 (u_longlong_t)(val / 100),
2256 (u_longlong_t)(val % 100));
2260 switch (zhp->zfs_type) {
2261 case ZFS_TYPE_FILESYSTEM:
2264 case ZFS_TYPE_VOLUME:
2267 case ZFS_TYPE_SNAPSHOT:
2273 (void) snprintf(propbuf, proplen, "%s", str);
2276 case ZFS_PROP_MOUNTED:
2278 * The 'mounted' property is a pseudo-property that described
2279 * whether the filesystem is currently mounted. Even though
2280 * it's a boolean value, the typical values of "on" and "off"
2281 * don't make sense, so we translate to "yes" and "no".
2283 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2284 src, &source, &val) != 0)
2287 (void) strlcpy(propbuf, "yes", proplen);
2289 (void) strlcpy(propbuf, "no", proplen);
2294 * The 'name' property is a pseudo-property derived from the
2295 * dataset name. It is presented as a real property to simplify
2298 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2301 case ZFS_PROP_MLSLABEL:
2304 m_label_t *new_sl = NULL;
2305 char *ascii = NULL; /* human readable label */
2307 (void) strlcpy(propbuf,
2308 getprop_string(zhp, prop, &source), proplen);
2310 if (literal || (strcasecmp(propbuf,
2311 ZFS_MLSLABEL_DEFAULT) == 0))
2315 * Try to translate the internal hex string to
2316 * human-readable output. If there are any
2317 * problems just use the hex string.
2320 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2321 L_NO_CORRECTION, NULL) == -1) {
2322 m_label_free(new_sl);
2326 if (label_to_str(new_sl, &ascii, M_LABEL,
2330 m_label_free(new_sl);
2333 m_label_free(new_sl);
2335 (void) strlcpy(propbuf, ascii, proplen);
2345 * GUIDs are stored as numbers, but they are identifiers.
2346 * We don't want them to be pretty printed, because pretty
2347 * printing mangles the ID into a truncated and useless value.
2349 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2351 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2355 switch (zfs_prop_get_type(prop)) {
2356 case PROP_TYPE_NUMBER:
2357 if (get_numeric_property(zhp, prop, src,
2358 &source, &val) != 0)
2361 (void) snprintf(propbuf, proplen, "%llu",
2364 zfs_nicenum(val, propbuf, proplen);
2367 case PROP_TYPE_STRING:
2368 (void) strlcpy(propbuf,
2369 getprop_string(zhp, prop, &source), proplen);
2372 case PROP_TYPE_INDEX:
2373 if (get_numeric_property(zhp, prop, src,
2374 &source, &val) != 0)
2376 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2378 (void) strlcpy(propbuf, strval, proplen);
2386 get_source(zhp, src, source, statbuf, statlen);
2392 * Utility function to get the given numeric property. Does no validation that
2393 * the given property is the appropriate type; should only be used with
2394 * hard-coded property types.
2397 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2402 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2408 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2412 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2413 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2417 * Similar to zfs_prop_get(), but returns the value as an integer.
2420 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2421 zprop_source_t *src, char *statbuf, size_t statlen)
2426 * Check to see if this property applies to our object
2428 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2429 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2430 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2431 zfs_prop_to_name(prop)));
2435 *src = ZPROP_SRC_NONE;
2437 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2440 get_source(zhp, src, source, statbuf, statlen);
2446 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2447 char **domainp, idmap_rid_t *ridp)
2450 idmap_get_handle_t *get_hdl = NULL;
2454 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2458 err = idmap_get_sidbyuid(get_hdl, id,
2459 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2461 err = idmap_get_sidbygid(get_hdl, id,
2462 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2464 if (err == IDMAP_SUCCESS &&
2465 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2466 status == IDMAP_SUCCESS)
2472 idmap_get_destroy(get_hdl);
2475 assert(!"invalid code path");
2480 * convert the propname into parameters needed by kernel
2481 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2482 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2485 userquota_propname_decode(const char *propname, boolean_t zoned,
2486 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2488 zfs_userquota_prop_t type;
2490 char *numericsid = NULL;
2495 /* Figure out the property type ({user|group}{quota|space}) */
2496 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2497 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2498 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2501 if (type == ZFS_NUM_USERQUOTA_PROPS)
2505 isuser = (type == ZFS_PROP_USERQUOTA ||
2506 type == ZFS_PROP_USERUSED);
2508 cp = strchr(propname, '@') + 1;
2510 if (strchr(cp, '@')) {
2513 * It's a SID name (eg "user@domain") that needs to be
2514 * turned into S-1-domainID-RID.
2516 directory_error_t e;
2517 if (zoned && getzoneid() == GLOBAL_ZONEID)
2520 e = directory_sid_from_user_name(NULL,
2523 e = directory_sid_from_group_name(NULL,
2527 directory_error_free(e);
2530 if (numericsid == NULL)
2533 /* will be further decoded below */
2539 if (strncmp(cp, "S-1-", 4) == 0) {
2540 /* It's a numeric SID (eg "S-1-234-567-89") */
2541 (void) strlcpy(domain, cp, domainlen);
2542 cp = strrchr(domain, '-');
2547 *ridp = strtoull(cp, &end, 10);
2552 if (errno != 0 || *end != '\0')
2554 } else if (!isdigit(*cp)) {
2556 * It's a user/group name (eg "user") that needs to be
2557 * turned into a uid/gid
2559 if (zoned && getzoneid() == GLOBAL_ZONEID)
2575 /* It's a user/group ID (eg "12345"). */
2576 uid_t id = strtoul(cp, &end, 10);
2583 /* It's an ephemeral ID. */
2584 if (idmap_id_to_numeric_domain_rid(id, isuser,
2585 &mapdomain, &rid) != 0)
2587 (void) strlcpy(domain, mapdomain, domainlen);
2594 ASSERT3P(numericsid, ==, NULL);
2599 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2600 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2603 zfs_cmd_t zc = { 0 };
2605 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2607 err = userquota_propname_decode(propname,
2608 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2609 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2610 zc.zc_objset_type = *typep;
2614 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2618 *propvalue = zc.zc_cookie;
2623 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2624 uint64_t *propvalue)
2626 zfs_userquota_prop_t type;
2628 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2633 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2634 char *propbuf, int proplen, boolean_t literal)
2638 zfs_userquota_prop_t type;
2640 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2647 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2648 } else if (propvalue == 0 &&
2649 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2650 (void) strlcpy(propbuf, "none", proplen);
2652 zfs_nicenum(propvalue, propbuf, proplen);
2658 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2659 uint64_t *propvalue)
2662 zfs_cmd_t zc = { 0 };
2663 const char *snapname;
2665 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2667 snapname = strchr(propname, '@') + 1;
2668 if (strchr(snapname, '@')) {
2669 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2671 /* snapname is the short name, append it to zhp's fsname */
2674 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2675 sizeof (zc.zc_value));
2676 cp = strchr(zc.zc_value, '@');
2679 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2680 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2683 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2687 *propvalue = zc.zc_cookie;
2692 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2693 char *propbuf, int proplen, boolean_t literal)
2698 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2704 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2706 zfs_nicenum(propvalue, propbuf, proplen);
2712 * Returns the name of the given zfs handle.
2715 zfs_get_name(const zfs_handle_t *zhp)
2717 return (zhp->zfs_name);
2721 * Returns the type of the given zfs handle.
2724 zfs_get_type(const zfs_handle_t *zhp)
2726 return (zhp->zfs_type);
2730 * Is one dataset name a child dataset of another?
2732 * Needs to handle these cases:
2733 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2734 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2735 * Descendant? No. No. No. Yes.
2738 is_descendant(const char *ds1, const char *ds2)
2740 size_t d1len = strlen(ds1);
2742 /* ds2 can't be a descendant if it's smaller */
2743 if (strlen(ds2) < d1len)
2746 /* otherwise, compare strings and verify that there's a '/' char */
2747 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2751 * Given a complete name, return just the portion that refers to the parent.
2752 * Will return -1 if there is no parent (path is just the name of the
2756 parent_name(const char *path, char *buf, size_t buflen)
2760 (void) strlcpy(buf, path, buflen);
2762 if ((slashp = strrchr(buf, '/')) == NULL)
2770 * If accept_ancestor is false, then check to make sure that the given path has
2771 * a parent, and that it exists. If accept_ancestor is true, then find the
2772 * closest existing ancestor for the given path. In prefixlen return the
2773 * length of already existing prefix of the given path. We also fetch the
2774 * 'zoned' property, which is used to validate property settings when creating
2778 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2779 boolean_t accept_ancestor, int *prefixlen)
2781 zfs_cmd_t zc = { 0 };
2782 char parent[ZFS_MAXNAMELEN];
2788 (void) snprintf(errbuf, sizeof (errbuf),
2789 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2791 /* get parent, and check to see if this is just a pool */
2792 if (parent_name(path, parent, sizeof (parent)) != 0) {
2793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2794 "missing dataset name"));
2795 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2798 /* check to see if the pool exists */
2799 if ((slash = strchr(parent, '/')) == NULL)
2800 slash = parent + strlen(parent);
2801 (void) strncpy(zc.zc_name, parent, slash - parent);
2802 zc.zc_name[slash - parent] = '\0';
2803 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2806 "no such pool '%s'"), zc.zc_name);
2807 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2810 /* check to see if the parent dataset exists */
2811 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2812 if (errno == ENOENT && accept_ancestor) {
2814 * Go deeper to find an ancestor, give up on top level.
2816 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2818 "no such pool '%s'"), zc.zc_name);
2819 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2821 } else if (errno == ENOENT) {
2822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2823 "parent does not exist"));
2824 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2826 return (zfs_standard_error(hdl, errno, errbuf));
2829 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2833 /* we are in a non-global zone, but parent is in the global zone */
2834 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2835 (void) zfs_standard_error(hdl, EPERM, errbuf);
2840 /* make sure parent is a filesystem */
2841 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2843 "parent is not a filesystem"));
2844 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2850 if (prefixlen != NULL)
2851 *prefixlen = strlen(parent);
2856 * Finds whether the dataset of the given type(s) exists.
2859 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2863 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2867 * Try to get stats for the dataset, which will tell us if it exists.
2869 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2870 int ds_type = zhp->zfs_type;
2873 if (types & ds_type)
2880 * Given a path to 'target', create all the ancestors between
2881 * the prefixlen portion of the path, and the target itself.
2882 * Fail if the initial prefixlen-ancestor does not already exist.
2885 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2891 /* make sure prefix exists */
2892 cp = target + prefixlen;
2894 assert(strchr(cp, '/') == NULL);
2895 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2898 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2906 * Attempt to create, mount, and share any ancestor filesystems,
2907 * up to the prefixlen-long one.
2909 for (cp = target + prefixlen + 1;
2910 cp = strchr(cp, '/'); *cp = '/', cp++) {
2914 h = make_dataset_handle(hdl, target);
2916 /* it already exists, nothing to do here */
2921 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2923 opname = dgettext(TEXT_DOMAIN, "create");
2927 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2929 opname = dgettext(TEXT_DOMAIN, "open");
2933 if (zfs_mount(h, NULL, 0) != 0) {
2934 opname = dgettext(TEXT_DOMAIN, "mount");
2938 if (zfs_share(h) != 0) {
2939 opname = dgettext(TEXT_DOMAIN, "share");
2949 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2950 "failed to %s ancestor '%s'"), opname, target);
2955 * Creates non-existing ancestors of the given path.
2958 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2964 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2967 if ((path_copy = strdup(path)) != NULL) {
2968 rc = create_parents(hdl, path_copy, prefix);
2971 if (path_copy == NULL || rc != 0)
2978 * Create a new filesystem or volume.
2981 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2986 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2989 dmu_objset_type_t ost;
2991 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2992 "cannot create '%s'"), path);
2994 /* validate the path, taking care to note the extended error message */
2995 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2996 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2998 /* validate parents exist */
2999 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3003 * The failure modes when creating a dataset of a different type over
3004 * one that already exists is a little strange. In particular, if you
3005 * try to create a dataset on top of an existing dataset, the ioctl()
3006 * will return ENOENT, not EEXIST. To prevent this from happening, we
3007 * first try to see if the dataset exists.
3009 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3011 "dataset already exists"));
3012 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3015 if (type == ZFS_TYPE_VOLUME)
3020 if (props && (props = zfs_valid_proplist(hdl, type, props,
3021 zoned, NULL, errbuf)) == 0)
3024 if (type == ZFS_TYPE_VOLUME) {
3026 * If we are creating a volume, the size and block size must
3027 * satisfy a few restraints. First, the blocksize must be a
3028 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3029 * volsize must be a multiple of the block size, and cannot be
3032 if (props == NULL || nvlist_lookup_uint64(props,
3033 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3036 "missing volume size"));
3037 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3040 if ((ret = nvlist_lookup_uint64(props,
3041 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3042 &blocksize)) != 0) {
3043 if (ret == ENOENT) {
3044 blocksize = zfs_prop_default_numeric(
3045 ZFS_PROP_VOLBLOCKSIZE);
3048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3049 "missing volume block size"));
3050 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3056 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3057 "volume size cannot be zero"));
3058 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3061 if (size % blocksize != 0) {
3063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3064 "volume size must be a multiple of volume block "
3066 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3070 /* create the dataset */
3071 ret = lzc_create(path, ost, props);
3074 /* check for failure */
3076 char parent[ZFS_MAXNAMELEN];
3077 (void) parent_name(path, parent, sizeof (parent));
3081 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3082 "no such parent '%s'"), parent);
3083 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3087 "parent '%s' is not a filesystem"), parent);
3088 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3092 "volume block size must be power of 2 from "
3094 (uint_t)SPA_MINBLOCKSIZE,
3095 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3097 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3101 "pool must be upgraded to set this "
3102 "property or value"));
3103 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3107 * This platform can't address a volume this big.
3109 if (type == ZFS_TYPE_VOLUME)
3110 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3115 return (zfs_standard_error(hdl, errno, errbuf));
3123 * Destroys the given dataset. The caller must make sure that the filesystem
3124 * isn't mounted, and that there are no active dependents. If the file system
3125 * does not exist this function does nothing.
3128 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3130 zfs_cmd_t zc = { 0 };
3132 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3134 if (ZFS_IS_VOLUME(zhp)) {
3135 zc.zc_objset_type = DMU_OST_ZVOL;
3137 zc.zc_objset_type = DMU_OST_ZFS;
3140 zc.zc_defer_destroy = defer;
3141 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3143 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3144 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3148 remove_mountpoint(zhp);
3153 struct destroydata {
3155 const char *snapname;
3159 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3161 struct destroydata *dd = arg;
3162 char name[ZFS_MAXNAMELEN];
3165 (void) snprintf(name, sizeof (name),
3166 "%s@%s", zhp->zfs_name, dd->snapname);
3168 if (lzc_exists(name))
3169 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3171 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3177 * Destroys all snapshots with the given name in zhp & descendants.
3180 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3183 struct destroydata dd = { 0 };
3185 dd.snapname = snapname;
3186 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3187 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3189 if (nvlist_empty(dd.nvl)) {
3190 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3191 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3192 zhp->zfs_name, snapname);
3194 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3196 nvlist_free(dd.nvl);
3201 * Destroys all the snapshots named in the nvlist.
3204 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3209 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3214 if (nvlist_empty(errlist)) {
3216 (void) snprintf(errbuf, sizeof (errbuf),
3217 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3219 ret = zfs_standard_error(hdl, ret, errbuf);
3221 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3222 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3224 (void) snprintf(errbuf, sizeof (errbuf),
3225 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3228 switch (fnvpair_value_int32(pair)) {
3231 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3232 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3235 ret = zfs_standard_error(hdl, errno, errbuf);
3244 * Clones the given dataset. The target must be of the same type as the source.
3247 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3249 char parent[ZFS_MAXNAMELEN];
3252 libzfs_handle_t *hdl = zhp->zfs_hdl;
3255 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3257 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3258 "cannot create '%s'"), target);
3260 /* validate the target/clone name */
3261 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3262 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3264 /* validate parents exist */
3265 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3268 (void) parent_name(target, parent, sizeof (parent));
3274 if (ZFS_IS_VOLUME(zhp)) {
3275 type = ZFS_TYPE_VOLUME;
3277 type = ZFS_TYPE_FILESYSTEM;
3279 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3280 zhp, errbuf)) == NULL)
3284 ret = lzc_clone(target, zhp->zfs_name, props);
3292 * The parent doesn't exist. We should have caught this
3293 * above, but there may a race condition that has since
3294 * destroyed the parent.
3296 * At this point, we don't know whether it's the source
3297 * that doesn't exist anymore, or whether the target
3298 * dataset doesn't exist.
3300 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3301 "no such parent '%s'"), parent);
3302 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3305 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3306 "source and target pools differ"));
3307 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3311 return (zfs_standard_error(zhp->zfs_hdl, errno,
3320 * Promotes the given clone fs to be the clone parent.
3323 zfs_promote(zfs_handle_t *zhp)
3325 libzfs_handle_t *hdl = zhp->zfs_hdl;
3326 zfs_cmd_t zc = { 0 };
3327 char parent[MAXPATHLEN];
3331 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3332 "cannot promote '%s'"), zhp->zfs_name);
3334 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3336 "snapshots can not be promoted"));
3337 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3340 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3341 if (parent[0] == '\0') {
3342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3343 "not a cloned filesystem"));
3344 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3347 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3348 sizeof (zc.zc_value));
3349 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3350 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3353 int save_errno = errno;
3355 switch (save_errno) {
3357 /* There is a conflicting snapshot name. */
3358 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3359 "conflicting snapshot '%s' from parent '%s'"),
3360 zc.zc_string, parent);
3361 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3364 return (zfs_standard_error(hdl, save_errno, errbuf));
3370 typedef struct snapdata {
3372 const char *sd_snapname;
3376 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3378 snapdata_t *sd = arg;
3379 char name[ZFS_MAXNAMELEN];
3382 (void) snprintf(name, sizeof (name),
3383 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3385 fnvlist_add_boolean(sd->sd_nvl, name);
3387 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3393 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3397 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3404 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3405 "cannot create snapshots "));
3408 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3409 const char *snapname = nvpair_name(elem);
3411 /* validate the target name */
3412 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3414 (void) snprintf(errbuf, sizeof (errbuf),
3415 dgettext(TEXT_DOMAIN,
3416 "cannot create snapshot '%s'"), snapname);
3417 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3421 if (props != NULL &&
3422 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3423 props, B_FALSE, NULL, errbuf)) == NULL) {
3427 ret = lzc_snapshot(snaps, props, &errors);
3430 boolean_t printed = B_FALSE;
3431 for (elem = nvlist_next_nvpair(errors, NULL);
3433 elem = nvlist_next_nvpair(errors, elem)) {
3434 (void) snprintf(errbuf, sizeof (errbuf),
3435 dgettext(TEXT_DOMAIN,
3436 "cannot create snapshot '%s'"), nvpair_name(elem));
3437 (void) zfs_standard_error(hdl,
3438 fnvpair_value_int32(elem), errbuf);
3444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3445 "multiple snapshots of same "
3447 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3451 (void) zfs_standard_error(hdl, ret, errbuf);
3457 nvlist_free(errors);
3462 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3466 snapdata_t sd = { 0 };
3467 char fsname[ZFS_MAXNAMELEN];
3472 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3473 "cannot snapshot %s"), path);
3475 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3476 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3478 (void) strlcpy(fsname, path, sizeof (fsname));
3479 cp = strchr(fsname, '@');
3481 sd.sd_snapname = cp + 1;
3483 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3484 ZFS_TYPE_VOLUME)) == NULL) {
3488 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3490 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3492 fnvlist_add_boolean(sd.sd_nvl, path);
3495 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3496 nvlist_free(sd.sd_nvl);
3502 * Destroy any more recent snapshots. We invoke this callback on any dependents
3503 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3504 * is a dependent and we should just destroy it without checking the transaction
3507 typedef struct rollback_data {
3508 const char *cb_target; /* the snapshot */
3509 uint64_t cb_create; /* creation time reference */
3511 boolean_t cb_dependent;
3516 rollback_destroy(zfs_handle_t *zhp, void *data)
3518 rollback_data_t *cbp = data;
3520 if (!cbp->cb_dependent) {
3521 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3522 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3523 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3526 cbp->cb_dependent = B_TRUE;
3527 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3528 rollback_destroy, cbp);
3529 cbp->cb_dependent = B_FALSE;
3531 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3534 /* We must destroy this clone; first unmount it */
3535 prop_changelist_t *clp;
3537 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3538 cbp->cb_force ? MS_FORCE: 0);
3539 if (clp == NULL || changelist_prefix(clp) != 0) {
3540 cbp->cb_error = B_TRUE;
3544 if (zfs_destroy(zhp, B_FALSE) != 0)
3545 cbp->cb_error = B_TRUE;
3547 changelist_remove(clp, zhp->zfs_name);
3548 (void) changelist_postfix(clp);
3549 changelist_free(clp);
3557 * Given a dataset, rollback to a specific snapshot, discarding any
3558 * data changes since then and making it the active dataset.
3560 * Any snapshots more recent than the target are destroyed, along with
3564 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3566 rollback_data_t cb = { 0 };
3568 zfs_cmd_t zc = { 0 };
3569 boolean_t restore_resv = 0;
3570 uint64_t old_volsize, new_volsize;
3571 zfs_prop_t resv_prop;
3573 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3574 zhp->zfs_type == ZFS_TYPE_VOLUME);
3577 * Destroy all recent snapshots and their dependents.
3579 cb.cb_force = force;
3580 cb.cb_target = snap->zfs_name;
3581 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3582 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3588 * Now that we have verified that the snapshot is the latest,
3589 * rollback to the given snapshot.
3592 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3593 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3595 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3597 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3600 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3602 if (ZFS_IS_VOLUME(zhp))
3603 zc.zc_objset_type = DMU_OST_ZVOL;
3605 zc.zc_objset_type = DMU_OST_ZFS;
3608 * We rely on zfs_iter_children() to verify that there are no
3609 * newer snapshots for the given dataset. Therefore, we can
3610 * simply pass the name on to the ioctl() call. There is still
3611 * an unlikely race condition where the user has taken a
3612 * snapshot since we verified that this was the most recent.
3615 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3616 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3617 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3623 * For volumes, if the pre-rollback volsize matched the pre-
3624 * rollback reservation and the volsize has changed then set
3625 * the reservation property to the post-rollback volsize.
3626 * Make a new handle since the rollback closed the dataset.
3628 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3629 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3631 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3632 if (old_volsize != new_volsize)
3633 err = zfs_prop_set_int(zhp, resv_prop,
3642 * Renames the given dataset.
3645 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
3646 renameflags_t flags)
3649 zfs_cmd_t zc = { 0 };
3651 prop_changelist_t *cl = NULL;
3652 zfs_handle_t *zhrp = NULL;
3653 char *parentname = NULL;
3654 char parent[ZFS_MAXNAMELEN];
3655 char property[ZFS_MAXPROPLEN];
3656 libzfs_handle_t *hdl = zhp->zfs_hdl;
3659 /* if we have the same exact name, just return success */
3660 if (strcmp(zhp->zfs_name, target) == 0)
3663 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3664 "cannot rename to '%s'"), target);
3666 if (source != NULL) {
3668 * This is recursive snapshots rename, put snapshot name
3669 * (that might not exist) into zfs_name.
3671 assert(flags.recurse);
3673 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
3674 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
3675 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
3679 * Make sure the target name is valid
3681 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3682 if ((strchr(target, '@') == NULL) ||
3685 * Snapshot target name is abbreviated,
3686 * reconstruct full dataset name
3688 (void) strlcpy(parent, zhp->zfs_name,
3690 delim = strchr(parent, '@');
3691 if (strchr(target, '@') == NULL)
3695 (void) strlcat(parent, target, sizeof (parent));
3699 * Make sure we're renaming within the same dataset.
3701 delim = strchr(target, '@');
3702 if (strncmp(zhp->zfs_name, target, delim - target)
3703 != 0 || zhp->zfs_name[delim - target] != '@') {
3704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3705 "snapshots must be part of same "
3707 return (zfs_error(hdl, EZFS_CROSSTARGET,
3711 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3712 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3714 if (flags.recurse) {
3715 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3716 "recursive rename must be a snapshot"));
3717 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3720 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3721 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3723 /* validate parents */
3724 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3727 /* make sure we're in the same pool */
3728 verify((delim = strchr(target, '/')) != NULL);
3729 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3730 zhp->zfs_name[delim - target] != '/') {
3731 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3732 "datasets must be within same pool"));
3733 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3736 /* new name cannot be a child of the current dataset name */
3737 if (is_descendant(zhp->zfs_name, target)) {
3738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3739 "New dataset name cannot be a descendant of "
3740 "current dataset name"));
3741 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3745 (void) snprintf(errbuf, sizeof (errbuf),
3746 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3748 if (getzoneid() == GLOBAL_ZONEID &&
3749 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3751 "dataset is used in a non-global zone"));
3752 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3756 * Avoid unmounting file systems with mountpoint property set to
3757 * 'legacy' or 'none' even if -u option is not given.
3759 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3760 !flags.recurse && !flags.nounmount &&
3761 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
3762 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
3763 (strcmp(property, "legacy") == 0 ||
3764 strcmp(property, "none") == 0)) {
3765 flags.nounmount = B_TRUE;
3768 if (flags.recurse) {
3770 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3771 if (parentname == NULL) {
3775 delim = strchr(parentname, '@');
3777 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3784 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
3785 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
3786 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
3790 if (changelist_haszonedchild(cl)) {
3791 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3792 "child dataset with inherited mountpoint is used "
3793 "in a non-global zone"));
3794 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3798 if ((ret = changelist_prefix(cl)) != 0)
3802 if (ZFS_IS_VOLUME(zhp))
3803 zc.zc_objset_type = DMU_OST_ZVOL;
3805 zc.zc_objset_type = DMU_OST_ZFS;
3807 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3808 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3810 zc.zc_cookie = flags.recurse ? 1 : 0;
3811 if (flags.nounmount)
3814 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3816 * if it was recursive, the one that actually failed will
3819 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3820 "cannot rename '%s'"), zc.zc_name);
3822 if (flags.recurse && errno == EEXIST) {
3823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3824 "a child dataset already has a snapshot "
3825 "with the new name"));
3826 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3828 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3832 * On failure, we still want to remount any filesystems that
3833 * were previously mounted, so we don't alter the system state.
3836 (void) changelist_postfix(cl);
3838 if (!flags.recurse) {
3839 changelist_rename(cl, zfs_get_name(zhp), target);
3840 ret = changelist_postfix(cl);
3852 changelist_free(cl);
3858 zfs_get_user_props(zfs_handle_t *zhp)
3860 return (zhp->zfs_user_props);
3864 zfs_get_recvd_props(zfs_handle_t *zhp)
3866 if (zhp->zfs_recvd_props == NULL)
3867 if (get_recvd_props_ioctl(zhp) != 0)
3869 return (zhp->zfs_recvd_props);
3873 * This function is used by 'zfs list' to determine the exact set of columns to
3874 * display, and their maximum widths. This does two main things:
3876 * - If this is a list of all properties, then expand the list to include
3877 * all native properties, and set a flag so that for each dataset we look
3878 * for new unique user properties and add them to the list.
3880 * - For non fixed-width properties, keep track of the maximum width seen
3881 * so that we can size the column appropriately. If the user has
3882 * requested received property values, we also need to compute the width
3883 * of the RECEIVED column.
3886 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3888 libzfs_handle_t *hdl = zhp->zfs_hdl;
3889 zprop_list_t *entry;
3890 zprop_list_t **last, **start;
3891 nvlist_t *userprops, *propval;
3894 char buf[ZFS_MAXPROPLEN];
3896 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3899 userprops = zfs_get_user_props(zhp);
3902 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3904 * Go through and add any user properties as necessary. We
3905 * start by incrementing our list pointer to the first
3906 * non-native property.
3909 while (*start != NULL) {
3910 if ((*start)->pl_prop == ZPROP_INVAL)
3912 start = &(*start)->pl_next;
3916 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3918 * See if we've already found this property in our list.
3920 for (last = start; *last != NULL;
3921 last = &(*last)->pl_next) {
3922 if (strcmp((*last)->pl_user_prop,
3923 nvpair_name(elem)) == 0)
3927 if (*last == NULL) {
3928 if ((entry = zfs_alloc(hdl,
3929 sizeof (zprop_list_t))) == NULL ||
3930 ((entry->pl_user_prop = zfs_strdup(hdl,
3931 nvpair_name(elem)))) == NULL) {
3936 entry->pl_prop = ZPROP_INVAL;
3937 entry->pl_width = strlen(nvpair_name(elem));
3938 entry->pl_all = B_TRUE;
3945 * Now go through and check the width of any non-fixed columns
3947 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3948 if (entry->pl_fixed)
3951 if (entry->pl_prop != ZPROP_INVAL) {
3952 if (zfs_prop_get(zhp, entry->pl_prop,
3953 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3954 if (strlen(buf) > entry->pl_width)
3955 entry->pl_width = strlen(buf);
3957 if (received && zfs_prop_get_recvd(zhp,
3958 zfs_prop_to_name(entry->pl_prop),
3959 buf, sizeof (buf), B_FALSE) == 0)
3960 if (strlen(buf) > entry->pl_recvd_width)
3961 entry->pl_recvd_width = strlen(buf);
3963 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3965 verify(nvlist_lookup_string(propval,
3966 ZPROP_VALUE, &strval) == 0);
3967 if (strlen(strval) > entry->pl_width)
3968 entry->pl_width = strlen(strval);
3970 if (received && zfs_prop_get_recvd(zhp,
3971 entry->pl_user_prop,
3972 buf, sizeof (buf), B_FALSE) == 0)
3973 if (strlen(buf) > entry->pl_recvd_width)
3974 entry->pl_recvd_width = strlen(buf);
3982 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3983 char *resource, void *export, void *sharetab,
3984 int sharemax, zfs_share_op_t operation)
3986 zfs_cmd_t zc = { 0 };
3989 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3990 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3992 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3993 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3994 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3995 zc.zc_share.z_sharetype = operation;
3996 zc.zc_share.z_sharemax = sharemax;
3997 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4002 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4007 * Keep a reference to the props-table against which we prune the
4010 zhp->zfs_props_table = props;
4012 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4015 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4016 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4019 * User properties will result in ZPROP_INVAL, and since we
4020 * only know how to prune standard ZFS properties, we always
4021 * leave these in the list. This can also happen if we
4022 * encounter an unknown DSL property (when running older
4023 * software, for example).
4025 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4026 (void) nvlist_remove(zhp->zfs_props,
4027 nvpair_name(curr), nvpair_type(curr));
4034 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4035 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4037 zfs_cmd_t zc = { 0 };
4038 nvlist_t *nvlist = NULL;
4041 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4042 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4043 zc.zc_cookie = (uint64_t)cmd;
4045 if (cmd == ZFS_SMB_ACL_RENAME) {
4046 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4047 (void) no_memory(hdl);
4053 case ZFS_SMB_ACL_ADD:
4054 case ZFS_SMB_ACL_REMOVE:
4055 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4057 case ZFS_SMB_ACL_RENAME:
4058 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4060 (void) no_memory(hdl);
4063 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4065 (void) no_memory(hdl);
4068 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4069 nvlist_free(nvlist);
4073 case ZFS_SMB_ACL_PURGE:
4078 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4080 nvlist_free(nvlist);
4085 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4086 char *path, char *resource)
4088 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4093 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4094 char *path, char *resource)
4096 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4101 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4103 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4108 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4109 char *oldname, char *newname)
4111 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4117 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4118 zfs_userspace_cb_t func, void *arg)
4120 zfs_cmd_t zc = { 0 };
4121 zfs_useracct_t buf[100];
4122 libzfs_handle_t *hdl = zhp->zfs_hdl;
4125 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4127 zc.zc_objset_type = type;
4128 zc.zc_nvlist_dst = (uintptr_t)buf;
4131 zfs_useracct_t *zua = buf;
4133 zc.zc_nvlist_dst_size = sizeof (buf);
4134 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4137 (void) snprintf(errbuf, sizeof (errbuf),
4138 dgettext(TEXT_DOMAIN,
4139 "cannot get used/quota for %s"), zc.zc_name);
4140 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4142 if (zc.zc_nvlist_dst_size == 0)
4145 while (zc.zc_nvlist_dst_size > 0) {
4146 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4147 zua->zu_space)) != 0)
4150 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4159 const char *snapname;
4161 boolean_t recursive;
4166 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4168 struct holdarg *ha = arg;
4169 char name[ZFS_MAXNAMELEN];
4172 (void) snprintf(name, sizeof (name),
4173 "%s@%s", zhp->zfs_name, ha->snapname);
4175 if (lzc_exists(name))
4176 fnvlist_add_string(ha->nvl, name, ha->tag);
4179 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4185 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4186 boolean_t recursive, int cleanup_fd)
4191 ha.nvl = fnvlist_alloc();
4192 ha.snapname = snapname;
4194 ha.recursive = recursive;
4195 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4197 if (nvlist_empty(ha.nvl)) {
4200 fnvlist_free(ha.nvl);
4202 (void) snprintf(errbuf, sizeof (errbuf),
4203 dgettext(TEXT_DOMAIN,
4204 "cannot hold snapshot '%s@%s'"),
4205 zhp->zfs_name, snapname);
4206 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4210 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4211 fnvlist_free(ha.nvl);
4217 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4221 libzfs_handle_t *hdl = zhp->zfs_hdl;
4226 ret = lzc_hold(holds, cleanup_fd, &errors);
4229 /* There may be errors even in the success case. */
4230 fnvlist_free(errors);
4234 if (nvlist_empty(errors)) {
4235 /* no hold-specific errors */
4236 (void) snprintf(errbuf, sizeof (errbuf),
4237 dgettext(TEXT_DOMAIN, "cannot hold"));
4240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4241 "pool must be upgraded"));
4242 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4245 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4248 (void) zfs_standard_error(hdl, ret, errbuf);
4252 for (elem = nvlist_next_nvpair(errors, NULL);
4254 elem = nvlist_next_nvpair(errors, elem)) {
4255 (void) snprintf(errbuf, sizeof (errbuf),
4256 dgettext(TEXT_DOMAIN,
4257 "cannot hold snapshot '%s'"), nvpair_name(elem));
4258 switch (fnvpair_value_int32(elem)) {
4261 * Temporary tags wind up having the ds object id
4262 * prepended. So even if we passed the length check
4263 * above, it's still possible for the tag to wind
4264 * up being slightly too long.
4266 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4269 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4272 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4275 (void) zfs_standard_error(hdl,
4276 fnvpair_value_int32(elem), errbuf);
4280 fnvlist_free(errors);
4285 zfs_release_one(zfs_handle_t *zhp, void *arg)
4287 struct holdarg *ha = arg;
4288 char name[ZFS_MAXNAMELEN];
4290 nvlist_t *existing_holds;
4292 (void) snprintf(name, sizeof (name),
4293 "%s@%s", zhp->zfs_name, ha->snapname);
4295 if (lzc_get_holds(name, &existing_holds) != 0) {
4297 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4300 nvlist_t *torelease = fnvlist_alloc();
4301 fnvlist_add_boolean(torelease, ha->tag);
4302 fnvlist_add_nvlist(ha->nvl, name, torelease);
4303 fnvlist_free(torelease);
4307 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4313 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4314 boolean_t recursive)
4318 nvlist_t *errors = NULL;
4320 libzfs_handle_t *hdl = zhp->zfs_hdl;
4323 ha.nvl = fnvlist_alloc();
4324 ha.snapname = snapname;
4326 ha.recursive = recursive;
4328 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4330 if (nvlist_empty(ha.nvl)) {
4331 fnvlist_free(ha.nvl);
4333 (void) snprintf(errbuf, sizeof (errbuf),
4334 dgettext(TEXT_DOMAIN,
4335 "cannot release hold from snapshot '%s@%s'"),
4336 zhp->zfs_name, snapname);
4338 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4340 (void) zfs_standard_error(hdl, ret, errbuf);
4345 ret = lzc_release(ha.nvl, &errors);
4346 fnvlist_free(ha.nvl);
4349 /* There may be errors even in the success case. */
4350 fnvlist_free(errors);
4354 if (nvlist_empty(errors)) {
4355 /* no hold-specific errors */
4356 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4361 "pool must be upgraded"));
4362 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4365 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4369 for (elem = nvlist_next_nvpair(errors, NULL);
4371 elem = nvlist_next_nvpair(errors, elem)) {
4372 (void) snprintf(errbuf, sizeof (errbuf),
4373 dgettext(TEXT_DOMAIN,
4374 "cannot release hold from snapshot '%s'"),
4376 switch (fnvpair_value_int32(elem)) {
4378 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4381 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4384 (void) zfs_standard_error_fmt(hdl,
4385 fnvpair_value_int32(elem), errbuf);
4389 fnvlist_free(errors);
4394 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4396 zfs_cmd_t zc = { 0 };
4397 libzfs_handle_t *hdl = zhp->zfs_hdl;
4403 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4404 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4408 nvbuf = malloc(nvsz);
4409 if (nvbuf == NULL) {
4410 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4414 zc.zc_nvlist_dst_size = nvsz;
4415 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4417 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4419 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4420 (void) snprintf(errbuf, sizeof (errbuf),
4421 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4426 nvsz = zc.zc_nvlist_dst_size;
4430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4431 "pool must be upgraded"));
4432 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4435 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4438 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4441 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4446 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4448 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4449 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4451 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4461 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4463 zfs_cmd_t zc = { 0 };
4464 libzfs_handle_t *hdl = zhp->zfs_hdl;
4470 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4471 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4473 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4476 nvbuf = malloc(nvsz);
4478 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4481 zc.zc_nvlist_src_size = nvsz;
4482 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4483 zc.zc_perm_action = un;
4485 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4487 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4488 (void) snprintf(errbuf, sizeof (errbuf),
4489 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4494 "pool must be upgraded"));
4495 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4498 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4501 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4504 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4515 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4520 err = lzc_get_holds(zhp->zfs_name, nvl);
4523 libzfs_handle_t *hdl = zhp->zfs_hdl;
4525 (void) snprintf(errbuf, sizeof (errbuf),
4526 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4531 "pool must be upgraded"));
4532 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4535 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4538 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4541 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4550 * Convert the zvol's volume size to an appropriate reservation.
4551 * Note: If this routine is updated, it is necessary to update the ZFS test
4552 * suite's shell version in reservation.kshlib.
4555 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4558 uint64_t nblocks, volblocksize;
4562 if (nvlist_lookup_string(props,
4563 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4564 ncopies = atoi(strval);
4567 if (nvlist_lookup_uint64(props,
4568 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4569 &volblocksize) != 0)
4570 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4571 nblocks = volsize/volblocksize;
4572 /* start with metadnode L0-L6 */
4574 /* calculate number of indirects */
4575 while (nblocks > 1) {
4576 nblocks += DNODES_PER_LEVEL - 1;
4577 nblocks /= DNODES_PER_LEVEL;
4580 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4583 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4584 * compressed, but in practice they compress down to about
4587 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4593 * Attach/detach the given filesystem to/from the given jail.
4596 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4598 libzfs_handle_t *hdl = zhp->zfs_hdl;
4599 zfs_cmd_t zc = { 0 };
4605 (void) snprintf(errbuf, sizeof (errbuf),
4606 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4608 (void) snprintf(errbuf, sizeof (errbuf),
4609 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4612 switch (zhp->zfs_type) {
4613 case ZFS_TYPE_VOLUME:
4614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4615 "volumes can not be jailed"));
4616 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4617 case ZFS_TYPE_SNAPSHOT:
4618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4619 "snapshots can not be jailed"));
4620 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4622 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4624 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4625 zc.zc_objset_type = DMU_OST_ZFS;
4626 zc.zc_jailid = jailid;
4628 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4629 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4630 zfs_standard_error(hdl, errno, errbuf);