4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2010 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
40 #include <sys/mntent.h>
41 #include <sys/mount.h>
50 #include <directory.h>
51 #endif /* HAVE_IDMAP */
53 #include <sys/dnode.h>
58 #include "zfs_namecheck.h"
60 #include "libzfs_impl.h"
61 #include "zfs_deleg.h"
63 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
64 static int userquota_propname_decode(const char *propname, boolean_t zoned,
65 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
68 * Given a single type (not a mask of types), return the type in a human
72 zfs_type_to_name(zfs_type_t type)
75 case ZFS_TYPE_FILESYSTEM:
76 return (dgettext(TEXT_DOMAIN, "filesystem"));
77 case ZFS_TYPE_SNAPSHOT:
78 return (dgettext(TEXT_DOMAIN, "snapshot"));
80 return (dgettext(TEXT_DOMAIN, "volume"));
89 * Validate a ZFS path. This is used even before trying to open the dataset, to
90 * provide a more meaningful error message. We call zfs_error_aux() to
91 * explain exactly why the name was not valid.
94 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
100 (void) zfs_prop_get_table();
101 if (dataset_namecheck(path, &why, &what) != 0) {
104 case NAME_ERR_TOOLONG:
105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106 "name is too long"));
109 case NAME_ERR_LEADING_SLASH:
110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
111 "leading slash in name"));
114 case NAME_ERR_EMPTY_COMPONENT:
115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116 "empty component in name"));
119 case NAME_ERR_TRAILING_SLASH:
120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121 "trailing slash in name"));
124 case NAME_ERR_INVALCHAR:
126 dgettext(TEXT_DOMAIN, "invalid character "
127 "'%c' in name"), what);
130 case NAME_ERR_MULTIPLE_AT:
131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132 "multiple '@' delimiters in name"));
135 case NAME_ERR_NOLETTER:
136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
137 "pool doesn't begin with a letter"));
140 case NAME_ERR_RESERVED:
141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
142 "name is reserved"));
145 case NAME_ERR_DISKLIKE:
146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
147 "reserved disk name"));
157 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "snapshot delimiter '@' in filesystem name"));
164 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
167 "missing '@' delimiter in snapshot name"));
171 if (modifying && strchr(path, '%') != NULL) {
173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174 "invalid character %c in name"), '%');
182 zfs_name_valid(const char *name, zfs_type_t type)
184 if (type == ZFS_TYPE_POOL)
185 return (zpool_name_valid(NULL, B_FALSE, name));
186 return (zfs_validate_name(NULL, name, type, B_FALSE));
190 * This function takes the raw DSL properties, and filters out the user-defined
191 * properties into a separate nvlist.
194 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
196 libzfs_handle_t *hdl = zhp->zfs_hdl;
201 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
202 (void) no_memory(hdl);
207 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
208 if (!zfs_prop_user(nvpair_name(elem)))
211 verify(nvpair_value_nvlist(elem, &propval) == 0);
212 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
214 (void) no_memory(hdl);
222 static zpool_handle_t *
223 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
225 libzfs_handle_t *hdl = zhp->zfs_hdl;
228 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
229 if (hdl->libzfs_pool_handles != NULL)
230 zph->zpool_next = hdl->libzfs_pool_handles;
231 hdl->libzfs_pool_handles = zph;
236 static zpool_handle_t *
237 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
239 libzfs_handle_t *hdl = zhp->zfs_hdl;
240 zpool_handle_t *zph = hdl->libzfs_pool_handles;
242 while ((zph != NULL) &&
243 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
244 zph = zph->zpool_next;
249 * Returns a handle to the pool that contains the provided dataset.
250 * If a handle to that pool already exists then that handle is returned.
251 * Otherwise, a new handle is created and added to the list of handles.
253 static zpool_handle_t *
254 zpool_handle(zfs_handle_t *zhp)
260 len = strcspn(zhp->zfs_name, "/@") + 1;
261 pool_name = zfs_alloc(zhp->zfs_hdl, len);
262 (void) strlcpy(pool_name, zhp->zfs_name, len);
264 zph = zpool_find_handle(zhp, pool_name, len);
266 zph = zpool_add_handle(zhp, pool_name);
273 zpool_free_handles(libzfs_handle_t *hdl)
275 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
277 while (zph != NULL) {
278 next = zph->zpool_next;
282 hdl->libzfs_pool_handles = NULL;
286 * Utility function to gather stats (objset and zpl) for the given object.
289 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
291 libzfs_handle_t *hdl = zhp->zfs_hdl;
293 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
295 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
296 if (errno == ENOMEM) {
297 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
308 * Utility function to get the received properties of the given object.
311 get_recvd_props_ioctl(zfs_handle_t *zhp)
313 libzfs_handle_t *hdl = zhp->zfs_hdl;
314 nvlist_t *recvdprops;
315 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
318 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
321 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
323 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
324 if (errno == ENOMEM) {
325 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
329 zcmd_free_nvlists(&zc);
334 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
335 zcmd_free_nvlists(&zc);
339 nvlist_free(zhp->zfs_recvd_props);
340 zhp->zfs_recvd_props = recvdprops;
346 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
348 nvlist_t *allprops, *userprops;
350 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
352 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
357 * XXX Why do we store the user props separately, in addition to
358 * storing them in zfs_props?
360 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
361 nvlist_free(allprops);
365 nvlist_free(zhp->zfs_props);
366 nvlist_free(zhp->zfs_user_props);
368 zhp->zfs_props = allprops;
369 zhp->zfs_user_props = userprops;
375 get_stats(zfs_handle_t *zhp)
378 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
380 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
382 if (get_stats_ioctl(zhp, &zc) != 0)
384 else if (put_stats_zhdl(zhp, &zc) != 0)
386 zcmd_free_nvlists(&zc);
391 * Refresh the properties currently stored in the handle.
394 zfs_refresh_properties(zfs_handle_t *zhp)
396 (void) get_stats(zhp);
400 * Makes a handle from the given dataset name. Used by zfs_open() and
401 * zfs_iter_* to create child handles on the fly.
404 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
406 if (put_stats_zhdl(zhp, zc) != 0)
410 * We've managed to open the dataset and gather statistics. Determine
411 * the high-level type.
413 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
414 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
415 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
416 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
417 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
418 return (-1); /* zpios' and other testing datasets are
419 of this type, ignore if encountered */
423 if (zhp->zfs_dmustats.dds_is_snapshot)
424 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
425 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
426 zhp->zfs_type = ZFS_TYPE_VOLUME;
427 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
428 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
430 abort(); /* we should never see any other types */
432 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
439 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
441 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
443 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
449 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
450 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
454 if (get_stats_ioctl(zhp, &zc) == -1) {
455 zcmd_free_nvlists(&zc);
459 if (make_dataset_handle_common(zhp, &zc) == -1) {
463 zcmd_free_nvlists(&zc);
468 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
470 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
476 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
477 if (make_dataset_handle_common(zhp, zc) == -1) {
485 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
487 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
492 zhp->zfs_hdl = pzhp->zfs_hdl;
493 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
494 zhp->zfs_head_type = pzhp->zfs_type;
495 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
496 zhp->zpool_hdl = zpool_handle(zhp);
502 zfs_handle_dup(zfs_handle_t *zhp_orig)
504 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
509 zhp->zfs_hdl = zhp_orig->zfs_hdl;
510 zhp->zpool_hdl = zhp_orig->zpool_hdl;
511 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
512 sizeof (zhp->zfs_name));
513 zhp->zfs_type = zhp_orig->zfs_type;
514 zhp->zfs_head_type = zhp_orig->zfs_head_type;
515 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
516 if (zhp_orig->zfs_props != NULL) {
517 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
518 (void) no_memory(zhp->zfs_hdl);
523 if (zhp_orig->zfs_user_props != NULL) {
524 if (nvlist_dup(zhp_orig->zfs_user_props,
525 &zhp->zfs_user_props, 0) != 0) {
526 (void) no_memory(zhp->zfs_hdl);
531 if (zhp_orig->zfs_recvd_props != NULL) {
532 if (nvlist_dup(zhp_orig->zfs_recvd_props,
533 &zhp->zfs_recvd_props, 0)) {
534 (void) no_memory(zhp->zfs_hdl);
539 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
540 if (zhp_orig->zfs_mntopts != NULL) {
541 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
542 zhp_orig->zfs_mntopts);
544 zhp->zfs_props_table = zhp_orig->zfs_props_table;
549 * Opens the given snapshot, filesystem, or volume. The 'types'
550 * argument is a mask of acceptable types. The function will print an
551 * appropriate error message and return NULL if it can't be opened.
554 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
559 (void) snprintf(errbuf, sizeof (errbuf),
560 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
563 * Validate the name before we even try to open it.
565 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
566 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
567 "invalid dataset name"));
568 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
573 * Try to get stats for the dataset, which will tell us if it exists.
576 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
577 (void) zfs_standard_error(hdl, errno, errbuf);
581 if (!(types & zhp->zfs_type)) {
582 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
591 * Release a ZFS handle. Nothing to do but free the associated memory.
594 zfs_close(zfs_handle_t *zhp)
596 if (zhp->zfs_mntopts)
597 free(zhp->zfs_mntopts);
598 nvlist_free(zhp->zfs_props);
599 nvlist_free(zhp->zfs_user_props);
600 nvlist_free(zhp->zfs_recvd_props);
604 typedef struct mnttab_node {
605 struct mnttab mtn_mt;
610 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
612 const mnttab_node_t *mtn1 = arg1;
613 const mnttab_node_t *mtn2 = arg2;
616 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
620 return (rv > 0 ? 1 : -1);
624 libzfs_mnttab_init(libzfs_handle_t *hdl)
626 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
627 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
628 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
632 libzfs_mnttab_update(libzfs_handle_t *hdl)
636 rewind(hdl->libzfs_mnttab);
637 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
640 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
642 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
643 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
644 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
645 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
646 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
647 avl_add(&hdl->libzfs_mnttab_cache, mtn);
652 libzfs_mnttab_fini(libzfs_handle_t *hdl)
657 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
658 free(mtn->mtn_mt.mnt_special);
659 free(mtn->mtn_mt.mnt_mountp);
660 free(mtn->mtn_mt.mnt_fstype);
661 free(mtn->mtn_mt.mnt_mntopts);
664 avl_destroy(&hdl->libzfs_mnttab_cache);
668 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
670 hdl->libzfs_mnttab_enable = enable;
674 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
675 struct mnttab *entry)
680 if (!hdl->libzfs_mnttab_enable) {
681 struct mnttab srch = { 0 };
683 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
684 libzfs_mnttab_fini(hdl);
685 rewind(hdl->libzfs_mnttab);
686 srch.mnt_special = (char *)fsname;
687 srch.mnt_fstype = MNTTYPE_ZFS;
688 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
694 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
695 libzfs_mnttab_update(hdl);
697 find.mtn_mt.mnt_special = (char *)fsname;
698 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
700 *entry = mtn->mtn_mt;
707 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
708 const char *mountp, const char *mntopts)
712 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
714 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
715 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
716 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
717 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
718 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
719 avl_add(&hdl->libzfs_mnttab_cache, mtn);
723 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
728 find.mtn_mt.mnt_special = (char *)fsname;
729 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
730 avl_remove(&hdl->libzfs_mnttab_cache, ret);
731 free(ret->mtn_mt.mnt_special);
732 free(ret->mtn_mt.mnt_mountp);
733 free(ret->mtn_mt.mnt_fstype);
734 free(ret->mtn_mt.mnt_mntopts);
740 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
742 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
744 if (zpool_handle == NULL)
747 *spa_version = zpool_get_prop_int(zpool_handle,
748 ZPOOL_PROP_VERSION, NULL);
753 * The choice of reservation property depends on the SPA version.
756 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
760 if (zfs_spa_version(zhp, &spa_version) < 0)
763 if (spa_version >= SPA_VERSION_REFRESERVATION)
764 *resv_prop = ZFS_PROP_REFRESERVATION;
766 *resv_prop = ZFS_PROP_RESERVATION;
772 * Given an nvlist of properties to set, validates that they are correct, and
773 * parses any numeric properties (index, boolean, etc) if they are specified as
777 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
778 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
785 int chosen_normal = -1;
788 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
789 (void) no_memory(hdl);
794 * Make sure this property is valid and applies to this type.
798 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
799 const char *propname = nvpair_name(elem);
801 prop = zfs_name_to_prop(propname);
802 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
804 * This is a user property: make sure it's a
805 * string, and that it's less than ZAP_MAXNAMELEN.
807 if (nvpair_type(elem) != DATA_TYPE_STRING) {
808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
809 "'%s' must be a string"), propname);
810 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
814 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
816 "property name '%s' is too long"),
818 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
822 (void) nvpair_value_string(elem, &strval);
823 if (nvlist_add_string(ret, propname, strval) != 0) {
824 (void) no_memory(hdl);
831 * Currently, only user properties can be modified on
834 if (type == ZFS_TYPE_SNAPSHOT) {
835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
836 "this property can not be modified for snapshots"));
837 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
841 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
842 zfs_userquota_prop_t uqtype;
843 char newpropname[128];
848 if (userquota_propname_decode(propname, zoned,
849 &uqtype, domain, sizeof (domain), &rid) != 0) {
851 dgettext(TEXT_DOMAIN,
852 "'%s' has an invalid user/group name"),
854 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
858 if (uqtype != ZFS_PROP_USERQUOTA &&
859 uqtype != ZFS_PROP_GROUPQUOTA) {
861 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
863 (void) zfs_error(hdl, EZFS_PROPREADONLY,
868 if (nvpair_type(elem) == DATA_TYPE_STRING) {
869 (void) nvpair_value_string(elem, &strval);
870 if (strcmp(strval, "none") == 0) {
872 } else if (zfs_nicestrtonum(hdl,
873 strval, &intval) != 0) {
874 (void) zfs_error(hdl,
875 EZFS_BADPROP, errbuf);
878 } else if (nvpair_type(elem) ==
880 (void) nvpair_value_uint64(elem, &intval);
882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
883 "use 'none' to disable "
884 "userquota/groupquota"));
888 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
889 "'%s' must be a number"), propname);
890 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
895 * Encode the prop name as
896 * userquota@<hex-rid>-domain, to make it easy
897 * for the kernel to decode.
899 (void) snprintf(newpropname, sizeof (newpropname),
900 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
901 (longlong_t)rid, domain);
905 if (nvlist_add_uint64_array(ret, newpropname,
907 (void) no_memory(hdl);
911 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
915 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
919 if (prop == ZPROP_INVAL) {
920 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
921 "invalid property '%s'"), propname);
922 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
926 if (!zfs_prop_valid_for_type(prop, type)) {
928 dgettext(TEXT_DOMAIN, "'%s' does not "
929 "apply to datasets of this type"), propname);
930 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
934 if (zfs_prop_readonly(prop) &&
935 (!zfs_prop_setonce(prop) || zhp != NULL)) {
937 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
939 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
943 if (zprop_parse_value(hdl, elem, prop, type, ret,
944 &strval, &intval, errbuf) != 0)
948 * Perform some additional checks for specific properties.
951 case ZFS_PROP_VERSION:
957 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
958 if (intval < version) {
959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
960 "Can not downgrade; already at version %u"),
962 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
968 case ZFS_PROP_RECORDSIZE:
969 case ZFS_PROP_VOLBLOCKSIZE:
970 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
971 if (intval < SPA_MINBLOCKSIZE ||
972 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
974 "'%s' must be power of 2 from %u "
976 (uint_t)SPA_MINBLOCKSIZE,
977 (uint_t)SPA_MAXBLOCKSIZE >> 10);
978 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
983 case ZFS_PROP_MLSLABEL:
987 * Verify the mlslabel string and convert to
988 * internal hex label string.
992 char *hex = NULL; /* internal label string */
994 /* Default value is already OK. */
995 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
998 /* Verify the label can be converted to binary form */
999 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1000 (str_to_label(strval, &new_sl, MAC_LABEL,
1001 L_NO_CORRECTION, NULL) == -1)) {
1005 /* Now translate to hex internal label string */
1006 if (label_to_str(new_sl, &hex, M_INTERNAL,
1012 m_label_free(new_sl);
1014 /* If string is already in internal form, we're done. */
1015 if (strcmp(strval, hex) == 0) {
1020 /* Replace the label string with the internal form. */
1021 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1023 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031 "invalid mlslabel '%s'"), strval);
1032 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1033 m_label_free(new_sl); /* OK if null */
1036 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1037 "mlslabels are unsupported"));
1038 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1040 #endif /* HAVE_MLSLABEL */
1043 case ZFS_PROP_MOUNTPOINT:
1045 namecheck_err_t why;
1047 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1048 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1051 if (mountpoint_namecheck(strval, &why)) {
1053 case NAME_ERR_LEADING_SLASH:
1055 dgettext(TEXT_DOMAIN,
1056 "'%s' must be an absolute path, "
1057 "'none', or 'legacy'"), propname);
1059 case NAME_ERR_TOOLONG:
1061 dgettext(TEXT_DOMAIN,
1062 "component of '%s' is too long"),
1068 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1075 case ZFS_PROP_SHARESMB:
1076 case ZFS_PROP_SHARENFS:
1078 * For the mountpoint and sharenfs or sharesmb
1079 * properties, check if it can be set in a
1080 * global/non-global zone based on
1081 * the zoned property value:
1083 * global zone non-global zone
1084 * --------------------------------------------------
1085 * zoned=on mountpoint (no) mountpoint (yes)
1086 * sharenfs (no) sharenfs (no)
1087 * sharesmb (no) sharesmb (no)
1089 * zoned=off mountpoint (yes) N/A
1094 if (getzoneid() == GLOBAL_ZONEID) {
1095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1096 "'%s' cannot be set on "
1097 "dataset in a non-global zone"),
1099 (void) zfs_error(hdl, EZFS_ZONED,
1102 } else if (prop == ZFS_PROP_SHARENFS ||
1103 prop == ZFS_PROP_SHARESMB) {
1104 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1105 "'%s' cannot be set in "
1106 "a non-global zone"), propname);
1107 (void) zfs_error(hdl, EZFS_ZONED,
1111 } else if (getzoneid() != GLOBAL_ZONEID) {
1113 * If zoned property is 'off', this must be in
1114 * a global zone. If not, something is wrong.
1116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1117 "'%s' cannot be set while dataset "
1118 "'zoned' property is set"), propname);
1119 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1124 * At this point, it is legitimate to set the
1125 * property. Now we want to make sure that the
1126 * property value is valid if it is sharenfs.
1128 if ((prop == ZFS_PROP_SHARENFS ||
1129 prop == ZFS_PROP_SHARESMB) &&
1130 strcmp(strval, "on") != 0 &&
1131 strcmp(strval, "off") != 0) {
1132 zfs_share_proto_t proto;
1134 if (prop == ZFS_PROP_SHARESMB)
1140 * Must be an valid sharing protocol
1141 * option string so init the libshare
1142 * in order to enable the parser and
1143 * then parse the options. We use the
1144 * control API since we don't care about
1145 * the current configuration and don't
1146 * want the overhead of loading it
1147 * until we actually do something.
1150 if (zfs_init_libshare(hdl,
1151 SA_INIT_CONTROL_API) != SA_OK) {
1153 * An error occurred so we can't do
1156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1157 "'%s' cannot be set: problem "
1158 "in share initialization"),
1160 (void) zfs_error(hdl, EZFS_BADPROP,
1165 if (zfs_parse_options(strval, proto) != SA_OK) {
1167 * There was an error in parsing so
1168 * deal with it by issuing an error
1169 * message and leaving after
1170 * uninitializing the the libshare
1173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1174 "'%s' cannot be set to invalid "
1175 "options"), propname);
1176 (void) zfs_error(hdl, EZFS_BADPROP,
1178 zfs_uninit_libshare(hdl);
1181 zfs_uninit_libshare(hdl);
1185 case ZFS_PROP_UTF8ONLY:
1186 chosen_utf = (int)intval;
1188 case ZFS_PROP_NORMALIZE:
1189 chosen_normal = (int)intval;
1196 * For changes to existing volumes, we have some additional
1197 * checks to enforce.
1199 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1200 uint64_t volsize = zfs_prop_get_int(zhp,
1202 uint64_t blocksize = zfs_prop_get_int(zhp,
1203 ZFS_PROP_VOLBLOCKSIZE);
1207 case ZFS_PROP_RESERVATION:
1208 case ZFS_PROP_REFRESERVATION:
1209 if (intval > volsize) {
1210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1211 "'%s' is greater than current "
1212 "volume size"), propname);
1213 (void) zfs_error(hdl, EZFS_BADPROP,
1219 case ZFS_PROP_VOLSIZE:
1220 if (intval % blocksize != 0) {
1221 zfs_nicenum(blocksize, buf,
1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224 "'%s' must be a multiple of "
1225 "volume block size (%s)"),
1227 (void) zfs_error(hdl, EZFS_BADPROP,
1233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1234 "'%s' cannot be zero"),
1236 (void) zfs_error(hdl, EZFS_BADPROP,
1248 * If normalization was chosen, but no UTF8 choice was made,
1249 * enforce rejection of non-UTF8 names.
1251 * If normalization was chosen, but rejecting non-UTF8 names
1252 * was explicitly not chosen, it is an error.
1254 if (chosen_normal > 0 && chosen_utf < 0) {
1255 if (nvlist_add_uint64(ret,
1256 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1257 (void) no_memory(hdl);
1260 } else if (chosen_normal > 0 && chosen_utf == 0) {
1261 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1262 "'%s' must be set 'on' if normalization chosen"),
1263 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1264 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1275 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1277 uint64_t old_volsize;
1278 uint64_t new_volsize;
1279 uint64_t old_reservation;
1280 uint64_t new_reservation;
1281 zfs_prop_t resv_prop;
1284 * If this is an existing volume, and someone is setting the volsize,
1285 * make sure that it matches the reservation, or add it if necessary.
1287 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1288 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1290 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1291 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1292 old_reservation) || nvlist_lookup_uint64(nvl,
1293 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1296 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1299 new_reservation = zvol_volsize_to_reservation(new_volsize,
1301 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1302 new_reservation) != 0) {
1303 (void) no_memory(zhp->zfs_hdl);
1310 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1317 * For quotas and reservations, ENOSPC indicates
1318 * something different; setting a quota or reservation
1319 * doesn't use any disk space.
1322 case ZFS_PROP_QUOTA:
1323 case ZFS_PROP_REFQUOTA:
1324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1325 "size is less than current used or "
1327 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1330 case ZFS_PROP_RESERVATION:
1331 case ZFS_PROP_REFRESERVATION:
1332 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1333 "size is greater than available space"));
1334 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1338 (void) zfs_standard_error(hdl, err, errbuf);
1344 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1348 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1353 "pool and or dataset must be upgraded to set this "
1354 "property or value"));
1355 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1359 if (prop == ZFS_PROP_COMPRESSION) {
1360 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1361 "property setting is not allowed on "
1362 "bootable datasets"));
1363 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1365 (void) zfs_standard_error(hdl, err, errbuf);
1370 if (prop == ZPROP_INVAL) {
1371 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1373 (void) zfs_standard_error(hdl, err, errbuf);
1379 * This platform can't address a volume this big.
1382 if (prop == ZFS_PROP_VOLSIZE) {
1383 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1389 (void) zfs_standard_error(hdl, err, errbuf);
1394 zfs_is_namespace_prop(zfs_prop_t prop)
1398 case ZFS_PROP_ATIME:
1399 case ZFS_PROP_DEVICES:
1401 case ZFS_PROP_SETUID:
1402 case ZFS_PROP_READONLY:
1403 case ZFS_PROP_XATTR:
1404 case ZFS_PROP_NBMAND:
1413 * Given a property name and value, set the property for the given dataset.
1416 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1418 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1420 prop_changelist_t *cl = NULL;
1422 libzfs_handle_t *hdl = zhp->zfs_hdl;
1423 nvlist_t *nvl = NULL, *realprops;
1425 boolean_t do_prefix;
1429 (void) snprintf(errbuf, sizeof (errbuf),
1430 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1433 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1434 nvlist_add_string(nvl, propname, propval) != 0) {
1435 (void) no_memory(hdl);
1439 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1440 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1446 prop = zfs_name_to_prop(propname);
1448 if (prop == ZFS_PROP_VOLSIZE) {
1449 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1453 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1456 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1458 "child dataset with inherited mountpoint is used "
1459 "in a non-global zone"));
1460 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1465 * If the dataset's canmount property is being set to noauto,
1466 * then we want to prevent unmounting & remounting it.
1468 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1469 (zprop_string_to_index(prop, propval, &idx,
1470 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1472 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1476 * Execute the corresponding ioctl() to set this property.
1478 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1480 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1483 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1486 zfs_setprop_error(hdl, prop, errno, errbuf);
1487 if (added_resv && errno == ENOSPC) {
1488 /* clean up the volsize property we tried to set */
1489 uint64_t old_volsize = zfs_prop_get_int(zhp,
1492 zcmd_free_nvlists(&zc);
1493 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1495 if (nvlist_add_uint64(nvl,
1496 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1499 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1501 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1505 ret = changelist_postfix(cl);
1509 * Refresh the statistics so the new property
1510 * value is reflected.
1512 (void) get_stats(zhp);
1515 * Remount the filesystem to propagate the change
1516 * if one of the options handled by the generic
1517 * Linux namespace layer has been modified.
1519 if (zfs_is_namespace_prop(prop) &&
1520 zfs_is_mounted(zhp, NULL))
1521 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1527 zcmd_free_nvlists(&zc);
1529 changelist_free(cl);
1534 * Given a property, inherit the value from the parent dataset, or if received
1535 * is TRUE, revert to the received value, if any.
1538 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1540 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1542 prop_changelist_t *cl;
1543 libzfs_handle_t *hdl = zhp->zfs_hdl;
1547 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1548 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1550 zc.zc_cookie = received;
1551 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1553 * For user properties, the amount of work we have to do is very
1554 * small, so just do it here.
1556 if (!zfs_prop_user(propname)) {
1557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1558 "invalid property"));
1559 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1562 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1563 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1565 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1566 return (zfs_standard_error(hdl, errno, errbuf));
1572 * Verify that this property is inheritable.
1574 if (zfs_prop_readonly(prop))
1575 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1577 if (!zfs_prop_inheritable(prop) && !received)
1578 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1581 * Check to see if the value applies to this type
1583 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1584 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1587 * Normalize the name, to get rid of shorthand abbreviations.
1589 propname = zfs_prop_to_name(prop);
1590 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1591 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1593 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1594 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1596 "dataset is used in a non-global zone"));
1597 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1601 * Determine datasets which will be affected by this change, if any.
1603 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1606 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1607 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1608 "child dataset with inherited mountpoint is used "
1609 "in a non-global zone"));
1610 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1614 if ((ret = changelist_prefix(cl)) != 0)
1617 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1618 return (zfs_standard_error(hdl, errno, errbuf));
1621 if ((ret = changelist_postfix(cl)) != 0)
1625 * Refresh the statistics so the new property is reflected.
1627 (void) get_stats(zhp);
1631 changelist_free(cl);
1636 * True DSL properties are stored in an nvlist. The following two functions
1637 * extract them appropriately.
1640 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1646 if (nvlist_lookup_nvlist(zhp->zfs_props,
1647 zfs_prop_to_name(prop), &nv) == 0) {
1648 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1649 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1651 verify(!zhp->zfs_props_table ||
1652 zhp->zfs_props_table[prop] == B_TRUE);
1653 value = zfs_prop_default_numeric(prop);
1661 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1667 if (nvlist_lookup_nvlist(zhp->zfs_props,
1668 zfs_prop_to_name(prop), &nv) == 0) {
1669 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1670 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1672 verify(!zhp->zfs_props_table ||
1673 zhp->zfs_props_table[prop] == B_TRUE);
1674 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1683 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1685 return (zhp->zfs_props == zhp->zfs_recvd_props);
1689 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1691 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1692 zhp->zfs_props = zhp->zfs_recvd_props;
1696 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1698 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1703 * Internal function for getting a numeric property. Both zfs_prop_get() and
1704 * zfs_prop_get_int() are built using this interface.
1706 * Certain properties can be overridden using 'mount -o'. In this case, scan
1707 * the contents of the /etc/mtab entry, searching for the appropriate options.
1708 * If they differ from the on-disk values, report the current values and mark
1709 * the source "temporary".
1712 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1713 char **source, uint64_t *val)
1715 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1716 nvlist_t *zplprops = NULL;
1718 char *mntopt_on = NULL;
1719 char *mntopt_off = NULL;
1720 boolean_t received = zfs_is_recvd_props_mode(zhp);
1725 case ZFS_PROP_ATIME:
1726 mntopt_on = MNTOPT_ATIME;
1727 mntopt_off = MNTOPT_NOATIME;
1730 case ZFS_PROP_DEVICES:
1731 mntopt_on = MNTOPT_DEVICES;
1732 mntopt_off = MNTOPT_NODEVICES;
1736 mntopt_on = MNTOPT_EXEC;
1737 mntopt_off = MNTOPT_NOEXEC;
1740 case ZFS_PROP_READONLY:
1741 mntopt_on = MNTOPT_RO;
1742 mntopt_off = MNTOPT_RW;
1745 case ZFS_PROP_SETUID:
1746 mntopt_on = MNTOPT_SETUID;
1747 mntopt_off = MNTOPT_NOSETUID;
1750 case ZFS_PROP_XATTR:
1751 mntopt_on = MNTOPT_XATTR;
1752 mntopt_off = MNTOPT_NOXATTR;
1755 case ZFS_PROP_NBMAND:
1756 mntopt_on = MNTOPT_NBMAND;
1757 mntopt_off = MNTOPT_NONBMAND;
1764 * Because looking up the mount options is potentially expensive
1765 * (iterating over all of /etc/mtab), we defer its calculation until
1766 * we're looking up a property which requires its presence.
1768 if (!zhp->zfs_mntcheck &&
1769 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1770 libzfs_handle_t *hdl = zhp->zfs_hdl;
1771 struct mnttab entry;
1773 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1774 zhp->zfs_mntopts = zfs_strdup(hdl,
1776 if (zhp->zfs_mntopts == NULL)
1780 zhp->zfs_mntcheck = B_TRUE;
1783 if (zhp->zfs_mntopts == NULL)
1784 mnt.mnt_mntopts = "";
1786 mnt.mnt_mntopts = zhp->zfs_mntopts;
1789 case ZFS_PROP_ATIME:
1790 case ZFS_PROP_DEVICES:
1792 case ZFS_PROP_READONLY:
1793 case ZFS_PROP_SETUID:
1794 case ZFS_PROP_XATTR:
1795 case ZFS_PROP_NBMAND:
1796 *val = getprop_uint64(zhp, prop, source);
1801 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1804 *src = ZPROP_SRC_TEMPORARY;
1805 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1808 *src = ZPROP_SRC_TEMPORARY;
1812 case ZFS_PROP_CANMOUNT:
1813 case ZFS_PROP_VOLSIZE:
1814 case ZFS_PROP_QUOTA:
1815 case ZFS_PROP_REFQUOTA:
1816 case ZFS_PROP_RESERVATION:
1817 case ZFS_PROP_REFRESERVATION:
1818 *val = getprop_uint64(zhp, prop, source);
1820 if (*source == NULL) {
1821 /* not default, must be local */
1822 *source = zhp->zfs_name;
1826 case ZFS_PROP_MOUNTED:
1827 *val = (zhp->zfs_mntopts != NULL);
1830 case ZFS_PROP_NUMCLONES:
1831 *val = zhp->zfs_dmustats.dds_num_clones;
1834 case ZFS_PROP_VERSION:
1835 case ZFS_PROP_NORMALIZE:
1836 case ZFS_PROP_UTF8ONLY:
1838 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1839 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1841 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1842 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1843 zcmd_free_nvlists(&zc);
1846 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1847 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1849 zcmd_free_nvlists(&zc);
1853 nvlist_free(zplprops);
1854 zcmd_free_nvlists(&zc);
1858 switch (zfs_prop_get_type(prop)) {
1859 case PROP_TYPE_NUMBER:
1860 case PROP_TYPE_INDEX:
1861 *val = getprop_uint64(zhp, prop, source);
1863 * If we tried to use a default value for a
1864 * readonly property, it means that it was not
1867 if (zfs_prop_readonly(prop) &&
1868 *source != NULL && (*source)[0] == '\0') {
1873 case PROP_TYPE_STRING:
1875 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1876 "cannot get non-numeric property"));
1877 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1878 dgettext(TEXT_DOMAIN, "internal error")));
1886 * Calculate the source type, given the raw source string.
1889 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1890 char *statbuf, size_t statlen)
1892 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1895 if (source == NULL) {
1896 *srctype = ZPROP_SRC_NONE;
1897 } else if (source[0] == '\0') {
1898 *srctype = ZPROP_SRC_DEFAULT;
1899 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1900 *srctype = ZPROP_SRC_RECEIVED;
1902 if (strcmp(source, zhp->zfs_name) == 0) {
1903 *srctype = ZPROP_SRC_LOCAL;
1905 (void) strlcpy(statbuf, source, statlen);
1906 *srctype = ZPROP_SRC_INHERITED;
1913 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1914 size_t proplen, boolean_t literal)
1919 if (zhp->zfs_recvd_props == NULL)
1920 if (get_recvd_props_ioctl(zhp) != 0)
1923 prop = zfs_name_to_prop(propname);
1925 if (prop != ZPROP_INVAL) {
1927 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1929 zfs_set_recvd_props_mode(zhp, &cookie);
1930 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1931 NULL, NULL, 0, literal);
1932 zfs_unset_recvd_props_mode(zhp, &cookie);
1936 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1937 propname, &propval) != 0)
1939 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1941 (void) strlcpy(propbuf, recvdval, proplen);
1944 return (err == 0 ? 0 : -1);
1948 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1953 value = zfs_get_clones_nvl(zhp);
1958 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1959 pair = nvlist_next_nvpair(value, pair)) {
1960 if (propbuf[0] != '\0')
1961 (void) strlcat(propbuf, ",", proplen);
1962 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1968 struct get_clones_arg {
1972 char buf[ZFS_MAXNAMELEN];
1976 get_clones_cb(zfs_handle_t *zhp, void *arg)
1978 struct get_clones_arg *gca = arg;
1980 if (gca->numclones == 0) {
1985 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
1986 NULL, NULL, 0, B_TRUE) != 0)
1988 if (strcmp(gca->buf, gca->origin) == 0) {
1989 if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
1991 return (no_memory(zhp->zfs_hdl));
1997 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2003 zfs_get_clones_nvl(zfs_handle_t *zhp)
2005 nvlist_t *nv, *value;
2007 if (nvlist_lookup_nvlist(zhp->zfs_props,
2008 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2009 struct get_clones_arg gca;
2012 * if this is a snapshot, then the kernel wasn't able
2013 * to get the clones. Do it by slowly iterating.
2015 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2017 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2019 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2024 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2026 gca.origin = zhp->zfs_name;
2028 if (gca.numclones != 0) {
2030 char pool[ZFS_MAXNAMELEN];
2033 /* get the pool name */
2034 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2035 (void) strsep(&cp, "/@");
2036 root = zfs_open(zhp->zfs_hdl, pool,
2037 ZFS_TYPE_FILESYSTEM);
2039 (void) get_clones_cb(root, &gca);
2042 if (gca.numclones != 0 ||
2043 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2044 nvlist_add_nvlist(zhp->zfs_props,
2045 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2052 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2053 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2056 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2062 * Retrieve a property from the given object. If 'literal' is specified, then
2063 * numbers are left as exact values. Otherwise, numbers are converted to a
2064 * human-readable form.
2066 * Returns 0 on success, or -1 on error.
2069 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2070 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2072 char *source = NULL;
2076 boolean_t received = zfs_is_recvd_props_mode(zhp);
2079 * Check to see if this property applies to our object
2081 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2084 if (received && zfs_prop_readonly(prop))
2088 *src = ZPROP_SRC_NONE;
2091 case ZFS_PROP_CREATION:
2093 * 'creation' is a time_t stored in the statistics. We convert
2094 * this into a string unless 'literal' is specified.
2097 val = getprop_uint64(zhp, prop, &source);
2098 time_t time = (time_t)val;
2102 localtime_r(&time, &t) == NULL ||
2103 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2105 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
2109 case ZFS_PROP_MOUNTPOINT:
2111 * Getting the precise mountpoint can be tricky.
2113 * - for 'none' or 'legacy', return those values.
2114 * - for inherited mountpoints, we want to take everything
2115 * after our ancestor and append it to the inherited value.
2117 * If the pool has an alternate root, we want to prepend that
2118 * root to any values we return.
2121 str = getprop_string(zhp, prop, &source);
2123 if (str[0] == '/') {
2124 char buf[MAXPATHLEN];
2126 const char *relpath;
2129 * If we inherit the mountpoint, even from a dataset
2130 * with a received value, the source will be the path of
2131 * the dataset we inherit from. If source is
2132 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2135 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2138 relpath = zhp->zfs_name + strlen(source);
2139 if (relpath[0] == '/')
2143 if ((zpool_get_prop(zhp->zpool_hdl,
2144 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2145 (strcmp(root, "-") == 0))
2148 * Special case an alternate root of '/'. This will
2149 * avoid having multiple leading slashes in the
2152 if (strcmp(root, "/") == 0)
2156 * If the mountpoint is '/' then skip over this
2157 * if we are obtaining either an alternate root or
2158 * an inherited mountpoint.
2160 if (str[1] == '\0' && (root[0] != '\0' ||
2161 relpath[0] != '\0'))
2164 if (relpath[0] == '\0')
2165 (void) snprintf(propbuf, proplen, "%s%s",
2168 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2169 root, str, relpath[0] == '@' ? "" : "/",
2172 /* 'legacy' or 'none' */
2173 (void) strlcpy(propbuf, str, proplen);
2178 case ZFS_PROP_ORIGIN:
2179 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2182 * If there is no parent at all, return failure to indicate that
2183 * it doesn't apply to this dataset.
2185 if (propbuf[0] == '\0')
2189 case ZFS_PROP_CLONES:
2190 if (get_clones_string(zhp, propbuf, proplen) != 0)
2194 case ZFS_PROP_QUOTA:
2195 case ZFS_PROP_REFQUOTA:
2196 case ZFS_PROP_RESERVATION:
2197 case ZFS_PROP_REFRESERVATION:
2199 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2203 * If quota or reservation is 0, we translate this into 'none'
2204 * (unless literal is set), and indicate that it's the default
2205 * value. Otherwise, we print the number nicely and indicate
2206 * that its set locally.
2210 (void) strlcpy(propbuf, "0", proplen);
2212 (void) strlcpy(propbuf, "none", proplen);
2215 (void) snprintf(propbuf, proplen, "%llu",
2218 zfs_nicenum(val, propbuf, proplen);
2222 case ZFS_PROP_REFRATIO:
2223 case ZFS_PROP_COMPRESSRATIO:
2224 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2226 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2227 (u_longlong_t)(val / 100),
2228 (u_longlong_t)(val % 100));
2232 switch (zhp->zfs_type) {
2233 case ZFS_TYPE_FILESYSTEM:
2236 case ZFS_TYPE_VOLUME:
2239 case ZFS_TYPE_SNAPSHOT:
2245 (void) snprintf(propbuf, proplen, "%s", str);
2248 case ZFS_PROP_MOUNTED:
2250 * The 'mounted' property is a pseudo-property that described
2251 * whether the filesystem is currently mounted. Even though
2252 * it's a boolean value, the typical values of "on" and "off"
2253 * don't make sense, so we translate to "yes" and "no".
2255 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2256 src, &source, &val) != 0)
2259 (void) strlcpy(propbuf, "yes", proplen);
2261 (void) strlcpy(propbuf, "no", proplen);
2266 * The 'name' property is a pseudo-property derived from the
2267 * dataset name. It is presented as a real property to simplify
2270 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2273 case ZFS_PROP_MLSLABEL:
2275 #ifdef HAVE_MLSLABEL
2276 m_label_t *new_sl = NULL;
2277 char *ascii = NULL; /* human readable label */
2279 (void) strlcpy(propbuf,
2280 getprop_string(zhp, prop, &source), proplen);
2282 if (literal || (strcasecmp(propbuf,
2283 ZFS_MLSLABEL_DEFAULT) == 0))
2287 * Try to translate the internal hex string to
2288 * human-readable output. If there are any
2289 * problems just use the hex string.
2292 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2293 L_NO_CORRECTION, NULL) == -1) {
2294 m_label_free(new_sl);
2298 if (label_to_str(new_sl, &ascii, M_LABEL,
2302 m_label_free(new_sl);
2305 m_label_free(new_sl);
2307 (void) strlcpy(propbuf, ascii, proplen);
2310 (void) strlcpy(propbuf,
2311 getprop_string(zhp, prop, &source), proplen);
2312 #endif /* HAVE_MLSLABEL */
2317 switch (zfs_prop_get_type(prop)) {
2318 case PROP_TYPE_NUMBER:
2319 if (get_numeric_property(zhp, prop, src,
2320 &source, &val) != 0)
2323 (void) snprintf(propbuf, proplen, "%llu",
2326 zfs_nicenum(val, propbuf, proplen);
2329 case PROP_TYPE_STRING:
2330 (void) strlcpy(propbuf,
2331 getprop_string(zhp, prop, &source), proplen);
2334 case PROP_TYPE_INDEX:
2335 if (get_numeric_property(zhp, prop, src,
2336 &source, &val) != 0)
2338 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2340 (void) strlcpy(propbuf, strval, proplen);
2348 get_source(zhp, src, source, statbuf, statlen);
2354 * Utility function to get the given numeric property. Does no validation that
2355 * the given property is the appropriate type; should only be used with
2356 * hard-coded property types.
2359 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2364 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2370 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2374 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2375 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2379 * Similar to zfs_prop_get(), but returns the value as an integer.
2382 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2383 zprop_source_t *src, char *statbuf, size_t statlen)
2388 * Check to see if this property applies to our object
2390 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2391 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2392 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2393 zfs_prop_to_name(prop)));
2397 *src = ZPROP_SRC_NONE;
2399 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2402 get_source(zhp, src, source, statbuf, statlen);
2409 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2410 char **domainp, idmap_rid_t *ridp)
2412 idmap_get_handle_t *get_hdl = NULL;
2416 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2420 err = idmap_get_sidbyuid(get_hdl, id,
2421 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2423 err = idmap_get_sidbygid(get_hdl, id,
2424 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2426 if (err == IDMAP_SUCCESS &&
2427 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2428 status == IDMAP_SUCCESS)
2434 idmap_get_destroy(get_hdl);
2437 #endif /* HAVE_IDMAP */
2440 * convert the propname into parameters needed by kernel
2441 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2442 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2443 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2444 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2447 userquota_propname_decode(const char *propname, boolean_t zoned,
2448 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2450 zfs_userquota_prop_t type;
2459 /* Figure out the property type ({user|group}{quota|space}) */
2460 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2461 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2462 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2465 if (type == ZFS_NUM_USERQUOTA_PROPS)
2469 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2470 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2472 cp = strchr(propname, '@') + 1;
2474 if (isuser && (pw = getpwnam(cp)) != NULL) {
2475 if (zoned && getzoneid() == GLOBAL_ZONEID)
2478 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2479 if (zoned && getzoneid() == GLOBAL_ZONEID)
2482 } else if (strchr(cp, '@')) {
2485 * It's a SID name (eg "user@domain") that needs to be
2486 * turned into S-1-domainID-RID.
2488 directory_error_t e;
2489 char *numericsid = NULL;
2492 if (zoned && getzoneid() == GLOBAL_ZONEID)
2495 e = directory_sid_from_user_name(NULL,
2498 e = directory_sid_from_group_name(NULL,
2502 directory_error_free(e);
2505 if (numericsid == NULL)
2508 (void) strlcpy(domain, cp, domainlen);
2509 cp = strrchr(domain, '-');
2514 *ridp = strtoull(cp, &end, 10);
2517 if (errno != 0 || *end != '\0')
2521 #endif /* HAVE_IDMAP */
2524 /* It's a user/group ID (eg "12345"). */
2530 id = strtoul(cp, &end, 10);
2534 /* It's an ephemeral ID. */
2535 if (idmap_id_to_numeric_domain_rid(id, isuser,
2536 &mapdomain, &rid) != 0)
2538 (void) strlcpy(domain, mapdomain, domainlen);
2545 #endif /* HAVE_IDMAP */
2552 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2553 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2556 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2558 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2560 err = userquota_propname_decode(propname,
2561 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2562 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2563 zc.zc_objset_type = *typep;
2567 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2571 *propvalue = zc.zc_cookie;
2576 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2577 uint64_t *propvalue)
2579 zfs_userquota_prop_t type;
2581 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2586 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2587 char *propbuf, int proplen, boolean_t literal)
2591 zfs_userquota_prop_t type;
2593 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2600 (void) snprintf(propbuf, proplen, "%llu",
2601 (u_longlong_t)propvalue);
2602 } else if (propvalue == 0 &&
2603 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2604 (void) strlcpy(propbuf, "none", proplen);
2606 zfs_nicenum(propvalue, propbuf, proplen);
2612 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2613 uint64_t *propvalue)
2616 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2617 const char *snapname;
2619 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2621 snapname = strchr(propname, '@') + 1;
2622 if (strchr(snapname, '@')) {
2623 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2625 /* snapname is the short name, append it to zhp's fsname */
2628 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2629 sizeof (zc.zc_value));
2630 cp = strchr(zc.zc_value, '@');
2633 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2634 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2637 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2641 *propvalue = zc.zc_cookie;
2646 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2647 char *propbuf, int proplen, boolean_t literal)
2652 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2658 (void) snprintf(propbuf, proplen, "%llu", (long long unsigned int)propvalue);
2660 zfs_nicenum(propvalue, propbuf, proplen);
2667 zfs_get_snapused_int(zfs_handle_t *firstsnap, zfs_handle_t *lastsnap,
2671 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2673 (void) strlcpy(zc.zc_name, lastsnap->zfs_name, sizeof (zc.zc_name));
2674 (void) strlcpy(zc.zc_value, firstsnap->zfs_name, sizeof (zc.zc_value));
2676 err = ioctl(lastsnap->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_SNAPS, &zc);
2680 *usedp = zc.zc_cookie;
2686 * Returns the name of the given zfs handle.
2689 zfs_get_name(const zfs_handle_t *zhp)
2691 return (zhp->zfs_name);
2695 * Returns the type of the given zfs handle.
2698 zfs_get_type(const zfs_handle_t *zhp)
2700 return (zhp->zfs_type);
2704 * Is one dataset name a child dataset of another?
2706 * Needs to handle these cases:
2707 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2708 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2709 * Descendant? No. No. No. Yes.
2712 is_descendant(const char *ds1, const char *ds2)
2714 size_t d1len = strlen(ds1);
2716 /* ds2 can't be a descendant if it's smaller */
2717 if (strlen(ds2) < d1len)
2720 /* otherwise, compare strings and verify that there's a '/' char */
2721 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2725 * Given a complete name, return just the portion that refers to the parent.
2726 * Will return -1 if there is no parent (path is just the name of the
2730 parent_name(const char *path, char *buf, size_t buflen)
2734 (void) strlcpy(buf, path, buflen);
2736 if ((slashp = strrchr(buf, '/')) == NULL)
2744 * If accept_ancestor is false, then check to make sure that the given path has
2745 * a parent, and that it exists. If accept_ancestor is true, then find the
2746 * closest existing ancestor for the given path. In prefixlen return the
2747 * length of already existing prefix of the given path. We also fetch the
2748 * 'zoned' property, which is used to validate property settings when creating
2752 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2753 boolean_t accept_ancestor, int *prefixlen)
2755 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2756 char parent[ZFS_MAXNAMELEN];
2762 (void) snprintf(errbuf, sizeof (errbuf),
2763 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2765 /* get parent, and check to see if this is just a pool */
2766 if (parent_name(path, parent, sizeof (parent)) != 0) {
2767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2768 "missing dataset name"));
2769 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2772 /* check to see if the pool exists */
2773 if ((slash = strchr(parent, '/')) == NULL)
2774 slash = parent + strlen(parent);
2775 (void) strncpy(zc.zc_name, parent, slash - parent);
2776 zc.zc_name[slash - parent] = '\0';
2777 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2779 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2780 "no such pool '%s'"), zc.zc_name);
2781 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2784 /* check to see if the parent dataset exists */
2785 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2786 if (errno == ENOENT && accept_ancestor) {
2788 * Go deeper to find an ancestor, give up on top level.
2790 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2791 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2792 "no such pool '%s'"), zc.zc_name);
2793 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2795 } else if (errno == ENOENT) {
2796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2797 "parent does not exist"));
2798 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2800 return (zfs_standard_error(hdl, errno, errbuf));
2803 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2807 /* we are in a non-global zone, but parent is in the global zone */
2808 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2809 (void) zfs_standard_error(hdl, EPERM, errbuf);
2814 /* make sure parent is a filesystem */
2815 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2816 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2817 "parent is not a filesystem"));
2818 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2824 if (prefixlen != NULL)
2825 *prefixlen = strlen(parent);
2830 * Finds whether the dataset of the given type(s) exists.
2833 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2837 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2841 * Try to get stats for the dataset, which will tell us if it exists.
2843 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2844 int ds_type = zhp->zfs_type;
2847 if (types & ds_type)
2854 * Given a path to 'target', create all the ancestors between
2855 * the prefixlen portion of the path, and the target itself.
2856 * Fail if the initial prefixlen-ancestor does not already exist.
2859 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2865 /* make sure prefix exists */
2866 cp = target + prefixlen;
2868 assert(strchr(cp, '/') == NULL);
2869 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2872 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2880 * Attempt to create, mount, and share any ancestor filesystems,
2881 * up to the prefixlen-long one.
2883 for (cp = target + prefixlen + 1;
2884 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2889 h = make_dataset_handle(hdl, target);
2891 /* it already exists, nothing to do here */
2896 logstr = hdl->libzfs_log_str;
2897 hdl->libzfs_log_str = NULL;
2898 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2900 hdl->libzfs_log_str = logstr;
2901 opname = dgettext(TEXT_DOMAIN, "create");
2905 hdl->libzfs_log_str = logstr;
2906 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2908 opname = dgettext(TEXT_DOMAIN, "open");
2912 if (zfs_mount(h, NULL, 0) != 0) {
2913 opname = dgettext(TEXT_DOMAIN, "mount");
2917 if (zfs_share(h) != 0) {
2918 opname = dgettext(TEXT_DOMAIN, "share");
2928 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2929 "failed to %s ancestor '%s'"), opname, target);
2934 * Creates non-existing ancestors of the given path.
2937 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2943 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2946 if ((path_copy = strdup(path)) != NULL) {
2947 rc = create_parents(hdl, path_copy, prefix);
2950 if (path_copy == NULL || rc != 0)
2957 * Create a new filesystem or volume.
2960 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2963 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2966 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2970 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2971 "cannot create '%s'"), path);
2973 /* validate the path, taking care to note the extended error message */
2974 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2975 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2977 /* validate parents exist */
2978 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2982 * The failure modes when creating a dataset of a different type over
2983 * one that already exists is a little strange. In particular, if you
2984 * try to create a dataset on top of an existing dataset, the ioctl()
2985 * will return ENOENT, not EEXIST. To prevent this from happening, we
2986 * first try to see if the dataset exists.
2988 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2989 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2991 "dataset already exists"));
2992 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2995 if (type == ZFS_TYPE_VOLUME)
2996 zc.zc_objset_type = DMU_OST_ZVOL;
2998 zc.zc_objset_type = DMU_OST_ZFS;
3000 if (props && (props = zfs_valid_proplist(hdl, type, props,
3001 zoned, NULL, errbuf)) == 0)
3004 if (type == ZFS_TYPE_VOLUME) {
3006 * If we are creating a volume, the size and block size must
3007 * satisfy a few restraints. First, the blocksize must be a
3008 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3009 * volsize must be a multiple of the block size, and cannot be
3012 if (props == NULL || nvlist_lookup_uint64(props,
3013 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3015 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3016 "missing volume size"));
3017 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3020 if ((ret = nvlist_lookup_uint64(props,
3021 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3022 &blocksize)) != 0) {
3023 if (ret == ENOENT) {
3024 blocksize = zfs_prop_default_numeric(
3025 ZFS_PROP_VOLBLOCKSIZE);
3028 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3029 "missing volume block size"));
3030 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3036 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3037 "volume size cannot be zero"));
3038 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3041 if (size % blocksize != 0) {
3043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3044 "volume size must be a multiple of volume block "
3046 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3050 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
3054 /* create the dataset */
3055 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3057 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
3058 ret = zvol_create_link(hdl, path);
3060 (void) zfs_standard_error(hdl, errno,
3061 dgettext(TEXT_DOMAIN,
3062 "Volume successfully created, but device links "
3063 "were not created"));
3064 zcmd_free_nvlists(&zc);
3069 zcmd_free_nvlists(&zc);
3071 /* check for failure */
3073 char parent[ZFS_MAXNAMELEN];
3074 (void) parent_name(path, parent, sizeof (parent));
3078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3079 "no such parent '%s'"), parent);
3080 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3084 "parent '%s' is not a filesystem"), parent);
3085 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3088 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3089 "volume block size must be power of 2 from "
3091 (uint_t)SPA_MINBLOCKSIZE,
3092 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3094 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3098 "pool must be upgraded to set this "
3099 "property or value"));
3100 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3104 * This platform can't address a volume this big.
3106 if (type == ZFS_TYPE_VOLUME)
3107 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3112 return (zfs_standard_error(hdl, errno, errbuf));
3120 * Destroys the given dataset. The caller must make sure that the filesystem
3121 * isn't mounted, and that there are no active dependents.
3124 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3126 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3128 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3130 if (ZFS_IS_VOLUME(zhp)) {
3131 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3134 zc.zc_objset_type = DMU_OST_ZVOL;
3136 zc.zc_objset_type = DMU_OST_ZFS;
3139 zc.zc_defer_destroy = defer;
3140 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3141 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3142 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3146 remove_mountpoint(zhp);
3151 struct destroydata {
3153 const char *snapname;
3157 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3159 struct destroydata *dd = arg;
3161 char name[ZFS_MAXNAMELEN];
3164 (void) snprintf(name, sizeof (name),
3165 "%s@%s", zhp->zfs_name, dd->snapname);
3167 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3169 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3173 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3174 (void) zvol_remove_link(zhp->zfs_hdl, name);
3176 * NB: this is simply a best-effort. We don't want to
3177 * return an error, because then we wouldn't visit all
3182 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3188 * Destroys all snapshots with the given name in zhp & descendants.
3191 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3194 struct destroydata dd = { 0 };
3196 dd.snapname = snapname;
3197 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3198 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3200 if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3201 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3202 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3203 zhp->zfs_name, snapname);
3205 ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3207 nvlist_free(dd.nvl);
3212 * Destroys all the snapshots named in the nvlist. They must be underneath
3213 * the zhp (either snapshots of it, or snapshots of its descendants).
3216 zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3219 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3221 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3222 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
3224 zc.zc_defer_destroy = defer;
3226 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
3230 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3231 "cannot destroy snapshots in %s"), zc.zc_name);
3235 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3236 "snapshot is cloned"));
3237 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3240 return (zfs_standard_error(zhp->zfs_hdl, errno,
3249 * Clones the given dataset. The target must be of the same type as the source.
3252 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3254 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3255 char parent[ZFS_MAXNAMELEN];
3258 libzfs_handle_t *hdl = zhp->zfs_hdl;
3262 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3264 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3265 "cannot create '%s'"), target);
3267 /* validate the target/clone name */
3268 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3269 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3271 /* validate parents exist */
3272 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3275 (void) parent_name(target, parent, sizeof (parent));
3278 if (ZFS_IS_VOLUME(zhp)) {
3279 zc.zc_objset_type = DMU_OST_ZVOL;
3280 type = ZFS_TYPE_VOLUME;
3282 zc.zc_objset_type = DMU_OST_ZFS;
3283 type = ZFS_TYPE_FILESYSTEM;
3287 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3288 zhp, errbuf)) == NULL)
3291 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3299 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3300 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3301 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3303 zcmd_free_nvlists(&zc);
3310 * The parent doesn't exist. We should have caught this
3311 * above, but there may a race condition that has since
3312 * destroyed the parent.
3314 * At this point, we don't know whether it's the source
3315 * that doesn't exist anymore, or whether the target
3316 * dataset doesn't exist.
3318 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3319 "no such parent '%s'"), parent);
3320 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3323 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3324 "source and target pools differ"));
3325 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3329 return (zfs_standard_error(zhp->zfs_hdl, errno,
3332 } else if (ZFS_IS_VOLUME(zhp)) {
3333 ret = zvol_create_link(zhp->zfs_hdl, target);
3339 typedef struct promote_data {
3340 char cb_mountpoint[MAXPATHLEN];
3341 const char *cb_target;
3342 const char *cb_errbuf;
3343 uint64_t cb_pivot_txg;
3347 promote_snap_cb(zfs_handle_t *zhp, void *data)
3349 promote_data_t *pd = data;
3351 char snapname[MAXPATHLEN];
3354 /* We don't care about snapshots after the pivot point */
3355 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3360 /* Remove the device link if it's a zvol. */
3361 if (ZFS_IS_VOLUME(zhp))
3362 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3364 /* Check for conflicting names */
3365 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3366 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3367 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3370 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3371 "snapshot name '%s' from origin \n"
3372 "conflicts with '%s' from target"),
3373 zhp->zfs_name, snapname);
3374 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3381 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3383 promote_data_t *pd = data;
3385 /* We don't care about snapshots after the pivot point */
3386 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3387 /* Create the device link if it's a zvol. */
3388 if (ZFS_IS_VOLUME(zhp))
3389 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3397 * Promotes the given clone fs to be the clone parent.
3400 zfs_promote(zfs_handle_t *zhp)
3402 libzfs_handle_t *hdl = zhp->zfs_hdl;
3403 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3404 char parent[MAXPATHLEN];
3411 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3412 "cannot promote '%s'"), zhp->zfs_name);
3414 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3416 "snapshots can not be promoted"));
3417 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3420 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3421 if (parent[0] == '\0') {
3422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3423 "not a cloned filesystem"));
3424 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3426 cp = strchr(parent, '@');
3429 /* Walk the snapshots we will be moving */
3430 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3433 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3435 pd.cb_target = zhp->zfs_name;
3436 pd.cb_errbuf = errbuf;
3437 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3440 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3441 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3442 ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
3448 /* issue the ioctl */
3449 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3450 sizeof (zc.zc_value));
3451 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3452 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3455 int save_errno = errno;
3457 (void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3461 switch (save_errno) {
3464 * There is a conflicting snapshot name. We
3465 * should have caught this above, but they could
3466 * have renamed something in the mean time.
3468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3469 "conflicting snapshot '%s' from parent '%s'"),
3470 zc.zc_string, parent);
3471 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3474 return (zfs_standard_error(hdl, save_errno, errbuf));
3477 (void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3486 const char *cd_snapname;
3491 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3493 struct createdata *cd = arg;
3496 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3497 char name[MAXPATHLEN];
3499 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3500 (void) strlcat(name, "@", sizeof (name));
3501 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3502 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3505 * NB: this is simply a best-effort. We don't want to
3506 * return an error, because then we wouldn't visit all
3511 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3519 * Takes a snapshot of the given dataset.
3522 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3526 char parent[ZFS_MAXNAMELEN];
3528 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3532 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3533 "cannot snapshot '%s'"), path);
3535 /* validate the target name */
3536 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3537 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3540 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3541 props, B_FALSE, NULL, errbuf)) == NULL)
3544 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3552 /* make sure the parent exists and is of the appropriate type */
3553 delim = strchr(path, '@');
3554 (void) strncpy(parent, path, delim - path);
3555 parent[delim - path] = '\0';
3557 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3558 ZFS_TYPE_VOLUME)) == NULL) {
3559 zcmd_free_nvlists(&zc);
3563 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3564 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3565 if (ZFS_IS_VOLUME(zhp))
3566 zc.zc_objset_type = DMU_OST_ZVOL;
3568 zc.zc_objset_type = DMU_OST_ZFS;
3569 zc.zc_cookie = recursive;
3570 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3572 zcmd_free_nvlists(&zc);
3575 * if it was recursive, the one that actually failed will be in
3579 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3580 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3582 if (ret == 0 && recursive) {
3583 struct createdata cd;
3585 cd.cd_snapname = delim + 1;
3586 cd.cd_ifexists = B_FALSE;
3587 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3589 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3590 ret = zvol_create_link(zhp->zfs_hdl, path);
3592 (void) zfs_standard_error(hdl, errno,
3593 dgettext(TEXT_DOMAIN,
3594 "Volume successfully snapshotted, but device links "
3595 "were not created"));
3602 (void) zfs_standard_error(hdl, errno, errbuf);
3610 * Destroy any more recent snapshots. We invoke this callback on any dependents
3611 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3612 * is a dependent and we should just destroy it without checking the transaction
3615 typedef struct rollback_data {
3616 const char *cb_target; /* the snapshot */
3617 uint64_t cb_create; /* creation time reference */
3619 boolean_t cb_dependent;
3624 rollback_destroy(zfs_handle_t *zhp, void *data)
3626 rollback_data_t *cbp = data;
3628 if (!cbp->cb_dependent) {
3629 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3630 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3631 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3635 cbp->cb_dependent = B_TRUE;
3636 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3637 rollback_destroy, cbp);
3638 cbp->cb_dependent = B_FALSE;
3640 logstr = zhp->zfs_hdl->libzfs_log_str;
3641 zhp->zfs_hdl->libzfs_log_str = NULL;
3642 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3643 zhp->zfs_hdl->libzfs_log_str = logstr;
3646 /* We must destroy this clone; first unmount it */
3647 prop_changelist_t *clp;
3649 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3650 cbp->cb_force ? MS_FORCE: 0);
3651 if (clp == NULL || changelist_prefix(clp) != 0) {
3652 cbp->cb_error = B_TRUE;
3656 if (zfs_destroy(zhp, B_FALSE) != 0)
3657 cbp->cb_error = B_TRUE;
3659 changelist_remove(clp, zhp->zfs_name);
3660 (void) changelist_postfix(clp);
3661 changelist_free(clp);
3669 * Given a dataset, rollback to a specific snapshot, discarding any
3670 * data changes since then and making it the active dataset.
3672 * Any snapshots more recent than the target are destroyed, along with
3676 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3678 rollback_data_t cb = { 0 };
3680 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3681 boolean_t restore_resv = 0;
3682 uint64_t old_volsize = 0, new_volsize;
3683 zfs_prop_t resv_prop = { 0 };
3685 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3686 zhp->zfs_type == ZFS_TYPE_VOLUME);
3689 * Destroy all recent snapshots and its dependends.
3691 cb.cb_force = force;
3692 cb.cb_target = snap->zfs_name;
3693 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3694 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3700 * Now that we have verified that the snapshot is the latest,
3701 * rollback to the given snapshot.
3704 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3705 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3707 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3709 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3711 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3714 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3716 if (ZFS_IS_VOLUME(zhp))
3717 zc.zc_objset_type = DMU_OST_ZVOL;
3719 zc.zc_objset_type = DMU_OST_ZFS;
3722 * We rely on zfs_iter_children() to verify that there are no
3723 * newer snapshots for the given dataset. Therefore, we can
3724 * simply pass the name on to the ioctl() call. There is still
3725 * an unlikely race condition where the user has taken a
3726 * snapshot since we verified that this was the most recent.
3729 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3730 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3731 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3737 * For volumes, if the pre-rollback volsize matched the pre-
3738 * rollback reservation and the volsize has changed then set
3739 * the reservation property to the post-rollback volsize.
3740 * Make a new handle since the rollback closed the dataset.
3742 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3743 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3744 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3749 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3750 if (old_volsize != new_volsize)
3751 err = zfs_prop_set_int(zhp, resv_prop,
3760 * Renames the given dataset.
3763 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3766 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3768 prop_changelist_t *cl = NULL;
3769 zfs_handle_t *zhrp = NULL;
3770 char *parentname = NULL;
3771 char parent[ZFS_MAXNAMELEN];
3772 libzfs_handle_t *hdl = zhp->zfs_hdl;
3775 /* if we have the same exact name, just return success */
3776 if (strcmp(zhp->zfs_name, target) == 0)
3779 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3780 "cannot rename to '%s'"), target);
3783 * Make sure the target name is valid
3785 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3786 if ((strchr(target, '@') == NULL) ||
3789 * Snapshot target name is abbreviated,
3790 * reconstruct full dataset name
3792 (void) strlcpy(parent, zhp->zfs_name,
3794 delim = strchr(parent, '@');
3795 if (strchr(target, '@') == NULL)
3799 (void) strlcat(parent, target, sizeof (parent));
3803 * Make sure we're renaming within the same dataset.
3805 delim = strchr(target, '@');
3806 if (strncmp(zhp->zfs_name, target, delim - target)
3807 != 0 || zhp->zfs_name[delim - target] != '@') {
3808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3809 "snapshots must be part of same "
3811 return (zfs_error(hdl, EZFS_CROSSTARGET,
3815 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3816 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3819 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3820 "recursive rename must be a snapshot"));
3821 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3824 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3825 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3827 /* validate parents */
3828 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3831 /* make sure we're in the same pool */
3832 verify((delim = strchr(target, '/')) != NULL);
3833 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3834 zhp->zfs_name[delim - target] != '/') {
3835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3836 "datasets must be within same pool"));
3837 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3840 /* new name cannot be a child of the current dataset name */
3841 if (is_descendant(zhp->zfs_name, target)) {
3842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3843 "New dataset name cannot be a descendant of "
3844 "current dataset name"));
3845 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3849 (void) snprintf(errbuf, sizeof (errbuf),
3850 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3852 if (getzoneid() == GLOBAL_ZONEID &&
3853 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3854 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3855 "dataset is used in a non-global zone"));
3856 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3860 struct destroydata dd;
3862 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3863 if (parentname == NULL) {
3867 delim = strchr(parentname, '@');
3869 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3875 dd.snapname = delim + 1;
3877 /* We remove any zvol links prior to renaming them */
3878 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3879 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3880 nvlist_free(dd.nvl);
3885 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3888 if (changelist_haszonedchild(cl)) {
3889 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3890 "child dataset with inherited mountpoint is used "
3891 "in a non-global zone"));
3892 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3897 if ((ret = changelist_prefix(cl)) != 0)
3901 if (ZFS_IS_VOLUME(zhp))
3902 zc.zc_objset_type = DMU_OST_ZVOL;
3904 zc.zc_objset_type = DMU_OST_ZFS;
3906 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3907 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3909 zc.zc_cookie = recursive;
3911 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3913 * if it was recursive, the one that actually failed will
3916 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3917 "cannot rename '%s'"), zc.zc_name);
3919 if (recursive && errno == EEXIST) {
3920 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3921 "a child dataset already has a snapshot "
3922 "with the new name"));
3923 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3925 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3929 * On failure, we still want to remount any filesystems that
3930 * were previously mounted, so we don't alter the system state.
3933 struct createdata cd;
3935 /* only create links for datasets that had existed */
3936 cd.cd_snapname = delim + 1;
3937 cd.cd_ifexists = B_TRUE;
3938 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3941 (void) changelist_postfix(cl);
3945 struct createdata cd;
3947 /* only create links for datasets that had existed */
3948 cd.cd_snapname = strchr(target, '@') + 1;
3949 cd.cd_ifexists = B_TRUE;
3950 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3953 changelist_rename(cl, zfs_get_name(zhp), target);
3954 ret = changelist_postfix(cl);
3966 changelist_free(cl);
3972 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3973 * and wait briefly for udev to create the /dev link.
3976 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3978 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3982 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3984 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3985 char path[MAXPATHLEN];
3988 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3991 * Issue the appropriate ioctl.
3993 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3997 * Silently ignore the case where the link already
3998 * exists. This allows 'zfs volinit' to be run multiple
3999 * times without errors.
4005 * Dataset does not exist in the kernel. If we
4006 * don't care (see zfs_rename), then ignore the
4016 return (zfs_standard_error_fmt(hdl, errno,
4017 dgettext(TEXT_DOMAIN, "cannot create device links "
4018 "for '%s'"), dataset));
4023 * Wait up to 10 seconds for udev to create the device.
4025 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
4026 error = zpool_label_disk_wait(path, 10000);
4028 (void) printf(gettext("%s may not be immediately "
4029 "available\n"), path);
4035 * Remove a minor node for the given zvol and the associated /dev links.
4038 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4040 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4041 int timeout = 3000; /* in milliseconds */
4045 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4048 * Due to concurrent updates by udev the device may be reported as
4049 * busy. In this case don't immediately fail. Instead briefly delay
4050 * and retry the ioctl() which is now likely to succeed. If unable
4051 * remove the link after timeout milliseconds return the failure.
4053 for (i = 0; i < timeout; i++) {
4054 error = ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
4055 if (error && errno == EBUSY) {
4067 * Silently ignore the case where the link no longer
4068 * exists, so that 'zfs volfini' can be run multiple
4069 * times without errors.
4074 return (zfs_standard_error_fmt(hdl, errno,
4075 dgettext(TEXT_DOMAIN, "cannot remove device "
4076 "links for '%s': %s"), dataset, strerror(errno)));
4084 zfs_get_user_props(zfs_handle_t *zhp)
4086 return (zhp->zfs_user_props);
4090 * This function is used by 'zfs list' to determine the exact set of columns to
4091 * display, and their maximum widths. This does two main things:
4093 * - If this is a list of all properties, then expand the list to include
4094 * all native properties, and set a flag so that for each dataset we look
4095 * for new unique user properties and add them to the list.
4097 * - For non fixed-width properties, keep track of the maximum width seen
4098 * so that we can size the column appropriately. If the user has
4099 * requested received property values, we also need to compute the width
4100 * of the RECEIVED column.
4103 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
4105 libzfs_handle_t *hdl = zhp->zfs_hdl;
4106 zprop_list_t *entry;
4107 zprop_list_t **last, **start;
4108 nvlist_t *userprops, *propval;
4111 char buf[ZFS_MAXPROPLEN];
4113 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4116 userprops = zfs_get_user_props(zhp);
4119 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4121 * Go through and add any user properties as necessary. We
4122 * start by incrementing our list pointer to the first
4123 * non-native property.
4126 while (*start != NULL) {
4127 if ((*start)->pl_prop == ZPROP_INVAL)
4129 start = &(*start)->pl_next;
4133 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4135 * See if we've already found this property in our list.
4137 for (last = start; *last != NULL;
4138 last = &(*last)->pl_next) {
4139 if (strcmp((*last)->pl_user_prop,
4140 nvpair_name(elem)) == 0)
4144 if (*last == NULL) {
4145 if ((entry = zfs_alloc(hdl,
4146 sizeof (zprop_list_t))) == NULL ||
4147 ((entry->pl_user_prop = zfs_strdup(hdl,
4148 nvpair_name(elem)))) == NULL) {
4153 entry->pl_prop = ZPROP_INVAL;
4154 entry->pl_width = strlen(nvpair_name(elem));
4155 entry->pl_all = B_TRUE;
4162 * Now go through and check the width of any non-fixed columns
4164 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4165 if (entry->pl_fixed)
4168 if (entry->pl_prop != ZPROP_INVAL) {
4169 if (zfs_prop_get(zhp, entry->pl_prop,
4170 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4171 if (strlen(buf) > entry->pl_width)
4172 entry->pl_width = strlen(buf);
4174 if (received && zfs_prop_get_recvd(zhp,
4175 zfs_prop_to_name(entry->pl_prop),
4176 buf, sizeof (buf), B_FALSE) == 0)
4177 if (strlen(buf) > entry->pl_recvd_width)
4178 entry->pl_recvd_width = strlen(buf);
4180 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4182 verify(nvlist_lookup_string(propval,
4183 ZPROP_VALUE, &strval) == 0);
4184 if (strlen(strval) > entry->pl_width)
4185 entry->pl_width = strlen(strval);
4187 if (received && zfs_prop_get_recvd(zhp,
4188 entry->pl_user_prop,
4189 buf, sizeof (buf), B_FALSE) == 0)
4190 if (strlen(buf) > entry->pl_recvd_width)
4191 entry->pl_recvd_width = strlen(buf);
4199 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4204 * Keep a reference to the props-table against which we prune the
4207 zhp->zfs_props_table = props;
4209 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4212 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4213 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4216 * User properties will result in ZPROP_INVAL, and since we
4217 * only know how to prune standard ZFS properties, we always
4218 * leave these in the list. This can also happen if we
4219 * encounter an unknown DSL property (when running older
4220 * software, for example).
4222 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4223 (void) nvlist_remove(zhp->zfs_props,
4224 nvpair_name(curr), nvpair_type(curr));
4230 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4231 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4233 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4234 nvlist_t *nvlist = NULL;
4237 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4238 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4239 zc.zc_cookie = (uint64_t)cmd;
4241 if (cmd == ZFS_SMB_ACL_RENAME) {
4242 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4243 (void) no_memory(hdl);
4249 case ZFS_SMB_ACL_ADD:
4250 case ZFS_SMB_ACL_REMOVE:
4251 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4253 case ZFS_SMB_ACL_RENAME:
4254 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4256 (void) no_memory(hdl);
4259 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4261 (void) no_memory(hdl);
4264 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4265 nvlist_free(nvlist);
4269 case ZFS_SMB_ACL_PURGE:
4274 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4276 nvlist_free(nvlist);
4281 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4282 char *path, char *resource)
4284 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4289 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4290 char *path, char *resource)
4292 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4297 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4299 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4304 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4305 char *oldname, char *newname)
4307 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4312 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4313 zfs_userspace_cb_t func, void *arg)
4315 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4317 zfs_useracct_t buf[100];
4319 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4321 zc.zc_objset_type = type;
4322 zc.zc_nvlist_dst = (uintptr_t)buf;
4326 zfs_useracct_t *zua = buf;
4328 zc.zc_nvlist_dst_size = sizeof (buf);
4329 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4330 ZFS_IOC_USERSPACE_MANY, &zc);
4331 if (error || zc.zc_nvlist_dst_size == 0)
4334 while (zc.zc_nvlist_dst_size > 0) {
4335 error = func(arg, zua->zu_domain, zua->zu_rid,
4340 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4348 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4349 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4350 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4352 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4353 libzfs_handle_t *hdl = zhp->zfs_hdl;
4355 ASSERT(!recursive || dsobj == 0);
4357 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4358 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4359 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4360 >= sizeof (zc.zc_string))
4361 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4362 zc.zc_cookie = recursive;
4363 zc.zc_temphold = temphold;
4364 zc.zc_cleanup_fd = cleanup_fd;
4365 zc.zc_sendobj = dsobj;
4366 zc.zc_createtxg = createtxg;
4368 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4369 char errbuf[ZFS_MAXNAMELEN+32];
4372 * if it was recursive, the one that actually failed will be in
4375 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4376 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4380 * Temporary tags wind up having the ds object id
4381 * prepended. So even if we passed the length check
4382 * above, it's still possible for the tag to wind
4383 * up being slightly too long.
4385 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4388 "pool must be upgraded"));
4389 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4391 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4393 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4399 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4407 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4408 boolean_t recursive)
4410 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4411 libzfs_handle_t *hdl = zhp->zfs_hdl;
4413 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4414 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4415 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4416 >= sizeof (zc.zc_string))
4417 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4418 zc.zc_cookie = recursive;
4420 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4421 char errbuf[ZFS_MAXNAMELEN+32];
4424 * if it was recursive, the one that actually failed will be in
4427 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4428 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4432 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4435 "pool must be upgraded"));
4436 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4438 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4440 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4448 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4450 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4451 libzfs_handle_t *hdl = zhp->zfs_hdl;
4455 char errbuf[ZFS_MAXNAMELEN+32];
4457 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4458 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4462 nvbuf = malloc(nvsz);
4463 if (nvbuf == NULL) {
4464 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4468 zc.zc_nvlist_dst_size = nvsz;
4469 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4471 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4473 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4474 (void) snprintf(errbuf, sizeof (errbuf),
4475 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4480 nvsz = zc.zc_nvlist_dst_size;
4484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4485 "pool must be upgraded"));
4486 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4489 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4492 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4495 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4500 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4502 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4503 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4505 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4515 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4517 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4518 libzfs_handle_t *hdl = zhp->zfs_hdl;
4520 char errbuf[ZFS_MAXNAMELEN+32];
4524 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4525 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4527 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4530 nvbuf = malloc(nvsz);
4532 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4535 zc.zc_nvlist_src_size = nvsz;
4536 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4537 zc.zc_perm_action = un;
4539 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4541 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4542 (void) snprintf(errbuf, sizeof (errbuf),
4543 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4548 "pool must be upgraded"));
4549 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4552 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4555 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4558 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4569 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4571 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4572 libzfs_handle_t *hdl = zhp->zfs_hdl;
4576 char errbuf[ZFS_MAXNAMELEN+32];
4578 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4582 nvbuf = malloc(nvsz);
4583 if (nvbuf == NULL) {
4584 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4588 zc.zc_nvlist_dst_size = nvsz;
4589 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4591 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4593 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4594 (void) snprintf(errbuf, sizeof (errbuf),
4595 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4600 nvsz = zc.zc_nvlist_dst_size;
4604 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4605 "pool must be upgraded"));
4606 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4609 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4612 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4615 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4620 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4622 (void) snprintf(errbuf, sizeof (errbuf),
4623 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4625 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4635 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4638 uint64_t nblocks, volblocksize;
4642 if (nvlist_lookup_string(props,
4643 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4644 ncopies = atoi(strval);
4647 if (nvlist_lookup_uint64(props,
4648 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4649 &volblocksize) != 0)
4650 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4651 nblocks = volsize/volblocksize;
4652 /* start with metadnode L0-L6 */
4654 /* calculate number of indirects */
4655 while (nblocks > 1) {
4656 nblocks += DNODES_PER_LEVEL - 1;
4657 nblocks /= DNODES_PER_LEVEL;
4660 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4663 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4664 * compressed, but in practice they compress down to about
4667 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;