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 <pawel@dawidek.net>.
28 * All rights reserved.
29 * Copyright (c) 2012 Martin Matuska <mm@FreeBSD.org>. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
32 * Copyright (c) 2014 Integros [integros.com]
46 #include <sys/mntent.h>
47 #include <sys/mount.h>
54 #include <sys/dnode.h>
60 #include "zfs_namecheck.h"
62 #include "libzfs_impl.h"
63 #include "zfs_deleg.h"
65 static int userquota_propname_decode(const char *propname, boolean_t zoned,
66 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
69 * Given a single type (not a mask of types), return the type in a human
73 zfs_type_to_name(zfs_type_t type)
76 case ZFS_TYPE_FILESYSTEM:
77 return (dgettext(TEXT_DOMAIN, "filesystem"));
78 case ZFS_TYPE_SNAPSHOT:
79 return (dgettext(TEXT_DOMAIN, "snapshot"));
81 return (dgettext(TEXT_DOMAIN, "volume"));
88 * Given a path and mask of ZFS types, return a string describing this dataset.
89 * This is used when we fail to open a dataset and we cannot get an exact type.
90 * We guess what the type would have been based on the path and the mask of
94 path_to_str(const char *path, int types)
97 * When given a single type, always report the exact type.
99 if (types == ZFS_TYPE_SNAPSHOT)
100 return (dgettext(TEXT_DOMAIN, "snapshot"));
101 if (types == ZFS_TYPE_FILESYSTEM)
102 return (dgettext(TEXT_DOMAIN, "filesystem"));
103 if (types == ZFS_TYPE_VOLUME)
104 return (dgettext(TEXT_DOMAIN, "volume"));
107 * The user is requesting more than one type of dataset. If this is the
108 * case, consult the path itself. If we're looking for a snapshot, and
109 * a '@' is found, then report it as "snapshot". Otherwise, remove the
110 * snapshot attribute and try again.
112 if (types & ZFS_TYPE_SNAPSHOT) {
113 if (strchr(path, '@') != NULL)
114 return (dgettext(TEXT_DOMAIN, "snapshot"));
115 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
119 * The user has requested either filesystems or volumes.
120 * We have no way of knowing a priori what type this would be, so always
121 * report it as "filesystem" or "volume", our two primitive types.
123 if (types & ZFS_TYPE_FILESYSTEM)
124 return (dgettext(TEXT_DOMAIN, "filesystem"));
126 assert(types & ZFS_TYPE_VOLUME);
127 return (dgettext(TEXT_DOMAIN, "volume"));
131 * Validate a ZFS path. This is used even before trying to open the dataset, to
132 * provide a more meaningful error message. We call zfs_error_aux() to
133 * explain exactly why the name was not valid.
136 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
142 (void) zfs_prop_get_table();
143 if (dataset_namecheck(path, &why, &what) != 0) {
146 case NAME_ERR_TOOLONG:
147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148 "name is too long"));
151 case NAME_ERR_LEADING_SLASH:
152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
153 "leading slash in name"));
156 case NAME_ERR_EMPTY_COMPONENT:
157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
158 "empty component in name"));
161 case NAME_ERR_TRAILING_SLASH:
162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
163 "trailing slash in name"));
166 case NAME_ERR_INVALCHAR:
168 dgettext(TEXT_DOMAIN, "invalid character "
169 "'%c' in name"), what);
172 case NAME_ERR_MULTIPLE_AT:
173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174 "multiple '@' delimiters in name"));
177 case NAME_ERR_NOLETTER:
178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
179 "pool doesn't begin with a letter"));
182 case NAME_ERR_RESERVED:
183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
184 "name is reserved"));
187 case NAME_ERR_DISKLIKE:
188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
189 "reserved disk name"));
197 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200 "snapshot delimiter '@' in filesystem name"));
204 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
207 "missing '@' delimiter in snapshot name"));
211 if (modifying && strchr(path, '%') != NULL) {
213 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
214 "invalid character %c in name"), '%');
222 zfs_name_valid(const char *name, zfs_type_t type)
224 if (type == ZFS_TYPE_POOL)
225 return (zpool_name_valid(NULL, B_FALSE, name));
226 return (zfs_validate_name(NULL, name, type, B_FALSE));
230 * This function takes the raw DSL properties, and filters out the user-defined
231 * properties into a separate nvlist.
234 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
236 libzfs_handle_t *hdl = zhp->zfs_hdl;
241 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
242 (void) no_memory(hdl);
247 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
248 if (!zfs_prop_user(nvpair_name(elem)))
251 verify(nvpair_value_nvlist(elem, &propval) == 0);
252 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
254 (void) no_memory(hdl);
262 static zpool_handle_t *
263 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
265 libzfs_handle_t *hdl = zhp->zfs_hdl;
268 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
269 if (hdl->libzfs_pool_handles != NULL)
270 zph->zpool_next = hdl->libzfs_pool_handles;
271 hdl->libzfs_pool_handles = zph;
276 static zpool_handle_t *
277 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
279 libzfs_handle_t *hdl = zhp->zfs_hdl;
280 zpool_handle_t *zph = hdl->libzfs_pool_handles;
282 while ((zph != NULL) &&
283 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
284 zph = zph->zpool_next;
289 * Returns a handle to the pool that contains the provided dataset.
290 * If a handle to that pool already exists then that handle is returned.
291 * Otherwise, a new handle is created and added to the list of handles.
293 static zpool_handle_t *
294 zpool_handle(zfs_handle_t *zhp)
300 len = strcspn(zhp->zfs_name, "/@#") + 1;
301 pool_name = zfs_alloc(zhp->zfs_hdl, len);
302 (void) strlcpy(pool_name, zhp->zfs_name, len);
304 zph = zpool_find_handle(zhp, pool_name, len);
306 zph = zpool_add_handle(zhp, pool_name);
313 zpool_free_handles(libzfs_handle_t *hdl)
315 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
317 while (zph != NULL) {
318 next = zph->zpool_next;
322 hdl->libzfs_pool_handles = NULL;
326 * Utility function to gather stats (objset and zpl) for the given object.
329 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
331 libzfs_handle_t *hdl = zhp->zfs_hdl;
333 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
335 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
336 if (errno == ENOMEM) {
337 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
348 * Utility function to get the received properties of the given object.
351 get_recvd_props_ioctl(zfs_handle_t *zhp)
353 libzfs_handle_t *hdl = zhp->zfs_hdl;
354 nvlist_t *recvdprops;
355 zfs_cmd_t zc = { 0 };
358 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
361 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
363 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
364 if (errno == ENOMEM) {
365 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
369 zcmd_free_nvlists(&zc);
374 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
375 zcmd_free_nvlists(&zc);
379 nvlist_free(zhp->zfs_recvd_props);
380 zhp->zfs_recvd_props = recvdprops;
386 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
388 nvlist_t *allprops, *userprops;
390 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
392 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
397 * XXX Why do we store the user props separately, in addition to
398 * storing them in zfs_props?
400 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
401 nvlist_free(allprops);
405 nvlist_free(zhp->zfs_props);
406 nvlist_free(zhp->zfs_user_props);
408 zhp->zfs_props = allprops;
409 zhp->zfs_user_props = userprops;
415 get_stats(zfs_handle_t *zhp)
418 zfs_cmd_t zc = { 0 };
420 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
422 if (get_stats_ioctl(zhp, &zc) != 0)
424 else if (put_stats_zhdl(zhp, &zc) != 0)
426 zcmd_free_nvlists(&zc);
431 * Refresh the properties currently stored in the handle.
434 zfs_refresh_properties(zfs_handle_t *zhp)
436 (void) get_stats(zhp);
440 * Makes a handle from the given dataset name. Used by zfs_open() and
441 * zfs_iter_* to create child handles on the fly.
444 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
446 if (put_stats_zhdl(zhp, zc) != 0)
450 * We've managed to open the dataset and gather statistics. Determine
451 * the high-level type.
453 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
454 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
455 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
456 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
460 if (zhp->zfs_dmustats.dds_is_snapshot)
461 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
462 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
463 zhp->zfs_type = ZFS_TYPE_VOLUME;
464 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
465 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
467 abort(); /* we should never see any other types */
469 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
476 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
478 zfs_cmd_t zc = { 0 };
480 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
486 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
487 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
491 if (get_stats_ioctl(zhp, &zc) == -1) {
492 zcmd_free_nvlists(&zc);
496 if (make_dataset_handle_common(zhp, &zc) == -1) {
500 zcmd_free_nvlists(&zc);
505 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
507 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
513 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
514 if (make_dataset_handle_common(zhp, zc) == -1) {
522 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
524 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
529 zhp->zfs_hdl = pzhp->zfs_hdl;
530 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
531 zhp->zfs_head_type = pzhp->zfs_type;
532 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
533 zhp->zpool_hdl = zpool_handle(zhp);
538 zfs_handle_dup(zfs_handle_t *zhp_orig)
540 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
545 zhp->zfs_hdl = zhp_orig->zfs_hdl;
546 zhp->zpool_hdl = zhp_orig->zpool_hdl;
547 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
548 sizeof (zhp->zfs_name));
549 zhp->zfs_type = zhp_orig->zfs_type;
550 zhp->zfs_head_type = zhp_orig->zfs_head_type;
551 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
552 if (zhp_orig->zfs_props != NULL) {
553 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
554 (void) no_memory(zhp->zfs_hdl);
559 if (zhp_orig->zfs_user_props != NULL) {
560 if (nvlist_dup(zhp_orig->zfs_user_props,
561 &zhp->zfs_user_props, 0) != 0) {
562 (void) no_memory(zhp->zfs_hdl);
567 if (zhp_orig->zfs_recvd_props != NULL) {
568 if (nvlist_dup(zhp_orig->zfs_recvd_props,
569 &zhp->zfs_recvd_props, 0)) {
570 (void) no_memory(zhp->zfs_hdl);
575 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
576 if (zhp_orig->zfs_mntopts != NULL) {
577 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
578 zhp_orig->zfs_mntopts);
580 zhp->zfs_props_table = zhp_orig->zfs_props_table;
585 zfs_bookmark_exists(const char *path)
589 char fsname[ZFS_MAXNAMELEN];
596 (void) strlcpy(fsname, path, sizeof (fsname));
597 pound = strchr(fsname, '#');
602 bmark_name = pound + 1;
603 props = fnvlist_alloc();
604 err = lzc_get_bookmarks(fsname, props, &bmarks);
611 rv = nvlist_exists(bmarks, bmark_name);
617 make_bookmark_handle(zfs_handle_t *parent, const char *path,
618 nvlist_t *bmark_props)
620 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
625 /* Fill in the name. */
626 zhp->zfs_hdl = parent->zfs_hdl;
627 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
629 /* Set the property lists. */
630 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
636 zhp->zfs_head_type = parent->zfs_head_type;
637 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
639 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
640 nvlist_free(zhp->zfs_props);
649 * Opens the given snapshot, filesystem, or volume. The 'types'
650 * argument is a mask of acceptable types. The function will print an
651 * appropriate error message and return NULL if it can't be opened.
654 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
659 (void) snprintf(errbuf, sizeof (errbuf),
660 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
663 * Validate the name before we even try to open it.
665 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
666 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
667 "invalid dataset name"));
668 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
673 * Try to get stats for the dataset, which will tell us if it exists.
676 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
677 (void) zfs_standard_error(hdl, errno, errbuf);
682 char *at = strchr(path, '@');
687 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
688 (void) zfs_standard_error(hdl, errno, errbuf);
693 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
694 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
697 if (!(types & zhp->zfs_type)) {
698 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
707 * Release a ZFS handle. Nothing to do but free the associated memory.
710 zfs_close(zfs_handle_t *zhp)
712 if (zhp->zfs_mntopts)
713 free(zhp->zfs_mntopts);
714 nvlist_free(zhp->zfs_props);
715 nvlist_free(zhp->zfs_user_props);
716 nvlist_free(zhp->zfs_recvd_props);
720 typedef struct mnttab_node {
721 struct mnttab mtn_mt;
726 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
728 const mnttab_node_t *mtn1 = arg1;
729 const mnttab_node_t *mtn2 = arg2;
732 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
736 return (rv > 0 ? 1 : -1);
740 libzfs_mnttab_init(libzfs_handle_t *hdl)
742 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
743 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
744 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
748 libzfs_mnttab_update(libzfs_handle_t *hdl)
752 rewind(hdl->libzfs_mnttab);
753 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
756 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
758 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
759 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
760 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
761 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
762 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
763 avl_add(&hdl->libzfs_mnttab_cache, mtn);
768 libzfs_mnttab_fini(libzfs_handle_t *hdl)
773 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
774 free(mtn->mtn_mt.mnt_special);
775 free(mtn->mtn_mt.mnt_mountp);
776 free(mtn->mtn_mt.mnt_fstype);
777 free(mtn->mtn_mt.mnt_mntopts);
780 avl_destroy(&hdl->libzfs_mnttab_cache);
784 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
786 hdl->libzfs_mnttab_enable = enable;
790 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
791 struct mnttab *entry)
796 if (!hdl->libzfs_mnttab_enable) {
797 struct mnttab srch = { 0 };
799 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
800 libzfs_mnttab_fini(hdl);
801 rewind(hdl->libzfs_mnttab);
802 srch.mnt_special = (char *)fsname;
803 srch.mnt_fstype = MNTTYPE_ZFS;
804 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
810 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
811 libzfs_mnttab_update(hdl);
813 find.mtn_mt.mnt_special = (char *)fsname;
814 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
816 *entry = mtn->mtn_mt;
823 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
824 const char *mountp, const char *mntopts)
828 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
830 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
831 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
832 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
833 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
834 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
835 avl_add(&hdl->libzfs_mnttab_cache, mtn);
839 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
844 find.mtn_mt.mnt_special = (char *)fsname;
845 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
846 avl_remove(&hdl->libzfs_mnttab_cache, ret);
847 free(ret->mtn_mt.mnt_special);
848 free(ret->mtn_mt.mnt_mountp);
849 free(ret->mtn_mt.mnt_fstype);
850 free(ret->mtn_mt.mnt_mntopts);
856 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
858 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
860 if (zpool_handle == NULL)
863 *spa_version = zpool_get_prop_int(zpool_handle,
864 ZPOOL_PROP_VERSION, NULL);
869 * The choice of reservation property depends on the SPA version.
872 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
876 if (zfs_spa_version(zhp, &spa_version) < 0)
879 if (spa_version >= SPA_VERSION_REFRESERVATION)
880 *resv_prop = ZFS_PROP_REFRESERVATION;
882 *resv_prop = ZFS_PROP_RESERVATION;
888 * Given an nvlist of properties to set, validates that they are correct, and
889 * parses any numeric properties (index, boolean, etc) if they are specified as
893 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
894 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
902 int chosen_normal = -1;
905 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
906 (void) no_memory(hdl);
911 * Make sure this property is valid and applies to this type.
915 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
916 const char *propname = nvpair_name(elem);
918 prop = zfs_name_to_prop(propname);
919 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
921 * This is a user property: make sure it's a
922 * string, and that it's less than ZAP_MAXNAMELEN.
924 if (nvpair_type(elem) != DATA_TYPE_STRING) {
925 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
926 "'%s' must be a string"), propname);
927 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
931 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 "property name '%s' is too long"),
935 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
939 (void) nvpair_value_string(elem, &strval);
940 if (nvlist_add_string(ret, propname, strval) != 0) {
941 (void) no_memory(hdl);
948 * Currently, only user properties can be modified on
951 if (type == ZFS_TYPE_SNAPSHOT) {
952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953 "this property can not be modified for snapshots"));
954 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
958 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
959 zfs_userquota_prop_t uqtype;
960 char newpropname[128];
965 if (userquota_propname_decode(propname, zoned,
966 &uqtype, domain, sizeof (domain), &rid) != 0) {
968 dgettext(TEXT_DOMAIN,
969 "'%s' has an invalid user/group name"),
971 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
975 if (uqtype != ZFS_PROP_USERQUOTA &&
976 uqtype != ZFS_PROP_GROUPQUOTA) {
978 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
980 (void) zfs_error(hdl, EZFS_PROPREADONLY,
985 if (nvpair_type(elem) == DATA_TYPE_STRING) {
986 (void) nvpair_value_string(elem, &strval);
987 if (strcmp(strval, "none") == 0) {
989 } else if (zfs_nicestrtonum(hdl,
990 strval, &intval) != 0) {
991 (void) zfs_error(hdl,
992 EZFS_BADPROP, errbuf);
995 } else if (nvpair_type(elem) ==
997 (void) nvpair_value_uint64(elem, &intval);
999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1000 "use 'none' to disable "
1001 "userquota/groupquota"));
1005 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1006 "'%s' must be a number"), propname);
1007 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1012 * Encode the prop name as
1013 * userquota@<hex-rid>-domain, to make it easy
1014 * for the kernel to decode.
1016 (void) snprintf(newpropname, sizeof (newpropname),
1017 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1018 (longlong_t)rid, domain);
1022 if (nvlist_add_uint64_array(ret, newpropname,
1024 (void) no_memory(hdl);
1028 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1029 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1030 "'%s' is readonly"),
1032 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1036 if (prop == ZPROP_INVAL) {
1037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1038 "invalid property '%s'"), propname);
1039 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1043 if (!zfs_prop_valid_for_type(prop, type)) {
1045 dgettext(TEXT_DOMAIN, "'%s' does not "
1046 "apply to datasets of this type"), propname);
1047 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1051 if (zfs_prop_readonly(prop) &&
1052 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1054 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1056 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1060 if (zprop_parse_value(hdl, elem, prop, type, ret,
1061 &strval, &intval, errbuf) != 0)
1065 * Perform some additional checks for specific properties.
1068 case ZFS_PROP_VERSION:
1074 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1075 if (intval < version) {
1076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1077 "Can not downgrade; already at version %u"),
1079 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1085 case ZFS_PROP_VOLBLOCKSIZE:
1086 case ZFS_PROP_RECORDSIZE:
1088 int maxbs = SPA_MAXBLOCKSIZE;
1089 if (zpool_hdl != NULL) {
1090 maxbs = zpool_get_prop_int(zpool_hdl,
1091 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1094 * Volumes are limited to a volblocksize of 128KB,
1095 * because they typically service workloads with
1096 * small random writes, which incur a large performance
1097 * penalty with large blocks.
1099 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1100 maxbs = SPA_OLD_MAXBLOCKSIZE;
1102 * The value must be a power of two between
1103 * SPA_MINBLOCKSIZE and maxbs.
1105 if (intval < SPA_MINBLOCKSIZE ||
1106 intval > maxbs || !ISP2(intval)) {
1107 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1108 "'%s' must be power of 2 from 512B "
1109 "to %uKB"), propname, maxbs >> 10);
1110 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1115 case ZFS_PROP_MLSLABEL:
1119 * Verify the mlslabel string and convert to
1120 * internal hex label string.
1124 char *hex = NULL; /* internal label string */
1126 /* Default value is already OK. */
1127 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1130 /* Verify the label can be converted to binary form */
1131 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1132 (str_to_label(strval, &new_sl, MAC_LABEL,
1133 L_NO_CORRECTION, NULL) == -1)) {
1137 /* Now translate to hex internal label string */
1138 if (label_to_str(new_sl, &hex, M_INTERNAL,
1144 m_label_free(new_sl);
1146 /* If string is already in internal form, we're done. */
1147 if (strcmp(strval, hex) == 0) {
1152 /* Replace the label string with the internal form. */
1153 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1155 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1163 "invalid mlslabel '%s'"), strval);
1164 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1165 m_label_free(new_sl); /* OK if null */
1166 #else /* !illumos */
1167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1168 "mlslabel is not supported on FreeBSD"));
1169 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1170 #endif /* illumos */
1175 case ZFS_PROP_MOUNTPOINT:
1177 namecheck_err_t why;
1179 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1180 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1183 if (mountpoint_namecheck(strval, &why)) {
1185 case NAME_ERR_LEADING_SLASH:
1187 dgettext(TEXT_DOMAIN,
1188 "'%s' must be an absolute path, "
1189 "'none', or 'legacy'"), propname);
1191 case NAME_ERR_TOOLONG:
1193 dgettext(TEXT_DOMAIN,
1194 "component of '%s' is too long"),
1198 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1205 case ZFS_PROP_SHARESMB:
1206 case ZFS_PROP_SHARENFS:
1208 * For the mountpoint and sharenfs or sharesmb
1209 * properties, check if it can be set in a
1210 * global/non-global zone based on
1211 * the zoned property value:
1213 * global zone non-global zone
1214 * --------------------------------------------------
1215 * zoned=on mountpoint (no) mountpoint (yes)
1216 * sharenfs (no) sharenfs (no)
1217 * sharesmb (no) sharesmb (no)
1219 * zoned=off mountpoint (yes) N/A
1224 if (getzoneid() == GLOBAL_ZONEID) {
1225 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1226 "'%s' cannot be set on "
1227 "dataset in a non-global zone"),
1229 (void) zfs_error(hdl, EZFS_ZONED,
1232 } else if (prop == ZFS_PROP_SHARENFS ||
1233 prop == ZFS_PROP_SHARESMB) {
1234 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1235 "'%s' cannot be set in "
1236 "a non-global zone"), propname);
1237 (void) zfs_error(hdl, EZFS_ZONED,
1241 } else if (getzoneid() != GLOBAL_ZONEID) {
1243 * If zoned property is 'off', this must be in
1244 * a global zone. If not, something is wrong.
1246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1247 "'%s' cannot be set while dataset "
1248 "'zoned' property is set"), propname);
1249 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1254 * At this point, it is legitimate to set the
1255 * property. Now we want to make sure that the
1256 * property value is valid if it is sharenfs.
1258 if ((prop == ZFS_PROP_SHARENFS ||
1259 prop == ZFS_PROP_SHARESMB) &&
1260 strcmp(strval, "on") != 0 &&
1261 strcmp(strval, "off") != 0) {
1262 zfs_share_proto_t proto;
1264 if (prop == ZFS_PROP_SHARESMB)
1270 * Must be an valid sharing protocol
1271 * option string so init the libshare
1272 * in order to enable the parser and
1273 * then parse the options. We use the
1274 * control API since we don't care about
1275 * the current configuration and don't
1276 * want the overhead of loading it
1277 * until we actually do something.
1280 if (zfs_init_libshare(hdl,
1281 SA_INIT_CONTROL_API) != SA_OK) {
1283 * An error occurred so we can't do
1286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1287 "'%s' cannot be set: problem "
1288 "in share initialization"),
1290 (void) zfs_error(hdl, EZFS_BADPROP,
1295 if (zfs_parse_options(strval, proto) != SA_OK) {
1297 * There was an error in parsing so
1298 * deal with it by issuing an error
1299 * message and leaving after
1300 * uninitializing the the libshare
1303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1304 "'%s' cannot be set to invalid "
1305 "options"), propname);
1306 (void) zfs_error(hdl, EZFS_BADPROP,
1308 zfs_uninit_libshare(hdl);
1311 zfs_uninit_libshare(hdl);
1315 case ZFS_PROP_UTF8ONLY:
1316 chosen_utf = (int)intval;
1318 case ZFS_PROP_NORMALIZE:
1319 chosen_normal = (int)intval;
1324 * For changes to existing volumes, we have some additional
1325 * checks to enforce.
1327 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1328 uint64_t volsize = zfs_prop_get_int(zhp,
1330 uint64_t blocksize = zfs_prop_get_int(zhp,
1331 ZFS_PROP_VOLBLOCKSIZE);
1335 case ZFS_PROP_RESERVATION:
1336 case ZFS_PROP_REFRESERVATION:
1337 if (intval > volsize) {
1338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1339 "'%s' is greater than current "
1340 "volume size"), propname);
1341 (void) zfs_error(hdl, EZFS_BADPROP,
1347 case ZFS_PROP_VOLSIZE:
1348 if (intval % blocksize != 0) {
1349 zfs_nicenum(blocksize, buf,
1351 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352 "'%s' must be a multiple of "
1353 "volume block size (%s)"),
1355 (void) zfs_error(hdl, EZFS_BADPROP,
1361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1362 "'%s' cannot be zero"),
1364 (void) zfs_error(hdl, EZFS_BADPROP,
1374 * If normalization was chosen, but no UTF8 choice was made,
1375 * enforce rejection of non-UTF8 names.
1377 * If normalization was chosen, but rejecting non-UTF8 names
1378 * was explicitly not chosen, it is an error.
1380 if (chosen_normal > 0 && chosen_utf < 0) {
1381 if (nvlist_add_uint64(ret,
1382 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1383 (void) no_memory(hdl);
1386 } else if (chosen_normal > 0 && chosen_utf == 0) {
1387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1388 "'%s' must be set 'on' if normalization chosen"),
1389 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1390 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1401 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1403 uint64_t old_volsize;
1404 uint64_t new_volsize;
1405 uint64_t old_reservation;
1406 uint64_t new_reservation;
1407 zfs_prop_t resv_prop;
1411 * If this is an existing volume, and someone is setting the volsize,
1412 * make sure that it matches the reservation, or add it if necessary.
1414 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1415 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1417 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1419 props = fnvlist_alloc();
1420 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1421 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1423 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1424 old_reservation) || nvlist_exists(nvl,
1425 zfs_prop_to_name(resv_prop))) {
1426 fnvlist_free(props);
1429 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1430 &new_volsize) != 0) {
1431 fnvlist_free(props);
1434 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1435 fnvlist_free(props);
1437 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1438 new_reservation) != 0) {
1439 (void) no_memory(zhp->zfs_hdl);
1446 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1453 * For quotas and reservations, ENOSPC indicates
1454 * something different; setting a quota or reservation
1455 * doesn't use any disk space.
1458 case ZFS_PROP_QUOTA:
1459 case ZFS_PROP_REFQUOTA:
1460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1461 "size is less than current used or "
1463 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1466 case ZFS_PROP_RESERVATION:
1467 case ZFS_PROP_REFRESERVATION:
1468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1469 "size is greater than available space"));
1470 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1474 (void) zfs_standard_error(hdl, err, errbuf);
1480 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1484 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1489 "pool and or dataset must be upgraded to set this "
1490 "property or value"));
1491 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1496 if (prop == ZFS_PROP_COMPRESSION ||
1497 prop == ZFS_PROP_RECORDSIZE) {
1498 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1499 "property setting is not allowed on "
1500 "bootable datasets"));
1501 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1502 } else if (prop == ZFS_PROP_CHECKSUM ||
1503 prop == ZFS_PROP_DEDUP) {
1504 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1505 "property setting is not allowed on "
1507 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1509 (void) zfs_standard_error(hdl, err, errbuf);
1514 if (prop == ZPROP_INVAL) {
1515 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1517 (void) zfs_standard_error(hdl, err, errbuf);
1523 * This platform can't address a volume this big.
1526 if (prop == ZFS_PROP_VOLSIZE) {
1527 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1533 (void) zfs_standard_error(hdl, err, errbuf);
1538 * Given a property name and value, set the property for the given dataset.
1541 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1545 libzfs_handle_t *hdl = zhp->zfs_hdl;
1546 nvlist_t *nvl = NULL;
1548 (void) snprintf(errbuf, sizeof (errbuf),
1549 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1552 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1553 nvlist_add_string(nvl, propname, propval) != 0) {
1554 (void) no_memory(hdl);
1558 ret = zfs_prop_set_list(zhp, nvl);
1568 * Given an nvlist of property names and values, set the properties for the
1572 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1574 zfs_cmd_t zc = { 0 };
1576 prop_changelist_t **cls = NULL;
1579 libzfs_handle_t *hdl = zhp->zfs_hdl;
1584 (void) snprintf(errbuf, sizeof (errbuf),
1585 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1588 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1589 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1594 * We have to check for any extra properties which need to be added
1595 * before computing the length of the nvlist.
1597 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1599 elem = nvlist_next_nvpair(nvl, elem)) {
1600 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1601 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1606 * Check how many properties we're setting and allocate an array to
1607 * store changelist pointers for postfix().
1610 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1612 elem = nvlist_next_nvpair(nvl, elem))
1614 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1618 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1620 elem = nvlist_next_nvpair(nvl, elem)) {
1622 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1624 assert(cl_idx < nvl_len);
1626 * We don't want to unmount & remount the dataset when changing
1627 * its canmount property to 'on' or 'noauto'. We only use
1628 * the changelist logic to unmount when setting canmount=off.
1630 if (!(prop == ZFS_PROP_CANMOUNT &&
1631 fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) {
1632 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1633 if (cls[cl_idx] == NULL)
1637 if (prop == ZFS_PROP_MOUNTPOINT &&
1638 changelist_haszonedchild(cls[cl_idx])) {
1639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1640 "child dataset with inherited mountpoint is used "
1641 "in a non-global zone"));
1642 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1646 /* We don't support those properties on FreeBSD. */
1648 case ZFS_PROP_DEVICES:
1649 case ZFS_PROP_ISCSIOPTIONS:
1650 case ZFS_PROP_XATTR:
1651 case ZFS_PROP_VSCAN:
1652 case ZFS_PROP_NBMAND:
1653 case ZFS_PROP_MLSLABEL:
1654 (void) snprintf(errbuf, sizeof (errbuf),
1655 "property '%s' not supported on FreeBSD",
1657 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1661 if (cls[cl_idx] != NULL &&
1662 (ret = changelist_prefix(cls[cl_idx])) != 0)
1667 assert(cl_idx == nvl_len);
1670 * Execute the corresponding ioctl() to set this list of properties.
1672 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1674 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1675 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1678 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1681 /* Get the list of unset properties back and report them. */
1682 nvlist_t *errorprops = NULL;
1683 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1685 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1687 elem = nvlist_next_nvpair(nvl, elem)) {
1688 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1689 zfs_setprop_error(hdl, prop, errno, errbuf);
1691 nvlist_free(errorprops);
1693 if (added_resv && errno == ENOSPC) {
1694 /* clean up the volsize property we tried to set */
1695 uint64_t old_volsize = zfs_prop_get_int(zhp,
1699 zcmd_free_nvlists(&zc);
1701 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1703 if (nvlist_add_uint64(nvl,
1704 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1707 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1709 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1712 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1713 if (cls[cl_idx] != NULL) {
1714 int clp_err = changelist_postfix(cls[cl_idx]);
1721 * Refresh the statistics so the new property value
1725 (void) get_stats(zhp);
1730 zcmd_free_nvlists(&zc);
1732 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1733 if (cls[cl_idx] != NULL)
1734 changelist_free(cls[cl_idx]);
1742 * Given a property, inherit the value from the parent dataset, or if received
1743 * is TRUE, revert to the received value, if any.
1746 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1748 zfs_cmd_t zc = { 0 };
1750 prop_changelist_t *cl;
1751 libzfs_handle_t *hdl = zhp->zfs_hdl;
1755 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1756 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1758 zc.zc_cookie = received;
1759 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1761 * For user properties, the amount of work we have to do is very
1762 * small, so just do it here.
1764 if (!zfs_prop_user(propname)) {
1765 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1766 "invalid property"));
1767 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1770 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1771 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1773 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1774 return (zfs_standard_error(hdl, errno, errbuf));
1780 * Verify that this property is inheritable.
1782 if (zfs_prop_readonly(prop))
1783 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1785 if (!zfs_prop_inheritable(prop) && !received)
1786 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1789 * Check to see if the value applies to this type
1791 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1792 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1795 * Normalize the name, to get rid of shorthand abbreviations.
1797 propname = zfs_prop_to_name(prop);
1798 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1799 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1801 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1802 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1804 "dataset is used in a non-global zone"));
1805 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1809 * Determine datasets which will be affected by this change, if any.
1811 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1814 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1816 "child dataset with inherited mountpoint is used "
1817 "in a non-global zone"));
1818 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1822 if ((ret = changelist_prefix(cl)) != 0)
1825 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1826 return (zfs_standard_error(hdl, errno, errbuf));
1829 if ((ret = changelist_postfix(cl)) != 0)
1833 * Refresh the statistics so the new property is reflected.
1835 (void) get_stats(zhp);
1839 changelist_free(cl);
1844 * True DSL properties are stored in an nvlist. The following two functions
1845 * extract them appropriately.
1848 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1854 if (nvlist_lookup_nvlist(zhp->zfs_props,
1855 zfs_prop_to_name(prop), &nv) == 0) {
1856 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1857 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1859 verify(!zhp->zfs_props_table ||
1860 zhp->zfs_props_table[prop] == B_TRUE);
1861 value = zfs_prop_default_numeric(prop);
1869 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1875 if (nvlist_lookup_nvlist(zhp->zfs_props,
1876 zfs_prop_to_name(prop), &nv) == 0) {
1877 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1878 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1880 verify(!zhp->zfs_props_table ||
1881 zhp->zfs_props_table[prop] == B_TRUE);
1882 value = zfs_prop_default_string(prop);
1890 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1892 return (zhp->zfs_props == zhp->zfs_recvd_props);
1896 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1898 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1899 zhp->zfs_props = zhp->zfs_recvd_props;
1903 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1905 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1910 * Internal function for getting a numeric property. Both zfs_prop_get() and
1911 * zfs_prop_get_int() are built using this interface.
1913 * Certain properties can be overridden using 'mount -o'. In this case, scan
1914 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1915 * If they differ from the on-disk values, report the current values and mark
1916 * the source "temporary".
1919 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1920 char **source, uint64_t *val)
1922 zfs_cmd_t zc = { 0 };
1923 nvlist_t *zplprops = NULL;
1925 char *mntopt_on = NULL;
1926 char *mntopt_off = NULL;
1927 boolean_t received = zfs_is_recvd_props_mode(zhp);
1932 case ZFS_PROP_ATIME:
1933 mntopt_on = MNTOPT_ATIME;
1934 mntopt_off = MNTOPT_NOATIME;
1937 case ZFS_PROP_DEVICES:
1938 mntopt_on = MNTOPT_DEVICES;
1939 mntopt_off = MNTOPT_NODEVICES;
1943 mntopt_on = MNTOPT_EXEC;
1944 mntopt_off = MNTOPT_NOEXEC;
1947 case ZFS_PROP_READONLY:
1948 mntopt_on = MNTOPT_RO;
1949 mntopt_off = MNTOPT_RW;
1952 case ZFS_PROP_SETUID:
1953 mntopt_on = MNTOPT_SETUID;
1954 mntopt_off = MNTOPT_NOSETUID;
1957 case ZFS_PROP_XATTR:
1958 mntopt_on = MNTOPT_XATTR;
1959 mntopt_off = MNTOPT_NOXATTR;
1962 case ZFS_PROP_NBMAND:
1963 mntopt_on = MNTOPT_NBMAND;
1964 mntopt_off = MNTOPT_NONBMAND;
1969 * Because looking up the mount options is potentially expensive
1970 * (iterating over all of /etc/mnttab), we defer its calculation until
1971 * we're looking up a property which requires its presence.
1973 if (!zhp->zfs_mntcheck &&
1974 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1975 libzfs_handle_t *hdl = zhp->zfs_hdl;
1976 struct mnttab entry;
1978 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1979 zhp->zfs_mntopts = zfs_strdup(hdl,
1981 if (zhp->zfs_mntopts == NULL)
1985 zhp->zfs_mntcheck = B_TRUE;
1988 if (zhp->zfs_mntopts == NULL)
1989 mnt.mnt_mntopts = "";
1991 mnt.mnt_mntopts = zhp->zfs_mntopts;
1994 case ZFS_PROP_ATIME:
1995 case ZFS_PROP_DEVICES:
1997 case ZFS_PROP_READONLY:
1998 case ZFS_PROP_SETUID:
1999 case ZFS_PROP_XATTR:
2000 case ZFS_PROP_NBMAND:
2001 *val = getprop_uint64(zhp, prop, source);
2006 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2009 *src = ZPROP_SRC_TEMPORARY;
2010 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2013 *src = ZPROP_SRC_TEMPORARY;
2017 case ZFS_PROP_CANMOUNT:
2018 case ZFS_PROP_VOLSIZE:
2019 case ZFS_PROP_QUOTA:
2020 case ZFS_PROP_REFQUOTA:
2021 case ZFS_PROP_RESERVATION:
2022 case ZFS_PROP_REFRESERVATION:
2023 case ZFS_PROP_FILESYSTEM_LIMIT:
2024 case ZFS_PROP_SNAPSHOT_LIMIT:
2025 case ZFS_PROP_FILESYSTEM_COUNT:
2026 case ZFS_PROP_SNAPSHOT_COUNT:
2027 *val = getprop_uint64(zhp, prop, source);
2029 if (*source == NULL) {
2030 /* not default, must be local */
2031 *source = zhp->zfs_name;
2035 case ZFS_PROP_MOUNTED:
2036 *val = (zhp->zfs_mntopts != NULL);
2039 case ZFS_PROP_NUMCLONES:
2040 *val = zhp->zfs_dmustats.dds_num_clones;
2043 case ZFS_PROP_VERSION:
2044 case ZFS_PROP_NORMALIZE:
2045 case ZFS_PROP_UTF8ONLY:
2047 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2048 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2050 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2051 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2052 zcmd_free_nvlists(&zc);
2055 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2056 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2058 zcmd_free_nvlists(&zc);
2061 nvlist_free(zplprops);
2062 zcmd_free_nvlists(&zc);
2065 case ZFS_PROP_INCONSISTENT:
2066 *val = zhp->zfs_dmustats.dds_inconsistent;
2070 switch (zfs_prop_get_type(prop)) {
2071 case PROP_TYPE_NUMBER:
2072 case PROP_TYPE_INDEX:
2073 *val = getprop_uint64(zhp, prop, source);
2075 * If we tried to use a default value for a
2076 * readonly property, it means that it was not
2079 if (zfs_prop_readonly(prop) &&
2080 *source != NULL && (*source)[0] == '\0') {
2085 case PROP_TYPE_STRING:
2087 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2088 "cannot get non-numeric property"));
2089 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2090 dgettext(TEXT_DOMAIN, "internal error")));
2098 * Calculate the source type, given the raw source string.
2101 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2102 char *statbuf, size_t statlen)
2104 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2107 if (source == NULL) {
2108 *srctype = ZPROP_SRC_NONE;
2109 } else if (source[0] == '\0') {
2110 *srctype = ZPROP_SRC_DEFAULT;
2111 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2112 *srctype = ZPROP_SRC_RECEIVED;
2114 if (strcmp(source, zhp->zfs_name) == 0) {
2115 *srctype = ZPROP_SRC_LOCAL;
2117 (void) strlcpy(statbuf, source, statlen);
2118 *srctype = ZPROP_SRC_INHERITED;
2125 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2126 size_t proplen, boolean_t literal)
2131 if (zhp->zfs_recvd_props == NULL)
2132 if (get_recvd_props_ioctl(zhp) != 0)
2135 prop = zfs_name_to_prop(propname);
2137 if (prop != ZPROP_INVAL) {
2139 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2141 zfs_set_recvd_props_mode(zhp, &cookie);
2142 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2143 NULL, NULL, 0, literal);
2144 zfs_unset_recvd_props_mode(zhp, &cookie);
2148 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2149 propname, &propval) != 0)
2151 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2153 (void) strlcpy(propbuf, recvdval, proplen);
2156 return (err == 0 ? 0 : -1);
2160 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2165 value = zfs_get_clones_nvl(zhp);
2170 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2171 pair = nvlist_next_nvpair(value, pair)) {
2172 if (propbuf[0] != '\0')
2173 (void) strlcat(propbuf, ",", proplen);
2174 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2180 struct get_clones_arg {
2184 char buf[ZFS_MAXNAMELEN];
2188 get_clones_cb(zfs_handle_t *zhp, void *arg)
2190 struct get_clones_arg *gca = arg;
2192 if (gca->numclones == 0) {
2197 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2198 NULL, NULL, 0, B_TRUE) != 0)
2200 if (strcmp(gca->buf, gca->origin) == 0) {
2201 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2206 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2212 zfs_get_clones_nvl(zfs_handle_t *zhp)
2214 nvlist_t *nv, *value;
2216 if (nvlist_lookup_nvlist(zhp->zfs_props,
2217 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2218 struct get_clones_arg gca;
2221 * if this is a snapshot, then the kernel wasn't able
2222 * to get the clones. Do it by slowly iterating.
2224 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2226 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2228 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2233 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2235 gca.origin = zhp->zfs_name;
2237 if (gca.numclones != 0) {
2239 char pool[ZFS_MAXNAMELEN];
2242 /* get the pool name */
2243 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2244 (void) strsep(&cp, "/@");
2245 root = zfs_open(zhp->zfs_hdl, pool,
2246 ZFS_TYPE_FILESYSTEM);
2248 (void) get_clones_cb(root, &gca);
2251 if (gca.numclones != 0 ||
2252 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2253 nvlist_add_nvlist(zhp->zfs_props,
2254 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2261 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2262 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2265 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2271 * Retrieve a property from the given object. If 'literal' is specified, then
2272 * numbers are left as exact values. Otherwise, numbers are converted to a
2273 * human-readable form.
2275 * Returns 0 on success, or -1 on error.
2278 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2279 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2281 char *source = NULL;
2285 boolean_t received = zfs_is_recvd_props_mode(zhp);
2288 * Check to see if this property applies to our object
2290 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2293 if (received && zfs_prop_readonly(prop))
2297 *src = ZPROP_SRC_NONE;
2300 case ZFS_PROP_CREATION:
2302 * 'creation' is a time_t stored in the statistics. We convert
2303 * this into a string unless 'literal' is specified.
2306 val = getprop_uint64(zhp, prop, &source);
2307 time_t time = (time_t)val;
2311 localtime_r(&time, &t) == NULL ||
2312 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2314 (void) snprintf(propbuf, proplen, "%llu", val);
2318 case ZFS_PROP_MOUNTPOINT:
2320 * Getting the precise mountpoint can be tricky.
2322 * - for 'none' or 'legacy', return those values.
2323 * - for inherited mountpoints, we want to take everything
2324 * after our ancestor and append it to the inherited value.
2326 * If the pool has an alternate root, we want to prepend that
2327 * root to any values we return.
2330 str = getprop_string(zhp, prop, &source);
2332 if (str[0] == '/') {
2333 char buf[MAXPATHLEN];
2335 const char *relpath;
2338 * If we inherit the mountpoint, even from a dataset
2339 * with a received value, the source will be the path of
2340 * the dataset we inherit from. If source is
2341 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2344 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2347 relpath = zhp->zfs_name + strlen(source);
2348 if (relpath[0] == '/')
2352 if ((zpool_get_prop(zhp->zpool_hdl,
2353 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2354 B_FALSE)) || (strcmp(root, "-") == 0))
2357 * Special case an alternate root of '/'. This will
2358 * avoid having multiple leading slashes in the
2361 if (strcmp(root, "/") == 0)
2365 * If the mountpoint is '/' then skip over this
2366 * if we are obtaining either an alternate root or
2367 * an inherited mountpoint.
2369 if (str[1] == '\0' && (root[0] != '\0' ||
2370 relpath[0] != '\0'))
2373 if (relpath[0] == '\0')
2374 (void) snprintf(propbuf, proplen, "%s%s",
2377 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2378 root, str, relpath[0] == '@' ? "" : "/",
2381 /* 'legacy' or 'none' */
2382 (void) strlcpy(propbuf, str, proplen);
2387 case ZFS_PROP_ORIGIN:
2388 str = getprop_string(zhp, prop, &source);
2391 (void) strlcpy(propbuf, str, proplen);
2394 case ZFS_PROP_CLONES:
2395 if (get_clones_string(zhp, propbuf, proplen) != 0)
2399 case ZFS_PROP_QUOTA:
2400 case ZFS_PROP_REFQUOTA:
2401 case ZFS_PROP_RESERVATION:
2402 case ZFS_PROP_REFRESERVATION:
2404 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2408 * If quota or reservation is 0, we translate this into 'none'
2409 * (unless literal is set), and indicate that it's the default
2410 * value. Otherwise, we print the number nicely and indicate
2411 * that its set locally.
2415 (void) strlcpy(propbuf, "0", proplen);
2417 (void) strlcpy(propbuf, "none", proplen);
2420 (void) snprintf(propbuf, proplen, "%llu",
2423 zfs_nicenum(val, propbuf, proplen);
2427 case ZFS_PROP_FILESYSTEM_LIMIT:
2428 case ZFS_PROP_SNAPSHOT_LIMIT:
2429 case ZFS_PROP_FILESYSTEM_COUNT:
2430 case ZFS_PROP_SNAPSHOT_COUNT:
2432 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2436 * If limit is UINT64_MAX, we translate this into 'none' (unless
2437 * literal is set), and indicate that it's the default value.
2438 * Otherwise, we print the number nicely and indicate that it's
2442 (void) snprintf(propbuf, proplen, "%llu",
2444 } else if (val == UINT64_MAX) {
2445 (void) strlcpy(propbuf, "none", proplen);
2447 zfs_nicenum(val, propbuf, proplen);
2451 case ZFS_PROP_REFRATIO:
2452 case ZFS_PROP_COMPRESSRATIO:
2453 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2455 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2456 (u_longlong_t)(val / 100),
2457 (u_longlong_t)(val % 100));
2461 switch (zhp->zfs_type) {
2462 case ZFS_TYPE_FILESYSTEM:
2465 case ZFS_TYPE_VOLUME:
2468 case ZFS_TYPE_SNAPSHOT:
2471 case ZFS_TYPE_BOOKMARK:
2477 (void) snprintf(propbuf, proplen, "%s", str);
2480 case ZFS_PROP_MOUNTED:
2482 * The 'mounted' property is a pseudo-property that described
2483 * whether the filesystem is currently mounted. Even though
2484 * it's a boolean value, the typical values of "on" and "off"
2485 * don't make sense, so we translate to "yes" and "no".
2487 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2488 src, &source, &val) != 0)
2491 (void) strlcpy(propbuf, "yes", proplen);
2493 (void) strlcpy(propbuf, "no", proplen);
2498 * The 'name' property is a pseudo-property derived from the
2499 * dataset name. It is presented as a real property to simplify
2502 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2505 case ZFS_PROP_MLSLABEL:
2508 m_label_t *new_sl = NULL;
2509 char *ascii = NULL; /* human readable label */
2511 (void) strlcpy(propbuf,
2512 getprop_string(zhp, prop, &source), proplen);
2514 if (literal || (strcasecmp(propbuf,
2515 ZFS_MLSLABEL_DEFAULT) == 0))
2519 * Try to translate the internal hex string to
2520 * human-readable output. If there are any
2521 * problems just use the hex string.
2524 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2525 L_NO_CORRECTION, NULL) == -1) {
2526 m_label_free(new_sl);
2530 if (label_to_str(new_sl, &ascii, M_LABEL,
2534 m_label_free(new_sl);
2537 m_label_free(new_sl);
2539 (void) strlcpy(propbuf, ascii, proplen);
2541 #else /* !illumos */
2543 #endif /* illumos */
2549 * GUIDs are stored as numbers, but they are identifiers.
2550 * We don't want them to be pretty printed, because pretty
2551 * printing mangles the ID into a truncated and useless value.
2553 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2555 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2559 switch (zfs_prop_get_type(prop)) {
2560 case PROP_TYPE_NUMBER:
2561 if (get_numeric_property(zhp, prop, src,
2562 &source, &val) != 0)
2565 (void) snprintf(propbuf, proplen, "%llu",
2568 zfs_nicenum(val, propbuf, proplen);
2571 case PROP_TYPE_STRING:
2572 str = getprop_string(zhp, prop, &source);
2575 (void) strlcpy(propbuf, str, proplen);
2578 case PROP_TYPE_INDEX:
2579 if (get_numeric_property(zhp, prop, src,
2580 &source, &val) != 0)
2582 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2584 (void) strlcpy(propbuf, strval, proplen);
2592 get_source(zhp, src, source, statbuf, statlen);
2598 * Utility function to get the given numeric property. Does no validation that
2599 * the given property is the appropriate type; should only be used with
2600 * hard-coded property types.
2603 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2608 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2614 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2618 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2619 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2623 * Similar to zfs_prop_get(), but returns the value as an integer.
2626 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2627 zprop_source_t *src, char *statbuf, size_t statlen)
2632 * Check to see if this property applies to our object
2634 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2635 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2636 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2637 zfs_prop_to_name(prop)));
2641 *src = ZPROP_SRC_NONE;
2643 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2646 get_source(zhp, src, source, statbuf, statlen);
2652 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2653 char **domainp, idmap_rid_t *ridp)
2656 idmap_get_handle_t *get_hdl = NULL;
2660 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2664 err = idmap_get_sidbyuid(get_hdl, id,
2665 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2667 err = idmap_get_sidbygid(get_hdl, id,
2668 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2670 if (err == IDMAP_SUCCESS &&
2671 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2672 status == IDMAP_SUCCESS)
2678 idmap_get_destroy(get_hdl);
2680 #else /* !illumos */
2681 assert(!"invalid code path");
2682 return (EINVAL); // silence compiler warning
2683 #endif /* illumos */
2687 * convert the propname into parameters needed by kernel
2688 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2689 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2692 userquota_propname_decode(const char *propname, boolean_t zoned,
2693 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2695 zfs_userquota_prop_t type;
2697 char *numericsid = NULL;
2702 /* Figure out the property type ({user|group}{quota|space}) */
2703 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2704 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2705 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2708 if (type == ZFS_NUM_USERQUOTA_PROPS)
2712 isuser = (type == ZFS_PROP_USERQUOTA ||
2713 type == ZFS_PROP_USERUSED);
2715 cp = strchr(propname, '@') + 1;
2717 if (strchr(cp, '@')) {
2720 * It's a SID name (eg "user@domain") that needs to be
2721 * turned into S-1-domainID-RID.
2724 idmap_stat stat, map_stat;
2727 idmap_get_handle_t *gh = NULL;
2729 stat = idmap_get_create(&gh);
2730 if (stat != IDMAP_SUCCESS) {
2731 idmap_get_destroy(gh);
2734 if (zoned && getzoneid() == GLOBAL_ZONEID)
2737 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2740 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2743 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2746 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2750 idmap_get_destroy(gh);
2753 stat = idmap_get_mappings(gh);
2754 idmap_get_destroy(gh);
2759 if (numericsid == NULL)
2763 /* will be further decoded below */
2764 #else /* !illumos */
2766 #endif /* illumos */
2769 if (strncmp(cp, "S-1-", 4) == 0) {
2770 /* It's a numeric SID (eg "S-1-234-567-89") */
2771 (void) strlcpy(domain, cp, domainlen);
2774 cp = strrchr(domain, '-');
2777 *ridp = strtoull(cp, &end, 10);
2785 if (errno != 0 || *end != '\0')
2787 } else if (!isdigit(*cp)) {
2789 * It's a user/group name (eg "user") that needs to be
2790 * turned into a uid/gid
2792 if (zoned && getzoneid() == GLOBAL_ZONEID)
2808 /* It's a user/group ID (eg "12345"). */
2809 uid_t id = strtoul(cp, &end, 10);
2816 /* It's an ephemeral ID. */
2817 if (idmap_id_to_numeric_domain_rid(id, isuser,
2818 &mapdomain, &rid) != 0)
2820 (void) strlcpy(domain, mapdomain, domainlen);
2827 ASSERT3P(numericsid, ==, NULL);
2832 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2833 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2836 zfs_cmd_t zc = { 0 };
2838 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2840 err = userquota_propname_decode(propname,
2841 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2842 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2843 zc.zc_objset_type = *typep;
2847 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2851 *propvalue = zc.zc_cookie;
2856 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2857 uint64_t *propvalue)
2859 zfs_userquota_prop_t type;
2861 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2866 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2867 char *propbuf, int proplen, boolean_t literal)
2871 zfs_userquota_prop_t type;
2873 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2880 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2881 } else if (propvalue == 0 &&
2882 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2883 (void) strlcpy(propbuf, "none", proplen);
2885 zfs_nicenum(propvalue, propbuf, proplen);
2891 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2892 uint64_t *propvalue)
2895 zfs_cmd_t zc = { 0 };
2896 const char *snapname;
2898 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2900 snapname = strchr(propname, '@') + 1;
2901 if (strchr(snapname, '@')) {
2902 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2904 /* snapname is the short name, append it to zhp's fsname */
2907 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2908 sizeof (zc.zc_value));
2909 cp = strchr(zc.zc_value, '@');
2912 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2913 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2916 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2920 *propvalue = zc.zc_cookie;
2925 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2926 char *propbuf, int proplen, boolean_t literal)
2931 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2937 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2939 zfs_nicenum(propvalue, propbuf, proplen);
2945 * Returns the name of the given zfs handle.
2948 zfs_get_name(const zfs_handle_t *zhp)
2950 return (zhp->zfs_name);
2954 * Returns the type of the given zfs handle.
2957 zfs_get_type(const zfs_handle_t *zhp)
2959 return (zhp->zfs_type);
2963 * Is one dataset name a child dataset of another?
2965 * Needs to handle these cases:
2966 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2967 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2968 * Descendant? No. No. No. Yes.
2971 is_descendant(const char *ds1, const char *ds2)
2973 size_t d1len = strlen(ds1);
2975 /* ds2 can't be a descendant if it's smaller */
2976 if (strlen(ds2) < d1len)
2979 /* otherwise, compare strings and verify that there's a '/' char */
2980 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2984 * Given a complete name, return just the portion that refers to the parent.
2985 * Will return -1 if there is no parent (path is just the name of the
2989 parent_name(const char *path, char *buf, size_t buflen)
2993 (void) strlcpy(buf, path, buflen);
2995 if ((slashp = strrchr(buf, '/')) == NULL)
3003 * If accept_ancestor is false, then check to make sure that the given path has
3004 * a parent, and that it exists. If accept_ancestor is true, then find the
3005 * closest existing ancestor for the given path. In prefixlen return the
3006 * length of already existing prefix of the given path. We also fetch the
3007 * 'zoned' property, which is used to validate property settings when creating
3011 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3012 boolean_t accept_ancestor, int *prefixlen)
3014 zfs_cmd_t zc = { 0 };
3015 char parent[ZFS_MAXNAMELEN];
3021 (void) snprintf(errbuf, sizeof (errbuf),
3022 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3024 /* get parent, and check to see if this is just a pool */
3025 if (parent_name(path, parent, sizeof (parent)) != 0) {
3026 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3027 "missing dataset name"));
3028 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3031 /* check to see if the pool exists */
3032 if ((slash = strchr(parent, '/')) == NULL)
3033 slash = parent + strlen(parent);
3034 (void) strncpy(zc.zc_name, parent, slash - parent);
3035 zc.zc_name[slash - parent] = '\0';
3036 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3039 "no such pool '%s'"), zc.zc_name);
3040 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3043 /* check to see if the parent dataset exists */
3044 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3045 if (errno == ENOENT && accept_ancestor) {
3047 * Go deeper to find an ancestor, give up on top level.
3049 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3051 "no such pool '%s'"), zc.zc_name);
3052 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3054 } else if (errno == ENOENT) {
3055 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3056 "parent does not exist"));
3057 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3059 return (zfs_standard_error(hdl, errno, errbuf));
3062 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3066 /* we are in a non-global zone, but parent is in the global zone */
3067 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3068 (void) zfs_standard_error(hdl, EPERM, errbuf);
3073 /* make sure parent is a filesystem */
3074 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3075 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3076 "parent is not a filesystem"));
3077 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3083 if (prefixlen != NULL)
3084 *prefixlen = strlen(parent);
3089 * Finds whether the dataset of the given type(s) exists.
3092 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3096 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3100 * Try to get stats for the dataset, which will tell us if it exists.
3102 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3103 int ds_type = zhp->zfs_type;
3106 if (types & ds_type)
3113 * Given a path to 'target', create all the ancestors between
3114 * the prefixlen portion of the path, and the target itself.
3115 * Fail if the initial prefixlen-ancestor does not already exist.
3118 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3124 /* make sure prefix exists */
3125 cp = target + prefixlen;
3127 assert(strchr(cp, '/') == NULL);
3128 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3131 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3139 * Attempt to create, mount, and share any ancestor filesystems,
3140 * up to the prefixlen-long one.
3142 for (cp = target + prefixlen + 1;
3143 cp = strchr(cp, '/'); *cp = '/', cp++) {
3147 h = make_dataset_handle(hdl, target);
3149 /* it already exists, nothing to do here */
3154 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3156 opname = dgettext(TEXT_DOMAIN, "create");
3160 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3162 opname = dgettext(TEXT_DOMAIN, "open");
3166 if (zfs_mount(h, NULL, 0) != 0) {
3167 opname = dgettext(TEXT_DOMAIN, "mount");
3171 if (zfs_share(h) != 0) {
3172 opname = dgettext(TEXT_DOMAIN, "share");
3182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3183 "failed to %s ancestor '%s'"), opname, target);
3188 * Creates non-existing ancestors of the given path.
3191 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3197 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3200 if ((path_copy = strdup(path)) != NULL) {
3201 rc = create_parents(hdl, path_copy, prefix);
3204 if (path_copy == NULL || rc != 0)
3211 * Create a new filesystem or volume.
3214 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3219 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3222 dmu_objset_type_t ost;
3224 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3225 "cannot create '%s'"), path);
3227 /* validate the path, taking care to note the extended error message */
3228 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3229 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3231 /* validate parents exist */
3232 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3236 * The failure modes when creating a dataset of a different type over
3237 * one that already exists is a little strange. In particular, if you
3238 * try to create a dataset on top of an existing dataset, the ioctl()
3239 * will return ENOENT, not EEXIST. To prevent this from happening, we
3240 * first try to see if the dataset exists.
3242 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3243 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3244 "dataset already exists"));
3245 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3248 if (type == ZFS_TYPE_VOLUME)
3253 /* open zpool handle for prop validation */
3254 char pool_path[MAXNAMELEN];
3255 (void) strlcpy(pool_path, path, sizeof (pool_path));
3257 /* truncate pool_path at first slash */
3258 char *p = strchr(pool_path, '/');
3262 zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
3264 if (props && (props = zfs_valid_proplist(hdl, type, props,
3265 zoned, NULL, zpool_handle, errbuf)) == 0) {
3266 zpool_close(zpool_handle);
3269 zpool_close(zpool_handle);
3271 if (type == ZFS_TYPE_VOLUME) {
3273 * If we are creating a volume, the size and block size must
3274 * satisfy a few restraints. First, the blocksize must be a
3275 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3276 * volsize must be a multiple of the block size, and cannot be
3279 if (props == NULL || nvlist_lookup_uint64(props,
3280 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3282 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3283 "missing volume size"));
3284 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3287 if ((ret = nvlist_lookup_uint64(props,
3288 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3289 &blocksize)) != 0) {
3290 if (ret == ENOENT) {
3291 blocksize = zfs_prop_default_numeric(
3292 ZFS_PROP_VOLBLOCKSIZE);
3295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3296 "missing volume block size"));
3297 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3303 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3304 "volume size cannot be zero"));
3305 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3308 if (size % blocksize != 0) {
3310 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3311 "volume size must be a multiple of volume block "
3313 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3317 /* create the dataset */
3318 ret = lzc_create(path, ost, props);
3321 /* check for failure */
3323 char parent[ZFS_MAXNAMELEN];
3324 (void) parent_name(path, parent, sizeof (parent));
3328 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3329 "no such parent '%s'"), parent);
3330 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3333 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3334 "parent '%s' is not a filesystem"), parent);
3335 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3339 "pool must be upgraded to set this "
3340 "property or value"));
3341 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3345 * This platform can't address a volume this big.
3347 if (type == ZFS_TYPE_VOLUME)
3348 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3353 return (zfs_standard_error(hdl, errno, errbuf));
3361 * Destroys the given dataset. The caller must make sure that the filesystem
3362 * isn't mounted, and that there are no active dependents. If the file system
3363 * does not exist this function does nothing.
3366 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3368 zfs_cmd_t zc = { 0 };
3370 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3371 nvlist_t *nv = fnvlist_alloc();
3372 fnvlist_add_boolean(nv, zhp->zfs_name);
3373 int error = lzc_destroy_bookmarks(nv, NULL);
3376 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3377 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3383 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3385 if (ZFS_IS_VOLUME(zhp)) {
3386 zc.zc_objset_type = DMU_OST_ZVOL;
3388 zc.zc_objset_type = DMU_OST_ZFS;
3391 zc.zc_defer_destroy = defer;
3392 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3394 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3395 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3399 remove_mountpoint(zhp);
3404 struct destroydata {
3406 const char *snapname;
3410 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3412 struct destroydata *dd = arg;
3413 char name[ZFS_MAXNAMELEN];
3416 (void) snprintf(name, sizeof (name),
3417 "%s@%s", zhp->zfs_name, dd->snapname);
3419 if (lzc_exists(name))
3420 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3422 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3428 * Destroys all snapshots with the given name in zhp & descendants.
3431 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3434 struct destroydata dd = { 0 };
3436 dd.snapname = snapname;
3437 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3438 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3440 if (nvlist_empty(dd.nvl)) {
3441 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3442 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3443 zhp->zfs_name, snapname);
3445 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3447 nvlist_free(dd.nvl);
3452 * Destroys all the snapshots named in the nvlist.
3455 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3460 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3465 if (nvlist_empty(errlist)) {
3467 (void) snprintf(errbuf, sizeof (errbuf),
3468 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3470 ret = zfs_standard_error(hdl, ret, errbuf);
3472 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3473 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3475 (void) snprintf(errbuf, sizeof (errbuf),
3476 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3479 switch (fnvpair_value_int32(pair)) {
3482 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3483 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3486 ret = zfs_standard_error(hdl, errno, errbuf);
3495 * Clones the given dataset. The target must be of the same type as the source.
3498 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3500 char parent[ZFS_MAXNAMELEN];
3503 libzfs_handle_t *hdl = zhp->zfs_hdl;
3506 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3508 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3509 "cannot create '%s'"), target);
3511 /* validate the target/clone name */
3512 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3513 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3515 /* validate parents exist */
3516 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3519 (void) parent_name(target, parent, sizeof (parent));
3525 if (ZFS_IS_VOLUME(zhp)) {
3526 type = ZFS_TYPE_VOLUME;
3528 type = ZFS_TYPE_FILESYSTEM;
3530 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3531 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3535 ret = lzc_clone(target, zhp->zfs_name, props);
3543 * The parent doesn't exist. We should have caught this
3544 * above, but there may a race condition that has since
3545 * destroyed the parent.
3547 * At this point, we don't know whether it's the source
3548 * that doesn't exist anymore, or whether the target
3549 * dataset doesn't exist.
3551 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3552 "no such parent '%s'"), parent);
3553 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3556 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3557 "source and target pools differ"));
3558 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3562 return (zfs_standard_error(zhp->zfs_hdl, errno,
3571 * Promotes the given clone fs to be the clone parent.
3574 zfs_promote(zfs_handle_t *zhp)
3576 libzfs_handle_t *hdl = zhp->zfs_hdl;
3577 zfs_cmd_t zc = { 0 };
3578 char parent[MAXPATHLEN];
3582 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3583 "cannot promote '%s'"), zhp->zfs_name);
3585 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3587 "snapshots can not be promoted"));
3588 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3591 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3592 if (parent[0] == '\0') {
3593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3594 "not a cloned filesystem"));
3595 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3598 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3599 sizeof (zc.zc_value));
3600 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3601 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3604 int save_errno = errno;
3606 switch (save_errno) {
3608 /* There is a conflicting snapshot name. */
3609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3610 "conflicting snapshot '%s' from parent '%s'"),
3611 zc.zc_string, parent);
3612 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3615 return (zfs_standard_error(hdl, save_errno, errbuf));
3621 typedef struct snapdata {
3623 const char *sd_snapname;
3627 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3629 snapdata_t *sd = arg;
3630 char name[ZFS_MAXNAMELEN];
3633 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3634 (void) snprintf(name, sizeof (name),
3635 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3637 fnvlist_add_boolean(sd->sd_nvl, name);
3639 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3647 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3651 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3658 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3659 "cannot create snapshots "));
3662 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3663 const char *snapname = nvpair_name(elem);
3665 /* validate the target name */
3666 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3668 (void) snprintf(errbuf, sizeof (errbuf),
3669 dgettext(TEXT_DOMAIN,
3670 "cannot create snapshot '%s'"), snapname);
3671 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3676 * get pool handle for prop validation. assumes all snaps are in the
3677 * same pool, as does lzc_snapshot (below).
3679 char pool[MAXNAMELEN];
3680 elem = nvlist_next_nvpair(snaps, NULL);
3681 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3682 pool[strcspn(pool, "/@")] = '\0';
3683 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3685 if (props != NULL &&
3686 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3687 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3688 zpool_close(zpool_hdl);
3691 zpool_close(zpool_hdl);
3693 ret = lzc_snapshot(snaps, props, &errors);
3696 boolean_t printed = B_FALSE;
3697 for (elem = nvlist_next_nvpair(errors, NULL);
3699 elem = nvlist_next_nvpair(errors, elem)) {
3700 (void) snprintf(errbuf, sizeof (errbuf),
3701 dgettext(TEXT_DOMAIN,
3702 "cannot create snapshot '%s'"), nvpair_name(elem));
3703 (void) zfs_standard_error(hdl,
3704 fnvpair_value_int32(elem), errbuf);
3710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3711 "multiple snapshots of same "
3713 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3717 (void) zfs_standard_error(hdl, ret, errbuf);
3723 nvlist_free(errors);
3728 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3732 snapdata_t sd = { 0 };
3733 char fsname[ZFS_MAXNAMELEN];
3738 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3739 "cannot snapshot %s"), path);
3741 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3742 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3744 (void) strlcpy(fsname, path, sizeof (fsname));
3745 cp = strchr(fsname, '@');
3747 sd.sd_snapname = cp + 1;
3749 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3750 ZFS_TYPE_VOLUME)) == NULL) {
3754 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3756 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3758 fnvlist_add_boolean(sd.sd_nvl, path);
3761 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3762 nvlist_free(sd.sd_nvl);
3768 * Destroy any more recent snapshots. We invoke this callback on any dependents
3769 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3770 * is a dependent and we should just destroy it without checking the transaction
3773 typedef struct rollback_data {
3774 const char *cb_target; /* the snapshot */
3775 uint64_t cb_create; /* creation time reference */
3781 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3783 rollback_data_t *cbp = data;
3784 prop_changelist_t *clp;
3786 /* We must destroy this clone; first unmount it */
3787 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3788 cbp->cb_force ? MS_FORCE: 0);
3789 if (clp == NULL || changelist_prefix(clp) != 0) {
3790 cbp->cb_error = B_TRUE;
3794 if (zfs_destroy(zhp, B_FALSE) != 0)
3795 cbp->cb_error = B_TRUE;
3797 changelist_remove(clp, zhp->zfs_name);
3798 (void) changelist_postfix(clp);
3799 changelist_free(clp);
3806 rollback_destroy(zfs_handle_t *zhp, void *data)
3808 rollback_data_t *cbp = data;
3810 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3811 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3812 rollback_destroy_dependent, cbp);
3814 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3822 * Given a dataset, rollback to a specific snapshot, discarding any
3823 * data changes since then and making it the active dataset.
3825 * Any snapshots and bookmarks more recent than the target are
3826 * destroyed, along with their dependents (i.e. clones).
3829 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3831 rollback_data_t cb = { 0 };
3833 boolean_t restore_resv = 0;
3834 uint64_t old_volsize, new_volsize;
3835 zfs_prop_t resv_prop;
3837 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3838 zhp->zfs_type == ZFS_TYPE_VOLUME);
3841 * Destroy all recent snapshots and their dependents.
3843 cb.cb_force = force;
3844 cb.cb_target = snap->zfs_name;
3845 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3846 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3847 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3853 * Now that we have verified that the snapshot is the latest,
3854 * rollback to the given snapshot.
3857 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3858 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3860 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3862 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3866 * We rely on zfs_iter_children() to verify that there are no
3867 * newer snapshots for the given dataset. Therefore, we can
3868 * simply pass the name on to the ioctl() call. There is still
3869 * an unlikely race condition where the user has taken a
3870 * snapshot since we verified that this was the most recent.
3872 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3874 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3875 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3881 * For volumes, if the pre-rollback volsize matched the pre-
3882 * rollback reservation and the volsize has changed then set
3883 * the reservation property to the post-rollback volsize.
3884 * Make a new handle since the rollback closed the dataset.
3886 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3887 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3889 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3890 if (old_volsize != new_volsize)
3891 err = zfs_prop_set_int(zhp, resv_prop,
3900 * Renames the given dataset.
3903 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
3904 renameflags_t flags)
3907 zfs_cmd_t zc = { 0 };
3909 prop_changelist_t *cl = NULL;
3910 zfs_handle_t *zhrp = NULL;
3911 char *parentname = NULL;
3912 char parent[ZFS_MAXNAMELEN];
3913 char property[ZFS_MAXPROPLEN];
3914 libzfs_handle_t *hdl = zhp->zfs_hdl;
3917 /* if we have the same exact name, just return success */
3918 if (strcmp(zhp->zfs_name, target) == 0)
3921 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3922 "cannot rename to '%s'"), target);
3924 if (source != NULL) {
3926 * This is recursive snapshots rename, put snapshot name
3927 * (that might not exist) into zfs_name.
3929 assert(flags.recurse);
3931 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
3932 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
3933 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
3937 * Make sure the target name is valid
3939 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3940 if ((strchr(target, '@') == NULL) ||
3943 * Snapshot target name is abbreviated,
3944 * reconstruct full dataset name
3946 (void) strlcpy(parent, zhp->zfs_name,
3948 delim = strchr(parent, '@');
3949 if (strchr(target, '@') == NULL)
3953 (void) strlcat(parent, target, sizeof (parent));
3957 * Make sure we're renaming within the same dataset.
3959 delim = strchr(target, '@');
3960 if (strncmp(zhp->zfs_name, target, delim - target)
3961 != 0 || zhp->zfs_name[delim - target] != '@') {
3962 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3963 "snapshots must be part of same "
3965 return (zfs_error(hdl, EZFS_CROSSTARGET,
3969 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3970 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3972 if (flags.recurse) {
3973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3974 "recursive rename must be a snapshot"));
3975 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3978 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3979 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3981 /* validate parents */
3982 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3985 /* make sure we're in the same pool */
3986 verify((delim = strchr(target, '/')) != NULL);
3987 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3988 zhp->zfs_name[delim - target] != '/') {
3989 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3990 "datasets must be within same pool"));
3991 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3994 /* new name cannot be a child of the current dataset name */
3995 if (is_descendant(zhp->zfs_name, target)) {
3996 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3997 "New dataset name cannot be a descendant of "
3998 "current dataset name"));
3999 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4003 (void) snprintf(errbuf, sizeof (errbuf),
4004 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4006 if (getzoneid() == GLOBAL_ZONEID &&
4007 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4009 "dataset is used in a non-global zone"));
4010 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4014 * Avoid unmounting file systems with mountpoint property set to
4015 * 'legacy' or 'none' even if -u option is not given.
4017 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4018 !flags.recurse && !flags.nounmount &&
4019 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4020 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4021 (strcmp(property, "legacy") == 0 ||
4022 strcmp(property, "none") == 0)) {
4023 flags.nounmount = B_TRUE;
4025 if (flags.recurse) {
4027 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4028 if (parentname == NULL) {
4032 delim = strchr(parentname, '@');
4034 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4039 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4040 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4041 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
4042 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
4046 if (changelist_haszonedchild(cl)) {
4047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4048 "child dataset with inherited mountpoint is used "
4049 "in a non-global zone"));
4050 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4054 if ((ret = changelist_prefix(cl)) != 0)
4058 if (ZFS_IS_VOLUME(zhp))
4059 zc.zc_objset_type = DMU_OST_ZVOL;
4061 zc.zc_objset_type = DMU_OST_ZFS;
4063 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4064 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4066 zc.zc_cookie = flags.recurse ? 1 : 0;
4067 if (flags.nounmount)
4070 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4072 * if it was recursive, the one that actually failed will
4075 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4076 "cannot rename '%s'"), zc.zc_name);
4078 if (flags.recurse && errno == EEXIST) {
4079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4080 "a child dataset already has a snapshot "
4081 "with the new name"));
4082 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4084 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4088 * On failure, we still want to remount any filesystems that
4089 * were previously mounted, so we don't alter the system state.
4092 (void) changelist_postfix(cl);
4095 changelist_rename(cl, zfs_get_name(zhp), target);
4096 ret = changelist_postfix(cl);
4101 if (parentname != NULL) {
4108 changelist_free(cl);
4114 zfs_get_user_props(zfs_handle_t *zhp)
4116 return (zhp->zfs_user_props);
4120 zfs_get_recvd_props(zfs_handle_t *zhp)
4122 if (zhp->zfs_recvd_props == NULL)
4123 if (get_recvd_props_ioctl(zhp) != 0)
4125 return (zhp->zfs_recvd_props);
4129 * This function is used by 'zfs list' to determine the exact set of columns to
4130 * display, and their maximum widths. This does two main things:
4132 * - If this is a list of all properties, then expand the list to include
4133 * all native properties, and set a flag so that for each dataset we look
4134 * for new unique user properties and add them to the list.
4136 * - For non fixed-width properties, keep track of the maximum width seen
4137 * so that we can size the column appropriately. If the user has
4138 * requested received property values, we also need to compute the width
4139 * of the RECEIVED column.
4142 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4145 libzfs_handle_t *hdl = zhp->zfs_hdl;
4146 zprop_list_t *entry;
4147 zprop_list_t **last, **start;
4148 nvlist_t *userprops, *propval;
4151 char buf[ZFS_MAXPROPLEN];
4153 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4156 userprops = zfs_get_user_props(zhp);
4159 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4161 * Go through and add any user properties as necessary. We
4162 * start by incrementing our list pointer to the first
4163 * non-native property.
4166 while (*start != NULL) {
4167 if ((*start)->pl_prop == ZPROP_INVAL)
4169 start = &(*start)->pl_next;
4173 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4175 * See if we've already found this property in our list.
4177 for (last = start; *last != NULL;
4178 last = &(*last)->pl_next) {
4179 if (strcmp((*last)->pl_user_prop,
4180 nvpair_name(elem)) == 0)
4184 if (*last == NULL) {
4185 if ((entry = zfs_alloc(hdl,
4186 sizeof (zprop_list_t))) == NULL ||
4187 ((entry->pl_user_prop = zfs_strdup(hdl,
4188 nvpair_name(elem)))) == NULL) {
4193 entry->pl_prop = ZPROP_INVAL;
4194 entry->pl_width = strlen(nvpair_name(elem));
4195 entry->pl_all = B_TRUE;
4202 * Now go through and check the width of any non-fixed columns
4204 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4205 if (entry->pl_fixed && !literal)
4208 if (entry->pl_prop != ZPROP_INVAL) {
4209 if (zfs_prop_get(zhp, entry->pl_prop,
4210 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4211 if (strlen(buf) > entry->pl_width)
4212 entry->pl_width = strlen(buf);
4214 if (received && zfs_prop_get_recvd(zhp,
4215 zfs_prop_to_name(entry->pl_prop),
4216 buf, sizeof (buf), literal) == 0)
4217 if (strlen(buf) > entry->pl_recvd_width)
4218 entry->pl_recvd_width = strlen(buf);
4220 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4222 verify(nvlist_lookup_string(propval,
4223 ZPROP_VALUE, &strval) == 0);
4224 if (strlen(strval) > entry->pl_width)
4225 entry->pl_width = strlen(strval);
4227 if (received && zfs_prop_get_recvd(zhp,
4228 entry->pl_user_prop,
4229 buf, sizeof (buf), literal) == 0)
4230 if (strlen(buf) > entry->pl_recvd_width)
4231 entry->pl_recvd_width = strlen(buf);
4239 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4240 char *resource, void *export, void *sharetab,
4241 int sharemax, zfs_share_op_t operation)
4243 zfs_cmd_t zc = { 0 };
4246 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4247 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4249 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4250 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4251 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4252 zc.zc_share.z_sharetype = operation;
4253 zc.zc_share.z_sharemax = sharemax;
4254 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4259 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4264 * Keep a reference to the props-table against which we prune the
4267 zhp->zfs_props_table = props;
4269 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4272 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4273 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4276 * User properties will result in ZPROP_INVAL, and since we
4277 * only know how to prune standard ZFS properties, we always
4278 * leave these in the list. This can also happen if we
4279 * encounter an unknown DSL property (when running older
4280 * software, for example).
4282 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4283 (void) nvlist_remove(zhp->zfs_props,
4284 nvpair_name(curr), nvpair_type(curr));
4291 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4292 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4294 zfs_cmd_t zc = { 0 };
4295 nvlist_t *nvlist = NULL;
4298 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4299 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4300 zc.zc_cookie = (uint64_t)cmd;
4302 if (cmd == ZFS_SMB_ACL_RENAME) {
4303 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4304 (void) no_memory(hdl);
4310 case ZFS_SMB_ACL_ADD:
4311 case ZFS_SMB_ACL_REMOVE:
4312 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4314 case ZFS_SMB_ACL_RENAME:
4315 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4317 (void) no_memory(hdl);
4320 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4322 (void) no_memory(hdl);
4325 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4326 nvlist_free(nvlist);
4330 case ZFS_SMB_ACL_PURGE:
4335 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4336 nvlist_free(nvlist);
4341 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4342 char *path, char *resource)
4344 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4349 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4350 char *path, char *resource)
4352 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4357 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4359 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4364 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4365 char *oldname, char *newname)
4367 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4370 #endif /* illumos */
4373 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4374 zfs_userspace_cb_t func, void *arg)
4376 zfs_cmd_t zc = { 0 };
4377 zfs_useracct_t buf[100];
4378 libzfs_handle_t *hdl = zhp->zfs_hdl;
4381 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4383 zc.zc_objset_type = type;
4384 zc.zc_nvlist_dst = (uintptr_t)buf;
4387 zfs_useracct_t *zua = buf;
4389 zc.zc_nvlist_dst_size = sizeof (buf);
4390 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4393 (void) snprintf(errbuf, sizeof (errbuf),
4394 dgettext(TEXT_DOMAIN,
4395 "cannot get used/quota for %s"), zc.zc_name);
4396 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4398 if (zc.zc_nvlist_dst_size == 0)
4401 while (zc.zc_nvlist_dst_size > 0) {
4402 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4403 zua->zu_space)) != 0)
4406 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4415 const char *snapname;
4417 boolean_t recursive;
4422 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4424 struct holdarg *ha = arg;
4425 char name[ZFS_MAXNAMELEN];
4428 (void) snprintf(name, sizeof (name),
4429 "%s@%s", zhp->zfs_name, ha->snapname);
4431 if (lzc_exists(name))
4432 fnvlist_add_string(ha->nvl, name, ha->tag);
4435 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4441 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4442 boolean_t recursive, int cleanup_fd)
4447 ha.nvl = fnvlist_alloc();
4448 ha.snapname = snapname;
4450 ha.recursive = recursive;
4451 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4453 if (nvlist_empty(ha.nvl)) {
4456 fnvlist_free(ha.nvl);
4458 (void) snprintf(errbuf, sizeof (errbuf),
4459 dgettext(TEXT_DOMAIN,
4460 "cannot hold snapshot '%s@%s'"),
4461 zhp->zfs_name, snapname);
4462 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4466 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4467 fnvlist_free(ha.nvl);
4473 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4477 libzfs_handle_t *hdl = zhp->zfs_hdl;
4482 ret = lzc_hold(holds, cleanup_fd, &errors);
4485 /* There may be errors even in the success case. */
4486 fnvlist_free(errors);
4490 if (nvlist_empty(errors)) {
4491 /* no hold-specific errors */
4492 (void) snprintf(errbuf, sizeof (errbuf),
4493 dgettext(TEXT_DOMAIN, "cannot hold"));
4496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4497 "pool must be upgraded"));
4498 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4501 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4504 (void) zfs_standard_error(hdl, ret, errbuf);
4508 for (elem = nvlist_next_nvpair(errors, NULL);
4510 elem = nvlist_next_nvpair(errors, elem)) {
4511 (void) snprintf(errbuf, sizeof (errbuf),
4512 dgettext(TEXT_DOMAIN,
4513 "cannot hold snapshot '%s'"), nvpair_name(elem));
4514 switch (fnvpair_value_int32(elem)) {
4517 * Temporary tags wind up having the ds object id
4518 * prepended. So even if we passed the length check
4519 * above, it's still possible for the tag to wind
4520 * up being slightly too long.
4522 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4525 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4528 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4531 (void) zfs_standard_error(hdl,
4532 fnvpair_value_int32(elem), errbuf);
4536 fnvlist_free(errors);
4541 zfs_release_one(zfs_handle_t *zhp, void *arg)
4543 struct holdarg *ha = arg;
4544 char name[ZFS_MAXNAMELEN];
4546 nvlist_t *existing_holds;
4548 (void) snprintf(name, sizeof (name),
4549 "%s@%s", zhp->zfs_name, ha->snapname);
4551 if (lzc_get_holds(name, &existing_holds) != 0) {
4553 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4556 nvlist_t *torelease = fnvlist_alloc();
4557 fnvlist_add_boolean(torelease, ha->tag);
4558 fnvlist_add_nvlist(ha->nvl, name, torelease);
4559 fnvlist_free(torelease);
4563 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4569 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4570 boolean_t recursive)
4574 nvlist_t *errors = NULL;
4576 libzfs_handle_t *hdl = zhp->zfs_hdl;
4579 ha.nvl = fnvlist_alloc();
4580 ha.snapname = snapname;
4582 ha.recursive = recursive;
4584 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4586 if (nvlist_empty(ha.nvl)) {
4587 fnvlist_free(ha.nvl);
4589 (void) snprintf(errbuf, sizeof (errbuf),
4590 dgettext(TEXT_DOMAIN,
4591 "cannot release hold from snapshot '%s@%s'"),
4592 zhp->zfs_name, snapname);
4594 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4596 (void) zfs_standard_error(hdl, ret, errbuf);
4601 ret = lzc_release(ha.nvl, &errors);
4602 fnvlist_free(ha.nvl);
4605 /* There may be errors even in the success case. */
4606 fnvlist_free(errors);
4610 if (nvlist_empty(errors)) {
4611 /* no hold-specific errors */
4612 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4617 "pool must be upgraded"));
4618 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4621 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4625 for (elem = nvlist_next_nvpair(errors, NULL);
4627 elem = nvlist_next_nvpair(errors, elem)) {
4628 (void) snprintf(errbuf, sizeof (errbuf),
4629 dgettext(TEXT_DOMAIN,
4630 "cannot release hold from snapshot '%s'"),
4632 switch (fnvpair_value_int32(elem)) {
4634 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4637 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4640 (void) zfs_standard_error_fmt(hdl,
4641 fnvpair_value_int32(elem), errbuf);
4645 fnvlist_free(errors);
4650 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4652 zfs_cmd_t zc = { 0 };
4653 libzfs_handle_t *hdl = zhp->zfs_hdl;
4659 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4660 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4664 nvbuf = malloc(nvsz);
4665 if (nvbuf == NULL) {
4666 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4670 zc.zc_nvlist_dst_size = nvsz;
4671 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4673 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4675 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4676 (void) snprintf(errbuf, sizeof (errbuf),
4677 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4682 nvsz = zc.zc_nvlist_dst_size;
4686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4687 "pool must be upgraded"));
4688 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4691 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4694 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4697 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4702 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4704 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4705 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4707 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4717 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4719 zfs_cmd_t zc = { 0 };
4720 libzfs_handle_t *hdl = zhp->zfs_hdl;
4726 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4727 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4729 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4732 nvbuf = malloc(nvsz);
4734 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4737 zc.zc_nvlist_src_size = nvsz;
4738 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4739 zc.zc_perm_action = un;
4741 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4743 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4744 (void) snprintf(errbuf, sizeof (errbuf),
4745 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4750 "pool must be upgraded"));
4751 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4754 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4757 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4760 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4771 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4776 err = lzc_get_holds(zhp->zfs_name, nvl);
4779 libzfs_handle_t *hdl = zhp->zfs_hdl;
4781 (void) snprintf(errbuf, sizeof (errbuf),
4782 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4787 "pool must be upgraded"));
4788 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4791 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4794 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4797 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4806 * Convert the zvol's volume size to an appropriate reservation.
4807 * Note: If this routine is updated, it is necessary to update the ZFS test
4808 * suite's shell version in reservation.kshlib.
4811 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4814 uint64_t nblocks, volblocksize;
4818 if (nvlist_lookup_string(props,
4819 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4820 ncopies = atoi(strval);
4823 if (nvlist_lookup_uint64(props,
4824 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4825 &volblocksize) != 0)
4826 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4827 nblocks = volsize/volblocksize;
4828 /* start with metadnode L0-L6 */
4830 /* calculate number of indirects */
4831 while (nblocks > 1) {
4832 nblocks += DNODES_PER_LEVEL - 1;
4833 nblocks /= DNODES_PER_LEVEL;
4836 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4839 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4840 * compressed, but in practice they compress down to about
4843 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4849 * Attach/detach the given filesystem to/from the given jail.
4852 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4854 libzfs_handle_t *hdl = zhp->zfs_hdl;
4855 zfs_cmd_t zc = { 0 };
4861 (void) snprintf(errbuf, sizeof (errbuf),
4862 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4864 (void) snprintf(errbuf, sizeof (errbuf),
4865 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
4868 switch (zhp->zfs_type) {
4869 case ZFS_TYPE_VOLUME:
4870 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4871 "volumes can not be jailed"));
4872 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4873 case ZFS_TYPE_SNAPSHOT:
4874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4875 "snapshots can not be jailed"));
4876 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4878 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4880 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4881 zc.zc_objset_type = DMU_OST_ZFS;
4882 zc.zc_jailid = jailid;
4884 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4885 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4886 zfs_standard_error(hdl, errno, errbuf);