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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
39 #include <sys/mntent.h>
40 #include <sys/mount.h>
53 #include "zfs_namecheck.h"
55 #include "libzfs_impl.h"
56 #include "zfs_deleg.h"
58 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
59 static int userquota_propname_decode(const char *propname, boolean_t zoned,
60 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
63 * Given a single type (not a mask of types), return the type in a human
67 zfs_type_to_name(zfs_type_t type)
70 case ZFS_TYPE_FILESYSTEM:
71 return (dgettext(TEXT_DOMAIN, "filesystem"));
72 case ZFS_TYPE_SNAPSHOT:
73 return (dgettext(TEXT_DOMAIN, "snapshot"));
75 return (dgettext(TEXT_DOMAIN, "volume"));
82 * Given a path and mask of ZFS types, return a string describing this dataset.
83 * This is used when we fail to open a dataset and we cannot get an exact type.
84 * We guess what the type would have been based on the path and the mask of
88 path_to_str(const char *path, int types)
91 * When given a single type, always report the exact type.
93 if (types == ZFS_TYPE_SNAPSHOT)
94 return (dgettext(TEXT_DOMAIN, "snapshot"));
95 if (types == ZFS_TYPE_FILESYSTEM)
96 return (dgettext(TEXT_DOMAIN, "filesystem"));
97 if (types == ZFS_TYPE_VOLUME)
98 return (dgettext(TEXT_DOMAIN, "volume"));
101 * The user is requesting more than one type of dataset. If this is the
102 * case, consult the path itself. If we're looking for a snapshot, and
103 * a '@' is found, then report it as "snapshot". Otherwise, remove the
104 * snapshot attribute and try again.
106 if (types & ZFS_TYPE_SNAPSHOT) {
107 if (strchr(path, '@') != NULL)
108 return (dgettext(TEXT_DOMAIN, "snapshot"));
109 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
113 * The user has requested either filesystems or volumes.
114 * We have no way of knowing a priori what type this would be, so always
115 * report it as "filesystem" or "volume", our two primitive types.
117 if (types & ZFS_TYPE_FILESYSTEM)
118 return (dgettext(TEXT_DOMAIN, "filesystem"));
120 assert(types & ZFS_TYPE_VOLUME);
121 return (dgettext(TEXT_DOMAIN, "volume"));
125 * Validate a ZFS path. This is used even before trying to open the dataset, to
126 * provide a more meaningful error message. We call zfs_error_aux() to
127 * explain exactly why the name was not valid.
130 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
136 if (dataset_namecheck(path, &why, &what) != 0) {
139 case NAME_ERR_TOOLONG:
140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141 "name is too long"));
144 case NAME_ERR_LEADING_SLASH:
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "leading slash in name"));
149 case NAME_ERR_EMPTY_COMPONENT:
150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151 "empty component in name"));
154 case NAME_ERR_TRAILING_SLASH:
155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156 "trailing slash in name"));
159 case NAME_ERR_INVALCHAR:
161 dgettext(TEXT_DOMAIN, "invalid character "
162 "'%c' in name"), what);
165 case NAME_ERR_MULTIPLE_AT:
166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
167 "multiple '@' delimiters in name"));
170 case NAME_ERR_NOLETTER:
171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172 "pool doesn't begin with a letter"));
175 case NAME_ERR_RESERVED:
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "name is reserved"));
180 case NAME_ERR_DISKLIKE:
181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
182 "reserved disk name"));
190 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193 "snapshot delimiter '@' in filesystem name"));
197 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200 "missing '@' delimiter in snapshot name"));
204 if (modifying && strchr(path, '%') != NULL) {
206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
207 "invalid character %c in name"), '%');
215 zfs_name_valid(const char *name, zfs_type_t type)
217 if (type == ZFS_TYPE_POOL)
218 return (zpool_name_valid(NULL, B_FALSE, name));
219 return (zfs_validate_name(NULL, name, type, B_FALSE));
223 * This function takes the raw DSL properties, and filters out the user-defined
224 * properties into a separate nvlist.
227 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
229 libzfs_handle_t *hdl = zhp->zfs_hdl;
234 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
235 (void) no_memory(hdl);
240 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
241 if (!zfs_prop_user(nvpair_name(elem)))
244 verify(nvpair_value_nvlist(elem, &propval) == 0);
245 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
247 (void) no_memory(hdl);
255 static zpool_handle_t *
256 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
258 libzfs_handle_t *hdl = zhp->zfs_hdl;
261 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
262 if (hdl->libzfs_pool_handles != NULL)
263 zph->zpool_next = hdl->libzfs_pool_handles;
264 hdl->libzfs_pool_handles = zph;
269 static zpool_handle_t *
270 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
272 libzfs_handle_t *hdl = zhp->zfs_hdl;
273 zpool_handle_t *zph = hdl->libzfs_pool_handles;
275 while ((zph != NULL) &&
276 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
277 zph = zph->zpool_next;
282 * Returns a handle to the pool that contains the provided dataset.
283 * If a handle to that pool already exists then that handle is returned.
284 * Otherwise, a new handle is created and added to the list of handles.
286 static zpool_handle_t *
287 zpool_handle(zfs_handle_t *zhp)
293 len = strcspn(zhp->zfs_name, "/@") + 1;
294 pool_name = zfs_alloc(zhp->zfs_hdl, len);
295 (void) strlcpy(pool_name, zhp->zfs_name, len);
297 zph = zpool_find_handle(zhp, pool_name, len);
299 zph = zpool_add_handle(zhp, pool_name);
306 zpool_free_handles(libzfs_handle_t *hdl)
308 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
310 while (zph != NULL) {
311 next = zph->zpool_next;
315 hdl->libzfs_pool_handles = NULL;
319 * Utility function to gather stats (objset and zpl) for the given object.
321 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
323 libzfs_handle_t *hdl = zhp->zfs_hdl;
325 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
327 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
328 if (errno == ENOMEM) {
329 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
340 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
342 nvlist_t *allprops, *userprops;
344 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
346 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
351 * XXX Why do we store the user props separately, in addition to
352 * storing them in zfs_props?
354 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
355 nvlist_free(allprops);
359 nvlist_free(zhp->zfs_props);
360 nvlist_free(zhp->zfs_user_props);
362 zhp->zfs_props = allprops;
363 zhp->zfs_user_props = userprops;
369 get_stats(zfs_handle_t *zhp)
372 zfs_cmd_t zc = { 0 };
374 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
376 if (get_stats_ioctl(zhp, &zc) != 0)
378 else if (put_stats_zhdl(zhp, &zc) != 0)
380 zcmd_free_nvlists(&zc);
385 * Refresh the properties currently stored in the handle.
388 zfs_refresh_properties(zfs_handle_t *zhp)
390 (void) get_stats(zhp);
394 * Makes a handle from the given dataset name. Used by zfs_open() and
395 * zfs_iter_* to create child handles on the fly.
398 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
401 libzfs_handle_t *hdl = zhp->zfs_hdl;
404 * Preserve history log string.
405 * any changes performed here will be
406 * logged as an internal event.
408 logstr = zhp->zfs_hdl->libzfs_log_str;
409 zhp->zfs_hdl->libzfs_log_str = NULL;
412 if (put_stats_zhdl(zhp, zc) != 0) {
413 zhp->zfs_hdl->libzfs_log_str = logstr;
418 if (zhp->zfs_dmustats.dds_inconsistent) {
419 zfs_cmd_t zc2 = { 0 };
422 * If it is dds_inconsistent, then we've caught it in
423 * the middle of a 'zfs receive' or 'zfs destroy', and
424 * it is inconsistent from the ZPL's point of view, so
425 * can't be mounted. However, it could also be that we
426 * have crashed in the middle of one of those
427 * operations, in which case we need to get rid of the
428 * inconsistent state. We do that by either rolling
429 * back to the previous snapshot (which will fail if
430 * there is none), or destroying the filesystem. Note
431 * that if we are still in the middle of an active
432 * 'receive' or 'destroy', then the rollback and destroy
433 * will fail with EBUSY and we will drive on as usual.
436 (void) strlcpy(zc2.zc_name, zhp->zfs_name,
437 sizeof (zc2.zc_name));
439 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
440 (void) zvol_remove_link(hdl, zhp->zfs_name);
441 zc2.zc_objset_type = DMU_OST_ZVOL;
443 zc2.zc_objset_type = DMU_OST_ZFS;
447 * If we can successfully destroy it, pretend that it
450 if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) {
451 zhp->zfs_hdl->libzfs_log_str = logstr;
455 /* If we can successfully roll it back, reset the stats */
456 if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) {
457 if (get_stats_ioctl(zhp, zc) != 0) {
458 zhp->zfs_hdl->libzfs_log_str = logstr;
466 * We've managed to open the dataset and gather statistics. Determine
467 * the high-level type.
469 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
470 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
471 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
472 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
476 if (zhp->zfs_dmustats.dds_is_snapshot)
477 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
478 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
479 zhp->zfs_type = ZFS_TYPE_VOLUME;
480 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
481 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
483 abort(); /* we should never see any other types */
485 zhp->zfs_hdl->libzfs_log_str = logstr;
486 zhp->zpool_hdl = zpool_handle(zhp);
491 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
493 zfs_cmd_t zc = { 0 };
495 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
501 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
502 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
506 if (get_stats_ioctl(zhp, &zc) == -1) {
507 zcmd_free_nvlists(&zc);
511 if (make_dataset_handle_common(zhp, &zc) == -1) {
515 zcmd_free_nvlists(&zc);
519 static zfs_handle_t *
520 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
522 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
528 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
529 if (make_dataset_handle_common(zhp, zc) == -1) {
537 * Opens the given snapshot, filesystem, or volume. The 'types'
538 * argument is a mask of acceptable types. The function will print an
539 * appropriate error message and return NULL if it can't be opened.
542 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
547 (void) snprintf(errbuf, sizeof (errbuf),
548 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
551 * Validate the name before we even try to open it.
553 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "invalid dataset name"));
556 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
561 * Try to get stats for the dataset, which will tell us if it exists.
564 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
565 (void) zfs_standard_error(hdl, errno, errbuf);
569 if (!(types & zhp->zfs_type)) {
570 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
579 * Release a ZFS handle. Nothing to do but free the associated memory.
582 zfs_close(zfs_handle_t *zhp)
584 if (zhp->zfs_mntopts)
585 free(zhp->zfs_mntopts);
586 nvlist_free(zhp->zfs_props);
587 nvlist_free(zhp->zfs_user_props);
591 typedef struct mnttab_node {
592 struct mnttab mtn_mt;
597 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
599 const mnttab_node_t *mtn1 = arg1;
600 const mnttab_node_t *mtn2 = arg2;
603 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
607 return (rv > 0 ? 1 : -1);
611 libzfs_mnttab_init(libzfs_handle_t *hdl)
613 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
614 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
615 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
619 libzfs_mnttab_update(libzfs_handle_t *hdl)
623 rewind(hdl->libzfs_mnttab);
624 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
627 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
629 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
630 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
631 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
632 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
633 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
634 avl_add(&hdl->libzfs_mnttab_cache, mtn);
639 libzfs_mnttab_fini(libzfs_handle_t *hdl)
644 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
645 free(mtn->mtn_mt.mnt_special);
646 free(mtn->mtn_mt.mnt_mountp);
647 free(mtn->mtn_mt.mnt_fstype);
648 free(mtn->mtn_mt.mnt_mntopts);
651 avl_destroy(&hdl->libzfs_mnttab_cache);
655 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
657 hdl->libzfs_mnttab_enable = enable;
661 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
662 struct mnttab *entry)
667 if (!hdl->libzfs_mnttab_enable) {
668 struct mnttab srch = { 0 };
670 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
671 libzfs_mnttab_fini(hdl);
672 rewind(hdl->libzfs_mnttab);
673 srch.mnt_special = (char *)fsname;
674 srch.mnt_fstype = MNTTYPE_ZFS;
675 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
681 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
682 libzfs_mnttab_update(hdl);
684 find.mtn_mt.mnt_special = (char *)fsname;
685 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
687 *entry = mtn->mtn_mt;
694 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
695 const char *mountp, const char *mntopts)
699 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
701 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
702 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
703 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
704 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
705 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
706 avl_add(&hdl->libzfs_mnttab_cache, mtn);
710 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
715 find.mtn_mt.mnt_special = (char *)fsname;
716 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
717 avl_remove(&hdl->libzfs_mnttab_cache, ret);
718 free(ret->mtn_mt.mnt_special);
719 free(ret->mtn_mt.mnt_mountp);
720 free(ret->mtn_mt.mnt_fstype);
721 free(ret->mtn_mt.mnt_mntopts);
727 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
729 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
731 if (zpool_handle == NULL)
734 *spa_version = zpool_get_prop_int(zpool_handle,
735 ZPOOL_PROP_VERSION, NULL);
740 * The choice of reservation property depends on the SPA version.
743 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
747 if (zfs_spa_version(zhp, &spa_version) < 0)
750 if (spa_version >= SPA_VERSION_REFRESERVATION)
751 *resv_prop = ZFS_PROP_REFRESERVATION;
753 *resv_prop = ZFS_PROP_RESERVATION;
759 * Given an nvlist of properties to set, validates that they are correct, and
760 * parses any numeric properties (index, boolean, etc) if they are specified as
764 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
765 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
772 int chosen_normal = -1;
775 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
776 (void) no_memory(hdl);
781 * Make sure this property is valid and applies to this type.
785 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
786 const char *propname = nvpair_name(elem);
788 prop = zfs_name_to_prop(propname);
789 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
791 * This is a user property: make sure it's a
792 * string, and that it's less than ZAP_MAXNAMELEN.
794 if (nvpair_type(elem) != DATA_TYPE_STRING) {
795 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
796 "'%s' must be a string"), propname);
797 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
801 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
803 "property name '%s' is too long"),
805 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
809 (void) nvpair_value_string(elem, &strval);
810 if (nvlist_add_string(ret, propname, strval) != 0) {
811 (void) no_memory(hdl);
818 * Currently, only user properties can be modified on
821 if (type == ZFS_TYPE_SNAPSHOT) {
822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
823 "this property can not be modified for snapshots"));
824 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
828 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
829 zfs_userquota_prop_t uqtype;
830 char newpropname[128];
835 if (userquota_propname_decode(propname, zoned,
836 &uqtype, domain, sizeof (domain), &rid) != 0) {
838 dgettext(TEXT_DOMAIN,
839 "'%s' has an invalid user/group name"),
841 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
845 if (uqtype != ZFS_PROP_USERQUOTA &&
846 uqtype != ZFS_PROP_GROUPQUOTA) {
848 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
850 (void) zfs_error(hdl, EZFS_PROPREADONLY,
855 if (nvpair_type(elem) == DATA_TYPE_STRING) {
856 (void) nvpair_value_string(elem, &strval);
857 if (strcmp(strval, "none") == 0) {
859 } else if (zfs_nicestrtonum(hdl,
860 strval, &intval) != 0) {
861 (void) zfs_error(hdl,
862 EZFS_BADPROP, errbuf);
865 } else if (nvpair_type(elem) ==
867 (void) nvpair_value_uint64(elem, &intval);
869 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
870 "use 'none' to disable "
871 "userquota/groupquota"));
875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
876 "'%s' must be a number"), propname);
877 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
881 (void) snprintf(newpropname, sizeof (newpropname),
882 "%s%s", zfs_userquota_prop_prefixes[uqtype],
887 if (nvlist_add_uint64_array(ret, newpropname,
889 (void) no_memory(hdl);
895 if (prop == ZPROP_INVAL) {
896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
897 "invalid property '%s'"), propname);
898 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
902 if (!zfs_prop_valid_for_type(prop, type)) {
904 dgettext(TEXT_DOMAIN, "'%s' does not "
905 "apply to datasets of this type"), propname);
906 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
910 if (zfs_prop_readonly(prop) &&
911 (!zfs_prop_setonce(prop) || zhp != NULL)) {
913 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
915 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
919 if (zprop_parse_value(hdl, elem, prop, type, ret,
920 &strval, &intval, errbuf) != 0)
924 * Perform some additional checks for specific properties.
927 case ZFS_PROP_VERSION:
933 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
934 if (intval < version) {
935 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
936 "Can not downgrade; already at version %u"),
938 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
944 case ZFS_PROP_RECORDSIZE:
945 case ZFS_PROP_VOLBLOCKSIZE:
946 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
947 if (intval < SPA_MINBLOCKSIZE ||
948 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
949 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
950 "'%s' must be power of 2 from %u "
952 (uint_t)SPA_MINBLOCKSIZE,
953 (uint_t)SPA_MAXBLOCKSIZE >> 10);
954 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
959 case ZFS_PROP_SHAREISCSI:
960 if (strcmp(strval, "off") != 0 &&
961 strcmp(strval, "on") != 0 &&
962 strcmp(strval, "type=disk") != 0) {
963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964 "'%s' must be 'on', 'off', or 'type=disk'"),
966 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
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"),
995 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1002 case ZFS_PROP_SHARESMB:
1003 case ZFS_PROP_SHARENFS:
1005 * For the mountpoint and sharenfs or sharesmb
1006 * properties, check if it can be set in a
1007 * global/non-global zone based on
1008 * the zoned property value:
1010 * global zone non-global zone
1011 * --------------------------------------------------
1012 * zoned=on mountpoint (no) mountpoint (yes)
1013 * sharenfs (no) sharenfs (no)
1014 * sharesmb (no) sharesmb (no)
1016 * zoned=off mountpoint (yes) N/A
1021 if (getzoneid() == GLOBAL_ZONEID) {
1022 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1023 "'%s' cannot be set on "
1024 "dataset in a non-global zone"),
1026 (void) zfs_error(hdl, EZFS_ZONED,
1029 } else if (prop == ZFS_PROP_SHARENFS ||
1030 prop == ZFS_PROP_SHARESMB) {
1031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1032 "'%s' cannot be set in "
1033 "a non-global zone"), propname);
1034 (void) zfs_error(hdl, EZFS_ZONED,
1038 } else if (getzoneid() != GLOBAL_ZONEID) {
1040 * If zoned property is 'off', this must be in
1041 * a global zone. If not, something is wrong.
1043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1044 "'%s' cannot be set while dataset "
1045 "'zoned' property is set"), propname);
1046 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1051 * At this point, it is legitimate to set the
1052 * property. Now we want to make sure that the
1053 * property value is valid if it is sharenfs.
1055 if ((prop == ZFS_PROP_SHARENFS ||
1056 prop == ZFS_PROP_SHARESMB) &&
1057 strcmp(strval, "on") != 0 &&
1058 strcmp(strval, "off") != 0) {
1059 zfs_share_proto_t proto;
1061 if (prop == ZFS_PROP_SHARESMB)
1067 * Must be an valid sharing protocol
1068 * option string so init the libshare
1069 * in order to enable the parser and
1070 * then parse the options. We use the
1071 * control API since we don't care about
1072 * the current configuration and don't
1073 * want the overhead of loading it
1074 * until we actually do something.
1077 if (zfs_init_libshare(hdl,
1078 SA_INIT_CONTROL_API) != SA_OK) {
1080 * An error occurred so we can't do
1083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1084 "'%s' cannot be set: problem "
1085 "in share initialization"),
1087 (void) zfs_error(hdl, EZFS_BADPROP,
1092 if (zfs_parse_options(strval, proto) != SA_OK) {
1094 * There was an error in parsing so
1095 * deal with it by issuing an error
1096 * message and leaving after
1097 * uninitializing the the libshare
1100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1101 "'%s' cannot be set to invalid "
1102 "options"), propname);
1103 (void) zfs_error(hdl, EZFS_BADPROP,
1105 zfs_uninit_libshare(hdl);
1108 zfs_uninit_libshare(hdl);
1112 case ZFS_PROP_UTF8ONLY:
1113 chosen_utf = (int)intval;
1115 case ZFS_PROP_NORMALIZE:
1116 chosen_normal = (int)intval;
1121 * For changes to existing volumes, we have some additional
1122 * checks to enforce.
1124 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1125 uint64_t volsize = zfs_prop_get_int(zhp,
1127 uint64_t blocksize = zfs_prop_get_int(zhp,
1128 ZFS_PROP_VOLBLOCKSIZE);
1132 case ZFS_PROP_RESERVATION:
1133 case ZFS_PROP_REFRESERVATION:
1134 if (intval > volsize) {
1135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136 "'%s' is greater than current "
1137 "volume size"), propname);
1138 (void) zfs_error(hdl, EZFS_BADPROP,
1144 case ZFS_PROP_VOLSIZE:
1145 if (intval % blocksize != 0) {
1146 zfs_nicenum(blocksize, buf,
1148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 "'%s' must be a multiple of "
1150 "volume block size (%s)"),
1152 (void) zfs_error(hdl, EZFS_BADPROP,
1158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1159 "'%s' cannot be zero"),
1161 (void) zfs_error(hdl, EZFS_BADPROP,
1171 * If normalization was chosen, but no UTF8 choice was made,
1172 * enforce rejection of non-UTF8 names.
1174 * If normalization was chosen, but rejecting non-UTF8 names
1175 * was explicitly not chosen, it is an error.
1177 if (chosen_normal > 0 && chosen_utf < 0) {
1178 if (nvlist_add_uint64(ret,
1179 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1180 (void) no_memory(hdl);
1183 } else if (chosen_normal > 0 && chosen_utf == 0) {
1184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1185 "'%s' must be set 'on' if normalization chosen"),
1186 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1187 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1192 * If this is an existing volume, and someone is setting the volsize,
1193 * make sure that it matches the reservation, or add it if necessary.
1195 if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
1196 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1198 uint64_t old_volsize = zfs_prop_get_int(zhp,
1200 uint64_t old_reservation;
1201 uint64_t new_reservation;
1202 zfs_prop_t resv_prop;
1204 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1206 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1208 if (old_volsize == old_reservation &&
1209 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
1210 &new_reservation) != 0) {
1211 if (nvlist_add_uint64(ret,
1212 zfs_prop_to_name(resv_prop), intval) != 0) {
1213 (void) no_memory(hdl);
1226 * Given a property name and value, set the property for the given dataset.
1229 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1231 zfs_cmd_t zc = { 0 };
1233 prop_changelist_t *cl = NULL;
1235 libzfs_handle_t *hdl = zhp->zfs_hdl;
1236 nvlist_t *nvl = NULL, *realprops;
1238 boolean_t do_prefix;
1241 (void) snprintf(errbuf, sizeof (errbuf),
1242 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1245 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1246 nvlist_add_string(nvl, propname, propval) != 0) {
1247 (void) no_memory(hdl);
1251 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1252 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1258 prop = zfs_name_to_prop(propname);
1260 /* We don't support those properties on FreeBSD. */
1262 case ZFS_PROP_DEVICES:
1263 case ZFS_PROP_SHAREISCSI:
1264 case ZFS_PROP_ISCSIOPTIONS:
1265 case ZFS_PROP_XATTR:
1266 case ZFS_PROP_VSCAN:
1267 case ZFS_PROP_NBMAND:
1268 case ZFS_PROP_SHARESMB:
1269 (void) snprintf(errbuf, sizeof (errbuf),
1270 "property '%s' not supported on FreeBSD", propname);
1271 ret = zfs_error(hdl, EZFS_PERM, errbuf);
1275 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1278 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1279 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1280 "child dataset with inherited mountpoint is used "
1281 "in a non-global zone"));
1282 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1287 * If the dataset's canmount property is being set to noauto,
1288 * then we want to prevent unmounting & remounting it.
1290 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1291 (zprop_string_to_index(prop, propval, &idx,
1292 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1294 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1298 * Execute the corresponding ioctl() to set this property.
1300 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1302 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1305 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1312 * For quotas and reservations, ENOSPC indicates
1313 * something different; setting a quota or reservation
1314 * doesn't use any disk space.
1317 case ZFS_PROP_QUOTA:
1318 case ZFS_PROP_REFQUOTA:
1319 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1320 "size is less than current used or "
1322 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1325 case ZFS_PROP_RESERVATION:
1326 case ZFS_PROP_REFRESERVATION:
1327 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1328 "size is greater than available space"));
1329 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1333 (void) zfs_standard_error(hdl, errno, errbuf);
1339 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1340 (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
1342 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1346 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1350 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1351 "pool and or dataset must be upgraded to set this "
1352 "property or value"));
1353 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1357 if (prop == ZFS_PROP_COMPRESSION) {
1358 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1359 "property setting is not allowed on "
1360 "bootable datasets"));
1361 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1363 (void) zfs_standard_error(hdl, errno, errbuf);
1369 * This platform can't address a volume this big.
1372 if (prop == ZFS_PROP_VOLSIZE) {
1373 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1379 (void) zfs_standard_error(hdl, errno, errbuf);
1383 ret = changelist_postfix(cl);
1386 * Refresh the statistics so the new property value
1390 (void) get_stats(zhp);
1395 zcmd_free_nvlists(&zc);
1397 changelist_free(cl);
1402 * Given a property, inherit the value from the parent dataset.
1405 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1407 zfs_cmd_t zc = { 0 };
1409 prop_changelist_t *cl;
1410 libzfs_handle_t *hdl = zhp->zfs_hdl;
1414 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1415 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1417 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1419 * For user properties, the amount of work we have to do is very
1420 * small, so just do it here.
1422 if (!zfs_prop_user(propname)) {
1423 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1424 "invalid property"));
1425 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1428 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1429 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1431 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1432 return (zfs_standard_error(hdl, errno, errbuf));
1438 * Verify that this property is inheritable.
1440 if (zfs_prop_readonly(prop))
1441 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1443 if (!zfs_prop_inheritable(prop))
1444 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1447 * Check to see if the value applies to this type
1449 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1450 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1453 * Normalize the name, to get rid of shorthand abbrevations.
1455 propname = zfs_prop_to_name(prop);
1456 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1457 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1459 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1460 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1462 "dataset is used in a non-global zone"));
1463 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1467 * Determine datasets which will be affected by this change, if any.
1469 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1472 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1474 "child dataset with inherited mountpoint is used "
1475 "in a non-global zone"));
1476 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1480 if ((ret = changelist_prefix(cl)) != 0)
1483 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1484 return (zfs_standard_error(hdl, errno, errbuf));
1487 if ((ret = changelist_postfix(cl)) != 0)
1491 * Refresh the statistics so the new property is reflected.
1493 (void) get_stats(zhp);
1497 changelist_free(cl);
1502 * True DSL properties are stored in an nvlist. The following two functions
1503 * extract them appropriately.
1506 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1512 if (nvlist_lookup_nvlist(zhp->zfs_props,
1513 zfs_prop_to_name(prop), &nv) == 0) {
1514 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1515 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1517 verify(!zhp->zfs_props_table ||
1518 zhp->zfs_props_table[prop] == B_TRUE);
1519 value = zfs_prop_default_numeric(prop);
1527 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1533 if (nvlist_lookup_nvlist(zhp->zfs_props,
1534 zfs_prop_to_name(prop), &nv) == 0) {
1535 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1536 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1538 verify(!zhp->zfs_props_table ||
1539 zhp->zfs_props_table[prop] == B_TRUE);
1540 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1549 * Internal function for getting a numeric property. Both zfs_prop_get() and
1550 * zfs_prop_get_int() are built using this interface.
1552 * Certain properties can be overridden using 'mount -o'. In this case, scan
1553 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1554 * If they differ from the on-disk values, report the current values and mark
1555 * the source "temporary".
1558 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1559 char **source, uint64_t *val)
1561 zfs_cmd_t zc = { 0 };
1562 nvlist_t *zplprops = NULL;
1564 char *mntopt_on = NULL;
1565 char *mntopt_off = NULL;
1570 case ZFS_PROP_ATIME:
1571 mntopt_on = MNTOPT_ATIME;
1572 mntopt_off = MNTOPT_NOATIME;
1575 case ZFS_PROP_DEVICES:
1576 mntopt_on = MNTOPT_DEVICES;
1577 mntopt_off = MNTOPT_NODEVICES;
1581 mntopt_on = MNTOPT_EXEC;
1582 mntopt_off = MNTOPT_NOEXEC;
1585 case ZFS_PROP_READONLY:
1586 mntopt_on = MNTOPT_RO;
1587 mntopt_off = MNTOPT_RW;
1590 case ZFS_PROP_SETUID:
1591 mntopt_on = MNTOPT_SETUID;
1592 mntopt_off = MNTOPT_NOSETUID;
1595 case ZFS_PROP_XATTR:
1596 mntopt_on = MNTOPT_XATTR;
1597 mntopt_off = MNTOPT_NOXATTR;
1600 case ZFS_PROP_NBMAND:
1601 mntopt_on = MNTOPT_NBMAND;
1602 mntopt_off = MNTOPT_NONBMAND;
1607 * Because looking up the mount options is potentially expensive
1608 * (iterating over all of /etc/mnttab), we defer its calculation until
1609 * we're looking up a property which requires its presence.
1611 if (!zhp->zfs_mntcheck &&
1612 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1613 libzfs_handle_t *hdl = zhp->zfs_hdl;
1614 struct mnttab entry;
1616 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1617 zhp->zfs_mntopts = zfs_strdup(hdl,
1619 if (zhp->zfs_mntopts == NULL)
1623 zhp->zfs_mntcheck = B_TRUE;
1626 if (zhp->zfs_mntopts == NULL)
1627 mnt.mnt_mntopts = "";
1629 mnt.mnt_mntopts = zhp->zfs_mntopts;
1632 case ZFS_PROP_ATIME:
1633 case ZFS_PROP_DEVICES:
1635 case ZFS_PROP_READONLY:
1636 case ZFS_PROP_SETUID:
1637 case ZFS_PROP_XATTR:
1638 case ZFS_PROP_NBMAND:
1639 *val = getprop_uint64(zhp, prop, source);
1641 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1644 *src = ZPROP_SRC_TEMPORARY;
1645 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1648 *src = ZPROP_SRC_TEMPORARY;
1652 case ZFS_PROP_CANMOUNT:
1653 *val = getprop_uint64(zhp, prop, source);
1654 if (*val != ZFS_CANMOUNT_ON)
1655 *source = zhp->zfs_name;
1657 *source = ""; /* default */
1660 case ZFS_PROP_QUOTA:
1661 case ZFS_PROP_REFQUOTA:
1662 case ZFS_PROP_RESERVATION:
1663 case ZFS_PROP_REFRESERVATION:
1664 *val = getprop_uint64(zhp, prop, source);
1666 *source = ""; /* default */
1668 *source = zhp->zfs_name;
1671 case ZFS_PROP_MOUNTED:
1672 *val = (zhp->zfs_mntopts != NULL);
1675 case ZFS_PROP_NUMCLONES:
1676 *val = zhp->zfs_dmustats.dds_num_clones;
1679 case ZFS_PROP_VERSION:
1680 case ZFS_PROP_NORMALIZE:
1681 case ZFS_PROP_UTF8ONLY:
1683 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1684 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1686 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1687 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1688 zcmd_free_nvlists(&zc);
1689 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1690 "unable to get %s property"),
1691 zfs_prop_to_name(prop));
1692 return (zfs_error(zhp->zfs_hdl, EZFS_BADVERSION,
1693 dgettext(TEXT_DOMAIN, "internal error")));
1695 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1696 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1698 zcmd_free_nvlists(&zc);
1699 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1700 "unable to get %s property"),
1701 zfs_prop_to_name(prop));
1702 return (zfs_error(zhp->zfs_hdl, EZFS_NOMEM,
1703 dgettext(TEXT_DOMAIN, "internal error")));
1706 nvlist_free(zplprops);
1707 zcmd_free_nvlists(&zc);
1711 switch (zfs_prop_get_type(prop)) {
1712 case PROP_TYPE_NUMBER:
1713 case PROP_TYPE_INDEX:
1714 *val = getprop_uint64(zhp, prop, source);
1716 * If we tried to use a default value for a
1717 * readonly property, it means that it was not
1718 * present; return an error.
1720 if (zfs_prop_readonly(prop) &&
1721 *source && (*source)[0] == '\0') {
1726 case PROP_TYPE_STRING:
1728 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1729 "cannot get non-numeric property"));
1730 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1731 dgettext(TEXT_DOMAIN, "internal error")));
1739 * Calculate the source type, given the raw source string.
1742 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1743 char *statbuf, size_t statlen)
1745 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1748 if (source == NULL) {
1749 *srctype = ZPROP_SRC_NONE;
1750 } else if (source[0] == '\0') {
1751 *srctype = ZPROP_SRC_DEFAULT;
1753 if (strcmp(source, zhp->zfs_name) == 0) {
1754 *srctype = ZPROP_SRC_LOCAL;
1756 (void) strlcpy(statbuf, source, statlen);
1757 *srctype = ZPROP_SRC_INHERITED;
1764 * Retrieve a property from the given object. If 'literal' is specified, then
1765 * numbers are left as exact values. Otherwise, numbers are converted to a
1766 * human-readable form.
1768 * Returns 0 on success, or -1 on error.
1771 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1772 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1774 char *source = NULL;
1780 * Check to see if this property applies to our object
1782 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1786 *src = ZPROP_SRC_NONE;
1789 case ZFS_PROP_CREATION:
1791 * 'creation' is a time_t stored in the statistics. We convert
1792 * this into a string unless 'literal' is specified.
1795 val = getprop_uint64(zhp, prop, &source);
1796 time_t time = (time_t)val;
1800 localtime_r(&time, &t) == NULL ||
1801 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1803 (void) snprintf(propbuf, proplen, "%llu", val);
1807 case ZFS_PROP_MOUNTPOINT:
1809 * Getting the precise mountpoint can be tricky.
1811 * - for 'none' or 'legacy', return those values.
1812 * - for inherited mountpoints, we want to take everything
1813 * after our ancestor and append it to the inherited value.
1815 * If the pool has an alternate root, we want to prepend that
1816 * root to any values we return.
1819 str = getprop_string(zhp, prop, &source);
1821 if (str[0] == '/') {
1822 char buf[MAXPATHLEN];
1824 const char *relpath = zhp->zfs_name + strlen(source);
1826 if (relpath[0] == '/')
1829 if ((zpool_get_prop(zhp->zpool_hdl,
1830 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1831 (strcmp(root, "-") == 0))
1834 * Special case an alternate root of '/'. This will
1835 * avoid having multiple leading slashes in the
1838 if (strcmp(root, "/") == 0)
1842 * If the mountpoint is '/' then skip over this
1843 * if we are obtaining either an alternate root or
1844 * an inherited mountpoint.
1846 if (str[1] == '\0' && (root[0] != '\0' ||
1847 relpath[0] != '\0'))
1850 if (relpath[0] == '\0')
1851 (void) snprintf(propbuf, proplen, "%s%s",
1854 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1855 root, str, relpath[0] == '@' ? "" : "/",
1858 /* 'legacy' or 'none' */
1859 (void) strlcpy(propbuf, str, proplen);
1864 case ZFS_PROP_ORIGIN:
1865 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1868 * If there is no parent at all, return failure to indicate that
1869 * it doesn't apply to this dataset.
1871 if (propbuf[0] == '\0')
1875 case ZFS_PROP_QUOTA:
1876 case ZFS_PROP_REFQUOTA:
1877 case ZFS_PROP_RESERVATION:
1878 case ZFS_PROP_REFRESERVATION:
1880 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1884 * If quota or reservation is 0, we translate this into 'none'
1885 * (unless literal is set), and indicate that it's the default
1886 * value. Otherwise, we print the number nicely and indicate
1887 * that its set locally.
1891 (void) strlcpy(propbuf, "0", proplen);
1893 (void) strlcpy(propbuf, "none", proplen);
1896 (void) snprintf(propbuf, proplen, "%llu",
1899 zfs_nicenum(val, propbuf, proplen);
1903 case ZFS_PROP_COMPRESSRATIO:
1904 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1906 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
1907 val / 100, (longlong_t)val % 100);
1911 switch (zhp->zfs_type) {
1912 case ZFS_TYPE_FILESYSTEM:
1915 case ZFS_TYPE_VOLUME:
1918 case ZFS_TYPE_SNAPSHOT:
1924 (void) snprintf(propbuf, proplen, "%s", str);
1927 case ZFS_PROP_MOUNTED:
1929 * The 'mounted' property is a pseudo-property that described
1930 * whether the filesystem is currently mounted. Even though
1931 * it's a boolean value, the typical values of "on" and "off"
1932 * don't make sense, so we translate to "yes" and "no".
1934 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
1935 src, &source, &val) != 0)
1938 (void) strlcpy(propbuf, "yes", proplen);
1940 (void) strlcpy(propbuf, "no", proplen);
1945 * The 'name' property is a pseudo-property derived from the
1946 * dataset name. It is presented as a real property to simplify
1949 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
1953 switch (zfs_prop_get_type(prop)) {
1954 case PROP_TYPE_NUMBER:
1955 if (get_numeric_property(zhp, prop, src,
1956 &source, &val) != 0)
1959 (void) snprintf(propbuf, proplen, "%llu",
1962 zfs_nicenum(val, propbuf, proplen);
1965 case PROP_TYPE_STRING:
1966 (void) strlcpy(propbuf,
1967 getprop_string(zhp, prop, &source), proplen);
1970 case PROP_TYPE_INDEX:
1971 if (get_numeric_property(zhp, prop, src,
1972 &source, &val) != 0)
1974 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
1976 (void) strlcpy(propbuf, strval, proplen);
1984 get_source(zhp, src, source, statbuf, statlen);
1990 * Utility function to get the given numeric property. Does no validation that
1991 * the given property is the appropriate type; should only be used with
1992 * hard-coded property types.
1995 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2000 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2006 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2010 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2011 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2015 * Similar to zfs_prop_get(), but returns the value as an integer.
2018 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2019 zprop_source_t *src, char *statbuf, size_t statlen)
2024 * Check to see if this property applies to our object
2026 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2027 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2028 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2029 zfs_prop_to_name(prop)));
2033 *src = ZPROP_SRC_NONE;
2035 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2038 get_source(zhp, src, source, statbuf, statlen);
2044 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2045 char **domainp, idmap_rid_t *ridp)
2048 idmap_handle_t *idmap_hdl = NULL;
2049 idmap_get_handle_t *get_hdl = NULL;
2053 if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
2055 if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
2059 err = idmap_get_sidbyuid(get_hdl, id,
2060 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2062 err = idmap_get_sidbygid(get_hdl, id,
2063 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2065 if (err == IDMAP_SUCCESS &&
2066 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2067 status == IDMAP_SUCCESS)
2073 idmap_get_destroy(get_hdl);
2075 (void) idmap_fini(idmap_hdl);
2078 assert(!"invalid code path");
2083 /* Check if a string contains only digits */
2085 string_is_digits(char *cp)
2089 for(i = 0; i < strlen(cp); i++)
2098 * convert the propname into parameters needed by kernel
2099 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2100 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2103 userquota_propname_decode(const char *propname, boolean_t zoned,
2104 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2106 zfs_userquota_prop_t type;
2108 char *numericsid = NULL;
2113 /* Figure out the property type ({user|group}{quota|space}) */
2114 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2115 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2116 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2119 if (type == ZFS_NUM_USERQUOTA_PROPS)
2123 isuser = (type == ZFS_PROP_USERQUOTA ||
2124 type == ZFS_PROP_USERUSED);
2126 cp = strchr(propname, '@') + 1;
2128 if (strchr(cp, '@')) {
2131 * It's a SID name (eg "user@domain") that needs to be
2132 * turned into S-1-domainID-RID.
2134 directory_error_t e;
2136 if (zoned && getzoneid() == GLOBAL_ZONEID)
2139 e = directory_sid_from_user_name(NULL,
2142 e = directory_sid_from_group_name(NULL,
2146 directory_error_free(e);
2149 if (numericsid == NULL)
2152 /* will be further decoded below */
2158 if (strncmp(cp, "S-1-", 4) == 0) {
2159 /* It's a numeric SID (eg "S-1-234-567-89") */
2160 (void) strlcpy(domain, cp, domainlen);
2161 cp = strrchr(domain, '-');
2166 *ridp = strtoull(cp, &end, 10);
2171 if (errno != 0 || *end != '\0')
2174 } else if (!isdigit(*cp)) {
2177 * In FreeBSD user and group names can begin with a digit so treat
2178 * as a uid/gid if string contains digits only
2180 } else if (!string_is_digits(cp)) {
2183 * It's a user/group name (eg "user") that needs to be
2184 * turned into a uid/gid
2186 if (zoned && getzoneid() == GLOBAL_ZONEID)
2202 /* It's a user/group ID (eg "12345"). */
2203 uid_t id = strtoul(cp, &end, 10);
2210 /* It's an ephemeral ID. */
2211 if (idmap_id_to_numeric_domain_rid(id, isuser,
2212 &mapdomain, &rid) != 0)
2214 (void) strlcpy(domain, mapdomain, domainlen);
2221 ASSERT3P(numericsid, ==, NULL);
2226 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2227 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2230 zfs_cmd_t zc = { 0 };
2232 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2234 err = userquota_propname_decode(propname,
2235 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2236 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2237 zc.zc_objset_type = *typep;
2241 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2245 *propvalue = zc.zc_cookie;
2250 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2251 uint64_t *propvalue)
2253 zfs_userquota_prop_t type;
2255 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2260 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2261 char *propbuf, int proplen, boolean_t literal)
2265 zfs_userquota_prop_t type;
2267 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2274 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2275 } else if (propvalue == 0 &&
2276 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2277 (void) strlcpy(propbuf, "none", proplen);
2279 zfs_nicenum(propvalue, propbuf, proplen);
2285 * Returns the name of the given zfs handle.
2288 zfs_get_name(const zfs_handle_t *zhp)
2290 return (zhp->zfs_name);
2294 * Returns the type of the given zfs handle.
2297 zfs_get_type(const zfs_handle_t *zhp)
2299 return (zhp->zfs_type);
2303 zfs_do_list_ioctl(zfs_handle_t *zhp, unsigned long arg, zfs_cmd_t *zc)
2306 uint64_t orig_cookie;
2308 orig_cookie = zc->zc_cookie;
2310 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2311 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2314 * FreeBSD compatibility with pre-v15 kernel module.
2315 * Ignore private dataset names.
2317 if (strchr(zc->zc_name, '$') != NULL)
2323 /* expand nvlist memory and try again */
2324 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2325 zcmd_free_nvlists(zc);
2328 zc->zc_cookie = orig_cookie;
2331 * An errno value of ESRCH indicates normal completion.
2332 * If ENOENT is returned, then the underlying dataset
2333 * has been removed since we obtained the handle.
2340 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2341 dgettext(TEXT_DOMAIN,
2342 "cannot iterate filesystems"));
2350 * Iterate over all child filesystems
2353 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2355 zfs_cmd_t zc = { 0 };
2359 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2362 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2365 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2369 * FreeBSD compatibility with pre-v15 kernel module.
2370 * Ignore private dataset names.
2372 if (strchr(zc.zc_name, '$') != NULL)
2376 * Silently ignore errors, as the only plausible explanation is
2377 * that the pool has since been removed.
2379 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2384 if ((ret = func(nzhp, data)) != 0) {
2385 zcmd_free_nvlists(&zc);
2389 zcmd_free_nvlists(&zc);
2390 return ((ret < 0) ? ret : 0);
2394 * Iterate over all snapshots
2397 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2399 zfs_cmd_t zc = { 0 };
2403 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2406 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2408 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2412 * FreeBSD compatibility with pre-v15 kernel module.
2413 * Ignore private dataset names.
2415 if (strchr(zc.zc_name, '$') != NULL)
2418 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2423 if ((ret = func(nzhp, data)) != 0) {
2424 zcmd_free_nvlists(&zc);
2428 zcmd_free_nvlists(&zc);
2429 return ((ret < 0) ? ret : 0);
2433 * Iterate over all children, snapshots and filesystems
2436 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2440 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2443 return (zfs_iter_snapshots(zhp, func, data));
2447 * Given a complete name, return just the portion that refers to the parent.
2448 * Can return NULL if this is a pool.
2451 parent_name(const char *path, char *buf, size_t buflen)
2455 if ((loc = strrchr(path, '/')) == NULL)
2458 (void) strncpy(buf, path, MIN(buflen, loc - path));
2459 buf[loc - path] = '\0';
2465 * If accept_ancestor is false, then check to make sure that the given path has
2466 * a parent, and that it exists. If accept_ancestor is true, then find the
2467 * closest existing ancestor for the given path. In prefixlen return the
2468 * length of already existing prefix of the given path. We also fetch the
2469 * 'zoned' property, which is used to validate property settings when creating
2473 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2474 boolean_t accept_ancestor, int *prefixlen)
2476 zfs_cmd_t zc = { 0 };
2477 char parent[ZFS_MAXNAMELEN];
2482 (void) snprintf(errbuf, sizeof (errbuf),
2483 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2485 /* get parent, and check to see if this is just a pool */
2486 if (parent_name(path, parent, sizeof (parent)) != 0) {
2487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2488 "missing dataset name"));
2489 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2492 /* check to see if the pool exists */
2493 if ((slash = strchr(parent, '/')) == NULL)
2494 slash = parent + strlen(parent);
2495 (void) strncpy(zc.zc_name, parent, slash - parent);
2496 zc.zc_name[slash - parent] = '\0';
2497 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2500 "no such pool '%s'"), zc.zc_name);
2501 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2504 /* check to see if the parent dataset exists */
2505 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2506 if (errno == ENOENT && accept_ancestor) {
2508 * Go deeper to find an ancestor, give up on top level.
2510 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2512 "no such pool '%s'"), zc.zc_name);
2513 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2515 } else if (errno == ENOENT) {
2516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2517 "parent does not exist"));
2518 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2520 return (zfs_standard_error(hdl, errno, errbuf));
2523 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2524 /* we are in a non-global zone, but parent is in the global zone */
2525 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
2526 (void) zfs_standard_error(hdl, EPERM, errbuf);
2531 /* make sure parent is a filesystem */
2532 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2534 "parent is not a filesystem"));
2535 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2541 if (prefixlen != NULL)
2542 *prefixlen = strlen(parent);
2547 * Finds whether the dataset of the given type(s) exists.
2550 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2554 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2558 * Try to get stats for the dataset, which will tell us if it exists.
2560 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2561 int ds_type = zhp->zfs_type;
2564 if (types & ds_type)
2571 * Given a path to 'target', create all the ancestors between
2572 * the prefixlen portion of the path, and the target itself.
2573 * Fail if the initial prefixlen-ancestor does not already exist.
2576 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2582 /* make sure prefix exists */
2583 cp = target + prefixlen;
2585 assert(strchr(cp, '/') == NULL);
2586 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2589 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2597 * Attempt to create, mount, and share any ancestor filesystems,
2598 * up to the prefixlen-long one.
2600 for (cp = target + prefixlen + 1;
2601 cp = strchr(cp, '/'); *cp = '/', cp++) {
2606 h = make_dataset_handle(hdl, target);
2608 /* it already exists, nothing to do here */
2613 logstr = hdl->libzfs_log_str;
2614 hdl->libzfs_log_str = NULL;
2615 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2617 hdl->libzfs_log_str = logstr;
2618 opname = dgettext(TEXT_DOMAIN, "create");
2622 hdl->libzfs_log_str = logstr;
2623 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2625 opname = dgettext(TEXT_DOMAIN, "open");
2629 if (zfs_mount(h, NULL, 0) != 0) {
2630 opname = dgettext(TEXT_DOMAIN, "mount");
2634 if (zfs_share(h) != 0) {
2635 opname = dgettext(TEXT_DOMAIN, "share");
2645 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2646 "failed to %s ancestor '%s'"), opname, target);
2651 * Creates non-existing ancestors of the given path.
2654 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2661 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
2664 if ((path_copy = strdup(path)) != NULL) {
2665 rc = create_parents(hdl, path_copy, prefix);
2668 if (path_copy == NULL || rc != 0)
2675 * Create a new filesystem or volume.
2678 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2681 zfs_cmd_t zc = { 0 };
2684 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2688 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2689 "cannot create '%s'"), path);
2691 /* validate the path, taking care to note the extended error message */
2692 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2693 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2695 /* validate parents exist */
2696 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2700 * The failure modes when creating a dataset of a different type over
2701 * one that already exists is a little strange. In particular, if you
2702 * try to create a dataset on top of an existing dataset, the ioctl()
2703 * will return ENOENT, not EEXIST. To prevent this from happening, we
2704 * first try to see if the dataset exists.
2706 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2707 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2709 "dataset already exists"));
2710 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2713 if (type == ZFS_TYPE_VOLUME)
2714 zc.zc_objset_type = DMU_OST_ZVOL;
2716 zc.zc_objset_type = DMU_OST_ZFS;
2718 if (props && (props = zfs_valid_proplist(hdl, type, props,
2719 zoned, NULL, errbuf)) == 0)
2722 if (type == ZFS_TYPE_VOLUME) {
2724 * If we are creating a volume, the size and block size must
2725 * satisfy a few restraints. First, the blocksize must be a
2726 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2727 * volsize must be a multiple of the block size, and cannot be
2730 if (props == NULL || nvlist_lookup_uint64(props,
2731 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2734 "missing volume size"));
2735 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2738 if ((ret = nvlist_lookup_uint64(props,
2739 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2740 &blocksize)) != 0) {
2741 if (ret == ENOENT) {
2742 blocksize = zfs_prop_default_numeric(
2743 ZFS_PROP_VOLBLOCKSIZE);
2746 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2747 "missing volume block size"));
2748 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2755 "volume size cannot be zero"));
2756 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2759 if (size % blocksize != 0) {
2761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2762 "volume size must be a multiple of volume block "
2764 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2768 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2772 /* create the dataset */
2773 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2775 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2776 ret = zvol_create_link(hdl, path);
2778 (void) zfs_standard_error(hdl, errno,
2779 dgettext(TEXT_DOMAIN,
2780 "Volume successfully created, but device links "
2781 "were not created"));
2782 zcmd_free_nvlists(&zc);
2787 zcmd_free_nvlists(&zc);
2789 /* check for failure */
2791 char parent[ZFS_MAXNAMELEN];
2792 (void) parent_name(path, parent, sizeof (parent));
2796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2797 "no such parent '%s'"), parent);
2798 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2802 "parent '%s' is not a filesystem"), parent);
2803 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2807 "volume block size must be power of 2 from "
2809 (uint_t)SPA_MINBLOCKSIZE,
2810 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2812 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2816 "pool must be upgraded to set this "
2817 "property or value"));
2818 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2822 * This platform can't address a volume this big.
2824 if (type == ZFS_TYPE_VOLUME)
2825 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2830 return (zfs_standard_error(hdl, errno, errbuf));
2838 * Destroys the given dataset. The caller must make sure that the filesystem
2839 * isn't mounted, and that there are no active dependents.
2842 zfs_destroy(zfs_handle_t *zhp)
2844 zfs_cmd_t zc = { 0 };
2846 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2848 if (ZFS_IS_VOLUME(zhp)) {
2850 * If user doesn't have permissions to unshare volume, then
2851 * abort the request. This would only happen for a
2852 * non-privileged user.
2854 if (zfs_unshare_iscsi(zhp) != 0) {
2858 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2861 zc.zc_objset_type = DMU_OST_ZVOL;
2863 zc.zc_objset_type = DMU_OST_ZFS;
2866 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2867 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2868 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2872 remove_mountpoint(zhp);
2877 struct destroydata {
2884 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
2886 struct destroydata *dd = arg;
2888 char name[ZFS_MAXNAMELEN];
2889 boolean_t closezhp = dd->closezhp;
2892 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
2893 (void) strlcat(name, "@", sizeof (name));
2894 (void) strlcat(name, dd->snapname, sizeof (name));
2896 szhp = make_dataset_handle(zhp->zfs_hdl, name);
2898 dd->gotone = B_TRUE;
2902 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
2903 (void) zvol_remove_link(zhp->zfs_hdl, name);
2905 * NB: this is simply a best-effort. We don't want to
2906 * return an error, because then we wouldn't visit all
2911 dd->closezhp = B_TRUE;
2912 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
2919 * Destroys all snapshots with the given name in zhp & descendants.
2922 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname)
2924 zfs_cmd_t zc = { 0 };
2926 struct destroydata dd = { 0 };
2928 dd.snapname = snapname;
2929 (void) zfs_remove_link_cb(zhp, &dd);
2932 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
2933 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
2934 zhp->zfs_name, snapname));
2937 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2938 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2940 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
2944 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2945 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
2949 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2950 "snapshot is cloned"));
2951 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
2954 return (zfs_standard_error(zhp->zfs_hdl, errno,
2963 * Clones the given dataset. The target must be of the same type as the source.
2966 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2968 zfs_cmd_t zc = { 0 };
2969 char parent[ZFS_MAXNAMELEN];
2972 libzfs_handle_t *hdl = zhp->zfs_hdl;
2976 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2978 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2979 "cannot create '%s'"), target);
2981 /* validate the target name */
2982 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
2983 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2985 /* validate parents exist */
2986 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2989 (void) parent_name(target, parent, sizeof (parent));
2992 if (ZFS_IS_VOLUME(zhp)) {
2993 zc.zc_objset_type = DMU_OST_ZVOL;
2994 type = ZFS_TYPE_VOLUME;
2996 zc.zc_objset_type = DMU_OST_ZFS;
2997 type = ZFS_TYPE_FILESYSTEM;
3001 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3002 zhp, errbuf)) == NULL)
3005 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3013 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3014 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3015 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3017 zcmd_free_nvlists(&zc);
3024 * The parent doesn't exist. We should have caught this
3025 * above, but there may a race condition that has since
3026 * destroyed the parent.
3028 * At this point, we don't know whether it's the source
3029 * that doesn't exist anymore, or whether the target
3030 * dataset doesn't exist.
3032 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3033 "no such parent '%s'"), parent);
3034 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3037 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3038 "source and target pools differ"));
3039 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3043 return (zfs_standard_error(zhp->zfs_hdl, errno,
3046 } else if (ZFS_IS_VOLUME(zhp)) {
3047 ret = zvol_create_link(zhp->zfs_hdl, target);
3053 typedef struct promote_data {
3054 char cb_mountpoint[MAXPATHLEN];
3055 const char *cb_target;
3056 const char *cb_errbuf;
3057 uint64_t cb_pivot_txg;
3061 promote_snap_cb(zfs_handle_t *zhp, void *data)
3063 promote_data_t *pd = data;
3065 char snapname[MAXPATHLEN];
3068 /* We don't care about snapshots after the pivot point */
3069 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3074 /* Remove the device link if it's a zvol. */
3075 if (ZFS_IS_VOLUME(zhp))
3076 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3078 /* Check for conflicting names */
3079 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3080 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3081 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3084 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3085 "snapshot name '%s' from origin \n"
3086 "conflicts with '%s' from target"),
3087 zhp->zfs_name, snapname);
3088 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3095 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3097 promote_data_t *pd = data;
3099 /* We don't care about snapshots after the pivot point */
3100 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3101 /* Create the device link if it's a zvol. */
3102 if (ZFS_IS_VOLUME(zhp))
3103 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3111 * Promotes the given clone fs to be the clone parent.
3114 zfs_promote(zfs_handle_t *zhp)
3116 libzfs_handle_t *hdl = zhp->zfs_hdl;
3117 zfs_cmd_t zc = { 0 };
3118 char parent[MAXPATHLEN];
3125 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3126 "cannot promote '%s'"), zhp->zfs_name);
3128 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3130 "snapshots can not be promoted"));
3131 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3134 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3135 if (parent[0] == '\0') {
3136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3137 "not a cloned filesystem"));
3138 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3140 cp = strchr(parent, '@');
3143 /* Walk the snapshots we will be moving */
3144 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3147 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3149 pd.cb_target = zhp->zfs_name;
3150 pd.cb_errbuf = errbuf;
3151 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3154 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3155 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3156 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3162 /* issue the ioctl */
3163 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3164 sizeof (zc.zc_value));
3165 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3166 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3169 int save_errno = errno;
3171 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3174 switch (save_errno) {
3177 * There is a conflicting snapshot name. We
3178 * should have caught this above, but they could
3179 * have renamed something in the mean time.
3181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3182 "conflicting snapshot name from parent '%s'"),
3184 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3187 return (zfs_standard_error(hdl, save_errno, errbuf));
3190 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3198 const char *cd_snapname;
3203 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3205 struct createdata *cd = arg;
3208 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3209 char name[MAXPATHLEN];
3211 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3212 (void) strlcat(name, "@", sizeof (name));
3213 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3214 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3217 * NB: this is simply a best-effort. We don't want to
3218 * return an error, because then we wouldn't visit all
3223 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3231 * Takes a snapshot of the given dataset.
3234 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3238 char parent[ZFS_MAXNAMELEN];
3240 zfs_cmd_t zc = { 0 };
3244 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3245 "cannot snapshot '%s'"), path);
3247 /* validate the target name */
3248 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3249 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3252 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3253 props, B_FALSE, NULL, errbuf)) == NULL)
3256 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3264 /* make sure the parent exists and is of the appropriate type */
3265 delim = strchr(path, '@');
3266 (void) strncpy(parent, path, delim - path);
3267 parent[delim - path] = '\0';
3269 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3270 ZFS_TYPE_VOLUME)) == NULL) {
3271 zcmd_free_nvlists(&zc);
3275 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3276 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3277 if (ZFS_IS_VOLUME(zhp))
3278 zc.zc_objset_type = DMU_OST_ZVOL;
3280 zc.zc_objset_type = DMU_OST_ZFS;
3281 zc.zc_cookie = recursive;
3282 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3284 zcmd_free_nvlists(&zc);
3287 * if it was recursive, the one that actually failed will be in
3291 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3292 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3294 if (ret == 0 && recursive) {
3295 struct createdata cd;
3297 cd.cd_snapname = delim + 1;
3298 cd.cd_ifexists = B_FALSE;
3299 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3301 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3302 ret = zvol_create_link(zhp->zfs_hdl, path);
3304 (void) zfs_standard_error(hdl, errno,
3305 dgettext(TEXT_DOMAIN,
3306 "Volume successfully snapshotted, but device links "
3307 "were not created"));
3314 (void) zfs_standard_error(hdl, errno, errbuf);
3322 * Destroy any more recent snapshots. We invoke this callback on any dependents
3323 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3324 * is a dependent and we should just destroy it without checking the transaction
3327 typedef struct rollback_data {
3328 const char *cb_target; /* the snapshot */
3329 uint64_t cb_create; /* creation time reference */
3331 boolean_t cb_dependent;
3336 rollback_destroy(zfs_handle_t *zhp, void *data)
3338 rollback_data_t *cbp = data;
3340 if (!cbp->cb_dependent) {
3341 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3342 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3343 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3347 cbp->cb_dependent = B_TRUE;
3348 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3349 rollback_destroy, cbp);
3350 cbp->cb_dependent = B_FALSE;
3352 logstr = zhp->zfs_hdl->libzfs_log_str;
3353 zhp->zfs_hdl->libzfs_log_str = NULL;
3354 cbp->cb_error |= zfs_destroy(zhp);
3355 zhp->zfs_hdl->libzfs_log_str = logstr;
3358 /* We must destroy this clone; first unmount it */
3359 prop_changelist_t *clp;
3361 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3362 cbp->cb_force ? MS_FORCE: 0);
3363 if (clp == NULL || changelist_prefix(clp) != 0) {
3364 cbp->cb_error = B_TRUE;
3368 if (zfs_destroy(zhp) != 0)
3369 cbp->cb_error = B_TRUE;
3371 changelist_remove(clp, zhp->zfs_name);
3372 (void) changelist_postfix(clp);
3373 changelist_free(clp);
3381 * Given a dataset, rollback to a specific snapshot, discarding any
3382 * data changes since then and making it the active dataset.
3384 * Any snapshots more recent than the target are destroyed, along with
3388 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3390 rollback_data_t cb = { 0 };
3392 zfs_cmd_t zc = { 0 };
3393 boolean_t restore_resv = 0;
3394 uint64_t old_volsize, new_volsize;
3395 zfs_prop_t resv_prop;
3397 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3398 zhp->zfs_type == ZFS_TYPE_VOLUME);
3401 * Destroy all recent snapshots and its dependends.
3403 cb.cb_force = force;
3404 cb.cb_target = snap->zfs_name;
3405 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3406 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3412 * Now that we have verified that the snapshot is the latest,
3413 * rollback to the given snapshot.
3416 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3417 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3419 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3421 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3423 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3426 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3428 if (ZFS_IS_VOLUME(zhp))
3429 zc.zc_objset_type = DMU_OST_ZVOL;
3431 zc.zc_objset_type = DMU_OST_ZFS;
3434 * We rely on zfs_iter_children() to verify that there are no
3435 * newer snapshots for the given dataset. Therefore, we can
3436 * simply pass the name on to the ioctl() call. There is still
3437 * an unlikely race condition where the user has taken a
3438 * snapshot since we verified that this was the most recent.
3441 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3442 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3443 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3449 * For volumes, if the pre-rollback volsize matched the pre-
3450 * rollback reservation and the volsize has changed then set
3451 * the reservation property to the post-rollback volsize.
3452 * Make a new handle since the rollback closed the dataset.
3454 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3455 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3456 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
3461 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3462 if (old_volsize != new_volsize)
3463 err = zfs_prop_set_int(zhp, resv_prop,
3472 * Iterate over all dependents for a given dataset. This includes both
3473 * hierarchical dependents (children) and data dependents (snapshots and
3474 * clones). The bulk of the processing occurs in get_dependents() in
3478 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3479 zfs_iter_f func, void *data)
3484 zfs_handle_t *child;
3487 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3488 &dependents, &count) != 0)
3491 for (i = 0; i < count; i++) {
3492 if ((child = make_dataset_handle(zhp->zfs_hdl,
3493 dependents[i])) == NULL)
3496 if ((ret = func(child, data)) != 0)
3500 for (i = 0; i < count; i++)
3501 free(dependents[i]);
3508 * Renames the given dataset.
3511 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3514 zfs_cmd_t zc = { 0 };
3516 prop_changelist_t *cl = NULL;
3517 zfs_handle_t *zhrp = NULL;
3518 char *parentname = NULL;
3519 char parent[ZFS_MAXNAMELEN];
3520 libzfs_handle_t *hdl = zhp->zfs_hdl;
3523 /* if we have the same exact name, just return success */
3524 if (strcmp(zhp->zfs_name, target) == 0)
3527 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3528 "cannot rename to '%s'"), target);
3531 * Make sure the target name is valid
3533 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3534 if ((strchr(target, '@') == NULL) ||
3537 * Snapshot target name is abbreviated,
3538 * reconstruct full dataset name
3540 (void) strlcpy(parent, zhp->zfs_name,
3542 delim = strchr(parent, '@');
3543 if (strchr(target, '@') == NULL)
3547 (void) strlcat(parent, target, sizeof (parent));
3551 * Make sure we're renaming within the same dataset.
3553 delim = strchr(target, '@');
3554 if (strncmp(zhp->zfs_name, target, delim - target)
3555 != 0 || zhp->zfs_name[delim - target] != '@') {
3556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3557 "snapshots must be part of same "
3559 return (zfs_error(hdl, EZFS_CROSSTARGET,
3563 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3564 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3568 "recursive rename must be a snapshot"));
3569 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3572 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3573 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3576 /* validate parents */
3577 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3580 (void) parent_name(target, parent, sizeof (parent));
3582 /* make sure we're in the same pool */
3583 verify((delim = strchr(target, '/')) != NULL);
3584 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3585 zhp->zfs_name[delim - target] != '/') {
3586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3587 "datasets must be within same pool"));
3588 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3591 /* new name cannot be a child of the current dataset name */
3592 if (strncmp(parent, zhp->zfs_name,
3593 strlen(zhp->zfs_name)) == 0) {
3594 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3595 "New dataset name cannot be a descendent of "
3596 "current dataset name"));
3597 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3601 (void) snprintf(errbuf, sizeof (errbuf),
3602 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3604 if (getzoneid() == GLOBAL_ZONEID &&
3605 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3606 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3607 "dataset is used in a non-global zone"));
3608 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3612 struct destroydata dd;
3614 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3615 if (parentname == NULL) {
3619 delim = strchr(parentname, '@');
3621 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3627 dd.snapname = delim + 1;
3628 dd.gotone = B_FALSE;
3629 dd.closezhp = B_TRUE;
3631 /* We remove any zvol links prior to renaming them */
3632 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
3637 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3640 if (changelist_haszonedchild(cl)) {
3641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3642 "child dataset with inherited mountpoint is used "
3643 "in a non-global zone"));
3644 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3648 if ((ret = changelist_prefix(cl)) != 0)
3652 if (ZFS_IS_VOLUME(zhp))
3653 zc.zc_objset_type = DMU_OST_ZVOL;
3655 zc.zc_objset_type = DMU_OST_ZFS;
3657 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3658 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3660 zc.zc_cookie = recursive;
3662 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3664 * if it was recursive, the one that actually failed will
3667 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3668 "cannot rename '%s'"), zc.zc_name);
3670 if (recursive && errno == EEXIST) {
3671 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3672 "a child dataset already has a snapshot "
3673 "with the new name"));
3674 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3676 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3680 * On failure, we still want to remount any filesystems that
3681 * were previously mounted, so we don't alter the system state.
3684 struct createdata cd;
3686 /* only create links for datasets that had existed */
3687 cd.cd_snapname = delim + 1;
3688 cd.cd_ifexists = B_TRUE;
3689 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3692 (void) changelist_postfix(cl);
3696 struct createdata cd;
3698 /* only create links for datasets that had existed */
3699 cd.cd_snapname = strchr(target, '@') + 1;
3700 cd.cd_ifexists = B_TRUE;
3701 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3704 changelist_rename(cl, zfs_get_name(zhp), target);
3705 ret = changelist_postfix(cl);
3717 changelist_free(cl);
3723 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3724 * poke devfsadm to create the /dev link, and then wait for the link to appear.
3727 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3729 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3733 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3735 zfs_cmd_t zc = { 0 };
3737 di_devlink_handle_t dhdl;
3738 priv_set_t *priv_effective;
3742 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3745 * Issue the appropriate ioctl.
3747 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3751 * Silently ignore the case where the link already
3752 * exists. This allows 'zfs volinit' to be run multiple
3753 * times without errors.
3759 * Dataset does not exist in the kernel. If we
3760 * don't care (see zfs_rename), then ignore the
3770 return (zfs_standard_error_fmt(hdl, errno,
3771 dgettext(TEXT_DOMAIN, "cannot create device links "
3772 "for '%s'"), dataset));
3778 * If privileged call devfsadm and wait for the links to
3780 * Otherwise, print out an informational message.
3783 priv_effective = priv_allocset();
3784 (void) getppriv(PRIV_EFFECTIVE, priv_effective);
3785 privileged = (priv_isfullset(priv_effective) == B_TRUE);
3786 priv_freeset(priv_effective);
3789 if ((dhdl = di_devlink_init(ZFS_DRIVER,
3790 DI_MAKE_LINK)) == NULL) {
3791 zfs_error_aux(hdl, strerror(errno));
3792 (void) zfs_error_fmt(hdl, errno,
3793 dgettext(TEXT_DOMAIN, "cannot create device links "
3794 "for '%s'"), dataset);
3795 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
3798 (void) di_devlink_fini(&dhdl);
3801 char pathname[MAXPATHLEN];
3802 struct stat64 statbuf;
3808 * This is the poor mans way of waiting for the link
3809 * to show up. If after 10 seconds we still don't
3810 * have it, then print out a message.
3812 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
3815 for (i = 0; i != MAX_WAIT; i++) {
3816 if (stat64(pathname, &statbuf) == 0)
3821 (void) printf(gettext("%s may not be immediately "
3822 "available\n"), pathname);
3830 * Remove a minor node for the given zvol and the associated /dev links.
3833 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3835 zfs_cmd_t zc = { 0 };
3837 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3839 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3843 * Silently ignore the case where the link no longer
3844 * exists, so that 'zfs volfini' can be run multiple
3845 * times without errors.
3850 return (zfs_standard_error_fmt(hdl, errno,
3851 dgettext(TEXT_DOMAIN, "cannot remove device "
3852 "links for '%s'"), dataset));
3860 zfs_get_user_props(zfs_handle_t *zhp)
3862 return (zhp->zfs_user_props);
3866 * This function is used by 'zfs list' to determine the exact set of columns to
3867 * display, and their maximum widths. This does two main things:
3869 * - If this is a list of all properties, then expand the list to include
3870 * all native properties, and set a flag so that for each dataset we look
3871 * for new unique user properties and add them to the list.
3873 * - For non fixed-width properties, keep track of the maximum width seen
3874 * so that we can size the column appropriately.
3877 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
3879 libzfs_handle_t *hdl = zhp->zfs_hdl;
3880 zprop_list_t *entry;
3881 zprop_list_t **last, **start;
3882 nvlist_t *userprops, *propval;
3885 char buf[ZFS_MAXPROPLEN];
3887 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3890 userprops = zfs_get_user_props(zhp);
3893 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3895 * Go through and add any user properties as necessary. We
3896 * start by incrementing our list pointer to the first
3897 * non-native property.
3900 while (*start != NULL) {
3901 if ((*start)->pl_prop == ZPROP_INVAL)
3903 start = &(*start)->pl_next;
3907 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3909 * See if we've already found this property in our list.
3911 for (last = start; *last != NULL;
3912 last = &(*last)->pl_next) {
3913 if (strcmp((*last)->pl_user_prop,
3914 nvpair_name(elem)) == 0)
3918 if (*last == NULL) {
3919 if ((entry = zfs_alloc(hdl,
3920 sizeof (zprop_list_t))) == NULL ||
3921 ((entry->pl_user_prop = zfs_strdup(hdl,
3922 nvpair_name(elem)))) == NULL) {
3927 entry->pl_prop = ZPROP_INVAL;
3928 entry->pl_width = strlen(nvpair_name(elem));
3929 entry->pl_all = B_TRUE;
3936 * Now go through and check the width of any non-fixed columns
3938 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3939 if (entry->pl_fixed)
3942 if (entry->pl_prop != ZPROP_INVAL) {
3943 if (zfs_prop_get(zhp, entry->pl_prop,
3944 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3945 if (strlen(buf) > entry->pl_width)
3946 entry->pl_width = strlen(buf);
3948 } else if (nvlist_lookup_nvlist(userprops,
3949 entry->pl_user_prop, &propval) == 0) {
3950 verify(nvlist_lookup_string(propval,
3951 ZPROP_VALUE, &strval) == 0);
3952 if (strlen(strval) > entry->pl_width)
3953 entry->pl_width = strlen(strval);
3962 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
3964 zfs_cmd_t zc = { 0 };
3968 const gid_t *groups;
3972 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
3973 return (no_memory(hdl));
3975 uid = ucred_geteuid(cred);
3976 gid = ucred_getegid(cred);
3977 group_cnt = ucred_getgroups(cred, &groups);
3979 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
3982 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
3987 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
3992 if (nvlist_add_uint32_array(nvp,
3993 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
3997 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3999 if (zcmd_write_src_nvlist(hdl, &zc, nvp))
4002 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
4009 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4010 char *resource, void *export, void *sharetab,
4011 int sharemax, zfs_share_op_t operation)
4013 zfs_cmd_t zc = { 0 };
4016 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4017 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4019 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4020 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4021 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4022 zc.zc_share.z_sharetype = operation;
4023 zc.zc_share.z_sharemax = sharemax;
4024 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4029 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4034 * Keep a reference to the props-table against which we prune the
4037 zhp->zfs_props_table = props;
4039 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4042 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4043 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4046 * We leave user:props in the nvlist, so there will be
4047 * some ZPROP_INVAL. To be extra safe, don't prune
4050 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4051 (void) nvlist_remove(zhp->zfs_props,
4052 nvpair_name(curr), nvpair_type(curr));
4059 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4060 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4062 zfs_cmd_t zc = { 0 };
4063 nvlist_t *nvlist = NULL;
4066 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4067 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4068 zc.zc_cookie = (uint64_t)cmd;
4070 if (cmd == ZFS_SMB_ACL_RENAME) {
4071 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4072 (void) no_memory(hdl);
4078 case ZFS_SMB_ACL_ADD:
4079 case ZFS_SMB_ACL_REMOVE:
4080 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4082 case ZFS_SMB_ACL_RENAME:
4083 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4085 (void) no_memory(hdl);
4088 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4090 (void) no_memory(hdl);
4093 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4094 nvlist_free(nvlist);
4098 case ZFS_SMB_ACL_PURGE:
4103 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4105 nvlist_free(nvlist);
4110 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4111 char *path, char *resource)
4113 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4118 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4119 char *path, char *resource)
4121 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4126 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4128 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4133 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4134 char *oldname, char *newname)
4136 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4142 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4143 zfs_userspace_cb_t func, void *arg)
4145 zfs_cmd_t zc = { 0 };
4147 zfs_useracct_t buf[100];
4149 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4151 zc.zc_objset_type = type;
4152 zc.zc_nvlist_dst = (uintptr_t)buf;
4156 zfs_useracct_t *zua = buf;
4158 zc.zc_nvlist_dst_size = sizeof (buf);
4159 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4160 ZFS_IOC_USERSPACE_MANY, &zc);
4161 if (error || zc.zc_nvlist_dst_size == 0)
4164 while (zc.zc_nvlist_dst_size > 0) {
4165 error = func(arg, zua->zu_domain, zua->zu_rid,
4170 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4178 * Attach/detach the given filesystem to/from the given jail.
4181 zfs_jail(zfs_handle_t *zhp, int jailid, int attach)
4183 libzfs_handle_t *hdl = zhp->zfs_hdl;
4184 zfs_cmd_t zc = { 0 };
4189 (void) snprintf(errbuf, sizeof (errbuf),
4190 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4192 (void) snprintf(errbuf, sizeof (errbuf),
4193 dgettext(TEXT_DOMAIN, "cannot jail '%s'"), zhp->zfs_name);
4196 switch (zhp->zfs_type) {
4197 case ZFS_TYPE_VOLUME:
4198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4199 "volumes can not be jailed"));
4200 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4201 case ZFS_TYPE_SNAPSHOT:
4202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4203 "snapshots can not be jailed"));
4204 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4206 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4208 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4209 zc.zc_objset_type = DMU_OST_ZFS;
4210 zc.zc_jailid = jailid;
4212 cmd = attach ? ZFS_IOC_JAIL : ZFS_IOC_UNJAIL;
4213 if ((ret = ioctl(hdl->libzfs_fd, cmd, &zc)) != 0)
4214 zfs_standard_error(hdl, errno, errbuf);