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) 2010 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
41 #include <sys/mntent.h>
42 #include <sys/mount.h>
51 #include <directory.h>
52 #endif /* HAVE_IDMAP */
54 #include <sys/dnode.h>
59 #include "zfs_namecheck.h"
61 #include "libzfs_impl.h"
62 #include "zfs_deleg.h"
64 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
65 static int userquota_propname_decode(const char *propname, boolean_t zoned,
66 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
69 * Given a single type (not a mask of types), return the type in a human
73 zfs_type_to_name(zfs_type_t type)
76 case ZFS_TYPE_FILESYSTEM:
77 return (dgettext(TEXT_DOMAIN, "filesystem"));
78 case ZFS_TYPE_SNAPSHOT:
79 return (dgettext(TEXT_DOMAIN, "snapshot"));
81 return (dgettext(TEXT_DOMAIN, "volume"));
90 * Validate a ZFS path. This is used even before trying to open the dataset, to
91 * provide a more meaningful error message. We call zfs_error_aux() to
92 * explain exactly why the name was not valid.
95 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
101 (void) zfs_prop_get_table();
102 if (dataset_namecheck(path, &why, &what) != 0) {
105 case NAME_ERR_TOOLONG:
106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107 "name is too long"));
110 case NAME_ERR_LEADING_SLASH:
111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
112 "leading slash in name"));
115 case NAME_ERR_EMPTY_COMPONENT:
116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
117 "empty component in name"));
120 case NAME_ERR_TRAILING_SLASH:
121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
122 "trailing slash in name"));
125 case NAME_ERR_INVALCHAR:
127 dgettext(TEXT_DOMAIN, "invalid character "
128 "'%c' in name"), what);
131 case NAME_ERR_MULTIPLE_AT:
132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133 "multiple '@' delimiters in name"));
136 case NAME_ERR_NOLETTER:
137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
138 "pool doesn't begin with a letter"));
141 case NAME_ERR_RESERVED:
142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 "name is reserved"));
146 case NAME_ERR_DISKLIKE:
147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148 "reserved disk name"));
158 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
161 "snapshot delimiter '@' in filesystem name"));
165 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
168 "missing '@' delimiter in snapshot name"));
172 if (modifying && strchr(path, '%') != NULL) {
174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
175 "invalid character %c in name"), '%');
183 zfs_name_valid(const char *name, zfs_type_t type)
185 if (type == ZFS_TYPE_POOL)
186 return (zpool_name_valid(NULL, B_FALSE, name));
187 return (zfs_validate_name(NULL, name, type, B_FALSE));
191 * This function takes the raw DSL properties, and filters out the user-defined
192 * properties into a separate nvlist.
195 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
197 libzfs_handle_t *hdl = zhp->zfs_hdl;
202 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
203 (void) no_memory(hdl);
208 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
209 if (!zfs_prop_user(nvpair_name(elem)))
212 verify(nvpair_value_nvlist(elem, &propval) == 0);
213 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
215 (void) no_memory(hdl);
223 static zpool_handle_t *
224 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
226 libzfs_handle_t *hdl = zhp->zfs_hdl;
229 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
230 if (hdl->libzfs_pool_handles != NULL)
231 zph->zpool_next = hdl->libzfs_pool_handles;
232 hdl->libzfs_pool_handles = zph;
237 static zpool_handle_t *
238 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
240 libzfs_handle_t *hdl = zhp->zfs_hdl;
241 zpool_handle_t *zph = hdl->libzfs_pool_handles;
243 while ((zph != NULL) &&
244 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
245 zph = zph->zpool_next;
250 * Returns a handle to the pool that contains the provided dataset.
251 * If a handle to that pool already exists then that handle is returned.
252 * Otherwise, a new handle is created and added to the list of handles.
254 static zpool_handle_t *
255 zpool_handle(zfs_handle_t *zhp)
261 len = strcspn(zhp->zfs_name, "/@") + 1;
262 pool_name = zfs_alloc(zhp->zfs_hdl, len);
263 (void) strlcpy(pool_name, zhp->zfs_name, len);
265 zph = zpool_find_handle(zhp, pool_name, len);
267 zph = zpool_add_handle(zhp, pool_name);
274 zpool_free_handles(libzfs_handle_t *hdl)
276 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
278 while (zph != NULL) {
279 next = zph->zpool_next;
283 hdl->libzfs_pool_handles = NULL;
287 * Utility function to gather stats (objset and zpl) for the given object.
290 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
292 libzfs_handle_t *hdl = zhp->zfs_hdl;
294 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
296 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
297 if (errno == ENOMEM) {
298 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
309 * Utility function to get the received properties of the given object.
312 get_recvd_props_ioctl(zfs_handle_t *zhp)
314 libzfs_handle_t *hdl = zhp->zfs_hdl;
315 nvlist_t *recvdprops;
316 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
319 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
322 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
324 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
325 if (errno == ENOMEM) {
326 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
330 zcmd_free_nvlists(&zc);
335 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
336 zcmd_free_nvlists(&zc);
340 nvlist_free(zhp->zfs_recvd_props);
341 zhp->zfs_recvd_props = recvdprops;
347 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
349 nvlist_t *allprops, *userprops;
351 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
353 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
358 * XXX Why do we store the user props separately, in addition to
359 * storing them in zfs_props?
361 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
362 nvlist_free(allprops);
366 nvlist_free(zhp->zfs_props);
367 nvlist_free(zhp->zfs_user_props);
369 zhp->zfs_props = allprops;
370 zhp->zfs_user_props = userprops;
376 get_stats(zfs_handle_t *zhp)
379 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
381 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
383 if (get_stats_ioctl(zhp, &zc) != 0)
385 else if (put_stats_zhdl(zhp, &zc) != 0)
387 zcmd_free_nvlists(&zc);
392 * Refresh the properties currently stored in the handle.
395 zfs_refresh_properties(zfs_handle_t *zhp)
397 (void) get_stats(zhp);
401 * Makes a handle from the given dataset name. Used by zfs_open() and
402 * zfs_iter_* to create child handles on the fly.
405 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
407 if (put_stats_zhdl(zhp, zc) != 0)
411 * We've managed to open the dataset and gather statistics. Determine
412 * the high-level type.
414 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
415 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
416 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
417 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
418 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
419 return (-1); /* zpios' and other testing datasets are
420 of this type, ignore if encountered */
424 if (zhp->zfs_dmustats.dds_is_snapshot)
425 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
426 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
427 zhp->zfs_type = ZFS_TYPE_VOLUME;
428 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
429 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
431 abort(); /* we should never see any other types */
433 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
440 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
442 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
444 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
450 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
451 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
455 if (get_stats_ioctl(zhp, &zc) == -1) {
456 zcmd_free_nvlists(&zc);
460 if (make_dataset_handle_common(zhp, &zc) == -1) {
464 zcmd_free_nvlists(&zc);
469 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
471 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
477 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
478 if (make_dataset_handle_common(zhp, zc) == -1) {
486 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
488 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
493 zhp->zfs_hdl = pzhp->zfs_hdl;
494 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
495 zhp->zfs_head_type = pzhp->zfs_type;
496 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
497 zhp->zpool_hdl = zpool_handle(zhp);
503 zfs_handle_dup(zfs_handle_t *zhp_orig)
505 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
510 zhp->zfs_hdl = zhp_orig->zfs_hdl;
511 zhp->zpool_hdl = zhp_orig->zpool_hdl;
512 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
513 sizeof (zhp->zfs_name));
514 zhp->zfs_type = zhp_orig->zfs_type;
515 zhp->zfs_head_type = zhp_orig->zfs_head_type;
516 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
517 if (zhp_orig->zfs_props != NULL) {
518 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
519 (void) no_memory(zhp->zfs_hdl);
524 if (zhp_orig->zfs_user_props != NULL) {
525 if (nvlist_dup(zhp_orig->zfs_user_props,
526 &zhp->zfs_user_props, 0) != 0) {
527 (void) no_memory(zhp->zfs_hdl);
532 if (zhp_orig->zfs_recvd_props != NULL) {
533 if (nvlist_dup(zhp_orig->zfs_recvd_props,
534 &zhp->zfs_recvd_props, 0)) {
535 (void) no_memory(zhp->zfs_hdl);
540 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
541 if (zhp_orig->zfs_mntopts != NULL) {
542 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
543 zhp_orig->zfs_mntopts);
545 zhp->zfs_props_table = zhp_orig->zfs_props_table;
550 * Opens the given snapshot, filesystem, or volume. The 'types'
551 * argument is a mask of acceptable types. The function will print an
552 * appropriate error message and return NULL if it can't be opened.
555 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
560 (void) snprintf(errbuf, sizeof (errbuf),
561 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
564 * Validate the name before we even try to open it.
566 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
568 "invalid dataset name"));
569 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
574 * Try to get stats for the dataset, which will tell us if it exists.
577 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
578 (void) zfs_standard_error(hdl, errno, errbuf);
582 if (!(types & zhp->zfs_type)) {
583 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
592 * Release a ZFS handle. Nothing to do but free the associated memory.
595 zfs_close(zfs_handle_t *zhp)
597 if (zhp->zfs_mntopts)
598 free(zhp->zfs_mntopts);
599 nvlist_free(zhp->zfs_props);
600 nvlist_free(zhp->zfs_user_props);
601 nvlist_free(zhp->zfs_recvd_props);
605 typedef struct mnttab_node {
606 struct mnttab mtn_mt;
611 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
613 const mnttab_node_t *mtn1 = arg1;
614 const mnttab_node_t *mtn2 = arg2;
617 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
621 return (rv > 0 ? 1 : -1);
625 libzfs_mnttab_init(libzfs_handle_t *hdl)
627 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
628 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
629 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
633 libzfs_mnttab_update(libzfs_handle_t *hdl)
637 rewind(hdl->libzfs_mnttab);
638 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
641 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
643 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
644 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
645 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
646 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
647 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
648 avl_add(&hdl->libzfs_mnttab_cache, mtn);
653 libzfs_mnttab_fini(libzfs_handle_t *hdl)
658 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
659 free(mtn->mtn_mt.mnt_special);
660 free(mtn->mtn_mt.mnt_mountp);
661 free(mtn->mtn_mt.mnt_fstype);
662 free(mtn->mtn_mt.mnt_mntopts);
665 avl_destroy(&hdl->libzfs_mnttab_cache);
669 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
671 hdl->libzfs_mnttab_enable = enable;
675 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
676 struct mnttab *entry)
681 if (!hdl->libzfs_mnttab_enable) {
682 struct mnttab srch = { 0 };
684 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
685 libzfs_mnttab_fini(hdl);
686 rewind(hdl->libzfs_mnttab);
687 srch.mnt_special = (char *)fsname;
688 srch.mnt_fstype = MNTTYPE_ZFS;
689 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
695 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
696 libzfs_mnttab_update(hdl);
698 find.mtn_mt.mnt_special = (char *)fsname;
699 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
701 *entry = mtn->mtn_mt;
708 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
709 const char *mountp, const char *mntopts)
713 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
715 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
716 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
717 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
718 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
719 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
720 avl_add(&hdl->libzfs_mnttab_cache, mtn);
724 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
729 find.mtn_mt.mnt_special = (char *)fsname;
730 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
731 avl_remove(&hdl->libzfs_mnttab_cache, ret);
732 free(ret->mtn_mt.mnt_special);
733 free(ret->mtn_mt.mnt_mountp);
734 free(ret->mtn_mt.mnt_fstype);
735 free(ret->mtn_mt.mnt_mntopts);
741 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
743 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
745 if (zpool_handle == NULL)
748 *spa_version = zpool_get_prop_int(zpool_handle,
749 ZPOOL_PROP_VERSION, NULL);
754 * The choice of reservation property depends on the SPA version.
757 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
761 if (zfs_spa_version(zhp, &spa_version) < 0)
764 if (spa_version >= SPA_VERSION_REFRESERVATION)
765 *resv_prop = ZFS_PROP_REFRESERVATION;
767 *resv_prop = ZFS_PROP_RESERVATION;
773 * Given an nvlist of properties to set, validates that they are correct, and
774 * parses any numeric properties (index, boolean, etc) if they are specified as
778 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
779 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
786 int chosen_normal = -1;
789 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
790 (void) no_memory(hdl);
795 * Make sure this property is valid and applies to this type.
799 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
800 const char *propname = nvpair_name(elem);
802 prop = zfs_name_to_prop(propname);
803 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
805 * This is a user property: make sure it's a
806 * string, and that it's less than ZAP_MAXNAMELEN.
808 if (nvpair_type(elem) != DATA_TYPE_STRING) {
809 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
810 "'%s' must be a string"), propname);
811 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
815 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
816 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
817 "property name '%s' is too long"),
819 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
823 (void) nvpair_value_string(elem, &strval);
824 if (nvlist_add_string(ret, propname, strval) != 0) {
825 (void) no_memory(hdl);
832 * Currently, only user properties can be modified on
835 if (type == ZFS_TYPE_SNAPSHOT) {
836 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
837 "this property can not be modified for snapshots"));
838 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
842 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
843 zfs_userquota_prop_t uqtype;
844 char newpropname[128];
849 if (userquota_propname_decode(propname, zoned,
850 &uqtype, domain, sizeof (domain), &rid) != 0) {
852 dgettext(TEXT_DOMAIN,
853 "'%s' has an invalid user/group name"),
855 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
859 if (uqtype != ZFS_PROP_USERQUOTA &&
860 uqtype != ZFS_PROP_GROUPQUOTA) {
862 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
864 (void) zfs_error(hdl, EZFS_PROPREADONLY,
869 if (nvpair_type(elem) == DATA_TYPE_STRING) {
870 (void) nvpair_value_string(elem, &strval);
871 if (strcmp(strval, "none") == 0) {
873 } else if (zfs_nicestrtonum(hdl,
874 strval, &intval) != 0) {
875 (void) zfs_error(hdl,
876 EZFS_BADPROP, errbuf);
879 } else if (nvpair_type(elem) ==
881 (void) nvpair_value_uint64(elem, &intval);
883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
884 "use 'none' to disable "
885 "userquota/groupquota"));
889 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
890 "'%s' must be a number"), propname);
891 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
896 * Encode the prop name as
897 * userquota@<hex-rid>-domain, to make it easy
898 * for the kernel to decode.
900 (void) snprintf(newpropname, sizeof (newpropname),
901 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
902 (longlong_t)rid, domain);
906 if (nvlist_add_uint64_array(ret, newpropname,
908 (void) no_memory(hdl);
912 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
913 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
916 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
920 if (prop == ZPROP_INVAL) {
921 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
922 "invalid property '%s'"), propname);
923 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
927 if (!zfs_prop_valid_for_type(prop, type)) {
929 dgettext(TEXT_DOMAIN, "'%s' does not "
930 "apply to datasets of this type"), propname);
931 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
935 if (zfs_prop_readonly(prop) &&
936 (!zfs_prop_setonce(prop) || zhp != NULL)) {
938 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
940 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
944 if (zprop_parse_value(hdl, elem, prop, type, ret,
945 &strval, &intval, errbuf) != 0)
949 * Perform some additional checks for specific properties.
952 case ZFS_PROP_VERSION:
958 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
959 if (intval < version) {
960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
961 "Can not downgrade; already at version %u"),
963 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
969 case ZFS_PROP_RECORDSIZE:
970 case ZFS_PROP_VOLBLOCKSIZE:
971 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
972 if (intval < SPA_MINBLOCKSIZE ||
973 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
974 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
975 "'%s' must be power of 2 from %u "
977 (uint_t)SPA_MINBLOCKSIZE,
978 (uint_t)SPA_MAXBLOCKSIZE >> 10);
979 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
984 case ZFS_PROP_MLSLABEL:
988 * Verify the mlslabel string and convert to
989 * internal hex label string.
993 char *hex = NULL; /* internal label string */
995 /* Default value is already OK. */
996 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
999 /* Verify the label can be converted to binary form */
1000 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1001 (str_to_label(strval, &new_sl, MAC_LABEL,
1002 L_NO_CORRECTION, NULL) == -1)) {
1006 /* Now translate to hex internal label string */
1007 if (label_to_str(new_sl, &hex, M_INTERNAL,
1013 m_label_free(new_sl);
1015 /* If string is already in internal form, we're done. */
1016 if (strcmp(strval, hex) == 0) {
1021 /* Replace the label string with the internal form. */
1022 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1024 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1032 "invalid mlslabel '%s'"), strval);
1033 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1034 m_label_free(new_sl); /* OK if null */
1037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1038 "mlslabels are unsupported"));
1039 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1041 #endif /* HAVE_MLSLABEL */
1044 case ZFS_PROP_MOUNTPOINT:
1046 namecheck_err_t why;
1048 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1049 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1052 if (mountpoint_namecheck(strval, &why)) {
1054 case NAME_ERR_LEADING_SLASH:
1056 dgettext(TEXT_DOMAIN,
1057 "'%s' must be an absolute path, "
1058 "'none', or 'legacy'"), propname);
1060 case NAME_ERR_TOOLONG:
1062 dgettext(TEXT_DOMAIN,
1063 "component of '%s' is too long"),
1069 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1076 case ZFS_PROP_SHARESMB:
1077 case ZFS_PROP_SHARENFS:
1079 * For the mountpoint and sharenfs or sharesmb
1080 * properties, check if it can be set in a
1081 * global/non-global zone based on
1082 * the zoned property value:
1084 * global zone non-global zone
1085 * --------------------------------------------------
1086 * zoned=on mountpoint (no) mountpoint (yes)
1087 * sharenfs (no) sharenfs (no)
1088 * sharesmb (no) sharesmb (no)
1090 * zoned=off mountpoint (yes) N/A
1095 if (getzoneid() == GLOBAL_ZONEID) {
1096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1097 "'%s' cannot be set on "
1098 "dataset in a non-global zone"),
1100 (void) zfs_error(hdl, EZFS_ZONED,
1103 } else if (prop == ZFS_PROP_SHARENFS ||
1104 prop == ZFS_PROP_SHARESMB) {
1105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1106 "'%s' cannot be set in "
1107 "a non-global zone"), propname);
1108 (void) zfs_error(hdl, EZFS_ZONED,
1112 } else if (getzoneid() != GLOBAL_ZONEID) {
1114 * If zoned property is 'off', this must be in
1115 * a global zone. If not, something is wrong.
1117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1118 "'%s' cannot be set while dataset "
1119 "'zoned' property is set"), propname);
1120 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1125 * At this point, it is legitimate to set the
1126 * property. Now we want to make sure that the
1127 * property value is valid if it is sharenfs.
1129 if ((prop == ZFS_PROP_SHARENFS ||
1130 prop == ZFS_PROP_SHARESMB) &&
1131 strcmp(strval, "on") != 0 &&
1132 strcmp(strval, "off") != 0) {
1133 zfs_share_proto_t proto;
1135 if (prop == ZFS_PROP_SHARESMB)
1141 * Must be an valid sharing protocol
1142 * option string so init the libshare
1143 * in order to enable the parser and
1144 * then parse the options. We use the
1145 * control API since we don't care about
1146 * the current configuration and don't
1147 * want the overhead of loading it
1148 * until we actually do something.
1151 if (zfs_init_libshare(hdl,
1152 SA_INIT_CONTROL_API) != SA_OK) {
1154 * An error occurred so we can't do
1157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1158 "'%s' cannot be set: problem "
1159 "in share initialization"),
1161 (void) zfs_error(hdl, EZFS_BADPROP,
1166 if (zfs_parse_options(strval, proto) != SA_OK) {
1168 * There was an error in parsing so
1169 * deal with it by issuing an error
1170 * message and leaving after
1171 * uninitializing the the libshare
1174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1175 "'%s' cannot be set to invalid "
1176 "options"), propname);
1177 (void) zfs_error(hdl, EZFS_BADPROP,
1179 zfs_uninit_libshare(hdl);
1182 zfs_uninit_libshare(hdl);
1186 case ZFS_PROP_UTF8ONLY:
1187 chosen_utf = (int)intval;
1189 case ZFS_PROP_NORMALIZE:
1190 chosen_normal = (int)intval;
1197 * For changes to existing volumes, we have some additional
1198 * checks to enforce.
1200 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1201 uint64_t volsize = zfs_prop_get_int(zhp,
1203 uint64_t blocksize = zfs_prop_get_int(zhp,
1204 ZFS_PROP_VOLBLOCKSIZE);
1208 case ZFS_PROP_RESERVATION:
1209 case ZFS_PROP_REFRESERVATION:
1210 if (intval > volsize) {
1211 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1212 "'%s' is greater than current "
1213 "volume size"), propname);
1214 (void) zfs_error(hdl, EZFS_BADPROP,
1220 case ZFS_PROP_VOLSIZE:
1221 if (intval % blocksize != 0) {
1222 zfs_nicenum(blocksize, buf,
1224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225 "'%s' must be a multiple of "
1226 "volume block size (%s)"),
1228 (void) zfs_error(hdl, EZFS_BADPROP,
1234 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1235 "'%s' cannot be zero"),
1237 (void) zfs_error(hdl, EZFS_BADPROP,
1249 * If normalization was chosen, but no UTF8 choice was made,
1250 * enforce rejection of non-UTF8 names.
1252 * If normalization was chosen, but rejecting non-UTF8 names
1253 * was explicitly not chosen, it is an error.
1255 if (chosen_normal > 0 && chosen_utf < 0) {
1256 if (nvlist_add_uint64(ret,
1257 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1258 (void) no_memory(hdl);
1261 } else if (chosen_normal > 0 && chosen_utf == 0) {
1262 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1263 "'%s' must be set 'on' if normalization chosen"),
1264 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1265 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1276 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1278 uint64_t old_volsize;
1279 uint64_t new_volsize;
1280 uint64_t old_reservation;
1281 uint64_t new_reservation;
1282 zfs_prop_t resv_prop;
1285 * If this is an existing volume, and someone is setting the volsize,
1286 * make sure that it matches the reservation, or add it if necessary.
1288 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1289 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1291 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1292 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1293 old_reservation) || nvlist_lookup_uint64(nvl,
1294 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1297 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1300 new_reservation = zvol_volsize_to_reservation(new_volsize,
1302 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1303 new_reservation) != 0) {
1304 (void) no_memory(zhp->zfs_hdl);
1311 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1318 * For quotas and reservations, ENOSPC indicates
1319 * something different; setting a quota or reservation
1320 * doesn't use any disk space.
1323 case ZFS_PROP_QUOTA:
1324 case ZFS_PROP_REFQUOTA:
1325 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1326 "size is less than current used or "
1328 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1331 case ZFS_PROP_RESERVATION:
1332 case ZFS_PROP_REFRESERVATION:
1333 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1334 "size is greater than available space"));
1335 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1339 (void) zfs_standard_error(hdl, err, errbuf);
1345 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1349 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1353 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1354 "pool and or dataset must be upgraded to set this "
1355 "property or value"));
1356 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1360 if (prop == ZFS_PROP_COMPRESSION) {
1361 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1362 "property setting is not allowed on "
1363 "bootable datasets"));
1364 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1366 (void) zfs_standard_error(hdl, err, errbuf);
1371 if (prop == ZPROP_INVAL) {
1372 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1374 (void) zfs_standard_error(hdl, err, errbuf);
1380 * This platform can't address a volume this big.
1383 if (prop == ZFS_PROP_VOLSIZE) {
1384 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1390 (void) zfs_standard_error(hdl, err, errbuf);
1395 zfs_is_namespace_prop(zfs_prop_t prop)
1399 case ZFS_PROP_ATIME:
1400 case ZFS_PROP_DEVICES:
1402 case ZFS_PROP_SETUID:
1403 case ZFS_PROP_READONLY:
1404 case ZFS_PROP_XATTR:
1405 case ZFS_PROP_NBMAND:
1414 * Given a property name and value, set the property for the given dataset.
1417 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1419 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1421 prop_changelist_t *cl = NULL;
1423 libzfs_handle_t *hdl = zhp->zfs_hdl;
1424 nvlist_t *nvl = NULL, *realprops;
1426 boolean_t do_prefix;
1430 (void) snprintf(errbuf, sizeof (errbuf),
1431 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1434 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1435 nvlist_add_string(nvl, propname, propval) != 0) {
1436 (void) no_memory(hdl);
1440 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1441 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1447 prop = zfs_name_to_prop(propname);
1449 if (prop == ZFS_PROP_VOLSIZE) {
1450 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1454 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1457 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1459 "child dataset with inherited mountpoint is used "
1460 "in a non-global zone"));
1461 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1466 * If the dataset's canmount property is being set to noauto,
1467 * then we want to prevent unmounting & remounting it.
1469 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1470 (zprop_string_to_index(prop, propval, &idx,
1471 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1473 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1477 * Execute the corresponding ioctl() to set this property.
1479 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1481 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1484 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1487 zfs_setprop_error(hdl, prop, errno, errbuf);
1488 if (added_resv && errno == ENOSPC) {
1489 /* clean up the volsize property we tried to set */
1490 uint64_t old_volsize = zfs_prop_get_int(zhp,
1493 zcmd_free_nvlists(&zc);
1494 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1496 if (nvlist_add_uint64(nvl,
1497 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1500 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1502 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1506 ret = changelist_postfix(cl);
1510 * Refresh the statistics so the new property
1511 * value is reflected.
1513 (void) get_stats(zhp);
1516 * Remount the filesystem to propagate the change
1517 * if one of the options handled by the generic
1518 * Linux namespace layer has been modified.
1520 if (zfs_is_namespace_prop(prop) &&
1521 zfs_is_mounted(zhp, NULL))
1522 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1528 zcmd_free_nvlists(&zc);
1530 changelist_free(cl);
1535 * Given a property, inherit the value from the parent dataset, or if received
1536 * is TRUE, revert to the received value, if any.
1539 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1541 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1543 prop_changelist_t *cl;
1544 libzfs_handle_t *hdl = zhp->zfs_hdl;
1548 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1549 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1551 zc.zc_cookie = received;
1552 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1554 * For user properties, the amount of work we have to do is very
1555 * small, so just do it here.
1557 if (!zfs_prop_user(propname)) {
1558 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1559 "invalid property"));
1560 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1563 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1564 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1566 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1567 return (zfs_standard_error(hdl, errno, errbuf));
1573 * Verify that this property is inheritable.
1575 if (zfs_prop_readonly(prop))
1576 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1578 if (!zfs_prop_inheritable(prop) && !received)
1579 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1582 * Check to see if the value applies to this type
1584 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1585 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1588 * Normalize the name, to get rid of shorthand abbreviations.
1590 propname = zfs_prop_to_name(prop);
1591 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1592 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1594 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1595 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1597 "dataset is used in a non-global zone"));
1598 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1602 * Determine datasets which will be affected by this change, if any.
1604 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1607 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1609 "child dataset with inherited mountpoint is used "
1610 "in a non-global zone"));
1611 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1615 if ((ret = changelist_prefix(cl)) != 0)
1618 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1619 return (zfs_standard_error(hdl, errno, errbuf));
1622 if ((ret = changelist_postfix(cl)) != 0)
1626 * Refresh the statistics so the new property is reflected.
1628 (void) get_stats(zhp);
1632 changelist_free(cl);
1637 * True DSL properties are stored in an nvlist. The following two functions
1638 * extract them appropriately.
1641 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1647 if (nvlist_lookup_nvlist(zhp->zfs_props,
1648 zfs_prop_to_name(prop), &nv) == 0) {
1649 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1650 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1652 verify(!zhp->zfs_props_table ||
1653 zhp->zfs_props_table[prop] == B_TRUE);
1654 value = zfs_prop_default_numeric(prop);
1662 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1668 if (nvlist_lookup_nvlist(zhp->zfs_props,
1669 zfs_prop_to_name(prop), &nv) == 0) {
1670 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1671 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1673 verify(!zhp->zfs_props_table ||
1674 zhp->zfs_props_table[prop] == B_TRUE);
1675 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1684 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1686 return (zhp->zfs_props == zhp->zfs_recvd_props);
1690 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1692 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1693 zhp->zfs_props = zhp->zfs_recvd_props;
1697 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1699 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1704 * Internal function for getting a numeric property. Both zfs_prop_get() and
1705 * zfs_prop_get_int() are built using this interface.
1707 * Certain properties can be overridden using 'mount -o'. In this case, scan
1708 * the contents of the /etc/mtab entry, searching for the appropriate options.
1709 * If they differ from the on-disk values, report the current values and mark
1710 * the source "temporary".
1713 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1714 char **source, uint64_t *val)
1716 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1717 nvlist_t *zplprops = NULL;
1719 char *mntopt_on = NULL;
1720 char *mntopt_off = NULL;
1721 boolean_t received = zfs_is_recvd_props_mode(zhp);
1726 case ZFS_PROP_ATIME:
1727 mntopt_on = MNTOPT_ATIME;
1728 mntopt_off = MNTOPT_NOATIME;
1731 case ZFS_PROP_DEVICES:
1732 mntopt_on = MNTOPT_DEVICES;
1733 mntopt_off = MNTOPT_NODEVICES;
1737 mntopt_on = MNTOPT_EXEC;
1738 mntopt_off = MNTOPT_NOEXEC;
1741 case ZFS_PROP_READONLY:
1742 mntopt_on = MNTOPT_RO;
1743 mntopt_off = MNTOPT_RW;
1746 case ZFS_PROP_SETUID:
1747 mntopt_on = MNTOPT_SETUID;
1748 mntopt_off = MNTOPT_NOSETUID;
1751 case ZFS_PROP_XATTR:
1752 mntopt_on = MNTOPT_XATTR;
1753 mntopt_off = MNTOPT_NOXATTR;
1756 case ZFS_PROP_NBMAND:
1757 mntopt_on = MNTOPT_NBMAND;
1758 mntopt_off = MNTOPT_NONBMAND;
1765 * Because looking up the mount options is potentially expensive
1766 * (iterating over all of /etc/mtab), we defer its calculation until
1767 * we're looking up a property which requires its presence.
1769 if (!zhp->zfs_mntcheck &&
1770 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1771 libzfs_handle_t *hdl = zhp->zfs_hdl;
1772 struct mnttab entry;
1774 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1775 zhp->zfs_mntopts = zfs_strdup(hdl,
1777 if (zhp->zfs_mntopts == NULL)
1781 zhp->zfs_mntcheck = B_TRUE;
1784 if (zhp->zfs_mntopts == NULL)
1785 mnt.mnt_mntopts = "";
1787 mnt.mnt_mntopts = zhp->zfs_mntopts;
1790 case ZFS_PROP_ATIME:
1791 case ZFS_PROP_DEVICES:
1793 case ZFS_PROP_READONLY:
1794 case ZFS_PROP_SETUID:
1795 case ZFS_PROP_XATTR:
1796 case ZFS_PROP_NBMAND:
1797 *val = getprop_uint64(zhp, prop, source);
1802 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1805 *src = ZPROP_SRC_TEMPORARY;
1806 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1809 *src = ZPROP_SRC_TEMPORARY;
1813 case ZFS_PROP_CANMOUNT:
1814 case ZFS_PROP_VOLSIZE:
1815 case ZFS_PROP_QUOTA:
1816 case ZFS_PROP_REFQUOTA:
1817 case ZFS_PROP_RESERVATION:
1818 case ZFS_PROP_REFRESERVATION:
1819 *val = getprop_uint64(zhp, prop, source);
1821 if (*source == NULL) {
1822 /* not default, must be local */
1823 *source = zhp->zfs_name;
1827 case ZFS_PROP_MOUNTED:
1828 *val = (zhp->zfs_mntopts != NULL);
1831 case ZFS_PROP_NUMCLONES:
1832 *val = zhp->zfs_dmustats.dds_num_clones;
1835 case ZFS_PROP_VERSION:
1836 case ZFS_PROP_NORMALIZE:
1837 case ZFS_PROP_UTF8ONLY:
1839 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1840 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1842 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1843 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1844 zcmd_free_nvlists(&zc);
1847 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1848 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1850 zcmd_free_nvlists(&zc);
1854 nvlist_free(zplprops);
1855 zcmd_free_nvlists(&zc);
1859 switch (zfs_prop_get_type(prop)) {
1860 case PROP_TYPE_NUMBER:
1861 case PROP_TYPE_INDEX:
1862 *val = getprop_uint64(zhp, prop, source);
1864 * If we tried to use a default value for a
1865 * readonly property, it means that it was not
1868 if (zfs_prop_readonly(prop) &&
1869 *source != NULL && (*source)[0] == '\0') {
1874 case PROP_TYPE_STRING:
1876 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1877 "cannot get non-numeric property"));
1878 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1879 dgettext(TEXT_DOMAIN, "internal error")));
1887 * Calculate the source type, given the raw source string.
1890 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1891 char *statbuf, size_t statlen)
1893 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1896 if (source == NULL) {
1897 *srctype = ZPROP_SRC_NONE;
1898 } else if (source[0] == '\0') {
1899 *srctype = ZPROP_SRC_DEFAULT;
1900 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1901 *srctype = ZPROP_SRC_RECEIVED;
1903 if (strcmp(source, zhp->zfs_name) == 0) {
1904 *srctype = ZPROP_SRC_LOCAL;
1906 (void) strlcpy(statbuf, source, statlen);
1907 *srctype = ZPROP_SRC_INHERITED;
1914 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1915 size_t proplen, boolean_t literal)
1920 if (zhp->zfs_recvd_props == NULL)
1921 if (get_recvd_props_ioctl(zhp) != 0)
1924 prop = zfs_name_to_prop(propname);
1926 if (prop != ZPROP_INVAL) {
1928 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1930 zfs_set_recvd_props_mode(zhp, &cookie);
1931 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1932 NULL, NULL, 0, literal);
1933 zfs_unset_recvd_props_mode(zhp, &cookie);
1937 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1938 propname, &propval) != 0)
1940 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1942 (void) strlcpy(propbuf, recvdval, proplen);
1945 return (err == 0 ? 0 : -1);
1949 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1954 value = zfs_get_clones_nvl(zhp);
1959 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1960 pair = nvlist_next_nvpair(value, pair)) {
1961 if (propbuf[0] != '\0')
1962 (void) strlcat(propbuf, ",", proplen);
1963 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1969 struct get_clones_arg {
1973 char buf[ZFS_MAXNAMELEN];
1977 get_clones_cb(zfs_handle_t *zhp, void *arg)
1979 struct get_clones_arg *gca = arg;
1981 if (gca->numclones == 0) {
1986 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
1987 NULL, NULL, 0, B_TRUE) != 0)
1989 if (strcmp(gca->buf, gca->origin) == 0) {
1990 if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
1992 return (no_memory(zhp->zfs_hdl));
1998 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2004 zfs_get_clones_nvl(zfs_handle_t *zhp)
2006 nvlist_t *nv, *value;
2008 if (nvlist_lookup_nvlist(zhp->zfs_props,
2009 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2010 struct get_clones_arg gca;
2013 * if this is a snapshot, then the kernel wasn't able
2014 * to get the clones. Do it by slowly iterating.
2016 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2018 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2020 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2025 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2027 gca.origin = zhp->zfs_name;
2029 if (gca.numclones != 0) {
2031 char pool[ZFS_MAXNAMELEN];
2034 /* get the pool name */
2035 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2036 (void) strsep(&cp, "/@");
2037 root = zfs_open(zhp->zfs_hdl, pool,
2038 ZFS_TYPE_FILESYSTEM);
2040 (void) get_clones_cb(root, &gca);
2043 if (gca.numclones != 0 ||
2044 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2045 nvlist_add_nvlist(zhp->zfs_props,
2046 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2053 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2054 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2057 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2063 * Retrieve a property from the given object. If 'literal' is specified, then
2064 * numbers are left as exact values. Otherwise, numbers are converted to a
2065 * human-readable form.
2067 * Returns 0 on success, or -1 on error.
2070 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2071 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2073 char *source = NULL;
2077 boolean_t received = zfs_is_recvd_props_mode(zhp);
2080 * Check to see if this property applies to our object
2082 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2085 if (received && zfs_prop_readonly(prop))
2089 *src = ZPROP_SRC_NONE;
2092 case ZFS_PROP_CREATION:
2094 * 'creation' is a time_t stored in the statistics. We convert
2095 * this into a string unless 'literal' is specified.
2098 val = getprop_uint64(zhp, prop, &source);
2099 time_t time = (time_t)val;
2103 localtime_r(&time, &t) == NULL ||
2104 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2106 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
2110 case ZFS_PROP_MOUNTPOINT:
2112 * Getting the precise mountpoint can be tricky.
2114 * - for 'none' or 'legacy', return those values.
2115 * - for inherited mountpoints, we want to take everything
2116 * after our ancestor and append it to the inherited value.
2118 * If the pool has an alternate root, we want to prepend that
2119 * root to any values we return.
2122 str = getprop_string(zhp, prop, &source);
2124 if (str[0] == '/') {
2125 char buf[MAXPATHLEN];
2127 const char *relpath;
2130 * If we inherit the mountpoint, even from a dataset
2131 * with a received value, the source will be the path of
2132 * the dataset we inherit from. If source is
2133 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2136 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2139 relpath = zhp->zfs_name + strlen(source);
2140 if (relpath[0] == '/')
2144 if ((zpool_get_prop(zhp->zpool_hdl,
2145 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2146 (strcmp(root, "-") == 0))
2149 * Special case an alternate root of '/'. This will
2150 * avoid having multiple leading slashes in the
2153 if (strcmp(root, "/") == 0)
2157 * If the mountpoint is '/' then skip over this
2158 * if we are obtaining either an alternate root or
2159 * an inherited mountpoint.
2161 if (str[1] == '\0' && (root[0] != '\0' ||
2162 relpath[0] != '\0'))
2165 if (relpath[0] == '\0')
2166 (void) snprintf(propbuf, proplen, "%s%s",
2169 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2170 root, str, relpath[0] == '@' ? "" : "/",
2173 /* 'legacy' or 'none' */
2174 (void) strlcpy(propbuf, str, proplen);
2179 case ZFS_PROP_ORIGIN:
2180 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2183 * If there is no parent at all, return failure to indicate that
2184 * it doesn't apply to this dataset.
2186 if (propbuf[0] == '\0')
2190 case ZFS_PROP_CLONES:
2191 if (get_clones_string(zhp, propbuf, proplen) != 0)
2195 case ZFS_PROP_QUOTA:
2196 case ZFS_PROP_REFQUOTA:
2197 case ZFS_PROP_RESERVATION:
2198 case ZFS_PROP_REFRESERVATION:
2200 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2204 * If quota or reservation is 0, we translate this into 'none'
2205 * (unless literal is set), and indicate that it's the default
2206 * value. Otherwise, we print the number nicely and indicate
2207 * that its set locally.
2211 (void) strlcpy(propbuf, "0", proplen);
2213 (void) strlcpy(propbuf, "none", proplen);
2216 (void) snprintf(propbuf, proplen, "%llu",
2219 zfs_nicenum(val, propbuf, proplen);
2223 case ZFS_PROP_REFRATIO:
2224 case ZFS_PROP_COMPRESSRATIO:
2225 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2227 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2228 (u_longlong_t)(val / 100),
2229 (u_longlong_t)(val % 100));
2233 switch (zhp->zfs_type) {
2234 case ZFS_TYPE_FILESYSTEM:
2237 case ZFS_TYPE_VOLUME:
2240 case ZFS_TYPE_SNAPSHOT:
2246 (void) snprintf(propbuf, proplen, "%s", str);
2249 case ZFS_PROP_MOUNTED:
2251 * The 'mounted' property is a pseudo-property that described
2252 * whether the filesystem is currently mounted. Even though
2253 * it's a boolean value, the typical values of "on" and "off"
2254 * don't make sense, so we translate to "yes" and "no".
2256 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2257 src, &source, &val) != 0)
2260 (void) strlcpy(propbuf, "yes", proplen);
2262 (void) strlcpy(propbuf, "no", proplen);
2267 * The 'name' property is a pseudo-property derived from the
2268 * dataset name. It is presented as a real property to simplify
2271 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2274 case ZFS_PROP_MLSLABEL:
2276 #ifdef HAVE_MLSLABEL
2277 m_label_t *new_sl = NULL;
2278 char *ascii = NULL; /* human readable label */
2280 (void) strlcpy(propbuf,
2281 getprop_string(zhp, prop, &source), proplen);
2283 if (literal || (strcasecmp(propbuf,
2284 ZFS_MLSLABEL_DEFAULT) == 0))
2288 * Try to translate the internal hex string to
2289 * human-readable output. If there are any
2290 * problems just use the hex string.
2293 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2294 L_NO_CORRECTION, NULL) == -1) {
2295 m_label_free(new_sl);
2299 if (label_to_str(new_sl, &ascii, M_LABEL,
2303 m_label_free(new_sl);
2306 m_label_free(new_sl);
2308 (void) strlcpy(propbuf, ascii, proplen);
2311 (void) strlcpy(propbuf,
2312 getprop_string(zhp, prop, &source), proplen);
2313 #endif /* HAVE_MLSLABEL */
2319 * GUIDs are stored as numbers, but they are identifiers.
2320 * We don't want them to be pretty printed, because pretty
2321 * printing mangles the ID into a truncated and useless value.
2323 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2325 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2329 switch (zfs_prop_get_type(prop)) {
2330 case PROP_TYPE_NUMBER:
2331 if (get_numeric_property(zhp, prop, src,
2332 &source, &val) != 0)
2335 (void) snprintf(propbuf, proplen, "%llu",
2338 zfs_nicenum(val, propbuf, proplen);
2341 case PROP_TYPE_STRING:
2342 (void) strlcpy(propbuf,
2343 getprop_string(zhp, prop, &source), proplen);
2346 case PROP_TYPE_INDEX:
2347 if (get_numeric_property(zhp, prop, src,
2348 &source, &val) != 0)
2350 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2352 (void) strlcpy(propbuf, strval, proplen);
2360 get_source(zhp, src, source, statbuf, statlen);
2366 * Utility function to get the given numeric property. Does no validation that
2367 * the given property is the appropriate type; should only be used with
2368 * hard-coded property types.
2371 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2376 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2382 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2386 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2387 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2391 * Similar to zfs_prop_get(), but returns the value as an integer.
2394 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2395 zprop_source_t *src, char *statbuf, size_t statlen)
2400 * Check to see if this property applies to our object
2402 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2403 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2404 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2405 zfs_prop_to_name(prop)));
2409 *src = ZPROP_SRC_NONE;
2411 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2414 get_source(zhp, src, source, statbuf, statlen);
2421 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2422 char **domainp, idmap_rid_t *ridp)
2424 idmap_get_handle_t *get_hdl = NULL;
2428 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2432 err = idmap_get_sidbyuid(get_hdl, id,
2433 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2435 err = idmap_get_sidbygid(get_hdl, id,
2436 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2438 if (err == IDMAP_SUCCESS &&
2439 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2440 status == IDMAP_SUCCESS)
2446 idmap_get_destroy(get_hdl);
2449 #endif /* HAVE_IDMAP */
2452 * convert the propname into parameters needed by kernel
2453 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2454 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2455 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2456 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2459 userquota_propname_decode(const char *propname, boolean_t zoned,
2460 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2462 zfs_userquota_prop_t type;
2471 /* Figure out the property type ({user|group}{quota|space}) */
2472 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2473 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2474 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2477 if (type == ZFS_NUM_USERQUOTA_PROPS)
2481 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2482 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2484 cp = strchr(propname, '@') + 1;
2486 if (isuser && (pw = getpwnam(cp)) != NULL) {
2487 if (zoned && getzoneid() == GLOBAL_ZONEID)
2490 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2491 if (zoned && getzoneid() == GLOBAL_ZONEID)
2494 } else if (strchr(cp, '@')) {
2497 * It's a SID name (eg "user@domain") that needs to be
2498 * turned into S-1-domainID-RID.
2500 directory_error_t e;
2501 char *numericsid = NULL;
2504 if (zoned && getzoneid() == GLOBAL_ZONEID)
2507 e = directory_sid_from_user_name(NULL,
2510 e = directory_sid_from_group_name(NULL,
2514 directory_error_free(e);
2517 if (numericsid == NULL)
2520 (void) strlcpy(domain, cp, domainlen);
2521 cp = strrchr(domain, '-');
2526 *ridp = strtoull(cp, &end, 10);
2529 if (errno != 0 || *end != '\0')
2533 #endif /* HAVE_IDMAP */
2535 /* It's a user/group ID (eg "12345"). */
2538 id = strtoul(cp, &end, 10);
2543 /* It's an ephemeral ID. */
2547 if (idmap_id_to_numeric_domain_rid(id, isuser,
2548 &mapdomain, &rid) != 0)
2550 (void) strlcpy(domain, mapdomain, domainlen);
2554 #endif /* HAVE_IDMAP */
2564 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2565 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2568 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2570 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2572 err = userquota_propname_decode(propname,
2573 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2574 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2575 zc.zc_objset_type = *typep;
2579 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2583 *propvalue = zc.zc_cookie;
2588 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2589 uint64_t *propvalue)
2591 zfs_userquota_prop_t type;
2593 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2598 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2599 char *propbuf, int proplen, boolean_t literal)
2603 zfs_userquota_prop_t type;
2605 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2612 (void) snprintf(propbuf, proplen, "%llu",
2613 (u_longlong_t)propvalue);
2614 } else if (propvalue == 0 &&
2615 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2616 (void) strlcpy(propbuf, "none", proplen);
2618 zfs_nicenum(propvalue, propbuf, proplen);
2624 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2625 uint64_t *propvalue)
2628 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2629 const char *snapname;
2631 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2633 snapname = strchr(propname, '@') + 1;
2634 if (strchr(snapname, '@')) {
2635 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2637 /* snapname is the short name, append it to zhp's fsname */
2640 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2641 sizeof (zc.zc_value));
2642 cp = strchr(zc.zc_value, '@');
2645 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2646 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2649 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2653 *propvalue = zc.zc_cookie;
2658 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2659 char *propbuf, int proplen, boolean_t literal)
2664 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2670 (void) snprintf(propbuf, proplen, "%llu", (long long unsigned int)propvalue);
2672 zfs_nicenum(propvalue, propbuf, proplen);
2679 zfs_get_snapused_int(zfs_handle_t *firstsnap, zfs_handle_t *lastsnap,
2683 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2685 (void) strlcpy(zc.zc_name, lastsnap->zfs_name, sizeof (zc.zc_name));
2686 (void) strlcpy(zc.zc_value, firstsnap->zfs_name, sizeof (zc.zc_value));
2688 err = ioctl(lastsnap->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_SNAPS, &zc);
2692 *usedp = zc.zc_cookie;
2698 * Returns the name of the given zfs handle.
2701 zfs_get_name(const zfs_handle_t *zhp)
2703 return (zhp->zfs_name);
2707 * Returns the type of the given zfs handle.
2710 zfs_get_type(const zfs_handle_t *zhp)
2712 return (zhp->zfs_type);
2716 * Is one dataset name a child dataset of another?
2718 * Needs to handle these cases:
2719 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2720 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2721 * Descendant? No. No. No. Yes.
2724 is_descendant(const char *ds1, const char *ds2)
2726 size_t d1len = strlen(ds1);
2728 /* ds2 can't be a descendant if it's smaller */
2729 if (strlen(ds2) < d1len)
2732 /* otherwise, compare strings and verify that there's a '/' char */
2733 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2737 * Given a complete name, return just the portion that refers to the parent.
2738 * Will return -1 if there is no parent (path is just the name of the
2742 parent_name(const char *path, char *buf, size_t buflen)
2746 (void) strlcpy(buf, path, buflen);
2748 if ((slashp = strrchr(buf, '/')) == NULL)
2756 * If accept_ancestor is false, then check to make sure that the given path has
2757 * a parent, and that it exists. If accept_ancestor is true, then find the
2758 * closest existing ancestor for the given path. In prefixlen return the
2759 * length of already existing prefix of the given path. We also fetch the
2760 * 'zoned' property, which is used to validate property settings when creating
2764 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2765 boolean_t accept_ancestor, int *prefixlen)
2767 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2768 char parent[ZFS_MAXNAMELEN];
2774 (void) snprintf(errbuf, sizeof (errbuf),
2775 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2777 /* get parent, and check to see if this is just a pool */
2778 if (parent_name(path, parent, sizeof (parent)) != 0) {
2779 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2780 "missing dataset name"));
2781 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2784 /* check to see if the pool exists */
2785 if ((slash = strchr(parent, '/')) == NULL)
2786 slash = parent + strlen(parent);
2787 (void) strncpy(zc.zc_name, parent, slash - parent);
2788 zc.zc_name[slash - parent] = '\0';
2789 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2791 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2792 "no such pool '%s'"), zc.zc_name);
2793 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2796 /* check to see if the parent dataset exists */
2797 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2798 if (errno == ENOENT && accept_ancestor) {
2800 * Go deeper to find an ancestor, give up on top level.
2802 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2804 "no such pool '%s'"), zc.zc_name);
2805 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2807 } else if (errno == ENOENT) {
2808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2809 "parent does not exist"));
2810 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2812 return (zfs_standard_error(hdl, errno, errbuf));
2815 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2819 /* we are in a non-global zone, but parent is in the global zone */
2820 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2821 (void) zfs_standard_error(hdl, EPERM, errbuf);
2826 /* make sure parent is a filesystem */
2827 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2829 "parent is not a filesystem"));
2830 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2836 if (prefixlen != NULL)
2837 *prefixlen = strlen(parent);
2842 * Finds whether the dataset of the given type(s) exists.
2845 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2849 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2853 * Try to get stats for the dataset, which will tell us if it exists.
2855 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2856 int ds_type = zhp->zfs_type;
2859 if (types & ds_type)
2866 * Given a path to 'target', create all the ancestors between
2867 * the prefixlen portion of the path, and the target itself.
2868 * Fail if the initial prefixlen-ancestor does not already exist.
2871 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2877 /* make sure prefix exists */
2878 cp = target + prefixlen;
2880 assert(strchr(cp, '/') == NULL);
2881 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2884 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2892 * Attempt to create, mount, and share any ancestor filesystems,
2893 * up to the prefixlen-long one.
2895 for (cp = target + prefixlen + 1;
2896 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2901 h = make_dataset_handle(hdl, target);
2903 /* it already exists, nothing to do here */
2908 logstr = hdl->libzfs_log_str;
2909 hdl->libzfs_log_str = NULL;
2910 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2912 hdl->libzfs_log_str = logstr;
2913 opname = dgettext(TEXT_DOMAIN, "create");
2917 hdl->libzfs_log_str = logstr;
2918 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2920 opname = dgettext(TEXT_DOMAIN, "open");
2924 if (zfs_mount(h, NULL, 0) != 0) {
2925 opname = dgettext(TEXT_DOMAIN, "mount");
2929 if (zfs_share(h) != 0) {
2930 opname = dgettext(TEXT_DOMAIN, "share");
2940 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2941 "failed to %s ancestor '%s'"), opname, target);
2946 * Creates non-existing ancestors of the given path.
2949 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2955 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2958 if ((path_copy = strdup(path)) != NULL) {
2959 rc = create_parents(hdl, path_copy, prefix);
2962 if (path_copy == NULL || rc != 0)
2969 * Create a new filesystem or volume.
2972 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2975 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2978 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2982 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2983 "cannot create '%s'"), path);
2985 /* validate the path, taking care to note the extended error message */
2986 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2987 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2989 /* validate parents exist */
2990 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2994 * The failure modes when creating a dataset of a different type over
2995 * one that already exists is a little strange. In particular, if you
2996 * try to create a dataset on top of an existing dataset, the ioctl()
2997 * will return ENOENT, not EEXIST. To prevent this from happening, we
2998 * first try to see if the dataset exists.
3000 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
3001 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3003 "dataset already exists"));
3004 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3007 if (type == ZFS_TYPE_VOLUME)
3008 zc.zc_objset_type = DMU_OST_ZVOL;
3010 zc.zc_objset_type = DMU_OST_ZFS;
3012 if (props && (props = zfs_valid_proplist(hdl, type, props,
3013 zoned, NULL, errbuf)) == 0)
3016 if (type == ZFS_TYPE_VOLUME) {
3018 * If we are creating a volume, the size and block size must
3019 * satisfy a few restraints. First, the blocksize must be a
3020 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3021 * volsize must be a multiple of the block size, and cannot be
3024 if (props == NULL || nvlist_lookup_uint64(props,
3025 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3028 "missing volume size"));
3029 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3032 if ((ret = nvlist_lookup_uint64(props,
3033 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3034 &blocksize)) != 0) {
3035 if (ret == ENOENT) {
3036 blocksize = zfs_prop_default_numeric(
3037 ZFS_PROP_VOLBLOCKSIZE);
3040 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3041 "missing volume block size"));
3042 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3049 "volume size cannot be zero"));
3050 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3053 if (size % blocksize != 0) {
3055 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3056 "volume size must be a multiple of volume block "
3058 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3062 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
3066 /* create the dataset */
3067 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3069 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
3070 ret = zvol_create_link(hdl, path);
3072 (void) zfs_standard_error(hdl, errno,
3073 dgettext(TEXT_DOMAIN,
3074 "Volume successfully created, but device links "
3075 "were not created"));
3076 zcmd_free_nvlists(&zc);
3081 zcmd_free_nvlists(&zc);
3083 /* check for failure */
3085 char parent[ZFS_MAXNAMELEN];
3086 (void) parent_name(path, parent, sizeof (parent));
3090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3091 "no such parent '%s'"), parent);
3092 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3096 "parent '%s' is not a filesystem"), parent);
3097 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3101 "volume block size must be power of 2 from "
3103 (uint_t)SPA_MINBLOCKSIZE,
3104 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3106 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3110 "pool must be upgraded to set this "
3111 "property or value"));
3112 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3116 * This platform can't address a volume this big.
3118 if (type == ZFS_TYPE_VOLUME)
3119 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3124 return (zfs_standard_error(hdl, errno, errbuf));
3132 * Destroys the given dataset. The caller must make sure that the filesystem
3133 * isn't mounted, and that there are no active dependents. If the file system
3134 * does not exist this function does nothing.
3137 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3139 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3141 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3143 if (ZFS_IS_VOLUME(zhp)) {
3144 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3147 zc.zc_objset_type = DMU_OST_ZVOL;
3149 zc.zc_objset_type = DMU_OST_ZFS;
3152 zc.zc_defer_destroy = defer;
3153 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3155 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3156 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3160 remove_mountpoint(zhp);
3165 struct destroydata {
3167 const char *snapname;
3171 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3173 struct destroydata *dd = arg;
3175 char name[ZFS_MAXNAMELEN];
3178 (void) snprintf(name, sizeof (name),
3179 "%s@%s", zhp->zfs_name, dd->snapname);
3181 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3183 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3187 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3188 (void) zvol_remove_link(zhp->zfs_hdl, name);
3190 * NB: this is simply a best-effort. We don't want to
3191 * return an error, because then we wouldn't visit all
3196 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3202 * Destroys all snapshots with the given name in zhp & descendants.
3205 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3208 struct destroydata dd = { 0 };
3210 dd.snapname = snapname;
3211 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3212 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3214 if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3215 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3216 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3217 zhp->zfs_name, snapname);
3219 ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3221 nvlist_free(dd.nvl);
3226 * Destroys all the snapshots named in the nvlist. They must be underneath
3227 * the zhp (either snapshots of it, or snapshots of its descendants).
3230 zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3233 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3235 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3236 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
3238 zc.zc_defer_destroy = defer;
3240 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
3244 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3245 "cannot destroy snapshots in %s"), zc.zc_name);
3249 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3250 "snapshot is cloned"));
3251 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3254 return (zfs_standard_error(zhp->zfs_hdl, errno,
3263 * Clones the given dataset. The target must be of the same type as the source.
3266 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3268 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3269 char parent[ZFS_MAXNAMELEN];
3272 libzfs_handle_t *hdl = zhp->zfs_hdl;
3276 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3278 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3279 "cannot create '%s'"), target);
3281 /* validate the target/clone name */
3282 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3283 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3285 /* validate parents exist */
3286 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3289 (void) parent_name(target, parent, sizeof (parent));
3292 if (ZFS_IS_VOLUME(zhp)) {
3293 zc.zc_objset_type = DMU_OST_ZVOL;
3294 type = ZFS_TYPE_VOLUME;
3296 zc.zc_objset_type = DMU_OST_ZFS;
3297 type = ZFS_TYPE_FILESYSTEM;
3301 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3302 zhp, errbuf)) == NULL)
3305 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3313 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3314 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3315 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3317 zcmd_free_nvlists(&zc);
3324 * The parent doesn't exist. We should have caught this
3325 * above, but there may a race condition that has since
3326 * destroyed the parent.
3328 * At this point, we don't know whether it's the source
3329 * that doesn't exist anymore, or whether the target
3330 * dataset doesn't exist.
3332 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3333 "no such parent '%s'"), parent);
3334 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3337 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3338 "source and target pools differ"));
3339 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3343 return (zfs_standard_error(zhp->zfs_hdl, errno,
3346 } else if (ZFS_IS_VOLUME(zhp)) {
3347 ret = zvol_create_link(zhp->zfs_hdl, target);
3353 typedef struct promote_data {
3354 char cb_mountpoint[MAXPATHLEN];
3355 const char *cb_target;
3356 const char *cb_errbuf;
3357 uint64_t cb_pivot_txg;
3361 promote_snap_cb(zfs_handle_t *zhp, void *data)
3363 promote_data_t *pd = data;
3365 char snapname[MAXPATHLEN];
3368 /* We don't care about snapshots after the pivot point */
3369 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3374 /* Remove the device link if it's a zvol. */
3375 if (ZFS_IS_VOLUME(zhp))
3376 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3378 /* Check for conflicting names */
3379 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3380 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3381 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3384 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3385 "snapshot name '%s' from origin \n"
3386 "conflicts with '%s' from target"),
3387 zhp->zfs_name, snapname);
3388 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3395 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3397 promote_data_t *pd = data;
3399 /* We don't care about snapshots after the pivot point */
3400 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3401 /* Create the device link if it's a zvol. */
3402 if (ZFS_IS_VOLUME(zhp))
3403 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3411 * Promotes the given clone fs to be the clone parent.
3414 zfs_promote(zfs_handle_t *zhp)
3416 libzfs_handle_t *hdl = zhp->zfs_hdl;
3417 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3418 char parent[MAXPATHLEN];
3425 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3426 "cannot promote '%s'"), zhp->zfs_name);
3428 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3430 "snapshots can not be promoted"));
3431 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3434 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3435 if (parent[0] == '\0') {
3436 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3437 "not a cloned filesystem"));
3438 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3440 cp = strchr(parent, '@');
3443 /* Walk the snapshots we will be moving */
3444 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3447 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3449 pd.cb_target = zhp->zfs_name;
3450 pd.cb_errbuf = errbuf;
3451 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3454 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3455 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3456 ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
3462 /* issue the ioctl */
3463 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3464 sizeof (zc.zc_value));
3465 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3466 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3469 int save_errno = errno;
3471 (void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3475 switch (save_errno) {
3478 * There is a conflicting snapshot name. We
3479 * should have caught this above, but they could
3480 * have renamed something in the mean time.
3482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3483 "conflicting snapshot '%s' from parent '%s'"),
3484 zc.zc_string, parent);
3485 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3488 return (zfs_standard_error(hdl, save_errno, errbuf));
3491 (void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3500 const char *cd_snapname;
3505 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3507 struct createdata *cd = arg;
3510 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3511 char name[MAXPATHLEN];
3513 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3514 (void) strlcat(name, "@", sizeof (name));
3515 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3516 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3519 * NB: this is simply a best-effort. We don't want to
3520 * return an error, because then we wouldn't visit all
3525 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3533 * Takes a snapshot of the given dataset.
3536 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3540 char parent[ZFS_MAXNAMELEN];
3542 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3546 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3547 "cannot snapshot '%s'"), path);
3549 /* validate the target name */
3550 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3551 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3554 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3555 props, B_FALSE, NULL, errbuf)) == NULL)
3558 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3566 /* make sure the parent exists and is of the appropriate type */
3567 delim = strchr(path, '@');
3568 (void) strncpy(parent, path, delim - path);
3569 parent[delim - path] = '\0';
3571 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3572 ZFS_TYPE_VOLUME)) == NULL) {
3573 zcmd_free_nvlists(&zc);
3577 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3578 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3579 if (ZFS_IS_VOLUME(zhp))
3580 zc.zc_objset_type = DMU_OST_ZVOL;
3582 zc.zc_objset_type = DMU_OST_ZFS;
3583 zc.zc_cookie = recursive;
3584 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3586 zcmd_free_nvlists(&zc);
3589 * if it was recursive, the one that actually failed will be in
3593 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3594 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3596 if (ret == 0 && recursive) {
3597 struct createdata cd;
3599 cd.cd_snapname = delim + 1;
3600 cd.cd_ifexists = B_FALSE;
3601 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3603 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3604 ret = zvol_create_link(zhp->zfs_hdl, path);
3606 (void) zfs_standard_error(hdl, errno,
3607 dgettext(TEXT_DOMAIN,
3608 "Volume successfully snapshotted, but device links "
3609 "were not created"));
3616 (void) zfs_standard_error(hdl, errno, errbuf);
3624 * Destroy any more recent snapshots. We invoke this callback on any dependents
3625 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3626 * is a dependent and we should just destroy it without checking the transaction
3629 typedef struct rollback_data {
3630 const char *cb_target; /* the snapshot */
3631 uint64_t cb_create; /* creation time reference */
3633 boolean_t cb_dependent;
3638 rollback_destroy(zfs_handle_t *zhp, void *data)
3640 rollback_data_t *cbp = data;
3642 if (!cbp->cb_dependent) {
3643 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3644 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3645 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3649 cbp->cb_dependent = B_TRUE;
3650 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3651 rollback_destroy, cbp);
3652 cbp->cb_dependent = B_FALSE;
3654 logstr = zhp->zfs_hdl->libzfs_log_str;
3655 zhp->zfs_hdl->libzfs_log_str = NULL;
3656 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3657 zhp->zfs_hdl->libzfs_log_str = logstr;
3660 /* We must destroy this clone; first unmount it */
3661 prop_changelist_t *clp;
3663 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3664 cbp->cb_force ? MS_FORCE: 0);
3665 if (clp == NULL || changelist_prefix(clp) != 0) {
3666 cbp->cb_error = B_TRUE;
3670 if (zfs_destroy(zhp, B_FALSE) != 0)
3671 cbp->cb_error = B_TRUE;
3673 changelist_remove(clp, zhp->zfs_name);
3674 (void) changelist_postfix(clp);
3675 changelist_free(clp);
3683 * Given a dataset, rollback to a specific snapshot, discarding any
3684 * data changes since then and making it the active dataset.
3686 * Any snapshots more recent than the target are destroyed, along with
3690 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3692 rollback_data_t cb = { 0 };
3694 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3695 boolean_t restore_resv = 0;
3696 uint64_t old_volsize = 0, new_volsize;
3697 zfs_prop_t resv_prop = { 0 };
3699 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3700 zhp->zfs_type == ZFS_TYPE_VOLUME);
3703 * Destroy all recent snapshots and their dependents.
3705 cb.cb_force = force;
3706 cb.cb_target = snap->zfs_name;
3707 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3708 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3714 * Now that we have verified that the snapshot is the latest,
3715 * rollback to the given snapshot.
3718 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3719 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3721 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3723 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3725 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3728 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3730 if (ZFS_IS_VOLUME(zhp))
3731 zc.zc_objset_type = DMU_OST_ZVOL;
3733 zc.zc_objset_type = DMU_OST_ZFS;
3736 * We rely on zfs_iter_children() to verify that there are no
3737 * newer snapshots for the given dataset. Therefore, we can
3738 * simply pass the name on to the ioctl() call. There is still
3739 * an unlikely race condition where the user has taken a
3740 * snapshot since we verified that this was the most recent.
3743 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3744 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3745 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3751 * For volumes, if the pre-rollback volsize matched the pre-
3752 * rollback reservation and the volsize has changed then set
3753 * the reservation property to the post-rollback volsize.
3754 * Make a new handle since the rollback closed the dataset.
3756 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3757 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3758 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3763 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3764 if (old_volsize != new_volsize)
3765 err = zfs_prop_set_int(zhp, resv_prop,
3774 * Renames the given dataset.
3777 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3778 boolean_t force_unmount)
3781 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3783 prop_changelist_t *cl = NULL;
3784 zfs_handle_t *zhrp = NULL;
3785 char *parentname = NULL;
3786 char parent[ZFS_MAXNAMELEN];
3787 libzfs_handle_t *hdl = zhp->zfs_hdl;
3790 /* if we have the same exact name, just return success */
3791 if (strcmp(zhp->zfs_name, target) == 0)
3794 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3795 "cannot rename to '%s'"), target);
3798 * Make sure the target name is valid
3800 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3801 if ((strchr(target, '@') == NULL) ||
3804 * Snapshot target name is abbreviated,
3805 * reconstruct full dataset name
3807 (void) strlcpy(parent, zhp->zfs_name,
3809 delim = strchr(parent, '@');
3810 if (strchr(target, '@') == NULL)
3814 (void) strlcat(parent, target, sizeof (parent));
3818 * Make sure we're renaming within the same dataset.
3820 delim = strchr(target, '@');
3821 if (strncmp(zhp->zfs_name, target, delim - target)
3822 != 0 || zhp->zfs_name[delim - target] != '@') {
3823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3824 "snapshots must be part of same "
3826 return (zfs_error(hdl, EZFS_CROSSTARGET,
3830 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3831 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3835 "recursive rename must be a snapshot"));
3836 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3839 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3840 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3842 /* validate parents */
3843 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3846 /* make sure we're in the same pool */
3847 verify((delim = strchr(target, '/')) != NULL);
3848 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3849 zhp->zfs_name[delim - target] != '/') {
3850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3851 "datasets must be within same pool"));
3852 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3855 /* new name cannot be a child of the current dataset name */
3856 if (is_descendant(zhp->zfs_name, target)) {
3857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3858 "New dataset name cannot be a descendant of "
3859 "current dataset name"));
3860 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3864 (void) snprintf(errbuf, sizeof (errbuf),
3865 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3867 if (getzoneid() == GLOBAL_ZONEID &&
3868 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3869 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3870 "dataset is used in a non-global zone"));
3871 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3875 struct destroydata dd;
3877 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3878 if (parentname == NULL) {
3882 delim = strchr(parentname, '@');
3884 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3890 dd.snapname = delim + 1;
3892 /* We remove any zvol links prior to renaming them */
3893 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3894 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3895 nvlist_free(dd.nvl);
3900 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3901 force_unmount ? MS_FORCE : 0)) == NULL)
3904 if (changelist_haszonedchild(cl)) {
3905 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3906 "child dataset with inherited mountpoint is used "
3907 "in a non-global zone"));
3908 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3913 if ((ret = changelist_prefix(cl)) != 0)
3917 if (ZFS_IS_VOLUME(zhp))
3918 zc.zc_objset_type = DMU_OST_ZVOL;
3920 zc.zc_objset_type = DMU_OST_ZFS;
3922 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3923 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3925 zc.zc_cookie = recursive;
3927 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3929 * if it was recursive, the one that actually failed will
3932 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3933 "cannot rename '%s'"), zc.zc_name);
3935 if (recursive && errno == EEXIST) {
3936 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3937 "a child dataset already has a snapshot "
3938 "with the new name"));
3939 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3941 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3945 * On failure, we still want to remount any filesystems that
3946 * were previously mounted, so we don't alter the system state.
3949 struct createdata cd;
3951 /* only create links for datasets that had existed */
3952 cd.cd_snapname = delim + 1;
3953 cd.cd_ifexists = B_TRUE;
3954 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3957 (void) changelist_postfix(cl);
3961 struct createdata cd;
3963 /* only create links for datasets that had existed */
3964 cd.cd_snapname = strchr(target, '@') + 1;
3965 cd.cd_ifexists = B_TRUE;
3966 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3969 changelist_rename(cl, zfs_get_name(zhp), target);
3970 ret = changelist_postfix(cl);
3982 changelist_free(cl);
3988 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3989 * and wait briefly for udev to create the /dev link.
3992 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3994 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3998 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
4000 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4001 char path[MAXPATHLEN];
4004 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4007 * Issue the appropriate ioctl.
4009 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
4013 * Silently ignore the case where the link already
4014 * exists. This allows 'zfs volinit' to be run multiple
4015 * times without errors.
4021 * Dataset does not exist in the kernel. If we
4022 * don't care (see zfs_rename), then ignore the
4032 return (zfs_standard_error_fmt(hdl, errno,
4033 dgettext(TEXT_DOMAIN, "cannot create device links "
4034 "for '%s'"), dataset));
4039 * Wait up to 10 seconds for udev to create the device.
4041 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
4042 error = zpool_label_disk_wait(path, 10000);
4044 (void) printf(gettext("%s may not be immediately "
4045 "available\n"), path);
4051 * Remove a minor node for the given zvol and the associated /dev links.
4054 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4056 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4057 int timeout = 3000; /* in milliseconds */
4061 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4064 * Due to concurrent updates by udev the device may be reported as
4065 * busy. In this case don't immediately fail. Instead briefly delay
4066 * and retry the ioctl() which is now likely to succeed. If unable
4067 * remove the link after timeout milliseconds return the failure.
4069 for (i = 0; i < timeout; i++) {
4070 error = ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
4071 if (error && errno == EBUSY) {
4083 * Silently ignore the case where the link no longer
4084 * exists, so that 'zfs volfini' can be run multiple
4085 * times without errors.
4090 return (zfs_standard_error_fmt(hdl, errno,
4091 dgettext(TEXT_DOMAIN, "cannot remove device "
4092 "links for '%s': %s"), dataset, strerror(errno)));
4100 zfs_get_user_props(zfs_handle_t *zhp)
4102 return (zhp->zfs_user_props);
4106 * This function is used by 'zfs list' to determine the exact set of columns to
4107 * display, and their maximum widths. This does two main things:
4109 * - If this is a list of all properties, then expand the list to include
4110 * all native properties, and set a flag so that for each dataset we look
4111 * for new unique user properties and add them to the list.
4113 * - For non fixed-width properties, keep track of the maximum width seen
4114 * so that we can size the column appropriately. If the user has
4115 * requested received property values, we also need to compute the width
4116 * of the RECEIVED column.
4119 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
4121 libzfs_handle_t *hdl = zhp->zfs_hdl;
4122 zprop_list_t *entry;
4123 zprop_list_t **last, **start;
4124 nvlist_t *userprops, *propval;
4127 char buf[ZFS_MAXPROPLEN];
4129 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4132 userprops = zfs_get_user_props(zhp);
4135 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4137 * Go through and add any user properties as necessary. We
4138 * start by incrementing our list pointer to the first
4139 * non-native property.
4142 while (*start != NULL) {
4143 if ((*start)->pl_prop == ZPROP_INVAL)
4145 start = &(*start)->pl_next;
4149 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4151 * See if we've already found this property in our list.
4153 for (last = start; *last != NULL;
4154 last = &(*last)->pl_next) {
4155 if (strcmp((*last)->pl_user_prop,
4156 nvpair_name(elem)) == 0)
4160 if (*last == NULL) {
4161 if ((entry = zfs_alloc(hdl,
4162 sizeof (zprop_list_t))) == NULL ||
4163 ((entry->pl_user_prop = zfs_strdup(hdl,
4164 nvpair_name(elem)))) == NULL) {
4169 entry->pl_prop = ZPROP_INVAL;
4170 entry->pl_width = strlen(nvpair_name(elem));
4171 entry->pl_all = B_TRUE;
4178 * Now go through and check the width of any non-fixed columns
4180 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4181 if (entry->pl_fixed)
4184 if (entry->pl_prop != ZPROP_INVAL) {
4185 if (zfs_prop_get(zhp, entry->pl_prop,
4186 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4187 if (strlen(buf) > entry->pl_width)
4188 entry->pl_width = strlen(buf);
4190 if (received && zfs_prop_get_recvd(zhp,
4191 zfs_prop_to_name(entry->pl_prop),
4192 buf, sizeof (buf), B_FALSE) == 0)
4193 if (strlen(buf) > entry->pl_recvd_width)
4194 entry->pl_recvd_width = strlen(buf);
4196 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4198 verify(nvlist_lookup_string(propval,
4199 ZPROP_VALUE, &strval) == 0);
4200 if (strlen(strval) > entry->pl_width)
4201 entry->pl_width = strlen(strval);
4203 if (received && zfs_prop_get_recvd(zhp,
4204 entry->pl_user_prop,
4205 buf, sizeof (buf), B_FALSE) == 0)
4206 if (strlen(buf) > entry->pl_recvd_width)
4207 entry->pl_recvd_width = strlen(buf);
4215 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4220 * Keep a reference to the props-table against which we prune the
4223 zhp->zfs_props_table = props;
4225 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4228 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4229 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4232 * User properties will result in ZPROP_INVAL, and since we
4233 * only know how to prune standard ZFS properties, we always
4234 * leave these in the list. This can also happen if we
4235 * encounter an unknown DSL property (when running older
4236 * software, for example).
4238 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4239 (void) nvlist_remove(zhp->zfs_props,
4240 nvpair_name(curr), nvpair_type(curr));
4246 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4247 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4249 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4250 nvlist_t *nvlist = NULL;
4253 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4254 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4255 zc.zc_cookie = (uint64_t)cmd;
4257 if (cmd == ZFS_SMB_ACL_RENAME) {
4258 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4259 (void) no_memory(hdl);
4265 case ZFS_SMB_ACL_ADD:
4266 case ZFS_SMB_ACL_REMOVE:
4267 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4269 case ZFS_SMB_ACL_RENAME:
4270 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4272 (void) no_memory(hdl);
4275 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4277 (void) no_memory(hdl);
4280 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4281 nvlist_free(nvlist);
4285 case ZFS_SMB_ACL_PURGE:
4290 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4292 nvlist_free(nvlist);
4297 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4298 char *path, char *resource)
4300 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4305 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4306 char *path, char *resource)
4308 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4313 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4315 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4320 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4321 char *oldname, char *newname)
4323 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4328 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4329 zfs_userspace_cb_t func, void *arg)
4331 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4333 zfs_useracct_t buf[100];
4335 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4337 zc.zc_objset_type = type;
4338 zc.zc_nvlist_dst = (uintptr_t)buf;
4342 zfs_useracct_t *zua = buf;
4344 zc.zc_nvlist_dst_size = sizeof (buf);
4345 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4346 ZFS_IOC_USERSPACE_MANY, &zc);
4347 if (error || zc.zc_nvlist_dst_size == 0)
4350 while (zc.zc_nvlist_dst_size > 0) {
4351 error = func(arg, zua->zu_domain, zua->zu_rid,
4356 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4364 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4365 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4366 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4368 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4369 libzfs_handle_t *hdl = zhp->zfs_hdl;
4371 ASSERT(!recursive || dsobj == 0);
4373 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4374 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4375 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4376 >= sizeof (zc.zc_string))
4377 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4378 zc.zc_cookie = recursive;
4379 zc.zc_temphold = temphold;
4380 zc.zc_cleanup_fd = cleanup_fd;
4381 zc.zc_sendobj = dsobj;
4382 zc.zc_createtxg = createtxg;
4384 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4385 char errbuf[ZFS_MAXNAMELEN+32];
4388 * if it was recursive, the one that actually failed will be in
4391 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4392 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4396 * Temporary tags wind up having the ds object id
4397 * prepended. So even if we passed the length check
4398 * above, it's still possible for the tag to wind
4399 * up being slightly too long.
4401 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4404 "pool must be upgraded"));
4405 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4407 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4409 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4415 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4423 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4424 boolean_t recursive)
4426 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4427 libzfs_handle_t *hdl = zhp->zfs_hdl;
4429 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4430 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4431 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4432 >= sizeof (zc.zc_string))
4433 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4434 zc.zc_cookie = recursive;
4436 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4437 char errbuf[ZFS_MAXNAMELEN+32];
4440 * if it was recursive, the one that actually failed will be in
4443 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4444 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4448 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4451 "pool must be upgraded"));
4452 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4454 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4456 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4464 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4466 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4467 libzfs_handle_t *hdl = zhp->zfs_hdl;
4471 char errbuf[ZFS_MAXNAMELEN+32];
4473 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4474 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4478 nvbuf = malloc(nvsz);
4479 if (nvbuf == NULL) {
4480 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4484 zc.zc_nvlist_dst_size = nvsz;
4485 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4487 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4489 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4490 (void) snprintf(errbuf, sizeof (errbuf),
4491 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4496 nvsz = zc.zc_nvlist_dst_size;
4500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4501 "pool must be upgraded"));
4502 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4505 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4508 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4511 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4516 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4518 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4519 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4521 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4531 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4533 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4534 libzfs_handle_t *hdl = zhp->zfs_hdl;
4536 char errbuf[ZFS_MAXNAMELEN+32];
4540 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4541 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4543 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4546 nvbuf = malloc(nvsz);
4548 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4551 zc.zc_nvlist_src_size = nvsz;
4552 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4553 zc.zc_perm_action = un;
4555 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4557 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4558 (void) snprintf(errbuf, sizeof (errbuf),
4559 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4564 "pool must be upgraded"));
4565 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4568 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4571 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4574 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4585 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4587 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4588 libzfs_handle_t *hdl = zhp->zfs_hdl;
4592 char errbuf[ZFS_MAXNAMELEN+32];
4594 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4598 nvbuf = malloc(nvsz);
4599 if (nvbuf == NULL) {
4600 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4604 zc.zc_nvlist_dst_size = nvsz;
4605 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4607 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4609 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4610 (void) snprintf(errbuf, sizeof (errbuf),
4611 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4616 nvsz = zc.zc_nvlist_dst_size;
4620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4621 "pool must be upgraded"));
4622 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4625 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4628 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4631 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4636 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4638 (void) snprintf(errbuf, sizeof (errbuf),
4639 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4641 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4651 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4654 uint64_t nblocks, volblocksize;
4658 if (nvlist_lookup_string(props,
4659 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4660 ncopies = atoi(strval);
4663 if (nvlist_lookup_uint64(props,
4664 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4665 &volblocksize) != 0)
4666 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4667 nblocks = volsize/volblocksize;
4668 /* start with metadnode L0-L6 */
4670 /* calculate number of indirects */
4671 while (nblocks > 1) {
4672 nblocks += DNODES_PER_LEVEL - 1;
4673 nblocks /= DNODES_PER_LEVEL;
4676 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4679 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4680 * compressed, but in practice they compress down to about
4683 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;