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 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
803 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
804 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
808 libzfs_mnttab_update(libzfs_handle_t *hdl)
812 rewind(hdl->libzfs_mnttab);
813 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
816 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
818 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
819 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
820 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
821 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
822 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
823 avl_add(&hdl->libzfs_mnttab_cache, mtn);
828 libzfs_mnttab_fini(libzfs_handle_t *hdl)
833 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
835 free(mtn->mtn_mt.mnt_special);
836 free(mtn->mtn_mt.mnt_mountp);
837 free(mtn->mtn_mt.mnt_fstype);
838 free(mtn->mtn_mt.mnt_mntopts);
841 avl_destroy(&hdl->libzfs_mnttab_cache);
845 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
847 hdl->libzfs_mnttab_enable = enable;
851 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
852 struct mnttab *entry)
857 if (!hdl->libzfs_mnttab_enable) {
858 struct mnttab srch = { 0 };
860 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
861 libzfs_mnttab_fini(hdl);
862 rewind(hdl->libzfs_mnttab);
863 srch.mnt_special = (char *)fsname;
864 srch.mnt_fstype = MNTTYPE_ZFS;
865 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
871 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
872 libzfs_mnttab_update(hdl);
874 find.mtn_mt.mnt_special = (char *)fsname;
875 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
877 *entry = mtn->mtn_mt;
884 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
885 const char *mountp, const char *mntopts)
889 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
891 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
892 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
893 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
894 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
895 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
896 avl_add(&hdl->libzfs_mnttab_cache, mtn);
900 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
905 find.mtn_mt.mnt_special = (char *)fsname;
906 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
908 avl_remove(&hdl->libzfs_mnttab_cache, ret);
909 free(ret->mtn_mt.mnt_special);
910 free(ret->mtn_mt.mnt_mountp);
911 free(ret->mtn_mt.mnt_fstype);
912 free(ret->mtn_mt.mnt_mntopts);
918 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
920 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
922 if (zpool_handle == NULL)
925 *spa_version = zpool_get_prop_int(zpool_handle,
926 ZPOOL_PROP_VERSION, NULL);
931 * The choice of reservation property depends on the SPA version.
934 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
938 if (zfs_spa_version(zhp, &spa_version) < 0)
941 if (spa_version >= SPA_VERSION_REFRESERVATION)
942 *resv_prop = ZFS_PROP_REFRESERVATION;
944 *resv_prop = ZFS_PROP_RESERVATION;
950 * Given an nvlist of properties to set, validates that they are correct, and
951 * parses any numeric properties (index, boolean, etc) if they are specified as
955 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
956 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
964 int chosen_normal = -1;
967 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
968 (void) no_memory(hdl);
973 * Make sure this property is valid and applies to this type.
977 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
978 const char *propname = nvpair_name(elem);
980 prop = zfs_name_to_prop(propname);
981 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
983 * This is a user property: make sure it's a
984 * string, and that it's less than ZAP_MAXNAMELEN.
986 if (nvpair_type(elem) != DATA_TYPE_STRING) {
987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
988 "'%s' must be a string"), propname);
989 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
993 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
995 "property name '%s' is too long"),
997 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1001 (void) nvpair_value_string(elem, &strval);
1002 if (nvlist_add_string(ret, propname, strval) != 0) {
1003 (void) no_memory(hdl);
1010 * Currently, only user properties can be modified on
1013 if (type == ZFS_TYPE_SNAPSHOT) {
1014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1015 "this property can not be modified for snapshots"));
1016 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1020 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1021 zfs_userquota_prop_t uqtype;
1022 char newpropname[128];
1027 if (userquota_propname_decode(propname, zoned,
1028 &uqtype, domain, sizeof (domain), &rid) != 0) {
1030 dgettext(TEXT_DOMAIN,
1031 "'%s' has an invalid user/group name"),
1033 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1037 if (uqtype != ZFS_PROP_USERQUOTA &&
1038 uqtype != ZFS_PROP_GROUPQUOTA) {
1040 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1042 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1047 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1048 (void) nvpair_value_string(elem, &strval);
1049 if (strcmp(strval, "none") == 0) {
1051 } else if (zfs_nicestrtonum(hdl,
1052 strval, &intval) != 0) {
1053 (void) zfs_error(hdl,
1054 EZFS_BADPROP, errbuf);
1057 } else if (nvpair_type(elem) ==
1059 (void) nvpair_value_uint64(elem, &intval);
1061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 "use 'none' to disable "
1063 "userquota/groupquota"));
1067 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1068 "'%s' must be a number"), propname);
1069 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1074 * Encode the prop name as
1075 * userquota@<hex-rid>-domain, to make it easy
1076 * for the kernel to decode.
1078 (void) snprintf(newpropname, sizeof (newpropname),
1079 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1080 (longlong_t)rid, domain);
1084 if (nvlist_add_uint64_array(ret, newpropname,
1086 (void) no_memory(hdl);
1090 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1092 "'%s' is readonly"),
1094 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1098 if (prop == ZPROP_INVAL) {
1099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1100 "invalid property '%s'"), propname);
1101 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1105 if (!zfs_prop_valid_for_type(prop, type)) {
1107 dgettext(TEXT_DOMAIN, "'%s' does not "
1108 "apply to datasets of this type"), propname);
1109 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1113 if (zfs_prop_readonly(prop) &&
1114 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1116 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1118 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1122 if (zprop_parse_value(hdl, elem, prop, type, ret,
1123 &strval, &intval, errbuf) != 0)
1127 * Perform some additional checks for specific properties.
1130 case ZFS_PROP_VERSION:
1136 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1137 if (intval < version) {
1138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1139 "Can not downgrade; already at version %u"),
1141 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1147 case ZFS_PROP_VOLBLOCKSIZE:
1148 case ZFS_PROP_RECORDSIZE:
1150 int maxbs = SPA_MAXBLOCKSIZE;
1151 if (zpool_hdl != NULL) {
1152 maxbs = zpool_get_prop_int(zpool_hdl,
1153 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1156 * Volumes are limited to a volblocksize of 128KB,
1157 * because they typically service workloads with
1158 * small random writes, which incur a large performance
1159 * penalty with large blocks.
1161 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1162 maxbs = SPA_OLD_MAXBLOCKSIZE;
1164 * The value must be a power of two between
1165 * SPA_MINBLOCKSIZE and maxbs.
1167 if (intval < SPA_MINBLOCKSIZE ||
1168 intval > maxbs || !ISP2(intval)) {
1169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1170 "'%s' must be power of 2 from 512B "
1171 "to %uKB"), propname, maxbs >> 10);
1172 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1177 case ZFS_PROP_MLSLABEL:
1181 * Verify the mlslabel string and convert to
1182 * internal hex label string.
1186 char *hex = NULL; /* internal label string */
1188 /* Default value is already OK. */
1189 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1192 /* Verify the label can be converted to binary form */
1193 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1194 (str_to_label(strval, &new_sl, MAC_LABEL,
1195 L_NO_CORRECTION, NULL) == -1)) {
1199 /* Now translate to hex internal label string */
1200 if (label_to_str(new_sl, &hex, M_INTERNAL,
1206 m_label_free(new_sl);
1208 /* If string is already in internal form, we're done. */
1209 if (strcmp(strval, hex) == 0) {
1214 /* Replace the label string with the internal form. */
1215 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1217 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225 "invalid mlslabel '%s'"), strval);
1226 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1227 m_label_free(new_sl); /* OK if null */
1228 #else /* !illumos */
1229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1230 "mlslabel is not supported on FreeBSD"));
1231 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1232 #endif /* illumos */
1237 case ZFS_PROP_MOUNTPOINT:
1239 namecheck_err_t why;
1241 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1242 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1245 if (mountpoint_namecheck(strval, &why)) {
1247 case NAME_ERR_LEADING_SLASH:
1249 dgettext(TEXT_DOMAIN,
1250 "'%s' must be an absolute path, "
1251 "'none', or 'legacy'"), propname);
1253 case NAME_ERR_TOOLONG:
1255 dgettext(TEXT_DOMAIN,
1256 "component of '%s' is too long"),
1262 dgettext(TEXT_DOMAIN,
1263 "(%d) not defined"),
1267 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1274 case ZFS_PROP_SHARESMB:
1275 case ZFS_PROP_SHARENFS:
1277 * For the mountpoint and sharenfs or sharesmb
1278 * properties, check if it can be set in a
1279 * global/non-global zone based on
1280 * the zoned property value:
1282 * global zone non-global zone
1283 * --------------------------------------------------
1284 * zoned=on mountpoint (no) mountpoint (yes)
1285 * sharenfs (no) sharenfs (no)
1286 * sharesmb (no) sharesmb (no)
1288 * zoned=off mountpoint (yes) N/A
1293 if (getzoneid() == GLOBAL_ZONEID) {
1294 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1295 "'%s' cannot be set on "
1296 "dataset in a non-global zone"),
1298 (void) zfs_error(hdl, EZFS_ZONED,
1301 } else if (prop == ZFS_PROP_SHARENFS ||
1302 prop == ZFS_PROP_SHARESMB) {
1303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1304 "'%s' cannot be set in "
1305 "a non-global zone"), propname);
1306 (void) zfs_error(hdl, EZFS_ZONED,
1310 } else if (getzoneid() != GLOBAL_ZONEID) {
1312 * If zoned property is 'off', this must be in
1313 * a global zone. If not, something is wrong.
1315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1316 "'%s' cannot be set while dataset "
1317 "'zoned' property is set"), propname);
1318 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1323 * At this point, it is legitimate to set the
1324 * property. Now we want to make sure that the
1325 * property value is valid if it is sharenfs.
1327 if ((prop == ZFS_PROP_SHARENFS ||
1328 prop == ZFS_PROP_SHARESMB) &&
1329 strcmp(strval, "on") != 0 &&
1330 strcmp(strval, "off") != 0) {
1331 zfs_share_proto_t proto;
1333 if (prop == ZFS_PROP_SHARESMB)
1339 * Must be an valid sharing protocol
1340 * option string so init the libshare
1341 * in order to enable the parser and
1342 * then parse the options. We use the
1343 * control API since we don't care about
1344 * the current configuration and don't
1345 * want the overhead of loading it
1346 * until we actually do something.
1349 if (zfs_init_libshare(hdl,
1350 SA_INIT_CONTROL_API) != SA_OK) {
1352 * An error occurred so we can't do
1355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1356 "'%s' cannot be set: problem "
1357 "in share initialization"),
1359 (void) zfs_error(hdl, EZFS_BADPROP,
1364 if (zfs_parse_options(strval, proto) != SA_OK) {
1366 * There was an error in parsing so
1367 * deal with it by issuing an error
1368 * message and leaving after
1369 * uninitializing the the libshare
1372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1373 "'%s' cannot be set to invalid "
1374 "options"), propname);
1375 (void) zfs_error(hdl, EZFS_BADPROP,
1377 zfs_uninit_libshare(hdl);
1380 zfs_uninit_libshare(hdl);
1385 case ZFS_PROP_UTF8ONLY:
1386 chosen_utf = (int)intval;
1389 case ZFS_PROP_NORMALIZE:
1390 chosen_normal = (int)intval;
1398 * For changes to existing volumes, we have some additional
1399 * checks to enforce.
1401 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1402 uint64_t volsize = zfs_prop_get_int(zhp,
1404 uint64_t blocksize = zfs_prop_get_int(zhp,
1405 ZFS_PROP_VOLBLOCKSIZE);
1409 case ZFS_PROP_RESERVATION:
1410 if (intval > volsize) {
1411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1412 "'%s' is greater than current "
1413 "volume size"), propname);
1414 (void) zfs_error(hdl, EZFS_BADPROP,
1420 case ZFS_PROP_REFRESERVATION:
1421 if (intval > volsize && intval != UINT64_MAX) {
1422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1423 "'%s' is greater than current "
1424 "volume size"), propname);
1425 (void) zfs_error(hdl, EZFS_BADPROP,
1431 case ZFS_PROP_VOLSIZE:
1432 if (intval % blocksize != 0) {
1433 zfs_nicenum(blocksize, buf,
1435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1436 "'%s' must be a multiple of "
1437 "volume block size (%s)"),
1439 (void) zfs_error(hdl, EZFS_BADPROP,
1445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1446 "'%s' cannot be zero"),
1448 (void) zfs_error(hdl, EZFS_BADPROP,
1461 * If normalization was chosen, but no UTF8 choice was made,
1462 * enforce rejection of non-UTF8 names.
1464 * If normalization was chosen, but rejecting non-UTF8 names
1465 * was explicitly not chosen, it is an error.
1467 if (chosen_normal > 0 && chosen_utf < 0) {
1468 if (nvlist_add_uint64(ret,
1469 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1470 (void) no_memory(hdl);
1473 } else if (chosen_normal > 0 && chosen_utf == 0) {
1474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475 "'%s' must be set 'on' if normalization chosen"),
1476 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1477 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1488 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1490 uint64_t old_volsize;
1491 uint64_t new_volsize;
1492 uint64_t old_reservation;
1493 uint64_t new_reservation;
1494 zfs_prop_t resv_prop;
1498 * If this is an existing volume, and someone is setting the volsize,
1499 * make sure that it matches the reservation, or add it if necessary.
1501 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1502 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1504 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1506 props = fnvlist_alloc();
1507 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1508 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1510 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1511 old_reservation) || nvlist_exists(nvl,
1512 zfs_prop_to_name(resv_prop))) {
1513 fnvlist_free(props);
1516 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1517 &new_volsize) != 0) {
1518 fnvlist_free(props);
1521 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1522 fnvlist_free(props);
1524 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1525 new_reservation) != 0) {
1526 (void) no_memory(zhp->zfs_hdl);
1533 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1534 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1535 * Return codes must match zfs_add_synthetic_resv().
1538 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1545 if (!ZFS_IS_VOLUME(zhp)) {
1549 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1553 if (prop != ZFS_PROP_REFRESERVATION) {
1557 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1558 /* No value being set, so it can't be "auto" */
1561 if (resvsize != UINT64_MAX) {
1562 /* Being set to a value other than "auto" */
1566 props = fnvlist_alloc();
1568 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1569 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1571 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1573 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1576 resvsize = zvol_volsize_to_reservation(volsize, props);
1577 fnvlist_free(props);
1579 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1580 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1581 (void) no_memory(zhp->zfs_hdl);
1588 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1595 * For quotas and reservations, ENOSPC indicates
1596 * something different; setting a quota or reservation
1597 * doesn't use any disk space.
1600 case ZFS_PROP_QUOTA:
1601 case ZFS_PROP_REFQUOTA:
1602 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1603 "size is less than current used or "
1605 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1608 case ZFS_PROP_RESERVATION:
1609 case ZFS_PROP_REFRESERVATION:
1610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1611 "size is greater than available space"));
1612 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1616 (void) zfs_standard_error(hdl, err, errbuf);
1622 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1626 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1631 "property value too long"));
1632 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1637 "pool and or dataset must be upgraded to set this "
1638 "property or value"));
1639 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1644 if (prop == ZFS_PROP_COMPRESSION ||
1645 prop == ZFS_PROP_RECORDSIZE) {
1646 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1647 "property setting is not allowed on "
1648 "bootable datasets"));
1649 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1650 } else if (prop == ZFS_PROP_CHECKSUM ||
1651 prop == ZFS_PROP_DEDUP) {
1652 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1653 "property setting is not allowed on "
1655 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1657 (void) zfs_standard_error(hdl, err, errbuf);
1662 if (prop == ZPROP_INVAL) {
1663 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1665 (void) zfs_standard_error(hdl, err, errbuf);
1671 * This platform can't address a volume this big.
1674 if (prop == ZFS_PROP_VOLSIZE) {
1675 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1681 (void) zfs_standard_error(hdl, err, errbuf);
1686 * Given a property name and value, set the property for the given dataset.
1689 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1693 libzfs_handle_t *hdl = zhp->zfs_hdl;
1694 nvlist_t *nvl = NULL;
1696 (void) snprintf(errbuf, sizeof (errbuf),
1697 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1700 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1701 nvlist_add_string(nvl, propname, propval) != 0) {
1702 (void) no_memory(hdl);
1706 ret = zfs_prop_set_list(zhp, nvl);
1716 * Given an nvlist of property names and values, set the properties for the
1720 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1722 zfs_cmd_t zc = { 0 };
1724 prop_changelist_t **cls = NULL;
1727 libzfs_handle_t *hdl = zhp->zfs_hdl;
1732 (void) snprintf(errbuf, sizeof (errbuf),
1733 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1736 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1737 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1742 * We have to check for any extra properties which need to be added
1743 * before computing the length of the nvlist.
1745 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1747 elem = nvlist_next_nvpair(nvl, elem)) {
1748 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1749 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1754 if (added_resv != 1 &&
1755 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1760 * Check how many properties we're setting and allocate an array to
1761 * store changelist pointers for postfix().
1764 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1766 elem = nvlist_next_nvpair(nvl, elem))
1768 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1772 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1774 elem = nvlist_next_nvpair(nvl, elem)) {
1776 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1778 assert(cl_idx < nvl_len);
1780 * We don't want to unmount & remount the dataset when changing
1781 * its canmount property to 'on' or 'noauto'. We only use
1782 * the changelist logic to unmount when setting canmount=off.
1784 if (prop != ZFS_PROP_CANMOUNT ||
1785 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1786 zfs_is_mounted(zhp, NULL))) {
1787 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1788 if (cls[cl_idx] == NULL)
1792 if (prop == ZFS_PROP_MOUNTPOINT &&
1793 changelist_haszonedchild(cls[cl_idx])) {
1794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1795 "child dataset with inherited mountpoint is used "
1796 "in a non-global zone"));
1797 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1801 /* We don't support those properties on FreeBSD. */
1803 case ZFS_PROP_DEVICES:
1804 case ZFS_PROP_ISCSIOPTIONS:
1805 case ZFS_PROP_XATTR:
1806 case ZFS_PROP_VSCAN:
1807 case ZFS_PROP_NBMAND:
1808 case ZFS_PROP_MLSLABEL:
1809 (void) snprintf(errbuf, sizeof (errbuf),
1810 "property '%s' not supported on FreeBSD",
1812 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1816 if (cls[cl_idx] != NULL &&
1817 (ret = changelist_prefix(cls[cl_idx])) != 0)
1822 assert(cl_idx == nvl_len);
1825 * Execute the corresponding ioctl() to set this list of properties.
1827 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1829 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1830 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1833 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1836 /* Get the list of unset properties back and report them. */
1837 nvlist_t *errorprops = NULL;
1838 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1840 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1842 elem = nvlist_next_nvpair(nvl, elem)) {
1843 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1844 zfs_setprop_error(hdl, prop, errno, errbuf);
1846 nvlist_free(errorprops);
1848 if (added_resv && errno == ENOSPC) {
1849 /* clean up the volsize property we tried to set */
1850 uint64_t old_volsize = zfs_prop_get_int(zhp,
1854 zcmd_free_nvlists(&zc);
1856 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1858 if (nvlist_add_uint64(nvl,
1859 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1862 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1864 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1867 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1868 if (cls[cl_idx] != NULL) {
1869 int clp_err = changelist_postfix(cls[cl_idx]);
1876 * Refresh the statistics so the new property value
1880 (void) get_stats(zhp);
1885 zcmd_free_nvlists(&zc);
1887 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1888 if (cls[cl_idx] != NULL)
1889 changelist_free(cls[cl_idx]);
1897 * Given a property, inherit the value from the parent dataset, or if received
1898 * is TRUE, revert to the received value, if any.
1901 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1903 zfs_cmd_t zc = { 0 };
1905 prop_changelist_t *cl;
1906 libzfs_handle_t *hdl = zhp->zfs_hdl;
1910 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1911 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1913 zc.zc_cookie = received;
1914 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1916 * For user properties, the amount of work we have to do is very
1917 * small, so just do it here.
1919 if (!zfs_prop_user(propname)) {
1920 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1921 "invalid property"));
1922 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1925 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1926 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1928 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1929 return (zfs_standard_error(hdl, errno, errbuf));
1935 * Verify that this property is inheritable.
1937 if (zfs_prop_readonly(prop))
1938 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1940 if (!zfs_prop_inheritable(prop) && !received)
1941 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1944 * Check to see if the value applies to this type
1946 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1947 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1950 * Normalize the name, to get rid of shorthand abbreviations.
1952 propname = zfs_prop_to_name(prop);
1953 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1954 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1956 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1957 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1959 "dataset is used in a non-global zone"));
1960 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1964 * Determine datasets which will be affected by this change, if any.
1966 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1969 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1970 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1971 "child dataset with inherited mountpoint is used "
1972 "in a non-global zone"));
1973 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1977 if ((ret = changelist_prefix(cl)) != 0)
1980 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1981 return (zfs_standard_error(hdl, errno, errbuf));
1984 if ((ret = changelist_postfix(cl)) != 0)
1988 * Refresh the statistics so the new property is reflected.
1990 (void) get_stats(zhp);
1994 changelist_free(cl);
1999 * True DSL properties are stored in an nvlist. The following two functions
2000 * extract them appropriately.
2003 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2009 if (nvlist_lookup_nvlist(zhp->zfs_props,
2010 zfs_prop_to_name(prop), &nv) == 0) {
2011 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2012 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2014 verify(!zhp->zfs_props_table ||
2015 zhp->zfs_props_table[prop] == B_TRUE);
2016 value = zfs_prop_default_numeric(prop);
2024 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2030 if (nvlist_lookup_nvlist(zhp->zfs_props,
2031 zfs_prop_to_name(prop), &nv) == 0) {
2032 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2033 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2035 verify(!zhp->zfs_props_table ||
2036 zhp->zfs_props_table[prop] == B_TRUE);
2037 value = zfs_prop_default_string(prop);
2045 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2047 return (zhp->zfs_props == zhp->zfs_recvd_props);
2051 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2053 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2054 zhp->zfs_props = zhp->zfs_recvd_props;
2058 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2060 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2065 * Internal function for getting a numeric property. Both zfs_prop_get() and
2066 * zfs_prop_get_int() are built using this interface.
2068 * Certain properties can be overridden using 'mount -o'. In this case, scan
2069 * the contents of the /etc/mnttab entry, searching for the appropriate options.
2070 * If they differ from the on-disk values, report the current values and mark
2071 * the source "temporary".
2074 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2075 char **source, uint64_t *val)
2077 zfs_cmd_t zc = { 0 };
2078 nvlist_t *zplprops = NULL;
2080 char *mntopt_on = NULL;
2081 char *mntopt_off = NULL;
2082 boolean_t received = zfs_is_recvd_props_mode(zhp);
2087 case ZFS_PROP_ATIME:
2088 mntopt_on = MNTOPT_ATIME;
2089 mntopt_off = MNTOPT_NOATIME;
2092 case ZFS_PROP_DEVICES:
2093 mntopt_on = MNTOPT_DEVICES;
2094 mntopt_off = MNTOPT_NODEVICES;
2098 mntopt_on = MNTOPT_EXEC;
2099 mntopt_off = MNTOPT_NOEXEC;
2102 case ZFS_PROP_READONLY:
2103 mntopt_on = MNTOPT_RO;
2104 mntopt_off = MNTOPT_RW;
2107 case ZFS_PROP_SETUID:
2108 mntopt_on = MNTOPT_SETUID;
2109 mntopt_off = MNTOPT_NOSETUID;
2112 case ZFS_PROP_XATTR:
2113 mntopt_on = MNTOPT_XATTR;
2114 mntopt_off = MNTOPT_NOXATTR;
2117 case ZFS_PROP_NBMAND:
2118 mntopt_on = MNTOPT_NBMAND;
2119 mntopt_off = MNTOPT_NONBMAND;
2127 * Because looking up the mount options is potentially expensive
2128 * (iterating over all of /etc/mnttab), we defer its calculation until
2129 * we're looking up a property which requires its presence.
2131 if (!zhp->zfs_mntcheck &&
2132 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2133 libzfs_handle_t *hdl = zhp->zfs_hdl;
2134 struct mnttab entry;
2136 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2137 zhp->zfs_mntopts = zfs_strdup(hdl,
2139 if (zhp->zfs_mntopts == NULL)
2143 zhp->zfs_mntcheck = B_TRUE;
2146 if (zhp->zfs_mntopts == NULL)
2147 mnt.mnt_mntopts = "";
2149 mnt.mnt_mntopts = zhp->zfs_mntopts;
2152 case ZFS_PROP_ATIME:
2153 case ZFS_PROP_DEVICES:
2155 case ZFS_PROP_READONLY:
2156 case ZFS_PROP_SETUID:
2157 case ZFS_PROP_XATTR:
2158 case ZFS_PROP_NBMAND:
2159 *val = getprop_uint64(zhp, prop, source);
2164 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2167 *src = ZPROP_SRC_TEMPORARY;
2168 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2171 *src = ZPROP_SRC_TEMPORARY;
2175 case ZFS_PROP_CANMOUNT:
2176 case ZFS_PROP_VOLSIZE:
2177 case ZFS_PROP_QUOTA:
2178 case ZFS_PROP_REFQUOTA:
2179 case ZFS_PROP_RESERVATION:
2180 case ZFS_PROP_REFRESERVATION:
2181 case ZFS_PROP_FILESYSTEM_LIMIT:
2182 case ZFS_PROP_SNAPSHOT_LIMIT:
2183 case ZFS_PROP_FILESYSTEM_COUNT:
2184 case ZFS_PROP_SNAPSHOT_COUNT:
2185 *val = getprop_uint64(zhp, prop, source);
2187 if (*source == NULL) {
2188 /* not default, must be local */
2189 *source = zhp->zfs_name;
2193 case ZFS_PROP_MOUNTED:
2194 *val = (zhp->zfs_mntopts != NULL);
2197 case ZFS_PROP_NUMCLONES:
2198 *val = zhp->zfs_dmustats.dds_num_clones;
2201 case ZFS_PROP_VERSION:
2202 case ZFS_PROP_NORMALIZE:
2203 case ZFS_PROP_UTF8ONLY:
2205 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2206 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2208 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2209 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2210 zcmd_free_nvlists(&zc);
2213 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2214 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2216 zcmd_free_nvlists(&zc);
2219 nvlist_free(zplprops);
2220 zcmd_free_nvlists(&zc);
2223 case ZFS_PROP_INCONSISTENT:
2224 *val = zhp->zfs_dmustats.dds_inconsistent;
2228 switch (zfs_prop_get_type(prop)) {
2229 case PROP_TYPE_NUMBER:
2230 case PROP_TYPE_INDEX:
2231 *val = getprop_uint64(zhp, prop, source);
2233 * If we tried to use a default value for a
2234 * readonly property, it means that it was not
2235 * present. Note this only applies to "truly"
2236 * readonly properties, not set-once properties
2237 * like volblocksize.
2239 if (zfs_prop_readonly(prop) &&
2240 !zfs_prop_setonce(prop) &&
2241 *source != NULL && (*source)[0] == '\0') {
2247 case PROP_TYPE_STRING:
2249 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2250 "cannot get non-numeric property"));
2251 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2252 dgettext(TEXT_DOMAIN, "internal error")));
2260 * Calculate the source type, given the raw source string.
2263 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2264 char *statbuf, size_t statlen)
2266 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2269 if (source == NULL) {
2270 *srctype = ZPROP_SRC_NONE;
2271 } else if (source[0] == '\0') {
2272 *srctype = ZPROP_SRC_DEFAULT;
2273 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2274 *srctype = ZPROP_SRC_RECEIVED;
2276 if (strcmp(source, zhp->zfs_name) == 0) {
2277 *srctype = ZPROP_SRC_LOCAL;
2279 (void) strlcpy(statbuf, source, statlen);
2280 *srctype = ZPROP_SRC_INHERITED;
2287 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2288 size_t proplen, boolean_t literal)
2293 if (zhp->zfs_recvd_props == NULL)
2294 if (get_recvd_props_ioctl(zhp) != 0)
2297 prop = zfs_name_to_prop(propname);
2299 if (prop != ZPROP_INVAL) {
2301 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2303 zfs_set_recvd_props_mode(zhp, &cookie);
2304 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2305 NULL, NULL, 0, literal);
2306 zfs_unset_recvd_props_mode(zhp, &cookie);
2310 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2311 propname, &propval) != 0)
2313 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2315 (void) strlcpy(propbuf, recvdval, proplen);
2318 return (err == 0 ? 0 : -1);
2322 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2327 value = zfs_get_clones_nvl(zhp);
2332 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2333 pair = nvlist_next_nvpair(value, pair)) {
2334 if (propbuf[0] != '\0')
2335 (void) strlcat(propbuf, ",", proplen);
2336 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2342 struct get_clones_arg {
2346 char buf[ZFS_MAX_DATASET_NAME_LEN];
2350 get_clones_cb(zfs_handle_t *zhp, void *arg)
2352 struct get_clones_arg *gca = arg;
2354 if (gca->numclones == 0) {
2359 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2360 NULL, NULL, 0, B_TRUE) != 0)
2362 if (strcmp(gca->buf, gca->origin) == 0) {
2363 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2368 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2374 zfs_get_clones_nvl(zfs_handle_t *zhp)
2376 nvlist_t *nv, *value;
2378 if (nvlist_lookup_nvlist(zhp->zfs_props,
2379 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2380 struct get_clones_arg gca;
2383 * if this is a snapshot, then the kernel wasn't able
2384 * to get the clones. Do it by slowly iterating.
2386 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2388 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2390 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2395 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2397 gca.origin = zhp->zfs_name;
2399 if (gca.numclones != 0) {
2401 char pool[ZFS_MAX_DATASET_NAME_LEN];
2404 /* get the pool name */
2405 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2406 (void) strsep(&cp, "/@");
2407 root = zfs_open(zhp->zfs_hdl, pool,
2408 ZFS_TYPE_FILESYSTEM);
2410 (void) get_clones_cb(root, &gca);
2413 if (gca.numclones != 0 ||
2414 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2415 nvlist_add_nvlist(zhp->zfs_props,
2416 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2423 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2424 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2427 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2433 * Accepts a property and value and checks that the value
2434 * matches the one found by the channel program. If they are
2435 * not equal, print both of them.
2438 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2441 if (!zhp->zfs_hdl->libzfs_prop_debug)
2444 char *poolname = zhp->zpool_hdl->zpool_name;
2445 const char *program =
2447 "ds = args['dataset']\n"
2448 "prop = args['property']\n"
2449 "value, setpoint = zfs.get_prop(ds, prop)\n"
2450 "return {value=value, setpoint=setpoint}\n";
2453 nvlist_t *argnvl = fnvlist_alloc();
2455 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2456 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2458 error = lzc_channel_program_nosync(poolname, program,
2459 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2462 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2463 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2465 error = nvlist_lookup_int64(retnvl, "value", &ans);
2467 (void) fprintf(stderr, "zcp check error: %u\n",
2471 if (ans != intval) {
2472 (void) fprintf(stderr,
2473 "%s: zfs found %lld, but zcp found %lld\n",
2474 zfs_prop_to_name(prop),
2475 (longlong_t)intval, (longlong_t)ans);
2479 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2481 (void) fprintf(stderr, "zcp check error: %u\n",
2485 if (strcmp(strval, str_ans) != 0) {
2486 (void) fprintf(stderr,
2487 "%s: zfs found %s, but zcp found %s\n",
2488 zfs_prop_to_name(prop),
2493 (void) fprintf(stderr,
2494 "zcp check failed, channel program error: %u\n", error);
2496 nvlist_free(argnvl);
2497 nvlist_free(outnvl);
2501 * Retrieve a property from the given object. If 'literal' is specified, then
2502 * numbers are left as exact values. Otherwise, numbers are converted to a
2503 * human-readable form.
2505 * Returns 0 on success, or -1 on error.
2508 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2509 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2511 char *source = NULL;
2515 boolean_t received = zfs_is_recvd_props_mode(zhp);
2518 * Check to see if this property applies to our object
2520 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2523 if (received && zfs_prop_readonly(prop))
2527 *src = ZPROP_SRC_NONE;
2530 case ZFS_PROP_CREATION:
2532 * 'creation' is a time_t stored in the statistics. We convert
2533 * this into a string unless 'literal' is specified.
2536 val = getprop_uint64(zhp, prop, &source);
2537 time_t time = (time_t)val;
2541 localtime_r(&time, &t) == NULL ||
2542 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2544 (void) snprintf(propbuf, proplen, "%llu", val);
2546 zcp_check(zhp, prop, val, NULL);
2549 case ZFS_PROP_MOUNTPOINT:
2551 * Getting the precise mountpoint can be tricky.
2553 * - for 'none' or 'legacy', return those values.
2554 * - for inherited mountpoints, we want to take everything
2555 * after our ancestor and append it to the inherited value.
2557 * If the pool has an alternate root, we want to prepend that
2558 * root to any values we return.
2561 str = getprop_string(zhp, prop, &source);
2563 if (str[0] == '/') {
2564 char buf[MAXPATHLEN];
2566 const char *relpath;
2569 * If we inherit the mountpoint, even from a dataset
2570 * with a received value, the source will be the path of
2571 * the dataset we inherit from. If source is
2572 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2575 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2578 relpath = zhp->zfs_name + strlen(source);
2579 if (relpath[0] == '/')
2583 if ((zpool_get_prop(zhp->zpool_hdl,
2584 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2585 B_FALSE)) || (strcmp(root, "-") == 0))
2588 * Special case an alternate root of '/'. This will
2589 * avoid having multiple leading slashes in the
2592 if (strcmp(root, "/") == 0)
2596 * If the mountpoint is '/' then skip over this
2597 * if we are obtaining either an alternate root or
2598 * an inherited mountpoint.
2600 if (str[1] == '\0' && (root[0] != '\0' ||
2601 relpath[0] != '\0'))
2604 if (relpath[0] == '\0')
2605 (void) snprintf(propbuf, proplen, "%s%s",
2608 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2609 root, str, relpath[0] == '@' ? "" : "/",
2612 /* 'legacy' or 'none' */
2613 (void) strlcpy(propbuf, str, proplen);
2615 zcp_check(zhp, prop, NULL, propbuf);
2618 case ZFS_PROP_ORIGIN:
2619 str = getprop_string(zhp, prop, &source);
2622 (void) strlcpy(propbuf, str, proplen);
2623 zcp_check(zhp, prop, NULL, str);
2626 case ZFS_PROP_CLONES:
2627 if (get_clones_string(zhp, propbuf, proplen) != 0)
2631 case ZFS_PROP_QUOTA:
2632 case ZFS_PROP_REFQUOTA:
2633 case ZFS_PROP_RESERVATION:
2634 case ZFS_PROP_REFRESERVATION:
2636 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2639 * If quota or reservation is 0, we translate this into 'none'
2640 * (unless literal is set), and indicate that it's the default
2641 * value. Otherwise, we print the number nicely and indicate
2642 * that its set locally.
2646 (void) strlcpy(propbuf, "0", proplen);
2648 (void) strlcpy(propbuf, "none", proplen);
2651 (void) snprintf(propbuf, proplen, "%llu",
2654 zfs_nicenum(val, propbuf, proplen);
2656 zcp_check(zhp, prop, val, NULL);
2659 case ZFS_PROP_FILESYSTEM_LIMIT:
2660 case ZFS_PROP_SNAPSHOT_LIMIT:
2661 case ZFS_PROP_FILESYSTEM_COUNT:
2662 case ZFS_PROP_SNAPSHOT_COUNT:
2664 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2668 * If limit is UINT64_MAX, we translate this into 'none' (unless
2669 * literal is set), and indicate that it's the default value.
2670 * Otherwise, we print the number nicely and indicate that it's
2674 (void) snprintf(propbuf, proplen, "%llu",
2676 } else if (val == UINT64_MAX) {
2677 (void) strlcpy(propbuf, "none", proplen);
2679 zfs_nicenum(val, propbuf, proplen);
2682 zcp_check(zhp, prop, val, NULL);
2685 case ZFS_PROP_REFRATIO:
2686 case ZFS_PROP_COMPRESSRATIO:
2687 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2689 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2690 (u_longlong_t)(val / 100),
2691 (u_longlong_t)(val % 100));
2692 zcp_check(zhp, prop, val, NULL);
2696 switch (zhp->zfs_type) {
2697 case ZFS_TYPE_FILESYSTEM:
2700 case ZFS_TYPE_VOLUME:
2703 case ZFS_TYPE_SNAPSHOT:
2706 case ZFS_TYPE_BOOKMARK:
2712 (void) snprintf(propbuf, proplen, "%s", str);
2713 zcp_check(zhp, prop, NULL, propbuf);
2716 case ZFS_PROP_MOUNTED:
2718 * The 'mounted' property is a pseudo-property that described
2719 * whether the filesystem is currently mounted. Even though
2720 * it's a boolean value, the typical values of "on" and "off"
2721 * don't make sense, so we translate to "yes" and "no".
2723 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2724 src, &source, &val) != 0)
2727 (void) strlcpy(propbuf, "yes", proplen);
2729 (void) strlcpy(propbuf, "no", proplen);
2734 * The 'name' property is a pseudo-property derived from the
2735 * dataset name. It is presented as a real property to simplify
2738 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2739 zcp_check(zhp, prop, NULL, propbuf);
2742 case ZFS_PROP_MLSLABEL:
2745 m_label_t *new_sl = NULL;
2746 char *ascii = NULL; /* human readable label */
2748 (void) strlcpy(propbuf,
2749 getprop_string(zhp, prop, &source), proplen);
2751 if (literal || (strcasecmp(propbuf,
2752 ZFS_MLSLABEL_DEFAULT) == 0))
2756 * Try to translate the internal hex string to
2757 * human-readable output. If there are any
2758 * problems just use the hex string.
2761 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2762 L_NO_CORRECTION, NULL) == -1) {
2763 m_label_free(new_sl);
2767 if (label_to_str(new_sl, &ascii, M_LABEL,
2771 m_label_free(new_sl);
2774 m_label_free(new_sl);
2776 (void) strlcpy(propbuf, ascii, proplen);
2778 #else /* !illumos */
2780 #endif /* illumos */
2785 case ZFS_PROP_CREATETXG:
2787 * GUIDs are stored as numbers, but they are identifiers.
2788 * We don't want them to be pretty printed, because pretty
2789 * printing mangles the ID into a truncated and useless value.
2791 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2793 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2794 zcp_check(zhp, prop, val, NULL);
2798 switch (zfs_prop_get_type(prop)) {
2799 case PROP_TYPE_NUMBER:
2800 if (get_numeric_property(zhp, prop, src,
2801 &source, &val) != 0) {
2806 (void) snprintf(propbuf, proplen, "%llu",
2809 zfs_nicenum(val, propbuf, proplen);
2811 zcp_check(zhp, prop, val, NULL);
2814 case PROP_TYPE_STRING:
2815 str = getprop_string(zhp, prop, &source);
2819 (void) strlcpy(propbuf, str, proplen);
2820 zcp_check(zhp, prop, NULL, str);
2823 case PROP_TYPE_INDEX:
2824 if (get_numeric_property(zhp, prop, src,
2825 &source, &val) != 0)
2827 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2830 (void) strlcpy(propbuf, strval, proplen);
2831 zcp_check(zhp, prop, NULL, strval);
2839 get_source(zhp, src, source, statbuf, statlen);
2845 * Utility function to get the given numeric property. Does no validation that
2846 * the given property is the appropriate type; should only be used with
2847 * hard-coded property types.
2850 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2855 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2861 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2865 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2866 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2870 * Similar to zfs_prop_get(), but returns the value as an integer.
2873 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2874 zprop_source_t *src, char *statbuf, size_t statlen)
2879 * Check to see if this property applies to our object
2881 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2882 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2883 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2884 zfs_prop_to_name(prop)));
2888 *src = ZPROP_SRC_NONE;
2890 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2893 get_source(zhp, src, source, statbuf, statlen);
2899 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2900 char **domainp, idmap_rid_t *ridp)
2903 idmap_get_handle_t *get_hdl = NULL;
2907 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2911 err = idmap_get_sidbyuid(get_hdl, id,
2912 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2914 err = idmap_get_sidbygid(get_hdl, id,
2915 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2917 if (err == IDMAP_SUCCESS &&
2918 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2919 status == IDMAP_SUCCESS)
2925 idmap_get_destroy(get_hdl);
2927 #else /* !illumos */
2928 assert(!"invalid code path");
2929 return (EINVAL); // silence compiler warning
2930 #endif /* illumos */
2934 * convert the propname into parameters needed by kernel
2935 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2936 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2939 userquota_propname_decode(const char *propname, boolean_t zoned,
2940 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2942 zfs_userquota_prop_t type;
2944 char *numericsid = NULL;
2949 /* Figure out the property type ({user|group}{quota|space}) */
2950 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2951 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2952 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2955 if (type == ZFS_NUM_USERQUOTA_PROPS)
2959 isuser = (type == ZFS_PROP_USERQUOTA ||
2960 type == ZFS_PROP_USERUSED);
2962 cp = strchr(propname, '@') + 1;
2964 if (strchr(cp, '@')) {
2967 * It's a SID name (eg "user@domain") that needs to be
2968 * turned into S-1-domainID-RID.
2971 idmap_stat stat, map_stat;
2974 idmap_get_handle_t *gh = NULL;
2976 stat = idmap_get_create(&gh);
2977 if (stat != IDMAP_SUCCESS) {
2978 idmap_get_destroy(gh);
2981 if (zoned && getzoneid() == GLOBAL_ZONEID)
2984 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2987 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2990 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2993 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2997 idmap_get_destroy(gh);
3000 stat = idmap_get_mappings(gh);
3001 idmap_get_destroy(gh);
3006 if (numericsid == NULL)
3010 /* will be further decoded below */
3011 #else /* !illumos */
3013 #endif /* illumos */
3016 if (strncmp(cp, "S-1-", 4) == 0) {
3017 /* It's a numeric SID (eg "S-1-234-567-89") */
3018 (void) strlcpy(domain, cp, domainlen);
3021 cp = strrchr(domain, '-');
3024 *ridp = strtoull(cp, &end, 10);
3032 if (errno != 0 || *end != '\0')
3034 } else if (!isdigit(*cp)) {
3036 * It's a user/group name (eg "user") that needs to be
3037 * turned into a uid/gid
3039 if (zoned && getzoneid() == GLOBAL_ZONEID)
3055 /* It's a user/group ID (eg "12345"). */
3056 uid_t id = strtoul(cp, &end, 10);
3063 /* It's an ephemeral ID. */
3064 if (idmap_id_to_numeric_domain_rid(id, isuser,
3065 &mapdomain, &rid) != 0)
3067 (void) strlcpy(domain, mapdomain, domainlen);
3074 ASSERT3P(numericsid, ==, NULL);
3079 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3080 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3083 zfs_cmd_t zc = { 0 };
3085 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3087 err = userquota_propname_decode(propname,
3088 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3089 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3090 zc.zc_objset_type = *typep;
3094 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3098 *propvalue = zc.zc_cookie;
3103 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3104 uint64_t *propvalue)
3106 zfs_userquota_prop_t type;
3108 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3113 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3114 char *propbuf, int proplen, boolean_t literal)
3118 zfs_userquota_prop_t type;
3120 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3127 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3128 } else if (propvalue == 0 &&
3129 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
3130 (void) strlcpy(propbuf, "none", proplen);
3132 zfs_nicenum(propvalue, propbuf, proplen);
3138 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3139 uint64_t *propvalue)
3142 zfs_cmd_t zc = { 0 };
3143 const char *snapname;
3145 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3147 snapname = strchr(propname, '@') + 1;
3148 if (strchr(snapname, '@')) {
3149 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3151 /* snapname is the short name, append it to zhp's fsname */
3154 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3155 sizeof (zc.zc_value));
3156 cp = strchr(zc.zc_value, '@');
3159 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3160 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3163 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3167 *propvalue = zc.zc_cookie;
3172 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3173 char *propbuf, int proplen, boolean_t literal)
3178 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3184 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3186 zfs_nicenum(propvalue, propbuf, proplen);
3192 * Returns the name of the given zfs handle.
3195 zfs_get_name(const zfs_handle_t *zhp)
3197 return (zhp->zfs_name);
3201 * Returns the name of the parent pool for the given zfs handle.
3204 zfs_get_pool_name(const zfs_handle_t *zhp)
3206 return (zhp->zpool_hdl->zpool_name);
3210 * Returns the type of the given zfs handle.
3213 zfs_get_type(const zfs_handle_t *zhp)
3215 return (zhp->zfs_type);
3219 * Is one dataset name a child dataset of another?
3221 * Needs to handle these cases:
3222 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3223 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3224 * Descendant? No. No. No. Yes.
3227 is_descendant(const char *ds1, const char *ds2)
3229 size_t d1len = strlen(ds1);
3231 /* ds2 can't be a descendant if it's smaller */
3232 if (strlen(ds2) < d1len)
3235 /* otherwise, compare strings and verify that there's a '/' char */
3236 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3240 * Given a complete name, return just the portion that refers to the parent.
3241 * Will return -1 if there is no parent (path is just the name of the
3245 parent_name(const char *path, char *buf, size_t buflen)
3249 (void) strlcpy(buf, path, buflen);
3251 if ((slashp = strrchr(buf, '/')) == NULL)
3259 * If accept_ancestor is false, then check to make sure that the given path has
3260 * a parent, and that it exists. If accept_ancestor is true, then find the
3261 * closest existing ancestor for the given path. In prefixlen return the
3262 * length of already existing prefix of the given path. We also fetch the
3263 * 'zoned' property, which is used to validate property settings when creating
3267 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3268 boolean_t accept_ancestor, int *prefixlen)
3270 zfs_cmd_t zc = { 0 };
3271 char parent[ZFS_MAX_DATASET_NAME_LEN];
3277 (void) snprintf(errbuf, sizeof (errbuf),
3278 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3280 /* get parent, and check to see if this is just a pool */
3281 if (parent_name(path, parent, sizeof (parent)) != 0) {
3282 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3283 "missing dataset name"));
3284 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3287 /* check to see if the pool exists */
3288 if ((slash = strchr(parent, '/')) == NULL)
3289 slash = parent + strlen(parent);
3290 (void) strncpy(zc.zc_name, parent, slash - parent);
3291 zc.zc_name[slash - parent] = '\0';
3292 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3294 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3295 "no such pool '%s'"), zc.zc_name);
3296 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3299 /* check to see if the parent dataset exists */
3300 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3301 if (errno == ENOENT && accept_ancestor) {
3303 * Go deeper to find an ancestor, give up on top level.
3305 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3306 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3307 "no such pool '%s'"), zc.zc_name);
3308 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3310 } else if (errno == ENOENT) {
3311 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3312 "parent does not exist"));
3313 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3315 return (zfs_standard_error(hdl, errno, errbuf));
3318 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3322 /* we are in a non-global zone, but parent is in the global zone */
3323 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3324 (void) zfs_standard_error(hdl, EPERM, errbuf);
3329 /* make sure parent is a filesystem */
3330 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3331 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3332 "parent is not a filesystem"));
3333 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3339 if (prefixlen != NULL)
3340 *prefixlen = strlen(parent);
3345 * Finds whether the dataset of the given type(s) exists.
3348 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3352 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3356 * Try to get stats for the dataset, which will tell us if it exists.
3358 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3359 int ds_type = zhp->zfs_type;
3362 if (types & ds_type)
3369 * Given a path to 'target', create all the ancestors between
3370 * the prefixlen portion of the path, and the target itself.
3371 * Fail if the initial prefixlen-ancestor does not already exist.
3374 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3380 /* make sure prefix exists */
3381 cp = target + prefixlen;
3383 assert(strchr(cp, '/') == NULL);
3384 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3387 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3395 * Attempt to create, mount, and share any ancestor filesystems,
3396 * up to the prefixlen-long one.
3398 for (cp = target + prefixlen + 1;
3399 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3403 h = make_dataset_handle(hdl, target);
3405 /* it already exists, nothing to do here */
3410 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3412 opname = dgettext(TEXT_DOMAIN, "create");
3416 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3418 opname = dgettext(TEXT_DOMAIN, "open");
3422 if (zfs_mount(h, NULL, 0) != 0) {
3423 opname = dgettext(TEXT_DOMAIN, "mount");
3427 if (zfs_share(h) != 0) {
3428 opname = dgettext(TEXT_DOMAIN, "share");
3438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3439 "failed to %s ancestor '%s'"), opname, target);
3444 * Creates non-existing ancestors of the given path.
3447 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3454 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3455 "cannot create '%s'"), path);
3458 * Check that we are not passing the nesting limit
3459 * before we start creating any ancestors.
3461 if (dataset_nestcheck(path) != 0) {
3462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3463 "maximum name nesting depth exceeded"));
3464 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3467 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3470 if ((path_copy = strdup(path)) != NULL) {
3471 rc = create_parents(hdl, path_copy, prefix);
3474 if (path_copy == NULL || rc != 0)
3481 * Create a new filesystem or volume.
3484 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3489 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3492 enum lzc_dataset_type ost;
3493 zpool_handle_t *zpool_handle;
3495 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3496 "cannot create '%s'"), path);
3498 /* validate the path, taking care to note the extended error message */
3499 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3500 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3502 if (dataset_nestcheck(path) != 0) {
3503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3504 "maximum name nesting depth exceeded"));
3505 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3508 /* validate parents exist */
3509 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3513 * The failure modes when creating a dataset of a different type over
3514 * one that already exists is a little strange. In particular, if you
3515 * try to create a dataset on top of an existing dataset, the ioctl()
3516 * will return ENOENT, not EEXIST. To prevent this from happening, we
3517 * first try to see if the dataset exists.
3519 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3521 "dataset already exists"));
3522 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3525 if (type == ZFS_TYPE_VOLUME)
3526 ost = LZC_DATSET_TYPE_ZVOL;
3528 ost = LZC_DATSET_TYPE_ZFS;
3530 /* open zpool handle for prop validation */
3531 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3532 (void) strlcpy(pool_path, path, sizeof (pool_path));
3534 /* truncate pool_path at first slash */
3535 char *p = strchr(pool_path, '/');
3539 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3542 if (props && (props = zfs_valid_proplist(hdl, type, props,
3543 zoned, NULL, zpool_handle, errbuf)) == 0) {
3544 zpool_close(zpool_handle);
3547 zpool_close(zpool_handle);
3549 if (type == ZFS_TYPE_VOLUME) {
3551 * If we are creating a volume, the size and block size must
3552 * satisfy a few restraints. First, the blocksize must be a
3553 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3554 * volsize must be a multiple of the block size, and cannot be
3557 if (props == NULL || nvlist_lookup_uint64(props,
3558 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3561 "missing volume size"));
3562 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3565 if ((ret = nvlist_lookup_uint64(props,
3566 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3567 &blocksize)) != 0) {
3568 if (ret == ENOENT) {
3569 blocksize = zfs_prop_default_numeric(
3570 ZFS_PROP_VOLBLOCKSIZE);
3573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3574 "missing volume block size"));
3575 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3582 "volume size cannot be zero"));
3583 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3586 if (size % blocksize != 0) {
3588 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3589 "volume size must be a multiple of volume block "
3591 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3595 /* create the dataset */
3596 ret = lzc_create(path, ost, props);
3599 /* check for failure */
3601 char parent[ZFS_MAX_DATASET_NAME_LEN];
3602 (void) parent_name(path, parent, sizeof (parent));
3606 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3607 "no such parent '%s'"), parent);
3608 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3611 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3612 "parent '%s' is not a filesystem"), parent);
3613 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3617 "pool must be upgraded to set this "
3618 "property or value"));
3619 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3622 "invalid property value(s) specified"));
3623 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3627 * This platform can't address a volume this big.
3629 if (type == ZFS_TYPE_VOLUME)
3630 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3635 return (zfs_standard_error(hdl, errno, errbuf));
3643 * Destroys the given dataset. The caller must make sure that the filesystem
3644 * isn't mounted, and that there are no active dependents. If the file system
3645 * does not exist this function does nothing.
3648 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3652 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3655 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3656 nvlist_t *nv = fnvlist_alloc();
3657 fnvlist_add_boolean(nv, zhp->zfs_name);
3658 error = lzc_destroy_bookmarks(nv, NULL);
3661 return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3662 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3668 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3669 nvlist_t *nv = fnvlist_alloc();
3670 fnvlist_add_boolean(nv, zhp->zfs_name);
3671 error = lzc_destroy_snaps(nv, defer, NULL);
3674 error = lzc_destroy(zhp->zfs_name);
3677 if (error != 0 && error != ENOENT) {
3678 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3679 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3683 remove_mountpoint(zhp);
3688 struct destroydata {
3690 const char *snapname;
3694 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3696 struct destroydata *dd = arg;
3697 char name[ZFS_MAX_DATASET_NAME_LEN];
3700 (void) snprintf(name, sizeof (name),
3701 "%s@%s", zhp->zfs_name, dd->snapname);
3703 if (lzc_exists(name))
3704 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3706 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3712 * Destroys all snapshots with the given name in zhp & descendants.
3715 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3718 struct destroydata dd = { 0 };
3720 dd.snapname = snapname;
3721 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3722 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3724 if (nvlist_empty(dd.nvl)) {
3725 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3726 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3727 zhp->zfs_name, snapname);
3729 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3731 nvlist_free(dd.nvl);
3736 * Destroys all the snapshots named in the nvlist.
3739 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3742 nvlist_t *errlist = NULL;
3744 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3747 nvlist_free(errlist);
3751 if (nvlist_empty(errlist)) {
3753 (void) snprintf(errbuf, sizeof (errbuf),
3754 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3756 ret = zfs_standard_error(hdl, ret, errbuf);
3758 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3759 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3761 (void) snprintf(errbuf, sizeof (errbuf),
3762 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3765 switch (fnvpair_value_int32(pair)) {
3768 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3769 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3772 ret = zfs_standard_error(hdl, errno, errbuf);
3777 nvlist_free(errlist);
3782 * Clones the given dataset. The target must be of the same type as the source.
3785 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3787 char parent[ZFS_MAX_DATASET_NAME_LEN];
3790 libzfs_handle_t *hdl = zhp->zfs_hdl;
3793 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3795 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3796 "cannot create '%s'"), target);
3798 /* validate the target/clone name */
3799 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3800 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3802 /* validate parents exist */
3803 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3806 (void) parent_name(target, parent, sizeof (parent));
3813 if (ZFS_IS_VOLUME(zhp)) {
3814 type = ZFS_TYPE_VOLUME;
3816 type = ZFS_TYPE_FILESYSTEM;
3818 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3819 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3821 if (zfs_fix_auto_resv(zhp, props) == -1) {
3827 ret = lzc_clone(target, zhp->zfs_name, props);
3835 * The parent doesn't exist. We should have caught this
3836 * above, but there may a race condition that has since
3837 * destroyed the parent.
3839 * At this point, we don't know whether it's the source
3840 * that doesn't exist anymore, or whether the target
3841 * dataset doesn't exist.
3843 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3844 "no such parent '%s'"), parent);
3845 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3848 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3849 "source and target pools differ"));
3850 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3854 return (zfs_standard_error(zhp->zfs_hdl, errno,
3863 * Promotes the given clone fs to be the clone parent.
3866 zfs_promote(zfs_handle_t *zhp)
3868 libzfs_handle_t *hdl = zhp->zfs_hdl;
3869 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3873 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3874 "cannot promote '%s'"), zhp->zfs_name);
3876 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3877 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3878 "snapshots can not be promoted"));
3879 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3882 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3884 "not a cloned filesystem"));
3885 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3888 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3889 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3891 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3896 /* There is a conflicting snapshot name. */
3897 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3898 "conflicting snapshot '%s' from parent '%s'"),
3899 snapname, zhp->zfs_dmustats.dds_origin);
3900 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3903 return (zfs_standard_error(hdl, ret, errbuf));
3909 typedef struct snapdata {
3911 const char *sd_snapname;
3915 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3917 snapdata_t *sd = arg;
3918 char name[ZFS_MAX_DATASET_NAME_LEN];
3921 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3922 (void) snprintf(name, sizeof (name),
3923 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3925 fnvlist_add_boolean(sd->sd_nvl, name);
3927 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3935 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3940 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3941 "cannot remap dataset '%s'"), fs);
3943 err = lzc_remap(fs);
3948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3949 "pool must be upgraded"));
3950 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3953 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3956 (void) zfs_standard_error(hdl, err, errbuf);
3965 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3969 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3976 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3977 "cannot create snapshots "));
3980 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3981 const char *snapname = nvpair_name(elem);
3983 /* validate the target name */
3984 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3986 (void) snprintf(errbuf, sizeof (errbuf),
3987 dgettext(TEXT_DOMAIN,
3988 "cannot create snapshot '%s'"), snapname);
3989 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3994 * get pool handle for prop validation. assumes all snaps are in the
3995 * same pool, as does lzc_snapshot (below).
3997 char pool[ZFS_MAX_DATASET_NAME_LEN];
3998 elem = nvlist_next_nvpair(snaps, NULL);
3999 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4000 pool[strcspn(pool, "/@")] = '\0';
4001 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
4003 if (props != NULL &&
4004 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4005 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
4006 zpool_close(zpool_hdl);
4009 zpool_close(zpool_hdl);
4011 ret = lzc_snapshot(snaps, props, &errors);
4014 boolean_t printed = B_FALSE;
4015 for (elem = nvlist_next_nvpair(errors, NULL);
4017 elem = nvlist_next_nvpair(errors, elem)) {
4018 (void) snprintf(errbuf, sizeof (errbuf),
4019 dgettext(TEXT_DOMAIN,
4020 "cannot create snapshot '%s'"), nvpair_name(elem));
4021 (void) zfs_standard_error(hdl,
4022 fnvpair_value_int32(elem), errbuf);
4028 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4029 "multiple snapshots of same "
4031 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4035 (void) zfs_standard_error(hdl, ret, errbuf);
4041 nvlist_free(errors);
4046 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4050 snapdata_t sd = { 0 };
4051 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4056 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4057 "cannot snapshot %s"), path);
4059 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4060 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4062 (void) strlcpy(fsname, path, sizeof (fsname));
4063 cp = strchr(fsname, '@');
4065 sd.sd_snapname = cp + 1;
4067 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4068 ZFS_TYPE_VOLUME)) == NULL) {
4072 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4074 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4076 fnvlist_add_boolean(sd.sd_nvl, path);
4079 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4080 nvlist_free(sd.sd_nvl);
4086 * Destroy any more recent snapshots. We invoke this callback on any dependents
4087 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4088 * is a dependent and we should just destroy it without checking the transaction
4091 typedef struct rollback_data {
4092 const char *cb_target; /* the snapshot */
4093 uint64_t cb_create; /* creation time reference */
4099 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4101 rollback_data_t *cbp = data;
4102 prop_changelist_t *clp;
4104 /* We must destroy this clone; first unmount it */
4105 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4106 cbp->cb_force ? MS_FORCE: 0);
4107 if (clp == NULL || changelist_prefix(clp) != 0) {
4108 cbp->cb_error = B_TRUE;
4112 if (zfs_destroy(zhp, B_FALSE) != 0)
4113 cbp->cb_error = B_TRUE;
4115 changelist_remove(clp, zhp->zfs_name);
4116 (void) changelist_postfix(clp);
4117 changelist_free(clp);
4124 rollback_destroy(zfs_handle_t *zhp, void *data)
4126 rollback_data_t *cbp = data;
4128 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4129 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4130 rollback_destroy_dependent, cbp);
4132 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4140 * Given a dataset, rollback to a specific snapshot, discarding any
4141 * data changes since then and making it the active dataset.
4143 * Any snapshots and bookmarks more recent than the target are
4144 * destroyed, along with their dependents (i.e. clones).
4147 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4149 rollback_data_t cb = { 0 };
4151 boolean_t restore_resv = 0;
4152 uint64_t old_volsize = 0, new_volsize;
4153 zfs_prop_t resv_prop;
4155 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4156 zhp->zfs_type == ZFS_TYPE_VOLUME);
4159 * Destroy all recent snapshots and their dependents.
4161 cb.cb_force = force;
4162 cb.cb_target = snap->zfs_name;
4163 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4164 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4165 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4171 * Now that we have verified that the snapshot is the latest,
4172 * rollback to the given snapshot.
4175 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4176 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4178 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4180 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4184 * Pass both the filesystem and the wanted snapshot names,
4185 * we would get an error back if the snapshot is destroyed or
4186 * a new snapshot is created before this request is processed.
4188 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4192 (void) snprintf(errbuf, sizeof (errbuf),
4193 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4197 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4198 "there is a snapshot or bookmark more recent "
4199 "than '%s'"), snap->zfs_name);
4200 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4203 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4204 "'%s' is not found among snapshots of '%s'"),
4205 snap->zfs_name, zhp->zfs_name);
4206 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4209 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4212 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4218 * For volumes, if the pre-rollback volsize matched the pre-
4219 * rollback reservation and the volsize has changed then set
4220 * the reservation property to the post-rollback volsize.
4221 * Make a new handle since the rollback closed the dataset.
4223 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4224 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4226 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4227 if (old_volsize != new_volsize)
4228 err = zfs_prop_set_int(zhp, resv_prop,
4237 * Renames the given dataset.
4240 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
4241 renameflags_t flags)
4244 zfs_cmd_t zc = { 0 };
4246 prop_changelist_t *cl = NULL;
4247 zfs_handle_t *zhrp = NULL;
4248 char *parentname = NULL;
4249 char parent[ZFS_MAX_DATASET_NAME_LEN];
4250 char property[ZFS_MAXPROPLEN];
4251 libzfs_handle_t *hdl = zhp->zfs_hdl;
4254 /* if we have the same exact name, just return success */
4255 if (strcmp(zhp->zfs_name, target) == 0)
4258 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4259 "cannot rename to '%s'"), target);
4261 if (source != NULL) {
4263 * This is recursive snapshots rename, put snapshot name
4264 * (that might not exist) into zfs_name.
4266 assert(flags.recurse);
4268 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
4269 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
4270 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
4273 /* make sure source name is valid */
4274 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4275 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4278 * Make sure the target name is valid
4280 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4281 if ((strchr(target, '@') == NULL) ||
4284 * Snapshot target name is abbreviated,
4285 * reconstruct full dataset name
4287 (void) strlcpy(parent, zhp->zfs_name,
4289 delim = strchr(parent, '@');
4290 if (strchr(target, '@') == NULL)
4294 (void) strlcat(parent, target, sizeof (parent));
4298 * Make sure we're renaming within the same dataset.
4300 delim = strchr(target, '@');
4301 if (strncmp(zhp->zfs_name, target, delim - target)
4302 != 0 || zhp->zfs_name[delim - target] != '@') {
4303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4304 "snapshots must be part of same "
4306 return (zfs_error(hdl, EZFS_CROSSTARGET,
4311 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4312 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4314 if (flags.recurse) {
4315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4316 "recursive rename must be a snapshot"));
4317 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4320 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4321 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4323 /* validate parents */
4324 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4327 /* make sure we're in the same pool */
4328 verify((delim = strchr(target, '/')) != NULL);
4329 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4330 zhp->zfs_name[delim - target] != '/') {
4331 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4332 "datasets must be within same pool"));
4333 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4336 /* new name cannot be a child of the current dataset name */
4337 if (is_descendant(zhp->zfs_name, target)) {
4338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4339 "New dataset name cannot be a descendant of "
4340 "current dataset name"));
4341 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4345 (void) snprintf(errbuf, sizeof (errbuf),
4346 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4348 if (getzoneid() == GLOBAL_ZONEID &&
4349 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4350 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4351 "dataset is used in a non-global zone"));
4352 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4356 * Avoid unmounting file systems with mountpoint property set to
4357 * 'legacy' or 'none' even if -u option is not given.
4359 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4360 !flags.recurse && !flags.nounmount &&
4361 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4362 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4363 (strcmp(property, "legacy") == 0 ||
4364 strcmp(property, "none") == 0)) {
4365 flags.nounmount = B_TRUE;
4367 if (flags.recurse) {
4369 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4370 if (parentname == NULL) {
4374 delim = strchr(parentname, '@');
4376 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4381 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4382 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4383 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
4384 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
4388 if (changelist_haszonedchild(cl)) {
4389 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4390 "child dataset with inherited mountpoint is used "
4391 "in a non-global zone"));
4392 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4397 if ((ret = changelist_prefix(cl)) != 0)
4401 if (ZFS_IS_VOLUME(zhp))
4402 zc.zc_objset_type = DMU_OST_ZVOL;
4404 zc.zc_objset_type = DMU_OST_ZFS;
4406 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4407 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4409 zc.zc_cookie = flags.recurse ? 1 : 0;
4410 if (flags.nounmount)
4413 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4415 * if it was recursive, the one that actually failed will
4418 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4419 "cannot rename '%s'"), zc.zc_name);
4421 if (flags.recurse && errno == EEXIST) {
4422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4423 "a child dataset already has a snapshot "
4424 "with the new name"));
4425 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4427 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4431 * On failure, we still want to remount any filesystems that
4432 * were previously mounted, so we don't alter the system state.
4435 (void) changelist_postfix(cl);
4438 changelist_rename(cl, zfs_get_name(zhp), target);
4439 ret = changelist_postfix(cl);
4444 if (parentname != NULL) {
4451 changelist_free(cl);
4457 zfs_get_user_props(zfs_handle_t *zhp)
4459 return (zhp->zfs_user_props);
4463 zfs_get_recvd_props(zfs_handle_t *zhp)
4465 if (zhp->zfs_recvd_props == NULL)
4466 if (get_recvd_props_ioctl(zhp) != 0)
4468 return (zhp->zfs_recvd_props);
4472 * This function is used by 'zfs list' to determine the exact set of columns to
4473 * display, and their maximum widths. This does two main things:
4475 * - If this is a list of all properties, then expand the list to include
4476 * all native properties, and set a flag so that for each dataset we look
4477 * for new unique user properties and add them to the list.
4479 * - For non fixed-width properties, keep track of the maximum width seen
4480 * so that we can size the column appropriately. If the user has
4481 * requested received property values, we also need to compute the width
4482 * of the RECEIVED column.
4485 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4488 libzfs_handle_t *hdl = zhp->zfs_hdl;
4489 zprop_list_t *entry;
4490 zprop_list_t **last, **start;
4491 nvlist_t *userprops, *propval;
4494 char buf[ZFS_MAXPROPLEN];
4496 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4499 userprops = zfs_get_user_props(zhp);
4502 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4504 * Go through and add any user properties as necessary. We
4505 * start by incrementing our list pointer to the first
4506 * non-native property.
4509 while (*start != NULL) {
4510 if ((*start)->pl_prop == ZPROP_INVAL)
4512 start = &(*start)->pl_next;
4516 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4518 * See if we've already found this property in our list.
4520 for (last = start; *last != NULL;
4521 last = &(*last)->pl_next) {
4522 if (strcmp((*last)->pl_user_prop,
4523 nvpair_name(elem)) == 0)
4527 if (*last == NULL) {
4528 if ((entry = zfs_alloc(hdl,
4529 sizeof (zprop_list_t))) == NULL ||
4530 ((entry->pl_user_prop = zfs_strdup(hdl,
4531 nvpair_name(elem)))) == NULL) {
4536 entry->pl_prop = ZPROP_INVAL;
4537 entry->pl_width = strlen(nvpair_name(elem));
4538 entry->pl_all = B_TRUE;
4545 * Now go through and check the width of any non-fixed columns
4547 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4548 if (entry->pl_fixed && !literal)
4551 if (entry->pl_prop != ZPROP_INVAL) {
4552 if (zfs_prop_get(zhp, entry->pl_prop,
4553 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4554 if (strlen(buf) > entry->pl_width)
4555 entry->pl_width = strlen(buf);
4557 if (received && zfs_prop_get_recvd(zhp,
4558 zfs_prop_to_name(entry->pl_prop),
4559 buf, sizeof (buf), literal) == 0)
4560 if (strlen(buf) > entry->pl_recvd_width)
4561 entry->pl_recvd_width = strlen(buf);
4563 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4565 verify(nvlist_lookup_string(propval,
4566 ZPROP_VALUE, &strval) == 0);
4567 if (strlen(strval) > entry->pl_width)
4568 entry->pl_width = strlen(strval);
4570 if (received && zfs_prop_get_recvd(zhp,
4571 entry->pl_user_prop,
4572 buf, sizeof (buf), literal) == 0)
4573 if (strlen(buf) > entry->pl_recvd_width)
4574 entry->pl_recvd_width = strlen(buf);
4582 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4583 char *resource, void *export, void *sharetab,
4584 int sharemax, zfs_share_op_t operation)
4586 zfs_cmd_t zc = { 0 };
4589 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4590 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4592 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4593 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4594 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4595 zc.zc_share.z_sharetype = operation;
4596 zc.zc_share.z_sharemax = sharemax;
4597 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4602 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4607 * Keep a reference to the props-table against which we prune the
4610 zhp->zfs_props_table = props;
4612 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4615 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4616 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4619 * User properties will result in ZPROP_INVAL, and since we
4620 * only know how to prune standard ZFS properties, we always
4621 * leave these in the list. This can also happen if we
4622 * encounter an unknown DSL property (when running older
4623 * software, for example).
4625 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4626 (void) nvlist_remove(zhp->zfs_props,
4627 nvpair_name(curr), nvpair_type(curr));
4634 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4635 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4637 zfs_cmd_t zc = { 0 };
4638 nvlist_t *nvlist = NULL;
4641 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4642 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4643 zc.zc_cookie = (uint64_t)cmd;
4645 if (cmd == ZFS_SMB_ACL_RENAME) {
4646 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4647 (void) no_memory(hdl);
4653 case ZFS_SMB_ACL_ADD:
4654 case ZFS_SMB_ACL_REMOVE:
4655 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4657 case ZFS_SMB_ACL_RENAME:
4658 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4660 (void) no_memory(hdl);
4663 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4665 (void) no_memory(hdl);
4668 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4669 nvlist_free(nvlist);
4673 case ZFS_SMB_ACL_PURGE:
4678 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4679 nvlist_free(nvlist);
4684 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4685 char *path, char *resource)
4687 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4692 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4693 char *path, char *resource)
4695 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4700 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4702 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4707 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4708 char *oldname, char *newname)
4710 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4713 #endif /* illumos */
4716 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4717 zfs_userspace_cb_t func, void *arg)
4719 zfs_cmd_t zc = { 0 };
4720 zfs_useracct_t buf[100];
4721 libzfs_handle_t *hdl = zhp->zfs_hdl;
4724 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4726 zc.zc_objset_type = type;
4727 zc.zc_nvlist_dst = (uintptr_t)buf;
4730 zfs_useracct_t *zua = buf;
4732 zc.zc_nvlist_dst_size = sizeof (buf);
4733 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4736 (void) snprintf(errbuf, sizeof (errbuf),
4737 dgettext(TEXT_DOMAIN,
4738 "cannot get used/quota for %s"), zc.zc_name);
4739 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4741 if (zc.zc_nvlist_dst_size == 0)
4744 while (zc.zc_nvlist_dst_size > 0) {
4745 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4746 zua->zu_space)) != 0)
4749 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4758 const char *snapname;
4760 boolean_t recursive;
4765 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4767 struct holdarg *ha = arg;
4768 char name[ZFS_MAX_DATASET_NAME_LEN];
4771 (void) snprintf(name, sizeof (name),
4772 "%s@%s", zhp->zfs_name, ha->snapname);
4774 if (lzc_exists(name))
4775 fnvlist_add_string(ha->nvl, name, ha->tag);
4778 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4784 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4785 boolean_t recursive, int cleanup_fd)
4790 ha.nvl = fnvlist_alloc();
4791 ha.snapname = snapname;
4793 ha.recursive = recursive;
4794 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4796 if (nvlist_empty(ha.nvl)) {
4799 fnvlist_free(ha.nvl);
4801 (void) snprintf(errbuf, sizeof (errbuf),
4802 dgettext(TEXT_DOMAIN,
4803 "cannot hold snapshot '%s@%s'"),
4804 zhp->zfs_name, snapname);
4805 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4809 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4810 fnvlist_free(ha.nvl);
4816 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4820 libzfs_handle_t *hdl = zhp->zfs_hdl;
4825 ret = lzc_hold(holds, cleanup_fd, &errors);
4828 /* There may be errors even in the success case. */
4829 fnvlist_free(errors);
4833 if (nvlist_empty(errors)) {
4834 /* no hold-specific errors */
4835 (void) snprintf(errbuf, sizeof (errbuf),
4836 dgettext(TEXT_DOMAIN, "cannot hold"));
4839 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4840 "pool must be upgraded"));
4841 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4844 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4847 (void) zfs_standard_error(hdl, ret, errbuf);
4851 for (elem = nvlist_next_nvpair(errors, NULL);
4853 elem = nvlist_next_nvpair(errors, elem)) {
4854 (void) snprintf(errbuf, sizeof (errbuf),
4855 dgettext(TEXT_DOMAIN,
4856 "cannot hold snapshot '%s'"), nvpair_name(elem));
4857 switch (fnvpair_value_int32(elem)) {
4860 * Temporary tags wind up having the ds object id
4861 * prepended. So even if we passed the length check
4862 * above, it's still possible for the tag to wind
4863 * up being slightly too long.
4865 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4868 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4871 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4874 (void) zfs_standard_error(hdl,
4875 fnvpair_value_int32(elem), errbuf);
4879 fnvlist_free(errors);
4884 zfs_release_one(zfs_handle_t *zhp, void *arg)
4886 struct holdarg *ha = arg;
4887 char name[ZFS_MAX_DATASET_NAME_LEN];
4889 nvlist_t *existing_holds;
4891 (void) snprintf(name, sizeof (name),
4892 "%s@%s", zhp->zfs_name, ha->snapname);
4894 if (lzc_get_holds(name, &existing_holds) != 0) {
4896 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4899 nvlist_t *torelease = fnvlist_alloc();
4900 fnvlist_add_boolean(torelease, ha->tag);
4901 fnvlist_add_nvlist(ha->nvl, name, torelease);
4902 fnvlist_free(torelease);
4906 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4912 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4913 boolean_t recursive)
4917 nvlist_t *errors = NULL;
4919 libzfs_handle_t *hdl = zhp->zfs_hdl;
4922 ha.nvl = fnvlist_alloc();
4923 ha.snapname = snapname;
4925 ha.recursive = recursive;
4927 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4929 if (nvlist_empty(ha.nvl)) {
4930 fnvlist_free(ha.nvl);
4932 (void) snprintf(errbuf, sizeof (errbuf),
4933 dgettext(TEXT_DOMAIN,
4934 "cannot release hold from snapshot '%s@%s'"),
4935 zhp->zfs_name, snapname);
4937 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4939 (void) zfs_standard_error(hdl, ret, errbuf);
4944 ret = lzc_release(ha.nvl, &errors);
4945 fnvlist_free(ha.nvl);
4948 /* There may be errors even in the success case. */
4949 fnvlist_free(errors);
4953 if (nvlist_empty(errors)) {
4954 /* no hold-specific errors */
4955 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4960 "pool must be upgraded"));
4961 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4964 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4968 for (elem = nvlist_next_nvpair(errors, NULL);
4970 elem = nvlist_next_nvpair(errors, elem)) {
4971 (void) snprintf(errbuf, sizeof (errbuf),
4972 dgettext(TEXT_DOMAIN,
4973 "cannot release hold from snapshot '%s'"),
4975 switch (fnvpair_value_int32(elem)) {
4977 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4980 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4983 (void) zfs_standard_error_fmt(hdl,
4984 fnvpair_value_int32(elem), errbuf);
4988 fnvlist_free(errors);
4993 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4995 zfs_cmd_t zc = { 0 };
4996 libzfs_handle_t *hdl = zhp->zfs_hdl;
5002 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5003 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5007 nvbuf = malloc(nvsz);
5008 if (nvbuf == NULL) {
5009 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5013 zc.zc_nvlist_dst_size = nvsz;
5014 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5016 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5018 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
5019 (void) snprintf(errbuf, sizeof (errbuf),
5020 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5025 nvsz = zc.zc_nvlist_dst_size;
5029 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5030 "pool must be upgraded"));
5031 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5034 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5037 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5040 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5045 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5047 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
5048 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5050 err = zfs_standard_error_fmt(hdl, rc, errbuf);
5060 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5062 zfs_cmd_t zc = { 0 };
5063 libzfs_handle_t *hdl = zhp->zfs_hdl;
5069 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5070 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5072 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5075 nvbuf = malloc(nvsz);
5077 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5080 zc.zc_nvlist_src_size = nvsz;
5081 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5082 zc.zc_perm_action = un;
5084 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5086 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5087 (void) snprintf(errbuf, sizeof (errbuf),
5088 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5093 "pool must be upgraded"));
5094 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5097 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5100 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5103 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5114 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5119 err = lzc_get_holds(zhp->zfs_name, nvl);
5122 libzfs_handle_t *hdl = zhp->zfs_hdl;
5124 (void) snprintf(errbuf, sizeof (errbuf),
5125 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5130 "pool must be upgraded"));
5131 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5134 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5137 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5140 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5149 * Convert the zvol's volume size to an appropriate reservation.
5150 * Note: If this routine is updated, it is necessary to update the ZFS test
5151 * suite's shell version in reservation.kshlib.
5154 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5157 uint64_t nblocks, volblocksize;
5161 if (nvlist_lookup_string(props,
5162 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5163 ncopies = atoi(strval);
5166 if (nvlist_lookup_uint64(props,
5167 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5168 &volblocksize) != 0)
5169 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5170 nblocks = volsize/volblocksize;
5171 /* start with metadnode L0-L6 */
5173 /* calculate number of indirects */
5174 while (nblocks > 1) {
5175 nblocks += DNODES_PER_LEVEL - 1;
5176 nblocks /= DNODES_PER_LEVEL;
5179 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5182 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5183 * compressed, but in practice they compress down to about
5186 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5192 * Attach/detach the given filesystem to/from the given jail.
5195 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
5197 libzfs_handle_t *hdl = zhp->zfs_hdl;
5198 zfs_cmd_t zc = { 0 };
5204 (void) snprintf(errbuf, sizeof (errbuf),
5205 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
5207 (void) snprintf(errbuf, sizeof (errbuf),
5208 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
5211 switch (zhp->zfs_type) {
5212 case ZFS_TYPE_VOLUME:
5213 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5214 "volumes can not be jailed"));
5215 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5216 case ZFS_TYPE_SNAPSHOT:
5217 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5218 "snapshots can not be jailed"));
5219 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5221 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5223 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5224 zc.zc_objset_type = DMU_OST_ZFS;
5225 zc.zc_jailid = jailid;
5227 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
5228 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
5229 zfs_standard_error(hdl, errno, errbuf);