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 https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2019 Joyent, Inc.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017-2018 RackTop Systems.
33 * Copyright (c) 2019 Datto Inc.
34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
35 * Copyright (c) 2021 Matt Fiddaman
48 #include <sys/mntent.h>
49 #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"
68 #include "zfs_deleg.h"
70 static int userquota_propname_decode(const char *propname, boolean_t zoned,
71 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
74 * Given a single type (not a mask of types), return the type in a human
78 zfs_type_to_name(zfs_type_t type)
81 case ZFS_TYPE_FILESYSTEM:
82 return (dgettext(TEXT_DOMAIN, "filesystem"));
83 case ZFS_TYPE_SNAPSHOT:
84 return (dgettext(TEXT_DOMAIN, "snapshot"));
86 return (dgettext(TEXT_DOMAIN, "volume"));
88 return (dgettext(TEXT_DOMAIN, "pool"));
89 case ZFS_TYPE_BOOKMARK:
90 return (dgettext(TEXT_DOMAIN, "bookmark"));
92 assert(!"unhandled zfs_type_t");
99 * Validate a ZFS path. This is used even before trying to open the dataset, to
100 * provide a more meaningful error message. We call zfs_error_aux() to
101 * explain exactly why the name was not valid.
104 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
110 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
113 "snapshot delimiter '@' is not expected here"));
117 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
120 "missing '@' delimiter in snapshot name"));
124 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
127 "bookmark delimiter '#' is not expected here"));
131 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
134 "missing '#' delimiter in bookmark name"));
138 if (modifying && strchr(path, '%') != NULL) {
140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141 "invalid character %c in name"), '%');
145 if (entity_namecheck(path, &why, &what) != 0) {
148 case NAME_ERR_TOOLONG:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "name is too long"));
153 case NAME_ERR_LEADING_SLASH:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "leading slash in name"));
158 case NAME_ERR_EMPTY_COMPONENT:
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "empty component or misplaced '@'"
161 " or '#' delimiter in name"));
164 case NAME_ERR_TRAILING_SLASH:
165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
166 "trailing slash in name"));
169 case NAME_ERR_INVALCHAR:
171 dgettext(TEXT_DOMAIN, "invalid character "
172 "'%c' in name"), what);
175 case NAME_ERR_MULTIPLE_DELIMITERS:
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "multiple '@' and/or '#' delimiters in "
181 case NAME_ERR_NOLETTER:
182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
183 "pool doesn't begin with a letter"));
186 case NAME_ERR_RESERVED:
187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
188 "name is reserved"));
191 case NAME_ERR_DISKLIKE:
192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193 "reserved disk name"));
196 case NAME_ERR_SELF_REF:
197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
198 "self reference, '.' is found in name"));
201 case NAME_ERR_PARENT_REF:
202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
203 "parent reference, '..' is found in name"));
207 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
208 "(%d) not defined"), why);
220 zfs_name_valid(const char *name, zfs_type_t type)
222 if (type == ZFS_TYPE_POOL)
223 return (zpool_name_valid(NULL, B_FALSE, name));
224 return (zfs_validate_name(NULL, name, type, B_FALSE));
228 * This function takes the raw DSL properties, and filters out the user-defined
229 * properties into a separate nvlist.
232 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
234 libzfs_handle_t *hdl = zhp->zfs_hdl;
238 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
239 (void) no_memory(hdl);
244 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
245 if (!zfs_prop_user(nvpair_name(elem)))
248 nvlist_t *propval = fnvpair_value_nvlist(elem);
249 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
251 (void) no_memory(hdl);
259 static zpool_handle_t *
260 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
262 libzfs_handle_t *hdl = zhp->zfs_hdl;
265 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
266 if (hdl->libzfs_pool_handles != NULL)
267 zph->zpool_next = hdl->libzfs_pool_handles;
268 hdl->libzfs_pool_handles = zph;
273 static zpool_handle_t *
274 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
276 libzfs_handle_t *hdl = zhp->zfs_hdl;
277 zpool_handle_t *zph = hdl->libzfs_pool_handles;
279 while ((zph != NULL) &&
280 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
281 zph = zph->zpool_next;
286 * Returns a handle to the pool that contains the provided dataset.
287 * If a handle to that pool already exists then that handle is returned.
288 * Otherwise, a new handle is created and added to the list of handles.
290 static zpool_handle_t *
291 zpool_handle(zfs_handle_t *zhp)
297 len = strcspn(zhp->zfs_name, "/@#") + 1;
298 pool_name = zfs_alloc(zhp->zfs_hdl, len);
299 (void) strlcpy(pool_name, zhp->zfs_name, len);
301 zph = zpool_find_handle(zhp, pool_name, len);
303 zph = zpool_add_handle(zhp, pool_name);
310 zpool_free_handles(libzfs_handle_t *hdl)
312 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
314 while (zph != NULL) {
315 next = zph->zpool_next;
319 hdl->libzfs_pool_handles = NULL;
323 * Utility function to gather stats (objset and zpl) for the given object.
326 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
328 libzfs_handle_t *hdl = zhp->zfs_hdl;
330 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
332 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, zc) != 0) {
334 zcmd_expand_dst_nvlist(hdl, zc);
342 * Utility function to get the received properties of the given object.
345 get_recvd_props_ioctl(zfs_handle_t *zhp)
347 libzfs_handle_t *hdl = zhp->zfs_hdl;
348 nvlist_t *recvdprops;
349 zfs_cmd_t zc = {"\0"};
352 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
354 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
356 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
358 zcmd_expand_dst_nvlist(hdl, &zc);
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 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 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) {
451 } else if (zhp->zfs_dmustats.dds_inconsistent) {
458 if (zhp->zfs_dmustats.dds_is_snapshot)
459 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
460 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
461 zhp->zfs_type = ZFS_TYPE_VOLUME;
462 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
463 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
465 abort(); /* we should never see any other types */
467 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
474 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
476 zfs_cmd_t zc = {"\0"};
478 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
484 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
485 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
487 if (get_stats_ioctl(zhp, &zc) == -1) {
488 zcmd_free_nvlists(&zc);
492 if (make_dataset_handle_common(zhp, &zc) == -1) {
496 zcmd_free_nvlists(&zc);
501 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
503 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
509 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
510 if (make_dataset_handle_common(zhp, zc) == -1) {
518 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
520 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
525 zhp->zfs_hdl = pzhp->zfs_hdl;
526 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
527 zhp->zfs_head_type = pzhp->zfs_type;
528 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
529 zhp->zpool_hdl = zpool_handle(zhp);
530 zhp->zfs_dmustats = zc->zc_objset_stats;
536 zfs_handle_dup(zfs_handle_t *zhp_orig)
538 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
543 zhp->zfs_hdl = zhp_orig->zfs_hdl;
544 zhp->zpool_hdl = zhp_orig->zpool_hdl;
545 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
546 sizeof (zhp->zfs_name));
547 zhp->zfs_type = zhp_orig->zfs_type;
548 zhp->zfs_head_type = zhp_orig->zfs_head_type;
549 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
550 if (zhp_orig->zfs_props != NULL) {
551 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
552 (void) no_memory(zhp->zfs_hdl);
557 if (zhp_orig->zfs_user_props != NULL) {
558 if (nvlist_dup(zhp_orig->zfs_user_props,
559 &zhp->zfs_user_props, 0) != 0) {
560 (void) no_memory(zhp->zfs_hdl);
565 if (zhp_orig->zfs_recvd_props != NULL) {
566 if (nvlist_dup(zhp_orig->zfs_recvd_props,
567 &zhp->zfs_recvd_props, 0)) {
568 (void) no_memory(zhp->zfs_hdl);
573 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
574 if (zhp_orig->zfs_mntopts != NULL) {
575 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
576 zhp_orig->zfs_mntopts);
578 zhp->zfs_props_table = zhp_orig->zfs_props_table;
583 zfs_bookmark_exists(const char *path)
587 char fsname[ZFS_MAX_DATASET_NAME_LEN];
593 (void) strlcpy(fsname, path, sizeof (fsname));
594 pound = strchr(fsname, '#');
599 bmark_name = pound + 1;
600 props = fnvlist_alloc();
601 err = lzc_get_bookmarks(fsname, props, &bmarks);
608 rv = nvlist_exists(bmarks, bmark_name);
614 make_bookmark_handle(zfs_handle_t *parent, const char *path,
615 nvlist_t *bmark_props)
617 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
622 /* Fill in the name. */
623 zhp->zfs_hdl = parent->zfs_hdl;
624 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
626 /* Set the property lists. */
627 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
633 zhp->zfs_head_type = parent->zfs_head_type;
634 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
636 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
637 nvlist_free(zhp->zfs_props);
645 struct zfs_open_bookmarks_cb_data {
651 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
653 struct zfs_open_bookmarks_cb_data *dp = data;
656 * Is it the one we are looking for?
658 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
660 * We found it. Save it and let the caller know we are done.
667 * Not found. Close the handle and ask for another one.
674 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
675 * argument is a mask of acceptable types. The function will print an
676 * appropriate error message and return NULL if it can't be opened.
679 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
682 char errbuf[ERRBUFLEN];
685 (void) snprintf(errbuf, sizeof (errbuf),
686 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
689 * Validate the name before we even try to open it.
691 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
692 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
697 * Bookmarks needs to be handled separately.
699 bookp = strchr(path, '#');
702 * Try to get stats for the dataset, which will tell us if it
706 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
707 (void) zfs_standard_error(hdl, errno, errbuf);
711 char dsname[ZFS_MAX_DATASET_NAME_LEN];
713 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
716 * We need to cut out '#' and everything after '#'
717 * to get the parent dataset name only.
719 assert(bookp - path < sizeof (dsname));
720 (void) strlcpy(dsname, path,
721 MIN(sizeof (dsname), bookp - path + 1));
724 * Create handle for the parent dataset.
727 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
728 (void) zfs_standard_error(hdl, errno, errbuf);
733 * Iterate bookmarks to find the right one.
736 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
737 &cb_data) == 0) && (cb_data.zhp == NULL)) {
738 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
742 if (cb_data.zhp == NULL) {
743 (void) zfs_standard_error(hdl, errno, errbuf);
755 if (!(types & zhp->zfs_type)) {
756 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
765 * Release a ZFS handle. Nothing to do but free the associated memory.
768 zfs_close(zfs_handle_t *zhp)
770 if (zhp->zfs_mntopts)
771 free(zhp->zfs_mntopts);
772 nvlist_free(zhp->zfs_props);
773 nvlist_free(zhp->zfs_user_props);
774 nvlist_free(zhp->zfs_recvd_props);
778 typedef struct mnttab_node {
779 struct mnttab mtn_mt;
784 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
786 const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
787 const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
790 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
792 return (TREE_ISIGN(rv));
796 libzfs_mnttab_init(libzfs_handle_t *hdl)
798 pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL);
799 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
800 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
801 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
805 libzfs_mnttab_update(libzfs_handle_t *hdl)
810 if ((mnttab = fopen(MNTTAB, "re")) == NULL)
813 while (getmntent(mnttab, &entry) == 0) {
817 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
820 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
821 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
822 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
823 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
824 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
826 /* Exclude duplicate mounts */
827 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
828 free(mtn->mtn_mt.mnt_special);
829 free(mtn->mtn_mt.mnt_mountp);
830 free(mtn->mtn_mt.mnt_fstype);
831 free(mtn->mtn_mt.mnt_mntopts);
836 avl_add(&hdl->libzfs_mnttab_cache, mtn);
839 (void) fclose(mnttab);
844 libzfs_mnttab_fini(libzfs_handle_t *hdl)
849 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
851 free(mtn->mtn_mt.mnt_special);
852 free(mtn->mtn_mt.mnt_mountp);
853 free(mtn->mtn_mt.mnt_fstype);
854 free(mtn->mtn_mt.mnt_mntopts);
857 avl_destroy(&hdl->libzfs_mnttab_cache);
858 (void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
862 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
864 hdl->libzfs_mnttab_enable = enable;
868 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
869 struct mnttab *entry)
876 if (!hdl->libzfs_mnttab_enable) {
877 struct mnttab srch = { 0 };
879 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
880 libzfs_mnttab_fini(hdl);
882 if ((mnttab = fopen(MNTTAB, "re")) == NULL)
885 srch.mnt_special = (char *)fsname;
886 srch.mnt_fstype = (char *)MNTTYPE_ZFS;
887 ret = getmntany(mnttab, entry, &srch) ? ENOENT : 0;
888 (void) fclose(mnttab);
892 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
893 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) {
896 if ((error = libzfs_mnttab_update(hdl)) != 0) {
897 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
902 find.mtn_mt.mnt_special = (char *)fsname;
903 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
905 *entry = mtn->mtn_mt;
908 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
913 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
914 const char *mountp, const char *mntopts)
918 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
919 if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
920 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
921 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
922 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
923 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
924 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
926 * Another thread may have already added this entry
927 * via libzfs_mnttab_update. If so we should skip it.
929 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, NULL) != NULL) {
930 free(mtn->mtn_mt.mnt_special);
931 free(mtn->mtn_mt.mnt_mountp);
932 free(mtn->mtn_mt.mnt_fstype);
933 free(mtn->mtn_mt.mnt_mntopts);
936 avl_add(&hdl->libzfs_mnttab_cache, mtn);
939 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
943 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
948 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
949 find.mtn_mt.mnt_special = (char *)fsname;
950 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
952 avl_remove(&hdl->libzfs_mnttab_cache, ret);
953 free(ret->mtn_mt.mnt_special);
954 free(ret->mtn_mt.mnt_mountp);
955 free(ret->mtn_mt.mnt_fstype);
956 free(ret->mtn_mt.mnt_mntopts);
959 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
963 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
965 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
967 if (zpool_handle == NULL)
970 *spa_version = zpool_get_prop_int(zpool_handle,
971 ZPOOL_PROP_VERSION, NULL);
976 * The choice of reservation property depends on the SPA version.
979 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
983 if (zfs_spa_version(zhp, &spa_version) < 0)
986 if (spa_version >= SPA_VERSION_REFRESERVATION)
987 *resv_prop = ZFS_PROP_REFRESERVATION;
989 *resv_prop = ZFS_PROP_RESERVATION;
995 * Given an nvlist of properties to set, validates that they are correct, and
996 * parses any numeric properties (index, boolean, etc) if they are specified as
1000 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
1001 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
1002 boolean_t key_params_ok, const char *errbuf)
1009 int chosen_normal = -1;
1010 int chosen_utf = -1;
1012 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
1013 (void) no_memory(hdl);
1018 * Make sure this property is valid and applies to this type.
1022 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1023 const char *propname = nvpair_name(elem);
1025 prop = zfs_name_to_prop(propname);
1026 if (prop == ZPROP_USERPROP && zfs_prop_user(propname)) {
1028 * This is a user property: make sure it's a
1029 * string, and that it's less than ZAP_MAXNAMELEN.
1031 if (nvpair_type(elem) != DATA_TYPE_STRING) {
1032 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1033 "'%s' must be a string"), propname);
1034 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1038 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
1039 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1040 "property name '%s' is too long"),
1042 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1046 (void) nvpair_value_string(elem, &strval);
1047 if (nvlist_add_string(ret, propname, strval) != 0) {
1048 (void) no_memory(hdl);
1055 * Currently, only user properties can be modified on
1058 if (type == ZFS_TYPE_SNAPSHOT) {
1059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1060 "this property can not be modified for snapshots"));
1061 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1065 if (prop == ZPROP_USERPROP && zfs_prop_userquota(propname)) {
1066 zfs_userquota_prop_t uqtype;
1067 char *newpropname = NULL;
1073 if (userquota_propname_decode(propname, zoned,
1074 &uqtype, domain, sizeof (domain), &rid) != 0) {
1076 dgettext(TEXT_DOMAIN,
1077 "'%s' has an invalid user/group name"),
1079 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1083 if (uqtype != ZFS_PROP_USERQUOTA &&
1084 uqtype != ZFS_PROP_GROUPQUOTA &&
1085 uqtype != ZFS_PROP_USEROBJQUOTA &&
1086 uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1087 uqtype != ZFS_PROP_PROJECTQUOTA &&
1088 uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
1090 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1092 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1097 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1098 (void) nvpair_value_string(elem, &strval);
1099 if (strcmp(strval, "none") == 0) {
1101 } else if (zfs_nicestrtonum(hdl,
1102 strval, &intval) != 0) {
1103 (void) zfs_error(hdl,
1104 EZFS_BADPROP, errbuf);
1107 } else if (nvpair_type(elem) ==
1109 (void) nvpair_value_uint64(elem, &intval);
1111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1112 "use 'none' to disable "
1113 "{user|group|project}quota"));
1117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1118 "'%s' must be a number"), propname);
1119 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1124 * Encode the prop name as
1125 * userquota@<hex-rid>-domain, to make it easy
1126 * for the kernel to decode.
1128 rc = asprintf(&newpropname, "%s%llx-%s",
1129 zfs_userquota_prop_prefixes[uqtype],
1130 (longlong_t)rid, domain);
1131 if (rc == -1 || newpropname == NULL) {
1132 (void) no_memory(hdl);
1139 if (nvlist_add_uint64_array(ret, newpropname,
1142 (void) no_memory(hdl);
1147 } else if (prop == ZPROP_USERPROP &&
1148 zfs_prop_written(propname)) {
1149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1150 "'%s' is readonly"),
1152 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1156 if (prop == ZPROP_INVAL) {
1157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1158 "invalid property '%s'"), propname);
1159 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1163 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1165 dgettext(TEXT_DOMAIN, "'%s' does not "
1166 "apply to datasets of this type"), propname);
1167 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1171 if (zfs_prop_readonly(prop) &&
1172 !(zfs_prop_setonce(prop) && zhp == NULL) &&
1173 !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1175 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1177 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1181 if (zprop_parse_value(hdl, elem, prop, type, ret,
1182 &strval, &intval, errbuf) != 0)
1186 * Perform some additional checks for specific properties.
1189 case ZFS_PROP_VERSION:
1195 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1196 if (intval < version) {
1197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1198 "Can not downgrade; already at version %u"),
1200 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1206 case ZFS_PROP_VOLBLOCKSIZE:
1207 case ZFS_PROP_RECORDSIZE:
1209 int maxbs = SPA_MAXBLOCKSIZE;
1212 if (zpool_hdl != NULL) {
1213 maxbs = zpool_get_prop_int(zpool_hdl,
1214 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1217 * The value must be a power of two between
1218 * SPA_MINBLOCKSIZE and maxbs.
1220 if (intval < SPA_MINBLOCKSIZE ||
1221 intval > maxbs || !ISP2(intval)) {
1222 zfs_nicebytes(maxbs, buf, sizeof (buf));
1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224 "'%s' must be power of 2 from 512B "
1225 "to %s"), propname, buf);
1226 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1232 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1234 int maxbs = SPA_OLD_MAXBLOCKSIZE;
1237 if (zpool_hdl != NULL) {
1238 char state[64] = "";
1240 maxbs = zpool_get_prop_int(zpool_hdl,
1241 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1244 * Issue a warning but do not fail so that
1245 * tests for settable properties succeed.
1247 if (zpool_prop_get_feature(zpool_hdl,
1248 "feature@allocation_classes", state,
1249 sizeof (state)) != 0 ||
1250 strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1251 (void) fprintf(stderr, gettext(
1252 "%s: property requires a special "
1253 "device in the pool\n"), propname);
1257 (intval < SPA_MINBLOCKSIZE ||
1258 intval > maxbs || !ISP2(intval))) {
1259 zfs_nicebytes(maxbs, buf, sizeof (buf));
1260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1261 "invalid '%s=%llu' property: must be zero "
1262 "or a power of 2 from 512B to %s"),
1263 propname, (unsigned long long)intval, buf);
1264 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1270 case ZFS_PROP_MLSLABEL:
1272 #ifdef HAVE_MLSLABEL
1274 * Verify the mlslabel string and convert to
1275 * internal hex label string.
1279 char *hex = NULL; /* internal label string */
1281 /* Default value is already OK. */
1282 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1285 /* Verify the label can be converted to binary form */
1286 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1287 (str_to_label(strval, &new_sl, MAC_LABEL,
1288 L_NO_CORRECTION, NULL) == -1)) {
1292 /* Now translate to hex internal label string */
1293 if (label_to_str(new_sl, &hex, M_INTERNAL,
1299 m_label_free(new_sl);
1301 /* If string is already in internal form, we're done. */
1302 if (strcmp(strval, hex) == 0) {
1307 /* Replace the label string with the internal form. */
1308 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1310 fnvlist_add_string(ret, zfs_prop_to_name(prop), hex);
1316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1317 "invalid mlslabel '%s'"), strval);
1318 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1319 m_label_free(new_sl); /* OK if null */
1322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1323 "mlslabels are unsupported"));
1324 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1326 #endif /* HAVE_MLSLABEL */
1329 case ZFS_PROP_MOUNTPOINT:
1331 namecheck_err_t why;
1333 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1334 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1337 if (mountpoint_namecheck(strval, &why)) {
1339 case NAME_ERR_LEADING_SLASH:
1341 dgettext(TEXT_DOMAIN,
1342 "'%s' must be an absolute path, "
1343 "'none', or 'legacy'"), propname);
1345 case NAME_ERR_TOOLONG:
1347 dgettext(TEXT_DOMAIN,
1348 "component of '%s' is too long"),
1354 dgettext(TEXT_DOMAIN,
1355 "(%d) not defined"),
1359 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1365 case ZFS_PROP_SHARESMB:
1366 case ZFS_PROP_SHARENFS:
1368 * For the mountpoint and sharenfs or sharesmb
1369 * properties, check if it can be set in a
1370 * global/non-global zone based on
1371 * the zoned property value:
1373 * global zone non-global zone
1374 * --------------------------------------------------
1375 * zoned=on mountpoint (no) mountpoint (yes)
1376 * sharenfs (no) sharenfs (no)
1377 * sharesmb (no) sharesmb (no)
1379 * zoned=off mountpoint (yes) N/A
1384 if (getzoneid() == GLOBAL_ZONEID) {
1385 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1386 "'%s' cannot be set on "
1387 "dataset in a non-global zone"),
1389 (void) zfs_error(hdl, EZFS_ZONED,
1392 } else if (prop == ZFS_PROP_SHARENFS ||
1393 prop == ZFS_PROP_SHARESMB) {
1394 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1395 "'%s' cannot be set in "
1396 "a non-global zone"), propname);
1397 (void) zfs_error(hdl, EZFS_ZONED,
1401 } else if (getzoneid() != GLOBAL_ZONEID) {
1403 * If zoned property is 'off', this must be in
1404 * a global zone. If not, something is wrong.
1406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1407 "'%s' cannot be set while dataset "
1408 "'zoned' property is set"), propname);
1409 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1414 * At this point, it is legitimate to set the
1415 * property. Now we want to make sure that the
1416 * property value is valid if it is sharenfs.
1418 if ((prop == ZFS_PROP_SHARENFS ||
1419 prop == ZFS_PROP_SHARESMB) &&
1420 strcmp(strval, "on") != 0 &&
1421 strcmp(strval, "off") != 0) {
1422 enum sa_protocol proto;
1424 if (prop == ZFS_PROP_SHARESMB)
1425 proto = SA_PROTOCOL_SMB;
1427 proto = SA_PROTOCOL_NFS;
1429 if (sa_validate_shareopts(strval, proto) !=
1431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432 "'%s' cannot be set to invalid "
1433 "options"), propname);
1434 (void) zfs_error(hdl, EZFS_BADPROP,
1442 case ZFS_PROP_KEYLOCATION:
1443 if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1445 "invalid keylocation"));
1446 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1452 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1454 if (crypt == ZIO_CRYPT_OFF &&
1455 strcmp(strval, "none") != 0) {
1456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1457 "keylocation must be 'none' "
1458 "for unencrypted datasets"));
1459 (void) zfs_error(hdl, EZFS_BADPROP,
1462 } else if (crypt != ZIO_CRYPT_OFF &&
1463 strcmp(strval, "none") == 0) {
1464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465 "keylocation must not be 'none' "
1466 "for encrypted datasets"));
1467 (void) zfs_error(hdl, EZFS_BADPROP,
1474 case ZFS_PROP_PBKDF2_ITERS:
1475 if (intval < MIN_PBKDF2_ITERATIONS) {
1476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1477 "minimum pbkdf2 iterations is %u"),
1478 MIN_PBKDF2_ITERATIONS);
1479 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1484 case ZFS_PROP_UTF8ONLY:
1485 chosen_utf = (int)intval;
1488 case ZFS_PROP_NORMALIZE:
1489 chosen_normal = (int)intval;
1497 * For changes to existing volumes, we have some additional
1498 * checks to enforce.
1500 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1501 uint64_t blocksize = zfs_prop_get_int(zhp,
1502 ZFS_PROP_VOLBLOCKSIZE);
1506 case ZFS_PROP_VOLSIZE:
1507 if (intval % blocksize != 0) {
1508 zfs_nicebytes(blocksize, buf,
1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511 "'%s' must be a multiple of "
1512 "volume block size (%s)"),
1514 (void) zfs_error(hdl, EZFS_BADPROP,
1520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1521 "'%s' cannot be zero"),
1523 (void) zfs_error(hdl, EZFS_BADPROP,
1534 /* check encryption properties */
1536 int64_t crypt = zfs_prop_get_int(zhp,
1537 ZFS_PROP_ENCRYPTION);
1540 case ZFS_PROP_COPIES:
1541 if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1543 "encrypted datasets cannot have "
1545 (void) zfs_error(hdl, EZFS_BADPROP,
1557 * If normalization was chosen, but no UTF8 choice was made,
1558 * enforce rejection of non-UTF8 names.
1560 * If normalization was chosen, but rejecting non-UTF8 names
1561 * was explicitly not chosen, it is an error.
1563 * If utf8only was turned off, but the parent has normalization,
1564 * turn off normalization.
1566 if (chosen_normal > 0 && chosen_utf < 0) {
1567 if (nvlist_add_uint64(ret,
1568 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1569 (void) no_memory(hdl);
1572 } else if (chosen_normal > 0 && chosen_utf == 0) {
1573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1574 "'%s' must be set 'on' if normalization chosen"),
1575 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1576 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1578 } else if (chosen_normal < 0 && chosen_utf == 0) {
1579 if (nvlist_add_uint64(ret,
1580 zfs_prop_to_name(ZFS_PROP_NORMALIZE), 0) != 0) {
1581 (void) no_memory(hdl);
1593 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1595 uint64_t old_volsize;
1596 uint64_t new_volsize;
1597 uint64_t old_reservation;
1598 uint64_t new_reservation;
1599 zfs_prop_t resv_prop;
1601 zpool_handle_t *zph = zpool_handle(zhp);
1604 * If this is an existing volume, and someone is setting the volsize,
1605 * make sure that it matches the reservation, or add it if necessary.
1607 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1608 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1610 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1612 props = fnvlist_alloc();
1613 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1614 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1616 if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1617 old_reservation) || nvlist_exists(nvl,
1618 zfs_prop_to_name(resv_prop))) {
1619 fnvlist_free(props);
1622 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1623 &new_volsize) != 0) {
1624 fnvlist_free(props);
1627 new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1628 fnvlist_free(props);
1630 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1631 new_reservation) != 0) {
1632 (void) no_memory(zhp->zfs_hdl);
1639 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1640 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1641 * Return codes must match zfs_add_synthetic_resv().
1644 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1651 if (!ZFS_IS_VOLUME(zhp)) {
1655 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1659 if (prop != ZFS_PROP_REFRESERVATION) {
1663 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1664 /* No value being set, so it can't be "auto" */
1667 if (resvsize != UINT64_MAX) {
1668 /* Being set to a value other than "auto" */
1672 props = fnvlist_alloc();
1674 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1675 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1677 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1679 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1682 resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1684 fnvlist_free(props);
1686 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1687 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1688 (void) no_memory(zhp->zfs_hdl);
1695 zfs_is_namespace_prop(zfs_prop_t prop)
1699 case ZFS_PROP_ATIME:
1700 case ZFS_PROP_RELATIME:
1701 case ZFS_PROP_DEVICES:
1703 case ZFS_PROP_SETUID:
1704 case ZFS_PROP_READONLY:
1705 case ZFS_PROP_XATTR:
1706 case ZFS_PROP_NBMAND:
1715 * Given a property name and value, set the property for the given dataset.
1718 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1721 char errbuf[ERRBUFLEN];
1722 libzfs_handle_t *hdl = zhp->zfs_hdl;
1723 nvlist_t *nvl = NULL;
1725 (void) snprintf(errbuf, sizeof (errbuf),
1726 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1729 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1730 nvlist_add_string(nvl, propname, propval) != 0) {
1731 (void) no_memory(hdl);
1735 ret = zfs_prop_set_list(zhp, nvl);
1745 * Given an nvlist of property names and values, set the properties for the
1749 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1751 zfs_cmd_t zc = {"\0"};
1753 prop_changelist_t **cls = NULL;
1755 char errbuf[ERRBUFLEN];
1756 libzfs_handle_t *hdl = zhp->zfs_hdl;
1760 zfs_prop_t prop = 0;
1763 (void) snprintf(errbuf, sizeof (errbuf),
1764 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1767 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1768 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1769 B_FALSE, errbuf)) == NULL)
1773 * We have to check for any extra properties which need to be added
1774 * before computing the length of the nvlist.
1776 for (elem = nvlist_next_nvpair(nvl, NULL);
1778 elem = nvlist_next_nvpair(nvl, elem)) {
1779 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1780 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1785 if (added_resv != 1 &&
1786 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1791 * Check how many properties we're setting and allocate an array to
1792 * store changelist pointers for postfix().
1794 for (elem = nvlist_next_nvpair(nvl, NULL);
1796 elem = nvlist_next_nvpair(nvl, elem))
1798 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1802 for (elem = nvlist_next_nvpair(nvl, NULL);
1804 elem = nvlist_next_nvpair(nvl, elem)) {
1806 prop = zfs_name_to_prop(nvpair_name(elem));
1808 assert(cl_idx < nvl_len);
1810 * We don't want to unmount & remount the dataset when changing
1811 * its canmount property to 'on' or 'noauto'. We only use
1812 * the changelist logic to unmount when setting canmount=off.
1814 if (prop != ZFS_PROP_CANMOUNT ||
1815 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1816 zfs_is_mounted(zhp, NULL))) {
1817 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1818 if (cls[cl_idx] == NULL)
1822 if (prop == ZFS_PROP_MOUNTPOINT &&
1823 changelist_haszonedchild(cls[cl_idx])) {
1824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825 "child dataset with inherited mountpoint is used "
1826 "in a non-global zone"));
1827 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1831 if (cls[cl_idx] != NULL &&
1832 (ret = changelist_prefix(cls[cl_idx])) != 0)
1837 assert(cl_idx == nvl_len);
1840 * Execute the corresponding ioctl() to set this list of properties.
1842 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1844 zcmd_write_src_nvlist(hdl, &zc, nvl);
1845 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
1847 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1850 if (zc.zc_nvlist_dst_filled == B_FALSE) {
1851 (void) zfs_standard_error(hdl, errno, errbuf);
1855 /* Get the list of unset properties back and report them. */
1856 nvlist_t *errorprops = NULL;
1857 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1859 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1861 elem = nvlist_next_nvpair(errorprops, elem)) {
1862 prop = zfs_name_to_prop(nvpair_name(elem));
1863 zfs_setprop_error(hdl, prop, errno, errbuf);
1865 nvlist_free(errorprops);
1867 if (added_resv && errno == ENOSPC) {
1868 /* clean up the volsize property we tried to set */
1869 uint64_t old_volsize = zfs_prop_get_int(zhp,
1873 zcmd_free_nvlists(&zc);
1875 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1877 if (nvlist_add_uint64(nvl,
1878 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1881 zcmd_write_src_nvlist(hdl, &zc, nvl);
1882 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1885 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1886 if (cls[cl_idx] != NULL) {
1887 int clp_err = changelist_postfix(cls[cl_idx]);
1895 * Refresh the statistics so the new property
1896 * value is reflected.
1898 (void) get_stats(zhp);
1901 * Remount the filesystem to propagate the change
1902 * if one of the options handled by the generic
1903 * Linux namespace layer has been modified.
1905 if (zfs_is_namespace_prop(prop) &&
1906 zfs_is_mounted(zhp, NULL))
1907 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1913 zcmd_free_nvlists(&zc);
1915 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1916 if (cls[cl_idx] != NULL)
1917 changelist_free(cls[cl_idx]);
1925 * Given a property, inherit the value from the parent dataset, or if received
1926 * is TRUE, revert to the received value, if any.
1929 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1931 zfs_cmd_t zc = {"\0"};
1933 prop_changelist_t *cl;
1934 libzfs_handle_t *hdl = zhp->zfs_hdl;
1935 char errbuf[ERRBUFLEN];
1938 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1939 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1941 zc.zc_cookie = received;
1942 if ((prop = zfs_name_to_prop(propname)) == ZPROP_USERPROP) {
1944 * For user properties, the amount of work we have to do is very
1945 * small, so just do it here.
1947 if (!zfs_prop_user(propname)) {
1948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1949 "invalid property"));
1950 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1953 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1954 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1956 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1957 return (zfs_standard_error(hdl, errno, errbuf));
1959 (void) get_stats(zhp);
1964 * Verify that this property is inheritable.
1966 if (zfs_prop_readonly(prop))
1967 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1969 if (!zfs_prop_inheritable(prop) && !received)
1970 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1973 * Check to see if the value applies to this type
1975 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
1976 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1979 * Normalize the name, to get rid of shorthand abbreviations.
1981 propname = zfs_prop_to_name(prop);
1982 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1983 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1985 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1986 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1988 "dataset is used in a non-global zone"));
1989 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1993 * Determine datasets which will be affected by this change, if any.
1995 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1998 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2000 "child dataset with inherited mountpoint is used "
2001 "in a non-global zone"));
2002 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2006 if ((ret = changelist_prefix(cl)) != 0)
2009 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
2010 changelist_free(cl);
2011 return (zfs_standard_error(hdl, errno, errbuf));
2014 if ((ret = changelist_postfix(cl)) != 0)
2018 * Refresh the statistics so the new property is reflected.
2020 (void) get_stats(zhp);
2023 * Remount the filesystem to propagate the change
2024 * if one of the options handled by the generic
2025 * Linux namespace layer has been modified.
2027 if (zfs_is_namespace_prop(prop) &&
2028 zfs_is_mounted(zhp, NULL))
2029 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
2033 changelist_free(cl);
2038 * True DSL properties are stored in an nvlist. The following two functions
2039 * extract them appropriately.
2042 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2048 if (nvlist_lookup_nvlist(zhp->zfs_props,
2049 zfs_prop_to_name(prop), &nv) == 0) {
2050 value = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
2051 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2053 verify(!zhp->zfs_props_table ||
2054 zhp->zfs_props_table[prop] == B_TRUE);
2055 value = zfs_prop_default_numeric(prop);
2056 *source = (char *)"";
2063 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2069 if (nvlist_lookup_nvlist(zhp->zfs_props,
2070 zfs_prop_to_name(prop), &nv) == 0) {
2071 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2072 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2074 verify(!zhp->zfs_props_table ||
2075 zhp->zfs_props_table[prop] == B_TRUE);
2076 value = zfs_prop_default_string(prop);
2077 *source = (char *)"";
2084 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2086 return (zhp->zfs_props == zhp->zfs_recvd_props);
2090 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2092 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2093 zhp->zfs_props = zhp->zfs_recvd_props;
2097 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2099 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2104 * Internal function for getting a numeric property. Both zfs_prop_get() and
2105 * zfs_prop_get_int() are built using this interface.
2107 * Certain properties can be overridden using 'mount -o'. In this case, scan
2108 * the contents of the /proc/self/mounts entry, searching for the
2109 * appropriate options. If they differ from the on-disk values, report the
2110 * current values and mark the source "temporary".
2113 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2114 char **source, uint64_t *val)
2116 zfs_cmd_t zc = {"\0"};
2117 nvlist_t *zplprops = NULL;
2119 const char *mntopt_on = NULL;
2120 const char *mntopt_off = NULL;
2121 boolean_t received = zfs_is_recvd_props_mode(zhp);
2126 * If the property is being fetched for a snapshot, check whether
2127 * the property is valid for the snapshot's head dataset type.
2129 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
2130 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
2131 *val = zfs_prop_default_numeric(prop);
2136 case ZFS_PROP_ATIME:
2137 mntopt_on = MNTOPT_ATIME;
2138 mntopt_off = MNTOPT_NOATIME;
2141 case ZFS_PROP_RELATIME:
2142 mntopt_on = MNTOPT_RELATIME;
2143 mntopt_off = MNTOPT_NORELATIME;
2146 case ZFS_PROP_DEVICES:
2147 mntopt_on = MNTOPT_DEVICES;
2148 mntopt_off = MNTOPT_NODEVICES;
2152 mntopt_on = MNTOPT_EXEC;
2153 mntopt_off = MNTOPT_NOEXEC;
2156 case ZFS_PROP_READONLY:
2157 mntopt_on = MNTOPT_RO;
2158 mntopt_off = MNTOPT_RW;
2161 case ZFS_PROP_SETUID:
2162 mntopt_on = MNTOPT_SETUID;
2163 mntopt_off = MNTOPT_NOSETUID;
2166 case ZFS_PROP_XATTR:
2167 mntopt_on = MNTOPT_XATTR;
2168 mntopt_off = MNTOPT_NOXATTR;
2171 case ZFS_PROP_NBMAND:
2172 mntopt_on = MNTOPT_NBMAND;
2173 mntopt_off = MNTOPT_NONBMAND;
2181 * Because looking up the mount options is potentially expensive
2182 * (iterating over all of /proc/self/mounts), we defer its
2183 * calculation until we're looking up a property which requires
2186 if (!zhp->zfs_mntcheck &&
2187 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2188 libzfs_handle_t *hdl = zhp->zfs_hdl;
2189 struct mnttab entry;
2191 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)
2192 zhp->zfs_mntopts = zfs_strdup(hdl,
2195 zhp->zfs_mntcheck = B_TRUE;
2198 if (zhp->zfs_mntopts == NULL)
2199 mnt.mnt_mntopts = (char *)"";
2201 mnt.mnt_mntopts = zhp->zfs_mntopts;
2204 case ZFS_PROP_ATIME:
2205 case ZFS_PROP_RELATIME:
2206 case ZFS_PROP_DEVICES:
2208 case ZFS_PROP_READONLY:
2209 case ZFS_PROP_SETUID:
2211 case ZFS_PROP_XATTR:
2213 case ZFS_PROP_NBMAND:
2214 *val = getprop_uint64(zhp, prop, source);
2219 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2222 *src = ZPROP_SRC_TEMPORARY;
2223 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2226 *src = ZPROP_SRC_TEMPORARY;
2230 case ZFS_PROP_CANMOUNT:
2231 case ZFS_PROP_VOLSIZE:
2232 case ZFS_PROP_QUOTA:
2233 case ZFS_PROP_REFQUOTA:
2234 case ZFS_PROP_RESERVATION:
2235 case ZFS_PROP_REFRESERVATION:
2236 case ZFS_PROP_FILESYSTEM_LIMIT:
2237 case ZFS_PROP_SNAPSHOT_LIMIT:
2238 case ZFS_PROP_FILESYSTEM_COUNT:
2239 case ZFS_PROP_SNAPSHOT_COUNT:
2240 *val = getprop_uint64(zhp, prop, source);
2242 if (*source == NULL) {
2243 /* not default, must be local */
2244 *source = zhp->zfs_name;
2248 case ZFS_PROP_MOUNTED:
2249 *val = (zhp->zfs_mntopts != NULL);
2252 case ZFS_PROP_NUMCLONES:
2253 *val = zhp->zfs_dmustats.dds_num_clones;
2256 case ZFS_PROP_VERSION:
2257 case ZFS_PROP_NORMALIZE:
2258 case ZFS_PROP_UTF8ONLY:
2260 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
2262 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2263 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2264 zcmd_free_nvlists(&zc);
2265 if (prop == ZFS_PROP_VERSION &&
2266 zhp->zfs_type == ZFS_TYPE_VOLUME)
2267 *val = zfs_prop_default_numeric(prop);
2270 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2271 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2273 zcmd_free_nvlists(&zc);
2276 nvlist_free(zplprops);
2277 zcmd_free_nvlists(&zc);
2280 case ZFS_PROP_INCONSISTENT:
2281 *val = zhp->zfs_dmustats.dds_inconsistent;
2284 case ZFS_PROP_REDACTED:
2285 *val = zhp->zfs_dmustats.dds_redacted;
2288 case ZFS_PROP_CREATETXG:
2290 * We can directly read createtxg property from zfs
2291 * handle for Filesystem, Snapshot and ZVOL types.
2293 if ((zhp->zfs_type == ZFS_TYPE_FILESYSTEM) ||
2294 (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) ||
2295 (zhp->zfs_type == ZFS_TYPE_VOLUME)) {
2296 *val = zhp->zfs_dmustats.dds_creation_txg;
2302 switch (zfs_prop_get_type(prop)) {
2303 case PROP_TYPE_NUMBER:
2304 case PROP_TYPE_INDEX:
2305 *val = getprop_uint64(zhp, prop, source);
2307 * If we tried to use a default value for a
2308 * readonly property, it means that it was not
2309 * present. Note this only applies to "truly"
2310 * readonly properties, not set-once properties
2311 * like volblocksize.
2313 if (zfs_prop_readonly(prop) &&
2314 !zfs_prop_setonce(prop) &&
2315 *source != NULL && (*source)[0] == '\0') {
2321 case PROP_TYPE_STRING:
2323 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2324 "cannot get non-numeric property"));
2325 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2326 dgettext(TEXT_DOMAIN, "internal error")));
2334 * Calculate the source type, given the raw source string.
2337 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2338 char *statbuf, size_t statlen)
2340 if (statbuf == NULL ||
2341 srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) {
2345 if (source == NULL) {
2346 *srctype = ZPROP_SRC_NONE;
2347 } else if (source[0] == '\0') {
2348 *srctype = ZPROP_SRC_DEFAULT;
2349 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2350 *srctype = ZPROP_SRC_RECEIVED;
2352 if (strcmp(source, zhp->zfs_name) == 0) {
2353 *srctype = ZPROP_SRC_LOCAL;
2355 (void) strlcpy(statbuf, source, statlen);
2356 *srctype = ZPROP_SRC_INHERITED;
2363 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2364 size_t proplen, boolean_t literal)
2369 if (zhp->zfs_recvd_props == NULL)
2370 if (get_recvd_props_ioctl(zhp) != 0)
2373 prop = zfs_name_to_prop(propname);
2375 if (prop != ZPROP_USERPROP) {
2377 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2379 zfs_set_recvd_props_mode(zhp, &cookie);
2380 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2381 NULL, NULL, 0, literal);
2382 zfs_unset_recvd_props_mode(zhp, &cookie);
2386 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2387 propname, &propval) != 0)
2389 recvdval = fnvlist_lookup_string(propval, ZPROP_VALUE);
2390 (void) strlcpy(propbuf, recvdval, proplen);
2393 return (err == 0 ? 0 : -1);
2397 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2402 value = zfs_get_clones_nvl(zhp);
2403 if (value == NULL || nvlist_empty(value))
2407 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2408 pair = nvlist_next_nvpair(value, pair)) {
2409 if (propbuf[0] != '\0')
2410 (void) strlcat(propbuf, ",", proplen);
2411 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2417 struct get_clones_arg {
2421 char buf[ZFS_MAX_DATASET_NAME_LEN];
2425 get_clones_cb(zfs_handle_t *zhp, void *arg)
2427 struct get_clones_arg *gca = arg;
2429 if (gca->numclones == 0) {
2434 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2435 NULL, NULL, 0, B_TRUE) != 0)
2437 if (strcmp(gca->buf, gca->origin) == 0) {
2438 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2443 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2449 zfs_get_clones_nvl(zfs_handle_t *zhp)
2451 nvlist_t *nv, *value;
2453 if (nvlist_lookup_nvlist(zhp->zfs_props,
2454 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2455 struct get_clones_arg gca;
2458 * if this is a snapshot, then the kernel wasn't able
2459 * to get the clones. Do it by slowly iterating.
2461 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2463 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2465 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2470 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2472 gca.origin = zhp->zfs_name;
2474 if (gca.numclones != 0) {
2476 char pool[ZFS_MAX_DATASET_NAME_LEN];
2479 /* get the pool name */
2480 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2481 (void) strsep(&cp, "/@");
2482 root = zfs_open(zhp->zfs_hdl, pool,
2483 ZFS_TYPE_FILESYSTEM);
2490 (void) get_clones_cb(root, &gca);
2493 if (gca.numclones != 0 ||
2494 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2495 nvlist_add_nvlist(zhp->zfs_props,
2496 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2503 nv = fnvlist_lookup_nvlist(zhp->zfs_props,
2504 zfs_prop_to_name(ZFS_PROP_CLONES));
2507 return (fnvlist_lookup_nvlist(nv, ZPROP_VALUE));
2511 get_rsnaps_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2517 if (nvlist_lookup_nvlist(zhp->zfs_props,
2518 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &value) != 0)
2520 if (nvlist_lookup_uint64_array(value, ZPROP_VALUE, &snaps,
2524 /* There's no redaction snapshots; pass a special value back */
2525 (void) snprintf(propbuf, proplen, "none");
2529 for (int i = 0; i < nsnaps; i++) {
2531 if (propbuf[0] != '\0')
2532 (void) strlcat(propbuf, ",", proplen);
2533 (void) snprintf(buf, sizeof (buf), "%llu",
2534 (u_longlong_t)snaps[i]);
2535 (void) strlcat(propbuf, buf, proplen);
2542 * Accepts a property and value and checks that the value
2543 * matches the one found by the channel program. If they are
2544 * not equal, print both of them.
2547 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2550 if (!zhp->zfs_hdl->libzfs_prop_debug)
2553 char *poolname = zhp->zpool_hdl->zpool_name;
2554 const char *prop_name = zfs_prop_to_name(prop);
2555 const char *program =
2557 "ds = args['dataset']\n"
2558 "prop = args['property']\n"
2559 "value, setpoint = zfs.get_prop(ds, prop)\n"
2560 "return {value=value, setpoint=setpoint}\n";
2563 nvlist_t *argnvl = fnvlist_alloc();
2565 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2566 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2568 error = lzc_channel_program_nosync(poolname, program,
2569 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2572 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2573 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2575 error = nvlist_lookup_int64(retnvl, "value", &ans);
2577 (void) fprintf(stderr, "%s: zcp check error: "
2578 "%u\n", prop_name, error);
2581 if (ans != intval) {
2582 (void) fprintf(stderr, "%s: zfs found %llu, "
2583 "but zcp found %llu\n", prop_name,
2584 (u_longlong_t)intval, (u_longlong_t)ans);
2588 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2590 (void) fprintf(stderr, "%s: zcp check error: "
2591 "%u\n", prop_name, error);
2594 if (strcmp(strval, str_ans) != 0) {
2595 (void) fprintf(stderr,
2596 "%s: zfs found '%s', but zcp found '%s'\n",
2597 prop_name, strval, str_ans);
2601 (void) fprintf(stderr, "%s: zcp check failed, channel program "
2602 "error: %u\n", prop_name, error);
2604 nvlist_free(argnvl);
2605 nvlist_free(outnvl);
2609 * Retrieve a property from the given object. If 'literal' is specified, then
2610 * numbers are left as exact values. Otherwise, numbers are converted to a
2611 * human-readable form.
2613 * Returns 0 on success, or -1 on error.
2616 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2617 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2619 char *source = NULL;
2623 boolean_t received = zfs_is_recvd_props_mode(zhp);
2626 * Check to see if this property applies to our object
2628 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2631 if (received && zfs_prop_readonly(prop))
2635 *src = ZPROP_SRC_NONE;
2638 case ZFS_PROP_CREATION:
2640 * 'creation' is a time_t stored in the statistics. We convert
2641 * this into a string unless 'literal' is specified.
2644 val = getprop_uint64(zhp, prop, &source);
2645 time_t time = (time_t)val;
2649 localtime_r(&time, &t) == NULL ||
2650 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2652 (void) snprintf(propbuf, proplen, "%llu",
2655 zcp_check(zhp, prop, val, NULL);
2658 case ZFS_PROP_MOUNTPOINT:
2660 * Getting the precise mountpoint can be tricky.
2662 * - for 'none' or 'legacy', return those values.
2663 * - for inherited mountpoints, we want to take everything
2664 * after our ancestor and append it to the inherited value.
2666 * If the pool has an alternate root, we want to prepend that
2667 * root to any values we return.
2670 str = getprop_string(zhp, prop, &source);
2672 if (str[0] == '/') {
2673 char buf[MAXPATHLEN];
2675 const char *relpath;
2678 * If we inherit the mountpoint, even from a dataset
2679 * with a received value, the source will be the path of
2680 * the dataset we inherit from. If source is
2681 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2684 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2687 relpath = zhp->zfs_name + strlen(source);
2688 if (relpath[0] == '/')
2692 if ((zpool_get_prop(zhp->zpool_hdl,
2693 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2694 B_FALSE)) || (strcmp(root, "-") == 0))
2697 * Special case an alternate root of '/'. This will
2698 * avoid having multiple leading slashes in the
2701 if (strcmp(root, "/") == 0)
2705 * If the mountpoint is '/' then skip over this
2706 * if we are obtaining either an alternate root or
2707 * an inherited mountpoint.
2709 if (str[1] == '\0' && (root[0] != '\0' ||
2710 relpath[0] != '\0'))
2713 if (relpath[0] == '\0')
2714 (void) snprintf(propbuf, proplen, "%s%s",
2717 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2718 root, str, relpath[0] == '@' ? "" : "/",
2721 /* 'legacy' or 'none' */
2722 (void) strlcpy(propbuf, str, proplen);
2724 zcp_check(zhp, prop, 0, propbuf);
2727 case ZFS_PROP_ORIGIN:
2728 str = getprop_string(zhp, prop, &source);
2731 (void) strlcpy(propbuf, str, proplen);
2732 zcp_check(zhp, prop, 0, str);
2735 case ZFS_PROP_REDACT_SNAPS:
2736 if (get_rsnaps_string(zhp, propbuf, proplen) != 0)
2740 case ZFS_PROP_CLONES:
2741 if (get_clones_string(zhp, propbuf, proplen) != 0)
2745 case ZFS_PROP_QUOTA:
2746 case ZFS_PROP_REFQUOTA:
2747 case ZFS_PROP_RESERVATION:
2748 case ZFS_PROP_REFRESERVATION:
2750 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2753 * If quota or reservation is 0, we translate this into 'none'
2754 * (unless literal is set), and indicate that it's the default
2755 * value. Otherwise, we print the number nicely and indicate
2756 * that its set locally.
2760 (void) strlcpy(propbuf, "0", proplen);
2762 (void) strlcpy(propbuf, "none", proplen);
2765 (void) snprintf(propbuf, proplen, "%llu",
2768 zfs_nicebytes(val, propbuf, proplen);
2770 zcp_check(zhp, prop, val, NULL);
2773 case ZFS_PROP_FILESYSTEM_LIMIT:
2774 case ZFS_PROP_SNAPSHOT_LIMIT:
2775 case ZFS_PROP_FILESYSTEM_COUNT:
2776 case ZFS_PROP_SNAPSHOT_COUNT:
2778 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2782 * If limit is UINT64_MAX, we translate this into 'none', and
2783 * indicate that it's the default value. Otherwise, we print
2784 * the number nicely and indicate that it's set locally.
2786 if (val == UINT64_MAX) {
2787 (void) strlcpy(propbuf, "none", proplen);
2788 } else if (literal) {
2789 (void) snprintf(propbuf, proplen, "%llu",
2792 zfs_nicenum(val, propbuf, proplen);
2795 zcp_check(zhp, prop, val, NULL);
2798 case ZFS_PROP_REFRATIO:
2799 case ZFS_PROP_COMPRESSRATIO:
2800 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2803 (void) snprintf(propbuf, proplen, "%llu.%02llu",
2804 (u_longlong_t)(val / 100),
2805 (u_longlong_t)(val % 100));
2807 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2808 (u_longlong_t)(val / 100),
2809 (u_longlong_t)(val % 100));
2810 zcp_check(zhp, prop, val, NULL);
2814 switch (zhp->zfs_type) {
2815 case ZFS_TYPE_FILESYSTEM:
2818 case ZFS_TYPE_VOLUME:
2821 case ZFS_TYPE_SNAPSHOT:
2824 case ZFS_TYPE_BOOKMARK:
2830 (void) snprintf(propbuf, proplen, "%s", str);
2831 zcp_check(zhp, prop, 0, propbuf);
2834 case ZFS_PROP_MOUNTED:
2836 * The 'mounted' property is a pseudo-property that described
2837 * whether the filesystem is currently mounted. Even though
2838 * it's a boolean value, the typical values of "on" and "off"
2839 * don't make sense, so we translate to "yes" and "no".
2841 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2842 src, &source, &val) != 0)
2845 (void) strlcpy(propbuf, "yes", proplen);
2847 (void) strlcpy(propbuf, "no", proplen);
2852 * The 'name' property is a pseudo-property derived from the
2853 * dataset name. It is presented as a real property to simplify
2856 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2857 zcp_check(zhp, prop, 0, propbuf);
2860 case ZFS_PROP_MLSLABEL:
2862 #ifdef HAVE_MLSLABEL
2863 m_label_t *new_sl = NULL;
2864 char *ascii = NULL; /* human readable label */
2866 (void) strlcpy(propbuf,
2867 getprop_string(zhp, prop, &source), proplen);
2869 if (literal || (strcasecmp(propbuf,
2870 ZFS_MLSLABEL_DEFAULT) == 0))
2874 * Try to translate the internal hex string to
2875 * human-readable output. If there are any
2876 * problems just use the hex string.
2879 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2880 L_NO_CORRECTION, NULL) == -1) {
2881 m_label_free(new_sl);
2885 if (label_to_str(new_sl, &ascii, M_LABEL,
2889 m_label_free(new_sl);
2892 m_label_free(new_sl);
2894 (void) strlcpy(propbuf, ascii, proplen);
2897 (void) strlcpy(propbuf,
2898 getprop_string(zhp, prop, &source), proplen);
2899 #endif /* HAVE_MLSLABEL */
2904 case ZFS_PROP_KEY_GUID:
2905 case ZFS_PROP_IVSET_GUID:
2906 case ZFS_PROP_CREATETXG:
2907 case ZFS_PROP_OBJSETID:
2908 case ZFS_PROP_PBKDF2_ITERS:
2910 * These properties are stored as numbers, but they are
2911 * identifiers or counters.
2912 * We don't want them to be pretty printed, because pretty
2913 * printing truncates their values making them useless.
2915 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2917 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2918 zcp_check(zhp, prop, val, NULL);
2921 case ZFS_PROP_REFERENCED:
2922 case ZFS_PROP_AVAILABLE:
2924 case ZFS_PROP_USEDSNAP:
2925 case ZFS_PROP_USEDDS:
2926 case ZFS_PROP_USEDREFRESERV:
2927 case ZFS_PROP_USEDCHILD:
2928 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2931 (void) snprintf(propbuf, proplen, "%llu",
2934 zfs_nicebytes(val, propbuf, proplen);
2936 zcp_check(zhp, prop, val, NULL);
2939 case ZFS_PROP_SNAPSHOTS_CHANGED:
2941 if ((get_numeric_property(zhp, prop, src, &source,
2942 &val) != 0) || val == 0) {
2946 time_t time = (time_t)val;
2950 localtime_r(&time, &t) == NULL ||
2951 strftime(propbuf, proplen, "%a %b %e %k:%M:%S %Y",
2953 (void) snprintf(propbuf, proplen, "%llu",
2956 zcp_check(zhp, prop, val, NULL);
2960 switch (zfs_prop_get_type(prop)) {
2961 case PROP_TYPE_NUMBER:
2962 if (get_numeric_property(zhp, prop, src,
2963 &source, &val) != 0) {
2968 (void) snprintf(propbuf, proplen, "%llu",
2971 zfs_nicenum(val, propbuf, proplen);
2973 zcp_check(zhp, prop, val, NULL);
2976 case PROP_TYPE_STRING:
2977 str = getprop_string(zhp, prop, &source);
2981 (void) strlcpy(propbuf, str, proplen);
2982 zcp_check(zhp, prop, 0, str);
2985 case PROP_TYPE_INDEX:
2986 if (get_numeric_property(zhp, prop, src,
2987 &source, &val) != 0)
2989 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2992 (void) strlcpy(propbuf, strval, proplen);
2993 zcp_check(zhp, prop, 0, strval);
3001 get_source(zhp, src, source, statbuf, statlen);
3007 * Utility function to get the given numeric property. Does no validation that
3008 * the given property is the appropriate type; should only be used with
3009 * hard-coded property types.
3012 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
3017 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
3023 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
3027 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
3028 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
3032 * Similar to zfs_prop_get(), but returns the value as an integer.
3035 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
3036 zprop_source_t *src, char *statbuf, size_t statlen)
3041 * Check to see if this property applies to our object
3043 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
3044 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
3045 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
3046 zfs_prop_to_name(prop)));
3050 *src = ZPROP_SRC_NONE;
3052 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
3055 get_source(zhp, src, source, statbuf, statlen);
3062 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
3063 char **domainp, idmap_rid_t *ridp)
3065 idmap_get_handle_t *get_hdl = NULL;
3069 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
3073 err = idmap_get_sidbyuid(get_hdl, id,
3074 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3076 err = idmap_get_sidbygid(get_hdl, id,
3077 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3079 if (err == IDMAP_SUCCESS &&
3080 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
3081 status == IDMAP_SUCCESS)
3087 idmap_get_destroy(get_hdl);
3090 #endif /* HAVE_IDMAP */
3093 * convert the propname into parameters needed by kernel
3094 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3095 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3096 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3097 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3098 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3099 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3102 userquota_propname_decode(const char *propname, boolean_t zoned,
3103 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
3105 zfs_userquota_prop_t type;
3109 boolean_t isproject;
3115 /* Figure out the property type ({user|group|project}{quota|space}) */
3116 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
3117 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
3118 strlen(zfs_userquota_prop_prefixes[type])) == 0)
3121 if (type == ZFS_NUM_USERQUOTA_PROPS)
3125 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3126 type == ZFS_PROP_USEROBJQUOTA ||
3127 type == ZFS_PROP_USEROBJUSED);
3128 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3129 type == ZFS_PROP_GROUPOBJQUOTA ||
3130 type == ZFS_PROP_GROUPOBJUSED);
3131 isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3132 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3133 type == ZFS_PROP_PROJECTOBJUSED);
3135 cp = strchr(propname, '@') + 1;
3137 if (isuser && (pw = getpwnam(cp)) != NULL) {
3138 if (zoned && getzoneid() == GLOBAL_ZONEID)
3141 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3142 if (zoned && getzoneid() == GLOBAL_ZONEID)
3145 } else if (!isproject && strchr(cp, '@')) {
3148 * It's a SID name (eg "user@domain") that needs to be
3149 * turned into S-1-domainID-RID.
3151 directory_error_t e;
3152 char *numericsid = NULL;
3155 if (zoned && getzoneid() == GLOBAL_ZONEID)
3158 e = directory_sid_from_user_name(NULL,
3161 e = directory_sid_from_group_name(NULL,
3165 directory_error_free(e);
3168 if (numericsid == NULL)
3171 (void) strlcpy(domain, cp, domainlen);
3172 cp = strrchr(domain, '-');
3177 *ridp = strtoull(cp, &end, 10);
3180 if (errno != 0 || *end != '\0')
3185 #endif /* HAVE_IDMAP */
3187 /* It's a user/group/project ID (eg "12345"). */
3190 id = strtoul(cp, &end, 10);
3193 if (id > MAXUID && !isproject) {
3195 /* It's an ephemeral ID. */
3199 if (idmap_id_to_numeric_domain_rid(id, isuser,
3200 &mapdomain, &rid) != 0)
3202 (void) strlcpy(domain, mapdomain, domainlen);
3206 #endif /* HAVE_IDMAP */
3216 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3217 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3220 zfs_cmd_t zc = {"\0"};
3222 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3224 err = userquota_propname_decode(propname,
3225 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3226 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3227 zc.zc_objset_type = *typep;
3231 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_USERSPACE_ONE, &zc);
3235 *propvalue = zc.zc_cookie;
3240 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3241 uint64_t *propvalue)
3243 zfs_userquota_prop_t type;
3245 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3250 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3251 char *propbuf, int proplen, boolean_t literal)
3255 zfs_userquota_prop_t type;
3257 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3264 (void) snprintf(propbuf, proplen, "%llu",
3265 (u_longlong_t)propvalue);
3266 } else if (propvalue == 0 &&
3267 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3268 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3269 type == ZFS_PROP_PROJECTQUOTA ||
3270 type == ZFS_PROP_PROJECTOBJQUOTA)) {
3271 (void) strlcpy(propbuf, "none", proplen);
3272 } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3273 type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3274 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3275 zfs_nicebytes(propvalue, propbuf, proplen);
3277 zfs_nicenum(propvalue, propbuf, proplen);
3283 * propname must start with "written@" or "written#".
3286 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3287 uint64_t *propvalue)
3290 zfs_cmd_t zc = {"\0"};
3291 const char *snapname;
3293 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3295 assert(zfs_prop_written(propname));
3296 snapname = propname + strlen("written@");
3297 if (strchr(snapname, '@') != NULL || strchr(snapname, '#') != NULL) {
3298 /* full snapshot or bookmark name specified */
3299 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3301 /* snapname is the short name, append it to zhp's fsname */
3304 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3305 sizeof (zc.zc_value));
3306 cp = strchr(zc.zc_value, '@');
3309 (void) strlcat(zc.zc_value, snapname - 1, sizeof (zc.zc_value));
3312 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SPACE_WRITTEN, &zc);
3316 *propvalue = zc.zc_cookie;
3321 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3322 char *propbuf, int proplen, boolean_t literal)
3327 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3333 (void) snprintf(propbuf, proplen, "%llu",
3334 (u_longlong_t)propvalue);
3336 zfs_nicebytes(propvalue, propbuf, proplen);
3343 * Returns the name of the given zfs handle.
3346 zfs_get_name(const zfs_handle_t *zhp)
3348 return (zhp->zfs_name);
3352 * Returns the name of the parent pool for the given zfs handle.
3355 zfs_get_pool_name(const zfs_handle_t *zhp)
3357 return (zhp->zpool_hdl->zpool_name);
3361 * Returns the type of the given zfs handle.
3364 zfs_get_type(const zfs_handle_t *zhp)
3366 return (zhp->zfs_type);
3370 * Returns the type of the given zfs handle,
3371 * or, if a snapshot, the type of the snapshotted dataset.
3374 zfs_get_underlying_type(const zfs_handle_t *zhp)
3376 return (zhp->zfs_head_type);
3380 * Is one dataset name a child dataset of another?
3382 * Needs to handle these cases:
3383 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3384 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3385 * Descendant? No. No. No. Yes.
3388 is_descendant(const char *ds1, const char *ds2)
3390 size_t d1len = strlen(ds1);
3392 /* ds2 can't be a descendant if it's smaller */
3393 if (strlen(ds2) < d1len)
3396 /* otherwise, compare strings and verify that there's a '/' char */
3397 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3401 * Given a complete name, return just the portion that refers to the parent.
3402 * Will return -1 if there is no parent (path is just the name of the
3406 parent_name(const char *path, char *buf, size_t buflen)
3410 (void) strlcpy(buf, path, buflen);
3412 if ((slashp = strrchr(buf, '/')) == NULL)
3420 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3422 return (parent_name(zfs_get_name(zhp), buf, buflen));
3426 * If accept_ancestor is false, then check to make sure that the given path has
3427 * a parent, and that it exists. If accept_ancestor is true, then find the
3428 * closest existing ancestor for the given path. In prefixlen return the
3429 * length of already existing prefix of the given path. We also fetch the
3430 * 'zoned' property, which is used to validate property settings when creating
3434 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3435 boolean_t accept_ancestor, int *prefixlen)
3437 zfs_cmd_t zc = {"\0"};
3438 char parent[ZFS_MAX_DATASET_NAME_LEN];
3441 char errbuf[ERRBUFLEN];
3444 (void) snprintf(errbuf, sizeof (errbuf),
3445 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3447 /* get parent, and check to see if this is just a pool */
3448 if (parent_name(path, parent, sizeof (parent)) != 0) {
3449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3450 "missing dataset name"));
3451 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3454 /* check to see if the pool exists */
3455 if ((slash = strchr(parent, '/')) == NULL)
3456 slash = parent + strlen(parent);
3457 (void) strlcpy(zc.zc_name, parent,
3458 MIN(sizeof (zc.zc_name), slash - parent + 1));
3459 if (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3462 "no such pool '%s'"), zc.zc_name);
3463 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3466 /* check to see if the parent dataset exists */
3467 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3468 if (errno == ENOENT && accept_ancestor) {
3470 * Go deeper to find an ancestor, give up on top level.
3472 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3474 "no such pool '%s'"), zc.zc_name);
3475 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3477 } else if (errno == ENOENT) {
3478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3479 "parent does not exist"));
3480 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3482 return (zfs_standard_error(hdl, errno, errbuf));
3485 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3489 /* we are in a non-global zone, but parent is in the global zone */
3490 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3491 (void) zfs_standard_error(hdl, EPERM, errbuf);
3496 /* make sure parent is a filesystem */
3497 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3499 "parent is not a filesystem"));
3500 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3506 if (prefixlen != NULL)
3507 *prefixlen = strlen(parent);
3512 * Finds whether the dataset of the given type(s) exists.
3515 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3519 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3523 * Try to get stats for the dataset, which will tell us if it exists.
3525 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3526 int ds_type = zhp->zfs_type;
3529 if (types & ds_type)
3536 * Given a path to 'target', create all the ancestors between
3537 * the prefixlen portion of the path, and the target itself.
3538 * Fail if the initial prefixlen-ancestor does not already exist.
3541 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3547 /* make sure prefix exists */
3548 cp = target + prefixlen;
3550 assert(strchr(cp, '/') == NULL);
3551 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3554 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3562 * Attempt to create, mount, and share any ancestor filesystems,
3563 * up to the prefixlen-long one.
3565 for (cp = target + prefixlen + 1;
3566 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3570 h = make_dataset_handle(hdl, target);
3572 /* it already exists, nothing to do here */
3577 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3579 opname = dgettext(TEXT_DOMAIN, "create");
3583 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3585 opname = dgettext(TEXT_DOMAIN, "open");
3589 if (zfs_mount(h, NULL, 0) != 0) {
3590 opname = dgettext(TEXT_DOMAIN, "mount");
3594 if (zfs_share(h, NULL) != 0) {
3595 opname = dgettext(TEXT_DOMAIN, "share");
3601 zfs_commit_shares(NULL);
3606 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3607 "failed to %s ancestor '%s'"), opname, target);
3612 * Creates non-existing ancestors of the given path.
3615 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3619 char errbuf[ERRBUFLEN];
3622 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3623 "cannot create '%s'"), path);
3626 * Check that we are not passing the nesting limit
3627 * before we start creating any ancestors.
3629 if (dataset_nestcheck(path) != 0) {
3630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3631 "maximum name nesting depth exceeded"));
3632 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3635 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3638 if ((path_copy = strdup(path)) != NULL) {
3639 rc = create_parents(hdl, path_copy, prefix);
3642 if (path_copy == NULL || rc != 0)
3649 * Create a new filesystem or volume.
3652 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3657 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3659 enum lzc_dataset_type ost;
3660 zpool_handle_t *zpool_handle;
3661 uint8_t *wkeydata = NULL;
3663 char errbuf[ERRBUFLEN];
3664 char parent[ZFS_MAX_DATASET_NAME_LEN];
3666 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3667 "cannot create '%s'"), path);
3669 /* validate the path, taking care to note the extended error message */
3670 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3671 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3673 if (dataset_nestcheck(path) != 0) {
3674 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3675 "maximum name nesting depth exceeded"));
3676 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3679 /* validate parents exist */
3680 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3684 * The failure modes when creating a dataset of a different type over
3685 * one that already exists is a little strange. In particular, if you
3686 * try to create a dataset on top of an existing dataset, the ioctl()
3687 * will return ENOENT, not EEXIST. To prevent this from happening, we
3688 * first try to see if the dataset exists.
3690 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3692 "dataset already exists"));
3693 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3696 if (type == ZFS_TYPE_VOLUME)
3697 ost = LZC_DATSET_TYPE_ZVOL;
3699 ost = LZC_DATSET_TYPE_ZFS;
3701 /* open zpool handle for prop validation */
3702 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3703 (void) strlcpy(pool_path, path, sizeof (pool_path));
3705 /* truncate pool_path at first slash */
3706 char *p = strchr(pool_path, '/');
3710 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3713 if (props && (props = zfs_valid_proplist(hdl, type, props,
3714 zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3715 zpool_close(zpool_handle);
3718 zpool_close(zpool_handle);
3720 if (type == ZFS_TYPE_VOLUME) {
3722 * If we are creating a volume, the size and block size must
3723 * satisfy a few restraints. First, the blocksize must be a
3724 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3725 * volsize must be a multiple of the block size, and cannot be
3728 if (props == NULL || nvlist_lookup_uint64(props,
3729 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3731 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3732 "missing volume size"));
3733 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3736 if ((ret = nvlist_lookup_uint64(props,
3737 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3738 &blocksize)) != 0) {
3739 if (ret == ENOENT) {
3740 blocksize = zfs_prop_default_numeric(
3741 ZFS_PROP_VOLBLOCKSIZE);
3744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3745 "missing volume block size"));
3746 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3753 "volume size cannot be zero"));
3754 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3757 if (size % blocksize != 0) {
3759 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3760 "volume size must be a multiple of volume block "
3762 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3766 (void) parent_name(path, parent, sizeof (parent));
3767 if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
3768 &wkeydata, &wkeylen) != 0) {
3770 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3773 /* create the dataset */
3774 ret = lzc_create(path, ost, props, wkeydata, wkeylen);
3776 if (wkeydata != NULL)
3779 /* check for failure */
3783 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3784 "no such parent '%s'"), parent);
3785 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3789 "pool must be upgraded to set this "
3790 "property or value"));
3791 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3795 "encryption root's key is not loaded "
3797 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3801 "invalid property value(s) specified"));
3802 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3806 * This platform can't address a volume this big.
3808 if (type == ZFS_TYPE_VOLUME)
3809 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3814 return (zfs_standard_error(hdl, errno, errbuf));
3822 * Destroys the given dataset. The caller must make sure that the filesystem
3823 * isn't mounted, and that there are no active dependents. If the file system
3824 * does not exist this function does nothing.
3827 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3831 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3834 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3835 nvlist_t *nv = fnvlist_alloc();
3836 fnvlist_add_boolean(nv, zhp->zfs_name);
3837 error = lzc_destroy_bookmarks(nv, NULL);
3840 return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3841 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3847 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3848 nvlist_t *nv = fnvlist_alloc();
3849 fnvlist_add_boolean(nv, zhp->zfs_name);
3850 error = lzc_destroy_snaps(nv, defer, NULL);
3853 error = lzc_destroy(zhp->zfs_name);
3856 if (error != 0 && error != ENOENT) {
3857 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3858 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3862 remove_mountpoint(zhp);
3867 struct destroydata {
3869 const char *snapname;
3873 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3875 struct destroydata *dd = arg;
3876 char name[ZFS_MAX_DATASET_NAME_LEN];
3879 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
3880 dd->snapname) >= sizeof (name))
3883 if (lzc_exists(name))
3884 fnvlist_add_boolean(dd->nvl, name);
3886 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3892 * Destroys all snapshots with the given name in zhp & descendants.
3895 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3898 struct destroydata dd = { 0 };
3900 dd.snapname = snapname;
3901 dd.nvl = fnvlist_alloc();
3902 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3904 if (nvlist_empty(dd.nvl)) {
3905 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3906 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3907 zhp->zfs_name, snapname);
3909 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3911 fnvlist_free(dd.nvl);
3916 * Destroys all the snapshots named in the nvlist.
3919 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3921 nvlist_t *errlist = NULL;
3924 int ret = zfs_destroy_snaps_nvl_os(hdl, snaps);
3928 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3931 nvlist_free(errlist);
3935 if (nvlist_empty(errlist)) {
3936 char errbuf[ERRBUFLEN];
3937 (void) snprintf(errbuf, sizeof (errbuf),
3938 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3940 ret = zfs_standard_error(hdl, ret, errbuf);
3942 for (pair = nvlist_next_nvpair(errlist, NULL);
3943 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3944 char errbuf[ERRBUFLEN];
3945 (void) snprintf(errbuf, sizeof (errbuf),
3946 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3949 switch (fnvpair_value_int32(pair)) {
3952 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3953 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3956 ret = zfs_standard_error(hdl, errno, errbuf);
3961 nvlist_free(errlist);
3966 * Clones the given dataset. The target must be of the same type as the source.
3969 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3971 char parent[ZFS_MAX_DATASET_NAME_LEN];
3973 char errbuf[ERRBUFLEN];
3974 libzfs_handle_t *hdl = zhp->zfs_hdl;
3977 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3979 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3980 "cannot create '%s'"), target);
3982 /* validate the target/clone name */
3983 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3984 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3986 /* validate parents exist */
3987 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3990 (void) parent_name(target, parent, sizeof (parent));
3995 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
3997 if (ZFS_IS_VOLUME(zhp))
3998 type = ZFS_TYPE_VOLUME;
3999 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
4000 zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
4002 if (zfs_fix_auto_resv(zhp, props) == -1) {
4008 if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
4010 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
4013 ret = lzc_clone(target, zhp->zfs_name, props);
4021 * The parent doesn't exist. We should have caught this
4022 * above, but there may a race condition that has since
4023 * destroyed the parent.
4025 * At this point, we don't know whether it's the source
4026 * that doesn't exist anymore, or whether the target
4027 * dataset doesn't exist.
4029 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4030 "no such parent '%s'"), parent);
4031 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
4034 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4035 "source and target pools differ"));
4036 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
4040 return (zfs_standard_error(zhp->zfs_hdl, errno,
4049 * Promotes the given clone fs to be the clone parent.
4052 zfs_promote(zfs_handle_t *zhp)
4054 libzfs_handle_t *hdl = zhp->zfs_hdl;
4055 char snapname[ZFS_MAX_DATASET_NAME_LEN];
4057 char errbuf[ERRBUFLEN];
4059 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4060 "cannot promote '%s'"), zhp->zfs_name);
4062 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4064 "snapshots can not be promoted"));
4065 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4068 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
4069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4070 "not a cloned filesystem"));
4071 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4074 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4075 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4077 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
4083 * Promoting encrypted dataset outside its
4086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4087 "cannot promote dataset outside its "
4088 "encryption root"));
4089 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4092 /* There is a conflicting snapshot name. */
4093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4094 "conflicting snapshot '%s' from parent '%s'"),
4095 snapname, zhp->zfs_dmustats.dds_origin);
4096 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4099 return (zfs_standard_error(hdl, ret, errbuf));
4105 typedef struct snapdata {
4107 const char *sd_snapname;
4111 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4113 snapdata_t *sd = arg;
4114 char name[ZFS_MAX_DATASET_NAME_LEN];
4117 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4118 if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp),
4119 sd->sd_snapname) >= sizeof (name))
4122 fnvlist_add_boolean(sd->sd_nvl, name);
4124 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4132 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4136 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4139 char errbuf[ERRBUFLEN];
4142 zpool_handle_t *zpool_hdl;
4143 char pool[ZFS_MAX_DATASET_NAME_LEN];
4145 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4146 "cannot create snapshots "));
4149 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
4150 const char *snapname = nvpair_name(elem);
4152 /* validate the target name */
4153 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
4155 (void) snprintf(errbuf, sizeof (errbuf),
4156 dgettext(TEXT_DOMAIN,
4157 "cannot create snapshot '%s'"), snapname);
4158 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4163 * get pool handle for prop validation. assumes all snaps are in the
4164 * same pool, as does lzc_snapshot (below).
4166 elem = nvlist_next_nvpair(snaps, NULL);
4167 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4168 pool[strcspn(pool, "/@")] = '\0';
4169 zpool_hdl = zpool_open(hdl, pool);
4170 if (zpool_hdl == NULL)
4173 if (props != NULL &&
4174 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4175 props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4176 zpool_close(zpool_hdl);
4179 zpool_close(zpool_hdl);
4181 ret = lzc_snapshot(snaps, props, &errors);
4184 boolean_t printed = B_FALSE;
4185 for (elem = nvlist_next_nvpair(errors, NULL);
4187 elem = nvlist_next_nvpair(errors, elem)) {
4188 (void) snprintf(errbuf, sizeof (errbuf),
4189 dgettext(TEXT_DOMAIN,
4190 "cannot create snapshot '%s'"), nvpair_name(elem));
4191 (void) zfs_standard_error(hdl,
4192 fnvpair_value_int32(elem), errbuf);
4198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4199 "multiple snapshots of same "
4201 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4205 (void) zfs_standard_error(hdl, ret, errbuf);
4211 nvlist_free(errors);
4216 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4220 snapdata_t sd = { 0 };
4221 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4224 char errbuf[ERRBUFLEN];
4226 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4227 "cannot snapshot %s"), path);
4229 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4230 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4232 (void) strlcpy(fsname, path, sizeof (fsname));
4233 cp = strchr(fsname, '@');
4235 sd.sd_snapname = cp + 1;
4237 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4238 ZFS_TYPE_VOLUME)) == NULL) {
4242 sd.sd_nvl = fnvlist_alloc();
4244 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4246 fnvlist_add_boolean(sd.sd_nvl, path);
4249 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4250 fnvlist_free(sd.sd_nvl);
4256 * Destroy any more recent snapshots. We invoke this callback on any dependents
4257 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4258 * is a dependent and we should just destroy it without checking the transaction
4261 typedef struct rollback_data {
4262 const char *cb_target; /* the snapshot */
4263 uint64_t cb_create; /* creation time reference */
4269 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4271 rollback_data_t *cbp = data;
4272 prop_changelist_t *clp;
4274 /* We must destroy this clone; first unmount it */
4275 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4276 cbp->cb_force ? MS_FORCE: 0);
4277 if (clp == NULL || changelist_prefix(clp) != 0) {
4278 cbp->cb_error = B_TRUE;
4282 if (zfs_destroy(zhp, B_FALSE) != 0)
4283 cbp->cb_error = B_TRUE;
4285 changelist_remove(clp, zhp->zfs_name);
4286 (void) changelist_postfix(clp);
4287 changelist_free(clp);
4294 rollback_destroy(zfs_handle_t *zhp, void *data)
4296 rollback_data_t *cbp = data;
4298 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4299 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4300 rollback_destroy_dependent, cbp);
4302 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4310 * Given a dataset, rollback to a specific snapshot, discarding any
4311 * data changes since then and making it the active dataset.
4313 * Any snapshots and bookmarks more recent than the target are
4314 * destroyed, along with their dependents (i.e. clones).
4317 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4319 rollback_data_t cb = { 0 };
4321 boolean_t restore_resv = 0;
4322 uint64_t old_volsize = 0, new_volsize;
4323 zfs_prop_t resv_prop = { 0 };
4324 uint64_t min_txg = 0;
4326 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4327 zhp->zfs_type == ZFS_TYPE_VOLUME);
4330 * Destroy all recent snapshots and their dependents.
4332 cb.cb_force = force;
4333 cb.cb_target = snap->zfs_name;
4334 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4336 if (cb.cb_create > 0)
4337 min_txg = cb.cb_create;
4339 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb,
4342 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4348 * Now that we have verified that the snapshot is the latest,
4349 * rollback to the given snapshot.
4352 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4353 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4355 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4357 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4361 * Pass both the filesystem and the wanted snapshot names,
4362 * we would get an error back if the snapshot is destroyed or
4363 * a new snapshot is created before this request is processed.
4365 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4367 char errbuf[ERRBUFLEN];
4369 (void) snprintf(errbuf, sizeof (errbuf),
4370 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4374 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4375 "there is a snapshot or bookmark more recent "
4376 "than '%s'"), snap->zfs_name);
4377 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4380 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4381 "'%s' is not found among snapshots of '%s'"),
4382 snap->zfs_name, zhp->zfs_name);
4383 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4386 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4389 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4395 * For volumes, if the pre-rollback volsize matched the pre-
4396 * rollback reservation and the volsize has changed then set
4397 * the reservation property to the post-rollback volsize.
4398 * Make a new handle since the rollback closed the dataset.
4400 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4401 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4403 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4404 if (old_volsize != new_volsize)
4405 err = zfs_prop_set_int(zhp, resv_prop,
4414 * Renames the given dataset.
4417 zfs_rename(zfs_handle_t *zhp, const char *target, renameflags_t flags)
4420 zfs_cmd_t zc = {"\0"};
4422 prop_changelist_t *cl = NULL;
4423 char parent[ZFS_MAX_DATASET_NAME_LEN];
4424 char property[ZFS_MAXPROPLEN];
4425 libzfs_handle_t *hdl = zhp->zfs_hdl;
4426 char errbuf[ERRBUFLEN];
4428 /* if we have the same exact name, just return success */
4429 if (strcmp(zhp->zfs_name, target) == 0)
4432 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4433 "cannot rename to '%s'"), target);
4435 /* make sure source name is valid */
4436 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4437 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4440 * Make sure the target name is valid
4442 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4443 if ((strchr(target, '@') == NULL) ||
4446 * Snapshot target name is abbreviated,
4447 * reconstruct full dataset name
4449 (void) strlcpy(parent, zhp->zfs_name,
4451 delim = strchr(parent, '@');
4452 if (strchr(target, '@') == NULL)
4456 (void) strlcat(parent, target, sizeof (parent));
4460 * Make sure we're renaming within the same dataset.
4462 delim = strchr(target, '@');
4463 if (strncmp(zhp->zfs_name, target, delim - target)
4464 != 0 || zhp->zfs_name[delim - target] != '@') {
4465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4466 "snapshots must be part of same "
4468 return (zfs_error(hdl, EZFS_CROSSTARGET,
4473 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4474 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4476 if (flags.recursive) {
4477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4478 "recursive rename must be a snapshot"));
4479 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4482 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4483 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4485 /* validate parents */
4486 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4489 /* make sure we're in the same pool */
4490 verify((delim = strchr(target, '/')) != NULL);
4491 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4492 zhp->zfs_name[delim - target] != '/') {
4493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4494 "datasets must be within same pool"));
4495 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4498 /* new name cannot be a child of the current dataset name */
4499 if (is_descendant(zhp->zfs_name, target)) {
4500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4501 "New dataset name cannot be a descendant of "
4502 "current dataset name"));
4503 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4507 (void) snprintf(errbuf, sizeof (errbuf),
4508 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4510 if (getzoneid() == GLOBAL_ZONEID &&
4511 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4513 "dataset is used in a non-global zone"));
4514 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4518 * Avoid unmounting file systems with mountpoint property set to
4519 * 'legacy' or 'none' even if -u option is not given.
4521 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4522 !flags.recursive && !flags.nounmount &&
4523 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4524 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4525 (strcmp(property, "legacy") == 0 ||
4526 strcmp(property, "none") == 0)) {
4527 flags.nounmount = B_TRUE;
4529 if (flags.recursive) {
4530 char *parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4531 delim = strchr(parentname, '@');
4533 zfs_handle_t *zhrp = zfs_open(zhp->zfs_hdl, parentname,
4541 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4542 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4543 flags.nounmount ? CL_GATHER_DONT_UNMOUNT :
4544 CL_GATHER_ITER_MOUNTED,
4545 flags.forceunmount ? MS_FORCE : 0)) == NULL)
4548 if (changelist_haszonedchild(cl)) {
4549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4550 "child dataset with inherited mountpoint is used "
4551 "in a non-global zone"));
4552 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4557 if ((ret = changelist_prefix(cl)) != 0)
4561 if (ZFS_IS_VOLUME(zhp))
4562 zc.zc_objset_type = DMU_OST_ZVOL;
4564 zc.zc_objset_type = DMU_OST_ZFS;
4566 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4567 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4569 zc.zc_cookie = !!flags.recursive;
4570 zc.zc_cookie |= (!!flags.nounmount) << 1;
4572 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4574 * if it was recursive, the one that actually failed will
4577 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4578 "cannot rename '%s'"), zc.zc_name);
4580 if (flags.recursive && errno == EEXIST) {
4581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4582 "a child dataset already has a snapshot "
4583 "with the new name"));
4584 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4585 } else if (errno == EACCES) {
4586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4587 "cannot move encrypted child outside of "
4588 "its encryption root"));
4589 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4591 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4595 * On failure, we still want to remount any filesystems that
4596 * were previously mounted, so we don't alter the system state.
4599 (void) changelist_postfix(cl);
4602 changelist_rename(cl, zfs_get_name(zhp), target);
4603 ret = changelist_postfix(cl);
4609 changelist_free(cl);
4615 zfs_get_all_props(zfs_handle_t *zhp)
4617 return (zhp->zfs_props);
4621 zfs_get_recvd_props(zfs_handle_t *zhp)
4623 if (zhp->zfs_recvd_props == NULL)
4624 if (get_recvd_props_ioctl(zhp) != 0)
4626 return (zhp->zfs_recvd_props);
4630 zfs_get_user_props(zfs_handle_t *zhp)
4632 return (zhp->zfs_user_props);
4636 * This function is used by 'zfs list' to determine the exact set of columns to
4637 * display, and their maximum widths. This does two main things:
4639 * - If this is a list of all properties, then expand the list to include
4640 * all native properties, and set a flag so that for each dataset we look
4641 * for new unique user properties and add them to the list.
4643 * - For non fixed-width properties, keep track of the maximum width seen
4644 * so that we can size the column appropriately. If the user has
4645 * requested received property values, we also need to compute the width
4646 * of the RECEIVED column.
4649 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4652 libzfs_handle_t *hdl = zhp->zfs_hdl;
4653 zprop_list_t *entry;
4654 zprop_list_t **last, **start;
4655 nvlist_t *userprops, *propval;
4658 char buf[ZFS_MAXPROPLEN];
4660 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4663 userprops = zfs_get_user_props(zhp);
4666 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4668 * Go through and add any user properties as necessary. We
4669 * start by incrementing our list pointer to the first
4670 * non-native property.
4673 while (*start != NULL) {
4674 if ((*start)->pl_prop == ZPROP_USERPROP)
4676 start = &(*start)->pl_next;
4680 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4682 * See if we've already found this property in our list.
4684 for (last = start; *last != NULL;
4685 last = &(*last)->pl_next) {
4686 if (strcmp((*last)->pl_user_prop,
4687 nvpair_name(elem)) == 0)
4691 if (*last == NULL) {
4692 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
4693 entry->pl_user_prop =
4694 zfs_strdup(hdl, nvpair_name(elem));
4695 entry->pl_prop = ZPROP_USERPROP;
4696 entry->pl_width = strlen(nvpair_name(elem));
4697 entry->pl_all = B_TRUE;
4704 * Now go through and check the width of any non-fixed columns
4706 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4707 if (entry->pl_fixed && !literal)
4710 if (entry->pl_prop != ZPROP_USERPROP) {
4711 if (zfs_prop_get(zhp, entry->pl_prop,
4712 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4713 if (strlen(buf) > entry->pl_width)
4714 entry->pl_width = strlen(buf);
4716 if (received && zfs_prop_get_recvd(zhp,
4717 zfs_prop_to_name(entry->pl_prop),
4718 buf, sizeof (buf), literal) == 0)
4719 if (strlen(buf) > entry->pl_recvd_width)
4720 entry->pl_recvd_width = strlen(buf);
4722 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4724 strval = fnvlist_lookup_string(propval,
4726 if (strlen(strval) > entry->pl_width)
4727 entry->pl_width = strlen(strval);
4729 if (received && zfs_prop_get_recvd(zhp,
4730 entry->pl_user_prop,
4731 buf, sizeof (buf), literal) == 0)
4732 if (strlen(buf) > entry->pl_recvd_width)
4733 entry->pl_recvd_width = strlen(buf);
4741 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4747 * Keep a reference to the props-table against which we prune the
4750 zhp->zfs_props_table = props;
4752 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4755 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4756 next = nvlist_next_nvpair(zhp->zfs_props, curr);
4759 * User properties will result in ZPROP_USERPROP (an alias
4760 * for ZPROP_INVAL), and since we
4761 * only know how to prune standard ZFS properties, we always
4762 * leave these in the list. This can also happen if we
4763 * encounter an unknown DSL property (when running older
4764 * software, for example).
4766 if (zfs_prop != ZPROP_USERPROP && props[zfs_prop] == B_FALSE)
4767 (void) nvlist_remove(zhp->zfs_props,
4768 nvpair_name(curr), nvpair_type(curr));
4774 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4775 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4777 zfs_cmd_t zc = {"\0"};
4778 nvlist_t *nvlist = NULL;
4781 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4782 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4783 zc.zc_cookie = (uint64_t)cmd;
4785 if (cmd == ZFS_SMB_ACL_RENAME) {
4786 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4787 (void) no_memory(hdl);
4793 case ZFS_SMB_ACL_ADD:
4794 case ZFS_SMB_ACL_REMOVE:
4795 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4797 case ZFS_SMB_ACL_RENAME:
4798 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4800 (void) no_memory(hdl);
4803 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4805 (void) no_memory(hdl);
4808 zcmd_write_src_nvlist(hdl, &zc, nvlist);
4810 case ZFS_SMB_ACL_PURGE:
4815 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4816 nvlist_free(nvlist);
4821 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4822 char *path, char *resource)
4824 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4829 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4830 char *path, char *resource)
4832 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4837 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4839 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4844 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4845 char *oldname, char *newname)
4847 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4852 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4853 zfs_userspace_cb_t func, void *arg)
4855 zfs_cmd_t zc = {"\0"};
4856 zfs_useracct_t buf[100];
4857 libzfs_handle_t *hdl = zhp->zfs_hdl;
4860 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4862 zc.zc_objset_type = type;
4863 zc.zc_nvlist_dst = (uintptr_t)buf;
4866 zfs_useracct_t *zua = buf;
4868 zc.zc_nvlist_dst_size = sizeof (buf);
4869 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4870 if ((errno == ENOTSUP &&
4871 (type == ZFS_PROP_USEROBJUSED ||
4872 type == ZFS_PROP_GROUPOBJUSED ||
4873 type == ZFS_PROP_USEROBJQUOTA ||
4874 type == ZFS_PROP_GROUPOBJQUOTA ||
4875 type == ZFS_PROP_PROJECTOBJUSED ||
4876 type == ZFS_PROP_PROJECTOBJQUOTA ||
4877 type == ZFS_PROP_PROJECTUSED ||
4878 type == ZFS_PROP_PROJECTQUOTA)))
4881 return (zfs_standard_error_fmt(hdl, errno,
4882 dgettext(TEXT_DOMAIN,
4883 "cannot get used/quota for %s"), zc.zc_name));
4885 if (zc.zc_nvlist_dst_size == 0)
4888 while (zc.zc_nvlist_dst_size > 0) {
4889 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4890 zua->zu_space)) != 0)
4893 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4902 const char *snapname;
4904 boolean_t recursive;
4909 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4911 struct holdarg *ha = arg;
4912 char name[ZFS_MAX_DATASET_NAME_LEN];
4915 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
4916 ha->snapname) >= sizeof (name))
4919 if (lzc_exists(name))
4920 fnvlist_add_string(ha->nvl, name, ha->tag);
4923 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4929 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4930 boolean_t recursive, int cleanup_fd)
4935 ha.nvl = fnvlist_alloc();
4936 ha.snapname = snapname;
4938 ha.recursive = recursive;
4939 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4941 if (nvlist_empty(ha.nvl)) {
4942 char errbuf[ERRBUFLEN];
4944 fnvlist_free(ha.nvl);
4946 (void) snprintf(errbuf, sizeof (errbuf),
4947 dgettext(TEXT_DOMAIN,
4948 "cannot hold snapshot '%s@%s'"),
4949 zhp->zfs_name, snapname);
4950 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4954 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4955 fnvlist_free(ha.nvl);
4961 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4965 libzfs_handle_t *hdl = zhp->zfs_hdl;
4966 char errbuf[ERRBUFLEN];
4970 ret = lzc_hold(holds, cleanup_fd, &errors);
4973 /* There may be errors even in the success case. */
4974 fnvlist_free(errors);
4978 if (nvlist_empty(errors)) {
4979 /* no hold-specific errors */
4980 (void) snprintf(errbuf, sizeof (errbuf),
4981 dgettext(TEXT_DOMAIN, "cannot hold"));
4984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4985 "pool must be upgraded"));
4986 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4989 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4992 (void) zfs_standard_error(hdl, ret, errbuf);
4996 for (elem = nvlist_next_nvpair(errors, NULL);
4998 elem = nvlist_next_nvpair(errors, elem)) {
4999 (void) snprintf(errbuf, sizeof (errbuf),
5000 dgettext(TEXT_DOMAIN,
5001 "cannot hold snapshot '%s'"), nvpair_name(elem));
5002 switch (fnvpair_value_int32(elem)) {
5005 * Temporary tags wind up having the ds object id
5006 * prepended. So even if we passed the length check
5007 * above, it's still possible for the tag to wind
5008 * up being slightly too long.
5010 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
5013 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5016 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
5019 (void) zfs_standard_error(hdl,
5020 fnvpair_value_int32(elem), errbuf);
5024 fnvlist_free(errors);
5029 zfs_release_one(zfs_handle_t *zhp, void *arg)
5031 struct holdarg *ha = arg;
5032 char name[ZFS_MAX_DATASET_NAME_LEN];
5034 nvlist_t *existing_holds;
5036 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
5037 ha->snapname) >= sizeof (name)) {
5042 if (lzc_get_holds(name, &existing_holds) != 0) {
5044 } else if (!nvlist_exists(existing_holds, ha->tag)) {
5047 nvlist_t *torelease = fnvlist_alloc();
5048 fnvlist_add_boolean(torelease, ha->tag);
5049 fnvlist_add_nvlist(ha->nvl, name, torelease);
5050 fnvlist_free(torelease);
5054 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
5060 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
5061 boolean_t recursive)
5065 nvlist_t *errors = NULL;
5067 libzfs_handle_t *hdl = zhp->zfs_hdl;
5068 char errbuf[ERRBUFLEN];
5070 ha.nvl = fnvlist_alloc();
5071 ha.snapname = snapname;
5073 ha.recursive = recursive;
5075 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
5077 if (nvlist_empty(ha.nvl)) {
5078 fnvlist_free(ha.nvl);
5080 (void) snprintf(errbuf, sizeof (errbuf),
5081 dgettext(TEXT_DOMAIN,
5082 "cannot release hold from snapshot '%s@%s'"),
5083 zhp->zfs_name, snapname);
5085 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5087 (void) zfs_standard_error(hdl, ret, errbuf);
5092 ret = lzc_release(ha.nvl, &errors);
5093 fnvlist_free(ha.nvl);
5096 /* There may be errors even in the success case. */
5097 fnvlist_free(errors);
5101 if (nvlist_empty(errors)) {
5102 /* no hold-specific errors */
5103 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5107 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5108 "pool must be upgraded"));
5109 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5112 (void) zfs_standard_error(hdl, errno, errbuf);
5116 for (elem = nvlist_next_nvpair(errors, NULL);
5118 elem = nvlist_next_nvpair(errors, elem)) {
5119 (void) snprintf(errbuf, sizeof (errbuf),
5120 dgettext(TEXT_DOMAIN,
5121 "cannot release hold from snapshot '%s'"),
5123 switch (fnvpair_value_int32(elem)) {
5125 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5128 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5131 (void) zfs_standard_error(hdl,
5132 fnvpair_value_int32(elem), errbuf);
5136 fnvlist_free(errors);
5141 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5143 zfs_cmd_t zc = {"\0"};
5144 libzfs_handle_t *hdl = zhp->zfs_hdl;
5148 char errbuf[ERRBUFLEN];
5150 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5151 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5155 nvbuf = malloc(nvsz);
5156 if (nvbuf == NULL) {
5157 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5161 zc.zc_nvlist_dst_size = nvsz;
5162 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5164 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5166 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) {
5167 (void) snprintf(errbuf, sizeof (errbuf),
5168 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5173 nvsz = zc.zc_nvlist_dst_size;
5177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5178 "pool must be upgraded"));
5179 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5182 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5185 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5188 err = zfs_standard_error(hdl, errno, errbuf);
5193 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5195 err = zfs_standard_error_fmt(hdl, rc, dgettext(
5196 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5207 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5209 zfs_cmd_t zc = {"\0"};
5210 libzfs_handle_t *hdl = zhp->zfs_hdl;
5212 char errbuf[ERRBUFLEN];
5216 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5217 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5219 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5222 nvbuf = malloc(nvsz);
5224 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5227 zc.zc_nvlist_src_size = nvsz;
5228 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5229 zc.zc_perm_action = un;
5231 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5233 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5234 (void) snprintf(errbuf, sizeof (errbuf),
5235 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5239 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5240 "pool must be upgraded"));
5241 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5244 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5247 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5250 err = zfs_standard_error(hdl, errno, errbuf);
5261 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5264 char errbuf[ERRBUFLEN];
5266 err = lzc_get_holds(zhp->zfs_name, nvl);
5269 libzfs_handle_t *hdl = zhp->zfs_hdl;
5271 (void) snprintf(errbuf, sizeof (errbuf),
5272 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5276 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5277 "pool must be upgraded"));
5278 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5281 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5284 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5287 err = zfs_standard_error(hdl, errno, errbuf);
5296 * The theory of raidz space accounting
5298 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5299 * will "reference" 128KB, even though it allocates more than that, to store the
5300 * parity information (and perhaps skip sectors). This concept of the
5301 * "referenced" (and other DMU space accounting) being lower than the allocated
5302 * space by a constant factor is called "raidz deflation."
5304 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5305 * block size. However, zvols typically have a much smaller block size (default
5306 * 8KB). These smaller blocks may require proportionally much more parity
5307 * information (and perhaps skip sectors). In this case, the change to the
5308 * "referenced" property may be much more than the logical block size.
5310 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5313 * +-------+-------+-------+-------+-------+
5314 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5315 * +-------+-------+-------+-------+-------+
5316 * | P0 | D0 | D8 | D16 | D24 |
5317 * | P1 | D1 | D9 | D17 | D25 |
5318 * | P2 | D2 | D10 | D18 | D26 |
5319 * | P3 | D3 | D11 | D19 | D27 |
5320 * | P4 | D4 | D12 | D20 | D28 |
5321 * | P5 | D5 | D13 | D21 | D29 |
5322 * | P6 | D6 | D14 | D22 | D30 |
5323 * | P7 | D7 | D15 | D23 | D31 |
5324 * +-------+-------+-------+-------+-------+
5326 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5327 * sectors. The dataset's referenced will increase by 128k and the pool's
5328 * allocated and free properties will be adjusted by 160k.
5330 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5331 * blank cells represent unallocated space.
5333 * +-------+-------+-------+-------+-------+
5334 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5335 * +-------+-------+-------+-------+-------+
5337 * +-------+-------+-------+-------+-------+
5339 * Above, notice that the 4k block required one sector for parity and another
5340 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5341 * and free properties will be adjusted by 8k. The dataset will not be charged
5342 * 8k. Rather, it will be charged a value that is scaled according to the
5343 * overhead of the 128k block on the same vdev. This 8k allocation will be
5344 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5345 * calculated in the 128k block example above.
5347 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5348 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5349 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5350 * multiple of of 4 sectors. When a block does not fill the required number of
5351 * sectors, skip blocks (sectors) are used.
5353 * An 8k block being written to a raidz vdev may be written as follows:
5355 * +-------+-------+-------+-------+-------+
5356 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5357 * +-------+-------+-------+-------+-------+
5358 * | P0 | D0 | D1 | S0 | |
5359 * +-------+-------+-------+-------+-------+
5361 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5362 * added. For this 8k block, the pool's allocated and free properties are
5363 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5364 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5365 * the 128k block example above.
5367 * The situation is slightly different for dRAID since the minimum allocation
5368 * size is the full group width. The same 8K block above would be written as
5369 * follows in a dRAID group:
5371 * +-------+-------+-------+-------+-------+
5372 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5373 * +-------+-------+-------+-------+-------+
5374 * | P0 | D0 | D1 | S0 | S1 |
5375 * +-------+-------+-------+-------+-------+
5377 * Compression may lead to a variety of block sizes being written for the same
5378 * volume or file. There is no clear way to reserve just the amount of space
5379 * that will be required, so the worst case (no compression) is assumed.
5380 * Note that metadata blocks will typically be compressed, so the reservation
5381 * size returned by zvol_volsize_to_reservation() will generally be slightly
5382 * larger than the maximum that the volume can reference.
5386 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5387 * amount of space (in bytes) that will be allocated for the specified block
5388 * size. Note that the "referenced" space accounted will be less than this, but
5389 * not necessarily equal to "blksize", due to RAIDZ deflation.
5392 vdev_raidz_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5395 uint64_t asize, ndata;
5397 ASSERT3U(ndisks, >, nparity);
5398 ndata = ndisks - nparity;
5399 asize = ((blksize - 1) >> ashift) + 1;
5400 asize += nparity * ((asize + ndata - 1) / ndata);
5401 asize = roundup(asize, nparity + 1) << ashift;
5407 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5408 * amount of space (in bytes) that will be allocated for the specified block
5412 vdev_draid_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5415 ASSERT3U(ndisks, >, nparity);
5416 uint64_t ndata = ndisks - nparity;
5417 uint64_t rows = ((blksize - 1) / (ndata << ashift)) + 1;
5418 uint64_t asize = (rows * ndisks) << ashift;
5424 * Determine how much space will be allocated if it lands on the most space-
5425 * inefficient top-level vdev. Returns the size in bytes required to store one
5426 * copy of the volume data. See theory comment above.
5429 volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5431 nvlist_t *config, *tree, **vdevs;
5435 config = zpool_get_config(zhp, NULL);
5436 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5437 nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5438 &vdevs, &nvdevs) != 0) {
5439 return (nblocks * blksize);
5442 for (int v = 0; v < nvdevs; v++) {
5444 uint64_t nparity, ashift, asize, tsize;
5447 if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5451 if (strcmp(type, VDEV_TYPE_RAIDZ) != 0 &&
5452 strcmp(type, VDEV_TYPE_DRAID) != 0)
5455 if (nvlist_lookup_uint64(vdevs[v],
5456 ZPOOL_CONFIG_NPARITY, &nparity) != 0)
5459 if (nvlist_lookup_uint64(vdevs[v],
5460 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)
5463 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
5467 if (nvlist_lookup_nvlist_array(vdevs[v],
5468 ZPOOL_CONFIG_CHILDREN, &disks, &ndisks) != 0)
5471 /* allocation size for the "typical" 128k block */
5472 tsize = vdev_raidz_asize(ndisks, nparity, ashift,
5473 SPA_OLD_MAXBLOCKSIZE);
5475 /* allocation size for the blksize block */
5476 asize = vdev_raidz_asize(ndisks, nparity, ashift,
5481 if (nvlist_lookup_uint64(vdevs[v],
5482 ZPOOL_CONFIG_DRAID_NDATA, &ndata) != 0)
5485 /* allocation size for the "typical" 128k block */
5486 tsize = vdev_draid_asize(ndata + nparity, nparity,
5487 ashift, SPA_OLD_MAXBLOCKSIZE);
5489 /* allocation size for the blksize block */
5490 asize = vdev_draid_asize(ndata + nparity, nparity,
5495 * Scale this size down as a ratio of 128k / tsize.
5496 * See theory statement above.
5498 volsize = nblocks * asize * SPA_OLD_MAXBLOCKSIZE / tsize;
5499 if (volsize > ret) {
5505 ret = nblocks * blksize;
5512 * Convert the zvol's volume size to an appropriate reservation. See theory
5515 * Note: If this routine is updated, it is necessary to update the ZFS test
5516 * suite's shell version in reservation.shlib.
5519 zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5523 uint64_t nblocks, volblocksize;
5527 if (nvlist_lookup_string(props,
5528 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5529 ncopies = atoi(strval);
5532 if (nvlist_lookup_uint64(props,
5533 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5534 &volblocksize) != 0)
5535 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5537 nblocks = volsize / volblocksize;
5539 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5540 * this reason, only the data blocks are scaled based on vdev config.
5542 volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5544 /* start with metadnode L0-L6 */
5546 /* calculate number of indirects */
5547 while (nblocks > 1) {
5548 nblocks += DNODES_PER_LEVEL - 1;
5549 nblocks /= DNODES_PER_LEVEL;
5552 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5555 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5556 * compressed, but in practice they compress down to about
5559 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5565 * Wait for the given activity and return the status of the wait (whether or not
5566 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5567 * reported via the 'missing' parameter, rather than by printing an error
5568 * message. This is convenient when this function is called in a loop over a
5569 * long period of time (as it is, for example, by zfs's wait cmd). In that
5570 * scenario, a fs being exported or destroyed should be considered a normal
5571 * event, so we don't want to print an error when we find that the fs doesn't
5575 zfs_wait_status(zfs_handle_t *zhp, zfs_wait_activity_t activity,
5576 boolean_t *missing, boolean_t *waited)
5578 int error = lzc_wait_fs(zhp->zfs_name, activity, waited);
5579 *missing = (error == ENOENT);
5584 (void) zfs_standard_error_fmt(zhp->zfs_hdl, error,
5585 dgettext(TEXT_DOMAIN, "error waiting in fs '%s'"),