4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
43 #include <sys/mntent.h>
44 #include <sys/mount.h>
51 #include <sys/dnode.h>
57 #include "zfs_namecheck.h"
59 #include "libzfs_impl.h"
60 #include "zfs_deleg.h"
62 static int userquota_propname_decode(const char *propname, boolean_t zoned,
63 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
66 * Given a single type (not a mask of types), return the type in a human
70 zfs_type_to_name(zfs_type_t type)
73 case ZFS_TYPE_FILESYSTEM:
74 return (dgettext(TEXT_DOMAIN, "filesystem"));
75 case ZFS_TYPE_SNAPSHOT:
76 return (dgettext(TEXT_DOMAIN, "snapshot"));
78 return (dgettext(TEXT_DOMAIN, "volume"));
85 * Given a path and mask of ZFS types, return a string describing this dataset.
86 * This is used when we fail to open a dataset and we cannot get an exact type.
87 * We guess what the type would have been based on the path and the mask of
91 path_to_str(const char *path, int types)
94 * When given a single type, always report the exact type.
96 if (types == ZFS_TYPE_SNAPSHOT)
97 return (dgettext(TEXT_DOMAIN, "snapshot"));
98 if (types == ZFS_TYPE_FILESYSTEM)
99 return (dgettext(TEXT_DOMAIN, "filesystem"));
100 if (types == ZFS_TYPE_VOLUME)
101 return (dgettext(TEXT_DOMAIN, "volume"));
104 * The user is requesting more than one type of dataset. If this is the
105 * case, consult the path itself. If we're looking for a snapshot, and
106 * a '@' is found, then report it as "snapshot". Otherwise, remove the
107 * snapshot attribute and try again.
109 if (types & ZFS_TYPE_SNAPSHOT) {
110 if (strchr(path, '@') != NULL)
111 return (dgettext(TEXT_DOMAIN, "snapshot"));
112 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
116 * The user has requested either filesystems or volumes.
117 * We have no way of knowing a priori what type this would be, so always
118 * report it as "filesystem" or "volume", our two primitive types.
120 if (types & ZFS_TYPE_FILESYSTEM)
121 return (dgettext(TEXT_DOMAIN, "filesystem"));
123 assert(types & ZFS_TYPE_VOLUME);
124 return (dgettext(TEXT_DOMAIN, "volume"));
128 * Validate a ZFS path. This is used even before trying to open the dataset, to
129 * provide a more meaningful error message. We call zfs_error_aux() to
130 * explain exactly why the name was not valid.
133 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
139 (void) zfs_prop_get_table();
140 if (dataset_namecheck(path, &why, &what) != 0) {
143 case NAME_ERR_TOOLONG:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "name is too long"));
148 case NAME_ERR_LEADING_SLASH:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "leading slash in name"));
153 case NAME_ERR_EMPTY_COMPONENT:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "empty component in name"));
158 case NAME_ERR_TRAILING_SLASH:
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "trailing slash in name"));
163 case NAME_ERR_INVALCHAR:
165 dgettext(TEXT_DOMAIN, "invalid character "
166 "'%c' in name"), what);
169 case NAME_ERR_MULTIPLE_AT:
170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 "multiple '@' delimiters in name"));
174 case NAME_ERR_NOLETTER:
175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176 "pool doesn't begin with a letter"));
179 case NAME_ERR_RESERVED:
180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
181 "name is reserved"));
184 case NAME_ERR_DISKLIKE:
185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
186 "reserved disk name"));
194 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
197 "snapshot delimiter '@' in filesystem name"));
201 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
204 "missing '@' delimiter in snapshot name"));
208 if (modifying && strchr(path, '%') != NULL) {
210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
211 "invalid character %c in name"), '%');
219 zfs_name_valid(const char *name, zfs_type_t type)
221 if (type == ZFS_TYPE_POOL)
222 return (zpool_name_valid(NULL, B_FALSE, name));
223 return (zfs_validate_name(NULL, name, type, B_FALSE));
227 * This function takes the raw DSL properties, and filters out the user-defined
228 * properties into a separate nvlist.
231 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
233 libzfs_handle_t *hdl = zhp->zfs_hdl;
238 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
239 (void) no_memory(hdl);
244 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
245 if (!zfs_prop_user(nvpair_name(elem)))
248 verify(nvpair_value_nvlist(elem, &propval) == 0);
249 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
251 (void) no_memory(hdl);
259 static zpool_handle_t *
260 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
262 libzfs_handle_t *hdl = zhp->zfs_hdl;
265 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
266 if (hdl->libzfs_pool_handles != NULL)
267 zph->zpool_next = hdl->libzfs_pool_handles;
268 hdl->libzfs_pool_handles = zph;
273 static zpool_handle_t *
274 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
276 libzfs_handle_t *hdl = zhp->zfs_hdl;
277 zpool_handle_t *zph = hdl->libzfs_pool_handles;
279 while ((zph != NULL) &&
280 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
281 zph = zph->zpool_next;
286 * Returns a handle to the pool that contains the provided dataset.
287 * If a handle to that pool already exists then that handle is returned.
288 * Otherwise, a new handle is created and added to the list of handles.
290 static zpool_handle_t *
291 zpool_handle(zfs_handle_t *zhp)
297 len = strcspn(zhp->zfs_name, "/@") + 1;
298 pool_name = zfs_alloc(zhp->zfs_hdl, len);
299 (void) strlcpy(pool_name, zhp->zfs_name, len);
301 zph = zpool_find_handle(zhp, pool_name, len);
303 zph = zpool_add_handle(zhp, pool_name);
310 zpool_free_handles(libzfs_handle_t *hdl)
312 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
314 while (zph != NULL) {
315 next = zph->zpool_next;
319 hdl->libzfs_pool_handles = NULL;
323 * Utility function to gather stats (objset and zpl) for the given object.
326 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
328 libzfs_handle_t *hdl = zhp->zfs_hdl;
330 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
332 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
333 if (errno == ENOMEM) {
334 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
345 * Utility function to get the received properties of the given object.
348 get_recvd_props_ioctl(zfs_handle_t *zhp)
350 libzfs_handle_t *hdl = zhp->zfs_hdl;
351 nvlist_t *recvdprops;
352 zfs_cmd_t zc = { 0 };
355 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
358 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
360 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
361 if (errno == ENOMEM) {
362 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
366 zcmd_free_nvlists(&zc);
371 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
372 zcmd_free_nvlists(&zc);
376 nvlist_free(zhp->zfs_recvd_props);
377 zhp->zfs_recvd_props = recvdprops;
383 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
385 nvlist_t *allprops, *userprops;
387 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
389 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
394 * XXX Why do we store the user props separately, in addition to
395 * storing them in zfs_props?
397 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
398 nvlist_free(allprops);
402 nvlist_free(zhp->zfs_props);
403 nvlist_free(zhp->zfs_user_props);
405 zhp->zfs_props = allprops;
406 zhp->zfs_user_props = userprops;
412 get_stats(zfs_handle_t *zhp)
415 zfs_cmd_t zc = { 0 };
417 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
419 if (get_stats_ioctl(zhp, &zc) != 0)
421 else if (put_stats_zhdl(zhp, &zc) != 0)
423 zcmd_free_nvlists(&zc);
428 * Refresh the properties currently stored in the handle.
431 zfs_refresh_properties(zfs_handle_t *zhp)
433 (void) get_stats(zhp);
437 * Makes a handle from the given dataset name. Used by zfs_open() and
438 * zfs_iter_* to create child handles on the fly.
441 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
443 if (put_stats_zhdl(zhp, zc) != 0)
447 * We've managed to open the dataset and gather statistics. Determine
448 * the high-level type.
450 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
451 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
452 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
453 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
457 if (zhp->zfs_dmustats.dds_is_snapshot)
458 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
459 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
460 zhp->zfs_type = ZFS_TYPE_VOLUME;
461 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
462 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
464 abort(); /* we should never see any other types */
466 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
473 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
475 zfs_cmd_t zc = { 0 };
477 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
483 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
484 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
488 if (get_stats_ioctl(zhp, &zc) == -1) {
489 zcmd_free_nvlists(&zc);
493 if (make_dataset_handle_common(zhp, &zc) == -1) {
497 zcmd_free_nvlists(&zc);
502 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
504 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
510 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
511 if (make_dataset_handle_common(zhp, zc) == -1) {
519 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
521 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
526 zhp->zfs_hdl = pzhp->zfs_hdl;
527 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
528 zhp->zfs_head_type = pzhp->zfs_type;
529 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
530 zhp->zpool_hdl = zpool_handle(zhp);
535 zfs_handle_dup(zfs_handle_t *zhp_orig)
537 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
542 zhp->zfs_hdl = zhp_orig->zfs_hdl;
543 zhp->zpool_hdl = zhp_orig->zpool_hdl;
544 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
545 sizeof (zhp->zfs_name));
546 zhp->zfs_type = zhp_orig->zfs_type;
547 zhp->zfs_head_type = zhp_orig->zfs_head_type;
548 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
549 if (zhp_orig->zfs_props != NULL) {
550 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
551 (void) no_memory(zhp->zfs_hdl);
556 if (zhp_orig->zfs_user_props != NULL) {
557 if (nvlist_dup(zhp_orig->zfs_user_props,
558 &zhp->zfs_user_props, 0) != 0) {
559 (void) no_memory(zhp->zfs_hdl);
564 if (zhp_orig->zfs_recvd_props != NULL) {
565 if (nvlist_dup(zhp_orig->zfs_recvd_props,
566 &zhp->zfs_recvd_props, 0)) {
567 (void) no_memory(zhp->zfs_hdl);
572 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
573 if (zhp_orig->zfs_mntopts != NULL) {
574 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
575 zhp_orig->zfs_mntopts);
577 zhp->zfs_props_table = zhp_orig->zfs_props_table;
582 * Opens the given snapshot, filesystem, or volume. The 'types'
583 * argument is a mask of acceptable types. The function will print an
584 * appropriate error message and return NULL if it can't be opened.
587 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
592 (void) snprintf(errbuf, sizeof (errbuf),
593 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
596 * Validate the name before we even try to open it.
598 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
600 "invalid dataset name"));
601 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
606 * Try to get stats for the dataset, which will tell us if it exists.
609 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
610 (void) zfs_standard_error(hdl, errno, errbuf);
614 if (!(types & zhp->zfs_type)) {
615 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
624 * Release a ZFS handle. Nothing to do but free the associated memory.
627 zfs_close(zfs_handle_t *zhp)
629 if (zhp->zfs_mntopts)
630 free(zhp->zfs_mntopts);
631 nvlist_free(zhp->zfs_props);
632 nvlist_free(zhp->zfs_user_props);
633 nvlist_free(zhp->zfs_recvd_props);
637 typedef struct mnttab_node {
638 struct mnttab mtn_mt;
643 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
645 const mnttab_node_t *mtn1 = arg1;
646 const mnttab_node_t *mtn2 = arg2;
649 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
653 return (rv > 0 ? 1 : -1);
657 libzfs_mnttab_init(libzfs_handle_t *hdl)
659 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
660 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
661 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
665 libzfs_mnttab_update(libzfs_handle_t *hdl)
669 rewind(hdl->libzfs_mnttab);
670 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
673 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
675 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
676 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
677 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
678 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
679 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
680 avl_add(&hdl->libzfs_mnttab_cache, mtn);
685 libzfs_mnttab_fini(libzfs_handle_t *hdl)
690 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
691 free(mtn->mtn_mt.mnt_special);
692 free(mtn->mtn_mt.mnt_mountp);
693 free(mtn->mtn_mt.mnt_fstype);
694 free(mtn->mtn_mt.mnt_mntopts);
697 avl_destroy(&hdl->libzfs_mnttab_cache);
701 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
703 hdl->libzfs_mnttab_enable = enable;
707 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
708 struct mnttab *entry)
713 if (!hdl->libzfs_mnttab_enable) {
714 struct mnttab srch = { 0 };
716 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
717 libzfs_mnttab_fini(hdl);
718 rewind(hdl->libzfs_mnttab);
719 srch.mnt_special = (char *)fsname;
720 srch.mnt_fstype = MNTTYPE_ZFS;
721 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
727 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
728 libzfs_mnttab_update(hdl);
730 find.mtn_mt.mnt_special = (char *)fsname;
731 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
733 *entry = mtn->mtn_mt;
740 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
741 const char *mountp, const char *mntopts)
745 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
747 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
748 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
749 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
750 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
751 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
752 avl_add(&hdl->libzfs_mnttab_cache, mtn);
756 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
761 find.mtn_mt.mnt_special = (char *)fsname;
762 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
763 avl_remove(&hdl->libzfs_mnttab_cache, ret);
764 free(ret->mtn_mt.mnt_special);
765 free(ret->mtn_mt.mnt_mountp);
766 free(ret->mtn_mt.mnt_fstype);
767 free(ret->mtn_mt.mnt_mntopts);
773 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
775 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
777 if (zpool_handle == NULL)
780 *spa_version = zpool_get_prop_int(zpool_handle,
781 ZPOOL_PROP_VERSION, NULL);
786 * The choice of reservation property depends on the SPA version.
789 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
793 if (zfs_spa_version(zhp, &spa_version) < 0)
796 if (spa_version >= SPA_VERSION_REFRESERVATION)
797 *resv_prop = ZFS_PROP_REFRESERVATION;
799 *resv_prop = ZFS_PROP_RESERVATION;
805 * Given an nvlist of properties to set, validates that they are correct, and
806 * parses any numeric properties (index, boolean, etc) if they are specified as
810 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
811 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
818 int chosen_normal = -1;
821 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
822 (void) no_memory(hdl);
827 * Make sure this property is valid and applies to this type.
831 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
832 const char *propname = nvpair_name(elem);
834 prop = zfs_name_to_prop(propname);
835 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
837 * This is a user property: make sure it's a
838 * string, and that it's less than ZAP_MAXNAMELEN.
840 if (nvpair_type(elem) != DATA_TYPE_STRING) {
841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
842 "'%s' must be a string"), propname);
843 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
847 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
849 "property name '%s' is too long"),
851 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
855 (void) nvpair_value_string(elem, &strval);
856 if (nvlist_add_string(ret, propname, strval) != 0) {
857 (void) no_memory(hdl);
864 * Currently, only user properties can be modified on
867 if (type == ZFS_TYPE_SNAPSHOT) {
868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
869 "this property can not be modified for snapshots"));
870 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
874 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
875 zfs_userquota_prop_t uqtype;
876 char newpropname[128];
881 if (userquota_propname_decode(propname, zoned,
882 &uqtype, domain, sizeof (domain), &rid) != 0) {
884 dgettext(TEXT_DOMAIN,
885 "'%s' has an invalid user/group name"),
887 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
891 if (uqtype != ZFS_PROP_USERQUOTA &&
892 uqtype != ZFS_PROP_GROUPQUOTA) {
894 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
896 (void) zfs_error(hdl, EZFS_PROPREADONLY,
901 if (nvpair_type(elem) == DATA_TYPE_STRING) {
902 (void) nvpair_value_string(elem, &strval);
903 if (strcmp(strval, "none") == 0) {
905 } else if (zfs_nicestrtonum(hdl,
906 strval, &intval) != 0) {
907 (void) zfs_error(hdl,
908 EZFS_BADPROP, errbuf);
911 } else if (nvpair_type(elem) ==
913 (void) nvpair_value_uint64(elem, &intval);
915 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
916 "use 'none' to disable "
917 "userquota/groupquota"));
921 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
922 "'%s' must be a number"), propname);
923 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
928 * Encode the prop name as
929 * userquota@<hex-rid>-domain, to make it easy
930 * for the kernel to decode.
932 (void) snprintf(newpropname, sizeof (newpropname),
933 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
934 (longlong_t)rid, domain);
938 if (nvlist_add_uint64_array(ret, newpropname,
940 (void) no_memory(hdl);
944 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
948 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
952 if (prop == ZPROP_INVAL) {
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "invalid property '%s'"), propname);
955 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
959 if (!zfs_prop_valid_for_type(prop, type)) {
961 dgettext(TEXT_DOMAIN, "'%s' does not "
962 "apply to datasets of this type"), propname);
963 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
967 if (zfs_prop_readonly(prop) &&
968 (!zfs_prop_setonce(prop) || zhp != NULL)) {
970 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
972 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
976 if (zprop_parse_value(hdl, elem, prop, type, ret,
977 &strval, &intval, errbuf) != 0)
981 * Perform some additional checks for specific properties.
984 case ZFS_PROP_VERSION:
990 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
991 if (intval < version) {
992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
993 "Can not downgrade; already at version %u"),
995 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1001 case ZFS_PROP_RECORDSIZE:
1002 case ZFS_PROP_VOLBLOCKSIZE:
1003 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
1004 if (intval < SPA_MINBLOCKSIZE ||
1005 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
1006 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1007 "'%s' must be power of 2 from %u "
1008 "to %uk"), propname,
1009 (uint_t)SPA_MINBLOCKSIZE,
1010 (uint_t)SPA_MAXBLOCKSIZE >> 10);
1011 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1016 case ZFS_PROP_MLSLABEL:
1020 * Verify the mlslabel string and convert to
1021 * internal hex label string.
1025 char *hex = NULL; /* internal label string */
1027 /* Default value is already OK. */
1028 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1031 /* Verify the label can be converted to binary form */
1032 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1033 (str_to_label(strval, &new_sl, MAC_LABEL,
1034 L_NO_CORRECTION, NULL) == -1)) {
1038 /* Now translate to hex internal label string */
1039 if (label_to_str(new_sl, &hex, M_INTERNAL,
1045 m_label_free(new_sl);
1047 /* If string is already in internal form, we're done. */
1048 if (strcmp(strval, hex) == 0) {
1053 /* Replace the label string with the internal form. */
1054 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1056 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1064 "invalid mlslabel '%s'"), strval);
1065 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1066 m_label_free(new_sl); /* OK if null */
1068 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1069 "mlslabel is not supported on FreeBSD"));
1070 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1076 case ZFS_PROP_MOUNTPOINT:
1078 namecheck_err_t why;
1080 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1081 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1084 if (mountpoint_namecheck(strval, &why)) {
1086 case NAME_ERR_LEADING_SLASH:
1088 dgettext(TEXT_DOMAIN,
1089 "'%s' must be an absolute path, "
1090 "'none', or 'legacy'"), propname);
1092 case NAME_ERR_TOOLONG:
1094 dgettext(TEXT_DOMAIN,
1095 "component of '%s' is too long"),
1099 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1106 case ZFS_PROP_SHARESMB:
1107 case ZFS_PROP_SHARENFS:
1109 * For the mountpoint and sharenfs or sharesmb
1110 * properties, check if it can be set in a
1111 * global/non-global zone based on
1112 * the zoned property value:
1114 * global zone non-global zone
1115 * --------------------------------------------------
1116 * zoned=on mountpoint (no) mountpoint (yes)
1117 * sharenfs (no) sharenfs (no)
1118 * sharesmb (no) sharesmb (no)
1120 * zoned=off mountpoint (yes) N/A
1125 if (getzoneid() == GLOBAL_ZONEID) {
1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127 "'%s' cannot be set on "
1128 "dataset in a non-global zone"),
1130 (void) zfs_error(hdl, EZFS_ZONED,
1133 } else if (prop == ZFS_PROP_SHARENFS ||
1134 prop == ZFS_PROP_SHARESMB) {
1135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136 "'%s' cannot be set in "
1137 "a non-global zone"), propname);
1138 (void) zfs_error(hdl, EZFS_ZONED,
1142 } else if (getzoneid() != GLOBAL_ZONEID) {
1144 * If zoned property is 'off', this must be in
1145 * a global zone. If not, something is wrong.
1147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1148 "'%s' cannot be set while dataset "
1149 "'zoned' property is set"), propname);
1150 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1155 * At this point, it is legitimate to set the
1156 * property. Now we want to make sure that the
1157 * property value is valid if it is sharenfs.
1159 if ((prop == ZFS_PROP_SHARENFS ||
1160 prop == ZFS_PROP_SHARESMB) &&
1161 strcmp(strval, "on") != 0 &&
1162 strcmp(strval, "off") != 0) {
1163 zfs_share_proto_t proto;
1165 if (prop == ZFS_PROP_SHARESMB)
1171 * Must be an valid sharing protocol
1172 * option string so init the libshare
1173 * in order to enable the parser and
1174 * then parse the options. We use the
1175 * control API since we don't care about
1176 * the current configuration and don't
1177 * want the overhead of loading it
1178 * until we actually do something.
1181 if (zfs_init_libshare(hdl,
1182 SA_INIT_CONTROL_API) != SA_OK) {
1184 * An error occurred so we can't do
1187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1188 "'%s' cannot be set: problem "
1189 "in share initialization"),
1191 (void) zfs_error(hdl, EZFS_BADPROP,
1196 if (zfs_parse_options(strval, proto) != SA_OK) {
1198 * There was an error in parsing so
1199 * deal with it by issuing an error
1200 * message and leaving after
1201 * uninitializing the the libshare
1204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1205 "'%s' cannot be set to invalid "
1206 "options"), propname);
1207 (void) zfs_error(hdl, EZFS_BADPROP,
1209 zfs_uninit_libshare(hdl);
1212 zfs_uninit_libshare(hdl);
1216 case ZFS_PROP_UTF8ONLY:
1217 chosen_utf = (int)intval;
1219 case ZFS_PROP_NORMALIZE:
1220 chosen_normal = (int)intval;
1225 * For changes to existing volumes, we have some additional
1226 * checks to enforce.
1228 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1229 uint64_t volsize = zfs_prop_get_int(zhp,
1231 uint64_t blocksize = zfs_prop_get_int(zhp,
1232 ZFS_PROP_VOLBLOCKSIZE);
1236 case ZFS_PROP_RESERVATION:
1237 case ZFS_PROP_REFRESERVATION:
1238 if (intval > volsize) {
1239 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1240 "'%s' is greater than current "
1241 "volume size"), propname);
1242 (void) zfs_error(hdl, EZFS_BADPROP,
1248 case ZFS_PROP_VOLSIZE:
1249 if (intval % blocksize != 0) {
1250 zfs_nicenum(blocksize, buf,
1252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1253 "'%s' must be a multiple of "
1254 "volume block size (%s)"),
1256 (void) zfs_error(hdl, EZFS_BADPROP,
1262 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1263 "'%s' cannot be zero"),
1265 (void) zfs_error(hdl, EZFS_BADPROP,
1275 * If normalization was chosen, but no UTF8 choice was made,
1276 * enforce rejection of non-UTF8 names.
1278 * If normalization was chosen, but rejecting non-UTF8 names
1279 * was explicitly not chosen, it is an error.
1281 if (chosen_normal > 0 && chosen_utf < 0) {
1282 if (nvlist_add_uint64(ret,
1283 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1284 (void) no_memory(hdl);
1287 } else if (chosen_normal > 0 && chosen_utf == 0) {
1288 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1289 "'%s' must be set 'on' if normalization chosen"),
1290 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1291 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1302 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1304 uint64_t old_volsize;
1305 uint64_t new_volsize;
1306 uint64_t old_reservation;
1307 uint64_t new_reservation;
1308 zfs_prop_t resv_prop;
1311 * If this is an existing volume, and someone is setting the volsize,
1312 * make sure that it matches the reservation, or add it if necessary.
1314 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1315 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1317 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1318 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1319 old_reservation) || nvlist_lookup_uint64(nvl,
1320 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1323 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1326 new_reservation = zvol_volsize_to_reservation(new_volsize,
1328 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1329 new_reservation) != 0) {
1330 (void) no_memory(zhp->zfs_hdl);
1337 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1344 * For quotas and reservations, ENOSPC indicates
1345 * something different; setting a quota or reservation
1346 * doesn't use any disk space.
1349 case ZFS_PROP_QUOTA:
1350 case ZFS_PROP_REFQUOTA:
1351 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352 "size is less than current used or "
1354 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1357 case ZFS_PROP_RESERVATION:
1358 case ZFS_PROP_REFRESERVATION:
1359 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1360 "size is greater than available space"));
1361 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1365 (void) zfs_standard_error(hdl, err, errbuf);
1371 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1375 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1379 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1380 "pool and or dataset must be upgraded to set this "
1381 "property or value"));
1382 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1386 if (prop == ZFS_PROP_COMPRESSION) {
1387 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1388 "property setting is not allowed on "
1389 "bootable datasets"));
1390 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1392 (void) zfs_standard_error(hdl, err, errbuf);
1397 if (prop == ZPROP_INVAL) {
1398 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1400 (void) zfs_standard_error(hdl, err, errbuf);
1406 * This platform can't address a volume this big.
1409 if (prop == ZFS_PROP_VOLSIZE) {
1410 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1416 (void) zfs_standard_error(hdl, err, errbuf);
1421 * Given a property name and value, set the property for the given dataset.
1424 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1426 zfs_cmd_t zc = { 0 };
1428 prop_changelist_t *cl = NULL;
1430 libzfs_handle_t *hdl = zhp->zfs_hdl;
1431 nvlist_t *nvl = NULL, *realprops;
1433 boolean_t do_prefix;
1437 (void) snprintf(errbuf, sizeof (errbuf),
1438 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1441 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1442 nvlist_add_string(nvl, propname, propval) != 0) {
1443 (void) no_memory(hdl);
1447 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1448 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1454 prop = zfs_name_to_prop(propname);
1456 /* We don't support those properties on FreeBSD. */
1458 case ZFS_PROP_DEVICES:
1459 case ZFS_PROP_ISCSIOPTIONS:
1460 case ZFS_PROP_XATTR:
1461 case ZFS_PROP_VSCAN:
1462 case ZFS_PROP_NBMAND:
1463 case ZFS_PROP_MLSLABEL:
1464 (void) snprintf(errbuf, sizeof (errbuf),
1465 "property '%s' not supported on FreeBSD", propname);
1466 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1470 if (prop == ZFS_PROP_VOLSIZE) {
1471 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1475 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1478 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1480 "child dataset with inherited mountpoint is used "
1481 "in a non-global zone"));
1482 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1487 * If the dataset's canmount property is being set to noauto,
1488 * or being set to on and the dataset is already mounted,
1489 * then we want to prevent unmounting & remounting it.
1491 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1492 (zprop_string_to_index(prop, propval, &idx,
1493 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO ||
1494 (idx == ZFS_CANMOUNT_ON && zfs_is_mounted(zhp, NULL))));
1496 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1500 * Execute the corresponding ioctl() to set this property.
1502 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1504 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1507 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1510 zfs_setprop_error(hdl, prop, errno, errbuf);
1511 if (added_resv && errno == ENOSPC) {
1512 /* clean up the volsize property we tried to set */
1513 uint64_t old_volsize = zfs_prop_get_int(zhp,
1516 zcmd_free_nvlists(&zc);
1517 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1519 if (nvlist_add_uint64(nvl,
1520 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1523 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1525 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1529 ret = changelist_postfix(cl);
1532 * Refresh the statistics so the new property value
1536 (void) get_stats(zhp);
1541 zcmd_free_nvlists(&zc);
1543 changelist_free(cl);
1548 * Given a property, inherit the value from the parent dataset, or if received
1549 * is TRUE, revert to the received value, if any.
1552 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1554 zfs_cmd_t zc = { 0 };
1556 prop_changelist_t *cl;
1557 libzfs_handle_t *hdl = zhp->zfs_hdl;
1561 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1562 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1564 zc.zc_cookie = received;
1565 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1567 * For user properties, the amount of work we have to do is very
1568 * small, so just do it here.
1570 if (!zfs_prop_user(propname)) {
1571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1572 "invalid property"));
1573 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1576 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1577 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1579 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1580 return (zfs_standard_error(hdl, errno, errbuf));
1586 * Verify that this property is inheritable.
1588 if (zfs_prop_readonly(prop))
1589 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1591 if (!zfs_prop_inheritable(prop) && !received)
1592 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1595 * Check to see if the value applies to this type
1597 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1598 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1601 * Normalize the name, to get rid of shorthand abbreviations.
1603 propname = zfs_prop_to_name(prop);
1604 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1605 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1607 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1608 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1610 "dataset is used in a non-global zone"));
1611 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1615 * Determine datasets which will be affected by this change, if any.
1617 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1620 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1622 "child dataset with inherited mountpoint is used "
1623 "in a non-global zone"));
1624 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1628 if ((ret = changelist_prefix(cl)) != 0)
1631 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1632 return (zfs_standard_error(hdl, errno, errbuf));
1635 if ((ret = changelist_postfix(cl)) != 0)
1639 * Refresh the statistics so the new property is reflected.
1641 (void) get_stats(zhp);
1645 changelist_free(cl);
1650 * True DSL properties are stored in an nvlist. The following two functions
1651 * extract them appropriately.
1654 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1660 if (nvlist_lookup_nvlist(zhp->zfs_props,
1661 zfs_prop_to_name(prop), &nv) == 0) {
1662 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1663 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1665 verify(!zhp->zfs_props_table ||
1666 zhp->zfs_props_table[prop] == B_TRUE);
1667 value = zfs_prop_default_numeric(prop);
1675 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1681 if (nvlist_lookup_nvlist(zhp->zfs_props,
1682 zfs_prop_to_name(prop), &nv) == 0) {
1683 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1684 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1686 verify(!zhp->zfs_props_table ||
1687 zhp->zfs_props_table[prop] == B_TRUE);
1688 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1697 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1699 return (zhp->zfs_props == zhp->zfs_recvd_props);
1703 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1705 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1706 zhp->zfs_props = zhp->zfs_recvd_props;
1710 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1712 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1717 * Internal function for getting a numeric property. Both zfs_prop_get() and
1718 * zfs_prop_get_int() are built using this interface.
1720 * Certain properties can be overridden using 'mount -o'. In this case, scan
1721 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1722 * If they differ from the on-disk values, report the current values and mark
1723 * the source "temporary".
1726 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1727 char **source, uint64_t *val)
1729 zfs_cmd_t zc = { 0 };
1730 nvlist_t *zplprops = NULL;
1732 char *mntopt_on = NULL;
1733 char *mntopt_off = NULL;
1734 boolean_t received = zfs_is_recvd_props_mode(zhp);
1739 case ZFS_PROP_ATIME:
1740 mntopt_on = MNTOPT_ATIME;
1741 mntopt_off = MNTOPT_NOATIME;
1744 case ZFS_PROP_DEVICES:
1745 mntopt_on = MNTOPT_DEVICES;
1746 mntopt_off = MNTOPT_NODEVICES;
1750 mntopt_on = MNTOPT_EXEC;
1751 mntopt_off = MNTOPT_NOEXEC;
1754 case ZFS_PROP_READONLY:
1755 mntopt_on = MNTOPT_RO;
1756 mntopt_off = MNTOPT_RW;
1759 case ZFS_PROP_SETUID:
1760 mntopt_on = MNTOPT_SETUID;
1761 mntopt_off = MNTOPT_NOSETUID;
1764 case ZFS_PROP_XATTR:
1765 mntopt_on = MNTOPT_XATTR;
1766 mntopt_off = MNTOPT_NOXATTR;
1769 case ZFS_PROP_NBMAND:
1770 mntopt_on = MNTOPT_NBMAND;
1771 mntopt_off = MNTOPT_NONBMAND;
1776 * Because looking up the mount options is potentially expensive
1777 * (iterating over all of /etc/mnttab), we defer its calculation until
1778 * we're looking up a property which requires its presence.
1780 if (!zhp->zfs_mntcheck &&
1781 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1782 libzfs_handle_t *hdl = zhp->zfs_hdl;
1783 struct mnttab entry;
1785 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1786 zhp->zfs_mntopts = zfs_strdup(hdl,
1788 if (zhp->zfs_mntopts == NULL)
1792 zhp->zfs_mntcheck = B_TRUE;
1795 if (zhp->zfs_mntopts == NULL)
1796 mnt.mnt_mntopts = "";
1798 mnt.mnt_mntopts = zhp->zfs_mntopts;
1801 case ZFS_PROP_ATIME:
1802 case ZFS_PROP_DEVICES:
1804 case ZFS_PROP_READONLY:
1805 case ZFS_PROP_SETUID:
1806 case ZFS_PROP_XATTR:
1807 case ZFS_PROP_NBMAND:
1808 *val = getprop_uint64(zhp, prop, source);
1813 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1816 *src = ZPROP_SRC_TEMPORARY;
1817 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1820 *src = ZPROP_SRC_TEMPORARY;
1824 case ZFS_PROP_CANMOUNT:
1825 case ZFS_PROP_VOLSIZE:
1826 case ZFS_PROP_QUOTA:
1827 case ZFS_PROP_REFQUOTA:
1828 case ZFS_PROP_RESERVATION:
1829 case ZFS_PROP_REFRESERVATION:
1830 *val = getprop_uint64(zhp, prop, source);
1832 if (*source == NULL) {
1833 /* not default, must be local */
1834 *source = zhp->zfs_name;
1838 case ZFS_PROP_MOUNTED:
1839 *val = (zhp->zfs_mntopts != NULL);
1842 case ZFS_PROP_NUMCLONES:
1843 *val = zhp->zfs_dmustats.dds_num_clones;
1846 case ZFS_PROP_VERSION:
1847 case ZFS_PROP_NORMALIZE:
1848 case ZFS_PROP_UTF8ONLY:
1850 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1851 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1853 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1854 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1855 zcmd_free_nvlists(&zc);
1858 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1859 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1861 zcmd_free_nvlists(&zc);
1865 nvlist_free(zplprops);
1866 zcmd_free_nvlists(&zc);
1870 switch (zfs_prop_get_type(prop)) {
1871 case PROP_TYPE_NUMBER:
1872 case PROP_TYPE_INDEX:
1873 *val = getprop_uint64(zhp, prop, source);
1875 * If we tried to use a default value for a
1876 * readonly property, it means that it was not
1879 if (zfs_prop_readonly(prop) &&
1880 *source != NULL && (*source)[0] == '\0') {
1885 case PROP_TYPE_STRING:
1887 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1888 "cannot get non-numeric property"));
1889 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1890 dgettext(TEXT_DOMAIN, "internal error")));
1898 * Calculate the source type, given the raw source string.
1901 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1902 char *statbuf, size_t statlen)
1904 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1907 if (source == NULL) {
1908 *srctype = ZPROP_SRC_NONE;
1909 } else if (source[0] == '\0') {
1910 *srctype = ZPROP_SRC_DEFAULT;
1911 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1912 *srctype = ZPROP_SRC_RECEIVED;
1914 if (strcmp(source, zhp->zfs_name) == 0) {
1915 *srctype = ZPROP_SRC_LOCAL;
1917 (void) strlcpy(statbuf, source, statlen);
1918 *srctype = ZPROP_SRC_INHERITED;
1925 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1926 size_t proplen, boolean_t literal)
1931 if (zhp->zfs_recvd_props == NULL)
1932 if (get_recvd_props_ioctl(zhp) != 0)
1935 prop = zfs_name_to_prop(propname);
1937 if (prop != ZPROP_INVAL) {
1939 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1941 zfs_set_recvd_props_mode(zhp, &cookie);
1942 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1943 NULL, NULL, 0, literal);
1944 zfs_unset_recvd_props_mode(zhp, &cookie);
1948 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1949 propname, &propval) != 0)
1951 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1953 (void) strlcpy(propbuf, recvdval, proplen);
1956 return (err == 0 ? 0 : -1);
1960 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1965 value = zfs_get_clones_nvl(zhp);
1970 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1971 pair = nvlist_next_nvpair(value, pair)) {
1972 if (propbuf[0] != '\0')
1973 (void) strlcat(propbuf, ",", proplen);
1974 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1980 struct get_clones_arg {
1984 char buf[ZFS_MAXNAMELEN];
1988 get_clones_cb(zfs_handle_t *zhp, void *arg)
1990 struct get_clones_arg *gca = arg;
1992 if (gca->numclones == 0) {
1997 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
1998 NULL, NULL, 0, B_TRUE) != 0)
2000 if (strcmp(gca->buf, gca->origin) == 0) {
2001 if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
2003 return (no_memory(zhp->zfs_hdl));
2009 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2015 zfs_get_clones_nvl(zfs_handle_t *zhp)
2017 nvlist_t *nv, *value;
2019 if (nvlist_lookup_nvlist(zhp->zfs_props,
2020 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2021 struct get_clones_arg gca;
2024 * if this is a snapshot, then the kernel wasn't able
2025 * to get the clones. Do it by slowly iterating.
2027 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2029 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2031 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2036 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2038 gca.origin = zhp->zfs_name;
2040 if (gca.numclones != 0) {
2042 char pool[ZFS_MAXNAMELEN];
2045 /* get the pool name */
2046 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2047 (void) strsep(&cp, "/@");
2048 root = zfs_open(zhp->zfs_hdl, pool,
2049 ZFS_TYPE_FILESYSTEM);
2051 (void) get_clones_cb(root, &gca);
2054 if (gca.numclones != 0 ||
2055 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2056 nvlist_add_nvlist(zhp->zfs_props,
2057 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2064 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2065 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2068 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2074 * Retrieve a property from the given object. If 'literal' is specified, then
2075 * numbers are left as exact values. Otherwise, numbers are converted to a
2076 * human-readable form.
2078 * Returns 0 on success, or -1 on error.
2081 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2082 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2084 char *source = NULL;
2088 boolean_t received = zfs_is_recvd_props_mode(zhp);
2091 * Check to see if this property applies to our object
2093 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2096 if (received && zfs_prop_readonly(prop))
2100 *src = ZPROP_SRC_NONE;
2103 case ZFS_PROP_CREATION:
2105 * 'creation' is a time_t stored in the statistics. We convert
2106 * this into a string unless 'literal' is specified.
2109 val = getprop_uint64(zhp, prop, &source);
2110 time_t time = (time_t)val;
2114 localtime_r(&time, &t) == NULL ||
2115 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2117 (void) snprintf(propbuf, proplen, "%llu", val);
2121 case ZFS_PROP_MOUNTPOINT:
2123 * Getting the precise mountpoint can be tricky.
2125 * - for 'none' or 'legacy', return those values.
2126 * - for inherited mountpoints, we want to take everything
2127 * after our ancestor and append it to the inherited value.
2129 * If the pool has an alternate root, we want to prepend that
2130 * root to any values we return.
2133 str = getprop_string(zhp, prop, &source);
2135 if (str[0] == '/') {
2136 char buf[MAXPATHLEN];
2138 const char *relpath;
2141 * If we inherit the mountpoint, even from a dataset
2142 * with a received value, the source will be the path of
2143 * the dataset we inherit from. If source is
2144 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2147 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2150 relpath = zhp->zfs_name + strlen(source);
2151 if (relpath[0] == '/')
2155 if ((zpool_get_prop(zhp->zpool_hdl,
2156 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2157 (strcmp(root, "-") == 0))
2160 * Special case an alternate root of '/'. This will
2161 * avoid having multiple leading slashes in the
2164 if (strcmp(root, "/") == 0)
2168 * If the mountpoint is '/' then skip over this
2169 * if we are obtaining either an alternate root or
2170 * an inherited mountpoint.
2172 if (str[1] == '\0' && (root[0] != '\0' ||
2173 relpath[0] != '\0'))
2176 if (relpath[0] == '\0')
2177 (void) snprintf(propbuf, proplen, "%s%s",
2180 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2181 root, str, relpath[0] == '@' ? "" : "/",
2184 /* 'legacy' or 'none' */
2185 (void) strlcpy(propbuf, str, proplen);
2190 case ZFS_PROP_ORIGIN:
2191 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2194 * If there is no parent at all, return failure to indicate that
2195 * it doesn't apply to this dataset.
2197 if (propbuf[0] == '\0')
2201 case ZFS_PROP_CLONES:
2202 if (get_clones_string(zhp, propbuf, proplen) != 0)
2206 case ZFS_PROP_QUOTA:
2207 case ZFS_PROP_REFQUOTA:
2208 case ZFS_PROP_RESERVATION:
2209 case ZFS_PROP_REFRESERVATION:
2211 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2215 * If quota or reservation is 0, we translate this into 'none'
2216 * (unless literal is set), and indicate that it's the default
2217 * value. Otherwise, we print the number nicely and indicate
2218 * that its set locally.
2222 (void) strlcpy(propbuf, "0", proplen);
2224 (void) strlcpy(propbuf, "none", proplen);
2227 (void) snprintf(propbuf, proplen, "%llu",
2230 zfs_nicenum(val, propbuf, proplen);
2234 case ZFS_PROP_REFRATIO:
2235 case ZFS_PROP_COMPRESSRATIO:
2236 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2238 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2239 (u_longlong_t)(val / 100),
2240 (u_longlong_t)(val % 100));
2244 switch (zhp->zfs_type) {
2245 case ZFS_TYPE_FILESYSTEM:
2248 case ZFS_TYPE_VOLUME:
2251 case ZFS_TYPE_SNAPSHOT:
2257 (void) snprintf(propbuf, proplen, "%s", str);
2260 case ZFS_PROP_MOUNTED:
2262 * The 'mounted' property is a pseudo-property that described
2263 * whether the filesystem is currently mounted. Even though
2264 * it's a boolean value, the typical values of "on" and "off"
2265 * don't make sense, so we translate to "yes" and "no".
2267 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2268 src, &source, &val) != 0)
2271 (void) strlcpy(propbuf, "yes", proplen);
2273 (void) strlcpy(propbuf, "no", proplen);
2278 * The 'name' property is a pseudo-property derived from the
2279 * dataset name. It is presented as a real property to simplify
2282 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2285 case ZFS_PROP_MLSLABEL:
2288 m_label_t *new_sl = NULL;
2289 char *ascii = NULL; /* human readable label */
2291 (void) strlcpy(propbuf,
2292 getprop_string(zhp, prop, &source), proplen);
2294 if (literal || (strcasecmp(propbuf,
2295 ZFS_MLSLABEL_DEFAULT) == 0))
2299 * Try to translate the internal hex string to
2300 * human-readable output. If there are any
2301 * problems just use the hex string.
2304 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2305 L_NO_CORRECTION, NULL) == -1) {
2306 m_label_free(new_sl);
2310 if (label_to_str(new_sl, &ascii, M_LABEL,
2314 m_label_free(new_sl);
2317 m_label_free(new_sl);
2319 (void) strlcpy(propbuf, ascii, proplen);
2329 * GUIDs are stored as numbers, but they are identifiers.
2330 * We don't want them to be pretty printed, because pretty
2331 * printing mangles the ID into a truncated and useless value.
2333 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2335 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2339 switch (zfs_prop_get_type(prop)) {
2340 case PROP_TYPE_NUMBER:
2341 if (get_numeric_property(zhp, prop, src,
2342 &source, &val) != 0)
2345 (void) snprintf(propbuf, proplen, "%llu",
2348 zfs_nicenum(val, propbuf, proplen);
2351 case PROP_TYPE_STRING:
2352 (void) strlcpy(propbuf,
2353 getprop_string(zhp, prop, &source), proplen);
2356 case PROP_TYPE_INDEX:
2357 if (get_numeric_property(zhp, prop, src,
2358 &source, &val) != 0)
2360 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2362 (void) strlcpy(propbuf, strval, proplen);
2370 get_source(zhp, src, source, statbuf, statlen);
2376 * Utility function to get the given numeric property. Does no validation that
2377 * the given property is the appropriate type; should only be used with
2378 * hard-coded property types.
2381 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2386 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2392 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2396 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2397 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2401 * Similar to zfs_prop_get(), but returns the value as an integer.
2404 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2405 zprop_source_t *src, char *statbuf, size_t statlen)
2410 * Check to see if this property applies to our object
2412 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2413 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2414 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2415 zfs_prop_to_name(prop)));
2419 *src = ZPROP_SRC_NONE;
2421 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2424 get_source(zhp, src, source, statbuf, statlen);
2430 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2431 char **domainp, idmap_rid_t *ridp)
2434 idmap_get_handle_t *get_hdl = NULL;
2438 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2442 err = idmap_get_sidbyuid(get_hdl, id,
2443 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2445 err = idmap_get_sidbygid(get_hdl, id,
2446 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2448 if (err == IDMAP_SUCCESS &&
2449 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2450 status == IDMAP_SUCCESS)
2456 idmap_get_destroy(get_hdl);
2459 assert(!"invalid code path");
2464 * convert the propname into parameters needed by kernel
2465 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2466 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2469 userquota_propname_decode(const char *propname, boolean_t zoned,
2470 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2472 zfs_userquota_prop_t type;
2474 char *numericsid = NULL;
2479 /* Figure out the property type ({user|group}{quota|space}) */
2480 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2481 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2482 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2485 if (type == ZFS_NUM_USERQUOTA_PROPS)
2489 isuser = (type == ZFS_PROP_USERQUOTA ||
2490 type == ZFS_PROP_USERUSED);
2492 cp = strchr(propname, '@') + 1;
2494 if (strchr(cp, '@')) {
2497 * It's a SID name (eg "user@domain") that needs to be
2498 * turned into S-1-domainID-RID.
2500 directory_error_t e;
2501 if (zoned && getzoneid() == GLOBAL_ZONEID)
2504 e = directory_sid_from_user_name(NULL,
2507 e = directory_sid_from_group_name(NULL,
2511 directory_error_free(e);
2514 if (numericsid == NULL)
2517 /* will be further decoded below */
2523 if (strncmp(cp, "S-1-", 4) == 0) {
2524 /* It's a numeric SID (eg "S-1-234-567-89") */
2525 (void) strlcpy(domain, cp, domainlen);
2526 cp = strrchr(domain, '-');
2531 *ridp = strtoull(cp, &end, 10);
2536 if (errno != 0 || *end != '\0')
2538 } else if (!isdigit(*cp)) {
2540 * It's a user/group name (eg "user") that needs to be
2541 * turned into a uid/gid
2543 if (zoned && getzoneid() == GLOBAL_ZONEID)
2559 /* It's a user/group ID (eg "12345"). */
2560 uid_t id = strtoul(cp, &end, 10);
2567 /* It's an ephemeral ID. */
2568 if (idmap_id_to_numeric_domain_rid(id, isuser,
2569 &mapdomain, &rid) != 0)
2571 (void) strlcpy(domain, mapdomain, domainlen);
2578 ASSERT3P(numericsid, ==, NULL);
2583 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2584 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2587 zfs_cmd_t zc = { 0 };
2589 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2591 err = userquota_propname_decode(propname,
2592 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2593 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2594 zc.zc_objset_type = *typep;
2598 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2602 *propvalue = zc.zc_cookie;
2607 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2608 uint64_t *propvalue)
2610 zfs_userquota_prop_t type;
2612 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2617 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2618 char *propbuf, int proplen, boolean_t literal)
2622 zfs_userquota_prop_t type;
2624 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2631 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2632 } else if (propvalue == 0 &&
2633 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2634 (void) strlcpy(propbuf, "none", proplen);
2636 zfs_nicenum(propvalue, propbuf, proplen);
2642 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2643 uint64_t *propvalue)
2646 zfs_cmd_t zc = { 0 };
2647 const char *snapname;
2649 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2651 snapname = strchr(propname, '@') + 1;
2652 if (strchr(snapname, '@')) {
2653 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2655 /* snapname is the short name, append it to zhp's fsname */
2658 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2659 sizeof (zc.zc_value));
2660 cp = strchr(zc.zc_value, '@');
2663 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2664 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2667 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2671 *propvalue = zc.zc_cookie;
2676 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2677 char *propbuf, int proplen, boolean_t literal)
2682 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2688 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2690 zfs_nicenum(propvalue, propbuf, proplen);
2696 zfs_get_snapused_int(zfs_handle_t *firstsnap, zfs_handle_t *lastsnap,
2700 zfs_cmd_t zc = { 0 };
2702 (void) strlcpy(zc.zc_name, lastsnap->zfs_name, sizeof (zc.zc_name));
2703 (void) strlcpy(zc.zc_value, firstsnap->zfs_name, sizeof (zc.zc_value));
2705 err = ioctl(lastsnap->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_SNAPS, &zc);
2709 *usedp = zc.zc_cookie;
2715 * Returns the name of the given zfs handle.
2718 zfs_get_name(const zfs_handle_t *zhp)
2720 return (zhp->zfs_name);
2724 * Returns the type of the given zfs handle.
2727 zfs_get_type(const zfs_handle_t *zhp)
2729 return (zhp->zfs_type);
2733 * Is one dataset name a child dataset of another?
2735 * Needs to handle these cases:
2736 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2737 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2738 * Descendant? No. No. No. Yes.
2741 is_descendant(const char *ds1, const char *ds2)
2743 size_t d1len = strlen(ds1);
2745 /* ds2 can't be a descendant if it's smaller */
2746 if (strlen(ds2) < d1len)
2749 /* otherwise, compare strings and verify that there's a '/' char */
2750 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2754 * Given a complete name, return just the portion that refers to the parent.
2755 * Will return -1 if there is no parent (path is just the name of the
2759 parent_name(const char *path, char *buf, size_t buflen)
2763 (void) strlcpy(buf, path, buflen);
2765 if ((slashp = strrchr(buf, '/')) == NULL)
2773 * If accept_ancestor is false, then check to make sure that the given path has
2774 * a parent, and that it exists. If accept_ancestor is true, then find the
2775 * closest existing ancestor for the given path. In prefixlen return the
2776 * length of already existing prefix of the given path. We also fetch the
2777 * 'zoned' property, which is used to validate property settings when creating
2781 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2782 boolean_t accept_ancestor, int *prefixlen)
2784 zfs_cmd_t zc = { 0 };
2785 char parent[ZFS_MAXNAMELEN];
2791 (void) snprintf(errbuf, sizeof (errbuf),
2792 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2794 /* get parent, and check to see if this is just a pool */
2795 if (parent_name(path, parent, sizeof (parent)) != 0) {
2796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2797 "missing dataset name"));
2798 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2801 /* check to see if the pool exists */
2802 if ((slash = strchr(parent, '/')) == NULL)
2803 slash = parent + strlen(parent);
2804 (void) strncpy(zc.zc_name, parent, slash - parent);
2805 zc.zc_name[slash - parent] = '\0';
2806 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2809 "no such pool '%s'"), zc.zc_name);
2810 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2813 /* check to see if the parent dataset exists */
2814 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2815 if (errno == ENOENT && accept_ancestor) {
2817 * Go deeper to find an ancestor, give up on top level.
2819 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2820 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2821 "no such pool '%s'"), zc.zc_name);
2822 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2824 } else if (errno == ENOENT) {
2825 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2826 "parent does not exist"));
2827 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2829 return (zfs_standard_error(hdl, errno, errbuf));
2832 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2836 /* we are in a non-global zone, but parent is in the global zone */
2837 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2838 (void) zfs_standard_error(hdl, EPERM, errbuf);
2843 /* make sure parent is a filesystem */
2844 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2846 "parent is not a filesystem"));
2847 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2853 if (prefixlen != NULL)
2854 *prefixlen = strlen(parent);
2859 * Finds whether the dataset of the given type(s) exists.
2862 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2866 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2870 * Try to get stats for the dataset, which will tell us if it exists.
2872 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2873 int ds_type = zhp->zfs_type;
2876 if (types & ds_type)
2883 * Given a path to 'target', create all the ancestors between
2884 * the prefixlen portion of the path, and the target itself.
2885 * Fail if the initial prefixlen-ancestor does not already exist.
2888 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2894 /* make sure prefix exists */
2895 cp = target + prefixlen;
2897 assert(strchr(cp, '/') == NULL);
2898 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2901 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2909 * Attempt to create, mount, and share any ancestor filesystems,
2910 * up to the prefixlen-long one.
2912 for (cp = target + prefixlen + 1;
2913 cp = strchr(cp, '/'); *cp = '/', cp++) {
2918 h = make_dataset_handle(hdl, target);
2920 /* it already exists, nothing to do here */
2925 logstr = hdl->libzfs_log_str;
2926 hdl->libzfs_log_str = NULL;
2927 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2929 hdl->libzfs_log_str = logstr;
2930 opname = dgettext(TEXT_DOMAIN, "create");
2934 hdl->libzfs_log_str = logstr;
2935 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2937 opname = dgettext(TEXT_DOMAIN, "open");
2941 if (zfs_mount(h, NULL, 0) != 0) {
2942 opname = dgettext(TEXT_DOMAIN, "mount");
2946 if (zfs_share(h) != 0) {
2947 opname = dgettext(TEXT_DOMAIN, "share");
2957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2958 "failed to %s ancestor '%s'"), opname, target);
2963 * Creates non-existing ancestors of the given path.
2966 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2972 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2975 if ((path_copy = strdup(path)) != NULL) {
2976 rc = create_parents(hdl, path_copy, prefix);
2979 if (path_copy == NULL || rc != 0)
2986 * Create a new filesystem or volume.
2989 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2992 zfs_cmd_t zc = { 0 };
2995 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2999 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3000 "cannot create '%s'"), path);
3002 /* validate the path, taking care to note the extended error message */
3003 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3004 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3006 /* validate parents exist */
3007 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3011 * The failure modes when creating a dataset of a different type over
3012 * one that already exists is a little strange. In particular, if you
3013 * try to create a dataset on top of an existing dataset, the ioctl()
3014 * will return ENOENT, not EEXIST. To prevent this from happening, we
3015 * first try to see if the dataset exists.
3017 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
3018 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3020 "dataset already exists"));
3021 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3024 if (type == ZFS_TYPE_VOLUME)
3025 zc.zc_objset_type = DMU_OST_ZVOL;
3027 zc.zc_objset_type = DMU_OST_ZFS;
3029 if (props && (props = zfs_valid_proplist(hdl, type, props,
3030 zoned, NULL, errbuf)) == 0)
3033 if (type == ZFS_TYPE_VOLUME) {
3035 * If we are creating a volume, the size and block size must
3036 * satisfy a few restraints. First, the blocksize must be a
3037 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3038 * volsize must be a multiple of the block size, and cannot be
3041 if (props == NULL || nvlist_lookup_uint64(props,
3042 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3044 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3045 "missing volume size"));
3046 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3049 if ((ret = nvlist_lookup_uint64(props,
3050 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3051 &blocksize)) != 0) {
3052 if (ret == ENOENT) {
3053 blocksize = zfs_prop_default_numeric(
3054 ZFS_PROP_VOLBLOCKSIZE);
3057 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3058 "missing volume block size"));
3059 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3065 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3066 "volume size cannot be zero"));
3067 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3070 if (size % blocksize != 0) {
3072 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3073 "volume size must be a multiple of volume block "
3075 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3079 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
3083 /* create the dataset */
3084 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3086 zcmd_free_nvlists(&zc);
3088 /* check for failure */
3090 char parent[ZFS_MAXNAMELEN];
3091 (void) parent_name(path, parent, sizeof (parent));
3095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3096 "no such parent '%s'"), parent);
3097 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3101 "parent '%s' is not a filesystem"), parent);
3102 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3106 "volume block size must be power of 2 from "
3108 (uint_t)SPA_MINBLOCKSIZE,
3109 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3111 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3115 "pool must be upgraded to set this "
3116 "property or value"));
3117 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3121 * This platform can't address a volume this big.
3123 if (type == ZFS_TYPE_VOLUME)
3124 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3129 return (zfs_standard_error(hdl, errno, errbuf));
3137 * Destroys the given dataset. The caller must make sure that the filesystem
3138 * isn't mounted, and that there are no active dependents.
3141 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3143 zfs_cmd_t zc = { 0 };
3145 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3147 if (ZFS_IS_VOLUME(zhp)) {
3148 zc.zc_objset_type = DMU_OST_ZVOL;
3150 zc.zc_objset_type = DMU_OST_ZFS;
3153 zc.zc_defer_destroy = defer;
3154 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3155 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3156 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3160 remove_mountpoint(zhp);
3165 struct destroydata {
3167 const char *snapname;
3171 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3173 struct destroydata *dd = arg;
3175 char name[ZFS_MAXNAMELEN];
3178 (void) snprintf(name, sizeof (name),
3179 "%s@%s", zhp->zfs_name, dd->snapname);
3181 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3183 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3187 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3193 * Destroys all snapshots with the given name in zhp & descendants.
3196 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3199 struct destroydata dd = { 0 };
3201 dd.snapname = snapname;
3202 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3203 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3205 if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3206 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3207 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3208 zhp->zfs_name, snapname);
3210 ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3212 nvlist_free(dd.nvl);
3217 * Destroys all the snapshots named in the nvlist. They must be underneath
3218 * the zhp (either snapshots of it, or snapshots of its descendants).
3221 zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3224 zfs_cmd_t zc = { 0 };
3226 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3227 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
3229 zc.zc_defer_destroy = defer;
3231 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
3235 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3236 "cannot destroy snapshots in %s"), zc.zc_name);
3240 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3241 "snapshot is cloned"));
3242 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3245 return (zfs_standard_error(zhp->zfs_hdl, errno,
3254 * Clones the given dataset. The target must be of the same type as the source.
3257 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3259 zfs_cmd_t zc = { 0 };
3260 char parent[ZFS_MAXNAMELEN];
3263 libzfs_handle_t *hdl = zhp->zfs_hdl;
3267 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3269 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3270 "cannot create '%s'"), target);
3272 /* validate the target/clone name */
3273 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3274 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3276 /* validate parents exist */
3277 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3280 (void) parent_name(target, parent, sizeof (parent));
3283 if (ZFS_IS_VOLUME(zhp)) {
3284 zc.zc_objset_type = DMU_OST_ZVOL;
3285 type = ZFS_TYPE_VOLUME;
3287 zc.zc_objset_type = DMU_OST_ZFS;
3288 type = ZFS_TYPE_FILESYSTEM;
3292 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3293 zhp, errbuf)) == NULL)
3296 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3304 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3305 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3306 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3308 zcmd_free_nvlists(&zc);
3315 * The parent doesn't exist. We should have caught this
3316 * above, but there may a race condition that has since
3317 * destroyed the parent.
3319 * At this point, we don't know whether it's the source
3320 * that doesn't exist anymore, or whether the target
3321 * dataset doesn't exist.
3323 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3324 "no such parent '%s'"), parent);
3325 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3328 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3329 "source and target pools differ"));
3330 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3334 return (zfs_standard_error(zhp->zfs_hdl, errno,
3343 * Promotes the given clone fs to be the clone parent.
3346 zfs_promote(zfs_handle_t *zhp)
3348 libzfs_handle_t *hdl = zhp->zfs_hdl;
3349 zfs_cmd_t zc = { 0 };
3350 char parent[MAXPATHLEN];
3354 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3355 "cannot promote '%s'"), zhp->zfs_name);
3357 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3358 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3359 "snapshots can not be promoted"));
3360 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3363 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3364 if (parent[0] == '\0') {
3365 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3366 "not a cloned filesystem"));
3367 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3370 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3371 sizeof (zc.zc_value));
3372 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3373 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3376 int save_errno = errno;
3378 switch (save_errno) {
3380 /* There is a conflicting snapshot name. */
3381 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3382 "conflicting snapshot '%s' from parent '%s'"),
3383 zc.zc_string, parent);
3384 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3387 return (zfs_standard_error(hdl, save_errno, errbuf));
3394 * Takes a snapshot of the given dataset.
3397 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3401 char parent[ZFS_MAXNAMELEN];
3403 zfs_cmd_t zc = { 0 };
3407 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3408 "cannot snapshot '%s'"), path);
3410 /* validate the target name */
3411 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3412 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3415 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3416 props, B_FALSE, NULL, errbuf)) == NULL)
3419 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3427 /* make sure the parent exists and is of the appropriate type */
3428 delim = strchr(path, '@');
3429 (void) strncpy(parent, path, delim - path);
3430 parent[delim - path] = '\0';
3432 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3433 ZFS_TYPE_VOLUME)) == NULL) {
3434 zcmd_free_nvlists(&zc);
3438 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3439 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3440 if (ZFS_IS_VOLUME(zhp))
3441 zc.zc_objset_type = DMU_OST_ZVOL;
3443 zc.zc_objset_type = DMU_OST_ZFS;
3444 zc.zc_cookie = recursive;
3445 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3447 zcmd_free_nvlists(&zc);
3450 * if it was recursive, the one that actually failed will be in
3454 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3455 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3456 (void) zfs_standard_error(hdl, errno, errbuf);
3465 * Destroy any more recent snapshots. We invoke this callback on any dependents
3466 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3467 * is a dependent and we should just destroy it without checking the transaction
3470 typedef struct rollback_data {
3471 const char *cb_target; /* the snapshot */
3472 uint64_t cb_create; /* creation time reference */
3474 boolean_t cb_dependent;
3479 rollback_destroy(zfs_handle_t *zhp, void *data)
3481 rollback_data_t *cbp = data;
3483 if (!cbp->cb_dependent) {
3484 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3485 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3486 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3490 cbp->cb_dependent = B_TRUE;
3491 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3492 rollback_destroy, cbp);
3493 cbp->cb_dependent = B_FALSE;
3495 logstr = zhp->zfs_hdl->libzfs_log_str;
3496 zhp->zfs_hdl->libzfs_log_str = NULL;
3497 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3498 zhp->zfs_hdl->libzfs_log_str = logstr;
3501 /* We must destroy this clone; first unmount it */
3502 prop_changelist_t *clp;
3504 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3505 cbp->cb_force ? MS_FORCE: 0);
3506 if (clp == NULL || changelist_prefix(clp) != 0) {
3507 cbp->cb_error = B_TRUE;
3511 if (zfs_destroy(zhp, B_FALSE) != 0)
3512 cbp->cb_error = B_TRUE;
3514 changelist_remove(clp, zhp->zfs_name);
3515 (void) changelist_postfix(clp);
3516 changelist_free(clp);
3524 * Given a dataset, rollback to a specific snapshot, discarding any
3525 * data changes since then and making it the active dataset.
3527 * Any snapshots more recent than the target are destroyed, along with
3531 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3533 rollback_data_t cb = { 0 };
3535 zfs_cmd_t zc = { 0 };
3536 boolean_t restore_resv = 0;
3537 uint64_t old_volsize, new_volsize;
3538 zfs_prop_t resv_prop;
3540 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3541 zhp->zfs_type == ZFS_TYPE_VOLUME);
3544 * Destroy all recent snapshots and its dependends.
3546 cb.cb_force = force;
3547 cb.cb_target = snap->zfs_name;
3548 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3549 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3555 * Now that we have verified that the snapshot is the latest,
3556 * rollback to the given snapshot.
3559 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3560 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3562 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3564 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3567 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3569 if (ZFS_IS_VOLUME(zhp))
3570 zc.zc_objset_type = DMU_OST_ZVOL;
3572 zc.zc_objset_type = DMU_OST_ZFS;
3575 * We rely on zfs_iter_children() to verify that there are no
3576 * newer snapshots for the given dataset. Therefore, we can
3577 * simply pass the name on to the ioctl() call. There is still
3578 * an unlikely race condition where the user has taken a
3579 * snapshot since we verified that this was the most recent.
3582 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3583 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3584 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3590 * For volumes, if the pre-rollback volsize matched the pre-
3591 * rollback reservation and the volsize has changed then set
3592 * the reservation property to the post-rollback volsize.
3593 * Make a new handle since the rollback closed the dataset.
3595 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3596 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3598 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3599 if (old_volsize != new_volsize)
3600 err = zfs_prop_set_int(zhp, resv_prop,
3609 * Renames the given dataset.
3612 zfs_rename(zfs_handle_t *zhp, const char *target, renameflags_t flags)
3615 zfs_cmd_t zc = { 0 };
3617 prop_changelist_t *cl = NULL;
3618 zfs_handle_t *zhrp = NULL;
3619 char *parentname = NULL;
3620 char parent[ZFS_MAXNAMELEN];
3621 char property[ZFS_MAXPROPLEN];
3622 libzfs_handle_t *hdl = zhp->zfs_hdl;
3625 /* if we have the same exact name, just return success */
3626 if (strcmp(zhp->zfs_name, target) == 0)
3629 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3630 "cannot rename to '%s'"), target);
3633 * Make sure the target name is valid
3635 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3636 if ((strchr(target, '@') == NULL) ||
3639 * Snapshot target name is abbreviated,
3640 * reconstruct full dataset name
3642 (void) strlcpy(parent, zhp->zfs_name,
3644 delim = strchr(parent, '@');
3645 if (strchr(target, '@') == NULL)
3649 (void) strlcat(parent, target, sizeof (parent));
3653 * Make sure we're renaming within the same dataset.
3655 delim = strchr(target, '@');
3656 if (strncmp(zhp->zfs_name, target, delim - target)
3657 != 0 || zhp->zfs_name[delim - target] != '@') {
3658 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3659 "snapshots must be part of same "
3661 return (zfs_error(hdl, EZFS_CROSSTARGET,
3665 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3666 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3668 if (flags.recurse) {
3669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3670 "recursive rename must be a snapshot"));
3671 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3674 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3675 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3677 /* validate parents */
3678 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3681 /* make sure we're in the same pool */
3682 verify((delim = strchr(target, '/')) != NULL);
3683 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3684 zhp->zfs_name[delim - target] != '/') {
3685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3686 "datasets must be within same pool"));
3687 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3690 /* new name cannot be a child of the current dataset name */
3691 if (is_descendant(zhp->zfs_name, target)) {
3692 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3693 "New dataset name cannot be a descendant of "
3694 "current dataset name"));
3695 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3699 (void) snprintf(errbuf, sizeof (errbuf),
3700 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3702 if (getzoneid() == GLOBAL_ZONEID &&
3703 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3705 "dataset is used in a non-global zone"));
3706 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3710 * Avoid unmounting file systems with mountpoint property set to
3711 * 'legacy' or 'none' even if -u option is not given.
3713 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3714 !flags.recurse && !flags.nounmount &&
3715 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
3716 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
3717 (strcmp(property, "legacy") == 0 ||
3718 strcmp(property, "none") == 0)) {
3719 flags.nounmount = B_TRUE;
3722 if (flags.recurse) {
3724 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3725 if (parentname == NULL) {
3729 delim = strchr(parentname, '@');
3731 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3738 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
3739 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
3740 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
3744 if (changelist_haszonedchild(cl)) {
3745 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3746 "child dataset with inherited mountpoint is used "
3747 "in a non-global zone"));
3748 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3752 if ((ret = changelist_prefix(cl)) != 0)
3756 if (ZFS_IS_VOLUME(zhp))
3757 zc.zc_objset_type = DMU_OST_ZVOL;
3759 zc.zc_objset_type = DMU_OST_ZFS;
3761 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3762 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3764 zc.zc_cookie = flags.recurse ? 1 : 0;
3765 if (flags.nounmount)
3768 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3770 * if it was recursive, the one that actually failed will
3773 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3774 "cannot rename '%s'"), zc.zc_name);
3776 if (flags.recurse && errno == EEXIST) {
3777 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3778 "a child dataset already has a snapshot "
3779 "with the new name"));
3780 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3782 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3786 * On failure, we still want to remount any filesystems that
3787 * were previously mounted, so we don't alter the system state.
3790 (void) changelist_postfix(cl);
3792 if (!flags.recurse) {
3793 changelist_rename(cl, zfs_get_name(zhp), target);
3794 ret = changelist_postfix(cl);
3806 changelist_free(cl);
3812 zfs_get_user_props(zfs_handle_t *zhp)
3814 return (zhp->zfs_user_props);
3818 zfs_get_recvd_props(zfs_handle_t *zhp)
3820 if (zhp->zfs_recvd_props == NULL)
3821 if (get_recvd_props_ioctl(zhp) != 0)
3823 return (zhp->zfs_recvd_props);
3827 * This function is used by 'zfs list' to determine the exact set of columns to
3828 * display, and their maximum widths. This does two main things:
3830 * - If this is a list of all properties, then expand the list to include
3831 * all native properties, and set a flag so that for each dataset we look
3832 * for new unique user properties and add them to the list.
3834 * - For non fixed-width properties, keep track of the maximum width seen
3835 * so that we can size the column appropriately. If the user has
3836 * requested received property values, we also need to compute the width
3837 * of the RECEIVED column.
3840 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3842 libzfs_handle_t *hdl = zhp->zfs_hdl;
3843 zprop_list_t *entry;
3844 zprop_list_t **last, **start;
3845 nvlist_t *userprops, *propval;
3848 char buf[ZFS_MAXPROPLEN];
3850 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3853 userprops = zfs_get_user_props(zhp);
3856 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3858 * Go through and add any user properties as necessary. We
3859 * start by incrementing our list pointer to the first
3860 * non-native property.
3863 while (*start != NULL) {
3864 if ((*start)->pl_prop == ZPROP_INVAL)
3866 start = &(*start)->pl_next;
3870 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3872 * See if we've already found this property in our list.
3874 for (last = start; *last != NULL;
3875 last = &(*last)->pl_next) {
3876 if (strcmp((*last)->pl_user_prop,
3877 nvpair_name(elem)) == 0)
3881 if (*last == NULL) {
3882 if ((entry = zfs_alloc(hdl,
3883 sizeof (zprop_list_t))) == NULL ||
3884 ((entry->pl_user_prop = zfs_strdup(hdl,
3885 nvpair_name(elem)))) == NULL) {
3890 entry->pl_prop = ZPROP_INVAL;
3891 entry->pl_width = strlen(nvpair_name(elem));
3892 entry->pl_all = B_TRUE;
3899 * Now go through and check the width of any non-fixed columns
3901 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3902 if (entry->pl_fixed)
3905 if (entry->pl_prop != ZPROP_INVAL) {
3906 if (zfs_prop_get(zhp, entry->pl_prop,
3907 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3908 if (strlen(buf) > entry->pl_width)
3909 entry->pl_width = strlen(buf);
3911 if (received && zfs_prop_get_recvd(zhp,
3912 zfs_prop_to_name(entry->pl_prop),
3913 buf, sizeof (buf), B_FALSE) == 0)
3914 if (strlen(buf) > entry->pl_recvd_width)
3915 entry->pl_recvd_width = strlen(buf);
3917 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3919 verify(nvlist_lookup_string(propval,
3920 ZPROP_VALUE, &strval) == 0);
3921 if (strlen(strval) > entry->pl_width)
3922 entry->pl_width = strlen(strval);
3924 if (received && zfs_prop_get_recvd(zhp,
3925 entry->pl_user_prop,
3926 buf, sizeof (buf), B_FALSE) == 0)
3927 if (strlen(buf) > entry->pl_recvd_width)
3928 entry->pl_recvd_width = strlen(buf);
3936 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3937 char *resource, void *export, void *sharetab,
3938 int sharemax, zfs_share_op_t operation)
3940 zfs_cmd_t zc = { 0 };
3943 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3944 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3946 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3947 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3948 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3949 zc.zc_share.z_sharetype = operation;
3950 zc.zc_share.z_sharemax = sharemax;
3951 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3956 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3961 * Keep a reference to the props-table against which we prune the
3964 zhp->zfs_props_table = props;
3966 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3969 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3970 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3973 * User properties will result in ZPROP_INVAL, and since we
3974 * only know how to prune standard ZFS properties, we always
3975 * leave these in the list. This can also happen if we
3976 * encounter an unknown DSL property (when running older
3977 * software, for example).
3979 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3980 (void) nvlist_remove(zhp->zfs_props,
3981 nvpair_name(curr), nvpair_type(curr));
3988 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3989 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3991 zfs_cmd_t zc = { 0 };
3992 nvlist_t *nvlist = NULL;
3995 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3996 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3997 zc.zc_cookie = (uint64_t)cmd;
3999 if (cmd == ZFS_SMB_ACL_RENAME) {
4000 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4001 (void) no_memory(hdl);
4007 case ZFS_SMB_ACL_ADD:
4008 case ZFS_SMB_ACL_REMOVE:
4009 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4011 case ZFS_SMB_ACL_RENAME:
4012 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4014 (void) no_memory(hdl);
4017 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4019 (void) no_memory(hdl);
4022 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4023 nvlist_free(nvlist);
4027 case ZFS_SMB_ACL_PURGE:
4032 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4034 nvlist_free(nvlist);
4039 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4040 char *path, char *resource)
4042 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4047 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4048 char *path, char *resource)
4050 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4055 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4057 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4062 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4063 char *oldname, char *newname)
4065 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4071 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4072 zfs_userspace_cb_t func, void *arg)
4074 zfs_cmd_t zc = { 0 };
4076 zfs_useracct_t buf[100];
4078 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4080 zc.zc_objset_type = type;
4081 zc.zc_nvlist_dst = (uintptr_t)buf;
4085 zfs_useracct_t *zua = buf;
4087 zc.zc_nvlist_dst_size = sizeof (buf);
4088 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4089 ZFS_IOC_USERSPACE_MANY, &zc);
4090 if (error || zc.zc_nvlist_dst_size == 0)
4093 while (zc.zc_nvlist_dst_size > 0) {
4094 error = func(arg, zua->zu_domain, zua->zu_rid,
4099 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4107 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4108 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4109 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4111 zfs_cmd_t zc = { 0 };
4112 libzfs_handle_t *hdl = zhp->zfs_hdl;
4114 ASSERT(!recursive || dsobj == 0);
4116 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4117 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4118 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4119 >= sizeof (zc.zc_string))
4120 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4121 zc.zc_cookie = recursive;
4122 zc.zc_temphold = temphold;
4123 zc.zc_cleanup_fd = cleanup_fd;
4124 zc.zc_sendobj = dsobj;
4125 zc.zc_createtxg = createtxg;
4127 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4128 char errbuf[ZFS_MAXNAMELEN+32];
4131 * if it was recursive, the one that actually failed will be in
4134 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4135 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4139 * Temporary tags wind up having the ds object id
4140 * prepended. So even if we passed the length check
4141 * above, it's still possible for the tag to wind
4142 * up being slightly too long.
4144 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4147 "pool must be upgraded"));
4148 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4150 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4152 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4158 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4166 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4167 boolean_t recursive)
4169 zfs_cmd_t zc = { 0 };
4170 libzfs_handle_t *hdl = zhp->zfs_hdl;
4172 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4173 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4174 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4175 >= sizeof (zc.zc_string))
4176 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4177 zc.zc_cookie = recursive;
4179 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4180 char errbuf[ZFS_MAXNAMELEN+32];
4183 * if it was recursive, the one that actually failed will be in
4186 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4187 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4191 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4194 "pool must be upgraded"));
4195 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4197 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4199 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4207 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4209 zfs_cmd_t zc = { 0 };
4210 libzfs_handle_t *hdl = zhp->zfs_hdl;
4214 char errbuf[ZFS_MAXNAMELEN+32];
4216 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4217 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4221 nvbuf = malloc(nvsz);
4222 if (nvbuf == NULL) {
4223 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4227 zc.zc_nvlist_dst_size = nvsz;
4228 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4230 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4232 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4233 (void) snprintf(errbuf, sizeof (errbuf),
4234 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4239 nvsz = zc.zc_nvlist_dst_size;
4243 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4244 "pool must be upgraded"));
4245 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4248 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4251 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4254 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4259 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4261 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4262 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4264 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4274 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4276 zfs_cmd_t zc = { 0 };
4277 libzfs_handle_t *hdl = zhp->zfs_hdl;
4279 char errbuf[ZFS_MAXNAMELEN+32];
4283 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4284 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4286 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4289 nvbuf = malloc(nvsz);
4291 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4294 zc.zc_nvlist_src_size = nvsz;
4295 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4296 zc.zc_perm_action = un;
4298 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4300 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4301 (void) snprintf(errbuf, sizeof (errbuf),
4302 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4306 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4307 "pool must be upgraded"));
4308 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4311 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4314 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4317 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4328 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4330 zfs_cmd_t zc = { 0 };
4331 libzfs_handle_t *hdl = zhp->zfs_hdl;
4335 char errbuf[ZFS_MAXNAMELEN+32];
4337 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4341 nvbuf = malloc(nvsz);
4342 if (nvbuf == NULL) {
4343 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4347 zc.zc_nvlist_dst_size = nvsz;
4348 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4350 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4352 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4353 (void) snprintf(errbuf, sizeof (errbuf),
4354 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4359 nvsz = zc.zc_nvlist_dst_size;
4363 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4364 "pool must be upgraded"));
4365 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4368 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4371 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4374 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4379 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4381 (void) snprintf(errbuf, sizeof (errbuf),
4382 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4384 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4394 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4397 uint64_t nblocks, volblocksize;
4401 if (nvlist_lookup_string(props,
4402 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4403 ncopies = atoi(strval);
4406 if (nvlist_lookup_uint64(props,
4407 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4408 &volblocksize) != 0)
4409 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4410 nblocks = volsize/volblocksize;
4411 /* start with metadnode L0-L6 */
4413 /* calculate number of indirects */
4414 while (nblocks > 1) {
4415 nblocks += DNODES_PER_LEVEL - 1;
4416 nblocks /= DNODES_PER_LEVEL;
4419 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4422 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4423 * compressed, but in practice they compress down to about
4426 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4432 * Attach/detach the given filesystem to/from the given jail.
4435 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4437 libzfs_handle_t *hdl = zhp->zfs_hdl;
4438 zfs_cmd_t zc = { 0 };
4444 (void) snprintf(errbuf, sizeof (errbuf),
4445 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4447 (void) snprintf(errbuf, sizeof (errbuf),
4448 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4451 switch (zhp->zfs_type) {
4452 case ZFS_TYPE_VOLUME:
4453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4454 "volumes can not be jailed"));
4455 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4456 case ZFS_TYPE_SNAPSHOT:
4457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4458 "snapshots can not be jailed"));
4459 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4461 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4463 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4464 zc.zc_objset_type = DMU_OST_ZFS;
4465 zc.zc_jailid = jailid;
4467 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4468 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4469 zfs_standard_error(hdl, errno, errbuf);