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) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
41 #include <sys/mntent.h>
42 #include <sys/mount.h>
51 #include <directory.h>
52 #endif /* HAVE_IDMAP */
54 #include <sys/dnode.h>
59 #include "zfs_namecheck.h"
61 #include "libzfs_impl.h"
62 #include "zfs_deleg.h"
64 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
65 static int userquota_propname_decode(const char *propname, boolean_t zoned,
66 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
69 * Given a single type (not a mask of types), return the type in a human
73 zfs_type_to_name(zfs_type_t type)
76 case ZFS_TYPE_FILESYSTEM:
77 return (dgettext(TEXT_DOMAIN, "filesystem"));
78 case ZFS_TYPE_SNAPSHOT:
79 return (dgettext(TEXT_DOMAIN, "snapshot"));
81 return (dgettext(TEXT_DOMAIN, "volume"));
90 * Validate a ZFS path. This is used even before trying to open the dataset, to
91 * provide a more meaningful error message. We call zfs_error_aux() to
92 * explain exactly why the name was not valid.
95 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
101 (void) zfs_prop_get_table();
102 if (dataset_namecheck(path, &why, &what) != 0) {
105 case NAME_ERR_TOOLONG:
106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
107 "name is too long"));
110 case NAME_ERR_LEADING_SLASH:
111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
112 "leading slash in name"));
115 case NAME_ERR_EMPTY_COMPONENT:
116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
117 "empty component in name"));
120 case NAME_ERR_TRAILING_SLASH:
121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
122 "trailing slash in name"));
125 case NAME_ERR_INVALCHAR:
127 dgettext(TEXT_DOMAIN, "invalid character "
128 "'%c' in name"), what);
131 case NAME_ERR_MULTIPLE_AT:
132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133 "multiple '@' delimiters in name"));
136 case NAME_ERR_NOLETTER:
137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
138 "pool doesn't begin with a letter"));
141 case NAME_ERR_RESERVED:
142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 "name is reserved"));
146 case NAME_ERR_DISKLIKE:
147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148 "reserved disk name"));
158 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
161 "snapshot delimiter '@' in filesystem name"));
165 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
168 "missing '@' delimiter in snapshot name"));
172 if (modifying && strchr(path, '%') != NULL) {
174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
175 "invalid character %c in name"), '%');
183 zfs_name_valid(const char *name, zfs_type_t type)
185 if (type == ZFS_TYPE_POOL)
186 return (zpool_name_valid(NULL, B_FALSE, name));
187 return (zfs_validate_name(NULL, name, type, B_FALSE));
191 * This function takes the raw DSL properties, and filters out the user-defined
192 * properties into a separate nvlist.
195 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
197 libzfs_handle_t *hdl = zhp->zfs_hdl;
202 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
203 (void) no_memory(hdl);
208 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
209 if (!zfs_prop_user(nvpair_name(elem)))
212 verify(nvpair_value_nvlist(elem, &propval) == 0);
213 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
215 (void) no_memory(hdl);
223 static zpool_handle_t *
224 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
226 libzfs_handle_t *hdl = zhp->zfs_hdl;
229 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
230 if (hdl->libzfs_pool_handles != NULL)
231 zph->zpool_next = hdl->libzfs_pool_handles;
232 hdl->libzfs_pool_handles = zph;
237 static zpool_handle_t *
238 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
240 libzfs_handle_t *hdl = zhp->zfs_hdl;
241 zpool_handle_t *zph = hdl->libzfs_pool_handles;
243 while ((zph != NULL) &&
244 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
245 zph = zph->zpool_next;
250 * Returns a handle to the pool that contains the provided dataset.
251 * If a handle to that pool already exists then that handle is returned.
252 * Otherwise, a new handle is created and added to the list of handles.
254 static zpool_handle_t *
255 zpool_handle(zfs_handle_t *zhp)
261 len = strcspn(zhp->zfs_name, "/@") + 1;
262 pool_name = zfs_alloc(zhp->zfs_hdl, len);
263 (void) strlcpy(pool_name, zhp->zfs_name, len);
265 zph = zpool_find_handle(zhp, pool_name, len);
267 zph = zpool_add_handle(zhp, pool_name);
274 zpool_free_handles(libzfs_handle_t *hdl)
276 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
278 while (zph != NULL) {
279 next = zph->zpool_next;
283 hdl->libzfs_pool_handles = NULL;
287 * Utility function to gather stats (objset and zpl) for the given object.
290 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
292 libzfs_handle_t *hdl = zhp->zfs_hdl;
294 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
296 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
297 if (errno == ENOMEM) {
298 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
309 * Utility function to get the received properties of the given object.
312 get_recvd_props_ioctl(zfs_handle_t *zhp)
314 libzfs_handle_t *hdl = zhp->zfs_hdl;
315 nvlist_t *recvdprops;
316 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
319 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
322 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
324 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
325 if (errno == ENOMEM) {
326 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
330 zcmd_free_nvlists(&zc);
335 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
336 zcmd_free_nvlists(&zc);
340 nvlist_free(zhp->zfs_recvd_props);
341 zhp->zfs_recvd_props = recvdprops;
347 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
349 nvlist_t *allprops, *userprops;
351 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
353 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
358 * XXX Why do we store the user props separately, in addition to
359 * storing them in zfs_props?
361 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
362 nvlist_free(allprops);
366 nvlist_free(zhp->zfs_props);
367 nvlist_free(zhp->zfs_user_props);
369 zhp->zfs_props = allprops;
370 zhp->zfs_user_props = userprops;
376 get_stats(zfs_handle_t *zhp)
379 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
381 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
383 if (get_stats_ioctl(zhp, &zc) != 0)
385 else if (put_stats_zhdl(zhp, &zc) != 0)
387 zcmd_free_nvlists(&zc);
392 * Refresh the properties currently stored in the handle.
395 zfs_refresh_properties(zfs_handle_t *zhp)
397 (void) get_stats(zhp);
401 * Makes a handle from the given dataset name. Used by zfs_open() and
402 * zfs_iter_* to create child handles on the fly.
405 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
407 if (put_stats_zhdl(zhp, zc) != 0)
411 * We've managed to open the dataset and gather statistics. Determine
412 * the high-level type.
414 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
415 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
416 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
417 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
418 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
419 return (-1); /* zpios' and other testing datasets are
420 of this type, ignore if encountered */
424 if (zhp->zfs_dmustats.dds_is_snapshot)
425 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
426 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
427 zhp->zfs_type = ZFS_TYPE_VOLUME;
428 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
429 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
431 abort(); /* we should never see any other types */
433 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
440 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
442 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
444 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
450 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
451 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
455 if (get_stats_ioctl(zhp, &zc) == -1) {
456 zcmd_free_nvlists(&zc);
460 if (make_dataset_handle_common(zhp, &zc) == -1) {
464 zcmd_free_nvlists(&zc);
469 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
471 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
477 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
478 if (make_dataset_handle_common(zhp, zc) == -1) {
486 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
488 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
493 zhp->zfs_hdl = pzhp->zfs_hdl;
494 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
495 zhp->zfs_head_type = pzhp->zfs_type;
496 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
497 zhp->zpool_hdl = zpool_handle(zhp);
503 zfs_handle_dup(zfs_handle_t *zhp_orig)
505 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
510 zhp->zfs_hdl = zhp_orig->zfs_hdl;
511 zhp->zpool_hdl = zhp_orig->zpool_hdl;
512 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
513 sizeof (zhp->zfs_name));
514 zhp->zfs_type = zhp_orig->zfs_type;
515 zhp->zfs_head_type = zhp_orig->zfs_head_type;
516 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
517 if (zhp_orig->zfs_props != NULL) {
518 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
519 (void) no_memory(zhp->zfs_hdl);
524 if (zhp_orig->zfs_user_props != NULL) {
525 if (nvlist_dup(zhp_orig->zfs_user_props,
526 &zhp->zfs_user_props, 0) != 0) {
527 (void) no_memory(zhp->zfs_hdl);
532 if (zhp_orig->zfs_recvd_props != NULL) {
533 if (nvlist_dup(zhp_orig->zfs_recvd_props,
534 &zhp->zfs_recvd_props, 0)) {
535 (void) no_memory(zhp->zfs_hdl);
540 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
541 if (zhp_orig->zfs_mntopts != NULL) {
542 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
543 zhp_orig->zfs_mntopts);
545 zhp->zfs_props_table = zhp_orig->zfs_props_table;
550 * Opens the given snapshot, filesystem, or volume. The 'types'
551 * argument is a mask of acceptable types. The function will print an
552 * appropriate error message and return NULL if it can't be opened.
555 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
560 (void) snprintf(errbuf, sizeof (errbuf),
561 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
564 * Validate the name before we even try to open it.
566 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
568 "invalid dataset name"));
569 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
574 * Try to get stats for the dataset, which will tell us if it exists.
577 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
578 (void) zfs_standard_error(hdl, errno, errbuf);
582 if (!(types & zhp->zfs_type)) {
583 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
592 * Release a ZFS handle. Nothing to do but free the associated memory.
595 zfs_close(zfs_handle_t *zhp)
597 if (zhp->zfs_mntopts)
598 free(zhp->zfs_mntopts);
599 nvlist_free(zhp->zfs_props);
600 nvlist_free(zhp->zfs_user_props);
601 nvlist_free(zhp->zfs_recvd_props);
605 typedef struct mnttab_node {
606 struct mnttab mtn_mt;
611 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
613 const mnttab_node_t *mtn1 = arg1;
614 const mnttab_node_t *mtn2 = arg2;
617 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
621 return (rv > 0 ? 1 : -1);
625 libzfs_mnttab_init(libzfs_handle_t *hdl)
627 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
628 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
629 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
633 libzfs_mnttab_update(libzfs_handle_t *hdl)
637 /* Reopen MNTTAB to prevent reading stale data from open file */
638 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
641 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
644 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
646 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
647 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
648 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
649 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
650 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
651 avl_add(&hdl->libzfs_mnttab_cache, mtn);
658 libzfs_mnttab_fini(libzfs_handle_t *hdl)
663 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
664 free(mtn->mtn_mt.mnt_special);
665 free(mtn->mtn_mt.mnt_mountp);
666 free(mtn->mtn_mt.mnt_fstype);
667 free(mtn->mtn_mt.mnt_mntopts);
670 avl_destroy(&hdl->libzfs_mnttab_cache);
674 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
676 hdl->libzfs_mnttab_enable = enable;
680 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
681 struct mnttab *entry)
687 if (!hdl->libzfs_mnttab_enable) {
688 struct mnttab srch = { 0 };
690 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
691 libzfs_mnttab_fini(hdl);
693 /* Reopen MNTTAB to prevent reading stale data from open file */
694 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
697 srch.mnt_special = (char *)fsname;
698 srch.mnt_fstype = MNTTYPE_ZFS;
699 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
705 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
706 if ((error = libzfs_mnttab_update(hdl)) != 0)
709 find.mtn_mt.mnt_special = (char *)fsname;
710 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
712 *entry = mtn->mtn_mt;
719 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
720 const char *mountp, const char *mntopts)
724 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
726 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
727 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
728 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
729 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
730 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
731 avl_add(&hdl->libzfs_mnttab_cache, mtn);
735 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
740 find.mtn_mt.mnt_special = (char *)fsname;
741 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
742 avl_remove(&hdl->libzfs_mnttab_cache, ret);
743 free(ret->mtn_mt.mnt_special);
744 free(ret->mtn_mt.mnt_mountp);
745 free(ret->mtn_mt.mnt_fstype);
746 free(ret->mtn_mt.mnt_mntopts);
752 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
754 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
756 if (zpool_handle == NULL)
759 *spa_version = zpool_get_prop_int(zpool_handle,
760 ZPOOL_PROP_VERSION, NULL);
765 * The choice of reservation property depends on the SPA version.
768 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
772 if (zfs_spa_version(zhp, &spa_version) < 0)
775 if (spa_version >= SPA_VERSION_REFRESERVATION)
776 *resv_prop = ZFS_PROP_REFRESERVATION;
778 *resv_prop = ZFS_PROP_RESERVATION;
784 * Given an nvlist of properties to set, validates that they are correct, and
785 * parses any numeric properties (index, boolean, etc) if they are specified as
789 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
790 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
797 int chosen_normal = -1;
800 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
801 (void) no_memory(hdl);
806 * Make sure this property is valid and applies to this type.
810 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
811 const char *propname = nvpair_name(elem);
813 prop = zfs_name_to_prop(propname);
814 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
816 * This is a user property: make sure it's a
817 * string, and that it's less than ZAP_MAXNAMELEN.
819 if (nvpair_type(elem) != DATA_TYPE_STRING) {
820 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
821 "'%s' must be a string"), propname);
822 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
826 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
827 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
828 "property name '%s' is too long"),
830 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
834 (void) nvpair_value_string(elem, &strval);
835 if (nvlist_add_string(ret, propname, strval) != 0) {
836 (void) no_memory(hdl);
843 * Currently, only user properties can be modified on
846 if (type == ZFS_TYPE_SNAPSHOT) {
847 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
848 "this property can not be modified for snapshots"));
849 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
853 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
854 zfs_userquota_prop_t uqtype;
855 char newpropname[128];
860 if (userquota_propname_decode(propname, zoned,
861 &uqtype, domain, sizeof (domain), &rid) != 0) {
863 dgettext(TEXT_DOMAIN,
864 "'%s' has an invalid user/group name"),
866 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
870 if (uqtype != ZFS_PROP_USERQUOTA &&
871 uqtype != ZFS_PROP_GROUPQUOTA) {
873 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
875 (void) zfs_error(hdl, EZFS_PROPREADONLY,
880 if (nvpair_type(elem) == DATA_TYPE_STRING) {
881 (void) nvpair_value_string(elem, &strval);
882 if (strcmp(strval, "none") == 0) {
884 } else if (zfs_nicestrtonum(hdl,
885 strval, &intval) != 0) {
886 (void) zfs_error(hdl,
887 EZFS_BADPROP, errbuf);
890 } else if (nvpair_type(elem) ==
892 (void) nvpair_value_uint64(elem, &intval);
894 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
895 "use 'none' to disable "
896 "userquota/groupquota"));
900 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
901 "'%s' must be a number"), propname);
902 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
907 * Encode the prop name as
908 * userquota@<hex-rid>-domain, to make it easy
909 * for the kernel to decode.
911 (void) snprintf(newpropname, sizeof (newpropname),
912 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
913 (longlong_t)rid, domain);
917 if (nvlist_add_uint64_array(ret, newpropname,
919 (void) no_memory(hdl);
923 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
927 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
931 if (prop == ZPROP_INVAL) {
932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 "invalid property '%s'"), propname);
934 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
938 if (!zfs_prop_valid_for_type(prop, type)) {
940 dgettext(TEXT_DOMAIN, "'%s' does not "
941 "apply to datasets of this type"), propname);
942 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
946 if (zfs_prop_readonly(prop) &&
947 (!zfs_prop_setonce(prop) || zhp != NULL)) {
949 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
951 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
955 if (zprop_parse_value(hdl, elem, prop, type, ret,
956 &strval, &intval, errbuf) != 0)
960 * Perform some additional checks for specific properties.
963 case ZFS_PROP_VERSION:
969 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
970 if (intval < version) {
971 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
972 "Can not downgrade; already at version %u"),
974 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
980 case ZFS_PROP_RECORDSIZE:
981 case ZFS_PROP_VOLBLOCKSIZE:
982 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
983 if (intval < SPA_MINBLOCKSIZE ||
984 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986 "'%s' must be power of 2 from %u "
988 (uint_t)SPA_MINBLOCKSIZE,
989 (uint_t)SPA_MAXBLOCKSIZE >> 10);
990 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
995 case ZFS_PROP_MLSLABEL:
999 * Verify the mlslabel string and convert to
1000 * internal hex label string.
1004 char *hex = NULL; /* internal label string */
1006 /* Default value is already OK. */
1007 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1010 /* Verify the label can be converted to binary form */
1011 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1012 (str_to_label(strval, &new_sl, MAC_LABEL,
1013 L_NO_CORRECTION, NULL) == -1)) {
1017 /* Now translate to hex internal label string */
1018 if (label_to_str(new_sl, &hex, M_INTERNAL,
1024 m_label_free(new_sl);
1026 /* If string is already in internal form, we're done. */
1027 if (strcmp(strval, hex) == 0) {
1032 /* Replace the label string with the internal form. */
1033 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1035 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1043 "invalid mlslabel '%s'"), strval);
1044 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1045 m_label_free(new_sl); /* OK if null */
1048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1049 "mlslabels are unsupported"));
1050 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1052 #endif /* HAVE_MLSLABEL */
1055 case ZFS_PROP_MOUNTPOINT:
1057 namecheck_err_t why;
1059 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1060 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1063 if (mountpoint_namecheck(strval, &why)) {
1065 case NAME_ERR_LEADING_SLASH:
1067 dgettext(TEXT_DOMAIN,
1068 "'%s' must be an absolute path, "
1069 "'none', or 'legacy'"), propname);
1071 case NAME_ERR_TOOLONG:
1073 dgettext(TEXT_DOMAIN,
1074 "component of '%s' is too long"),
1080 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1087 case ZFS_PROP_SHARESMB:
1088 case ZFS_PROP_SHARENFS:
1090 * For the mountpoint and sharenfs or sharesmb
1091 * properties, check if it can be set in a
1092 * global/non-global zone based on
1093 * the zoned property value:
1095 * global zone non-global zone
1096 * --------------------------------------------------
1097 * zoned=on mountpoint (no) mountpoint (yes)
1098 * sharenfs (no) sharenfs (no)
1099 * sharesmb (no) sharesmb (no)
1101 * zoned=off mountpoint (yes) N/A
1106 if (getzoneid() == GLOBAL_ZONEID) {
1107 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1108 "'%s' cannot be set on "
1109 "dataset in a non-global zone"),
1111 (void) zfs_error(hdl, EZFS_ZONED,
1114 } else if (prop == ZFS_PROP_SHARENFS ||
1115 prop == ZFS_PROP_SHARESMB) {
1116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1117 "'%s' cannot be set in "
1118 "a non-global zone"), propname);
1119 (void) zfs_error(hdl, EZFS_ZONED,
1123 } else if (getzoneid() != GLOBAL_ZONEID) {
1125 * If zoned property is 'off', this must be in
1126 * a global zone. If not, something is wrong.
1128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1129 "'%s' cannot be set while dataset "
1130 "'zoned' property is set"), propname);
1131 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1136 * At this point, it is legitimate to set the
1137 * property. Now we want to make sure that the
1138 * property value is valid if it is sharenfs.
1140 if ((prop == ZFS_PROP_SHARENFS ||
1141 prop == ZFS_PROP_SHARESMB) &&
1142 strcmp(strval, "on") != 0 &&
1143 strcmp(strval, "off") != 0) {
1144 zfs_share_proto_t proto;
1146 if (prop == ZFS_PROP_SHARESMB)
1152 * Must be an valid sharing protocol
1153 * option string so init the libshare
1154 * in order to enable the parser and
1155 * then parse the options. We use the
1156 * control API since we don't care about
1157 * the current configuration and don't
1158 * want the overhead of loading it
1159 * until we actually do something.
1162 if (zfs_init_libshare(hdl,
1163 SA_INIT_CONTROL_API) != SA_OK) {
1165 * An error occurred so we can't do
1168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1169 "'%s' cannot be set: problem "
1170 "in share initialization"),
1172 (void) zfs_error(hdl, EZFS_BADPROP,
1177 if (zfs_parse_options(strval, proto) != SA_OK) {
1179 * There was an error in parsing so
1180 * deal with it by issuing an error
1181 * message and leaving after
1182 * uninitializing the the libshare
1185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1186 "'%s' cannot be set to invalid "
1187 "options"), propname);
1188 (void) zfs_error(hdl, EZFS_BADPROP,
1190 zfs_uninit_libshare(hdl);
1193 zfs_uninit_libshare(hdl);
1197 case ZFS_PROP_UTF8ONLY:
1198 chosen_utf = (int)intval;
1200 case ZFS_PROP_NORMALIZE:
1201 chosen_normal = (int)intval;
1208 * For changes to existing volumes, we have some additional
1209 * checks to enforce.
1211 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1212 uint64_t volsize = zfs_prop_get_int(zhp,
1214 uint64_t blocksize = zfs_prop_get_int(zhp,
1215 ZFS_PROP_VOLBLOCKSIZE);
1219 case ZFS_PROP_RESERVATION:
1220 case ZFS_PROP_REFRESERVATION:
1221 if (intval > volsize) {
1222 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1223 "'%s' is greater than current "
1224 "volume size"), propname);
1225 (void) zfs_error(hdl, EZFS_BADPROP,
1231 case ZFS_PROP_VOLSIZE:
1232 if (intval % blocksize != 0) {
1233 zfs_nicenum(blocksize, buf,
1235 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1236 "'%s' must be a multiple of "
1237 "volume block size (%s)"),
1239 (void) zfs_error(hdl, EZFS_BADPROP,
1245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1246 "'%s' cannot be zero"),
1248 (void) zfs_error(hdl, EZFS_BADPROP,
1260 * If normalization was chosen, but no UTF8 choice was made,
1261 * enforce rejection of non-UTF8 names.
1263 * If normalization was chosen, but rejecting non-UTF8 names
1264 * was explicitly not chosen, it is an error.
1266 if (chosen_normal > 0 && chosen_utf < 0) {
1267 if (nvlist_add_uint64(ret,
1268 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1269 (void) no_memory(hdl);
1272 } else if (chosen_normal > 0 && chosen_utf == 0) {
1273 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1274 "'%s' must be set 'on' if normalization chosen"),
1275 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1276 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1287 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1289 uint64_t old_volsize;
1290 uint64_t new_volsize;
1291 uint64_t old_reservation;
1292 uint64_t new_reservation;
1293 zfs_prop_t resv_prop;
1296 * If this is an existing volume, and someone is setting the volsize,
1297 * make sure that it matches the reservation, or add it if necessary.
1299 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1300 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1302 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1303 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1304 old_reservation) || nvlist_lookup_uint64(nvl,
1305 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1308 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1311 new_reservation = zvol_volsize_to_reservation(new_volsize,
1313 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1314 new_reservation) != 0) {
1315 (void) no_memory(zhp->zfs_hdl);
1322 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1329 * For quotas and reservations, ENOSPC indicates
1330 * something different; setting a quota or reservation
1331 * doesn't use any disk space.
1334 case ZFS_PROP_QUOTA:
1335 case ZFS_PROP_REFQUOTA:
1336 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1337 "size is less than current used or "
1339 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1342 case ZFS_PROP_RESERVATION:
1343 case ZFS_PROP_REFRESERVATION:
1344 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1345 "size is greater than available space"));
1346 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1350 (void) zfs_standard_error(hdl, err, errbuf);
1356 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1360 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1364 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1365 "pool and or dataset must be upgraded to set this "
1366 "property or value"));
1367 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1371 if (prop == ZFS_PROP_COMPRESSION) {
1372 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1373 "property setting is not allowed on "
1374 "bootable datasets"));
1375 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1377 (void) zfs_standard_error(hdl, err, errbuf);
1382 if (prop == ZPROP_INVAL) {
1383 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1385 (void) zfs_standard_error(hdl, err, errbuf);
1391 * This platform can't address a volume this big.
1394 if (prop == ZFS_PROP_VOLSIZE) {
1395 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1401 (void) zfs_standard_error(hdl, err, errbuf);
1406 zfs_is_namespace_prop(zfs_prop_t prop)
1410 case ZFS_PROP_ATIME:
1411 case ZFS_PROP_DEVICES:
1413 case ZFS_PROP_SETUID:
1414 case ZFS_PROP_READONLY:
1415 case ZFS_PROP_XATTR:
1416 case ZFS_PROP_NBMAND:
1425 * Given a property name and value, set the property for the given dataset.
1428 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1430 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1432 prop_changelist_t *cl = NULL;
1434 libzfs_handle_t *hdl = zhp->zfs_hdl;
1435 nvlist_t *nvl = NULL, *realprops;
1437 boolean_t do_prefix;
1441 (void) snprintf(errbuf, sizeof (errbuf),
1442 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1445 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1446 nvlist_add_string(nvl, propname, propval) != 0) {
1447 (void) no_memory(hdl);
1451 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1452 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1458 prop = zfs_name_to_prop(propname);
1460 if (prop == ZFS_PROP_VOLSIZE) {
1461 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1465 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1468 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1470 "child dataset with inherited mountpoint is used "
1471 "in a non-global zone"));
1472 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1477 * If the dataset's canmount property is being set to noauto,
1478 * then we want to prevent unmounting & remounting it.
1480 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1481 (zprop_string_to_index(prop, propval, &idx,
1482 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1484 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1488 * Execute the corresponding ioctl() to set this property.
1490 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1492 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1495 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1498 zfs_setprop_error(hdl, prop, errno, errbuf);
1499 if (added_resv && errno == ENOSPC) {
1500 /* clean up the volsize property we tried to set */
1501 uint64_t old_volsize = zfs_prop_get_int(zhp,
1504 zcmd_free_nvlists(&zc);
1505 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1507 if (nvlist_add_uint64(nvl,
1508 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1511 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1513 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1517 ret = changelist_postfix(cl);
1521 * Refresh the statistics so the new property
1522 * value is reflected.
1524 (void) get_stats(zhp);
1527 * Remount the filesystem to propagate the change
1528 * if one of the options handled by the generic
1529 * Linux namespace layer has been modified.
1531 if (zfs_is_namespace_prop(prop) &&
1532 zfs_is_mounted(zhp, NULL))
1533 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1539 zcmd_free_nvlists(&zc);
1541 changelist_free(cl);
1546 * Given a property, inherit the value from the parent dataset, or if received
1547 * is TRUE, revert to the received value, if any.
1550 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1552 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1554 prop_changelist_t *cl;
1555 libzfs_handle_t *hdl = zhp->zfs_hdl;
1559 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1560 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1562 zc.zc_cookie = received;
1563 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1565 * For user properties, the amount of work we have to do is very
1566 * small, so just do it here.
1568 if (!zfs_prop_user(propname)) {
1569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1570 "invalid property"));
1571 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1574 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1575 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1577 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1578 return (zfs_standard_error(hdl, errno, errbuf));
1584 * Verify that this property is inheritable.
1586 if (zfs_prop_readonly(prop))
1587 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1589 if (!zfs_prop_inheritable(prop) && !received)
1590 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1593 * Check to see if the value applies to this type
1595 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1596 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1599 * Normalize the name, to get rid of shorthand abbreviations.
1601 propname = zfs_prop_to_name(prop);
1602 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1603 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1605 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1606 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1607 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1608 "dataset is used in a non-global zone"));
1609 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1613 * Determine datasets which will be affected by this change, if any.
1615 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1618 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1619 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1620 "child dataset with inherited mountpoint is used "
1621 "in a non-global zone"));
1622 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1626 if ((ret = changelist_prefix(cl)) != 0)
1629 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1630 return (zfs_standard_error(hdl, errno, errbuf));
1633 if ((ret = changelist_postfix(cl)) != 0)
1637 * Refresh the statistics so the new property is reflected.
1639 (void) get_stats(zhp);
1643 changelist_free(cl);
1648 * True DSL properties are stored in an nvlist. The following two functions
1649 * extract them appropriately.
1652 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1658 if (nvlist_lookup_nvlist(zhp->zfs_props,
1659 zfs_prop_to_name(prop), &nv) == 0) {
1660 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1661 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1663 verify(!zhp->zfs_props_table ||
1664 zhp->zfs_props_table[prop] == B_TRUE);
1665 value = zfs_prop_default_numeric(prop);
1673 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1679 if (nvlist_lookup_nvlist(zhp->zfs_props,
1680 zfs_prop_to_name(prop), &nv) == 0) {
1681 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1682 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1684 verify(!zhp->zfs_props_table ||
1685 zhp->zfs_props_table[prop] == B_TRUE);
1686 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1695 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1697 return (zhp->zfs_props == zhp->zfs_recvd_props);
1701 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1703 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1704 zhp->zfs_props = zhp->zfs_recvd_props;
1708 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1710 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1715 * Internal function for getting a numeric property. Both zfs_prop_get() and
1716 * zfs_prop_get_int() are built using this interface.
1718 * Certain properties can be overridden using 'mount -o'. In this case, scan
1719 * the contents of the /etc/mtab entry, searching for the appropriate options.
1720 * If they differ from the on-disk values, report the current values and mark
1721 * the source "temporary".
1724 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1725 char **source, uint64_t *val)
1727 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1728 nvlist_t *zplprops = NULL;
1730 char *mntopt_on = NULL;
1731 char *mntopt_off = NULL;
1732 boolean_t received = zfs_is_recvd_props_mode(zhp);
1737 case ZFS_PROP_ATIME:
1738 mntopt_on = MNTOPT_ATIME;
1739 mntopt_off = MNTOPT_NOATIME;
1742 case ZFS_PROP_DEVICES:
1743 mntopt_on = MNTOPT_DEVICES;
1744 mntopt_off = MNTOPT_NODEVICES;
1748 mntopt_on = MNTOPT_EXEC;
1749 mntopt_off = MNTOPT_NOEXEC;
1752 case ZFS_PROP_READONLY:
1753 mntopt_on = MNTOPT_RO;
1754 mntopt_off = MNTOPT_RW;
1757 case ZFS_PROP_SETUID:
1758 mntopt_on = MNTOPT_SETUID;
1759 mntopt_off = MNTOPT_NOSETUID;
1762 case ZFS_PROP_XATTR:
1763 mntopt_on = MNTOPT_XATTR;
1764 mntopt_off = MNTOPT_NOXATTR;
1767 case ZFS_PROP_NBMAND:
1768 mntopt_on = MNTOPT_NBMAND;
1769 mntopt_off = MNTOPT_NONBMAND;
1776 * Because looking up the mount options is potentially expensive
1777 * (iterating over all of /etc/mtab), we defer its calculation until
1778 * we're looking up a property which requires its presence.
1780 if (!zhp->zfs_mntcheck &&
1781 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1782 libzfs_handle_t *hdl = zhp->zfs_hdl;
1783 struct mnttab entry;
1785 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1786 zhp->zfs_mntopts = zfs_strdup(hdl,
1788 if (zhp->zfs_mntopts == NULL)
1792 zhp->zfs_mntcheck = B_TRUE;
1795 if (zhp->zfs_mntopts == NULL)
1796 mnt.mnt_mntopts = "";
1798 mnt.mnt_mntopts = zhp->zfs_mntopts;
1801 case ZFS_PROP_ATIME:
1802 case ZFS_PROP_DEVICES:
1804 case ZFS_PROP_READONLY:
1805 case ZFS_PROP_SETUID:
1806 case ZFS_PROP_XATTR:
1807 case ZFS_PROP_NBMAND:
1808 *val = getprop_uint64(zhp, prop, source);
1813 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1816 *src = ZPROP_SRC_TEMPORARY;
1817 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1820 *src = ZPROP_SRC_TEMPORARY;
1824 case ZFS_PROP_CANMOUNT:
1825 case ZFS_PROP_VOLSIZE:
1826 case ZFS_PROP_QUOTA:
1827 case ZFS_PROP_REFQUOTA:
1828 case ZFS_PROP_RESERVATION:
1829 case ZFS_PROP_REFRESERVATION:
1830 *val = getprop_uint64(zhp, prop, source);
1832 if (*source == NULL) {
1833 /* not default, must be local */
1834 *source = zhp->zfs_name;
1838 case ZFS_PROP_MOUNTED:
1839 *val = (zhp->zfs_mntopts != NULL);
1842 case ZFS_PROP_NUMCLONES:
1843 *val = zhp->zfs_dmustats.dds_num_clones;
1846 case ZFS_PROP_VERSION:
1847 case ZFS_PROP_NORMALIZE:
1848 case ZFS_PROP_UTF8ONLY:
1850 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1851 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1853 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1854 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1855 zcmd_free_nvlists(&zc);
1858 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1859 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1861 zcmd_free_nvlists(&zc);
1865 nvlist_free(zplprops);
1866 zcmd_free_nvlists(&zc);
1870 switch (zfs_prop_get_type(prop)) {
1871 case PROP_TYPE_NUMBER:
1872 case PROP_TYPE_INDEX:
1873 *val = getprop_uint64(zhp, prop, source);
1875 * If we tried to use a default value for a
1876 * readonly property, it means that it was not
1879 if (zfs_prop_readonly(prop) &&
1880 *source != NULL && (*source)[0] == '\0') {
1885 case PROP_TYPE_STRING:
1887 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1888 "cannot get non-numeric property"));
1889 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1890 dgettext(TEXT_DOMAIN, "internal error")));
1898 * Calculate the source type, given the raw source string.
1901 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1902 char *statbuf, size_t statlen)
1904 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1907 if (source == NULL) {
1908 *srctype = ZPROP_SRC_NONE;
1909 } else if (source[0] == '\0') {
1910 *srctype = ZPROP_SRC_DEFAULT;
1911 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1912 *srctype = ZPROP_SRC_RECEIVED;
1914 if (strcmp(source, zhp->zfs_name) == 0) {
1915 *srctype = ZPROP_SRC_LOCAL;
1917 (void) strlcpy(statbuf, source, statlen);
1918 *srctype = ZPROP_SRC_INHERITED;
1925 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1926 size_t proplen, boolean_t literal)
1931 if (zhp->zfs_recvd_props == NULL)
1932 if (get_recvd_props_ioctl(zhp) != 0)
1935 prop = zfs_name_to_prop(propname);
1937 if (prop != ZPROP_INVAL) {
1939 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1941 zfs_set_recvd_props_mode(zhp, &cookie);
1942 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1943 NULL, NULL, 0, literal);
1944 zfs_unset_recvd_props_mode(zhp, &cookie);
1948 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1949 propname, &propval) != 0)
1951 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1953 (void) strlcpy(propbuf, recvdval, proplen);
1956 return (err == 0 ? 0 : -1);
1960 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1965 value = zfs_get_clones_nvl(zhp);
1970 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1971 pair = nvlist_next_nvpair(value, pair)) {
1972 if (propbuf[0] != '\0')
1973 (void) strlcat(propbuf, ",", proplen);
1974 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1980 struct get_clones_arg {
1984 char buf[ZFS_MAXNAMELEN];
1988 get_clones_cb(zfs_handle_t *zhp, void *arg)
1990 struct get_clones_arg *gca = arg;
1992 if (gca->numclones == 0) {
1997 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
1998 NULL, NULL, 0, B_TRUE) != 0)
2000 if (strcmp(gca->buf, gca->origin) == 0) {
2001 if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) {
2003 return (no_memory(zhp->zfs_hdl));
2009 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2015 zfs_get_clones_nvl(zfs_handle_t *zhp)
2017 nvlist_t *nv, *value;
2019 if (nvlist_lookup_nvlist(zhp->zfs_props,
2020 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2021 struct get_clones_arg gca;
2024 * if this is a snapshot, then the kernel wasn't able
2025 * to get the clones. Do it by slowly iterating.
2027 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2029 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2031 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2036 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2038 gca.origin = zhp->zfs_name;
2040 if (gca.numclones != 0) {
2042 char pool[ZFS_MAXNAMELEN];
2045 /* get the pool name */
2046 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2047 (void) strsep(&cp, "/@");
2048 root = zfs_open(zhp->zfs_hdl, pool,
2049 ZFS_TYPE_FILESYSTEM);
2051 (void) get_clones_cb(root, &gca);
2054 if (gca.numclones != 0 ||
2055 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2056 nvlist_add_nvlist(zhp->zfs_props,
2057 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2064 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2065 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2068 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2074 * Retrieve a property from the given object. If 'literal' is specified, then
2075 * numbers are left as exact values. Otherwise, numbers are converted to a
2076 * human-readable form.
2078 * Returns 0 on success, or -1 on error.
2081 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2082 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2084 char *source = NULL;
2088 boolean_t received = zfs_is_recvd_props_mode(zhp);
2091 * Check to see if this property applies to our object
2093 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2096 if (received && zfs_prop_readonly(prop))
2100 *src = ZPROP_SRC_NONE;
2103 case ZFS_PROP_CREATION:
2105 * 'creation' is a time_t stored in the statistics. We convert
2106 * this into a string unless 'literal' is specified.
2109 val = getprop_uint64(zhp, prop, &source);
2110 time_t time = (time_t)val;
2114 localtime_r(&time, &t) == NULL ||
2115 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2117 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
2121 case ZFS_PROP_MOUNTPOINT:
2123 * Getting the precise mountpoint can be tricky.
2125 * - for 'none' or 'legacy', return those values.
2126 * - for inherited mountpoints, we want to take everything
2127 * after our ancestor and append it to the inherited value.
2129 * If the pool has an alternate root, we want to prepend that
2130 * root to any values we return.
2133 str = getprop_string(zhp, prop, &source);
2135 if (str[0] == '/') {
2136 char buf[MAXPATHLEN];
2138 const char *relpath;
2141 * If we inherit the mountpoint, even from a dataset
2142 * with a received value, the source will be the path of
2143 * the dataset we inherit from. If source is
2144 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2147 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2150 relpath = zhp->zfs_name + strlen(source);
2151 if (relpath[0] == '/')
2155 if ((zpool_get_prop(zhp->zpool_hdl,
2156 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2157 (strcmp(root, "-") == 0))
2160 * Special case an alternate root of '/'. This will
2161 * avoid having multiple leading slashes in the
2164 if (strcmp(root, "/") == 0)
2168 * If the mountpoint is '/' then skip over this
2169 * if we are obtaining either an alternate root or
2170 * an inherited mountpoint.
2172 if (str[1] == '\0' && (root[0] != '\0' ||
2173 relpath[0] != '\0'))
2176 if (relpath[0] == '\0')
2177 (void) snprintf(propbuf, proplen, "%s%s",
2180 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2181 root, str, relpath[0] == '@' ? "" : "/",
2184 /* 'legacy' or 'none' */
2185 (void) strlcpy(propbuf, str, proplen);
2190 case ZFS_PROP_ORIGIN:
2191 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2194 * If there is no parent at all, return failure to indicate that
2195 * it doesn't apply to this dataset.
2197 if (propbuf[0] == '\0')
2201 case ZFS_PROP_CLONES:
2202 if (get_clones_string(zhp, propbuf, proplen) != 0)
2206 case ZFS_PROP_QUOTA:
2207 case ZFS_PROP_REFQUOTA:
2208 case ZFS_PROP_RESERVATION:
2209 case ZFS_PROP_REFRESERVATION:
2211 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2215 * If quota or reservation is 0, we translate this into 'none'
2216 * (unless literal is set), and indicate that it's the default
2217 * value. Otherwise, we print the number nicely and indicate
2218 * that its set locally.
2222 (void) strlcpy(propbuf, "0", proplen);
2224 (void) strlcpy(propbuf, "none", proplen);
2227 (void) snprintf(propbuf, proplen, "%llu",
2230 zfs_nicenum(val, propbuf, proplen);
2234 case ZFS_PROP_REFRATIO:
2235 case ZFS_PROP_COMPRESSRATIO:
2236 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2238 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2239 (u_longlong_t)(val / 100),
2240 (u_longlong_t)(val % 100));
2244 switch (zhp->zfs_type) {
2245 case ZFS_TYPE_FILESYSTEM:
2248 case ZFS_TYPE_VOLUME:
2251 case ZFS_TYPE_SNAPSHOT:
2257 (void) snprintf(propbuf, proplen, "%s", str);
2260 case ZFS_PROP_MOUNTED:
2262 * The 'mounted' property is a pseudo-property that described
2263 * whether the filesystem is currently mounted. Even though
2264 * it's a boolean value, the typical values of "on" and "off"
2265 * don't make sense, so we translate to "yes" and "no".
2267 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2268 src, &source, &val) != 0)
2271 (void) strlcpy(propbuf, "yes", proplen);
2273 (void) strlcpy(propbuf, "no", proplen);
2278 * The 'name' property is a pseudo-property derived from the
2279 * dataset name. It is presented as a real property to simplify
2282 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2285 case ZFS_PROP_MLSLABEL:
2287 #ifdef HAVE_MLSLABEL
2288 m_label_t *new_sl = NULL;
2289 char *ascii = NULL; /* human readable label */
2291 (void) strlcpy(propbuf,
2292 getprop_string(zhp, prop, &source), proplen);
2294 if (literal || (strcasecmp(propbuf,
2295 ZFS_MLSLABEL_DEFAULT) == 0))
2299 * Try to translate the internal hex string to
2300 * human-readable output. If there are any
2301 * problems just use the hex string.
2304 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2305 L_NO_CORRECTION, NULL) == -1) {
2306 m_label_free(new_sl);
2310 if (label_to_str(new_sl, &ascii, M_LABEL,
2314 m_label_free(new_sl);
2317 m_label_free(new_sl);
2319 (void) strlcpy(propbuf, ascii, proplen);
2322 (void) strlcpy(propbuf,
2323 getprop_string(zhp, prop, &source), proplen);
2324 #endif /* HAVE_MLSLABEL */
2330 * GUIDs are stored as numbers, but they are identifiers.
2331 * We don't want them to be pretty printed, because pretty
2332 * printing mangles the ID into a truncated and useless value.
2334 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2336 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2340 switch (zfs_prop_get_type(prop)) {
2341 case PROP_TYPE_NUMBER:
2342 if (get_numeric_property(zhp, prop, src,
2343 &source, &val) != 0)
2346 (void) snprintf(propbuf, proplen, "%llu",
2349 zfs_nicenum(val, propbuf, proplen);
2352 case PROP_TYPE_STRING:
2353 (void) strlcpy(propbuf,
2354 getprop_string(zhp, prop, &source), proplen);
2357 case PROP_TYPE_INDEX:
2358 if (get_numeric_property(zhp, prop, src,
2359 &source, &val) != 0)
2361 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2363 (void) strlcpy(propbuf, strval, proplen);
2371 get_source(zhp, src, source, statbuf, statlen);
2377 * Utility function to get the given numeric property. Does no validation that
2378 * the given property is the appropriate type; should only be used with
2379 * hard-coded property types.
2382 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2387 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2393 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2397 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2398 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2402 * Similar to zfs_prop_get(), but returns the value as an integer.
2405 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2406 zprop_source_t *src, char *statbuf, size_t statlen)
2411 * Check to see if this property applies to our object
2413 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2414 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2415 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2416 zfs_prop_to_name(prop)));
2420 *src = ZPROP_SRC_NONE;
2422 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2425 get_source(zhp, src, source, statbuf, statlen);
2432 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2433 char **domainp, idmap_rid_t *ridp)
2435 idmap_get_handle_t *get_hdl = NULL;
2439 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2443 err = idmap_get_sidbyuid(get_hdl, id,
2444 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2446 err = idmap_get_sidbygid(get_hdl, id,
2447 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2449 if (err == IDMAP_SUCCESS &&
2450 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2451 status == IDMAP_SUCCESS)
2457 idmap_get_destroy(get_hdl);
2460 #endif /* HAVE_IDMAP */
2463 * convert the propname into parameters needed by kernel
2464 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2465 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2466 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2467 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2470 userquota_propname_decode(const char *propname, boolean_t zoned,
2471 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2473 zfs_userquota_prop_t type;
2482 /* Figure out the property type ({user|group}{quota|space}) */
2483 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2484 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2485 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2488 if (type == ZFS_NUM_USERQUOTA_PROPS)
2492 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2493 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2495 cp = strchr(propname, '@') + 1;
2497 if (isuser && (pw = getpwnam(cp)) != NULL) {
2498 if (zoned && getzoneid() == GLOBAL_ZONEID)
2501 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2502 if (zoned && getzoneid() == GLOBAL_ZONEID)
2505 } else if (strchr(cp, '@')) {
2508 * It's a SID name (eg "user@domain") that needs to be
2509 * turned into S-1-domainID-RID.
2511 directory_error_t e;
2512 char *numericsid = NULL;
2515 if (zoned && getzoneid() == GLOBAL_ZONEID)
2518 e = directory_sid_from_user_name(NULL,
2521 e = directory_sid_from_group_name(NULL,
2525 directory_error_free(e);
2528 if (numericsid == NULL)
2531 (void) strlcpy(domain, cp, domainlen);
2532 cp = strrchr(domain, '-');
2537 *ridp = strtoull(cp, &end, 10);
2540 if (errno != 0 || *end != '\0')
2544 #endif /* HAVE_IDMAP */
2546 /* It's a user/group ID (eg "12345"). */
2549 id = strtoul(cp, &end, 10);
2554 /* It's an ephemeral ID. */
2558 if (idmap_id_to_numeric_domain_rid(id, isuser,
2559 &mapdomain, &rid) != 0)
2561 (void) strlcpy(domain, mapdomain, domainlen);
2565 #endif /* HAVE_IDMAP */
2575 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2576 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2579 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2581 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2583 err = userquota_propname_decode(propname,
2584 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2585 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2586 zc.zc_objset_type = *typep;
2590 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2594 *propvalue = zc.zc_cookie;
2599 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2600 uint64_t *propvalue)
2602 zfs_userquota_prop_t type;
2604 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2609 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2610 char *propbuf, int proplen, boolean_t literal)
2614 zfs_userquota_prop_t type;
2616 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2623 (void) snprintf(propbuf, proplen, "%llu",
2624 (u_longlong_t)propvalue);
2625 } else if (propvalue == 0 &&
2626 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2627 (void) strlcpy(propbuf, "none", proplen);
2629 zfs_nicenum(propvalue, propbuf, proplen);
2635 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2636 uint64_t *propvalue)
2639 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2640 const char *snapname;
2642 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2644 snapname = strchr(propname, '@') + 1;
2645 if (strchr(snapname, '@')) {
2646 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2648 /* snapname is the short name, append it to zhp's fsname */
2651 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2652 sizeof (zc.zc_value));
2653 cp = strchr(zc.zc_value, '@');
2656 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2657 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2660 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2664 *propvalue = zc.zc_cookie;
2669 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2670 char *propbuf, int proplen, boolean_t literal)
2675 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2681 (void) snprintf(propbuf, proplen, "%llu", (long long unsigned int)propvalue);
2683 zfs_nicenum(propvalue, propbuf, proplen);
2690 zfs_get_snapused_int(zfs_handle_t *firstsnap, zfs_handle_t *lastsnap,
2694 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2696 (void) strlcpy(zc.zc_name, lastsnap->zfs_name, sizeof (zc.zc_name));
2697 (void) strlcpy(zc.zc_value, firstsnap->zfs_name, sizeof (zc.zc_value));
2699 err = ioctl(lastsnap->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_SNAPS, &zc);
2703 *usedp = zc.zc_cookie;
2709 * Returns the name of the given zfs handle.
2712 zfs_get_name(const zfs_handle_t *zhp)
2714 return (zhp->zfs_name);
2718 * Returns the type of the given zfs handle.
2721 zfs_get_type(const zfs_handle_t *zhp)
2723 return (zhp->zfs_type);
2727 * Is one dataset name a child dataset of another?
2729 * Needs to handle these cases:
2730 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2731 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2732 * Descendant? No. No. No. Yes.
2735 is_descendant(const char *ds1, const char *ds2)
2737 size_t d1len = strlen(ds1);
2739 /* ds2 can't be a descendant if it's smaller */
2740 if (strlen(ds2) < d1len)
2743 /* otherwise, compare strings and verify that there's a '/' char */
2744 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2748 * Given a complete name, return just the portion that refers to the parent.
2749 * Will return -1 if there is no parent (path is just the name of the
2753 parent_name(const char *path, char *buf, size_t buflen)
2757 (void) strlcpy(buf, path, buflen);
2759 if ((slashp = strrchr(buf, '/')) == NULL)
2767 * If accept_ancestor is false, then check to make sure that the given path has
2768 * a parent, and that it exists. If accept_ancestor is true, then find the
2769 * closest existing ancestor for the given path. In prefixlen return the
2770 * length of already existing prefix of the given path. We also fetch the
2771 * 'zoned' property, which is used to validate property settings when creating
2775 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2776 boolean_t accept_ancestor, int *prefixlen)
2778 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2779 char parent[ZFS_MAXNAMELEN];
2785 (void) snprintf(errbuf, sizeof (errbuf),
2786 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2788 /* get parent, and check to see if this is just a pool */
2789 if (parent_name(path, parent, sizeof (parent)) != 0) {
2790 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2791 "missing dataset name"));
2792 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2795 /* check to see if the pool exists */
2796 if ((slash = strchr(parent, '/')) == NULL)
2797 slash = parent + strlen(parent);
2798 (void) strncpy(zc.zc_name, parent, slash - parent);
2799 zc.zc_name[slash - parent] = '\0';
2800 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2803 "no such pool '%s'"), zc.zc_name);
2804 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2807 /* check to see if the parent dataset exists */
2808 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2809 if (errno == ENOENT && accept_ancestor) {
2811 * Go deeper to find an ancestor, give up on top level.
2813 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2815 "no such pool '%s'"), zc.zc_name);
2816 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2818 } else if (errno == ENOENT) {
2819 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2820 "parent does not exist"));
2821 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2823 return (zfs_standard_error(hdl, errno, errbuf));
2826 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2830 /* we are in a non-global zone, but parent is in the global zone */
2831 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2832 (void) zfs_standard_error(hdl, EPERM, errbuf);
2837 /* make sure parent is a filesystem */
2838 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2839 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2840 "parent is not a filesystem"));
2841 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2847 if (prefixlen != NULL)
2848 *prefixlen = strlen(parent);
2853 * Finds whether the dataset of the given type(s) exists.
2856 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2860 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2864 * Try to get stats for the dataset, which will tell us if it exists.
2866 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2867 int ds_type = zhp->zfs_type;
2870 if (types & ds_type)
2877 * Given a path to 'target', create all the ancestors between
2878 * the prefixlen portion of the path, and the target itself.
2879 * Fail if the initial prefixlen-ancestor does not already exist.
2882 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2888 /* make sure prefix exists */
2889 cp = target + prefixlen;
2891 assert(strchr(cp, '/') == NULL);
2892 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2895 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2903 * Attempt to create, mount, and share any ancestor filesystems,
2904 * up to the prefixlen-long one.
2906 for (cp = target + prefixlen + 1;
2907 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2912 h = make_dataset_handle(hdl, target);
2914 /* it already exists, nothing to do here */
2919 logstr = hdl->libzfs_log_str;
2920 hdl->libzfs_log_str = NULL;
2921 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2923 hdl->libzfs_log_str = logstr;
2924 opname = dgettext(TEXT_DOMAIN, "create");
2928 hdl->libzfs_log_str = logstr;
2929 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2931 opname = dgettext(TEXT_DOMAIN, "open");
2935 if (zfs_mount(h, NULL, 0) != 0) {
2936 opname = dgettext(TEXT_DOMAIN, "mount");
2940 if (zfs_share(h) != 0) {
2941 opname = dgettext(TEXT_DOMAIN, "share");
2951 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2952 "failed to %s ancestor '%s'"), opname, target);
2957 * Creates non-existing ancestors of the given path.
2960 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2966 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2969 if ((path_copy = strdup(path)) != NULL) {
2970 rc = create_parents(hdl, path_copy, prefix);
2973 if (path_copy == NULL || rc != 0)
2980 * Create a new filesystem or volume.
2983 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2986 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2989 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2993 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2994 "cannot create '%s'"), path);
2996 /* validate the path, taking care to note the extended error message */
2997 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2998 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3000 /* validate parents exist */
3001 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3005 * The failure modes when creating a dataset of a different type over
3006 * one that already exists is a little strange. In particular, if you
3007 * try to create a dataset on top of an existing dataset, the ioctl()
3008 * will return ENOENT, not EEXIST. To prevent this from happening, we
3009 * first try to see if the dataset exists.
3011 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
3012 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3013 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3014 "dataset already exists"));
3015 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3018 if (type == ZFS_TYPE_VOLUME)
3019 zc.zc_objset_type = DMU_OST_ZVOL;
3021 zc.zc_objset_type = DMU_OST_ZFS;
3023 if (props && (props = zfs_valid_proplist(hdl, type, props,
3024 zoned, NULL, errbuf)) == 0)
3027 if (type == ZFS_TYPE_VOLUME) {
3029 * If we are creating a volume, the size and block size must
3030 * satisfy a few restraints. First, the blocksize must be a
3031 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3032 * volsize must be a multiple of the block size, and cannot be
3035 if (props == NULL || nvlist_lookup_uint64(props,
3036 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3039 "missing volume size"));
3040 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3043 if ((ret = nvlist_lookup_uint64(props,
3044 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3045 &blocksize)) != 0) {
3046 if (ret == ENOENT) {
3047 blocksize = zfs_prop_default_numeric(
3048 ZFS_PROP_VOLBLOCKSIZE);
3051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3052 "missing volume block size"));
3053 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3060 "volume size cannot be zero"));
3061 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3064 if (size % blocksize != 0) {
3066 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3067 "volume size must be a multiple of volume block "
3069 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3073 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
3077 /* create the dataset */
3078 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
3080 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
3081 ret = zvol_create_link(hdl, path);
3083 (void) zfs_standard_error(hdl, errno,
3084 dgettext(TEXT_DOMAIN,
3085 "Volume successfully created, but device links "
3086 "were not created"));
3087 zcmd_free_nvlists(&zc);
3092 zcmd_free_nvlists(&zc);
3094 /* check for failure */
3096 char parent[ZFS_MAXNAMELEN];
3097 (void) parent_name(path, parent, sizeof (parent));
3101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3102 "no such parent '%s'"), parent);
3103 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3107 "parent '%s' is not a filesystem"), parent);
3108 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3112 "volume block size must be power of 2 from "
3114 (uint_t)SPA_MINBLOCKSIZE,
3115 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3117 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3121 "pool must be upgraded to set this "
3122 "property or value"));
3123 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3127 * This platform can't address a volume this big.
3129 if (type == ZFS_TYPE_VOLUME)
3130 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3135 return (zfs_standard_error(hdl, errno, errbuf));
3143 * Destroys the given dataset. The caller must make sure that the filesystem
3144 * isn't mounted, and that there are no active dependents. If the file system
3145 * does not exist this function does nothing.
3148 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3150 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3152 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3154 if (ZFS_IS_VOLUME(zhp)) {
3155 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3158 zc.zc_objset_type = DMU_OST_ZVOL;
3160 zc.zc_objset_type = DMU_OST_ZFS;
3163 zc.zc_defer_destroy = defer;
3164 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3166 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3167 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3171 remove_mountpoint(zhp);
3176 struct destroydata {
3178 const char *snapname;
3182 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3184 struct destroydata *dd = arg;
3186 char name[ZFS_MAXNAMELEN];
3189 (void) snprintf(name, sizeof (name),
3190 "%s@%s", zhp->zfs_name, dd->snapname);
3192 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3194 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3198 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3199 (void) zvol_remove_link(zhp->zfs_hdl, name);
3201 * NB: this is simply a best-effort. We don't want to
3202 * return an error, because then we wouldn't visit all
3207 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3213 * Destroys all snapshots with the given name in zhp & descendants.
3216 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3219 struct destroydata dd = { 0 };
3221 dd.snapname = snapname;
3222 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3223 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3225 if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) {
3226 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3227 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3228 zhp->zfs_name, snapname);
3230 ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer);
3232 nvlist_free(dd.nvl);
3237 * Destroys all the snapshots named in the nvlist. They must be underneath
3238 * the zhp (either snapshots of it, or snapshots of its descendants).
3241 zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer)
3244 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3246 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3247 if (zcmd_write_src_nvlist(zhp->zfs_hdl, &zc, snaps) != 0)
3249 zc.zc_defer_destroy = defer;
3251 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS_NVL, &zc);
3255 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3256 "cannot destroy snapshots in %s"), zc.zc_name);
3260 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3261 "snapshot is cloned"));
3262 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3265 return (zfs_standard_error(zhp->zfs_hdl, errno,
3274 * Clones the given dataset. The target must be of the same type as the source.
3277 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3279 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3280 char parent[ZFS_MAXNAMELEN];
3283 libzfs_handle_t *hdl = zhp->zfs_hdl;
3287 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3289 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3290 "cannot create '%s'"), target);
3292 /* validate the target/clone name */
3293 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3294 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3296 /* validate parents exist */
3297 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3300 (void) parent_name(target, parent, sizeof (parent));
3303 if (ZFS_IS_VOLUME(zhp)) {
3304 zc.zc_objset_type = DMU_OST_ZVOL;
3305 type = ZFS_TYPE_VOLUME;
3307 zc.zc_objset_type = DMU_OST_ZFS;
3308 type = ZFS_TYPE_FILESYSTEM;
3312 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3313 zhp, errbuf)) == NULL)
3316 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3324 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3325 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3326 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3328 zcmd_free_nvlists(&zc);
3335 * The parent doesn't exist. We should have caught this
3336 * above, but there may a race condition that has since
3337 * destroyed the parent.
3339 * At this point, we don't know whether it's the source
3340 * that doesn't exist anymore, or whether the target
3341 * dataset doesn't exist.
3343 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3344 "no such parent '%s'"), parent);
3345 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3348 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3349 "source and target pools differ"));
3350 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3354 return (zfs_standard_error(zhp->zfs_hdl, errno,
3357 } else if (ZFS_IS_VOLUME(zhp)) {
3358 ret = zvol_create_link(zhp->zfs_hdl, target);
3364 typedef struct promote_data {
3365 char cb_mountpoint[MAXPATHLEN];
3366 const char *cb_target;
3367 const char *cb_errbuf;
3368 uint64_t cb_pivot_txg;
3372 promote_snap_cb(zfs_handle_t *zhp, void *data)
3374 promote_data_t *pd = data;
3376 char snapname[MAXPATHLEN];
3379 /* We don't care about snapshots after the pivot point */
3380 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3385 /* Remove the device link if it's a zvol. */
3386 if (ZFS_IS_VOLUME(zhp))
3387 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3389 /* Check for conflicting names */
3390 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3391 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3392 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3395 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3396 "snapshot name '%s' from origin \n"
3397 "conflicts with '%s' from target"),
3398 zhp->zfs_name, snapname);
3399 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3406 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3408 promote_data_t *pd = data;
3410 /* We don't care about snapshots after the pivot point */
3411 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3412 /* Create the device link if it's a zvol. */
3413 if (ZFS_IS_VOLUME(zhp))
3414 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3422 * Promotes the given clone fs to be the clone parent.
3425 zfs_promote(zfs_handle_t *zhp)
3427 libzfs_handle_t *hdl = zhp->zfs_hdl;
3428 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3429 char parent[MAXPATHLEN];
3436 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3437 "cannot promote '%s'"), zhp->zfs_name);
3439 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3441 "snapshots can not be promoted"));
3442 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3445 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3446 if (parent[0] == '\0') {
3447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3448 "not a cloned filesystem"));
3449 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3451 cp = strchr(parent, '@');
3454 /* Walk the snapshots we will be moving */
3455 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3458 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3460 pd.cb_target = zhp->zfs_name;
3461 pd.cb_errbuf = errbuf;
3462 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3465 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3466 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3467 ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
3473 /* issue the ioctl */
3474 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3475 sizeof (zc.zc_value));
3476 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3477 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3480 int save_errno = errno;
3482 (void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3486 switch (save_errno) {
3489 * There is a conflicting snapshot name. We
3490 * should have caught this above, but they could
3491 * have renamed something in the mean time.
3493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3494 "conflicting snapshot '%s' from parent '%s'"),
3495 zc.zc_string, parent);
3496 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3499 return (zfs_standard_error(hdl, save_errno, errbuf));
3502 (void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3511 const char *cd_snapname;
3516 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3518 struct createdata *cd = arg;
3521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3522 char name[MAXPATHLEN];
3524 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3525 (void) strlcat(name, "@", sizeof (name));
3526 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3527 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3530 * NB: this is simply a best-effort. We don't want to
3531 * return an error, because then we wouldn't visit all
3536 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3544 * Takes a snapshot of the given dataset.
3547 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3551 char parent[ZFS_MAXNAMELEN];
3553 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3557 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3558 "cannot snapshot '%s'"), path);
3560 /* validate the target name */
3561 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3562 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3565 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3566 props, B_FALSE, NULL, errbuf)) == NULL)
3569 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3577 /* make sure the parent exists and is of the appropriate type */
3578 delim = strchr(path, '@');
3579 (void) strncpy(parent, path, delim - path);
3580 parent[delim - path] = '\0';
3582 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3583 ZFS_TYPE_VOLUME)) == NULL) {
3584 zcmd_free_nvlists(&zc);
3588 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3589 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3590 if (ZFS_IS_VOLUME(zhp))
3591 zc.zc_objset_type = DMU_OST_ZVOL;
3593 zc.zc_objset_type = DMU_OST_ZFS;
3594 zc.zc_cookie = recursive;
3595 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3597 zcmd_free_nvlists(&zc);
3600 * if it was recursive, the one that actually failed will be in
3604 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3605 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3607 if (ret == 0 && recursive) {
3608 struct createdata cd;
3610 cd.cd_snapname = delim + 1;
3611 cd.cd_ifexists = B_FALSE;
3612 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3614 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3615 ret = zvol_create_link(zhp->zfs_hdl, path);
3617 (void) zfs_standard_error(hdl, errno,
3618 dgettext(TEXT_DOMAIN,
3619 "Volume successfully snapshotted, but device links "
3620 "were not created"));
3627 (void) zfs_standard_error(hdl, errno, errbuf);
3635 * Destroy any more recent snapshots. We invoke this callback on any dependents
3636 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3637 * is a dependent and we should just destroy it without checking the transaction
3640 typedef struct rollback_data {
3641 const char *cb_target; /* the snapshot */
3642 uint64_t cb_create; /* creation time reference */
3644 boolean_t cb_dependent;
3649 rollback_destroy(zfs_handle_t *zhp, void *data)
3651 rollback_data_t *cbp = data;
3653 if (!cbp->cb_dependent) {
3654 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3655 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3656 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3660 cbp->cb_dependent = B_TRUE;
3661 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3662 rollback_destroy, cbp);
3663 cbp->cb_dependent = B_FALSE;
3665 logstr = zhp->zfs_hdl->libzfs_log_str;
3666 zhp->zfs_hdl->libzfs_log_str = NULL;
3667 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3668 zhp->zfs_hdl->libzfs_log_str = logstr;
3671 /* We must destroy this clone; first unmount it */
3672 prop_changelist_t *clp;
3674 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3675 cbp->cb_force ? MS_FORCE: 0);
3676 if (clp == NULL || changelist_prefix(clp) != 0) {
3677 cbp->cb_error = B_TRUE;
3681 if (zfs_destroy(zhp, B_FALSE) != 0)
3682 cbp->cb_error = B_TRUE;
3684 changelist_remove(clp, zhp->zfs_name);
3685 (void) changelist_postfix(clp);
3686 changelist_free(clp);
3694 * Given a dataset, rollback to a specific snapshot, discarding any
3695 * data changes since then and making it the active dataset.
3697 * Any snapshots more recent than the target are destroyed, along with
3701 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3703 rollback_data_t cb = { 0 };
3705 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3706 boolean_t restore_resv = 0;
3707 uint64_t old_volsize = 0, new_volsize;
3708 zfs_prop_t resv_prop = { 0 };
3710 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3711 zhp->zfs_type == ZFS_TYPE_VOLUME);
3714 * Destroy all recent snapshots and their dependents.
3716 cb.cb_force = force;
3717 cb.cb_target = snap->zfs_name;
3718 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3719 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3725 * Now that we have verified that the snapshot is the latest,
3726 * rollback to the given snapshot.
3729 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3730 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3732 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3734 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3736 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3739 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3741 if (ZFS_IS_VOLUME(zhp))
3742 zc.zc_objset_type = DMU_OST_ZVOL;
3744 zc.zc_objset_type = DMU_OST_ZFS;
3747 * We rely on zfs_iter_children() to verify that there are no
3748 * newer snapshots for the given dataset. Therefore, we can
3749 * simply pass the name on to the ioctl() call. There is still
3750 * an unlikely race condition where the user has taken a
3751 * snapshot since we verified that this was the most recent.
3754 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3755 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3756 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3762 * For volumes, if the pre-rollback volsize matched the pre-
3763 * rollback reservation and the volsize has changed then set
3764 * the reservation property to the post-rollback volsize.
3765 * Make a new handle since the rollback closed the dataset.
3767 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3768 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3769 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3774 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3775 if (old_volsize != new_volsize)
3776 err = zfs_prop_set_int(zhp, resv_prop,
3785 * Renames the given dataset.
3788 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3789 boolean_t force_unmount)
3792 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3794 prop_changelist_t *cl = NULL;
3795 zfs_handle_t *zhrp = NULL;
3796 char *parentname = NULL;
3797 char parent[ZFS_MAXNAMELEN];
3798 libzfs_handle_t *hdl = zhp->zfs_hdl;
3801 /* if we have the same exact name, just return success */
3802 if (strcmp(zhp->zfs_name, target) == 0)
3805 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3806 "cannot rename to '%s'"), target);
3809 * Make sure the target name is valid
3811 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3812 if ((strchr(target, '@') == NULL) ||
3815 * Snapshot target name is abbreviated,
3816 * reconstruct full dataset name
3818 (void) strlcpy(parent, zhp->zfs_name,
3820 delim = strchr(parent, '@');
3821 if (strchr(target, '@') == NULL)
3825 (void) strlcat(parent, target, sizeof (parent));
3829 * Make sure we're renaming within the same dataset.
3831 delim = strchr(target, '@');
3832 if (strncmp(zhp->zfs_name, target, delim - target)
3833 != 0 || zhp->zfs_name[delim - target] != '@') {
3834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3835 "snapshots must be part of same "
3837 return (zfs_error(hdl, EZFS_CROSSTARGET,
3841 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3842 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3846 "recursive rename must be a snapshot"));
3847 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3850 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3851 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3853 /* validate parents */
3854 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3857 /* make sure we're in the same pool */
3858 verify((delim = strchr(target, '/')) != NULL);
3859 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3860 zhp->zfs_name[delim - target] != '/') {
3861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3862 "datasets must be within same pool"));
3863 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3866 /* new name cannot be a child of the current dataset name */
3867 if (is_descendant(zhp->zfs_name, target)) {
3868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3869 "New dataset name cannot be a descendant of "
3870 "current dataset name"));
3871 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3875 (void) snprintf(errbuf, sizeof (errbuf),
3876 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3878 if (getzoneid() == GLOBAL_ZONEID &&
3879 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3880 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3881 "dataset is used in a non-global zone"));
3882 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3886 struct destroydata dd;
3888 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3889 if (parentname == NULL) {
3893 delim = strchr(parentname, '@');
3895 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3901 dd.snapname = delim + 1;
3903 /* We remove any zvol links prior to renaming them */
3904 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3905 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3906 nvlist_free(dd.nvl);
3911 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3912 force_unmount ? MS_FORCE : 0)) == NULL)
3915 if (changelist_haszonedchild(cl)) {
3916 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3917 "child dataset with inherited mountpoint is used "
3918 "in a non-global zone"));
3919 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3924 if ((ret = changelist_prefix(cl)) != 0)
3928 if (ZFS_IS_VOLUME(zhp))
3929 zc.zc_objset_type = DMU_OST_ZVOL;
3931 zc.zc_objset_type = DMU_OST_ZFS;
3933 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3934 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3936 zc.zc_cookie = recursive;
3938 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3940 * if it was recursive, the one that actually failed will
3943 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3944 "cannot rename '%s'"), zc.zc_name);
3946 if (recursive && errno == EEXIST) {
3947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3948 "a child dataset already has a snapshot "
3949 "with the new name"));
3950 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3952 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3956 * On failure, we still want to remount any filesystems that
3957 * were previously mounted, so we don't alter the system state.
3960 struct createdata cd;
3962 /* only create links for datasets that had existed */
3963 cd.cd_snapname = delim + 1;
3964 cd.cd_ifexists = B_TRUE;
3965 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3968 (void) changelist_postfix(cl);
3972 struct createdata cd;
3974 /* only create links for datasets that had existed */
3975 cd.cd_snapname = strchr(target, '@') + 1;
3976 cd.cd_ifexists = B_TRUE;
3977 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3980 changelist_rename(cl, zfs_get_name(zhp), target);
3981 ret = changelist_postfix(cl);
3993 changelist_free(cl);
3999 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
4000 * and wait briefly for udev to create the /dev link.
4003 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
4005 return (zvol_create_link_common(hdl, dataset, B_FALSE));
4009 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
4011 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4012 char path[MAXPATHLEN];
4015 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4018 * Issue the appropriate ioctl.
4020 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
4024 * Silently ignore the case where the link already
4025 * exists. This allows 'zfs volinit' to be run multiple
4026 * times without errors.
4032 * snapdev set to hidden :
4033 * device creation was not permitted (see zvol.c)
4034 * ignore error quietly
4040 * Dataset does not exist in the kernel. If we
4041 * don't care (see zfs_rename), then ignore the
4051 return (zfs_standard_error_fmt(hdl, errno,
4052 dgettext(TEXT_DOMAIN, "cannot create device links "
4053 "for '%s'"), dataset));
4058 * Wait up to 10 seconds for udev to create the device.
4060 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
4061 error = zpool_label_disk_wait(path, 10000);
4063 (void) printf(gettext("%s may not be immediately "
4064 "available\n"), path);
4070 * Remove a minor node for the given zvol and the associated /dev links.
4073 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4075 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4076 int timeout = 3000; /* in milliseconds */
4080 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4083 * Due to concurrent updates by udev the device may be reported as
4084 * busy. In this case don't immediately fail. Instead briefly delay
4085 * and retry the ioctl() which is now likely to succeed. If unable
4086 * remove the link after timeout milliseconds return the failure.
4088 for (i = 0; i < timeout; i++) {
4089 error = ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
4090 if (error && errno == EBUSY) {
4102 * Silently ignore the case where the link no longer
4103 * exists, so that 'zfs volfini' can be run multiple
4104 * times without errors.
4109 return (zfs_standard_error_fmt(hdl, errno,
4110 dgettext(TEXT_DOMAIN, "cannot remove device "
4111 "links for '%s': %s"), dataset, strerror(errno)));
4119 zfs_get_user_props(zfs_handle_t *zhp)
4121 return (zhp->zfs_user_props);
4125 * This function is used by 'zfs list' to determine the exact set of columns to
4126 * display, and their maximum widths. This does two main things:
4128 * - If this is a list of all properties, then expand the list to include
4129 * all native properties, and set a flag so that for each dataset we look
4130 * for new unique user properties and add them to the list.
4132 * - For non fixed-width properties, keep track of the maximum width seen
4133 * so that we can size the column appropriately. If the user has
4134 * requested received property values, we also need to compute the width
4135 * of the RECEIVED column.
4138 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
4140 libzfs_handle_t *hdl = zhp->zfs_hdl;
4141 zprop_list_t *entry;
4142 zprop_list_t **last, **start;
4143 nvlist_t *userprops, *propval;
4146 char buf[ZFS_MAXPROPLEN];
4148 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4151 userprops = zfs_get_user_props(zhp);
4154 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4156 * Go through and add any user properties as necessary. We
4157 * start by incrementing our list pointer to the first
4158 * non-native property.
4161 while (*start != NULL) {
4162 if ((*start)->pl_prop == ZPROP_INVAL)
4164 start = &(*start)->pl_next;
4168 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4170 * See if we've already found this property in our list.
4172 for (last = start; *last != NULL;
4173 last = &(*last)->pl_next) {
4174 if (strcmp((*last)->pl_user_prop,
4175 nvpair_name(elem)) == 0)
4179 if (*last == NULL) {
4180 if ((entry = zfs_alloc(hdl,
4181 sizeof (zprop_list_t))) == NULL ||
4182 ((entry->pl_user_prop = zfs_strdup(hdl,
4183 nvpair_name(elem)))) == NULL) {
4188 entry->pl_prop = ZPROP_INVAL;
4189 entry->pl_width = strlen(nvpair_name(elem));
4190 entry->pl_all = B_TRUE;
4197 * Now go through and check the width of any non-fixed columns
4199 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4200 if (entry->pl_fixed)
4203 if (entry->pl_prop != ZPROP_INVAL) {
4204 if (zfs_prop_get(zhp, entry->pl_prop,
4205 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4206 if (strlen(buf) > entry->pl_width)
4207 entry->pl_width = strlen(buf);
4209 if (received && zfs_prop_get_recvd(zhp,
4210 zfs_prop_to_name(entry->pl_prop),
4211 buf, sizeof (buf), B_FALSE) == 0)
4212 if (strlen(buf) > entry->pl_recvd_width)
4213 entry->pl_recvd_width = strlen(buf);
4215 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4217 verify(nvlist_lookup_string(propval,
4218 ZPROP_VALUE, &strval) == 0);
4219 if (strlen(strval) > entry->pl_width)
4220 entry->pl_width = strlen(strval);
4222 if (received && zfs_prop_get_recvd(zhp,
4223 entry->pl_user_prop,
4224 buf, sizeof (buf), B_FALSE) == 0)
4225 if (strlen(buf) > entry->pl_recvd_width)
4226 entry->pl_recvd_width = strlen(buf);
4234 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4239 * Keep a reference to the props-table against which we prune the
4242 zhp->zfs_props_table = props;
4244 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4247 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4248 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4251 * User properties will result in ZPROP_INVAL, and since we
4252 * only know how to prune standard ZFS properties, we always
4253 * leave these in the list. This can also happen if we
4254 * encounter an unknown DSL property (when running older
4255 * software, for example).
4257 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4258 (void) nvlist_remove(zhp->zfs_props,
4259 nvpair_name(curr), nvpair_type(curr));
4265 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4266 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4268 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4269 nvlist_t *nvlist = NULL;
4272 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4273 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4274 zc.zc_cookie = (uint64_t)cmd;
4276 if (cmd == ZFS_SMB_ACL_RENAME) {
4277 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4278 (void) no_memory(hdl);
4284 case ZFS_SMB_ACL_ADD:
4285 case ZFS_SMB_ACL_REMOVE:
4286 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4288 case ZFS_SMB_ACL_RENAME:
4289 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4291 (void) no_memory(hdl);
4294 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4296 (void) no_memory(hdl);
4299 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4300 nvlist_free(nvlist);
4304 case ZFS_SMB_ACL_PURGE:
4309 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4311 nvlist_free(nvlist);
4316 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4317 char *path, char *resource)
4319 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4324 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4325 char *path, char *resource)
4327 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4332 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4334 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4339 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4340 char *oldname, char *newname)
4342 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4347 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4348 zfs_userspace_cb_t func, void *arg)
4350 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4351 zfs_useracct_t buf[100];
4352 libzfs_handle_t *hdl = zhp->zfs_hdl;
4355 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4357 zc.zc_objset_type = type;
4358 zc.zc_nvlist_dst = (uintptr_t)buf;
4361 zfs_useracct_t *zua = buf;
4363 zc.zc_nvlist_dst_size = sizeof (buf);
4364 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4365 char errbuf[ZFS_MAXNAMELEN + 32];
4367 (void) snprintf(errbuf, sizeof (errbuf),
4368 dgettext(TEXT_DOMAIN,
4369 "cannot get used/quota for %s"), zc.zc_name);
4370 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4372 if (zc.zc_nvlist_dst_size == 0)
4375 while (zc.zc_nvlist_dst_size > 0) {
4376 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4377 zua->zu_space)) != 0)
4380 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4388 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4389 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4390 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4392 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4393 libzfs_handle_t *hdl = zhp->zfs_hdl;
4395 ASSERT(!recursive || dsobj == 0);
4397 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4398 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4399 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4400 >= sizeof (zc.zc_string))
4401 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4402 zc.zc_cookie = recursive;
4403 zc.zc_temphold = temphold;
4404 zc.zc_cleanup_fd = cleanup_fd;
4405 zc.zc_sendobj = dsobj;
4406 zc.zc_createtxg = createtxg;
4408 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4409 char errbuf[ZFS_MAXNAMELEN+32];
4412 * if it was recursive, the one that actually failed will be in
4415 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4416 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4420 * Temporary tags wind up having the ds object id
4421 * prepended. So even if we passed the length check
4422 * above, it's still possible for the tag to wind
4423 * up being slightly too long.
4425 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4428 "pool must be upgraded"));
4429 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4431 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4433 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4439 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4447 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4448 boolean_t recursive)
4450 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4451 libzfs_handle_t *hdl = zhp->zfs_hdl;
4453 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4454 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4455 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4456 >= sizeof (zc.zc_string))
4457 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4458 zc.zc_cookie = recursive;
4460 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4461 char errbuf[ZFS_MAXNAMELEN+32];
4464 * if it was recursive, the one that actually failed will be in
4467 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4468 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4472 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4475 "pool must be upgraded"));
4476 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4478 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4480 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4488 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4490 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4491 libzfs_handle_t *hdl = zhp->zfs_hdl;
4495 char errbuf[ZFS_MAXNAMELEN+32];
4497 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4498 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4502 nvbuf = malloc(nvsz);
4503 if (nvbuf == NULL) {
4504 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4508 zc.zc_nvlist_dst_size = nvsz;
4509 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4511 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4513 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4514 (void) snprintf(errbuf, sizeof (errbuf),
4515 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4520 nvsz = zc.zc_nvlist_dst_size;
4524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4525 "pool must be upgraded"));
4526 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4529 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4532 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4535 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4540 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4542 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4543 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4545 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4555 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4557 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4558 libzfs_handle_t *hdl = zhp->zfs_hdl;
4560 char errbuf[ZFS_MAXNAMELEN+32];
4564 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4565 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4567 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4570 nvbuf = malloc(nvsz);
4572 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4575 zc.zc_nvlist_src_size = nvsz;
4576 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4577 zc.zc_perm_action = un;
4579 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4581 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4582 (void) snprintf(errbuf, sizeof (errbuf),
4583 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4588 "pool must be upgraded"));
4589 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4592 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4595 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4598 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4609 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4611 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4612 libzfs_handle_t *hdl = zhp->zfs_hdl;
4616 char errbuf[ZFS_MAXNAMELEN+32];
4618 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4622 nvbuf = malloc(nvsz);
4623 if (nvbuf == NULL) {
4624 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4628 zc.zc_nvlist_dst_size = nvsz;
4629 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4631 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4633 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4634 (void) snprintf(errbuf, sizeof (errbuf),
4635 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4640 nvsz = zc.zc_nvlist_dst_size;
4644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4645 "pool must be upgraded"));
4646 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4649 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4652 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4655 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4660 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4662 (void) snprintf(errbuf, sizeof (errbuf),
4663 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4665 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4675 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4678 uint64_t nblocks, volblocksize;
4682 if (nvlist_lookup_string(props,
4683 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4684 ncopies = atoi(strval);
4687 if (nvlist_lookup_uint64(props,
4688 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4689 &volblocksize) != 0)
4690 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4691 nblocks = volsize/volblocksize;
4692 /* start with metadnode L0-L6 */
4694 /* calculate number of indirects */
4695 while (nblocks > 1) {
4696 nblocks += DNODES_PER_LEVEL - 1;
4697 nblocks /= DNODES_PER_LEVEL;
4700 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4703 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4704 * compressed, but in practice they compress down to about
4707 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;