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 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017 Nexenta Systems, Inc.
33 * Copyright 2017 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 /* Get the list of unset properties back and report them. */
1846 nvlist_t *errorprops = NULL;
1847 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1849 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1851 elem = nvlist_next_nvpair(nvl, elem)) {
1852 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1853 zfs_setprop_error(hdl, prop, errno, errbuf);
1855 nvlist_free(errorprops);
1857 if (added_resv && errno == ENOSPC) {
1858 /* clean up the volsize property we tried to set */
1859 uint64_t old_volsize = zfs_prop_get_int(zhp,
1863 zcmd_free_nvlists(&zc);
1865 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1867 if (nvlist_add_uint64(nvl,
1868 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1871 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1873 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1876 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1877 if (cls[cl_idx] != NULL) {
1878 int clp_err = changelist_postfix(cls[cl_idx]);
1885 * Refresh the statistics so the new property value
1889 (void) get_stats(zhp);
1894 zcmd_free_nvlists(&zc);
1896 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1897 if (cls[cl_idx] != NULL)
1898 changelist_free(cls[cl_idx]);
1906 * Given a property, inherit the value from the parent dataset, or if received
1907 * is TRUE, revert to the received value, if any.
1910 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1912 zfs_cmd_t zc = { 0 };
1914 prop_changelist_t *cl;
1915 libzfs_handle_t *hdl = zhp->zfs_hdl;
1919 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1920 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1922 zc.zc_cookie = received;
1923 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1925 * For user properties, the amount of work we have to do is very
1926 * small, so just do it here.
1928 if (!zfs_prop_user(propname)) {
1929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1930 "invalid property"));
1931 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1934 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1935 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1937 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1938 return (zfs_standard_error(hdl, errno, errbuf));
1944 * Verify that this property is inheritable.
1946 if (zfs_prop_readonly(prop))
1947 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1949 if (!zfs_prop_inheritable(prop) && !received)
1950 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1953 * Check to see if the value applies to this type
1955 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1956 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1959 * Normalize the name, to get rid of shorthand abbreviations.
1961 propname = zfs_prop_to_name(prop);
1962 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1963 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1965 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1966 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1967 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1968 "dataset is used in a non-global zone"));
1969 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1973 * Determine datasets which will be affected by this change, if any.
1975 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1978 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1980 "child dataset with inherited mountpoint is used "
1981 "in a non-global zone"));
1982 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1986 if ((ret = changelist_prefix(cl)) != 0)
1989 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1990 return (zfs_standard_error(hdl, errno, errbuf));
1993 if ((ret = changelist_postfix(cl)) != 0)
1997 * Refresh the statistics so the new property is reflected.
1999 (void) get_stats(zhp);
2003 changelist_free(cl);
2008 * True DSL properties are stored in an nvlist. The following two functions
2009 * extract them appropriately.
2012 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2018 if (nvlist_lookup_nvlist(zhp->zfs_props,
2019 zfs_prop_to_name(prop), &nv) == 0) {
2020 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2021 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2023 verify(!zhp->zfs_props_table ||
2024 zhp->zfs_props_table[prop] == B_TRUE);
2025 value = zfs_prop_default_numeric(prop);
2033 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2039 if (nvlist_lookup_nvlist(zhp->zfs_props,
2040 zfs_prop_to_name(prop), &nv) == 0) {
2041 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2042 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2044 verify(!zhp->zfs_props_table ||
2045 zhp->zfs_props_table[prop] == B_TRUE);
2046 value = zfs_prop_default_string(prop);
2054 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2056 return (zhp->zfs_props == zhp->zfs_recvd_props);
2060 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2062 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2063 zhp->zfs_props = zhp->zfs_recvd_props;
2067 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2069 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2074 * Internal function for getting a numeric property. Both zfs_prop_get() and
2075 * zfs_prop_get_int() are built using this interface.
2077 * Certain properties can be overridden using 'mount -o'. In this case, scan
2078 * the contents of the /etc/mnttab entry, searching for the appropriate options.
2079 * If they differ from the on-disk values, report the current values and mark
2080 * the source "temporary".
2083 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2084 char **source, uint64_t *val)
2086 zfs_cmd_t zc = { 0 };
2087 nvlist_t *zplprops = NULL;
2089 char *mntopt_on = NULL;
2090 char *mntopt_off = NULL;
2091 boolean_t received = zfs_is_recvd_props_mode(zhp);
2096 case ZFS_PROP_ATIME:
2097 mntopt_on = MNTOPT_ATIME;
2098 mntopt_off = MNTOPT_NOATIME;
2101 case ZFS_PROP_DEVICES:
2102 mntopt_on = MNTOPT_DEVICES;
2103 mntopt_off = MNTOPT_NODEVICES;
2107 mntopt_on = MNTOPT_EXEC;
2108 mntopt_off = MNTOPT_NOEXEC;
2111 case ZFS_PROP_READONLY:
2112 mntopt_on = MNTOPT_RO;
2113 mntopt_off = MNTOPT_RW;
2116 case ZFS_PROP_SETUID:
2117 mntopt_on = MNTOPT_SETUID;
2118 mntopt_off = MNTOPT_NOSETUID;
2121 case ZFS_PROP_XATTR:
2122 mntopt_on = MNTOPT_XATTR;
2123 mntopt_off = MNTOPT_NOXATTR;
2126 case ZFS_PROP_NBMAND:
2127 mntopt_on = MNTOPT_NBMAND;
2128 mntopt_off = MNTOPT_NONBMAND;
2136 * Because looking up the mount options is potentially expensive
2137 * (iterating over all of /etc/mnttab), we defer its calculation until
2138 * we're looking up a property which requires its presence.
2140 if (!zhp->zfs_mntcheck &&
2141 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2142 libzfs_handle_t *hdl = zhp->zfs_hdl;
2143 struct mnttab entry;
2145 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2146 zhp->zfs_mntopts = zfs_strdup(hdl,
2148 if (zhp->zfs_mntopts == NULL)
2152 zhp->zfs_mntcheck = B_TRUE;
2155 if (zhp->zfs_mntopts == NULL)
2156 mnt.mnt_mntopts = "";
2158 mnt.mnt_mntopts = zhp->zfs_mntopts;
2161 case ZFS_PROP_ATIME:
2162 case ZFS_PROP_DEVICES:
2164 case ZFS_PROP_READONLY:
2165 case ZFS_PROP_SETUID:
2166 case ZFS_PROP_XATTR:
2167 case ZFS_PROP_NBMAND:
2168 *val = getprop_uint64(zhp, prop, source);
2173 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2176 *src = ZPROP_SRC_TEMPORARY;
2177 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2180 *src = ZPROP_SRC_TEMPORARY;
2184 case ZFS_PROP_CANMOUNT:
2185 case ZFS_PROP_VOLSIZE:
2186 case ZFS_PROP_QUOTA:
2187 case ZFS_PROP_REFQUOTA:
2188 case ZFS_PROP_RESERVATION:
2189 case ZFS_PROP_REFRESERVATION:
2190 case ZFS_PROP_FILESYSTEM_LIMIT:
2191 case ZFS_PROP_SNAPSHOT_LIMIT:
2192 case ZFS_PROP_FILESYSTEM_COUNT:
2193 case ZFS_PROP_SNAPSHOT_COUNT:
2194 *val = getprop_uint64(zhp, prop, source);
2196 if (*source == NULL) {
2197 /* not default, must be local */
2198 *source = zhp->zfs_name;
2202 case ZFS_PROP_MOUNTED:
2203 *val = (zhp->zfs_mntopts != NULL);
2206 case ZFS_PROP_NUMCLONES:
2207 *val = zhp->zfs_dmustats.dds_num_clones;
2210 case ZFS_PROP_VERSION:
2211 case ZFS_PROP_NORMALIZE:
2212 case ZFS_PROP_UTF8ONLY:
2214 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2215 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2217 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2218 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2219 zcmd_free_nvlists(&zc);
2222 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2223 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2225 zcmd_free_nvlists(&zc);
2228 nvlist_free(zplprops);
2229 zcmd_free_nvlists(&zc);
2232 case ZFS_PROP_INCONSISTENT:
2233 *val = zhp->zfs_dmustats.dds_inconsistent;
2237 switch (zfs_prop_get_type(prop)) {
2238 case PROP_TYPE_NUMBER:
2239 case PROP_TYPE_INDEX:
2240 *val = getprop_uint64(zhp, prop, source);
2242 * If we tried to use a default value for a
2243 * readonly property, it means that it was not
2244 * present. Note this only applies to "truly"
2245 * readonly properties, not set-once properties
2246 * like volblocksize.
2248 if (zfs_prop_readonly(prop) &&
2249 !zfs_prop_setonce(prop) &&
2250 *source != NULL && (*source)[0] == '\0') {
2256 case PROP_TYPE_STRING:
2258 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2259 "cannot get non-numeric property"));
2260 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2261 dgettext(TEXT_DOMAIN, "internal error")));
2269 * Calculate the source type, given the raw source string.
2272 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2273 char *statbuf, size_t statlen)
2275 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2278 if (source == NULL) {
2279 *srctype = ZPROP_SRC_NONE;
2280 } else if (source[0] == '\0') {
2281 *srctype = ZPROP_SRC_DEFAULT;
2282 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2283 *srctype = ZPROP_SRC_RECEIVED;
2285 if (strcmp(source, zhp->zfs_name) == 0) {
2286 *srctype = ZPROP_SRC_LOCAL;
2288 (void) strlcpy(statbuf, source, statlen);
2289 *srctype = ZPROP_SRC_INHERITED;
2296 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2297 size_t proplen, boolean_t literal)
2302 if (zhp->zfs_recvd_props == NULL)
2303 if (get_recvd_props_ioctl(zhp) != 0)
2306 prop = zfs_name_to_prop(propname);
2308 if (prop != ZPROP_INVAL) {
2310 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2312 zfs_set_recvd_props_mode(zhp, &cookie);
2313 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2314 NULL, NULL, 0, literal);
2315 zfs_unset_recvd_props_mode(zhp, &cookie);
2319 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2320 propname, &propval) != 0)
2322 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2324 (void) strlcpy(propbuf, recvdval, proplen);
2327 return (err == 0 ? 0 : -1);
2331 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2336 value = zfs_get_clones_nvl(zhp);
2341 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2342 pair = nvlist_next_nvpair(value, pair)) {
2343 if (propbuf[0] != '\0')
2344 (void) strlcat(propbuf, ",", proplen);
2345 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2351 struct get_clones_arg {
2355 char buf[ZFS_MAX_DATASET_NAME_LEN];
2359 get_clones_cb(zfs_handle_t *zhp, void *arg)
2361 struct get_clones_arg *gca = arg;
2363 if (gca->numclones == 0) {
2368 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2369 NULL, NULL, 0, B_TRUE) != 0)
2371 if (strcmp(gca->buf, gca->origin) == 0) {
2372 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2377 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2383 zfs_get_clones_nvl(zfs_handle_t *zhp)
2385 nvlist_t *nv, *value;
2387 if (nvlist_lookup_nvlist(zhp->zfs_props,
2388 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2389 struct get_clones_arg gca;
2392 * if this is a snapshot, then the kernel wasn't able
2393 * to get the clones. Do it by slowly iterating.
2395 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2397 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2399 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2404 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2406 gca.origin = zhp->zfs_name;
2408 if (gca.numclones != 0) {
2410 char pool[ZFS_MAX_DATASET_NAME_LEN];
2413 /* get the pool name */
2414 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2415 (void) strsep(&cp, "/@");
2416 root = zfs_open(zhp->zfs_hdl, pool,
2417 ZFS_TYPE_FILESYSTEM);
2419 (void) get_clones_cb(root, &gca);
2422 if (gca.numclones != 0 ||
2423 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2424 nvlist_add_nvlist(zhp->zfs_props,
2425 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2432 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2433 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2436 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2442 * Accepts a property and value and checks that the value
2443 * matches the one found by the channel program. If they are
2444 * not equal, print both of them.
2447 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2450 if (!zhp->zfs_hdl->libzfs_prop_debug)
2453 char *poolname = zhp->zpool_hdl->zpool_name;
2454 const char *program =
2456 "ds = args['dataset']\n"
2457 "prop = args['property']\n"
2458 "value, setpoint = zfs.get_prop(ds, prop)\n"
2459 "return {value=value, setpoint=setpoint}\n";
2462 nvlist_t *argnvl = fnvlist_alloc();
2464 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2465 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2467 error = lzc_channel_program_nosync(poolname, program,
2468 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2471 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2472 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2474 error = nvlist_lookup_int64(retnvl, "value", &ans);
2476 (void) fprintf(stderr, "zcp check error: %u\n",
2480 if (ans != intval) {
2481 (void) fprintf(stderr,
2482 "%s: zfs found %lld, but zcp found %lld\n",
2483 zfs_prop_to_name(prop),
2484 (longlong_t)intval, (longlong_t)ans);
2488 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2490 (void) fprintf(stderr, "zcp check error: %u\n",
2494 if (strcmp(strval, str_ans) != 0) {
2495 (void) fprintf(stderr,
2496 "%s: zfs found %s, but zcp found %s\n",
2497 zfs_prop_to_name(prop),
2502 (void) fprintf(stderr,
2503 "zcp check failed, channel program error: %u\n", error);
2505 nvlist_free(argnvl);
2506 nvlist_free(outnvl);
2510 * Retrieve a property from the given object. If 'literal' is specified, then
2511 * numbers are left as exact values. Otherwise, numbers are converted to a
2512 * human-readable form.
2514 * Returns 0 on success, or -1 on error.
2517 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2518 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2520 char *source = NULL;
2524 boolean_t received = zfs_is_recvd_props_mode(zhp);
2527 * Check to see if this property applies to our object
2529 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2532 if (received && zfs_prop_readonly(prop))
2536 *src = ZPROP_SRC_NONE;
2539 case ZFS_PROP_CREATION:
2541 * 'creation' is a time_t stored in the statistics. We convert
2542 * this into a string unless 'literal' is specified.
2545 val = getprop_uint64(zhp, prop, &source);
2546 time_t time = (time_t)val;
2550 localtime_r(&time, &t) == NULL ||
2551 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2553 (void) snprintf(propbuf, proplen, "%llu", val);
2555 zcp_check(zhp, prop, val, NULL);
2558 case ZFS_PROP_MOUNTPOINT:
2560 * Getting the precise mountpoint can be tricky.
2562 * - for 'none' or 'legacy', return those values.
2563 * - for inherited mountpoints, we want to take everything
2564 * after our ancestor and append it to the inherited value.
2566 * If the pool has an alternate root, we want to prepend that
2567 * root to any values we return.
2570 str = getprop_string(zhp, prop, &source);
2572 if (str[0] == '/') {
2573 char buf[MAXPATHLEN];
2575 const char *relpath;
2578 * If we inherit the mountpoint, even from a dataset
2579 * with a received value, the source will be the path of
2580 * the dataset we inherit from. If source is
2581 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2584 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2587 relpath = zhp->zfs_name + strlen(source);
2588 if (relpath[0] == '/')
2592 if ((zpool_get_prop(zhp->zpool_hdl,
2593 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2594 B_FALSE)) || (strcmp(root, "-") == 0))
2597 * Special case an alternate root of '/'. This will
2598 * avoid having multiple leading slashes in the
2601 if (strcmp(root, "/") == 0)
2605 * If the mountpoint is '/' then skip over this
2606 * if we are obtaining either an alternate root or
2607 * an inherited mountpoint.
2609 if (str[1] == '\0' && (root[0] != '\0' ||
2610 relpath[0] != '\0'))
2613 if (relpath[0] == '\0')
2614 (void) snprintf(propbuf, proplen, "%s%s",
2617 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2618 root, str, relpath[0] == '@' ? "" : "/",
2621 /* 'legacy' or 'none' */
2622 (void) strlcpy(propbuf, str, proplen);
2624 zcp_check(zhp, prop, NULL, propbuf);
2627 case ZFS_PROP_ORIGIN:
2628 str = getprop_string(zhp, prop, &source);
2631 (void) strlcpy(propbuf, str, proplen);
2632 zcp_check(zhp, prop, NULL, str);
2635 case ZFS_PROP_CLONES:
2636 if (get_clones_string(zhp, propbuf, proplen) != 0)
2640 case ZFS_PROP_QUOTA:
2641 case ZFS_PROP_REFQUOTA:
2642 case ZFS_PROP_RESERVATION:
2643 case ZFS_PROP_REFRESERVATION:
2645 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2648 * If quota or reservation is 0, we translate this into 'none'
2649 * (unless literal is set), and indicate that it's the default
2650 * value. Otherwise, we print the number nicely and indicate
2651 * that its set locally.
2655 (void) strlcpy(propbuf, "0", proplen);
2657 (void) strlcpy(propbuf, "none", proplen);
2660 (void) snprintf(propbuf, proplen, "%llu",
2663 zfs_nicenum(val, propbuf, proplen);
2665 zcp_check(zhp, prop, val, NULL);
2668 case ZFS_PROP_FILESYSTEM_LIMIT:
2669 case ZFS_PROP_SNAPSHOT_LIMIT:
2670 case ZFS_PROP_FILESYSTEM_COUNT:
2671 case ZFS_PROP_SNAPSHOT_COUNT:
2673 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2677 * If limit is UINT64_MAX, we translate this into 'none' (unless
2678 * literal is set), and indicate that it's the default value.
2679 * Otherwise, we print the number nicely and indicate that it's
2683 (void) snprintf(propbuf, proplen, "%llu",
2685 } else if (val == UINT64_MAX) {
2686 (void) strlcpy(propbuf, "none", proplen);
2688 zfs_nicenum(val, propbuf, proplen);
2691 zcp_check(zhp, prop, val, NULL);
2694 case ZFS_PROP_REFRATIO:
2695 case ZFS_PROP_COMPRESSRATIO:
2696 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2698 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2699 (u_longlong_t)(val / 100),
2700 (u_longlong_t)(val % 100));
2701 zcp_check(zhp, prop, val, NULL);
2705 switch (zhp->zfs_type) {
2706 case ZFS_TYPE_FILESYSTEM:
2709 case ZFS_TYPE_VOLUME:
2712 case ZFS_TYPE_SNAPSHOT:
2715 case ZFS_TYPE_BOOKMARK:
2721 (void) snprintf(propbuf, proplen, "%s", str);
2722 zcp_check(zhp, prop, NULL, propbuf);
2725 case ZFS_PROP_MOUNTED:
2727 * The 'mounted' property is a pseudo-property that described
2728 * whether the filesystem is currently mounted. Even though
2729 * it's a boolean value, the typical values of "on" and "off"
2730 * don't make sense, so we translate to "yes" and "no".
2732 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2733 src, &source, &val) != 0)
2736 (void) strlcpy(propbuf, "yes", proplen);
2738 (void) strlcpy(propbuf, "no", proplen);
2743 * The 'name' property is a pseudo-property derived from the
2744 * dataset name. It is presented as a real property to simplify
2747 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2748 zcp_check(zhp, prop, NULL, propbuf);
2751 case ZFS_PROP_MLSLABEL:
2754 m_label_t *new_sl = NULL;
2755 char *ascii = NULL; /* human readable label */
2757 (void) strlcpy(propbuf,
2758 getprop_string(zhp, prop, &source), proplen);
2760 if (literal || (strcasecmp(propbuf,
2761 ZFS_MLSLABEL_DEFAULT) == 0))
2765 * Try to translate the internal hex string to
2766 * human-readable output. If there are any
2767 * problems just use the hex string.
2770 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2771 L_NO_CORRECTION, NULL) == -1) {
2772 m_label_free(new_sl);
2776 if (label_to_str(new_sl, &ascii, M_LABEL,
2780 m_label_free(new_sl);
2783 m_label_free(new_sl);
2785 (void) strlcpy(propbuf, ascii, proplen);
2787 #else /* !illumos */
2789 #endif /* illumos */
2794 case ZFS_PROP_CREATETXG:
2796 * GUIDs are stored as numbers, but they are identifiers.
2797 * We don't want them to be pretty printed, because pretty
2798 * printing mangles the ID into a truncated and useless value.
2800 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2802 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2803 zcp_check(zhp, prop, val, NULL);
2807 switch (zfs_prop_get_type(prop)) {
2808 case PROP_TYPE_NUMBER:
2809 if (get_numeric_property(zhp, prop, src,
2810 &source, &val) != 0) {
2815 (void) snprintf(propbuf, proplen, "%llu",
2818 zfs_nicenum(val, propbuf, proplen);
2820 zcp_check(zhp, prop, val, NULL);
2823 case PROP_TYPE_STRING:
2824 str = getprop_string(zhp, prop, &source);
2828 (void) strlcpy(propbuf, str, proplen);
2829 zcp_check(zhp, prop, NULL, str);
2832 case PROP_TYPE_INDEX:
2833 if (get_numeric_property(zhp, prop, src,
2834 &source, &val) != 0)
2836 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2839 (void) strlcpy(propbuf, strval, proplen);
2840 zcp_check(zhp, prop, NULL, strval);
2848 get_source(zhp, src, source, statbuf, statlen);
2854 * Utility function to get the given numeric property. Does no validation that
2855 * the given property is the appropriate type; should only be used with
2856 * hard-coded property types.
2859 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2864 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2870 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2874 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2875 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2879 * Similar to zfs_prop_get(), but returns the value as an integer.
2882 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2883 zprop_source_t *src, char *statbuf, size_t statlen)
2888 * Check to see if this property applies to our object
2890 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2891 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2892 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2893 zfs_prop_to_name(prop)));
2897 *src = ZPROP_SRC_NONE;
2899 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2902 get_source(zhp, src, source, statbuf, statlen);
2908 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2909 char **domainp, idmap_rid_t *ridp)
2912 idmap_get_handle_t *get_hdl = NULL;
2916 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2920 err = idmap_get_sidbyuid(get_hdl, id,
2921 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2923 err = idmap_get_sidbygid(get_hdl, id,
2924 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2926 if (err == IDMAP_SUCCESS &&
2927 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2928 status == IDMAP_SUCCESS)
2934 idmap_get_destroy(get_hdl);
2936 #else /* !illumos */
2937 assert(!"invalid code path");
2938 return (EINVAL); // silence compiler warning
2939 #endif /* illumos */
2943 * convert the propname into parameters needed by kernel
2944 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2945 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2948 userquota_propname_decode(const char *propname, boolean_t zoned,
2949 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2951 zfs_userquota_prop_t type;
2953 char *numericsid = NULL;
2958 /* Figure out the property type ({user|group}{quota|space}) */
2959 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2960 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2961 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2964 if (type == ZFS_NUM_USERQUOTA_PROPS)
2968 isuser = (type == ZFS_PROP_USERQUOTA ||
2969 type == ZFS_PROP_USERUSED);
2971 cp = strchr(propname, '@') + 1;
2973 if (strchr(cp, '@')) {
2976 * It's a SID name (eg "user@domain") that needs to be
2977 * turned into S-1-domainID-RID.
2980 idmap_stat stat, map_stat;
2983 idmap_get_handle_t *gh = NULL;
2985 stat = idmap_get_create(&gh);
2986 if (stat != IDMAP_SUCCESS) {
2987 idmap_get_destroy(gh);
2990 if (zoned && getzoneid() == GLOBAL_ZONEID)
2993 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2996 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2999 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
3002 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
3006 idmap_get_destroy(gh);
3009 stat = idmap_get_mappings(gh);
3010 idmap_get_destroy(gh);
3015 if (numericsid == NULL)
3019 /* will be further decoded below */
3020 #else /* !illumos */
3022 #endif /* illumos */
3025 if (strncmp(cp, "S-1-", 4) == 0) {
3026 /* It's a numeric SID (eg "S-1-234-567-89") */
3027 (void) strlcpy(domain, cp, domainlen);
3030 cp = strrchr(domain, '-');
3033 *ridp = strtoull(cp, &end, 10);
3041 if (errno != 0 || *end != '\0')
3043 } else if (!isdigit(*cp)) {
3045 * It's a user/group name (eg "user") that needs to be
3046 * turned into a uid/gid
3048 if (zoned && getzoneid() == GLOBAL_ZONEID)
3064 /* It's a user/group ID (eg "12345"). */
3065 uid_t id = strtoul(cp, &end, 10);
3072 /* It's an ephemeral ID. */
3073 if (idmap_id_to_numeric_domain_rid(id, isuser,
3074 &mapdomain, &rid) != 0)
3076 (void) strlcpy(domain, mapdomain, domainlen);
3083 ASSERT3P(numericsid, ==, NULL);
3088 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3089 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3092 zfs_cmd_t zc = { 0 };
3094 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3096 err = userquota_propname_decode(propname,
3097 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3098 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3099 zc.zc_objset_type = *typep;
3103 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3107 *propvalue = zc.zc_cookie;
3112 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3113 uint64_t *propvalue)
3115 zfs_userquota_prop_t type;
3117 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3122 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3123 char *propbuf, int proplen, boolean_t literal)
3127 zfs_userquota_prop_t type;
3129 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3136 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3137 } else if (propvalue == 0 &&
3138 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
3139 (void) strlcpy(propbuf, "none", proplen);
3141 zfs_nicenum(propvalue, propbuf, proplen);
3147 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3148 uint64_t *propvalue)
3151 zfs_cmd_t zc = { 0 };
3152 const char *snapname;
3154 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3156 snapname = strchr(propname, '@') + 1;
3157 if (strchr(snapname, '@')) {
3158 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3160 /* snapname is the short name, append it to zhp's fsname */
3163 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3164 sizeof (zc.zc_value));
3165 cp = strchr(zc.zc_value, '@');
3168 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3169 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3172 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3176 *propvalue = zc.zc_cookie;
3181 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3182 char *propbuf, int proplen, boolean_t literal)
3187 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3193 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3195 zfs_nicenum(propvalue, propbuf, proplen);
3201 * Returns the name of the given zfs handle.
3204 zfs_get_name(const zfs_handle_t *zhp)
3206 return (zhp->zfs_name);
3210 * Returns the name of the parent pool for the given zfs handle.
3213 zfs_get_pool_name(const zfs_handle_t *zhp)
3215 return (zhp->zpool_hdl->zpool_name);
3219 * Returns the type of the given zfs handle.
3222 zfs_get_type(const zfs_handle_t *zhp)
3224 return (zhp->zfs_type);
3228 * Is one dataset name a child dataset of another?
3230 * Needs to handle these cases:
3231 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3232 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3233 * Descendant? No. No. No. Yes.
3236 is_descendant(const char *ds1, const char *ds2)
3238 size_t d1len = strlen(ds1);
3240 /* ds2 can't be a descendant if it's smaller */
3241 if (strlen(ds2) < d1len)
3244 /* otherwise, compare strings and verify that there's a '/' char */
3245 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3249 * Given a complete name, return just the portion that refers to the parent.
3250 * Will return -1 if there is no parent (path is just the name of the
3254 parent_name(const char *path, char *buf, size_t buflen)
3258 (void) strlcpy(buf, path, buflen);
3260 if ((slashp = strrchr(buf, '/')) == NULL)
3268 * If accept_ancestor is false, then check to make sure that the given path has
3269 * a parent, and that it exists. If accept_ancestor is true, then find the
3270 * closest existing ancestor for the given path. In prefixlen return the
3271 * length of already existing prefix of the given path. We also fetch the
3272 * 'zoned' property, which is used to validate property settings when creating
3276 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3277 boolean_t accept_ancestor, int *prefixlen)
3279 zfs_cmd_t zc = { 0 };
3280 char parent[ZFS_MAX_DATASET_NAME_LEN];
3286 (void) snprintf(errbuf, sizeof (errbuf),
3287 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3289 /* get parent, and check to see if this is just a pool */
3290 if (parent_name(path, parent, sizeof (parent)) != 0) {
3291 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3292 "missing dataset name"));
3293 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3296 /* check to see if the pool exists */
3297 if ((slash = strchr(parent, '/')) == NULL)
3298 slash = parent + strlen(parent);
3299 (void) strncpy(zc.zc_name, parent, slash - parent);
3300 zc.zc_name[slash - parent] = '\0';
3301 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3304 "no such pool '%s'"), zc.zc_name);
3305 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3308 /* check to see if the parent dataset exists */
3309 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3310 if (errno == ENOENT && accept_ancestor) {
3312 * Go deeper to find an ancestor, give up on top level.
3314 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3316 "no such pool '%s'"), zc.zc_name);
3317 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3319 } else if (errno == ENOENT) {
3320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3321 "parent does not exist"));
3322 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3324 return (zfs_standard_error(hdl, errno, errbuf));
3327 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3331 /* we are in a non-global zone, but parent is in the global zone */
3332 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3333 (void) zfs_standard_error(hdl, EPERM, errbuf);
3338 /* make sure parent is a filesystem */
3339 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3341 "parent is not a filesystem"));
3342 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3348 if (prefixlen != NULL)
3349 *prefixlen = strlen(parent);
3354 * Finds whether the dataset of the given type(s) exists.
3357 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3361 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3365 * Try to get stats for the dataset, which will tell us if it exists.
3367 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3368 int ds_type = zhp->zfs_type;
3371 if (types & ds_type)
3378 * Given a path to 'target', create all the ancestors between
3379 * the prefixlen portion of the path, and the target itself.
3380 * Fail if the initial prefixlen-ancestor does not already exist.
3383 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3389 /* make sure prefix exists */
3390 cp = target + prefixlen;
3392 assert(strchr(cp, '/') == NULL);
3393 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3396 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3404 * Attempt to create, mount, and share any ancestor filesystems,
3405 * up to the prefixlen-long one.
3407 for (cp = target + prefixlen + 1;
3408 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3412 h = make_dataset_handle(hdl, target);
3414 /* it already exists, nothing to do here */
3419 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3421 opname = dgettext(TEXT_DOMAIN, "create");
3425 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3427 opname = dgettext(TEXT_DOMAIN, "open");
3431 if (zfs_mount(h, NULL, 0) != 0) {
3432 opname = dgettext(TEXT_DOMAIN, "mount");
3436 if (zfs_share(h) != 0) {
3437 opname = dgettext(TEXT_DOMAIN, "share");
3447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3448 "failed to %s ancestor '%s'"), opname, target);
3453 * Creates non-existing ancestors of the given path.
3456 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3463 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3464 "cannot create '%s'"), path);
3467 * Check that we are not passing the nesting limit
3468 * before we start creating any ancestors.
3470 if (dataset_nestcheck(path) != 0) {
3471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3472 "maximum name nesting depth exceeded"));
3473 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3476 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3479 if ((path_copy = strdup(path)) != NULL) {
3480 rc = create_parents(hdl, path_copy, prefix);
3483 if (path_copy == NULL || rc != 0)
3490 * Create a new filesystem or volume.
3493 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3498 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3501 enum lzc_dataset_type ost;
3502 zpool_handle_t *zpool_handle;
3504 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3505 "cannot create '%s'"), path);
3507 /* validate the path, taking care to note the extended error message */
3508 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3509 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3511 if (dataset_nestcheck(path) != 0) {
3512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3513 "maximum name nesting depth exceeded"));
3514 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3517 /* validate parents exist */
3518 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3522 * The failure modes when creating a dataset of a different type over
3523 * one that already exists is a little strange. In particular, if you
3524 * try to create a dataset on top of an existing dataset, the ioctl()
3525 * will return ENOENT, not EEXIST. To prevent this from happening, we
3526 * first try to see if the dataset exists.
3528 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3530 "dataset already exists"));
3531 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3534 if (type == ZFS_TYPE_VOLUME)
3535 ost = LZC_DATSET_TYPE_ZVOL;
3537 ost = LZC_DATSET_TYPE_ZFS;
3539 /* open zpool handle for prop validation */
3540 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3541 (void) strlcpy(pool_path, path, sizeof (pool_path));
3543 /* truncate pool_path at first slash */
3544 char *p = strchr(pool_path, '/');
3548 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3551 if (props && (props = zfs_valid_proplist(hdl, type, props,
3552 zoned, NULL, zpool_handle, errbuf)) == 0) {
3553 zpool_close(zpool_handle);
3556 zpool_close(zpool_handle);
3558 if (type == ZFS_TYPE_VOLUME) {
3560 * If we are creating a volume, the size and block size must
3561 * satisfy a few restraints. First, the blocksize must be a
3562 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3563 * volsize must be a multiple of the block size, and cannot be
3566 if (props == NULL || nvlist_lookup_uint64(props,
3567 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3570 "missing volume size"));
3571 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3574 if ((ret = nvlist_lookup_uint64(props,
3575 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3576 &blocksize)) != 0) {
3577 if (ret == ENOENT) {
3578 blocksize = zfs_prop_default_numeric(
3579 ZFS_PROP_VOLBLOCKSIZE);
3582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3583 "missing volume block size"));
3584 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3591 "volume size cannot be zero"));
3592 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3595 if (size % blocksize != 0) {
3597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3598 "volume size must be a multiple of volume block "
3600 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3604 /* create the dataset */
3605 ret = lzc_create(path, ost, props);
3608 /* check for failure */
3610 char parent[ZFS_MAX_DATASET_NAME_LEN];
3611 (void) parent_name(path, parent, sizeof (parent));
3615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3616 "no such parent '%s'"), parent);
3617 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3621 "parent '%s' is not a filesystem"), parent);
3622 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3626 "pool must be upgraded to set this "
3627 "property or value"));
3628 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3631 "invalid property value(s) specified"));
3632 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3636 * This platform can't address a volume this big.
3638 if (type == ZFS_TYPE_VOLUME)
3639 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3644 return (zfs_standard_error(hdl, errno, errbuf));
3652 * Destroys the given dataset. The caller must make sure that the filesystem
3653 * isn't mounted, and that there are no active dependents. If the file system
3654 * does not exist this function does nothing.
3657 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3661 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3664 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3665 nvlist_t *nv = fnvlist_alloc();
3666 fnvlist_add_boolean(nv, zhp->zfs_name);
3667 error = lzc_destroy_bookmarks(nv, NULL);
3670 return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3671 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3677 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3678 nvlist_t *nv = fnvlist_alloc();
3679 fnvlist_add_boolean(nv, zhp->zfs_name);
3680 error = lzc_destroy_snaps(nv, defer, NULL);
3683 error = lzc_destroy(zhp->zfs_name);
3686 if (error != 0 && error != ENOENT) {
3687 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3688 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3692 remove_mountpoint(zhp);
3697 struct destroydata {
3699 const char *snapname;
3703 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3705 struct destroydata *dd = arg;
3706 char name[ZFS_MAX_DATASET_NAME_LEN];
3709 (void) snprintf(name, sizeof (name),
3710 "%s@%s", zhp->zfs_name, dd->snapname);
3712 if (lzc_exists(name))
3713 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3715 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3721 * Destroys all snapshots with the given name in zhp & descendants.
3724 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3727 struct destroydata dd = { 0 };
3729 dd.snapname = snapname;
3730 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3731 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3733 if (nvlist_empty(dd.nvl)) {
3734 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3735 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3736 zhp->zfs_name, snapname);
3738 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3740 nvlist_free(dd.nvl);
3745 * Destroys all the snapshots named in the nvlist.
3748 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3751 nvlist_t *errlist = NULL;
3753 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3756 nvlist_free(errlist);
3760 if (nvlist_empty(errlist)) {
3762 (void) snprintf(errbuf, sizeof (errbuf),
3763 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3765 ret = zfs_standard_error(hdl, ret, errbuf);
3767 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3768 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3770 (void) snprintf(errbuf, sizeof (errbuf),
3771 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3774 switch (fnvpair_value_int32(pair)) {
3777 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3778 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3781 ret = zfs_standard_error(hdl, errno, errbuf);
3786 nvlist_free(errlist);
3791 * Clones the given dataset. The target must be of the same type as the source.
3794 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3796 char parent[ZFS_MAX_DATASET_NAME_LEN];
3799 libzfs_handle_t *hdl = zhp->zfs_hdl;
3802 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3804 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3805 "cannot create '%s'"), target);
3807 /* validate the target/clone name */
3808 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3809 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3811 /* validate parents exist */
3812 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3815 (void) parent_name(target, parent, sizeof (parent));
3822 if (ZFS_IS_VOLUME(zhp)) {
3823 type = ZFS_TYPE_VOLUME;
3825 type = ZFS_TYPE_FILESYSTEM;
3827 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3828 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3830 if (zfs_fix_auto_resv(zhp, props) == -1) {
3836 ret = lzc_clone(target, zhp->zfs_name, props);
3844 * The parent doesn't exist. We should have caught this
3845 * above, but there may a race condition that has since
3846 * destroyed the parent.
3848 * At this point, we don't know whether it's the source
3849 * that doesn't exist anymore, or whether the target
3850 * dataset doesn't exist.
3852 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3853 "no such parent '%s'"), parent);
3854 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3857 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3858 "source and target pools differ"));
3859 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3863 return (zfs_standard_error(zhp->zfs_hdl, errno,
3872 * Promotes the given clone fs to be the clone parent.
3875 zfs_promote(zfs_handle_t *zhp)
3877 libzfs_handle_t *hdl = zhp->zfs_hdl;
3878 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3882 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3883 "cannot promote '%s'"), zhp->zfs_name);
3885 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3886 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3887 "snapshots can not be promoted"));
3888 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3891 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3892 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3893 "not a cloned filesystem"));
3894 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3897 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3898 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3900 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3905 /* There is a conflicting snapshot name. */
3906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3907 "conflicting snapshot '%s' from parent '%s'"),
3908 snapname, zhp->zfs_dmustats.dds_origin);
3909 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3912 return (zfs_standard_error(hdl, ret, errbuf));
3918 typedef struct snapdata {
3920 const char *sd_snapname;
3924 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3926 snapdata_t *sd = arg;
3927 char name[ZFS_MAX_DATASET_NAME_LEN];
3930 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3931 (void) snprintf(name, sizeof (name),
3932 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3934 fnvlist_add_boolean(sd->sd_nvl, name);
3936 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3944 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3949 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3950 "cannot remap dataset '%s'"), fs);
3952 err = lzc_remap(fs);
3957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3958 "pool must be upgraded"));
3959 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3962 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3965 (void) zfs_standard_error(hdl, err, errbuf);
3974 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3978 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3985 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3986 "cannot create snapshots "));
3989 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3990 const char *snapname = nvpair_name(elem);
3992 /* validate the target name */
3993 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3995 (void) snprintf(errbuf, sizeof (errbuf),
3996 dgettext(TEXT_DOMAIN,
3997 "cannot create snapshot '%s'"), snapname);
3998 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4003 * get pool handle for prop validation. assumes all snaps are in the
4004 * same pool, as does lzc_snapshot (below).
4006 char pool[ZFS_MAX_DATASET_NAME_LEN];
4007 elem = nvlist_next_nvpair(snaps, NULL);
4008 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4009 pool[strcspn(pool, "/@")] = '\0';
4010 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
4012 if (props != NULL &&
4013 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4014 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
4015 zpool_close(zpool_hdl);
4018 zpool_close(zpool_hdl);
4020 ret = lzc_snapshot(snaps, props, &errors);
4023 boolean_t printed = B_FALSE;
4024 for (elem = nvlist_next_nvpair(errors, NULL);
4026 elem = nvlist_next_nvpair(errors, elem)) {
4027 (void) snprintf(errbuf, sizeof (errbuf),
4028 dgettext(TEXT_DOMAIN,
4029 "cannot create snapshot '%s'"), nvpair_name(elem));
4030 (void) zfs_standard_error(hdl,
4031 fnvpair_value_int32(elem), errbuf);
4037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4038 "multiple snapshots of same "
4040 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4044 (void) zfs_standard_error(hdl, ret, errbuf);
4050 nvlist_free(errors);
4055 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4059 snapdata_t sd = { 0 };
4060 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4065 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4066 "cannot snapshot %s"), path);
4068 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4069 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4071 (void) strlcpy(fsname, path, sizeof (fsname));
4072 cp = strchr(fsname, '@');
4074 sd.sd_snapname = cp + 1;
4076 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4077 ZFS_TYPE_VOLUME)) == NULL) {
4081 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4083 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4085 fnvlist_add_boolean(sd.sd_nvl, path);
4088 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4089 nvlist_free(sd.sd_nvl);
4095 * Destroy any more recent snapshots. We invoke this callback on any dependents
4096 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4097 * is a dependent and we should just destroy it without checking the transaction
4100 typedef struct rollback_data {
4101 const char *cb_target; /* the snapshot */
4102 uint64_t cb_create; /* creation time reference */
4108 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4110 rollback_data_t *cbp = data;
4111 prop_changelist_t *clp;
4113 /* We must destroy this clone; first unmount it */
4114 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4115 cbp->cb_force ? MS_FORCE: 0);
4116 if (clp == NULL || changelist_prefix(clp) != 0) {
4117 cbp->cb_error = B_TRUE;
4121 if (zfs_destroy(zhp, B_FALSE) != 0)
4122 cbp->cb_error = B_TRUE;
4124 changelist_remove(clp, zhp->zfs_name);
4125 (void) changelist_postfix(clp);
4126 changelist_free(clp);
4133 rollback_destroy(zfs_handle_t *zhp, void *data)
4135 rollback_data_t *cbp = data;
4137 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4138 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4139 rollback_destroy_dependent, cbp);
4141 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4149 * Given a dataset, rollback to a specific snapshot, discarding any
4150 * data changes since then and making it the active dataset.
4152 * Any snapshots and bookmarks more recent than the target are
4153 * destroyed, along with their dependents (i.e. clones).
4156 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4158 rollback_data_t cb = { 0 };
4160 boolean_t restore_resv = 0;
4161 uint64_t old_volsize = 0, new_volsize;
4162 zfs_prop_t resv_prop;
4164 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4165 zhp->zfs_type == ZFS_TYPE_VOLUME);
4168 * Destroy all recent snapshots and their dependents.
4170 cb.cb_force = force;
4171 cb.cb_target = snap->zfs_name;
4172 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4173 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4174 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4180 * Now that we have verified that the snapshot is the latest,
4181 * rollback to the given snapshot.
4184 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4185 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4187 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4189 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4193 * Pass both the filesystem and the wanted snapshot names,
4194 * we would get an error back if the snapshot is destroyed or
4195 * a new snapshot is created before this request is processed.
4197 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4201 (void) snprintf(errbuf, sizeof (errbuf),
4202 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4206 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4207 "there is a snapshot or bookmark more recent "
4208 "than '%s'"), snap->zfs_name);
4209 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4212 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4213 "'%s' is not found among snapshots of '%s'"),
4214 snap->zfs_name, zhp->zfs_name);
4215 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4218 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4221 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4227 * For volumes, if the pre-rollback volsize matched the pre-
4228 * rollback reservation and the volsize has changed then set
4229 * the reservation property to the post-rollback volsize.
4230 * Make a new handle since the rollback closed the dataset.
4232 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4233 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4235 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4236 if (old_volsize != new_volsize)
4237 err = zfs_prop_set_int(zhp, resv_prop,
4246 * Renames the given dataset.
4249 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
4250 renameflags_t flags)
4253 zfs_cmd_t zc = { 0 };
4255 prop_changelist_t *cl = NULL;
4256 zfs_handle_t *zhrp = NULL;
4257 char *parentname = NULL;
4258 char parent[ZFS_MAX_DATASET_NAME_LEN];
4259 char property[ZFS_MAXPROPLEN];
4260 libzfs_handle_t *hdl = zhp->zfs_hdl;
4263 /* if we have the same exact name, just return success */
4264 if (strcmp(zhp->zfs_name, target) == 0)
4267 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4268 "cannot rename to '%s'"), target);
4270 if (source != NULL) {
4272 * This is recursive snapshots rename, put snapshot name
4273 * (that might not exist) into zfs_name.
4275 assert(flags.recurse);
4277 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
4278 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
4279 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
4282 /* make sure source name is valid */
4283 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4284 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4287 * Make sure the target name is valid
4289 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4290 if ((strchr(target, '@') == NULL) ||
4293 * Snapshot target name is abbreviated,
4294 * reconstruct full dataset name
4296 (void) strlcpy(parent, zhp->zfs_name,
4298 delim = strchr(parent, '@');
4299 if (strchr(target, '@') == NULL)
4303 (void) strlcat(parent, target, sizeof (parent));
4307 * Make sure we're renaming within the same dataset.
4309 delim = strchr(target, '@');
4310 if (strncmp(zhp->zfs_name, target, delim - target)
4311 != 0 || zhp->zfs_name[delim - target] != '@') {
4312 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4313 "snapshots must be part of same "
4315 return (zfs_error(hdl, EZFS_CROSSTARGET,
4320 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4321 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4323 if (flags.recurse) {
4324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4325 "recursive rename must be a snapshot"));
4326 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4329 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4330 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4332 /* validate parents */
4333 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4336 /* make sure we're in the same pool */
4337 verify((delim = strchr(target, '/')) != NULL);
4338 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4339 zhp->zfs_name[delim - target] != '/') {
4340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4341 "datasets must be within same pool"));
4342 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4345 /* new name cannot be a child of the current dataset name */
4346 if (is_descendant(zhp->zfs_name, target)) {
4347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4348 "New dataset name cannot be a descendant of "
4349 "current dataset name"));
4350 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4354 (void) snprintf(errbuf, sizeof (errbuf),
4355 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4357 if (getzoneid() == GLOBAL_ZONEID &&
4358 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4359 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4360 "dataset is used in a non-global zone"));
4361 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4365 * Avoid unmounting file systems with mountpoint property set to
4366 * 'legacy' or 'none' even if -u option is not given.
4368 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4369 !flags.recurse && !flags.nounmount &&
4370 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4371 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4372 (strcmp(property, "legacy") == 0 ||
4373 strcmp(property, "none") == 0)) {
4374 flags.nounmount = B_TRUE;
4376 if (flags.recurse) {
4378 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4379 if (parentname == NULL) {
4383 delim = strchr(parentname, '@');
4385 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4390 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4391 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4392 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
4393 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
4397 if (changelist_haszonedchild(cl)) {
4398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4399 "child dataset with inherited mountpoint is used "
4400 "in a non-global zone"));
4401 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4406 if ((ret = changelist_prefix(cl)) != 0)
4410 if (ZFS_IS_VOLUME(zhp))
4411 zc.zc_objset_type = DMU_OST_ZVOL;
4413 zc.zc_objset_type = DMU_OST_ZFS;
4415 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4416 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4418 zc.zc_cookie = flags.recurse ? 1 : 0;
4419 if (flags.nounmount)
4422 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4424 * if it was recursive, the one that actually failed will
4427 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4428 "cannot rename '%s'"), zc.zc_name);
4430 if (flags.recurse && errno == EEXIST) {
4431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4432 "a child dataset already has a snapshot "
4433 "with the new name"));
4434 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4436 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4440 * On failure, we still want to remount any filesystems that
4441 * were previously mounted, so we don't alter the system state.
4444 (void) changelist_postfix(cl);
4447 changelist_rename(cl, zfs_get_name(zhp), target);
4448 ret = changelist_postfix(cl);
4453 if (parentname != NULL) {
4460 changelist_free(cl);
4466 zfs_get_user_props(zfs_handle_t *zhp)
4468 return (zhp->zfs_user_props);
4472 zfs_get_recvd_props(zfs_handle_t *zhp)
4474 if (zhp->zfs_recvd_props == NULL)
4475 if (get_recvd_props_ioctl(zhp) != 0)
4477 return (zhp->zfs_recvd_props);
4481 * This function is used by 'zfs list' to determine the exact set of columns to
4482 * display, and their maximum widths. This does two main things:
4484 * - If this is a list of all properties, then expand the list to include
4485 * all native properties, and set a flag so that for each dataset we look
4486 * for new unique user properties and add them to the list.
4488 * - For non fixed-width properties, keep track of the maximum width seen
4489 * so that we can size the column appropriately. If the user has
4490 * requested received property values, we also need to compute the width
4491 * of the RECEIVED column.
4494 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4497 libzfs_handle_t *hdl = zhp->zfs_hdl;
4498 zprop_list_t *entry;
4499 zprop_list_t **last, **start;
4500 nvlist_t *userprops, *propval;
4503 char buf[ZFS_MAXPROPLEN];
4505 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4508 userprops = zfs_get_user_props(zhp);
4511 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4513 * Go through and add any user properties as necessary. We
4514 * start by incrementing our list pointer to the first
4515 * non-native property.
4518 while (*start != NULL) {
4519 if ((*start)->pl_prop == ZPROP_INVAL)
4521 start = &(*start)->pl_next;
4525 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4527 * See if we've already found this property in our list.
4529 for (last = start; *last != NULL;
4530 last = &(*last)->pl_next) {
4531 if (strcmp((*last)->pl_user_prop,
4532 nvpair_name(elem)) == 0)
4536 if (*last == NULL) {
4537 if ((entry = zfs_alloc(hdl,
4538 sizeof (zprop_list_t))) == NULL ||
4539 ((entry->pl_user_prop = zfs_strdup(hdl,
4540 nvpair_name(elem)))) == NULL) {
4545 entry->pl_prop = ZPROP_INVAL;
4546 entry->pl_width = strlen(nvpair_name(elem));
4547 entry->pl_all = B_TRUE;
4554 * Now go through and check the width of any non-fixed columns
4556 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4557 if (entry->pl_fixed && !literal)
4560 if (entry->pl_prop != ZPROP_INVAL) {
4561 if (zfs_prop_get(zhp, entry->pl_prop,
4562 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4563 if (strlen(buf) > entry->pl_width)
4564 entry->pl_width = strlen(buf);
4566 if (received && zfs_prop_get_recvd(zhp,
4567 zfs_prop_to_name(entry->pl_prop),
4568 buf, sizeof (buf), literal) == 0)
4569 if (strlen(buf) > entry->pl_recvd_width)
4570 entry->pl_recvd_width = strlen(buf);
4572 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4574 verify(nvlist_lookup_string(propval,
4575 ZPROP_VALUE, &strval) == 0);
4576 if (strlen(strval) > entry->pl_width)
4577 entry->pl_width = strlen(strval);
4579 if (received && zfs_prop_get_recvd(zhp,
4580 entry->pl_user_prop,
4581 buf, sizeof (buf), literal) == 0)
4582 if (strlen(buf) > entry->pl_recvd_width)
4583 entry->pl_recvd_width = strlen(buf);
4591 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4592 char *resource, void *export, void *sharetab,
4593 int sharemax, zfs_share_op_t operation)
4595 zfs_cmd_t zc = { 0 };
4598 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4599 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4601 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4602 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4603 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4604 zc.zc_share.z_sharetype = operation;
4605 zc.zc_share.z_sharemax = sharemax;
4606 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4611 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4616 * Keep a reference to the props-table against which we prune the
4619 zhp->zfs_props_table = props;
4621 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4624 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4625 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4628 * User properties will result in ZPROP_INVAL, and since we
4629 * only know how to prune standard ZFS properties, we always
4630 * leave these in the list. This can also happen if we
4631 * encounter an unknown DSL property (when running older
4632 * software, for example).
4634 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4635 (void) nvlist_remove(zhp->zfs_props,
4636 nvpair_name(curr), nvpair_type(curr));
4643 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4644 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4646 zfs_cmd_t zc = { 0 };
4647 nvlist_t *nvlist = NULL;
4650 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4651 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4652 zc.zc_cookie = (uint64_t)cmd;
4654 if (cmd == ZFS_SMB_ACL_RENAME) {
4655 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4656 (void) no_memory(hdl);
4662 case ZFS_SMB_ACL_ADD:
4663 case ZFS_SMB_ACL_REMOVE:
4664 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4666 case ZFS_SMB_ACL_RENAME:
4667 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4669 (void) no_memory(hdl);
4672 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4674 (void) no_memory(hdl);
4677 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4678 nvlist_free(nvlist);
4682 case ZFS_SMB_ACL_PURGE:
4687 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4688 nvlist_free(nvlist);
4693 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4694 char *path, char *resource)
4696 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4701 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4702 char *path, char *resource)
4704 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4709 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4711 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4716 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4717 char *oldname, char *newname)
4719 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4722 #endif /* illumos */
4725 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4726 zfs_userspace_cb_t func, void *arg)
4728 zfs_cmd_t zc = { 0 };
4729 zfs_useracct_t buf[100];
4730 libzfs_handle_t *hdl = zhp->zfs_hdl;
4733 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4735 zc.zc_objset_type = type;
4736 zc.zc_nvlist_dst = (uintptr_t)buf;
4739 zfs_useracct_t *zua = buf;
4741 zc.zc_nvlist_dst_size = sizeof (buf);
4742 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4745 (void) snprintf(errbuf, sizeof (errbuf),
4746 dgettext(TEXT_DOMAIN,
4747 "cannot get used/quota for %s"), zc.zc_name);
4748 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4750 if (zc.zc_nvlist_dst_size == 0)
4753 while (zc.zc_nvlist_dst_size > 0) {
4754 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4755 zua->zu_space)) != 0)
4758 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4767 const char *snapname;
4769 boolean_t recursive;
4774 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4776 struct holdarg *ha = arg;
4777 char name[ZFS_MAX_DATASET_NAME_LEN];
4780 (void) snprintf(name, sizeof (name),
4781 "%s@%s", zhp->zfs_name, ha->snapname);
4783 if (lzc_exists(name))
4784 fnvlist_add_string(ha->nvl, name, ha->tag);
4787 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4793 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4794 boolean_t recursive, int cleanup_fd)
4799 ha.nvl = fnvlist_alloc();
4800 ha.snapname = snapname;
4802 ha.recursive = recursive;
4803 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4805 if (nvlist_empty(ha.nvl)) {
4808 fnvlist_free(ha.nvl);
4810 (void) snprintf(errbuf, sizeof (errbuf),
4811 dgettext(TEXT_DOMAIN,
4812 "cannot hold snapshot '%s@%s'"),
4813 zhp->zfs_name, snapname);
4814 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4818 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4819 fnvlist_free(ha.nvl);
4825 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4829 libzfs_handle_t *hdl = zhp->zfs_hdl;
4834 ret = lzc_hold(holds, cleanup_fd, &errors);
4837 /* There may be errors even in the success case. */
4838 fnvlist_free(errors);
4842 if (nvlist_empty(errors)) {
4843 /* no hold-specific errors */
4844 (void) snprintf(errbuf, sizeof (errbuf),
4845 dgettext(TEXT_DOMAIN, "cannot hold"));
4848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4849 "pool must be upgraded"));
4850 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4853 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4856 (void) zfs_standard_error(hdl, ret, errbuf);
4860 for (elem = nvlist_next_nvpair(errors, NULL);
4862 elem = nvlist_next_nvpair(errors, elem)) {
4863 (void) snprintf(errbuf, sizeof (errbuf),
4864 dgettext(TEXT_DOMAIN,
4865 "cannot hold snapshot '%s'"), nvpair_name(elem));
4866 switch (fnvpair_value_int32(elem)) {
4869 * Temporary tags wind up having the ds object id
4870 * prepended. So even if we passed the length check
4871 * above, it's still possible for the tag to wind
4872 * up being slightly too long.
4874 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4877 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4880 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4883 (void) zfs_standard_error(hdl,
4884 fnvpair_value_int32(elem), errbuf);
4888 fnvlist_free(errors);
4893 zfs_release_one(zfs_handle_t *zhp, void *arg)
4895 struct holdarg *ha = arg;
4896 char name[ZFS_MAX_DATASET_NAME_LEN];
4898 nvlist_t *existing_holds;
4900 (void) snprintf(name, sizeof (name),
4901 "%s@%s", zhp->zfs_name, ha->snapname);
4903 if (lzc_get_holds(name, &existing_holds) != 0) {
4905 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4908 nvlist_t *torelease = fnvlist_alloc();
4909 fnvlist_add_boolean(torelease, ha->tag);
4910 fnvlist_add_nvlist(ha->nvl, name, torelease);
4911 fnvlist_free(torelease);
4915 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4921 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4922 boolean_t recursive)
4926 nvlist_t *errors = NULL;
4928 libzfs_handle_t *hdl = zhp->zfs_hdl;
4931 ha.nvl = fnvlist_alloc();
4932 ha.snapname = snapname;
4934 ha.recursive = recursive;
4936 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4938 if (nvlist_empty(ha.nvl)) {
4939 fnvlist_free(ha.nvl);
4941 (void) snprintf(errbuf, sizeof (errbuf),
4942 dgettext(TEXT_DOMAIN,
4943 "cannot release hold from snapshot '%s@%s'"),
4944 zhp->zfs_name, snapname);
4946 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4948 (void) zfs_standard_error(hdl, ret, errbuf);
4953 ret = lzc_release(ha.nvl, &errors);
4954 fnvlist_free(ha.nvl);
4957 /* There may be errors even in the success case. */
4958 fnvlist_free(errors);
4962 if (nvlist_empty(errors)) {
4963 /* no hold-specific errors */
4964 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4968 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4969 "pool must be upgraded"));
4970 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4973 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4977 for (elem = nvlist_next_nvpair(errors, NULL);
4979 elem = nvlist_next_nvpair(errors, elem)) {
4980 (void) snprintf(errbuf, sizeof (errbuf),
4981 dgettext(TEXT_DOMAIN,
4982 "cannot release hold from snapshot '%s'"),
4984 switch (fnvpair_value_int32(elem)) {
4986 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4989 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4992 (void) zfs_standard_error_fmt(hdl,
4993 fnvpair_value_int32(elem), errbuf);
4997 fnvlist_free(errors);
5002 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5004 zfs_cmd_t zc = { 0 };
5005 libzfs_handle_t *hdl = zhp->zfs_hdl;
5011 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5012 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5016 nvbuf = malloc(nvsz);
5017 if (nvbuf == NULL) {
5018 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5022 zc.zc_nvlist_dst_size = nvsz;
5023 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5025 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5027 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
5028 (void) snprintf(errbuf, sizeof (errbuf),
5029 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5034 nvsz = zc.zc_nvlist_dst_size;
5038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5039 "pool must be upgraded"));
5040 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5043 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5046 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5049 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5054 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5056 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
5057 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5059 err = zfs_standard_error_fmt(hdl, rc, errbuf);
5069 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5071 zfs_cmd_t zc = { 0 };
5072 libzfs_handle_t *hdl = zhp->zfs_hdl;
5078 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5079 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5081 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5084 nvbuf = malloc(nvsz);
5086 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5089 zc.zc_nvlist_src_size = nvsz;
5090 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5091 zc.zc_perm_action = un;
5093 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5095 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5096 (void) snprintf(errbuf, sizeof (errbuf),
5097 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5102 "pool must be upgraded"));
5103 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5106 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5109 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5112 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5123 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5128 err = lzc_get_holds(zhp->zfs_name, nvl);
5131 libzfs_handle_t *hdl = zhp->zfs_hdl;
5133 (void) snprintf(errbuf, sizeof (errbuf),
5134 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5139 "pool must be upgraded"));
5140 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5143 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5146 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5149 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5158 * Convert the zvol's volume size to an appropriate reservation.
5159 * Note: If this routine is updated, it is necessary to update the ZFS test
5160 * suite's shell version in reservation.kshlib.
5163 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5166 uint64_t nblocks, volblocksize;
5170 if (nvlist_lookup_string(props,
5171 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5172 ncopies = atoi(strval);
5175 if (nvlist_lookup_uint64(props,
5176 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5177 &volblocksize) != 0)
5178 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5179 nblocks = volsize/volblocksize;
5180 /* start with metadnode L0-L6 */
5182 /* calculate number of indirects */
5183 while (nblocks > 1) {
5184 nblocks += DNODES_PER_LEVEL - 1;
5185 nblocks /= DNODES_PER_LEVEL;
5188 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5191 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5192 * compressed, but in practice they compress down to about
5195 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5201 * Attach/detach the given filesystem to/from the given jail.
5204 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
5206 libzfs_handle_t *hdl = zhp->zfs_hdl;
5207 zfs_cmd_t zc = { 0 };
5213 (void) snprintf(errbuf, sizeof (errbuf),
5214 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
5216 (void) snprintf(errbuf, sizeof (errbuf),
5217 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
5220 switch (zhp->zfs_type) {
5221 case ZFS_TYPE_VOLUME:
5222 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5223 "volumes can not be jailed"));
5224 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5225 case ZFS_TYPE_SNAPSHOT:
5226 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5227 "snapshots can not be jailed"));
5228 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5230 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5232 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5233 zc.zc_objset_type = DMU_OST_ZFS;
5234 zc.zc_jailid = jailid;
5236 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
5237 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
5238 zfs_standard_error(hdl, errno, errbuf);