4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2014 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.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
45 #include <sys/mntent.h>
46 #include <sys/mount.h>
53 #include <sys/dnode.h>
59 #include "zfs_namecheck.h"
61 #include "libzfs_impl.h"
62 #include "zfs_deleg.h"
64 static int userquota_propname_decode(const char *propname, boolean_t zoned,
65 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
68 * Given a single type (not a mask of types), return the type in a human
72 zfs_type_to_name(zfs_type_t type)
75 case ZFS_TYPE_FILESYSTEM:
76 return (dgettext(TEXT_DOMAIN, "filesystem"));
77 case ZFS_TYPE_SNAPSHOT:
78 return (dgettext(TEXT_DOMAIN, "snapshot"));
80 return (dgettext(TEXT_DOMAIN, "volume"));
87 * Given a path and mask of ZFS types, return a string describing this dataset.
88 * This is used when we fail to open a dataset and we cannot get an exact type.
89 * We guess what the type would have been based on the path and the mask of
93 path_to_str(const char *path, int types)
96 * When given a single type, always report the exact type.
98 if (types == ZFS_TYPE_SNAPSHOT)
99 return (dgettext(TEXT_DOMAIN, "snapshot"));
100 if (types == ZFS_TYPE_FILESYSTEM)
101 return (dgettext(TEXT_DOMAIN, "filesystem"));
102 if (types == ZFS_TYPE_VOLUME)
103 return (dgettext(TEXT_DOMAIN, "volume"));
106 * The user is requesting more than one type of dataset. If this is the
107 * case, consult the path itself. If we're looking for a snapshot, and
108 * a '@' is found, then report it as "snapshot". Otherwise, remove the
109 * snapshot attribute and try again.
111 if (types & ZFS_TYPE_SNAPSHOT) {
112 if (strchr(path, '@') != NULL)
113 return (dgettext(TEXT_DOMAIN, "snapshot"));
114 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
118 * The user has requested either filesystems or volumes.
119 * We have no way of knowing a priori what type this would be, so always
120 * report it as "filesystem" or "volume", our two primitive types.
122 if (types & ZFS_TYPE_FILESYSTEM)
123 return (dgettext(TEXT_DOMAIN, "filesystem"));
125 assert(types & ZFS_TYPE_VOLUME);
126 return (dgettext(TEXT_DOMAIN, "volume"));
130 * Validate a ZFS path. This is used even before trying to open the dataset, to
131 * provide a more meaningful error message. We call zfs_error_aux() to
132 * explain exactly why the name was not valid.
135 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
141 (void) zfs_prop_get_table();
142 if (dataset_namecheck(path, &why, &what) != 0) {
145 case NAME_ERR_TOOLONG:
146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
147 "name is too long"));
150 case NAME_ERR_LEADING_SLASH:
151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152 "leading slash in name"));
155 case NAME_ERR_EMPTY_COMPONENT:
156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
157 "empty component in name"));
160 case NAME_ERR_TRAILING_SLASH:
161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
162 "trailing slash in name"));
165 case NAME_ERR_INVALCHAR:
167 dgettext(TEXT_DOMAIN, "invalid character "
168 "'%c' in name"), what);
171 case NAME_ERR_MULTIPLE_AT:
172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173 "multiple '@' delimiters in name"));
176 case NAME_ERR_NOLETTER:
177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178 "pool doesn't begin with a letter"));
181 case NAME_ERR_RESERVED:
182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
183 "name is reserved"));
186 case NAME_ERR_DISKLIKE:
187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
188 "reserved disk name"));
196 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
199 "snapshot delimiter '@' in filesystem name"));
203 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
205 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
206 "missing '@' delimiter in snapshot name"));
210 if (modifying && strchr(path, '%') != NULL) {
212 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
213 "invalid character %c in name"), '%');
221 zfs_name_valid(const char *name, zfs_type_t type)
223 if (type == ZFS_TYPE_POOL)
224 return (zpool_name_valid(NULL, B_FALSE, name));
225 return (zfs_validate_name(NULL, name, type, B_FALSE));
229 * This function takes the raw DSL properties, and filters out the user-defined
230 * properties into a separate nvlist.
233 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
235 libzfs_handle_t *hdl = zhp->zfs_hdl;
240 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
241 (void) no_memory(hdl);
246 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
247 if (!zfs_prop_user(nvpair_name(elem)))
250 verify(nvpair_value_nvlist(elem, &propval) == 0);
251 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
253 (void) no_memory(hdl);
261 static zpool_handle_t *
262 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
264 libzfs_handle_t *hdl = zhp->zfs_hdl;
267 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
268 if (hdl->libzfs_pool_handles != NULL)
269 zph->zpool_next = hdl->libzfs_pool_handles;
270 hdl->libzfs_pool_handles = zph;
275 static zpool_handle_t *
276 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
278 libzfs_handle_t *hdl = zhp->zfs_hdl;
279 zpool_handle_t *zph = hdl->libzfs_pool_handles;
281 while ((zph != NULL) &&
282 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
283 zph = zph->zpool_next;
288 * Returns a handle to the pool that contains the provided dataset.
289 * If a handle to that pool already exists then that handle is returned.
290 * Otherwise, a new handle is created and added to the list of handles.
292 static zpool_handle_t *
293 zpool_handle(zfs_handle_t *zhp)
299 len = strcspn(zhp->zfs_name, "/@#") + 1;
300 pool_name = zfs_alloc(zhp->zfs_hdl, len);
301 (void) strlcpy(pool_name, zhp->zfs_name, len);
303 zph = zpool_find_handle(zhp, pool_name, len);
305 zph = zpool_add_handle(zhp, pool_name);
312 zpool_free_handles(libzfs_handle_t *hdl)
314 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
316 while (zph != NULL) {
317 next = zph->zpool_next;
321 hdl->libzfs_pool_handles = NULL;
325 * Utility function to gather stats (objset and zpl) for the given object.
328 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
330 libzfs_handle_t *hdl = zhp->zfs_hdl;
332 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
334 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
335 if (errno == ENOMEM) {
336 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
347 * Utility function to get the received properties of the given object.
350 get_recvd_props_ioctl(zfs_handle_t *zhp)
352 libzfs_handle_t *hdl = zhp->zfs_hdl;
353 nvlist_t *recvdprops;
354 zfs_cmd_t zc = { 0 };
357 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
360 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
362 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
363 if (errno == ENOMEM) {
364 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
368 zcmd_free_nvlists(&zc);
373 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
374 zcmd_free_nvlists(&zc);
378 nvlist_free(zhp->zfs_recvd_props);
379 zhp->zfs_recvd_props = recvdprops;
385 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
387 nvlist_t *allprops, *userprops;
389 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
391 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
396 * XXX Why do we store the user props separately, in addition to
397 * storing them in zfs_props?
399 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
400 nvlist_free(allprops);
404 nvlist_free(zhp->zfs_props);
405 nvlist_free(zhp->zfs_user_props);
407 zhp->zfs_props = allprops;
408 zhp->zfs_user_props = userprops;
414 get_stats(zfs_handle_t *zhp)
417 zfs_cmd_t zc = { 0 };
419 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
421 if (get_stats_ioctl(zhp, &zc) != 0)
423 else if (put_stats_zhdl(zhp, &zc) != 0)
425 zcmd_free_nvlists(&zc);
430 * Refresh the properties currently stored in the handle.
433 zfs_refresh_properties(zfs_handle_t *zhp)
435 (void) get_stats(zhp);
439 * Makes a handle from the given dataset name. Used by zfs_open() and
440 * zfs_iter_* to create child handles on the fly.
443 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
445 if (put_stats_zhdl(zhp, zc) != 0)
449 * We've managed to open the dataset and gather statistics. Determine
450 * the high-level type.
452 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
453 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
454 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
455 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
459 if (zhp->zfs_dmustats.dds_is_snapshot)
460 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
461 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
462 zhp->zfs_type = ZFS_TYPE_VOLUME;
463 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
464 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
466 abort(); /* we should never see any other types */
468 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
475 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
477 zfs_cmd_t zc = { 0 };
479 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
485 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
486 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
490 if (get_stats_ioctl(zhp, &zc) == -1) {
491 zcmd_free_nvlists(&zc);
495 if (make_dataset_handle_common(zhp, &zc) == -1) {
499 zcmd_free_nvlists(&zc);
504 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
506 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
512 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
513 if (make_dataset_handle_common(zhp, zc) == -1) {
521 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
523 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
528 zhp->zfs_hdl = pzhp->zfs_hdl;
529 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
530 zhp->zfs_head_type = pzhp->zfs_type;
531 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
532 zhp->zpool_hdl = zpool_handle(zhp);
537 zfs_handle_dup(zfs_handle_t *zhp_orig)
539 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
544 zhp->zfs_hdl = zhp_orig->zfs_hdl;
545 zhp->zpool_hdl = zhp_orig->zpool_hdl;
546 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
547 sizeof (zhp->zfs_name));
548 zhp->zfs_type = zhp_orig->zfs_type;
549 zhp->zfs_head_type = zhp_orig->zfs_head_type;
550 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
551 if (zhp_orig->zfs_props != NULL) {
552 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
553 (void) no_memory(zhp->zfs_hdl);
558 if (zhp_orig->zfs_user_props != NULL) {
559 if (nvlist_dup(zhp_orig->zfs_user_props,
560 &zhp->zfs_user_props, 0) != 0) {
561 (void) no_memory(zhp->zfs_hdl);
566 if (zhp_orig->zfs_recvd_props != NULL) {
567 if (nvlist_dup(zhp_orig->zfs_recvd_props,
568 &zhp->zfs_recvd_props, 0)) {
569 (void) no_memory(zhp->zfs_hdl);
574 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
575 if (zhp_orig->zfs_mntopts != NULL) {
576 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
577 zhp_orig->zfs_mntopts);
579 zhp->zfs_props_table = zhp_orig->zfs_props_table;
584 zfs_bookmark_exists(const char *path)
588 char fsname[ZFS_MAXNAMELEN];
595 (void) strlcpy(fsname, path, sizeof (fsname));
596 pound = strchr(fsname, '#');
601 bmark_name = pound + 1;
602 props = fnvlist_alloc();
603 err = lzc_get_bookmarks(fsname, props, &bmarks);
610 rv = nvlist_exists(bmarks, bmark_name);
616 make_bookmark_handle(zfs_handle_t *parent, const char *path,
617 nvlist_t *bmark_props)
619 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
624 /* Fill in the name. */
625 zhp->zfs_hdl = parent->zfs_hdl;
626 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
628 /* Set the property lists. */
629 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
635 zhp->zfs_head_type = parent->zfs_head_type;
636 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
638 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
639 nvlist_free(zhp->zfs_props);
648 * Opens the given snapshot, filesystem, or volume. The 'types'
649 * argument is a mask of acceptable types. The function will print an
650 * appropriate error message and return NULL if it can't be opened.
653 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
658 (void) snprintf(errbuf, sizeof (errbuf),
659 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
662 * Validate the name before we even try to open it.
664 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
666 "invalid dataset name"));
667 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
672 * Try to get stats for the dataset, which will tell us if it exists.
675 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
676 (void) zfs_standard_error(hdl, errno, errbuf);
681 char *at = strchr(path, '@');
686 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
687 (void) zfs_standard_error(hdl, errno, errbuf);
692 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
693 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
696 if (!(types & zhp->zfs_type)) {
697 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
706 * Release a ZFS handle. Nothing to do but free the associated memory.
709 zfs_close(zfs_handle_t *zhp)
711 if (zhp->zfs_mntopts)
712 free(zhp->zfs_mntopts);
713 nvlist_free(zhp->zfs_props);
714 nvlist_free(zhp->zfs_user_props);
715 nvlist_free(zhp->zfs_recvd_props);
719 typedef struct mnttab_node {
720 struct mnttab mtn_mt;
725 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
727 const mnttab_node_t *mtn1 = arg1;
728 const mnttab_node_t *mtn2 = arg2;
731 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
735 return (rv > 0 ? 1 : -1);
739 libzfs_mnttab_init(libzfs_handle_t *hdl)
741 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
742 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
743 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
747 libzfs_mnttab_update(libzfs_handle_t *hdl)
751 rewind(hdl->libzfs_mnttab);
752 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
755 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
757 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
758 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
759 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
760 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
761 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
762 avl_add(&hdl->libzfs_mnttab_cache, mtn);
767 libzfs_mnttab_fini(libzfs_handle_t *hdl)
772 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
773 free(mtn->mtn_mt.mnt_special);
774 free(mtn->mtn_mt.mnt_mountp);
775 free(mtn->mtn_mt.mnt_fstype);
776 free(mtn->mtn_mt.mnt_mntopts);
779 avl_destroy(&hdl->libzfs_mnttab_cache);
783 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
785 hdl->libzfs_mnttab_enable = enable;
789 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
790 struct mnttab *entry)
795 if (!hdl->libzfs_mnttab_enable) {
796 struct mnttab srch = { 0 };
798 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
799 libzfs_mnttab_fini(hdl);
800 rewind(hdl->libzfs_mnttab);
801 srch.mnt_special = (char *)fsname;
802 srch.mnt_fstype = MNTTYPE_ZFS;
803 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
809 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
810 libzfs_mnttab_update(hdl);
812 find.mtn_mt.mnt_special = (char *)fsname;
813 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
815 *entry = mtn->mtn_mt;
822 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
823 const char *mountp, const char *mntopts)
827 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
829 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
830 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
831 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
832 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
833 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
834 avl_add(&hdl->libzfs_mnttab_cache, mtn);
838 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
843 find.mtn_mt.mnt_special = (char *)fsname;
844 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
845 avl_remove(&hdl->libzfs_mnttab_cache, ret);
846 free(ret->mtn_mt.mnt_special);
847 free(ret->mtn_mt.mnt_mountp);
848 free(ret->mtn_mt.mnt_fstype);
849 free(ret->mtn_mt.mnt_mntopts);
855 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
857 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
859 if (zpool_handle == NULL)
862 *spa_version = zpool_get_prop_int(zpool_handle,
863 ZPOOL_PROP_VERSION, NULL);
868 * The choice of reservation property depends on the SPA version.
871 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
875 if (zfs_spa_version(zhp, &spa_version) < 0)
878 if (spa_version >= SPA_VERSION_REFRESERVATION)
879 *resv_prop = ZFS_PROP_REFRESERVATION;
881 *resv_prop = ZFS_PROP_RESERVATION;
887 * Given an nvlist of properties to set, validates that they are correct, and
888 * parses any numeric properties (index, boolean, etc) if they are specified as
892 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
893 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
900 int chosen_normal = -1;
903 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
904 (void) no_memory(hdl);
909 * Make sure this property is valid and applies to this type.
913 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
914 const char *propname = nvpair_name(elem);
916 prop = zfs_name_to_prop(propname);
917 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
919 * This is a user property: make sure it's a
920 * string, and that it's less than ZAP_MAXNAMELEN.
922 if (nvpair_type(elem) != DATA_TYPE_STRING) {
923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
924 "'%s' must be a string"), propname);
925 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
929 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
930 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
931 "property name '%s' is too long"),
933 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
937 (void) nvpair_value_string(elem, &strval);
938 if (nvlist_add_string(ret, propname, strval) != 0) {
939 (void) no_memory(hdl);
946 * Currently, only user properties can be modified on
949 if (type == ZFS_TYPE_SNAPSHOT) {
950 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
951 "this property can not be modified for snapshots"));
952 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
956 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
957 zfs_userquota_prop_t uqtype;
958 char newpropname[128];
963 if (userquota_propname_decode(propname, zoned,
964 &uqtype, domain, sizeof (domain), &rid) != 0) {
966 dgettext(TEXT_DOMAIN,
967 "'%s' has an invalid user/group name"),
969 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
973 if (uqtype != ZFS_PROP_USERQUOTA &&
974 uqtype != ZFS_PROP_GROUPQUOTA) {
976 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
978 (void) zfs_error(hdl, EZFS_PROPREADONLY,
983 if (nvpair_type(elem) == DATA_TYPE_STRING) {
984 (void) nvpair_value_string(elem, &strval);
985 if (strcmp(strval, "none") == 0) {
987 } else if (zfs_nicestrtonum(hdl,
988 strval, &intval) != 0) {
989 (void) zfs_error(hdl,
990 EZFS_BADPROP, errbuf);
993 } else if (nvpair_type(elem) ==
995 (void) nvpair_value_uint64(elem, &intval);
997 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998 "use 'none' to disable "
999 "userquota/groupquota"));
1003 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1004 "'%s' must be a number"), propname);
1005 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1010 * Encode the prop name as
1011 * userquota@<hex-rid>-domain, to make it easy
1012 * for the kernel to decode.
1014 (void) snprintf(newpropname, sizeof (newpropname),
1015 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1016 (longlong_t)rid, domain);
1020 if (nvlist_add_uint64_array(ret, newpropname,
1022 (void) no_memory(hdl);
1026 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1028 "'%s' is readonly"),
1030 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1034 if (prop == ZPROP_INVAL) {
1035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1036 "invalid property '%s'"), propname);
1037 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1041 if (!zfs_prop_valid_for_type(prop, type)) {
1043 dgettext(TEXT_DOMAIN, "'%s' does not "
1044 "apply to datasets of this type"), propname);
1045 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1049 if (zfs_prop_readonly(prop) &&
1050 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1052 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1054 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1058 if (zprop_parse_value(hdl, elem, prop, type, ret,
1059 &strval, &intval, errbuf) != 0)
1063 * Perform some additional checks for specific properties.
1066 case ZFS_PROP_VERSION:
1072 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1073 if (intval < version) {
1074 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1075 "Can not downgrade; already at version %u"),
1077 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1083 case ZFS_PROP_VOLBLOCKSIZE:
1084 case ZFS_PROP_RECORDSIZE:
1086 int maxbs = SPA_MAXBLOCKSIZE;
1088 maxbs = zpool_get_prop_int(zhp->zpool_hdl,
1089 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1092 * Volumes are limited to a volblocksize of 128KB,
1093 * because they typically service workloads with
1094 * small random writes, which incur a large performance
1095 * penalty with large blocks.
1097 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1098 maxbs = SPA_OLD_MAXBLOCKSIZE;
1100 * The value must be a power of two between
1101 * SPA_MINBLOCKSIZE and maxbs.
1103 if (intval < SPA_MINBLOCKSIZE ||
1104 intval > maxbs || !ISP2(intval)) {
1105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1106 "'%s' must be power of 2 from 512B "
1107 "to %uKB"), propname, maxbs >> 10);
1108 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1113 case ZFS_PROP_MLSLABEL:
1117 * Verify the mlslabel string and convert to
1118 * internal hex label string.
1122 char *hex = NULL; /* internal label string */
1124 /* Default value is already OK. */
1125 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1128 /* Verify the label can be converted to binary form */
1129 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1130 (str_to_label(strval, &new_sl, MAC_LABEL,
1131 L_NO_CORRECTION, NULL) == -1)) {
1135 /* Now translate to hex internal label string */
1136 if (label_to_str(new_sl, &hex, M_INTERNAL,
1142 m_label_free(new_sl);
1144 /* If string is already in internal form, we're done. */
1145 if (strcmp(strval, hex) == 0) {
1150 /* Replace the label string with the internal form. */
1151 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1153 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1161 "invalid mlslabel '%s'"), strval);
1162 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1163 m_label_free(new_sl); /* OK if null */
1165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1166 "mlslabel is not supported on FreeBSD"));
1167 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1173 case ZFS_PROP_MOUNTPOINT:
1175 namecheck_err_t why;
1177 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1178 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1181 if (mountpoint_namecheck(strval, &why)) {
1183 case NAME_ERR_LEADING_SLASH:
1185 dgettext(TEXT_DOMAIN,
1186 "'%s' must be an absolute path, "
1187 "'none', or 'legacy'"), propname);
1189 case NAME_ERR_TOOLONG:
1191 dgettext(TEXT_DOMAIN,
1192 "component of '%s' is too long"),
1196 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1203 case ZFS_PROP_SHARESMB:
1204 case ZFS_PROP_SHARENFS:
1206 * For the mountpoint and sharenfs or sharesmb
1207 * properties, check if it can be set in a
1208 * global/non-global zone based on
1209 * the zoned property value:
1211 * global zone non-global zone
1212 * --------------------------------------------------
1213 * zoned=on mountpoint (no) mountpoint (yes)
1214 * sharenfs (no) sharenfs (no)
1215 * sharesmb (no) sharesmb (no)
1217 * zoned=off mountpoint (yes) N/A
1222 if (getzoneid() == GLOBAL_ZONEID) {
1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224 "'%s' cannot be set on "
1225 "dataset in a non-global zone"),
1227 (void) zfs_error(hdl, EZFS_ZONED,
1230 } else if (prop == ZFS_PROP_SHARENFS ||
1231 prop == ZFS_PROP_SHARESMB) {
1232 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1233 "'%s' cannot be set in "
1234 "a non-global zone"), propname);
1235 (void) zfs_error(hdl, EZFS_ZONED,
1239 } else if (getzoneid() != GLOBAL_ZONEID) {
1241 * If zoned property is 'off', this must be in
1242 * a global zone. If not, something is wrong.
1244 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1245 "'%s' cannot be set while dataset "
1246 "'zoned' property is set"), propname);
1247 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1252 * At this point, it is legitimate to set the
1253 * property. Now we want to make sure that the
1254 * property value is valid if it is sharenfs.
1256 if ((prop == ZFS_PROP_SHARENFS ||
1257 prop == ZFS_PROP_SHARESMB) &&
1258 strcmp(strval, "on") != 0 &&
1259 strcmp(strval, "off") != 0) {
1260 zfs_share_proto_t proto;
1262 if (prop == ZFS_PROP_SHARESMB)
1268 * Must be an valid sharing protocol
1269 * option string so init the libshare
1270 * in order to enable the parser and
1271 * then parse the options. We use the
1272 * control API since we don't care about
1273 * the current configuration and don't
1274 * want the overhead of loading it
1275 * until we actually do something.
1278 if (zfs_init_libshare(hdl,
1279 SA_INIT_CONTROL_API) != SA_OK) {
1281 * An error occurred so we can't do
1284 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1285 "'%s' cannot be set: problem "
1286 "in share initialization"),
1288 (void) zfs_error(hdl, EZFS_BADPROP,
1293 if (zfs_parse_options(strval, proto) != SA_OK) {
1295 * There was an error in parsing so
1296 * deal with it by issuing an error
1297 * message and leaving after
1298 * uninitializing the the libshare
1301 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1302 "'%s' cannot be set to invalid "
1303 "options"), propname);
1304 (void) zfs_error(hdl, EZFS_BADPROP,
1306 zfs_uninit_libshare(hdl);
1309 zfs_uninit_libshare(hdl);
1313 case ZFS_PROP_UTF8ONLY:
1314 chosen_utf = (int)intval;
1316 case ZFS_PROP_NORMALIZE:
1317 chosen_normal = (int)intval;
1322 * For changes to existing volumes, we have some additional
1323 * checks to enforce.
1325 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1326 uint64_t volsize = zfs_prop_get_int(zhp,
1328 uint64_t blocksize = zfs_prop_get_int(zhp,
1329 ZFS_PROP_VOLBLOCKSIZE);
1333 case ZFS_PROP_RESERVATION:
1334 case ZFS_PROP_REFRESERVATION:
1335 if (intval > volsize) {
1336 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1337 "'%s' is greater than current "
1338 "volume size"), propname);
1339 (void) zfs_error(hdl, EZFS_BADPROP,
1345 case ZFS_PROP_VOLSIZE:
1346 if (intval % blocksize != 0) {
1347 zfs_nicenum(blocksize, buf,
1349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1350 "'%s' must be a multiple of "
1351 "volume block size (%s)"),
1353 (void) zfs_error(hdl, EZFS_BADPROP,
1359 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1360 "'%s' cannot be zero"),
1362 (void) zfs_error(hdl, EZFS_BADPROP,
1372 * If normalization was chosen, but no UTF8 choice was made,
1373 * enforce rejection of non-UTF8 names.
1375 * If normalization was chosen, but rejecting non-UTF8 names
1376 * was explicitly not chosen, it is an error.
1378 if (chosen_normal > 0 && chosen_utf < 0) {
1379 if (nvlist_add_uint64(ret,
1380 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1381 (void) no_memory(hdl);
1384 } else if (chosen_normal > 0 && chosen_utf == 0) {
1385 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1386 "'%s' must be set 'on' if normalization chosen"),
1387 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1388 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1399 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1401 uint64_t old_volsize;
1402 uint64_t new_volsize;
1403 uint64_t old_reservation;
1404 uint64_t new_reservation;
1405 zfs_prop_t resv_prop;
1408 * If this is an existing volume, and someone is setting the volsize,
1409 * make sure that it matches the reservation, or add it if necessary.
1411 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1412 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1414 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1415 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1416 old_reservation) || nvlist_lookup_uint64(nvl,
1417 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1420 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1423 new_reservation = zvol_volsize_to_reservation(new_volsize,
1425 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1426 new_reservation) != 0) {
1427 (void) no_memory(zhp->zfs_hdl);
1434 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1441 * For quotas and reservations, ENOSPC indicates
1442 * something different; setting a quota or reservation
1443 * doesn't use any disk space.
1446 case ZFS_PROP_QUOTA:
1447 case ZFS_PROP_REFQUOTA:
1448 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1449 "size is less than current used or "
1451 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1454 case ZFS_PROP_RESERVATION:
1455 case ZFS_PROP_REFRESERVATION:
1456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1457 "size is greater than available space"));
1458 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1462 (void) zfs_standard_error(hdl, err, errbuf);
1468 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1472 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1477 "pool and or dataset must be upgraded to set this "
1478 "property or value"));
1479 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1484 if (prop == ZFS_PROP_COMPRESSION ||
1485 prop == ZFS_PROP_RECORDSIZE) {
1486 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1487 "property setting is not allowed on "
1488 "bootable datasets"));
1489 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1491 (void) zfs_standard_error(hdl, err, errbuf);
1496 if (prop == ZPROP_INVAL) {
1497 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1499 (void) zfs_standard_error(hdl, err, errbuf);
1505 * This platform can't address a volume this big.
1508 if (prop == ZFS_PROP_VOLSIZE) {
1509 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1515 (void) zfs_standard_error(hdl, err, errbuf);
1520 * Given a property name and value, set the property for the given dataset.
1523 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1525 zfs_cmd_t zc = { 0 };
1527 prop_changelist_t *cl = NULL;
1529 libzfs_handle_t *hdl = zhp->zfs_hdl;
1530 nvlist_t *nvl = NULL, *realprops;
1532 boolean_t do_prefix = B_TRUE;
1535 (void) snprintf(errbuf, sizeof (errbuf),
1536 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1539 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1540 nvlist_add_string(nvl, propname, propval) != 0) {
1541 (void) no_memory(hdl);
1545 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1546 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1552 prop = zfs_name_to_prop(propname);
1554 /* We don't support those properties on FreeBSD. */
1556 case ZFS_PROP_DEVICES:
1557 case ZFS_PROP_ISCSIOPTIONS:
1558 case ZFS_PROP_XATTR:
1559 case ZFS_PROP_VSCAN:
1560 case ZFS_PROP_NBMAND:
1561 case ZFS_PROP_MLSLABEL:
1562 (void) snprintf(errbuf, sizeof (errbuf),
1563 "property '%s' not supported on FreeBSD", propname);
1564 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1568 if (prop == ZFS_PROP_VOLSIZE) {
1569 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1573 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1576 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1578 "child dataset with inherited mountpoint is used "
1579 "in a non-global zone"));
1580 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1585 * We don't want to unmount & remount the dataset when changing
1586 * its canmount property to 'on' or 'noauto'. We only use
1587 * the changelist logic to unmount when setting canmount=off.
1589 if (prop == ZFS_PROP_CANMOUNT) {
1591 int err = zprop_string_to_index(prop, propval, &idx,
1593 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1594 do_prefix = B_FALSE;
1597 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1601 * Execute the corresponding ioctl() to set this property.
1603 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1605 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1608 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1611 zfs_setprop_error(hdl, prop, errno, errbuf);
1612 if (added_resv && errno == ENOSPC) {
1613 /* clean up the volsize property we tried to set */
1614 uint64_t old_volsize = zfs_prop_get_int(zhp,
1617 zcmd_free_nvlists(&zc);
1618 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1620 if (nvlist_add_uint64(nvl,
1621 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1624 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1626 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1630 ret = changelist_postfix(cl);
1633 * Refresh the statistics so the new property value
1637 (void) get_stats(zhp);
1642 zcmd_free_nvlists(&zc);
1644 changelist_free(cl);
1649 * Given a property, inherit the value from the parent dataset, or if received
1650 * is TRUE, revert to the received value, if any.
1653 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1655 zfs_cmd_t zc = { 0 };
1657 prop_changelist_t *cl;
1658 libzfs_handle_t *hdl = zhp->zfs_hdl;
1662 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1663 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1665 zc.zc_cookie = received;
1666 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1668 * For user properties, the amount of work we have to do is very
1669 * small, so just do it here.
1671 if (!zfs_prop_user(propname)) {
1672 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1673 "invalid property"));
1674 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1677 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1678 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1680 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1681 return (zfs_standard_error(hdl, errno, errbuf));
1687 * Verify that this property is inheritable.
1689 if (zfs_prop_readonly(prop))
1690 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1692 if (!zfs_prop_inheritable(prop) && !received)
1693 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1696 * Check to see if the value applies to this type
1698 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1699 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1702 * Normalize the name, to get rid of shorthand abbreviations.
1704 propname = zfs_prop_to_name(prop);
1705 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1706 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1708 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1709 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1711 "dataset is used in a non-global zone"));
1712 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1716 * Determine datasets which will be affected by this change, if any.
1718 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1721 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1723 "child dataset with inherited mountpoint is used "
1724 "in a non-global zone"));
1725 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1729 if ((ret = changelist_prefix(cl)) != 0)
1732 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1733 return (zfs_standard_error(hdl, errno, errbuf));
1736 if ((ret = changelist_postfix(cl)) != 0)
1740 * Refresh the statistics so the new property is reflected.
1742 (void) get_stats(zhp);
1746 changelist_free(cl);
1751 * True DSL properties are stored in an nvlist. The following two functions
1752 * extract them appropriately.
1755 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1761 if (nvlist_lookup_nvlist(zhp->zfs_props,
1762 zfs_prop_to_name(prop), &nv) == 0) {
1763 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1764 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1766 verify(!zhp->zfs_props_table ||
1767 zhp->zfs_props_table[prop] == B_TRUE);
1768 value = zfs_prop_default_numeric(prop);
1776 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1782 if (nvlist_lookup_nvlist(zhp->zfs_props,
1783 zfs_prop_to_name(prop), &nv) == 0) {
1784 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1785 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1787 verify(!zhp->zfs_props_table ||
1788 zhp->zfs_props_table[prop] == B_TRUE);
1789 value = zfs_prop_default_string(prop);
1797 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1799 return (zhp->zfs_props == zhp->zfs_recvd_props);
1803 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1805 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1806 zhp->zfs_props = zhp->zfs_recvd_props;
1810 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1812 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1817 * Internal function for getting a numeric property. Both zfs_prop_get() and
1818 * zfs_prop_get_int() are built using this interface.
1820 * Certain properties can be overridden using 'mount -o'. In this case, scan
1821 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1822 * If they differ from the on-disk values, report the current values and mark
1823 * the source "temporary".
1826 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1827 char **source, uint64_t *val)
1829 zfs_cmd_t zc = { 0 };
1830 nvlist_t *zplprops = NULL;
1832 char *mntopt_on = NULL;
1833 char *mntopt_off = NULL;
1834 boolean_t received = zfs_is_recvd_props_mode(zhp);
1839 case ZFS_PROP_ATIME:
1840 mntopt_on = MNTOPT_ATIME;
1841 mntopt_off = MNTOPT_NOATIME;
1844 case ZFS_PROP_DEVICES:
1845 mntopt_on = MNTOPT_DEVICES;
1846 mntopt_off = MNTOPT_NODEVICES;
1850 mntopt_on = MNTOPT_EXEC;
1851 mntopt_off = MNTOPT_NOEXEC;
1854 case ZFS_PROP_READONLY:
1855 mntopt_on = MNTOPT_RO;
1856 mntopt_off = MNTOPT_RW;
1859 case ZFS_PROP_SETUID:
1860 mntopt_on = MNTOPT_SETUID;
1861 mntopt_off = MNTOPT_NOSETUID;
1864 case ZFS_PROP_XATTR:
1865 mntopt_on = MNTOPT_XATTR;
1866 mntopt_off = MNTOPT_NOXATTR;
1869 case ZFS_PROP_NBMAND:
1870 mntopt_on = MNTOPT_NBMAND;
1871 mntopt_off = MNTOPT_NONBMAND;
1876 * Because looking up the mount options is potentially expensive
1877 * (iterating over all of /etc/mnttab), we defer its calculation until
1878 * we're looking up a property which requires its presence.
1880 if (!zhp->zfs_mntcheck &&
1881 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1882 libzfs_handle_t *hdl = zhp->zfs_hdl;
1883 struct mnttab entry;
1885 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1886 zhp->zfs_mntopts = zfs_strdup(hdl,
1888 if (zhp->zfs_mntopts == NULL)
1892 zhp->zfs_mntcheck = B_TRUE;
1895 if (zhp->zfs_mntopts == NULL)
1896 mnt.mnt_mntopts = "";
1898 mnt.mnt_mntopts = zhp->zfs_mntopts;
1901 case ZFS_PROP_ATIME:
1902 case ZFS_PROP_DEVICES:
1904 case ZFS_PROP_READONLY:
1905 case ZFS_PROP_SETUID:
1906 case ZFS_PROP_XATTR:
1907 case ZFS_PROP_NBMAND:
1908 *val = getprop_uint64(zhp, prop, source);
1913 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1916 *src = ZPROP_SRC_TEMPORARY;
1917 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1920 *src = ZPROP_SRC_TEMPORARY;
1924 case ZFS_PROP_CANMOUNT:
1925 case ZFS_PROP_VOLSIZE:
1926 case ZFS_PROP_QUOTA:
1927 case ZFS_PROP_REFQUOTA:
1928 case ZFS_PROP_RESERVATION:
1929 case ZFS_PROP_REFRESERVATION:
1930 case ZFS_PROP_FILESYSTEM_LIMIT:
1931 case ZFS_PROP_SNAPSHOT_LIMIT:
1932 case ZFS_PROP_FILESYSTEM_COUNT:
1933 case ZFS_PROP_SNAPSHOT_COUNT:
1934 *val = getprop_uint64(zhp, prop, source);
1936 if (*source == NULL) {
1937 /* not default, must be local */
1938 *source = zhp->zfs_name;
1942 case ZFS_PROP_MOUNTED:
1943 *val = (zhp->zfs_mntopts != NULL);
1946 case ZFS_PROP_NUMCLONES:
1947 *val = zhp->zfs_dmustats.dds_num_clones;
1950 case ZFS_PROP_VERSION:
1951 case ZFS_PROP_NORMALIZE:
1952 case ZFS_PROP_UTF8ONLY:
1954 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1955 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1957 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1958 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1959 zcmd_free_nvlists(&zc);
1962 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1963 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1965 zcmd_free_nvlists(&zc);
1969 nvlist_free(zplprops);
1970 zcmd_free_nvlists(&zc);
1973 case ZFS_PROP_INCONSISTENT:
1974 *val = zhp->zfs_dmustats.dds_inconsistent;
1978 switch (zfs_prop_get_type(prop)) {
1979 case PROP_TYPE_NUMBER:
1980 case PROP_TYPE_INDEX:
1981 *val = getprop_uint64(zhp, prop, source);
1983 * If we tried to use a default value for a
1984 * readonly property, it means that it was not
1987 if (zfs_prop_readonly(prop) &&
1988 *source != NULL && (*source)[0] == '\0') {
1993 case PROP_TYPE_STRING:
1995 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1996 "cannot get non-numeric property"));
1997 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1998 dgettext(TEXT_DOMAIN, "internal error")));
2006 * Calculate the source type, given the raw source string.
2009 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2010 char *statbuf, size_t statlen)
2012 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2015 if (source == NULL) {
2016 *srctype = ZPROP_SRC_NONE;
2017 } else if (source[0] == '\0') {
2018 *srctype = ZPROP_SRC_DEFAULT;
2019 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2020 *srctype = ZPROP_SRC_RECEIVED;
2022 if (strcmp(source, zhp->zfs_name) == 0) {
2023 *srctype = ZPROP_SRC_LOCAL;
2025 (void) strlcpy(statbuf, source, statlen);
2026 *srctype = ZPROP_SRC_INHERITED;
2033 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2034 size_t proplen, boolean_t literal)
2039 if (zhp->zfs_recvd_props == NULL)
2040 if (get_recvd_props_ioctl(zhp) != 0)
2043 prop = zfs_name_to_prop(propname);
2045 if (prop != ZPROP_INVAL) {
2047 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2049 zfs_set_recvd_props_mode(zhp, &cookie);
2050 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2051 NULL, NULL, 0, literal);
2052 zfs_unset_recvd_props_mode(zhp, &cookie);
2056 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2057 propname, &propval) != 0)
2059 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2061 (void) strlcpy(propbuf, recvdval, proplen);
2064 return (err == 0 ? 0 : -1);
2068 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2073 value = zfs_get_clones_nvl(zhp);
2078 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2079 pair = nvlist_next_nvpair(value, pair)) {
2080 if (propbuf[0] != '\0')
2081 (void) strlcat(propbuf, ",", proplen);
2082 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2088 struct get_clones_arg {
2092 char buf[ZFS_MAXNAMELEN];
2096 get_clones_cb(zfs_handle_t *zhp, void *arg)
2098 struct get_clones_arg *gca = arg;
2100 if (gca->numclones == 0) {
2105 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2106 NULL, NULL, 0, B_TRUE) != 0)
2108 if (strcmp(gca->buf, gca->origin) == 0) {
2109 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2114 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2120 zfs_get_clones_nvl(zfs_handle_t *zhp)
2122 nvlist_t *nv, *value;
2124 if (nvlist_lookup_nvlist(zhp->zfs_props,
2125 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2126 struct get_clones_arg gca;
2129 * if this is a snapshot, then the kernel wasn't able
2130 * to get the clones. Do it by slowly iterating.
2132 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2134 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2136 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2141 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2143 gca.origin = zhp->zfs_name;
2145 if (gca.numclones != 0) {
2147 char pool[ZFS_MAXNAMELEN];
2150 /* get the pool name */
2151 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2152 (void) strsep(&cp, "/@");
2153 root = zfs_open(zhp->zfs_hdl, pool,
2154 ZFS_TYPE_FILESYSTEM);
2156 (void) get_clones_cb(root, &gca);
2159 if (gca.numclones != 0 ||
2160 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2161 nvlist_add_nvlist(zhp->zfs_props,
2162 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2169 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2170 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2173 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2179 * Retrieve a property from the given object. If 'literal' is specified, then
2180 * numbers are left as exact values. Otherwise, numbers are converted to a
2181 * human-readable form.
2183 * Returns 0 on success, or -1 on error.
2186 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2187 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2189 char *source = NULL;
2193 boolean_t received = zfs_is_recvd_props_mode(zhp);
2196 * Check to see if this property applies to our object
2198 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2201 if (received && zfs_prop_readonly(prop))
2205 *src = ZPROP_SRC_NONE;
2208 case ZFS_PROP_CREATION:
2210 * 'creation' is a time_t stored in the statistics. We convert
2211 * this into a string unless 'literal' is specified.
2214 val = getprop_uint64(zhp, prop, &source);
2215 time_t time = (time_t)val;
2219 localtime_r(&time, &t) == NULL ||
2220 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2222 (void) snprintf(propbuf, proplen, "%llu", val);
2226 case ZFS_PROP_MOUNTPOINT:
2228 * Getting the precise mountpoint can be tricky.
2230 * - for 'none' or 'legacy', return those values.
2231 * - for inherited mountpoints, we want to take everything
2232 * after our ancestor and append it to the inherited value.
2234 * If the pool has an alternate root, we want to prepend that
2235 * root to any values we return.
2238 str = getprop_string(zhp, prop, &source);
2240 if (str[0] == '/') {
2241 char buf[MAXPATHLEN];
2243 const char *relpath;
2246 * If we inherit the mountpoint, even from a dataset
2247 * with a received value, the source will be the path of
2248 * the dataset we inherit from. If source is
2249 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2252 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2255 relpath = zhp->zfs_name + strlen(source);
2256 if (relpath[0] == '/')
2260 if ((zpool_get_prop(zhp->zpool_hdl,
2261 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2262 B_FALSE)) || (strcmp(root, "-") == 0))
2265 * Special case an alternate root of '/'. This will
2266 * avoid having multiple leading slashes in the
2269 if (strcmp(root, "/") == 0)
2273 * If the mountpoint is '/' then skip over this
2274 * if we are obtaining either an alternate root or
2275 * an inherited mountpoint.
2277 if (str[1] == '\0' && (root[0] != '\0' ||
2278 relpath[0] != '\0'))
2281 if (relpath[0] == '\0')
2282 (void) snprintf(propbuf, proplen, "%s%s",
2285 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2286 root, str, relpath[0] == '@' ? "" : "/",
2289 /* 'legacy' or 'none' */
2290 (void) strlcpy(propbuf, str, proplen);
2295 case ZFS_PROP_ORIGIN:
2296 str = getprop_string(zhp, prop, &source);
2299 (void) strlcpy(propbuf, str, proplen);
2302 case ZFS_PROP_CLONES:
2303 if (get_clones_string(zhp, propbuf, proplen) != 0)
2307 case ZFS_PROP_QUOTA:
2308 case ZFS_PROP_REFQUOTA:
2309 case ZFS_PROP_RESERVATION:
2310 case ZFS_PROP_REFRESERVATION:
2312 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2316 * If quota or reservation is 0, we translate this into 'none'
2317 * (unless literal is set), and indicate that it's the default
2318 * value. Otherwise, we print the number nicely and indicate
2319 * that its set locally.
2323 (void) strlcpy(propbuf, "0", proplen);
2325 (void) strlcpy(propbuf, "none", proplen);
2328 (void) snprintf(propbuf, proplen, "%llu",
2331 zfs_nicenum(val, propbuf, proplen);
2335 case ZFS_PROP_FILESYSTEM_LIMIT:
2336 case ZFS_PROP_SNAPSHOT_LIMIT:
2337 case ZFS_PROP_FILESYSTEM_COUNT:
2338 case ZFS_PROP_SNAPSHOT_COUNT:
2340 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2344 * If limit is UINT64_MAX, we translate this into 'none' (unless
2345 * literal is set), and indicate that it's the default value.
2346 * Otherwise, we print the number nicely and indicate that it's
2350 (void) snprintf(propbuf, proplen, "%llu",
2352 } else if (val == UINT64_MAX) {
2353 (void) strlcpy(propbuf, "none", proplen);
2355 zfs_nicenum(val, propbuf, proplen);
2359 case ZFS_PROP_REFRATIO:
2360 case ZFS_PROP_COMPRESSRATIO:
2361 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2363 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2364 (u_longlong_t)(val / 100),
2365 (u_longlong_t)(val % 100));
2369 switch (zhp->zfs_type) {
2370 case ZFS_TYPE_FILESYSTEM:
2373 case ZFS_TYPE_VOLUME:
2376 case ZFS_TYPE_SNAPSHOT:
2379 case ZFS_TYPE_BOOKMARK:
2385 (void) snprintf(propbuf, proplen, "%s", str);
2388 case ZFS_PROP_MOUNTED:
2390 * The 'mounted' property is a pseudo-property that described
2391 * whether the filesystem is currently mounted. Even though
2392 * it's a boolean value, the typical values of "on" and "off"
2393 * don't make sense, so we translate to "yes" and "no".
2395 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2396 src, &source, &val) != 0)
2399 (void) strlcpy(propbuf, "yes", proplen);
2401 (void) strlcpy(propbuf, "no", proplen);
2406 * The 'name' property is a pseudo-property derived from the
2407 * dataset name. It is presented as a real property to simplify
2410 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2413 case ZFS_PROP_MLSLABEL:
2416 m_label_t *new_sl = NULL;
2417 char *ascii = NULL; /* human readable label */
2419 (void) strlcpy(propbuf,
2420 getprop_string(zhp, prop, &source), proplen);
2422 if (literal || (strcasecmp(propbuf,
2423 ZFS_MLSLABEL_DEFAULT) == 0))
2427 * Try to translate the internal hex string to
2428 * human-readable output. If there are any
2429 * problems just use the hex string.
2432 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2433 L_NO_CORRECTION, NULL) == -1) {
2434 m_label_free(new_sl);
2438 if (label_to_str(new_sl, &ascii, M_LABEL,
2442 m_label_free(new_sl);
2445 m_label_free(new_sl);
2447 (void) strlcpy(propbuf, ascii, proplen);
2457 * GUIDs are stored as numbers, but they are identifiers.
2458 * We don't want them to be pretty printed, because pretty
2459 * printing mangles the ID into a truncated and useless value.
2461 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2463 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2467 switch (zfs_prop_get_type(prop)) {
2468 case PROP_TYPE_NUMBER:
2469 if (get_numeric_property(zhp, prop, src,
2470 &source, &val) != 0)
2473 (void) snprintf(propbuf, proplen, "%llu",
2476 zfs_nicenum(val, propbuf, proplen);
2479 case PROP_TYPE_STRING:
2480 str = getprop_string(zhp, prop, &source);
2483 (void) strlcpy(propbuf, str, proplen);
2486 case PROP_TYPE_INDEX:
2487 if (get_numeric_property(zhp, prop, src,
2488 &source, &val) != 0)
2490 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2492 (void) strlcpy(propbuf, strval, proplen);
2500 get_source(zhp, src, source, statbuf, statlen);
2506 * Utility function to get the given numeric property. Does no validation that
2507 * the given property is the appropriate type; should only be used with
2508 * hard-coded property types.
2511 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2516 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2522 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2526 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2527 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2531 * Similar to zfs_prop_get(), but returns the value as an integer.
2534 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2535 zprop_source_t *src, char *statbuf, size_t statlen)
2540 * Check to see if this property applies to our object
2542 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2543 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2544 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2545 zfs_prop_to_name(prop)));
2549 *src = ZPROP_SRC_NONE;
2551 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2554 get_source(zhp, src, source, statbuf, statlen);
2560 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2561 char **domainp, idmap_rid_t *ridp)
2564 idmap_get_handle_t *get_hdl = NULL;
2568 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2572 err = idmap_get_sidbyuid(get_hdl, id,
2573 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2575 err = idmap_get_sidbygid(get_hdl, id,
2576 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2578 if (err == IDMAP_SUCCESS &&
2579 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2580 status == IDMAP_SUCCESS)
2586 idmap_get_destroy(get_hdl);
2589 assert(!"invalid code path");
2590 return (EINVAL); // silence compiler warning
2595 * convert the propname into parameters needed by kernel
2596 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2597 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2600 userquota_propname_decode(const char *propname, boolean_t zoned,
2601 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2603 zfs_userquota_prop_t type;
2605 char *numericsid = NULL;
2610 /* Figure out the property type ({user|group}{quota|space}) */
2611 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2612 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2613 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2616 if (type == ZFS_NUM_USERQUOTA_PROPS)
2620 isuser = (type == ZFS_PROP_USERQUOTA ||
2621 type == ZFS_PROP_USERUSED);
2623 cp = strchr(propname, '@') + 1;
2625 if (strchr(cp, '@')) {
2628 * It's a SID name (eg "user@domain") that needs to be
2629 * turned into S-1-domainID-RID.
2632 idmap_stat stat, map_stat;
2635 idmap_get_handle_t *gh = NULL;
2637 stat = idmap_get_create(&gh);
2638 if (stat != IDMAP_SUCCESS) {
2639 idmap_get_destroy(gh);
2642 if (zoned && getzoneid() == GLOBAL_ZONEID)
2645 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2648 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2651 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2654 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2658 idmap_get_destroy(gh);
2661 stat = idmap_get_mappings(gh);
2662 idmap_get_destroy(gh);
2667 if (numericsid == NULL)
2671 /* will be further decoded below */
2677 if (strncmp(cp, "S-1-", 4) == 0) {
2678 /* It's a numeric SID (eg "S-1-234-567-89") */
2679 (void) strlcpy(domain, cp, domainlen);
2682 cp = strrchr(domain, '-');
2685 *ridp = strtoull(cp, &end, 10);
2693 if (errno != 0 || *end != '\0')
2695 } else if (!isdigit(*cp)) {
2697 * It's a user/group name (eg "user") that needs to be
2698 * turned into a uid/gid
2700 if (zoned && getzoneid() == GLOBAL_ZONEID)
2716 /* It's a user/group ID (eg "12345"). */
2717 uid_t id = strtoul(cp, &end, 10);
2724 /* It's an ephemeral ID. */
2725 if (idmap_id_to_numeric_domain_rid(id, isuser,
2726 &mapdomain, &rid) != 0)
2728 (void) strlcpy(domain, mapdomain, domainlen);
2735 ASSERT3P(numericsid, ==, NULL);
2740 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2741 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2744 zfs_cmd_t zc = { 0 };
2746 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2748 err = userquota_propname_decode(propname,
2749 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2750 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2751 zc.zc_objset_type = *typep;
2755 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2759 *propvalue = zc.zc_cookie;
2764 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2765 uint64_t *propvalue)
2767 zfs_userquota_prop_t type;
2769 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2774 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2775 char *propbuf, int proplen, boolean_t literal)
2779 zfs_userquota_prop_t type;
2781 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2788 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2789 } else if (propvalue == 0 &&
2790 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2791 (void) strlcpy(propbuf, "none", proplen);
2793 zfs_nicenum(propvalue, propbuf, proplen);
2799 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2800 uint64_t *propvalue)
2803 zfs_cmd_t zc = { 0 };
2804 const char *snapname;
2806 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2808 snapname = strchr(propname, '@') + 1;
2809 if (strchr(snapname, '@')) {
2810 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2812 /* snapname is the short name, append it to zhp's fsname */
2815 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2816 sizeof (zc.zc_value));
2817 cp = strchr(zc.zc_value, '@');
2820 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2821 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2824 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2828 *propvalue = zc.zc_cookie;
2833 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2834 char *propbuf, int proplen, boolean_t literal)
2839 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2845 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2847 zfs_nicenum(propvalue, propbuf, proplen);
2853 * Returns the name of the given zfs handle.
2856 zfs_get_name(const zfs_handle_t *zhp)
2858 return (zhp->zfs_name);
2862 * Returns the type of the given zfs handle.
2865 zfs_get_type(const zfs_handle_t *zhp)
2867 return (zhp->zfs_type);
2871 * Is one dataset name a child dataset of another?
2873 * Needs to handle these cases:
2874 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2875 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2876 * Descendant? No. No. No. Yes.
2879 is_descendant(const char *ds1, const char *ds2)
2881 size_t d1len = strlen(ds1);
2883 /* ds2 can't be a descendant if it's smaller */
2884 if (strlen(ds2) < d1len)
2887 /* otherwise, compare strings and verify that there's a '/' char */
2888 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2892 * Given a complete name, return just the portion that refers to the parent.
2893 * Will return -1 if there is no parent (path is just the name of the
2897 parent_name(const char *path, char *buf, size_t buflen)
2901 (void) strlcpy(buf, path, buflen);
2903 if ((slashp = strrchr(buf, '/')) == NULL)
2911 * If accept_ancestor is false, then check to make sure that the given path has
2912 * a parent, and that it exists. If accept_ancestor is true, then find the
2913 * closest existing ancestor for the given path. In prefixlen return the
2914 * length of already existing prefix of the given path. We also fetch the
2915 * 'zoned' property, which is used to validate property settings when creating
2919 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2920 boolean_t accept_ancestor, int *prefixlen)
2922 zfs_cmd_t zc = { 0 };
2923 char parent[ZFS_MAXNAMELEN];
2929 (void) snprintf(errbuf, sizeof (errbuf),
2930 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2932 /* get parent, and check to see if this is just a pool */
2933 if (parent_name(path, parent, sizeof (parent)) != 0) {
2934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2935 "missing dataset name"));
2936 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2939 /* check to see if the pool exists */
2940 if ((slash = strchr(parent, '/')) == NULL)
2941 slash = parent + strlen(parent);
2942 (void) strncpy(zc.zc_name, parent, slash - parent);
2943 zc.zc_name[slash - parent] = '\0';
2944 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2947 "no such pool '%s'"), zc.zc_name);
2948 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2951 /* check to see if the parent dataset exists */
2952 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2953 if (errno == ENOENT && accept_ancestor) {
2955 * Go deeper to find an ancestor, give up on top level.
2957 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2959 "no such pool '%s'"), zc.zc_name);
2960 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2962 } else if (errno == ENOENT) {
2963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2964 "parent does not exist"));
2965 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2967 return (zfs_standard_error(hdl, errno, errbuf));
2970 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2974 /* we are in a non-global zone, but parent is in the global zone */
2975 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2976 (void) zfs_standard_error(hdl, EPERM, errbuf);
2981 /* make sure parent is a filesystem */
2982 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2983 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2984 "parent is not a filesystem"));
2985 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2991 if (prefixlen != NULL)
2992 *prefixlen = strlen(parent);
2997 * Finds whether the dataset of the given type(s) exists.
3000 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3004 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3008 * Try to get stats for the dataset, which will tell us if it exists.
3010 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3011 int ds_type = zhp->zfs_type;
3014 if (types & ds_type)
3021 * Given a path to 'target', create all the ancestors between
3022 * the prefixlen portion of the path, and the target itself.
3023 * Fail if the initial prefixlen-ancestor does not already exist.
3026 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3032 /* make sure prefix exists */
3033 cp = target + prefixlen;
3035 assert(strchr(cp, '/') == NULL);
3036 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3039 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3047 * Attempt to create, mount, and share any ancestor filesystems,
3048 * up to the prefixlen-long one.
3050 for (cp = target + prefixlen + 1;
3051 cp = strchr(cp, '/'); *cp = '/', cp++) {
3055 h = make_dataset_handle(hdl, target);
3057 /* it already exists, nothing to do here */
3062 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3064 opname = dgettext(TEXT_DOMAIN, "create");
3068 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3070 opname = dgettext(TEXT_DOMAIN, "open");
3074 if (zfs_mount(h, NULL, 0) != 0) {
3075 opname = dgettext(TEXT_DOMAIN, "mount");
3079 if (zfs_share(h) != 0) {
3080 opname = dgettext(TEXT_DOMAIN, "share");
3090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3091 "failed to %s ancestor '%s'"), opname, target);
3096 * Creates non-existing ancestors of the given path.
3099 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3105 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3108 if ((path_copy = strdup(path)) != NULL) {
3109 rc = create_parents(hdl, path_copy, prefix);
3112 if (path_copy == NULL || rc != 0)
3119 * Create a new filesystem or volume.
3122 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3127 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3130 dmu_objset_type_t ost;
3132 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3133 "cannot create '%s'"), path);
3135 /* validate the path, taking care to note the extended error message */
3136 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3137 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3139 /* validate parents exist */
3140 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3144 * The failure modes when creating a dataset of a different type over
3145 * one that already exists is a little strange. In particular, if you
3146 * try to create a dataset on top of an existing dataset, the ioctl()
3147 * will return ENOENT, not EEXIST. To prevent this from happening, we
3148 * first try to see if the dataset exists.
3150 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3152 "dataset already exists"));
3153 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3156 if (type == ZFS_TYPE_VOLUME)
3161 if (props && (props = zfs_valid_proplist(hdl, type, props,
3162 zoned, NULL, errbuf)) == 0)
3165 if (type == ZFS_TYPE_VOLUME) {
3167 * If we are creating a volume, the size and block size must
3168 * satisfy a few restraints. First, the blocksize must be a
3169 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3170 * volsize must be a multiple of the block size, and cannot be
3173 if (props == NULL || nvlist_lookup_uint64(props,
3174 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3177 "missing volume size"));
3178 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3181 if ((ret = nvlist_lookup_uint64(props,
3182 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3183 &blocksize)) != 0) {
3184 if (ret == ENOENT) {
3185 blocksize = zfs_prop_default_numeric(
3186 ZFS_PROP_VOLBLOCKSIZE);
3189 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3190 "missing volume block size"));
3191 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3198 "volume size cannot be zero"));
3199 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3202 if (size % blocksize != 0) {
3204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3205 "volume size must be a multiple of volume block "
3207 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3211 /* create the dataset */
3212 ret = lzc_create(path, ost, props);
3215 /* check for failure */
3217 char parent[ZFS_MAXNAMELEN];
3218 (void) parent_name(path, parent, sizeof (parent));
3222 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3223 "no such parent '%s'"), parent);
3224 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3228 "parent '%s' is not a filesystem"), parent);
3229 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3232 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3233 "volume block size must be power of 2 from "
3236 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3239 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3240 "pool must be upgraded to set this "
3241 "property or value"));
3242 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3246 * This platform can't address a volume this big.
3248 if (type == ZFS_TYPE_VOLUME)
3249 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3254 return (zfs_standard_error(hdl, errno, errbuf));
3262 * Destroys the given dataset. The caller must make sure that the filesystem
3263 * isn't mounted, and that there are no active dependents. If the file system
3264 * does not exist this function does nothing.
3267 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3269 zfs_cmd_t zc = { 0 };
3271 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3272 nvlist_t *nv = fnvlist_alloc();
3273 fnvlist_add_boolean(nv, zhp->zfs_name);
3274 int error = lzc_destroy_bookmarks(nv, NULL);
3277 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3278 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3284 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3286 if (ZFS_IS_VOLUME(zhp)) {
3287 zc.zc_objset_type = DMU_OST_ZVOL;
3289 zc.zc_objset_type = DMU_OST_ZFS;
3292 zc.zc_defer_destroy = defer;
3293 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3295 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3296 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3300 remove_mountpoint(zhp);
3305 struct destroydata {
3307 const char *snapname;
3311 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3313 struct destroydata *dd = arg;
3314 char name[ZFS_MAXNAMELEN];
3317 (void) snprintf(name, sizeof (name),
3318 "%s@%s", zhp->zfs_name, dd->snapname);
3320 if (lzc_exists(name))
3321 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3323 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3329 * Destroys all snapshots with the given name in zhp & descendants.
3332 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3335 struct destroydata dd = { 0 };
3337 dd.snapname = snapname;
3338 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3339 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3341 if (nvlist_empty(dd.nvl)) {
3342 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3343 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3344 zhp->zfs_name, snapname);
3346 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3348 nvlist_free(dd.nvl);
3353 * Destroys all the snapshots named in the nvlist.
3356 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3361 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3366 if (nvlist_empty(errlist)) {
3368 (void) snprintf(errbuf, sizeof (errbuf),
3369 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3371 ret = zfs_standard_error(hdl, ret, errbuf);
3373 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3374 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3376 (void) snprintf(errbuf, sizeof (errbuf),
3377 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3380 switch (fnvpair_value_int32(pair)) {
3383 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3384 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3387 ret = zfs_standard_error(hdl, errno, errbuf);
3396 * Clones the given dataset. The target must be of the same type as the source.
3399 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3401 char parent[ZFS_MAXNAMELEN];
3404 libzfs_handle_t *hdl = zhp->zfs_hdl;
3407 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3409 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3410 "cannot create '%s'"), target);
3412 /* validate the target/clone name */
3413 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3414 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3416 /* validate parents exist */
3417 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3420 (void) parent_name(target, parent, sizeof (parent));
3426 if (ZFS_IS_VOLUME(zhp)) {
3427 type = ZFS_TYPE_VOLUME;
3429 type = ZFS_TYPE_FILESYSTEM;
3431 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3432 zhp, errbuf)) == NULL)
3436 ret = lzc_clone(target, zhp->zfs_name, props);
3444 * The parent doesn't exist. We should have caught this
3445 * above, but there may a race condition that has since
3446 * destroyed the parent.
3448 * At this point, we don't know whether it's the source
3449 * that doesn't exist anymore, or whether the target
3450 * dataset doesn't exist.
3452 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3453 "no such parent '%s'"), parent);
3454 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3457 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3458 "source and target pools differ"));
3459 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3463 return (zfs_standard_error(zhp->zfs_hdl, errno,
3472 * Promotes the given clone fs to be the clone parent.
3475 zfs_promote(zfs_handle_t *zhp)
3477 libzfs_handle_t *hdl = zhp->zfs_hdl;
3478 zfs_cmd_t zc = { 0 };
3479 char parent[MAXPATHLEN];
3483 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3484 "cannot promote '%s'"), zhp->zfs_name);
3486 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3488 "snapshots can not be promoted"));
3489 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3492 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3493 if (parent[0] == '\0') {
3494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3495 "not a cloned filesystem"));
3496 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3499 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3500 sizeof (zc.zc_value));
3501 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3502 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3505 int save_errno = errno;
3507 switch (save_errno) {
3509 /* There is a conflicting snapshot name. */
3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3511 "conflicting snapshot '%s' from parent '%s'"),
3512 zc.zc_string, parent);
3513 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3516 return (zfs_standard_error(hdl, save_errno, errbuf));
3522 typedef struct snapdata {
3524 const char *sd_snapname;
3528 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3530 snapdata_t *sd = arg;
3531 char name[ZFS_MAXNAMELEN];
3534 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3535 (void) snprintf(name, sizeof (name),
3536 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3538 fnvlist_add_boolean(sd->sd_nvl, name);
3540 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3548 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3552 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3559 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3560 "cannot create snapshots "));
3563 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3564 const char *snapname = nvpair_name(elem);
3566 /* validate the target name */
3567 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3569 (void) snprintf(errbuf, sizeof (errbuf),
3570 dgettext(TEXT_DOMAIN,
3571 "cannot create snapshot '%s'"), snapname);
3572 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3576 if (props != NULL &&
3577 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3578 props, B_FALSE, NULL, errbuf)) == NULL) {
3582 ret = lzc_snapshot(snaps, props, &errors);
3585 boolean_t printed = B_FALSE;
3586 for (elem = nvlist_next_nvpair(errors, NULL);
3588 elem = nvlist_next_nvpair(errors, elem)) {
3589 (void) snprintf(errbuf, sizeof (errbuf),
3590 dgettext(TEXT_DOMAIN,
3591 "cannot create snapshot '%s'"), nvpair_name(elem));
3592 (void) zfs_standard_error(hdl,
3593 fnvpair_value_int32(elem), errbuf);
3599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3600 "multiple snapshots of same "
3602 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3606 (void) zfs_standard_error(hdl, ret, errbuf);
3612 nvlist_free(errors);
3617 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3621 snapdata_t sd = { 0 };
3622 char fsname[ZFS_MAXNAMELEN];
3627 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3628 "cannot snapshot %s"), path);
3630 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3631 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3633 (void) strlcpy(fsname, path, sizeof (fsname));
3634 cp = strchr(fsname, '@');
3636 sd.sd_snapname = cp + 1;
3638 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3639 ZFS_TYPE_VOLUME)) == NULL) {
3643 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3645 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3647 fnvlist_add_boolean(sd.sd_nvl, path);
3650 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3651 nvlist_free(sd.sd_nvl);
3657 * Destroy any more recent snapshots. We invoke this callback on any dependents
3658 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3659 * is a dependent and we should just destroy it without checking the transaction
3662 typedef struct rollback_data {
3663 const char *cb_target; /* the snapshot */
3664 uint64_t cb_create; /* creation time reference */
3670 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3672 rollback_data_t *cbp = data;
3673 prop_changelist_t *clp;
3675 /* We must destroy this clone; first unmount it */
3676 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3677 cbp->cb_force ? MS_FORCE: 0);
3678 if (clp == NULL || changelist_prefix(clp) != 0) {
3679 cbp->cb_error = B_TRUE;
3683 if (zfs_destroy(zhp, B_FALSE) != 0)
3684 cbp->cb_error = B_TRUE;
3686 changelist_remove(clp, zhp->zfs_name);
3687 (void) changelist_postfix(clp);
3688 changelist_free(clp);
3695 rollback_destroy(zfs_handle_t *zhp, void *data)
3697 rollback_data_t *cbp = data;
3699 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3700 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3701 rollback_destroy_dependent, cbp);
3703 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3711 * Given a dataset, rollback to a specific snapshot, discarding any
3712 * data changes since then and making it the active dataset.
3714 * Any snapshots and bookmarks more recent than the target are
3715 * destroyed, along with their dependents (i.e. clones).
3718 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3720 rollback_data_t cb = { 0 };
3722 boolean_t restore_resv = 0;
3723 uint64_t old_volsize, new_volsize;
3724 zfs_prop_t resv_prop;
3726 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3727 zhp->zfs_type == ZFS_TYPE_VOLUME);
3730 * Destroy all recent snapshots and their dependents.
3732 cb.cb_force = force;
3733 cb.cb_target = snap->zfs_name;
3734 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3735 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3736 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3742 * Now that we have verified that the snapshot is the latest,
3743 * rollback to the given snapshot.
3746 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3747 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3749 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3751 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3755 * We rely on zfs_iter_children() to verify that there are no
3756 * newer snapshots for the given dataset. Therefore, we can
3757 * simply pass the name on to the ioctl() call. There is still
3758 * an unlikely race condition where the user has taken a
3759 * snapshot since we verified that this was the most recent.
3761 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3763 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3764 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3770 * For volumes, if the pre-rollback volsize matched the pre-
3771 * rollback reservation and the volsize has changed then set
3772 * the reservation property to the post-rollback volsize.
3773 * Make a new handle since the rollback closed the dataset.
3775 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3776 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3778 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3779 if (old_volsize != new_volsize)
3780 err = zfs_prop_set_int(zhp, resv_prop,
3789 * Renames the given dataset.
3792 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
3793 renameflags_t flags)
3796 zfs_cmd_t zc = { 0 };
3798 prop_changelist_t *cl = NULL;
3799 zfs_handle_t *zhrp = NULL;
3800 char *parentname = NULL;
3801 char parent[ZFS_MAXNAMELEN];
3802 char property[ZFS_MAXPROPLEN];
3803 libzfs_handle_t *hdl = zhp->zfs_hdl;
3806 /* if we have the same exact name, just return success */
3807 if (strcmp(zhp->zfs_name, target) == 0)
3810 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3811 "cannot rename to '%s'"), target);
3813 if (source != NULL) {
3815 * This is recursive snapshots rename, put snapshot name
3816 * (that might not exist) into zfs_name.
3818 assert(flags.recurse);
3820 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
3821 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
3822 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
3826 * Make sure the target name is valid
3828 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3829 if ((strchr(target, '@') == NULL) ||
3832 * Snapshot target name is abbreviated,
3833 * reconstruct full dataset name
3835 (void) strlcpy(parent, zhp->zfs_name,
3837 delim = strchr(parent, '@');
3838 if (strchr(target, '@') == NULL)
3842 (void) strlcat(parent, target, sizeof (parent));
3846 * Make sure we're renaming within the same dataset.
3848 delim = strchr(target, '@');
3849 if (strncmp(zhp->zfs_name, target, delim - target)
3850 != 0 || zhp->zfs_name[delim - target] != '@') {
3851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3852 "snapshots must be part of same "
3854 return (zfs_error(hdl, EZFS_CROSSTARGET,
3858 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3859 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3861 if (flags.recurse) {
3862 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3863 "recursive rename must be a snapshot"));
3864 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3867 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3868 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3870 /* validate parents */
3871 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3874 /* make sure we're in the same pool */
3875 verify((delim = strchr(target, '/')) != NULL);
3876 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3877 zhp->zfs_name[delim - target] != '/') {
3878 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3879 "datasets must be within same pool"));
3880 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3883 /* new name cannot be a child of the current dataset name */
3884 if (is_descendant(zhp->zfs_name, target)) {
3885 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3886 "New dataset name cannot be a descendant of "
3887 "current dataset name"));
3888 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3892 (void) snprintf(errbuf, sizeof (errbuf),
3893 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3895 if (getzoneid() == GLOBAL_ZONEID &&
3896 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3897 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3898 "dataset is used in a non-global zone"));
3899 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3903 * Avoid unmounting file systems with mountpoint property set to
3904 * 'legacy' or 'none' even if -u option is not given.
3906 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3907 !flags.recurse && !flags.nounmount &&
3908 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
3909 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
3910 (strcmp(property, "legacy") == 0 ||
3911 strcmp(property, "none") == 0)) {
3912 flags.nounmount = B_TRUE;
3914 if (flags.recurse) {
3916 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3917 if (parentname == NULL) {
3921 delim = strchr(parentname, '@');
3923 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3928 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
3929 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
3930 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
3931 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
3935 if (changelist_haszonedchild(cl)) {
3936 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3937 "child dataset with inherited mountpoint is used "
3938 "in a non-global zone"));
3939 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3943 if ((ret = changelist_prefix(cl)) != 0)
3947 if (ZFS_IS_VOLUME(zhp))
3948 zc.zc_objset_type = DMU_OST_ZVOL;
3950 zc.zc_objset_type = DMU_OST_ZFS;
3952 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3953 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3955 zc.zc_cookie = flags.recurse ? 1 : 0;
3956 if (flags.nounmount)
3959 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3961 * if it was recursive, the one that actually failed will
3964 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3965 "cannot rename '%s'"), zc.zc_name);
3967 if (flags.recurse && errno == EEXIST) {
3968 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3969 "a child dataset already has a snapshot "
3970 "with the new name"));
3971 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3973 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3977 * On failure, we still want to remount any filesystems that
3978 * were previously mounted, so we don't alter the system state.
3981 (void) changelist_postfix(cl);
3984 changelist_rename(cl, zfs_get_name(zhp), target);
3985 ret = changelist_postfix(cl);
3990 if (parentname != NULL) {
3997 changelist_free(cl);
4003 zfs_get_user_props(zfs_handle_t *zhp)
4005 return (zhp->zfs_user_props);
4009 zfs_get_recvd_props(zfs_handle_t *zhp)
4011 if (zhp->zfs_recvd_props == NULL)
4012 if (get_recvd_props_ioctl(zhp) != 0)
4014 return (zhp->zfs_recvd_props);
4018 * This function is used by 'zfs list' to determine the exact set of columns to
4019 * display, and their maximum widths. This does two main things:
4021 * - If this is a list of all properties, then expand the list to include
4022 * all native properties, and set a flag so that for each dataset we look
4023 * for new unique user properties and add them to the list.
4025 * - For non fixed-width properties, keep track of the maximum width seen
4026 * so that we can size the column appropriately. If the user has
4027 * requested received property values, we also need to compute the width
4028 * of the RECEIVED column.
4031 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4034 libzfs_handle_t *hdl = zhp->zfs_hdl;
4035 zprop_list_t *entry;
4036 zprop_list_t **last, **start;
4037 nvlist_t *userprops, *propval;
4040 char buf[ZFS_MAXPROPLEN];
4042 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4045 userprops = zfs_get_user_props(zhp);
4048 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4050 * Go through and add any user properties as necessary. We
4051 * start by incrementing our list pointer to the first
4052 * non-native property.
4055 while (*start != NULL) {
4056 if ((*start)->pl_prop == ZPROP_INVAL)
4058 start = &(*start)->pl_next;
4062 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4064 * See if we've already found this property in our list.
4066 for (last = start; *last != NULL;
4067 last = &(*last)->pl_next) {
4068 if (strcmp((*last)->pl_user_prop,
4069 nvpair_name(elem)) == 0)
4073 if (*last == NULL) {
4074 if ((entry = zfs_alloc(hdl,
4075 sizeof (zprop_list_t))) == NULL ||
4076 ((entry->pl_user_prop = zfs_strdup(hdl,
4077 nvpair_name(elem)))) == NULL) {
4082 entry->pl_prop = ZPROP_INVAL;
4083 entry->pl_width = strlen(nvpair_name(elem));
4084 entry->pl_all = B_TRUE;
4091 * Now go through and check the width of any non-fixed columns
4093 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4094 if (entry->pl_fixed && !literal)
4097 if (entry->pl_prop != ZPROP_INVAL) {
4098 if (zfs_prop_get(zhp, entry->pl_prop,
4099 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4100 if (strlen(buf) > entry->pl_width)
4101 entry->pl_width = strlen(buf);
4103 if (received && zfs_prop_get_recvd(zhp,
4104 zfs_prop_to_name(entry->pl_prop),
4105 buf, sizeof (buf), literal) == 0)
4106 if (strlen(buf) > entry->pl_recvd_width)
4107 entry->pl_recvd_width = strlen(buf);
4109 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4111 verify(nvlist_lookup_string(propval,
4112 ZPROP_VALUE, &strval) == 0);
4113 if (strlen(strval) > entry->pl_width)
4114 entry->pl_width = strlen(strval);
4116 if (received && zfs_prop_get_recvd(zhp,
4117 entry->pl_user_prop,
4118 buf, sizeof (buf), literal) == 0)
4119 if (strlen(buf) > entry->pl_recvd_width)
4120 entry->pl_recvd_width = strlen(buf);
4128 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4129 char *resource, void *export, void *sharetab,
4130 int sharemax, zfs_share_op_t operation)
4132 zfs_cmd_t zc = { 0 };
4135 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4136 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4138 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4139 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4140 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4141 zc.zc_share.z_sharetype = operation;
4142 zc.zc_share.z_sharemax = sharemax;
4143 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4148 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4153 * Keep a reference to the props-table against which we prune the
4156 zhp->zfs_props_table = props;
4158 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4161 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4162 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4165 * User properties will result in ZPROP_INVAL, and since we
4166 * only know how to prune standard ZFS properties, we always
4167 * leave these in the list. This can also happen if we
4168 * encounter an unknown DSL property (when running older
4169 * software, for example).
4171 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4172 (void) nvlist_remove(zhp->zfs_props,
4173 nvpair_name(curr), nvpair_type(curr));
4180 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4181 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4183 zfs_cmd_t zc = { 0 };
4184 nvlist_t *nvlist = NULL;
4187 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4188 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4189 zc.zc_cookie = (uint64_t)cmd;
4191 if (cmd == ZFS_SMB_ACL_RENAME) {
4192 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4193 (void) no_memory(hdl);
4199 case ZFS_SMB_ACL_ADD:
4200 case ZFS_SMB_ACL_REMOVE:
4201 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4203 case ZFS_SMB_ACL_RENAME:
4204 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4206 (void) no_memory(hdl);
4209 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4211 (void) no_memory(hdl);
4214 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4215 nvlist_free(nvlist);
4219 case ZFS_SMB_ACL_PURGE:
4224 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4226 nvlist_free(nvlist);
4231 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4232 char *path, char *resource)
4234 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4239 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4240 char *path, char *resource)
4242 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4247 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4249 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4254 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4255 char *oldname, char *newname)
4257 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4263 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4264 zfs_userspace_cb_t func, void *arg)
4266 zfs_cmd_t zc = { 0 };
4267 zfs_useracct_t buf[100];
4268 libzfs_handle_t *hdl = zhp->zfs_hdl;
4271 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4273 zc.zc_objset_type = type;
4274 zc.zc_nvlist_dst = (uintptr_t)buf;
4277 zfs_useracct_t *zua = buf;
4279 zc.zc_nvlist_dst_size = sizeof (buf);
4280 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4283 (void) snprintf(errbuf, sizeof (errbuf),
4284 dgettext(TEXT_DOMAIN,
4285 "cannot get used/quota for %s"), zc.zc_name);
4286 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4288 if (zc.zc_nvlist_dst_size == 0)
4291 while (zc.zc_nvlist_dst_size > 0) {
4292 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4293 zua->zu_space)) != 0)
4296 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4305 const char *snapname;
4307 boolean_t recursive;
4312 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4314 struct holdarg *ha = arg;
4315 char name[ZFS_MAXNAMELEN];
4318 (void) snprintf(name, sizeof (name),
4319 "%s@%s", zhp->zfs_name, ha->snapname);
4321 if (lzc_exists(name))
4322 fnvlist_add_string(ha->nvl, name, ha->tag);
4325 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4331 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4332 boolean_t recursive, int cleanup_fd)
4337 ha.nvl = fnvlist_alloc();
4338 ha.snapname = snapname;
4340 ha.recursive = recursive;
4341 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4343 if (nvlist_empty(ha.nvl)) {
4346 fnvlist_free(ha.nvl);
4348 (void) snprintf(errbuf, sizeof (errbuf),
4349 dgettext(TEXT_DOMAIN,
4350 "cannot hold snapshot '%s@%s'"),
4351 zhp->zfs_name, snapname);
4352 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4356 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4357 fnvlist_free(ha.nvl);
4363 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4367 libzfs_handle_t *hdl = zhp->zfs_hdl;
4372 ret = lzc_hold(holds, cleanup_fd, &errors);
4375 /* There may be errors even in the success case. */
4376 fnvlist_free(errors);
4380 if (nvlist_empty(errors)) {
4381 /* no hold-specific errors */
4382 (void) snprintf(errbuf, sizeof (errbuf),
4383 dgettext(TEXT_DOMAIN, "cannot hold"));
4386 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4387 "pool must be upgraded"));
4388 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4391 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4394 (void) zfs_standard_error(hdl, ret, errbuf);
4398 for (elem = nvlist_next_nvpair(errors, NULL);
4400 elem = nvlist_next_nvpair(errors, elem)) {
4401 (void) snprintf(errbuf, sizeof (errbuf),
4402 dgettext(TEXT_DOMAIN,
4403 "cannot hold snapshot '%s'"), nvpair_name(elem));
4404 switch (fnvpair_value_int32(elem)) {
4407 * Temporary tags wind up having the ds object id
4408 * prepended. So even if we passed the length check
4409 * above, it's still possible for the tag to wind
4410 * up being slightly too long.
4412 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4415 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4418 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4421 (void) zfs_standard_error(hdl,
4422 fnvpair_value_int32(elem), errbuf);
4426 fnvlist_free(errors);
4431 zfs_release_one(zfs_handle_t *zhp, void *arg)
4433 struct holdarg *ha = arg;
4434 char name[ZFS_MAXNAMELEN];
4436 nvlist_t *existing_holds;
4438 (void) snprintf(name, sizeof (name),
4439 "%s@%s", zhp->zfs_name, ha->snapname);
4441 if (lzc_get_holds(name, &existing_holds) != 0) {
4443 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4446 nvlist_t *torelease = fnvlist_alloc();
4447 fnvlist_add_boolean(torelease, ha->tag);
4448 fnvlist_add_nvlist(ha->nvl, name, torelease);
4449 fnvlist_free(torelease);
4453 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4459 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4460 boolean_t recursive)
4464 nvlist_t *errors = NULL;
4466 libzfs_handle_t *hdl = zhp->zfs_hdl;
4469 ha.nvl = fnvlist_alloc();
4470 ha.snapname = snapname;
4472 ha.recursive = recursive;
4474 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4476 if (nvlist_empty(ha.nvl)) {
4477 fnvlist_free(ha.nvl);
4479 (void) snprintf(errbuf, sizeof (errbuf),
4480 dgettext(TEXT_DOMAIN,
4481 "cannot release hold from snapshot '%s@%s'"),
4482 zhp->zfs_name, snapname);
4484 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4486 (void) zfs_standard_error(hdl, ret, errbuf);
4491 ret = lzc_release(ha.nvl, &errors);
4492 fnvlist_free(ha.nvl);
4495 /* There may be errors even in the success case. */
4496 fnvlist_free(errors);
4500 if (nvlist_empty(errors)) {
4501 /* no hold-specific errors */
4502 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4507 "pool must be upgraded"));
4508 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4511 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4515 for (elem = nvlist_next_nvpair(errors, NULL);
4517 elem = nvlist_next_nvpair(errors, elem)) {
4518 (void) snprintf(errbuf, sizeof (errbuf),
4519 dgettext(TEXT_DOMAIN,
4520 "cannot release hold from snapshot '%s'"),
4522 switch (fnvpair_value_int32(elem)) {
4524 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4527 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4530 (void) zfs_standard_error_fmt(hdl,
4531 fnvpair_value_int32(elem), errbuf);
4535 fnvlist_free(errors);
4540 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4542 zfs_cmd_t zc = { 0 };
4543 libzfs_handle_t *hdl = zhp->zfs_hdl;
4549 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4550 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4554 nvbuf = malloc(nvsz);
4555 if (nvbuf == NULL) {
4556 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4560 zc.zc_nvlist_dst_size = nvsz;
4561 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4563 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4565 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4566 (void) snprintf(errbuf, sizeof (errbuf),
4567 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4572 nvsz = zc.zc_nvlist_dst_size;
4576 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4577 "pool must be upgraded"));
4578 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4581 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4584 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4587 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4592 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4594 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4595 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4597 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4607 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4609 zfs_cmd_t zc = { 0 };
4610 libzfs_handle_t *hdl = zhp->zfs_hdl;
4616 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4617 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4619 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4622 nvbuf = malloc(nvsz);
4624 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4627 zc.zc_nvlist_src_size = nvsz;
4628 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4629 zc.zc_perm_action = un;
4631 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4633 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4634 (void) snprintf(errbuf, sizeof (errbuf),
4635 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4640 "pool must be upgraded"));
4641 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4644 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4647 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4650 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4661 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4666 err = lzc_get_holds(zhp->zfs_name, nvl);
4669 libzfs_handle_t *hdl = zhp->zfs_hdl;
4671 (void) snprintf(errbuf, sizeof (errbuf),
4672 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4677 "pool must be upgraded"));
4678 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4681 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4684 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4687 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4696 * Convert the zvol's volume size to an appropriate reservation.
4697 * Note: If this routine is updated, it is necessary to update the ZFS test
4698 * suite's shell version in reservation.kshlib.
4701 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4704 uint64_t nblocks, volblocksize;
4708 if (nvlist_lookup_string(props,
4709 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4710 ncopies = atoi(strval);
4713 if (nvlist_lookup_uint64(props,
4714 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4715 &volblocksize) != 0)
4716 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4717 nblocks = volsize/volblocksize;
4718 /* start with metadnode L0-L6 */
4720 /* calculate number of indirects */
4721 while (nblocks > 1) {
4722 nblocks += DNODES_PER_LEVEL - 1;
4723 nblocks /= DNODES_PER_LEVEL;
4726 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4729 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4730 * compressed, but in practice they compress down to about
4733 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4739 * Attach/detach the given filesystem to/from the given jail.
4742 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4744 libzfs_handle_t *hdl = zhp->zfs_hdl;
4745 zfs_cmd_t zc = { 0 };
4751 (void) snprintf(errbuf, sizeof (errbuf),
4752 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4754 (void) snprintf(errbuf, sizeof (errbuf),
4755 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
4758 switch (zhp->zfs_type) {
4759 case ZFS_TYPE_VOLUME:
4760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4761 "volumes can not be jailed"));
4762 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4763 case ZFS_TYPE_SNAPSHOT:
4764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4765 "snapshots can not be jailed"));
4766 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4768 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4770 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4771 zc.zc_objset_type = DMU_OST_ZFS;
4772 zc.zc_jailid = jailid;
4774 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4775 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4776 zfs_standard_error(hdl, errno, errbuf);