4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
46 #include <sys/mntent.h>
47 #include <sys/mount.h>
55 #include <directory.h>
57 #include <sys/dnode.h>
62 #include "zfs_namecheck.h"
64 #include "libzfs_impl.h"
65 #include "zfs_deleg.h"
67 static int userquota_propname_decode(const char *propname, boolean_t zoned,
68 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
71 * Given a single type (not a mask of types), return the type in a human
75 zfs_type_to_name(zfs_type_t type)
78 case ZFS_TYPE_FILESYSTEM:
79 return (dgettext(TEXT_DOMAIN, "filesystem"));
80 case ZFS_TYPE_SNAPSHOT:
81 return (dgettext(TEXT_DOMAIN, "snapshot"));
83 return (dgettext(TEXT_DOMAIN, "volume"));
85 return (dgettext(TEXT_DOMAIN, "pool"));
86 case ZFS_TYPE_BOOKMARK:
87 return (dgettext(TEXT_DOMAIN, "bookmark"));
89 assert(!"unhandled zfs_type_t");
96 * Validate a ZFS path. This is used even before trying to open the dataset, to
97 * provide a more meaningful error message. We call zfs_error_aux() to
98 * explain exactly why the name was not valid.
101 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
107 (void) zfs_prop_get_table();
108 if (dataset_namecheck(path, &why, &what) != 0) {
111 case NAME_ERR_TOOLONG:
112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
113 "name is too long"));
116 case NAME_ERR_LEADING_SLASH:
117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 "leading slash in name"));
121 case NAME_ERR_EMPTY_COMPONENT:
122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
123 "empty component in name"));
126 case NAME_ERR_TRAILING_SLASH:
127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
128 "trailing slash in name"));
131 case NAME_ERR_INVALCHAR:
133 dgettext(TEXT_DOMAIN, "invalid character "
134 "'%c' in name"), what);
137 case NAME_ERR_MULTIPLE_AT:
138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139 "multiple '@' delimiters in name"));
142 case NAME_ERR_NOLETTER:
143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
144 "pool doesn't begin with a letter"));
147 case NAME_ERR_RESERVED:
148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
149 "name is reserved"));
152 case NAME_ERR_DISKLIKE:
153 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
154 "reserved disk name"));
158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
159 "(%d) not defined"), why);
167 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170 "snapshot delimiter '@' in filesystem name"));
174 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "missing '@' delimiter in snapshot name"));
181 if (modifying && strchr(path, '%') != NULL) {
183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
184 "invalid character %c in name"), '%');
192 zfs_name_valid(const char *name, zfs_type_t type)
194 if (type == ZFS_TYPE_POOL)
195 return (zpool_name_valid(NULL, B_FALSE, name));
196 return (zfs_validate_name(NULL, name, type, B_FALSE));
200 * This function takes the raw DSL properties, and filters out the user-defined
201 * properties into a separate nvlist.
204 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
206 libzfs_handle_t *hdl = zhp->zfs_hdl;
211 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
212 (void) no_memory(hdl);
217 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
218 if (!zfs_prop_user(nvpair_name(elem)))
221 verify(nvpair_value_nvlist(elem, &propval) == 0);
222 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
224 (void) no_memory(hdl);
232 static zpool_handle_t *
233 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
235 libzfs_handle_t *hdl = zhp->zfs_hdl;
238 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
239 if (hdl->libzfs_pool_handles != NULL)
240 zph->zpool_next = hdl->libzfs_pool_handles;
241 hdl->libzfs_pool_handles = zph;
246 static zpool_handle_t *
247 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
249 libzfs_handle_t *hdl = zhp->zfs_hdl;
250 zpool_handle_t *zph = hdl->libzfs_pool_handles;
252 while ((zph != NULL) &&
253 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
254 zph = zph->zpool_next;
259 * Returns a handle to the pool that contains the provided dataset.
260 * If a handle to that pool already exists then that handle is returned.
261 * Otherwise, a new handle is created and added to the list of handles.
263 static zpool_handle_t *
264 zpool_handle(zfs_handle_t *zhp)
270 len = strcspn(zhp->zfs_name, "/@#") + 1;
271 pool_name = zfs_alloc(zhp->zfs_hdl, len);
272 (void) strlcpy(pool_name, zhp->zfs_name, len);
274 zph = zpool_find_handle(zhp, pool_name, len);
276 zph = zpool_add_handle(zhp, pool_name);
283 zpool_free_handles(libzfs_handle_t *hdl)
285 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
287 while (zph != NULL) {
288 next = zph->zpool_next;
292 hdl->libzfs_pool_handles = NULL;
296 * Utility function to gather stats (objset and zpl) for the given object.
299 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
301 libzfs_handle_t *hdl = zhp->zfs_hdl;
303 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
305 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
306 if (errno == ENOMEM) {
307 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
318 * Utility function to get the received properties of the given object.
321 get_recvd_props_ioctl(zfs_handle_t *zhp)
323 libzfs_handle_t *hdl = zhp->zfs_hdl;
324 nvlist_t *recvdprops;
325 zfs_cmd_t zc = { 0 };
328 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
331 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
333 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
334 if (errno == ENOMEM) {
335 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
339 zcmd_free_nvlists(&zc);
344 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
345 zcmd_free_nvlists(&zc);
349 nvlist_free(zhp->zfs_recvd_props);
350 zhp->zfs_recvd_props = recvdprops;
356 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
358 nvlist_t *allprops, *userprops;
360 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
362 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
367 * XXX Why do we store the user props separately, in addition to
368 * storing them in zfs_props?
370 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
371 nvlist_free(allprops);
375 nvlist_free(zhp->zfs_props);
376 nvlist_free(zhp->zfs_user_props);
378 zhp->zfs_props = allprops;
379 zhp->zfs_user_props = userprops;
385 get_stats(zfs_handle_t *zhp)
388 zfs_cmd_t zc = { 0 };
390 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
392 if (get_stats_ioctl(zhp, &zc) != 0)
394 else if (put_stats_zhdl(zhp, &zc) != 0)
396 zcmd_free_nvlists(&zc);
401 * Refresh the properties currently stored in the handle.
404 zfs_refresh_properties(zfs_handle_t *zhp)
406 (void) get_stats(zhp);
410 * Makes a handle from the given dataset name. Used by zfs_open() and
411 * zfs_iter_* to create child handles on the fly.
414 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
416 if (put_stats_zhdl(zhp, zc) != 0)
420 * We've managed to open the dataset and gather statistics. Determine
421 * the high-level type.
423 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
424 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
425 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
426 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
430 if (zhp->zfs_dmustats.dds_is_snapshot)
431 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
432 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
433 zhp->zfs_type = ZFS_TYPE_VOLUME;
434 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
435 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
437 abort(); /* we should never see any other types */
439 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
446 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
448 zfs_cmd_t zc = { 0 };
450 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
456 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
457 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
461 if (get_stats_ioctl(zhp, &zc) == -1) {
462 zcmd_free_nvlists(&zc);
466 if (make_dataset_handle_common(zhp, &zc) == -1) {
470 zcmd_free_nvlists(&zc);
475 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
477 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
483 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
484 if (make_dataset_handle_common(zhp, zc) == -1) {
492 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
494 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
499 zhp->zfs_hdl = pzhp->zfs_hdl;
500 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
501 zhp->zfs_head_type = pzhp->zfs_type;
502 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
503 zhp->zpool_hdl = zpool_handle(zhp);
508 zfs_handle_dup(zfs_handle_t *zhp_orig)
510 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
515 zhp->zfs_hdl = zhp_orig->zfs_hdl;
516 zhp->zpool_hdl = zhp_orig->zpool_hdl;
517 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
518 sizeof (zhp->zfs_name));
519 zhp->zfs_type = zhp_orig->zfs_type;
520 zhp->zfs_head_type = zhp_orig->zfs_head_type;
521 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
522 if (zhp_orig->zfs_props != NULL) {
523 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
524 (void) no_memory(zhp->zfs_hdl);
529 if (zhp_orig->zfs_user_props != NULL) {
530 if (nvlist_dup(zhp_orig->zfs_user_props,
531 &zhp->zfs_user_props, 0) != 0) {
532 (void) no_memory(zhp->zfs_hdl);
537 if (zhp_orig->zfs_recvd_props != NULL) {
538 if (nvlist_dup(zhp_orig->zfs_recvd_props,
539 &zhp->zfs_recvd_props, 0)) {
540 (void) no_memory(zhp->zfs_hdl);
545 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
546 if (zhp_orig->zfs_mntopts != NULL) {
547 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
548 zhp_orig->zfs_mntopts);
550 zhp->zfs_props_table = zhp_orig->zfs_props_table;
555 zfs_bookmark_exists(const char *path)
559 char fsname[ZFS_MAX_DATASET_NAME_LEN];
566 (void) strlcpy(fsname, path, sizeof (fsname));
567 pound = strchr(fsname, '#');
572 bmark_name = pound + 1;
573 props = fnvlist_alloc();
574 err = lzc_get_bookmarks(fsname, props, &bmarks);
581 rv = nvlist_exists(bmarks, bmark_name);
587 make_bookmark_handle(zfs_handle_t *parent, const char *path,
588 nvlist_t *bmark_props)
590 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
595 /* Fill in the name. */
596 zhp->zfs_hdl = parent->zfs_hdl;
597 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
599 /* Set the property lists. */
600 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
606 zhp->zfs_head_type = parent->zfs_head_type;
607 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
609 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
610 nvlist_free(zhp->zfs_props);
619 * Opens the given snapshot, filesystem, or volume. The 'types'
620 * argument is a mask of acceptable types. The function will print an
621 * appropriate error message and return NULL if it can't be opened.
624 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
629 (void) snprintf(errbuf, sizeof (errbuf),
630 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
633 * Validate the name before we even try to open it.
635 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
637 "invalid dataset name"));
638 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
643 * Try to get stats for the dataset, which will tell us if it exists.
646 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
647 (void) zfs_standard_error(hdl, errno, errbuf);
651 if (!(types & zhp->zfs_type)) {
652 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
661 * Release a ZFS handle. Nothing to do but free the associated memory.
664 zfs_close(zfs_handle_t *zhp)
666 if (zhp->zfs_mntopts)
667 free(zhp->zfs_mntopts);
668 nvlist_free(zhp->zfs_props);
669 nvlist_free(zhp->zfs_user_props);
670 nvlist_free(zhp->zfs_recvd_props);
674 typedef struct mnttab_node {
675 struct mnttab mtn_mt;
680 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
682 const mnttab_node_t *mtn1 = arg1;
683 const mnttab_node_t *mtn2 = arg2;
686 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
690 return (rv > 0 ? 1 : -1);
694 libzfs_mnttab_init(libzfs_handle_t *hdl)
696 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
697 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
698 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
702 libzfs_mnttab_update(libzfs_handle_t *hdl)
706 rewind(hdl->libzfs_mnttab);
707 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
710 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
712 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
713 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
714 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
715 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
716 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
717 avl_add(&hdl->libzfs_mnttab_cache, mtn);
722 libzfs_mnttab_fini(libzfs_handle_t *hdl)
727 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
729 free(mtn->mtn_mt.mnt_special);
730 free(mtn->mtn_mt.mnt_mountp);
731 free(mtn->mtn_mt.mnt_fstype);
732 free(mtn->mtn_mt.mnt_mntopts);
735 avl_destroy(&hdl->libzfs_mnttab_cache);
739 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
741 hdl->libzfs_mnttab_enable = enable;
745 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
746 struct mnttab *entry)
751 if (!hdl->libzfs_mnttab_enable) {
752 struct mnttab srch = { 0 };
754 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
755 libzfs_mnttab_fini(hdl);
756 rewind(hdl->libzfs_mnttab);
757 srch.mnt_special = (char *)fsname;
758 srch.mnt_fstype = MNTTYPE_ZFS;
759 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
765 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
766 libzfs_mnttab_update(hdl);
768 find.mtn_mt.mnt_special = (char *)fsname;
769 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
771 *entry = mtn->mtn_mt;
778 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
779 const char *mountp, const char *mntopts)
783 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
785 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
786 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
787 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
788 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
789 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
790 avl_add(&hdl->libzfs_mnttab_cache, mtn);
794 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
799 find.mtn_mt.mnt_special = (char *)fsname;
800 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
802 avl_remove(&hdl->libzfs_mnttab_cache, ret);
803 free(ret->mtn_mt.mnt_special);
804 free(ret->mtn_mt.mnt_mountp);
805 free(ret->mtn_mt.mnt_fstype);
806 free(ret->mtn_mt.mnt_mntopts);
812 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
814 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
816 if (zpool_handle == NULL)
819 *spa_version = zpool_get_prop_int(zpool_handle,
820 ZPOOL_PROP_VERSION, NULL);
825 * The choice of reservation property depends on the SPA version.
828 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
832 if (zfs_spa_version(zhp, &spa_version) < 0)
835 if (spa_version >= SPA_VERSION_REFRESERVATION)
836 *resv_prop = ZFS_PROP_REFRESERVATION;
838 *resv_prop = ZFS_PROP_RESERVATION;
844 * Given an nvlist of properties to set, validates that they are correct, and
845 * parses any numeric properties (index, boolean, etc) if they are specified as
849 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
850 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
858 int chosen_normal = -1;
861 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
862 (void) no_memory(hdl);
867 * Make sure this property is valid and applies to this type.
871 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
872 const char *propname = nvpair_name(elem);
874 prop = zfs_name_to_prop(propname);
875 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
877 * This is a user property: make sure it's a
878 * string, and that it's less than ZAP_MAXNAMELEN.
880 if (nvpair_type(elem) != DATA_TYPE_STRING) {
881 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
882 "'%s' must be a string"), propname);
883 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
887 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
888 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
889 "property name '%s' is too long"),
891 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
895 (void) nvpair_value_string(elem, &strval);
896 if (nvlist_add_string(ret, propname, strval) != 0) {
897 (void) no_memory(hdl);
904 * Currently, only user properties can be modified on
907 if (type == ZFS_TYPE_SNAPSHOT) {
908 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
909 "this property can not be modified for snapshots"));
910 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
914 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
915 zfs_userquota_prop_t uqtype;
916 char newpropname[128];
921 if (userquota_propname_decode(propname, zoned,
922 &uqtype, domain, sizeof (domain), &rid) != 0) {
924 dgettext(TEXT_DOMAIN,
925 "'%s' has an invalid user/group name"),
927 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
931 if (uqtype != ZFS_PROP_USERQUOTA &&
932 uqtype != ZFS_PROP_GROUPQUOTA) {
934 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
936 (void) zfs_error(hdl, EZFS_PROPREADONLY,
941 if (nvpair_type(elem) == DATA_TYPE_STRING) {
942 (void) nvpair_value_string(elem, &strval);
943 if (strcmp(strval, "none") == 0) {
945 } else if (zfs_nicestrtonum(hdl,
946 strval, &intval) != 0) {
947 (void) zfs_error(hdl,
948 EZFS_BADPROP, errbuf);
951 } else if (nvpair_type(elem) ==
953 (void) nvpair_value_uint64(elem, &intval);
955 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
956 "use 'none' to disable "
957 "userquota/groupquota"));
961 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
962 "'%s' must be a number"), propname);
963 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
968 * Encode the prop name as
969 * userquota@<hex-rid>-domain, to make it easy
970 * for the kernel to decode.
972 (void) snprintf(newpropname, sizeof (newpropname),
973 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
974 (longlong_t)rid, domain);
978 if (nvlist_add_uint64_array(ret, newpropname,
980 (void) no_memory(hdl);
984 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
988 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
992 if (prop == ZPROP_INVAL) {
993 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
994 "invalid property '%s'"), propname);
995 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
999 if (!zfs_prop_valid_for_type(prop, type)) {
1001 dgettext(TEXT_DOMAIN, "'%s' does not "
1002 "apply to datasets of this type"), propname);
1003 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1007 if (zfs_prop_readonly(prop) &&
1008 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1010 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1012 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1016 if (zprop_parse_value(hdl, elem, prop, type, ret,
1017 &strval, &intval, errbuf) != 0)
1021 * Perform some additional checks for specific properties.
1024 case ZFS_PROP_VERSION:
1030 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1031 if (intval < version) {
1032 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1033 "Can not downgrade; already at version %u"),
1035 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1041 case ZFS_PROP_VOLBLOCKSIZE:
1042 case ZFS_PROP_RECORDSIZE:
1044 int maxbs = SPA_MAXBLOCKSIZE;
1045 if (zpool_hdl != NULL) {
1046 maxbs = zpool_get_prop_int(zpool_hdl,
1047 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1050 * Volumes are limited to a volblocksize of 128KB,
1051 * because they typically service workloads with
1052 * small random writes, which incur a large performance
1053 * penalty with large blocks.
1055 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1056 maxbs = SPA_OLD_MAXBLOCKSIZE;
1058 * The value must be a power of two between
1059 * SPA_MINBLOCKSIZE and maxbs.
1061 if (intval < SPA_MINBLOCKSIZE ||
1062 intval > maxbs || !ISP2(intval)) {
1063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1064 "'%s' must be power of 2 from 512B "
1065 "to %uKB"), propname, maxbs >> 10);
1066 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1071 case ZFS_PROP_MLSLABEL:
1074 * Verify the mlslabel string and convert to
1075 * internal hex label string.
1079 char *hex = NULL; /* internal label string */
1081 /* Default value is already OK. */
1082 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1085 /* Verify the label can be converted to binary form */
1086 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1087 (str_to_label(strval, &new_sl, MAC_LABEL,
1088 L_NO_CORRECTION, NULL) == -1)) {
1092 /* Now translate to hex internal label string */
1093 if (label_to_str(new_sl, &hex, M_INTERNAL,
1099 m_label_free(new_sl);
1101 /* If string is already in internal form, we're done. */
1102 if (strcmp(strval, hex) == 0) {
1107 /* Replace the label string with the internal form. */
1108 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1110 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1118 "invalid mlslabel '%s'"), strval);
1119 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1120 m_label_free(new_sl); /* OK if null */
1125 case ZFS_PROP_MOUNTPOINT:
1127 namecheck_err_t why;
1129 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1130 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1133 if (mountpoint_namecheck(strval, &why)) {
1135 case NAME_ERR_LEADING_SLASH:
1137 dgettext(TEXT_DOMAIN,
1138 "'%s' must be an absolute path, "
1139 "'none', or 'legacy'"), propname);
1141 case NAME_ERR_TOOLONG:
1143 dgettext(TEXT_DOMAIN,
1144 "component of '%s' is too long"),
1150 dgettext(TEXT_DOMAIN,
1151 "(%d) not defined"),
1155 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1162 case ZFS_PROP_SHARESMB:
1163 case ZFS_PROP_SHARENFS:
1165 * For the mountpoint and sharenfs or sharesmb
1166 * properties, check if it can be set in a
1167 * global/non-global zone based on
1168 * the zoned property value:
1170 * global zone non-global zone
1171 * --------------------------------------------------
1172 * zoned=on mountpoint (no) mountpoint (yes)
1173 * sharenfs (no) sharenfs (no)
1174 * sharesmb (no) sharesmb (no)
1176 * zoned=off mountpoint (yes) N/A
1181 if (getzoneid() == GLOBAL_ZONEID) {
1182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1183 "'%s' cannot be set on "
1184 "dataset in a non-global zone"),
1186 (void) zfs_error(hdl, EZFS_ZONED,
1189 } else if (prop == ZFS_PROP_SHARENFS ||
1190 prop == ZFS_PROP_SHARESMB) {
1191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1192 "'%s' cannot be set in "
1193 "a non-global zone"), propname);
1194 (void) zfs_error(hdl, EZFS_ZONED,
1198 } else if (getzoneid() != GLOBAL_ZONEID) {
1200 * If zoned property is 'off', this must be in
1201 * a global zone. If not, something is wrong.
1203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1204 "'%s' cannot be set while dataset "
1205 "'zoned' property is set"), propname);
1206 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1211 * At this point, it is legitimate to set the
1212 * property. Now we want to make sure that the
1213 * property value is valid if it is sharenfs.
1215 if ((prop == ZFS_PROP_SHARENFS ||
1216 prop == ZFS_PROP_SHARESMB) &&
1217 strcmp(strval, "on") != 0 &&
1218 strcmp(strval, "off") != 0) {
1219 zfs_share_proto_t proto;
1221 if (prop == ZFS_PROP_SHARESMB)
1227 * Must be an valid sharing protocol
1228 * option string so init the libshare
1229 * in order to enable the parser and
1230 * then parse the options. We use the
1231 * control API since we don't care about
1232 * the current configuration and don't
1233 * want the overhead of loading it
1234 * until we actually do something.
1237 if (zfs_init_libshare(hdl,
1238 SA_INIT_CONTROL_API) != SA_OK) {
1240 * An error occurred so we can't do
1243 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1244 "'%s' cannot be set: problem "
1245 "in share initialization"),
1247 (void) zfs_error(hdl, EZFS_BADPROP,
1252 if (zfs_parse_options(strval, proto) != SA_OK) {
1254 * There was an error in parsing so
1255 * deal with it by issuing an error
1256 * message and leaving after
1257 * uninitializing the the libshare
1260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1261 "'%s' cannot be set to invalid "
1262 "options"), propname);
1263 (void) zfs_error(hdl, EZFS_BADPROP,
1265 zfs_uninit_libshare(hdl);
1268 zfs_uninit_libshare(hdl);
1273 case ZFS_PROP_UTF8ONLY:
1274 chosen_utf = (int)intval;
1277 case ZFS_PROP_NORMALIZE:
1278 chosen_normal = (int)intval;
1286 * For changes to existing volumes, we have some additional
1287 * checks to enforce.
1289 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1290 uint64_t volsize = zfs_prop_get_int(zhp,
1292 uint64_t blocksize = zfs_prop_get_int(zhp,
1293 ZFS_PROP_VOLBLOCKSIZE);
1297 case ZFS_PROP_RESERVATION:
1298 case ZFS_PROP_REFRESERVATION:
1299 if (intval > volsize) {
1300 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1301 "'%s' is greater than current "
1302 "volume size"), propname);
1303 (void) zfs_error(hdl, EZFS_BADPROP,
1309 case ZFS_PROP_VOLSIZE:
1310 if (intval % blocksize != 0) {
1311 zfs_nicenum(blocksize, buf,
1313 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1314 "'%s' must be a multiple of "
1315 "volume block size (%s)"),
1317 (void) zfs_error(hdl, EZFS_BADPROP,
1323 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1324 "'%s' cannot be zero"),
1326 (void) zfs_error(hdl, EZFS_BADPROP,
1339 * If normalization was chosen, but no UTF8 choice was made,
1340 * enforce rejection of non-UTF8 names.
1342 * If normalization was chosen, but rejecting non-UTF8 names
1343 * was explicitly not chosen, it is an error.
1345 if (chosen_normal > 0 && chosen_utf < 0) {
1346 if (nvlist_add_uint64(ret,
1347 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1348 (void) no_memory(hdl);
1351 } else if (chosen_normal > 0 && chosen_utf == 0) {
1352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1353 "'%s' must be set 'on' if normalization chosen"),
1354 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1355 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1366 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1368 uint64_t old_volsize;
1369 uint64_t new_volsize;
1370 uint64_t old_reservation;
1371 uint64_t new_reservation;
1372 zfs_prop_t resv_prop;
1376 * If this is an existing volume, and someone is setting the volsize,
1377 * make sure that it matches the reservation, or add it if necessary.
1379 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1380 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1382 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1384 props = fnvlist_alloc();
1385 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1386 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1388 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1389 old_reservation) || nvlist_exists(nvl,
1390 zfs_prop_to_name(resv_prop))) {
1391 fnvlist_free(props);
1394 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1395 &new_volsize) != 0) {
1396 fnvlist_free(props);
1399 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1400 fnvlist_free(props);
1402 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1403 new_reservation) != 0) {
1404 (void) no_memory(zhp->zfs_hdl);
1411 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1418 * For quotas and reservations, ENOSPC indicates
1419 * something different; setting a quota or reservation
1420 * doesn't use any disk space.
1423 case ZFS_PROP_QUOTA:
1424 case ZFS_PROP_REFQUOTA:
1425 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1426 "size is less than current used or "
1428 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1431 case ZFS_PROP_RESERVATION:
1432 case ZFS_PROP_REFRESERVATION:
1433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1434 "size is greater than available space"));
1435 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1439 (void) zfs_standard_error(hdl, err, errbuf);
1445 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1449 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1454 "property value too long"));
1455 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1460 "pool and or dataset must be upgraded to set this "
1461 "property or value"));
1462 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1466 if (prop == ZFS_PROP_COMPRESSION ||
1467 prop == ZFS_PROP_RECORDSIZE) {
1468 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1469 "property setting is not allowed on "
1470 "bootable datasets"));
1471 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1472 } else if (prop == ZFS_PROP_CHECKSUM ||
1473 prop == ZFS_PROP_DEDUP) {
1474 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475 "property setting is not allowed on "
1477 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1479 (void) zfs_standard_error(hdl, err, errbuf);
1484 if (prop == ZPROP_INVAL) {
1485 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1487 (void) zfs_standard_error(hdl, err, errbuf);
1493 * This platform can't address a volume this big.
1496 if (prop == ZFS_PROP_VOLSIZE) {
1497 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1503 (void) zfs_standard_error(hdl, err, errbuf);
1508 * Given a property name and value, set the property for the given dataset.
1511 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1515 libzfs_handle_t *hdl = zhp->zfs_hdl;
1516 nvlist_t *nvl = NULL;
1518 (void) snprintf(errbuf, sizeof (errbuf),
1519 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1522 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1523 nvlist_add_string(nvl, propname, propval) != 0) {
1524 (void) no_memory(hdl);
1528 ret = zfs_prop_set_list(zhp, nvl);
1538 * Given an nvlist of property names and values, set the properties for the
1542 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1544 zfs_cmd_t zc = { 0 };
1546 prop_changelist_t **cls = NULL;
1549 libzfs_handle_t *hdl = zhp->zfs_hdl;
1554 (void) snprintf(errbuf, sizeof (errbuf),
1555 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1558 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1559 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1564 * We have to check for any extra properties which need to be added
1565 * before computing the length of the nvlist.
1567 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1569 elem = nvlist_next_nvpair(nvl, elem)) {
1570 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1571 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1576 * Check how many properties we're setting and allocate an array to
1577 * store changelist pointers for postfix().
1580 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1582 elem = nvlist_next_nvpair(nvl, elem))
1584 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1588 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1590 elem = nvlist_next_nvpair(nvl, elem)) {
1592 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1594 assert(cl_idx < nvl_len);
1596 * We don't want to unmount & remount the dataset when changing
1597 * its canmount property to 'on' or 'noauto'. We only use
1598 * the changelist logic to unmount when setting canmount=off.
1600 if (!(prop == ZFS_PROP_CANMOUNT &&
1601 fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) {
1602 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1603 if (cls[cl_idx] == NULL)
1607 if (prop == ZFS_PROP_MOUNTPOINT &&
1608 changelist_haszonedchild(cls[cl_idx])) {
1609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1610 "child dataset with inherited mountpoint is used "
1611 "in a non-global zone"));
1612 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1616 if (cls[cl_idx] != NULL &&
1617 (ret = changelist_prefix(cls[cl_idx])) != 0)
1622 assert(cl_idx == nvl_len);
1625 * Execute the corresponding ioctl() to set this list of properties.
1627 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1629 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1630 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1633 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1636 /* Get the list of unset properties back and report them. */
1637 nvlist_t *errorprops = NULL;
1638 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1640 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1642 elem = nvlist_next_nvpair(nvl, elem)) {
1643 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1644 zfs_setprop_error(hdl, prop, errno, errbuf);
1646 nvlist_free(errorprops);
1648 if (added_resv && errno == ENOSPC) {
1649 /* clean up the volsize property we tried to set */
1650 uint64_t old_volsize = zfs_prop_get_int(zhp,
1654 zcmd_free_nvlists(&zc);
1656 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1658 if (nvlist_add_uint64(nvl,
1659 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1662 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1664 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1667 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1668 if (cls[cl_idx] != NULL) {
1669 int clp_err = changelist_postfix(cls[cl_idx]);
1676 * Refresh the statistics so the new property value
1680 (void) get_stats(zhp);
1685 zcmd_free_nvlists(&zc);
1687 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1688 if (cls[cl_idx] != NULL)
1689 changelist_free(cls[cl_idx]);
1697 * Given a property, inherit the value from the parent dataset, or if received
1698 * is TRUE, revert to the received value, if any.
1701 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1703 zfs_cmd_t zc = { 0 };
1705 prop_changelist_t *cl;
1706 libzfs_handle_t *hdl = zhp->zfs_hdl;
1710 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1711 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1713 zc.zc_cookie = received;
1714 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1716 * For user properties, the amount of work we have to do is very
1717 * small, so just do it here.
1719 if (!zfs_prop_user(propname)) {
1720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1721 "invalid property"));
1722 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1725 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1726 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1728 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1729 return (zfs_standard_error(hdl, errno, errbuf));
1735 * Verify that this property is inheritable.
1737 if (zfs_prop_readonly(prop))
1738 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1740 if (!zfs_prop_inheritable(prop) && !received)
1741 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1744 * Check to see if the value applies to this type
1746 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1747 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1750 * Normalize the name, to get rid of shorthand abbreviations.
1752 propname = zfs_prop_to_name(prop);
1753 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1754 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1756 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1757 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1759 "dataset is used in a non-global zone"));
1760 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1764 * Determine datasets which will be affected by this change, if any.
1766 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1769 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1771 "child dataset with inherited mountpoint is used "
1772 "in a non-global zone"));
1773 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1777 if ((ret = changelist_prefix(cl)) != 0)
1780 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1781 return (zfs_standard_error(hdl, errno, errbuf));
1784 if ((ret = changelist_postfix(cl)) != 0)
1788 * Refresh the statistics so the new property is reflected.
1790 (void) get_stats(zhp);
1794 changelist_free(cl);
1799 * True DSL properties are stored in an nvlist. The following two functions
1800 * extract them appropriately.
1803 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1809 if (nvlist_lookup_nvlist(zhp->zfs_props,
1810 zfs_prop_to_name(prop), &nv) == 0) {
1811 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1812 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1814 verify(!zhp->zfs_props_table ||
1815 zhp->zfs_props_table[prop] == B_TRUE);
1816 value = zfs_prop_default_numeric(prop);
1824 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1830 if (nvlist_lookup_nvlist(zhp->zfs_props,
1831 zfs_prop_to_name(prop), &nv) == 0) {
1832 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1833 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1835 verify(!zhp->zfs_props_table ||
1836 zhp->zfs_props_table[prop] == B_TRUE);
1837 value = zfs_prop_default_string(prop);
1845 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1847 return (zhp->zfs_props == zhp->zfs_recvd_props);
1851 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1853 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1854 zhp->zfs_props = zhp->zfs_recvd_props;
1858 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1860 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1865 * Internal function for getting a numeric property. Both zfs_prop_get() and
1866 * zfs_prop_get_int() are built using this interface.
1868 * Certain properties can be overridden using 'mount -o'. In this case, scan
1869 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1870 * If they differ from the on-disk values, report the current values and mark
1871 * the source "temporary".
1874 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1875 char **source, uint64_t *val)
1877 zfs_cmd_t zc = { 0 };
1878 nvlist_t *zplprops = NULL;
1880 char *mntopt_on = NULL;
1881 char *mntopt_off = NULL;
1882 boolean_t received = zfs_is_recvd_props_mode(zhp);
1887 case ZFS_PROP_ATIME:
1888 mntopt_on = MNTOPT_ATIME;
1889 mntopt_off = MNTOPT_NOATIME;
1892 case ZFS_PROP_DEVICES:
1893 mntopt_on = MNTOPT_DEVICES;
1894 mntopt_off = MNTOPT_NODEVICES;
1898 mntopt_on = MNTOPT_EXEC;
1899 mntopt_off = MNTOPT_NOEXEC;
1902 case ZFS_PROP_READONLY:
1903 mntopt_on = MNTOPT_RO;
1904 mntopt_off = MNTOPT_RW;
1907 case ZFS_PROP_SETUID:
1908 mntopt_on = MNTOPT_SETUID;
1909 mntopt_off = MNTOPT_NOSETUID;
1912 case ZFS_PROP_XATTR:
1913 mntopt_on = MNTOPT_XATTR;
1914 mntopt_off = MNTOPT_NOXATTR;
1917 case ZFS_PROP_NBMAND:
1918 mntopt_on = MNTOPT_NBMAND;
1919 mntopt_off = MNTOPT_NONBMAND;
1927 * Because looking up the mount options is potentially expensive
1928 * (iterating over all of /etc/mnttab), we defer its calculation until
1929 * we're looking up a property which requires its presence.
1931 if (!zhp->zfs_mntcheck &&
1932 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1933 libzfs_handle_t *hdl = zhp->zfs_hdl;
1934 struct mnttab entry;
1936 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1937 zhp->zfs_mntopts = zfs_strdup(hdl,
1939 if (zhp->zfs_mntopts == NULL)
1943 zhp->zfs_mntcheck = B_TRUE;
1946 if (zhp->zfs_mntopts == NULL)
1947 mnt.mnt_mntopts = "";
1949 mnt.mnt_mntopts = zhp->zfs_mntopts;
1952 case ZFS_PROP_ATIME:
1953 case ZFS_PROP_DEVICES:
1955 case ZFS_PROP_READONLY:
1956 case ZFS_PROP_SETUID:
1957 case ZFS_PROP_XATTR:
1958 case ZFS_PROP_NBMAND:
1959 *val = getprop_uint64(zhp, prop, source);
1964 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1967 *src = ZPROP_SRC_TEMPORARY;
1968 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1971 *src = ZPROP_SRC_TEMPORARY;
1975 case ZFS_PROP_CANMOUNT:
1976 case ZFS_PROP_VOLSIZE:
1977 case ZFS_PROP_QUOTA:
1978 case ZFS_PROP_REFQUOTA:
1979 case ZFS_PROP_RESERVATION:
1980 case ZFS_PROP_REFRESERVATION:
1981 case ZFS_PROP_FILESYSTEM_LIMIT:
1982 case ZFS_PROP_SNAPSHOT_LIMIT:
1983 case ZFS_PROP_FILESYSTEM_COUNT:
1984 case ZFS_PROP_SNAPSHOT_COUNT:
1985 *val = getprop_uint64(zhp, prop, source);
1987 if (*source == NULL) {
1988 /* not default, must be local */
1989 *source = zhp->zfs_name;
1993 case ZFS_PROP_MOUNTED:
1994 *val = (zhp->zfs_mntopts != NULL);
1997 case ZFS_PROP_NUMCLONES:
1998 *val = zhp->zfs_dmustats.dds_num_clones;
2001 case ZFS_PROP_VERSION:
2002 case ZFS_PROP_NORMALIZE:
2003 case ZFS_PROP_UTF8ONLY:
2005 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2006 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2008 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2009 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2010 zcmd_free_nvlists(&zc);
2013 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2014 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2016 zcmd_free_nvlists(&zc);
2019 nvlist_free(zplprops);
2020 zcmd_free_nvlists(&zc);
2023 case ZFS_PROP_INCONSISTENT:
2024 *val = zhp->zfs_dmustats.dds_inconsistent;
2028 switch (zfs_prop_get_type(prop)) {
2029 case PROP_TYPE_NUMBER:
2030 case PROP_TYPE_INDEX:
2031 *val = getprop_uint64(zhp, prop, source);
2033 * If we tried to use a default value for a
2034 * readonly property, it means that it was not
2037 if (zfs_prop_readonly(prop) &&
2038 *source != NULL && (*source)[0] == '\0') {
2043 case PROP_TYPE_STRING:
2045 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2046 "cannot get non-numeric property"));
2047 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2048 dgettext(TEXT_DOMAIN, "internal error")));
2056 * Calculate the source type, given the raw source string.
2059 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2060 char *statbuf, size_t statlen)
2062 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2065 if (source == NULL) {
2066 *srctype = ZPROP_SRC_NONE;
2067 } else if (source[0] == '\0') {
2068 *srctype = ZPROP_SRC_DEFAULT;
2069 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2070 *srctype = ZPROP_SRC_RECEIVED;
2072 if (strcmp(source, zhp->zfs_name) == 0) {
2073 *srctype = ZPROP_SRC_LOCAL;
2075 (void) strlcpy(statbuf, source, statlen);
2076 *srctype = ZPROP_SRC_INHERITED;
2083 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2084 size_t proplen, boolean_t literal)
2089 if (zhp->zfs_recvd_props == NULL)
2090 if (get_recvd_props_ioctl(zhp) != 0)
2093 prop = zfs_name_to_prop(propname);
2095 if (prop != ZPROP_INVAL) {
2097 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2099 zfs_set_recvd_props_mode(zhp, &cookie);
2100 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2101 NULL, NULL, 0, literal);
2102 zfs_unset_recvd_props_mode(zhp, &cookie);
2106 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2107 propname, &propval) != 0)
2109 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2111 (void) strlcpy(propbuf, recvdval, proplen);
2114 return (err == 0 ? 0 : -1);
2118 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2123 value = zfs_get_clones_nvl(zhp);
2128 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2129 pair = nvlist_next_nvpair(value, pair)) {
2130 if (propbuf[0] != '\0')
2131 (void) strlcat(propbuf, ",", proplen);
2132 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2138 struct get_clones_arg {
2142 char buf[ZFS_MAX_DATASET_NAME_LEN];
2146 get_clones_cb(zfs_handle_t *zhp, void *arg)
2148 struct get_clones_arg *gca = arg;
2150 if (gca->numclones == 0) {
2155 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2156 NULL, NULL, 0, B_TRUE) != 0)
2158 if (strcmp(gca->buf, gca->origin) == 0) {
2159 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2164 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2170 zfs_get_clones_nvl(zfs_handle_t *zhp)
2172 nvlist_t *nv, *value;
2174 if (nvlist_lookup_nvlist(zhp->zfs_props,
2175 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2176 struct get_clones_arg gca;
2179 * if this is a snapshot, then the kernel wasn't able
2180 * to get the clones. Do it by slowly iterating.
2182 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2184 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2186 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2191 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2193 gca.origin = zhp->zfs_name;
2195 if (gca.numclones != 0) {
2197 char pool[ZFS_MAX_DATASET_NAME_LEN];
2200 /* get the pool name */
2201 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2202 (void) strsep(&cp, "/@");
2203 root = zfs_open(zhp->zfs_hdl, pool,
2204 ZFS_TYPE_FILESYSTEM);
2206 (void) get_clones_cb(root, &gca);
2209 if (gca.numclones != 0 ||
2210 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2211 nvlist_add_nvlist(zhp->zfs_props,
2212 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2219 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2220 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2223 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2229 * Retrieve a property from the given object. If 'literal' is specified, then
2230 * numbers are left as exact values. Otherwise, numbers are converted to a
2231 * human-readable form.
2233 * Returns 0 on success, or -1 on error.
2236 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2237 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2239 char *source = NULL;
2243 boolean_t received = zfs_is_recvd_props_mode(zhp);
2246 * Check to see if this property applies to our object
2248 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2251 if (received && zfs_prop_readonly(prop))
2255 *src = ZPROP_SRC_NONE;
2258 case ZFS_PROP_CREATION:
2260 * 'creation' is a time_t stored in the statistics. We convert
2261 * this into a string unless 'literal' is specified.
2264 val = getprop_uint64(zhp, prop, &source);
2265 time_t time = (time_t)val;
2269 localtime_r(&time, &t) == NULL ||
2270 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2272 (void) snprintf(propbuf, proplen, "%llu", val);
2276 case ZFS_PROP_MOUNTPOINT:
2278 * Getting the precise mountpoint can be tricky.
2280 * - for 'none' or 'legacy', return those values.
2281 * - for inherited mountpoints, we want to take everything
2282 * after our ancestor and append it to the inherited value.
2284 * If the pool has an alternate root, we want to prepend that
2285 * root to any values we return.
2288 str = getprop_string(zhp, prop, &source);
2290 if (str[0] == '/') {
2291 char buf[MAXPATHLEN];
2293 const char *relpath;
2296 * If we inherit the mountpoint, even from a dataset
2297 * with a received value, the source will be the path of
2298 * the dataset we inherit from. If source is
2299 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2302 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2305 relpath = zhp->zfs_name + strlen(source);
2306 if (relpath[0] == '/')
2310 if ((zpool_get_prop(zhp->zpool_hdl,
2311 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2312 B_FALSE)) || (strcmp(root, "-") == 0))
2315 * Special case an alternate root of '/'. This will
2316 * avoid having multiple leading slashes in the
2319 if (strcmp(root, "/") == 0)
2323 * If the mountpoint is '/' then skip over this
2324 * if we are obtaining either an alternate root or
2325 * an inherited mountpoint.
2327 if (str[1] == '\0' && (root[0] != '\0' ||
2328 relpath[0] != '\0'))
2331 if (relpath[0] == '\0')
2332 (void) snprintf(propbuf, proplen, "%s%s",
2335 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2336 root, str, relpath[0] == '@' ? "" : "/",
2339 /* 'legacy' or 'none' */
2340 (void) strlcpy(propbuf, str, proplen);
2345 case ZFS_PROP_ORIGIN:
2346 str = getprop_string(zhp, prop, &source);
2349 (void) strlcpy(propbuf, str, proplen);
2352 case ZFS_PROP_CLONES:
2353 if (get_clones_string(zhp, propbuf, proplen) != 0)
2357 case ZFS_PROP_QUOTA:
2358 case ZFS_PROP_REFQUOTA:
2359 case ZFS_PROP_RESERVATION:
2360 case ZFS_PROP_REFRESERVATION:
2362 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2366 * If quota or reservation is 0, we translate this into 'none'
2367 * (unless literal is set), and indicate that it's the default
2368 * value. Otherwise, we print the number nicely and indicate
2369 * that its set locally.
2373 (void) strlcpy(propbuf, "0", proplen);
2375 (void) strlcpy(propbuf, "none", proplen);
2378 (void) snprintf(propbuf, proplen, "%llu",
2381 zfs_nicenum(val, propbuf, proplen);
2385 case ZFS_PROP_FILESYSTEM_LIMIT:
2386 case ZFS_PROP_SNAPSHOT_LIMIT:
2387 case ZFS_PROP_FILESYSTEM_COUNT:
2388 case ZFS_PROP_SNAPSHOT_COUNT:
2390 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2394 * If limit is UINT64_MAX, we translate this into 'none' (unless
2395 * literal is set), and indicate that it's the default value.
2396 * Otherwise, we print the number nicely and indicate that it's
2400 (void) snprintf(propbuf, proplen, "%llu",
2402 } else if (val == UINT64_MAX) {
2403 (void) strlcpy(propbuf, "none", proplen);
2405 zfs_nicenum(val, propbuf, proplen);
2409 case ZFS_PROP_REFRATIO:
2410 case ZFS_PROP_COMPRESSRATIO:
2411 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2413 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2414 (u_longlong_t)(val / 100),
2415 (u_longlong_t)(val % 100));
2419 switch (zhp->zfs_type) {
2420 case ZFS_TYPE_FILESYSTEM:
2423 case ZFS_TYPE_VOLUME:
2426 case ZFS_TYPE_SNAPSHOT:
2429 case ZFS_TYPE_BOOKMARK:
2435 (void) snprintf(propbuf, proplen, "%s", str);
2438 case ZFS_PROP_MOUNTED:
2440 * The 'mounted' property is a pseudo-property that described
2441 * whether the filesystem is currently mounted. Even though
2442 * it's a boolean value, the typical values of "on" and "off"
2443 * don't make sense, so we translate to "yes" and "no".
2445 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2446 src, &source, &val) != 0)
2449 (void) strlcpy(propbuf, "yes", proplen);
2451 (void) strlcpy(propbuf, "no", proplen);
2456 * The 'name' property is a pseudo-property derived from the
2457 * dataset name. It is presented as a real property to simplify
2460 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2463 case ZFS_PROP_MLSLABEL:
2465 m_label_t *new_sl = NULL;
2466 char *ascii = NULL; /* human readable label */
2468 (void) strlcpy(propbuf,
2469 getprop_string(zhp, prop, &source), proplen);
2471 if (literal || (strcasecmp(propbuf,
2472 ZFS_MLSLABEL_DEFAULT) == 0))
2476 * Try to translate the internal hex string to
2477 * human-readable output. If there are any
2478 * problems just use the hex string.
2481 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2482 L_NO_CORRECTION, NULL) == -1) {
2483 m_label_free(new_sl);
2487 if (label_to_str(new_sl, &ascii, M_LABEL,
2491 m_label_free(new_sl);
2494 m_label_free(new_sl);
2496 (void) strlcpy(propbuf, ascii, proplen);
2503 * GUIDs are stored as numbers, but they are identifiers.
2504 * We don't want them to be pretty printed, because pretty
2505 * printing mangles the ID into a truncated and useless value.
2507 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2509 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2513 switch (zfs_prop_get_type(prop)) {
2514 case PROP_TYPE_NUMBER:
2515 if (get_numeric_property(zhp, prop, src,
2516 &source, &val) != 0)
2519 (void) snprintf(propbuf, proplen, "%llu",
2522 zfs_nicenum(val, propbuf, proplen);
2525 case PROP_TYPE_STRING:
2526 str = getprop_string(zhp, prop, &source);
2529 (void) strlcpy(propbuf, str, proplen);
2532 case PROP_TYPE_INDEX:
2533 if (get_numeric_property(zhp, prop, src,
2534 &source, &val) != 0)
2536 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2538 (void) strlcpy(propbuf, strval, proplen);
2546 get_source(zhp, src, source, statbuf, statlen);
2552 * Utility function to get the given numeric property. Does no validation that
2553 * the given property is the appropriate type; should only be used with
2554 * hard-coded property types.
2557 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2562 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2568 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2572 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2573 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2577 * Similar to zfs_prop_get(), but returns the value as an integer.
2580 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2581 zprop_source_t *src, char *statbuf, size_t statlen)
2586 * Check to see if this property applies to our object
2588 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2589 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2590 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2591 zfs_prop_to_name(prop)));
2595 *src = ZPROP_SRC_NONE;
2597 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2600 get_source(zhp, src, source, statbuf, statlen);
2606 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2607 char **domainp, idmap_rid_t *ridp)
2609 idmap_get_handle_t *get_hdl = NULL;
2613 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2617 err = idmap_get_sidbyuid(get_hdl, id,
2618 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2620 err = idmap_get_sidbygid(get_hdl, id,
2621 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2623 if (err == IDMAP_SUCCESS &&
2624 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2625 status == IDMAP_SUCCESS)
2631 idmap_get_destroy(get_hdl);
2636 * convert the propname into parameters needed by kernel
2637 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2638 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2641 userquota_propname_decode(const char *propname, boolean_t zoned,
2642 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2644 zfs_userquota_prop_t type;
2646 char *numericsid = NULL;
2651 /* Figure out the property type ({user|group}{quota|space}) */
2652 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2653 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2654 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2657 if (type == ZFS_NUM_USERQUOTA_PROPS)
2661 isuser = (type == ZFS_PROP_USERQUOTA ||
2662 type == ZFS_PROP_USERUSED);
2664 cp = strchr(propname, '@') + 1;
2666 if (strchr(cp, '@')) {
2668 * It's a SID name (eg "user@domain") that needs to be
2669 * turned into S-1-domainID-RID.
2672 idmap_stat stat, map_stat;
2675 idmap_get_handle_t *gh = NULL;
2677 stat = idmap_get_create(&gh);
2678 if (stat != IDMAP_SUCCESS) {
2679 idmap_get_destroy(gh);
2682 if (zoned && getzoneid() == GLOBAL_ZONEID)
2685 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2688 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2691 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2694 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2698 idmap_get_destroy(gh);
2701 stat = idmap_get_mappings(gh);
2702 idmap_get_destroy(gh);
2707 if (numericsid == NULL)
2711 /* will be further decoded below */
2714 if (strncmp(cp, "S-1-", 4) == 0) {
2715 /* It's a numeric SID (eg "S-1-234-567-89") */
2716 (void) strlcpy(domain, cp, domainlen);
2719 cp = strrchr(domain, '-');
2722 *ridp = strtoull(cp, &end, 10);
2730 if (errno != 0 || *end != '\0')
2732 } else if (!isdigit(*cp)) {
2734 * It's a user/group name (eg "user") that needs to be
2735 * turned into a uid/gid
2737 if (zoned && getzoneid() == GLOBAL_ZONEID)
2753 /* It's a user/group ID (eg "12345"). */
2754 uid_t id = strtoul(cp, &end, 10);
2761 /* It's an ephemeral ID. */
2762 if (idmap_id_to_numeric_domain_rid(id, isuser,
2763 &mapdomain, &rid) != 0)
2765 (void) strlcpy(domain, mapdomain, domainlen);
2772 ASSERT3P(numericsid, ==, NULL);
2777 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2778 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2781 zfs_cmd_t zc = { 0 };
2783 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2785 err = userquota_propname_decode(propname,
2786 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2787 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2788 zc.zc_objset_type = *typep;
2792 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2796 *propvalue = zc.zc_cookie;
2801 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2802 uint64_t *propvalue)
2804 zfs_userquota_prop_t type;
2806 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2811 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2812 char *propbuf, int proplen, boolean_t literal)
2816 zfs_userquota_prop_t type;
2818 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2825 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2826 } else if (propvalue == 0 &&
2827 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2828 (void) strlcpy(propbuf, "none", proplen);
2830 zfs_nicenum(propvalue, propbuf, proplen);
2836 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2837 uint64_t *propvalue)
2840 zfs_cmd_t zc = { 0 };
2841 const char *snapname;
2843 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2845 snapname = strchr(propname, '@') + 1;
2846 if (strchr(snapname, '@')) {
2847 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2849 /* snapname is the short name, append it to zhp's fsname */
2852 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2853 sizeof (zc.zc_value));
2854 cp = strchr(zc.zc_value, '@');
2857 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2858 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2861 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2865 *propvalue = zc.zc_cookie;
2870 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2871 char *propbuf, int proplen, boolean_t literal)
2876 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2882 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2884 zfs_nicenum(propvalue, propbuf, proplen);
2890 * Returns the name of the given zfs handle.
2893 zfs_get_name(const zfs_handle_t *zhp)
2895 return (zhp->zfs_name);
2899 * Returns the name of the parent pool for the given zfs handle.
2902 zfs_get_pool_name(const zfs_handle_t *zhp)
2904 return (zhp->zpool_hdl->zpool_name);
2908 * Returns the type of the given zfs handle.
2911 zfs_get_type(const zfs_handle_t *zhp)
2913 return (zhp->zfs_type);
2917 * Is one dataset name a child dataset of another?
2919 * Needs to handle these cases:
2920 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2921 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2922 * Descendant? No. No. No. Yes.
2925 is_descendant(const char *ds1, const char *ds2)
2927 size_t d1len = strlen(ds1);
2929 /* ds2 can't be a descendant if it's smaller */
2930 if (strlen(ds2) < d1len)
2933 /* otherwise, compare strings and verify that there's a '/' char */
2934 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2938 * Given a complete name, return just the portion that refers to the parent.
2939 * Will return -1 if there is no parent (path is just the name of the
2943 parent_name(const char *path, char *buf, size_t buflen)
2947 (void) strlcpy(buf, path, buflen);
2949 if ((slashp = strrchr(buf, '/')) == NULL)
2957 * If accept_ancestor is false, then check to make sure that the given path has
2958 * a parent, and that it exists. If accept_ancestor is true, then find the
2959 * closest existing ancestor for the given path. In prefixlen return the
2960 * length of already existing prefix of the given path. We also fetch the
2961 * 'zoned' property, which is used to validate property settings when creating
2965 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2966 boolean_t accept_ancestor, int *prefixlen)
2968 zfs_cmd_t zc = { 0 };
2969 char parent[ZFS_MAX_DATASET_NAME_LEN];
2975 (void) snprintf(errbuf, sizeof (errbuf),
2976 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2978 /* get parent, and check to see if this is just a pool */
2979 if (parent_name(path, parent, sizeof (parent)) != 0) {
2980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2981 "missing dataset name"));
2982 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2985 /* check to see if the pool exists */
2986 if ((slash = strchr(parent, '/')) == NULL)
2987 slash = parent + strlen(parent);
2988 (void) strncpy(zc.zc_name, parent, slash - parent);
2989 zc.zc_name[slash - parent] = '\0';
2990 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2993 "no such pool '%s'"), zc.zc_name);
2994 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2997 /* check to see if the parent dataset exists */
2998 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2999 if (errno == ENOENT && accept_ancestor) {
3001 * Go deeper to find an ancestor, give up on top level.
3003 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3004 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3005 "no such pool '%s'"), zc.zc_name);
3006 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3008 } else if (errno == ENOENT) {
3009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3010 "parent does not exist"));
3011 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3013 return (zfs_standard_error(hdl, errno, errbuf));
3016 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3020 /* we are in a non-global zone, but parent is in the global zone */
3021 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3022 (void) zfs_standard_error(hdl, EPERM, errbuf);
3027 /* make sure parent is a filesystem */
3028 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3029 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3030 "parent is not a filesystem"));
3031 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3037 if (prefixlen != NULL)
3038 *prefixlen = strlen(parent);
3043 * Finds whether the dataset of the given type(s) exists.
3046 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3050 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3054 * Try to get stats for the dataset, which will tell us if it exists.
3056 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3057 int ds_type = zhp->zfs_type;
3060 if (types & ds_type)
3067 * Given a path to 'target', create all the ancestors between
3068 * the prefixlen portion of the path, and the target itself.
3069 * Fail if the initial prefixlen-ancestor does not already exist.
3072 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3078 /* make sure prefix exists */
3079 cp = target + prefixlen;
3081 assert(strchr(cp, '/') == NULL);
3082 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3085 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3093 * Attempt to create, mount, and share any ancestor filesystems,
3094 * up to the prefixlen-long one.
3096 for (cp = target + prefixlen + 1;
3097 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3101 h = make_dataset_handle(hdl, target);
3103 /* it already exists, nothing to do here */
3108 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3110 opname = dgettext(TEXT_DOMAIN, "create");
3114 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3116 opname = dgettext(TEXT_DOMAIN, "open");
3120 if (zfs_mount(h, NULL, 0) != 0) {
3121 opname = dgettext(TEXT_DOMAIN, "mount");
3125 if (zfs_share(h) != 0) {
3126 opname = dgettext(TEXT_DOMAIN, "share");
3136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3137 "failed to %s ancestor '%s'"), opname, target);
3142 * Creates non-existing ancestors of the given path.
3145 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3151 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3154 if ((path_copy = strdup(path)) != NULL) {
3155 rc = create_parents(hdl, path_copy, prefix);
3158 if (path_copy == NULL || rc != 0)
3165 * Create a new filesystem or volume.
3168 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3173 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3176 enum lzc_dataset_type ost;
3178 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3179 "cannot create '%s'"), path);
3181 /* validate the path, taking care to note the extended error message */
3182 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3183 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3185 /* validate parents exist */
3186 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3190 * The failure modes when creating a dataset of a different type over
3191 * one that already exists is a little strange. In particular, if you
3192 * try to create a dataset on top of an existing dataset, the ioctl()
3193 * will return ENOENT, not EEXIST. To prevent this from happening, we
3194 * first try to see if the dataset exists.
3196 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3198 "dataset already exists"));
3199 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3202 if (type == ZFS_TYPE_VOLUME)
3203 ost = LZC_DATSET_TYPE_ZVOL;
3205 ost = LZC_DATSET_TYPE_ZFS;
3207 /* open zpool handle for prop validation */
3208 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3209 (void) strlcpy(pool_path, path, sizeof (pool_path));
3211 /* truncate pool_path at first slash */
3212 char *p = strchr(pool_path, '/');
3216 zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path);
3218 if (props && (props = zfs_valid_proplist(hdl, type, props,
3219 zoned, NULL, zpool_handle, errbuf)) == 0) {
3220 zpool_close(zpool_handle);
3223 zpool_close(zpool_handle);
3225 if (type == ZFS_TYPE_VOLUME) {
3227 * If we are creating a volume, the size and block size must
3228 * satisfy a few restraints. First, the blocksize must be a
3229 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3230 * volsize must be a multiple of the block size, and cannot be
3233 if (props == NULL || nvlist_lookup_uint64(props,
3234 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3236 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3237 "missing volume size"));
3238 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3241 if ((ret = nvlist_lookup_uint64(props,
3242 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3243 &blocksize)) != 0) {
3244 if (ret == ENOENT) {
3245 blocksize = zfs_prop_default_numeric(
3246 ZFS_PROP_VOLBLOCKSIZE);
3249 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3250 "missing volume block size"));
3251 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3257 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3258 "volume size cannot be zero"));
3259 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3262 if (size % blocksize != 0) {
3264 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3265 "volume size must be a multiple of volume block "
3267 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3271 /* create the dataset */
3272 ret = lzc_create(path, ost, props);
3275 /* check for failure */
3277 char parent[ZFS_MAX_DATASET_NAME_LEN];
3278 (void) parent_name(path, parent, sizeof (parent));
3282 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3283 "no such parent '%s'"), parent);
3284 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3287 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3288 "parent '%s' is not a filesystem"), parent);
3289 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3293 "pool must be upgraded to set this "
3294 "property or value"));
3295 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3299 * This platform can't address a volume this big.
3301 if (type == ZFS_TYPE_VOLUME)
3302 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3307 return (zfs_standard_error(hdl, errno, errbuf));
3315 * Destroys the given dataset. The caller must make sure that the filesystem
3316 * isn't mounted, and that there are no active dependents. If the file system
3317 * does not exist this function does nothing.
3320 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3322 zfs_cmd_t zc = { 0 };
3324 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3325 nvlist_t *nv = fnvlist_alloc();
3326 fnvlist_add_boolean(nv, zhp->zfs_name);
3327 int error = lzc_destroy_bookmarks(nv, NULL);
3330 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3331 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3337 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3339 if (ZFS_IS_VOLUME(zhp)) {
3340 zc.zc_objset_type = DMU_OST_ZVOL;
3342 zc.zc_objset_type = DMU_OST_ZFS;
3345 zc.zc_defer_destroy = defer;
3346 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3348 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3349 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3353 remove_mountpoint(zhp);
3358 struct destroydata {
3360 const char *snapname;
3364 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3366 struct destroydata *dd = arg;
3367 char name[ZFS_MAX_DATASET_NAME_LEN];
3370 (void) snprintf(name, sizeof (name),
3371 "%s@%s", zhp->zfs_name, dd->snapname);
3373 if (lzc_exists(name))
3374 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3376 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3382 * Destroys all snapshots with the given name in zhp & descendants.
3385 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3388 struct destroydata dd = { 0 };
3390 dd.snapname = snapname;
3391 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3392 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3394 if (nvlist_empty(dd.nvl)) {
3395 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3396 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3397 zhp->zfs_name, snapname);
3399 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3401 nvlist_free(dd.nvl);
3406 * Destroys all the snapshots named in the nvlist.
3409 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3412 nvlist_t *errlist = NULL;
3414 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3417 nvlist_free(errlist);
3421 if (nvlist_empty(errlist)) {
3423 (void) snprintf(errbuf, sizeof (errbuf),
3424 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3426 ret = zfs_standard_error(hdl, ret, errbuf);
3428 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3429 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3431 (void) snprintf(errbuf, sizeof (errbuf),
3432 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3435 switch (fnvpair_value_int32(pair)) {
3438 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3439 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3442 ret = zfs_standard_error(hdl, errno, errbuf);
3447 nvlist_free(errlist);
3452 * Clones the given dataset. The target must be of the same type as the source.
3455 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3457 char parent[ZFS_MAX_DATASET_NAME_LEN];
3460 libzfs_handle_t *hdl = zhp->zfs_hdl;
3463 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3465 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3466 "cannot create '%s'"), target);
3468 /* validate the target/clone name */
3469 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3470 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3472 /* validate parents exist */
3473 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3476 (void) parent_name(target, parent, sizeof (parent));
3482 if (ZFS_IS_VOLUME(zhp)) {
3483 type = ZFS_TYPE_VOLUME;
3485 type = ZFS_TYPE_FILESYSTEM;
3487 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3488 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3492 ret = lzc_clone(target, zhp->zfs_name, props);
3500 * The parent doesn't exist. We should have caught this
3501 * above, but there may a race condition that has since
3502 * destroyed the parent.
3504 * At this point, we don't know whether it's the source
3505 * that doesn't exist anymore, or whether the target
3506 * dataset doesn't exist.
3508 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3509 "no such parent '%s'"), parent);
3510 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3513 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3514 "source and target pools differ"));
3515 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3519 return (zfs_standard_error(zhp->zfs_hdl, errno,
3528 * Promotes the given clone fs to be the clone parent.
3531 zfs_promote(zfs_handle_t *zhp)
3533 libzfs_handle_t *hdl = zhp->zfs_hdl;
3534 zfs_cmd_t zc = { 0 };
3535 char parent[MAXPATHLEN];
3539 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3540 "cannot promote '%s'"), zhp->zfs_name);
3542 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3544 "snapshots can not be promoted"));
3545 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3548 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3549 if (parent[0] == '\0') {
3550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3551 "not a cloned filesystem"));
3552 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3555 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3556 sizeof (zc.zc_value));
3557 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3558 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3561 int save_errno = errno;
3563 switch (save_errno) {
3565 /* There is a conflicting snapshot name. */
3566 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3567 "conflicting snapshot '%s' from parent '%s'"),
3568 zc.zc_string, parent);
3569 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3572 return (zfs_standard_error(hdl, save_errno, errbuf));
3578 typedef struct snapdata {
3580 const char *sd_snapname;
3584 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3586 snapdata_t *sd = arg;
3587 char name[ZFS_MAX_DATASET_NAME_LEN];
3590 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3591 (void) snprintf(name, sizeof (name),
3592 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3594 fnvlist_add_boolean(sd->sd_nvl, name);
3596 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3604 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3608 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3615 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3616 "cannot create snapshots "));
3619 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3620 const char *snapname = nvpair_name(elem);
3622 /* validate the target name */
3623 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3625 (void) snprintf(errbuf, sizeof (errbuf),
3626 dgettext(TEXT_DOMAIN,
3627 "cannot create snapshot '%s'"), snapname);
3628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3633 * get pool handle for prop validation. assumes all snaps are in the
3634 * same pool, as does lzc_snapshot (below).
3636 char pool[ZFS_MAX_DATASET_NAME_LEN];
3637 elem = nvlist_next_nvpair(snaps, NULL);
3638 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3639 pool[strcspn(pool, "/@")] = '\0';
3640 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3642 if (props != NULL &&
3643 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3644 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3645 zpool_close(zpool_hdl);
3648 zpool_close(zpool_hdl);
3650 ret = lzc_snapshot(snaps, props, &errors);
3653 boolean_t printed = B_FALSE;
3654 for (elem = nvlist_next_nvpair(errors, NULL);
3656 elem = nvlist_next_nvpair(errors, elem)) {
3657 (void) snprintf(errbuf, sizeof (errbuf),
3658 dgettext(TEXT_DOMAIN,
3659 "cannot create snapshot '%s'"), nvpair_name(elem));
3660 (void) zfs_standard_error(hdl,
3661 fnvpair_value_int32(elem), errbuf);
3667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3668 "multiple snapshots of same "
3670 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3674 (void) zfs_standard_error(hdl, ret, errbuf);
3680 nvlist_free(errors);
3685 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3689 snapdata_t sd = { 0 };
3690 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3695 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3696 "cannot snapshot %s"), path);
3698 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3699 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3701 (void) strlcpy(fsname, path, sizeof (fsname));
3702 cp = strchr(fsname, '@');
3704 sd.sd_snapname = cp + 1;
3706 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3707 ZFS_TYPE_VOLUME)) == NULL) {
3711 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3713 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3715 fnvlist_add_boolean(sd.sd_nvl, path);
3718 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3719 nvlist_free(sd.sd_nvl);
3725 * Destroy any more recent snapshots. We invoke this callback on any dependents
3726 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3727 * is a dependent and we should just destroy it without checking the transaction
3730 typedef struct rollback_data {
3731 const char *cb_target; /* the snapshot */
3732 uint64_t cb_create; /* creation time reference */
3738 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3740 rollback_data_t *cbp = data;
3741 prop_changelist_t *clp;
3743 /* We must destroy this clone; first unmount it */
3744 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3745 cbp->cb_force ? MS_FORCE: 0);
3746 if (clp == NULL || changelist_prefix(clp) != 0) {
3747 cbp->cb_error = B_TRUE;
3751 if (zfs_destroy(zhp, B_FALSE) != 0)
3752 cbp->cb_error = B_TRUE;
3754 changelist_remove(clp, zhp->zfs_name);
3755 (void) changelist_postfix(clp);
3756 changelist_free(clp);
3763 rollback_destroy(zfs_handle_t *zhp, void *data)
3765 rollback_data_t *cbp = data;
3767 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3768 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3769 rollback_destroy_dependent, cbp);
3771 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3779 * Given a dataset, rollback to a specific snapshot, discarding any
3780 * data changes since then and making it the active dataset.
3782 * Any snapshots and bookmarks more recent than the target are
3783 * destroyed, along with their dependents (i.e. clones).
3786 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3788 rollback_data_t cb = { 0 };
3790 boolean_t restore_resv = 0;
3791 uint64_t old_volsize = 0, new_volsize;
3792 zfs_prop_t resv_prop;
3794 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3795 zhp->zfs_type == ZFS_TYPE_VOLUME);
3798 * Destroy all recent snapshots and their dependents.
3800 cb.cb_force = force;
3801 cb.cb_target = snap->zfs_name;
3802 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3803 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3804 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3810 * Now that we have verified that the snapshot is the latest,
3811 * rollback to the given snapshot.
3814 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3815 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3817 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3819 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3823 * We rely on zfs_iter_children() to verify that there are no
3824 * newer snapshots for the given dataset. Therefore, we can
3825 * simply pass the name on to the ioctl() call. There is still
3826 * an unlikely race condition where the user has taken a
3827 * snapshot since we verified that this was the most recent.
3829 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3831 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3832 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3838 * For volumes, if the pre-rollback volsize matched the pre-
3839 * rollback reservation and the volsize has changed then set
3840 * the reservation property to the post-rollback volsize.
3841 * Make a new handle since the rollback closed the dataset.
3843 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3844 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3846 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3847 if (old_volsize != new_volsize)
3848 err = zfs_prop_set_int(zhp, resv_prop,
3857 * Renames the given dataset.
3860 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3861 boolean_t force_unmount)
3864 zfs_cmd_t zc = { 0 };
3866 prop_changelist_t *cl = NULL;
3867 zfs_handle_t *zhrp = NULL;
3868 char *parentname = NULL;
3869 char parent[ZFS_MAX_DATASET_NAME_LEN];
3870 libzfs_handle_t *hdl = zhp->zfs_hdl;
3873 /* if we have the same exact name, just return success */
3874 if (strcmp(zhp->zfs_name, target) == 0)
3877 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3878 "cannot rename to '%s'"), target);
3881 * Make sure the target name is valid
3883 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3884 if ((strchr(target, '@') == NULL) ||
3887 * Snapshot target name is abbreviated,
3888 * reconstruct full dataset name
3890 (void) strlcpy(parent, zhp->zfs_name,
3892 delim = strchr(parent, '@');
3893 if (strchr(target, '@') == NULL)
3897 (void) strlcat(parent, target, sizeof (parent));
3901 * Make sure we're renaming within the same dataset.
3903 delim = strchr(target, '@');
3904 if (strncmp(zhp->zfs_name, target, delim - target)
3905 != 0 || zhp->zfs_name[delim - target] != '@') {
3906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3907 "snapshots must be part of same "
3909 return (zfs_error(hdl, EZFS_CROSSTARGET,
3913 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3914 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3918 "recursive rename must be a snapshot"));
3919 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3922 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3923 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3925 /* validate parents */
3926 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3929 /* make sure we're in the same pool */
3930 verify((delim = strchr(target, '/')) != NULL);
3931 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3932 zhp->zfs_name[delim - target] != '/') {
3933 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3934 "datasets must be within same pool"));
3935 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3938 /* new name cannot be a child of the current dataset name */
3939 if (is_descendant(zhp->zfs_name, target)) {
3940 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3941 "New dataset name cannot be a descendant of "
3942 "current dataset name"));
3943 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3947 (void) snprintf(errbuf, sizeof (errbuf),
3948 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3950 if (getzoneid() == GLOBAL_ZONEID &&
3951 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3953 "dataset is used in a non-global zone"));
3954 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3958 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3959 if (parentname == NULL) {
3963 delim = strchr(parentname, '@');
3965 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3970 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
3971 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3972 force_unmount ? MS_FORCE : 0)) == NULL)
3975 if (changelist_haszonedchild(cl)) {
3976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3977 "child dataset with inherited mountpoint is used "
3978 "in a non-global zone"));
3979 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3984 if ((ret = changelist_prefix(cl)) != 0)
3988 if (ZFS_IS_VOLUME(zhp))
3989 zc.zc_objset_type = DMU_OST_ZVOL;
3991 zc.zc_objset_type = DMU_OST_ZFS;
3993 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3994 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3996 zc.zc_cookie = recursive;
3998 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4000 * if it was recursive, the one that actually failed will
4003 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4004 "cannot rename '%s'"), zc.zc_name);
4006 if (recursive && errno == EEXIST) {
4007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4008 "a child dataset already has a snapshot "
4009 "with the new name"));
4010 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4012 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4016 * On failure, we still want to remount any filesystems that
4017 * were previously mounted, so we don't alter the system state.
4020 (void) changelist_postfix(cl);
4023 changelist_rename(cl, zfs_get_name(zhp), target);
4024 ret = changelist_postfix(cl);
4029 if (parentname != NULL) {
4036 changelist_free(cl);
4042 zfs_get_user_props(zfs_handle_t *zhp)
4044 return (zhp->zfs_user_props);
4048 zfs_get_recvd_props(zfs_handle_t *zhp)
4050 if (zhp->zfs_recvd_props == NULL)
4051 if (get_recvd_props_ioctl(zhp) != 0)
4053 return (zhp->zfs_recvd_props);
4057 * This function is used by 'zfs list' to determine the exact set of columns to
4058 * display, and their maximum widths. This does two main things:
4060 * - If this is a list of all properties, then expand the list to include
4061 * all native properties, and set a flag so that for each dataset we look
4062 * for new unique user properties and add them to the list.
4064 * - For non fixed-width properties, keep track of the maximum width seen
4065 * so that we can size the column appropriately. If the user has
4066 * requested received property values, we also need to compute the width
4067 * of the RECEIVED column.
4070 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4073 libzfs_handle_t *hdl = zhp->zfs_hdl;
4074 zprop_list_t *entry;
4075 zprop_list_t **last, **start;
4076 nvlist_t *userprops, *propval;
4079 char buf[ZFS_MAXPROPLEN];
4081 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4084 userprops = zfs_get_user_props(zhp);
4087 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4089 * Go through and add any user properties as necessary. We
4090 * start by incrementing our list pointer to the first
4091 * non-native property.
4094 while (*start != NULL) {
4095 if ((*start)->pl_prop == ZPROP_INVAL)
4097 start = &(*start)->pl_next;
4101 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4103 * See if we've already found this property in our list.
4105 for (last = start; *last != NULL;
4106 last = &(*last)->pl_next) {
4107 if (strcmp((*last)->pl_user_prop,
4108 nvpair_name(elem)) == 0)
4112 if (*last == NULL) {
4113 if ((entry = zfs_alloc(hdl,
4114 sizeof (zprop_list_t))) == NULL ||
4115 ((entry->pl_user_prop = zfs_strdup(hdl,
4116 nvpair_name(elem)))) == NULL) {
4121 entry->pl_prop = ZPROP_INVAL;
4122 entry->pl_width = strlen(nvpair_name(elem));
4123 entry->pl_all = B_TRUE;
4130 * Now go through and check the width of any non-fixed columns
4132 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4133 if (entry->pl_fixed && !literal)
4136 if (entry->pl_prop != ZPROP_INVAL) {
4137 if (zfs_prop_get(zhp, entry->pl_prop,
4138 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4139 if (strlen(buf) > entry->pl_width)
4140 entry->pl_width = strlen(buf);
4142 if (received && zfs_prop_get_recvd(zhp,
4143 zfs_prop_to_name(entry->pl_prop),
4144 buf, sizeof (buf), literal) == 0)
4145 if (strlen(buf) > entry->pl_recvd_width)
4146 entry->pl_recvd_width = strlen(buf);
4148 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4150 verify(nvlist_lookup_string(propval,
4151 ZPROP_VALUE, &strval) == 0);
4152 if (strlen(strval) > entry->pl_width)
4153 entry->pl_width = strlen(strval);
4155 if (received && zfs_prop_get_recvd(zhp,
4156 entry->pl_user_prop,
4157 buf, sizeof (buf), literal) == 0)
4158 if (strlen(buf) > entry->pl_recvd_width)
4159 entry->pl_recvd_width = strlen(buf);
4167 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4168 char *resource, void *export, void *sharetab,
4169 int sharemax, zfs_share_op_t operation)
4171 zfs_cmd_t zc = { 0 };
4174 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4175 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4177 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4178 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4179 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4180 zc.zc_share.z_sharetype = operation;
4181 zc.zc_share.z_sharemax = sharemax;
4182 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4187 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4192 * Keep a reference to the props-table against which we prune the
4195 zhp->zfs_props_table = props;
4197 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4200 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4201 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4204 * User properties will result in ZPROP_INVAL, and since we
4205 * only know how to prune standard ZFS properties, we always
4206 * leave these in the list. This can also happen if we
4207 * encounter an unknown DSL property (when running older
4208 * software, for example).
4210 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4211 (void) nvlist_remove(zhp->zfs_props,
4212 nvpair_name(curr), nvpair_type(curr));
4218 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4219 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4221 zfs_cmd_t zc = { 0 };
4222 nvlist_t *nvlist = NULL;
4225 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4226 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4227 zc.zc_cookie = (uint64_t)cmd;
4229 if (cmd == ZFS_SMB_ACL_RENAME) {
4230 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4231 (void) no_memory(hdl);
4237 case ZFS_SMB_ACL_ADD:
4238 case ZFS_SMB_ACL_REMOVE:
4239 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4241 case ZFS_SMB_ACL_RENAME:
4242 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4244 (void) no_memory(hdl);
4247 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4249 (void) no_memory(hdl);
4252 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4253 nvlist_free(nvlist);
4257 case ZFS_SMB_ACL_PURGE:
4262 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4263 nvlist_free(nvlist);
4268 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4269 char *path, char *resource)
4271 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4276 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4277 char *path, char *resource)
4279 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4284 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4286 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4291 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4292 char *oldname, char *newname)
4294 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4299 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4300 zfs_userspace_cb_t func, void *arg)
4302 zfs_cmd_t zc = { 0 };
4303 zfs_useracct_t buf[100];
4304 libzfs_handle_t *hdl = zhp->zfs_hdl;
4307 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4309 zc.zc_objset_type = type;
4310 zc.zc_nvlist_dst = (uintptr_t)buf;
4313 zfs_useracct_t *zua = buf;
4315 zc.zc_nvlist_dst_size = sizeof (buf);
4316 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4319 (void) snprintf(errbuf, sizeof (errbuf),
4320 dgettext(TEXT_DOMAIN,
4321 "cannot get used/quota for %s"), zc.zc_name);
4322 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4324 if (zc.zc_nvlist_dst_size == 0)
4327 while (zc.zc_nvlist_dst_size > 0) {
4328 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4329 zua->zu_space)) != 0)
4332 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4341 const char *snapname;
4343 boolean_t recursive;
4348 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4350 struct holdarg *ha = arg;
4351 char name[ZFS_MAX_DATASET_NAME_LEN];
4354 (void) snprintf(name, sizeof (name),
4355 "%s@%s", zhp->zfs_name, ha->snapname);
4357 if (lzc_exists(name))
4358 fnvlist_add_string(ha->nvl, name, ha->tag);
4361 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4367 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4368 boolean_t recursive, int cleanup_fd)
4373 ha.nvl = fnvlist_alloc();
4374 ha.snapname = snapname;
4376 ha.recursive = recursive;
4377 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4379 if (nvlist_empty(ha.nvl)) {
4382 fnvlist_free(ha.nvl);
4384 (void) snprintf(errbuf, sizeof (errbuf),
4385 dgettext(TEXT_DOMAIN,
4386 "cannot hold snapshot '%s@%s'"),
4387 zhp->zfs_name, snapname);
4388 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4392 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4393 fnvlist_free(ha.nvl);
4399 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4403 libzfs_handle_t *hdl = zhp->zfs_hdl;
4408 ret = lzc_hold(holds, cleanup_fd, &errors);
4411 /* There may be errors even in the success case. */
4412 fnvlist_free(errors);
4416 if (nvlist_empty(errors)) {
4417 /* no hold-specific errors */
4418 (void) snprintf(errbuf, sizeof (errbuf),
4419 dgettext(TEXT_DOMAIN, "cannot hold"));
4422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4423 "pool must be upgraded"));
4424 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4427 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4430 (void) zfs_standard_error(hdl, ret, errbuf);
4434 for (elem = nvlist_next_nvpair(errors, NULL);
4436 elem = nvlist_next_nvpair(errors, elem)) {
4437 (void) snprintf(errbuf, sizeof (errbuf),
4438 dgettext(TEXT_DOMAIN,
4439 "cannot hold snapshot '%s'"), nvpair_name(elem));
4440 switch (fnvpair_value_int32(elem)) {
4443 * Temporary tags wind up having the ds object id
4444 * prepended. So even if we passed the length check
4445 * above, it's still possible for the tag to wind
4446 * up being slightly too long.
4448 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4451 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4454 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4457 (void) zfs_standard_error(hdl,
4458 fnvpair_value_int32(elem), errbuf);
4462 fnvlist_free(errors);
4467 zfs_release_one(zfs_handle_t *zhp, void *arg)
4469 struct holdarg *ha = arg;
4470 char name[ZFS_MAX_DATASET_NAME_LEN];
4472 nvlist_t *existing_holds;
4474 (void) snprintf(name, sizeof (name),
4475 "%s@%s", zhp->zfs_name, ha->snapname);
4477 if (lzc_get_holds(name, &existing_holds) != 0) {
4479 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4482 nvlist_t *torelease = fnvlist_alloc();
4483 fnvlist_add_boolean(torelease, ha->tag);
4484 fnvlist_add_nvlist(ha->nvl, name, torelease);
4485 fnvlist_free(torelease);
4489 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4495 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4496 boolean_t recursive)
4500 nvlist_t *errors = NULL;
4502 libzfs_handle_t *hdl = zhp->zfs_hdl;
4505 ha.nvl = fnvlist_alloc();
4506 ha.snapname = snapname;
4508 ha.recursive = recursive;
4510 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4512 if (nvlist_empty(ha.nvl)) {
4513 fnvlist_free(ha.nvl);
4515 (void) snprintf(errbuf, sizeof (errbuf),
4516 dgettext(TEXT_DOMAIN,
4517 "cannot release hold from snapshot '%s@%s'"),
4518 zhp->zfs_name, snapname);
4520 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4522 (void) zfs_standard_error(hdl, ret, errbuf);
4527 ret = lzc_release(ha.nvl, &errors);
4528 fnvlist_free(ha.nvl);
4531 /* There may be errors even in the success case. */
4532 fnvlist_free(errors);
4536 if (nvlist_empty(errors)) {
4537 /* no hold-specific errors */
4538 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4543 "pool must be upgraded"));
4544 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4547 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4551 for (elem = nvlist_next_nvpair(errors, NULL);
4553 elem = nvlist_next_nvpair(errors, elem)) {
4554 (void) snprintf(errbuf, sizeof (errbuf),
4555 dgettext(TEXT_DOMAIN,
4556 "cannot release hold from snapshot '%s'"),
4558 switch (fnvpair_value_int32(elem)) {
4560 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4563 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4566 (void) zfs_standard_error_fmt(hdl,
4567 fnvpair_value_int32(elem), errbuf);
4571 fnvlist_free(errors);
4576 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4578 zfs_cmd_t zc = { 0 };
4579 libzfs_handle_t *hdl = zhp->zfs_hdl;
4585 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4586 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4590 nvbuf = malloc(nvsz);
4591 if (nvbuf == NULL) {
4592 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4596 zc.zc_nvlist_dst_size = nvsz;
4597 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4599 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4601 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4602 (void) snprintf(errbuf, sizeof (errbuf),
4603 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4608 nvsz = zc.zc_nvlist_dst_size;
4612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4613 "pool must be upgraded"));
4614 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4617 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4620 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4623 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4628 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4630 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4631 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4633 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4643 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4645 zfs_cmd_t zc = { 0 };
4646 libzfs_handle_t *hdl = zhp->zfs_hdl;
4652 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4653 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4655 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4658 nvbuf = malloc(nvsz);
4660 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4663 zc.zc_nvlist_src_size = nvsz;
4664 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4665 zc.zc_perm_action = un;
4667 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4669 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4670 (void) snprintf(errbuf, sizeof (errbuf),
4671 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4675 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4676 "pool must be upgraded"));
4677 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4680 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4683 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4686 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4697 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4702 err = lzc_get_holds(zhp->zfs_name, nvl);
4705 libzfs_handle_t *hdl = zhp->zfs_hdl;
4707 (void) snprintf(errbuf, sizeof (errbuf),
4708 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4712 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4713 "pool must be upgraded"));
4714 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4717 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4720 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4723 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4732 * Convert the zvol's volume size to an appropriate reservation.
4733 * Note: If this routine is updated, it is necessary to update the ZFS test
4734 * suite's shell version in reservation.kshlib.
4737 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4740 uint64_t nblocks, volblocksize;
4744 if (nvlist_lookup_string(props,
4745 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4746 ncopies = atoi(strval);
4749 if (nvlist_lookup_uint64(props,
4750 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4751 &volblocksize) != 0)
4752 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4753 nblocks = volsize/volblocksize;
4754 /* start with metadnode L0-L6 */
4756 /* calculate number of indirects */
4757 while (nblocks > 1) {
4758 nblocks += DNODES_PER_LEVEL - 1;
4759 nblocks /= DNODES_PER_LEVEL;
4762 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4765 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4766 * compressed, but in practice they compress down to about
4769 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;