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. If the file system
3122 * does not exist this function does nothing.
3125 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3127 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3129 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3131 if (ZFS_IS_VOLUME(zhp)) {
3132 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3135 zc.zc_objset_type = DMU_OST_ZVOL;
3137 zc.zc_objset_type = DMU_OST_ZFS;
3140 zc.zc_defer_destroy = defer;
3141 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3143 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3144 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3148 remove_mountpoint(zhp);
3153 struct destroydata {
3155 const char *snapname;
3159 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3161 struct destroydata *dd = arg;
3163 char name[ZFS_MAXNAMELEN];
3166 (void) snprintf(name, sizeof (name),
3167 "%s@%s", zhp->zfs_name, dd->snapname);
3169 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3171 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3175 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3176 (void) zvol_remove_link(zhp->zfs_hdl, name);
3178 * NB: this is simply a best-effort. We don't want to
3179 * return an error, because then we wouldn't visit all
3184 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3190 * Destroys all snapshots with the given name in zhp & descendants.
3193 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3196 struct destroydata dd = { 0 };
3198 dd.snapname = snapname;
3199 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3200 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3202 if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3203 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3204 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3205 zhp->zfs_name, snapname);
3207 ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3209 nvlist_free(dd.nvl);
3214 * Destroys all the snapshots named in the nvlist. They must be underneath
3215 * the zhp (either snapshots of it, or snapshots of its descendants).
3218 zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3221 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3223 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3224 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
3226 zc.zc_defer_destroy = defer;
3228 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
3232 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3233 "cannot destroy snapshots in %s"), zc.zc_name);
3237 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3238 "snapshot is cloned"));
3239 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3242 return (zfs_standard_error(zhp->zfs_hdl, errno,
3251 * Clones the given dataset. The target must be of the same type as the source.
3254 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3256 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3257 char parent[ZFS_MAXNAMELEN];
3260 libzfs_handle_t *hdl = zhp->zfs_hdl;
3264 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3266 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3267 "cannot create '%s'"), target);
3269 /* validate the target/clone name */
3270 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3271 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3273 /* validate parents exist */
3274 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3277 (void) parent_name(target, parent, sizeof (parent));
3280 if (ZFS_IS_VOLUME(zhp)) {
3281 zc.zc_objset_type = DMU_OST_ZVOL;
3282 type = ZFS_TYPE_VOLUME;
3284 zc.zc_objset_type = DMU_OST_ZFS;
3285 type = ZFS_TYPE_FILESYSTEM;
3289 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3290 zhp, errbuf)) == NULL)
3293 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3301 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3302 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3303 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3305 zcmd_free_nvlists(&zc);
3312 * The parent doesn't exist. We should have caught this
3313 * above, but there may a race condition that has since
3314 * destroyed the parent.
3316 * At this point, we don't know whether it's the source
3317 * that doesn't exist anymore, or whether the target
3318 * dataset doesn't exist.
3320 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3321 "no such parent '%s'"), parent);
3322 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3325 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3326 "source and target pools differ"));
3327 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3331 return (zfs_standard_error(zhp->zfs_hdl, errno,
3334 } else if (ZFS_IS_VOLUME(zhp)) {
3335 ret = zvol_create_link(zhp->zfs_hdl, target);
3341 typedef struct promote_data {
3342 char cb_mountpoint[MAXPATHLEN];
3343 const char *cb_target;
3344 const char *cb_errbuf;
3345 uint64_t cb_pivot_txg;
3349 promote_snap_cb(zfs_handle_t *zhp, void *data)
3351 promote_data_t *pd = data;
3353 char snapname[MAXPATHLEN];
3356 /* We don't care about snapshots after the pivot point */
3357 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3362 /* Remove the device link if it's a zvol. */
3363 if (ZFS_IS_VOLUME(zhp))
3364 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3366 /* Check for conflicting names */
3367 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3368 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3369 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3372 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3373 "snapshot name '%s' from origin \n"
3374 "conflicts with '%s' from target"),
3375 zhp->zfs_name, snapname);
3376 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3383 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3385 promote_data_t *pd = data;
3387 /* We don't care about snapshots after the pivot point */
3388 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3389 /* Create the device link if it's a zvol. */
3390 if (ZFS_IS_VOLUME(zhp))
3391 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3399 * Promotes the given clone fs to be the clone parent.
3402 zfs_promote(zfs_handle_t *zhp)
3404 libzfs_handle_t *hdl = zhp->zfs_hdl;
3405 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3406 char parent[MAXPATHLEN];
3413 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3414 "cannot promote '%s'"), zhp->zfs_name);
3416 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3417 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3418 "snapshots can not be promoted"));
3419 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3422 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3423 if (parent[0] == '\0') {
3424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3425 "not a cloned filesystem"));
3426 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3428 cp = strchr(parent, '@');
3431 /* Walk the snapshots we will be moving */
3432 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3435 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3437 pd.cb_target = zhp->zfs_name;
3438 pd.cb_errbuf = errbuf;
3439 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3442 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3443 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3444 ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
3450 /* issue the ioctl */
3451 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3452 sizeof (zc.zc_value));
3453 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3454 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3457 int save_errno = errno;
3459 (void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3463 switch (save_errno) {
3466 * There is a conflicting snapshot name. We
3467 * should have caught this above, but they could
3468 * have renamed something in the mean time.
3470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3471 "conflicting snapshot '%s' from parent '%s'"),
3472 zc.zc_string, parent);
3473 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3476 return (zfs_standard_error(hdl, save_errno, errbuf));
3479 (void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3488 const char *cd_snapname;
3493 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3495 struct createdata *cd = arg;
3498 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3499 char name[MAXPATHLEN];
3501 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3502 (void) strlcat(name, "@", sizeof (name));
3503 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3504 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3507 * NB: this is simply a best-effort. We don't want to
3508 * return an error, because then we wouldn't visit all
3513 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3521 * Takes a snapshot of the given dataset.
3524 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3528 char parent[ZFS_MAXNAMELEN];
3530 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3534 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3535 "cannot snapshot '%s'"), path);
3537 /* validate the target name */
3538 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3539 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3542 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3543 props, B_FALSE, NULL, errbuf)) == NULL)
3546 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3554 /* make sure the parent exists and is of the appropriate type */
3555 delim = strchr(path, '@');
3556 (void) strncpy(parent, path, delim - path);
3557 parent[delim - path] = '\0';
3559 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3560 ZFS_TYPE_VOLUME)) == NULL) {
3561 zcmd_free_nvlists(&zc);
3565 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3566 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3567 if (ZFS_IS_VOLUME(zhp))
3568 zc.zc_objset_type = DMU_OST_ZVOL;
3570 zc.zc_objset_type = DMU_OST_ZFS;
3571 zc.zc_cookie = recursive;
3572 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3574 zcmd_free_nvlists(&zc);
3577 * if it was recursive, the one that actually failed will be in
3581 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3582 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3584 if (ret == 0 && recursive) {
3585 struct createdata cd;
3587 cd.cd_snapname = delim + 1;
3588 cd.cd_ifexists = B_FALSE;
3589 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3591 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3592 ret = zvol_create_link(zhp->zfs_hdl, path);
3594 (void) zfs_standard_error(hdl, errno,
3595 dgettext(TEXT_DOMAIN,
3596 "Volume successfully snapshotted, but device links "
3597 "were not created"));
3604 (void) zfs_standard_error(hdl, errno, errbuf);
3612 * Destroy any more recent snapshots. We invoke this callback on any dependents
3613 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3614 * is a dependent and we should just destroy it without checking the transaction
3617 typedef struct rollback_data {
3618 const char *cb_target; /* the snapshot */
3619 uint64_t cb_create; /* creation time reference */
3621 boolean_t cb_dependent;
3626 rollback_destroy(zfs_handle_t *zhp, void *data)
3628 rollback_data_t *cbp = data;
3630 if (!cbp->cb_dependent) {
3631 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3632 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3633 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3637 cbp->cb_dependent = B_TRUE;
3638 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3639 rollback_destroy, cbp);
3640 cbp->cb_dependent = B_FALSE;
3642 logstr = zhp->zfs_hdl->libzfs_log_str;
3643 zhp->zfs_hdl->libzfs_log_str = NULL;
3644 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3645 zhp->zfs_hdl->libzfs_log_str = logstr;
3648 /* We must destroy this clone; first unmount it */
3649 prop_changelist_t *clp;
3651 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3652 cbp->cb_force ? MS_FORCE: 0);
3653 if (clp == NULL || changelist_prefix(clp) != 0) {
3654 cbp->cb_error = B_TRUE;
3658 if (zfs_destroy(zhp, B_FALSE) != 0)
3659 cbp->cb_error = B_TRUE;
3661 changelist_remove(clp, zhp->zfs_name);
3662 (void) changelist_postfix(clp);
3663 changelist_free(clp);
3671 * Given a dataset, rollback to a specific snapshot, discarding any
3672 * data changes since then and making it the active dataset.
3674 * Any snapshots more recent than the target are destroyed, along with
3678 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3680 rollback_data_t cb = { 0 };
3682 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3683 boolean_t restore_resv = 0;
3684 uint64_t old_volsize = 0, new_volsize;
3685 zfs_prop_t resv_prop = { 0 };
3687 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3688 zhp->zfs_type == ZFS_TYPE_VOLUME);
3691 * Destroy all recent snapshots and its dependends.
3693 cb.cb_force = force;
3694 cb.cb_target = snap->zfs_name;
3695 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3696 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3702 * Now that we have verified that the snapshot is the latest,
3703 * rollback to the given snapshot.
3706 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3707 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3709 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3711 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3713 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3716 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3718 if (ZFS_IS_VOLUME(zhp))
3719 zc.zc_objset_type = DMU_OST_ZVOL;
3721 zc.zc_objset_type = DMU_OST_ZFS;
3724 * We rely on zfs_iter_children() to verify that there are no
3725 * newer snapshots for the given dataset. Therefore, we can
3726 * simply pass the name on to the ioctl() call. There is still
3727 * an unlikely race condition where the user has taken a
3728 * snapshot since we verified that this was the most recent.
3731 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3732 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3733 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3739 * For volumes, if the pre-rollback volsize matched the pre-
3740 * rollback reservation and the volsize has changed then set
3741 * the reservation property to the post-rollback volsize.
3742 * Make a new handle since the rollback closed the dataset.
3744 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3745 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3746 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3751 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3752 if (old_volsize != new_volsize)
3753 err = zfs_prop_set_int(zhp, resv_prop,
3762 * Renames the given dataset.
3765 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3766 boolean_t force_unmount)
3769 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3771 prop_changelist_t *cl = NULL;
3772 zfs_handle_t *zhrp = NULL;
3773 char *parentname = NULL;
3774 char parent[ZFS_MAXNAMELEN];
3775 libzfs_handle_t *hdl = zhp->zfs_hdl;
3778 /* if we have the same exact name, just return success */
3779 if (strcmp(zhp->zfs_name, target) == 0)
3782 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3783 "cannot rename to '%s'"), target);
3786 * Make sure the target name is valid
3788 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3789 if ((strchr(target, '@') == NULL) ||
3792 * Snapshot target name is abbreviated,
3793 * reconstruct full dataset name
3795 (void) strlcpy(parent, zhp->zfs_name,
3797 delim = strchr(parent, '@');
3798 if (strchr(target, '@') == NULL)
3802 (void) strlcat(parent, target, sizeof (parent));
3806 * Make sure we're renaming within the same dataset.
3808 delim = strchr(target, '@');
3809 if (strncmp(zhp->zfs_name, target, delim - target)
3810 != 0 || zhp->zfs_name[delim - target] != '@') {
3811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3812 "snapshots must be part of same "
3814 return (zfs_error(hdl, EZFS_CROSSTARGET,
3818 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3819 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3823 "recursive rename must be a snapshot"));
3824 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3827 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3828 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3830 /* validate parents */
3831 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3834 /* make sure we're in the same pool */
3835 verify((delim = strchr(target, '/')) != NULL);
3836 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3837 zhp->zfs_name[delim - target] != '/') {
3838 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3839 "datasets must be within same pool"));
3840 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3843 /* new name cannot be a child of the current dataset name */
3844 if (is_descendant(zhp->zfs_name, target)) {
3845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3846 "New dataset name cannot be a descendant of "
3847 "current dataset name"));
3848 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3852 (void) snprintf(errbuf, sizeof (errbuf),
3853 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3855 if (getzoneid() == GLOBAL_ZONEID &&
3856 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3858 "dataset is used in a non-global zone"));
3859 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3863 struct destroydata dd;
3865 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3866 if (parentname == NULL) {
3870 delim = strchr(parentname, '@');
3872 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3878 dd.snapname = delim + 1;
3880 /* We remove any zvol links prior to renaming them */
3881 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3882 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3883 nvlist_free(dd.nvl);
3888 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3889 force_unmount ? MS_FORCE : 0)) == NULL)
3892 if (changelist_haszonedchild(cl)) {
3893 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3894 "child dataset with inherited mountpoint is used "
3895 "in a non-global zone"));
3896 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3901 if ((ret = changelist_prefix(cl)) != 0)
3905 if (ZFS_IS_VOLUME(zhp))
3906 zc.zc_objset_type = DMU_OST_ZVOL;
3908 zc.zc_objset_type = DMU_OST_ZFS;
3910 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3911 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3913 zc.zc_cookie = recursive;
3915 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3917 * if it was recursive, the one that actually failed will
3920 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3921 "cannot rename '%s'"), zc.zc_name);
3923 if (recursive && errno == EEXIST) {
3924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3925 "a child dataset already has a snapshot "
3926 "with the new name"));
3927 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3929 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3933 * On failure, we still want to remount any filesystems that
3934 * were previously mounted, so we don't alter the system state.
3937 struct createdata cd;
3939 /* only create links for datasets that had existed */
3940 cd.cd_snapname = delim + 1;
3941 cd.cd_ifexists = B_TRUE;
3942 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3945 (void) changelist_postfix(cl);
3949 struct createdata cd;
3951 /* only create links for datasets that had existed */
3952 cd.cd_snapname = strchr(target, '@') + 1;
3953 cd.cd_ifexists = B_TRUE;
3954 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3957 changelist_rename(cl, zfs_get_name(zhp), target);
3958 ret = changelist_postfix(cl);
3970 changelist_free(cl);
3976 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3977 * and wait briefly for udev to create the /dev link.
3980 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3982 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3986 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3988 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3989 char path[MAXPATHLEN];
3992 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3995 * Issue the appropriate ioctl.
3997 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
4001 * Silently ignore the case where the link already
4002 * exists. This allows 'zfs volinit' to be run multiple
4003 * times without errors.
4009 * Dataset does not exist in the kernel. If we
4010 * don't care (see zfs_rename), then ignore the
4020 return (zfs_standard_error_fmt(hdl, errno,
4021 dgettext(TEXT_DOMAIN, "cannot create device links "
4022 "for '%s'"), dataset));
4027 * Wait up to 10 seconds for udev to create the device.
4029 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
4030 error = zpool_label_disk_wait(path, 10000);
4032 (void) printf(gettext("%s may not be immediately "
4033 "available\n"), path);
4039 * Remove a minor node for the given zvol and the associated /dev links.
4042 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4044 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4045 int timeout = 3000; /* in milliseconds */
4049 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4052 * Due to concurrent updates by udev the device may be reported as
4053 * busy. In this case don't immediately fail. Instead briefly delay
4054 * and retry the ioctl() which is now likely to succeed. If unable
4055 * remove the link after timeout milliseconds return the failure.
4057 for (i = 0; i < timeout; i++) {
4058 error = ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
4059 if (error && errno == EBUSY) {
4071 * Silently ignore the case where the link no longer
4072 * exists, so that 'zfs volfini' can be run multiple
4073 * times without errors.
4078 return (zfs_standard_error_fmt(hdl, errno,
4079 dgettext(TEXT_DOMAIN, "cannot remove device "
4080 "links for '%s': %s"), dataset, strerror(errno)));
4088 zfs_get_user_props(zfs_handle_t *zhp)
4090 return (zhp->zfs_user_props);
4094 * This function is used by 'zfs list' to determine the exact set of columns to
4095 * display, and their maximum widths. This does two main things:
4097 * - If this is a list of all properties, then expand the list to include
4098 * all native properties, and set a flag so that for each dataset we look
4099 * for new unique user properties and add them to the list.
4101 * - For non fixed-width properties, keep track of the maximum width seen
4102 * so that we can size the column appropriately. If the user has
4103 * requested received property values, we also need to compute the width
4104 * of the RECEIVED column.
4107 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
4109 libzfs_handle_t *hdl = zhp->zfs_hdl;
4110 zprop_list_t *entry;
4111 zprop_list_t **last, **start;
4112 nvlist_t *userprops, *propval;
4115 char buf[ZFS_MAXPROPLEN];
4117 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4120 userprops = zfs_get_user_props(zhp);
4123 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4125 * Go through and add any user properties as necessary. We
4126 * start by incrementing our list pointer to the first
4127 * non-native property.
4130 while (*start != NULL) {
4131 if ((*start)->pl_prop == ZPROP_INVAL)
4133 start = &(*start)->pl_next;
4137 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4139 * See if we've already found this property in our list.
4141 for (last = start; *last != NULL;
4142 last = &(*last)->pl_next) {
4143 if (strcmp((*last)->pl_user_prop,
4144 nvpair_name(elem)) == 0)
4148 if (*last == NULL) {
4149 if ((entry = zfs_alloc(hdl,
4150 sizeof (zprop_list_t))) == NULL ||
4151 ((entry->pl_user_prop = zfs_strdup(hdl,
4152 nvpair_name(elem)))) == NULL) {
4157 entry->pl_prop = ZPROP_INVAL;
4158 entry->pl_width = strlen(nvpair_name(elem));
4159 entry->pl_all = B_TRUE;
4166 * Now go through and check the width of any non-fixed columns
4168 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4169 if (entry->pl_fixed)
4172 if (entry->pl_prop != ZPROP_INVAL) {
4173 if (zfs_prop_get(zhp, entry->pl_prop,
4174 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4175 if (strlen(buf) > entry->pl_width)
4176 entry->pl_width = strlen(buf);
4178 if (received && zfs_prop_get_recvd(zhp,
4179 zfs_prop_to_name(entry->pl_prop),
4180 buf, sizeof (buf), B_FALSE) == 0)
4181 if (strlen(buf) > entry->pl_recvd_width)
4182 entry->pl_recvd_width = strlen(buf);
4184 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4186 verify(nvlist_lookup_string(propval,
4187 ZPROP_VALUE, &strval) == 0);
4188 if (strlen(strval) > entry->pl_width)
4189 entry->pl_width = strlen(strval);
4191 if (received && zfs_prop_get_recvd(zhp,
4192 entry->pl_user_prop,
4193 buf, sizeof (buf), B_FALSE) == 0)
4194 if (strlen(buf) > entry->pl_recvd_width)
4195 entry->pl_recvd_width = strlen(buf);
4203 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4208 * Keep a reference to the props-table against which we prune the
4211 zhp->zfs_props_table = props;
4213 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4216 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4217 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4220 * User properties will result in ZPROP_INVAL, and since we
4221 * only know how to prune standard ZFS properties, we always
4222 * leave these in the list. This can also happen if we
4223 * encounter an unknown DSL property (when running older
4224 * software, for example).
4226 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4227 (void) nvlist_remove(zhp->zfs_props,
4228 nvpair_name(curr), nvpair_type(curr));
4234 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4235 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4237 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4238 nvlist_t *nvlist = NULL;
4241 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4242 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4243 zc.zc_cookie = (uint64_t)cmd;
4245 if (cmd == ZFS_SMB_ACL_RENAME) {
4246 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4247 (void) no_memory(hdl);
4253 case ZFS_SMB_ACL_ADD:
4254 case ZFS_SMB_ACL_REMOVE:
4255 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4257 case ZFS_SMB_ACL_RENAME:
4258 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4260 (void) no_memory(hdl);
4263 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4265 (void) no_memory(hdl);
4268 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4269 nvlist_free(nvlist);
4273 case ZFS_SMB_ACL_PURGE:
4278 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4280 nvlist_free(nvlist);
4285 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4286 char *path, char *resource)
4288 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4293 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4294 char *path, char *resource)
4296 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4301 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4303 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4308 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4309 char *oldname, char *newname)
4311 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4316 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4317 zfs_userspace_cb_t func, void *arg)
4319 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4321 zfs_useracct_t buf[100];
4323 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4325 zc.zc_objset_type = type;
4326 zc.zc_nvlist_dst = (uintptr_t)buf;
4330 zfs_useracct_t *zua = buf;
4332 zc.zc_nvlist_dst_size = sizeof (buf);
4333 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4334 ZFS_IOC_USERSPACE_MANY, &zc);
4335 if (error || zc.zc_nvlist_dst_size == 0)
4338 while (zc.zc_nvlist_dst_size > 0) {
4339 error = func(arg, zua->zu_domain, zua->zu_rid,
4344 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4352 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4353 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4354 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4356 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4357 libzfs_handle_t *hdl = zhp->zfs_hdl;
4359 ASSERT(!recursive || dsobj == 0);
4361 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4362 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4363 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4364 >= sizeof (zc.zc_string))
4365 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4366 zc.zc_cookie = recursive;
4367 zc.zc_temphold = temphold;
4368 zc.zc_cleanup_fd = cleanup_fd;
4369 zc.zc_sendobj = dsobj;
4370 zc.zc_createtxg = createtxg;
4372 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4373 char errbuf[ZFS_MAXNAMELEN+32];
4376 * if it was recursive, the one that actually failed will be in
4379 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4380 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4384 * Temporary tags wind up having the ds object id
4385 * prepended. So even if we passed the length check
4386 * above, it's still possible for the tag to wind
4387 * up being slightly too long.
4389 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4392 "pool must be upgraded"));
4393 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4395 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4397 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4403 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4411 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4412 boolean_t recursive)
4414 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4415 libzfs_handle_t *hdl = zhp->zfs_hdl;
4417 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4418 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4419 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4420 >= sizeof (zc.zc_string))
4421 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4422 zc.zc_cookie = recursive;
4424 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4425 char errbuf[ZFS_MAXNAMELEN+32];
4428 * if it was recursive, the one that actually failed will be in
4431 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4432 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4436 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4439 "pool must be upgraded"));
4440 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4442 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4444 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4452 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4454 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4455 libzfs_handle_t *hdl = zhp->zfs_hdl;
4459 char errbuf[ZFS_MAXNAMELEN+32];
4461 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4462 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4466 nvbuf = malloc(nvsz);
4467 if (nvbuf == NULL) {
4468 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4472 zc.zc_nvlist_dst_size = nvsz;
4473 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4475 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4477 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4478 (void) snprintf(errbuf, sizeof (errbuf),
4479 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4484 nvsz = zc.zc_nvlist_dst_size;
4488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4489 "pool must be upgraded"));
4490 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4493 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4496 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4499 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4504 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4506 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4507 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4509 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4519 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4521 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4522 libzfs_handle_t *hdl = zhp->zfs_hdl;
4524 char errbuf[ZFS_MAXNAMELEN+32];
4528 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4529 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4531 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4534 nvbuf = malloc(nvsz);
4536 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4539 zc.zc_nvlist_src_size = nvsz;
4540 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4541 zc.zc_perm_action = un;
4543 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4545 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4546 (void) snprintf(errbuf, sizeof (errbuf),
4547 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4552 "pool must be upgraded"));
4553 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4556 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4559 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4562 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4573 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4575 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4576 libzfs_handle_t *hdl = zhp->zfs_hdl;
4580 char errbuf[ZFS_MAXNAMELEN+32];
4582 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4586 nvbuf = malloc(nvsz);
4587 if (nvbuf == NULL) {
4588 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4592 zc.zc_nvlist_dst_size = nvsz;
4593 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4595 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4597 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4598 (void) snprintf(errbuf, sizeof (errbuf),
4599 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4604 nvsz = zc.zc_nvlist_dst_size;
4608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4609 "pool must be upgraded"));
4610 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4613 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4616 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4619 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4624 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4626 (void) snprintf(errbuf, sizeof (errbuf),
4627 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4629 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4639 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4642 uint64_t nblocks, volblocksize;
4646 if (nvlist_lookup_string(props,
4647 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4648 ncopies = atoi(strval);
4651 if (nvlist_lookup_uint64(props,
4652 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4653 &volblocksize) != 0)
4654 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4655 nblocks = volsize/volblocksize;
4656 /* start with metadnode L0-L6 */
4658 /* calculate number of indirects */
4659 while (nblocks > 1) {
4660 nblocks += DNODES_PER_LEVEL - 1;
4661 nblocks /= DNODES_PER_LEVEL;
4664 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4667 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4668 * compressed, but in practice they compress down to about
4671 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;