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) 2011 by Delphix. All rights reserved.
39 #include <sys/mntent.h>
40 #include <sys/mount.h>
49 #include <directory.h>
50 #endif /* HAVE_IDMAP */
52 #include <sys/dnode.h>
57 #include "zfs_namecheck.h"
59 #include "libzfs_impl.h"
60 #include "zfs_deleg.h"
62 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
63 static int userquota_propname_decode(const char *propname, boolean_t zoned,
64 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
67 * Given a single type (not a mask of types), return the type in a human
71 zfs_type_to_name(zfs_type_t type)
74 case ZFS_TYPE_FILESYSTEM:
75 return (dgettext(TEXT_DOMAIN, "filesystem"));
76 case ZFS_TYPE_SNAPSHOT:
77 return (dgettext(TEXT_DOMAIN, "snapshot"));
79 return (dgettext(TEXT_DOMAIN, "volume"));
88 * Validate a ZFS path. This is used even before trying to open the dataset, to
89 * provide a more meaningful error message. We call zfs_error_aux() to
90 * explain exactly why the name was not valid.
93 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
99 (void) zfs_prop_get_table();
100 if (dataset_namecheck(path, &why, &what) != 0) {
103 case NAME_ERR_TOOLONG:
104 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105 "name is too long"));
108 case NAME_ERR_LEADING_SLASH:
109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
110 "leading slash in name"));
113 case NAME_ERR_EMPTY_COMPONENT:
114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
115 "empty component in name"));
118 case NAME_ERR_TRAILING_SLASH:
119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
120 "trailing slash in name"));
123 case NAME_ERR_INVALCHAR:
125 dgettext(TEXT_DOMAIN, "invalid character "
126 "'%c' in name"), what);
129 case NAME_ERR_MULTIPLE_AT:
130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
131 "multiple '@' delimiters in name"));
134 case NAME_ERR_NOLETTER:
135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
136 "pool doesn't begin with a letter"));
139 case NAME_ERR_RESERVED:
140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141 "name is reserved"));
144 case NAME_ERR_DISKLIKE:
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "reserved disk name"));
156 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
159 "snapshot delimiter '@' in filesystem name"));
163 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
166 "missing '@' delimiter in snapshot name"));
170 if (modifying && strchr(path, '%') != NULL) {
172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173 "invalid character %c in name"), '%');
181 zfs_name_valid(const char *name, zfs_type_t type)
183 if (type == ZFS_TYPE_POOL)
184 return (zpool_name_valid(NULL, B_FALSE, name));
185 return (zfs_validate_name(NULL, name, type, B_FALSE));
189 * This function takes the raw DSL properties, and filters out the user-defined
190 * properties into a separate nvlist.
193 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
195 libzfs_handle_t *hdl = zhp->zfs_hdl;
200 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
201 (void) no_memory(hdl);
206 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
207 if (!zfs_prop_user(nvpair_name(elem)))
210 verify(nvpair_value_nvlist(elem, &propval) == 0);
211 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
213 (void) no_memory(hdl);
221 static zpool_handle_t *
222 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
224 libzfs_handle_t *hdl = zhp->zfs_hdl;
227 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
228 if (hdl->libzfs_pool_handles != NULL)
229 zph->zpool_next = hdl->libzfs_pool_handles;
230 hdl->libzfs_pool_handles = zph;
235 static zpool_handle_t *
236 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
238 libzfs_handle_t *hdl = zhp->zfs_hdl;
239 zpool_handle_t *zph = hdl->libzfs_pool_handles;
241 while ((zph != NULL) &&
242 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
243 zph = zph->zpool_next;
248 * Returns a handle to the pool that contains the provided dataset.
249 * If a handle to that pool already exists then that handle is returned.
250 * Otherwise, a new handle is created and added to the list of handles.
252 static zpool_handle_t *
253 zpool_handle(zfs_handle_t *zhp)
259 len = strcspn(zhp->zfs_name, "/@") + 1;
260 pool_name = zfs_alloc(zhp->zfs_hdl, len);
261 (void) strlcpy(pool_name, zhp->zfs_name, len);
263 zph = zpool_find_handle(zhp, pool_name, len);
265 zph = zpool_add_handle(zhp, pool_name);
272 zpool_free_handles(libzfs_handle_t *hdl)
274 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
276 while (zph != NULL) {
277 next = zph->zpool_next;
281 hdl->libzfs_pool_handles = NULL;
285 * Utility function to gather stats (objset and zpl) for the given object.
288 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
290 libzfs_handle_t *hdl = zhp->zfs_hdl;
292 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
294 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
295 if (errno == ENOMEM) {
296 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
307 * Utility function to get the received properties of the given object.
310 get_recvd_props_ioctl(zfs_handle_t *zhp)
312 libzfs_handle_t *hdl = zhp->zfs_hdl;
313 nvlist_t *recvdprops;
314 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
317 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
320 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
322 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
323 if (errno == ENOMEM) {
324 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
328 zcmd_free_nvlists(&zc);
333 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
334 zcmd_free_nvlists(&zc);
338 nvlist_free(zhp->zfs_recvd_props);
339 zhp->zfs_recvd_props = recvdprops;
345 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
347 nvlist_t *allprops, *userprops;
349 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
351 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
356 * XXX Why do we store the user props separately, in addition to
357 * storing them in zfs_props?
359 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
360 nvlist_free(allprops);
364 nvlist_free(zhp->zfs_props);
365 nvlist_free(zhp->zfs_user_props);
367 zhp->zfs_props = allprops;
368 zhp->zfs_user_props = userprops;
374 get_stats(zfs_handle_t *zhp)
377 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
379 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
381 if (get_stats_ioctl(zhp, &zc) != 0)
383 else if (put_stats_zhdl(zhp, &zc) != 0)
385 zcmd_free_nvlists(&zc);
390 * Refresh the properties currently stored in the handle.
393 zfs_refresh_properties(zfs_handle_t *zhp)
395 (void) get_stats(zhp);
399 * Makes a handle from the given dataset name. Used by zfs_open() and
400 * zfs_iter_* to create child handles on the fly.
403 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
405 if (put_stats_zhdl(zhp, zc) != 0)
409 * We've managed to open the dataset and gather statistics. Determine
410 * the high-level type.
412 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
413 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
414 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
415 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
416 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
417 return (-1); /* zpios' and other testing datasets are
418 of this type, ignore if encountered */
422 if (zhp->zfs_dmustats.dds_is_snapshot)
423 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
424 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
425 zhp->zfs_type = ZFS_TYPE_VOLUME;
426 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
427 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
429 abort(); /* we should never see any other types */
431 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
438 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
440 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
442 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
448 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
449 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
453 if (get_stats_ioctl(zhp, &zc) == -1) {
454 zcmd_free_nvlists(&zc);
458 if (make_dataset_handle_common(zhp, &zc) == -1) {
462 zcmd_free_nvlists(&zc);
466 static zfs_handle_t *
467 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
469 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
475 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
476 if (make_dataset_handle_common(zhp, zc) == -1) {
484 * Opens the given snapshot, filesystem, or volume. The 'types'
485 * argument is a mask of acceptable types. The function will print an
486 * appropriate error message and return NULL if it can't be opened.
489 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
494 (void) snprintf(errbuf, sizeof (errbuf),
495 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
498 * Validate the name before we even try to open it.
500 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
502 "invalid dataset name"));
503 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
508 * Try to get stats for the dataset, which will tell us if it exists.
511 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
512 (void) zfs_standard_error(hdl, errno, errbuf);
516 if (!(types & zhp->zfs_type)) {
517 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
526 * Release a ZFS handle. Nothing to do but free the associated memory.
529 zfs_close(zfs_handle_t *zhp)
531 if (zhp->zfs_mntopts)
532 free(zhp->zfs_mntopts);
533 nvlist_free(zhp->zfs_props);
534 nvlist_free(zhp->zfs_user_props);
535 nvlist_free(zhp->zfs_recvd_props);
539 typedef struct mnttab_node {
540 struct mnttab mtn_mt;
545 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
547 const mnttab_node_t *mtn1 = arg1;
548 const mnttab_node_t *mtn2 = arg2;
551 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
555 return (rv > 0 ? 1 : -1);
559 libzfs_mnttab_init(libzfs_handle_t *hdl)
561 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
562 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
563 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
567 libzfs_mnttab_update(libzfs_handle_t *hdl)
571 rewind(hdl->libzfs_mnttab);
572 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
575 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
577 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
578 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
579 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
580 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
581 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
582 avl_add(&hdl->libzfs_mnttab_cache, mtn);
587 libzfs_mnttab_fini(libzfs_handle_t *hdl)
592 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
593 free(mtn->mtn_mt.mnt_special);
594 free(mtn->mtn_mt.mnt_mountp);
595 free(mtn->mtn_mt.mnt_fstype);
596 free(mtn->mtn_mt.mnt_mntopts);
599 avl_destroy(&hdl->libzfs_mnttab_cache);
603 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
605 hdl->libzfs_mnttab_enable = enable;
609 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
610 struct mnttab *entry)
615 if (!hdl->libzfs_mnttab_enable) {
616 struct mnttab srch = { 0 };
618 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
619 libzfs_mnttab_fini(hdl);
620 rewind(hdl->libzfs_mnttab);
621 srch.mnt_special = (char *)fsname;
622 srch.mnt_fstype = MNTTYPE_ZFS;
623 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
629 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
630 libzfs_mnttab_update(hdl);
632 find.mtn_mt.mnt_special = (char *)fsname;
633 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
635 *entry = mtn->mtn_mt;
642 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
643 const char *mountp, const char *mntopts)
647 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
649 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
650 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
651 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
652 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
653 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
654 avl_add(&hdl->libzfs_mnttab_cache, mtn);
658 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
663 find.mtn_mt.mnt_special = (char *)fsname;
664 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
665 avl_remove(&hdl->libzfs_mnttab_cache, ret);
666 free(ret->mtn_mt.mnt_special);
667 free(ret->mtn_mt.mnt_mountp);
668 free(ret->mtn_mt.mnt_fstype);
669 free(ret->mtn_mt.mnt_mntopts);
675 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
677 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
679 if (zpool_handle == NULL)
682 *spa_version = zpool_get_prop_int(zpool_handle,
683 ZPOOL_PROP_VERSION, NULL);
688 * The choice of reservation property depends on the SPA version.
691 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
695 if (zfs_spa_version(zhp, &spa_version) < 0)
698 if (spa_version >= SPA_VERSION_REFRESERVATION)
699 *resv_prop = ZFS_PROP_REFRESERVATION;
701 *resv_prop = ZFS_PROP_RESERVATION;
707 * Given an nvlist of properties to set, validates that they are correct, and
708 * parses any numeric properties (index, boolean, etc) if they are specified as
712 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
713 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
720 int chosen_normal = -1;
723 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
724 (void) no_memory(hdl);
729 * Make sure this property is valid and applies to this type.
733 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
734 const char *propname = nvpair_name(elem);
736 prop = zfs_name_to_prop(propname);
737 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
739 * This is a user property: make sure it's a
740 * string, and that it's less than ZAP_MAXNAMELEN.
742 if (nvpair_type(elem) != DATA_TYPE_STRING) {
743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
744 "'%s' must be a string"), propname);
745 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
749 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
751 "property name '%s' is too long"),
753 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
757 (void) nvpair_value_string(elem, &strval);
758 if (nvlist_add_string(ret, propname, strval) != 0) {
759 (void) no_memory(hdl);
766 * Currently, only user properties can be modified on
769 if (type == ZFS_TYPE_SNAPSHOT) {
770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
771 "this property can not be modified for snapshots"));
772 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
776 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
777 zfs_userquota_prop_t uqtype;
778 char newpropname[128];
783 if (userquota_propname_decode(propname, zoned,
784 &uqtype, domain, sizeof (domain), &rid) != 0) {
786 dgettext(TEXT_DOMAIN,
787 "'%s' has an invalid user/group name"),
789 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
793 if (uqtype != ZFS_PROP_USERQUOTA &&
794 uqtype != ZFS_PROP_GROUPQUOTA) {
796 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
798 (void) zfs_error(hdl, EZFS_PROPREADONLY,
803 if (nvpair_type(elem) == DATA_TYPE_STRING) {
804 (void) nvpair_value_string(elem, &strval);
805 if (strcmp(strval, "none") == 0) {
807 } else if (zfs_nicestrtonum(hdl,
808 strval, &intval) != 0) {
809 (void) zfs_error(hdl,
810 EZFS_BADPROP, errbuf);
813 } else if (nvpair_type(elem) ==
815 (void) nvpair_value_uint64(elem, &intval);
817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
818 "use 'none' to disable "
819 "userquota/groupquota"));
823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
824 "'%s' must be a number"), propname);
825 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
830 * Encode the prop name as
831 * userquota@<hex-rid>-domain, to make it easy
832 * for the kernel to decode.
834 (void) snprintf(newpropname, sizeof (newpropname),
835 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
836 (longlong_t)rid, domain);
840 if (nvlist_add_uint64_array(ret, newpropname,
842 (void) no_memory(hdl);
848 if (prop == ZPROP_INVAL) {
849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
850 "invalid property '%s'"), propname);
851 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
855 if (!zfs_prop_valid_for_type(prop, type)) {
857 dgettext(TEXT_DOMAIN, "'%s' does not "
858 "apply to datasets of this type"), propname);
859 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
863 if (zfs_prop_readonly(prop) &&
864 (!zfs_prop_setonce(prop) || zhp != NULL)) {
866 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
868 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
872 if (zprop_parse_value(hdl, elem, prop, type, ret,
873 &strval, &intval, errbuf) != 0)
877 * Perform some additional checks for specific properties.
880 case ZFS_PROP_VERSION:
886 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
887 if (intval < version) {
888 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
889 "Can not downgrade; already at version %u"),
891 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
897 case ZFS_PROP_RECORDSIZE:
898 case ZFS_PROP_VOLBLOCKSIZE:
899 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
900 if (intval < SPA_MINBLOCKSIZE ||
901 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
903 "'%s' must be power of 2 from %u "
905 (uint_t)SPA_MINBLOCKSIZE,
906 (uint_t)SPA_MAXBLOCKSIZE >> 10);
907 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
912 case ZFS_PROP_MLSLABEL:
916 * Verify the mlslabel string and convert to
917 * internal hex label string.
921 char *hex = NULL; /* internal label string */
923 /* Default value is already OK. */
924 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
927 /* Verify the label can be converted to binary form */
928 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
929 (str_to_label(strval, &new_sl, MAC_LABEL,
930 L_NO_CORRECTION, NULL) == -1)) {
934 /* Now translate to hex internal label string */
935 if (label_to_str(new_sl, &hex, M_INTERNAL,
941 m_label_free(new_sl);
943 /* If string is already in internal form, we're done. */
944 if (strcmp(strval, hex) == 0) {
949 /* Replace the label string with the internal form. */
950 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
952 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
960 "invalid mlslabel '%s'"), strval);
961 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
962 m_label_free(new_sl); /* OK if null */
965 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
966 "mlslabels are unsupported"));
967 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
969 #endif /* HAVE_MLSLABEL */
972 case ZFS_PROP_MOUNTPOINT:
976 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
977 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
980 if (mountpoint_namecheck(strval, &why)) {
982 case NAME_ERR_LEADING_SLASH:
984 dgettext(TEXT_DOMAIN,
985 "'%s' must be an absolute path, "
986 "'none', or 'legacy'"), propname);
988 case NAME_ERR_TOOLONG:
990 dgettext(TEXT_DOMAIN,
991 "component of '%s' is too long"),
997 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1004 case ZFS_PROP_SHARESMB:
1005 case ZFS_PROP_SHARENFS:
1007 * For the mountpoint and sharenfs or sharesmb
1008 * properties, check if it can be set in a
1009 * global/non-global zone based on
1010 * the zoned property value:
1012 * global zone non-global zone
1013 * --------------------------------------------------
1014 * zoned=on mountpoint (no) mountpoint (yes)
1015 * sharenfs (no) sharenfs (no)
1016 * sharesmb (no) sharesmb (no)
1018 * zoned=off mountpoint (yes) N/A
1023 if (getzoneid() == GLOBAL_ZONEID) {
1024 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1025 "'%s' cannot be set on "
1026 "dataset in a non-global zone"),
1028 (void) zfs_error(hdl, EZFS_ZONED,
1031 } else if (prop == ZFS_PROP_SHARENFS ||
1032 prop == ZFS_PROP_SHARESMB) {
1033 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1034 "'%s' cannot be set in "
1035 "a non-global zone"), propname);
1036 (void) zfs_error(hdl, EZFS_ZONED,
1040 } else if (getzoneid() != GLOBAL_ZONEID) {
1042 * If zoned property is 'off', this must be in
1043 * a global zone. If not, something is wrong.
1045 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1046 "'%s' cannot be set while dataset "
1047 "'zoned' property is set"), propname);
1048 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1053 * At this point, it is legitimate to set the
1054 * property. Now we want to make sure that the
1055 * property value is valid if it is sharenfs.
1057 if ((prop == ZFS_PROP_SHARENFS ||
1058 prop == ZFS_PROP_SHARESMB) &&
1059 strcmp(strval, "on") != 0 &&
1060 strcmp(strval, "off") != 0) {
1061 zfs_share_proto_t proto;
1063 if (prop == ZFS_PROP_SHARESMB)
1069 * Must be an valid sharing protocol
1070 * option string so init the libshare
1071 * in order to enable the parser and
1072 * then parse the options. We use the
1073 * control API since we don't care about
1074 * the current configuration and don't
1075 * want the overhead of loading it
1076 * until we actually do something.
1079 if (zfs_init_libshare(hdl,
1080 SA_INIT_CONTROL_API) != SA_OK) {
1082 * An error occurred so we can't do
1085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1086 "'%s' cannot be set: problem "
1087 "in share initialization"),
1089 (void) zfs_error(hdl, EZFS_BADPROP,
1094 if (zfs_parse_options(strval, proto) != SA_OK) {
1096 * There was an error in parsing so
1097 * deal with it by issuing an error
1098 * message and leaving after
1099 * uninitializing the the libshare
1102 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1103 "'%s' cannot be set to invalid "
1104 "options"), propname);
1105 (void) zfs_error(hdl, EZFS_BADPROP,
1107 zfs_uninit_libshare(hdl);
1110 zfs_uninit_libshare(hdl);
1114 case ZFS_PROP_UTF8ONLY:
1115 chosen_utf = (int)intval;
1117 case ZFS_PROP_NORMALIZE:
1118 chosen_normal = (int)intval;
1125 * For changes to existing volumes, we have some additional
1126 * checks to enforce.
1128 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1129 uint64_t volsize = zfs_prop_get_int(zhp,
1131 uint64_t blocksize = zfs_prop_get_int(zhp,
1132 ZFS_PROP_VOLBLOCKSIZE);
1136 case ZFS_PROP_RESERVATION:
1137 case ZFS_PROP_REFRESERVATION:
1138 if (intval > volsize) {
1139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1140 "'%s' is greater than current "
1141 "volume size"), propname);
1142 (void) zfs_error(hdl, EZFS_BADPROP,
1148 case ZFS_PROP_VOLSIZE:
1149 if (intval % blocksize != 0) {
1150 zfs_nicenum(blocksize, buf,
1152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1153 "'%s' must be a multiple of "
1154 "volume block size (%s)"),
1156 (void) zfs_error(hdl, EZFS_BADPROP,
1162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1163 "'%s' cannot be zero"),
1165 (void) zfs_error(hdl, EZFS_BADPROP,
1177 * If normalization was chosen, but no UTF8 choice was made,
1178 * enforce rejection of non-UTF8 names.
1180 * If normalization was chosen, but rejecting non-UTF8 names
1181 * was explicitly not chosen, it is an error.
1183 if (chosen_normal > 0 && chosen_utf < 0) {
1184 if (nvlist_add_uint64(ret,
1185 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1186 (void) no_memory(hdl);
1189 } else if (chosen_normal > 0 && chosen_utf == 0) {
1190 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1191 "'%s' must be set 'on' if normalization chosen"),
1192 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1193 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1204 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1206 uint64_t old_volsize;
1207 uint64_t new_volsize;
1208 uint64_t old_reservation;
1209 uint64_t new_reservation;
1210 zfs_prop_t resv_prop;
1213 * If this is an existing volume, and someone is setting the volsize,
1214 * make sure that it matches the reservation, or add it if necessary.
1216 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1217 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1219 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1220 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1221 old_reservation) || nvlist_lookup_uint64(nvl,
1222 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1225 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1228 new_reservation = zvol_volsize_to_reservation(new_volsize,
1230 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1231 new_reservation) != 0) {
1232 (void) no_memory(zhp->zfs_hdl);
1239 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1246 * For quotas and reservations, ENOSPC indicates
1247 * something different; setting a quota or reservation
1248 * doesn't use any disk space.
1251 case ZFS_PROP_QUOTA:
1252 case ZFS_PROP_REFQUOTA:
1253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1254 "size is less than current used or "
1256 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1259 case ZFS_PROP_RESERVATION:
1260 case ZFS_PROP_REFRESERVATION:
1261 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1262 "size is greater than available space"));
1263 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1267 (void) zfs_standard_error(hdl, err, errbuf);
1273 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1277 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1281 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1282 "pool and or dataset must be upgraded to set this "
1283 "property or value"));
1284 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1288 if (prop == ZFS_PROP_COMPRESSION) {
1289 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1290 "property setting is not allowed on "
1291 "bootable datasets"));
1292 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1294 (void) zfs_standard_error(hdl, err, errbuf);
1299 if (prop == ZPROP_INVAL) {
1300 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1302 (void) zfs_standard_error(hdl, err, errbuf);
1308 * This platform can't address a volume this big.
1311 if (prop == ZFS_PROP_VOLSIZE) {
1312 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1318 (void) zfs_standard_error(hdl, err, errbuf);
1323 zfs_is_namespace_prop(zfs_prop_t prop)
1327 case ZFS_PROP_ATIME:
1328 case ZFS_PROP_DEVICES:
1330 case ZFS_PROP_SETUID:
1331 case ZFS_PROP_READONLY:
1332 case ZFS_PROP_XATTR:
1333 case ZFS_PROP_NBMAND:
1342 * Given a property name and value, set the property for the given dataset.
1345 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1347 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1349 prop_changelist_t *cl = NULL;
1351 libzfs_handle_t *hdl = zhp->zfs_hdl;
1352 nvlist_t *nvl = NULL, *realprops;
1354 boolean_t do_prefix;
1358 (void) snprintf(errbuf, sizeof (errbuf),
1359 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1362 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1363 nvlist_add_string(nvl, propname, propval) != 0) {
1364 (void) no_memory(hdl);
1368 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1369 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1375 prop = zfs_name_to_prop(propname);
1377 if (prop == ZFS_PROP_VOLSIZE) {
1378 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1382 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1385 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1386 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1387 "child dataset with inherited mountpoint is used "
1388 "in a non-global zone"));
1389 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1394 * If the dataset's canmount property is being set to noauto,
1395 * then we want to prevent unmounting & remounting it.
1397 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1398 (zprop_string_to_index(prop, propval, &idx,
1399 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1401 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1405 * Execute the corresponding ioctl() to set this property.
1407 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1409 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1412 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1415 zfs_setprop_error(hdl, prop, errno, errbuf);
1416 if (added_resv && errno == ENOSPC) {
1417 /* clean up the volsize property we tried to set */
1418 uint64_t old_volsize = zfs_prop_get_int(zhp,
1421 zcmd_free_nvlists(&zc);
1422 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1424 if (nvlist_add_uint64(nvl,
1425 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1428 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1430 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1434 ret = changelist_postfix(cl);
1438 * Refresh the statistics so the new property
1439 * value is reflected.
1441 (void) get_stats(zhp);
1444 * Remount the filesystem to propagate the change
1445 * if one of the options handled by the generic
1446 * Linux namespace layer has been modified.
1448 if (zfs_is_namespace_prop(prop) &&
1449 zfs_is_mounted(zhp, NULL))
1450 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1456 zcmd_free_nvlists(&zc);
1458 changelist_free(cl);
1463 * Given a property, inherit the value from the parent dataset, or if received
1464 * is TRUE, revert to the received value, if any.
1467 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1469 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1471 prop_changelist_t *cl;
1472 libzfs_handle_t *hdl = zhp->zfs_hdl;
1476 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1477 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1479 zc.zc_cookie = received;
1480 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1482 * For user properties, the amount of work we have to do is very
1483 * small, so just do it here.
1485 if (!zfs_prop_user(propname)) {
1486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1487 "invalid property"));
1488 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1491 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1492 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1494 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1495 return (zfs_standard_error(hdl, errno, errbuf));
1501 * Verify that this property is inheritable.
1503 if (zfs_prop_readonly(prop))
1504 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1506 if (!zfs_prop_inheritable(prop) && !received)
1507 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1510 * Check to see if the value applies to this type
1512 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1513 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1516 * Normalize the name, to get rid of shorthand abbreviations.
1518 propname = zfs_prop_to_name(prop);
1519 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1520 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1522 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1523 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1525 "dataset is used in a non-global zone"));
1526 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1530 * Determine datasets which will be affected by this change, if any.
1532 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1535 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1536 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1537 "child dataset with inherited mountpoint is used "
1538 "in a non-global zone"));
1539 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1543 if ((ret = changelist_prefix(cl)) != 0)
1546 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1547 return (zfs_standard_error(hdl, errno, errbuf));
1550 if ((ret = changelist_postfix(cl)) != 0)
1554 * Refresh the statistics so the new property is reflected.
1556 (void) get_stats(zhp);
1560 changelist_free(cl);
1565 * True DSL properties are stored in an nvlist. The following two functions
1566 * extract them appropriately.
1569 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1575 if (nvlist_lookup_nvlist(zhp->zfs_props,
1576 zfs_prop_to_name(prop), &nv) == 0) {
1577 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1578 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1580 verify(!zhp->zfs_props_table ||
1581 zhp->zfs_props_table[prop] == B_TRUE);
1582 value = zfs_prop_default_numeric(prop);
1590 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1596 if (nvlist_lookup_nvlist(zhp->zfs_props,
1597 zfs_prop_to_name(prop), &nv) == 0) {
1598 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1599 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1601 verify(!zhp->zfs_props_table ||
1602 zhp->zfs_props_table[prop] == B_TRUE);
1603 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1612 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1614 return (zhp->zfs_props == zhp->zfs_recvd_props);
1618 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1620 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1621 zhp->zfs_props = zhp->zfs_recvd_props;
1625 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1627 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1632 * Internal function for getting a numeric property. Both zfs_prop_get() and
1633 * zfs_prop_get_int() are built using this interface.
1635 * Certain properties can be overridden using 'mount -o'. In this case, scan
1636 * the contents of the /etc/mtab entry, searching for the appropriate options.
1637 * If they differ from the on-disk values, report the current values and mark
1638 * the source "temporary".
1641 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1642 char **source, uint64_t *val)
1644 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1645 nvlist_t *zplprops = NULL;
1647 char *mntopt_on = NULL;
1648 char *mntopt_off = NULL;
1649 boolean_t received = zfs_is_recvd_props_mode(zhp);
1654 case ZFS_PROP_ATIME:
1655 mntopt_on = MNTOPT_ATIME;
1656 mntopt_off = MNTOPT_NOATIME;
1659 case ZFS_PROP_DEVICES:
1660 mntopt_on = MNTOPT_DEVICES;
1661 mntopt_off = MNTOPT_NODEVICES;
1665 mntopt_on = MNTOPT_EXEC;
1666 mntopt_off = MNTOPT_NOEXEC;
1669 case ZFS_PROP_READONLY:
1670 mntopt_on = MNTOPT_RO;
1671 mntopt_off = MNTOPT_RW;
1674 case ZFS_PROP_SETUID:
1675 mntopt_on = MNTOPT_SETUID;
1676 mntopt_off = MNTOPT_NOSETUID;
1679 case ZFS_PROP_XATTR:
1680 mntopt_on = MNTOPT_XATTR;
1681 mntopt_off = MNTOPT_NOXATTR;
1684 case ZFS_PROP_NBMAND:
1685 mntopt_on = MNTOPT_NBMAND;
1686 mntopt_off = MNTOPT_NONBMAND;
1693 * Because looking up the mount options is potentially expensive
1694 * (iterating over all of /etc/mtab), we defer its calculation until
1695 * we're looking up a property which requires its presence.
1697 if (!zhp->zfs_mntcheck &&
1698 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1699 libzfs_handle_t *hdl = zhp->zfs_hdl;
1700 struct mnttab entry;
1702 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1703 zhp->zfs_mntopts = zfs_strdup(hdl,
1705 if (zhp->zfs_mntopts == NULL)
1709 zhp->zfs_mntcheck = B_TRUE;
1712 if (zhp->zfs_mntopts == NULL)
1713 mnt.mnt_mntopts = "";
1715 mnt.mnt_mntopts = zhp->zfs_mntopts;
1718 case ZFS_PROP_ATIME:
1719 case ZFS_PROP_DEVICES:
1721 case ZFS_PROP_READONLY:
1722 case ZFS_PROP_SETUID:
1723 case ZFS_PROP_XATTR:
1724 case ZFS_PROP_NBMAND:
1725 *val = getprop_uint64(zhp, prop, source);
1730 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1733 *src = ZPROP_SRC_TEMPORARY;
1734 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1737 *src = ZPROP_SRC_TEMPORARY;
1741 case ZFS_PROP_CANMOUNT:
1742 case ZFS_PROP_VOLSIZE:
1743 case ZFS_PROP_QUOTA:
1744 case ZFS_PROP_REFQUOTA:
1745 case ZFS_PROP_RESERVATION:
1746 case ZFS_PROP_REFRESERVATION:
1747 *val = getprop_uint64(zhp, prop, source);
1749 if (*source == NULL) {
1750 /* not default, must be local */
1751 *source = zhp->zfs_name;
1755 case ZFS_PROP_MOUNTED:
1756 *val = (zhp->zfs_mntopts != NULL);
1759 case ZFS_PROP_NUMCLONES:
1760 *val = zhp->zfs_dmustats.dds_num_clones;
1763 case ZFS_PROP_VERSION:
1764 case ZFS_PROP_NORMALIZE:
1765 case ZFS_PROP_UTF8ONLY:
1767 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1768 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1770 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1771 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1772 zcmd_free_nvlists(&zc);
1775 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1776 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1778 zcmd_free_nvlists(&zc);
1782 nvlist_free(zplprops);
1783 zcmd_free_nvlists(&zc);
1787 switch (zfs_prop_get_type(prop)) {
1788 case PROP_TYPE_NUMBER:
1789 case PROP_TYPE_INDEX:
1790 *val = getprop_uint64(zhp, prop, source);
1792 * If we tried to use a default value for a
1793 * readonly property, it means that it was not
1796 if (zfs_prop_readonly(prop) &&
1797 *source != NULL && (*source)[0] == '\0') {
1802 case PROP_TYPE_STRING:
1804 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1805 "cannot get non-numeric property"));
1806 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1807 dgettext(TEXT_DOMAIN, "internal error")));
1815 * Calculate the source type, given the raw source string.
1818 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1819 char *statbuf, size_t statlen)
1821 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1824 if (source == NULL) {
1825 *srctype = ZPROP_SRC_NONE;
1826 } else if (source[0] == '\0') {
1827 *srctype = ZPROP_SRC_DEFAULT;
1828 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1829 *srctype = ZPROP_SRC_RECEIVED;
1831 if (strcmp(source, zhp->zfs_name) == 0) {
1832 *srctype = ZPROP_SRC_LOCAL;
1834 (void) strlcpy(statbuf, source, statlen);
1835 *srctype = ZPROP_SRC_INHERITED;
1842 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1843 size_t proplen, boolean_t literal)
1848 if (zhp->zfs_recvd_props == NULL)
1849 if (get_recvd_props_ioctl(zhp) != 0)
1852 prop = zfs_name_to_prop(propname);
1854 if (prop != ZPROP_INVAL) {
1856 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1858 zfs_set_recvd_props_mode(zhp, &cookie);
1859 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1860 NULL, NULL, 0, literal);
1861 zfs_unset_recvd_props_mode(zhp, &cookie);
1862 } else if (zfs_prop_userquota(propname)) {
1867 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1868 propname, &propval) != 0)
1870 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1872 (void) strlcpy(propbuf, recvdval, proplen);
1875 return (err == 0 ? 0 : -1);
1879 * Retrieve a property from the given object. If 'literal' is specified, then
1880 * numbers are left as exact values. Otherwise, numbers are converted to a
1881 * human-readable form.
1883 * Returns 0 on success, or -1 on error.
1886 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1887 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1889 char *source = NULL;
1893 boolean_t received = zfs_is_recvd_props_mode(zhp);
1896 * Check to see if this property applies to our object
1898 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1901 if (received && zfs_prop_readonly(prop))
1905 *src = ZPROP_SRC_NONE;
1908 case ZFS_PROP_CREATION:
1910 * 'creation' is a time_t stored in the statistics. We convert
1911 * this into a string unless 'literal' is specified.
1914 val = getprop_uint64(zhp, prop, &source);
1915 time_t time = (time_t)val;
1919 localtime_r(&time, &t) == NULL ||
1920 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1922 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
1926 case ZFS_PROP_MOUNTPOINT:
1928 * Getting the precise mountpoint can be tricky.
1930 * - for 'none' or 'legacy', return those values.
1931 * - for inherited mountpoints, we want to take everything
1932 * after our ancestor and append it to the inherited value.
1934 * If the pool has an alternate root, we want to prepend that
1935 * root to any values we return.
1938 str = getprop_string(zhp, prop, &source);
1940 if (str[0] == '/') {
1941 char buf[MAXPATHLEN];
1943 const char *relpath;
1946 * If we inherit the mountpoint, even from a dataset
1947 * with a received value, the source will be the path of
1948 * the dataset we inherit from. If source is
1949 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1952 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1955 relpath = zhp->zfs_name + strlen(source);
1956 if (relpath[0] == '/')
1960 if ((zpool_get_prop(zhp->zpool_hdl,
1961 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1962 (strcmp(root, "-") == 0))
1965 * Special case an alternate root of '/'. This will
1966 * avoid having multiple leading slashes in the
1969 if (strcmp(root, "/") == 0)
1973 * If the mountpoint is '/' then skip over this
1974 * if we are obtaining either an alternate root or
1975 * an inherited mountpoint.
1977 if (str[1] == '\0' && (root[0] != '\0' ||
1978 relpath[0] != '\0'))
1981 if (relpath[0] == '\0')
1982 (void) snprintf(propbuf, proplen, "%s%s",
1985 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1986 root, str, relpath[0] == '@' ? "" : "/",
1989 /* 'legacy' or 'none' */
1990 (void) strlcpy(propbuf, str, proplen);
1995 case ZFS_PROP_ORIGIN:
1996 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1999 * If there is no parent at all, return failure to indicate that
2000 * it doesn't apply to this dataset.
2002 if (propbuf[0] == '\0')
2006 case ZFS_PROP_QUOTA:
2007 case ZFS_PROP_REFQUOTA:
2008 case ZFS_PROP_RESERVATION:
2009 case ZFS_PROP_REFRESERVATION:
2011 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2015 * If quota or reservation is 0, we translate this into 'none'
2016 * (unless literal is set), and indicate that it's the default
2017 * value. Otherwise, we print the number nicely and indicate
2018 * that its set locally.
2022 (void) strlcpy(propbuf, "0", proplen);
2024 (void) strlcpy(propbuf, "none", proplen);
2027 (void) snprintf(propbuf, proplen, "%llu",
2030 zfs_nicenum(val, propbuf, proplen);
2034 case ZFS_PROP_REFRATIO:
2035 case ZFS_PROP_COMPRESSRATIO:
2036 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2038 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2039 (u_longlong_t)(val / 100),
2040 (u_longlong_t)(val % 100));
2044 switch (zhp->zfs_type) {
2045 case ZFS_TYPE_FILESYSTEM:
2048 case ZFS_TYPE_VOLUME:
2051 case ZFS_TYPE_SNAPSHOT:
2057 (void) snprintf(propbuf, proplen, "%s", str);
2060 case ZFS_PROP_MOUNTED:
2062 * The 'mounted' property is a pseudo-property that described
2063 * whether the filesystem is currently mounted. Even though
2064 * it's a boolean value, the typical values of "on" and "off"
2065 * don't make sense, so we translate to "yes" and "no".
2067 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2068 src, &source, &val) != 0)
2071 (void) strlcpy(propbuf, "yes", proplen);
2073 (void) strlcpy(propbuf, "no", proplen);
2078 * The 'name' property is a pseudo-property derived from the
2079 * dataset name. It is presented as a real property to simplify
2082 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2085 case ZFS_PROP_MLSLABEL:
2087 #ifdef HAVE_MLSLABEL
2088 m_label_t *new_sl = NULL;
2089 char *ascii = NULL; /* human readable label */
2091 (void) strlcpy(propbuf,
2092 getprop_string(zhp, prop, &source), proplen);
2094 if (literal || (strcasecmp(propbuf,
2095 ZFS_MLSLABEL_DEFAULT) == 0))
2099 * Try to translate the internal hex string to
2100 * human-readable output. If there are any
2101 * problems just use the hex string.
2104 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2105 L_NO_CORRECTION, NULL) == -1) {
2106 m_label_free(new_sl);
2110 if (label_to_str(new_sl, &ascii, M_LABEL,
2114 m_label_free(new_sl);
2117 m_label_free(new_sl);
2119 (void) strlcpy(propbuf, ascii, proplen);
2122 (void) strlcpy(propbuf,
2123 getprop_string(zhp, prop, &source), proplen);
2124 #endif /* HAVE_MLSLABEL */
2129 switch (zfs_prop_get_type(prop)) {
2130 case PROP_TYPE_NUMBER:
2131 if (get_numeric_property(zhp, prop, src,
2132 &source, &val) != 0)
2135 (void) snprintf(propbuf, proplen, "%llu",
2138 zfs_nicenum(val, propbuf, proplen);
2141 case PROP_TYPE_STRING:
2142 (void) strlcpy(propbuf,
2143 getprop_string(zhp, prop, &source), proplen);
2146 case PROP_TYPE_INDEX:
2147 if (get_numeric_property(zhp, prop, src,
2148 &source, &val) != 0)
2150 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2152 (void) strlcpy(propbuf, strval, proplen);
2160 get_source(zhp, src, source, statbuf, statlen);
2166 * Utility function to get the given numeric property. Does no validation that
2167 * the given property is the appropriate type; should only be used with
2168 * hard-coded property types.
2171 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2176 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2182 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2186 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2187 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2191 * Similar to zfs_prop_get(), but returns the value as an integer.
2194 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2195 zprop_source_t *src, char *statbuf, size_t statlen)
2200 * Check to see if this property applies to our object
2202 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2203 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2204 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2205 zfs_prop_to_name(prop)));
2209 *src = ZPROP_SRC_NONE;
2211 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2214 get_source(zhp, src, source, statbuf, statlen);
2221 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2222 char **domainp, idmap_rid_t *ridp)
2224 idmap_get_handle_t *get_hdl = NULL;
2228 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2232 err = idmap_get_sidbyuid(get_hdl, id,
2233 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2235 err = idmap_get_sidbygid(get_hdl, id,
2236 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2238 if (err == IDMAP_SUCCESS &&
2239 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2240 status == IDMAP_SUCCESS)
2246 idmap_get_destroy(get_hdl);
2249 #endif /* HAVE_IDMAP */
2252 * convert the propname into parameters needed by kernel
2253 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2254 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2255 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2256 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2259 userquota_propname_decode(const char *propname, boolean_t zoned,
2260 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2262 zfs_userquota_prop_t type;
2271 /* Figure out the property type ({user|group}{quota|space}) */
2272 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2273 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2274 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2277 if (type == ZFS_NUM_USERQUOTA_PROPS)
2281 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2282 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2284 cp = strchr(propname, '@') + 1;
2286 if (isuser && (pw = getpwnam(cp)) != NULL) {
2287 if (zoned && getzoneid() == GLOBAL_ZONEID)
2290 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2291 if (zoned && getzoneid() == GLOBAL_ZONEID)
2294 } else if (strchr(cp, '@')) {
2297 * It's a SID name (eg "user@domain") that needs to be
2298 * turned into S-1-domainID-RID.
2300 directory_error_t e;
2301 char *numericsid = NULL;
2304 if (zoned && getzoneid() == GLOBAL_ZONEID)
2307 e = directory_sid_from_user_name(NULL,
2310 e = directory_sid_from_group_name(NULL,
2314 directory_error_free(e);
2317 if (numericsid == NULL)
2320 (void) strlcpy(domain, cp, domainlen);
2321 cp = strrchr(domain, '-');
2326 *ridp = strtoull(cp, &end, 10);
2329 if (errno != 0 || *end != '\0')
2333 #endif /* HAVE_IDMAP */
2336 /* It's a user/group ID (eg "12345"). */
2342 id = strtoul(cp, &end, 10);
2346 /* It's an ephemeral ID. */
2347 if (idmap_id_to_numeric_domain_rid(id, isuser,
2348 &mapdomain, &rid) != 0)
2350 (void) strlcpy(domain, mapdomain, domainlen);
2357 #endif /* HAVE_IDMAP */
2364 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2365 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2368 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2370 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2372 err = userquota_propname_decode(propname,
2373 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2374 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2375 zc.zc_objset_type = *typep;
2379 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2383 *propvalue = zc.zc_cookie;
2388 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2389 uint64_t *propvalue)
2391 zfs_userquota_prop_t type;
2393 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2398 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2399 char *propbuf, int proplen, boolean_t literal)
2403 zfs_userquota_prop_t type;
2405 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2412 (void) snprintf(propbuf, proplen, "%llu",
2413 (u_longlong_t)propvalue);
2414 } else if (propvalue == 0 &&
2415 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2416 (void) strlcpy(propbuf, "none", proplen);
2418 zfs_nicenum(propvalue, propbuf, proplen);
2424 * Returns the name of the given zfs handle.
2427 zfs_get_name(const zfs_handle_t *zhp)
2429 return (zhp->zfs_name);
2433 * Returns the type of the given zfs handle.
2436 zfs_get_type(const zfs_handle_t *zhp)
2438 return (zhp->zfs_type);
2442 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2445 uint64_t orig_cookie;
2447 orig_cookie = zc->zc_cookie;
2449 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2450 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2455 /* expand nvlist memory and try again */
2456 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2457 zcmd_free_nvlists(zc);
2460 zc->zc_cookie = orig_cookie;
2463 * An errno value of ESRCH indicates normal completion.
2464 * If ENOENT is returned, then the underlying dataset
2465 * has been removed since we obtained the handle.
2472 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2473 dgettext(TEXT_DOMAIN,
2474 "cannot iterate filesystems"));
2482 * Iterate over all child filesystems
2485 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2487 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2491 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2494 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2497 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2500 * Silently ignore errors, as the only plausible explanation is
2501 * that the pool has since been removed.
2503 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2508 if ((ret = func(nzhp, data)) != 0) {
2509 zcmd_free_nvlists(&zc);
2513 zcmd_free_nvlists(&zc);
2514 return ((ret < 0) ? ret : 0);
2518 * Iterate over all snapshots
2521 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2523 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2527 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2530 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2532 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2535 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2540 if ((ret = func(nzhp, data)) != 0) {
2541 zcmd_free_nvlists(&zc);
2545 zcmd_free_nvlists(&zc);
2546 return ((ret < 0) ? ret : 0);
2550 * Iterate over all children, snapshots and filesystems
2553 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2557 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2560 return (zfs_iter_snapshots(zhp, func, data));
2564 * Is one dataset name a child dataset of another?
2566 * Needs to handle these cases:
2567 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2568 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2569 * Descendant? No. No. No. Yes.
2572 is_descendant(const char *ds1, const char *ds2)
2574 size_t d1len = strlen(ds1);
2576 /* ds2 can't be a descendant if it's smaller */
2577 if (strlen(ds2) < d1len)
2580 /* otherwise, compare strings and verify that there's a '/' char */
2581 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2585 * Given a complete name, return just the portion that refers to the parent.
2586 * Can return NULL if this is a pool.
2589 parent_name(const char *path, char *buf, size_t buflen)
2593 if ((loc = strrchr(path, '/')) == NULL)
2596 (void) strncpy(buf, path, MIN(buflen, loc - path));
2597 buf[loc - path] = '\0';
2603 * If accept_ancestor is false, then check to make sure that the given path has
2604 * a parent, and that it exists. If accept_ancestor is true, then find the
2605 * closest existing ancestor for the given path. In prefixlen return the
2606 * length of already existing prefix of the given path. We also fetch the
2607 * 'zoned' property, which is used to validate property settings when creating
2611 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2612 boolean_t accept_ancestor, int *prefixlen)
2614 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2615 char parent[ZFS_MAXNAMELEN];
2621 (void) snprintf(errbuf, sizeof (errbuf),
2622 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2624 /* get parent, and check to see if this is just a pool */
2625 if (parent_name(path, parent, sizeof (parent)) != 0) {
2626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2627 "missing dataset name"));
2628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2631 /* check to see if the pool exists */
2632 if ((slash = strchr(parent, '/')) == NULL)
2633 slash = parent + strlen(parent);
2634 (void) strncpy(zc.zc_name, parent, slash - parent);
2635 zc.zc_name[slash - parent] = '\0';
2636 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2638 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2639 "no such pool '%s'"), zc.zc_name);
2640 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2643 /* check to see if the parent dataset exists */
2644 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2645 if (errno == ENOENT && accept_ancestor) {
2647 * Go deeper to find an ancestor, give up on top level.
2649 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2650 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2651 "no such pool '%s'"), zc.zc_name);
2652 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2654 } else if (errno == ENOENT) {
2655 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2656 "parent does not exist"));
2657 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2659 return (zfs_standard_error(hdl, errno, errbuf));
2662 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2666 /* we are in a non-global zone, but parent is in the global zone */
2667 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2668 (void) zfs_standard_error(hdl, EPERM, errbuf);
2673 /* make sure parent is a filesystem */
2674 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2675 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2676 "parent is not a filesystem"));
2677 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2683 if (prefixlen != NULL)
2684 *prefixlen = strlen(parent);
2689 * Finds whether the dataset of the given type(s) exists.
2692 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2696 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2700 * Try to get stats for the dataset, which will tell us if it exists.
2702 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2703 int ds_type = zhp->zfs_type;
2706 if (types & ds_type)
2713 * Given a path to 'target', create all the ancestors between
2714 * the prefixlen portion of the path, and the target itself.
2715 * Fail if the initial prefixlen-ancestor does not already exist.
2718 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2724 /* make sure prefix exists */
2725 cp = target + prefixlen;
2727 assert(strchr(cp, '/') == NULL);
2728 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2731 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2739 * Attempt to create, mount, and share any ancestor filesystems,
2740 * up to the prefixlen-long one.
2742 for (cp = target + prefixlen + 1;
2743 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2748 h = make_dataset_handle(hdl, target);
2750 /* it already exists, nothing to do here */
2755 logstr = hdl->libzfs_log_str;
2756 hdl->libzfs_log_str = NULL;
2757 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2759 hdl->libzfs_log_str = logstr;
2760 opname = dgettext(TEXT_DOMAIN, "create");
2764 hdl->libzfs_log_str = logstr;
2765 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2767 opname = dgettext(TEXT_DOMAIN, "open");
2771 if (zfs_mount(h, NULL, 0) != 0) {
2772 opname = dgettext(TEXT_DOMAIN, "mount");
2776 if (zfs_share(h) != 0) {
2777 opname = dgettext(TEXT_DOMAIN, "share");
2787 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2788 "failed to %s ancestor '%s'"), opname, target);
2793 * Creates non-existing ancestors of the given path.
2796 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2802 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2805 if ((path_copy = strdup(path)) != NULL) {
2806 rc = create_parents(hdl, path_copy, prefix);
2809 if (path_copy == NULL || rc != 0)
2816 * Create a new filesystem or volume.
2819 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2822 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2825 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2829 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2830 "cannot create '%s'"), path);
2832 /* validate the path, taking care to note the extended error message */
2833 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2834 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2836 /* validate parents exist */
2837 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2841 * The failure modes when creating a dataset of a different type over
2842 * one that already exists is a little strange. In particular, if you
2843 * try to create a dataset on top of an existing dataset, the ioctl()
2844 * will return ENOENT, not EEXIST. To prevent this from happening, we
2845 * first try to see if the dataset exists.
2847 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2848 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2850 "dataset already exists"));
2851 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2854 if (type == ZFS_TYPE_VOLUME)
2855 zc.zc_objset_type = DMU_OST_ZVOL;
2857 zc.zc_objset_type = DMU_OST_ZFS;
2859 if (props && (props = zfs_valid_proplist(hdl, type, props,
2860 zoned, NULL, errbuf)) == 0)
2863 if (type == ZFS_TYPE_VOLUME) {
2865 * If we are creating a volume, the size and block size must
2866 * satisfy a few restraints. First, the blocksize must be a
2867 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2868 * volsize must be a multiple of the block size, and cannot be
2871 if (props == NULL || nvlist_lookup_uint64(props,
2872 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2875 "missing volume size"));
2876 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2879 if ((ret = nvlist_lookup_uint64(props,
2880 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2881 &blocksize)) != 0) {
2882 if (ret == ENOENT) {
2883 blocksize = zfs_prop_default_numeric(
2884 ZFS_PROP_VOLBLOCKSIZE);
2887 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2888 "missing volume block size"));
2889 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2895 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2896 "volume size cannot be zero"));
2897 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2900 if (size % blocksize != 0) {
2902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2903 "volume size must be a multiple of volume block "
2905 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2909 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2913 /* create the dataset */
2914 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2916 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2917 ret = zvol_create_link(hdl, path);
2919 (void) zfs_standard_error(hdl, errno,
2920 dgettext(TEXT_DOMAIN,
2921 "Volume successfully created, but device links "
2922 "were not created"));
2923 zcmd_free_nvlists(&zc);
2928 zcmd_free_nvlists(&zc);
2930 /* check for failure */
2932 char parent[ZFS_MAXNAMELEN];
2933 (void) parent_name(path, parent, sizeof (parent));
2937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2938 "no such parent '%s'"), parent);
2939 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2942 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2943 "parent '%s' is not a filesystem"), parent);
2944 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2948 "volume block size must be power of 2 from "
2950 (uint_t)SPA_MINBLOCKSIZE,
2951 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2953 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2956 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2957 "pool must be upgraded to set this "
2958 "property or value"));
2959 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2963 * This platform can't address a volume this big.
2965 if (type == ZFS_TYPE_VOLUME)
2966 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2971 return (zfs_standard_error(hdl, errno, errbuf));
2979 * Destroys the given dataset. The caller must make sure that the filesystem
2980 * isn't mounted, and that there are no active dependents.
2983 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2985 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2987 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2989 if (ZFS_IS_VOLUME(zhp)) {
2990 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2993 zc.zc_objset_type = DMU_OST_ZVOL;
2995 zc.zc_objset_type = DMU_OST_ZFS;
2998 zc.zc_defer_destroy = defer;
2999 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3000 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3001 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3005 remove_mountpoint(zhp);
3010 struct destroydata {
3017 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3019 struct destroydata *dd = arg;
3021 char name[ZFS_MAXNAMELEN];
3022 boolean_t closezhp = dd->closezhp;
3025 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3026 (void) strlcat(name, "@", sizeof (name));
3027 (void) strlcat(name, dd->snapname, sizeof (name));
3029 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3031 dd->gotone = B_TRUE;
3035 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3036 (void) zvol_remove_link(zhp->zfs_hdl, name);
3038 * NB: this is simply a best-effort. We don't want to
3039 * return an error, because then we wouldn't visit all
3044 dd->closezhp = B_TRUE;
3045 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
3052 * Destroys all snapshots with the given name in zhp & descendants.
3055 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3057 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3059 struct destroydata dd = { 0 };
3061 dd.snapname = snapname;
3062 (void) zfs_check_snap_cb(zhp, &dd);
3065 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3066 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3067 zhp->zfs_name, snapname));
3070 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3071 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3072 zc.zc_defer_destroy = defer;
3074 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3078 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3079 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3083 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3084 "snapshot is cloned"));
3085 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3088 return (zfs_standard_error(zhp->zfs_hdl, errno,
3097 * Clones the given dataset. The target must be of the same type as the source.
3100 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3102 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3103 char parent[ZFS_MAXNAMELEN];
3106 libzfs_handle_t *hdl = zhp->zfs_hdl;
3110 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3112 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3113 "cannot create '%s'"), target);
3115 /* validate the target name */
3116 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3117 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3119 /* validate parents exist */
3120 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3123 (void) parent_name(target, parent, sizeof (parent));
3126 if (ZFS_IS_VOLUME(zhp)) {
3127 zc.zc_objset_type = DMU_OST_ZVOL;
3128 type = ZFS_TYPE_VOLUME;
3130 zc.zc_objset_type = DMU_OST_ZFS;
3131 type = ZFS_TYPE_FILESYSTEM;
3135 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3136 zhp, errbuf)) == NULL)
3139 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3147 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3148 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3149 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3151 zcmd_free_nvlists(&zc);
3158 * The parent doesn't exist. We should have caught this
3159 * above, but there may a race condition that has since
3160 * destroyed the parent.
3162 * At this point, we don't know whether it's the source
3163 * that doesn't exist anymore, or whether the target
3164 * dataset doesn't exist.
3166 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3167 "no such parent '%s'"), parent);
3168 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3171 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3172 "source and target pools differ"));
3173 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3177 return (zfs_standard_error(zhp->zfs_hdl, errno,
3180 } else if (ZFS_IS_VOLUME(zhp)) {
3181 ret = zvol_create_link(zhp->zfs_hdl, target);
3187 typedef struct promote_data {
3188 char cb_mountpoint[MAXPATHLEN];
3189 const char *cb_target;
3190 const char *cb_errbuf;
3191 uint64_t cb_pivot_txg;
3195 promote_snap_cb(zfs_handle_t *zhp, void *data)
3197 promote_data_t *pd = data;
3199 char snapname[MAXPATHLEN];
3202 /* We don't care about snapshots after the pivot point */
3203 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3208 /* Remove the device link if it's a zvol. */
3209 if (ZFS_IS_VOLUME(zhp))
3210 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3212 /* Check for conflicting names */
3213 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3214 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3215 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3218 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3219 "snapshot name '%s' from origin \n"
3220 "conflicts with '%s' from target"),
3221 zhp->zfs_name, snapname);
3222 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3229 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3231 promote_data_t *pd = data;
3233 /* We don't care about snapshots after the pivot point */
3234 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3235 /* Create the device link if it's a zvol. */
3236 if (ZFS_IS_VOLUME(zhp))
3237 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3245 * Promotes the given clone fs to be the clone parent.
3248 zfs_promote(zfs_handle_t *zhp)
3250 libzfs_handle_t *hdl = zhp->zfs_hdl;
3251 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3252 char parent[MAXPATHLEN];
3259 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3260 "cannot promote '%s'"), zhp->zfs_name);
3262 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3263 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3264 "snapshots can not be promoted"));
3265 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3268 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3269 if (parent[0] == '\0') {
3270 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3271 "not a cloned filesystem"));
3272 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3274 cp = strchr(parent, '@');
3277 /* Walk the snapshots we will be moving */
3278 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3281 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3283 pd.cb_target = zhp->zfs_name;
3284 pd.cb_errbuf = errbuf;
3285 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3288 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3289 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3290 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3296 /* issue the ioctl */
3297 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3298 sizeof (zc.zc_value));
3299 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3300 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3303 int save_errno = errno;
3305 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3308 switch (save_errno) {
3311 * There is a conflicting snapshot name. We
3312 * should have caught this above, but they could
3313 * have renamed something in the mean time.
3315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3316 "conflicting snapshot '%s' from parent '%s'"),
3317 zc.zc_string, parent);
3318 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3321 return (zfs_standard_error(hdl, save_errno, errbuf));
3324 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3332 const char *cd_snapname;
3337 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3339 struct createdata *cd = arg;
3342 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3343 char name[MAXPATHLEN];
3345 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3346 (void) strlcat(name, "@", sizeof (name));
3347 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3348 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3351 * NB: this is simply a best-effort. We don't want to
3352 * return an error, because then we wouldn't visit all
3357 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3365 * Takes a snapshot of the given dataset.
3368 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3372 char parent[ZFS_MAXNAMELEN];
3374 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3378 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3379 "cannot snapshot '%s'"), path);
3381 /* validate the target name */
3382 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3383 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3386 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3387 props, B_FALSE, NULL, errbuf)) == NULL)
3390 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3398 /* make sure the parent exists and is of the appropriate type */
3399 delim = strchr(path, '@');
3400 (void) strncpy(parent, path, delim - path);
3401 parent[delim - path] = '\0';
3403 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3404 ZFS_TYPE_VOLUME)) == NULL) {
3405 zcmd_free_nvlists(&zc);
3409 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3410 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3411 if (ZFS_IS_VOLUME(zhp))
3412 zc.zc_objset_type = DMU_OST_ZVOL;
3414 zc.zc_objset_type = DMU_OST_ZFS;
3415 zc.zc_cookie = recursive;
3416 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3418 zcmd_free_nvlists(&zc);
3421 * if it was recursive, the one that actually failed will be in
3425 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3426 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3428 if (ret == 0 && recursive) {
3429 struct createdata cd;
3431 cd.cd_snapname = delim + 1;
3432 cd.cd_ifexists = B_FALSE;
3433 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3435 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3436 ret = zvol_create_link(zhp->zfs_hdl, path);
3438 (void) zfs_standard_error(hdl, errno,
3439 dgettext(TEXT_DOMAIN,
3440 "Volume successfully snapshotted, but device links "
3441 "were not created"));
3448 (void) zfs_standard_error(hdl, errno, errbuf);
3456 * Destroy any more recent snapshots. We invoke this callback on any dependents
3457 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3458 * is a dependent and we should just destroy it without checking the transaction
3461 typedef struct rollback_data {
3462 const char *cb_target; /* the snapshot */
3463 uint64_t cb_create; /* creation time reference */
3465 boolean_t cb_dependent;
3470 rollback_destroy(zfs_handle_t *zhp, void *data)
3472 rollback_data_t *cbp = data;
3474 if (!cbp->cb_dependent) {
3475 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3476 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3477 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3481 cbp->cb_dependent = B_TRUE;
3482 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3483 rollback_destroy, cbp);
3484 cbp->cb_dependent = B_FALSE;
3486 logstr = zhp->zfs_hdl->libzfs_log_str;
3487 zhp->zfs_hdl->libzfs_log_str = NULL;
3488 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3489 zhp->zfs_hdl->libzfs_log_str = logstr;
3492 /* We must destroy this clone; first unmount it */
3493 prop_changelist_t *clp;
3495 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3496 cbp->cb_force ? MS_FORCE: 0);
3497 if (clp == NULL || changelist_prefix(clp) != 0) {
3498 cbp->cb_error = B_TRUE;
3502 if (zfs_destroy(zhp, B_FALSE) != 0)
3503 cbp->cb_error = B_TRUE;
3505 changelist_remove(clp, zhp->zfs_name);
3506 (void) changelist_postfix(clp);
3507 changelist_free(clp);
3515 * Given a dataset, rollback to a specific snapshot, discarding any
3516 * data changes since then and making it the active dataset.
3518 * Any snapshots more recent than the target are destroyed, along with
3522 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3524 rollback_data_t cb = { 0 };
3526 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3527 boolean_t restore_resv = 0;
3528 uint64_t old_volsize = 0, new_volsize;
3529 zfs_prop_t resv_prop = { 0 };
3531 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3532 zhp->zfs_type == ZFS_TYPE_VOLUME);
3535 * Destroy all recent snapshots and its dependends.
3537 cb.cb_force = force;
3538 cb.cb_target = snap->zfs_name;
3539 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3540 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3546 * Now that we have verified that the snapshot is the latest,
3547 * rollback to the given snapshot.
3550 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3551 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3553 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3555 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3557 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3560 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3562 if (ZFS_IS_VOLUME(zhp))
3563 zc.zc_objset_type = DMU_OST_ZVOL;
3565 zc.zc_objset_type = DMU_OST_ZFS;
3568 * We rely on zfs_iter_children() to verify that there are no
3569 * newer snapshots for the given dataset. Therefore, we can
3570 * simply pass the name on to the ioctl() call. There is still
3571 * an unlikely race condition where the user has taken a
3572 * snapshot since we verified that this was the most recent.
3575 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3576 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3577 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3583 * For volumes, if the pre-rollback volsize matched the pre-
3584 * rollback reservation and the volsize has changed then set
3585 * the reservation property to the post-rollback volsize.
3586 * Make a new handle since the rollback closed the dataset.
3588 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3589 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3590 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3595 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3596 if (old_volsize != new_volsize)
3597 err = zfs_prop_set_int(zhp, resv_prop,
3606 * Iterate over all dependents for a given dataset. This includes both
3607 * hierarchical dependents (children) and data dependents (snapshots and
3608 * clones). The bulk of the processing occurs in get_dependents() in
3612 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3613 zfs_iter_f func, void *data)
3618 zfs_handle_t *child;
3621 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3622 &dependents, &count) != 0)
3625 for (i = 0; i < count; i++) {
3626 if ((child = make_dataset_handle(zhp->zfs_hdl,
3627 dependents[i])) == NULL)
3630 if ((ret = func(child, data)) != 0)
3634 for (i = 0; i < count; i++)
3635 free(dependents[i]);
3642 * Renames the given dataset.
3645 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3648 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3650 prop_changelist_t *cl = NULL;
3651 zfs_handle_t *zhrp = NULL;
3652 char *parentname = NULL;
3653 char parent[ZFS_MAXNAMELEN];
3654 libzfs_handle_t *hdl = zhp->zfs_hdl;
3657 /* if we have the same exact name, just return success */
3658 if (strcmp(zhp->zfs_name, target) == 0)
3661 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3662 "cannot rename to '%s'"), target);
3665 * Make sure the target name is valid
3667 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3668 if ((strchr(target, '@') == NULL) ||
3671 * Snapshot target name is abbreviated,
3672 * reconstruct full dataset name
3674 (void) strlcpy(parent, zhp->zfs_name,
3676 delim = strchr(parent, '@');
3677 if (strchr(target, '@') == NULL)
3681 (void) strlcat(parent, target, sizeof (parent));
3685 * Make sure we're renaming within the same dataset.
3687 delim = strchr(target, '@');
3688 if (strncmp(zhp->zfs_name, target, delim - target)
3689 != 0 || zhp->zfs_name[delim - target] != '@') {
3690 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3691 "snapshots must be part of same "
3693 return (zfs_error(hdl, EZFS_CROSSTARGET,
3697 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3698 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3701 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3702 "recursive rename must be a snapshot"));
3703 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3706 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3707 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3709 /* validate parents */
3710 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3713 /* make sure we're in the same pool */
3714 verify((delim = strchr(target, '/')) != NULL);
3715 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3716 zhp->zfs_name[delim - target] != '/') {
3717 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3718 "datasets must be within same pool"));
3719 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3722 /* new name cannot be a child of the current dataset name */
3723 if (is_descendant(zhp->zfs_name, target)) {
3724 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3725 "New dataset name cannot be a descendant of "
3726 "current dataset name"));
3727 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3731 (void) snprintf(errbuf, sizeof (errbuf),
3732 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3734 if (getzoneid() == GLOBAL_ZONEID &&
3735 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3737 "dataset is used in a non-global zone"));
3738 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3742 struct destroydata dd;
3744 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3745 if (parentname == NULL) {
3749 delim = strchr(parentname, '@');
3751 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3757 dd.snapname = delim + 1;
3758 dd.gotone = B_FALSE;
3759 dd.closezhp = B_TRUE;
3761 /* We remove any zvol links prior to renaming them */
3762 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3767 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3770 if (changelist_haszonedchild(cl)) {
3771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3772 "child dataset with inherited mountpoint is used "
3773 "in a non-global zone"));
3774 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3779 if ((ret = changelist_prefix(cl)) != 0)
3783 if (ZFS_IS_VOLUME(zhp))
3784 zc.zc_objset_type = DMU_OST_ZVOL;
3786 zc.zc_objset_type = DMU_OST_ZFS;
3788 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3789 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3791 zc.zc_cookie = recursive;
3793 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3795 * if it was recursive, the one that actually failed will
3798 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3799 "cannot rename '%s'"), zc.zc_name);
3801 if (recursive && errno == EEXIST) {
3802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3803 "a child dataset already has a snapshot "
3804 "with the new name"));
3805 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3807 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3811 * On failure, we still want to remount any filesystems that
3812 * were previously mounted, so we don't alter the system state.
3815 struct createdata cd;
3817 /* only create links for datasets that had existed */
3818 cd.cd_snapname = delim + 1;
3819 cd.cd_ifexists = B_TRUE;
3820 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3823 (void) changelist_postfix(cl);
3827 struct createdata cd;
3829 /* only create links for datasets that had existed */
3830 cd.cd_snapname = strchr(target, '@') + 1;
3831 cd.cd_ifexists = B_TRUE;
3832 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3835 changelist_rename(cl, zfs_get_name(zhp), target);
3836 ret = changelist_postfix(cl);
3848 changelist_free(cl);
3854 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3855 * and wait briefly for udev to create the /dev link.
3858 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3860 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3864 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3866 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3867 char path[MAXPATHLEN];
3870 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3873 * Issue the appropriate ioctl.
3875 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3879 * Silently ignore the case where the link already
3880 * exists. This allows 'zfs volinit' to be run multiple
3881 * times without errors.
3887 * Dataset does not exist in the kernel. If we
3888 * don't care (see zfs_rename), then ignore the
3898 return (zfs_standard_error_fmt(hdl, errno,
3899 dgettext(TEXT_DOMAIN, "cannot create device links "
3900 "for '%s'"), dataset));
3905 * Wait up to 10 seconds for udev to create the device.
3907 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
3908 error = zpool_label_disk_wait(path, 10000);
3910 (void) printf(gettext("%s may not be immediately "
3911 "available\n"), path);
3917 * Remove a minor node for the given zvol and the associated /dev links.
3920 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3922 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3924 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3926 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3930 * Silently ignore the case where the link no longer
3931 * exists, so that 'zfs volfini' can be run multiple
3932 * times without errors.
3937 return (zfs_standard_error_fmt(hdl, errno,
3938 dgettext(TEXT_DOMAIN, "cannot remove device "
3939 "links for '%s'"), dataset));
3947 zfs_get_user_props(zfs_handle_t *zhp)
3949 return (zhp->zfs_user_props);
3953 * This function is used by 'zfs list' to determine the exact set of columns to
3954 * display, and their maximum widths. This does two main things:
3956 * - If this is a list of all properties, then expand the list to include
3957 * all native properties, and set a flag so that for each dataset we look
3958 * for new unique user properties and add them to the list.
3960 * - For non fixed-width properties, keep track of the maximum width seen
3961 * so that we can size the column appropriately. If the user has
3962 * requested received property values, we also need to compute the width
3963 * of the RECEIVED column.
3966 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3968 libzfs_handle_t *hdl = zhp->zfs_hdl;
3969 zprop_list_t *entry;
3970 zprop_list_t **last, **start;
3971 nvlist_t *userprops, *propval;
3974 char buf[ZFS_MAXPROPLEN];
3976 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3979 userprops = zfs_get_user_props(zhp);
3982 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3984 * Go through and add any user properties as necessary. We
3985 * start by incrementing our list pointer to the first
3986 * non-native property.
3989 while (*start != NULL) {
3990 if ((*start)->pl_prop == ZPROP_INVAL)
3992 start = &(*start)->pl_next;
3996 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3998 * See if we've already found this property in our list.
4000 for (last = start; *last != NULL;
4001 last = &(*last)->pl_next) {
4002 if (strcmp((*last)->pl_user_prop,
4003 nvpair_name(elem)) == 0)
4007 if (*last == NULL) {
4008 if ((entry = zfs_alloc(hdl,
4009 sizeof (zprop_list_t))) == NULL ||
4010 ((entry->pl_user_prop = zfs_strdup(hdl,
4011 nvpair_name(elem)))) == NULL) {
4016 entry->pl_prop = ZPROP_INVAL;
4017 entry->pl_width = strlen(nvpair_name(elem));
4018 entry->pl_all = B_TRUE;
4025 * Now go through and check the width of any non-fixed columns
4027 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4028 if (entry->pl_fixed)
4031 if (entry->pl_prop != ZPROP_INVAL) {
4032 if (zfs_prop_get(zhp, entry->pl_prop,
4033 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4034 if (strlen(buf) > entry->pl_width)
4035 entry->pl_width = strlen(buf);
4037 if (received && zfs_prop_get_recvd(zhp,
4038 zfs_prop_to_name(entry->pl_prop),
4039 buf, sizeof (buf), B_FALSE) == 0)
4040 if (strlen(buf) > entry->pl_recvd_width)
4041 entry->pl_recvd_width = strlen(buf);
4043 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4045 verify(nvlist_lookup_string(propval,
4046 ZPROP_VALUE, &strval) == 0);
4047 if (strlen(strval) > entry->pl_width)
4048 entry->pl_width = strlen(strval);
4050 if (received && zfs_prop_get_recvd(zhp,
4051 entry->pl_user_prop,
4052 buf, sizeof (buf), B_FALSE) == 0)
4053 if (strlen(buf) > entry->pl_recvd_width)
4054 entry->pl_recvd_width = strlen(buf);
4062 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4067 * Keep a reference to the props-table against which we prune the
4070 zhp->zfs_props_table = props;
4072 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4075 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4076 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4079 * User properties will result in ZPROP_INVAL, and since we
4080 * only know how to prune standard ZFS properties, we always
4081 * leave these in the list. This can also happen if we
4082 * encounter an unknown DSL property (when running older
4083 * software, for example).
4085 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4086 (void) nvlist_remove(zhp->zfs_props,
4087 nvpair_name(curr), nvpair_type(curr));
4093 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4094 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4096 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4097 nvlist_t *nvlist = NULL;
4100 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4101 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4102 zc.zc_cookie = (uint64_t)cmd;
4104 if (cmd == ZFS_SMB_ACL_RENAME) {
4105 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4106 (void) no_memory(hdl);
4112 case ZFS_SMB_ACL_ADD:
4113 case ZFS_SMB_ACL_REMOVE:
4114 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4116 case ZFS_SMB_ACL_RENAME:
4117 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4119 (void) no_memory(hdl);
4122 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4124 (void) no_memory(hdl);
4127 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4128 nvlist_free(nvlist);
4132 case ZFS_SMB_ACL_PURGE:
4137 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4139 nvlist_free(nvlist);
4144 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4145 char *path, char *resource)
4147 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4152 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4153 char *path, char *resource)
4155 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4160 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4162 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4167 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4168 char *oldname, char *newname)
4170 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4175 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4176 zfs_userspace_cb_t func, void *arg)
4178 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4180 zfs_useracct_t buf[100];
4182 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4184 zc.zc_objset_type = type;
4185 zc.zc_nvlist_dst = (uintptr_t)buf;
4189 zfs_useracct_t *zua = buf;
4191 zc.zc_nvlist_dst_size = sizeof (buf);
4192 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4193 ZFS_IOC_USERSPACE_MANY, &zc);
4194 if (error || zc.zc_nvlist_dst_size == 0)
4197 while (zc.zc_nvlist_dst_size > 0) {
4198 error = func(arg, zua->zu_domain, zua->zu_rid,
4203 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4211 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4212 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4213 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4215 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4216 libzfs_handle_t *hdl = zhp->zfs_hdl;
4218 ASSERT(!recursive || dsobj == 0);
4220 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4221 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4222 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4223 >= sizeof (zc.zc_string))
4224 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4225 zc.zc_cookie = recursive;
4226 zc.zc_temphold = temphold;
4227 zc.zc_cleanup_fd = cleanup_fd;
4228 zc.zc_sendobj = dsobj;
4229 zc.zc_createtxg = createtxg;
4231 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4232 char errbuf[ZFS_MAXNAMELEN+32];
4235 * if it was recursive, the one that actually failed will be in
4238 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4239 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4243 * Temporary tags wind up having the ds object id
4244 * prepended. So even if we passed the length check
4245 * above, it's still possible for the tag to wind
4246 * up being slightly too long.
4248 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4250 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4251 "pool must be upgraded"));
4252 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4254 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4256 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4262 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4270 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4271 boolean_t recursive)
4273 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4274 libzfs_handle_t *hdl = zhp->zfs_hdl;
4276 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4277 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4278 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4279 >= sizeof (zc.zc_string))
4280 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4281 zc.zc_cookie = recursive;
4283 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4284 char errbuf[ZFS_MAXNAMELEN+32];
4287 * if it was recursive, the one that actually failed will be in
4290 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4291 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4295 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4297 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4298 "pool must be upgraded"));
4299 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4301 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4303 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4311 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4313 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4314 libzfs_handle_t *hdl = zhp->zfs_hdl;
4318 char errbuf[ZFS_MAXNAMELEN+32];
4320 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4321 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4325 nvbuf = malloc(nvsz);
4326 if (nvbuf == NULL) {
4327 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4331 zc.zc_nvlist_dst_size = nvsz;
4332 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4334 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4336 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) {
4337 (void) snprintf(errbuf, sizeof (errbuf),
4338 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4343 nvsz = zc.zc_nvlist_dst_size;
4347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4348 "pool must be upgraded"));
4349 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4352 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4355 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4358 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4363 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4365 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4366 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4368 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4378 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4380 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4381 libzfs_handle_t *hdl = zhp->zfs_hdl;
4383 char errbuf[ZFS_MAXNAMELEN+32];
4387 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4388 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4390 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4393 nvbuf = malloc(nvsz);
4395 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4398 zc.zc_nvlist_src_size = nvsz;
4399 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4400 zc.zc_perm_action = un;
4402 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4404 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4405 (void) snprintf(errbuf, sizeof (errbuf),
4406 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4411 "pool must be upgraded"));
4412 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4415 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4418 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4421 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4432 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4434 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4435 libzfs_handle_t *hdl = zhp->zfs_hdl;
4439 char errbuf[ZFS_MAXNAMELEN+32];
4441 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4445 nvbuf = malloc(nvsz);
4446 if (nvbuf == NULL) {
4447 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4451 zc.zc_nvlist_dst_size = nvsz;
4452 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4454 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4456 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4457 (void) snprintf(errbuf, sizeof (errbuf),
4458 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4463 nvsz = zc.zc_nvlist_dst_size;
4467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4468 "pool must be upgraded"));
4469 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4472 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4475 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4478 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4483 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4485 (void) snprintf(errbuf, sizeof (errbuf),
4486 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4488 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4498 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4501 uint64_t nblocks, volblocksize;
4505 if (nvlist_lookup_string(props,
4506 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4507 ncopies = atoi(strval);
4510 if (nvlist_lookup_uint64(props,
4511 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4512 &volblocksize) != 0)
4513 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4514 nblocks = volsize/volblocksize;
4515 /* start with metadnode L0-L6 */
4517 /* calculate number of indirects */
4518 while (nblocks > 1) {
4519 nblocks += DNODES_PER_LEVEL - 1;
4520 nblocks /= DNODES_PER_LEVEL;
4523 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4526 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4527 * compressed, but in practice they compress down to about
4530 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;