4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2016 Nexenta Systems, Inc.
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"));
83 return (dgettext(TEXT_DOMAIN, "pool"));
84 case ZFS_TYPE_BOOKMARK:
85 return (dgettext(TEXT_DOMAIN, "bookmark"));
87 assert(!"unhandled zfs_type_t");
94 * Validate a ZFS path. This is used even before trying to open the dataset, to
95 * provide a more meaningful error message. We call zfs_error_aux() to
96 * explain exactly why the name was not valid.
99 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
105 (void) zfs_prop_get_table();
106 if (dataset_namecheck(path, &why, &what) != 0) {
109 case NAME_ERR_TOOLONG:
110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
111 "name is too long"));
114 case NAME_ERR_LEADING_SLASH:
115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116 "leading slash in name"));
119 case NAME_ERR_EMPTY_COMPONENT:
120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121 "empty component in name"));
124 case NAME_ERR_TRAILING_SLASH:
125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
126 "trailing slash in name"));
129 case NAME_ERR_INVALCHAR:
131 dgettext(TEXT_DOMAIN, "invalid character "
132 "'%c' in name"), what);
135 case NAME_ERR_MULTIPLE_AT:
136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
137 "multiple '@' delimiters in name"));
140 case NAME_ERR_NOLETTER:
141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
142 "pool doesn't begin with a letter"));
145 case NAME_ERR_RESERVED:
146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
147 "name is reserved"));
150 case NAME_ERR_DISKLIKE:
151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152 "reserved disk name"));
156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
157 "(%d) not defined"), why);
165 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
168 "snapshot delimiter '@' in filesystem name"));
172 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
175 "missing '@' delimiter in snapshot name"));
179 if (modifying && strchr(path, '%') != NULL) {
181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
182 "invalid character %c in name"), '%');
190 zfs_name_valid(const char *name, zfs_type_t type)
192 if (type == ZFS_TYPE_POOL)
193 return (zpool_name_valid(NULL, B_FALSE, name));
194 return (zfs_validate_name(NULL, name, type, B_FALSE));
198 * This function takes the raw DSL properties, and filters out the user-defined
199 * properties into a separate nvlist.
202 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
204 libzfs_handle_t *hdl = zhp->zfs_hdl;
209 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
210 (void) no_memory(hdl);
215 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
216 if (!zfs_prop_user(nvpair_name(elem)))
219 verify(nvpair_value_nvlist(elem, &propval) == 0);
220 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
222 (void) no_memory(hdl);
230 static zpool_handle_t *
231 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
233 libzfs_handle_t *hdl = zhp->zfs_hdl;
236 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
237 if (hdl->libzfs_pool_handles != NULL)
238 zph->zpool_next = hdl->libzfs_pool_handles;
239 hdl->libzfs_pool_handles = zph;
244 static zpool_handle_t *
245 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
247 libzfs_handle_t *hdl = zhp->zfs_hdl;
248 zpool_handle_t *zph = hdl->libzfs_pool_handles;
250 while ((zph != NULL) &&
251 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
252 zph = zph->zpool_next;
257 * Returns a handle to the pool that contains the provided dataset.
258 * If a handle to that pool already exists then that handle is returned.
259 * Otherwise, a new handle is created and added to the list of handles.
261 static zpool_handle_t *
262 zpool_handle(zfs_handle_t *zhp)
268 len = strcspn(zhp->zfs_name, "/@#") + 1;
269 pool_name = zfs_alloc(zhp->zfs_hdl, len);
270 (void) strlcpy(pool_name, zhp->zfs_name, len);
272 zph = zpool_find_handle(zhp, pool_name, len);
274 zph = zpool_add_handle(zhp, pool_name);
281 zpool_free_handles(libzfs_handle_t *hdl)
283 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
285 while (zph != NULL) {
286 next = zph->zpool_next;
290 hdl->libzfs_pool_handles = NULL;
294 * Utility function to gather stats (objset and zpl) for the given object.
297 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
299 libzfs_handle_t *hdl = zhp->zfs_hdl;
301 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
303 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
304 if (errno == ENOMEM) {
305 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
316 * Utility function to get the received properties of the given object.
319 get_recvd_props_ioctl(zfs_handle_t *zhp)
321 libzfs_handle_t *hdl = zhp->zfs_hdl;
322 nvlist_t *recvdprops;
323 zfs_cmd_t zc = { 0 };
326 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
329 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
331 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
332 if (errno == ENOMEM) {
333 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
337 zcmd_free_nvlists(&zc);
342 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
343 zcmd_free_nvlists(&zc);
347 nvlist_free(zhp->zfs_recvd_props);
348 zhp->zfs_recvd_props = recvdprops;
354 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
356 nvlist_t *allprops, *userprops;
358 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
360 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
365 * XXX Why do we store the user props separately, in addition to
366 * storing them in zfs_props?
368 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
369 nvlist_free(allprops);
373 nvlist_free(zhp->zfs_props);
374 nvlist_free(zhp->zfs_user_props);
376 zhp->zfs_props = allprops;
377 zhp->zfs_user_props = userprops;
383 get_stats(zfs_handle_t *zhp)
386 zfs_cmd_t zc = { 0 };
388 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
390 if (get_stats_ioctl(zhp, &zc) != 0)
392 else if (put_stats_zhdl(zhp, &zc) != 0)
394 zcmd_free_nvlists(&zc);
399 * Refresh the properties currently stored in the handle.
402 zfs_refresh_properties(zfs_handle_t *zhp)
404 (void) get_stats(zhp);
408 * Makes a handle from the given dataset name. Used by zfs_open() and
409 * zfs_iter_* to create child handles on the fly.
412 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
414 if (put_stats_zhdl(zhp, zc) != 0)
418 * We've managed to open the dataset and gather statistics. Determine
419 * the high-level type.
421 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
422 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
423 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
424 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
428 if (zhp->zfs_dmustats.dds_is_snapshot)
429 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
430 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
431 zhp->zfs_type = ZFS_TYPE_VOLUME;
432 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
433 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
435 abort(); /* we should never see any other types */
437 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
444 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
446 zfs_cmd_t zc = { 0 };
448 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
454 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
455 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
459 if (get_stats_ioctl(zhp, &zc) == -1) {
460 zcmd_free_nvlists(&zc);
464 if (make_dataset_handle_common(zhp, &zc) == -1) {
468 zcmd_free_nvlists(&zc);
473 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
475 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
481 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
482 if (make_dataset_handle_common(zhp, zc) == -1) {
490 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
492 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
497 zhp->zfs_hdl = pzhp->zfs_hdl;
498 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
499 zhp->zfs_head_type = pzhp->zfs_type;
500 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
501 zhp->zpool_hdl = zpool_handle(zhp);
506 zfs_handle_dup(zfs_handle_t *zhp_orig)
508 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
513 zhp->zfs_hdl = zhp_orig->zfs_hdl;
514 zhp->zpool_hdl = zhp_orig->zpool_hdl;
515 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
516 sizeof (zhp->zfs_name));
517 zhp->zfs_type = zhp_orig->zfs_type;
518 zhp->zfs_head_type = zhp_orig->zfs_head_type;
519 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
520 if (zhp_orig->zfs_props != NULL) {
521 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
522 (void) no_memory(zhp->zfs_hdl);
527 if (zhp_orig->zfs_user_props != NULL) {
528 if (nvlist_dup(zhp_orig->zfs_user_props,
529 &zhp->zfs_user_props, 0) != 0) {
530 (void) no_memory(zhp->zfs_hdl);
535 if (zhp_orig->zfs_recvd_props != NULL) {
536 if (nvlist_dup(zhp_orig->zfs_recvd_props,
537 &zhp->zfs_recvd_props, 0)) {
538 (void) no_memory(zhp->zfs_hdl);
543 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
544 if (zhp_orig->zfs_mntopts != NULL) {
545 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
546 zhp_orig->zfs_mntopts);
548 zhp->zfs_props_table = zhp_orig->zfs_props_table;
553 zfs_bookmark_exists(const char *path)
557 char fsname[ZFS_MAXNAMELEN];
564 (void) strlcpy(fsname, path, sizeof (fsname));
565 pound = strchr(fsname, '#');
570 bmark_name = pound + 1;
571 props = fnvlist_alloc();
572 err = lzc_get_bookmarks(fsname, props, &bmarks);
579 rv = nvlist_exists(bmarks, bmark_name);
585 make_bookmark_handle(zfs_handle_t *parent, const char *path,
586 nvlist_t *bmark_props)
588 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
593 /* Fill in the name. */
594 zhp->zfs_hdl = parent->zfs_hdl;
595 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
597 /* Set the property lists. */
598 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
604 zhp->zfs_head_type = parent->zfs_head_type;
605 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
607 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
608 nvlist_free(zhp->zfs_props);
617 * Opens the given snapshot, filesystem, or volume. The 'types'
618 * argument is a mask of acceptable types. The function will print an
619 * appropriate error message and return NULL if it can't be opened.
622 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
627 (void) snprintf(errbuf, sizeof (errbuf),
628 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
631 * Validate the name before we even try to open it.
633 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
634 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
635 "invalid dataset name"));
636 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
641 * Try to get stats for the dataset, which will tell us if it exists.
644 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
645 (void) zfs_standard_error(hdl, errno, errbuf);
650 char *at = strchr(path, '@');
655 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
656 (void) zfs_standard_error(hdl, errno, errbuf);
661 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
662 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
665 if (!(types & zhp->zfs_type)) {
666 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
675 * Release a ZFS handle. Nothing to do but free the associated memory.
678 zfs_close(zfs_handle_t *zhp)
680 if (zhp->zfs_mntopts)
681 free(zhp->zfs_mntopts);
682 nvlist_free(zhp->zfs_props);
683 nvlist_free(zhp->zfs_user_props);
684 nvlist_free(zhp->zfs_recvd_props);
688 typedef struct mnttab_node {
689 struct mnttab mtn_mt;
694 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
696 const mnttab_node_t *mtn1 = arg1;
697 const mnttab_node_t *mtn2 = arg2;
700 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
704 return (rv > 0 ? 1 : -1);
708 libzfs_mnttab_init(libzfs_handle_t *hdl)
710 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
711 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
712 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
716 libzfs_mnttab_update(libzfs_handle_t *hdl)
720 rewind(hdl->libzfs_mnttab);
721 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
724 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
726 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
727 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
728 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
729 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
730 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
731 avl_add(&hdl->libzfs_mnttab_cache, mtn);
736 libzfs_mnttab_fini(libzfs_handle_t *hdl)
741 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
743 free(mtn->mtn_mt.mnt_special);
744 free(mtn->mtn_mt.mnt_mountp);
745 free(mtn->mtn_mt.mnt_fstype);
746 free(mtn->mtn_mt.mnt_mntopts);
749 avl_destroy(&hdl->libzfs_mnttab_cache);
753 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
755 hdl->libzfs_mnttab_enable = enable;
759 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
760 struct mnttab *entry)
765 if (!hdl->libzfs_mnttab_enable) {
766 struct mnttab srch = { 0 };
768 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
769 libzfs_mnttab_fini(hdl);
770 rewind(hdl->libzfs_mnttab);
771 srch.mnt_special = (char *)fsname;
772 srch.mnt_fstype = MNTTYPE_ZFS;
773 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
779 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
780 libzfs_mnttab_update(hdl);
782 find.mtn_mt.mnt_special = (char *)fsname;
783 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
785 *entry = mtn->mtn_mt;
792 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
793 const char *mountp, const char *mntopts)
797 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
799 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
800 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
801 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
802 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
803 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
804 avl_add(&hdl->libzfs_mnttab_cache, mtn);
808 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
813 find.mtn_mt.mnt_special = (char *)fsname;
814 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
816 avl_remove(&hdl->libzfs_mnttab_cache, ret);
817 free(ret->mtn_mt.mnt_special);
818 free(ret->mtn_mt.mnt_mountp);
819 free(ret->mtn_mt.mnt_fstype);
820 free(ret->mtn_mt.mnt_mntopts);
826 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
828 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
830 if (zpool_handle == NULL)
833 *spa_version = zpool_get_prop_int(zpool_handle,
834 ZPOOL_PROP_VERSION, NULL);
839 * The choice of reservation property depends on the SPA version.
842 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
846 if (zfs_spa_version(zhp, &spa_version) < 0)
849 if (spa_version >= SPA_VERSION_REFRESERVATION)
850 *resv_prop = ZFS_PROP_REFRESERVATION;
852 *resv_prop = ZFS_PROP_RESERVATION;
858 * Given an nvlist of properties to set, validates that they are correct, and
859 * parses any numeric properties (index, boolean, etc) if they are specified as
863 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
864 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
872 int chosen_normal = -1;
875 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
876 (void) no_memory(hdl);
881 * Make sure this property is valid and applies to this type.
885 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
886 const char *propname = nvpair_name(elem);
888 prop = zfs_name_to_prop(propname);
889 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
891 * This is a user property: make sure it's a
892 * string, and that it's less than ZAP_MAXNAMELEN.
894 if (nvpair_type(elem) != DATA_TYPE_STRING) {
895 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
896 "'%s' must be a string"), propname);
897 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
901 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
903 "property name '%s' is too long"),
905 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
909 (void) nvpair_value_string(elem, &strval);
910 if (nvlist_add_string(ret, propname, strval) != 0) {
911 (void) no_memory(hdl);
918 * Currently, only user properties can be modified on
921 if (type == ZFS_TYPE_SNAPSHOT) {
922 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
923 "this property can not be modified for snapshots"));
924 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
928 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
929 zfs_userquota_prop_t uqtype;
930 char newpropname[128];
935 if (userquota_propname_decode(propname, zoned,
936 &uqtype, domain, sizeof (domain), &rid) != 0) {
938 dgettext(TEXT_DOMAIN,
939 "'%s' has an invalid user/group name"),
941 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
945 if (uqtype != ZFS_PROP_USERQUOTA &&
946 uqtype != ZFS_PROP_GROUPQUOTA) {
948 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
950 (void) zfs_error(hdl, EZFS_PROPREADONLY,
955 if (nvpair_type(elem) == DATA_TYPE_STRING) {
956 (void) nvpair_value_string(elem, &strval);
957 if (strcmp(strval, "none") == 0) {
959 } else if (zfs_nicestrtonum(hdl,
960 strval, &intval) != 0) {
961 (void) zfs_error(hdl,
962 EZFS_BADPROP, errbuf);
965 } else if (nvpair_type(elem) ==
967 (void) nvpair_value_uint64(elem, &intval);
969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
970 "use 'none' to disable "
971 "userquota/groupquota"));
975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
976 "'%s' must be a number"), propname);
977 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
982 * Encode the prop name as
983 * userquota@<hex-rid>-domain, to make it easy
984 * for the kernel to decode.
986 (void) snprintf(newpropname, sizeof (newpropname),
987 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
988 (longlong_t)rid, domain);
992 if (nvlist_add_uint64_array(ret, newpropname,
994 (void) no_memory(hdl);
998 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1000 "'%s' is readonly"),
1002 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1006 if (prop == ZPROP_INVAL) {
1007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1008 "invalid property '%s'"), propname);
1009 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1013 if (!zfs_prop_valid_for_type(prop, type)) {
1015 dgettext(TEXT_DOMAIN, "'%s' does not "
1016 "apply to datasets of this type"), propname);
1017 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1021 if (zfs_prop_readonly(prop) &&
1022 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1024 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1026 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1030 if (zprop_parse_value(hdl, elem, prop, type, ret,
1031 &strval, &intval, errbuf) != 0)
1035 * Perform some additional checks for specific properties.
1038 case ZFS_PROP_VERSION:
1044 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1045 if (intval < version) {
1046 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1047 "Can not downgrade; already at version %u"),
1049 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1055 case ZFS_PROP_VOLBLOCKSIZE:
1056 case ZFS_PROP_RECORDSIZE:
1058 int maxbs = SPA_MAXBLOCKSIZE;
1059 if (zpool_hdl != NULL) {
1060 maxbs = zpool_get_prop_int(zpool_hdl,
1061 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1064 * Volumes are limited to a volblocksize of 128KB,
1065 * because they typically service workloads with
1066 * small random writes, which incur a large performance
1067 * penalty with large blocks.
1069 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1070 maxbs = SPA_OLD_MAXBLOCKSIZE;
1072 * The value must be a power of two between
1073 * SPA_MINBLOCKSIZE and maxbs.
1075 if (intval < SPA_MINBLOCKSIZE ||
1076 intval > maxbs || !ISP2(intval)) {
1077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1078 "'%s' must be power of 2 from 512B "
1079 "to %uKB"), propname, maxbs >> 10);
1080 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1085 case ZFS_PROP_MLSLABEL:
1089 * Verify the mlslabel string and convert to
1090 * internal hex label string.
1094 char *hex = NULL; /* internal label string */
1096 /* Default value is already OK. */
1097 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1100 /* Verify the label can be converted to binary form */
1101 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1102 (str_to_label(strval, &new_sl, MAC_LABEL,
1103 L_NO_CORRECTION, NULL) == -1)) {
1107 /* Now translate to hex internal label string */
1108 if (label_to_str(new_sl, &hex, M_INTERNAL,
1114 m_label_free(new_sl);
1116 /* If string is already in internal form, we're done. */
1117 if (strcmp(strval, hex) == 0) {
1122 /* Replace the label string with the internal form. */
1123 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1125 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1133 "invalid mlslabel '%s'"), strval);
1134 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1135 m_label_free(new_sl); /* OK if null */
1136 #else /* !illumos */
1137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1138 "mlslabel is not supported on FreeBSD"));
1139 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1140 #endif /* illumos */
1145 case ZFS_PROP_MOUNTPOINT:
1147 namecheck_err_t why;
1149 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1150 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1153 if (mountpoint_namecheck(strval, &why)) {
1155 case NAME_ERR_LEADING_SLASH:
1157 dgettext(TEXT_DOMAIN,
1158 "'%s' must be an absolute path, "
1159 "'none', or 'legacy'"), propname);
1161 case NAME_ERR_TOOLONG:
1163 dgettext(TEXT_DOMAIN,
1164 "component of '%s' is too long"),
1170 dgettext(TEXT_DOMAIN,
1171 "(%d) not defined"),
1175 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1182 case ZFS_PROP_SHARESMB:
1183 case ZFS_PROP_SHARENFS:
1185 * For the mountpoint and sharenfs or sharesmb
1186 * properties, check if it can be set in a
1187 * global/non-global zone based on
1188 * the zoned property value:
1190 * global zone non-global zone
1191 * --------------------------------------------------
1192 * zoned=on mountpoint (no) mountpoint (yes)
1193 * sharenfs (no) sharenfs (no)
1194 * sharesmb (no) sharesmb (no)
1196 * zoned=off mountpoint (yes) N/A
1201 if (getzoneid() == GLOBAL_ZONEID) {
1202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1203 "'%s' cannot be set on "
1204 "dataset in a non-global zone"),
1206 (void) zfs_error(hdl, EZFS_ZONED,
1209 } else if (prop == ZFS_PROP_SHARENFS ||
1210 prop == ZFS_PROP_SHARESMB) {
1211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1212 "'%s' cannot be set in "
1213 "a non-global zone"), propname);
1214 (void) zfs_error(hdl, EZFS_ZONED,
1218 } else if (getzoneid() != GLOBAL_ZONEID) {
1220 * If zoned property is 'off', this must be in
1221 * a global zone. If not, something is wrong.
1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224 "'%s' cannot be set while dataset "
1225 "'zoned' property is set"), propname);
1226 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1231 * At this point, it is legitimate to set the
1232 * property. Now we want to make sure that the
1233 * property value is valid if it is sharenfs.
1235 if ((prop == ZFS_PROP_SHARENFS ||
1236 prop == ZFS_PROP_SHARESMB) &&
1237 strcmp(strval, "on") != 0 &&
1238 strcmp(strval, "off") != 0) {
1239 zfs_share_proto_t proto;
1241 if (prop == ZFS_PROP_SHARESMB)
1247 * Must be an valid sharing protocol
1248 * option string so init the libshare
1249 * in order to enable the parser and
1250 * then parse the options. We use the
1251 * control API since we don't care about
1252 * the current configuration and don't
1253 * want the overhead of loading it
1254 * until we actually do something.
1257 if (zfs_init_libshare(hdl,
1258 SA_INIT_CONTROL_API) != SA_OK) {
1260 * An error occurred so we can't do
1263 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1264 "'%s' cannot be set: problem "
1265 "in share initialization"),
1267 (void) zfs_error(hdl, EZFS_BADPROP,
1272 if (zfs_parse_options(strval, proto) != SA_OK) {
1274 * There was an error in parsing so
1275 * deal with it by issuing an error
1276 * message and leaving after
1277 * uninitializing the the libshare
1280 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1281 "'%s' cannot be set to invalid "
1282 "options"), propname);
1283 (void) zfs_error(hdl, EZFS_BADPROP,
1285 zfs_uninit_libshare(hdl);
1288 zfs_uninit_libshare(hdl);
1293 case ZFS_PROP_UTF8ONLY:
1294 chosen_utf = (int)intval;
1297 case ZFS_PROP_NORMALIZE:
1298 chosen_normal = (int)intval;
1306 * For changes to existing volumes, we have some additional
1307 * checks to enforce.
1309 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1310 uint64_t volsize = zfs_prop_get_int(zhp,
1312 uint64_t blocksize = zfs_prop_get_int(zhp,
1313 ZFS_PROP_VOLBLOCKSIZE);
1317 case ZFS_PROP_RESERVATION:
1318 case ZFS_PROP_REFRESERVATION:
1319 if (intval > volsize) {
1320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1321 "'%s' is greater than current "
1322 "volume size"), propname);
1323 (void) zfs_error(hdl, EZFS_BADPROP,
1329 case ZFS_PROP_VOLSIZE:
1330 if (intval % blocksize != 0) {
1331 zfs_nicenum(blocksize, buf,
1333 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1334 "'%s' must be a multiple of "
1335 "volume block size (%s)"),
1337 (void) zfs_error(hdl, EZFS_BADPROP,
1343 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1344 "'%s' cannot be zero"),
1346 (void) zfs_error(hdl, EZFS_BADPROP,
1359 * If normalization was chosen, but no UTF8 choice was made,
1360 * enforce rejection of non-UTF8 names.
1362 * If normalization was chosen, but rejecting non-UTF8 names
1363 * was explicitly not chosen, it is an error.
1365 if (chosen_normal > 0 && chosen_utf < 0) {
1366 if (nvlist_add_uint64(ret,
1367 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1368 (void) no_memory(hdl);
1371 } else if (chosen_normal > 0 && chosen_utf == 0) {
1372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1373 "'%s' must be set 'on' if normalization chosen"),
1374 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1375 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1386 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1388 uint64_t old_volsize;
1389 uint64_t new_volsize;
1390 uint64_t old_reservation;
1391 uint64_t new_reservation;
1392 zfs_prop_t resv_prop;
1396 * If this is an existing volume, and someone is setting the volsize,
1397 * make sure that it matches the reservation, or add it if necessary.
1399 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1400 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1402 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1404 props = fnvlist_alloc();
1405 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1406 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1408 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1409 old_reservation) || nvlist_exists(nvl,
1410 zfs_prop_to_name(resv_prop))) {
1411 fnvlist_free(props);
1414 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1415 &new_volsize) != 0) {
1416 fnvlist_free(props);
1419 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1420 fnvlist_free(props);
1422 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1423 new_reservation) != 0) {
1424 (void) no_memory(zhp->zfs_hdl);
1431 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1438 * For quotas and reservations, ENOSPC indicates
1439 * something different; setting a quota or reservation
1440 * doesn't use any disk space.
1443 case ZFS_PROP_QUOTA:
1444 case ZFS_PROP_REFQUOTA:
1445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1446 "size is less than current used or "
1448 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1451 case ZFS_PROP_RESERVATION:
1452 case ZFS_PROP_REFRESERVATION:
1453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1454 "size is greater than available space"));
1455 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1459 (void) zfs_standard_error(hdl, err, errbuf);
1465 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1469 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1474 "property value too long"));
1475 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1480 "pool and or dataset must be upgraded to set this "
1481 "property or value"));
1482 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1487 if (prop == ZFS_PROP_COMPRESSION ||
1488 prop == ZFS_PROP_RECORDSIZE) {
1489 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1490 "property setting is not allowed on "
1491 "bootable datasets"));
1492 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1493 } else if (prop == ZFS_PROP_CHECKSUM ||
1494 prop == ZFS_PROP_DEDUP) {
1495 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1496 "property setting is not allowed on "
1498 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1500 (void) zfs_standard_error(hdl, err, errbuf);
1505 if (prop == ZPROP_INVAL) {
1506 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1508 (void) zfs_standard_error(hdl, err, errbuf);
1514 * This platform can't address a volume this big.
1517 if (prop == ZFS_PROP_VOLSIZE) {
1518 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1524 (void) zfs_standard_error(hdl, err, errbuf);
1529 * Given a property name and value, set the property for the given dataset.
1532 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1536 libzfs_handle_t *hdl = zhp->zfs_hdl;
1537 nvlist_t *nvl = NULL;
1539 (void) snprintf(errbuf, sizeof (errbuf),
1540 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1543 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1544 nvlist_add_string(nvl, propname, propval) != 0) {
1545 (void) no_memory(hdl);
1549 ret = zfs_prop_set_list(zhp, nvl);
1559 * Given an nvlist of property names and values, set the properties for the
1563 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1565 zfs_cmd_t zc = { 0 };
1567 prop_changelist_t **cls = NULL;
1570 libzfs_handle_t *hdl = zhp->zfs_hdl;
1575 (void) snprintf(errbuf, sizeof (errbuf),
1576 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1579 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1580 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1585 * We have to check for any extra properties which need to be added
1586 * before computing the length of the nvlist.
1588 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1590 elem = nvlist_next_nvpair(nvl, elem)) {
1591 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1592 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1597 * Check how many properties we're setting and allocate an array to
1598 * store changelist pointers for postfix().
1601 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1603 elem = nvlist_next_nvpair(nvl, elem))
1605 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1609 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1611 elem = nvlist_next_nvpair(nvl, elem)) {
1613 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1615 assert(cl_idx < nvl_len);
1617 * We don't want to unmount & remount the dataset when changing
1618 * its canmount property. We only use the changelist logic to
1619 * unmount when setting canmount=off for a mounted filesystem
1620 * or when setting canmount=on for an unmounted filesystem.
1621 * For all other changes to canmount property the filesystem
1624 if (prop != ZFS_PROP_CANMOUNT ||
1625 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1626 zfs_is_mounted(zhp, NULL)) ||
1627 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_ON &&
1628 !zfs_is_mounted(zhp, NULL))) {
1629 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1630 if (cls[cl_idx] == NULL)
1634 if (prop == ZFS_PROP_MOUNTPOINT &&
1635 changelist_haszonedchild(cls[cl_idx])) {
1636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1637 "child dataset with inherited mountpoint is used "
1638 "in a non-global zone"));
1639 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1643 /* We don't support those properties on FreeBSD. */
1645 case ZFS_PROP_DEVICES:
1646 case ZFS_PROP_ISCSIOPTIONS:
1647 case ZFS_PROP_XATTR:
1648 case ZFS_PROP_VSCAN:
1649 case ZFS_PROP_NBMAND:
1650 case ZFS_PROP_MLSLABEL:
1651 (void) snprintf(errbuf, sizeof (errbuf),
1652 "property '%s' not supported on FreeBSD",
1654 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1658 if (cls[cl_idx] != NULL &&
1659 (ret = changelist_prefix(cls[cl_idx])) != 0)
1664 assert(cl_idx == nvl_len);
1667 * Execute the corresponding ioctl() to set this list of properties.
1669 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1671 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1672 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1675 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1678 /* Get the list of unset properties back and report them. */
1679 nvlist_t *errorprops = NULL;
1680 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1682 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1684 elem = nvlist_next_nvpair(nvl, elem)) {
1685 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1686 zfs_setprop_error(hdl, prop, errno, errbuf);
1688 nvlist_free(errorprops);
1690 if (added_resv && errno == ENOSPC) {
1691 /* clean up the volsize property we tried to set */
1692 uint64_t old_volsize = zfs_prop_get_int(zhp,
1696 zcmd_free_nvlists(&zc);
1698 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1700 if (nvlist_add_uint64(nvl,
1701 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1704 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1706 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1709 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1710 if (cls[cl_idx] != NULL) {
1711 int clp_err = changelist_postfix(cls[cl_idx]);
1718 * Refresh the statistics so the new property value
1722 (void) get_stats(zhp);
1727 zcmd_free_nvlists(&zc);
1729 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1730 if (cls[cl_idx] != NULL)
1731 changelist_free(cls[cl_idx]);
1739 * Given a property, inherit the value from the parent dataset, or if received
1740 * is TRUE, revert to the received value, if any.
1743 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1745 zfs_cmd_t zc = { 0 };
1747 prop_changelist_t *cl;
1748 libzfs_handle_t *hdl = zhp->zfs_hdl;
1752 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1753 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1755 zc.zc_cookie = received;
1756 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1758 * For user properties, the amount of work we have to do is very
1759 * small, so just do it here.
1761 if (!zfs_prop_user(propname)) {
1762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1763 "invalid property"));
1764 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1767 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1768 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1770 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1771 return (zfs_standard_error(hdl, errno, errbuf));
1777 * Verify that this property is inheritable.
1779 if (zfs_prop_readonly(prop))
1780 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1782 if (!zfs_prop_inheritable(prop) && !received)
1783 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1786 * Check to see if the value applies to this type
1788 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1789 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1792 * Normalize the name, to get rid of shorthand abbreviations.
1794 propname = zfs_prop_to_name(prop);
1795 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1796 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1798 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1799 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1801 "dataset is used in a non-global zone"));
1802 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1806 * Determine datasets which will be affected by this change, if any.
1808 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1811 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1812 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1813 "child dataset with inherited mountpoint is used "
1814 "in a non-global zone"));
1815 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1819 if ((ret = changelist_prefix(cl)) != 0)
1822 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1823 return (zfs_standard_error(hdl, errno, errbuf));
1826 if ((ret = changelist_postfix(cl)) != 0)
1830 * Refresh the statistics so the new property is reflected.
1832 (void) get_stats(zhp);
1836 changelist_free(cl);
1841 * True DSL properties are stored in an nvlist. The following two functions
1842 * extract them appropriately.
1845 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1851 if (nvlist_lookup_nvlist(zhp->zfs_props,
1852 zfs_prop_to_name(prop), &nv) == 0) {
1853 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1854 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1856 verify(!zhp->zfs_props_table ||
1857 zhp->zfs_props_table[prop] == B_TRUE);
1858 value = zfs_prop_default_numeric(prop);
1866 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1872 if (nvlist_lookup_nvlist(zhp->zfs_props,
1873 zfs_prop_to_name(prop), &nv) == 0) {
1874 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1875 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1877 verify(!zhp->zfs_props_table ||
1878 zhp->zfs_props_table[prop] == B_TRUE);
1879 value = zfs_prop_default_string(prop);
1887 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1889 return (zhp->zfs_props == zhp->zfs_recvd_props);
1893 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1895 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1896 zhp->zfs_props = zhp->zfs_recvd_props;
1900 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1902 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1907 * Internal function for getting a numeric property. Both zfs_prop_get() and
1908 * zfs_prop_get_int() are built using this interface.
1910 * Certain properties can be overridden using 'mount -o'. In this case, scan
1911 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1912 * If they differ from the on-disk values, report the current values and mark
1913 * the source "temporary".
1916 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1917 char **source, uint64_t *val)
1919 zfs_cmd_t zc = { 0 };
1920 nvlist_t *zplprops = NULL;
1922 char *mntopt_on = NULL;
1923 char *mntopt_off = NULL;
1924 boolean_t received = zfs_is_recvd_props_mode(zhp);
1929 case ZFS_PROP_ATIME:
1930 mntopt_on = MNTOPT_ATIME;
1931 mntopt_off = MNTOPT_NOATIME;
1934 case ZFS_PROP_DEVICES:
1935 mntopt_on = MNTOPT_DEVICES;
1936 mntopt_off = MNTOPT_NODEVICES;
1940 mntopt_on = MNTOPT_EXEC;
1941 mntopt_off = MNTOPT_NOEXEC;
1944 case ZFS_PROP_READONLY:
1945 mntopt_on = MNTOPT_RO;
1946 mntopt_off = MNTOPT_RW;
1949 case ZFS_PROP_SETUID:
1950 mntopt_on = MNTOPT_SETUID;
1951 mntopt_off = MNTOPT_NOSETUID;
1954 case ZFS_PROP_XATTR:
1955 mntopt_on = MNTOPT_XATTR;
1956 mntopt_off = MNTOPT_NOXATTR;
1959 case ZFS_PROP_NBMAND:
1960 mntopt_on = MNTOPT_NBMAND;
1961 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 name of the parent pool for the given zfs handle.
2957 zfs_get_pool_name(const zfs_handle_t *zhp)
2959 return (zhp->zpool_hdl->zpool_name);
2963 * Returns the type of the given zfs handle.
2966 zfs_get_type(const zfs_handle_t *zhp)
2968 return (zhp->zfs_type);
2972 * Is one dataset name a child dataset of another?
2974 * Needs to handle these cases:
2975 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2976 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2977 * Descendant? No. No. No. Yes.
2980 is_descendant(const char *ds1, const char *ds2)
2982 size_t d1len = strlen(ds1);
2984 /* ds2 can't be a descendant if it's smaller */
2985 if (strlen(ds2) < d1len)
2988 /* otherwise, compare strings and verify that there's a '/' char */
2989 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2993 * Given a complete name, return just the portion that refers to the parent.
2994 * Will return -1 if there is no parent (path is just the name of the
2998 parent_name(const char *path, char *buf, size_t buflen)
3002 (void) strlcpy(buf, path, buflen);
3004 if ((slashp = strrchr(buf, '/')) == NULL)
3012 * If accept_ancestor is false, then check to make sure that the given path has
3013 * a parent, and that it exists. If accept_ancestor is true, then find the
3014 * closest existing ancestor for the given path. In prefixlen return the
3015 * length of already existing prefix of the given path. We also fetch the
3016 * 'zoned' property, which is used to validate property settings when creating
3020 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3021 boolean_t accept_ancestor, int *prefixlen)
3023 zfs_cmd_t zc = { 0 };
3024 char parent[ZFS_MAXNAMELEN];
3030 (void) snprintf(errbuf, sizeof (errbuf),
3031 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3033 /* get parent, and check to see if this is just a pool */
3034 if (parent_name(path, parent, sizeof (parent)) != 0) {
3035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3036 "missing dataset name"));
3037 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3040 /* check to see if the pool exists */
3041 if ((slash = strchr(parent, '/')) == NULL)
3042 slash = parent + strlen(parent);
3043 (void) strncpy(zc.zc_name, parent, slash - parent);
3044 zc.zc_name[slash - parent] = '\0';
3045 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3048 "no such pool '%s'"), zc.zc_name);
3049 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3052 /* check to see if the parent dataset exists */
3053 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3054 if (errno == ENOENT && accept_ancestor) {
3056 * Go deeper to find an ancestor, give up on top level.
3058 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3060 "no such pool '%s'"), zc.zc_name);
3061 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3063 } else if (errno == ENOENT) {
3064 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3065 "parent does not exist"));
3066 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3068 return (zfs_standard_error(hdl, errno, errbuf));
3071 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3075 /* we are in a non-global zone, but parent is in the global zone */
3076 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3077 (void) zfs_standard_error(hdl, EPERM, errbuf);
3082 /* make sure parent is a filesystem */
3083 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3085 "parent is not a filesystem"));
3086 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3092 if (prefixlen != NULL)
3093 *prefixlen = strlen(parent);
3098 * Finds whether the dataset of the given type(s) exists.
3101 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3105 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3109 * Try to get stats for the dataset, which will tell us if it exists.
3111 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3112 int ds_type = zhp->zfs_type;
3115 if (types & ds_type)
3122 * Given a path to 'target', create all the ancestors between
3123 * the prefixlen portion of the path, and the target itself.
3124 * Fail if the initial prefixlen-ancestor does not already exist.
3127 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3133 /* make sure prefix exists */
3134 cp = target + prefixlen;
3136 assert(strchr(cp, '/') == NULL);
3137 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3140 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3148 * Attempt to create, mount, and share any ancestor filesystems,
3149 * up to the prefixlen-long one.
3151 for (cp = target + prefixlen + 1;
3152 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3156 h = make_dataset_handle(hdl, target);
3158 /* it already exists, nothing to do here */
3163 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3165 opname = dgettext(TEXT_DOMAIN, "create");
3169 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3171 opname = dgettext(TEXT_DOMAIN, "open");
3175 if (zfs_mount(h, NULL, 0) != 0) {
3176 opname = dgettext(TEXT_DOMAIN, "mount");
3180 if (zfs_share(h) != 0) {
3181 opname = dgettext(TEXT_DOMAIN, "share");
3191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3192 "failed to %s ancestor '%s'"), opname, target);
3197 * Creates non-existing ancestors of the given path.
3200 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3206 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3209 if ((path_copy = strdup(path)) != NULL) {
3210 rc = create_parents(hdl, path_copy, prefix);
3213 if (path_copy == NULL || rc != 0)
3220 * Create a new filesystem or volume.
3223 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3228 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3231 enum lzc_dataset_type ost;
3233 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3234 "cannot create '%s'"), path);
3236 /* validate the path, taking care to note the extended error message */
3237 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3238 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3240 /* validate parents exist */
3241 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3245 * The failure modes when creating a dataset of a different type over
3246 * one that already exists is a little strange. In particular, if you
3247 * try to create a dataset on top of an existing dataset, the ioctl()
3248 * will return ENOENT, not EEXIST. To prevent this from happening, we
3249 * first try to see if the dataset exists.
3251 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3253 "dataset already exists"));
3254 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3257 if (type == ZFS_TYPE_VOLUME)
3258 ost = LZC_DATSET_TYPE_ZVOL;
3260 ost = LZC_DATSET_TYPE_ZFS;
3262 /* open zpool handle for prop validation */
3263 char pool_path[MAXNAMELEN];
3264 (void) strlcpy(pool_path, path, sizeof (pool_path));
3266 /* truncate pool_path at first slash */
3267 char *p = strchr(pool_path, '/');
3271 zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
3273 if (props && (props = zfs_valid_proplist(hdl, type, props,
3274 zoned, NULL, zpool_handle, errbuf)) == 0) {
3275 zpool_close(zpool_handle);
3278 zpool_close(zpool_handle);
3280 if (type == ZFS_TYPE_VOLUME) {
3282 * If we are creating a volume, the size and block size must
3283 * satisfy a few restraints. First, the blocksize must be a
3284 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3285 * volsize must be a multiple of the block size, and cannot be
3288 if (props == NULL || nvlist_lookup_uint64(props,
3289 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3291 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3292 "missing volume size"));
3293 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3296 if ((ret = nvlist_lookup_uint64(props,
3297 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3298 &blocksize)) != 0) {
3299 if (ret == ENOENT) {
3300 blocksize = zfs_prop_default_numeric(
3301 ZFS_PROP_VOLBLOCKSIZE);
3304 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3305 "missing volume block size"));
3306 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3312 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3313 "volume size cannot be zero"));
3314 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3317 if (size % blocksize != 0) {
3319 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3320 "volume size must be a multiple of volume block "
3322 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3326 /* create the dataset */
3327 ret = lzc_create(path, ost, props);
3330 /* check for failure */
3332 char parent[ZFS_MAXNAMELEN];
3333 (void) parent_name(path, parent, sizeof (parent));
3337 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3338 "no such parent '%s'"), parent);
3339 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3343 "parent '%s' is not a filesystem"), parent);
3344 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3348 "pool must be upgraded to set this "
3349 "property or value"));
3350 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3354 * This platform can't address a volume this big.
3356 if (type == ZFS_TYPE_VOLUME)
3357 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3362 return (zfs_standard_error(hdl, errno, errbuf));
3370 * Destroys the given dataset. The caller must make sure that the filesystem
3371 * isn't mounted, and that there are no active dependents. If the file system
3372 * does not exist this function does nothing.
3375 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3377 zfs_cmd_t zc = { 0 };
3379 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3380 nvlist_t *nv = fnvlist_alloc();
3381 fnvlist_add_boolean(nv, zhp->zfs_name);
3382 int error = lzc_destroy_bookmarks(nv, NULL);
3385 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3386 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3392 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3394 if (ZFS_IS_VOLUME(zhp)) {
3395 zc.zc_objset_type = DMU_OST_ZVOL;
3397 zc.zc_objset_type = DMU_OST_ZFS;
3400 zc.zc_defer_destroy = defer;
3401 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3403 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3404 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3408 remove_mountpoint(zhp);
3413 struct destroydata {
3415 const char *snapname;
3419 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3421 struct destroydata *dd = arg;
3422 char name[ZFS_MAXNAMELEN];
3425 (void) snprintf(name, sizeof (name),
3426 "%s@%s", zhp->zfs_name, dd->snapname);
3428 if (lzc_exists(name))
3429 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3431 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3437 * Destroys all snapshots with the given name in zhp & descendants.
3440 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3443 struct destroydata dd = { 0 };
3445 dd.snapname = snapname;
3446 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3447 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3449 if (nvlist_empty(dd.nvl)) {
3450 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3451 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3452 zhp->zfs_name, snapname);
3454 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3456 nvlist_free(dd.nvl);
3461 * Destroys all the snapshots named in the nvlist.
3464 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3467 nvlist_t *errlist = NULL;
3469 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3472 nvlist_free(errlist);
3476 if (nvlist_empty(errlist)) {
3478 (void) snprintf(errbuf, sizeof (errbuf),
3479 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3481 ret = zfs_standard_error(hdl, ret, errbuf);
3483 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3484 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3486 (void) snprintf(errbuf, sizeof (errbuf),
3487 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3490 switch (fnvpair_value_int32(pair)) {
3493 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3494 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3497 ret = zfs_standard_error(hdl, errno, errbuf);
3502 nvlist_free(errlist);
3507 * Clones the given dataset. The target must be of the same type as the source.
3510 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3512 char parent[ZFS_MAXNAMELEN];
3515 libzfs_handle_t *hdl = zhp->zfs_hdl;
3518 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3520 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3521 "cannot create '%s'"), target);
3523 /* validate the target/clone name */
3524 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3525 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3527 /* validate parents exist */
3528 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3531 (void) parent_name(target, parent, sizeof (parent));
3537 if (ZFS_IS_VOLUME(zhp)) {
3538 type = ZFS_TYPE_VOLUME;
3540 type = ZFS_TYPE_FILESYSTEM;
3542 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3543 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3547 ret = lzc_clone(target, zhp->zfs_name, props);
3555 * The parent doesn't exist. We should have caught this
3556 * above, but there may a race condition that has since
3557 * destroyed the parent.
3559 * At this point, we don't know whether it's the source
3560 * that doesn't exist anymore, or whether the target
3561 * dataset doesn't exist.
3563 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3564 "no such parent '%s'"), parent);
3565 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3568 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3569 "source and target pools differ"));
3570 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3574 return (zfs_standard_error(zhp->zfs_hdl, errno,
3583 * Promotes the given clone fs to be the clone parent.
3586 zfs_promote(zfs_handle_t *zhp)
3588 libzfs_handle_t *hdl = zhp->zfs_hdl;
3589 zfs_cmd_t zc = { 0 };
3590 char parent[MAXPATHLEN];
3594 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3595 "cannot promote '%s'"), zhp->zfs_name);
3597 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3598 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3599 "snapshots can not be promoted"));
3600 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3603 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3604 if (parent[0] == '\0') {
3605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3606 "not a cloned filesystem"));
3607 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3610 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3611 sizeof (zc.zc_value));
3612 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3613 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3616 int save_errno = errno;
3618 switch (save_errno) {
3620 /* There is a conflicting snapshot name. */
3621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3622 "conflicting snapshot '%s' from parent '%s'"),
3623 zc.zc_string, parent);
3624 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3627 return (zfs_standard_error(hdl, save_errno, errbuf));
3633 typedef struct snapdata {
3635 const char *sd_snapname;
3639 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3641 snapdata_t *sd = arg;
3642 char name[ZFS_MAXNAMELEN];
3645 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3646 (void) snprintf(name, sizeof (name),
3647 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3649 fnvlist_add_boolean(sd->sd_nvl, name);
3651 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3659 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3663 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3670 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3671 "cannot create snapshots "));
3674 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3675 const char *snapname = nvpair_name(elem);
3677 /* validate the target name */
3678 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3680 (void) snprintf(errbuf, sizeof (errbuf),
3681 dgettext(TEXT_DOMAIN,
3682 "cannot create snapshot '%s'"), snapname);
3683 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3688 * get pool handle for prop validation. assumes all snaps are in the
3689 * same pool, as does lzc_snapshot (below).
3691 char pool[MAXNAMELEN];
3692 elem = nvlist_next_nvpair(snaps, NULL);
3693 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3694 pool[strcspn(pool, "/@")] = '\0';
3695 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3697 if (props != NULL &&
3698 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3699 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3700 zpool_close(zpool_hdl);
3703 zpool_close(zpool_hdl);
3705 ret = lzc_snapshot(snaps, props, &errors);
3708 boolean_t printed = B_FALSE;
3709 for (elem = nvlist_next_nvpair(errors, NULL);
3711 elem = nvlist_next_nvpair(errors, elem)) {
3712 (void) snprintf(errbuf, sizeof (errbuf),
3713 dgettext(TEXT_DOMAIN,
3714 "cannot create snapshot '%s'"), nvpair_name(elem));
3715 (void) zfs_standard_error(hdl,
3716 fnvpair_value_int32(elem), errbuf);
3722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3723 "multiple snapshots of same "
3725 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3729 (void) zfs_standard_error(hdl, ret, errbuf);
3735 nvlist_free(errors);
3740 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3744 snapdata_t sd = { 0 };
3745 char fsname[ZFS_MAXNAMELEN];
3750 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3751 "cannot snapshot %s"), path);
3753 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3754 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3756 (void) strlcpy(fsname, path, sizeof (fsname));
3757 cp = strchr(fsname, '@');
3759 sd.sd_snapname = cp + 1;
3761 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3762 ZFS_TYPE_VOLUME)) == NULL) {
3766 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3768 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3770 fnvlist_add_boolean(sd.sd_nvl, path);
3773 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3774 nvlist_free(sd.sd_nvl);
3780 * Destroy any more recent snapshots. We invoke this callback on any dependents
3781 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3782 * is a dependent and we should just destroy it without checking the transaction
3785 typedef struct rollback_data {
3786 const char *cb_target; /* the snapshot */
3787 uint64_t cb_create; /* creation time reference */
3793 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3795 rollback_data_t *cbp = data;
3796 prop_changelist_t *clp;
3798 /* We must destroy this clone; first unmount it */
3799 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3800 cbp->cb_force ? MS_FORCE: 0);
3801 if (clp == NULL || changelist_prefix(clp) != 0) {
3802 cbp->cb_error = B_TRUE;
3806 if (zfs_destroy(zhp, B_FALSE) != 0)
3807 cbp->cb_error = B_TRUE;
3809 changelist_remove(clp, zhp->zfs_name);
3810 (void) changelist_postfix(clp);
3811 changelist_free(clp);
3818 rollback_destroy(zfs_handle_t *zhp, void *data)
3820 rollback_data_t *cbp = data;
3822 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3823 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3824 rollback_destroy_dependent, cbp);
3826 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3834 * Given a dataset, rollback to a specific snapshot, discarding any
3835 * data changes since then and making it the active dataset.
3837 * Any snapshots and bookmarks more recent than the target are
3838 * destroyed, along with their dependents (i.e. clones).
3841 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3843 rollback_data_t cb = { 0 };
3845 boolean_t restore_resv = 0;
3846 uint64_t old_volsize, new_volsize;
3847 zfs_prop_t resv_prop;
3849 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3850 zhp->zfs_type == ZFS_TYPE_VOLUME);
3853 * Destroy all recent snapshots and their dependents.
3855 cb.cb_force = force;
3856 cb.cb_target = snap->zfs_name;
3857 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3858 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3859 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3865 * Now that we have verified that the snapshot is the latest,
3866 * rollback to the given snapshot.
3869 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3870 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3872 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3874 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3878 * We rely on zfs_iter_children() to verify that there are no
3879 * newer snapshots for the given dataset. Therefore, we can
3880 * simply pass the name on to the ioctl() call. There is still
3881 * an unlikely race condition where the user has taken a
3882 * snapshot since we verified that this was the most recent.
3884 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3886 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3887 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3893 * For volumes, if the pre-rollback volsize matched the pre-
3894 * rollback reservation and the volsize has changed then set
3895 * the reservation property to the post-rollback volsize.
3896 * Make a new handle since the rollback closed the dataset.
3898 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3899 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3901 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3902 if (old_volsize != new_volsize)
3903 err = zfs_prop_set_int(zhp, resv_prop,
3912 * Renames the given dataset.
3915 zfs_rename(zfs_handle_t *zhp, const char *source, const char *target,
3916 renameflags_t flags)
3919 zfs_cmd_t zc = { 0 };
3921 prop_changelist_t *cl = NULL;
3922 zfs_handle_t *zhrp = NULL;
3923 char *parentname = NULL;
3924 char parent[ZFS_MAXNAMELEN];
3925 char property[ZFS_MAXPROPLEN];
3926 libzfs_handle_t *hdl = zhp->zfs_hdl;
3929 /* if we have the same exact name, just return success */
3930 if (strcmp(zhp->zfs_name, target) == 0)
3933 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3934 "cannot rename to '%s'"), target);
3936 if (source != NULL) {
3938 * This is recursive snapshots rename, put snapshot name
3939 * (that might not exist) into zfs_name.
3941 assert(flags.recurse);
3943 (void) strlcat(zhp->zfs_name, "@", sizeof(zhp->zfs_name));
3944 (void) strlcat(zhp->zfs_name, source, sizeof(zhp->zfs_name));
3945 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
3949 * Make sure the target name is valid
3951 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3952 if ((strchr(target, '@') == NULL) ||
3955 * Snapshot target name is abbreviated,
3956 * reconstruct full dataset name
3958 (void) strlcpy(parent, zhp->zfs_name,
3960 delim = strchr(parent, '@');
3961 if (strchr(target, '@') == NULL)
3965 (void) strlcat(parent, target, sizeof (parent));
3969 * Make sure we're renaming within the same dataset.
3971 delim = strchr(target, '@');
3972 if (strncmp(zhp->zfs_name, target, delim - target)
3973 != 0 || zhp->zfs_name[delim - target] != '@') {
3974 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3975 "snapshots must be part of same "
3977 return (zfs_error(hdl, EZFS_CROSSTARGET,
3981 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3982 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3984 if (flags.recurse) {
3985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3986 "recursive rename must be a snapshot"));
3987 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3990 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3991 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3993 /* validate parents */
3994 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3997 /* make sure we're in the same pool */
3998 verify((delim = strchr(target, '/')) != NULL);
3999 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4000 zhp->zfs_name[delim - target] != '/') {
4001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4002 "datasets must be within same pool"));
4003 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4006 /* new name cannot be a child of the current dataset name */
4007 if (is_descendant(zhp->zfs_name, target)) {
4008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4009 "New dataset name cannot be a descendant of "
4010 "current dataset name"));
4011 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4015 (void) snprintf(errbuf, sizeof (errbuf),
4016 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4018 if (getzoneid() == GLOBAL_ZONEID &&
4019 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4020 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4021 "dataset is used in a non-global zone"));
4022 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4026 * Avoid unmounting file systems with mountpoint property set to
4027 * 'legacy' or 'none' even if -u option is not given.
4029 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4030 !flags.recurse && !flags.nounmount &&
4031 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4032 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4033 (strcmp(property, "legacy") == 0 ||
4034 strcmp(property, "none") == 0)) {
4035 flags.nounmount = B_TRUE;
4037 if (flags.recurse) {
4039 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4040 if (parentname == NULL) {
4044 delim = strchr(parentname, '@');
4046 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4051 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4052 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4053 flags.nounmount ? CL_GATHER_DONT_UNMOUNT : 0,
4054 flags.forceunmount ? MS_FORCE : 0)) == NULL) {
4058 if (changelist_haszonedchild(cl)) {
4059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4060 "child dataset with inherited mountpoint is used "
4061 "in a non-global zone"));
4062 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4066 if ((ret = changelist_prefix(cl)) != 0)
4070 if (ZFS_IS_VOLUME(zhp))
4071 zc.zc_objset_type = DMU_OST_ZVOL;
4073 zc.zc_objset_type = DMU_OST_ZFS;
4075 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4076 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4078 zc.zc_cookie = flags.recurse ? 1 : 0;
4079 if (flags.nounmount)
4082 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4084 * if it was recursive, the one that actually failed will
4087 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4088 "cannot rename '%s'"), zc.zc_name);
4090 if (flags.recurse && errno == EEXIST) {
4091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4092 "a child dataset already has a snapshot "
4093 "with the new name"));
4094 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4096 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4100 * On failure, we still want to remount any filesystems that
4101 * were previously mounted, so we don't alter the system state.
4104 (void) changelist_postfix(cl);
4107 changelist_rename(cl, zfs_get_name(zhp), target);
4108 ret = changelist_postfix(cl);
4113 if (parentname != NULL) {
4120 changelist_free(cl);
4126 zfs_get_user_props(zfs_handle_t *zhp)
4128 return (zhp->zfs_user_props);
4132 zfs_get_recvd_props(zfs_handle_t *zhp)
4134 if (zhp->zfs_recvd_props == NULL)
4135 if (get_recvd_props_ioctl(zhp) != 0)
4137 return (zhp->zfs_recvd_props);
4141 * This function is used by 'zfs list' to determine the exact set of columns to
4142 * display, and their maximum widths. This does two main things:
4144 * - If this is a list of all properties, then expand the list to include
4145 * all native properties, and set a flag so that for each dataset we look
4146 * for new unique user properties and add them to the list.
4148 * - For non fixed-width properties, keep track of the maximum width seen
4149 * so that we can size the column appropriately. If the user has
4150 * requested received property values, we also need to compute the width
4151 * of the RECEIVED column.
4154 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4157 libzfs_handle_t *hdl = zhp->zfs_hdl;
4158 zprop_list_t *entry;
4159 zprop_list_t **last, **start;
4160 nvlist_t *userprops, *propval;
4163 char buf[ZFS_MAXPROPLEN];
4165 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4168 userprops = zfs_get_user_props(zhp);
4171 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4173 * Go through and add any user properties as necessary. We
4174 * start by incrementing our list pointer to the first
4175 * non-native property.
4178 while (*start != NULL) {
4179 if ((*start)->pl_prop == ZPROP_INVAL)
4181 start = &(*start)->pl_next;
4185 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4187 * See if we've already found this property in our list.
4189 for (last = start; *last != NULL;
4190 last = &(*last)->pl_next) {
4191 if (strcmp((*last)->pl_user_prop,
4192 nvpair_name(elem)) == 0)
4196 if (*last == NULL) {
4197 if ((entry = zfs_alloc(hdl,
4198 sizeof (zprop_list_t))) == NULL ||
4199 ((entry->pl_user_prop = zfs_strdup(hdl,
4200 nvpair_name(elem)))) == NULL) {
4205 entry->pl_prop = ZPROP_INVAL;
4206 entry->pl_width = strlen(nvpair_name(elem));
4207 entry->pl_all = B_TRUE;
4214 * Now go through and check the width of any non-fixed columns
4216 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4217 if (entry->pl_fixed && !literal)
4220 if (entry->pl_prop != ZPROP_INVAL) {
4221 if (zfs_prop_get(zhp, entry->pl_prop,
4222 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4223 if (strlen(buf) > entry->pl_width)
4224 entry->pl_width = strlen(buf);
4226 if (received && zfs_prop_get_recvd(zhp,
4227 zfs_prop_to_name(entry->pl_prop),
4228 buf, sizeof (buf), literal) == 0)
4229 if (strlen(buf) > entry->pl_recvd_width)
4230 entry->pl_recvd_width = strlen(buf);
4232 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4234 verify(nvlist_lookup_string(propval,
4235 ZPROP_VALUE, &strval) == 0);
4236 if (strlen(strval) > entry->pl_width)
4237 entry->pl_width = strlen(strval);
4239 if (received && zfs_prop_get_recvd(zhp,
4240 entry->pl_user_prop,
4241 buf, sizeof (buf), literal) == 0)
4242 if (strlen(buf) > entry->pl_recvd_width)
4243 entry->pl_recvd_width = strlen(buf);
4251 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4252 char *resource, void *export, void *sharetab,
4253 int sharemax, zfs_share_op_t operation)
4255 zfs_cmd_t zc = { 0 };
4258 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4259 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4261 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4262 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4263 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4264 zc.zc_share.z_sharetype = operation;
4265 zc.zc_share.z_sharemax = sharemax;
4266 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4271 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4276 * Keep a reference to the props-table against which we prune the
4279 zhp->zfs_props_table = props;
4281 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4284 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4285 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4288 * User properties will result in ZPROP_INVAL, and since we
4289 * only know how to prune standard ZFS properties, we always
4290 * leave these in the list. This can also happen if we
4291 * encounter an unknown DSL property (when running older
4292 * software, for example).
4294 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4295 (void) nvlist_remove(zhp->zfs_props,
4296 nvpair_name(curr), nvpair_type(curr));
4303 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4304 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4306 zfs_cmd_t zc = { 0 };
4307 nvlist_t *nvlist = NULL;
4310 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4311 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4312 zc.zc_cookie = (uint64_t)cmd;
4314 if (cmd == ZFS_SMB_ACL_RENAME) {
4315 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4316 (void) no_memory(hdl);
4322 case ZFS_SMB_ACL_ADD:
4323 case ZFS_SMB_ACL_REMOVE:
4324 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4326 case ZFS_SMB_ACL_RENAME:
4327 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4329 (void) no_memory(hdl);
4332 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4334 (void) no_memory(hdl);
4337 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4338 nvlist_free(nvlist);
4342 case ZFS_SMB_ACL_PURGE:
4347 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4348 nvlist_free(nvlist);
4353 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4354 char *path, char *resource)
4356 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4361 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4362 char *path, char *resource)
4364 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4369 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4371 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4376 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4377 char *oldname, char *newname)
4379 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4382 #endif /* illumos */
4385 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4386 zfs_userspace_cb_t func, void *arg)
4388 zfs_cmd_t zc = { 0 };
4389 zfs_useracct_t buf[100];
4390 libzfs_handle_t *hdl = zhp->zfs_hdl;
4393 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4395 zc.zc_objset_type = type;
4396 zc.zc_nvlist_dst = (uintptr_t)buf;
4399 zfs_useracct_t *zua = buf;
4401 zc.zc_nvlist_dst_size = sizeof (buf);
4402 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4405 (void) snprintf(errbuf, sizeof (errbuf),
4406 dgettext(TEXT_DOMAIN,
4407 "cannot get used/quota for %s"), zc.zc_name);
4408 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4410 if (zc.zc_nvlist_dst_size == 0)
4413 while (zc.zc_nvlist_dst_size > 0) {
4414 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4415 zua->zu_space)) != 0)
4418 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4427 const char *snapname;
4429 boolean_t recursive;
4434 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4436 struct holdarg *ha = arg;
4437 char name[ZFS_MAXNAMELEN];
4440 (void) snprintf(name, sizeof (name),
4441 "%s@%s", zhp->zfs_name, ha->snapname);
4443 if (lzc_exists(name))
4444 fnvlist_add_string(ha->nvl, name, ha->tag);
4447 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4453 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4454 boolean_t recursive, int cleanup_fd)
4459 ha.nvl = fnvlist_alloc();
4460 ha.snapname = snapname;
4462 ha.recursive = recursive;
4463 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4465 if (nvlist_empty(ha.nvl)) {
4468 fnvlist_free(ha.nvl);
4470 (void) snprintf(errbuf, sizeof (errbuf),
4471 dgettext(TEXT_DOMAIN,
4472 "cannot hold snapshot '%s@%s'"),
4473 zhp->zfs_name, snapname);
4474 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4478 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4479 fnvlist_free(ha.nvl);
4485 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4489 libzfs_handle_t *hdl = zhp->zfs_hdl;
4494 ret = lzc_hold(holds, cleanup_fd, &errors);
4497 /* There may be errors even in the success case. */
4498 fnvlist_free(errors);
4502 if (nvlist_empty(errors)) {
4503 /* no hold-specific errors */
4504 (void) snprintf(errbuf, sizeof (errbuf),
4505 dgettext(TEXT_DOMAIN, "cannot hold"));
4508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4509 "pool must be upgraded"));
4510 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4513 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4516 (void) zfs_standard_error(hdl, ret, errbuf);
4520 for (elem = nvlist_next_nvpair(errors, NULL);
4522 elem = nvlist_next_nvpair(errors, elem)) {
4523 (void) snprintf(errbuf, sizeof (errbuf),
4524 dgettext(TEXT_DOMAIN,
4525 "cannot hold snapshot '%s'"), nvpair_name(elem));
4526 switch (fnvpair_value_int32(elem)) {
4529 * Temporary tags wind up having the ds object id
4530 * prepended. So even if we passed the length check
4531 * above, it's still possible for the tag to wind
4532 * up being slightly too long.
4534 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4537 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4540 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4543 (void) zfs_standard_error(hdl,
4544 fnvpair_value_int32(elem), errbuf);
4548 fnvlist_free(errors);
4553 zfs_release_one(zfs_handle_t *zhp, void *arg)
4555 struct holdarg *ha = arg;
4556 char name[ZFS_MAXNAMELEN];
4558 nvlist_t *existing_holds;
4560 (void) snprintf(name, sizeof (name),
4561 "%s@%s", zhp->zfs_name, ha->snapname);
4563 if (lzc_get_holds(name, &existing_holds) != 0) {
4565 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4568 nvlist_t *torelease = fnvlist_alloc();
4569 fnvlist_add_boolean(torelease, ha->tag);
4570 fnvlist_add_nvlist(ha->nvl, name, torelease);
4571 fnvlist_free(torelease);
4575 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4581 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4582 boolean_t recursive)
4586 nvlist_t *errors = NULL;
4588 libzfs_handle_t *hdl = zhp->zfs_hdl;
4591 ha.nvl = fnvlist_alloc();
4592 ha.snapname = snapname;
4594 ha.recursive = recursive;
4596 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4598 if (nvlist_empty(ha.nvl)) {
4599 fnvlist_free(ha.nvl);
4601 (void) snprintf(errbuf, sizeof (errbuf),
4602 dgettext(TEXT_DOMAIN,
4603 "cannot release hold from snapshot '%s@%s'"),
4604 zhp->zfs_name, snapname);
4606 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4608 (void) zfs_standard_error(hdl, ret, errbuf);
4613 ret = lzc_release(ha.nvl, &errors);
4614 fnvlist_free(ha.nvl);
4617 /* There may be errors even in the success case. */
4618 fnvlist_free(errors);
4622 if (nvlist_empty(errors)) {
4623 /* no hold-specific errors */
4624 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4629 "pool must be upgraded"));
4630 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4633 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4637 for (elem = nvlist_next_nvpair(errors, NULL);
4639 elem = nvlist_next_nvpair(errors, elem)) {
4640 (void) snprintf(errbuf, sizeof (errbuf),
4641 dgettext(TEXT_DOMAIN,
4642 "cannot release hold from snapshot '%s'"),
4644 switch (fnvpair_value_int32(elem)) {
4646 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4649 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4652 (void) zfs_standard_error_fmt(hdl,
4653 fnvpair_value_int32(elem), errbuf);
4657 fnvlist_free(errors);
4662 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4664 zfs_cmd_t zc = { 0 };
4665 libzfs_handle_t *hdl = zhp->zfs_hdl;
4671 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4672 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4676 nvbuf = malloc(nvsz);
4677 if (nvbuf == NULL) {
4678 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4682 zc.zc_nvlist_dst_size = nvsz;
4683 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4685 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4687 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4688 (void) snprintf(errbuf, sizeof (errbuf),
4689 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4694 nvsz = zc.zc_nvlist_dst_size;
4698 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4699 "pool must be upgraded"));
4700 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4703 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4706 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4709 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4714 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4716 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4717 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4719 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4729 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4731 zfs_cmd_t zc = { 0 };
4732 libzfs_handle_t *hdl = zhp->zfs_hdl;
4738 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4739 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4741 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4744 nvbuf = malloc(nvsz);
4746 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4749 zc.zc_nvlist_src_size = nvsz;
4750 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4751 zc.zc_perm_action = un;
4753 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4755 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4756 (void) snprintf(errbuf, sizeof (errbuf),
4757 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4762 "pool must be upgraded"));
4763 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4766 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4769 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4772 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4783 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4788 err = lzc_get_holds(zhp->zfs_name, nvl);
4791 libzfs_handle_t *hdl = zhp->zfs_hdl;
4793 (void) snprintf(errbuf, sizeof (errbuf),
4794 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4798 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4799 "pool must be upgraded"));
4800 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4803 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4806 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4809 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4818 * Convert the zvol's volume size to an appropriate reservation.
4819 * Note: If this routine is updated, it is necessary to update the ZFS test
4820 * suite's shell version in reservation.kshlib.
4823 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4826 uint64_t nblocks, volblocksize;
4830 if (nvlist_lookup_string(props,
4831 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4832 ncopies = atoi(strval);
4835 if (nvlist_lookup_uint64(props,
4836 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4837 &volblocksize) != 0)
4838 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4839 nblocks = volsize/volblocksize;
4840 /* start with metadnode L0-L6 */
4842 /* calculate number of indirects */
4843 while (nblocks > 1) {
4844 nblocks += DNODES_PER_LEVEL - 1;
4845 nblocks /= DNODES_PER_LEVEL;
4848 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4851 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4852 * compressed, but in practice they compress down to about
4855 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4861 * Attach/detach the given filesystem to/from the given jail.
4864 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4866 libzfs_handle_t *hdl = zhp->zfs_hdl;
4867 zfs_cmd_t zc = { 0 };
4873 (void) snprintf(errbuf, sizeof (errbuf),
4874 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4876 (void) snprintf(errbuf, sizeof (errbuf),
4877 dgettext(TEXT_DOMAIN, "cannot unjail '%s'"), zhp->zfs_name);
4880 switch (zhp->zfs_type) {
4881 case ZFS_TYPE_VOLUME:
4882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4883 "volumes can not be jailed"));
4884 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4885 case ZFS_TYPE_SNAPSHOT:
4886 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4887 "snapshots can not be jailed"));
4888 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4890 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4892 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4893 zc.zc_objset_type = DMU_OST_ZFS;
4894 zc.zc_jailid = jailid;
4896 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4897 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4898 zfs_standard_error(hdl, errno, errbuf);