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 2019 Joyent, Inc.
25 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017-2018 RackTop Systems.
33 * Copyright (c) 2019 Datto Inc.
34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
56 #include <directory.h>
57 #endif /* HAVE_IDMAP */
59 #include <sys/dnode.h>
62 #include <sys/dsl_crypt.h>
66 #include "zfs_namecheck.h"
68 #include "libzfs_impl.h"
70 #include "zfs_deleg.h"
72 static int userquota_propname_decode(const char *propname, boolean_t zoned,
73 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
76 * Given a single type (not a mask of types), return the type in a human
80 zfs_type_to_name(zfs_type_t type)
83 case ZFS_TYPE_FILESYSTEM:
84 return (dgettext(TEXT_DOMAIN, "filesystem"));
85 case ZFS_TYPE_SNAPSHOT:
86 return (dgettext(TEXT_DOMAIN, "snapshot"));
88 return (dgettext(TEXT_DOMAIN, "volume"));
90 return (dgettext(TEXT_DOMAIN, "pool"));
91 case ZFS_TYPE_BOOKMARK:
92 return (dgettext(TEXT_DOMAIN, "bookmark"));
94 assert(!"unhandled zfs_type_t");
101 * Validate a ZFS path. This is used even before trying to open the dataset, to
102 * provide a more meaningful error message. We call zfs_error_aux() to
103 * explain exactly why the name was not valid.
106 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
112 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
115 "snapshot delimiter '@' is not expected here"));
119 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
122 "missing '@' delimiter in snapshot name"));
126 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
129 "bookmark delimiter '#' is not expected here"));
133 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
136 "missing '#' delimiter in bookmark name"));
140 if (modifying && strchr(path, '%') != NULL) {
142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 "invalid character %c in name"), '%');
147 if (entity_namecheck(path, &why, &what) != 0) {
150 case NAME_ERR_TOOLONG:
151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152 "name is too long"));
155 case NAME_ERR_LEADING_SLASH:
156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
157 "leading slash in name"));
160 case NAME_ERR_EMPTY_COMPONENT:
161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
162 "empty component or misplaced '@'"
163 " or '#' delimiter in name"));
166 case NAME_ERR_TRAILING_SLASH:
167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
168 "trailing slash in name"));
171 case NAME_ERR_INVALCHAR:
173 dgettext(TEXT_DOMAIN, "invalid character "
174 "'%c' in name"), what);
177 case NAME_ERR_MULTIPLE_DELIMITERS:
178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
179 "multiple '@' and/or '#' delimiters in "
183 case NAME_ERR_NOLETTER:
184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185 "pool doesn't begin with a letter"));
188 case NAME_ERR_RESERVED:
189 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
190 "name is reserved"));
193 case NAME_ERR_DISKLIKE:
194 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
195 "reserved disk name"));
198 case NAME_ERR_SELF_REF:
199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200 "self reference, '.' is found in name"));
203 case NAME_ERR_PARENT_REF:
204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
205 "parent reference, '..' is found in name"));
209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
210 "(%d) not defined"), why);
222 zfs_name_valid(const char *name, zfs_type_t type)
224 if (type == ZFS_TYPE_POOL)
225 return (zpool_name_valid(NULL, B_FALSE, name));
226 return (zfs_validate_name(NULL, name, type, B_FALSE));
230 * This function takes the raw DSL properties, and filters out the user-defined
231 * properties into a separate nvlist.
234 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
236 libzfs_handle_t *hdl = zhp->zfs_hdl;
241 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
242 (void) no_memory(hdl);
247 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
248 if (!zfs_prop_user(nvpair_name(elem)))
251 verify(nvpair_value_nvlist(elem, &propval) == 0);
252 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
254 (void) no_memory(hdl);
262 static zpool_handle_t *
263 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
265 libzfs_handle_t *hdl = zhp->zfs_hdl;
268 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
269 if (hdl->libzfs_pool_handles != NULL)
270 zph->zpool_next = hdl->libzfs_pool_handles;
271 hdl->libzfs_pool_handles = zph;
276 static zpool_handle_t *
277 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
279 libzfs_handle_t *hdl = zhp->zfs_hdl;
280 zpool_handle_t *zph = hdl->libzfs_pool_handles;
282 while ((zph != NULL) &&
283 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
284 zph = zph->zpool_next;
289 * Returns a handle to the pool that contains the provided dataset.
290 * If a handle to that pool already exists then that handle is returned.
291 * Otherwise, a new handle is created and added to the list of handles.
293 static zpool_handle_t *
294 zpool_handle(zfs_handle_t *zhp)
300 len = strcspn(zhp->zfs_name, "/@#") + 1;
301 pool_name = zfs_alloc(zhp->zfs_hdl, len);
302 (void) strlcpy(pool_name, zhp->zfs_name, len);
304 zph = zpool_find_handle(zhp, pool_name, len);
306 zph = zpool_add_handle(zhp, pool_name);
313 zpool_free_handles(libzfs_handle_t *hdl)
315 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
317 while (zph != NULL) {
318 next = zph->zpool_next;
322 hdl->libzfs_pool_handles = NULL;
326 * Utility function to gather stats (objset and zpl) for the given object.
329 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
331 libzfs_handle_t *hdl = zhp->zfs_hdl;
333 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
335 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, zc) != 0) {
336 if (errno == ENOMEM) {
337 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
348 * Utility function to get the received properties of the given object.
351 get_recvd_props_ioctl(zfs_handle_t *zhp)
353 libzfs_handle_t *hdl = zhp->zfs_hdl;
354 nvlist_t *recvdprops;
355 zfs_cmd_t zc = {"\0"};
358 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
361 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
363 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
364 if (errno == ENOMEM) {
365 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
369 zcmd_free_nvlists(&zc);
374 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
375 zcmd_free_nvlists(&zc);
379 nvlist_free(zhp->zfs_recvd_props);
380 zhp->zfs_recvd_props = recvdprops;
386 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
388 nvlist_t *allprops, *userprops;
390 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
392 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
397 * XXX Why do we store the user props separately, in addition to
398 * storing them in zfs_props?
400 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
401 nvlist_free(allprops);
405 nvlist_free(zhp->zfs_props);
406 nvlist_free(zhp->zfs_user_props);
408 zhp->zfs_props = allprops;
409 zhp->zfs_user_props = userprops;
415 get_stats(zfs_handle_t *zhp)
418 zfs_cmd_t zc = {"\0"};
420 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
422 if (get_stats_ioctl(zhp, &zc) != 0)
424 else if (put_stats_zhdl(zhp, &zc) != 0)
426 zcmd_free_nvlists(&zc);
431 * Refresh the properties currently stored in the handle.
434 zfs_refresh_properties(zfs_handle_t *zhp)
436 (void) get_stats(zhp);
440 * Makes a handle from the given dataset name. Used by zfs_open() and
441 * zfs_iter_* to create child handles on the fly.
444 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
446 if (put_stats_zhdl(zhp, zc) != 0)
450 * We've managed to open the dataset and gather statistics. Determine
451 * the high-level type.
453 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
454 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
455 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
456 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
457 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
462 if (zhp->zfs_dmustats.dds_is_snapshot)
463 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
464 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
465 zhp->zfs_type = ZFS_TYPE_VOLUME;
466 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
467 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
469 abort(); /* we should never see any other types */
471 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
478 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
480 zfs_cmd_t zc = {"\0"};
482 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
488 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
489 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
493 if (get_stats_ioctl(zhp, &zc) == -1) {
494 zcmd_free_nvlists(&zc);
498 if (make_dataset_handle_common(zhp, &zc) == -1) {
502 zcmd_free_nvlists(&zc);
507 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
509 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
515 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
516 if (make_dataset_handle_common(zhp, zc) == -1) {
524 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
526 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
531 zhp->zfs_hdl = pzhp->zfs_hdl;
532 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
533 zhp->zfs_head_type = pzhp->zfs_type;
534 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
535 zhp->zpool_hdl = zpool_handle(zhp);
541 zfs_handle_dup(zfs_handle_t *zhp_orig)
543 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
548 zhp->zfs_hdl = zhp_orig->zfs_hdl;
549 zhp->zpool_hdl = zhp_orig->zpool_hdl;
550 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
551 sizeof (zhp->zfs_name));
552 zhp->zfs_type = zhp_orig->zfs_type;
553 zhp->zfs_head_type = zhp_orig->zfs_head_type;
554 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
555 if (zhp_orig->zfs_props != NULL) {
556 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
557 (void) no_memory(zhp->zfs_hdl);
562 if (zhp_orig->zfs_user_props != NULL) {
563 if (nvlist_dup(zhp_orig->zfs_user_props,
564 &zhp->zfs_user_props, 0) != 0) {
565 (void) no_memory(zhp->zfs_hdl);
570 if (zhp_orig->zfs_recvd_props != NULL) {
571 if (nvlist_dup(zhp_orig->zfs_recvd_props,
572 &zhp->zfs_recvd_props, 0)) {
573 (void) no_memory(zhp->zfs_hdl);
578 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
579 if (zhp_orig->zfs_mntopts != NULL) {
580 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
581 zhp_orig->zfs_mntopts);
583 zhp->zfs_props_table = zhp_orig->zfs_props_table;
588 zfs_bookmark_exists(const char *path)
592 char fsname[ZFS_MAX_DATASET_NAME_LEN];
598 (void) strlcpy(fsname, path, sizeof (fsname));
599 pound = strchr(fsname, '#');
604 bmark_name = pound + 1;
605 props = fnvlist_alloc();
606 err = lzc_get_bookmarks(fsname, props, &bmarks);
613 rv = nvlist_exists(bmarks, bmark_name);
619 make_bookmark_handle(zfs_handle_t *parent, const char *path,
620 nvlist_t *bmark_props)
622 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
627 /* Fill in the name. */
628 zhp->zfs_hdl = parent->zfs_hdl;
629 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
631 /* Set the property lists. */
632 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
638 zhp->zfs_head_type = parent->zfs_head_type;
639 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
641 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
642 nvlist_free(zhp->zfs_props);
650 struct zfs_open_bookmarks_cb_data {
656 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
658 struct zfs_open_bookmarks_cb_data *dp = data;
661 * Is it the one we are looking for?
663 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
665 * We found it. Save it and let the caller know we are done.
672 * Not found. Close the handle and ask for another one.
679 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
680 * argument is a mask of acceptable types. The function will print an
681 * appropriate error message and return NULL if it can't be opened.
684 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
690 (void) snprintf(errbuf, sizeof (errbuf),
691 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
694 * Validate the name before we even try to open it.
696 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
697 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
702 * Bookmarks needs to be handled separately.
704 bookp = strchr(path, '#');
707 * Try to get stats for the dataset, which will tell us if it
711 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
712 (void) zfs_standard_error(hdl, errno, errbuf);
716 char dsname[ZFS_MAX_DATASET_NAME_LEN];
718 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
721 * We need to cut out '#' and everything after '#'
722 * to get the parent dataset name only.
724 assert(bookp - path < sizeof (dsname));
725 (void) strncpy(dsname, path, bookp - path);
726 dsname[bookp - path] = '\0';
729 * Create handle for the parent dataset.
732 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
733 (void) zfs_standard_error(hdl, errno, errbuf);
738 * Iterate bookmarks to find the right one.
741 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
742 &cb_data) == 0) && (cb_data.zhp == NULL)) {
743 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
747 if (cb_data.zhp == NULL) {
748 (void) zfs_standard_error(hdl, errno, errbuf);
760 if (!(types & zhp->zfs_type)) {
761 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
770 * Release a ZFS handle. Nothing to do but free the associated memory.
773 zfs_close(zfs_handle_t *zhp)
775 if (zhp->zfs_mntopts)
776 free(zhp->zfs_mntopts);
777 nvlist_free(zhp->zfs_props);
778 nvlist_free(zhp->zfs_user_props);
779 nvlist_free(zhp->zfs_recvd_props);
783 typedef struct mnttab_node {
784 struct mnttab mtn_mt;
789 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
791 const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
792 const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
795 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
797 return (TREE_ISIGN(rv));
801 libzfs_mnttab_init(libzfs_handle_t *hdl)
803 pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL);
804 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
805 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
806 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
810 libzfs_mnttab_update(libzfs_handle_t *hdl)
814 /* Reopen MNTTAB to prevent reading stale data from open file */
815 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
818 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
822 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
825 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
826 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
827 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
828 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
829 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
831 /* Exclude duplicate mounts */
832 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
833 free(mtn->mtn_mt.mnt_special);
834 free(mtn->mtn_mt.mnt_mountp);
835 free(mtn->mtn_mt.mnt_fstype);
836 free(mtn->mtn_mt.mnt_mntopts);
841 avl_add(&hdl->libzfs_mnttab_cache, mtn);
848 libzfs_mnttab_fini(libzfs_handle_t *hdl)
853 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
855 free(mtn->mtn_mt.mnt_special);
856 free(mtn->mtn_mt.mnt_mountp);
857 free(mtn->mtn_mt.mnt_fstype);
858 free(mtn->mtn_mt.mnt_mntopts);
861 avl_destroy(&hdl->libzfs_mnttab_cache);
862 (void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
866 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
868 hdl->libzfs_mnttab_enable = enable;
872 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
873 struct mnttab *entry)
879 if (!hdl->libzfs_mnttab_enable) {
880 struct mnttab srch = { 0 };
882 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
883 libzfs_mnttab_fini(hdl);
885 /* Reopen MNTTAB to prevent reading stale data from open file */
886 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
889 srch.mnt_special = (char *)fsname;
890 srch.mnt_fstype = MNTTYPE_ZFS;
891 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
897 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
898 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) {
901 if ((error = libzfs_mnttab_update(hdl)) != 0) {
902 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
907 find.mtn_mt.mnt_special = (char *)fsname;
908 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
910 *entry = mtn->mtn_mt;
913 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
918 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
919 const char *mountp, const char *mntopts)
923 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
924 if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
925 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
926 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
927 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
928 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
929 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
931 * Another thread may have already added this entry
932 * via libzfs_mnttab_update. If so we should skip it.
934 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, NULL) != NULL)
937 avl_add(&hdl->libzfs_mnttab_cache, mtn);
939 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
943 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
948 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
949 find.mtn_mt.mnt_special = (char *)fsname;
950 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
952 avl_remove(&hdl->libzfs_mnttab_cache, ret);
953 free(ret->mtn_mt.mnt_special);
954 free(ret->mtn_mt.mnt_mountp);
955 free(ret->mtn_mt.mnt_fstype);
956 free(ret->mtn_mt.mnt_mntopts);
959 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
963 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
965 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
967 if (zpool_handle == NULL)
970 *spa_version = zpool_get_prop_int(zpool_handle,
971 ZPOOL_PROP_VERSION, NULL);
976 * The choice of reservation property depends on the SPA version.
979 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
983 if (zfs_spa_version(zhp, &spa_version) < 0)
986 if (spa_version >= SPA_VERSION_REFRESERVATION)
987 *resv_prop = ZFS_PROP_REFRESERVATION;
989 *resv_prop = ZFS_PROP_RESERVATION;
995 * Given an nvlist of properties to set, validates that they are correct, and
996 * parses any numeric properties (index, boolean, etc) if they are specified as
1000 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
1001 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
1002 boolean_t key_params_ok, const char *errbuf)
1009 int chosen_normal = -1;
1010 int chosen_utf = -1;
1012 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
1013 (void) no_memory(hdl);
1018 * Make sure this property is valid and applies to this type.
1022 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1023 const char *propname = nvpair_name(elem);
1025 prop = zfs_name_to_prop(propname);
1026 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
1028 * This is a user property: make sure it's a
1029 * string, and that it's less than ZAP_MAXNAMELEN.
1031 if (nvpair_type(elem) != DATA_TYPE_STRING) {
1032 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1033 "'%s' must be a string"), propname);
1034 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1038 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
1039 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1040 "property name '%s' is too long"),
1042 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1046 (void) nvpair_value_string(elem, &strval);
1047 if (nvlist_add_string(ret, propname, strval) != 0) {
1048 (void) no_memory(hdl);
1055 * Currently, only user properties can be modified on
1058 if (type == ZFS_TYPE_SNAPSHOT) {
1059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1060 "this property can not be modified for snapshots"));
1061 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1065 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1066 zfs_userquota_prop_t uqtype;
1067 char *newpropname = NULL;
1073 if (userquota_propname_decode(propname, zoned,
1074 &uqtype, domain, sizeof (domain), &rid) != 0) {
1076 dgettext(TEXT_DOMAIN,
1077 "'%s' has an invalid user/group name"),
1079 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1083 if (uqtype != ZFS_PROP_USERQUOTA &&
1084 uqtype != ZFS_PROP_GROUPQUOTA &&
1085 uqtype != ZFS_PROP_USEROBJQUOTA &&
1086 uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1087 uqtype != ZFS_PROP_PROJECTQUOTA &&
1088 uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
1090 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1092 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1097 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1098 (void) nvpair_value_string(elem, &strval);
1099 if (strcmp(strval, "none") == 0) {
1101 } else if (zfs_nicestrtonum(hdl,
1102 strval, &intval) != 0) {
1103 (void) zfs_error(hdl,
1104 EZFS_BADPROP, errbuf);
1107 } else if (nvpair_type(elem) ==
1109 (void) nvpair_value_uint64(elem, &intval);
1111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1112 "use 'none' to disable "
1113 "{user|group|project}quota"));
1117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1118 "'%s' must be a number"), propname);
1119 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1124 * Encode the prop name as
1125 * userquota@<hex-rid>-domain, to make it easy
1126 * for the kernel to decode.
1128 rc = asprintf(&newpropname, "%s%llx-%s",
1129 zfs_userquota_prop_prefixes[uqtype],
1130 (longlong_t)rid, domain);
1131 if (rc == -1 || newpropname == NULL) {
1132 (void) no_memory(hdl);
1139 if (nvlist_add_uint64_array(ret, newpropname,
1142 (void) no_memory(hdl);
1147 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 "'%s' is readonly"),
1151 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1155 if (prop == ZPROP_INVAL) {
1156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1157 "invalid property '%s'"), propname);
1158 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1162 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1164 dgettext(TEXT_DOMAIN, "'%s' does not "
1165 "apply to datasets of this type"), propname);
1166 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1170 if (zfs_prop_readonly(prop) &&
1171 !(zfs_prop_setonce(prop) && zhp == NULL) &&
1172 !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1174 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1176 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1180 if (zprop_parse_value(hdl, elem, prop, type, ret,
1181 &strval, &intval, errbuf) != 0)
1185 * Perform some additional checks for specific properties.
1188 case ZFS_PROP_VERSION:
1194 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1195 if (intval < version) {
1196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1197 "Can not downgrade; already at version %u"),
1199 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1205 case ZFS_PROP_VOLBLOCKSIZE:
1206 case ZFS_PROP_RECORDSIZE:
1208 int maxbs = SPA_MAXBLOCKSIZE;
1211 if (zpool_hdl != NULL) {
1212 maxbs = zpool_get_prop_int(zpool_hdl,
1213 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1216 * The value must be a power of two between
1217 * SPA_MINBLOCKSIZE and maxbs.
1219 if (intval < SPA_MINBLOCKSIZE ||
1220 intval > maxbs || !ISP2(intval)) {
1221 zfs_nicebytes(maxbs, buf, sizeof (buf));
1222 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1223 "'%s' must be power of 2 from 512B "
1224 "to %s"), propname, buf);
1225 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1231 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1233 int maxbs = SPA_OLD_MAXBLOCKSIZE;
1236 if (zpool_hdl != NULL) {
1237 char state[64] = "";
1239 maxbs = zpool_get_prop_int(zpool_hdl,
1240 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1243 * Issue a warning but do not fail so that
1244 * tests for settable properties succeed.
1246 if (zpool_prop_get_feature(zpool_hdl,
1247 "feature@allocation_classes", state,
1248 sizeof (state)) != 0 ||
1249 strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1250 (void) fprintf(stderr, gettext(
1251 "%s: property requires a special "
1252 "device in the pool\n"), propname);
1256 (intval < SPA_MINBLOCKSIZE ||
1257 intval > maxbs || !ISP2(intval))) {
1258 zfs_nicebytes(maxbs, buf, sizeof (buf));
1259 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1260 "invalid '%s=%d' property: must be zero or "
1261 "a power of 2 from 512B to %s"), propname,
1263 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1269 case ZFS_PROP_MLSLABEL:
1271 #ifdef HAVE_MLSLABEL
1273 * Verify the mlslabel string and convert to
1274 * internal hex label string.
1278 char *hex = NULL; /* internal label string */
1280 /* Default value is already OK. */
1281 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1284 /* Verify the label can be converted to binary form */
1285 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1286 (str_to_label(strval, &new_sl, MAC_LABEL,
1287 L_NO_CORRECTION, NULL) == -1)) {
1291 /* Now translate to hex internal label string */
1292 if (label_to_str(new_sl, &hex, M_INTERNAL,
1298 m_label_free(new_sl);
1300 /* If string is already in internal form, we're done. */
1301 if (strcmp(strval, hex) == 0) {
1306 /* Replace the label string with the internal form. */
1307 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1309 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1317 "invalid mlslabel '%s'"), strval);
1318 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1319 m_label_free(new_sl); /* OK if null */
1322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1323 "mlslabels are unsupported"));
1324 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1326 #endif /* HAVE_MLSLABEL */
1329 case ZFS_PROP_MOUNTPOINT:
1331 namecheck_err_t why;
1333 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1334 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1337 if (mountpoint_namecheck(strval, &why)) {
1339 case NAME_ERR_LEADING_SLASH:
1341 dgettext(TEXT_DOMAIN,
1342 "'%s' must be an absolute path, "
1343 "'none', or 'legacy'"), propname);
1345 case NAME_ERR_TOOLONG:
1347 dgettext(TEXT_DOMAIN,
1348 "component of '%s' is too long"),
1354 dgettext(TEXT_DOMAIN,
1355 "(%d) not defined"),
1359 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1366 case ZFS_PROP_SHARESMB:
1367 case ZFS_PROP_SHARENFS:
1369 * For the mountpoint and sharenfs or sharesmb
1370 * properties, check if it can be set in a
1371 * global/non-global zone based on
1372 * the zoned property value:
1374 * global zone non-global zone
1375 * --------------------------------------------------
1376 * zoned=on mountpoint (no) mountpoint (yes)
1377 * sharenfs (no) sharenfs (no)
1378 * sharesmb (no) sharesmb (no)
1380 * zoned=off mountpoint (yes) N/A
1385 if (getzoneid() == GLOBAL_ZONEID) {
1386 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1387 "'%s' cannot be set on "
1388 "dataset in a non-global zone"),
1390 (void) zfs_error(hdl, EZFS_ZONED,
1393 } else if (prop == ZFS_PROP_SHARENFS ||
1394 prop == ZFS_PROP_SHARESMB) {
1395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1396 "'%s' cannot be set in "
1397 "a non-global zone"), propname);
1398 (void) zfs_error(hdl, EZFS_ZONED,
1402 } else if (getzoneid() != GLOBAL_ZONEID) {
1404 * If zoned property is 'off', this must be in
1405 * a global zone. If not, something is wrong.
1407 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1408 "'%s' cannot be set while dataset "
1409 "'zoned' property is set"), propname);
1410 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1415 * At this point, it is legitimate to set the
1416 * property. Now we want to make sure that the
1417 * property value is valid if it is sharenfs.
1419 if ((prop == ZFS_PROP_SHARENFS ||
1420 prop == ZFS_PROP_SHARESMB) &&
1421 strcmp(strval, "on") != 0 &&
1422 strcmp(strval, "off") != 0) {
1423 zfs_share_proto_t proto;
1425 if (prop == ZFS_PROP_SHARESMB)
1431 * Must be an valid sharing protocol
1432 * option string so init the libshare
1433 * in order to enable the parser and
1434 * then parse the options. We use the
1435 * control API since we don't care about
1436 * the current configuration and don't
1437 * want the overhead of loading it
1438 * until we actually do something.
1441 if (zfs_init_libshare(hdl,
1442 SA_INIT_CONTROL_API) != SA_OK) {
1444 * An error occurred so we can't do
1447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1448 "'%s' cannot be set: problem "
1449 "in share initialization"),
1451 (void) zfs_error(hdl, EZFS_BADPROP,
1456 if (zfs_parse_options(strval, proto) != SA_OK) {
1458 * There was an error in parsing so
1459 * deal with it by issuing an error
1460 * message and leaving after
1461 * uninitializing the libshare
1464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465 "'%s' cannot be set to invalid "
1466 "options"), propname);
1467 (void) zfs_error(hdl, EZFS_BADPROP,
1469 zfs_uninit_libshare(hdl);
1472 zfs_uninit_libshare(hdl);
1477 case ZFS_PROP_KEYLOCATION:
1478 if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1480 "invalid keylocation"));
1481 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1487 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1489 if (crypt == ZIO_CRYPT_OFF &&
1490 strcmp(strval, "none") != 0) {
1491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1492 "keylocation must be 'none' "
1493 "for unencrypted datasets"));
1494 (void) zfs_error(hdl, EZFS_BADPROP,
1497 } else if (crypt != ZIO_CRYPT_OFF &&
1498 strcmp(strval, "none") == 0) {
1499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1500 "keylocation must not be 'none' "
1501 "for encrypted datasets"));
1502 (void) zfs_error(hdl, EZFS_BADPROP,
1509 case ZFS_PROP_PBKDF2_ITERS:
1510 if (intval < MIN_PBKDF2_ITERATIONS) {
1511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1512 "minimum pbkdf2 iterations is %u"),
1513 MIN_PBKDF2_ITERATIONS);
1514 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1519 case ZFS_PROP_UTF8ONLY:
1520 chosen_utf = (int)intval;
1523 case ZFS_PROP_NORMALIZE:
1524 chosen_normal = (int)intval;
1532 * For changes to existing volumes, we have some additional
1533 * checks to enforce.
1535 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1536 uint64_t blocksize = zfs_prop_get_int(zhp,
1537 ZFS_PROP_VOLBLOCKSIZE);
1541 case ZFS_PROP_VOLSIZE:
1542 if (intval % blocksize != 0) {
1543 zfs_nicebytes(blocksize, buf,
1545 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1546 "'%s' must be a multiple of "
1547 "volume block size (%s)"),
1549 (void) zfs_error(hdl, EZFS_BADPROP,
1555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1556 "'%s' cannot be zero"),
1558 (void) zfs_error(hdl, EZFS_BADPROP,
1569 /* check encryption properties */
1571 int64_t crypt = zfs_prop_get_int(zhp,
1572 ZFS_PROP_ENCRYPTION);
1575 case ZFS_PROP_COPIES:
1576 if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1578 "encrypted datasets cannot have "
1580 (void) zfs_error(hdl, EZFS_BADPROP,
1592 * If normalization was chosen, but no UTF8 choice was made,
1593 * enforce rejection of non-UTF8 names.
1595 * If normalization was chosen, but rejecting non-UTF8 names
1596 * was explicitly not chosen, it is an error.
1598 if (chosen_normal > 0 && chosen_utf < 0) {
1599 if (nvlist_add_uint64(ret,
1600 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1601 (void) no_memory(hdl);
1604 } else if (chosen_normal > 0 && chosen_utf == 0) {
1605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1606 "'%s' must be set 'on' if normalization chosen"),
1607 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1608 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1619 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1621 uint64_t old_volsize;
1622 uint64_t new_volsize;
1623 uint64_t old_reservation;
1624 uint64_t new_reservation;
1625 zfs_prop_t resv_prop;
1627 zpool_handle_t *zph = zpool_handle(zhp);
1630 * If this is an existing volume, and someone is setting the volsize,
1631 * make sure that it matches the reservation, or add it if necessary.
1633 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1634 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1636 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1638 props = fnvlist_alloc();
1639 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1640 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1642 if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1643 old_reservation) || nvlist_exists(nvl,
1644 zfs_prop_to_name(resv_prop))) {
1645 fnvlist_free(props);
1648 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1649 &new_volsize) != 0) {
1650 fnvlist_free(props);
1653 new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1654 fnvlist_free(props);
1656 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1657 new_reservation) != 0) {
1658 (void) no_memory(zhp->zfs_hdl);
1665 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1666 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1667 * Return codes must match zfs_add_synthetic_resv().
1670 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1677 if (!ZFS_IS_VOLUME(zhp)) {
1681 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1685 if (prop != ZFS_PROP_REFRESERVATION) {
1689 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1690 /* No value being set, so it can't be "auto" */
1693 if (resvsize != UINT64_MAX) {
1694 /* Being set to a value other than "auto" */
1698 props = fnvlist_alloc();
1700 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1701 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1703 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1705 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1708 resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1710 fnvlist_free(props);
1712 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1713 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1714 (void) no_memory(zhp->zfs_hdl);
1721 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1728 * For quotas and reservations, ENOSPC indicates
1729 * something different; setting a quota or reservation
1730 * doesn't use any disk space.
1733 case ZFS_PROP_QUOTA:
1734 case ZFS_PROP_REFQUOTA:
1735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1736 "size is less than current used or "
1738 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1741 case ZFS_PROP_RESERVATION:
1742 case ZFS_PROP_REFRESERVATION:
1743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1744 "size is greater than available space"));
1745 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1749 (void) zfs_standard_error(hdl, err, errbuf);
1755 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1759 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1763 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1764 "property value too long"));
1765 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1770 "pool and or dataset must be upgraded to set this "
1771 "property or value"));
1772 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1776 if (prop == ZFS_PROP_COMPRESSION ||
1777 prop == ZFS_PROP_DNODESIZE ||
1778 prop == ZFS_PROP_RECORDSIZE) {
1779 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1780 "property setting is not allowed on "
1781 "bootable datasets"));
1782 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1783 } else if (prop == ZFS_PROP_CHECKSUM ||
1784 prop == ZFS_PROP_DEDUP) {
1785 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1786 "property setting is not allowed on "
1788 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1790 (void) zfs_standard_error(hdl, err, errbuf);
1795 if (prop == ZPROP_INVAL) {
1796 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1798 (void) zfs_standard_error(hdl, err, errbuf);
1803 if (prop == ZFS_PROP_KEYLOCATION) {
1804 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1805 "keylocation may only be set on encryption roots"));
1806 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1808 (void) zfs_standard_error(hdl, err, errbuf);
1814 * This platform can't address a volume this big.
1817 if (prop == ZFS_PROP_VOLSIZE) {
1818 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1824 (void) zfs_standard_error(hdl, err, errbuf);
1829 zfs_is_namespace_prop(zfs_prop_t prop)
1833 case ZFS_PROP_ATIME:
1834 case ZFS_PROP_RELATIME:
1835 case ZFS_PROP_DEVICES:
1837 case ZFS_PROP_SETUID:
1838 case ZFS_PROP_READONLY:
1839 case ZFS_PROP_XATTR:
1840 case ZFS_PROP_NBMAND:
1849 * Given a property name and value, set the property for the given dataset.
1852 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1856 libzfs_handle_t *hdl = zhp->zfs_hdl;
1857 nvlist_t *nvl = NULL;
1859 (void) snprintf(errbuf, sizeof (errbuf),
1860 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1863 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1864 nvlist_add_string(nvl, propname, propval) != 0) {
1865 (void) no_memory(hdl);
1869 ret = zfs_prop_set_list(zhp, nvl);
1879 * Given an nvlist of property names and values, set the properties for the
1883 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1885 zfs_cmd_t zc = {"\0"};
1887 prop_changelist_t **cls = NULL;
1890 libzfs_handle_t *hdl = zhp->zfs_hdl;
1894 zfs_prop_t prop = 0;
1897 (void) snprintf(errbuf, sizeof (errbuf),
1898 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1901 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1902 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1903 B_FALSE, errbuf)) == NULL)
1907 * We have to check for any extra properties which need to be added
1908 * before computing the length of the nvlist.
1910 for (elem = nvlist_next_nvpair(nvl, NULL);
1912 elem = nvlist_next_nvpair(nvl, elem)) {
1913 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1914 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1919 if (added_resv != 1 &&
1920 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1925 * Check how many properties we're setting and allocate an array to
1926 * store changelist pointers for postfix().
1928 for (elem = nvlist_next_nvpair(nvl, NULL);
1930 elem = nvlist_next_nvpair(nvl, elem))
1932 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1936 for (elem = nvlist_next_nvpair(nvl, NULL);
1938 elem = nvlist_next_nvpair(nvl, elem)) {
1940 prop = zfs_name_to_prop(nvpair_name(elem));
1942 assert(cl_idx < nvl_len);
1944 * We don't want to unmount & remount the dataset when changing
1945 * its canmount property to 'on' or 'noauto'. We only use
1946 * the changelist logic to unmount when setting canmount=off.
1948 if (prop != ZFS_PROP_CANMOUNT ||
1949 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1950 zfs_is_mounted(zhp, NULL))) {
1951 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1952 if (cls[cl_idx] == NULL)
1956 if (prop == ZFS_PROP_MOUNTPOINT &&
1957 changelist_haszonedchild(cls[cl_idx])) {
1958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1959 "child dataset with inherited mountpoint is used "
1960 "in a non-global zone"));
1961 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1965 if (cls[cl_idx] != NULL &&
1966 (ret = changelist_prefix(cls[cl_idx])) != 0)
1971 assert(cl_idx == nvl_len);
1974 * Execute the corresponding ioctl() to set this list of properties.
1976 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1978 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1979 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1982 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1985 if (zc.zc_nvlist_dst_filled == B_FALSE) {
1986 (void) zfs_standard_error(hdl, errno, errbuf);
1990 /* Get the list of unset properties back and report them. */
1991 nvlist_t *errorprops = NULL;
1992 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1994 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1996 elem = nvlist_next_nvpair(errorprops, elem)) {
1997 prop = zfs_name_to_prop(nvpair_name(elem));
1998 zfs_setprop_error(hdl, prop, errno, errbuf);
2000 nvlist_free(errorprops);
2002 if (added_resv && errno == ENOSPC) {
2003 /* clean up the volsize property we tried to set */
2004 uint64_t old_volsize = zfs_prop_get_int(zhp,
2008 zcmd_free_nvlists(&zc);
2010 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
2012 if (nvlist_add_uint64(nvl,
2013 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
2016 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
2018 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
2021 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
2022 if (cls[cl_idx] != NULL) {
2023 int clp_err = changelist_postfix(cls[cl_idx]);
2031 * Refresh the statistics so the new property
2032 * value is reflected.
2034 (void) get_stats(zhp);
2037 * Remount the filesystem to propagate the change
2038 * if one of the options handled by the generic
2039 * Linux namespace layer has been modified.
2041 if (zfs_is_namespace_prop(prop) &&
2042 zfs_is_mounted(zhp, NULL))
2043 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
2049 zcmd_free_nvlists(&zc);
2051 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
2052 if (cls[cl_idx] != NULL)
2053 changelist_free(cls[cl_idx]);
2061 * Given a property, inherit the value from the parent dataset, or if received
2062 * is TRUE, revert to the received value, if any.
2065 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
2067 zfs_cmd_t zc = {"\0"};
2069 prop_changelist_t *cl;
2070 libzfs_handle_t *hdl = zhp->zfs_hdl;
2074 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2075 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2077 zc.zc_cookie = received;
2078 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
2080 * For user properties, the amount of work we have to do is very
2081 * small, so just do it here.
2083 if (!zfs_prop_user(propname)) {
2084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2085 "invalid property"));
2086 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2089 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2090 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2092 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
2093 return (zfs_standard_error(hdl, errno, errbuf));
2099 * Verify that this property is inheritable.
2101 if (zfs_prop_readonly(prop))
2102 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
2104 if (!zfs_prop_inheritable(prop) && !received)
2105 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2108 * Check to see if the value applies to this type
2110 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2111 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2114 * Normalize the name, to get rid of shorthand abbreviations.
2116 propname = zfs_prop_to_name(prop);
2117 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2118 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2120 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2121 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
2122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2123 "dataset is used in a non-global zone"));
2124 return (zfs_error(hdl, EZFS_ZONED, errbuf));
2128 * Determine datasets which will be affected by this change, if any.
2130 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2133 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
2134 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2135 "child dataset with inherited mountpoint is used "
2136 "in a non-global zone"));
2137 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2141 if ((ret = changelist_prefix(cl)) != 0)
2144 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
2145 return (zfs_standard_error(hdl, errno, errbuf));
2148 if ((ret = changelist_postfix(cl)) != 0)
2152 * Refresh the statistics so the new property is reflected.
2154 (void) get_stats(zhp);
2157 * Remount the filesystem to propagate the change
2158 * if one of the options handled by the generic
2159 * Linux namespace layer has been modified.
2161 if (zfs_is_namespace_prop(prop) &&
2162 zfs_is_mounted(zhp, NULL))
2163 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
2167 changelist_free(cl);
2172 * True DSL properties are stored in an nvlist. The following two functions
2173 * extract them appropriately.
2176 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2182 if (nvlist_lookup_nvlist(zhp->zfs_props,
2183 zfs_prop_to_name(prop), &nv) == 0) {
2184 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2185 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2187 verify(!zhp->zfs_props_table ||
2188 zhp->zfs_props_table[prop] == B_TRUE);
2189 value = zfs_prop_default_numeric(prop);
2197 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2203 if (nvlist_lookup_nvlist(zhp->zfs_props,
2204 zfs_prop_to_name(prop), &nv) == 0) {
2205 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2206 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2208 verify(!zhp->zfs_props_table ||
2209 zhp->zfs_props_table[prop] == B_TRUE);
2210 value = zfs_prop_default_string(prop);
2218 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2220 return (zhp->zfs_props == zhp->zfs_recvd_props);
2224 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2226 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2227 zhp->zfs_props = zhp->zfs_recvd_props;
2231 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2233 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2238 * Internal function for getting a numeric property. Both zfs_prop_get() and
2239 * zfs_prop_get_int() are built using this interface.
2241 * Certain properties can be overridden using 'mount -o'. In this case, scan
2242 * the contents of the /proc/self/mounts entry, searching for the
2243 * appropriate options. If they differ from the on-disk values, report the
2244 * current values and mark the source "temporary".
2247 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2248 char **source, uint64_t *val)
2250 zfs_cmd_t zc = {"\0"};
2251 nvlist_t *zplprops = NULL;
2253 char *mntopt_on = NULL;
2254 char *mntopt_off = NULL;
2255 boolean_t received = zfs_is_recvd_props_mode(zhp);
2260 * If the property is being fetched for a snapshot, check whether
2261 * the property is valid for the snapshot's head dataset type.
2263 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
2264 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
2265 *val = zfs_prop_default_numeric(prop);
2270 case ZFS_PROP_ATIME:
2271 mntopt_on = MNTOPT_ATIME;
2272 mntopt_off = MNTOPT_NOATIME;
2275 case ZFS_PROP_RELATIME:
2276 mntopt_on = MNTOPT_RELATIME;
2277 mntopt_off = MNTOPT_NORELATIME;
2280 case ZFS_PROP_DEVICES:
2281 mntopt_on = MNTOPT_DEVICES;
2282 mntopt_off = MNTOPT_NODEVICES;
2286 mntopt_on = MNTOPT_EXEC;
2287 mntopt_off = MNTOPT_NOEXEC;
2290 case ZFS_PROP_READONLY:
2291 mntopt_on = MNTOPT_RO;
2292 mntopt_off = MNTOPT_RW;
2295 case ZFS_PROP_SETUID:
2296 mntopt_on = MNTOPT_SETUID;
2297 mntopt_off = MNTOPT_NOSETUID;
2300 case ZFS_PROP_XATTR:
2301 mntopt_on = MNTOPT_XATTR;
2302 mntopt_off = MNTOPT_NOXATTR;
2305 case ZFS_PROP_NBMAND:
2306 mntopt_on = MNTOPT_NBMAND;
2307 mntopt_off = MNTOPT_NONBMAND;
2315 * Because looking up the mount options is potentially expensive
2316 * (iterating over all of /proc/self/mounts), we defer its
2317 * calculation until we're looking up a property which requires
2320 if (!zhp->zfs_mntcheck &&
2321 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2322 libzfs_handle_t *hdl = zhp->zfs_hdl;
2323 struct mnttab entry;
2325 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2326 zhp->zfs_mntopts = zfs_strdup(hdl,
2328 if (zhp->zfs_mntopts == NULL)
2332 zhp->zfs_mntcheck = B_TRUE;
2335 if (zhp->zfs_mntopts == NULL)
2336 mnt.mnt_mntopts = "";
2338 mnt.mnt_mntopts = zhp->zfs_mntopts;
2341 case ZFS_PROP_ATIME:
2342 case ZFS_PROP_RELATIME:
2343 case ZFS_PROP_DEVICES:
2345 case ZFS_PROP_READONLY:
2346 case ZFS_PROP_SETUID:
2347 case ZFS_PROP_XATTR:
2348 case ZFS_PROP_NBMAND:
2349 *val = getprop_uint64(zhp, prop, source);
2354 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2357 *src = ZPROP_SRC_TEMPORARY;
2358 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2361 *src = ZPROP_SRC_TEMPORARY;
2365 case ZFS_PROP_CANMOUNT:
2366 case ZFS_PROP_VOLSIZE:
2367 case ZFS_PROP_QUOTA:
2368 case ZFS_PROP_REFQUOTA:
2369 case ZFS_PROP_RESERVATION:
2370 case ZFS_PROP_REFRESERVATION:
2371 case ZFS_PROP_FILESYSTEM_LIMIT:
2372 case ZFS_PROP_SNAPSHOT_LIMIT:
2373 case ZFS_PROP_FILESYSTEM_COUNT:
2374 case ZFS_PROP_SNAPSHOT_COUNT:
2375 *val = getprop_uint64(zhp, prop, source);
2377 if (*source == NULL) {
2378 /* not default, must be local */
2379 *source = zhp->zfs_name;
2383 case ZFS_PROP_MOUNTED:
2384 *val = (zhp->zfs_mntopts != NULL);
2387 case ZFS_PROP_NUMCLONES:
2388 *val = zhp->zfs_dmustats.dds_num_clones;
2391 case ZFS_PROP_VERSION:
2392 case ZFS_PROP_NORMALIZE:
2393 case ZFS_PROP_UTF8ONLY:
2395 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2397 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2398 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2399 zcmd_free_nvlists(&zc);
2400 if (prop == ZFS_PROP_VERSION &&
2401 zhp->zfs_type == ZFS_TYPE_VOLUME)
2402 *val = zfs_prop_default_numeric(prop);
2405 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2406 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2408 zcmd_free_nvlists(&zc);
2411 nvlist_free(zplprops);
2412 zcmd_free_nvlists(&zc);
2415 case ZFS_PROP_INCONSISTENT:
2416 *val = zhp->zfs_dmustats.dds_inconsistent;
2419 case ZFS_PROP_REDACTED:
2420 *val = zhp->zfs_dmustats.dds_redacted;
2424 switch (zfs_prop_get_type(prop)) {
2425 case PROP_TYPE_NUMBER:
2426 case PROP_TYPE_INDEX:
2427 *val = getprop_uint64(zhp, prop, source);
2429 * If we tried to use a default value for a
2430 * readonly property, it means that it was not
2431 * present. Note this only applies to "truly"
2432 * readonly properties, not set-once properties
2433 * like volblocksize.
2435 if (zfs_prop_readonly(prop) &&
2436 !zfs_prop_setonce(prop) &&
2437 *source != NULL && (*source)[0] == '\0') {
2443 case PROP_TYPE_STRING:
2445 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2446 "cannot get non-numeric property"));
2447 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2448 dgettext(TEXT_DOMAIN, "internal error")));
2456 * Calculate the source type, given the raw source string.
2459 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2460 char *statbuf, size_t statlen)
2462 if (statbuf == NULL ||
2463 srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) {
2467 if (source == NULL) {
2468 *srctype = ZPROP_SRC_NONE;
2469 } else if (source[0] == '\0') {
2470 *srctype = ZPROP_SRC_DEFAULT;
2471 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2472 *srctype = ZPROP_SRC_RECEIVED;
2474 if (strcmp(source, zhp->zfs_name) == 0) {
2475 *srctype = ZPROP_SRC_LOCAL;
2477 (void) strlcpy(statbuf, source, statlen);
2478 *srctype = ZPROP_SRC_INHERITED;
2485 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2486 size_t proplen, boolean_t literal)
2491 if (zhp->zfs_recvd_props == NULL)
2492 if (get_recvd_props_ioctl(zhp) != 0)
2495 prop = zfs_name_to_prop(propname);
2497 if (prop != ZPROP_INVAL) {
2499 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2501 zfs_set_recvd_props_mode(zhp, &cookie);
2502 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2503 NULL, NULL, 0, literal);
2504 zfs_unset_recvd_props_mode(zhp, &cookie);
2508 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2509 propname, &propval) != 0)
2511 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2513 (void) strlcpy(propbuf, recvdval, proplen);
2516 return (err == 0 ? 0 : -1);
2520 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2525 value = zfs_get_clones_nvl(zhp);
2530 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2531 pair = nvlist_next_nvpair(value, pair)) {
2532 if (propbuf[0] != '\0')
2533 (void) strlcat(propbuf, ",", proplen);
2534 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2540 struct get_clones_arg {
2544 char buf[ZFS_MAX_DATASET_NAME_LEN];
2548 get_clones_cb(zfs_handle_t *zhp, void *arg)
2550 struct get_clones_arg *gca = arg;
2552 if (gca->numclones == 0) {
2557 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2558 NULL, NULL, 0, B_TRUE) != 0)
2560 if (strcmp(gca->buf, gca->origin) == 0) {
2561 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2566 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2572 zfs_get_clones_nvl(zfs_handle_t *zhp)
2574 nvlist_t *nv, *value;
2576 if (nvlist_lookup_nvlist(zhp->zfs_props,
2577 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2578 struct get_clones_arg gca;
2581 * if this is a snapshot, then the kernel wasn't able
2582 * to get the clones. Do it by slowly iterating.
2584 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2586 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2588 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2593 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2595 gca.origin = zhp->zfs_name;
2597 if (gca.numclones != 0) {
2599 char pool[ZFS_MAX_DATASET_NAME_LEN];
2602 /* get the pool name */
2603 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2604 (void) strsep(&cp, "/@");
2605 root = zfs_open(zhp->zfs_hdl, pool,
2606 ZFS_TYPE_FILESYSTEM);
2613 (void) get_clones_cb(root, &gca);
2616 if (gca.numclones != 0 ||
2617 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2618 nvlist_add_nvlist(zhp->zfs_props,
2619 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2626 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2627 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2630 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2636 get_rsnaps_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2642 if (nvlist_lookup_nvlist(zhp->zfs_props,
2643 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &value) != 0)
2645 if (nvlist_lookup_uint64_array(value, ZPROP_VALUE, &snaps,
2649 /* There's no redaction snapshots; pass a special value back */
2650 (void) snprintf(propbuf, proplen, "none");
2654 for (int i = 0; i < nsnaps; i++) {
2656 if (propbuf[0] != '\0')
2657 (void) strlcat(propbuf, ",", proplen);
2658 (void) snprintf(buf, sizeof (buf), "%llu",
2659 (u_longlong_t)snaps[i]);
2660 (void) strlcat(propbuf, buf, proplen);
2667 * Accepts a property and value and checks that the value
2668 * matches the one found by the channel program. If they are
2669 * not equal, print both of them.
2672 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2675 if (!zhp->zfs_hdl->libzfs_prop_debug)
2678 char *poolname = zhp->zpool_hdl->zpool_name;
2679 const char *prop_name = zfs_prop_to_name(prop);
2680 const char *program =
2682 "ds = args['dataset']\n"
2683 "prop = args['property']\n"
2684 "value, setpoint = zfs.get_prop(ds, prop)\n"
2685 "return {value=value, setpoint=setpoint}\n";
2688 nvlist_t *argnvl = fnvlist_alloc();
2690 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2691 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2693 error = lzc_channel_program_nosync(poolname, program,
2694 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2697 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2698 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2700 error = nvlist_lookup_int64(retnvl, "value", &ans);
2702 (void) fprintf(stderr, "%s: zcp check error: "
2703 "%u\n", prop_name, error);
2706 if (ans != intval) {
2707 (void) fprintf(stderr, "%s: zfs found %llu, "
2708 "but zcp found %llu\n", prop_name,
2709 (u_longlong_t)intval, (u_longlong_t)ans);
2713 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2715 (void) fprintf(stderr, "%s: zcp check error: "
2716 "%u\n", prop_name, error);
2719 if (strcmp(strval, str_ans) != 0) {
2720 (void) fprintf(stderr,
2721 "%s: zfs found '%s', but zcp found '%s'\n",
2722 prop_name, strval, str_ans);
2726 (void) fprintf(stderr, "%s: zcp check failed, channel program "
2727 "error: %u\n", prop_name, error);
2729 nvlist_free(argnvl);
2730 nvlist_free(outnvl);
2734 * Retrieve a property from the given object. If 'literal' is specified, then
2735 * numbers are left as exact values. Otherwise, numbers are converted to a
2736 * human-readable form.
2738 * Returns 0 on success, or -1 on error.
2741 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2742 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2744 char *source = NULL;
2748 boolean_t received = zfs_is_recvd_props_mode(zhp);
2751 * Check to see if this property applies to our object
2753 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2756 if (received && zfs_prop_readonly(prop))
2760 *src = ZPROP_SRC_NONE;
2763 case ZFS_PROP_CREATION:
2765 * 'creation' is a time_t stored in the statistics. We convert
2766 * this into a string unless 'literal' is specified.
2769 val = getprop_uint64(zhp, prop, &source);
2770 time_t time = (time_t)val;
2774 localtime_r(&time, &t) == NULL ||
2775 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2777 (void) snprintf(propbuf, proplen, "%llu",
2780 zcp_check(zhp, prop, val, NULL);
2783 case ZFS_PROP_MOUNTPOINT:
2785 * Getting the precise mountpoint can be tricky.
2787 * - for 'none' or 'legacy', return those values.
2788 * - for inherited mountpoints, we want to take everything
2789 * after our ancestor and append it to the inherited value.
2791 * If the pool has an alternate root, we want to prepend that
2792 * root to any values we return.
2795 str = getprop_string(zhp, prop, &source);
2797 if (str[0] == '/') {
2798 char buf[MAXPATHLEN];
2800 const char *relpath;
2803 * If we inherit the mountpoint, even from a dataset
2804 * with a received value, the source will be the path of
2805 * the dataset we inherit from. If source is
2806 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2809 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2812 relpath = zhp->zfs_name + strlen(source);
2813 if (relpath[0] == '/')
2817 if ((zpool_get_prop(zhp->zpool_hdl,
2818 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2819 B_FALSE)) || (strcmp(root, "-") == 0))
2822 * Special case an alternate root of '/'. This will
2823 * avoid having multiple leading slashes in the
2826 if (strcmp(root, "/") == 0)
2830 * If the mountpoint is '/' then skip over this
2831 * if we are obtaining either an alternate root or
2832 * an inherited mountpoint.
2834 if (str[1] == '\0' && (root[0] != '\0' ||
2835 relpath[0] != '\0'))
2838 if (relpath[0] == '\0')
2839 (void) snprintf(propbuf, proplen, "%s%s",
2842 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2843 root, str, relpath[0] == '@' ? "" : "/",
2846 /* 'legacy' or 'none' */
2847 (void) strlcpy(propbuf, str, proplen);
2849 zcp_check(zhp, prop, 0, propbuf);
2852 case ZFS_PROP_ORIGIN:
2853 str = getprop_string(zhp, prop, &source);
2856 (void) strlcpy(propbuf, str, proplen);
2857 zcp_check(zhp, prop, 0, str);
2860 case ZFS_PROP_REDACT_SNAPS:
2861 if (get_rsnaps_string(zhp, propbuf, proplen) != 0)
2865 case ZFS_PROP_CLONES:
2866 if (get_clones_string(zhp, propbuf, proplen) != 0)
2870 case ZFS_PROP_QUOTA:
2871 case ZFS_PROP_REFQUOTA:
2872 case ZFS_PROP_RESERVATION:
2873 case ZFS_PROP_REFRESERVATION:
2875 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2878 * If quota or reservation is 0, we translate this into 'none'
2879 * (unless literal is set), and indicate that it's the default
2880 * value. Otherwise, we print the number nicely and indicate
2881 * that its set locally.
2885 (void) strlcpy(propbuf, "0", proplen);
2887 (void) strlcpy(propbuf, "none", proplen);
2890 (void) snprintf(propbuf, proplen, "%llu",
2893 zfs_nicebytes(val, propbuf, proplen);
2895 zcp_check(zhp, prop, val, NULL);
2898 case ZFS_PROP_FILESYSTEM_LIMIT:
2899 case ZFS_PROP_SNAPSHOT_LIMIT:
2900 case ZFS_PROP_FILESYSTEM_COUNT:
2901 case ZFS_PROP_SNAPSHOT_COUNT:
2903 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2907 * If limit is UINT64_MAX, we translate this into 'none' (unless
2908 * literal is set), and indicate that it's the default value.
2909 * Otherwise, we print the number nicely and indicate that it's
2913 (void) snprintf(propbuf, proplen, "%llu",
2915 } else if (val == UINT64_MAX) {
2916 (void) strlcpy(propbuf, "none", proplen);
2918 zfs_nicenum(val, propbuf, proplen);
2921 zcp_check(zhp, prop, val, NULL);
2924 case ZFS_PROP_REFRATIO:
2925 case ZFS_PROP_COMPRESSRATIO:
2926 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2929 (void) snprintf(propbuf, proplen, "%llu.%02llu",
2930 (u_longlong_t)(val / 100),
2931 (u_longlong_t)(val % 100));
2933 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2934 (u_longlong_t)(val / 100),
2935 (u_longlong_t)(val % 100));
2936 zcp_check(zhp, prop, val, NULL);
2940 switch (zhp->zfs_type) {
2941 case ZFS_TYPE_FILESYSTEM:
2944 case ZFS_TYPE_VOLUME:
2947 case ZFS_TYPE_SNAPSHOT:
2950 case ZFS_TYPE_BOOKMARK:
2956 (void) snprintf(propbuf, proplen, "%s", str);
2957 zcp_check(zhp, prop, 0, propbuf);
2960 case ZFS_PROP_MOUNTED:
2962 * The 'mounted' property is a pseudo-property that described
2963 * whether the filesystem is currently mounted. Even though
2964 * it's a boolean value, the typical values of "on" and "off"
2965 * don't make sense, so we translate to "yes" and "no".
2967 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2968 src, &source, &val) != 0)
2971 (void) strlcpy(propbuf, "yes", proplen);
2973 (void) strlcpy(propbuf, "no", proplen);
2978 * The 'name' property is a pseudo-property derived from the
2979 * dataset name. It is presented as a real property to simplify
2982 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2983 zcp_check(zhp, prop, 0, propbuf);
2986 case ZFS_PROP_MLSLABEL:
2988 #ifdef HAVE_MLSLABEL
2989 m_label_t *new_sl = NULL;
2990 char *ascii = NULL; /* human readable label */
2992 (void) strlcpy(propbuf,
2993 getprop_string(zhp, prop, &source), proplen);
2995 if (literal || (strcasecmp(propbuf,
2996 ZFS_MLSLABEL_DEFAULT) == 0))
3000 * Try to translate the internal hex string to
3001 * human-readable output. If there are any
3002 * problems just use the hex string.
3005 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
3006 L_NO_CORRECTION, NULL) == -1) {
3007 m_label_free(new_sl);
3011 if (label_to_str(new_sl, &ascii, M_LABEL,
3015 m_label_free(new_sl);
3018 m_label_free(new_sl);
3020 (void) strlcpy(propbuf, ascii, proplen);
3023 (void) strlcpy(propbuf,
3024 getprop_string(zhp, prop, &source), proplen);
3025 #endif /* HAVE_MLSLABEL */
3030 case ZFS_PROP_CREATETXG:
3031 case ZFS_PROP_OBJSETID:
3033 * These properties are stored as numbers, but they are
3035 * We don't want them to be pretty printed, because pretty
3036 * printing mangles the ID into a truncated and useless value.
3038 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
3040 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
3041 zcp_check(zhp, prop, val, NULL);
3044 case ZFS_PROP_REFERENCED:
3045 case ZFS_PROP_AVAILABLE:
3047 case ZFS_PROP_USEDSNAP:
3048 case ZFS_PROP_USEDDS:
3049 case ZFS_PROP_USEDREFRESERV:
3050 case ZFS_PROP_USEDCHILD:
3051 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
3054 (void) snprintf(propbuf, proplen, "%llu",
3057 zfs_nicebytes(val, propbuf, proplen);
3059 zcp_check(zhp, prop, val, NULL);
3063 switch (zfs_prop_get_type(prop)) {
3064 case PROP_TYPE_NUMBER:
3065 if (get_numeric_property(zhp, prop, src,
3066 &source, &val) != 0) {
3071 (void) snprintf(propbuf, proplen, "%llu",
3074 zfs_nicenum(val, propbuf, proplen);
3076 zcp_check(zhp, prop, val, NULL);
3079 case PROP_TYPE_STRING:
3080 str = getprop_string(zhp, prop, &source);
3084 (void) strlcpy(propbuf, str, proplen);
3085 zcp_check(zhp, prop, 0, str);
3088 case PROP_TYPE_INDEX:
3089 if (get_numeric_property(zhp, prop, src,
3090 &source, &val) != 0)
3092 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
3095 (void) strlcpy(propbuf, strval, proplen);
3096 zcp_check(zhp, prop, 0, strval);
3104 get_source(zhp, src, source, statbuf, statlen);
3110 * Utility function to get the given numeric property. Does no validation that
3111 * the given property is the appropriate type; should only be used with
3112 * hard-coded property types.
3115 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
3120 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
3126 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
3130 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
3131 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
3135 * Similar to zfs_prop_get(), but returns the value as an integer.
3138 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
3139 zprop_source_t *src, char *statbuf, size_t statlen)
3144 * Check to see if this property applies to our object
3146 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
3147 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
3148 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
3149 zfs_prop_to_name(prop)));
3153 *src = ZPROP_SRC_NONE;
3155 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
3158 get_source(zhp, src, source, statbuf, statlen);
3165 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
3166 char **domainp, idmap_rid_t *ridp)
3168 idmap_get_handle_t *get_hdl = NULL;
3172 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
3176 err = idmap_get_sidbyuid(get_hdl, id,
3177 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3179 err = idmap_get_sidbygid(get_hdl, id,
3180 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3182 if (err == IDMAP_SUCCESS &&
3183 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
3184 status == IDMAP_SUCCESS)
3190 idmap_get_destroy(get_hdl);
3193 #endif /* HAVE_IDMAP */
3196 * convert the propname into parameters needed by kernel
3197 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3198 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3199 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3200 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3201 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3202 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3205 userquota_propname_decode(const char *propname, boolean_t zoned,
3206 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
3208 zfs_userquota_prop_t type;
3212 boolean_t isproject;
3218 /* Figure out the property type ({user|group|project}{quota|space}) */
3219 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
3220 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
3221 strlen(zfs_userquota_prop_prefixes[type])) == 0)
3224 if (type == ZFS_NUM_USERQUOTA_PROPS)
3228 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3229 type == ZFS_PROP_USEROBJQUOTA ||
3230 type == ZFS_PROP_USEROBJUSED);
3231 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3232 type == ZFS_PROP_GROUPOBJQUOTA ||
3233 type == ZFS_PROP_GROUPOBJUSED);
3234 isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3235 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3236 type == ZFS_PROP_PROJECTOBJUSED);
3238 cp = strchr(propname, '@') + 1;
3240 if (isuser && (pw = getpwnam(cp)) != NULL) {
3241 if (zoned && getzoneid() == GLOBAL_ZONEID)
3244 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3245 if (zoned && getzoneid() == GLOBAL_ZONEID)
3248 } else if (!isproject && strchr(cp, '@')) {
3251 * It's a SID name (eg "user@domain") that needs to be
3252 * turned into S-1-domainID-RID.
3254 directory_error_t e;
3255 char *numericsid = NULL;
3258 if (zoned && getzoneid() == GLOBAL_ZONEID)
3261 e = directory_sid_from_user_name(NULL,
3264 e = directory_sid_from_group_name(NULL,
3268 directory_error_free(e);
3271 if (numericsid == NULL)
3274 (void) strlcpy(domain, cp, domainlen);
3275 cp = strrchr(domain, '-');
3280 *ridp = strtoull(cp, &end, 10);
3283 if (errno != 0 || *end != '\0')
3287 #endif /* HAVE_IDMAP */
3289 /* It's a user/group/project ID (eg "12345"). */
3292 id = strtoul(cp, &end, 10);
3295 if (id > MAXUID && !isproject) {
3297 /* It's an ephemeral ID. */
3301 if (idmap_id_to_numeric_domain_rid(id, isuser,
3302 &mapdomain, &rid) != 0)
3304 (void) strlcpy(domain, mapdomain, domainlen);
3308 #endif /* HAVE_IDMAP */
3318 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3319 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3322 zfs_cmd_t zc = {"\0"};
3324 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3326 err = userquota_propname_decode(propname,
3327 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3328 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3329 zc.zc_objset_type = *typep;
3333 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_USERSPACE_ONE, &zc);
3337 *propvalue = zc.zc_cookie;
3342 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3343 uint64_t *propvalue)
3345 zfs_userquota_prop_t type;
3347 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3352 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3353 char *propbuf, int proplen, boolean_t literal)
3357 zfs_userquota_prop_t type;
3359 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3366 (void) snprintf(propbuf, proplen, "%llu",
3367 (u_longlong_t)propvalue);
3368 } else if (propvalue == 0 &&
3369 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3370 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3371 type == ZFS_PROP_PROJECTQUOTA ||
3372 type == ZFS_PROP_PROJECTOBJQUOTA)) {
3373 (void) strlcpy(propbuf, "none", proplen);
3374 } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3375 type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3376 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3377 zfs_nicebytes(propvalue, propbuf, proplen);
3379 zfs_nicenum(propvalue, propbuf, proplen);
3385 * propname must start with "written@" or "written#".
3388 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3389 uint64_t *propvalue)
3392 zfs_cmd_t zc = {"\0"};
3393 const char *snapname;
3395 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3397 assert(zfs_prop_written(propname));
3398 snapname = propname + strlen("written@");
3399 if (strchr(snapname, '@') != NULL || strchr(snapname, '#') != NULL) {
3400 /* full snapshot or bookmark name specified */
3401 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3403 /* snapname is the short name, append it to zhp's fsname */
3406 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3407 sizeof (zc.zc_value));
3408 cp = strchr(zc.zc_value, '@');
3411 (void) strlcat(zc.zc_value, snapname - 1, sizeof (zc.zc_value));
3414 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SPACE_WRITTEN, &zc);
3418 *propvalue = zc.zc_cookie;
3423 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3424 char *propbuf, int proplen, boolean_t literal)
3429 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3435 (void) snprintf(propbuf, proplen, "%llu",
3436 (u_longlong_t)propvalue);
3438 zfs_nicebytes(propvalue, propbuf, proplen);
3445 * Returns the name of the given zfs handle.
3448 zfs_get_name(const zfs_handle_t *zhp)
3450 return (zhp->zfs_name);
3454 * Returns the name of the parent pool for the given zfs handle.
3457 zfs_get_pool_name(const zfs_handle_t *zhp)
3459 return (zhp->zpool_hdl->zpool_name);
3463 * Returns the type of the given zfs handle.
3466 zfs_get_type(const zfs_handle_t *zhp)
3468 return (zhp->zfs_type);
3472 * Is one dataset name a child dataset of another?
3474 * Needs to handle these cases:
3475 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3476 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3477 * Descendant? No. No. No. Yes.
3480 is_descendant(const char *ds1, const char *ds2)
3482 size_t d1len = strlen(ds1);
3484 /* ds2 can't be a descendant if it's smaller */
3485 if (strlen(ds2) < d1len)
3488 /* otherwise, compare strings and verify that there's a '/' char */
3489 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3493 * Given a complete name, return just the portion that refers to the parent.
3494 * Will return -1 if there is no parent (path is just the name of the
3498 parent_name(const char *path, char *buf, size_t buflen)
3502 (void) strlcpy(buf, path, buflen);
3504 if ((slashp = strrchr(buf, '/')) == NULL)
3512 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3514 return (parent_name(zfs_get_name(zhp), buf, buflen));
3518 * If accept_ancestor is false, then check to make sure that the given path has
3519 * a parent, and that it exists. If accept_ancestor is true, then find the
3520 * closest existing ancestor for the given path. In prefixlen return the
3521 * length of already existing prefix of the given path. We also fetch the
3522 * 'zoned' property, which is used to validate property settings when creating
3526 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3527 boolean_t accept_ancestor, int *prefixlen)
3529 zfs_cmd_t zc = {"\0"};
3530 char parent[ZFS_MAX_DATASET_NAME_LEN];
3536 (void) snprintf(errbuf, sizeof (errbuf),
3537 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3539 /* get parent, and check to see if this is just a pool */
3540 if (parent_name(path, parent, sizeof (parent)) != 0) {
3541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3542 "missing dataset name"));
3543 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3546 /* check to see if the pool exists */
3547 if ((slash = strchr(parent, '/')) == NULL)
3548 slash = parent + strlen(parent);
3549 (void) strncpy(zc.zc_name, parent, slash - parent);
3550 zc.zc_name[slash - parent] = '\0';
3551 if (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3554 "no such pool '%s'"), zc.zc_name);
3555 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3558 /* check to see if the parent dataset exists */
3559 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3560 if (errno == ENOENT && accept_ancestor) {
3562 * Go deeper to find an ancestor, give up on top level.
3564 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3566 "no such pool '%s'"), zc.zc_name);
3567 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3569 } else if (errno == ENOENT) {
3570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3571 "parent does not exist"));
3572 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3574 return (zfs_standard_error(hdl, errno, errbuf));
3577 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3581 /* we are in a non-global zone, but parent is in the global zone */
3582 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3583 (void) zfs_standard_error(hdl, EPERM, errbuf);
3588 /* make sure parent is a filesystem */
3589 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3591 "parent is not a filesystem"));
3592 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3598 if (prefixlen != NULL)
3599 *prefixlen = strlen(parent);
3604 * Finds whether the dataset of the given type(s) exists.
3607 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3611 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3615 * Try to get stats for the dataset, which will tell us if it exists.
3617 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3618 int ds_type = zhp->zfs_type;
3621 if (types & ds_type)
3628 * Given a path to 'target', create all the ancestors between
3629 * the prefixlen portion of the path, and the target itself.
3630 * Fail if the initial prefixlen-ancestor does not already exist.
3633 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3639 /* make sure prefix exists */
3640 cp = target + prefixlen;
3642 assert(strchr(cp, '/') == NULL);
3643 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3646 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3654 * Attempt to create, mount, and share any ancestor filesystems,
3655 * up to the prefixlen-long one.
3657 for (cp = target + prefixlen + 1;
3658 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3662 h = make_dataset_handle(hdl, target);
3664 /* it already exists, nothing to do here */
3669 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3671 opname = dgettext(TEXT_DOMAIN, "create");
3675 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3677 opname = dgettext(TEXT_DOMAIN, "open");
3681 if (zfs_mount(h, NULL, 0) != 0) {
3682 opname = dgettext(TEXT_DOMAIN, "mount");
3686 if (zfs_share(h) != 0) {
3687 opname = dgettext(TEXT_DOMAIN, "share");
3697 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3698 "failed to %s ancestor '%s'"), opname, target);
3703 * Creates non-existing ancestors of the given path.
3706 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3713 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3714 "cannot create '%s'"), path);
3717 * Check that we are not passing the nesting limit
3718 * before we start creating any ancestors.
3720 if (dataset_nestcheck(path) != 0) {
3721 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3722 "maximum name nesting depth exceeded"));
3723 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3726 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3729 if ((path_copy = strdup(path)) != NULL) {
3730 rc = create_parents(hdl, path_copy, prefix);
3733 if (path_copy == NULL || rc != 0)
3740 * Create a new filesystem or volume.
3743 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3748 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3750 enum lzc_dataset_type ost;
3751 zpool_handle_t *zpool_handle;
3752 uint8_t *wkeydata = NULL;
3755 char parent[ZFS_MAX_DATASET_NAME_LEN];
3757 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3758 "cannot create '%s'"), path);
3760 /* validate the path, taking care to note the extended error message */
3761 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3762 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3764 if (dataset_nestcheck(path) != 0) {
3765 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3766 "maximum name nesting depth exceeded"));
3767 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3770 /* validate parents exist */
3771 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3775 * The failure modes when creating a dataset of a different type over
3776 * one that already exists is a little strange. In particular, if you
3777 * try to create a dataset on top of an existing dataset, the ioctl()
3778 * will return ENOENT, not EEXIST. To prevent this from happening, we
3779 * first try to see if the dataset exists.
3781 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3783 "dataset already exists"));
3784 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3787 if (type == ZFS_TYPE_VOLUME)
3788 ost = LZC_DATSET_TYPE_ZVOL;
3790 ost = LZC_DATSET_TYPE_ZFS;
3792 /* open zpool handle for prop validation */
3793 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3794 (void) strlcpy(pool_path, path, sizeof (pool_path));
3796 /* truncate pool_path at first slash */
3797 char *p = strchr(pool_path, '/');
3801 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3804 if (props && (props = zfs_valid_proplist(hdl, type, props,
3805 zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3806 zpool_close(zpool_handle);
3809 zpool_close(zpool_handle);
3811 if (type == ZFS_TYPE_VOLUME) {
3813 * If we are creating a volume, the size and block size must
3814 * satisfy a few restraints. First, the blocksize must be a
3815 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3816 * volsize must be a multiple of the block size, and cannot be
3819 if (props == NULL || nvlist_lookup_uint64(props,
3820 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3823 "missing volume size"));
3824 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3827 if ((ret = nvlist_lookup_uint64(props,
3828 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3829 &blocksize)) != 0) {
3830 if (ret == ENOENT) {
3831 blocksize = zfs_prop_default_numeric(
3832 ZFS_PROP_VOLBLOCKSIZE);
3835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3836 "missing volume block size"));
3837 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3843 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3844 "volume size cannot be zero"));
3845 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3848 if (size % blocksize != 0) {
3850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3851 "volume size must be a multiple of volume block "
3853 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3857 (void) parent_name(path, parent, sizeof (parent));
3858 if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
3859 &wkeydata, &wkeylen) != 0) {
3861 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3864 /* create the dataset */
3865 ret = lzc_create(path, ost, props, wkeydata, wkeylen);
3867 if (wkeydata != NULL)
3870 /* check for failure */
3874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3875 "no such parent '%s'"), parent);
3876 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3879 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3880 "pool must be upgraded to set this "
3881 "property or value"));
3882 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3885 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3886 "encryption root's key is not loaded "
3888 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3891 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3892 "invalid property value(s) specified"));
3893 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3897 * This platform can't address a volume this big.
3899 if (type == ZFS_TYPE_VOLUME)
3900 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3905 return (zfs_standard_error(hdl, errno, errbuf));
3913 * Destroys the given dataset. The caller must make sure that the filesystem
3914 * isn't mounted, and that there are no active dependents. If the file system
3915 * does not exist this function does nothing.
3918 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3922 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3925 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3926 nvlist_t *nv = fnvlist_alloc();
3927 fnvlist_add_boolean(nv, zhp->zfs_name);
3928 error = lzc_destroy_bookmarks(nv, NULL);
3931 return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3932 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3938 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3939 nvlist_t *nv = fnvlist_alloc();
3940 fnvlist_add_boolean(nv, zhp->zfs_name);
3941 error = lzc_destroy_snaps(nv, defer, NULL);
3944 error = lzc_destroy(zhp->zfs_name);
3947 if (error != 0 && error != ENOENT) {
3948 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3949 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3953 remove_mountpoint(zhp);
3958 struct destroydata {
3960 const char *snapname;
3964 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3966 struct destroydata *dd = arg;
3967 char name[ZFS_MAX_DATASET_NAME_LEN];
3970 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
3971 dd->snapname) >= sizeof (name))
3974 if (lzc_exists(name))
3975 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3977 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3983 * Destroys all snapshots with the given name in zhp & descendants.
3986 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3989 struct destroydata dd = { 0 };
3991 dd.snapname = snapname;
3992 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3993 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3995 if (nvlist_empty(dd.nvl)) {
3996 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3997 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3998 zhp->zfs_name, snapname);
4000 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
4002 nvlist_free(dd.nvl);
4007 * Destroys all the snapshots named in the nvlist.
4010 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
4013 nvlist_t *errlist = NULL;
4016 ret = lzc_destroy_snaps(snaps, defer, &errlist);
4019 nvlist_free(errlist);
4023 if (nvlist_empty(errlist)) {
4025 (void) snprintf(errbuf, sizeof (errbuf),
4026 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
4028 ret = zfs_standard_error(hdl, ret, errbuf);
4030 for (pair = nvlist_next_nvpair(errlist, NULL);
4031 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
4033 (void) snprintf(errbuf, sizeof (errbuf),
4034 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
4037 switch (fnvpair_value_int32(pair)) {
4040 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
4041 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
4044 ret = zfs_standard_error(hdl, errno, errbuf);
4049 nvlist_free(errlist);
4054 * Clones the given dataset. The target must be of the same type as the source.
4057 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
4059 char parent[ZFS_MAX_DATASET_NAME_LEN];
4062 libzfs_handle_t *hdl = zhp->zfs_hdl;
4065 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4067 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4068 "cannot create '%s'"), target);
4070 /* validate the target/clone name */
4071 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
4072 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4074 /* validate parents exist */
4075 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
4078 (void) parent_name(target, parent, sizeof (parent));
4085 if (ZFS_IS_VOLUME(zhp)) {
4086 type = ZFS_TYPE_VOLUME;
4088 type = ZFS_TYPE_FILESYSTEM;
4090 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
4091 zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
4093 if (zfs_fix_auto_resv(zhp, props) == -1) {
4099 if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
4101 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
4104 ret = lzc_clone(target, zhp->zfs_name, props);
4112 * The parent doesn't exist. We should have caught this
4113 * above, but there may a race condition that has since
4114 * destroyed the parent.
4116 * At this point, we don't know whether it's the source
4117 * that doesn't exist anymore, or whether the target
4118 * dataset doesn't exist.
4120 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4121 "no such parent '%s'"), parent);
4122 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
4125 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4126 "source and target pools differ"));
4127 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
4131 return (zfs_standard_error(zhp->zfs_hdl, errno,
4140 * Promotes the given clone fs to be the clone parent.
4143 zfs_promote(zfs_handle_t *zhp)
4145 libzfs_handle_t *hdl = zhp->zfs_hdl;
4146 char snapname[ZFS_MAX_DATASET_NAME_LEN];
4150 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4151 "cannot promote '%s'"), zhp->zfs_name);
4153 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4155 "snapshots can not be promoted"));
4156 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4159 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
4160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4161 "not a cloned filesystem"));
4162 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4165 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4166 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4168 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
4174 * Promoting encrypted dataset outside its
4177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4178 "cannot promote dataset outside its "
4179 "encryption root"));
4180 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4183 /* There is a conflicting snapshot name. */
4184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4185 "conflicting snapshot '%s' from parent '%s'"),
4186 snapname, zhp->zfs_dmustats.dds_origin);
4187 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4190 return (zfs_standard_error(hdl, ret, errbuf));
4196 typedef struct snapdata {
4198 const char *sd_snapname;
4202 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4204 snapdata_t *sd = arg;
4205 char name[ZFS_MAX_DATASET_NAME_LEN];
4208 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4209 if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp),
4210 sd->sd_snapname) >= sizeof (name))
4213 fnvlist_add_boolean(sd->sd_nvl, name);
4215 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4223 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4227 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4233 zpool_handle_t *zpool_hdl;
4234 char pool[ZFS_MAX_DATASET_NAME_LEN];
4236 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4237 "cannot create snapshots "));
4240 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
4241 const char *snapname = nvpair_name(elem);
4243 /* validate the target name */
4244 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
4246 (void) snprintf(errbuf, sizeof (errbuf),
4247 dgettext(TEXT_DOMAIN,
4248 "cannot create snapshot '%s'"), snapname);
4249 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4254 * get pool handle for prop validation. assumes all snaps are in the
4255 * same pool, as does lzc_snapshot (below).
4257 elem = nvlist_next_nvpair(snaps, NULL);
4258 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4259 pool[strcspn(pool, "/@")] = '\0';
4260 zpool_hdl = zpool_open(hdl, pool);
4261 if (zpool_hdl == NULL)
4264 if (props != NULL &&
4265 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4266 props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4267 zpool_close(zpool_hdl);
4270 zpool_close(zpool_hdl);
4272 ret = lzc_snapshot(snaps, props, &errors);
4275 boolean_t printed = B_FALSE;
4276 for (elem = nvlist_next_nvpair(errors, NULL);
4278 elem = nvlist_next_nvpair(errors, elem)) {
4279 (void) snprintf(errbuf, sizeof (errbuf),
4280 dgettext(TEXT_DOMAIN,
4281 "cannot create snapshot '%s'"), nvpair_name(elem));
4282 (void) zfs_standard_error(hdl,
4283 fnvpair_value_int32(elem), errbuf);
4289 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4290 "multiple snapshots of same "
4292 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4296 (void) zfs_standard_error(hdl, ret, errbuf);
4302 nvlist_free(errors);
4307 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4311 snapdata_t sd = { 0 };
4312 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4317 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4318 "cannot snapshot %s"), path);
4320 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4321 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4323 (void) strlcpy(fsname, path, sizeof (fsname));
4324 cp = strchr(fsname, '@');
4326 sd.sd_snapname = cp + 1;
4328 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4329 ZFS_TYPE_VOLUME)) == NULL) {
4333 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4335 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4337 fnvlist_add_boolean(sd.sd_nvl, path);
4340 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4341 nvlist_free(sd.sd_nvl);
4347 * Destroy any more recent snapshots. We invoke this callback on any dependents
4348 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4349 * is a dependent and we should just destroy it without checking the transaction
4352 typedef struct rollback_data {
4353 const char *cb_target; /* the snapshot */
4354 uint64_t cb_create; /* creation time reference */
4360 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4362 rollback_data_t *cbp = data;
4363 prop_changelist_t *clp;
4365 /* We must destroy this clone; first unmount it */
4366 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4367 cbp->cb_force ? MS_FORCE: 0);
4368 if (clp == NULL || changelist_prefix(clp) != 0) {
4369 cbp->cb_error = B_TRUE;
4373 if (zfs_destroy(zhp, B_FALSE) != 0)
4374 cbp->cb_error = B_TRUE;
4376 changelist_remove(clp, zhp->zfs_name);
4377 (void) changelist_postfix(clp);
4378 changelist_free(clp);
4385 rollback_destroy(zfs_handle_t *zhp, void *data)
4387 rollback_data_t *cbp = data;
4389 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4390 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4391 rollback_destroy_dependent, cbp);
4393 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4401 * Given a dataset, rollback to a specific snapshot, discarding any
4402 * data changes since then and making it the active dataset.
4404 * Any snapshots and bookmarks more recent than the target are
4405 * destroyed, along with their dependents (i.e. clones).
4408 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4410 rollback_data_t cb = { 0 };
4412 boolean_t restore_resv = 0;
4413 uint64_t old_volsize = 0, new_volsize;
4414 zfs_prop_t resv_prop = { 0 };
4415 uint64_t min_txg = 0;
4417 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4418 zhp->zfs_type == ZFS_TYPE_VOLUME);
4421 * Destroy all recent snapshots and their dependents.
4423 cb.cb_force = force;
4424 cb.cb_target = snap->zfs_name;
4425 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4427 if (cb.cb_create > 0)
4428 min_txg = cb.cb_create;
4430 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb,
4433 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4439 * Now that we have verified that the snapshot is the latest,
4440 * rollback to the given snapshot.
4443 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4444 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4446 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4448 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4452 * Pass both the filesystem and the wanted snapshot names,
4453 * we would get an error back if the snapshot is destroyed or
4454 * a new snapshot is created before this request is processed.
4456 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4460 (void) snprintf(errbuf, sizeof (errbuf),
4461 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4465 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4466 "there is a snapshot or bookmark more recent "
4467 "than '%s'"), snap->zfs_name);
4468 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4471 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4472 "'%s' is not found among snapshots of '%s'"),
4473 snap->zfs_name, zhp->zfs_name);
4474 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4477 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4480 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4486 * For volumes, if the pre-rollback volsize matched the pre-
4487 * rollback reservation and the volsize has changed then set
4488 * the reservation property to the post-rollback volsize.
4489 * Make a new handle since the rollback closed the dataset.
4491 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4492 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4494 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4495 if (old_volsize != new_volsize)
4496 err = zfs_prop_set_int(zhp, resv_prop,
4505 * Renames the given dataset.
4508 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4509 boolean_t force_unmount)
4512 zfs_cmd_t zc = {"\0"};
4514 prop_changelist_t *cl = NULL;
4515 char parent[ZFS_MAX_DATASET_NAME_LEN];
4516 libzfs_handle_t *hdl = zhp->zfs_hdl;
4519 /* if we have the same exact name, just return success */
4520 if (strcmp(zhp->zfs_name, target) == 0)
4523 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4524 "cannot rename to '%s'"), target);
4526 /* make sure source name is valid */
4527 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4528 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4531 * Make sure the target name is valid
4533 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4534 if ((strchr(target, '@') == NULL) ||
4537 * Snapshot target name is abbreviated,
4538 * reconstruct full dataset name
4540 (void) strlcpy(parent, zhp->zfs_name,
4542 delim = strchr(parent, '@');
4543 if (strchr(target, '@') == NULL)
4547 (void) strlcat(parent, target, sizeof (parent));
4551 * Make sure we're renaming within the same dataset.
4553 delim = strchr(target, '@');
4554 if (strncmp(zhp->zfs_name, target, delim - target)
4555 != 0 || zhp->zfs_name[delim - target] != '@') {
4556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4557 "snapshots must be part of same "
4559 return (zfs_error(hdl, EZFS_CROSSTARGET,
4564 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4565 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4568 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4569 "recursive rename must be a snapshot"));
4570 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4573 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4574 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4576 /* validate parents */
4577 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4580 /* make sure we're in the same pool */
4581 verify((delim = strchr(target, '/')) != NULL);
4582 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4583 zhp->zfs_name[delim - target] != '/') {
4584 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4585 "datasets must be within same pool"));
4586 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4589 /* new name cannot be a child of the current dataset name */
4590 if (is_descendant(zhp->zfs_name, target)) {
4591 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4592 "New dataset name cannot be a descendant of "
4593 "current dataset name"));
4594 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4598 (void) snprintf(errbuf, sizeof (errbuf),
4599 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4601 if (getzoneid() == GLOBAL_ZONEID &&
4602 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4604 "dataset is used in a non-global zone"));
4605 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4610 char *parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4611 if (parentname == NULL) {
4615 delim = strchr(parentname, '@');
4617 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4624 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4625 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4626 CL_GATHER_ITER_MOUNTED,
4627 force_unmount ? MS_FORCE : 0)) == NULL)
4630 if (changelist_haszonedchild(cl)) {
4631 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4632 "child dataset with inherited mountpoint is used "
4633 "in a non-global zone"));
4634 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4639 if ((ret = changelist_prefix(cl)) != 0)
4643 if (ZFS_IS_VOLUME(zhp))
4644 zc.zc_objset_type = DMU_OST_ZVOL;
4646 zc.zc_objset_type = DMU_OST_ZFS;
4648 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4649 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4651 zc.zc_cookie = recursive;
4653 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4655 * if it was recursive, the one that actually failed will
4658 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4659 "cannot rename '%s'"), zc.zc_name);
4661 if (recursive && errno == EEXIST) {
4662 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4663 "a child dataset already has a snapshot "
4664 "with the new name"));
4665 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4666 } else if (errno == EACCES) {
4667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4668 "cannot move encrypted child outside of "
4669 "its encryption root"));
4670 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4672 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4676 * On failure, we still want to remount any filesystems that
4677 * were previously mounted, so we don't alter the system state.
4680 (void) changelist_postfix(cl);
4683 changelist_rename(cl, zfs_get_name(zhp), target);
4684 ret = changelist_postfix(cl);
4690 changelist_free(cl);
4696 zfs_get_all_props(zfs_handle_t *zhp)
4698 return (zhp->zfs_props);
4702 zfs_get_recvd_props(zfs_handle_t *zhp)
4704 if (zhp->zfs_recvd_props == NULL)
4705 if (get_recvd_props_ioctl(zhp) != 0)
4707 return (zhp->zfs_recvd_props);
4711 zfs_get_user_props(zfs_handle_t *zhp)
4713 return (zhp->zfs_user_props);
4717 * This function is used by 'zfs list' to determine the exact set of columns to
4718 * display, and their maximum widths. This does two main things:
4720 * - If this is a list of all properties, then expand the list to include
4721 * all native properties, and set a flag so that for each dataset we look
4722 * for new unique user properties and add them to the list.
4724 * - For non fixed-width properties, keep track of the maximum width seen
4725 * so that we can size the column appropriately. If the user has
4726 * requested received property values, we also need to compute the width
4727 * of the RECEIVED column.
4730 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4733 libzfs_handle_t *hdl = zhp->zfs_hdl;
4734 zprop_list_t *entry;
4735 zprop_list_t **last, **start;
4736 nvlist_t *userprops, *propval;
4739 char buf[ZFS_MAXPROPLEN];
4741 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4744 userprops = zfs_get_user_props(zhp);
4747 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4749 * Go through and add any user properties as necessary. We
4750 * start by incrementing our list pointer to the first
4751 * non-native property.
4754 while (*start != NULL) {
4755 if ((*start)->pl_prop == ZPROP_INVAL)
4757 start = &(*start)->pl_next;
4761 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4763 * See if we've already found this property in our list.
4765 for (last = start; *last != NULL;
4766 last = &(*last)->pl_next) {
4767 if (strcmp((*last)->pl_user_prop,
4768 nvpair_name(elem)) == 0)
4772 if (*last == NULL) {
4773 if ((entry = zfs_alloc(hdl,
4774 sizeof (zprop_list_t))) == NULL ||
4775 ((entry->pl_user_prop = zfs_strdup(hdl,
4776 nvpair_name(elem)))) == NULL) {
4781 entry->pl_prop = ZPROP_INVAL;
4782 entry->pl_width = strlen(nvpair_name(elem));
4783 entry->pl_all = B_TRUE;
4790 * Now go through and check the width of any non-fixed columns
4792 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4793 if (entry->pl_fixed && !literal)
4796 if (entry->pl_prop != ZPROP_INVAL) {
4797 if (zfs_prop_get(zhp, entry->pl_prop,
4798 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4799 if (strlen(buf) > entry->pl_width)
4800 entry->pl_width = strlen(buf);
4802 if (received && zfs_prop_get_recvd(zhp,
4803 zfs_prop_to_name(entry->pl_prop),
4804 buf, sizeof (buf), literal) == 0)
4805 if (strlen(buf) > entry->pl_recvd_width)
4806 entry->pl_recvd_width = strlen(buf);
4808 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4810 verify(nvlist_lookup_string(propval,
4811 ZPROP_VALUE, &strval) == 0);
4812 if (strlen(strval) > entry->pl_width)
4813 entry->pl_width = strlen(strval);
4815 if (received && zfs_prop_get_recvd(zhp,
4816 entry->pl_user_prop,
4817 buf, sizeof (buf), literal) == 0)
4818 if (strlen(buf) > entry->pl_recvd_width)
4819 entry->pl_recvd_width = strlen(buf);
4827 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4833 * Keep a reference to the props-table against which we prune the
4836 zhp->zfs_props_table = props;
4838 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4841 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4842 next = nvlist_next_nvpair(zhp->zfs_props, curr);
4845 * User properties will result in ZPROP_INVAL, and since we
4846 * only know how to prune standard ZFS properties, we always
4847 * leave these in the list. This can also happen if we
4848 * encounter an unknown DSL property (when running older
4849 * software, for example).
4851 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4852 (void) nvlist_remove(zhp->zfs_props,
4853 nvpair_name(curr), nvpair_type(curr));
4859 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4860 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4862 zfs_cmd_t zc = {"\0"};
4863 nvlist_t *nvlist = NULL;
4866 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4867 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4868 zc.zc_cookie = (uint64_t)cmd;
4870 if (cmd == ZFS_SMB_ACL_RENAME) {
4871 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4872 (void) no_memory(hdl);
4878 case ZFS_SMB_ACL_ADD:
4879 case ZFS_SMB_ACL_REMOVE:
4880 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4882 case ZFS_SMB_ACL_RENAME:
4883 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4885 (void) no_memory(hdl);
4888 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4890 (void) no_memory(hdl);
4893 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4894 nvlist_free(nvlist);
4898 case ZFS_SMB_ACL_PURGE:
4903 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4904 nvlist_free(nvlist);
4909 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4910 char *path, char *resource)
4912 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4917 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4918 char *path, char *resource)
4920 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4925 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4927 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4932 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4933 char *oldname, char *newname)
4935 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4940 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4941 zfs_userspace_cb_t func, void *arg)
4943 zfs_cmd_t zc = {"\0"};
4944 zfs_useracct_t buf[100];
4945 libzfs_handle_t *hdl = zhp->zfs_hdl;
4948 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4950 zc.zc_objset_type = type;
4951 zc.zc_nvlist_dst = (uintptr_t)buf;
4954 zfs_useracct_t *zua = buf;
4956 zc.zc_nvlist_dst_size = sizeof (buf);
4957 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4960 if ((errno == ENOTSUP &&
4961 (type == ZFS_PROP_USEROBJUSED ||
4962 type == ZFS_PROP_GROUPOBJUSED ||
4963 type == ZFS_PROP_USEROBJQUOTA ||
4964 type == ZFS_PROP_GROUPOBJQUOTA ||
4965 type == ZFS_PROP_PROJECTOBJUSED ||
4966 type == ZFS_PROP_PROJECTOBJQUOTA ||
4967 type == ZFS_PROP_PROJECTUSED ||
4968 type == ZFS_PROP_PROJECTQUOTA)))
4971 (void) snprintf(errbuf, sizeof (errbuf),
4972 dgettext(TEXT_DOMAIN,
4973 "cannot get used/quota for %s"), zc.zc_name);
4974 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4976 if (zc.zc_nvlist_dst_size == 0)
4979 while (zc.zc_nvlist_dst_size > 0) {
4980 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4981 zua->zu_space)) != 0)
4984 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4993 const char *snapname;
4995 boolean_t recursive;
5000 zfs_hold_one(zfs_handle_t *zhp, void *arg)
5002 struct holdarg *ha = arg;
5003 char name[ZFS_MAX_DATASET_NAME_LEN];
5006 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
5007 ha->snapname) >= sizeof (name))
5010 if (lzc_exists(name))
5011 fnvlist_add_string(ha->nvl, name, ha->tag);
5014 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
5020 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
5021 boolean_t recursive, int cleanup_fd)
5026 ha.nvl = fnvlist_alloc();
5027 ha.snapname = snapname;
5029 ha.recursive = recursive;
5030 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
5032 if (nvlist_empty(ha.nvl)) {
5035 fnvlist_free(ha.nvl);
5037 (void) snprintf(errbuf, sizeof (errbuf),
5038 dgettext(TEXT_DOMAIN,
5039 "cannot hold snapshot '%s@%s'"),
5040 zhp->zfs_name, snapname);
5041 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
5045 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
5046 fnvlist_free(ha.nvl);
5052 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
5056 libzfs_handle_t *hdl = zhp->zfs_hdl;
5061 ret = lzc_hold(holds, cleanup_fd, &errors);
5064 /* There may be errors even in the success case. */
5065 fnvlist_free(errors);
5069 if (nvlist_empty(errors)) {
5070 /* no hold-specific errors */
5071 (void) snprintf(errbuf, sizeof (errbuf),
5072 dgettext(TEXT_DOMAIN, "cannot hold"));
5075 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5076 "pool must be upgraded"));
5077 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5080 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5083 (void) zfs_standard_error(hdl, ret, errbuf);
5087 for (elem = nvlist_next_nvpair(errors, NULL);
5089 elem = nvlist_next_nvpair(errors, elem)) {
5090 (void) snprintf(errbuf, sizeof (errbuf),
5091 dgettext(TEXT_DOMAIN,
5092 "cannot hold snapshot '%s'"), nvpair_name(elem));
5093 switch (fnvpair_value_int32(elem)) {
5096 * Temporary tags wind up having the ds object id
5097 * prepended. So even if we passed the length check
5098 * above, it's still possible for the tag to wind
5099 * up being slightly too long.
5101 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
5104 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5107 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
5110 (void) zfs_standard_error(hdl,
5111 fnvpair_value_int32(elem), errbuf);
5115 fnvlist_free(errors);
5120 zfs_release_one(zfs_handle_t *zhp, void *arg)
5122 struct holdarg *ha = arg;
5123 char name[ZFS_MAX_DATASET_NAME_LEN];
5125 nvlist_t *existing_holds;
5127 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
5128 ha->snapname) >= sizeof (name)) {
5133 if (lzc_get_holds(name, &existing_holds) != 0) {
5135 } else if (!nvlist_exists(existing_holds, ha->tag)) {
5138 nvlist_t *torelease = fnvlist_alloc();
5139 fnvlist_add_boolean(torelease, ha->tag);
5140 fnvlist_add_nvlist(ha->nvl, name, torelease);
5141 fnvlist_free(torelease);
5145 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
5151 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
5152 boolean_t recursive)
5156 nvlist_t *errors = NULL;
5158 libzfs_handle_t *hdl = zhp->zfs_hdl;
5161 ha.nvl = fnvlist_alloc();
5162 ha.snapname = snapname;
5164 ha.recursive = recursive;
5166 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
5168 if (nvlist_empty(ha.nvl)) {
5169 fnvlist_free(ha.nvl);
5171 (void) snprintf(errbuf, sizeof (errbuf),
5172 dgettext(TEXT_DOMAIN,
5173 "cannot release hold from snapshot '%s@%s'"),
5174 zhp->zfs_name, snapname);
5176 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5178 (void) zfs_standard_error(hdl, ret, errbuf);
5183 ret = lzc_release(ha.nvl, &errors);
5184 fnvlist_free(ha.nvl);
5187 /* There may be errors even in the success case. */
5188 fnvlist_free(errors);
5192 if (nvlist_empty(errors)) {
5193 /* no hold-specific errors */
5194 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5199 "pool must be upgraded"));
5200 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5203 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
5207 for (elem = nvlist_next_nvpair(errors, NULL);
5209 elem = nvlist_next_nvpair(errors, elem)) {
5210 (void) snprintf(errbuf, sizeof (errbuf),
5211 dgettext(TEXT_DOMAIN,
5212 "cannot release hold from snapshot '%s'"),
5214 switch (fnvpair_value_int32(elem)) {
5216 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5219 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5222 (void) zfs_standard_error_fmt(hdl,
5223 fnvpair_value_int32(elem), errbuf);
5227 fnvlist_free(errors);
5232 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5234 zfs_cmd_t zc = {"\0"};
5235 libzfs_handle_t *hdl = zhp->zfs_hdl;
5241 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5242 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5246 nvbuf = malloc(nvsz);
5247 if (nvbuf == NULL) {
5248 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5252 zc.zc_nvlist_dst_size = nvsz;
5253 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5255 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5257 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) {
5258 (void) snprintf(errbuf, sizeof (errbuf),
5259 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5264 nvsz = zc.zc_nvlist_dst_size;
5268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5269 "pool must be upgraded"));
5270 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5273 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5276 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5279 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5284 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5286 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
5287 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5289 err = zfs_standard_error_fmt(hdl, rc, errbuf);
5299 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5301 zfs_cmd_t zc = {"\0"};
5302 libzfs_handle_t *hdl = zhp->zfs_hdl;
5308 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5309 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5311 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5314 nvbuf = malloc(nvsz);
5316 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5319 zc.zc_nvlist_src_size = nvsz;
5320 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5321 zc.zc_perm_action = un;
5323 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5325 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5326 (void) snprintf(errbuf, sizeof (errbuf),
5327 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5331 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5332 "pool must be upgraded"));
5333 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5336 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5339 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5342 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5353 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5358 err = lzc_get_holds(zhp->zfs_name, nvl);
5361 libzfs_handle_t *hdl = zhp->zfs_hdl;
5363 (void) snprintf(errbuf, sizeof (errbuf),
5364 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5369 "pool must be upgraded"));
5370 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5373 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5376 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5379 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5388 * The theory of raidz space accounting
5390 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5391 * will "reference" 128KB, even though it allocates more than that, to store the
5392 * parity information (and perhaps skip sectors). This concept of the
5393 * "referenced" (and other DMU space accounting) being lower than the allocated
5394 * space by a constant factor is called "raidz deflation."
5396 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5397 * block size. However, zvols typically have a much smaller block size (default
5398 * 8KB). These smaller blocks may require proportionally much more parity
5399 * information (and perhaps skip sectors). In this case, the change to the
5400 * "referenced" property may be much more than the logical block size.
5402 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5405 * +-------+-------+-------+-------+-------+
5406 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5407 * +-------+-------+-------+-------+-------+
5408 * | P0 | D0 | D8 | D16 | D24 |
5409 * | P1 | D1 | D9 | D17 | D25 |
5410 * | P2 | D2 | D10 | D18 | D26 |
5411 * | P3 | D3 | D11 | D19 | D27 |
5412 * | P4 | D4 | D12 | D20 | D28 |
5413 * | P5 | D5 | D13 | D21 | D29 |
5414 * | P6 | D6 | D14 | D22 | D30 |
5415 * | P7 | D7 | D15 | D23 | D31 |
5416 * +-------+-------+-------+-------+-------+
5418 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5419 * sectors. The dataset's referenced will increase by 128k and the pool's
5420 * allocated and free properties will be adjusted by 160k.
5422 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5423 * blank cells represent unallocated space.
5425 * +-------+-------+-------+-------+-------+
5426 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5427 * +-------+-------+-------+-------+-------+
5429 * +-------+-------+-------+-------+-------+
5431 * Above, notice that the 4k block required one sector for parity and another
5432 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5433 * and free properties will be adjusted by 8k. The dataset will not be charged
5434 * 8k. Rather, it will be charged a value that is scaled according to the
5435 * overhead of the 128k block on the same vdev. This 8k allocation will be
5436 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5437 * calculated in the 128k block example above.
5439 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5440 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5441 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5442 * multiple of of 4 sectors. When a block does not fill the required number of
5443 * sectors, skip blocks (sectors) are used.
5445 * An 8k block being written to a raidz vdev may be written as follows:
5447 * +-------+-------+-------+-------+-------+
5448 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5449 * +-------+-------+-------+-------+-------+
5450 * | P0 | D0 | D1 | S0 | |
5451 * +-------+-------+-------+-------+-------+
5453 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5454 * added. For this 8k block, the pool's allocated and free properties are
5455 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5456 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5457 * the 128k block example above.
5459 * Compression may lead to a variety of block sizes being written for the same
5460 * volume or file. There is no clear way to reserve just the amount of space
5461 * that will be required, so the worst case (no compression) is assumed.
5462 * Note that metadata blocks will typically be compressed, so the reservation
5463 * size returned by zvol_volsize_to_reservation() will generally be slightly
5464 * larger than the maximum that the volume can reference.
5468 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5469 * amount of space (in bytes) that will be allocated for the specified block
5470 * size. Note that the "referenced" space accounted will be less than this, but
5471 * not necessarily equal to "blksize", due to RAIDZ deflation.
5474 vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5477 uint64_t asize, ndata;
5479 ASSERT3U(ndisks, >, nparity);
5480 ndata = ndisks - nparity;
5481 asize = ((blksize - 1) >> ashift) + 1;
5482 asize += nparity * ((asize + ndata - 1) / ndata);
5483 asize = roundup(asize, nparity + 1) << ashift;
5489 * Determine how much space will be allocated if it lands on the most space-
5490 * inefficient top-level vdev. Returns the size in bytes required to store one
5491 * copy of the volume data. See theory comment above.
5494 volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5496 nvlist_t *config, *tree, **vdevs;
5500 config = zpool_get_config(zhp, NULL);
5501 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5502 nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5503 &vdevs, &nvdevs) != 0) {
5504 return (nblocks * blksize);
5507 for (v = 0; v < nvdevs; v++) {
5509 uint64_t nparity, ashift, asize, tsize;
5514 if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5515 &type) != 0 || strcmp(type, VDEV_TYPE_RAIDZ) != 0 ||
5516 nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_NPARITY,
5518 nvlist_lookup_uint64(vdevs[v], ZPOOL_CONFIG_ASHIFT,
5520 nvlist_lookup_nvlist_array(vdevs[v], ZPOOL_CONFIG_CHILDREN,
5521 &disks, &ndisks) != 0) {
5525 /* allocation size for the "typical" 128k block */
5526 tsize = vdev_raidz_asize(ndisks, nparity, ashift,
5527 SPA_OLD_MAXBLOCKSIZE);
5528 /* allocation size for the blksize block */
5529 asize = vdev_raidz_asize(ndisks, nparity, ashift, blksize);
5532 * Scale this size down as a ratio of 128k / tsize. See theory
5535 volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize;
5536 if (volsize > ret) {
5542 ret = nblocks * blksize;
5549 * Convert the zvol's volume size to an appropriate reservation. See theory
5552 * Note: If this routine is updated, it is necessary to update the ZFS test
5553 * suite's shell version in reservation.shlib.
5556 zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5560 uint64_t nblocks, volblocksize;
5564 if (nvlist_lookup_string(props,
5565 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5566 ncopies = atoi(strval);
5569 if (nvlist_lookup_uint64(props,
5570 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5571 &volblocksize) != 0)
5572 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5574 nblocks = volsize / volblocksize;
5576 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5577 * this reason, only the data blocks are scaled based on vdev config.
5579 volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5581 /* start with metadnode L0-L6 */
5583 /* calculate number of indirects */
5584 while (nblocks > 1) {
5585 nblocks += DNODES_PER_LEVEL - 1;
5586 nblocks /= DNODES_PER_LEVEL;
5589 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5592 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5593 * compressed, but in practice they compress down to about
5596 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;