4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2018, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2017 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.
46 #include <sys/mntent.h>
47 #include <sys/mount.h>
55 #include <directory.h>
56 #endif /* HAVE_IDMAP */
58 #include <sys/dnode.h>
61 #include <sys/dsl_crypt.h>
65 #include "zfs_namecheck.h"
67 #include "libzfs_impl.h"
69 #include "zfs_deleg.h"
71 static int userquota_propname_decode(const char *propname, boolean_t zoned,
72 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
75 * Given a single type (not a mask of types), return the type in a human
79 zfs_type_to_name(zfs_type_t type)
82 case ZFS_TYPE_FILESYSTEM:
83 return (dgettext(TEXT_DOMAIN, "filesystem"));
84 case ZFS_TYPE_SNAPSHOT:
85 return (dgettext(TEXT_DOMAIN, "snapshot"));
87 return (dgettext(TEXT_DOMAIN, "volume"));
89 return (dgettext(TEXT_DOMAIN, "pool"));
90 case ZFS_TYPE_BOOKMARK:
91 return (dgettext(TEXT_DOMAIN, "bookmark"));
93 assert(!"unhandled zfs_type_t");
100 * Validate a ZFS path. This is used even before trying to open the dataset, to
101 * provide a more meaningful error message. We call zfs_error_aux() to
102 * explain exactly why the name was not valid.
105 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
111 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114 "snapshot delimiter '@' is not expected here"));
118 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121 "missing '@' delimiter in snapshot name, "
122 "did you mean to use -r?"));
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, "
137 "did you mean to use -r?"));
141 if (modifying && strchr(path, '%') != NULL) {
143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
144 "invalid character %c in name"), '%');
148 if (entity_namecheck(path, &why, &what) != 0) {
151 case NAME_ERR_TOOLONG:
152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
153 "name is too long"));
156 case NAME_ERR_LEADING_SLASH:
157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158 "leading slash in name"));
161 case NAME_ERR_EMPTY_COMPONENT:
162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
163 "empty component or misplaced '@'"
164 " or '#' delimiter in name"));
167 case NAME_ERR_TRAILING_SLASH:
168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169 "trailing slash in name"));
172 case NAME_ERR_INVALCHAR:
174 dgettext(TEXT_DOMAIN, "invalid character "
175 "'%c' in name"), what);
178 case NAME_ERR_MULTIPLE_DELIMITERS:
179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
180 "multiple '@' and/or '#' delimiters in "
184 case NAME_ERR_NOLETTER:
185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
186 "pool doesn't begin with a letter"));
189 case NAME_ERR_RESERVED:
190 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
191 "name is reserved"));
194 case NAME_ERR_DISKLIKE:
195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
196 "reserved disk name"));
200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
201 "(%d) not defined"), why);
213 zfs_name_valid(const char *name, zfs_type_t type)
215 if (type == ZFS_TYPE_POOL)
216 return (zpool_name_valid(NULL, B_FALSE, name));
217 return (zfs_validate_name(NULL, name, type, B_FALSE));
221 * This function takes the raw DSL properties, and filters out the user-defined
222 * properties into a separate nvlist.
225 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
227 libzfs_handle_t *hdl = zhp->zfs_hdl;
232 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
233 (void) no_memory(hdl);
238 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
239 if (!zfs_prop_user(nvpair_name(elem)))
242 verify(nvpair_value_nvlist(elem, &propval) == 0);
243 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
245 (void) no_memory(hdl);
253 static zpool_handle_t *
254 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
256 libzfs_handle_t *hdl = zhp->zfs_hdl;
259 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
260 if (hdl->libzfs_pool_handles != NULL)
261 zph->zpool_next = hdl->libzfs_pool_handles;
262 hdl->libzfs_pool_handles = zph;
267 static zpool_handle_t *
268 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
270 libzfs_handle_t *hdl = zhp->zfs_hdl;
271 zpool_handle_t *zph = hdl->libzfs_pool_handles;
273 while ((zph != NULL) &&
274 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
275 zph = zph->zpool_next;
280 * Returns a handle to the pool that contains the provided dataset.
281 * If a handle to that pool already exists then that handle is returned.
282 * Otherwise, a new handle is created and added to the list of handles.
284 static zpool_handle_t *
285 zpool_handle(zfs_handle_t *zhp)
291 len = strcspn(zhp->zfs_name, "/@#") + 1;
292 pool_name = zfs_alloc(zhp->zfs_hdl, len);
293 (void) strlcpy(pool_name, zhp->zfs_name, len);
295 zph = zpool_find_handle(zhp, pool_name, len);
297 zph = zpool_add_handle(zhp, pool_name);
304 zpool_free_handles(libzfs_handle_t *hdl)
306 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
308 while (zph != NULL) {
309 next = zph->zpool_next;
313 hdl->libzfs_pool_handles = NULL;
317 * Utility function to gather stats (objset and zpl) for the given object.
320 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
322 libzfs_handle_t *hdl = zhp->zfs_hdl;
324 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
326 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
327 if (errno == ENOMEM) {
328 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
339 * Utility function to get the received properties of the given object.
342 get_recvd_props_ioctl(zfs_handle_t *zhp)
344 libzfs_handle_t *hdl = zhp->zfs_hdl;
345 nvlist_t *recvdprops;
346 zfs_cmd_t zc = {"\0"};
349 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
352 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
354 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
355 if (errno == ENOMEM) {
356 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
360 zcmd_free_nvlists(&zc);
365 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
366 zcmd_free_nvlists(&zc);
370 nvlist_free(zhp->zfs_recvd_props);
371 zhp->zfs_recvd_props = recvdprops;
377 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
379 nvlist_t *allprops, *userprops;
381 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
383 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
388 * XXX Why do we store the user props separately, in addition to
389 * storing them in zfs_props?
391 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
392 nvlist_free(allprops);
396 nvlist_free(zhp->zfs_props);
397 nvlist_free(zhp->zfs_user_props);
399 zhp->zfs_props = allprops;
400 zhp->zfs_user_props = userprops;
406 get_stats(zfs_handle_t *zhp)
409 zfs_cmd_t zc = {"\0"};
411 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
413 if (get_stats_ioctl(zhp, &zc) != 0)
415 else if (put_stats_zhdl(zhp, &zc) != 0)
417 zcmd_free_nvlists(&zc);
422 * Refresh the properties currently stored in the handle.
425 zfs_refresh_properties(zfs_handle_t *zhp)
427 (void) get_stats(zhp);
431 * Makes a handle from the given dataset name. Used by zfs_open() and
432 * zfs_iter_* to create child handles on the fly.
435 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
437 if (put_stats_zhdl(zhp, zc) != 0)
441 * We've managed to open the dataset and gather statistics. Determine
442 * the high-level type.
444 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
445 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
446 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
447 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
448 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
453 if (zhp->zfs_dmustats.dds_is_snapshot)
454 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
455 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
456 zhp->zfs_type = ZFS_TYPE_VOLUME;
457 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
458 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
460 abort(); /* we should never see any other types */
462 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
469 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
471 zfs_cmd_t zc = {"\0"};
473 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
479 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
480 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
484 if (get_stats_ioctl(zhp, &zc) == -1) {
485 zcmd_free_nvlists(&zc);
489 if (make_dataset_handle_common(zhp, &zc) == -1) {
493 zcmd_free_nvlists(&zc);
498 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
500 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
506 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
507 if (make_dataset_handle_common(zhp, zc) == -1) {
515 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
517 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
522 zhp->zfs_hdl = pzhp->zfs_hdl;
523 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
524 zhp->zfs_head_type = pzhp->zfs_type;
525 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
526 zhp->zpool_hdl = zpool_handle(zhp);
532 zfs_handle_dup(zfs_handle_t *zhp_orig)
534 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
539 zhp->zfs_hdl = zhp_orig->zfs_hdl;
540 zhp->zpool_hdl = zhp_orig->zpool_hdl;
541 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
542 sizeof (zhp->zfs_name));
543 zhp->zfs_type = zhp_orig->zfs_type;
544 zhp->zfs_head_type = zhp_orig->zfs_head_type;
545 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
546 if (zhp_orig->zfs_props != NULL) {
547 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
548 (void) no_memory(zhp->zfs_hdl);
553 if (zhp_orig->zfs_user_props != NULL) {
554 if (nvlist_dup(zhp_orig->zfs_user_props,
555 &zhp->zfs_user_props, 0) != 0) {
556 (void) no_memory(zhp->zfs_hdl);
561 if (zhp_orig->zfs_recvd_props != NULL) {
562 if (nvlist_dup(zhp_orig->zfs_recvd_props,
563 &zhp->zfs_recvd_props, 0)) {
564 (void) no_memory(zhp->zfs_hdl);
569 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
570 if (zhp_orig->zfs_mntopts != NULL) {
571 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
572 zhp_orig->zfs_mntopts);
574 zhp->zfs_props_table = zhp_orig->zfs_props_table;
579 zfs_bookmark_exists(const char *path)
583 char fsname[ZFS_MAX_DATASET_NAME_LEN];
590 (void) strlcpy(fsname, path, sizeof (fsname));
591 pound = strchr(fsname, '#');
596 bmark_name = pound + 1;
597 props = fnvlist_alloc();
598 err = lzc_get_bookmarks(fsname, props, &bmarks);
605 rv = nvlist_exists(bmarks, bmark_name);
611 make_bookmark_handle(zfs_handle_t *parent, const char *path,
612 nvlist_t *bmark_props)
614 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
619 /* Fill in the name. */
620 zhp->zfs_hdl = parent->zfs_hdl;
621 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
623 /* Set the property lists. */
624 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
630 zhp->zfs_head_type = parent->zfs_head_type;
631 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
633 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
634 nvlist_free(zhp->zfs_props);
642 struct zfs_open_bookmarks_cb_data {
648 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
650 struct zfs_open_bookmarks_cb_data *dp = data;
653 * Is it the one we are looking for?
655 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
657 * We found it. Save it and let the caller know we are done.
664 * Not found. Close the handle and ask for another one.
671 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
672 * argument is a mask of acceptable types. The function will print an
673 * appropriate error message and return NULL if it can't be opened.
676 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
682 (void) snprintf(errbuf, sizeof (errbuf),
683 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
686 * Validate the name before we even try to open it.
688 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
689 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
694 * Bookmarks needs to be handled separately.
696 bookp = strchr(path, '#');
699 * Try to get stats for the dataset, which will tell us if it
703 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
704 (void) zfs_standard_error(hdl, errno, errbuf);
708 char dsname[ZFS_MAX_DATASET_NAME_LEN];
710 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
713 * We need to cut out '#' and everything after '#'
714 * to get the parent dataset name only.
716 assert(bookp - path < sizeof (dsname));
717 (void) strncpy(dsname, path, bookp - path);
718 dsname[bookp - path] = '\0';
721 * Create handle for the parent dataset.
724 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
725 (void) zfs_standard_error(hdl, errno, errbuf);
730 * Iterate bookmarks to find the right one.
733 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
734 &cb_data) == 0) && (cb_data.zhp == NULL)) {
735 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
739 if (cb_data.zhp == NULL) {
740 (void) zfs_standard_error(hdl, errno, errbuf);
752 if (!(types & zhp->zfs_type)) {
753 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
762 * Release a ZFS handle. Nothing to do but free the associated memory.
765 zfs_close(zfs_handle_t *zhp)
767 if (zhp->zfs_mntopts)
768 free(zhp->zfs_mntopts);
769 nvlist_free(zhp->zfs_props);
770 nvlist_free(zhp->zfs_user_props);
771 nvlist_free(zhp->zfs_recvd_props);
775 typedef struct mnttab_node {
776 struct mnttab mtn_mt;
781 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
783 const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
784 const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
787 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
789 return (AVL_ISIGN(rv));
793 libzfs_mnttab_init(libzfs_handle_t *hdl)
795 pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL);
796 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
797 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
798 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
802 libzfs_mnttab_update(libzfs_handle_t *hdl)
806 /* Reopen MNTTAB to prevent reading stale data from open file */
807 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
810 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
814 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
817 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
818 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
819 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
820 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
821 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
823 /* Exclude duplicate mounts */
824 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
825 free(mtn->mtn_mt.mnt_special);
826 free(mtn->mtn_mt.mnt_mountp);
827 free(mtn->mtn_mt.mnt_fstype);
828 free(mtn->mtn_mt.mnt_mntopts);
833 avl_add(&hdl->libzfs_mnttab_cache, mtn);
840 libzfs_mnttab_fini(libzfs_handle_t *hdl)
845 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
847 free(mtn->mtn_mt.mnt_special);
848 free(mtn->mtn_mt.mnt_mountp);
849 free(mtn->mtn_mt.mnt_fstype);
850 free(mtn->mtn_mt.mnt_mntopts);
853 avl_destroy(&hdl->libzfs_mnttab_cache);
854 (void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
858 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
860 hdl->libzfs_mnttab_enable = enable;
864 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
865 struct mnttab *entry)
871 if (!hdl->libzfs_mnttab_enable) {
872 struct mnttab srch = { 0 };
874 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
875 libzfs_mnttab_fini(hdl);
877 /* Reopen MNTTAB to prevent reading stale data from open file */
878 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
881 srch.mnt_special = (char *)fsname;
882 srch.mnt_fstype = MNTTYPE_ZFS;
883 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
889 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
890 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) {
893 if ((error = libzfs_mnttab_update(hdl)) != 0) {
894 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
899 find.mtn_mt.mnt_special = (char *)fsname;
900 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
902 *entry = mtn->mtn_mt;
905 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
910 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
911 const char *mountp, const char *mntopts)
915 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
916 if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
917 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
918 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
919 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
920 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
921 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
923 * Another thread may have already added this entry
924 * via libzfs_mnttab_update. If so we should skip it.
926 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, NULL) != NULL)
929 avl_add(&hdl->libzfs_mnttab_cache, mtn);
931 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
935 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
940 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
941 find.mtn_mt.mnt_special = (char *)fsname;
942 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
944 avl_remove(&hdl->libzfs_mnttab_cache, ret);
945 free(ret->mtn_mt.mnt_special);
946 free(ret->mtn_mt.mnt_mountp);
947 free(ret->mtn_mt.mnt_fstype);
948 free(ret->mtn_mt.mnt_mntopts);
951 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
955 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
957 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
959 if (zpool_handle == NULL)
962 *spa_version = zpool_get_prop_int(zpool_handle,
963 ZPOOL_PROP_VERSION, NULL);
968 * The choice of reservation property depends on the SPA version.
971 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
975 if (zfs_spa_version(zhp, &spa_version) < 0)
978 if (spa_version >= SPA_VERSION_REFRESERVATION)
979 *resv_prop = ZFS_PROP_REFRESERVATION;
981 *resv_prop = ZFS_PROP_RESERVATION;
987 * Given an nvlist of properties to set, validates that they are correct, and
988 * parses any numeric properties (index, boolean, etc) if they are specified as
992 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
993 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
994 boolean_t key_params_ok, const char *errbuf)
1001 int chosen_normal = -1;
1002 int chosen_utf = -1;
1004 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
1005 (void) no_memory(hdl);
1010 * Make sure this property is valid and applies to this type.
1014 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1015 const char *propname = nvpair_name(elem);
1017 prop = zfs_name_to_prop(propname);
1018 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
1020 * This is a user property: make sure it's a
1021 * string, and that it's less than ZAP_MAXNAMELEN.
1023 if (nvpair_type(elem) != DATA_TYPE_STRING) {
1024 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1025 "'%s' must be a string"), propname);
1026 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1030 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
1031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1032 "property name '%s' is too long"),
1034 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1038 (void) nvpair_value_string(elem, &strval);
1039 if (nvlist_add_string(ret, propname, strval) != 0) {
1040 (void) no_memory(hdl);
1047 * Currently, only user properties can be modified on
1050 if (type == ZFS_TYPE_SNAPSHOT) {
1051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1052 "this property can not be modified for snapshots"));
1053 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1057 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1058 zfs_userquota_prop_t uqtype;
1059 char *newpropname = NULL;
1065 if (userquota_propname_decode(propname, zoned,
1066 &uqtype, domain, sizeof (domain), &rid) != 0) {
1068 dgettext(TEXT_DOMAIN,
1069 "'%s' has an invalid user/group name"),
1071 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1075 if (uqtype != ZFS_PROP_USERQUOTA &&
1076 uqtype != ZFS_PROP_GROUPQUOTA &&
1077 uqtype != ZFS_PROP_USEROBJQUOTA &&
1078 uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1079 uqtype != ZFS_PROP_PROJECTQUOTA &&
1080 uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
1082 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1084 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1089 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1090 (void) nvpair_value_string(elem, &strval);
1091 if (strcmp(strval, "none") == 0) {
1093 } else if (zfs_nicestrtonum(hdl,
1094 strval, &intval) != 0) {
1095 (void) zfs_error(hdl,
1096 EZFS_BADPROP, errbuf);
1099 } else if (nvpair_type(elem) ==
1101 (void) nvpair_value_uint64(elem, &intval);
1103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1104 "use 'none' to disable "
1105 "{user|group|project}quota"));
1109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1110 "'%s' must be a number"), propname);
1111 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1116 * Encode the prop name as
1117 * userquota@<hex-rid>-domain, to make it easy
1118 * for the kernel to decode.
1120 rc = asprintf(&newpropname, "%s%llx-%s",
1121 zfs_userquota_prop_prefixes[uqtype],
1122 (longlong_t)rid, domain);
1123 if (rc == -1 || newpropname == NULL) {
1124 (void) no_memory(hdl);
1131 if (nvlist_add_uint64_array(ret, newpropname,
1134 (void) no_memory(hdl);
1139 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1141 "'%s' is readonly"),
1143 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1147 if (prop == ZPROP_INVAL) {
1148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 "invalid property '%s'"), propname);
1150 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1154 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1156 dgettext(TEXT_DOMAIN, "'%s' does not "
1157 "apply to datasets of this type"), propname);
1158 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1162 if (zfs_prop_readonly(prop) &&
1163 !(zfs_prop_setonce(prop) && zhp == NULL) &&
1164 !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1166 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1168 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1172 if (zprop_parse_value(hdl, elem, prop, type, ret,
1173 &strval, &intval, errbuf) != 0)
1177 * Perform some additional checks for specific properties.
1180 case ZFS_PROP_VERSION:
1186 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1187 if (intval < version) {
1188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1189 "Can not downgrade; already at version %u"),
1191 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1197 case ZFS_PROP_VOLBLOCKSIZE:
1198 case ZFS_PROP_RECORDSIZE:
1200 int maxbs = SPA_MAXBLOCKSIZE;
1203 if (zpool_hdl != NULL) {
1204 maxbs = zpool_get_prop_int(zpool_hdl,
1205 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1208 * The value must be a power of two between
1209 * SPA_MINBLOCKSIZE and maxbs.
1211 if (intval < SPA_MINBLOCKSIZE ||
1212 intval > maxbs || !ISP2(intval)) {
1213 zfs_nicebytes(maxbs, buf, sizeof (buf));
1214 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1215 "'%s' must be power of 2 from 512B "
1216 "to %s"), propname, buf);
1217 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1223 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1224 if (zpool_hdl != NULL) {
1225 char state[64] = "";
1228 * Issue a warning but do not fail so that
1229 * tests for setable properties succeed.
1231 if (zpool_prop_get_feature(zpool_hdl,
1232 "feature@allocation_classes", state,
1233 sizeof (state)) != 0 ||
1234 strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1235 (void) fprintf(stderr, gettext(
1236 "%s: property requires a special "
1237 "device in the pool\n"), propname);
1241 (intval < SPA_MINBLOCKSIZE ||
1242 intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) {
1243 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1244 "invalid '%s=%d' property: must be zero or "
1245 "a power of 2 from 512B to 128K"), propname,
1247 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1252 case ZFS_PROP_MLSLABEL:
1254 #ifdef HAVE_MLSLABEL
1256 * Verify the mlslabel string and convert to
1257 * internal hex label string.
1261 char *hex = NULL; /* internal label string */
1263 /* Default value is already OK. */
1264 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1267 /* Verify the label can be converted to binary form */
1268 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1269 (str_to_label(strval, &new_sl, MAC_LABEL,
1270 L_NO_CORRECTION, NULL) == -1)) {
1274 /* Now translate to hex internal label string */
1275 if (label_to_str(new_sl, &hex, M_INTERNAL,
1281 m_label_free(new_sl);
1283 /* If string is already in internal form, we're done. */
1284 if (strcmp(strval, hex) == 0) {
1289 /* Replace the label string with the internal form. */
1290 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1292 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1299 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1300 "invalid mlslabel '%s'"), strval);
1301 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1302 m_label_free(new_sl); /* OK if null */
1305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1306 "mlslabels are unsupported"));
1307 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1309 #endif /* HAVE_MLSLABEL */
1312 case ZFS_PROP_MOUNTPOINT:
1314 namecheck_err_t why;
1316 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1317 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1320 if (mountpoint_namecheck(strval, &why)) {
1322 case NAME_ERR_LEADING_SLASH:
1324 dgettext(TEXT_DOMAIN,
1325 "'%s' must be an absolute path, "
1326 "'none', or 'legacy'"), propname);
1328 case NAME_ERR_TOOLONG:
1330 dgettext(TEXT_DOMAIN,
1331 "component of '%s' is too long"),
1337 dgettext(TEXT_DOMAIN,
1338 "(%d) not defined"),
1342 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1349 case ZFS_PROP_SHARESMB:
1350 case ZFS_PROP_SHARENFS:
1352 * For the mountpoint and sharenfs or sharesmb
1353 * properties, check if it can be set in a
1354 * global/non-global zone based on
1355 * the zoned property value:
1357 * global zone non-global zone
1358 * --------------------------------------------------
1359 * zoned=on mountpoint (no) mountpoint (yes)
1360 * sharenfs (no) sharenfs (no)
1361 * sharesmb (no) sharesmb (no)
1363 * zoned=off mountpoint (yes) N/A
1368 if (getzoneid() == GLOBAL_ZONEID) {
1369 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1370 "'%s' cannot be set on "
1371 "dataset in a non-global zone"),
1373 (void) zfs_error(hdl, EZFS_ZONED,
1376 } else if (prop == ZFS_PROP_SHARENFS ||
1377 prop == ZFS_PROP_SHARESMB) {
1378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1379 "'%s' cannot be set in "
1380 "a non-global zone"), propname);
1381 (void) zfs_error(hdl, EZFS_ZONED,
1385 } else if (getzoneid() != GLOBAL_ZONEID) {
1387 * If zoned property is 'off', this must be in
1388 * a global zone. If not, something is wrong.
1390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1391 "'%s' cannot be set while dataset "
1392 "'zoned' property is set"), propname);
1393 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1398 * At this point, it is legitimate to set the
1399 * property. Now we want to make sure that the
1400 * property value is valid if it is sharenfs.
1402 if ((prop == ZFS_PROP_SHARENFS ||
1403 prop == ZFS_PROP_SHARESMB) &&
1404 strcmp(strval, "on") != 0 &&
1405 strcmp(strval, "off") != 0) {
1406 zfs_share_proto_t proto;
1408 if (prop == ZFS_PROP_SHARESMB)
1414 * Must be an valid sharing protocol
1415 * option string so init the libshare
1416 * in order to enable the parser and
1417 * then parse the options. We use the
1418 * control API since we don't care about
1419 * the current configuration and don't
1420 * want the overhead of loading it
1421 * until we actually do something.
1424 if (zfs_init_libshare(hdl,
1425 SA_INIT_CONTROL_API) != SA_OK) {
1427 * An error occurred so we can't do
1430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1431 "'%s' cannot be set: problem "
1432 "in share initialization"),
1434 (void) zfs_error(hdl, EZFS_BADPROP,
1439 if (zfs_parse_options(strval, proto) != SA_OK) {
1441 * There was an error in parsing so
1442 * deal with it by issuing an error
1443 * message and leaving after
1444 * uninitializing the the libshare
1447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1448 "'%s' cannot be set to invalid "
1449 "options"), propname);
1450 (void) zfs_error(hdl, EZFS_BADPROP,
1452 zfs_uninit_libshare(hdl);
1455 zfs_uninit_libshare(hdl);
1460 case ZFS_PROP_KEYLOCATION:
1461 if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1463 "invalid keylocation"));
1464 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1470 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1472 if (crypt == ZIO_CRYPT_OFF &&
1473 strcmp(strval, "none") != 0) {
1474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475 "keylocation must be 'none' "
1476 "for unencrypted datasets"));
1477 (void) zfs_error(hdl, EZFS_BADPROP,
1480 } else if (crypt != ZIO_CRYPT_OFF &&
1481 strcmp(strval, "none") == 0) {
1482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1483 "keylocation must not be 'none' "
1484 "for encrypted datasets"));
1485 (void) zfs_error(hdl, EZFS_BADPROP,
1492 case ZFS_PROP_PBKDF2_ITERS:
1493 if (intval < MIN_PBKDF2_ITERATIONS) {
1494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1495 "minimum pbkdf2 iterations is %u"),
1496 MIN_PBKDF2_ITERATIONS);
1497 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1502 case ZFS_PROP_UTF8ONLY:
1503 chosen_utf = (int)intval;
1506 case ZFS_PROP_NORMALIZE:
1507 chosen_normal = (int)intval;
1515 * For changes to existing volumes, we have some additional
1516 * checks to enforce.
1518 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1519 uint64_t blocksize = zfs_prop_get_int(zhp,
1520 ZFS_PROP_VOLBLOCKSIZE);
1524 case ZFS_PROP_VOLSIZE:
1525 if (intval % blocksize != 0) {
1526 zfs_nicebytes(blocksize, buf,
1528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1529 "'%s' must be a multiple of "
1530 "volume block size (%s)"),
1532 (void) zfs_error(hdl, EZFS_BADPROP,
1538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1539 "'%s' cannot be zero"),
1541 (void) zfs_error(hdl, EZFS_BADPROP,
1552 /* check encryption properties */
1554 int64_t crypt = zfs_prop_get_int(zhp,
1555 ZFS_PROP_ENCRYPTION);
1558 case ZFS_PROP_COPIES:
1559 if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1561 "encrypted datasets cannot have "
1563 (void) zfs_error(hdl, EZFS_BADPROP,
1575 * If normalization was chosen, but no UTF8 choice was made,
1576 * enforce rejection of non-UTF8 names.
1578 * If normalization was chosen, but rejecting non-UTF8 names
1579 * was explicitly not chosen, it is an error.
1581 if (chosen_normal > 0 && chosen_utf < 0) {
1582 if (nvlist_add_uint64(ret,
1583 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1584 (void) no_memory(hdl);
1587 } else if (chosen_normal > 0 && chosen_utf == 0) {
1588 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1589 "'%s' must be set 'on' if normalization chosen"),
1590 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1591 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1602 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1604 uint64_t old_volsize;
1605 uint64_t new_volsize;
1606 uint64_t old_reservation;
1607 uint64_t new_reservation;
1608 zfs_prop_t resv_prop;
1612 * If this is an existing volume, and someone is setting the volsize,
1613 * make sure that it matches the reservation, or add it if necessary.
1615 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1616 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1618 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1620 props = fnvlist_alloc();
1621 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1622 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1624 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1625 old_reservation) || nvlist_exists(nvl,
1626 zfs_prop_to_name(resv_prop))) {
1627 fnvlist_free(props);
1630 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1631 &new_volsize) != 0) {
1632 fnvlist_free(props);
1635 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1636 fnvlist_free(props);
1638 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1639 new_reservation) != 0) {
1640 (void) no_memory(zhp->zfs_hdl);
1647 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1648 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1649 * Return codes must match zfs_add_synthetic_resv().
1652 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1659 if (!ZFS_IS_VOLUME(zhp)) {
1663 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1667 if (prop != ZFS_PROP_REFRESERVATION) {
1671 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1672 /* No value being set, so it can't be "auto" */
1675 if (resvsize != UINT64_MAX) {
1676 /* Being set to a value other than "auto" */
1680 props = fnvlist_alloc();
1682 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1683 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1685 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1687 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1690 resvsize = zvol_volsize_to_reservation(volsize, props);
1691 fnvlist_free(props);
1693 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1694 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1695 (void) no_memory(zhp->zfs_hdl);
1702 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1709 * For quotas and reservations, ENOSPC indicates
1710 * something different; setting a quota or reservation
1711 * doesn't use any disk space.
1714 case ZFS_PROP_QUOTA:
1715 case ZFS_PROP_REFQUOTA:
1716 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1717 "size is less than current used or "
1719 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1722 case ZFS_PROP_RESERVATION:
1723 case ZFS_PROP_REFRESERVATION:
1724 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1725 "size is greater than available space"));
1726 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1730 (void) zfs_standard_error(hdl, err, errbuf);
1736 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1740 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1745 "property value too long"));
1746 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1751 "pool and or dataset must be upgraded to set this "
1752 "property or value"));
1753 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1757 if (prop == ZFS_PROP_COMPRESSION ||
1758 prop == ZFS_PROP_DNODESIZE ||
1759 prop == ZFS_PROP_RECORDSIZE) {
1760 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1761 "property setting is not allowed on "
1762 "bootable datasets"));
1763 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1764 } else if (prop == ZFS_PROP_CHECKSUM ||
1765 prop == ZFS_PROP_DEDUP) {
1766 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1767 "property setting is not allowed on "
1769 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1771 (void) zfs_standard_error(hdl, err, errbuf);
1776 if (prop == ZPROP_INVAL) {
1777 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1779 (void) zfs_standard_error(hdl, err, errbuf);
1784 if (prop == ZFS_PROP_KEYLOCATION) {
1785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1786 "keylocation may only be set on encryption roots"));
1787 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1789 (void) zfs_standard_error(hdl, err, errbuf);
1795 * This platform can't address a volume this big.
1798 if (prop == ZFS_PROP_VOLSIZE) {
1799 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1805 (void) zfs_standard_error(hdl, err, errbuf);
1810 zfs_is_namespace_prop(zfs_prop_t prop)
1814 case ZFS_PROP_ATIME:
1815 case ZFS_PROP_RELATIME:
1816 case ZFS_PROP_DEVICES:
1818 case ZFS_PROP_SETUID:
1819 case ZFS_PROP_READONLY:
1820 case ZFS_PROP_XATTR:
1821 case ZFS_PROP_NBMAND:
1830 * Given a property name and value, set the property for the given dataset.
1833 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1837 libzfs_handle_t *hdl = zhp->zfs_hdl;
1838 nvlist_t *nvl = NULL;
1840 (void) snprintf(errbuf, sizeof (errbuf),
1841 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1844 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1845 nvlist_add_string(nvl, propname, propval) != 0) {
1846 (void) no_memory(hdl);
1850 ret = zfs_prop_set_list(zhp, nvl);
1860 * Given an nvlist of property names and values, set the properties for the
1864 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1866 zfs_cmd_t zc = {"\0"};
1868 prop_changelist_t **cls = NULL;
1871 libzfs_handle_t *hdl = zhp->zfs_hdl;
1875 zfs_prop_t prop = 0;
1878 (void) snprintf(errbuf, sizeof (errbuf),
1879 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1882 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1883 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1884 B_FALSE, errbuf)) == NULL)
1888 * We have to check for any extra properties which need to be added
1889 * before computing the length of the nvlist.
1891 for (elem = nvlist_next_nvpair(nvl, NULL);
1893 elem = nvlist_next_nvpair(nvl, elem)) {
1894 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1895 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1900 if (added_resv != 1 &&
1901 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1906 * Check how many properties we're setting and allocate an array to
1907 * store changelist pointers for postfix().
1909 for (elem = nvlist_next_nvpair(nvl, NULL);
1911 elem = nvlist_next_nvpair(nvl, elem))
1913 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1917 for (elem = nvlist_next_nvpair(nvl, NULL);
1919 elem = nvlist_next_nvpair(nvl, elem)) {
1921 prop = zfs_name_to_prop(nvpair_name(elem));
1923 assert(cl_idx < nvl_len);
1925 * We don't want to unmount & remount the dataset when changing
1926 * its canmount property to 'on' or 'noauto'. We only use
1927 * the changelist logic to unmount when setting canmount=off.
1929 if (prop != ZFS_PROP_CANMOUNT ||
1930 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1931 zfs_is_mounted(zhp, NULL))) {
1932 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1933 if (cls[cl_idx] == NULL)
1937 if (prop == ZFS_PROP_MOUNTPOINT &&
1938 changelist_haszonedchild(cls[cl_idx])) {
1939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1940 "child dataset with inherited mountpoint is used "
1941 "in a non-global zone"));
1942 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1946 if (cls[cl_idx] != NULL &&
1947 (ret = changelist_prefix(cls[cl_idx])) != 0)
1952 assert(cl_idx == nvl_len);
1955 * Execute the corresponding ioctl() to set this list of properties.
1957 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1959 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1960 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1963 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1966 if (zc.zc_nvlist_dst_filled == B_FALSE) {
1967 (void) zfs_standard_error(hdl, errno, errbuf);
1971 /* Get the list of unset properties back and report them. */
1972 nvlist_t *errorprops = NULL;
1973 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1975 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1977 elem = nvlist_next_nvpair(errorprops, elem)) {
1978 prop = zfs_name_to_prop(nvpair_name(elem));
1979 zfs_setprop_error(hdl, prop, errno, errbuf);
1981 nvlist_free(errorprops);
1983 if (added_resv && errno == ENOSPC) {
1984 /* clean up the volsize property we tried to set */
1985 uint64_t old_volsize = zfs_prop_get_int(zhp,
1989 zcmd_free_nvlists(&zc);
1991 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1993 if (nvlist_add_uint64(nvl,
1994 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1997 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1999 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
2002 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
2003 if (cls[cl_idx] != NULL) {
2004 int clp_err = changelist_postfix(cls[cl_idx]);
2012 * Refresh the statistics so the new property
2013 * value is reflected.
2015 (void) get_stats(zhp);
2018 * Remount the filesystem to propagate the change
2019 * if one of the options handled by the generic
2020 * Linux namespace layer has been modified.
2022 if (zfs_is_namespace_prop(prop) &&
2023 zfs_is_mounted(zhp, NULL))
2024 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
2030 zcmd_free_nvlists(&zc);
2032 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
2033 if (cls[cl_idx] != NULL)
2034 changelist_free(cls[cl_idx]);
2042 * Given a property, inherit the value from the parent dataset, or if received
2043 * is TRUE, revert to the received value, if any.
2046 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
2048 zfs_cmd_t zc = {"\0"};
2050 prop_changelist_t *cl;
2051 libzfs_handle_t *hdl = zhp->zfs_hdl;
2055 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2056 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2058 zc.zc_cookie = received;
2059 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
2061 * For user properties, the amount of work we have to do is very
2062 * small, so just do it here.
2064 if (!zfs_prop_user(propname)) {
2065 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2066 "invalid property"));
2067 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2070 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2071 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2073 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
2074 return (zfs_standard_error(hdl, errno, errbuf));
2080 * Verify that this property is inheritable.
2082 if (zfs_prop_readonly(prop))
2083 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
2085 if (!zfs_prop_inheritable(prop) && !received)
2086 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2089 * Check to see if the value applies to this type
2091 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2092 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2095 * Normalize the name, to get rid of shorthand abbreviations.
2097 propname = zfs_prop_to_name(prop);
2098 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2099 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2101 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2102 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
2103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2104 "dataset is used in a non-global zone"));
2105 return (zfs_error(hdl, EZFS_ZONED, errbuf));
2109 * Determine datasets which will be affected by this change, if any.
2111 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2114 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
2115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2116 "child dataset with inherited mountpoint is used "
2117 "in a non-global zone"));
2118 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2122 if ((ret = changelist_prefix(cl)) != 0)
2125 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
2126 return (zfs_standard_error(hdl, errno, errbuf));
2129 if ((ret = changelist_postfix(cl)) != 0)
2133 * Refresh the statistics so the new property is reflected.
2135 (void) get_stats(zhp);
2138 * Remount the filesystem to propagate the change
2139 * if one of the options handled by the generic
2140 * Linux namespace layer has been modified.
2142 if (zfs_is_namespace_prop(prop) &&
2143 zfs_is_mounted(zhp, NULL))
2144 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
2148 changelist_free(cl);
2153 * True DSL properties are stored in an nvlist. The following two functions
2154 * extract them appropriately.
2157 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2163 if (nvlist_lookup_nvlist(zhp->zfs_props,
2164 zfs_prop_to_name(prop), &nv) == 0) {
2165 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2166 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2168 verify(!zhp->zfs_props_table ||
2169 zhp->zfs_props_table[prop] == B_TRUE);
2170 value = zfs_prop_default_numeric(prop);
2178 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2184 if (nvlist_lookup_nvlist(zhp->zfs_props,
2185 zfs_prop_to_name(prop), &nv) == 0) {
2186 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2187 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2189 verify(!zhp->zfs_props_table ||
2190 zhp->zfs_props_table[prop] == B_TRUE);
2191 value = zfs_prop_default_string(prop);
2199 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2201 return (zhp->zfs_props == zhp->zfs_recvd_props);
2205 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2207 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2208 zhp->zfs_props = zhp->zfs_recvd_props;
2212 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2214 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2219 * Internal function for getting a numeric property. Both zfs_prop_get() and
2220 * zfs_prop_get_int() are built using this interface.
2222 * Certain properties can be overridden using 'mount -o'. In this case, scan
2223 * the contents of the /proc/self/mounts entry, searching for the
2224 * appropriate options. If they differ from the on-disk values, report the
2225 * current values and mark the source "temporary".
2228 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2229 char **source, uint64_t *val)
2231 zfs_cmd_t zc = {"\0"};
2232 nvlist_t *zplprops = NULL;
2234 char *mntopt_on = NULL;
2235 char *mntopt_off = NULL;
2236 boolean_t received = zfs_is_recvd_props_mode(zhp);
2241 * If the property is being fetched for a snapshot, check whether
2242 * the property is valid for the snapshot's head dataset type.
2244 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
2245 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
2246 *val = zfs_prop_default_numeric(prop);
2251 case ZFS_PROP_ATIME:
2252 mntopt_on = MNTOPT_ATIME;
2253 mntopt_off = MNTOPT_NOATIME;
2256 case ZFS_PROP_RELATIME:
2257 mntopt_on = MNTOPT_RELATIME;
2258 mntopt_off = MNTOPT_NORELATIME;
2261 case ZFS_PROP_DEVICES:
2262 mntopt_on = MNTOPT_DEVICES;
2263 mntopt_off = MNTOPT_NODEVICES;
2267 mntopt_on = MNTOPT_EXEC;
2268 mntopt_off = MNTOPT_NOEXEC;
2271 case ZFS_PROP_READONLY:
2272 mntopt_on = MNTOPT_RO;
2273 mntopt_off = MNTOPT_RW;
2276 case ZFS_PROP_SETUID:
2277 mntopt_on = MNTOPT_SETUID;
2278 mntopt_off = MNTOPT_NOSETUID;
2281 case ZFS_PROP_XATTR:
2282 mntopt_on = MNTOPT_XATTR;
2283 mntopt_off = MNTOPT_NOXATTR;
2286 case ZFS_PROP_NBMAND:
2287 mntopt_on = MNTOPT_NBMAND;
2288 mntopt_off = MNTOPT_NONBMAND;
2296 * Because looking up the mount options is potentially expensive
2297 * (iterating over all of /proc/self/mounts), we defer its
2298 * calculation until we're looking up a property which requires
2301 if (!zhp->zfs_mntcheck &&
2302 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2303 libzfs_handle_t *hdl = zhp->zfs_hdl;
2304 struct mnttab entry;
2306 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2307 zhp->zfs_mntopts = zfs_strdup(hdl,
2309 if (zhp->zfs_mntopts == NULL)
2313 zhp->zfs_mntcheck = B_TRUE;
2316 if (zhp->zfs_mntopts == NULL)
2317 mnt.mnt_mntopts = "";
2319 mnt.mnt_mntopts = zhp->zfs_mntopts;
2322 case ZFS_PROP_ATIME:
2323 case ZFS_PROP_RELATIME:
2324 case ZFS_PROP_DEVICES:
2326 case ZFS_PROP_READONLY:
2327 case ZFS_PROP_SETUID:
2328 case ZFS_PROP_XATTR:
2329 case ZFS_PROP_NBMAND:
2330 *val = getprop_uint64(zhp, prop, source);
2335 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2338 *src = ZPROP_SRC_TEMPORARY;
2339 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2342 *src = ZPROP_SRC_TEMPORARY;
2346 case ZFS_PROP_CANMOUNT:
2347 case ZFS_PROP_VOLSIZE:
2348 case ZFS_PROP_QUOTA:
2349 case ZFS_PROP_REFQUOTA:
2350 case ZFS_PROP_RESERVATION:
2351 case ZFS_PROP_REFRESERVATION:
2352 case ZFS_PROP_FILESYSTEM_LIMIT:
2353 case ZFS_PROP_SNAPSHOT_LIMIT:
2354 case ZFS_PROP_FILESYSTEM_COUNT:
2355 case ZFS_PROP_SNAPSHOT_COUNT:
2356 *val = getprop_uint64(zhp, prop, source);
2358 if (*source == NULL) {
2359 /* not default, must be local */
2360 *source = zhp->zfs_name;
2364 case ZFS_PROP_MOUNTED:
2365 *val = (zhp->zfs_mntopts != NULL);
2368 case ZFS_PROP_NUMCLONES:
2369 *val = zhp->zfs_dmustats.dds_num_clones;
2372 case ZFS_PROP_VERSION:
2373 case ZFS_PROP_NORMALIZE:
2374 case ZFS_PROP_UTF8ONLY:
2376 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2378 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2379 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2380 zcmd_free_nvlists(&zc);
2381 if (prop == ZFS_PROP_VERSION &&
2382 zhp->zfs_type == ZFS_TYPE_VOLUME)
2383 *val = zfs_prop_default_numeric(prop);
2386 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2387 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2389 zcmd_free_nvlists(&zc);
2392 nvlist_free(zplprops);
2393 zcmd_free_nvlists(&zc);
2396 case ZFS_PROP_INCONSISTENT:
2397 *val = zhp->zfs_dmustats.dds_inconsistent;
2401 switch (zfs_prop_get_type(prop)) {
2402 case PROP_TYPE_NUMBER:
2403 case PROP_TYPE_INDEX:
2404 *val = getprop_uint64(zhp, prop, source);
2406 * If we tried to use a default value for a
2407 * readonly property, it means that it was not
2408 * present. Note this only applies to "truly"
2409 * readonly properties, not set-once properties
2410 * like volblocksize.
2412 if (zfs_prop_readonly(prop) &&
2413 !zfs_prop_setonce(prop) &&
2414 *source != NULL && (*source)[0] == '\0') {
2420 case PROP_TYPE_STRING:
2422 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2423 "cannot get non-numeric property"));
2424 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2425 dgettext(TEXT_DOMAIN, "internal error")));
2433 * Calculate the source type, given the raw source string.
2436 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2437 char *statbuf, size_t statlen)
2439 if (statbuf == NULL ||
2440 srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) {
2444 if (source == NULL) {
2445 *srctype = ZPROP_SRC_NONE;
2446 } else if (source[0] == '\0') {
2447 *srctype = ZPROP_SRC_DEFAULT;
2448 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2449 *srctype = ZPROP_SRC_RECEIVED;
2451 if (strcmp(source, zhp->zfs_name) == 0) {
2452 *srctype = ZPROP_SRC_LOCAL;
2454 (void) strlcpy(statbuf, source, statlen);
2455 *srctype = ZPROP_SRC_INHERITED;
2462 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2463 size_t proplen, boolean_t literal)
2468 if (zhp->zfs_recvd_props == NULL)
2469 if (get_recvd_props_ioctl(zhp) != 0)
2472 prop = zfs_name_to_prop(propname);
2474 if (prop != ZPROP_INVAL) {
2476 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2478 zfs_set_recvd_props_mode(zhp, &cookie);
2479 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2480 NULL, NULL, 0, literal);
2481 zfs_unset_recvd_props_mode(zhp, &cookie);
2485 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2486 propname, &propval) != 0)
2488 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2490 (void) strlcpy(propbuf, recvdval, proplen);
2493 return (err == 0 ? 0 : -1);
2497 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2502 value = zfs_get_clones_nvl(zhp);
2507 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2508 pair = nvlist_next_nvpair(value, pair)) {
2509 if (propbuf[0] != '\0')
2510 (void) strlcat(propbuf, ",", proplen);
2511 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2517 struct get_clones_arg {
2521 char buf[ZFS_MAX_DATASET_NAME_LEN];
2525 get_clones_cb(zfs_handle_t *zhp, void *arg)
2527 struct get_clones_arg *gca = arg;
2529 if (gca->numclones == 0) {
2534 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2535 NULL, NULL, 0, B_TRUE) != 0)
2537 if (strcmp(gca->buf, gca->origin) == 0) {
2538 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2543 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2549 zfs_get_clones_nvl(zfs_handle_t *zhp)
2551 nvlist_t *nv, *value;
2553 if (nvlist_lookup_nvlist(zhp->zfs_props,
2554 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2555 struct get_clones_arg gca;
2558 * if this is a snapshot, then the kernel wasn't able
2559 * to get the clones. Do it by slowly iterating.
2561 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2563 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2565 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2570 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2572 gca.origin = zhp->zfs_name;
2574 if (gca.numclones != 0) {
2576 char pool[ZFS_MAX_DATASET_NAME_LEN];
2579 /* get the pool name */
2580 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2581 (void) strsep(&cp, "/@");
2582 root = zfs_open(zhp->zfs_hdl, pool,
2583 ZFS_TYPE_FILESYSTEM);
2590 (void) get_clones_cb(root, &gca);
2593 if (gca.numclones != 0 ||
2594 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2595 nvlist_add_nvlist(zhp->zfs_props,
2596 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2603 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2604 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2607 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2613 * Accepts a property and value and checks that the value
2614 * matches the one found by the channel program. If they are
2615 * not equal, print both of them.
2618 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2621 if (!zhp->zfs_hdl->libzfs_prop_debug)
2624 char *poolname = zhp->zpool_hdl->zpool_name;
2625 const char *prop_name = zfs_prop_to_name(prop);
2626 const char *program =
2628 "ds = args['dataset']\n"
2629 "prop = args['property']\n"
2630 "value, setpoint = zfs.get_prop(ds, prop)\n"
2631 "return {value=value, setpoint=setpoint}\n";
2634 nvlist_t *argnvl = fnvlist_alloc();
2636 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2637 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2639 error = lzc_channel_program_nosync(poolname, program,
2640 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2643 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2644 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2646 error = nvlist_lookup_int64(retnvl, "value", &ans);
2648 (void) fprintf(stderr, "%s: zcp check error: "
2649 "%u\n", prop_name, error);
2652 if (ans != intval) {
2653 (void) fprintf(stderr, "%s: zfs found %llu, "
2654 "but zcp found %llu\n", prop_name,
2655 (u_longlong_t)intval, (u_longlong_t)ans);
2659 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2661 (void) fprintf(stderr, "%s: zcp check error: "
2662 "%u\n", prop_name, error);
2665 if (strcmp(strval, str_ans) != 0) {
2666 (void) fprintf(stderr,
2667 "%s: zfs found '%s', but zcp found '%s'\n",
2668 prop_name, strval, str_ans);
2672 (void) fprintf(stderr, "%s: zcp check failed, channel program "
2673 "error: %u\n", prop_name, error);
2675 nvlist_free(argnvl);
2676 nvlist_free(outnvl);
2680 * Retrieve a property from the given object. If 'literal' is specified, then
2681 * numbers are left as exact values. Otherwise, numbers are converted to a
2682 * human-readable form.
2684 * Returns 0 on success, or -1 on error.
2687 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2688 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2690 char *source = NULL;
2694 boolean_t received = zfs_is_recvd_props_mode(zhp);
2697 * Check to see if this property applies to our object
2699 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2702 if (received && zfs_prop_readonly(prop))
2706 *src = ZPROP_SRC_NONE;
2709 case ZFS_PROP_CREATION:
2711 * 'creation' is a time_t stored in the statistics. We convert
2712 * this into a string unless 'literal' is specified.
2715 val = getprop_uint64(zhp, prop, &source);
2716 time_t time = (time_t)val;
2720 localtime_r(&time, &t) == NULL ||
2721 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2723 (void) snprintf(propbuf, proplen, "%llu",
2726 zcp_check(zhp, prop, val, NULL);
2729 case ZFS_PROP_MOUNTPOINT:
2731 * Getting the precise mountpoint can be tricky.
2733 * - for 'none' or 'legacy', return those values.
2734 * - for inherited mountpoints, we want to take everything
2735 * after our ancestor and append it to the inherited value.
2737 * If the pool has an alternate root, we want to prepend that
2738 * root to any values we return.
2741 str = getprop_string(zhp, prop, &source);
2743 if (str[0] == '/') {
2744 char buf[MAXPATHLEN];
2746 const char *relpath;
2749 * If we inherit the mountpoint, even from a dataset
2750 * with a received value, the source will be the path of
2751 * the dataset we inherit from. If source is
2752 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2755 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2758 relpath = zhp->zfs_name + strlen(source);
2759 if (relpath[0] == '/')
2763 if ((zpool_get_prop(zhp->zpool_hdl,
2764 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2765 B_FALSE)) || (strcmp(root, "-") == 0))
2768 * Special case an alternate root of '/'. This will
2769 * avoid having multiple leading slashes in the
2772 if (strcmp(root, "/") == 0)
2776 * If the mountpoint is '/' then skip over this
2777 * if we are obtaining either an alternate root or
2778 * an inherited mountpoint.
2780 if (str[1] == '\0' && (root[0] != '\0' ||
2781 relpath[0] != '\0'))
2784 if (relpath[0] == '\0')
2785 (void) snprintf(propbuf, proplen, "%s%s",
2788 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2789 root, str, relpath[0] == '@' ? "" : "/",
2792 /* 'legacy' or 'none' */
2793 (void) strlcpy(propbuf, str, proplen);
2795 zcp_check(zhp, prop, 0, propbuf);
2798 case ZFS_PROP_ORIGIN:
2799 str = getprop_string(zhp, prop, &source);
2802 (void) strlcpy(propbuf, str, proplen);
2803 zcp_check(zhp, prop, 0, str);
2806 case ZFS_PROP_CLONES:
2807 if (get_clones_string(zhp, propbuf, proplen) != 0)
2811 case ZFS_PROP_QUOTA:
2812 case ZFS_PROP_REFQUOTA:
2813 case ZFS_PROP_RESERVATION:
2814 case ZFS_PROP_REFRESERVATION:
2816 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2819 * If quota or reservation is 0, we translate this into 'none'
2820 * (unless literal is set), and indicate that it's the default
2821 * value. Otherwise, we print the number nicely and indicate
2822 * that its set locally.
2826 (void) strlcpy(propbuf, "0", proplen);
2828 (void) strlcpy(propbuf, "none", proplen);
2831 (void) snprintf(propbuf, proplen, "%llu",
2834 zfs_nicebytes(val, propbuf, proplen);
2836 zcp_check(zhp, prop, val, NULL);
2839 case ZFS_PROP_FILESYSTEM_LIMIT:
2840 case ZFS_PROP_SNAPSHOT_LIMIT:
2841 case ZFS_PROP_FILESYSTEM_COUNT:
2842 case ZFS_PROP_SNAPSHOT_COUNT:
2844 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2848 * If limit is UINT64_MAX, we translate this into 'none' (unless
2849 * literal is set), and indicate that it's the default value.
2850 * Otherwise, we print the number nicely and indicate that it's
2854 (void) snprintf(propbuf, proplen, "%llu",
2856 } else if (val == UINT64_MAX) {
2857 (void) strlcpy(propbuf, "none", proplen);
2859 zfs_nicenum(val, propbuf, proplen);
2862 zcp_check(zhp, prop, val, NULL);
2865 case ZFS_PROP_REFRATIO:
2866 case ZFS_PROP_COMPRESSRATIO:
2867 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2870 (void) snprintf(propbuf, proplen, "%llu.%02llu",
2871 (u_longlong_t)(val / 100),
2872 (u_longlong_t)(val % 100));
2874 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2875 (u_longlong_t)(val / 100),
2876 (u_longlong_t)(val % 100));
2877 zcp_check(zhp, prop, val, NULL);
2881 switch (zhp->zfs_type) {
2882 case ZFS_TYPE_FILESYSTEM:
2885 case ZFS_TYPE_VOLUME:
2888 case ZFS_TYPE_SNAPSHOT:
2891 case ZFS_TYPE_BOOKMARK:
2897 (void) snprintf(propbuf, proplen, "%s", str);
2898 zcp_check(zhp, prop, 0, propbuf);
2901 case ZFS_PROP_MOUNTED:
2903 * The 'mounted' property is a pseudo-property that described
2904 * whether the filesystem is currently mounted. Even though
2905 * it's a boolean value, the typical values of "on" and "off"
2906 * don't make sense, so we translate to "yes" and "no".
2908 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2909 src, &source, &val) != 0)
2912 (void) strlcpy(propbuf, "yes", proplen);
2914 (void) strlcpy(propbuf, "no", proplen);
2919 * The 'name' property is a pseudo-property derived from the
2920 * dataset name. It is presented as a real property to simplify
2923 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2924 zcp_check(zhp, prop, 0, propbuf);
2927 case ZFS_PROP_MLSLABEL:
2929 #ifdef HAVE_MLSLABEL
2930 m_label_t *new_sl = NULL;
2931 char *ascii = NULL; /* human readable label */
2933 (void) strlcpy(propbuf,
2934 getprop_string(zhp, prop, &source), proplen);
2936 if (literal || (strcasecmp(propbuf,
2937 ZFS_MLSLABEL_DEFAULT) == 0))
2941 * Try to translate the internal hex string to
2942 * human-readable output. If there are any
2943 * problems just use the hex string.
2946 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2947 L_NO_CORRECTION, NULL) == -1) {
2948 m_label_free(new_sl);
2952 if (label_to_str(new_sl, &ascii, M_LABEL,
2956 m_label_free(new_sl);
2959 m_label_free(new_sl);
2961 (void) strlcpy(propbuf, ascii, proplen);
2964 (void) strlcpy(propbuf,
2965 getprop_string(zhp, prop, &source), proplen);
2966 #endif /* HAVE_MLSLABEL */
2971 case ZFS_PROP_CREATETXG:
2973 * GUIDs are stored as numbers, but they are identifiers.
2974 * We don't want them to be pretty printed, because pretty
2975 * printing mangles the ID into a truncated and useless value.
2977 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2979 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2980 zcp_check(zhp, prop, val, NULL);
2983 case ZFS_PROP_REFERENCED:
2984 case ZFS_PROP_AVAILABLE:
2986 case ZFS_PROP_USEDSNAP:
2987 case ZFS_PROP_USEDDS:
2988 case ZFS_PROP_USEDREFRESERV:
2989 case ZFS_PROP_USEDCHILD:
2990 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2993 (void) snprintf(propbuf, proplen, "%llu",
2996 zfs_nicebytes(val, propbuf, proplen);
2998 zcp_check(zhp, prop, val, NULL);
3002 switch (zfs_prop_get_type(prop)) {
3003 case PROP_TYPE_NUMBER:
3004 if (get_numeric_property(zhp, prop, src,
3005 &source, &val) != 0) {
3010 (void) snprintf(propbuf, proplen, "%llu",
3013 zfs_nicenum(val, propbuf, proplen);
3015 zcp_check(zhp, prop, val, NULL);
3018 case PROP_TYPE_STRING:
3019 str = getprop_string(zhp, prop, &source);
3023 (void) strlcpy(propbuf, str, proplen);
3024 zcp_check(zhp, prop, 0, str);
3027 case PROP_TYPE_INDEX:
3028 if (get_numeric_property(zhp, prop, src,
3029 &source, &val) != 0)
3031 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
3034 (void) strlcpy(propbuf, strval, proplen);
3035 zcp_check(zhp, prop, 0, strval);
3043 get_source(zhp, src, source, statbuf, statlen);
3049 * Utility function to get the given numeric property. Does no validation that
3050 * the given property is the appropriate type; should only be used with
3051 * hard-coded property types.
3054 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
3059 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
3065 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
3069 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
3070 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
3074 * Similar to zfs_prop_get(), but returns the value as an integer.
3077 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
3078 zprop_source_t *src, char *statbuf, size_t statlen)
3083 * Check to see if this property applies to our object
3085 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
3086 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
3087 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
3088 zfs_prop_to_name(prop)));
3092 *src = ZPROP_SRC_NONE;
3094 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
3097 get_source(zhp, src, source, statbuf, statlen);
3104 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
3105 char **domainp, idmap_rid_t *ridp)
3107 idmap_get_handle_t *get_hdl = NULL;
3111 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
3115 err = idmap_get_sidbyuid(get_hdl, id,
3116 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3118 err = idmap_get_sidbygid(get_hdl, id,
3119 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3121 if (err == IDMAP_SUCCESS &&
3122 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
3123 status == IDMAP_SUCCESS)
3129 idmap_get_destroy(get_hdl);
3132 #endif /* HAVE_IDMAP */
3135 * convert the propname into parameters needed by kernel
3136 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3137 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3138 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3139 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3140 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3141 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3144 userquota_propname_decode(const char *propname, boolean_t zoned,
3145 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
3147 zfs_userquota_prop_t type;
3151 boolean_t isproject;
3157 /* Figure out the property type ({user|group|project}{quota|space}) */
3158 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
3159 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
3160 strlen(zfs_userquota_prop_prefixes[type])) == 0)
3163 if (type == ZFS_NUM_USERQUOTA_PROPS)
3167 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3168 type == ZFS_PROP_USEROBJQUOTA ||
3169 type == ZFS_PROP_USEROBJUSED);
3170 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3171 type == ZFS_PROP_GROUPOBJQUOTA ||
3172 type == ZFS_PROP_GROUPOBJUSED);
3173 isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3174 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3175 type == ZFS_PROP_PROJECTOBJUSED);
3177 cp = strchr(propname, '@') + 1;
3179 if (isuser && (pw = getpwnam(cp)) != NULL) {
3180 if (zoned && getzoneid() == GLOBAL_ZONEID)
3183 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3184 if (zoned && getzoneid() == GLOBAL_ZONEID)
3187 } else if (!isproject && strchr(cp, '@')) {
3190 * It's a SID name (eg "user@domain") that needs to be
3191 * turned into S-1-domainID-RID.
3193 directory_error_t e;
3194 char *numericsid = NULL;
3197 if (zoned && getzoneid() == GLOBAL_ZONEID)
3200 e = directory_sid_from_user_name(NULL,
3203 e = directory_sid_from_group_name(NULL,
3207 directory_error_free(e);
3210 if (numericsid == NULL)
3213 (void) strlcpy(domain, cp, domainlen);
3214 cp = strrchr(domain, '-');
3219 *ridp = strtoull(cp, &end, 10);
3222 if (errno != 0 || *end != '\0')
3226 #endif /* HAVE_IDMAP */
3228 /* It's a user/group/project ID (eg "12345"). */
3231 id = strtoul(cp, &end, 10);
3234 if (id > MAXUID && !isproject) {
3236 /* It's an ephemeral ID. */
3240 if (idmap_id_to_numeric_domain_rid(id, isuser,
3241 &mapdomain, &rid) != 0)
3243 (void) strlcpy(domain, mapdomain, domainlen);
3247 #endif /* HAVE_IDMAP */
3257 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3258 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3261 zfs_cmd_t zc = {"\0"};
3263 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3265 err = userquota_propname_decode(propname,
3266 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3267 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3268 zc.zc_objset_type = *typep;
3272 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3276 *propvalue = zc.zc_cookie;
3281 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3282 uint64_t *propvalue)
3284 zfs_userquota_prop_t type;
3286 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3291 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3292 char *propbuf, int proplen, boolean_t literal)
3296 zfs_userquota_prop_t type;
3298 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3305 (void) snprintf(propbuf, proplen, "%llu",
3306 (u_longlong_t)propvalue);
3307 } else if (propvalue == 0 &&
3308 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3309 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3310 type == ZFS_PROP_PROJECTQUOTA ||
3311 type == ZFS_PROP_PROJECTOBJQUOTA)) {
3312 (void) strlcpy(propbuf, "none", proplen);
3313 } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3314 type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3315 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3316 zfs_nicebytes(propvalue, propbuf, proplen);
3318 zfs_nicenum(propvalue, propbuf, proplen);
3324 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3325 uint64_t *propvalue)
3328 zfs_cmd_t zc = {"\0"};
3329 const char *snapname;
3331 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3333 snapname = strchr(propname, '@') + 1;
3334 if (strchr(snapname, '@')) {
3335 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3337 /* snapname is the short name, append it to zhp's fsname */
3340 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3341 sizeof (zc.zc_value));
3342 cp = strchr(zc.zc_value, '@');
3345 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3346 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3349 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3353 *propvalue = zc.zc_cookie;
3358 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3359 char *propbuf, int proplen, boolean_t literal)
3364 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3370 (void) snprintf(propbuf, proplen, "%llu",
3371 (u_longlong_t)propvalue);
3373 zfs_nicebytes(propvalue, propbuf, proplen);
3380 * Returns the name of the given zfs handle.
3383 zfs_get_name(const zfs_handle_t *zhp)
3385 return (zhp->zfs_name);
3389 * Returns the name of the parent pool for the given zfs handle.
3392 zfs_get_pool_name(const zfs_handle_t *zhp)
3394 return (zhp->zpool_hdl->zpool_name);
3398 * Returns the type of the given zfs handle.
3401 zfs_get_type(const zfs_handle_t *zhp)
3403 return (zhp->zfs_type);
3407 * Is one dataset name a child dataset of another?
3409 * Needs to handle these cases:
3410 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3411 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3412 * Descendant? No. No. No. Yes.
3415 is_descendant(const char *ds1, const char *ds2)
3417 size_t d1len = strlen(ds1);
3419 /* ds2 can't be a descendant if it's smaller */
3420 if (strlen(ds2) < d1len)
3423 /* otherwise, compare strings and verify that there's a '/' char */
3424 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3428 * Given a complete name, return just the portion that refers to the parent.
3429 * Will return -1 if there is no parent (path is just the name of the
3433 parent_name(const char *path, char *buf, size_t buflen)
3437 (void) strlcpy(buf, path, buflen);
3439 if ((slashp = strrchr(buf, '/')) == NULL)
3447 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3449 return (parent_name(zfs_get_name(zhp), buf, buflen));
3453 * If accept_ancestor is false, then check to make sure that the given path has
3454 * a parent, and that it exists. If accept_ancestor is true, then find the
3455 * closest existing ancestor for the given path. In prefixlen return the
3456 * length of already existing prefix of the given path. We also fetch the
3457 * 'zoned' property, which is used to validate property settings when creating
3461 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3462 boolean_t accept_ancestor, int *prefixlen)
3464 zfs_cmd_t zc = {"\0"};
3465 char parent[ZFS_MAX_DATASET_NAME_LEN];
3471 (void) snprintf(errbuf, sizeof (errbuf),
3472 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3474 /* get parent, and check to see if this is just a pool */
3475 if (parent_name(path, parent, sizeof (parent)) != 0) {
3476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3477 "missing dataset name"));
3478 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3481 /* check to see if the pool exists */
3482 if ((slash = strchr(parent, '/')) == NULL)
3483 slash = parent + strlen(parent);
3484 (void) strncpy(zc.zc_name, parent, slash - parent);
3485 zc.zc_name[slash - parent] = '\0';
3486 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3489 "no such pool '%s'"), zc.zc_name);
3490 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3493 /* check to see if the parent dataset exists */
3494 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3495 if (errno == ENOENT && accept_ancestor) {
3497 * Go deeper to find an ancestor, give up on top level.
3499 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3501 "no such pool '%s'"), zc.zc_name);
3502 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3504 } else if (errno == ENOENT) {
3505 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3506 "parent does not exist"));
3507 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3509 return (zfs_standard_error(hdl, errno, errbuf));
3512 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3516 /* we are in a non-global zone, but parent is in the global zone */
3517 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3518 (void) zfs_standard_error(hdl, EPERM, errbuf);
3523 /* make sure parent is a filesystem */
3524 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3526 "parent is not a filesystem"));
3527 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3533 if (prefixlen != NULL)
3534 *prefixlen = strlen(parent);
3539 * Finds whether the dataset of the given type(s) exists.
3542 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3546 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3550 * Try to get stats for the dataset, which will tell us if it exists.
3552 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3553 int ds_type = zhp->zfs_type;
3556 if (types & ds_type)
3563 * Given a path to 'target', create all the ancestors between
3564 * the prefixlen portion of the path, and the target itself.
3565 * Fail if the initial prefixlen-ancestor does not already exist.
3568 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3574 /* make sure prefix exists */
3575 cp = target + prefixlen;
3577 assert(strchr(cp, '/') == NULL);
3578 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3581 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3589 * Attempt to create, mount, and share any ancestor filesystems,
3590 * up to the prefixlen-long one.
3592 for (cp = target + prefixlen + 1;
3593 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3597 h = make_dataset_handle(hdl, target);
3599 /* it already exists, nothing to do here */
3604 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3606 opname = dgettext(TEXT_DOMAIN, "create");
3610 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3612 opname = dgettext(TEXT_DOMAIN, "open");
3616 if (zfs_mount(h, NULL, 0) != 0) {
3617 opname = dgettext(TEXT_DOMAIN, "mount");
3621 if (zfs_share(h) != 0) {
3622 opname = dgettext(TEXT_DOMAIN, "share");
3632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3633 "failed to %s ancestor '%s'"), opname, target);
3638 * Creates non-existing ancestors of the given path.
3641 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3648 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3649 "cannot create '%s'"), path);
3652 * Check that we are not passing the nesting limit
3653 * before we start creating any ancestors.
3655 if (dataset_nestcheck(path) != 0) {
3656 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3657 "maximum name nesting depth exceeded"));
3658 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3661 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3664 if ((path_copy = strdup(path)) != NULL) {
3665 rc = create_parents(hdl, path_copy, prefix);
3668 if (path_copy == NULL || rc != 0)
3675 * Create a new filesystem or volume.
3678 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3683 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3685 enum lzc_dataset_type ost;
3686 zpool_handle_t *zpool_handle;
3687 uint8_t *wkeydata = NULL;
3690 char parent[ZFS_MAX_DATASET_NAME_LEN];
3692 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3693 "cannot create '%s'"), path);
3695 /* validate the path, taking care to note the extended error message */
3696 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3697 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3699 if (dataset_nestcheck(path) != 0) {
3700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3701 "maximum name nesting depth exceeded"));
3702 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3705 /* validate parents exist */
3706 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3710 * The failure modes when creating a dataset of a different type over
3711 * one that already exists is a little strange. In particular, if you
3712 * try to create a dataset on top of an existing dataset, the ioctl()
3713 * will return ENOENT, not EEXIST. To prevent this from happening, we
3714 * first try to see if the dataset exists.
3716 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3717 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3718 "dataset already exists"));
3719 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3722 if (type == ZFS_TYPE_VOLUME)
3723 ost = LZC_DATSET_TYPE_ZVOL;
3725 ost = LZC_DATSET_TYPE_ZFS;
3727 /* open zpool handle for prop validation */
3728 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3729 (void) strlcpy(pool_path, path, sizeof (pool_path));
3731 /* truncate pool_path at first slash */
3732 char *p = strchr(pool_path, '/');
3736 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3739 if (props && (props = zfs_valid_proplist(hdl, type, props,
3740 zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3741 zpool_close(zpool_handle);
3744 zpool_close(zpool_handle);
3746 if (type == ZFS_TYPE_VOLUME) {
3748 * If we are creating a volume, the size and block size must
3749 * satisfy a few restraints. First, the blocksize must be a
3750 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3751 * volsize must be a multiple of the block size, and cannot be
3754 if (props == NULL || nvlist_lookup_uint64(props,
3755 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3757 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3758 "missing volume size"));
3759 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3762 if ((ret = nvlist_lookup_uint64(props,
3763 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3764 &blocksize)) != 0) {
3765 if (ret == ENOENT) {
3766 blocksize = zfs_prop_default_numeric(
3767 ZFS_PROP_VOLBLOCKSIZE);
3770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3771 "missing volume block size"));
3772 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3779 "volume size cannot be zero"));
3780 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3783 if (size % blocksize != 0) {
3785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3786 "volume size must be a multiple of volume block "
3788 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3792 (void) parent_name(path, parent, sizeof (parent));
3793 if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
3794 &wkeydata, &wkeylen) != 0) {
3796 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3799 /* create the dataset */
3800 ret = lzc_create(path, ost, props, wkeydata, wkeylen);
3802 if (wkeydata != NULL)
3805 /* check for failure */
3809 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3810 "no such parent '%s'"), parent);
3811 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3815 "pool must be upgraded to set this "
3816 "property or value"));
3817 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3820 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3821 "encryption root's key is not loaded "
3823 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3827 "invalid property value(s) specified"));
3828 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3832 * This platform can't address a volume this big.
3834 if (type == ZFS_TYPE_VOLUME)
3835 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3840 return (zfs_standard_error(hdl, errno, errbuf));
3848 * Destroys the given dataset. The caller must make sure that the filesystem
3849 * isn't mounted, and that there are no active dependents. If the file system
3850 * does not exist this function does nothing.
3853 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3857 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3860 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3861 nvlist_t *nv = fnvlist_alloc();
3862 fnvlist_add_boolean(nv, zhp->zfs_name);
3863 error = lzc_destroy_bookmarks(nv, NULL);
3866 return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3867 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3873 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3874 nvlist_t *nv = fnvlist_alloc();
3875 fnvlist_add_boolean(nv, zhp->zfs_name);
3876 error = lzc_destroy_snaps(nv, defer, NULL);
3879 error = lzc_destroy(zhp->zfs_name);
3882 if (error != 0 && error != ENOENT) {
3883 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3884 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3888 remove_mountpoint(zhp);
3893 struct destroydata {
3895 const char *snapname;
3899 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3901 struct destroydata *dd = arg;
3902 char name[ZFS_MAX_DATASET_NAME_LEN];
3905 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
3906 dd->snapname) >= sizeof (name))
3909 if (lzc_exists(name))
3910 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3912 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3918 * Destroys all snapshots with the given name in zhp & descendants.
3921 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3924 struct destroydata dd = { 0 };
3926 dd.snapname = snapname;
3927 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3928 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3930 if (nvlist_empty(dd.nvl)) {
3931 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3932 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3933 zhp->zfs_name, snapname);
3935 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3937 nvlist_free(dd.nvl);
3942 * Destroys all the snapshots named in the nvlist.
3945 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3948 nvlist_t *errlist = NULL;
3951 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3954 nvlist_free(errlist);
3958 if (nvlist_empty(errlist)) {
3960 (void) snprintf(errbuf, sizeof (errbuf),
3961 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3963 ret = zfs_standard_error(hdl, ret, errbuf);
3965 for (pair = nvlist_next_nvpair(errlist, NULL);
3966 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3968 (void) snprintf(errbuf, sizeof (errbuf),
3969 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3972 switch (fnvpair_value_int32(pair)) {
3975 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3976 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3979 ret = zfs_standard_error(hdl, errno, errbuf);
3984 nvlist_free(errlist);
3989 * Clones the given dataset. The target must be of the same type as the source.
3992 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3994 char parent[ZFS_MAX_DATASET_NAME_LEN];
3997 libzfs_handle_t *hdl = zhp->zfs_hdl;
4000 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4002 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4003 "cannot create '%s'"), target);
4005 /* validate the target/clone name */
4006 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
4007 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4009 /* validate parents exist */
4010 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
4013 (void) parent_name(target, parent, sizeof (parent));
4020 if (ZFS_IS_VOLUME(zhp)) {
4021 type = ZFS_TYPE_VOLUME;
4023 type = ZFS_TYPE_FILESYSTEM;
4025 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
4026 zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
4028 if (zfs_fix_auto_resv(zhp, props) == -1) {
4034 if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
4036 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
4039 ret = lzc_clone(target, zhp->zfs_name, props);
4047 * The parent doesn't exist. We should have caught this
4048 * above, but there may a race condition that has since
4049 * destroyed the parent.
4051 * At this point, we don't know whether it's the source
4052 * that doesn't exist anymore, or whether the target
4053 * dataset doesn't exist.
4055 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4056 "no such parent '%s'"), parent);
4057 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
4060 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4061 "source and target pools differ"));
4062 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
4066 return (zfs_standard_error(zhp->zfs_hdl, errno,
4075 * Promotes the given clone fs to be the clone parent.
4078 zfs_promote(zfs_handle_t *zhp)
4080 libzfs_handle_t *hdl = zhp->zfs_hdl;
4081 char snapname[ZFS_MAX_DATASET_NAME_LEN];
4085 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4086 "cannot promote '%s'"), zhp->zfs_name);
4088 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4089 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4090 "snapshots can not be promoted"));
4091 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4094 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
4095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4096 "not a cloned filesystem"));
4097 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4100 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4101 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4103 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
4108 /* There is a conflicting snapshot name. */
4109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4110 "conflicting snapshot '%s' from parent '%s'"),
4111 snapname, zhp->zfs_dmustats.dds_origin);
4112 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4115 return (zfs_standard_error(hdl, ret, errbuf));
4121 typedef struct snapdata {
4123 const char *sd_snapname;
4127 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4129 snapdata_t *sd = arg;
4130 char name[ZFS_MAX_DATASET_NAME_LEN];
4133 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4134 if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp),
4135 sd->sd_snapname) >= sizeof (name))
4138 fnvlist_add_boolean(sd->sd_nvl, name);
4140 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4148 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
4153 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4154 "cannot remap dataset '%s'"), fs);
4156 err = lzc_remap(fs);
4161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4162 "pool must be upgraded"));
4163 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4166 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4169 (void) zfs_standard_error(hdl, err, errbuf);
4178 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4182 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4188 zpool_handle_t *zpool_hdl;
4189 char pool[ZFS_MAX_DATASET_NAME_LEN];
4191 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4192 "cannot create snapshots "));
4195 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
4196 const char *snapname = nvpair_name(elem);
4198 /* validate the target name */
4199 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
4201 (void) snprintf(errbuf, sizeof (errbuf),
4202 dgettext(TEXT_DOMAIN,
4203 "cannot create snapshot '%s'"), snapname);
4204 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4209 * get pool handle for prop validation. assumes all snaps are in the
4210 * same pool, as does lzc_snapshot (below).
4212 elem = nvlist_next_nvpair(snaps, NULL);
4213 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4214 pool[strcspn(pool, "/@")] = '\0';
4215 zpool_hdl = zpool_open(hdl, pool);
4216 if (zpool_hdl == NULL)
4219 if (props != NULL &&
4220 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4221 props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4222 zpool_close(zpool_hdl);
4225 zpool_close(zpool_hdl);
4227 ret = lzc_snapshot(snaps, props, &errors);
4230 boolean_t printed = B_FALSE;
4231 for (elem = nvlist_next_nvpair(errors, NULL);
4233 elem = nvlist_next_nvpair(errors, elem)) {
4234 (void) snprintf(errbuf, sizeof (errbuf),
4235 dgettext(TEXT_DOMAIN,
4236 "cannot create snapshot '%s'"), nvpair_name(elem));
4237 (void) zfs_standard_error(hdl,
4238 fnvpair_value_int32(elem), errbuf);
4244 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4245 "multiple snapshots of same "
4247 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4251 (void) zfs_standard_error(hdl, ret, errbuf);
4257 nvlist_free(errors);
4262 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4266 snapdata_t sd = { 0 };
4267 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4272 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4273 "cannot snapshot %s"), path);
4275 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4276 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4278 (void) strlcpy(fsname, path, sizeof (fsname));
4279 cp = strchr(fsname, '@');
4281 sd.sd_snapname = cp + 1;
4283 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4284 ZFS_TYPE_VOLUME)) == NULL) {
4288 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4290 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4292 fnvlist_add_boolean(sd.sd_nvl, path);
4295 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4296 nvlist_free(sd.sd_nvl);
4302 * Destroy any more recent snapshots. We invoke this callback on any dependents
4303 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4304 * is a dependent and we should just destroy it without checking the transaction
4307 typedef struct rollback_data {
4308 const char *cb_target; /* the snapshot */
4309 uint64_t cb_create; /* creation time reference */
4315 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4317 rollback_data_t *cbp = data;
4318 prop_changelist_t *clp;
4320 /* We must destroy this clone; first unmount it */
4321 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4322 cbp->cb_force ? MS_FORCE: 0);
4323 if (clp == NULL || changelist_prefix(clp) != 0) {
4324 cbp->cb_error = B_TRUE;
4328 if (zfs_destroy(zhp, B_FALSE) != 0)
4329 cbp->cb_error = B_TRUE;
4331 changelist_remove(clp, zhp->zfs_name);
4332 (void) changelist_postfix(clp);
4333 changelist_free(clp);
4340 rollback_destroy(zfs_handle_t *zhp, void *data)
4342 rollback_data_t *cbp = data;
4344 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4345 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4346 rollback_destroy_dependent, cbp);
4348 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4356 * Given a dataset, rollback to a specific snapshot, discarding any
4357 * data changes since then and making it the active dataset.
4359 * Any snapshots and bookmarks more recent than the target are
4360 * destroyed, along with their dependents (i.e. clones).
4363 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4365 rollback_data_t cb = { 0 };
4367 boolean_t restore_resv = 0;
4368 uint64_t old_volsize = 0, new_volsize;
4369 zfs_prop_t resv_prop = { 0 };
4370 uint64_t min_txg = 0;
4372 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4373 zhp->zfs_type == ZFS_TYPE_VOLUME);
4376 * Destroy all recent snapshots and their dependents.
4378 cb.cb_force = force;
4379 cb.cb_target = snap->zfs_name;
4380 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4382 if (cb.cb_create > 0)
4383 min_txg = cb.cb_create;
4385 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb,
4388 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4394 * Now that we have verified that the snapshot is the latest,
4395 * rollback to the given snapshot.
4398 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4399 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4401 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4403 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4407 * Pass both the filesystem and the wanted snapshot names,
4408 * we would get an error back if the snapshot is destroyed or
4409 * a new snapshot is created before this request is processed.
4411 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4415 (void) snprintf(errbuf, sizeof (errbuf),
4416 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4420 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4421 "there is a snapshot or bookmark more recent "
4422 "than '%s'"), snap->zfs_name);
4423 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4426 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4427 "'%s' is not found among snapshots of '%s'"),
4428 snap->zfs_name, zhp->zfs_name);
4429 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4432 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4435 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4441 * For volumes, if the pre-rollback volsize matched the pre-
4442 * rollback reservation and the volsize has changed then set
4443 * the reservation property to the post-rollback volsize.
4444 * Make a new handle since the rollback closed the dataset.
4446 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4447 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4449 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4450 if (old_volsize != new_volsize)
4451 err = zfs_prop_set_int(zhp, resv_prop,
4460 * Renames the given dataset.
4463 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4464 boolean_t force_unmount)
4467 zfs_cmd_t zc = {"\0"};
4469 prop_changelist_t *cl = NULL;
4470 zfs_handle_t *zhrp = NULL;
4471 char *parentname = NULL;
4472 char parent[ZFS_MAX_DATASET_NAME_LEN];
4473 libzfs_handle_t *hdl = zhp->zfs_hdl;
4476 /* if we have the same exact name, just return success */
4477 if (strcmp(zhp->zfs_name, target) == 0)
4480 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4481 "cannot rename to '%s'"), target);
4483 /* make sure source name is valid */
4484 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4485 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4488 * Make sure the target name is valid
4490 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4491 if ((strchr(target, '@') == NULL) ||
4494 * Snapshot target name is abbreviated,
4495 * reconstruct full dataset name
4497 (void) strlcpy(parent, zhp->zfs_name,
4499 delim = strchr(parent, '@');
4500 if (strchr(target, '@') == NULL)
4504 (void) strlcat(parent, target, sizeof (parent));
4508 * Make sure we're renaming within the same dataset.
4510 delim = strchr(target, '@');
4511 if (strncmp(zhp->zfs_name, target, delim - target)
4512 != 0 || zhp->zfs_name[delim - target] != '@') {
4513 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4514 "snapshots must be part of same "
4516 return (zfs_error(hdl, EZFS_CROSSTARGET,
4521 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4522 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4526 "recursive rename must be a snapshot"));
4527 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4530 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4531 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4533 /* validate parents */
4534 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4537 /* make sure we're in the same pool */
4538 verify((delim = strchr(target, '/')) != NULL);
4539 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4540 zhp->zfs_name[delim - target] != '/') {
4541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4542 "datasets must be within same pool"));
4543 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4546 /* new name cannot be a child of the current dataset name */
4547 if (is_descendant(zhp->zfs_name, target)) {
4548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4549 "New dataset name cannot be a descendant of "
4550 "current dataset name"));
4551 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4555 (void) snprintf(errbuf, sizeof (errbuf),
4556 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4558 if (getzoneid() == GLOBAL_ZONEID &&
4559 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4561 "dataset is used in a non-global zone"));
4562 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4566 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4567 if (parentname == NULL) {
4571 delim = strchr(parentname, '@');
4573 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4578 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4579 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4580 CL_GATHER_ITER_MOUNTED,
4581 force_unmount ? MS_FORCE : 0)) == NULL)
4584 if (changelist_haszonedchild(cl)) {
4585 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4586 "child dataset with inherited mountpoint is used "
4587 "in a non-global zone"));
4588 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4593 if ((ret = changelist_prefix(cl)) != 0)
4597 if (ZFS_IS_VOLUME(zhp))
4598 zc.zc_objset_type = DMU_OST_ZVOL;
4600 zc.zc_objset_type = DMU_OST_ZFS;
4602 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4603 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4605 zc.zc_cookie = recursive;
4607 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4609 * if it was recursive, the one that actually failed will
4612 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4613 "cannot rename '%s'"), zc.zc_name);
4615 if (recursive && errno == EEXIST) {
4616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4617 "a child dataset already has a snapshot "
4618 "with the new name"));
4619 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4620 } else if (errno == EACCES) {
4621 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) ==
4623 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4624 "cannot rename an unencrypted dataset to "
4625 "be a decendent of an encrypted one"));
4627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4628 "cannot move encryption child outside of "
4629 "its encryption root"));
4631 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4633 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4637 * On failure, we still want to remount any filesystems that
4638 * were previously mounted, so we don't alter the system state.
4641 (void) changelist_postfix(cl);
4644 changelist_rename(cl, zfs_get_name(zhp), target);
4645 ret = changelist_postfix(cl);
4650 if (parentname != NULL) {
4657 changelist_free(cl);
4663 zfs_get_all_props(zfs_handle_t *zhp)
4665 return (zhp->zfs_props);
4669 zfs_get_recvd_props(zfs_handle_t *zhp)
4671 if (zhp->zfs_recvd_props == NULL)
4672 if (get_recvd_props_ioctl(zhp) != 0)
4674 return (zhp->zfs_recvd_props);
4678 zfs_get_user_props(zfs_handle_t *zhp)
4680 return (zhp->zfs_user_props);
4684 * This function is used by 'zfs list' to determine the exact set of columns to
4685 * display, and their maximum widths. This does two main things:
4687 * - If this is a list of all properties, then expand the list to include
4688 * all native properties, and set a flag so that for each dataset we look
4689 * for new unique user properties and add them to the list.
4691 * - For non fixed-width properties, keep track of the maximum width seen
4692 * so that we can size the column appropriately. If the user has
4693 * requested received property values, we also need to compute the width
4694 * of the RECEIVED column.
4697 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4700 libzfs_handle_t *hdl = zhp->zfs_hdl;
4701 zprop_list_t *entry;
4702 zprop_list_t **last, **start;
4703 nvlist_t *userprops, *propval;
4706 char buf[ZFS_MAXPROPLEN];
4708 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4711 userprops = zfs_get_user_props(zhp);
4714 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4716 * Go through and add any user properties as necessary. We
4717 * start by incrementing our list pointer to the first
4718 * non-native property.
4721 while (*start != NULL) {
4722 if ((*start)->pl_prop == ZPROP_INVAL)
4724 start = &(*start)->pl_next;
4728 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4730 * See if we've already found this property in our list.
4732 for (last = start; *last != NULL;
4733 last = &(*last)->pl_next) {
4734 if (strcmp((*last)->pl_user_prop,
4735 nvpair_name(elem)) == 0)
4739 if (*last == NULL) {
4740 if ((entry = zfs_alloc(hdl,
4741 sizeof (zprop_list_t))) == NULL ||
4742 ((entry->pl_user_prop = zfs_strdup(hdl,
4743 nvpair_name(elem)))) == NULL) {
4748 entry->pl_prop = ZPROP_INVAL;
4749 entry->pl_width = strlen(nvpair_name(elem));
4750 entry->pl_all = B_TRUE;
4757 * Now go through and check the width of any non-fixed columns
4759 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4760 if (entry->pl_fixed && !literal)
4763 if (entry->pl_prop != ZPROP_INVAL) {
4764 if (zfs_prop_get(zhp, entry->pl_prop,
4765 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4766 if (strlen(buf) > entry->pl_width)
4767 entry->pl_width = strlen(buf);
4769 if (received && zfs_prop_get_recvd(zhp,
4770 zfs_prop_to_name(entry->pl_prop),
4771 buf, sizeof (buf), literal) == 0)
4772 if (strlen(buf) > entry->pl_recvd_width)
4773 entry->pl_recvd_width = strlen(buf);
4775 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4777 verify(nvlist_lookup_string(propval,
4778 ZPROP_VALUE, &strval) == 0);
4779 if (strlen(strval) > entry->pl_width)
4780 entry->pl_width = strlen(strval);
4782 if (received && zfs_prop_get_recvd(zhp,
4783 entry->pl_user_prop,
4784 buf, sizeof (buf), literal) == 0)
4785 if (strlen(buf) > entry->pl_recvd_width)
4786 entry->pl_recvd_width = strlen(buf);
4794 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4800 * Keep a reference to the props-table against which we prune the
4803 zhp->zfs_props_table = props;
4805 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4808 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4809 next = nvlist_next_nvpair(zhp->zfs_props, curr);
4812 * User properties will result in ZPROP_INVAL, and since we
4813 * only know how to prune standard ZFS properties, we always
4814 * leave these in the list. This can also happen if we
4815 * encounter an unknown DSL property (when running older
4816 * software, for example).
4818 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4819 (void) nvlist_remove(zhp->zfs_props,
4820 nvpair_name(curr), nvpair_type(curr));
4826 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4827 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4829 zfs_cmd_t zc = {"\0"};
4830 nvlist_t *nvlist = NULL;
4833 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4834 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4835 zc.zc_cookie = (uint64_t)cmd;
4837 if (cmd == ZFS_SMB_ACL_RENAME) {
4838 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4839 (void) no_memory(hdl);
4845 case ZFS_SMB_ACL_ADD:
4846 case ZFS_SMB_ACL_REMOVE:
4847 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4849 case ZFS_SMB_ACL_RENAME:
4850 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4852 (void) no_memory(hdl);
4855 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4857 (void) no_memory(hdl);
4860 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4861 nvlist_free(nvlist);
4865 case ZFS_SMB_ACL_PURGE:
4870 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4871 nvlist_free(nvlist);
4876 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4877 char *path, char *resource)
4879 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4884 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4885 char *path, char *resource)
4887 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4892 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4894 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4899 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4900 char *oldname, char *newname)
4902 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4907 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4908 zfs_userspace_cb_t func, void *arg)
4910 zfs_cmd_t zc = {"\0"};
4911 zfs_useracct_t buf[100];
4912 libzfs_handle_t *hdl = zhp->zfs_hdl;
4915 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4917 zc.zc_objset_type = type;
4918 zc.zc_nvlist_dst = (uintptr_t)buf;
4921 zfs_useracct_t *zua = buf;
4923 zc.zc_nvlist_dst_size = sizeof (buf);
4924 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4927 if ((errno == ENOTSUP &&
4928 (type == ZFS_PROP_USEROBJUSED ||
4929 type == ZFS_PROP_GROUPOBJUSED ||
4930 type == ZFS_PROP_USEROBJQUOTA ||
4931 type == ZFS_PROP_GROUPOBJQUOTA ||
4932 type == ZFS_PROP_PROJECTOBJUSED ||
4933 type == ZFS_PROP_PROJECTOBJQUOTA ||
4934 type == ZFS_PROP_PROJECTUSED ||
4935 type == ZFS_PROP_PROJECTQUOTA)))
4938 (void) snprintf(errbuf, sizeof (errbuf),
4939 dgettext(TEXT_DOMAIN,
4940 "cannot get used/quota for %s"), zc.zc_name);
4941 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4943 if (zc.zc_nvlist_dst_size == 0)
4946 while (zc.zc_nvlist_dst_size > 0) {
4947 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4948 zua->zu_space)) != 0)
4951 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4960 const char *snapname;
4962 boolean_t recursive;
4967 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4969 struct holdarg *ha = arg;
4970 char name[ZFS_MAX_DATASET_NAME_LEN];
4973 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
4974 ha->snapname) >= sizeof (name))
4977 if (lzc_exists(name))
4978 fnvlist_add_string(ha->nvl, name, ha->tag);
4981 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4987 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4988 boolean_t recursive, int cleanup_fd)
4993 ha.nvl = fnvlist_alloc();
4994 ha.snapname = snapname;
4996 ha.recursive = recursive;
4997 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4999 if (nvlist_empty(ha.nvl)) {
5002 fnvlist_free(ha.nvl);
5004 (void) snprintf(errbuf, sizeof (errbuf),
5005 dgettext(TEXT_DOMAIN,
5006 "cannot hold snapshot '%s@%s'"),
5007 zhp->zfs_name, snapname);
5008 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
5012 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
5013 fnvlist_free(ha.nvl);
5019 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
5023 libzfs_handle_t *hdl = zhp->zfs_hdl;
5028 ret = lzc_hold(holds, cleanup_fd, &errors);
5031 /* There may be errors even in the success case. */
5032 fnvlist_free(errors);
5036 if (nvlist_empty(errors)) {
5037 /* no hold-specific errors */
5038 (void) snprintf(errbuf, sizeof (errbuf),
5039 dgettext(TEXT_DOMAIN, "cannot hold"));
5042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5043 "pool must be upgraded"));
5044 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5047 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5050 (void) zfs_standard_error(hdl, ret, errbuf);
5054 for (elem = nvlist_next_nvpair(errors, NULL);
5056 elem = nvlist_next_nvpair(errors, elem)) {
5057 (void) snprintf(errbuf, sizeof (errbuf),
5058 dgettext(TEXT_DOMAIN,
5059 "cannot hold snapshot '%s'"), nvpair_name(elem));
5060 switch (fnvpair_value_int32(elem)) {
5063 * Temporary tags wind up having the ds object id
5064 * prepended. So even if we passed the length check
5065 * above, it's still possible for the tag to wind
5066 * up being slightly too long.
5068 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
5071 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5074 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
5077 (void) zfs_standard_error(hdl,
5078 fnvpair_value_int32(elem), errbuf);
5082 fnvlist_free(errors);
5087 zfs_release_one(zfs_handle_t *zhp, void *arg)
5089 struct holdarg *ha = arg;
5090 char name[ZFS_MAX_DATASET_NAME_LEN];
5092 nvlist_t *existing_holds;
5094 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
5095 ha->snapname) >= sizeof (name)) {
5100 if (lzc_get_holds(name, &existing_holds) != 0) {
5102 } else if (!nvlist_exists(existing_holds, ha->tag)) {
5105 nvlist_t *torelease = fnvlist_alloc();
5106 fnvlist_add_boolean(torelease, ha->tag);
5107 fnvlist_add_nvlist(ha->nvl, name, torelease);
5108 fnvlist_free(torelease);
5112 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
5118 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
5119 boolean_t recursive)
5123 nvlist_t *errors = NULL;
5125 libzfs_handle_t *hdl = zhp->zfs_hdl;
5128 ha.nvl = fnvlist_alloc();
5129 ha.snapname = snapname;
5131 ha.recursive = recursive;
5133 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
5135 if (nvlist_empty(ha.nvl)) {
5136 fnvlist_free(ha.nvl);
5138 (void) snprintf(errbuf, sizeof (errbuf),
5139 dgettext(TEXT_DOMAIN,
5140 "cannot release hold from snapshot '%s@%s'"),
5141 zhp->zfs_name, snapname);
5143 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5145 (void) zfs_standard_error(hdl, ret, errbuf);
5150 ret = lzc_release(ha.nvl, &errors);
5151 fnvlist_free(ha.nvl);
5154 /* There may be errors even in the success case. */
5155 fnvlist_free(errors);
5159 if (nvlist_empty(errors)) {
5160 /* no hold-specific errors */
5161 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5166 "pool must be upgraded"));
5167 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5170 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
5174 for (elem = nvlist_next_nvpair(errors, NULL);
5176 elem = nvlist_next_nvpair(errors, elem)) {
5177 (void) snprintf(errbuf, sizeof (errbuf),
5178 dgettext(TEXT_DOMAIN,
5179 "cannot release hold from snapshot '%s'"),
5181 switch (fnvpair_value_int32(elem)) {
5183 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5186 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5189 (void) zfs_standard_error_fmt(hdl,
5190 fnvpair_value_int32(elem), errbuf);
5194 fnvlist_free(errors);
5199 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5201 zfs_cmd_t zc = {"\0"};
5202 libzfs_handle_t *hdl = zhp->zfs_hdl;
5208 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5209 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5213 nvbuf = malloc(nvsz);
5214 if (nvbuf == NULL) {
5215 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5219 zc.zc_nvlist_dst_size = nvsz;
5220 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5222 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5224 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
5225 (void) snprintf(errbuf, sizeof (errbuf),
5226 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5231 nvsz = zc.zc_nvlist_dst_size;
5235 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5236 "pool must be upgraded"));
5237 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5240 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5243 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5246 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5251 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5253 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
5254 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5256 err = zfs_standard_error_fmt(hdl, rc, errbuf);
5266 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5268 zfs_cmd_t zc = {"\0"};
5269 libzfs_handle_t *hdl = zhp->zfs_hdl;
5275 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5276 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5278 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5281 nvbuf = malloc(nvsz);
5283 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5286 zc.zc_nvlist_src_size = nvsz;
5287 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5288 zc.zc_perm_action = un;
5290 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5292 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5293 (void) snprintf(errbuf, sizeof (errbuf),
5294 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5299 "pool must be upgraded"));
5300 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5303 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5306 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5309 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5320 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5325 err = lzc_get_holds(zhp->zfs_name, nvl);
5328 libzfs_handle_t *hdl = zhp->zfs_hdl;
5330 (void) snprintf(errbuf, sizeof (errbuf),
5331 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5336 "pool must be upgraded"));
5337 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5340 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5343 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5346 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5355 * Convert the zvol's volume size to an appropriate reservation.
5356 * Note: If this routine is updated, it is necessary to update the ZFS test
5357 * suite's shell version in reservation.kshlib.
5360 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5363 uint64_t nblocks, volblocksize;
5367 if (nvlist_lookup_string(props,
5368 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5369 ncopies = atoi(strval);
5372 if (nvlist_lookup_uint64(props,
5373 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5374 &volblocksize) != 0)
5375 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5376 nblocks = volsize/volblocksize;
5377 /* start with metadnode L0-L6 */
5379 /* calculate number of indirects */
5380 while (nblocks > 1) {
5381 nblocks += DNODES_PER_LEVEL - 1;
5382 nblocks /= DNODES_PER_LEVEL;
5385 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5388 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5389 * compressed, but in practice they compress down to about
5392 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;