4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017 RackTop Systems.
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
55 #include <sys/dnode.h>
61 #include "zfs_namecheck.h"
63 #include "libzfs_impl.h"
64 #include "zfs_deleg.h"
66 static int userquota_propname_decode(const char *propname, boolean_t zoned,
67 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
70 * Given a single type (not a mask of types), return the type in a human
74 zfs_type_to_name(zfs_type_t type)
77 case ZFS_TYPE_FILESYSTEM:
78 return (dgettext(TEXT_DOMAIN, "filesystem"));
79 case ZFS_TYPE_SNAPSHOT:
80 return (dgettext(TEXT_DOMAIN, "snapshot"));
82 return (dgettext(TEXT_DOMAIN, "volume"));
84 return (dgettext(TEXT_DOMAIN, "pool"));
85 case ZFS_TYPE_BOOKMARK:
86 return (dgettext(TEXT_DOMAIN, "bookmark"));
88 assert(!"unhandled zfs_type_t");
95 * Validate a ZFS path. This is used even before trying to open the dataset, to
96 * provide a more meaningful error message. We call zfs_error_aux() to
97 * explain exactly why the name was not valid.
100 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
106 if (entity_namecheck(path, &why, &what) != 0) {
109 case NAME_ERR_TOOLONG:
110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
111 "name is too long"));
114 case NAME_ERR_LEADING_SLASH:
115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116 "leading slash in name"));
119 case NAME_ERR_EMPTY_COMPONENT:
120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121 "empty component in name"));
124 case NAME_ERR_TRAILING_SLASH:
125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
126 "trailing slash in name"));
129 case NAME_ERR_INVALCHAR:
131 dgettext(TEXT_DOMAIN, "invalid character "
132 "'%c' in name"), what);
135 case NAME_ERR_MULTIPLE_DELIMITERS:
136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
137 "multiple '@' and/or '#' delimiters in "
141 case NAME_ERR_NOLETTER:
142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 "pool doesn't begin with a letter"));
146 case NAME_ERR_RESERVED:
147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148 "name is reserved"));
151 case NAME_ERR_DISKLIKE:
152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
153 "reserved disk name"));
157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158 "(%d) not defined"), why);
166 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169 "snapshot delimiter '@' is not expected here"));
173 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176 "missing '@' delimiter in snapshot name"));
180 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
183 "bookmark delimiter '#' is not expected here"));
187 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
189 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
190 "missing '#' delimiter in bookmark name"));
194 if (modifying && strchr(path, '%') != NULL) {
196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
197 "invalid character %c in name"), '%');
205 zfs_name_valid(const char *name, zfs_type_t type)
207 if (type == ZFS_TYPE_POOL)
208 return (zpool_name_valid(NULL, B_FALSE, name));
209 return (zfs_validate_name(NULL, name, type, B_FALSE));
213 * This function takes the raw DSL properties, and filters out the user-defined
214 * properties into a separate nvlist.
217 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
219 libzfs_handle_t *hdl = zhp->zfs_hdl;
224 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
225 (void) no_memory(hdl);
230 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
231 if (!zfs_prop_user(nvpair_name(elem)))
234 verify(nvpair_value_nvlist(elem, &propval) == 0);
235 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
237 (void) no_memory(hdl);
245 static zpool_handle_t *
246 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
248 libzfs_handle_t *hdl = zhp->zfs_hdl;
251 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
252 if (hdl->libzfs_pool_handles != NULL)
253 zph->zpool_next = hdl->libzfs_pool_handles;
254 hdl->libzfs_pool_handles = zph;
259 static zpool_handle_t *
260 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
262 libzfs_handle_t *hdl = zhp->zfs_hdl;
263 zpool_handle_t *zph = hdl->libzfs_pool_handles;
265 while ((zph != NULL) &&
266 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
267 zph = zph->zpool_next;
272 * Returns a handle to the pool that contains the provided dataset.
273 * If a handle to that pool already exists then that handle is returned.
274 * Otherwise, a new handle is created and added to the list of handles.
276 static zpool_handle_t *
277 zpool_handle(zfs_handle_t *zhp)
283 len = strcspn(zhp->zfs_name, "/@#") + 1;
284 pool_name = zfs_alloc(zhp->zfs_hdl, len);
285 (void) strlcpy(pool_name, zhp->zfs_name, len);
287 zph = zpool_find_handle(zhp, pool_name, len);
289 zph = zpool_add_handle(zhp, pool_name);
296 zpool_free_handles(libzfs_handle_t *hdl)
298 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
300 while (zph != NULL) {
301 next = zph->zpool_next;
305 hdl->libzfs_pool_handles = NULL;
309 * Utility function to gather stats (objset and zpl) for the given object.
312 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
314 libzfs_handle_t *hdl = zhp->zfs_hdl;
316 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
318 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
319 if (errno == ENOMEM) {
320 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
331 * Utility function to get the received properties of the given object.
334 get_recvd_props_ioctl(zfs_handle_t *zhp)
336 libzfs_handle_t *hdl = zhp->zfs_hdl;
337 nvlist_t *recvdprops;
338 zfs_cmd_t zc = { 0 };
341 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
344 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
346 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
347 if (errno == ENOMEM) {
348 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
352 zcmd_free_nvlists(&zc);
357 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
358 zcmd_free_nvlists(&zc);
362 nvlist_free(zhp->zfs_recvd_props);
363 zhp->zfs_recvd_props = recvdprops;
369 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
371 nvlist_t *allprops, *userprops;
373 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
375 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
380 * XXX Why do we store the user props separately, in addition to
381 * storing them in zfs_props?
383 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
384 nvlist_free(allprops);
388 nvlist_free(zhp->zfs_props);
389 nvlist_free(zhp->zfs_user_props);
391 zhp->zfs_props = allprops;
392 zhp->zfs_user_props = userprops;
398 get_stats(zfs_handle_t *zhp)
401 zfs_cmd_t zc = { 0 };
403 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
405 if (get_stats_ioctl(zhp, &zc) != 0)
407 else if (put_stats_zhdl(zhp, &zc) != 0)
409 zcmd_free_nvlists(&zc);
414 * Refresh the properties currently stored in the handle.
417 zfs_refresh_properties(zfs_handle_t *zhp)
419 (void) get_stats(zhp);
423 * Makes a handle from the given dataset name. Used by zfs_open() and
424 * zfs_iter_* to create child handles on the fly.
427 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
429 if (put_stats_zhdl(zhp, zc) != 0)
433 * We've managed to open the dataset and gather statistics. Determine
434 * the high-level type.
436 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
437 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
438 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
439 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
443 if (zhp->zfs_dmustats.dds_is_snapshot)
444 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
445 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
446 zhp->zfs_type = ZFS_TYPE_VOLUME;
447 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
448 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
450 abort(); /* we should never see any other types */
452 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
459 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
461 zfs_cmd_t zc = { 0 };
463 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
469 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
470 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
474 if (get_stats_ioctl(zhp, &zc) == -1) {
475 zcmd_free_nvlists(&zc);
479 if (make_dataset_handle_common(zhp, &zc) == -1) {
483 zcmd_free_nvlists(&zc);
488 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
490 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
496 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
497 if (make_dataset_handle_common(zhp, zc) == -1) {
505 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
507 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
512 zhp->zfs_hdl = pzhp->zfs_hdl;
513 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
514 zhp->zfs_head_type = pzhp->zfs_type;
515 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
516 zhp->zpool_hdl = zpool_handle(zhp);
521 zfs_handle_dup(zfs_handle_t *zhp_orig)
523 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
528 zhp->zfs_hdl = zhp_orig->zfs_hdl;
529 zhp->zpool_hdl = zhp_orig->zpool_hdl;
530 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
531 sizeof (zhp->zfs_name));
532 zhp->zfs_type = zhp_orig->zfs_type;
533 zhp->zfs_head_type = zhp_orig->zfs_head_type;
534 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
535 if (zhp_orig->zfs_props != NULL) {
536 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
537 (void) no_memory(zhp->zfs_hdl);
542 if (zhp_orig->zfs_user_props != NULL) {
543 if (nvlist_dup(zhp_orig->zfs_user_props,
544 &zhp->zfs_user_props, 0) != 0) {
545 (void) no_memory(zhp->zfs_hdl);
550 if (zhp_orig->zfs_recvd_props != NULL) {
551 if (nvlist_dup(zhp_orig->zfs_recvd_props,
552 &zhp->zfs_recvd_props, 0)) {
553 (void) no_memory(zhp->zfs_hdl);
558 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
559 if (zhp_orig->zfs_mntopts != NULL) {
560 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
561 zhp_orig->zfs_mntopts);
563 zhp->zfs_props_table = zhp_orig->zfs_props_table;
568 zfs_bookmark_exists(const char *path)
572 char fsname[ZFS_MAX_DATASET_NAME_LEN];
579 (void) strlcpy(fsname, path, sizeof (fsname));
580 pound = strchr(fsname, '#');
585 bmark_name = pound + 1;
586 props = fnvlist_alloc();
587 err = lzc_get_bookmarks(fsname, props, &bmarks);
594 rv = nvlist_exists(bmarks, bmark_name);
600 make_bookmark_handle(zfs_handle_t *parent, const char *path,
601 nvlist_t *bmark_props)
603 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
608 /* Fill in the name. */
609 zhp->zfs_hdl = parent->zfs_hdl;
610 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
612 /* Set the property lists. */
613 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
619 zhp->zfs_head_type = parent->zfs_head_type;
620 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
622 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
623 nvlist_free(zhp->zfs_props);
631 struct zfs_open_bookmarks_cb_data {
637 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
639 struct zfs_open_bookmarks_cb_data *dp = data;
642 * Is it the one we are looking for?
644 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
646 * We found it. Save it and let the caller know we are done.
653 * Not found. Close the handle and ask for another one.
660 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
661 * argument is a mask of acceptable types. The function will print an
662 * appropriate error message and return NULL if it can't be opened.
665 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
671 (void) snprintf(errbuf, sizeof (errbuf),
672 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
675 * Validate the name before we even try to open it.
677 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
678 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
683 * Bookmarks needs to be handled separately.
685 bookp = strchr(path, '#');
688 * Try to get stats for the dataset, which will tell us if it
692 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
693 (void) zfs_standard_error(hdl, errno, errbuf);
697 char dsname[ZFS_MAX_DATASET_NAME_LEN];
699 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
702 * We need to cut out '#' and everything after '#'
703 * to get the parent dataset name only.
705 assert(bookp - path < sizeof (dsname));
706 (void) strncpy(dsname, path, bookp - path);
707 dsname[bookp - path] = '\0';
710 * Create handle for the parent dataset.
713 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
714 (void) zfs_standard_error(hdl, errno, errbuf);
719 * Iterate bookmarks to find the right one.
722 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
723 &cb_data) == 0) && (cb_data.zhp == NULL)) {
724 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
728 if (cb_data.zhp == NULL) {
729 (void) zfs_standard_error(hdl, errno, errbuf);
742 char *at = strchr(path, '@');
747 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
748 (void) zfs_standard_error(hdl, errno, errbuf);
753 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
754 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
757 if (!(types & zhp->zfs_type)) {
758 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
767 * Release a ZFS handle. Nothing to do but free the associated memory.
770 zfs_close(zfs_handle_t *zhp)
772 if (zhp->zfs_mntopts)
773 free(zhp->zfs_mntopts);
774 nvlist_free(zhp->zfs_props);
775 nvlist_free(zhp->zfs_user_props);
776 nvlist_free(zhp->zfs_recvd_props);
780 typedef struct mnttab_node {
781 struct mnttab mtn_mt;
786 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
788 const mnttab_node_t *mtn1 = arg1;
789 const mnttab_node_t *mtn2 = arg2;
792 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
796 return (rv > 0 ? 1 : -1);
800 libzfs_mnttab_init(libzfs_handle_t *hdl)
802 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
803 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
804 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
808 libzfs_mnttab_update(libzfs_handle_t *hdl)
812 rewind(hdl->libzfs_mnttab);
813 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
816 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
818 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
819 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
820 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
821 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
822 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
823 avl_add(&hdl->libzfs_mnttab_cache, mtn);
828 libzfs_mnttab_fini(libzfs_handle_t *hdl)
833 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
835 free(mtn->mtn_mt.mnt_special);
836 free(mtn->mtn_mt.mnt_mountp);
837 free(mtn->mtn_mt.mnt_fstype);
838 free(mtn->mtn_mt.mnt_mntopts);
841 avl_destroy(&hdl->libzfs_mnttab_cache);
845 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
847 hdl->libzfs_mnttab_enable = enable;
851 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
852 struct mnttab *entry)
857 if (!hdl->libzfs_mnttab_enable) {
858 struct mnttab srch = { 0 };
860 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
861 libzfs_mnttab_fini(hdl);
862 rewind(hdl->libzfs_mnttab);
863 srch.mnt_special = (char *)fsname;
864 srch.mnt_fstype = MNTTYPE_ZFS;
865 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
871 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
872 libzfs_mnttab_update(hdl);
874 find.mtn_mt.mnt_special = (char *)fsname;
875 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
877 *entry = mtn->mtn_mt;
884 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
885 const char *mountp, const char *mntopts)
889 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
891 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
892 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
893 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
894 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
895 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
896 avl_add(&hdl->libzfs_mnttab_cache, mtn);
900 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
905 find.mtn_mt.mnt_special = (char *)fsname;
906 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
908 avl_remove(&hdl->libzfs_mnttab_cache, ret);
909 free(ret->mtn_mt.mnt_special);
910 free(ret->mtn_mt.mnt_mountp);
911 free(ret->mtn_mt.mnt_fstype);
912 free(ret->mtn_mt.mnt_mntopts);
918 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
920 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
922 if (zpool_handle == NULL)
925 *spa_version = zpool_get_prop_int(zpool_handle,
926 ZPOOL_PROP_VERSION, NULL);
931 * The choice of reservation property depends on the SPA version.
934 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
938 if (zfs_spa_version(zhp, &spa_version) < 0)
941 if (spa_version >= SPA_VERSION_REFRESERVATION)
942 *resv_prop = ZFS_PROP_REFRESERVATION;
944 *resv_prop = ZFS_PROP_RESERVATION;
950 * Given an nvlist of properties to set, validates that they are correct, and
951 * parses any numeric properties (index, boolean, etc) if they are specified as
955 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
956 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
964 int chosen_normal = -1;
967 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
968 (void) no_memory(hdl);
973 * Make sure this property is valid and applies to this type.
977 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
978 const char *propname = nvpair_name(elem);
980 prop = zfs_name_to_prop(propname);
981 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
983 * This is a user property: make sure it's a
984 * string, and that it's less than ZAP_MAXNAMELEN.
986 if (nvpair_type(elem) != DATA_TYPE_STRING) {
987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
988 "'%s' must be a string"), propname);
989 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
993 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
995 "property name '%s' is too long"),
997 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1001 (void) nvpair_value_string(elem, &strval);
1002 if (nvlist_add_string(ret, propname, strval) != 0) {
1003 (void) no_memory(hdl);
1010 * Currently, only user properties can be modified on
1013 if (type == ZFS_TYPE_SNAPSHOT) {
1014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1015 "this property can not be modified for snapshots"));
1016 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1020 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1021 zfs_userquota_prop_t uqtype;
1022 char newpropname[128];
1027 if (userquota_propname_decode(propname, zoned,
1028 &uqtype, domain, sizeof (domain), &rid) != 0) {
1030 dgettext(TEXT_DOMAIN,
1031 "'%s' has an invalid user/group name"),
1033 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1037 if (uqtype != ZFS_PROP_USERQUOTA &&
1038 uqtype != ZFS_PROP_GROUPQUOTA) {
1040 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1042 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1047 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1048 (void) nvpair_value_string(elem, &strval);
1049 if (strcmp(strval, "none") == 0) {
1051 } else if (zfs_nicestrtonum(hdl,
1052 strval, &intval) != 0) {
1053 (void) zfs_error(hdl,
1054 EZFS_BADPROP, errbuf);
1057 } else if (nvpair_type(elem) ==
1059 (void) nvpair_value_uint64(elem, &intval);
1061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 "use 'none' to disable "
1063 "userquota/groupquota"));
1067 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1068 "'%s' must be a number"), propname);
1069 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1074 * Encode the prop name as
1075 * userquota@<hex-rid>-domain, to make it easy
1076 * for the kernel to decode.
1078 (void) snprintf(newpropname, sizeof (newpropname),
1079 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1080 (longlong_t)rid, domain);
1084 if (nvlist_add_uint64_array(ret, newpropname,
1086 (void) no_memory(hdl);
1090 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1092 "'%s' is readonly"),
1094 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1098 if (prop == ZPROP_INVAL) {
1099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1100 "invalid property '%s'"), propname);
1101 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1105 if (!zfs_prop_valid_for_type(prop, type)) {
1107 dgettext(TEXT_DOMAIN, "'%s' does not "
1108 "apply to datasets of this type"), propname);
1109 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1113 if (zfs_prop_readonly(prop) &&
1114 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1116 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1118 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1122 if (zprop_parse_value(hdl, elem, prop, type, ret,
1123 &strval, &intval, errbuf) != 0)
1127 * Perform some additional checks for specific properties.
1130 case ZFS_PROP_VERSION:
1136 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1137 if (intval < version) {
1138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1139 "Can not downgrade; already at version %u"),
1141 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1147 case ZFS_PROP_VOLBLOCKSIZE:
1148 case ZFS_PROP_RECORDSIZE:
1150 int maxbs = SPA_MAXBLOCKSIZE;
1151 if (zpool_hdl != NULL) {
1152 maxbs = zpool_get_prop_int(zpool_hdl,
1153 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1156 * Volumes are limited to a volblocksize of 128KB,
1157 * because they typically service workloads with
1158 * small random writes, which incur a large performance
1159 * penalty with large blocks.
1161 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1162 maxbs = SPA_OLD_MAXBLOCKSIZE;
1164 * The value must be a power of two between
1165 * SPA_MINBLOCKSIZE and maxbs.
1167 if (intval < SPA_MINBLOCKSIZE ||
1168 intval > maxbs || !ISP2(intval)) {
1169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1170 "'%s' must be power of 2 from 512B "
1171 "to %uKB"), propname, maxbs >> 10);
1172 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1177 case ZFS_PROP_MLSLABEL:
1181 * Verify the mlslabel string and convert to
1182 * internal hex label string.
1186 char *hex = NULL; /* internal label string */
1188 /* Default value is already OK. */
1189 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1192 /* Verify the label can be converted to binary form */
1193 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1194 (str_to_label(strval, &new_sl, MAC_LABEL,
1195 L_NO_CORRECTION, NULL) == -1)) {
1199 /* Now translate to hex internal label string */
1200 if (label_to_str(new_sl, &hex, M_INTERNAL,
1206 m_label_free(new_sl);
1208 /* If string is already in internal form, we're done. */
1209 if (strcmp(strval, hex) == 0) {
1214 /* Replace the label string with the internal form. */
1215 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1217 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225 "invalid mlslabel '%s'"), strval);
1226 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1227 m_label_free(new_sl); /* OK if null */
1228 #else /* !illumos */
1229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1230 "mlslabel is not supported on FreeBSD"));
1231 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1232 #endif /* illumos */
1237 case ZFS_PROP_MOUNTPOINT:
1239 namecheck_err_t why;
1241 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1242 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1245 if (mountpoint_namecheck(strval, &why)) {
1247 case NAME_ERR_LEADING_SLASH:
1249 dgettext(TEXT_DOMAIN,
1250 "'%s' must be an absolute path, "
1251 "'none', or 'legacy'"), propname);
1253 case NAME_ERR_TOOLONG:
1255 dgettext(TEXT_DOMAIN,
1256 "component of '%s' is too long"),
1262 dgettext(TEXT_DOMAIN,
1263 "(%d) not defined"),
1267 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1274 case ZFS_PROP_SHARESMB:
1275 case ZFS_PROP_SHARENFS:
1277 * For the mountpoint and sharenfs or sharesmb
1278 * properties, check if it can be set in a
1279 * global/non-global zone based on
1280 * the zoned property value:
1282 * global zone non-global zone
1283 * --------------------------------------------------
1284 * zoned=on mountpoint (no) mountpoint (yes)
1285 * sharenfs (no) sharenfs (no)
1286 * sharesmb (no) sharesmb (no)
1288 * zoned=off mountpoint (yes) N/A
1293 if (getzoneid() == GLOBAL_ZONEID) {
1294 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1295 "'%s' cannot be set on "
1296 "dataset in a non-global zone"),
1298 (void) zfs_error(hdl, EZFS_ZONED,
1301 } else if (prop == ZFS_PROP_SHARENFS ||
1302 prop == ZFS_PROP_SHARESMB) {
1303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1304 "'%s' cannot be set in "
1305 "a non-global zone"), propname);
1306 (void) zfs_error(hdl, EZFS_ZONED,
1310 } else if (getzoneid() != GLOBAL_ZONEID) {
1312 * If zoned property is 'off', this must be in
1313 * a global zone. If not, something is wrong.
1315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1316 "'%s' cannot be set while dataset "
1317 "'zoned' property is set"), propname);
1318 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1323 * At this point, it is legitimate to set the
1324 * property. Now we want to make sure that the
1325 * property value is valid if it is sharenfs.
1327 if ((prop == ZFS_PROP_SHARENFS ||
1328 prop == ZFS_PROP_SHARESMB) &&
1329 strcmp(strval, "on") != 0 &&
1330 strcmp(strval, "off") != 0) {
1331 zfs_share_proto_t proto;
1333 if (prop == ZFS_PROP_SHARESMB)
1339 * Must be an valid sharing protocol
1340 * option string so init the libshare
1341 * in order to enable the parser and
1342 * then parse the options. We use the
1343 * control API since we don't care about
1344 * the current configuration and don't
1345 * want the overhead of loading it
1346 * until we actually do something.
1349 if (zfs_init_libshare(hdl,
1350 SA_INIT_CONTROL_API) != SA_OK) {
1352 * An error occurred so we can't do
1355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1356 "'%s' cannot be set: problem "
1357 "in share initialization"),
1359 (void) zfs_error(hdl, EZFS_BADPROP,
1364 if (zfs_parse_options(strval, proto) != SA_OK) {
1366 * There was an error in parsing so
1367 * deal with it by issuing an error
1368 * message and leaving after
1369 * uninitializing the the libshare
1372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1373 "'%s' cannot be set to invalid "
1374 "options"), propname);
1375 (void) zfs_error(hdl, EZFS_BADPROP,
1377 zfs_uninit_libshare(hdl);
1380 zfs_uninit_libshare(hdl);
1385 case ZFS_PROP_UTF8ONLY:
1386 chosen_utf = (int)intval;
1389 case ZFS_PROP_NORMALIZE:
1390 chosen_normal = (int)intval;
1398 * For changes to existing volumes, we have some additional
1399 * checks to enforce.
1401 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1402 uint64_t volsize = zfs_prop_get_int(zhp,
1404 uint64_t blocksize = zfs_prop_get_int(zhp,
1405 ZFS_PROP_VOLBLOCKSIZE);
1409 case ZFS_PROP_RESERVATION:
1410 case ZFS_PROP_REFRESERVATION:
1411 if (intval > volsize) {
1412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1413 "'%s' is greater than current "
1414 "volume size"), propname);
1415 (void) zfs_error(hdl, EZFS_BADPROP,
1421 case ZFS_PROP_VOLSIZE:
1422 if (intval % blocksize != 0) {
1423 zfs_nicenum(blocksize, buf,
1425 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1426 "'%s' must be a multiple of "
1427 "volume block size (%s)"),
1429 (void) zfs_error(hdl, EZFS_BADPROP,
1435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1436 "'%s' cannot be zero"),
1438 (void) zfs_error(hdl, EZFS_BADPROP,
1451 * If normalization was chosen, but no UTF8 choice was made,
1452 * enforce rejection of non-UTF8 names.
1454 * If normalization was chosen, but rejecting non-UTF8 names
1455 * was explicitly not chosen, it is an error.
1457 if (chosen_normal > 0 && chosen_utf < 0) {
1458 if (nvlist_add_uint64(ret,
1459 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1460 (void) no_memory(hdl);
1463 } else if (chosen_normal > 0 && chosen_utf == 0) {
1464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1465 "'%s' must be set 'on' if normalization chosen"),
1466 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1467 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1478 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1480 uint64_t old_volsize;
1481 uint64_t new_volsize;
1482 uint64_t old_reservation;
1483 uint64_t new_reservation;
1484 zfs_prop_t resv_prop;
1488 * If this is an existing volume, and someone is setting the volsize,
1489 * make sure that it matches the reservation, or add it if necessary.
1491 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1492 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1494 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1496 props = fnvlist_alloc();
1497 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1498 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1500 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1501 old_reservation) || nvlist_exists(nvl,
1502 zfs_prop_to_name(resv_prop))) {
1503 fnvlist_free(props);
1506 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1507 &new_volsize) != 0) {
1508 fnvlist_free(props);
1511 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1512 fnvlist_free(props);
1514 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1515 new_reservation) != 0) {
1516 (void) no_memory(zhp->zfs_hdl);
1523 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1530 * For quotas and reservations, ENOSPC indicates
1531 * something different; setting a quota or reservation
1532 * doesn't use any disk space.
1535 case ZFS_PROP_QUOTA:
1536 case ZFS_PROP_REFQUOTA:
1537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1538 "size is less than current used or "
1540 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1543 case ZFS_PROP_RESERVATION:
1544 case ZFS_PROP_REFRESERVATION:
1545 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1546 "size is greater than available space"));
1547 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1551 (void) zfs_standard_error(hdl, err, errbuf);
1557 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1561 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1566 "property value too long"));
1567 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1572 "pool and or dataset must be upgraded to set this "
1573 "property or value"));
1574 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1579 if (prop == ZFS_PROP_COMPRESSION ||
1580 prop == ZFS_PROP_RECORDSIZE) {
1581 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1582 "property setting is not allowed on "
1583 "bootable datasets"));
1584 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1585 } else if (prop == ZFS_PROP_CHECKSUM ||
1586 prop == ZFS_PROP_DEDUP) {
1587 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1588 "property setting is not allowed on "
1590 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1592 (void) zfs_standard_error(hdl, err, errbuf);
1597 if (prop == ZPROP_INVAL) {
1598 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1600 (void) zfs_standard_error(hdl, err, errbuf);
1606 * This platform can't address a volume this big.
1609 if (prop == ZFS_PROP_VOLSIZE) {
1610 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1616 (void) zfs_standard_error(hdl, err, errbuf);
1621 * Given a property name and value, set the property for the given dataset.
1624 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1628 libzfs_handle_t *hdl = zhp->zfs_hdl;
1629 nvlist_t *nvl = NULL;
1631 (void) snprintf(errbuf, sizeof (errbuf),
1632 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1635 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1636 nvlist_add_string(nvl, propname, propval) != 0) {
1637 (void) no_memory(hdl);
1641 ret = zfs_prop_set_list(zhp, nvl);
1651 * Given an nvlist of property names and values, set the properties for the
1655 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1657 zfs_cmd_t zc = { 0 };
1659 prop_changelist_t **cls = NULL;
1662 libzfs_handle_t *hdl = zhp->zfs_hdl;
1667 (void) snprintf(errbuf, sizeof (errbuf),
1668 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1671 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1672 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1677 * We have to check for any extra properties which need to be added
1678 * before computing the length of the nvlist.
1680 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1682 elem = nvlist_next_nvpair(nvl, elem)) {
1683 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1684 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1689 * Check how many properties we're setting and allocate an array to
1690 * store changelist pointers for postfix().
1693 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1695 elem = nvlist_next_nvpair(nvl, elem))
1697 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1701 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1703 elem = nvlist_next_nvpair(nvl, elem)) {
1705 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1707 assert(cl_idx < nvl_len);
1709 * We don't want to unmount & remount the dataset when changing
1710 * its canmount property to 'on' or 'noauto'. We only use
1711 * the changelist logic to unmount when setting canmount=off.
1713 if (prop != ZFS_PROP_CANMOUNT ||
1714 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1715 zfs_is_mounted(zhp, NULL))) {
1716 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1717 if (cls[cl_idx] == NULL)
1721 if (prop == ZFS_PROP_MOUNTPOINT &&
1722 changelist_haszonedchild(cls[cl_idx])) {
1723 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1724 "child dataset with inherited mountpoint is used "
1725 "in a non-global zone"));
1726 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1730 /* We don't support those properties on FreeBSD. */
1732 case ZFS_PROP_DEVICES:
1733 case ZFS_PROP_ISCSIOPTIONS:
1734 case ZFS_PROP_XATTR:
1735 case ZFS_PROP_VSCAN:
1736 case ZFS_PROP_NBMAND:
1737 case ZFS_PROP_MLSLABEL:
1738 (void) snprintf(errbuf, sizeof (errbuf),
1739 "property '%s' not supported on FreeBSD",
1741 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1745 if (cls[cl_idx] != NULL &&
1746 (ret = changelist_prefix(cls[cl_idx])) != 0)
1751 assert(cl_idx == nvl_len);
1754 * Execute the corresponding ioctl() to set this list of properties.
1756 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1758 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1759 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1762 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1765 /* Get the list of unset properties back and report them. */
1766 nvlist_t *errorprops = NULL;
1767 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1769 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1771 elem = nvlist_next_nvpair(nvl, elem)) {
1772 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1773 zfs_setprop_error(hdl, prop, errno, errbuf);
1775 nvlist_free(errorprops);
1777 if (added_resv && errno == ENOSPC) {
1778 /* clean up the volsize property we tried to set */
1779 uint64_t old_volsize = zfs_prop_get_int(zhp,
1783 zcmd_free_nvlists(&zc);
1785 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1787 if (nvlist_add_uint64(nvl,
1788 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1791 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1793 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1796 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1797 if (cls[cl_idx] != NULL) {
1798 int clp_err = changelist_postfix(cls[cl_idx]);
1805 * Refresh the statistics so the new property value
1809 (void) get_stats(zhp);
1814 zcmd_free_nvlists(&zc);
1816 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1817 if (cls[cl_idx] != NULL)
1818 changelist_free(cls[cl_idx]);
1826 * Given a property, inherit the value from the parent dataset, or if received
1827 * is TRUE, revert to the received value, if any.
1830 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1832 zfs_cmd_t zc = { 0 };
1834 prop_changelist_t *cl;
1835 libzfs_handle_t *hdl = zhp->zfs_hdl;
1839 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1840 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1842 zc.zc_cookie = received;
1843 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1845 * For user properties, the amount of work we have to do is very
1846 * small, so just do it here.
1848 if (!zfs_prop_user(propname)) {
1849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1850 "invalid property"));
1851 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1854 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1855 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1857 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1858 return (zfs_standard_error(hdl, errno, errbuf));
1864 * Verify that this property is inheritable.
1866 if (zfs_prop_readonly(prop))
1867 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1869 if (!zfs_prop_inheritable(prop) && !received)
1870 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1873 * Check to see if the value applies to this type
1875 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1876 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1879 * Normalize the name, to get rid of shorthand abbreviations.
1881 propname = zfs_prop_to_name(prop);
1882 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1883 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1885 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1886 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1887 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1888 "dataset is used in a non-global zone"));
1889 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1893 * Determine datasets which will be affected by this change, if any.
1895 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1898 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1899 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1900 "child dataset with inherited mountpoint is used "
1901 "in a non-global zone"));
1902 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1906 if ((ret = changelist_prefix(cl)) != 0)
1909 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1910 return (zfs_standard_error(hdl, errno, errbuf));
1913 if ((ret = changelist_postfix(cl)) != 0)
1917 * Refresh the statistics so the new property is reflected.
1919 (void) get_stats(zhp);
1923 changelist_free(cl);
1928 * True DSL properties are stored in an nvlist. The following two functions
1929 * extract them appropriately.
1932 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1938 if (nvlist_lookup_nvlist(zhp->zfs_props,
1939 zfs_prop_to_name(prop), &nv) == 0) {
1940 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1941 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1943 verify(!zhp->zfs_props_table ||
1944 zhp->zfs_props_table[prop] == B_TRUE);
1945 value = zfs_prop_default_numeric(prop);
1953 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1959 if (nvlist_lookup_nvlist(zhp->zfs_props,
1960 zfs_prop_to_name(prop), &nv) == 0) {
1961 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1962 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1964 verify(!zhp->zfs_props_table ||
1965 zhp->zfs_props_table[prop] == B_TRUE);
1966 value = zfs_prop_default_string(prop);
1974 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1976 return (zhp->zfs_props == zhp->zfs_recvd_props);
1980 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1982 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1983 zhp->zfs_props = zhp->zfs_recvd_props;
1987 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1989 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1994 * Internal function for getting a numeric property. Both zfs_prop_get() and
1995 * zfs_prop_get_int() are built using this interface.
1997 * Certain properties can be overridden using 'mount -o'. In this case, scan
1998 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1999 * If they differ from the on-disk values, report the current values and mark
2000 * the source "temporary".
2003 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2004 char **source, uint64_t *val)
2006 zfs_cmd_t zc = { 0 };
2007 nvlist_t *zplprops = NULL;
2009 char *mntopt_on = NULL;
2010 char *mntopt_off = NULL;
2011 boolean_t received = zfs_is_recvd_props_mode(zhp);
2016 case ZFS_PROP_ATIME:
2017 mntopt_on = MNTOPT_ATIME;
2018 mntopt_off = MNTOPT_NOATIME;
2021 case ZFS_PROP_DEVICES:
2022 mntopt_on = MNTOPT_DEVICES;
2023 mntopt_off = MNTOPT_NODEVICES;
2027 mntopt_on = MNTOPT_EXEC;
2028 mntopt_off = MNTOPT_NOEXEC;
2031 case ZFS_PROP_READONLY:
2032 mntopt_on = MNTOPT_RO;
2033 mntopt_off = MNTOPT_RW;
2036 case ZFS_PROP_SETUID:
2037 mntopt_on = MNTOPT_SETUID;
2038 mntopt_off = MNTOPT_NOSETUID;
2041 case ZFS_PROP_XATTR:
2042 mntopt_on = MNTOPT_XATTR;
2043 mntopt_off = MNTOPT_NOXATTR;
2046 case ZFS_PROP_NBMAND:
2047 mntopt_on = MNTOPT_NBMAND;
2048 mntopt_off = MNTOPT_NONBMAND;
2056 * Because looking up the mount options is potentially expensive
2057 * (iterating over all of /etc/mnttab), we defer its calculation until
2058 * we're looking up a property which requires its presence.
2060 if (!zhp->zfs_mntcheck &&
2061 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2062 libzfs_handle_t *hdl = zhp->zfs_hdl;
2063 struct mnttab entry;
2065 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2066 zhp->zfs_mntopts = zfs_strdup(hdl,
2068 if (zhp->zfs_mntopts == NULL)
2072 zhp->zfs_mntcheck = B_TRUE;
2075 if (zhp->zfs_mntopts == NULL)
2076 mnt.mnt_mntopts = "";
2078 mnt.mnt_mntopts = zhp->zfs_mntopts;
2081 case ZFS_PROP_ATIME:
2082 case ZFS_PROP_DEVICES:
2084 case ZFS_PROP_READONLY:
2085 case ZFS_PROP_SETUID:
2086 case ZFS_PROP_XATTR:
2087 case ZFS_PROP_NBMAND:
2088 *val = getprop_uint64(zhp, prop, source);
2093 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2096 *src = ZPROP_SRC_TEMPORARY;
2097 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2100 *src = ZPROP_SRC_TEMPORARY;
2104 case ZFS_PROP_CANMOUNT:
2105 case ZFS_PROP_VOLSIZE:
2106 case ZFS_PROP_QUOTA:
2107 case ZFS_PROP_REFQUOTA:
2108 case ZFS_PROP_RESERVATION:
2109 case ZFS_PROP_REFRESERVATION:
2110 case ZFS_PROP_FILESYSTEM_LIMIT:
2111 case ZFS_PROP_SNAPSHOT_LIMIT:
2112 case ZFS_PROP_FILESYSTEM_COUNT:
2113 case ZFS_PROP_SNAPSHOT_COUNT:
2114 *val = getprop_uint64(zhp, prop, source);
2116 if (*source == NULL) {
2117 /* not default, must be local */
2118 *source = zhp->zfs_name;
2122 case ZFS_PROP_MOUNTED:
2123 *val = (zhp->zfs_mntopts != NULL);
2126 case ZFS_PROP_NUMCLONES:
2127 *val = zhp->zfs_dmustats.dds_num_clones;
2130 case ZFS_PROP_VERSION:
2131 case ZFS_PROP_NORMALIZE:
2132 case ZFS_PROP_UTF8ONLY:
2134 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2135 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2137 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2138 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2139 zcmd_free_nvlists(&zc);
2142 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2143 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2145 zcmd_free_nvlists(&zc);
2148 nvlist_free(zplprops);
2149 zcmd_free_nvlists(&zc);
2152 case ZFS_PROP_INCONSISTENT:
2153 *val = zhp->zfs_dmustats.dds_inconsistent;
2157 switch (zfs_prop_get_type(prop)) {
2158 case PROP_TYPE_NUMBER:
2159 case PROP_TYPE_INDEX:
2160 *val = getprop_uint64(zhp, prop, source);
2162 * If we tried to use a default value for a
2163 * readonly property, it means that it was not
2166 if (zfs_prop_readonly(prop) &&
2167 *source != NULL && (*source)[0] == '\0') {
2172 case PROP_TYPE_STRING:
2174 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2175 "cannot get non-numeric property"));
2176 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2177 dgettext(TEXT_DOMAIN, "internal error")));
2185 * Calculate the source type, given the raw source string.
2188 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2189 char *statbuf, size_t statlen)
2191 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2194 if (source == NULL) {
2195 *srctype = ZPROP_SRC_NONE;
2196 } else if (source[0] == '\0') {
2197 *srctype = ZPROP_SRC_DEFAULT;
2198 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2199 *srctype = ZPROP_SRC_RECEIVED;
2201 if (strcmp(source, zhp->zfs_name) == 0) {
2202 *srctype = ZPROP_SRC_LOCAL;
2204 (void) strlcpy(statbuf, source, statlen);
2205 *srctype = ZPROP_SRC_INHERITED;
2212 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2213 size_t proplen, boolean_t literal)
2218 if (zhp->zfs_recvd_props == NULL)
2219 if (get_recvd_props_ioctl(zhp) != 0)
2222 prop = zfs_name_to_prop(propname);
2224 if (prop != ZPROP_INVAL) {
2226 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2228 zfs_set_recvd_props_mode(zhp, &cookie);
2229 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2230 NULL, NULL, 0, literal);
2231 zfs_unset_recvd_props_mode(zhp, &cookie);
2235 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2236 propname, &propval) != 0)
2238 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2240 (void) strlcpy(propbuf, recvdval, proplen);
2243 return (err == 0 ? 0 : -1);
2247 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2252 value = zfs_get_clones_nvl(zhp);
2257 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2258 pair = nvlist_next_nvpair(value, pair)) {
2259 if (propbuf[0] != '\0')
2260 (void) strlcat(propbuf, ",", proplen);
2261 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2267 struct get_clones_arg {
2271 char buf[ZFS_MAX_DATASET_NAME_LEN];
2275 get_clones_cb(zfs_handle_t *zhp, void *arg)
2277 struct get_clones_arg *gca = arg;
2279 if (gca->numclones == 0) {
2284 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2285 NULL, NULL, 0, B_TRUE) != 0)
2287 if (strcmp(gca->buf, gca->origin) == 0) {
2288 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2293 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2299 zfs_get_clones_nvl(zfs_handle_t *zhp)
2301 nvlist_t *nv, *value;
2303 if (nvlist_lookup_nvlist(zhp->zfs_props,
2304 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2305 struct get_clones_arg gca;
2308 * if this is a snapshot, then the kernel wasn't able
2309 * to get the clones. Do it by slowly iterating.
2311 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2313 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2315 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2320 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2322 gca.origin = zhp->zfs_name;
2324 if (gca.numclones != 0) {
2326 char pool[ZFS_MAX_DATASET_NAME_LEN];
2329 /* get the pool name */
2330 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2331 (void) strsep(&cp, "/@");
2332 root = zfs_open(zhp->zfs_hdl, pool,
2333 ZFS_TYPE_FILESYSTEM);
2335 (void) get_clones_cb(root, &gca);
2338 if (gca.numclones != 0 ||
2339 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2340 nvlist_add_nvlist(zhp->zfs_props,
2341 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2348 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2349 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2352 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2358 * Accepts a property and value and checks that the value
2359 * matches the one found by the channel program. If they are
2360 * not equal, print both of them.
2363 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2366 if (!zhp->zfs_hdl->libzfs_prop_debug)
2369 char *poolname = zhp->zpool_hdl->zpool_name;
2370 const char *program =
2372 "ds = args['dataset']\n"
2373 "prop = args['property']\n"
2374 "value, setpoint = zfs.get_prop(ds, prop)\n"
2375 "return {value=value, setpoint=setpoint}\n";
2378 nvlist_t *argnvl = fnvlist_alloc();
2380 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2381 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2383 error = lzc_channel_program(poolname, program,
2384 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2387 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2388 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2390 error = nvlist_lookup_int64(retnvl, "value", &ans);
2392 (void) fprintf(stderr, "zcp check error: %u\n",
2396 if (ans != intval) {
2397 (void) fprintf(stderr,
2398 "%s: zfs found %lld, but zcp found %lld\n",
2399 zfs_prop_to_name(prop),
2400 (longlong_t)intval, (longlong_t)ans);
2404 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2406 (void) fprintf(stderr, "zcp check error: %u\n",
2410 if (strcmp(strval, str_ans) != 0) {
2411 (void) fprintf(stderr,
2412 "%s: zfs found %s, but zcp found %s\n",
2413 zfs_prop_to_name(prop),
2418 (void) fprintf(stderr,
2419 "zcp check failed, channel program error: %u\n", error);
2421 nvlist_free(argnvl);
2422 nvlist_free(outnvl);
2426 * Retrieve a property from the given object. If 'literal' is specified, then
2427 * numbers are left as exact values. Otherwise, numbers are converted to a
2428 * human-readable form.
2430 * Returns 0 on success, or -1 on error.
2433 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2434 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2436 char *source = NULL;
2440 boolean_t received = zfs_is_recvd_props_mode(zhp);
2443 * Check to see if this property applies to our object
2445 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2448 if (received && zfs_prop_readonly(prop))
2452 *src = ZPROP_SRC_NONE;
2455 case ZFS_PROP_CREATION:
2457 * 'creation' is a time_t stored in the statistics. We convert
2458 * this into a string unless 'literal' is specified.
2461 val = getprop_uint64(zhp, prop, &source);
2462 time_t time = (time_t)val;
2466 localtime_r(&time, &t) == NULL ||
2467 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2469 (void) snprintf(propbuf, proplen, "%llu", val);
2471 zcp_check(zhp, prop, val, NULL);
2474 case ZFS_PROP_MOUNTPOINT:
2476 * Getting the precise mountpoint can be tricky.
2478 * - for 'none' or 'legacy', return those values.
2479 * - for inherited mountpoints, we want to take everything
2480 * after our ancestor and append it to the inherited value.
2482 * If the pool has an alternate root, we want to prepend that
2483 * root to any values we return.
2486 str = getprop_string(zhp, prop, &source);
2488 if (str[0] == '/') {
2489 char buf[MAXPATHLEN];
2491 const char *relpath;
2494 * If we inherit the mountpoint, even from a dataset
2495 * with a received value, the source will be the path of
2496 * the dataset we inherit from. If source is
2497 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2500 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2503 relpath = zhp->zfs_name + strlen(source);
2504 if (relpath[0] == '/')
2508 if ((zpool_get_prop(zhp->zpool_hdl,
2509 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2510 B_FALSE)) || (strcmp(root, "-") == 0))
2513 * Special case an alternate root of '/'. This will
2514 * avoid having multiple leading slashes in the
2517 if (strcmp(root, "/") == 0)
2521 * If the mountpoint is '/' then skip over this
2522 * if we are obtaining either an alternate root or
2523 * an inherited mountpoint.
2525 if (str[1] == '\0' && (root[0] != '\0' ||
2526 relpath[0] != '\0'))
2529 if (relpath[0] == '\0')
2530 (void) snprintf(propbuf, proplen, "%s%s",
2533 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2534 root, str, relpath[0] == '@' ? "" : "/",
2537 /* 'legacy' or 'none' */
2538 (void) strlcpy(propbuf, str, proplen);
2540 zcp_check(zhp, prop, NULL, propbuf);
2543 case ZFS_PROP_ORIGIN:
2544 str = getprop_string(zhp, prop, &source);
2547 (void) strlcpy(propbuf, str, proplen);
2548 zcp_check(zhp, prop, NULL, str);
2551 case ZFS_PROP_CLONES:
2552 if (get_clones_string(zhp, propbuf, proplen) != 0)
2556 case ZFS_PROP_QUOTA:
2557 case ZFS_PROP_REFQUOTA:
2558 case ZFS_PROP_RESERVATION:
2559 case ZFS_PROP_REFRESERVATION:
2561 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2564 * If quota or reservation is 0, we translate this into 'none'
2565 * (unless literal is set), and indicate that it's the default
2566 * value. Otherwise, we print the number nicely and indicate
2567 * that its set locally.
2571 (void) strlcpy(propbuf, "0", proplen);
2573 (void) strlcpy(propbuf, "none", proplen);
2576 (void) snprintf(propbuf, proplen, "%llu",
2579 zfs_nicenum(val, propbuf, proplen);
2581 zcp_check(zhp, prop, val, NULL);
2584 case ZFS_PROP_FILESYSTEM_LIMIT:
2585 case ZFS_PROP_SNAPSHOT_LIMIT:
2586 case ZFS_PROP_FILESYSTEM_COUNT:
2587 case ZFS_PROP_SNAPSHOT_COUNT:
2589 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2593 * If limit is UINT64_MAX, we translate this into 'none' (unless
2594 * literal is set), and indicate that it's the default value.
2595 * Otherwise, we print the number nicely and indicate that it's
2599 (void) snprintf(propbuf, proplen, "%llu",
2601 } else if (val == UINT64_MAX) {
2602 (void) strlcpy(propbuf, "none", proplen);
2604 zfs_nicenum(val, propbuf, proplen);
2607 zcp_check(zhp, prop, val, NULL);
2610 case ZFS_PROP_REFRATIO:
2611 case ZFS_PROP_COMPRESSRATIO:
2612 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2614 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2615 (u_longlong_t)(val / 100),
2616 (u_longlong_t)(val % 100));
2617 zcp_check(zhp, prop, val, NULL);
2621 switch (zhp->zfs_type) {
2622 case ZFS_TYPE_FILESYSTEM:
2625 case ZFS_TYPE_VOLUME:
2628 case ZFS_TYPE_SNAPSHOT:
2631 case ZFS_TYPE_BOOKMARK:
2637 (void) snprintf(propbuf, proplen, "%s", str);
2638 zcp_check(zhp, prop, NULL, propbuf);
2641 case ZFS_PROP_MOUNTED:
2643 * The 'mounted' property is a pseudo-property that described
2644 * whether the filesystem is currently mounted. Even though
2645 * it's a boolean value, the typical values of "on" and "off"
2646 * don't make sense, so we translate to "yes" and "no".
2648 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2649 src, &source, &val) != 0)
2652 (void) strlcpy(propbuf, "yes", proplen);
2654 (void) strlcpy(propbuf, "no", proplen);
2659 * The 'name' property is a pseudo-property derived from the
2660 * dataset name. It is presented as a real property to simplify
2663 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2664 zcp_check(zhp, prop, NULL, propbuf);
2667 case ZFS_PROP_MLSLABEL:
2670 m_label_t *new_sl = NULL;
2671 char *ascii = NULL; /* human readable label */
2673 (void) strlcpy(propbuf,
2674 getprop_string(zhp, prop, &source), proplen);
2676 if (literal || (strcasecmp(propbuf,
2677 ZFS_MLSLABEL_DEFAULT) == 0))
2681 * Try to translate the internal hex string to
2682 * human-readable output. If there are any
2683 * problems just use the hex string.
2686 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2687 L_NO_CORRECTION, NULL) == -1) {
2688 m_label_free(new_sl);
2692 if (label_to_str(new_sl, &ascii, M_LABEL,
2696 m_label_free(new_sl);
2699 m_label_free(new_sl);
2701 (void) strlcpy(propbuf, ascii, proplen);
2703 #else /* !illumos */
2705 #endif /* illumos */
2711 * GUIDs are stored as numbers, but they are identifiers.
2712 * We don't want them to be pretty printed, because pretty
2713 * printing mangles the ID into a truncated and useless value.
2715 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2717 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2718 zcp_check(zhp, prop, val, NULL);
2722 switch (zfs_prop_get_type(prop)) {
2723 case PROP_TYPE_NUMBER:
2724 if (get_numeric_property(zhp, prop, src,
2725 &source, &val) != 0) {
2730 (void) snprintf(propbuf, proplen, "%llu",
2733 zfs_nicenum(val, propbuf, proplen);
2735 zcp_check(zhp, prop, val, NULL);
2738 case PROP_TYPE_STRING:
2739 str = getprop_string(zhp, prop, &source);
2743 (void) strlcpy(propbuf, str, proplen);
2744 zcp_check(zhp, prop, NULL, str);
2747 case PROP_TYPE_INDEX:
2748 if (get_numeric_property(zhp, prop, src,
2749 &source, &val) != 0)
2751 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2754 (void) strlcpy(propbuf, strval, proplen);
2755 zcp_check(zhp, prop, NULL, strval);
2763 get_source(zhp, src, source, statbuf, statlen);
2769 * Utility function to get the given numeric property. Does no validation that
2770 * the given property is the appropriate type; should only be used with
2771 * hard-coded property types.
2774 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2779 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2785 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2789 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2790 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2794 * Similar to zfs_prop_get(), but returns the value as an integer.
2797 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2798 zprop_source_t *src, char *statbuf, size_t statlen)
2803 * Check to see if this property applies to our object
2805 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2806 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2807 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2808 zfs_prop_to_name(prop)));
2812 *src = ZPROP_SRC_NONE;
2814 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2817 get_source(zhp, src, source, statbuf, statlen);
2823 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2824 char **domainp, idmap_rid_t *ridp)
2827 idmap_get_handle_t *get_hdl = NULL;
2831 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2835 err = idmap_get_sidbyuid(get_hdl, id,
2836 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2838 err = idmap_get_sidbygid(get_hdl, id,
2839 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2841 if (err == IDMAP_SUCCESS &&
2842 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2843 status == IDMAP_SUCCESS)
2849 idmap_get_destroy(get_hdl);
2851 #else /* !illumos */
2852 assert(!"invalid code path");
2853 return (EINVAL); // silence compiler warning
2854 #endif /* illumos */
2858 * convert the propname into parameters needed by kernel
2859 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2860 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2863 userquota_propname_decode(const char *propname, boolean_t zoned,
2864 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2866 zfs_userquota_prop_t type;
2868 char *numericsid = NULL;
2873 /* Figure out the property type ({user|group}{quota|space}) */
2874 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2875 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2876 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2879 if (type == ZFS_NUM_USERQUOTA_PROPS)
2883 isuser = (type == ZFS_PROP_USERQUOTA ||
2884 type == ZFS_PROP_USERUSED);
2886 cp = strchr(propname, '@') + 1;
2888 if (strchr(cp, '@')) {
2891 * It's a SID name (eg "user@domain") that needs to be
2892 * turned into S-1-domainID-RID.
2895 idmap_stat stat, map_stat;
2898 idmap_get_handle_t *gh = NULL;
2900 stat = idmap_get_create(&gh);
2901 if (stat != IDMAP_SUCCESS) {
2902 idmap_get_destroy(gh);
2905 if (zoned && getzoneid() == GLOBAL_ZONEID)
2908 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2911 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2914 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2917 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2921 idmap_get_destroy(gh);
2924 stat = idmap_get_mappings(gh);
2925 idmap_get_destroy(gh);
2930 if (numericsid == NULL)
2934 /* will be further decoded below */
2935 #else /* !illumos */
2937 #endif /* illumos */
2940 if (strncmp(cp, "S-1-", 4) == 0) {
2941 /* It's a numeric SID (eg "S-1-234-567-89") */
2942 (void) strlcpy(domain, cp, domainlen);
2945 cp = strrchr(domain, '-');
2948 *ridp = strtoull(cp, &end, 10);
2956 if (errno != 0 || *end != '\0')
2958 } else if (!isdigit(*cp)) {
2960 * It's a user/group name (eg "user") that needs to be
2961 * turned into a uid/gid
2963 if (zoned && getzoneid() == GLOBAL_ZONEID)
2979 /* It's a user/group ID (eg "12345"). */
2980 uid_t id = strtoul(cp, &end, 10);
2987 /* It's an ephemeral ID. */
2988 if (idmap_id_to_numeric_domain_rid(id, isuser,
2989 &mapdomain, &rid) != 0)
2991 (void) strlcpy(domain, mapdomain, domainlen);
2998 ASSERT3P(numericsid, ==, NULL);
3003 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3004 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3007 zfs_cmd_t zc = { 0 };
3009 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3011 err = userquota_propname_decode(propname,
3012 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3013 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3014 zc.zc_objset_type = *typep;
3018 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3022 *propvalue = zc.zc_cookie;
3027 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3028 uint64_t *propvalue)
3030 zfs_userquota_prop_t type;
3032 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3037 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3038 char *propbuf, int proplen, boolean_t literal)
3042 zfs_userquota_prop_t type;
3044 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3051 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3052 } else if (propvalue == 0 &&
3053 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
3054 (void) strlcpy(propbuf, "none", proplen);
3056 zfs_nicenum(propvalue, propbuf, proplen);
3062 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3063 uint64_t *propvalue)
3066 zfs_cmd_t zc = { 0 };
3067 const char *snapname;
3069 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3071 snapname = strchr(propname, '@') + 1;
3072 if (strchr(snapname, '@')) {
3073 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3075 /* snapname is the short name, append it to zhp's fsname */
3078 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3079 sizeof (zc.zc_value));
3080 cp = strchr(zc.zc_value, '@');
3083 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3084 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3087 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3091 *propvalue = zc.zc_cookie;
3096 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3097 char *propbuf, int proplen, boolean_t literal)
3102 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3108 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3110 zfs_nicenum(propvalue, propbuf, proplen);
3116 * Returns the name of the given zfs handle.
3119 zfs_get_name(const zfs_handle_t *zhp)
3121 return (zhp->zfs_name);
3125 * Returns the name of the parent pool for the given zfs handle.
3128 zfs_get_pool_name(const zfs_handle_t *zhp)
3130 return (zhp->zpool_hdl->zpool_name);
3134 * Returns the type of the given zfs handle.
3137 zfs_get_type(const zfs_handle_t *zhp)
3139 return (zhp->zfs_type);
3143 * Is one dataset name a child dataset of another?
3145 * Needs to handle these cases:
3146 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3147 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3148 * Descendant? No. No. No. Yes.
3151 is_descendant(const char *ds1, const char *ds2)
3153 size_t d1len = strlen(ds1);
3155 /* ds2 can't be a descendant if it's smaller */
3156 if (strlen(ds2) < d1len)
3159 /* otherwise, compare strings and verify that there's a '/' char */
3160 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3164 * Given a complete name, return just the portion that refers to the parent.
3165 * Will return -1 if there is no parent (path is just the name of the
3169 parent_name(const char *path, char *buf, size_t buflen)
3173 (void) strlcpy(buf, path, buflen);
3175 if ((slashp = strrchr(buf, '/')) == NULL)
3183 * If accept_ancestor is false, then check to make sure that the given path has
3184 * a parent, and that it exists. If accept_ancestor is true, then find the
3185 * closest existing ancestor for the given path. In prefixlen return the
3186 * length of already existing prefix of the given path. We also fetch the
3187 * 'zoned' property, which is used to validate property settings when creating
3191 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3192 boolean_t accept_ancestor, int *prefixlen)
3194 zfs_cmd_t zc = { 0 };
3195 char parent[ZFS_MAX_DATASET_NAME_LEN];
3201 (void) snprintf(errbuf, sizeof (errbuf),
3202 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3204 /* get parent, and check to see if this is just a pool */
3205 if (parent_name(path, parent, sizeof (parent)) != 0) {
3206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3207 "missing dataset name"));
3208 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3211 /* check to see if the pool exists */
3212 if ((slash = strchr(parent, '/')) == NULL)
3213 slash = parent + strlen(parent);
3214 (void) strncpy(zc.zc_name, parent, slash - parent);
3215 zc.zc_name[slash - parent] = '\0';
3216 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3218 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3219 "no such pool '%s'"), zc.zc_name);
3220 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3223 /* check to see if the parent dataset exists */
3224 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3225 if (errno == ENOENT && accept_ancestor) {
3227 * Go deeper to find an ancestor, give up on top level.
3229 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3230 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3231 "no such pool '%s'"), zc.zc_name);
3232 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3234 } else if (errno == ENOENT) {
3235 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3236 "parent does not exist"));
3237 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3239 return (zfs_standard_error(hdl, errno, errbuf));
3242 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3246 /* we are in a non-global zone, but parent is in the global zone */
3247 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3248 (void) zfs_standard_error(hdl, EPERM, errbuf);
3253 /* make sure parent is a filesystem */
3254 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3255 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3256 "parent is not a filesystem"));
3257 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3263 if (prefixlen != NULL)
3264 *prefixlen = strlen(parent);
3269 * Finds whether the dataset of the given type(s) exists.
3272 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3276 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3280 * Try to get stats for the dataset, which will tell us if it exists.
3282 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3283 int ds_type = zhp->zfs_type;
3286 if (types & ds_type)
3293 * Given a path to 'target', create all the ancestors between
3294 * the prefixlen portion of the path, and the target itself.
3295 * Fail if the initial prefixlen-ancestor does not already exist.
3298 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3304 /* make sure prefix exists */
3305 cp = target + prefixlen;
3307 assert(strchr(cp, '/') == NULL);
3308 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3311 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3319 * Attempt to create, mount, and share any ancestor filesystems,
3320 * up to the prefixlen-long one.
3322 for (cp = target + prefixlen + 1;
3323 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3327 h = make_dataset_handle(hdl, target);
3329 /* it already exists, nothing to do here */
3334 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3336 opname = dgettext(TEXT_DOMAIN, "create");
3340 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3342 opname = dgettext(TEXT_DOMAIN, "open");
3346 if (zfs_mount(h, NULL, 0) != 0) {
3347 opname = dgettext(TEXT_DOMAIN, "mount");
3351 if (zfs_share(h) != 0) {
3352 opname = dgettext(TEXT_DOMAIN, "share");
3362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3363 "failed to %s ancestor '%s'"), opname, target);
3368 * Creates non-existing ancestors of the given path.
3371 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3377 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3380 if ((path_copy = strdup(path)) != NULL) {
3381 rc = create_parents(hdl, path_copy, prefix);
3384 if (path_copy == NULL || rc != 0)
3391 * Create a new filesystem or volume.
3394 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3399 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3402 enum lzc_dataset_type ost;
3403 zpool_handle_t *zpool_handle;
3405 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3406 "cannot create '%s'"), path);
3408 /* validate the path, taking care to note the extended error message */
3409 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3410 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3412 /* validate parents exist */
3413 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3417 * The failure modes when creating a dataset of a different type over
3418 * one that already exists is a little strange. In particular, if you
3419 * try to create a dataset on top of an existing dataset, the ioctl()
3420 * will return ENOENT, not EEXIST. To prevent this from happening, we
3421 * first try to see if the dataset exists.
3423 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3425 "dataset already exists"));
3426 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3429 if (type == ZFS_TYPE_VOLUME)
3430 ost = LZC_DATSET_TYPE_ZVOL;
3432 ost = LZC_DATSET_TYPE_ZFS;
3434 /* open zpool handle for prop validation */
3435 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3436 (void) strlcpy(pool_path, path, sizeof (pool_path));
3438 /* truncate pool_path at first slash */
3439 char *p = strchr(pool_path, '/');
3443 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3446 if (props && (props = zfs_valid_proplist(hdl, type, props,
3447 zoned, NULL, zpool_handle, errbuf)) == 0) {
3448 zpool_close(zpool_handle);
3451 zpool_close(zpool_handle);
3453 if (type == ZFS_TYPE_VOLUME) {
3455 * If we are creating a volume, the size and block size must
3456 * satisfy a few restraints. First, the blocksize must be a
3457 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3458 * volsize must be a multiple of the block size, and cannot be
3461 if (props == NULL || nvlist_lookup_uint64(props,
3462 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3465 "missing volume size"));
3466 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3469 if ((ret = nvlist_lookup_uint64(props,
3470 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3471 &blocksize)) != 0) {
3472 if (ret == ENOENT) {
3473 blocksize = zfs_prop_default_numeric(
3474 ZFS_PROP_VOLBLOCKSIZE);
3477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3478 "missing volume block size"));
3479 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3486 "volume size cannot be zero"));
3487 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3490 if (size % blocksize != 0) {
3492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3493 "volume size must be a multiple of volume block "
3495 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3499 /* create the dataset */
3500 ret = lzc_create(path, ost, props);
3503 /* check for failure */
3505 char parent[ZFS_MAX_DATASET_NAME_LEN];
3506 (void) parent_name(path, parent, sizeof (parent));
3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3511 "no such parent '%s'"), parent);
3512 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3516 "parent '%s' is not a filesystem"), parent);
3517 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3521 "pool must be upgraded to set this "
3522 "property or value"));
3523 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3527 * This platform can't address a volume this big.
3529 if (type == ZFS_TYPE_VOLUME)
3530 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3535 return (zfs_standard_error(hdl, errno, errbuf));
3543 * Destroys the given dataset. The caller must make sure that the filesystem
3544 * isn't mounted, and that there are no active dependents. If the file system
3545 * does not exist this function does nothing.
3548 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3550 zfs_cmd_t zc = { 0 };
3552 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3553 nvlist_t *nv = fnvlist_alloc();
3554 fnvlist_add_boolean(nv, zhp->zfs_name);
3555 int error = lzc_destroy_bookmarks(nv, NULL);
3558 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3559 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3565 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3567 if (ZFS_IS_VOLUME(zhp)) {
3568 zc.zc_objset_type = DMU_OST_ZVOL;
3570 zc.zc_objset_type = DMU_OST_ZFS;
3573 zc.zc_defer_destroy = defer;
3574 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3576 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3577 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3581 remove_mountpoint(zhp);
3586 struct destroydata {
3588 const char *snapname;
3592 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3594 struct destroydata *dd = arg;
3595 char name[ZFS_MAX_DATASET_NAME_LEN];
3598 (void) snprintf(name, sizeof (name),
3599 "%s@%s", zhp->zfs_name, dd->snapname);
3601 if (lzc_exists(name))
3602 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3604 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3610 * Destroys all snapshots with the given name in zhp & descendants.
3613 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3616 struct destroydata dd = { 0 };
3618 dd.snapname = snapname;
3619 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3620 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3622 if (nvlist_empty(dd.nvl)) {
3623 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3624 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3625 zhp->zfs_name, snapname);
3627 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3629 nvlist_free(dd.nvl);
3634 * Destroys all the snapshots named in the nvlist.
3637 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3640 nvlist_t *errlist = NULL;
3642 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3645 nvlist_free(errlist);
3649 if (nvlist_empty(errlist)) {
3651 (void) snprintf(errbuf, sizeof (errbuf),
3652 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3654 ret = zfs_standard_error(hdl, ret, errbuf);
3656 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3657 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3659 (void) snprintf(errbuf, sizeof (errbuf),
3660 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3663 switch (fnvpair_value_int32(pair)) {
3666 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3667 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3670 ret = zfs_standard_error(hdl, errno, errbuf);
3675 nvlist_free(errlist);
3680 * Clones the given dataset. The target must be of the same type as the source.
3683 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3685 char parent[ZFS_MAX_DATASET_NAME_LEN];
3688 libzfs_handle_t *hdl = zhp->zfs_hdl;
3691 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3693 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3694 "cannot create '%s'"), target);
3696 /* validate the target/clone name */
3697 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3698 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3700 /* validate parents exist */
3701 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3704 (void) parent_name(target, parent, sizeof (parent));
3710 if (ZFS_IS_VOLUME(zhp)) {
3711 type = ZFS_TYPE_VOLUME;
3713 type = ZFS_TYPE_FILESYSTEM;
3715 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3716 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3720 ret = lzc_clone(target, zhp->zfs_name, props);
3728 * The parent doesn't exist. We should have caught this
3729 * above, but there may a race condition that has since
3730 * destroyed the parent.
3732 * At this point, we don't know whether it's the source
3733 * that doesn't exist anymore, or whether the target
3734 * dataset doesn't exist.
3736 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3737 "no such parent '%s'"), parent);
3738 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3741 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3742 "source and target pools differ"));
3743 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3747 return (zfs_standard_error(zhp->zfs_hdl, errno,
3756 * Promotes the given clone fs to be the clone parent.
3759 zfs_promote(zfs_handle_t *zhp)
3761 libzfs_handle_t *hdl = zhp->zfs_hdl;
3762 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3766 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3767 "cannot promote '%s'"), zhp->zfs_name);
3769 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3771 "snapshots can not be promoted"));
3772 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3775 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3777 "not a cloned filesystem"));
3778 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3781 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3786 /* There is a conflicting snapshot name. */
3787 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3788 "conflicting snapshot '%s' from parent '%s'"),
3789 snapname, zhp->zfs_dmustats.dds_origin);
3790 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3793 return (zfs_standard_error(hdl, ret, errbuf));
3799 typedef struct snapdata {
3801 const char *sd_snapname;
3805 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3807 snapdata_t *sd = arg;
3808 char name[ZFS_MAX_DATASET_NAME_LEN];
3811 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3812 (void) snprintf(name, sizeof (name),
3813 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3815 fnvlist_add_boolean(sd->sd_nvl, name);
3817 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3825 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3829 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3836 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3837 "cannot create snapshots "));
3840 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3841 const char *snapname = nvpair_name(elem);
3843 /* validate the target name */
3844 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3846 (void) snprintf(errbuf, sizeof (errbuf),
3847 dgettext(TEXT_DOMAIN,
3848 "cannot create snapshot '%s'"), snapname);
3849 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3854 * get pool handle for prop validation. assumes all snaps are in the
3855 * same pool, as does lzc_snapshot (below).
3857 char pool[ZFS_MAX_DATASET_NAME_LEN];
3858 elem = nvlist_next_nvpair(snaps, NULL);
3859 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3860 pool[strcspn(pool, "/@")] = '\0';
3861 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3863 if (props != NULL &&
3864 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3865 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3866 zpool_close(zpool_hdl);
3869 zpool_close(zpool_hdl);
3871 ret = lzc_snapshot(snaps, props, &errors);
3874 boolean_t printed = B_FALSE;
3875 for (elem = nvlist_next_nvpair(errors, NULL);
3877 elem = nvlist_next_nvpair(errors, elem)) {
3878 (void) snprintf(errbuf, sizeof (errbuf),
3879 dgettext(TEXT_DOMAIN,
3880 "cannot create snapshot '%s'"), nvpair_name(elem));
3881 (void) zfs_standard_error(hdl,
3882 fnvpair_value_int32(elem), errbuf);
3888 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3889 "multiple snapshots of same "
3891 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3895 (void) zfs_standard_error(hdl, ret, errbuf);
3901 nvlist_free(errors);
3906 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3910 snapdata_t sd = { 0 };
3911 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3916 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3917 "cannot snapshot %s"), path);
3919 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3920 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3922 (void) strlcpy(fsname, path, sizeof (fsname));
3923 cp = strchr(fsname, '@');
3925 sd.sd_snapname = cp + 1;
3927 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3928 ZFS_TYPE_VOLUME)) == NULL) {
3932 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3934 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3936 fnvlist_add_boolean(sd.sd_nvl, path);
3939 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3940 nvlist_free(sd.sd_nvl);
3946 * Destroy any more recent snapshots. We invoke this callback on any dependents
3947 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3948 * is a dependent and we should just destroy it without checking the transaction
3951 typedef struct rollback_data {
3952 const char *cb_target; /* the snapshot */
3953 uint64_t cb_create; /* creation time reference */
3959 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3961 rollback_data_t *cbp = data;
3962 prop_changelist_t *clp;
3964 /* We must destroy this clone; first unmount it */
3965 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3966 cbp->cb_force ? MS_FORCE: 0);
3967 if (clp == NULL || changelist_prefix(clp) != 0) {
3968 cbp->cb_error = B_TRUE;
3972 if (zfs_destroy(zhp, B_FALSE) != 0)
3973 cbp->cb_error = B_TRUE;
3975 changelist_remove(clp, zhp->zfs_name);
3976 (void) changelist_postfix(clp);
3977 changelist_free(clp);
3984 rollback_destroy(zfs_handle_t *zhp, void *data)
3986 rollback_data_t *cbp = data;
3988 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3989 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3990 rollback_destroy_dependent, cbp);
3992 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4000 * Given a dataset, rollback to a specific snapshot, discarding any
4001 * data changes since then and making it the active dataset.
4003 * Any snapshots and bookmarks more recent than the target are
4004 * destroyed, along with their dependents (i.e. clones).
4007 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4009 rollback_data_t cb = { 0 };
4011 boolean_t restore_resv = 0;
4012 uint64_t old_volsize = 0, new_volsize;
4013 zfs_prop_t resv_prop;
4015 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4016 zhp->zfs_type == ZFS_TYPE_VOLUME);
4019 * Destroy all recent snapshots and their dependents.
4021 cb.cb_force = force;
4022 cb.cb_target = snap->zfs_name;
4023 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4024 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4025 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4031 * Now that we have verified that the snapshot is the latest,
4032 * rollback to the given snapshot.
4035 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4036 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4038 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4040 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4044 * Pass both the filesystem and the wanted snapshot names,
4045 * we would get an error back if the snapshot is destroyed or
4046 * a new snapshot is created before this request is processed.
4048 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4050 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4051 "'%s' is not the latest snapshot"), snap->zfs_name);
4052 (void) zfs_error_fmt(zhp->zfs_hdl, EZFS_BUSY,
4053 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4056 } else if (err != 0) {
4057 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
4058 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4064 * For volumes, if the pre-rollback volsize matched the pre-
4065 * rollback reservation and the volsize has changed then set
4066 * the reservation property to the post-rollback volsize.
4067 * Make a new handle since the rollback closed the dataset.
4069 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4070 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4072 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4073 if (old_volsize != new_volsize)
4074 err = zfs_prop_set_int(zhp, resv_prop,
4083 * Renames the given dataset.
4086 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
4087 renameflags_t flags)
4090 zfs_cmd_t zc = { 0 };
4092 prop_changelist_t *cl = NULL;
4093 zfs_handle_t *zhrp = NULL;
4094 char *parentname = NULL;
4095 char parent[ZFS_MAX_DATASET_NAME_LEN];
4096 char property[ZFS_MAXPROPLEN];
4097 libzfs_handle_t *hdl = zhp->zfs_hdl;
4100 /* if we have the same exact name, just return success */
4101 if (strcmp(zhp->zfs_name, target) == 0)
4104 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4105 "cannot rename to '%s'"), target);
4107 if (source != NULL) {
4109 * This is recursive snapshots rename, put snapshot name
4110 * (that might not exist) into zfs_name.
4112 assert(flags.recurse);
4114 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
4115 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
4116 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
4120 * Make sure the target name is valid
4122 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4123 if ((strchr(target, '@') == NULL) ||
4126 * Snapshot target name is abbreviated,
4127 * reconstruct full dataset name
4129 (void) strlcpy(parent, zhp->zfs_name,
4131 delim = strchr(parent, '@');
4132 if (strchr(target, '@') == NULL)
4136 (void) strlcat(parent, target, sizeof (parent));
4140 * Make sure we're renaming within the same dataset.
4142 delim = strchr(target, '@');
4143 if (strncmp(zhp->zfs_name, target, delim - target)
4144 != 0 || zhp->zfs_name[delim - target] != '@') {
4145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4146 "snapshots must be part of same "
4148 return (zfs_error(hdl, EZFS_CROSSTARGET,
4152 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4153 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4155 if (flags.recurse) {
4156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4157 "recursive rename must be a snapshot"));
4158 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4161 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4162 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4164 /* validate parents */
4165 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4168 /* make sure we're in the same pool */
4169 verify((delim = strchr(target, '/')) != NULL);
4170 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4171 zhp->zfs_name[delim - target] != '/') {
4172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4173 "datasets must be within same pool"));
4174 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4177 /* new name cannot be a child of the current dataset name */
4178 if (is_descendant(zhp->zfs_name, target)) {
4179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4180 "New dataset name cannot be a descendant of "
4181 "current dataset name"));
4182 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4186 (void) snprintf(errbuf, sizeof (errbuf),
4187 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4189 if (getzoneid() == GLOBAL_ZONEID &&
4190 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4192 "dataset is used in a non-global zone"));
4193 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4197 * Avoid unmounting file systems with mountpoint property set to
4198 * 'legacy' or 'none' even if -u option is not given.
4200 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4201 !flags.recurse && !flags.nounmount &&
4202 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4203 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4204 (strcmp(property, "legacy") == 0 ||
4205 strcmp(property, "none") == 0)) {
4206 flags.nounmount = B_TRUE;
4208 if (flags.recurse) {
4210 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4211 if (parentname == NULL) {
4215 delim = strchr(parentname, '@');
4217 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4222 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4223 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4224 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
4225 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
4229 if (changelist_haszonedchild(cl)) {
4230 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4231 "child dataset with inherited mountpoint is used "
4232 "in a non-global zone"));
4233 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4238 if ((ret = changelist_prefix(cl)) != 0)
4242 if (ZFS_IS_VOLUME(zhp))
4243 zc.zc_objset_type = DMU_OST_ZVOL;
4245 zc.zc_objset_type = DMU_OST_ZFS;
4247 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4248 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4250 zc.zc_cookie = flags.recurse ? 1 : 0;
4251 if (flags.nounmount)
4254 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4256 * if it was recursive, the one that actually failed will
4259 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4260 "cannot rename '%s'"), zc.zc_name);
4262 if (flags.recurse && errno == EEXIST) {
4263 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4264 "a child dataset already has a snapshot "
4265 "with the new name"));
4266 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4268 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4272 * On failure, we still want to remount any filesystems that
4273 * were previously mounted, so we don't alter the system state.
4276 (void) changelist_postfix(cl);
4279 changelist_rename(cl, zfs_get_name(zhp), target);
4280 ret = changelist_postfix(cl);
4285 if (parentname != NULL) {
4292 changelist_free(cl);
4298 zfs_get_user_props(zfs_handle_t *zhp)
4300 return (zhp->zfs_user_props);
4304 zfs_get_recvd_props(zfs_handle_t *zhp)
4306 if (zhp->zfs_recvd_props == NULL)
4307 if (get_recvd_props_ioctl(zhp) != 0)
4309 return (zhp->zfs_recvd_props);
4313 * This function is used by 'zfs list' to determine the exact set of columns to
4314 * display, and their maximum widths. This does two main things:
4316 * - If this is a list of all properties, then expand the list to include
4317 * all native properties, and set a flag so that for each dataset we look
4318 * for new unique user properties and add them to the list.
4320 * - For non fixed-width properties, keep track of the maximum width seen
4321 * so that we can size the column appropriately. If the user has
4322 * requested received property values, we also need to compute the width
4323 * of the RECEIVED column.
4326 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4329 libzfs_handle_t *hdl = zhp->zfs_hdl;
4330 zprop_list_t *entry;
4331 zprop_list_t **last, **start;
4332 nvlist_t *userprops, *propval;
4335 char buf[ZFS_MAXPROPLEN];
4337 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4340 userprops = zfs_get_user_props(zhp);
4343 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4345 * Go through and add any user properties as necessary. We
4346 * start by incrementing our list pointer to the first
4347 * non-native property.
4350 while (*start != NULL) {
4351 if ((*start)->pl_prop == ZPROP_INVAL)
4353 start = &(*start)->pl_next;
4357 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4359 * See if we've already found this property in our list.
4361 for (last = start; *last != NULL;
4362 last = &(*last)->pl_next) {
4363 if (strcmp((*last)->pl_user_prop,
4364 nvpair_name(elem)) == 0)
4368 if (*last == NULL) {
4369 if ((entry = zfs_alloc(hdl,
4370 sizeof (zprop_list_t))) == NULL ||
4371 ((entry->pl_user_prop = zfs_strdup(hdl,
4372 nvpair_name(elem)))) == NULL) {
4377 entry->pl_prop = ZPROP_INVAL;
4378 entry->pl_width = strlen(nvpair_name(elem));
4379 entry->pl_all = B_TRUE;
4386 * Now go through and check the width of any non-fixed columns
4388 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4389 if (entry->pl_fixed && !literal)
4392 if (entry->pl_prop != ZPROP_INVAL) {
4393 if (zfs_prop_get(zhp, entry->pl_prop,
4394 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4395 if (strlen(buf) > entry->pl_width)
4396 entry->pl_width = strlen(buf);
4398 if (received && zfs_prop_get_recvd(zhp,
4399 zfs_prop_to_name(entry->pl_prop),
4400 buf, sizeof (buf), literal) == 0)
4401 if (strlen(buf) > entry->pl_recvd_width)
4402 entry->pl_recvd_width = strlen(buf);
4404 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4406 verify(nvlist_lookup_string(propval,
4407 ZPROP_VALUE, &strval) == 0);
4408 if (strlen(strval) > entry->pl_width)
4409 entry->pl_width = strlen(strval);
4411 if (received && zfs_prop_get_recvd(zhp,
4412 entry->pl_user_prop,
4413 buf, sizeof (buf), literal) == 0)
4414 if (strlen(buf) > entry->pl_recvd_width)
4415 entry->pl_recvd_width = strlen(buf);
4423 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4424 char *resource, void *export, void *sharetab,
4425 int sharemax, zfs_share_op_t operation)
4427 zfs_cmd_t zc = { 0 };
4430 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4431 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4433 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4434 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4435 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4436 zc.zc_share.z_sharetype = operation;
4437 zc.zc_share.z_sharemax = sharemax;
4438 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4443 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4448 * Keep a reference to the props-table against which we prune the
4451 zhp->zfs_props_table = props;
4453 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4456 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4457 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4460 * User properties will result in ZPROP_INVAL, and since we
4461 * only know how to prune standard ZFS properties, we always
4462 * leave these in the list. This can also happen if we
4463 * encounter an unknown DSL property (when running older
4464 * software, for example).
4466 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4467 (void) nvlist_remove(zhp->zfs_props,
4468 nvpair_name(curr), nvpair_type(curr));
4475 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4476 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4478 zfs_cmd_t zc = { 0 };
4479 nvlist_t *nvlist = NULL;
4482 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4483 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4484 zc.zc_cookie = (uint64_t)cmd;
4486 if (cmd == ZFS_SMB_ACL_RENAME) {
4487 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4488 (void) no_memory(hdl);
4494 case ZFS_SMB_ACL_ADD:
4495 case ZFS_SMB_ACL_REMOVE:
4496 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4498 case ZFS_SMB_ACL_RENAME:
4499 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4501 (void) no_memory(hdl);
4504 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4506 (void) no_memory(hdl);
4509 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4510 nvlist_free(nvlist);
4514 case ZFS_SMB_ACL_PURGE:
4519 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4520 nvlist_free(nvlist);
4525 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4526 char *path, char *resource)
4528 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4533 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4534 char *path, char *resource)
4536 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4541 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4543 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4548 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4549 char *oldname, char *newname)
4551 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4554 #endif /* illumos */
4557 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4558 zfs_userspace_cb_t func, void *arg)
4560 zfs_cmd_t zc = { 0 };
4561 zfs_useracct_t buf[100];
4562 libzfs_handle_t *hdl = zhp->zfs_hdl;
4565 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4567 zc.zc_objset_type = type;
4568 zc.zc_nvlist_dst = (uintptr_t)buf;
4571 zfs_useracct_t *zua = buf;
4573 zc.zc_nvlist_dst_size = sizeof (buf);
4574 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4577 (void) snprintf(errbuf, sizeof (errbuf),
4578 dgettext(TEXT_DOMAIN,
4579 "cannot get used/quota for %s"), zc.zc_name);
4580 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4582 if (zc.zc_nvlist_dst_size == 0)
4585 while (zc.zc_nvlist_dst_size > 0) {
4586 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4587 zua->zu_space)) != 0)
4590 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4599 const char *snapname;
4601 boolean_t recursive;
4606 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4608 struct holdarg *ha = arg;
4609 char name[ZFS_MAX_DATASET_NAME_LEN];
4612 (void) snprintf(name, sizeof (name),
4613 "%s@%s", zhp->zfs_name, ha->snapname);
4615 if (lzc_exists(name))
4616 fnvlist_add_string(ha->nvl, name, ha->tag);
4619 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4625 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4626 boolean_t recursive, int cleanup_fd)
4631 ha.nvl = fnvlist_alloc();
4632 ha.snapname = snapname;
4634 ha.recursive = recursive;
4635 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4637 if (nvlist_empty(ha.nvl)) {
4640 fnvlist_free(ha.nvl);
4642 (void) snprintf(errbuf, sizeof (errbuf),
4643 dgettext(TEXT_DOMAIN,
4644 "cannot hold snapshot '%s@%s'"),
4645 zhp->zfs_name, snapname);
4646 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4650 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4651 fnvlist_free(ha.nvl);
4657 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4661 libzfs_handle_t *hdl = zhp->zfs_hdl;
4666 ret = lzc_hold(holds, cleanup_fd, &errors);
4669 /* There may be errors even in the success case. */
4670 fnvlist_free(errors);
4674 if (nvlist_empty(errors)) {
4675 /* no hold-specific errors */
4676 (void) snprintf(errbuf, sizeof (errbuf),
4677 dgettext(TEXT_DOMAIN, "cannot hold"));
4680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4681 "pool must be upgraded"));
4682 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4685 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4688 (void) zfs_standard_error(hdl, ret, errbuf);
4692 for (elem = nvlist_next_nvpair(errors, NULL);
4694 elem = nvlist_next_nvpair(errors, elem)) {
4695 (void) snprintf(errbuf, sizeof (errbuf),
4696 dgettext(TEXT_DOMAIN,
4697 "cannot hold snapshot '%s'"), nvpair_name(elem));
4698 switch (fnvpair_value_int32(elem)) {
4701 * Temporary tags wind up having the ds object id
4702 * prepended. So even if we passed the length check
4703 * above, it's still possible for the tag to wind
4704 * up being slightly too long.
4706 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4709 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4712 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4715 (void) zfs_standard_error(hdl,
4716 fnvpair_value_int32(elem), errbuf);
4720 fnvlist_free(errors);
4725 zfs_release_one(zfs_handle_t *zhp, void *arg)
4727 struct holdarg *ha = arg;
4728 char name[ZFS_MAX_DATASET_NAME_LEN];
4730 nvlist_t *existing_holds;
4732 (void) snprintf(name, sizeof (name),
4733 "%s@%s", zhp->zfs_name, ha->snapname);
4735 if (lzc_get_holds(name, &existing_holds) != 0) {
4737 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4740 nvlist_t *torelease = fnvlist_alloc();
4741 fnvlist_add_boolean(torelease, ha->tag);
4742 fnvlist_add_nvlist(ha->nvl, name, torelease);
4743 fnvlist_free(torelease);
4747 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4753 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4754 boolean_t recursive)
4758 nvlist_t *errors = NULL;
4760 libzfs_handle_t *hdl = zhp->zfs_hdl;
4763 ha.nvl = fnvlist_alloc();
4764 ha.snapname = snapname;
4766 ha.recursive = recursive;
4768 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4770 if (nvlist_empty(ha.nvl)) {
4771 fnvlist_free(ha.nvl);
4773 (void) snprintf(errbuf, sizeof (errbuf),
4774 dgettext(TEXT_DOMAIN,
4775 "cannot release hold from snapshot '%s@%s'"),
4776 zhp->zfs_name, snapname);
4778 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4780 (void) zfs_standard_error(hdl, ret, errbuf);
4785 ret = lzc_release(ha.nvl, &errors);
4786 fnvlist_free(ha.nvl);
4789 /* There may be errors even in the success case. */
4790 fnvlist_free(errors);
4794 if (nvlist_empty(errors)) {
4795 /* no hold-specific errors */
4796 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4801 "pool must be upgraded"));
4802 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4805 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4809 for (elem = nvlist_next_nvpair(errors, NULL);
4811 elem = nvlist_next_nvpair(errors, elem)) {
4812 (void) snprintf(errbuf, sizeof (errbuf),
4813 dgettext(TEXT_DOMAIN,
4814 "cannot release hold from snapshot '%s'"),
4816 switch (fnvpair_value_int32(elem)) {
4818 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4821 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4824 (void) zfs_standard_error_fmt(hdl,
4825 fnvpair_value_int32(elem), errbuf);
4829 fnvlist_free(errors);
4834 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4836 zfs_cmd_t zc = { 0 };
4837 libzfs_handle_t *hdl = zhp->zfs_hdl;
4843 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4844 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4848 nvbuf = malloc(nvsz);
4849 if (nvbuf == NULL) {
4850 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4854 zc.zc_nvlist_dst_size = nvsz;
4855 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4857 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4859 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4860 (void) snprintf(errbuf, sizeof (errbuf),
4861 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4866 nvsz = zc.zc_nvlist_dst_size;
4870 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4871 "pool must be upgraded"));
4872 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4875 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4878 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4881 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4886 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4888 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4889 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4891 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4901 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4903 zfs_cmd_t zc = { 0 };
4904 libzfs_handle_t *hdl = zhp->zfs_hdl;
4910 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4911 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4913 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4916 nvbuf = malloc(nvsz);
4918 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4921 zc.zc_nvlist_src_size = nvsz;
4922 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4923 zc.zc_perm_action = un;
4925 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4927 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4928 (void) snprintf(errbuf, sizeof (errbuf),
4929 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4933 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4934 "pool must be upgraded"));
4935 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4938 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4941 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4944 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4955 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4960 err = lzc_get_holds(zhp->zfs_name, nvl);
4963 libzfs_handle_t *hdl = zhp->zfs_hdl;
4965 (void) snprintf(errbuf, sizeof (errbuf),
4966 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4970 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4971 "pool must be upgraded"));
4972 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4975 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4978 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4981 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4990 * Convert the zvol's volume size to an appropriate reservation.
4991 * Note: If this routine is updated, it is necessary to update the ZFS test
4992 * suite's shell version in reservation.kshlib.
4995 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4998 uint64_t nblocks, volblocksize;
5002 if (nvlist_lookup_string(props,
5003 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5004 ncopies = atoi(strval);
5007 if (nvlist_lookup_uint64(props,
5008 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5009 &volblocksize) != 0)
5010 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5011 nblocks = volsize/volblocksize;
5012 /* start with metadnode L0-L6 */
5014 /* calculate number of indirects */
5015 while (nblocks > 1) {
5016 nblocks += DNODES_PER_LEVEL - 1;
5017 nblocks /= DNODES_PER_LEVEL;
5020 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5023 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5024 * compressed, but in practice they compress down to about
5027 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5033 * Attach/detach the given filesystem to/from the given jail.
5036 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
5038 libzfs_handle_t *hdl = zhp->zfs_hdl;
5039 zfs_cmd_t zc = { 0 };
5045 (void) snprintf(errbuf, sizeof (errbuf),
5046 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
5048 (void) snprintf(errbuf, sizeof (errbuf),
5049 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
5052 switch (zhp->zfs_type) {
5053 case ZFS_TYPE_VOLUME:
5054 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5055 "volumes can not be jailed"));
5056 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5057 case ZFS_TYPE_SNAPSHOT:
5058 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5059 "snapshots can not be jailed"));
5060 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
5062 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5064 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5065 zc.zc_objset_type = DMU_OST_ZFS;
5066 zc.zc_jailid = jailid;
5068 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
5069 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
5070 zfs_standard_error(hdl, errno, errbuf);