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.
30 #include <libdevinfo.h>
40 #include <sys/mntent.h>
41 #include <sys/mount.h>
50 #include <directory.h>
56 #include "zfs_namecheck.h"
58 #include "libzfs_impl.h"
59 #include "zfs_deleg.h"
61 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
62 static int userquota_propname_decode(const char *propname, boolean_t zoned,
63 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
66 * Given a single type (not a mask of types), return the type in a human
70 zfs_type_to_name(zfs_type_t type)
73 case ZFS_TYPE_FILESYSTEM:
74 return (dgettext(TEXT_DOMAIN, "filesystem"));
75 case ZFS_TYPE_SNAPSHOT:
76 return (dgettext(TEXT_DOMAIN, "snapshot"));
78 return (dgettext(TEXT_DOMAIN, "volume"));
85 * Given a path and mask of ZFS types, return a string describing this dataset.
86 * This is used when we fail to open a dataset and we cannot get an exact type.
87 * We guess what the type would have been based on the path and the mask of
91 path_to_str(const char *path, int types)
94 * When given a single type, always report the exact type.
96 if (types == ZFS_TYPE_SNAPSHOT)
97 return (dgettext(TEXT_DOMAIN, "snapshot"));
98 if (types == ZFS_TYPE_FILESYSTEM)
99 return (dgettext(TEXT_DOMAIN, "filesystem"));
100 if (types == ZFS_TYPE_VOLUME)
101 return (dgettext(TEXT_DOMAIN, "volume"));
104 * The user is requesting more than one type of dataset. If this is the
105 * case, consult the path itself. If we're looking for a snapshot, and
106 * a '@' is found, then report it as "snapshot". Otherwise, remove the
107 * snapshot attribute and try again.
109 if (types & ZFS_TYPE_SNAPSHOT) {
110 if (strchr(path, '@') != NULL)
111 return (dgettext(TEXT_DOMAIN, "snapshot"));
112 return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT));
116 * The user has requested either filesystems or volumes.
117 * We have no way of knowing a priori what type this would be, so always
118 * report it as "filesystem" or "volume", our two primitive types.
120 if (types & ZFS_TYPE_FILESYSTEM)
121 return (dgettext(TEXT_DOMAIN, "filesystem"));
123 assert(types & ZFS_TYPE_VOLUME);
124 return (dgettext(TEXT_DOMAIN, "volume"));
128 * Validate a ZFS path. This is used even before trying to open the dataset, to
129 * provide a more meaningful error message. We call zfs_error_aux() to
130 * explain exactly why the name was not valid.
133 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
139 if (dataset_namecheck(path, &why, &what) != 0) {
142 case NAME_ERR_TOOLONG:
143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
144 "name is too long"));
147 case NAME_ERR_LEADING_SLASH:
148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
149 "leading slash in name"));
152 case NAME_ERR_EMPTY_COMPONENT:
153 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
154 "empty component in name"));
157 case NAME_ERR_TRAILING_SLASH:
158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
159 "trailing slash in name"));
162 case NAME_ERR_INVALCHAR:
164 dgettext(TEXT_DOMAIN, "invalid character "
165 "'%c' in name"), what);
168 case NAME_ERR_MULTIPLE_AT:
169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170 "multiple '@' delimiters in name"));
173 case NAME_ERR_NOLETTER:
174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
175 "pool doesn't begin with a letter"));
178 case NAME_ERR_RESERVED:
179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
180 "name is reserved"));
183 case NAME_ERR_DISKLIKE:
184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185 "reserved disk name"));
193 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
196 "snapshot delimiter '@' in filesystem name"));
200 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
203 "missing '@' delimiter in snapshot name"));
207 if (modifying && strchr(path, '%') != NULL) {
209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
210 "invalid character %c in name"), '%');
218 zfs_name_valid(const char *name, zfs_type_t type)
220 if (type == ZFS_TYPE_POOL)
221 return (zpool_name_valid(NULL, B_FALSE, name));
222 return (zfs_validate_name(NULL, name, type, B_FALSE));
226 * This function takes the raw DSL properties, and filters out the user-defined
227 * properties into a separate nvlist.
230 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
232 libzfs_handle_t *hdl = zhp->zfs_hdl;
237 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
238 (void) no_memory(hdl);
243 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
244 if (!zfs_prop_user(nvpair_name(elem)))
247 verify(nvpair_value_nvlist(elem, &propval) == 0);
248 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
250 (void) no_memory(hdl);
258 static zpool_handle_t *
259 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
261 libzfs_handle_t *hdl = zhp->zfs_hdl;
264 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
265 if (hdl->libzfs_pool_handles != NULL)
266 zph->zpool_next = hdl->libzfs_pool_handles;
267 hdl->libzfs_pool_handles = zph;
272 static zpool_handle_t *
273 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
275 libzfs_handle_t *hdl = zhp->zfs_hdl;
276 zpool_handle_t *zph = hdl->libzfs_pool_handles;
278 while ((zph != NULL) &&
279 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
280 zph = zph->zpool_next;
285 * Returns a handle to the pool that contains the provided dataset.
286 * If a handle to that pool already exists then that handle is returned.
287 * Otherwise, a new handle is created and added to the list of handles.
289 static zpool_handle_t *
290 zpool_handle(zfs_handle_t *zhp)
296 len = strcspn(zhp->zfs_name, "/@") + 1;
297 pool_name = zfs_alloc(zhp->zfs_hdl, len);
298 (void) strlcpy(pool_name, zhp->zfs_name, len);
300 zph = zpool_find_handle(zhp, pool_name, len);
302 zph = zpool_add_handle(zhp, pool_name);
309 zpool_free_handles(libzfs_handle_t *hdl)
311 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
313 while (zph != NULL) {
314 next = zph->zpool_next;
318 hdl->libzfs_pool_handles = NULL;
322 * Utility function to gather stats (objset and zpl) for the given object.
325 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
327 libzfs_handle_t *hdl = zhp->zfs_hdl;
329 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
331 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
332 if (errno == ENOMEM) {
333 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
344 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
346 nvlist_t *allprops, *userprops;
348 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
350 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
355 * XXX Why do we store the user props separately, in addition to
356 * storing them in zfs_props?
358 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
359 nvlist_free(allprops);
363 nvlist_free(zhp->zfs_props);
364 nvlist_free(zhp->zfs_user_props);
366 zhp->zfs_props = allprops;
367 zhp->zfs_user_props = userprops;
373 get_stats(zfs_handle_t *zhp)
376 zfs_cmd_t zc = { 0 };
378 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
380 if (get_stats_ioctl(zhp, &zc) != 0)
382 else if (put_stats_zhdl(zhp, &zc) != 0)
384 zcmd_free_nvlists(&zc);
389 * Refresh the properties currently stored in the handle.
392 zfs_refresh_properties(zfs_handle_t *zhp)
394 (void) get_stats(zhp);
398 * Makes a handle from the given dataset name. Used by zfs_open() and
399 * zfs_iter_* to create child handles on the fly.
402 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
405 libzfs_handle_t *hdl = zhp->zfs_hdl;
408 * Preserve history log string.
409 * any changes performed here will be
410 * logged as an internal event.
412 logstr = zhp->zfs_hdl->libzfs_log_str;
413 zhp->zfs_hdl->libzfs_log_str = NULL;
416 if (put_stats_zhdl(zhp, zc) != 0) {
417 zhp->zfs_hdl->libzfs_log_str = logstr;
422 if (zhp->zfs_dmustats.dds_inconsistent) {
423 zfs_cmd_t zc2 = { 0 };
426 * If it is dds_inconsistent, then we've caught it in
427 * the middle of a 'zfs receive' or 'zfs destroy', and
428 * it is inconsistent from the ZPL's point of view, so
429 * can't be mounted. However, it could also be that we
430 * have crashed in the middle of one of those
431 * operations, in which case we need to get rid of the
432 * inconsistent state. We do that by either rolling
433 * back to the previous snapshot (which will fail if
434 * there is none), or destroying the filesystem. Note
435 * that if we are still in the middle of an active
436 * 'receive' or 'destroy', then the rollback and destroy
437 * will fail with EBUSY and we will drive on as usual.
440 (void) strlcpy(zc2.zc_name, zhp->zfs_name,
441 sizeof (zc2.zc_name));
443 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
444 (void) zvol_remove_link(hdl, zhp->zfs_name);
445 zc2.zc_objset_type = DMU_OST_ZVOL;
447 zc2.zc_objset_type = DMU_OST_ZFS;
451 * If we can successfully destroy it, pretend that it
454 if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc2) == 0) {
455 zhp->zfs_hdl->libzfs_log_str = logstr;
459 /* If we can successfully roll it back, reset the stats */
460 if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc2) == 0) {
461 if (get_stats_ioctl(zhp, zc) != 0) {
462 zhp->zfs_hdl->libzfs_log_str = logstr;
470 * We've managed to open the dataset and gather statistics. Determine
471 * the high-level type.
473 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
474 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
475 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
476 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
480 if (zhp->zfs_dmustats.dds_is_snapshot)
481 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
482 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
483 zhp->zfs_type = ZFS_TYPE_VOLUME;
484 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
485 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
487 abort(); /* we should never see any other types */
489 zhp->zfs_hdl->libzfs_log_str = logstr;
490 zhp->zpool_hdl = zpool_handle(zhp);
495 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
497 zfs_cmd_t zc = { 0 };
499 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
505 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
506 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
510 if (get_stats_ioctl(zhp, &zc) == -1) {
511 zcmd_free_nvlists(&zc);
515 if (make_dataset_handle_common(zhp, &zc) == -1) {
519 zcmd_free_nvlists(&zc);
523 static zfs_handle_t *
524 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
526 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
532 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
533 if (make_dataset_handle_common(zhp, zc) == -1) {
541 * Opens the given snapshot, filesystem, or volume. The 'types'
542 * argument is a mask of acceptable types. The function will print an
543 * appropriate error message and return NULL if it can't be opened.
546 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
551 (void) snprintf(errbuf, sizeof (errbuf),
552 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
555 * Validate the name before we even try to open it.
557 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
558 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
559 "invalid dataset name"));
560 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
565 * Try to get stats for the dataset, which will tell us if it exists.
568 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
569 (void) zfs_standard_error(hdl, errno, errbuf);
573 if (!(types & zhp->zfs_type)) {
574 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
583 * Release a ZFS handle. Nothing to do but free the associated memory.
586 zfs_close(zfs_handle_t *zhp)
588 if (zhp->zfs_mntopts)
589 free(zhp->zfs_mntopts);
590 nvlist_free(zhp->zfs_props);
591 nvlist_free(zhp->zfs_user_props);
595 typedef struct mnttab_node {
596 struct mnttab mtn_mt;
601 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
603 const mnttab_node_t *mtn1 = arg1;
604 const mnttab_node_t *mtn2 = arg2;
607 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
611 return (rv > 0 ? 1 : -1);
615 libzfs_mnttab_init(libzfs_handle_t *hdl)
617 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
618 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
619 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
623 libzfs_mnttab_update(libzfs_handle_t *hdl)
627 rewind(hdl->libzfs_mnttab);
628 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
631 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
633 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
634 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
635 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
636 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
637 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
638 avl_add(&hdl->libzfs_mnttab_cache, mtn);
643 libzfs_mnttab_fini(libzfs_handle_t *hdl)
648 while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) {
649 free(mtn->mtn_mt.mnt_special);
650 free(mtn->mtn_mt.mnt_mountp);
651 free(mtn->mtn_mt.mnt_fstype);
652 free(mtn->mtn_mt.mnt_mntopts);
655 avl_destroy(&hdl->libzfs_mnttab_cache);
659 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
661 hdl->libzfs_mnttab_enable = enable;
665 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
666 struct mnttab *entry)
671 if (!hdl->libzfs_mnttab_enable) {
672 struct mnttab srch = { 0 };
674 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
675 libzfs_mnttab_fini(hdl);
676 rewind(hdl->libzfs_mnttab);
677 srch.mnt_special = (char *)fsname;
678 srch.mnt_fstype = MNTTYPE_ZFS;
679 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
685 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
686 libzfs_mnttab_update(hdl);
688 find.mtn_mt.mnt_special = (char *)fsname;
689 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
691 *entry = mtn->mtn_mt;
698 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
699 const char *mountp, const char *mntopts)
703 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
705 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
706 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
707 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
708 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
709 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
710 avl_add(&hdl->libzfs_mnttab_cache, mtn);
714 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
719 find.mtn_mt.mnt_special = (char *)fsname;
720 if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) {
721 avl_remove(&hdl->libzfs_mnttab_cache, ret);
722 free(ret->mtn_mt.mnt_special);
723 free(ret->mtn_mt.mnt_mountp);
724 free(ret->mtn_mt.mnt_fstype);
725 free(ret->mtn_mt.mnt_mntopts);
731 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
733 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
735 if (zpool_handle == NULL)
738 *spa_version = zpool_get_prop_int(zpool_handle,
739 ZPOOL_PROP_VERSION, NULL);
744 * The choice of reservation property depends on the SPA version.
747 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
751 if (zfs_spa_version(zhp, &spa_version) < 0)
754 if (spa_version >= SPA_VERSION_REFRESERVATION)
755 *resv_prop = ZFS_PROP_REFRESERVATION;
757 *resv_prop = ZFS_PROP_RESERVATION;
763 * Given an nvlist of properties to set, validates that they are correct, and
764 * parses any numeric properties (index, boolean, etc) if they are specified as
768 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
769 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
776 int chosen_normal = -1;
779 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
780 (void) no_memory(hdl);
785 * Make sure this property is valid and applies to this type.
789 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
790 const char *propname = nvpair_name(elem);
792 prop = zfs_name_to_prop(propname);
793 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
795 * This is a user property: make sure it's a
796 * string, and that it's less than ZAP_MAXNAMELEN.
798 if (nvpair_type(elem) != DATA_TYPE_STRING) {
799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
800 "'%s' must be a string"), propname);
801 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
805 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
807 "property name '%s' is too long"),
809 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
813 (void) nvpair_value_string(elem, &strval);
814 if (nvlist_add_string(ret, propname, strval) != 0) {
815 (void) no_memory(hdl);
822 * Currently, only user properties can be modified on
825 if (type == ZFS_TYPE_SNAPSHOT) {
826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
827 "this property can not be modified for snapshots"));
828 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
832 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
833 zfs_userquota_prop_t uqtype;
834 char newpropname[128];
839 if (userquota_propname_decode(propname, zoned,
840 &uqtype, domain, sizeof (domain), &rid) != 0) {
842 dgettext(TEXT_DOMAIN,
843 "'%s' has an invalid user/group name"),
845 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
849 if (uqtype != ZFS_PROP_USERQUOTA &&
850 uqtype != ZFS_PROP_GROUPQUOTA) {
852 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
854 (void) zfs_error(hdl, EZFS_PROPREADONLY,
859 if (nvpair_type(elem) == DATA_TYPE_STRING) {
860 (void) nvpair_value_string(elem, &strval);
861 if (strcmp(strval, "none") == 0) {
863 } else if (zfs_nicestrtonum(hdl,
864 strval, &intval) != 0) {
865 (void) zfs_error(hdl,
866 EZFS_BADPROP, errbuf);
869 } else if (nvpair_type(elem) ==
871 (void) nvpair_value_uint64(elem, &intval);
873 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
874 "use 'none' to disable "
875 "userquota/groupquota"));
879 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
880 "'%s' must be a number"), propname);
881 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
885 (void) snprintf(newpropname, sizeof (newpropname),
886 "%s%s", zfs_userquota_prop_prefixes[uqtype],
891 if (nvlist_add_uint64_array(ret, newpropname,
893 (void) no_memory(hdl);
899 if (prop == ZPROP_INVAL) {
900 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
901 "invalid property '%s'"), propname);
902 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
906 if (!zfs_prop_valid_for_type(prop, type)) {
908 dgettext(TEXT_DOMAIN, "'%s' does not "
909 "apply to datasets of this type"), propname);
910 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
914 if (zfs_prop_readonly(prop) &&
915 (!zfs_prop_setonce(prop) || zhp != NULL)) {
917 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
919 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
923 if (zprop_parse_value(hdl, elem, prop, type, ret,
924 &strval, &intval, errbuf) != 0)
928 * Perform some additional checks for specific properties.
931 case ZFS_PROP_VERSION:
937 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
938 if (intval < version) {
939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940 "Can not downgrade; already at version %u"),
942 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
948 case ZFS_PROP_RECORDSIZE:
949 case ZFS_PROP_VOLBLOCKSIZE:
950 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
951 if (intval < SPA_MINBLOCKSIZE ||
952 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "'%s' must be power of 2 from %u "
956 (uint_t)SPA_MINBLOCKSIZE,
957 (uint_t)SPA_MAXBLOCKSIZE >> 10);
958 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
963 case ZFS_PROP_SHAREISCSI:
964 if (strcmp(strval, "off") != 0 &&
965 strcmp(strval, "on") != 0 &&
966 strcmp(strval, "type=disk") != 0) {
967 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
968 "'%s' must be 'on', 'off', or 'type=disk'"),
970 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
976 case ZFS_PROP_MOUNTPOINT:
980 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
981 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
984 if (mountpoint_namecheck(strval, &why)) {
986 case NAME_ERR_LEADING_SLASH:
988 dgettext(TEXT_DOMAIN,
989 "'%s' must be an absolute path, "
990 "'none', or 'legacy'"), propname);
992 case NAME_ERR_TOOLONG:
994 dgettext(TEXT_DOMAIN,
995 "component of '%s' is too long"),
999 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1006 case ZFS_PROP_SHARESMB:
1007 case ZFS_PROP_SHARENFS:
1009 * For the mountpoint and sharenfs or sharesmb
1010 * properties, check if it can be set in a
1011 * global/non-global zone based on
1012 * the zoned property value:
1014 * global zone non-global zone
1015 * --------------------------------------------------
1016 * zoned=on mountpoint (no) mountpoint (yes)
1017 * sharenfs (no) sharenfs (no)
1018 * sharesmb (no) sharesmb (no)
1020 * zoned=off mountpoint (yes) N/A
1025 if (getzoneid() == GLOBAL_ZONEID) {
1026 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1027 "'%s' cannot be set on "
1028 "dataset in a non-global zone"),
1030 (void) zfs_error(hdl, EZFS_ZONED,
1033 } else if (prop == ZFS_PROP_SHARENFS ||
1034 prop == ZFS_PROP_SHARESMB) {
1035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1036 "'%s' cannot be set in "
1037 "a non-global zone"), propname);
1038 (void) zfs_error(hdl, EZFS_ZONED,
1042 } else if (getzoneid() != GLOBAL_ZONEID) {
1044 * If zoned property is 'off', this must be in
1045 * a global zone. If not, something is wrong.
1047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1048 "'%s' cannot be set while dataset "
1049 "'zoned' property is set"), propname);
1050 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1055 * At this point, it is legitimate to set the
1056 * property. Now we want to make sure that the
1057 * property value is valid if it is sharenfs.
1059 if ((prop == ZFS_PROP_SHARENFS ||
1060 prop == ZFS_PROP_SHARESMB) &&
1061 strcmp(strval, "on") != 0 &&
1062 strcmp(strval, "off") != 0) {
1063 zfs_share_proto_t proto;
1065 if (prop == ZFS_PROP_SHARESMB)
1071 * Must be an valid sharing protocol
1072 * option string so init the libshare
1073 * in order to enable the parser and
1074 * then parse the options. We use the
1075 * control API since we don't care about
1076 * the current configuration and don't
1077 * want the overhead of loading it
1078 * until we actually do something.
1081 if (zfs_init_libshare(hdl,
1082 SA_INIT_CONTROL_API) != SA_OK) {
1084 * An error occurred so we can't do
1087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1088 "'%s' cannot be set: problem "
1089 "in share initialization"),
1091 (void) zfs_error(hdl, EZFS_BADPROP,
1096 if (zfs_parse_options(strval, proto) != SA_OK) {
1098 * There was an error in parsing so
1099 * deal with it by issuing an error
1100 * message and leaving after
1101 * uninitializing the the libshare
1104 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1105 "'%s' cannot be set to invalid "
1106 "options"), propname);
1107 (void) zfs_error(hdl, EZFS_BADPROP,
1109 zfs_uninit_libshare(hdl);
1112 zfs_uninit_libshare(hdl);
1116 case ZFS_PROP_UTF8ONLY:
1117 chosen_utf = (int)intval;
1119 case ZFS_PROP_NORMALIZE:
1120 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,
1175 * If normalization was chosen, but no UTF8 choice was made,
1176 * enforce rejection of non-UTF8 names.
1178 * If normalization was chosen, but rejecting non-UTF8 names
1179 * was explicitly not chosen, it is an error.
1181 if (chosen_normal > 0 && chosen_utf < 0) {
1182 if (nvlist_add_uint64(ret,
1183 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1184 (void) no_memory(hdl);
1187 } else if (chosen_normal > 0 && chosen_utf == 0) {
1188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1189 "'%s' must be set 'on' if normalization chosen"),
1190 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1191 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1196 * If this is an existing volume, and someone is setting the volsize,
1197 * make sure that it matches the reservation, or add it if necessary.
1199 if (zhp != NULL && type == ZFS_TYPE_VOLUME &&
1200 nvlist_lookup_uint64(ret, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1202 uint64_t old_volsize = zfs_prop_get_int(zhp,
1204 uint64_t old_reservation;
1205 uint64_t new_reservation;
1206 zfs_prop_t resv_prop;
1208 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1210 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1212 if (old_volsize == old_reservation &&
1213 nvlist_lookup_uint64(ret, zfs_prop_to_name(resv_prop),
1214 &new_reservation) != 0) {
1215 if (nvlist_add_uint64(ret,
1216 zfs_prop_to_name(resv_prop), intval) != 0) {
1217 (void) no_memory(hdl);
1230 * Given a property name and value, set the property for the given dataset.
1233 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1235 zfs_cmd_t zc = { 0 };
1237 prop_changelist_t *cl = NULL;
1239 libzfs_handle_t *hdl = zhp->zfs_hdl;
1240 nvlist_t *nvl = NULL, *realprops;
1242 boolean_t do_prefix;
1245 (void) snprintf(errbuf, sizeof (errbuf),
1246 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1249 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1250 nvlist_add_string(nvl, propname, propval) != 0) {
1251 (void) no_memory(hdl);
1255 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1256 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1262 prop = zfs_name_to_prop(propname);
1264 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1267 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1269 "child dataset with inherited mountpoint is used "
1270 "in a non-global zone"));
1271 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1276 * If the dataset's canmount property is being set to noauto,
1277 * then we want to prevent unmounting & remounting it.
1279 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1280 (zprop_string_to_index(prop, propval, &idx,
1281 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1283 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1287 * Execute the corresponding ioctl() to set this property.
1289 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1291 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1294 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1301 * For quotas and reservations, ENOSPC indicates
1302 * something different; setting a quota or reservation
1303 * doesn't use any disk space.
1306 case ZFS_PROP_QUOTA:
1307 case ZFS_PROP_REFQUOTA:
1308 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1309 "size is less than current used or "
1311 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1314 case ZFS_PROP_RESERVATION:
1315 case ZFS_PROP_REFRESERVATION:
1316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1317 "size is greater than available space"));
1318 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1322 (void) zfs_standard_error(hdl, errno, errbuf);
1328 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1329 (void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf);
1331 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1335 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1339 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1340 "pool and or dataset must be upgraded to set this "
1341 "property or value"));
1342 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1346 if (prop == ZFS_PROP_COMPRESSION) {
1347 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1348 "property setting is not allowed on "
1349 "bootable datasets"));
1350 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1352 (void) zfs_standard_error(hdl, errno, errbuf);
1358 * This platform can't address a volume this big.
1361 if (prop == ZFS_PROP_VOLSIZE) {
1362 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1368 (void) zfs_standard_error(hdl, errno, errbuf);
1372 ret = changelist_postfix(cl);
1375 * Refresh the statistics so the new property value
1379 (void) get_stats(zhp);
1384 zcmd_free_nvlists(&zc);
1386 changelist_free(cl);
1391 * Given a property, inherit the value from the parent dataset.
1394 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname)
1396 zfs_cmd_t zc = { 0 };
1398 prop_changelist_t *cl;
1399 libzfs_handle_t *hdl = zhp->zfs_hdl;
1403 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1404 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1406 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1408 * For user properties, the amount of work we have to do is very
1409 * small, so just do it here.
1411 if (!zfs_prop_user(propname)) {
1412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1413 "invalid property"));
1414 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1417 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1418 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1420 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1421 return (zfs_standard_error(hdl, errno, errbuf));
1427 * Verify that this property is inheritable.
1429 if (zfs_prop_readonly(prop))
1430 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1432 if (!zfs_prop_inheritable(prop))
1433 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1436 * Check to see if the value applies to this type
1438 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1439 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1442 * Normalize the name, to get rid of shorthand abbrevations.
1444 propname = zfs_prop_to_name(prop);
1445 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1446 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1448 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1449 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1451 "dataset is used in a non-global zone"));
1452 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1456 * Determine datasets which will be affected by this change, if any.
1458 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1461 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1463 "child dataset with inherited mountpoint is used "
1464 "in a non-global zone"));
1465 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1469 if ((ret = changelist_prefix(cl)) != 0)
1472 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1473 return (zfs_standard_error(hdl, errno, errbuf));
1476 if ((ret = changelist_postfix(cl)) != 0)
1480 * Refresh the statistics so the new property is reflected.
1482 (void) get_stats(zhp);
1486 changelist_free(cl);
1491 * True DSL properties are stored in an nvlist. The following two functions
1492 * extract them appropriately.
1495 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1501 if (nvlist_lookup_nvlist(zhp->zfs_props,
1502 zfs_prop_to_name(prop), &nv) == 0) {
1503 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1504 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1506 verify(!zhp->zfs_props_table ||
1507 zhp->zfs_props_table[prop] == B_TRUE);
1508 value = zfs_prop_default_numeric(prop);
1516 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1522 if (nvlist_lookup_nvlist(zhp->zfs_props,
1523 zfs_prop_to_name(prop), &nv) == 0) {
1524 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1525 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1527 verify(!zhp->zfs_props_table ||
1528 zhp->zfs_props_table[prop] == B_TRUE);
1529 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1538 * Internal function for getting a numeric property. Both zfs_prop_get() and
1539 * zfs_prop_get_int() are built using this interface.
1541 * Certain properties can be overridden using 'mount -o'. In this case, scan
1542 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1543 * If they differ from the on-disk values, report the current values and mark
1544 * the source "temporary".
1547 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1548 char **source, uint64_t *val)
1550 zfs_cmd_t zc = { 0 };
1551 nvlist_t *zplprops = NULL;
1553 char *mntopt_on = NULL;
1554 char *mntopt_off = NULL;
1559 case ZFS_PROP_ATIME:
1560 mntopt_on = MNTOPT_ATIME;
1561 mntopt_off = MNTOPT_NOATIME;
1564 case ZFS_PROP_DEVICES:
1565 mntopt_on = MNTOPT_DEVICES;
1566 mntopt_off = MNTOPT_NODEVICES;
1570 mntopt_on = MNTOPT_EXEC;
1571 mntopt_off = MNTOPT_NOEXEC;
1574 case ZFS_PROP_READONLY:
1575 mntopt_on = MNTOPT_RO;
1576 mntopt_off = MNTOPT_RW;
1579 case ZFS_PROP_SETUID:
1580 mntopt_on = MNTOPT_SETUID;
1581 mntopt_off = MNTOPT_NOSETUID;
1584 case ZFS_PROP_XATTR:
1585 mntopt_on = MNTOPT_XATTR;
1586 mntopt_off = MNTOPT_NOXATTR;
1589 case ZFS_PROP_NBMAND:
1590 mntopt_on = MNTOPT_NBMAND;
1591 mntopt_off = MNTOPT_NONBMAND;
1596 * Because looking up the mount options is potentially expensive
1597 * (iterating over all of /etc/mnttab), we defer its calculation until
1598 * we're looking up a property which requires its presence.
1600 if (!zhp->zfs_mntcheck &&
1601 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1602 libzfs_handle_t *hdl = zhp->zfs_hdl;
1603 struct mnttab entry;
1605 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1606 zhp->zfs_mntopts = zfs_strdup(hdl,
1608 if (zhp->zfs_mntopts == NULL)
1612 zhp->zfs_mntcheck = B_TRUE;
1615 if (zhp->zfs_mntopts == NULL)
1616 mnt.mnt_mntopts = "";
1618 mnt.mnt_mntopts = zhp->zfs_mntopts;
1621 case ZFS_PROP_ATIME:
1622 case ZFS_PROP_DEVICES:
1624 case ZFS_PROP_READONLY:
1625 case ZFS_PROP_SETUID:
1626 case ZFS_PROP_XATTR:
1627 case ZFS_PROP_NBMAND:
1628 *val = getprop_uint64(zhp, prop, source);
1630 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1633 *src = ZPROP_SRC_TEMPORARY;
1634 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1637 *src = ZPROP_SRC_TEMPORARY;
1641 case ZFS_PROP_CANMOUNT:
1642 *val = getprop_uint64(zhp, prop, source);
1643 if (*val != ZFS_CANMOUNT_ON)
1644 *source = zhp->zfs_name;
1646 *source = ""; /* default */
1649 case ZFS_PROP_QUOTA:
1650 case ZFS_PROP_REFQUOTA:
1651 case ZFS_PROP_RESERVATION:
1652 case ZFS_PROP_REFRESERVATION:
1653 *val = getprop_uint64(zhp, prop, source);
1655 *source = ""; /* default */
1657 *source = zhp->zfs_name;
1660 case ZFS_PROP_MOUNTED:
1661 *val = (zhp->zfs_mntopts != NULL);
1664 case ZFS_PROP_NUMCLONES:
1665 *val = zhp->zfs_dmustats.dds_num_clones;
1668 case ZFS_PROP_VERSION:
1669 case ZFS_PROP_NORMALIZE:
1670 case ZFS_PROP_UTF8ONLY:
1672 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1673 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1675 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1676 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1677 zcmd_free_nvlists(&zc);
1680 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1681 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1683 zcmd_free_nvlists(&zc);
1687 nvlist_free(zplprops);
1688 zcmd_free_nvlists(&zc);
1692 switch (zfs_prop_get_type(prop)) {
1693 case PROP_TYPE_NUMBER:
1694 case PROP_TYPE_INDEX:
1695 *val = getprop_uint64(zhp, prop, source);
1697 * If we tried to use a default value for a
1698 * readonly property, it means that it was not
1699 * present; return an error.
1701 if (zfs_prop_readonly(prop) &&
1702 *source && (*source)[0] == '\0') {
1707 case PROP_TYPE_STRING:
1709 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1710 "cannot get non-numeric property"));
1711 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1712 dgettext(TEXT_DOMAIN, "internal error")));
1720 * Calculate the source type, given the raw source string.
1723 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1724 char *statbuf, size_t statlen)
1726 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1729 if (source == NULL) {
1730 *srctype = ZPROP_SRC_NONE;
1731 } else if (source[0] == '\0') {
1732 *srctype = ZPROP_SRC_DEFAULT;
1734 if (strcmp(source, zhp->zfs_name) == 0) {
1735 *srctype = ZPROP_SRC_LOCAL;
1737 (void) strlcpy(statbuf, source, statlen);
1738 *srctype = ZPROP_SRC_INHERITED;
1745 * Retrieve a property from the given object. If 'literal' is specified, then
1746 * numbers are left as exact values. Otherwise, numbers are converted to a
1747 * human-readable form.
1749 * Returns 0 on success, or -1 on error.
1752 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1753 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1755 char *source = NULL;
1761 * Check to see if this property applies to our object
1763 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1767 *src = ZPROP_SRC_NONE;
1770 case ZFS_PROP_CREATION:
1772 * 'creation' is a time_t stored in the statistics. We convert
1773 * this into a string unless 'literal' is specified.
1776 val = getprop_uint64(zhp, prop, &source);
1777 time_t time = (time_t)val;
1781 localtime_r(&time, &t) == NULL ||
1782 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1784 (void) snprintf(propbuf, proplen, "%llu", val);
1788 case ZFS_PROP_MOUNTPOINT:
1790 * Getting the precise mountpoint can be tricky.
1792 * - for 'none' or 'legacy', return those values.
1793 * - for inherited mountpoints, we want to take everything
1794 * after our ancestor and append it to the inherited value.
1796 * If the pool has an alternate root, we want to prepend that
1797 * root to any values we return.
1800 str = getprop_string(zhp, prop, &source);
1802 if (str[0] == '/') {
1803 char buf[MAXPATHLEN];
1805 const char *relpath = zhp->zfs_name + strlen(source);
1807 if (relpath[0] == '/')
1810 if ((zpool_get_prop(zhp->zpool_hdl,
1811 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1812 (strcmp(root, "-") == 0))
1815 * Special case an alternate root of '/'. This will
1816 * avoid having multiple leading slashes in the
1819 if (strcmp(root, "/") == 0)
1823 * If the mountpoint is '/' then skip over this
1824 * if we are obtaining either an alternate root or
1825 * an inherited mountpoint.
1827 if (str[1] == '\0' && (root[0] != '\0' ||
1828 relpath[0] != '\0'))
1831 if (relpath[0] == '\0')
1832 (void) snprintf(propbuf, proplen, "%s%s",
1835 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1836 root, str, relpath[0] == '@' ? "" : "/",
1839 /* 'legacy' or 'none' */
1840 (void) strlcpy(propbuf, str, proplen);
1845 case ZFS_PROP_ORIGIN:
1846 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1849 * If there is no parent at all, return failure to indicate that
1850 * it doesn't apply to this dataset.
1852 if (propbuf[0] == '\0')
1856 case ZFS_PROP_QUOTA:
1857 case ZFS_PROP_REFQUOTA:
1858 case ZFS_PROP_RESERVATION:
1859 case ZFS_PROP_REFRESERVATION:
1861 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1865 * If quota or reservation is 0, we translate this into 'none'
1866 * (unless literal is set), and indicate that it's the default
1867 * value. Otherwise, we print the number nicely and indicate
1868 * that its set locally.
1872 (void) strlcpy(propbuf, "0", proplen);
1874 (void) strlcpy(propbuf, "none", proplen);
1877 (void) snprintf(propbuf, proplen, "%llu",
1880 zfs_nicenum(val, propbuf, proplen);
1884 case ZFS_PROP_COMPRESSRATIO:
1885 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1887 (void) snprintf(propbuf, proplen, "%lld.%02lldx", (longlong_t)
1888 val / 100, (longlong_t)val % 100);
1892 switch (zhp->zfs_type) {
1893 case ZFS_TYPE_FILESYSTEM:
1896 case ZFS_TYPE_VOLUME:
1899 case ZFS_TYPE_SNAPSHOT:
1905 (void) snprintf(propbuf, proplen, "%s", str);
1908 case ZFS_PROP_MOUNTED:
1910 * The 'mounted' property is a pseudo-property that described
1911 * whether the filesystem is currently mounted. Even though
1912 * it's a boolean value, the typical values of "on" and "off"
1913 * don't make sense, so we translate to "yes" and "no".
1915 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
1916 src, &source, &val) != 0)
1919 (void) strlcpy(propbuf, "yes", proplen);
1921 (void) strlcpy(propbuf, "no", proplen);
1926 * The 'name' property is a pseudo-property derived from the
1927 * dataset name. It is presented as a real property to simplify
1930 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
1934 switch (zfs_prop_get_type(prop)) {
1935 case PROP_TYPE_NUMBER:
1936 if (get_numeric_property(zhp, prop, src,
1937 &source, &val) != 0)
1940 (void) snprintf(propbuf, proplen, "%llu",
1943 zfs_nicenum(val, propbuf, proplen);
1946 case PROP_TYPE_STRING:
1947 (void) strlcpy(propbuf,
1948 getprop_string(zhp, prop, &source), proplen);
1951 case PROP_TYPE_INDEX:
1952 if (get_numeric_property(zhp, prop, src,
1953 &source, &val) != 0)
1955 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
1957 (void) strlcpy(propbuf, strval, proplen);
1965 get_source(zhp, src, source, statbuf, statlen);
1971 * Utility function to get the given numeric property. Does no validation that
1972 * the given property is the appropriate type; should only be used with
1973 * hard-coded property types.
1976 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
1981 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
1987 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
1991 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
1992 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
1996 * Similar to zfs_prop_get(), but returns the value as an integer.
1999 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2000 zprop_source_t *src, char *statbuf, size_t statlen)
2005 * Check to see if this property applies to our object
2007 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2008 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2009 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2010 zfs_prop_to_name(prop)));
2014 *src = ZPROP_SRC_NONE;
2016 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2019 get_source(zhp, src, source, statbuf, statlen);
2025 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2026 char **domainp, idmap_rid_t *ridp)
2028 idmap_handle_t *idmap_hdl = NULL;
2029 idmap_get_handle_t *get_hdl = NULL;
2033 if (idmap_init(&idmap_hdl) != IDMAP_SUCCESS)
2035 if (idmap_get_create(idmap_hdl, &get_hdl) != IDMAP_SUCCESS)
2039 err = idmap_get_sidbyuid(get_hdl, id,
2040 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2042 err = idmap_get_sidbygid(get_hdl, id,
2043 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2045 if (err == IDMAP_SUCCESS &&
2046 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2047 status == IDMAP_SUCCESS)
2053 idmap_get_destroy(get_hdl);
2055 (void) idmap_fini(idmap_hdl);
2060 * convert the propname into parameters needed by kernel
2061 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2062 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2065 userquota_propname_decode(const char *propname, boolean_t zoned,
2066 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2068 zfs_userquota_prop_t type;
2070 char *numericsid = NULL;
2075 /* Figure out the property type ({user|group}{quota|space}) */
2076 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2077 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2078 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2081 if (type == ZFS_NUM_USERQUOTA_PROPS)
2085 isuser = (type == ZFS_PROP_USERQUOTA ||
2086 type == ZFS_PROP_USERUSED);
2088 cp = strchr(propname, '@') + 1;
2090 if (strchr(cp, '@')) {
2092 * It's a SID name (eg "user@domain") that needs to be
2093 * turned into S-1-domainID-RID.
2095 directory_error_t e;
2096 if (zoned && getzoneid() == GLOBAL_ZONEID)
2099 e = directory_sid_from_user_name(NULL,
2102 e = directory_sid_from_group_name(NULL,
2106 directory_error_free(e);
2109 if (numericsid == NULL)
2112 /* will be further decoded below */
2115 if (strncmp(cp, "S-1-", 4) == 0) {
2116 /* It's a numeric SID (eg "S-1-234-567-89") */
2117 (void) strlcpy(domain, cp, domainlen);
2118 cp = strrchr(domain, '-');
2123 *ridp = strtoull(cp, &end, 10);
2128 if (errno != 0 || *end != '\0')
2130 } else if (!isdigit(*cp)) {
2132 * It's a user/group name (eg "user") that needs to be
2133 * turned into a uid/gid
2135 if (zoned && getzoneid() == GLOBAL_ZONEID)
2151 /* It's a user/group ID (eg "12345"). */
2152 uid_t id = strtoul(cp, &end, 10);
2159 /* It's an ephemeral ID. */
2160 if (idmap_id_to_numeric_domain_rid(id, isuser,
2161 &mapdomain, &rid) != 0)
2163 (void) strlcpy(domain, mapdomain, domainlen);
2170 ASSERT3P(numericsid, ==, NULL);
2175 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2176 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2179 zfs_cmd_t zc = { 0 };
2181 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2183 err = userquota_propname_decode(propname,
2184 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2185 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2186 zc.zc_objset_type = *typep;
2190 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2194 *propvalue = zc.zc_cookie;
2199 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2200 uint64_t *propvalue)
2202 zfs_userquota_prop_t type;
2204 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2209 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2210 char *propbuf, int proplen, boolean_t literal)
2214 zfs_userquota_prop_t type;
2216 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2223 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2224 } else if (propvalue == 0 &&
2225 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2226 (void) strlcpy(propbuf, "none", proplen);
2228 zfs_nicenum(propvalue, propbuf, proplen);
2234 * Returns the name of the given zfs handle.
2237 zfs_get_name(const zfs_handle_t *zhp)
2239 return (zhp->zfs_name);
2243 * Returns the type of the given zfs handle.
2246 zfs_get_type(const zfs_handle_t *zhp)
2248 return (zhp->zfs_type);
2252 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2255 uint64_t orig_cookie;
2257 orig_cookie = zc->zc_cookie;
2259 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2260 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2265 /* expand nvlist memory and try again */
2266 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2267 zcmd_free_nvlists(zc);
2270 zc->zc_cookie = orig_cookie;
2273 * An errno value of ESRCH indicates normal completion.
2274 * If ENOENT is returned, then the underlying dataset
2275 * has been removed since we obtained the handle.
2282 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2283 dgettext(TEXT_DOMAIN,
2284 "cannot iterate filesystems"));
2292 * Iterate over all child filesystems
2295 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2297 zfs_cmd_t zc = { 0 };
2301 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2304 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2307 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2310 * Silently ignore errors, as the only plausible explanation is
2311 * that the pool has since been removed.
2313 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2318 if ((ret = func(nzhp, data)) != 0) {
2319 zcmd_free_nvlists(&zc);
2323 zcmd_free_nvlists(&zc);
2324 return ((ret < 0) ? ret : 0);
2328 * Iterate over all snapshots
2331 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2333 zfs_cmd_t zc = { 0 };
2337 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2340 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2342 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2345 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2350 if ((ret = func(nzhp, data)) != 0) {
2351 zcmd_free_nvlists(&zc);
2355 zcmd_free_nvlists(&zc);
2356 return ((ret < 0) ? ret : 0);
2360 * Iterate over all children, snapshots and filesystems
2363 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2367 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2370 return (zfs_iter_snapshots(zhp, func, data));
2374 * Given a complete name, return just the portion that refers to the parent.
2375 * Can return NULL if this is a pool.
2378 parent_name(const char *path, char *buf, size_t buflen)
2382 if ((loc = strrchr(path, '/')) == NULL)
2385 (void) strncpy(buf, path, MIN(buflen, loc - path));
2386 buf[loc - path] = '\0';
2392 * If accept_ancestor is false, then check to make sure that the given path has
2393 * a parent, and that it exists. If accept_ancestor is true, then find the
2394 * closest existing ancestor for the given path. In prefixlen return the
2395 * length of already existing prefix of the given path. We also fetch the
2396 * 'zoned' property, which is used to validate property settings when creating
2400 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2401 boolean_t accept_ancestor, int *prefixlen)
2403 zfs_cmd_t zc = { 0 };
2404 char parent[ZFS_MAXNAMELEN];
2409 (void) snprintf(errbuf, sizeof (errbuf),
2410 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2412 /* get parent, and check to see if this is just a pool */
2413 if (parent_name(path, parent, sizeof (parent)) != 0) {
2414 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2415 "missing dataset name"));
2416 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2419 /* check to see if the pool exists */
2420 if ((slash = strchr(parent, '/')) == NULL)
2421 slash = parent + strlen(parent);
2422 (void) strncpy(zc.zc_name, parent, slash - parent);
2423 zc.zc_name[slash - parent] = '\0';
2424 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2426 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2427 "no such pool '%s'"), zc.zc_name);
2428 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2431 /* check to see if the parent dataset exists */
2432 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2433 if (errno == ENOENT && accept_ancestor) {
2435 * Go deeper to find an ancestor, give up on top level.
2437 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2439 "no such pool '%s'"), zc.zc_name);
2440 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2442 } else if (errno == ENOENT) {
2443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2444 "parent does not exist"));
2445 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2447 return (zfs_standard_error(hdl, errno, errbuf));
2450 *zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2451 /* we are in a non-global zone, but parent is in the global zone */
2452 if (getzoneid() != GLOBAL_ZONEID && !(*zoned)) {
2453 (void) zfs_standard_error(hdl, EPERM, errbuf);
2458 /* make sure parent is a filesystem */
2459 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2461 "parent is not a filesystem"));
2462 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2468 if (prefixlen != NULL)
2469 *prefixlen = strlen(parent);
2474 * Finds whether the dataset of the given type(s) exists.
2477 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2481 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2485 * Try to get stats for the dataset, which will tell us if it exists.
2487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2488 int ds_type = zhp->zfs_type;
2491 if (types & ds_type)
2498 * Given a path to 'target', create all the ancestors between
2499 * the prefixlen portion of the path, and the target itself.
2500 * Fail if the initial prefixlen-ancestor does not already exist.
2503 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2509 /* make sure prefix exists */
2510 cp = target + prefixlen;
2512 assert(strchr(cp, '/') == NULL);
2513 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2516 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2524 * Attempt to create, mount, and share any ancestor filesystems,
2525 * up to the prefixlen-long one.
2527 for (cp = target + prefixlen + 1;
2528 cp = strchr(cp, '/'); *cp = '/', cp++) {
2533 h = make_dataset_handle(hdl, target);
2535 /* it already exists, nothing to do here */
2540 logstr = hdl->libzfs_log_str;
2541 hdl->libzfs_log_str = NULL;
2542 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2544 hdl->libzfs_log_str = logstr;
2545 opname = dgettext(TEXT_DOMAIN, "create");
2549 hdl->libzfs_log_str = logstr;
2550 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2552 opname = dgettext(TEXT_DOMAIN, "open");
2556 if (zfs_mount(h, NULL, 0) != 0) {
2557 opname = dgettext(TEXT_DOMAIN, "mount");
2561 if (zfs_share(h) != 0) {
2562 opname = dgettext(TEXT_DOMAIN, "share");
2572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2573 "failed to %s ancestor '%s'"), opname, target);
2578 * Creates non-existing ancestors of the given path.
2581 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2588 if (check_parents(hdl, path, &zoned, B_TRUE, &prefix) != 0)
2591 if ((path_copy = strdup(path)) != NULL) {
2592 rc = create_parents(hdl, path_copy, prefix);
2595 if (path_copy == NULL || rc != 0)
2602 * Create a new filesystem or volume.
2605 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2608 zfs_cmd_t zc = { 0 };
2611 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2615 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2616 "cannot create '%s'"), path);
2618 /* validate the path, taking care to note the extended error message */
2619 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2620 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2622 /* validate parents exist */
2623 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2627 * The failure modes when creating a dataset of a different type over
2628 * one that already exists is a little strange. In particular, if you
2629 * try to create a dataset on top of an existing dataset, the ioctl()
2630 * will return ENOENT, not EEXIST. To prevent this from happening, we
2631 * first try to see if the dataset exists.
2633 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2634 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2636 "dataset already exists"));
2637 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2640 if (type == ZFS_TYPE_VOLUME)
2641 zc.zc_objset_type = DMU_OST_ZVOL;
2643 zc.zc_objset_type = DMU_OST_ZFS;
2645 if (props && (props = zfs_valid_proplist(hdl, type, props,
2646 zoned, NULL, errbuf)) == 0)
2649 if (type == ZFS_TYPE_VOLUME) {
2651 * If we are creating a volume, the size and block size must
2652 * satisfy a few restraints. First, the blocksize must be a
2653 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2654 * volsize must be a multiple of the block size, and cannot be
2657 if (props == NULL || nvlist_lookup_uint64(props,
2658 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2661 "missing volume size"));
2662 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2665 if ((ret = nvlist_lookup_uint64(props,
2666 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2667 &blocksize)) != 0) {
2668 if (ret == ENOENT) {
2669 blocksize = zfs_prop_default_numeric(
2670 ZFS_PROP_VOLBLOCKSIZE);
2673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2674 "missing volume block size"));
2675 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2682 "volume size cannot be zero"));
2683 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2686 if (size % blocksize != 0) {
2688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2689 "volume size must be a multiple of volume block "
2691 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2695 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2699 /* create the dataset */
2700 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2702 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2703 ret = zvol_create_link(hdl, path);
2705 (void) zfs_standard_error(hdl, errno,
2706 dgettext(TEXT_DOMAIN,
2707 "Volume successfully created, but device links "
2708 "were not created"));
2709 zcmd_free_nvlists(&zc);
2714 zcmd_free_nvlists(&zc);
2716 /* check for failure */
2718 char parent[ZFS_MAXNAMELEN];
2719 (void) parent_name(path, parent, sizeof (parent));
2723 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2724 "no such parent '%s'"), parent);
2725 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2728 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2729 "parent '%s' is not a filesystem"), parent);
2730 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2734 "volume block size must be power of 2 from "
2736 (uint_t)SPA_MINBLOCKSIZE,
2737 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2739 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2743 "pool must be upgraded to set this "
2744 "property or value"));
2745 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2749 * This platform can't address a volume this big.
2751 if (type == ZFS_TYPE_VOLUME)
2752 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2757 return (zfs_standard_error(hdl, errno, errbuf));
2765 * Destroys the given dataset. The caller must make sure that the filesystem
2766 * isn't mounted, and that there are no active dependents.
2769 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2771 zfs_cmd_t zc = { 0 };
2773 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2775 if (ZFS_IS_VOLUME(zhp)) {
2777 * If user doesn't have permissions to unshare volume, then
2778 * abort the request. This would only happen for a
2779 * non-privileged user.
2781 if (zfs_unshare_iscsi(zhp) != 0) {
2785 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
2788 zc.zc_objset_type = DMU_OST_ZVOL;
2790 zc.zc_objset_type = DMU_OST_ZFS;
2793 zc.zc_defer_destroy = defer;
2794 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2795 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2796 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2800 remove_mountpoint(zhp);
2805 struct destroydata {
2812 zfs_remove_link_cb(zfs_handle_t *zhp, void *arg)
2814 struct destroydata *dd = arg;
2816 char name[ZFS_MAXNAMELEN];
2817 boolean_t closezhp = dd->closezhp;
2820 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
2821 (void) strlcat(name, "@", sizeof (name));
2822 (void) strlcat(name, dd->snapname, sizeof (name));
2824 szhp = make_dataset_handle(zhp->zfs_hdl, name);
2826 dd->gotone = B_TRUE;
2830 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
2831 (void) zvol_remove_link(zhp->zfs_hdl, name);
2833 * NB: this is simply a best-effort. We don't want to
2834 * return an error, because then we wouldn't visit all
2839 dd->closezhp = B_TRUE;
2840 rv = zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg);
2847 * Destroys all snapshots with the given name in zhp & descendants.
2850 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
2852 zfs_cmd_t zc = { 0 };
2854 struct destroydata dd = { 0 };
2856 dd.snapname = snapname;
2857 (void) zfs_remove_link_cb(zhp, &dd);
2860 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
2861 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
2862 zhp->zfs_name, snapname));
2865 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2866 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2867 zc.zc_defer_destroy = defer;
2869 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
2873 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2874 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
2878 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2879 "snapshot is cloned"));
2880 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
2883 return (zfs_standard_error(zhp->zfs_hdl, errno,
2892 * Clones the given dataset. The target must be of the same type as the source.
2895 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
2897 zfs_cmd_t zc = { 0 };
2898 char parent[ZFS_MAXNAMELEN];
2901 libzfs_handle_t *hdl = zhp->zfs_hdl;
2905 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
2907 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2908 "cannot create '%s'"), target);
2910 /* validate the target name */
2911 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
2912 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2914 /* validate parents exist */
2915 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
2918 (void) parent_name(target, parent, sizeof (parent));
2921 if (ZFS_IS_VOLUME(zhp)) {
2922 zc.zc_objset_type = DMU_OST_ZVOL;
2923 type = ZFS_TYPE_VOLUME;
2925 zc.zc_objset_type = DMU_OST_ZFS;
2926 type = ZFS_TYPE_FILESYSTEM;
2930 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
2931 zhp, errbuf)) == NULL)
2934 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
2942 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
2943 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
2944 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
2946 zcmd_free_nvlists(&zc);
2953 * The parent doesn't exist. We should have caught this
2954 * above, but there may a race condition that has since
2955 * destroyed the parent.
2957 * At this point, we don't know whether it's the source
2958 * that doesn't exist anymore, or whether the target
2959 * dataset doesn't exist.
2961 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2962 "no such parent '%s'"), parent);
2963 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2966 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2967 "source and target pools differ"));
2968 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
2972 return (zfs_standard_error(zhp->zfs_hdl, errno,
2975 } else if (ZFS_IS_VOLUME(zhp)) {
2976 ret = zvol_create_link(zhp->zfs_hdl, target);
2982 typedef struct promote_data {
2983 char cb_mountpoint[MAXPATHLEN];
2984 const char *cb_target;
2985 const char *cb_errbuf;
2986 uint64_t cb_pivot_txg;
2990 promote_snap_cb(zfs_handle_t *zhp, void *data)
2992 promote_data_t *pd = data;
2994 char snapname[MAXPATHLEN];
2997 /* We don't care about snapshots after the pivot point */
2998 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3003 /* Remove the device link if it's a zvol. */
3004 if (ZFS_IS_VOLUME(zhp))
3005 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3007 /* Check for conflicting names */
3008 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3009 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3010 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3013 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3014 "snapshot name '%s' from origin \n"
3015 "conflicts with '%s' from target"),
3016 zhp->zfs_name, snapname);
3017 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3024 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3026 promote_data_t *pd = data;
3028 /* We don't care about snapshots after the pivot point */
3029 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3030 /* Create the device link if it's a zvol. */
3031 if (ZFS_IS_VOLUME(zhp))
3032 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3040 * Promotes the given clone fs to be the clone parent.
3043 zfs_promote(zfs_handle_t *zhp)
3045 libzfs_handle_t *hdl = zhp->zfs_hdl;
3046 zfs_cmd_t zc = { 0 };
3047 char parent[MAXPATHLEN];
3054 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3055 "cannot promote '%s'"), zhp->zfs_name);
3057 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3058 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3059 "snapshots can not be promoted"));
3060 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3063 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3064 if (parent[0] == '\0') {
3065 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3066 "not a cloned filesystem"));
3067 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3069 cp = strchr(parent, '@');
3072 /* Walk the snapshots we will be moving */
3073 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3076 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3078 pd.cb_target = zhp->zfs_name;
3079 pd.cb_errbuf = errbuf;
3080 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3083 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3084 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3085 ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd);
3091 /* issue the ioctl */
3092 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3093 sizeof (zc.zc_value));
3094 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3095 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3098 int save_errno = errno;
3100 (void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd);
3103 switch (save_errno) {
3106 * There is a conflicting snapshot name. We
3107 * should have caught this above, but they could
3108 * have renamed something in the mean time.
3110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3111 "conflicting snapshot name from parent '%s'"),
3113 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3116 return (zfs_standard_error(hdl, save_errno, errbuf));
3119 (void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd);
3127 const char *cd_snapname;
3132 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3134 struct createdata *cd = arg;
3137 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3138 char name[MAXPATHLEN];
3140 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3141 (void) strlcat(name, "@", sizeof (name));
3142 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3143 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3146 * NB: this is simply a best-effort. We don't want to
3147 * return an error, because then we wouldn't visit all
3152 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3160 * Takes a snapshot of the given dataset.
3163 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3167 char parent[ZFS_MAXNAMELEN];
3169 zfs_cmd_t zc = { 0 };
3173 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3174 "cannot snapshot '%s'"), path);
3176 /* validate the target name */
3177 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3178 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3181 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3182 props, B_FALSE, NULL, errbuf)) == NULL)
3185 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3193 /* make sure the parent exists and is of the appropriate type */
3194 delim = strchr(path, '@');
3195 (void) strncpy(parent, path, delim - path);
3196 parent[delim - path] = '\0';
3198 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3199 ZFS_TYPE_VOLUME)) == NULL) {
3200 zcmd_free_nvlists(&zc);
3204 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3205 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3206 if (ZFS_IS_VOLUME(zhp))
3207 zc.zc_objset_type = DMU_OST_ZVOL;
3209 zc.zc_objset_type = DMU_OST_ZFS;
3210 zc.zc_cookie = recursive;
3211 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3213 zcmd_free_nvlists(&zc);
3216 * if it was recursive, the one that actually failed will be in
3220 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3221 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3223 if (ret == 0 && recursive) {
3224 struct createdata cd;
3226 cd.cd_snapname = delim + 1;
3227 cd.cd_ifexists = B_FALSE;
3228 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3230 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3231 ret = zvol_create_link(zhp->zfs_hdl, path);
3233 (void) zfs_standard_error(hdl, errno,
3234 dgettext(TEXT_DOMAIN,
3235 "Volume successfully snapshotted, but device links "
3236 "were not created"));
3243 (void) zfs_standard_error(hdl, errno, errbuf);
3251 * Destroy any more recent snapshots. We invoke this callback on any dependents
3252 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3253 * is a dependent and we should just destroy it without checking the transaction
3256 typedef struct rollback_data {
3257 const char *cb_target; /* the snapshot */
3258 uint64_t cb_create; /* creation time reference */
3260 boolean_t cb_dependent;
3265 rollback_destroy(zfs_handle_t *zhp, void *data)
3267 rollback_data_t *cbp = data;
3269 if (!cbp->cb_dependent) {
3270 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3271 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3272 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3276 cbp->cb_dependent = B_TRUE;
3277 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3278 rollback_destroy, cbp);
3279 cbp->cb_dependent = B_FALSE;
3281 logstr = zhp->zfs_hdl->libzfs_log_str;
3282 zhp->zfs_hdl->libzfs_log_str = NULL;
3283 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3284 zhp->zfs_hdl->libzfs_log_str = logstr;
3287 /* We must destroy this clone; first unmount it */
3288 prop_changelist_t *clp;
3290 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3291 cbp->cb_force ? MS_FORCE: 0);
3292 if (clp == NULL || changelist_prefix(clp) != 0) {
3293 cbp->cb_error = B_TRUE;
3297 if (zfs_destroy(zhp, B_FALSE) != 0)
3298 cbp->cb_error = B_TRUE;
3300 changelist_remove(clp, zhp->zfs_name);
3301 (void) changelist_postfix(clp);
3302 changelist_free(clp);
3310 * Given a dataset, rollback to a specific snapshot, discarding any
3311 * data changes since then and making it the active dataset.
3313 * Any snapshots more recent than the target are destroyed, along with
3317 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3319 rollback_data_t cb = { 0 };
3321 zfs_cmd_t zc = { 0 };
3322 boolean_t restore_resv = 0;
3323 uint64_t old_volsize, new_volsize;
3324 zfs_prop_t resv_prop;
3326 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3327 zhp->zfs_type == ZFS_TYPE_VOLUME);
3330 * Destroy all recent snapshots and its dependends.
3332 cb.cb_force = force;
3333 cb.cb_target = snap->zfs_name;
3334 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3335 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3341 * Now that we have verified that the snapshot is the latest,
3342 * rollback to the given snapshot.
3345 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3346 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3348 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3350 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3352 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3355 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3357 if (ZFS_IS_VOLUME(zhp))
3358 zc.zc_objset_type = DMU_OST_ZVOL;
3360 zc.zc_objset_type = DMU_OST_ZFS;
3363 * We rely on zfs_iter_children() to verify that there are no
3364 * newer snapshots for the given dataset. Therefore, we can
3365 * simply pass the name on to the ioctl() call. There is still
3366 * an unlikely race condition where the user has taken a
3367 * snapshot since we verified that this was the most recent.
3370 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3371 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3372 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3378 * For volumes, if the pre-rollback volsize matched the pre-
3379 * rollback reservation and the volsize has changed then set
3380 * the reservation property to the post-rollback volsize.
3381 * Make a new handle since the rollback closed the dataset.
3383 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3384 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3385 if (err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name)) {
3390 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3391 if (old_volsize != new_volsize)
3392 err = zfs_prop_set_int(zhp, resv_prop,
3401 * Iterate over all dependents for a given dataset. This includes both
3402 * hierarchical dependents (children) and data dependents (snapshots and
3403 * clones). The bulk of the processing occurs in get_dependents() in
3407 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3408 zfs_iter_f func, void *data)
3413 zfs_handle_t *child;
3416 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3417 &dependents, &count) != 0)
3420 for (i = 0; i < count; i++) {
3421 if ((child = make_dataset_handle(zhp->zfs_hdl,
3422 dependents[i])) == NULL)
3425 if ((ret = func(child, data)) != 0)
3429 for (i = 0; i < count; i++)
3430 free(dependents[i]);
3437 * Renames the given dataset.
3440 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3443 zfs_cmd_t zc = { 0 };
3445 prop_changelist_t *cl = NULL;
3446 zfs_handle_t *zhrp = NULL;
3447 char *parentname = NULL;
3448 char parent[ZFS_MAXNAMELEN];
3449 libzfs_handle_t *hdl = zhp->zfs_hdl;
3452 /* if we have the same exact name, just return success */
3453 if (strcmp(zhp->zfs_name, target) == 0)
3456 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3457 "cannot rename to '%s'"), target);
3460 * Make sure the target name is valid
3462 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3463 if ((strchr(target, '@') == NULL) ||
3466 * Snapshot target name is abbreviated,
3467 * reconstruct full dataset name
3469 (void) strlcpy(parent, zhp->zfs_name,
3471 delim = strchr(parent, '@');
3472 if (strchr(target, '@') == NULL)
3476 (void) strlcat(parent, target, sizeof (parent));
3480 * Make sure we're renaming within the same dataset.
3482 delim = strchr(target, '@');
3483 if (strncmp(zhp->zfs_name, target, delim - target)
3484 != 0 || zhp->zfs_name[delim - target] != '@') {
3485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3486 "snapshots must be part of same "
3488 return (zfs_error(hdl, EZFS_CROSSTARGET,
3492 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3493 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3497 "recursive rename must be a snapshot"));
3498 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3501 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3502 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3505 /* validate parents */
3506 if (check_parents(hdl, target, &unused, B_FALSE, NULL) != 0)
3509 (void) parent_name(target, parent, sizeof (parent));
3511 /* make sure we're in the same pool */
3512 verify((delim = strchr(target, '/')) != NULL);
3513 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3514 zhp->zfs_name[delim - target] != '/') {
3515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3516 "datasets must be within same pool"));
3517 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3520 /* new name cannot be a child of the current dataset name */
3521 if (strncmp(parent, zhp->zfs_name,
3522 strlen(zhp->zfs_name)) == 0) {
3523 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3524 "New dataset name cannot be a descendent of "
3525 "current dataset name"));
3526 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3530 (void) snprintf(errbuf, sizeof (errbuf),
3531 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3533 if (getzoneid() == GLOBAL_ZONEID &&
3534 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3536 "dataset is used in a non-global zone"));
3537 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3541 struct destroydata dd;
3543 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3544 if (parentname == NULL) {
3548 delim = strchr(parentname, '@');
3550 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3556 dd.snapname = delim + 1;
3557 dd.gotone = B_FALSE;
3558 dd.closezhp = B_TRUE;
3560 /* We remove any zvol links prior to renaming them */
3561 ret = zfs_iter_filesystems(zhrp, zfs_remove_link_cb, &dd);
3566 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3569 if (changelist_haszonedchild(cl)) {
3570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3571 "child dataset with inherited mountpoint is used "
3572 "in a non-global zone"));
3573 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3577 if ((ret = changelist_prefix(cl)) != 0)
3581 if (ZFS_IS_VOLUME(zhp))
3582 zc.zc_objset_type = DMU_OST_ZVOL;
3584 zc.zc_objset_type = DMU_OST_ZFS;
3586 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3587 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3589 zc.zc_cookie = recursive;
3591 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3593 * if it was recursive, the one that actually failed will
3596 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3597 "cannot rename '%s'"), zc.zc_name);
3599 if (recursive && errno == EEXIST) {
3600 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3601 "a child dataset already has a snapshot "
3602 "with the new name"));
3603 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3605 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3609 * On failure, we still want to remount any filesystems that
3610 * were previously mounted, so we don't alter the system state.
3613 struct createdata cd;
3615 /* only create links for datasets that had existed */
3616 cd.cd_snapname = delim + 1;
3617 cd.cd_ifexists = B_TRUE;
3618 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3621 (void) changelist_postfix(cl);
3625 struct createdata cd;
3627 /* only create links for datasets that had existed */
3628 cd.cd_snapname = strchr(target, '@') + 1;
3629 cd.cd_ifexists = B_TRUE;
3630 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3633 changelist_rename(cl, zfs_get_name(zhp), target);
3634 ret = changelist_postfix(cl);
3646 changelist_free(cl);
3652 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3653 * poke devfsadm to create the /dev link, and then wait for the link to appear.
3656 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3658 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3662 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3664 zfs_cmd_t zc = { 0 };
3665 di_devlink_handle_t dhdl;
3666 priv_set_t *priv_effective;
3669 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3672 * Issue the appropriate ioctl.
3674 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3678 * Silently ignore the case where the link already
3679 * exists. This allows 'zfs volinit' to be run multiple
3680 * times without errors.
3686 * Dataset does not exist in the kernel. If we
3687 * don't care (see zfs_rename), then ignore the
3697 return (zfs_standard_error_fmt(hdl, errno,
3698 dgettext(TEXT_DOMAIN, "cannot create device links "
3699 "for '%s'"), dataset));
3704 * If privileged call devfsadm and wait for the links to
3706 * Otherwise, print out an informational message.
3709 priv_effective = priv_allocset();
3710 (void) getppriv(PRIV_EFFECTIVE, priv_effective);
3711 privileged = (priv_isfullset(priv_effective) == B_TRUE);
3712 priv_freeset(priv_effective);
3715 if ((dhdl = di_devlink_init(ZFS_DRIVER,
3716 DI_MAKE_LINK)) == NULL) {
3717 zfs_error_aux(hdl, strerror(errno));
3718 (void) zfs_error_fmt(hdl, errno,
3719 dgettext(TEXT_DOMAIN, "cannot create device links "
3720 "for '%s'"), dataset);
3721 (void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
3724 (void) di_devlink_fini(&dhdl);
3727 char pathname[MAXPATHLEN];
3728 struct stat64 statbuf;
3734 * This is the poor mans way of waiting for the link
3735 * to show up. If after 10 seconds we still don't
3736 * have it, then print out a message.
3738 (void) snprintf(pathname, sizeof (pathname), "/dev/zvol/dsk/%s",
3741 for (i = 0; i != MAX_WAIT; i++) {
3742 if (stat64(pathname, &statbuf) == 0)
3747 (void) printf(gettext("%s may not be immediately "
3748 "available\n"), pathname);
3755 * Remove a minor node for the given zvol and the associated /dev links.
3758 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3760 zfs_cmd_t zc = { 0 };
3762 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3764 if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) {
3768 * Silently ignore the case where the link no longer
3769 * exists, so that 'zfs volfini' can be run multiple
3770 * times without errors.
3775 return (zfs_standard_error_fmt(hdl, errno,
3776 dgettext(TEXT_DOMAIN, "cannot remove device "
3777 "links for '%s'"), dataset));
3785 zfs_get_user_props(zfs_handle_t *zhp)
3787 return (zhp->zfs_user_props);
3791 * This function is used by 'zfs list' to determine the exact set of columns to
3792 * display, and their maximum widths. This does two main things:
3794 * - If this is a list of all properties, then expand the list to include
3795 * all native properties, and set a flag so that for each dataset we look
3796 * for new unique user properties and add them to the list.
3798 * - For non fixed-width properties, keep track of the maximum width seen
3799 * so that we can size the column appropriately.
3802 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp)
3804 libzfs_handle_t *hdl = zhp->zfs_hdl;
3805 zprop_list_t *entry;
3806 zprop_list_t **last, **start;
3807 nvlist_t *userprops, *propval;
3810 char buf[ZFS_MAXPROPLEN];
3812 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3815 userprops = zfs_get_user_props(zhp);
3818 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3820 * Go through and add any user properties as necessary. We
3821 * start by incrementing our list pointer to the first
3822 * non-native property.
3825 while (*start != NULL) {
3826 if ((*start)->pl_prop == ZPROP_INVAL)
3828 start = &(*start)->pl_next;
3832 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3834 * See if we've already found this property in our list.
3836 for (last = start; *last != NULL;
3837 last = &(*last)->pl_next) {
3838 if (strcmp((*last)->pl_user_prop,
3839 nvpair_name(elem)) == 0)
3843 if (*last == NULL) {
3844 if ((entry = zfs_alloc(hdl,
3845 sizeof (zprop_list_t))) == NULL ||
3846 ((entry->pl_user_prop = zfs_strdup(hdl,
3847 nvpair_name(elem)))) == NULL) {
3852 entry->pl_prop = ZPROP_INVAL;
3853 entry->pl_width = strlen(nvpair_name(elem));
3854 entry->pl_all = B_TRUE;
3861 * Now go through and check the width of any non-fixed columns
3863 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3864 if (entry->pl_fixed)
3867 if (entry->pl_prop != ZPROP_INVAL) {
3868 if (zfs_prop_get(zhp, entry->pl_prop,
3869 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3870 if (strlen(buf) > entry->pl_width)
3871 entry->pl_width = strlen(buf);
3873 } else if (nvlist_lookup_nvlist(userprops,
3874 entry->pl_user_prop, &propval) == 0) {
3875 verify(nvlist_lookup_string(propval,
3876 ZPROP_VALUE, &strval) == 0);
3877 if (strlen(strval) > entry->pl_width)
3878 entry->pl_width = strlen(strval);
3886 zfs_iscsi_perm_check(libzfs_handle_t *hdl, char *dataset, ucred_t *cred)
3888 zfs_cmd_t zc = { 0 };
3892 const gid_t *groups;
3896 if (nvlist_alloc(&nvp, NV_UNIQUE_NAME, 0) != 0)
3897 return (no_memory(hdl));
3899 uid = ucred_geteuid(cred);
3900 gid = ucred_getegid(cred);
3901 group_cnt = ucred_getgroups(cred, &groups);
3903 if (uid == (uid_t)-1 || gid == (uid_t)-1 || group_cnt == (uid_t)-1)
3906 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_UID, uid) != 0) {
3911 if (nvlist_add_uint32(nvp, ZFS_DELEG_PERM_GID, gid) != 0) {
3916 if (nvlist_add_uint32_array(nvp,
3917 ZFS_DELEG_PERM_GROUPS, (uint32_t *)groups, group_cnt) != 0) {
3921 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3923 if (zcmd_write_src_nvlist(hdl, &zc, nvp))
3926 error = ioctl(hdl->libzfs_fd, ZFS_IOC_ISCSI_PERM_CHECK, &zc);
3932 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3933 char *resource, void *export, void *sharetab,
3934 int sharemax, zfs_share_op_t operation)
3936 zfs_cmd_t zc = { 0 };
3939 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3940 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3942 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3943 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3944 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3945 zc.zc_share.z_sharetype = operation;
3946 zc.zc_share.z_sharemax = sharemax;
3947 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3952 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3957 * Keep a reference to the props-table against which we prune the
3960 zhp->zfs_props_table = props;
3962 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3965 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3966 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3969 * We leave user:props in the nvlist, so there will be
3970 * some ZPROP_INVAL. To be extra safe, don't prune
3973 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3974 (void) nvlist_remove(zhp->zfs_props,
3975 nvpair_name(curr), nvpair_type(curr));
3981 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3982 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3984 zfs_cmd_t zc = { 0 };
3985 nvlist_t *nvlist = NULL;
3988 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3989 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3990 zc.zc_cookie = (uint64_t)cmd;
3992 if (cmd == ZFS_SMB_ACL_RENAME) {
3993 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3994 (void) no_memory(hdl);
4000 case ZFS_SMB_ACL_ADD:
4001 case ZFS_SMB_ACL_REMOVE:
4002 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4004 case ZFS_SMB_ACL_RENAME:
4005 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4007 (void) no_memory(hdl);
4010 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4012 (void) no_memory(hdl);
4015 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4016 nvlist_free(nvlist);
4020 case ZFS_SMB_ACL_PURGE:
4025 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4027 nvlist_free(nvlist);
4032 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4033 char *path, char *resource)
4035 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4040 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4041 char *path, char *resource)
4043 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4048 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4050 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4055 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4056 char *oldname, char *newname)
4058 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4063 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4064 zfs_userspace_cb_t func, void *arg)
4066 zfs_cmd_t zc = { 0 };
4068 zfs_useracct_t buf[100];
4070 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4072 zc.zc_objset_type = type;
4073 zc.zc_nvlist_dst = (uintptr_t)buf;
4077 zfs_useracct_t *zua = buf;
4079 zc.zc_nvlist_dst_size = sizeof (buf);
4080 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4081 ZFS_IOC_USERSPACE_MANY, &zc);
4082 if (error || zc.zc_nvlist_dst_size == 0)
4085 while (zc.zc_nvlist_dst_size > 0) {
4086 error = func(arg, zua->zu_domain, zua->zu_rid,
4091 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4099 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4100 boolean_t recursive)
4102 zfs_cmd_t zc = { 0 };
4103 libzfs_handle_t *hdl = zhp->zfs_hdl;
4105 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4106 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4107 (void) strlcpy(zc.zc_string, tag, sizeof (zc.zc_string));
4108 zc.zc_cookie = recursive;
4110 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4111 char errbuf[ZFS_MAXNAMELEN+32];
4114 * if it was recursive, the one that actually failed will be in
4117 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4118 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4122 "pool must be upgraded"));
4123 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4125 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4127 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4129 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4137 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4138 boolean_t recursive)
4140 zfs_cmd_t zc = { 0 };
4141 libzfs_handle_t *hdl = zhp->zfs_hdl;
4143 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4144 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4145 (void) strlcpy(zc.zc_string, tag, sizeof (zc.zc_string));
4146 zc.zc_cookie = recursive;
4148 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4149 char errbuf[ZFS_MAXNAMELEN+32];
4152 * if it was recursive, the one that actually failed will be in
4155 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4156 "cannot release '%s@%s'"), zc.zc_name, snapname);
4159 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4162 "pool must be upgraded"));
4163 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4165 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4167 return (zfs_standard_error_fmt(hdl, errno, errbuf));