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) 2018, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2017 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017-2018 RackTop Systems.
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
57 #include <sys/dnode.h>
63 #include "zfs_namecheck.h"
65 #include "libzfs_impl.h"
66 #include "zfs_deleg.h"
68 static int userquota_propname_decode(const char *propname, boolean_t zoned,
69 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
72 * Given a single type (not a mask of types), return the type in a human
76 zfs_type_to_name(zfs_type_t type)
79 case ZFS_TYPE_FILESYSTEM:
80 return (dgettext(TEXT_DOMAIN, "filesystem"));
81 case ZFS_TYPE_SNAPSHOT:
82 return (dgettext(TEXT_DOMAIN, "snapshot"));
84 return (dgettext(TEXT_DOMAIN, "volume"));
86 return (dgettext(TEXT_DOMAIN, "pool"));
87 case ZFS_TYPE_BOOKMARK:
88 return (dgettext(TEXT_DOMAIN, "bookmark"));
90 assert(!"unhandled zfs_type_t");
97 * Validate a ZFS path. This is used even before trying to open the dataset, to
98 * provide a more meaningful error message. We call zfs_error_aux() to
99 * explain exactly why the name was not valid.
102 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
108 if (entity_namecheck(path, &why, &what) != 0) {
111 case NAME_ERR_TOOLONG:
112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
113 "name is too long"));
116 case NAME_ERR_LEADING_SLASH:
117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 "leading slash in name"));
121 case NAME_ERR_EMPTY_COMPONENT:
122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
123 "empty component in name"));
126 case NAME_ERR_TRAILING_SLASH:
127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
128 "trailing slash in name"));
131 case NAME_ERR_INVALCHAR:
133 dgettext(TEXT_DOMAIN, "invalid character "
134 "'%c' in name"), what);
137 case NAME_ERR_MULTIPLE_DELIMITERS:
138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139 "multiple '@' and/or '#' delimiters in "
143 case NAME_ERR_NOLETTER:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "pool doesn't begin with a letter"));
148 case NAME_ERR_RESERVED:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "name is reserved"));
153 case NAME_ERR_DISKLIKE:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "reserved disk name"));
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "(%d) not defined"), why);
168 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 "snapshot delimiter '@' is not expected here"));
175 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178 "missing '@' delimiter in snapshot name"));
182 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185 "bookmark delimiter '#' is not expected here"));
189 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
192 "missing '#' delimiter in bookmark name"));
196 if (modifying && strchr(path, '%') != NULL) {
198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
199 "invalid character %c in name"), '%');
207 zfs_name_valid(const char *name, zfs_type_t type)
209 if (type == ZFS_TYPE_POOL)
210 return (zpool_name_valid(NULL, B_FALSE, name));
211 return (zfs_validate_name(NULL, name, type, B_FALSE));
215 * This function takes the raw DSL properties, and filters out the user-defined
216 * properties into a separate nvlist.
219 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
221 libzfs_handle_t *hdl = zhp->zfs_hdl;
226 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
227 (void) no_memory(hdl);
232 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
233 if (!zfs_prop_user(nvpair_name(elem)))
236 verify(nvpair_value_nvlist(elem, &propval) == 0);
237 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
239 (void) no_memory(hdl);
247 static zpool_handle_t *
248 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
250 libzfs_handle_t *hdl = zhp->zfs_hdl;
253 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
254 if (hdl->libzfs_pool_handles != NULL)
255 zph->zpool_next = hdl->libzfs_pool_handles;
256 hdl->libzfs_pool_handles = zph;
261 static zpool_handle_t *
262 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
264 libzfs_handle_t *hdl = zhp->zfs_hdl;
265 zpool_handle_t *zph = hdl->libzfs_pool_handles;
267 while ((zph != NULL) &&
268 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
269 zph = zph->zpool_next;
274 * Returns a handle to the pool that contains the provided dataset.
275 * If a handle to that pool already exists then that handle is returned.
276 * Otherwise, a new handle is created and added to the list of handles.
278 static zpool_handle_t *
279 zpool_handle(zfs_handle_t *zhp)
285 len = strcspn(zhp->zfs_name, "/@#") + 1;
286 pool_name = zfs_alloc(zhp->zfs_hdl, len);
287 (void) strlcpy(pool_name, zhp->zfs_name, len);
289 zph = zpool_find_handle(zhp, pool_name, len);
291 zph = zpool_add_handle(zhp, pool_name);
298 zpool_free_handles(libzfs_handle_t *hdl)
300 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
302 while (zph != NULL) {
303 next = zph->zpool_next;
307 hdl->libzfs_pool_handles = NULL;
311 * Utility function to gather stats (objset and zpl) for the given object.
314 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
316 libzfs_handle_t *hdl = zhp->zfs_hdl;
318 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
320 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
321 if (errno == ENOMEM) {
322 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
333 * Utility function to get the received properties of the given object.
336 get_recvd_props_ioctl(zfs_handle_t *zhp)
338 libzfs_handle_t *hdl = zhp->zfs_hdl;
339 nvlist_t *recvdprops;
340 zfs_cmd_t zc = { 0 };
343 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
346 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
348 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
349 if (errno == ENOMEM) {
350 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
354 zcmd_free_nvlists(&zc);
359 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
360 zcmd_free_nvlists(&zc);
364 nvlist_free(zhp->zfs_recvd_props);
365 zhp->zfs_recvd_props = recvdprops;
371 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
373 nvlist_t *allprops, *userprops;
375 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
377 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
382 * XXX Why do we store the user props separately, in addition to
383 * storing them in zfs_props?
385 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
386 nvlist_free(allprops);
390 nvlist_free(zhp->zfs_props);
391 nvlist_free(zhp->zfs_user_props);
393 zhp->zfs_props = allprops;
394 zhp->zfs_user_props = userprops;
400 get_stats(zfs_handle_t *zhp)
403 zfs_cmd_t zc = { 0 };
405 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
407 if (get_stats_ioctl(zhp, &zc) != 0)
409 else if (put_stats_zhdl(zhp, &zc) != 0)
411 zcmd_free_nvlists(&zc);
416 * Refresh the properties currently stored in the handle.
419 zfs_refresh_properties(zfs_handle_t *zhp)
421 (void) get_stats(zhp);
425 * Makes a handle from the given dataset name. Used by zfs_open() and
426 * zfs_iter_* to create child handles on the fly.
429 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
431 if (put_stats_zhdl(zhp, zc) != 0)
435 * We've managed to open the dataset and gather statistics. Determine
436 * the high-level type.
438 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
439 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
440 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
441 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
445 if (zhp->zfs_dmustats.dds_is_snapshot)
446 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
447 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
448 zhp->zfs_type = ZFS_TYPE_VOLUME;
449 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
450 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
452 abort(); /* we should never see any other types */
454 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
461 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
463 zfs_cmd_t zc = { 0 };
465 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
471 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
472 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
476 if (get_stats_ioctl(zhp, &zc) == -1) {
477 zcmd_free_nvlists(&zc);
481 if (make_dataset_handle_common(zhp, &zc) == -1) {
485 zcmd_free_nvlists(&zc);
490 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
492 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
498 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
499 if (make_dataset_handle_common(zhp, zc) == -1) {
507 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
509 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
514 zhp->zfs_hdl = pzhp->zfs_hdl;
515 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
516 zhp->zfs_head_type = pzhp->zfs_type;
517 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
518 zhp->zpool_hdl = zpool_handle(zhp);
523 zfs_handle_dup(zfs_handle_t *zhp_orig)
525 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
530 zhp->zfs_hdl = zhp_orig->zfs_hdl;
531 zhp->zpool_hdl = zhp_orig->zpool_hdl;
532 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
533 sizeof (zhp->zfs_name));
534 zhp->zfs_type = zhp_orig->zfs_type;
535 zhp->zfs_head_type = zhp_orig->zfs_head_type;
536 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
537 if (zhp_orig->zfs_props != NULL) {
538 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
539 (void) no_memory(zhp->zfs_hdl);
544 if (zhp_orig->zfs_user_props != NULL) {
545 if (nvlist_dup(zhp_orig->zfs_user_props,
546 &zhp->zfs_user_props, 0) != 0) {
547 (void) no_memory(zhp->zfs_hdl);
552 if (zhp_orig->zfs_recvd_props != NULL) {
553 if (nvlist_dup(zhp_orig->zfs_recvd_props,
554 &zhp->zfs_recvd_props, 0)) {
555 (void) no_memory(zhp->zfs_hdl);
560 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
561 if (zhp_orig->zfs_mntopts != NULL) {
562 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
563 zhp_orig->zfs_mntopts);
565 zhp->zfs_props_table = zhp_orig->zfs_props_table;
570 zfs_bookmark_exists(const char *path)
574 char fsname[ZFS_MAX_DATASET_NAME_LEN];
581 (void) strlcpy(fsname, path, sizeof (fsname));
582 pound = strchr(fsname, '#');
587 bmark_name = pound + 1;
588 props = fnvlist_alloc();
589 err = lzc_get_bookmarks(fsname, props, &bmarks);
596 rv = nvlist_exists(bmarks, bmark_name);
602 make_bookmark_handle(zfs_handle_t *parent, const char *path,
603 nvlist_t *bmark_props)
605 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
610 /* Fill in the name. */
611 zhp->zfs_hdl = parent->zfs_hdl;
612 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
614 /* Set the property lists. */
615 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
621 zhp->zfs_head_type = parent->zfs_head_type;
622 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
624 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
625 nvlist_free(zhp->zfs_props);
633 struct zfs_open_bookmarks_cb_data {
639 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
641 struct zfs_open_bookmarks_cb_data *dp = data;
644 * Is it the one we are looking for?
646 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
648 * We found it. Save it and let the caller know we are done.
655 * Not found. Close the handle and ask for another one.
662 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
663 * argument is a mask of acceptable types. The function will print an
664 * appropriate error message and return NULL if it can't be opened.
667 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
673 (void) snprintf(errbuf, sizeof (errbuf),
674 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
677 * Validate the name before we even try to open it.
679 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
680 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
685 * Bookmarks needs to be handled separately.
687 bookp = strchr(path, '#');
690 * Try to get stats for the dataset, which will tell us if it
694 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
695 (void) zfs_standard_error(hdl, errno, errbuf);
699 char dsname[ZFS_MAX_DATASET_NAME_LEN];
701 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
704 * We need to cut out '#' and everything after '#'
705 * to get the parent dataset name only.
707 assert(bookp - path < sizeof (dsname));
708 (void) strncpy(dsname, path, bookp - path);
709 dsname[bookp - path] = '\0';
712 * Create handle for the parent dataset.
715 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
716 (void) zfs_standard_error(hdl, errno, errbuf);
721 * Iterate bookmarks to find the right one.
724 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
725 &cb_data) == 0) && (cb_data.zhp == NULL)) {
726 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
730 if (cb_data.zhp == NULL) {
731 (void) zfs_standard_error(hdl, errno, errbuf);
744 char *at = strchr(path, '@');
749 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
750 (void) zfs_standard_error(hdl, errno, errbuf);
755 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
756 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
759 if (!(types & zhp->zfs_type)) {
760 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
769 * Release a ZFS handle. Nothing to do but free the associated memory.
772 zfs_close(zfs_handle_t *zhp)
774 if (zhp->zfs_mntopts)
775 free(zhp->zfs_mntopts);
776 nvlist_free(zhp->zfs_props);
777 nvlist_free(zhp->zfs_user_props);
778 nvlist_free(zhp->zfs_recvd_props);
782 typedef struct mnttab_node {
783 struct mnttab mtn_mt;
788 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
790 const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
791 const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
794 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
796 return (AVL_ISIGN(rv));
800 libzfs_mnttab_init(libzfs_handle_t *hdl)
802 pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL);
803 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
804 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
805 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
809 libzfs_mnttab_update(libzfs_handle_t *hdl)
813 rewind(hdl->libzfs_mnttab);
814 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
817 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
819 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
820 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
821 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
822 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
823 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
824 avl_add(&hdl->libzfs_mnttab_cache, mtn);
829 libzfs_mnttab_fini(libzfs_handle_t *hdl)
834 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
836 free(mtn->mtn_mt.mnt_special);
837 free(mtn->mtn_mt.mnt_mountp);
838 free(mtn->mtn_mt.mnt_fstype);
839 free(mtn->mtn_mt.mnt_mntopts);
842 avl_destroy(&hdl->libzfs_mnttab_cache);
843 (void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
847 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
849 hdl->libzfs_mnttab_enable = enable;
853 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
854 struct mnttab *entry)
860 if (!hdl->libzfs_mnttab_enable) {
861 struct mnttab srch = { 0 };
863 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
864 libzfs_mnttab_fini(hdl);
865 rewind(hdl->libzfs_mnttab);
866 srch.mnt_special = (char *)fsname;
867 srch.mnt_fstype = MNTTYPE_ZFS;
868 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
874 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
875 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
876 libzfs_mnttab_update(hdl);
878 find.mtn_mt.mnt_special = (char *)fsname;
879 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
881 *entry = mtn->mtn_mt;
884 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
889 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
890 const char *mountp, const char *mntopts)
894 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
895 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) {
896 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
897 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
898 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
899 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
900 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
901 avl_add(&hdl->libzfs_mnttab_cache, mtn);
903 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
907 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
912 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
913 find.mtn_mt.mnt_special = (char *)fsname;
914 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
916 avl_remove(&hdl->libzfs_mnttab_cache, ret);
917 free(ret->mtn_mt.mnt_special);
918 free(ret->mtn_mt.mnt_mountp);
919 free(ret->mtn_mt.mnt_fstype);
920 free(ret->mtn_mt.mnt_mntopts);
923 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
927 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
929 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
931 if (zpool_handle == NULL)
934 *spa_version = zpool_get_prop_int(zpool_handle,
935 ZPOOL_PROP_VERSION, NULL);
940 * The choice of reservation property depends on the SPA version.
943 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
947 if (zfs_spa_version(zhp, &spa_version) < 0)
950 if (spa_version >= SPA_VERSION_REFRESERVATION)
951 *resv_prop = ZFS_PROP_REFRESERVATION;
953 *resv_prop = ZFS_PROP_RESERVATION;
959 * Given an nvlist of properties to set, validates that they are correct, and
960 * parses any numeric properties (index, boolean, etc) if they are specified as
964 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
965 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
973 int chosen_normal = -1;
976 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
977 (void) no_memory(hdl);
982 * Make sure this property is valid and applies to this type.
986 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
987 const char *propname = nvpair_name(elem);
989 prop = zfs_name_to_prop(propname);
990 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
992 * This is a user property: make sure it's a
993 * string, and that it's less than ZAP_MAXNAMELEN.
995 if (nvpair_type(elem) != DATA_TYPE_STRING) {
996 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
997 "'%s' must be a string"), propname);
998 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1002 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
1003 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1004 "property name '%s' is too long"),
1006 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1010 (void) nvpair_value_string(elem, &strval);
1011 if (nvlist_add_string(ret, propname, strval) != 0) {
1012 (void) no_memory(hdl);
1019 * Currently, only user properties can be modified on
1022 if (type == ZFS_TYPE_SNAPSHOT) {
1023 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1024 "this property can not be modified for snapshots"));
1025 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1029 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1030 zfs_userquota_prop_t uqtype;
1031 char newpropname[128];
1036 if (userquota_propname_decode(propname, zoned,
1037 &uqtype, domain, sizeof (domain), &rid) != 0) {
1039 dgettext(TEXT_DOMAIN,
1040 "'%s' has an invalid user/group name"),
1042 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1046 if (uqtype != ZFS_PROP_USERQUOTA &&
1047 uqtype != ZFS_PROP_GROUPQUOTA) {
1049 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1051 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1056 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1057 (void) nvpair_value_string(elem, &strval);
1058 if (strcmp(strval, "none") == 0) {
1060 } else if (zfs_nicestrtonum(hdl,
1061 strval, &intval) != 0) {
1062 (void) zfs_error(hdl,
1063 EZFS_BADPROP, errbuf);
1066 } else if (nvpair_type(elem) ==
1068 (void) nvpair_value_uint64(elem, &intval);
1070 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1071 "use 'none' to disable "
1072 "userquota/groupquota"));
1076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1077 "'%s' must be a number"), propname);
1078 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1083 * Encode the prop name as
1084 * userquota@<hex-rid>-domain, to make it easy
1085 * for the kernel to decode.
1087 (void) snprintf(newpropname, sizeof (newpropname),
1088 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1089 (longlong_t)rid, domain);
1093 if (nvlist_add_uint64_array(ret, newpropname,
1095 (void) no_memory(hdl);
1099 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1101 "'%s' is readonly"),
1103 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1107 if (prop == ZPROP_INVAL) {
1108 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1109 "invalid property '%s'"), propname);
1110 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1114 if (!zfs_prop_valid_for_type(prop, type)) {
1116 dgettext(TEXT_DOMAIN, "'%s' does not "
1117 "apply to datasets of this type"), propname);
1118 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1122 if (zfs_prop_readonly(prop) &&
1123 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1125 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1127 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1131 if (zprop_parse_value(hdl, elem, prop, type, ret,
1132 &strval, &intval, errbuf) != 0)
1136 * Perform some additional checks for specific properties.
1139 case ZFS_PROP_VERSION:
1145 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1146 if (intval < version) {
1147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1148 "Can not downgrade; already at version %u"),
1150 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1156 case ZFS_PROP_VOLBLOCKSIZE:
1157 case ZFS_PROP_RECORDSIZE:
1159 int maxbs = SPA_MAXBLOCKSIZE;
1160 if (zpool_hdl != NULL) {
1161 maxbs = zpool_get_prop_int(zpool_hdl,
1162 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1165 * Volumes are limited to a volblocksize of 128KB,
1166 * because they typically service workloads with
1167 * small random writes, which incur a large performance
1168 * penalty with large blocks.
1170 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1171 maxbs = SPA_OLD_MAXBLOCKSIZE;
1173 * The value must be a power of two between
1174 * SPA_MINBLOCKSIZE and maxbs.
1176 if (intval < SPA_MINBLOCKSIZE ||
1177 intval > maxbs || !ISP2(intval)) {
1178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1179 "'%s' must be power of 2 from 512B "
1180 "to %uKB"), propname, maxbs >> 10);
1181 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1186 case ZFS_PROP_MLSLABEL:
1190 * Verify the mlslabel string and convert to
1191 * internal hex label string.
1195 char *hex = NULL; /* internal label string */
1197 /* Default value is already OK. */
1198 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1201 /* Verify the label can be converted to binary form */
1202 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1203 (str_to_label(strval, &new_sl, MAC_LABEL,
1204 L_NO_CORRECTION, NULL) == -1)) {
1208 /* Now translate to hex internal label string */
1209 if (label_to_str(new_sl, &hex, M_INTERNAL,
1215 m_label_free(new_sl);
1217 /* If string is already in internal form, we're done. */
1218 if (strcmp(strval, hex) == 0) {
1223 /* Replace the label string with the internal form. */
1224 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1226 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1234 "invalid mlslabel '%s'"), strval);
1235 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1236 m_label_free(new_sl); /* OK if null */
1237 #else /* !illumos */
1238 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1239 "mlslabel is not supported on FreeBSD"));
1240 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1241 #endif /* illumos */
1246 case ZFS_PROP_MOUNTPOINT:
1248 namecheck_err_t why;
1250 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1251 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1254 if (mountpoint_namecheck(strval, &why)) {
1256 case NAME_ERR_LEADING_SLASH:
1258 dgettext(TEXT_DOMAIN,
1259 "'%s' must be an absolute path, "
1260 "'none', or 'legacy'"), propname);
1262 case NAME_ERR_TOOLONG:
1264 dgettext(TEXT_DOMAIN,
1265 "component of '%s' is too long"),
1271 dgettext(TEXT_DOMAIN,
1272 "(%d) not defined"),
1276 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1283 case ZFS_PROP_SHARESMB:
1284 case ZFS_PROP_SHARENFS:
1286 * For the mountpoint and sharenfs or sharesmb
1287 * properties, check if it can be set in a
1288 * global/non-global zone based on
1289 * the zoned property value:
1291 * global zone non-global zone
1292 * --------------------------------------------------
1293 * zoned=on mountpoint (no) mountpoint (yes)
1294 * sharenfs (no) sharenfs (no)
1295 * sharesmb (no) sharesmb (no)
1297 * zoned=off mountpoint (yes) N/A
1302 if (getzoneid() == GLOBAL_ZONEID) {
1303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1304 "'%s' cannot be set on "
1305 "dataset in a non-global zone"),
1307 (void) zfs_error(hdl, EZFS_ZONED,
1310 } else if (prop == ZFS_PROP_SHARENFS ||
1311 prop == ZFS_PROP_SHARESMB) {
1312 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1313 "'%s' cannot be set in "
1314 "a non-global zone"), propname);
1315 (void) zfs_error(hdl, EZFS_ZONED,
1319 } else if (getzoneid() != GLOBAL_ZONEID) {
1321 * If zoned property is 'off', this must be in
1322 * a global zone. If not, something is wrong.
1324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1325 "'%s' cannot be set while dataset "
1326 "'zoned' property is set"), propname);
1327 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1332 * At this point, it is legitimate to set the
1333 * property. Now we want to make sure that the
1334 * property value is valid if it is sharenfs.
1336 if ((prop == ZFS_PROP_SHARENFS ||
1337 prop == ZFS_PROP_SHARESMB) &&
1338 strcmp(strval, "on") != 0 &&
1339 strcmp(strval, "off") != 0) {
1340 zfs_share_proto_t proto;
1342 if (prop == ZFS_PROP_SHARESMB)
1348 * Must be an valid sharing protocol
1349 * option string so init the libshare
1350 * in order to enable the parser and
1351 * then parse the options. We use the
1352 * control API since we don't care about
1353 * the current configuration and don't
1354 * want the overhead of loading it
1355 * until we actually do something.
1358 if (zfs_init_libshare(hdl,
1359 SA_INIT_CONTROL_API) != SA_OK) {
1361 * An error occurred so we can't do
1364 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1365 "'%s' cannot be set: problem "
1366 "in share initialization"),
1368 (void) zfs_error(hdl, EZFS_BADPROP,
1373 if (zfs_parse_options(strval, proto) != SA_OK) {
1375 * There was an error in parsing so
1376 * deal with it by issuing an error
1377 * message and leaving after
1378 * uninitializing the the libshare
1381 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1382 "'%s' cannot be set to invalid "
1383 "options"), propname);
1384 (void) zfs_error(hdl, EZFS_BADPROP,
1386 zfs_uninit_libshare(hdl);
1389 zfs_uninit_libshare(hdl);
1394 case ZFS_PROP_UTF8ONLY:
1395 chosen_utf = (int)intval;
1398 case ZFS_PROP_NORMALIZE:
1399 chosen_normal = (int)intval;
1407 * For changes to existing volumes, we have some additional
1408 * checks to enforce.
1410 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1411 uint64_t volsize = zfs_prop_get_int(zhp,
1413 uint64_t blocksize = zfs_prop_get_int(zhp,
1414 ZFS_PROP_VOLBLOCKSIZE);
1418 case ZFS_PROP_RESERVATION:
1419 if (intval > volsize) {
1420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1421 "'%s' is greater than current "
1422 "volume size"), propname);
1423 (void) zfs_error(hdl, EZFS_BADPROP,
1429 case ZFS_PROP_REFRESERVATION:
1430 if (intval > volsize && intval != UINT64_MAX) {
1431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432 "'%s' is greater than current "
1433 "volume size"), propname);
1434 (void) zfs_error(hdl, EZFS_BADPROP,
1440 case ZFS_PROP_VOLSIZE:
1441 if (intval % blocksize != 0) {
1442 zfs_nicenum(blocksize, buf,
1444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1445 "'%s' must be a multiple of "
1446 "volume block size (%s)"),
1448 (void) zfs_error(hdl, EZFS_BADPROP,
1454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1455 "'%s' cannot be zero"),
1457 (void) zfs_error(hdl, EZFS_BADPROP,
1470 * If normalization was chosen, but no UTF8 choice was made,
1471 * enforce rejection of non-UTF8 names.
1473 * If normalization was chosen, but rejecting non-UTF8 names
1474 * was explicitly not chosen, it is an error.
1476 if (chosen_normal > 0 && chosen_utf < 0) {
1477 if (nvlist_add_uint64(ret,
1478 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1479 (void) no_memory(hdl);
1482 } else if (chosen_normal > 0 && chosen_utf == 0) {
1483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1484 "'%s' must be set 'on' if normalization chosen"),
1485 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1486 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1497 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1499 uint64_t old_volsize;
1500 uint64_t new_volsize;
1501 uint64_t old_reservation;
1502 uint64_t new_reservation;
1503 zfs_prop_t resv_prop;
1507 * If this is an existing volume, and someone is setting the volsize,
1508 * make sure that it matches the reservation, or add it if necessary.
1510 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1511 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1513 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1515 props = fnvlist_alloc();
1516 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1517 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1519 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1520 old_reservation) || nvlist_exists(nvl,
1521 zfs_prop_to_name(resv_prop))) {
1522 fnvlist_free(props);
1525 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1526 &new_volsize) != 0) {
1527 fnvlist_free(props);
1530 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1531 fnvlist_free(props);
1533 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1534 new_reservation) != 0) {
1535 (void) no_memory(zhp->zfs_hdl);
1542 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1543 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1544 * Return codes must match zfs_add_synthetic_resv().
1547 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1554 if (!ZFS_IS_VOLUME(zhp)) {
1558 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1562 if (prop != ZFS_PROP_REFRESERVATION) {
1566 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1567 /* No value being set, so it can't be "auto" */
1570 if (resvsize != UINT64_MAX) {
1571 /* Being set to a value other than "auto" */
1575 props = fnvlist_alloc();
1577 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1578 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1580 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1582 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1585 resvsize = zvol_volsize_to_reservation(volsize, props);
1586 fnvlist_free(props);
1588 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1589 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1590 (void) no_memory(zhp->zfs_hdl);
1597 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1604 * For quotas and reservations, ENOSPC indicates
1605 * something different; setting a quota or reservation
1606 * doesn't use any disk space.
1609 case ZFS_PROP_QUOTA:
1610 case ZFS_PROP_REFQUOTA:
1611 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1612 "size is less than current used or "
1614 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1617 case ZFS_PROP_RESERVATION:
1618 case ZFS_PROP_REFRESERVATION:
1619 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1620 "size is greater than available space"));
1621 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1625 (void) zfs_standard_error(hdl, err, errbuf);
1631 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1635 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1640 "property value too long"));
1641 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1645 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1646 "pool and or dataset must be upgraded to set this "
1647 "property or value"));
1648 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1653 if (prop == ZFS_PROP_COMPRESSION ||
1654 prop == ZFS_PROP_RECORDSIZE) {
1655 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1656 "property setting is not allowed on "
1657 "bootable datasets"));
1658 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1659 } else if (prop == ZFS_PROP_CHECKSUM ||
1660 prop == ZFS_PROP_DEDUP) {
1661 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1662 "property setting is not allowed on "
1664 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1666 (void) zfs_standard_error(hdl, err, errbuf);
1671 if (prop == ZPROP_INVAL) {
1672 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1674 (void) zfs_standard_error(hdl, err, errbuf);
1680 * This platform can't address a volume this big.
1683 if (prop == ZFS_PROP_VOLSIZE) {
1684 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1690 (void) zfs_standard_error(hdl, err, errbuf);
1695 * Given a property name and value, set the property for the given dataset.
1698 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1702 libzfs_handle_t *hdl = zhp->zfs_hdl;
1703 nvlist_t *nvl = NULL;
1705 (void) snprintf(errbuf, sizeof (errbuf),
1706 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1709 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1710 nvlist_add_string(nvl, propname, propval) != 0) {
1711 (void) no_memory(hdl);
1715 ret = zfs_prop_set_list(zhp, nvl);
1725 * Given an nvlist of property names and values, set the properties for the
1729 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1731 zfs_cmd_t zc = { 0 };
1733 prop_changelist_t **cls = NULL;
1736 libzfs_handle_t *hdl = zhp->zfs_hdl;
1741 (void) snprintf(errbuf, sizeof (errbuf),
1742 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1745 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1746 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1751 * We have to check for any extra properties which need to be added
1752 * before computing the length of the nvlist.
1754 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1756 elem = nvlist_next_nvpair(nvl, elem)) {
1757 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1758 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1763 if (added_resv != 1 &&
1764 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1769 * Check how many properties we're setting and allocate an array to
1770 * store changelist pointers for postfix().
1773 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1775 elem = nvlist_next_nvpair(nvl, elem))
1777 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1781 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1783 elem = nvlist_next_nvpair(nvl, elem)) {
1785 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1787 assert(cl_idx < nvl_len);
1789 * We don't want to unmount & remount the dataset when changing
1790 * its canmount property to 'on' or 'noauto'. We only use
1791 * the changelist logic to unmount when setting canmount=off.
1793 if (prop != ZFS_PROP_CANMOUNT ||
1794 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1795 zfs_is_mounted(zhp, NULL))) {
1796 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1797 if (cls[cl_idx] == NULL)
1801 if (prop == ZFS_PROP_MOUNTPOINT &&
1802 changelist_haszonedchild(cls[cl_idx])) {
1803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1804 "child dataset with inherited mountpoint is used "
1805 "in a non-global zone"));
1806 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1810 /* We don't support those properties on FreeBSD. */
1812 case ZFS_PROP_DEVICES:
1813 case ZFS_PROP_ISCSIOPTIONS:
1814 case ZFS_PROP_XATTR:
1815 case ZFS_PROP_VSCAN:
1816 case ZFS_PROP_NBMAND:
1817 case ZFS_PROP_MLSLABEL:
1818 (void) snprintf(errbuf, sizeof (errbuf),
1819 "property '%s' not supported on FreeBSD",
1821 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1825 if (cls[cl_idx] != NULL &&
1826 (ret = changelist_prefix(cls[cl_idx])) != 0)
1831 assert(cl_idx == nvl_len);
1834 * Execute the corresponding ioctl() to set this list of properties.
1836 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1838 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1839 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1842 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1845 if (zc.zc_nvlist_dst_filled == B_FALSE) {
1846 (void) zfs_standard_error(hdl, errno, errbuf);
1850 /* Get the list of unset properties back and report them. */
1851 nvlist_t *errorprops = NULL;
1852 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1854 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1856 elem = nvlist_next_nvpair(errorprops, elem)) {
1857 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1858 zfs_setprop_error(hdl, prop, errno, errbuf);
1860 nvlist_free(errorprops);
1862 if (added_resv && errno == ENOSPC) {
1863 /* clean up the volsize property we tried to set */
1864 uint64_t old_volsize = zfs_prop_get_int(zhp,
1868 zcmd_free_nvlists(&zc);
1870 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1872 if (nvlist_add_uint64(nvl,
1873 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1876 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1878 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1881 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1882 if (cls[cl_idx] != NULL) {
1883 int clp_err = changelist_postfix(cls[cl_idx]);
1890 * Refresh the statistics so the new property value
1894 (void) get_stats(zhp);
1899 zcmd_free_nvlists(&zc);
1901 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1902 if (cls[cl_idx] != NULL)
1903 changelist_free(cls[cl_idx]);
1911 * Given a property, inherit the value from the parent dataset, or if received
1912 * is TRUE, revert to the received value, if any.
1915 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1917 zfs_cmd_t zc = { 0 };
1919 prop_changelist_t *cl;
1920 libzfs_handle_t *hdl = zhp->zfs_hdl;
1924 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1925 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1927 zc.zc_cookie = received;
1928 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1930 * For user properties, the amount of work we have to do is very
1931 * small, so just do it here.
1933 if (!zfs_prop_user(propname)) {
1934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1935 "invalid property"));
1936 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1939 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1940 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1942 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1943 return (zfs_standard_error(hdl, errno, errbuf));
1949 * Verify that this property is inheritable.
1951 if (zfs_prop_readonly(prop))
1952 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1954 if (!zfs_prop_inheritable(prop) && !received)
1955 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1958 * Check to see if the value applies to this type
1960 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1961 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1964 * Normalize the name, to get rid of shorthand abbreviations.
1966 propname = zfs_prop_to_name(prop);
1967 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1968 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1970 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1971 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1972 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1973 "dataset is used in a non-global zone"));
1974 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1978 * Determine datasets which will be affected by this change, if any.
1980 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1983 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1985 "child dataset with inherited mountpoint is used "
1986 "in a non-global zone"));
1987 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1991 if ((ret = changelist_prefix(cl)) != 0)
1994 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1995 return (zfs_standard_error(hdl, errno, errbuf));
1998 if ((ret = changelist_postfix(cl)) != 0)
2002 * Refresh the statistics so the new property is reflected.
2004 (void) get_stats(zhp);
2008 changelist_free(cl);
2013 * True DSL properties are stored in an nvlist. The following two functions
2014 * extract them appropriately.
2017 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2023 if (nvlist_lookup_nvlist(zhp->zfs_props,
2024 zfs_prop_to_name(prop), &nv) == 0) {
2025 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2026 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2028 verify(!zhp->zfs_props_table ||
2029 zhp->zfs_props_table[prop] == B_TRUE);
2030 value = zfs_prop_default_numeric(prop);
2038 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2044 if (nvlist_lookup_nvlist(zhp->zfs_props,
2045 zfs_prop_to_name(prop), &nv) == 0) {
2046 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2047 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2049 verify(!zhp->zfs_props_table ||
2050 zhp->zfs_props_table[prop] == B_TRUE);
2051 value = zfs_prop_default_string(prop);
2059 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2061 return (zhp->zfs_props == zhp->zfs_recvd_props);
2065 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2067 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2068 zhp->zfs_props = zhp->zfs_recvd_props;
2072 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2074 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2079 * Internal function for getting a numeric property. Both zfs_prop_get() and
2080 * zfs_prop_get_int() are built using this interface.
2082 * Certain properties can be overridden using 'mount -o'. In this case, scan
2083 * the contents of the /etc/mnttab entry, searching for the appropriate options.
2084 * If they differ from the on-disk values, report the current values and mark
2085 * the source "temporary".
2088 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2089 char **source, uint64_t *val)
2091 zfs_cmd_t zc = { 0 };
2092 nvlist_t *zplprops = NULL;
2094 char *mntopt_on = NULL;
2095 char *mntopt_off = NULL;
2096 boolean_t received = zfs_is_recvd_props_mode(zhp);
2101 case ZFS_PROP_ATIME:
2102 mntopt_on = MNTOPT_ATIME;
2103 mntopt_off = MNTOPT_NOATIME;
2106 case ZFS_PROP_DEVICES:
2107 mntopt_on = MNTOPT_DEVICES;
2108 mntopt_off = MNTOPT_NODEVICES;
2112 mntopt_on = MNTOPT_EXEC;
2113 mntopt_off = MNTOPT_NOEXEC;
2116 case ZFS_PROP_READONLY:
2117 mntopt_on = MNTOPT_RO;
2118 mntopt_off = MNTOPT_RW;
2121 case ZFS_PROP_SETUID:
2122 mntopt_on = MNTOPT_SETUID;
2123 mntopt_off = MNTOPT_NOSETUID;
2126 case ZFS_PROP_XATTR:
2127 mntopt_on = MNTOPT_XATTR;
2128 mntopt_off = MNTOPT_NOXATTR;
2131 case ZFS_PROP_NBMAND:
2132 mntopt_on = MNTOPT_NBMAND;
2133 mntopt_off = MNTOPT_NONBMAND;
2141 * Because looking up the mount options is potentially expensive
2142 * (iterating over all of /etc/mnttab), we defer its calculation until
2143 * we're looking up a property which requires its presence.
2145 if (!zhp->zfs_mntcheck &&
2146 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2147 libzfs_handle_t *hdl = zhp->zfs_hdl;
2148 struct mnttab entry;
2150 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2151 zhp->zfs_mntopts = zfs_strdup(hdl,
2153 if (zhp->zfs_mntopts == NULL)
2157 zhp->zfs_mntcheck = B_TRUE;
2160 if (zhp->zfs_mntopts == NULL)
2161 mnt.mnt_mntopts = "";
2163 mnt.mnt_mntopts = zhp->zfs_mntopts;
2166 case ZFS_PROP_ATIME:
2167 case ZFS_PROP_DEVICES:
2169 case ZFS_PROP_READONLY:
2170 case ZFS_PROP_SETUID:
2171 case ZFS_PROP_XATTR:
2172 case ZFS_PROP_NBMAND:
2173 *val = getprop_uint64(zhp, prop, source);
2178 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2181 *src = ZPROP_SRC_TEMPORARY;
2182 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2185 *src = ZPROP_SRC_TEMPORARY;
2189 case ZFS_PROP_CANMOUNT:
2190 case ZFS_PROP_VOLSIZE:
2191 case ZFS_PROP_QUOTA:
2192 case ZFS_PROP_REFQUOTA:
2193 case ZFS_PROP_RESERVATION:
2194 case ZFS_PROP_REFRESERVATION:
2195 case ZFS_PROP_FILESYSTEM_LIMIT:
2196 case ZFS_PROP_SNAPSHOT_LIMIT:
2197 case ZFS_PROP_FILESYSTEM_COUNT:
2198 case ZFS_PROP_SNAPSHOT_COUNT:
2199 *val = getprop_uint64(zhp, prop, source);
2201 if (*source == NULL) {
2202 /* not default, must be local */
2203 *source = zhp->zfs_name;
2207 case ZFS_PROP_MOUNTED:
2208 *val = (zhp->zfs_mntopts != NULL);
2211 case ZFS_PROP_NUMCLONES:
2212 *val = zhp->zfs_dmustats.dds_num_clones;
2215 case ZFS_PROP_VERSION:
2216 case ZFS_PROP_NORMALIZE:
2217 case ZFS_PROP_UTF8ONLY:
2219 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2220 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2222 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2223 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2224 zcmd_free_nvlists(&zc);
2227 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2228 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2230 zcmd_free_nvlists(&zc);
2233 nvlist_free(zplprops);
2234 zcmd_free_nvlists(&zc);
2237 case ZFS_PROP_INCONSISTENT:
2238 *val = zhp->zfs_dmustats.dds_inconsistent;
2242 switch (zfs_prop_get_type(prop)) {
2243 case PROP_TYPE_NUMBER:
2244 case PROP_TYPE_INDEX:
2245 *val = getprop_uint64(zhp, prop, source);
2247 * If we tried to use a default value for a
2248 * readonly property, it means that it was not
2249 * present. Note this only applies to "truly"
2250 * readonly properties, not set-once properties
2251 * like volblocksize.
2253 if (zfs_prop_readonly(prop) &&
2254 !zfs_prop_setonce(prop) &&
2255 *source != NULL && (*source)[0] == '\0') {
2261 case PROP_TYPE_STRING:
2263 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2264 "cannot get non-numeric property"));
2265 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2266 dgettext(TEXT_DOMAIN, "internal error")));
2274 * Calculate the source type, given the raw source string.
2277 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2278 char *statbuf, size_t statlen)
2280 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2283 if (source == NULL) {
2284 *srctype = ZPROP_SRC_NONE;
2285 } else if (source[0] == '\0') {
2286 *srctype = ZPROP_SRC_DEFAULT;
2287 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2288 *srctype = ZPROP_SRC_RECEIVED;
2290 if (strcmp(source, zhp->zfs_name) == 0) {
2291 *srctype = ZPROP_SRC_LOCAL;
2293 (void) strlcpy(statbuf, source, statlen);
2294 *srctype = ZPROP_SRC_INHERITED;
2301 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2302 size_t proplen, boolean_t literal)
2307 if (zhp->zfs_recvd_props == NULL)
2308 if (get_recvd_props_ioctl(zhp) != 0)
2311 prop = zfs_name_to_prop(propname);
2313 if (prop != ZPROP_INVAL) {
2315 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2317 zfs_set_recvd_props_mode(zhp, &cookie);
2318 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2319 NULL, NULL, 0, literal);
2320 zfs_unset_recvd_props_mode(zhp, &cookie);
2324 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2325 propname, &propval) != 0)
2327 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2329 (void) strlcpy(propbuf, recvdval, proplen);
2332 return (err == 0 ? 0 : -1);
2336 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2341 value = zfs_get_clones_nvl(zhp);
2346 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2347 pair = nvlist_next_nvpair(value, pair)) {
2348 if (propbuf[0] != '\0')
2349 (void) strlcat(propbuf, ",", proplen);
2350 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2356 struct get_clones_arg {
2360 char buf[ZFS_MAX_DATASET_NAME_LEN];
2364 get_clones_cb(zfs_handle_t *zhp, void *arg)
2366 struct get_clones_arg *gca = arg;
2368 if (gca->numclones == 0) {
2373 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2374 NULL, NULL, 0, B_TRUE) != 0)
2376 if (strcmp(gca->buf, gca->origin) == 0) {
2377 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2382 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2388 zfs_get_clones_nvl(zfs_handle_t *zhp)
2390 nvlist_t *nv, *value;
2392 if (nvlist_lookup_nvlist(zhp->zfs_props,
2393 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2394 struct get_clones_arg gca;
2397 * if this is a snapshot, then the kernel wasn't able
2398 * to get the clones. Do it by slowly iterating.
2400 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2402 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2404 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2409 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2411 gca.origin = zhp->zfs_name;
2413 if (gca.numclones != 0) {
2415 char pool[ZFS_MAX_DATASET_NAME_LEN];
2418 /* get the pool name */
2419 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2420 (void) strsep(&cp, "/@");
2421 root = zfs_open(zhp->zfs_hdl, pool,
2422 ZFS_TYPE_FILESYSTEM);
2424 (void) get_clones_cb(root, &gca);
2427 if (gca.numclones != 0 ||
2428 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2429 nvlist_add_nvlist(zhp->zfs_props,
2430 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2437 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2438 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2441 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2447 * Accepts a property and value and checks that the value
2448 * matches the one found by the channel program. If they are
2449 * not equal, print both of them.
2452 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2455 if (!zhp->zfs_hdl->libzfs_prop_debug)
2458 char *poolname = zhp->zpool_hdl->zpool_name;
2459 const char *program =
2461 "ds = args['dataset']\n"
2462 "prop = args['property']\n"
2463 "value, setpoint = zfs.get_prop(ds, prop)\n"
2464 "return {value=value, setpoint=setpoint}\n";
2467 nvlist_t *argnvl = fnvlist_alloc();
2469 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2470 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2472 error = lzc_channel_program_nosync(poolname, program,
2473 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2476 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2477 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2479 error = nvlist_lookup_int64(retnvl, "value", &ans);
2481 (void) fprintf(stderr, "zcp check error: %u\n",
2485 if (ans != intval) {
2486 (void) fprintf(stderr,
2487 "%s: zfs found %lld, but zcp found %lld\n",
2488 zfs_prop_to_name(prop),
2489 (longlong_t)intval, (longlong_t)ans);
2493 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2495 (void) fprintf(stderr, "zcp check error: %u\n",
2499 if (strcmp(strval, str_ans) != 0) {
2500 (void) fprintf(stderr,
2501 "%s: zfs found %s, but zcp found %s\n",
2502 zfs_prop_to_name(prop),
2507 (void) fprintf(stderr,
2508 "zcp check failed, channel program error: %u\n", error);
2510 nvlist_free(argnvl);
2511 nvlist_free(outnvl);
2515 * Retrieve a property from the given object. If 'literal' is specified, then
2516 * numbers are left as exact values. Otherwise, numbers are converted to a
2517 * human-readable form.
2519 * Returns 0 on success, or -1 on error.
2522 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2523 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2525 char *source = NULL;
2529 boolean_t received = zfs_is_recvd_props_mode(zhp);
2532 * Check to see if this property applies to our object
2534 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2537 if (received && zfs_prop_readonly(prop))
2541 *src = ZPROP_SRC_NONE;
2544 case ZFS_PROP_CREATION:
2546 * 'creation' is a time_t stored in the statistics. We convert
2547 * this into a string unless 'literal' is specified.
2550 val = getprop_uint64(zhp, prop, &source);
2551 time_t time = (time_t)val;
2555 localtime_r(&time, &t) == NULL ||
2556 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2558 (void) snprintf(propbuf, proplen, "%llu", val);
2560 zcp_check(zhp, prop, val, NULL);
2563 case ZFS_PROP_MOUNTPOINT:
2565 * Getting the precise mountpoint can be tricky.
2567 * - for 'none' or 'legacy', return those values.
2568 * - for inherited mountpoints, we want to take everything
2569 * after our ancestor and append it to the inherited value.
2571 * If the pool has an alternate root, we want to prepend that
2572 * root to any values we return.
2575 str = getprop_string(zhp, prop, &source);
2577 if (str[0] == '/') {
2578 char buf[MAXPATHLEN];
2580 const char *relpath;
2583 * If we inherit the mountpoint, even from a dataset
2584 * with a received value, the source will be the path of
2585 * the dataset we inherit from. If source is
2586 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2589 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2592 relpath = zhp->zfs_name + strlen(source);
2593 if (relpath[0] == '/')
2597 if ((zpool_get_prop(zhp->zpool_hdl,
2598 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2599 B_FALSE)) || (strcmp(root, "-") == 0))
2602 * Special case an alternate root of '/'. This will
2603 * avoid having multiple leading slashes in the
2606 if (strcmp(root, "/") == 0)
2610 * If the mountpoint is '/' then skip over this
2611 * if we are obtaining either an alternate root or
2612 * an inherited mountpoint.
2614 if (str[1] == '\0' && (root[0] != '\0' ||
2615 relpath[0] != '\0'))
2618 if (relpath[0] == '\0')
2619 (void) snprintf(propbuf, proplen, "%s%s",
2622 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2623 root, str, relpath[0] == '@' ? "" : "/",
2626 /* 'legacy' or 'none' */
2627 (void) strlcpy(propbuf, str, proplen);
2629 zcp_check(zhp, prop, NULL, propbuf);
2632 case ZFS_PROP_ORIGIN:
2633 str = getprop_string(zhp, prop, &source);
2636 (void) strlcpy(propbuf, str, proplen);
2637 zcp_check(zhp, prop, NULL, str);
2640 case ZFS_PROP_CLONES:
2641 if (get_clones_string(zhp, propbuf, proplen) != 0)
2645 case ZFS_PROP_QUOTA:
2646 case ZFS_PROP_REFQUOTA:
2647 case ZFS_PROP_RESERVATION:
2648 case ZFS_PROP_REFRESERVATION:
2650 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2653 * If quota or reservation is 0, we translate this into 'none'
2654 * (unless literal is set), and indicate that it's the default
2655 * value. Otherwise, we print the number nicely and indicate
2656 * that its set locally.
2660 (void) strlcpy(propbuf, "0", proplen);
2662 (void) strlcpy(propbuf, "none", proplen);
2665 (void) snprintf(propbuf, proplen, "%llu",
2668 zfs_nicenum(val, propbuf, proplen);
2670 zcp_check(zhp, prop, val, NULL);
2673 case ZFS_PROP_FILESYSTEM_LIMIT:
2674 case ZFS_PROP_SNAPSHOT_LIMIT:
2675 case ZFS_PROP_FILESYSTEM_COUNT:
2676 case ZFS_PROP_SNAPSHOT_COUNT:
2678 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2682 * If limit is UINT64_MAX, we translate this into 'none' (unless
2683 * literal is set), and indicate that it's the default value.
2684 * Otherwise, we print the number nicely and indicate that it's
2688 (void) snprintf(propbuf, proplen, "%llu",
2690 } else if (val == UINT64_MAX) {
2691 (void) strlcpy(propbuf, "none", proplen);
2693 zfs_nicenum(val, propbuf, proplen);
2696 zcp_check(zhp, prop, val, NULL);
2699 case ZFS_PROP_REFRATIO:
2700 case ZFS_PROP_COMPRESSRATIO:
2701 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2703 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2704 (u_longlong_t)(val / 100),
2705 (u_longlong_t)(val % 100));
2706 zcp_check(zhp, prop, val, NULL);
2710 switch (zhp->zfs_type) {
2711 case ZFS_TYPE_FILESYSTEM:
2714 case ZFS_TYPE_VOLUME:
2717 case ZFS_TYPE_SNAPSHOT:
2720 case ZFS_TYPE_BOOKMARK:
2726 (void) snprintf(propbuf, proplen, "%s", str);
2727 zcp_check(zhp, prop, NULL, propbuf);
2730 case ZFS_PROP_MOUNTED:
2732 * The 'mounted' property is a pseudo-property that described
2733 * whether the filesystem is currently mounted. Even though
2734 * it's a boolean value, the typical values of "on" and "off"
2735 * don't make sense, so we translate to "yes" and "no".
2737 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2738 src, &source, &val) != 0)
2741 (void) strlcpy(propbuf, "yes", proplen);
2743 (void) strlcpy(propbuf, "no", proplen);
2748 * The 'name' property is a pseudo-property derived from the
2749 * dataset name. It is presented as a real property to simplify
2752 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2753 zcp_check(zhp, prop, NULL, propbuf);
2756 case ZFS_PROP_MLSLABEL:
2759 m_label_t *new_sl = NULL;
2760 char *ascii = NULL; /* human readable label */
2762 (void) strlcpy(propbuf,
2763 getprop_string(zhp, prop, &source), proplen);
2765 if (literal || (strcasecmp(propbuf,
2766 ZFS_MLSLABEL_DEFAULT) == 0))
2770 * Try to translate the internal hex string to
2771 * human-readable output. If there are any
2772 * problems just use the hex string.
2775 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2776 L_NO_CORRECTION, NULL) == -1) {
2777 m_label_free(new_sl);
2781 if (label_to_str(new_sl, &ascii, M_LABEL,
2785 m_label_free(new_sl);
2788 m_label_free(new_sl);
2790 (void) strlcpy(propbuf, ascii, proplen);
2792 #else /* !illumos */
2794 #endif /* illumos */
2799 case ZFS_PROP_CREATETXG:
2801 * GUIDs are stored as numbers, but they are identifiers.
2802 * We don't want them to be pretty printed, because pretty
2803 * printing mangles the ID into a truncated and useless value.
2805 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2807 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2808 zcp_check(zhp, prop, val, NULL);
2812 switch (zfs_prop_get_type(prop)) {
2813 case PROP_TYPE_NUMBER:
2814 if (get_numeric_property(zhp, prop, src,
2815 &source, &val) != 0) {
2820 (void) snprintf(propbuf, proplen, "%llu",
2823 zfs_nicenum(val, propbuf, proplen);
2825 zcp_check(zhp, prop, val, NULL);
2828 case PROP_TYPE_STRING:
2829 str = getprop_string(zhp, prop, &source);
2833 (void) strlcpy(propbuf, str, proplen);
2834 zcp_check(zhp, prop, NULL, str);
2837 case PROP_TYPE_INDEX:
2838 if (get_numeric_property(zhp, prop, src,
2839 &source, &val) != 0)
2841 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2844 (void) strlcpy(propbuf, strval, proplen);
2845 zcp_check(zhp, prop, NULL, strval);
2853 get_source(zhp, src, source, statbuf, statlen);
2859 * Utility function to get the given numeric property. Does no validation that
2860 * the given property is the appropriate type; should only be used with
2861 * hard-coded property types.
2864 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2869 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2875 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2879 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2880 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2884 * Similar to zfs_prop_get(), but returns the value as an integer.
2887 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2888 zprop_source_t *src, char *statbuf, size_t statlen)
2893 * Check to see if this property applies to our object
2895 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2896 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2897 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2898 zfs_prop_to_name(prop)));
2902 *src = ZPROP_SRC_NONE;
2904 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2907 get_source(zhp, src, source, statbuf, statlen);
2913 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2914 char **domainp, idmap_rid_t *ridp)
2917 idmap_get_handle_t *get_hdl = NULL;
2921 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2925 err = idmap_get_sidbyuid(get_hdl, id,
2926 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2928 err = idmap_get_sidbygid(get_hdl, id,
2929 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2931 if (err == IDMAP_SUCCESS &&
2932 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2933 status == IDMAP_SUCCESS)
2939 idmap_get_destroy(get_hdl);
2941 #else /* !illumos */
2942 assert(!"invalid code path");
2943 return (EINVAL); // silence compiler warning
2944 #endif /* illumos */
2948 * convert the propname into parameters needed by kernel
2949 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2950 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2953 userquota_propname_decode(const char *propname, boolean_t zoned,
2954 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2956 zfs_userquota_prop_t type;
2958 char *numericsid = NULL;
2963 /* Figure out the property type ({user|group}{quota|space}) */
2964 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2965 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2966 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2969 if (type == ZFS_NUM_USERQUOTA_PROPS)
2973 isuser = (type == ZFS_PROP_USERQUOTA ||
2974 type == ZFS_PROP_USERUSED);
2976 cp = strchr(propname, '@') + 1;
2978 if (strchr(cp, '@')) {
2981 * It's a SID name (eg "user@domain") that needs to be
2982 * turned into S-1-domainID-RID.
2985 idmap_stat stat, map_stat;
2988 idmap_get_handle_t *gh = NULL;
2990 stat = idmap_get_create(&gh);
2991 if (stat != IDMAP_SUCCESS) {
2992 idmap_get_destroy(gh);
2995 if (zoned && getzoneid() == GLOBAL_ZONEID)
2998 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
3001 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
3004 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
3007 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
3011 idmap_get_destroy(gh);
3014 stat = idmap_get_mappings(gh);
3015 idmap_get_destroy(gh);
3020 if (numericsid == NULL)
3024 /* will be further decoded below */
3025 #else /* !illumos */
3027 #endif /* illumos */
3030 if (strncmp(cp, "S-1-", 4) == 0) {
3031 /* It's a numeric SID (eg "S-1-234-567-89") */
3032 (void) strlcpy(domain, cp, domainlen);
3035 cp = strrchr(domain, '-');
3038 *ridp = strtoull(cp, &end, 10);
3046 if (errno != 0 || *end != '\0')
3048 } else if (!isdigit(*cp)) {
3050 * It's a user/group name (eg "user") that needs to be
3051 * turned into a uid/gid
3053 if (zoned && getzoneid() == GLOBAL_ZONEID)
3069 /* It's a user/group ID (eg "12345"). */
3070 uid_t id = strtoul(cp, &end, 10);
3077 /* It's an ephemeral ID. */
3078 if (idmap_id_to_numeric_domain_rid(id, isuser,
3079 &mapdomain, &rid) != 0)
3081 (void) strlcpy(domain, mapdomain, domainlen);
3088 ASSERT3P(numericsid, ==, NULL);
3093 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3094 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3097 zfs_cmd_t zc = { 0 };
3099 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3101 err = userquota_propname_decode(propname,
3102 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3103 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3104 zc.zc_objset_type = *typep;
3108 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3112 *propvalue = zc.zc_cookie;
3117 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3118 uint64_t *propvalue)
3120 zfs_userquota_prop_t type;
3122 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3127 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3128 char *propbuf, int proplen, boolean_t literal)
3132 zfs_userquota_prop_t type;
3134 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3141 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3142 } else if (propvalue == 0 &&
3143 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
3144 (void) strlcpy(propbuf, "none", proplen);
3146 zfs_nicenum(propvalue, propbuf, proplen);
3152 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3153 uint64_t *propvalue)
3156 zfs_cmd_t zc = { 0 };
3157 const char *snapname;
3159 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3161 snapname = strchr(propname, '@') + 1;
3162 if (strchr(snapname, '@')) {
3163 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3165 /* snapname is the short name, append it to zhp's fsname */
3168 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3169 sizeof (zc.zc_value));
3170 cp = strchr(zc.zc_value, '@');
3173 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3174 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3177 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3181 *propvalue = zc.zc_cookie;
3186 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3187 char *propbuf, int proplen, boolean_t literal)
3192 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3198 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3200 zfs_nicenum(propvalue, propbuf, proplen);
3206 * Returns the name of the given zfs handle.
3209 zfs_get_name(const zfs_handle_t *zhp)
3211 return (zhp->zfs_name);
3215 * Returns the name of the parent pool for the given zfs handle.
3218 zfs_get_pool_name(const zfs_handle_t *zhp)
3220 return (zhp->zpool_hdl->zpool_name);
3224 * Returns the type of the given zfs handle.
3227 zfs_get_type(const zfs_handle_t *zhp)
3229 return (zhp->zfs_type);
3233 * Is one dataset name a child dataset of another?
3235 * Needs to handle these cases:
3236 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3237 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3238 * Descendant? No. No. No. Yes.
3241 is_descendant(const char *ds1, const char *ds2)
3243 size_t d1len = strlen(ds1);
3245 /* ds2 can't be a descendant if it's smaller */
3246 if (strlen(ds2) < d1len)
3249 /* otherwise, compare strings and verify that there's a '/' char */
3250 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3254 * Given a complete name, return just the portion that refers to the parent.
3255 * Will return -1 if there is no parent (path is just the name of the
3259 parent_name(const char *path, char *buf, size_t buflen)
3263 (void) strlcpy(buf, path, buflen);
3265 if ((slashp = strrchr(buf, '/')) == NULL)
3273 * If accept_ancestor is false, then check to make sure that the given path has
3274 * a parent, and that it exists. If accept_ancestor is true, then find the
3275 * closest existing ancestor for the given path. In prefixlen return the
3276 * length of already existing prefix of the given path. We also fetch the
3277 * 'zoned' property, which is used to validate property settings when creating
3281 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3282 boolean_t accept_ancestor, int *prefixlen)
3284 zfs_cmd_t zc = { 0 };
3285 char parent[ZFS_MAX_DATASET_NAME_LEN];
3291 (void) snprintf(errbuf, sizeof (errbuf),
3292 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3294 /* get parent, and check to see if this is just a pool */
3295 if (parent_name(path, parent, sizeof (parent)) != 0) {
3296 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3297 "missing dataset name"));
3298 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3301 /* check to see if the pool exists */
3302 if ((slash = strchr(parent, '/')) == NULL)
3303 slash = parent + strlen(parent);
3304 (void) strncpy(zc.zc_name, parent, slash - parent);
3305 zc.zc_name[slash - parent] = '\0';
3306 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3308 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3309 "no such pool '%s'"), zc.zc_name);
3310 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3313 /* check to see if the parent dataset exists */
3314 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3315 if (errno == ENOENT && accept_ancestor) {
3317 * Go deeper to find an ancestor, give up on top level.
3319 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3321 "no such pool '%s'"), zc.zc_name);
3322 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3324 } else if (errno == ENOENT) {
3325 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3326 "parent does not exist"));
3327 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3329 return (zfs_standard_error(hdl, errno, errbuf));
3332 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3336 /* we are in a non-global zone, but parent is in the global zone */
3337 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3338 (void) zfs_standard_error(hdl, EPERM, errbuf);
3343 /* make sure parent is a filesystem */
3344 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3345 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3346 "parent is not a filesystem"));
3347 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3353 if (prefixlen != NULL)
3354 *prefixlen = strlen(parent);
3359 * Finds whether the dataset of the given type(s) exists.
3362 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3366 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3370 * Try to get stats for the dataset, which will tell us if it exists.
3372 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3373 int ds_type = zhp->zfs_type;
3376 if (types & ds_type)
3383 * Given a path to 'target', create all the ancestors between
3384 * the prefixlen portion of the path, and the target itself.
3385 * Fail if the initial prefixlen-ancestor does not already exist.
3388 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3394 /* make sure prefix exists */
3395 cp = target + prefixlen;
3397 assert(strchr(cp, '/') == NULL);
3398 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3401 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3409 * Attempt to create, mount, and share any ancestor filesystems,
3410 * up to the prefixlen-long one.
3412 for (cp = target + prefixlen + 1;
3413 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3417 h = make_dataset_handle(hdl, target);
3419 /* it already exists, nothing to do here */
3424 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3426 opname = dgettext(TEXT_DOMAIN, "create");
3430 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3432 opname = dgettext(TEXT_DOMAIN, "open");
3436 if (zfs_mount(h, NULL, 0) != 0) {
3437 opname = dgettext(TEXT_DOMAIN, "mount");
3441 if (zfs_share(h) != 0) {
3442 opname = dgettext(TEXT_DOMAIN, "share");
3452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3453 "failed to %s ancestor '%s'"), opname, target);
3458 * Creates non-existing ancestors of the given path.
3461 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3468 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3469 "cannot create '%s'"), path);
3472 * Check that we are not passing the nesting limit
3473 * before we start creating any ancestors.
3475 if (dataset_nestcheck(path) != 0) {
3476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3477 "maximum name nesting depth exceeded"));
3478 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3481 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3484 if ((path_copy = strdup(path)) != NULL) {
3485 rc = create_parents(hdl, path_copy, prefix);
3488 if (path_copy == NULL || rc != 0)
3495 * Create a new filesystem or volume.
3498 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3503 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3506 enum lzc_dataset_type ost;
3507 zpool_handle_t *zpool_handle;
3509 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3510 "cannot create '%s'"), path);
3512 /* validate the path, taking care to note the extended error message */
3513 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3514 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3516 if (dataset_nestcheck(path) != 0) {
3517 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3518 "maximum name nesting depth exceeded"));
3519 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3522 /* validate parents exist */
3523 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3527 * The failure modes when creating a dataset of a different type over
3528 * one that already exists is a little strange. In particular, if you
3529 * try to create a dataset on top of an existing dataset, the ioctl()
3530 * will return ENOENT, not EEXIST. To prevent this from happening, we
3531 * first try to see if the dataset exists.
3533 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3535 "dataset already exists"));
3536 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3539 if (type == ZFS_TYPE_VOLUME)
3540 ost = LZC_DATSET_TYPE_ZVOL;
3542 ost = LZC_DATSET_TYPE_ZFS;
3544 /* open zpool handle for prop validation */
3545 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3546 (void) strlcpy(pool_path, path, sizeof (pool_path));
3548 /* truncate pool_path at first slash */
3549 char *p = strchr(pool_path, '/');
3553 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3556 if (props && (props = zfs_valid_proplist(hdl, type, props,
3557 zoned, NULL, zpool_handle, errbuf)) == 0) {
3558 zpool_close(zpool_handle);
3561 zpool_close(zpool_handle);
3563 if (type == ZFS_TYPE_VOLUME) {
3565 * If we are creating a volume, the size and block size must
3566 * satisfy a few restraints. First, the blocksize must be a
3567 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3568 * volsize must be a multiple of the block size, and cannot be
3571 if (props == NULL || nvlist_lookup_uint64(props,
3572 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3574 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3575 "missing volume size"));
3576 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3579 if ((ret = nvlist_lookup_uint64(props,
3580 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3581 &blocksize)) != 0) {
3582 if (ret == ENOENT) {
3583 blocksize = zfs_prop_default_numeric(
3584 ZFS_PROP_VOLBLOCKSIZE);
3587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3588 "missing volume block size"));
3589 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3596 "volume size cannot be zero"));
3597 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3600 if (size % blocksize != 0) {
3602 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3603 "volume size must be a multiple of volume block "
3605 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3609 /* create the dataset */
3610 ret = lzc_create(path, ost, props);
3613 /* check for failure */
3615 char parent[ZFS_MAX_DATASET_NAME_LEN];
3616 (void) parent_name(path, parent, sizeof (parent));
3620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3621 "no such parent '%s'"), parent);
3622 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3626 "parent '%s' is not a filesystem"), parent);
3627 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3631 "pool must be upgraded to set this "
3632 "property or value"));
3633 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3636 "invalid property value(s) specified"));
3637 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3641 * This platform can't address a volume this big.
3643 if (type == ZFS_TYPE_VOLUME)
3644 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3649 return (zfs_standard_error(hdl, errno, errbuf));
3657 * Destroys the given dataset. The caller must make sure that the filesystem
3658 * isn't mounted, and that there are no active dependents. If the file system
3659 * does not exist this function does nothing.
3662 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3666 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3669 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3670 nvlist_t *nv = fnvlist_alloc();
3671 fnvlist_add_boolean(nv, zhp->zfs_name);
3672 error = lzc_destroy_bookmarks(nv, NULL);
3675 return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3676 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3682 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3683 nvlist_t *nv = fnvlist_alloc();
3684 fnvlist_add_boolean(nv, zhp->zfs_name);
3685 error = lzc_destroy_snaps(nv, defer, NULL);
3688 error = lzc_destroy(zhp->zfs_name);
3691 if (error != 0 && error != ENOENT) {
3692 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3693 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3697 remove_mountpoint(zhp);
3702 struct destroydata {
3704 const char *snapname;
3708 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3710 struct destroydata *dd = arg;
3711 char name[ZFS_MAX_DATASET_NAME_LEN];
3714 (void) snprintf(name, sizeof (name),
3715 "%s@%s", zhp->zfs_name, dd->snapname);
3717 if (lzc_exists(name))
3718 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3720 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3726 * Destroys all snapshots with the given name in zhp & descendants.
3729 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3732 struct destroydata dd = { 0 };
3734 dd.snapname = snapname;
3735 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3736 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3738 if (nvlist_empty(dd.nvl)) {
3739 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3740 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3741 zhp->zfs_name, snapname);
3743 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3745 nvlist_free(dd.nvl);
3750 * Destroys all the snapshots named in the nvlist.
3753 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3756 nvlist_t *errlist = NULL;
3758 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3761 nvlist_free(errlist);
3765 if (nvlist_empty(errlist)) {
3767 (void) snprintf(errbuf, sizeof (errbuf),
3768 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3770 ret = zfs_standard_error(hdl, ret, errbuf);
3772 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3773 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3775 (void) snprintf(errbuf, sizeof (errbuf),
3776 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3779 switch (fnvpair_value_int32(pair)) {
3782 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3783 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3786 ret = zfs_standard_error(hdl, errno, errbuf);
3791 nvlist_free(errlist);
3796 * Clones the given dataset. The target must be of the same type as the source.
3799 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3801 char parent[ZFS_MAX_DATASET_NAME_LEN];
3804 libzfs_handle_t *hdl = zhp->zfs_hdl;
3807 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3809 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3810 "cannot create '%s'"), target);
3812 /* validate the target/clone name */
3813 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3814 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3816 /* validate parents exist */
3817 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3820 (void) parent_name(target, parent, sizeof (parent));
3827 if (ZFS_IS_VOLUME(zhp)) {
3828 type = ZFS_TYPE_VOLUME;
3830 type = ZFS_TYPE_FILESYSTEM;
3832 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3833 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3835 if (zfs_fix_auto_resv(zhp, props) == -1) {
3841 ret = lzc_clone(target, zhp->zfs_name, props);
3849 * The parent doesn't exist. We should have caught this
3850 * above, but there may a race condition that has since
3851 * destroyed the parent.
3853 * At this point, we don't know whether it's the source
3854 * that doesn't exist anymore, or whether the target
3855 * dataset doesn't exist.
3857 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3858 "no such parent '%s'"), parent);
3859 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3862 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3863 "source and target pools differ"));
3864 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3868 return (zfs_standard_error(zhp->zfs_hdl, errno,
3877 * Promotes the given clone fs to be the clone parent.
3880 zfs_promote(zfs_handle_t *zhp)
3882 libzfs_handle_t *hdl = zhp->zfs_hdl;
3883 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3887 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3888 "cannot promote '%s'"), zhp->zfs_name);
3890 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3891 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3892 "snapshots can not be promoted"));
3893 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3896 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3897 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3898 "not a cloned filesystem"));
3899 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3902 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3903 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3905 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3910 /* There is a conflicting snapshot name. */
3911 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3912 "conflicting snapshot '%s' from parent '%s'"),
3913 snapname, zhp->zfs_dmustats.dds_origin);
3914 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3917 return (zfs_standard_error(hdl, ret, errbuf));
3923 typedef struct snapdata {
3925 const char *sd_snapname;
3929 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3931 snapdata_t *sd = arg;
3932 char name[ZFS_MAX_DATASET_NAME_LEN];
3935 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3936 (void) snprintf(name, sizeof (name),
3937 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3939 fnvlist_add_boolean(sd->sd_nvl, name);
3941 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3949 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3954 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3955 "cannot remap dataset '%s'"), fs);
3957 err = lzc_remap(fs);
3962 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3963 "pool must be upgraded"));
3964 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3967 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3970 (void) zfs_standard_error(hdl, err, errbuf);
3979 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3983 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3990 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3991 "cannot create snapshots "));
3994 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3995 const char *snapname = nvpair_name(elem);
3997 /* validate the target name */
3998 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
4000 (void) snprintf(errbuf, sizeof (errbuf),
4001 dgettext(TEXT_DOMAIN,
4002 "cannot create snapshot '%s'"), snapname);
4003 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4008 * get pool handle for prop validation. assumes all snaps are in the
4009 * same pool, as does lzc_snapshot (below).
4011 char pool[ZFS_MAX_DATASET_NAME_LEN];
4012 elem = nvlist_next_nvpair(snaps, NULL);
4013 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4014 pool[strcspn(pool, "/@")] = '\0';
4015 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
4017 if (props != NULL &&
4018 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4019 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
4020 zpool_close(zpool_hdl);
4023 zpool_close(zpool_hdl);
4025 ret = lzc_snapshot(snaps, props, &errors);
4028 boolean_t printed = B_FALSE;
4029 for (elem = nvlist_next_nvpair(errors, NULL);
4031 elem = nvlist_next_nvpair(errors, elem)) {
4032 (void) snprintf(errbuf, sizeof (errbuf),
4033 dgettext(TEXT_DOMAIN,
4034 "cannot create snapshot '%s'"), nvpair_name(elem));
4035 (void) zfs_standard_error(hdl,
4036 fnvpair_value_int32(elem), errbuf);
4042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4043 "multiple snapshots of same "
4045 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4049 (void) zfs_standard_error(hdl, ret, errbuf);
4055 nvlist_free(errors);
4060 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4064 snapdata_t sd = { 0 };
4065 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4070 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4071 "cannot snapshot %s"), path);
4073 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4074 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4076 (void) strlcpy(fsname, path, sizeof (fsname));
4077 cp = strchr(fsname, '@');
4079 sd.sd_snapname = cp + 1;
4081 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4082 ZFS_TYPE_VOLUME)) == NULL) {
4086 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4088 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4090 fnvlist_add_boolean(sd.sd_nvl, path);
4093 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4094 nvlist_free(sd.sd_nvl);
4100 * Destroy any more recent snapshots. We invoke this callback on any dependents
4101 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4102 * is a dependent and we should just destroy it without checking the transaction
4105 typedef struct rollback_data {
4106 const char *cb_target; /* the snapshot */
4107 uint64_t cb_create; /* creation time reference */
4113 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4115 rollback_data_t *cbp = data;
4116 prop_changelist_t *clp;
4118 /* We must destroy this clone; first unmount it */
4119 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4120 cbp->cb_force ? MS_FORCE: 0);
4121 if (clp == NULL || changelist_prefix(clp) != 0) {
4122 cbp->cb_error = B_TRUE;
4126 if (zfs_destroy(zhp, B_FALSE) != 0)
4127 cbp->cb_error = B_TRUE;
4129 changelist_remove(clp, zhp->zfs_name);
4130 (void) changelist_postfix(clp);
4131 changelist_free(clp);
4138 rollback_destroy(zfs_handle_t *zhp, void *data)
4140 rollback_data_t *cbp = data;
4142 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4143 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4144 rollback_destroy_dependent, cbp);
4146 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4154 * Given a dataset, rollback to a specific snapshot, discarding any
4155 * data changes since then and making it the active dataset.
4157 * Any snapshots and bookmarks more recent than the target are
4158 * destroyed, along with their dependents (i.e. clones).
4161 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4163 rollback_data_t cb = { 0 };
4165 boolean_t restore_resv = 0;
4166 uint64_t old_volsize = 0, new_volsize;
4167 zfs_prop_t resv_prop;
4169 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4170 zhp->zfs_type == ZFS_TYPE_VOLUME);
4173 * Destroy all recent snapshots and their dependents.
4175 cb.cb_force = force;
4176 cb.cb_target = snap->zfs_name;
4177 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4178 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4179 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4185 * Now that we have verified that the snapshot is the latest,
4186 * rollback to the given snapshot.
4189 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4190 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4192 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4194 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4198 * Pass both the filesystem and the wanted snapshot names,
4199 * we would get an error back if the snapshot is destroyed or
4200 * a new snapshot is created before this request is processed.
4202 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4206 (void) snprintf(errbuf, sizeof (errbuf),
4207 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4211 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4212 "there is a snapshot or bookmark more recent "
4213 "than '%s'"), snap->zfs_name);
4214 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4217 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4218 "'%s' is not found among snapshots of '%s'"),
4219 snap->zfs_name, zhp->zfs_name);
4220 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4223 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4226 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4232 * For volumes, if the pre-rollback volsize matched the pre-
4233 * rollback reservation and the volsize has changed then set
4234 * the reservation property to the post-rollback volsize.
4235 * Make a new handle since the rollback closed the dataset.
4237 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4238 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4240 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4241 if (old_volsize != new_volsize)
4242 err = zfs_prop_set_int(zhp, resv_prop,
4251 * Renames the given dataset.
4254 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
4255 renameflags_t flags)
4258 zfs_cmd_t zc = { 0 };
4260 prop_changelist_t *cl = NULL;
4261 zfs_handle_t *zhrp = NULL;
4262 char *parentname = NULL;
4263 char parent[ZFS_MAX_DATASET_NAME_LEN];
4264 char property[ZFS_MAXPROPLEN];
4265 libzfs_handle_t *hdl = zhp->zfs_hdl;
4268 /* if we have the same exact name, just return success */
4269 if (strcmp(zhp->zfs_name, target) == 0)
4272 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4273 "cannot rename to '%s'"), target);
4275 if (source != NULL) {
4277 * This is recursive snapshots rename, put snapshot name
4278 * (that might not exist) into zfs_name.
4280 assert(flags.recurse);
4282 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
4283 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
4284 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
4287 /* make sure source name is valid */
4288 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4289 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4292 * Make sure the target name is valid
4294 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4295 if ((strchr(target, '@') == NULL) ||
4298 * Snapshot target name is abbreviated,
4299 * reconstruct full dataset name
4301 (void) strlcpy(parent, zhp->zfs_name,
4303 delim = strchr(parent, '@');
4304 if (strchr(target, '@') == NULL)
4308 (void) strlcat(parent, target, sizeof (parent));
4312 * Make sure we're renaming within the same dataset.
4314 delim = strchr(target, '@');
4315 if (strncmp(zhp->zfs_name, target, delim - target)
4316 != 0 || zhp->zfs_name[delim - target] != '@') {
4317 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4318 "snapshots must be part of same "
4320 return (zfs_error(hdl, EZFS_CROSSTARGET,
4325 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4326 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4328 if (flags.recurse) {
4329 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4330 "recursive rename must be a snapshot"));
4331 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4334 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4335 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4337 /* validate parents */
4338 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4341 /* make sure we're in the same pool */
4342 verify((delim = strchr(target, '/')) != NULL);
4343 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4344 zhp->zfs_name[delim - target] != '/') {
4345 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4346 "datasets must be within same pool"));
4347 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4350 /* new name cannot be a child of the current dataset name */
4351 if (is_descendant(zhp->zfs_name, target)) {
4352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4353 "New dataset name cannot be a descendant of "
4354 "current dataset name"));
4355 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4359 (void) snprintf(errbuf, sizeof (errbuf),
4360 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4362 if (getzoneid() == GLOBAL_ZONEID &&
4363 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4364 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4365 "dataset is used in a non-global zone"));
4366 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4370 * Avoid unmounting file systems with mountpoint property set to
4371 * 'legacy' or 'none' even if -u option is not given.
4373 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4374 !flags.recurse && !flags.nounmount &&
4375 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4376 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4377 (strcmp(property, "legacy") == 0 ||
4378 strcmp(property, "none") == 0)) {
4379 flags.nounmount = B_TRUE;
4381 if (flags.recurse) {
4383 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4384 if (parentname == NULL) {
4388 delim = strchr(parentname, '@');
4390 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4395 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4396 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4397 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
4398 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
4402 if (changelist_haszonedchild(cl)) {
4403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4404 "child dataset with inherited mountpoint is used "
4405 "in a non-global zone"));
4406 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4411 if ((ret = changelist_prefix(cl)) != 0)
4415 if (ZFS_IS_VOLUME(zhp))
4416 zc.zc_objset_type = DMU_OST_ZVOL;
4418 zc.zc_objset_type = DMU_OST_ZFS;
4420 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4421 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4423 zc.zc_cookie = flags.recurse ? 1 : 0;
4424 if (flags.nounmount)
4427 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4429 * if it was recursive, the one that actually failed will
4432 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4433 "cannot rename '%s'"), zc.zc_name);
4435 if (flags.recurse && errno == EEXIST) {
4436 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4437 "a child dataset already has a snapshot "
4438 "with the new name"));
4439 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4441 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4445 * On failure, we still want to remount any filesystems that
4446 * were previously mounted, so we don't alter the system state.
4449 (void) changelist_postfix(cl);
4452 changelist_rename(cl, zfs_get_name(zhp), target);
4453 ret = changelist_postfix(cl);
4458 if (parentname != NULL) {
4465 changelist_free(cl);
4471 zfs_get_user_props(zfs_handle_t *zhp)
4473 return (zhp->zfs_user_props);
4477 zfs_get_recvd_props(zfs_handle_t *zhp)
4479 if (zhp->zfs_recvd_props == NULL)
4480 if (get_recvd_props_ioctl(zhp) != 0)
4482 return (zhp->zfs_recvd_props);
4486 * This function is used by 'zfs list' to determine the exact set of columns to
4487 * display, and their maximum widths. This does two main things:
4489 * - If this is a list of all properties, then expand the list to include
4490 * all native properties, and set a flag so that for each dataset we look
4491 * for new unique user properties and add them to the list.
4493 * - For non fixed-width properties, keep track of the maximum width seen
4494 * so that we can size the column appropriately. If the user has
4495 * requested received property values, we also need to compute the width
4496 * of the RECEIVED column.
4499 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4502 libzfs_handle_t *hdl = zhp->zfs_hdl;
4503 zprop_list_t *entry;
4504 zprop_list_t **last, **start;
4505 nvlist_t *userprops, *propval;
4508 char buf[ZFS_MAXPROPLEN];
4510 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4513 userprops = zfs_get_user_props(zhp);
4516 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4518 * Go through and add any user properties as necessary. We
4519 * start by incrementing our list pointer to the first
4520 * non-native property.
4523 while (*start != NULL) {
4524 if ((*start)->pl_prop == ZPROP_INVAL)
4526 start = &(*start)->pl_next;
4530 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4532 * See if we've already found this property in our list.
4534 for (last = start; *last != NULL;
4535 last = &(*last)->pl_next) {
4536 if (strcmp((*last)->pl_user_prop,
4537 nvpair_name(elem)) == 0)
4541 if (*last == NULL) {
4542 if ((entry = zfs_alloc(hdl,
4543 sizeof (zprop_list_t))) == NULL ||
4544 ((entry->pl_user_prop = zfs_strdup(hdl,
4545 nvpair_name(elem)))) == NULL) {
4550 entry->pl_prop = ZPROP_INVAL;
4551 entry->pl_width = strlen(nvpair_name(elem));
4552 entry->pl_all = B_TRUE;
4559 * Now go through and check the width of any non-fixed columns
4561 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4562 if (entry->pl_fixed && !literal)
4565 if (entry->pl_prop != ZPROP_INVAL) {
4566 if (zfs_prop_get(zhp, entry->pl_prop,
4567 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4568 if (strlen(buf) > entry->pl_width)
4569 entry->pl_width = strlen(buf);
4571 if (received && zfs_prop_get_recvd(zhp,
4572 zfs_prop_to_name(entry->pl_prop),
4573 buf, sizeof (buf), literal) == 0)
4574 if (strlen(buf) > entry->pl_recvd_width)
4575 entry->pl_recvd_width = strlen(buf);
4577 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4579 verify(nvlist_lookup_string(propval,
4580 ZPROP_VALUE, &strval) == 0);
4581 if (strlen(strval) > entry->pl_width)
4582 entry->pl_width = strlen(strval);
4584 if (received && zfs_prop_get_recvd(zhp,
4585 entry->pl_user_prop,
4586 buf, sizeof (buf), literal) == 0)
4587 if (strlen(buf) > entry->pl_recvd_width)
4588 entry->pl_recvd_width = strlen(buf);
4596 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4597 char *resource, void *export, void *sharetab,
4598 int sharemax, zfs_share_op_t operation)
4600 zfs_cmd_t zc = { 0 };
4603 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4604 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4606 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4607 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4608 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4609 zc.zc_share.z_sharetype = operation;
4610 zc.zc_share.z_sharemax = sharemax;
4611 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4616 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4621 * Keep a reference to the props-table against which we prune the
4624 zhp->zfs_props_table = props;
4626 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4629 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4630 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4633 * User properties will result in ZPROP_INVAL, and since we
4634 * only know how to prune standard ZFS properties, we always
4635 * leave these in the list. This can also happen if we
4636 * encounter an unknown DSL property (when running older
4637 * software, for example).
4639 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4640 (void) nvlist_remove(zhp->zfs_props,
4641 nvpair_name(curr), nvpair_type(curr));
4648 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4649 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4651 zfs_cmd_t zc = { 0 };
4652 nvlist_t *nvlist = NULL;
4655 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4656 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4657 zc.zc_cookie = (uint64_t)cmd;
4659 if (cmd == ZFS_SMB_ACL_RENAME) {
4660 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4661 (void) no_memory(hdl);
4667 case ZFS_SMB_ACL_ADD:
4668 case ZFS_SMB_ACL_REMOVE:
4669 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4671 case ZFS_SMB_ACL_RENAME:
4672 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4674 (void) no_memory(hdl);
4677 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4679 (void) no_memory(hdl);
4682 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4683 nvlist_free(nvlist);
4687 case ZFS_SMB_ACL_PURGE:
4692 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4693 nvlist_free(nvlist);
4698 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4699 char *path, char *resource)
4701 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4706 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4707 char *path, char *resource)
4709 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4714 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4716 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4721 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4722 char *oldname, char *newname)
4724 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4727 #endif /* illumos */
4730 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4731 zfs_userspace_cb_t func, void *arg)
4733 zfs_cmd_t zc = { 0 };
4734 zfs_useracct_t buf[100];
4735 libzfs_handle_t *hdl = zhp->zfs_hdl;
4738 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4740 zc.zc_objset_type = type;
4741 zc.zc_nvlist_dst = (uintptr_t)buf;
4744 zfs_useracct_t *zua = buf;
4746 zc.zc_nvlist_dst_size = sizeof (buf);
4747 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4750 (void) snprintf(errbuf, sizeof (errbuf),
4751 dgettext(TEXT_DOMAIN,
4752 "cannot get used/quota for %s"), zc.zc_name);
4753 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4755 if (zc.zc_nvlist_dst_size == 0)
4758 while (zc.zc_nvlist_dst_size > 0) {
4759 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4760 zua->zu_space)) != 0)
4763 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4772 const char *snapname;
4774 boolean_t recursive;
4779 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4781 struct holdarg *ha = arg;
4782 char name[ZFS_MAX_DATASET_NAME_LEN];
4785 (void) snprintf(name, sizeof (name),
4786 "%s@%s", zhp->zfs_name, ha->snapname);
4788 if (lzc_exists(name))
4789 fnvlist_add_string(ha->nvl, name, ha->tag);
4792 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4798 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4799 boolean_t recursive, int cleanup_fd)
4804 ha.nvl = fnvlist_alloc();
4805 ha.snapname = snapname;
4807 ha.recursive = recursive;
4808 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4810 if (nvlist_empty(ha.nvl)) {
4813 fnvlist_free(ha.nvl);
4815 (void) snprintf(errbuf, sizeof (errbuf),
4816 dgettext(TEXT_DOMAIN,
4817 "cannot hold snapshot '%s@%s'"),
4818 zhp->zfs_name, snapname);
4819 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4823 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4824 fnvlist_free(ha.nvl);
4830 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4834 libzfs_handle_t *hdl = zhp->zfs_hdl;
4839 ret = lzc_hold(holds, cleanup_fd, &errors);
4842 /* There may be errors even in the success case. */
4843 fnvlist_free(errors);
4847 if (nvlist_empty(errors)) {
4848 /* no hold-specific errors */
4849 (void) snprintf(errbuf, sizeof (errbuf),
4850 dgettext(TEXT_DOMAIN, "cannot hold"));
4853 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4854 "pool must be upgraded"));
4855 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4858 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4861 (void) zfs_standard_error(hdl, ret, errbuf);
4865 for (elem = nvlist_next_nvpair(errors, NULL);
4867 elem = nvlist_next_nvpair(errors, elem)) {
4868 (void) snprintf(errbuf, sizeof (errbuf),
4869 dgettext(TEXT_DOMAIN,
4870 "cannot hold snapshot '%s'"), nvpair_name(elem));
4871 switch (fnvpair_value_int32(elem)) {
4874 * Temporary tags wind up having the ds object id
4875 * prepended. So even if we passed the length check
4876 * above, it's still possible for the tag to wind
4877 * up being slightly too long.
4879 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4882 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4885 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4888 (void) zfs_standard_error(hdl,
4889 fnvpair_value_int32(elem), errbuf);
4893 fnvlist_free(errors);
4898 zfs_release_one(zfs_handle_t *zhp, void *arg)
4900 struct holdarg *ha = arg;
4901 char name[ZFS_MAX_DATASET_NAME_LEN];
4903 nvlist_t *existing_holds;
4905 (void) snprintf(name, sizeof (name),
4906 "%s@%s", zhp->zfs_name, ha->snapname);
4908 if (lzc_get_holds(name, &existing_holds) != 0) {
4910 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4913 nvlist_t *torelease = fnvlist_alloc();
4914 fnvlist_add_boolean(torelease, ha->tag);
4915 fnvlist_add_nvlist(ha->nvl, name, torelease);
4916 fnvlist_free(torelease);
4920 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4926 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4927 boolean_t recursive)
4931 nvlist_t *errors = NULL;
4933 libzfs_handle_t *hdl = zhp->zfs_hdl;
4936 ha.nvl = fnvlist_alloc();
4937 ha.snapname = snapname;
4939 ha.recursive = recursive;
4941 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4943 if (nvlist_empty(ha.nvl)) {
4944 fnvlist_free(ha.nvl);
4946 (void) snprintf(errbuf, sizeof (errbuf),
4947 dgettext(TEXT_DOMAIN,
4948 "cannot release hold from snapshot '%s@%s'"),
4949 zhp->zfs_name, snapname);
4951 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4953 (void) zfs_standard_error(hdl, ret, errbuf);
4958 ret = lzc_release(ha.nvl, &errors);
4959 fnvlist_free(ha.nvl);
4962 /* There may be errors even in the success case. */
4963 fnvlist_free(errors);
4967 if (nvlist_empty(errors)) {
4968 /* no hold-specific errors */
4969 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4974 "pool must be upgraded"));
4975 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4978 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4982 for (elem = nvlist_next_nvpair(errors, NULL);
4984 elem = nvlist_next_nvpair(errors, elem)) {
4985 (void) snprintf(errbuf, sizeof (errbuf),
4986 dgettext(TEXT_DOMAIN,
4987 "cannot release hold from snapshot '%s'"),
4989 switch (fnvpair_value_int32(elem)) {
4991 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4994 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4997 (void) zfs_standard_error_fmt(hdl,
4998 fnvpair_value_int32(elem), errbuf);
5002 fnvlist_free(errors);
5007 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5009 zfs_cmd_t zc = { 0 };
5010 libzfs_handle_t *hdl = zhp->zfs_hdl;
5016 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5017 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5021 nvbuf = malloc(nvsz);
5022 if (nvbuf == NULL) {
5023 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5027 zc.zc_nvlist_dst_size = nvsz;
5028 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5030 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5032 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
5033 (void) snprintf(errbuf, sizeof (errbuf),
5034 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5039 nvsz = zc.zc_nvlist_dst_size;
5043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5044 "pool must be upgraded"));
5045 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5048 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5051 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5054 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5059 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5061 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
5062 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5064 err = zfs_standard_error_fmt(hdl, rc, errbuf);
5074 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5076 zfs_cmd_t zc = { 0 };
5077 libzfs_handle_t *hdl = zhp->zfs_hdl;
5083 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5084 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5086 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5089 nvbuf = malloc(nvsz);
5091 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5094 zc.zc_nvlist_src_size = nvsz;
5095 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5096 zc.zc_perm_action = un;
5098 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5100 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5101 (void) snprintf(errbuf, sizeof (errbuf),
5102 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5107 "pool must be upgraded"));
5108 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5111 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5114 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5117 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5128 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5133 err = lzc_get_holds(zhp->zfs_name, nvl);
5136 libzfs_handle_t *hdl = zhp->zfs_hdl;
5138 (void) snprintf(errbuf, sizeof (errbuf),
5139 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5144 "pool must be upgraded"));
5145 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5148 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5151 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5154 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5163 * Convert the zvol's volume size to an appropriate reservation.
5164 * Note: If this routine is updated, it is necessary to update the ZFS test
5165 * suite's shell version in reservation.kshlib.
5168 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5171 uint64_t nblocks, volblocksize;
5175 if (nvlist_lookup_string(props,
5176 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5177 ncopies = atoi(strval);
5180 if (nvlist_lookup_uint64(props,
5181 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5182 &volblocksize) != 0)
5183 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5184 nblocks = volsize/volblocksize;
5185 /* start with metadnode L0-L6 */
5187 /* calculate number of indirects */
5188 while (nblocks > 1) {
5189 nblocks += DNODES_PER_LEVEL - 1;
5190 nblocks /= DNODES_PER_LEVEL;
5193 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5196 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5197 * compressed, but in practice they compress down to about
5200 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5206 * Attach/detach the given filesystem to/from the given jail.
5209 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
5211 libzfs_handle_t *hdl = zhp->zfs_hdl;
5212 zfs_cmd_t zc = { 0 };
5218 (void) snprintf(errbuf, sizeof (errbuf),
5219 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
5221 (void) snprintf(errbuf, sizeof (errbuf),
5222 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
5225 switch (zhp->zfs_type) {
5226 case ZFS_TYPE_VOLUME:
5227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5228 "volumes can not be jailed"));
5229 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5230 case ZFS_TYPE_SNAPSHOT:
5231 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5232 "snapshots can not be jailed"));
5233 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5235 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5237 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5238 zc.zc_objset_type = DMU_OST_ZFS;
5239 zc.zc_jailid = jailid;
5241 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
5242 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
5243 zfs_standard_error(hdl, errno, errbuf);