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) 2013 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.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
43 #include <sys/mntent.h>
44 #include <sys/mount.h>
53 #include <directory.h>
54 #endif /* HAVE_IDMAP */
56 #include <sys/dnode.h>
61 #include "zfs_namecheck.h"
63 #include "libzfs_impl.h"
64 #include "zfs_deleg.h"
66 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
67 static int userquota_propname_decode(const char *propname, boolean_t zoned,
68 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
71 * Given a single type (not a mask of types), return the type in a human
75 zfs_type_to_name(zfs_type_t type)
78 case ZFS_TYPE_FILESYSTEM:
79 return (dgettext(TEXT_DOMAIN, "filesystem"));
80 case ZFS_TYPE_SNAPSHOT:
81 return (dgettext(TEXT_DOMAIN, "snapshot"));
83 return (dgettext(TEXT_DOMAIN, "volume"));
92 * Validate a ZFS path. This is used even before trying to open the dataset, to
93 * provide a more meaningful error message. We call zfs_error_aux() to
94 * explain exactly why the name was not valid.
97 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
103 (void) zfs_prop_get_table();
104 if (dataset_namecheck(path, &why, &what) != 0) {
107 case NAME_ERR_TOOLONG:
108 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109 "name is too long"));
112 case NAME_ERR_LEADING_SLASH:
113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114 "leading slash in name"));
117 case NAME_ERR_EMPTY_COMPONENT:
118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
119 "empty component in name"));
122 case NAME_ERR_TRAILING_SLASH:
123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
124 "trailing slash in name"));
127 case NAME_ERR_INVALCHAR:
129 dgettext(TEXT_DOMAIN, "invalid character "
130 "'%c' in name"), what);
133 case NAME_ERR_MULTIPLE_AT:
134 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135 "multiple '@' delimiters in name"));
138 case NAME_ERR_NOLETTER:
139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
140 "pool doesn't begin with a letter"));
143 case NAME_ERR_RESERVED:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "name is reserved"));
148 case NAME_ERR_DISKLIKE:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "reserved disk name"));
160 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
163 "snapshot delimiter '@' in filesystem name"));
167 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
170 "missing '@' delimiter in snapshot name"));
174 if (modifying && strchr(path, '%') != NULL) {
176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
177 "invalid character %c in name"), '%');
185 zfs_name_valid(const char *name, zfs_type_t type)
187 if (type == ZFS_TYPE_POOL)
188 return (zpool_name_valid(NULL, B_FALSE, name));
189 return (zfs_validate_name(NULL, name, type, B_FALSE));
193 * This function takes the raw DSL properties, and filters out the user-defined
194 * properties into a separate nvlist.
197 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
199 libzfs_handle_t *hdl = zhp->zfs_hdl;
204 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
205 (void) no_memory(hdl);
210 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
211 if (!zfs_prop_user(nvpair_name(elem)))
214 verify(nvpair_value_nvlist(elem, &propval) == 0);
215 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
217 (void) no_memory(hdl);
225 static zpool_handle_t *
226 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
228 libzfs_handle_t *hdl = zhp->zfs_hdl;
231 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
232 if (hdl->libzfs_pool_handles != NULL)
233 zph->zpool_next = hdl->libzfs_pool_handles;
234 hdl->libzfs_pool_handles = zph;
239 static zpool_handle_t *
240 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
242 libzfs_handle_t *hdl = zhp->zfs_hdl;
243 zpool_handle_t *zph = hdl->libzfs_pool_handles;
245 while ((zph != NULL) &&
246 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
247 zph = zph->zpool_next;
252 * Returns a handle to the pool that contains the provided dataset.
253 * If a handle to that pool already exists then that handle is returned.
254 * Otherwise, a new handle is created and added to the list of handles.
256 static zpool_handle_t *
257 zpool_handle(zfs_handle_t *zhp)
263 len = strcspn(zhp->zfs_name, "/@") + 1;
264 pool_name = zfs_alloc(zhp->zfs_hdl, len);
265 (void) strlcpy(pool_name, zhp->zfs_name, len);
267 zph = zpool_find_handle(zhp, pool_name, len);
269 zph = zpool_add_handle(zhp, pool_name);
276 zpool_free_handles(libzfs_handle_t *hdl)
278 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
280 while (zph != NULL) {
281 next = zph->zpool_next;
285 hdl->libzfs_pool_handles = NULL;
289 * Utility function to gather stats (objset and zpl) for the given object.
292 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
294 libzfs_handle_t *hdl = zhp->zfs_hdl;
296 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
298 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
299 if (errno == ENOMEM) {
300 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
311 * Utility function to get the received properties of the given object.
314 get_recvd_props_ioctl(zfs_handle_t *zhp)
316 libzfs_handle_t *hdl = zhp->zfs_hdl;
317 nvlist_t *recvdprops;
318 zfs_cmd_t zc = {"\0"};
321 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
324 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
326 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
327 if (errno == ENOMEM) {
328 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
332 zcmd_free_nvlists(&zc);
337 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
338 zcmd_free_nvlists(&zc);
342 nvlist_free(zhp->zfs_recvd_props);
343 zhp->zfs_recvd_props = recvdprops;
349 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
351 nvlist_t *allprops, *userprops;
353 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
355 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
360 * XXX Why do we store the user props separately, in addition to
361 * storing them in zfs_props?
363 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
364 nvlist_free(allprops);
368 nvlist_free(zhp->zfs_props);
369 nvlist_free(zhp->zfs_user_props);
371 zhp->zfs_props = allprops;
372 zhp->zfs_user_props = userprops;
378 get_stats(zfs_handle_t *zhp)
381 zfs_cmd_t zc = {"\0"};
383 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
385 if (get_stats_ioctl(zhp, &zc) != 0)
387 else if (put_stats_zhdl(zhp, &zc) != 0)
389 zcmd_free_nvlists(&zc);
394 * Refresh the properties currently stored in the handle.
397 zfs_refresh_properties(zfs_handle_t *zhp)
399 (void) get_stats(zhp);
403 * Makes a handle from the given dataset name. Used by zfs_open() and
404 * zfs_iter_* to create child handles on the fly.
407 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
409 if (put_stats_zhdl(zhp, zc) != 0)
413 * We've managed to open the dataset and gather statistics. Determine
414 * the high-level type.
416 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
417 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
418 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
419 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
420 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
421 return (-1); /* zpios' and other testing datasets are
422 of this type, ignore if encountered */
426 if (zhp->zfs_dmustats.dds_is_snapshot)
427 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
428 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
429 zhp->zfs_type = ZFS_TYPE_VOLUME;
430 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
431 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
433 abort(); /* we should never see any other types */
435 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
442 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
444 zfs_cmd_t zc = {"\0"};
446 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
452 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
453 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
457 if (get_stats_ioctl(zhp, &zc) == -1) {
458 zcmd_free_nvlists(&zc);
462 if (make_dataset_handle_common(zhp, &zc) == -1) {
466 zcmd_free_nvlists(&zc);
471 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
473 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
479 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
480 if (make_dataset_handle_common(zhp, zc) == -1) {
488 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
490 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
495 zhp->zfs_hdl = pzhp->zfs_hdl;
496 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
497 zhp->zfs_head_type = pzhp->zfs_type;
498 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
499 zhp->zpool_hdl = zpool_handle(zhp);
505 zfs_handle_dup(zfs_handle_t *zhp_orig)
507 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
512 zhp->zfs_hdl = zhp_orig->zfs_hdl;
513 zhp->zpool_hdl = zhp_orig->zpool_hdl;
514 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
515 sizeof (zhp->zfs_name));
516 zhp->zfs_type = zhp_orig->zfs_type;
517 zhp->zfs_head_type = zhp_orig->zfs_head_type;
518 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
519 if (zhp_orig->zfs_props != NULL) {
520 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
521 (void) no_memory(zhp->zfs_hdl);
526 if (zhp_orig->zfs_user_props != NULL) {
527 if (nvlist_dup(zhp_orig->zfs_user_props,
528 &zhp->zfs_user_props, 0) != 0) {
529 (void) no_memory(zhp->zfs_hdl);
534 if (zhp_orig->zfs_recvd_props != NULL) {
535 if (nvlist_dup(zhp_orig->zfs_recvd_props,
536 &zhp->zfs_recvd_props, 0)) {
537 (void) no_memory(zhp->zfs_hdl);
542 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
543 if (zhp_orig->zfs_mntopts != NULL) {
544 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
545 zhp_orig->zfs_mntopts);
547 zhp->zfs_props_table = zhp_orig->zfs_props_table;
552 * Opens the given snapshot, filesystem, or volume. The 'types'
553 * argument is a mask of acceptable types. The function will print an
554 * appropriate error message and return NULL if it can't be opened.
557 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
562 (void) snprintf(errbuf, sizeof (errbuf),
563 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
566 * Validate the name before we even try to open it.
568 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
570 "invalid dataset name"));
571 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
576 * Try to get stats for the dataset, which will tell us if it exists.
579 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
580 (void) zfs_standard_error(hdl, errno, errbuf);
584 if (!(types & zhp->zfs_type)) {
585 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
594 * Release a ZFS handle. Nothing to do but free the associated memory.
597 zfs_close(zfs_handle_t *zhp)
599 if (zhp->zfs_mntopts)
600 free(zhp->zfs_mntopts);
601 nvlist_free(zhp->zfs_props);
602 nvlist_free(zhp->zfs_user_props);
603 nvlist_free(zhp->zfs_recvd_props);
607 typedef struct mnttab_node {
608 struct mnttab mtn_mt;
613 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
615 const mnttab_node_t *mtn1 = arg1;
616 const mnttab_node_t *mtn2 = arg2;
619 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
623 return (rv > 0 ? 1 : -1);
627 libzfs_mnttab_init(libzfs_handle_t *hdl)
629 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
630 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
631 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
635 libzfs_mnttab_update(libzfs_handle_t *hdl)
639 /* Reopen MNTTAB to prevent reading stale data from open file */
640 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
643 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
646 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
648 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
649 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
650 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
651 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
652 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
653 avl_add(&hdl->libzfs_mnttab_cache, mtn);
660 libzfs_mnttab_fini(libzfs_handle_t *hdl)
665 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
666 free(mtn->mtn_mt.mnt_special);
667 free(mtn->mtn_mt.mnt_mountp);
668 free(mtn->mtn_mt.mnt_fstype);
669 free(mtn->mtn_mt.mnt_mntopts);
672 avl_destroy(&hdl->libzfs_mnttab_cache);
676 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
678 hdl->libzfs_mnttab_enable = enable;
682 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
683 struct mnttab *entry)
689 if (!hdl->libzfs_mnttab_enable) {
690 struct mnttab srch = { 0 };
692 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
693 libzfs_mnttab_fini(hdl);
695 /* Reopen MNTTAB to prevent reading stale data from open file */
696 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
699 srch.mnt_special = (char *)fsname;
700 srch.mnt_fstype = MNTTYPE_ZFS;
701 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
707 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
708 if ((error = libzfs_mnttab_update(hdl)) != 0)
711 find.mtn_mt.mnt_special = (char *)fsname;
712 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
714 *entry = mtn->mtn_mt;
721 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
722 const char *mountp, const char *mntopts)
726 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
728 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
729 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
730 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
731 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
732 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
733 avl_add(&hdl->libzfs_mnttab_cache, mtn);
737 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
742 find.mtn_mt.mnt_special = (char *)fsname;
743 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
744 avl_remove(&hdl->libzfs_mnttab_cache, ret);
745 free(ret->mtn_mt.mnt_special);
746 free(ret->mtn_mt.mnt_mountp);
747 free(ret->mtn_mt.mnt_fstype);
748 free(ret->mtn_mt.mnt_mntopts);
754 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
756 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
758 if (zpool_handle == NULL)
761 *spa_version = zpool_get_prop_int(zpool_handle,
762 ZPOOL_PROP_VERSION, NULL);
767 * The choice of reservation property depends on the SPA version.
770 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
774 if (zfs_spa_version(zhp, &spa_version) < 0)
777 if (spa_version >= SPA_VERSION_REFRESERVATION)
778 *resv_prop = ZFS_PROP_REFRESERVATION;
780 *resv_prop = ZFS_PROP_RESERVATION;
786 * Given an nvlist of properties to set, validates that they are correct, and
787 * parses any numeric properties (index, boolean, etc) if they are specified as
791 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
792 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
799 int chosen_normal = -1;
802 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
803 (void) no_memory(hdl);
808 * Make sure this property is valid and applies to this type.
812 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
813 const char *propname = nvpair_name(elem);
815 prop = zfs_name_to_prop(propname);
816 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
818 * This is a user property: make sure it's a
819 * string, and that it's less than ZAP_MAXNAMELEN.
821 if (nvpair_type(elem) != DATA_TYPE_STRING) {
822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
823 "'%s' must be a string"), propname);
824 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
828 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
830 "property name '%s' is too long"),
832 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
836 (void) nvpair_value_string(elem, &strval);
837 if (nvlist_add_string(ret, propname, strval) != 0) {
838 (void) no_memory(hdl);
845 * Currently, only user properties can be modified on
848 if (type == ZFS_TYPE_SNAPSHOT) {
849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
850 "this property can not be modified for snapshots"));
851 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
855 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
856 zfs_userquota_prop_t uqtype;
857 char newpropname[128];
862 if (userquota_propname_decode(propname, zoned,
863 &uqtype, domain, sizeof (domain), &rid) != 0) {
865 dgettext(TEXT_DOMAIN,
866 "'%s' has an invalid user/group name"),
868 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
872 if (uqtype != ZFS_PROP_USERQUOTA &&
873 uqtype != ZFS_PROP_GROUPQUOTA) {
875 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
877 (void) zfs_error(hdl, EZFS_PROPREADONLY,
882 if (nvpair_type(elem) == DATA_TYPE_STRING) {
883 (void) nvpair_value_string(elem, &strval);
884 if (strcmp(strval, "none") == 0) {
886 } else if (zfs_nicestrtonum(hdl,
887 strval, &intval) != 0) {
888 (void) zfs_error(hdl,
889 EZFS_BADPROP, errbuf);
892 } else if (nvpair_type(elem) ==
894 (void) nvpair_value_uint64(elem, &intval);
896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
897 "use 'none' to disable "
898 "userquota/groupquota"));
902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
903 "'%s' must be a number"), propname);
904 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
909 * Encode the prop name as
910 * userquota@<hex-rid>-domain, to make it easy
911 * for the kernel to decode.
913 (void) snprintf(newpropname, sizeof (newpropname),
914 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
915 (longlong_t)rid, domain);
919 if (nvlist_add_uint64_array(ret, newpropname,
921 (void) no_memory(hdl);
925 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
926 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
929 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
933 if (prop == ZPROP_INVAL) {
934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
935 "invalid property '%s'"), propname);
936 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
940 if (!zfs_prop_valid_for_type(prop, type)) {
942 dgettext(TEXT_DOMAIN, "'%s' does not "
943 "apply to datasets of this type"), propname);
944 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
948 if (zfs_prop_readonly(prop) &&
949 (!zfs_prop_setonce(prop) || zhp != NULL)) {
951 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
953 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
957 if (zprop_parse_value(hdl, elem, prop, type, ret,
958 &strval, &intval, errbuf) != 0)
962 * Perform some additional checks for specific properties.
965 case ZFS_PROP_VERSION:
971 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
972 if (intval < version) {
973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
974 "Can not downgrade; already at version %u"),
976 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
982 case ZFS_PROP_RECORDSIZE:
983 case ZFS_PROP_VOLBLOCKSIZE:
984 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
985 if (intval < SPA_MINBLOCKSIZE ||
986 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
988 "'%s' must be power of 2 from %u "
990 (uint_t)SPA_MINBLOCKSIZE,
991 (uint_t)SPA_MAXBLOCKSIZE >> 10);
992 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
997 case ZFS_PROP_MLSLABEL:
1001 * Verify the mlslabel string and convert to
1002 * internal hex label string.
1006 char *hex = NULL; /* internal label string */
1008 /* Default value is already OK. */
1009 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1012 /* Verify the label can be converted to binary form */
1013 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1014 (str_to_label(strval, &new_sl, MAC_LABEL,
1015 L_NO_CORRECTION, NULL) == -1)) {
1019 /* Now translate to hex internal label string */
1020 if (label_to_str(new_sl, &hex, M_INTERNAL,
1026 m_label_free(new_sl);
1028 /* If string is already in internal form, we're done. */
1029 if (strcmp(strval, hex) == 0) {
1034 /* Replace the label string with the internal form. */
1035 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1037 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1044 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1045 "invalid mlslabel '%s'"), strval);
1046 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1047 m_label_free(new_sl); /* OK if null */
1050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1051 "mlslabels are unsupported"));
1052 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1054 #endif /* HAVE_MLSLABEL */
1057 case ZFS_PROP_MOUNTPOINT:
1059 namecheck_err_t why;
1061 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1062 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1065 if (mountpoint_namecheck(strval, &why)) {
1067 case NAME_ERR_LEADING_SLASH:
1069 dgettext(TEXT_DOMAIN,
1070 "'%s' must be an absolute path, "
1071 "'none', or 'legacy'"), propname);
1073 case NAME_ERR_TOOLONG:
1075 dgettext(TEXT_DOMAIN,
1076 "component of '%s' is too long"),
1082 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1089 case ZFS_PROP_SHARESMB:
1090 case ZFS_PROP_SHARENFS:
1092 * For the mountpoint and sharenfs or sharesmb
1093 * properties, check if it can be set in a
1094 * global/non-global zone based on
1095 * the zoned property value:
1097 * global zone non-global zone
1098 * --------------------------------------------------
1099 * zoned=on mountpoint (no) mountpoint (yes)
1100 * sharenfs (no) sharenfs (no)
1101 * sharesmb (no) sharesmb (no)
1103 * zoned=off mountpoint (yes) N/A
1108 if (getzoneid() == GLOBAL_ZONEID) {
1109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1110 "'%s' cannot be set on "
1111 "dataset in a non-global zone"),
1113 (void) zfs_error(hdl, EZFS_ZONED,
1116 } else if (prop == ZFS_PROP_SHARENFS ||
1117 prop == ZFS_PROP_SHARESMB) {
1118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1119 "'%s' cannot be set in "
1120 "a non-global zone"), propname);
1121 (void) zfs_error(hdl, EZFS_ZONED,
1125 } else if (getzoneid() != GLOBAL_ZONEID) {
1127 * If zoned property is 'off', this must be in
1128 * a global zone. If not, something is wrong.
1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1131 "'%s' cannot be set while dataset "
1132 "'zoned' property is set"), propname);
1133 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1138 * At this point, it is legitimate to set the
1139 * property. Now we want to make sure that the
1140 * property value is valid if it is sharenfs.
1142 if ((prop == ZFS_PROP_SHARENFS ||
1143 prop == ZFS_PROP_SHARESMB) &&
1144 strcmp(strval, "on") != 0 &&
1145 strcmp(strval, "off") != 0) {
1146 zfs_share_proto_t proto;
1148 if (prop == ZFS_PROP_SHARESMB)
1154 * Must be an valid sharing protocol
1155 * option string so init the libshare
1156 * in order to enable the parser and
1157 * then parse the options. We use the
1158 * control API since we don't care about
1159 * the current configuration and don't
1160 * want the overhead of loading it
1161 * until we actually do something.
1164 if (zfs_init_libshare(hdl,
1165 SA_INIT_CONTROL_API) != SA_OK) {
1167 * An error occurred so we can't do
1170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1171 "'%s' cannot be set: problem "
1172 "in share initialization"),
1174 (void) zfs_error(hdl, EZFS_BADPROP,
1179 if (zfs_parse_options(strval, proto) != SA_OK) {
1181 * There was an error in parsing so
1182 * deal with it by issuing an error
1183 * message and leaving after
1184 * uninitializing the the libshare
1187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1188 "'%s' cannot be set to invalid "
1189 "options"), propname);
1190 (void) zfs_error(hdl, EZFS_BADPROP,
1192 zfs_uninit_libshare(hdl);
1195 zfs_uninit_libshare(hdl);
1199 case ZFS_PROP_UTF8ONLY:
1200 chosen_utf = (int)intval;
1202 case ZFS_PROP_NORMALIZE:
1203 chosen_normal = (int)intval;
1210 * For changes to existing volumes, we have some additional
1211 * checks to enforce.
1213 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1214 uint64_t volsize = zfs_prop_get_int(zhp,
1216 uint64_t blocksize = zfs_prop_get_int(zhp,
1217 ZFS_PROP_VOLBLOCKSIZE);
1221 case ZFS_PROP_RESERVATION:
1222 case ZFS_PROP_REFRESERVATION:
1223 if (intval > volsize) {
1224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225 "'%s' is greater than current "
1226 "volume size"), propname);
1227 (void) zfs_error(hdl, EZFS_BADPROP,
1233 case ZFS_PROP_VOLSIZE:
1234 if (intval % blocksize != 0) {
1235 zfs_nicenum(blocksize, buf,
1237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1238 "'%s' must be a multiple of "
1239 "volume block size (%s)"),
1241 (void) zfs_error(hdl, EZFS_BADPROP,
1247 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1248 "'%s' cannot be zero"),
1250 (void) zfs_error(hdl, EZFS_BADPROP,
1262 * If normalization was chosen, but no UTF8 choice was made,
1263 * enforce rejection of non-UTF8 names.
1265 * If normalization was chosen, but rejecting non-UTF8 names
1266 * was explicitly not chosen, it is an error.
1268 if (chosen_normal > 0 && chosen_utf < 0) {
1269 if (nvlist_add_uint64(ret,
1270 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1271 (void) no_memory(hdl);
1274 } else if (chosen_normal > 0 && chosen_utf == 0) {
1275 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1276 "'%s' must be set 'on' if normalization chosen"),
1277 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1278 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1289 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1291 uint64_t old_volsize;
1292 uint64_t new_volsize;
1293 uint64_t old_reservation;
1294 uint64_t new_reservation;
1295 zfs_prop_t resv_prop;
1298 * If this is an existing volume, and someone is setting the volsize,
1299 * make sure that it matches the reservation, or add it if necessary.
1301 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1302 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1304 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1305 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1306 old_reservation) || nvlist_lookup_uint64(nvl,
1307 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1310 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1313 new_reservation = zvol_volsize_to_reservation(new_volsize,
1315 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1316 new_reservation) != 0) {
1317 (void) no_memory(zhp->zfs_hdl);
1324 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1331 * For quotas and reservations, ENOSPC indicates
1332 * something different; setting a quota or reservation
1333 * doesn't use any disk space.
1336 case ZFS_PROP_QUOTA:
1337 case ZFS_PROP_REFQUOTA:
1338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1339 "size is less than current used or "
1341 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1344 case ZFS_PROP_RESERVATION:
1345 case ZFS_PROP_REFRESERVATION:
1346 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1347 "size is greater than available space"));
1348 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1352 (void) zfs_standard_error(hdl, err, errbuf);
1358 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1362 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1366 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1367 "pool and or dataset must be upgraded to set this "
1368 "property or value"));
1369 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1373 if (prop == ZFS_PROP_COMPRESSION) {
1374 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1375 "property setting is not allowed on "
1376 "bootable datasets"));
1377 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1379 (void) zfs_standard_error(hdl, err, errbuf);
1384 if (prop == ZPROP_INVAL) {
1385 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1387 (void) zfs_standard_error(hdl, err, errbuf);
1393 * This platform can't address a volume this big.
1396 if (prop == ZFS_PROP_VOLSIZE) {
1397 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1403 (void) zfs_standard_error(hdl, err, errbuf);
1408 zfs_is_namespace_prop(zfs_prop_t prop)
1412 case ZFS_PROP_ATIME:
1413 case ZFS_PROP_DEVICES:
1415 case ZFS_PROP_SETUID:
1416 case ZFS_PROP_READONLY:
1417 case ZFS_PROP_XATTR:
1418 case ZFS_PROP_NBMAND:
1427 * Given a property name and value, set the property for the given dataset.
1430 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1432 zfs_cmd_t zc = {"\0"};
1434 prop_changelist_t *cl = NULL;
1436 libzfs_handle_t *hdl = zhp->zfs_hdl;
1437 nvlist_t *nvl = NULL, *realprops;
1439 boolean_t do_prefix = B_TRUE;
1442 (void) snprintf(errbuf, sizeof (errbuf),
1443 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1446 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1447 nvlist_add_string(nvl, propname, propval) != 0) {
1448 (void) no_memory(hdl);
1452 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1453 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1459 prop = zfs_name_to_prop(propname);
1461 if (prop == ZFS_PROP_VOLSIZE) {
1462 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1466 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1469 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1471 "child dataset with inherited mountpoint is used "
1472 "in a non-global zone"));
1473 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1478 * We don't want to unmount & remount the dataset when changing
1479 * its canmount property to 'on' or 'noauto'. We only use
1480 * the changelist logic to unmount when setting canmount=off.
1482 if (prop == ZFS_PROP_CANMOUNT) {
1484 int err = zprop_string_to_index(prop, propval, &idx,
1486 if (err == 0 && idx != ZFS_CANMOUNT_OFF)
1487 do_prefix = B_FALSE;
1490 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1494 * Execute the corresponding ioctl() to set this property.
1496 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1498 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1501 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1504 zfs_setprop_error(hdl, prop, errno, errbuf);
1505 if (added_resv && errno == ENOSPC) {
1506 /* clean up the volsize property we tried to set */
1507 uint64_t old_volsize = zfs_prop_get_int(zhp,
1510 zcmd_free_nvlists(&zc);
1511 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1513 if (nvlist_add_uint64(nvl,
1514 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1517 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1519 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1523 ret = changelist_postfix(cl);
1527 * Refresh the statistics so the new property
1528 * value is reflected.
1530 (void) get_stats(zhp);
1533 * Remount the filesystem to propagate the change
1534 * if one of the options handled by the generic
1535 * Linux namespace layer has been modified.
1537 if (zfs_is_namespace_prop(prop) &&
1538 zfs_is_mounted(zhp, NULL))
1539 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1545 zcmd_free_nvlists(&zc);
1547 changelist_free(cl);
1552 * Given a property, inherit the value from the parent dataset, or if received
1553 * is TRUE, revert to the received value, if any.
1556 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1558 zfs_cmd_t zc = {"\0"};
1560 prop_changelist_t *cl;
1561 libzfs_handle_t *hdl = zhp->zfs_hdl;
1565 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1566 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1568 zc.zc_cookie = received;
1569 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1571 * For user properties, the amount of work we have to do is very
1572 * small, so just do it here.
1574 if (!zfs_prop_user(propname)) {
1575 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1576 "invalid property"));
1577 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1580 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1581 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1583 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1584 return (zfs_standard_error(hdl, errno, errbuf));
1590 * Verify that this property is inheritable.
1592 if (zfs_prop_readonly(prop))
1593 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1595 if (!zfs_prop_inheritable(prop) && !received)
1596 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1599 * Check to see if the value applies to this type
1601 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1602 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1605 * Normalize the name, to get rid of shorthand abbreviations.
1607 propname = zfs_prop_to_name(prop);
1608 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1609 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1611 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1612 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1614 "dataset is used in a non-global zone"));
1615 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1619 * Determine datasets which will be affected by this change, if any.
1621 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1624 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1626 "child dataset with inherited mountpoint is used "
1627 "in a non-global zone"));
1628 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1632 if ((ret = changelist_prefix(cl)) != 0)
1635 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1636 return (zfs_standard_error(hdl, errno, errbuf));
1639 if ((ret = changelist_postfix(cl)) != 0)
1643 * Refresh the statistics so the new property is reflected.
1645 (void) get_stats(zhp);
1649 changelist_free(cl);
1654 * True DSL properties are stored in an nvlist. The following two functions
1655 * extract them appropriately.
1658 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1664 if (nvlist_lookup_nvlist(zhp->zfs_props,
1665 zfs_prop_to_name(prop), &nv) == 0) {
1666 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1667 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1669 verify(!zhp->zfs_props_table ||
1670 zhp->zfs_props_table[prop] == B_TRUE);
1671 value = zfs_prop_default_numeric(prop);
1679 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1685 if (nvlist_lookup_nvlist(zhp->zfs_props,
1686 zfs_prop_to_name(prop), &nv) == 0) {
1687 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1688 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1690 verify(!zhp->zfs_props_table ||
1691 zhp->zfs_props_table[prop] == B_TRUE);
1692 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1701 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1703 return (zhp->zfs_props == zhp->zfs_recvd_props);
1707 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1709 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1710 zhp->zfs_props = zhp->zfs_recvd_props;
1714 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1716 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1721 * Internal function for getting a numeric property. Both zfs_prop_get() and
1722 * zfs_prop_get_int() are built using this interface.
1724 * Certain properties can be overridden using 'mount -o'. In this case, scan
1725 * the contents of the /etc/mtab entry, searching for the appropriate options.
1726 * If they differ from the on-disk values, report the current values and mark
1727 * the source "temporary".
1730 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1731 char **source, uint64_t *val)
1733 zfs_cmd_t zc = {"\0"};
1734 nvlist_t *zplprops = NULL;
1736 char *mntopt_on = NULL;
1737 char *mntopt_off = NULL;
1738 boolean_t received = zfs_is_recvd_props_mode(zhp);
1743 case ZFS_PROP_ATIME:
1744 mntopt_on = MNTOPT_ATIME;
1745 mntopt_off = MNTOPT_NOATIME;
1748 case ZFS_PROP_DEVICES:
1749 mntopt_on = MNTOPT_DEVICES;
1750 mntopt_off = MNTOPT_NODEVICES;
1754 mntopt_on = MNTOPT_EXEC;
1755 mntopt_off = MNTOPT_NOEXEC;
1758 case ZFS_PROP_READONLY:
1759 mntopt_on = MNTOPT_RO;
1760 mntopt_off = MNTOPT_RW;
1763 case ZFS_PROP_SETUID:
1764 mntopt_on = MNTOPT_SETUID;
1765 mntopt_off = MNTOPT_NOSETUID;
1768 case ZFS_PROP_XATTR:
1769 mntopt_on = MNTOPT_XATTR;
1770 mntopt_off = MNTOPT_NOXATTR;
1773 case ZFS_PROP_NBMAND:
1774 mntopt_on = MNTOPT_NBMAND;
1775 mntopt_off = MNTOPT_NONBMAND;
1782 * Because looking up the mount options is potentially expensive
1783 * (iterating over all of /etc/mtab), we defer its calculation until
1784 * we're looking up a property which requires its presence.
1786 if (!zhp->zfs_mntcheck &&
1787 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1788 libzfs_handle_t *hdl = zhp->zfs_hdl;
1789 struct mnttab entry;
1791 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1792 zhp->zfs_mntopts = zfs_strdup(hdl,
1794 if (zhp->zfs_mntopts == NULL)
1798 zhp->zfs_mntcheck = B_TRUE;
1801 if (zhp->zfs_mntopts == NULL)
1802 mnt.mnt_mntopts = "";
1804 mnt.mnt_mntopts = zhp->zfs_mntopts;
1807 case ZFS_PROP_ATIME:
1808 case ZFS_PROP_DEVICES:
1810 case ZFS_PROP_READONLY:
1811 case ZFS_PROP_SETUID:
1812 case ZFS_PROP_XATTR:
1813 case ZFS_PROP_NBMAND:
1814 *val = getprop_uint64(zhp, prop, source);
1819 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1822 *src = ZPROP_SRC_TEMPORARY;
1823 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1826 *src = ZPROP_SRC_TEMPORARY;
1830 case ZFS_PROP_CANMOUNT:
1831 case ZFS_PROP_VOLSIZE:
1832 case ZFS_PROP_QUOTA:
1833 case ZFS_PROP_REFQUOTA:
1834 case ZFS_PROP_RESERVATION:
1835 case ZFS_PROP_REFRESERVATION:
1836 *val = getprop_uint64(zhp, prop, source);
1838 if (*source == NULL) {
1839 /* not default, must be local */
1840 *source = zhp->zfs_name;
1844 case ZFS_PROP_MOUNTED:
1845 *val = (zhp->zfs_mntopts != NULL);
1848 case ZFS_PROP_NUMCLONES:
1849 *val = zhp->zfs_dmustats.dds_num_clones;
1852 case ZFS_PROP_VERSION:
1853 case ZFS_PROP_NORMALIZE:
1854 case ZFS_PROP_UTF8ONLY:
1856 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1857 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1859 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1860 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1861 zcmd_free_nvlists(&zc);
1864 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1865 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1867 zcmd_free_nvlists(&zc);
1871 nvlist_free(zplprops);
1872 zcmd_free_nvlists(&zc);
1875 case ZFS_PROP_INCONSISTENT:
1876 *val = zhp->zfs_dmustats.dds_inconsistent;
1880 switch (zfs_prop_get_type(prop)) {
1881 case PROP_TYPE_NUMBER:
1882 case PROP_TYPE_INDEX:
1883 *val = getprop_uint64(zhp, prop, source);
1885 * If we tried to use a default value for a
1886 * readonly property, it means that it was not
1889 if (zfs_prop_readonly(prop) &&
1890 *source != NULL && (*source)[0] == '\0') {
1895 case PROP_TYPE_STRING:
1897 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1898 "cannot get non-numeric property"));
1899 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1900 dgettext(TEXT_DOMAIN, "internal error")));
1908 * Calculate the source type, given the raw source string.
1911 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1912 char *statbuf, size_t statlen)
1914 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1917 if (source == NULL) {
1918 *srctype = ZPROP_SRC_NONE;
1919 } else if (source[0] == '\0') {
1920 *srctype = ZPROP_SRC_DEFAULT;
1921 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1922 *srctype = ZPROP_SRC_RECEIVED;
1924 if (strcmp(source, zhp->zfs_name) == 0) {
1925 *srctype = ZPROP_SRC_LOCAL;
1927 (void) strlcpy(statbuf, source, statlen);
1928 *srctype = ZPROP_SRC_INHERITED;
1935 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1936 size_t proplen, boolean_t literal)
1941 if (zhp->zfs_recvd_props == NULL)
1942 if (get_recvd_props_ioctl(zhp) != 0)
1945 prop = zfs_name_to_prop(propname);
1947 if (prop != ZPROP_INVAL) {
1949 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1951 zfs_set_recvd_props_mode(zhp, &cookie);
1952 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1953 NULL, NULL, 0, literal);
1954 zfs_unset_recvd_props_mode(zhp, &cookie);
1958 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1959 propname, &propval) != 0)
1961 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1963 (void) strlcpy(propbuf, recvdval, proplen);
1966 return (err == 0 ? 0 : -1);
1970 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
1975 value = zfs_get_clones_nvl(zhp);
1980 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
1981 pair = nvlist_next_nvpair(value, pair)) {
1982 if (propbuf[0] != '\0')
1983 (void) strlcat(propbuf, ",", proplen);
1984 (void) strlcat(propbuf, nvpair_name(pair), proplen);
1990 struct get_clones_arg {
1994 char buf[ZFS_MAXNAMELEN];
1998 get_clones_cb(zfs_handle_t *zhp, void *arg)
2000 struct get_clones_arg *gca = arg;
2002 if (gca->numclones == 0) {
2007 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2008 NULL, NULL, 0, B_TRUE) != 0)
2010 if (strcmp(gca->buf, gca->origin) == 0) {
2011 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2016 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2022 zfs_get_clones_nvl(zfs_handle_t *zhp)
2024 nvlist_t *nv, *value;
2026 if (nvlist_lookup_nvlist(zhp->zfs_props,
2027 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2028 struct get_clones_arg gca;
2031 * if this is a snapshot, then the kernel wasn't able
2032 * to get the clones. Do it by slowly iterating.
2034 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2036 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2038 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2043 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2045 gca.origin = zhp->zfs_name;
2047 if (gca.numclones != 0) {
2049 char pool[ZFS_MAXNAMELEN];
2052 /* get the pool name */
2053 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2054 (void) strsep(&cp, "/@");
2055 root = zfs_open(zhp->zfs_hdl, pool,
2056 ZFS_TYPE_FILESYSTEM);
2058 (void) get_clones_cb(root, &gca);
2061 if (gca.numclones != 0 ||
2062 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2063 nvlist_add_nvlist(zhp->zfs_props,
2064 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2071 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2072 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2075 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2081 * Retrieve a property from the given object. If 'literal' is specified, then
2082 * numbers are left as exact values. Otherwise, numbers are converted to a
2083 * human-readable form.
2085 * Returns 0 on success, or -1 on error.
2088 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2089 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2091 char *source = NULL;
2095 boolean_t received = zfs_is_recvd_props_mode(zhp);
2098 * Check to see if this property applies to our object
2100 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2103 if (received && zfs_prop_readonly(prop))
2107 *src = ZPROP_SRC_NONE;
2110 case ZFS_PROP_CREATION:
2112 * 'creation' is a time_t stored in the statistics. We convert
2113 * this into a string unless 'literal' is specified.
2116 val = getprop_uint64(zhp, prop, &source);
2117 time_t time = (time_t)val;
2121 localtime_r(&time, &t) == NULL ||
2122 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2124 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
2128 case ZFS_PROP_MOUNTPOINT:
2130 * Getting the precise mountpoint can be tricky.
2132 * - for 'none' or 'legacy', return those values.
2133 * - for inherited mountpoints, we want to take everything
2134 * after our ancestor and append it to the inherited value.
2136 * If the pool has an alternate root, we want to prepend that
2137 * root to any values we return.
2140 str = getprop_string(zhp, prop, &source);
2142 if (str[0] == '/') {
2143 char buf[MAXPATHLEN];
2145 const char *relpath;
2148 * If we inherit the mountpoint, even from a dataset
2149 * with a received value, the source will be the path of
2150 * the dataset we inherit from. If source is
2151 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2154 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2157 relpath = zhp->zfs_name + strlen(source);
2158 if (relpath[0] == '/')
2162 if ((zpool_get_prop(zhp->zpool_hdl,
2163 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
2164 (strcmp(root, "-") == 0))
2167 * Special case an alternate root of '/'. This will
2168 * avoid having multiple leading slashes in the
2171 if (strcmp(root, "/") == 0)
2175 * If the mountpoint is '/' then skip over this
2176 * if we are obtaining either an alternate root or
2177 * an inherited mountpoint.
2179 if (str[1] == '\0' && (root[0] != '\0' ||
2180 relpath[0] != '\0'))
2183 if (relpath[0] == '\0')
2184 (void) snprintf(propbuf, proplen, "%s%s",
2187 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2188 root, str, relpath[0] == '@' ? "" : "/",
2191 /* 'legacy' or 'none' */
2192 (void) strlcpy(propbuf, str, proplen);
2197 case ZFS_PROP_ORIGIN:
2198 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2201 * If there is no parent at all, return failure to indicate that
2202 * it doesn't apply to this dataset.
2204 if (propbuf[0] == '\0')
2208 case ZFS_PROP_CLONES:
2209 if (get_clones_string(zhp, propbuf, proplen) != 0)
2213 case ZFS_PROP_QUOTA:
2214 case ZFS_PROP_REFQUOTA:
2215 case ZFS_PROP_RESERVATION:
2216 case ZFS_PROP_REFRESERVATION:
2218 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2222 * If quota or reservation is 0, we translate this into 'none'
2223 * (unless literal is set), and indicate that it's the default
2224 * value. Otherwise, we print the number nicely and indicate
2225 * that its set locally.
2229 (void) strlcpy(propbuf, "0", proplen);
2231 (void) strlcpy(propbuf, "none", proplen);
2234 (void) snprintf(propbuf, proplen, "%llu",
2237 zfs_nicenum(val, propbuf, proplen);
2241 case ZFS_PROP_REFRATIO:
2242 case ZFS_PROP_COMPRESSRATIO:
2243 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2245 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2246 (u_longlong_t)(val / 100),
2247 (u_longlong_t)(val % 100));
2251 switch (zhp->zfs_type) {
2252 case ZFS_TYPE_FILESYSTEM:
2255 case ZFS_TYPE_VOLUME:
2258 case ZFS_TYPE_SNAPSHOT:
2264 (void) snprintf(propbuf, proplen, "%s", str);
2267 case ZFS_PROP_MOUNTED:
2269 * The 'mounted' property is a pseudo-property that described
2270 * whether the filesystem is currently mounted. Even though
2271 * it's a boolean value, the typical values of "on" and "off"
2272 * don't make sense, so we translate to "yes" and "no".
2274 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2275 src, &source, &val) != 0)
2278 (void) strlcpy(propbuf, "yes", proplen);
2280 (void) strlcpy(propbuf, "no", proplen);
2285 * The 'name' property is a pseudo-property derived from the
2286 * dataset name. It is presented as a real property to simplify
2289 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2292 case ZFS_PROP_MLSLABEL:
2294 #ifdef HAVE_MLSLABEL
2295 m_label_t *new_sl = NULL;
2296 char *ascii = NULL; /* human readable label */
2298 (void) strlcpy(propbuf,
2299 getprop_string(zhp, prop, &source), proplen);
2301 if (literal || (strcasecmp(propbuf,
2302 ZFS_MLSLABEL_DEFAULT) == 0))
2306 * Try to translate the internal hex string to
2307 * human-readable output. If there are any
2308 * problems just use the hex string.
2311 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2312 L_NO_CORRECTION, NULL) == -1) {
2313 m_label_free(new_sl);
2317 if (label_to_str(new_sl, &ascii, M_LABEL,
2321 m_label_free(new_sl);
2324 m_label_free(new_sl);
2326 (void) strlcpy(propbuf, ascii, proplen);
2329 (void) strlcpy(propbuf,
2330 getprop_string(zhp, prop, &source), proplen);
2331 #endif /* HAVE_MLSLABEL */
2337 * GUIDs are stored as numbers, but they are identifiers.
2338 * We don't want them to be pretty printed, because pretty
2339 * printing mangles the ID into a truncated and useless value.
2341 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2343 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2347 switch (zfs_prop_get_type(prop)) {
2348 case PROP_TYPE_NUMBER:
2349 if (get_numeric_property(zhp, prop, src,
2350 &source, &val) != 0)
2353 (void) snprintf(propbuf, proplen, "%llu",
2356 zfs_nicenum(val, propbuf, proplen);
2359 case PROP_TYPE_STRING:
2360 (void) strlcpy(propbuf,
2361 getprop_string(zhp, prop, &source), proplen);
2364 case PROP_TYPE_INDEX:
2365 if (get_numeric_property(zhp, prop, src,
2366 &source, &val) != 0)
2368 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2370 (void) strlcpy(propbuf, strval, proplen);
2378 get_source(zhp, src, source, statbuf, statlen);
2384 * Utility function to get the given numeric property. Does no validation that
2385 * the given property is the appropriate type; should only be used with
2386 * hard-coded property types.
2389 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2394 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2400 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2404 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2405 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2409 * Similar to zfs_prop_get(), but returns the value as an integer.
2412 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2413 zprop_source_t *src, char *statbuf, size_t statlen)
2418 * Check to see if this property applies to our object
2420 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2421 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2422 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2423 zfs_prop_to_name(prop)));
2427 *src = ZPROP_SRC_NONE;
2429 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2432 get_source(zhp, src, source, statbuf, statlen);
2439 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2440 char **domainp, idmap_rid_t *ridp)
2442 idmap_get_handle_t *get_hdl = NULL;
2446 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2450 err = idmap_get_sidbyuid(get_hdl, id,
2451 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2453 err = idmap_get_sidbygid(get_hdl, id,
2454 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2456 if (err == IDMAP_SUCCESS &&
2457 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2458 status == IDMAP_SUCCESS)
2464 idmap_get_destroy(get_hdl);
2467 #endif /* HAVE_IDMAP */
2470 * convert the propname into parameters needed by kernel
2471 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2472 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2473 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2474 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2477 userquota_propname_decode(const char *propname, boolean_t zoned,
2478 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2480 zfs_userquota_prop_t type;
2489 /* Figure out the property type ({user|group}{quota|space}) */
2490 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2491 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2492 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2495 if (type == ZFS_NUM_USERQUOTA_PROPS)
2499 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2500 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2502 cp = strchr(propname, '@') + 1;
2504 if (isuser && (pw = getpwnam(cp)) != NULL) {
2505 if (zoned && getzoneid() == GLOBAL_ZONEID)
2508 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2509 if (zoned && getzoneid() == GLOBAL_ZONEID)
2512 } else if (strchr(cp, '@')) {
2515 * It's a SID name (eg "user@domain") that needs to be
2516 * turned into S-1-domainID-RID.
2518 directory_error_t e;
2519 char *numericsid = NULL;
2522 if (zoned && getzoneid() == GLOBAL_ZONEID)
2525 e = directory_sid_from_user_name(NULL,
2528 e = directory_sid_from_group_name(NULL,
2532 directory_error_free(e);
2535 if (numericsid == NULL)
2538 (void) strlcpy(domain, cp, domainlen);
2539 cp = strrchr(domain, '-');
2544 *ridp = strtoull(cp, &end, 10);
2547 if (errno != 0 || *end != '\0')
2551 #endif /* HAVE_IDMAP */
2553 /* It's a user/group ID (eg "12345"). */
2556 id = strtoul(cp, &end, 10);
2561 /* It's an ephemeral ID. */
2565 if (idmap_id_to_numeric_domain_rid(id, isuser,
2566 &mapdomain, &rid) != 0)
2568 (void) strlcpy(domain, mapdomain, domainlen);
2572 #endif /* HAVE_IDMAP */
2582 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2583 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2586 zfs_cmd_t zc = {"\0"};
2588 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2590 err = userquota_propname_decode(propname,
2591 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2592 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2593 zc.zc_objset_type = *typep;
2597 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2601 *propvalue = zc.zc_cookie;
2606 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2607 uint64_t *propvalue)
2609 zfs_userquota_prop_t type;
2611 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2616 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2617 char *propbuf, int proplen, boolean_t literal)
2621 zfs_userquota_prop_t type;
2623 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2630 (void) snprintf(propbuf, proplen, "%llu",
2631 (u_longlong_t)propvalue);
2632 } else if (propvalue == 0 &&
2633 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2634 (void) strlcpy(propbuf, "none", proplen);
2636 zfs_nicenum(propvalue, propbuf, proplen);
2642 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2643 uint64_t *propvalue)
2646 zfs_cmd_t zc = {"\0"};
2647 const char *snapname;
2649 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2651 snapname = strchr(propname, '@') + 1;
2652 if (strchr(snapname, '@')) {
2653 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2655 /* snapname is the short name, append it to zhp's fsname */
2658 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2659 sizeof (zc.zc_value));
2660 cp = strchr(zc.zc_value, '@');
2663 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2664 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2667 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2671 *propvalue = zc.zc_cookie;
2676 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2677 char *propbuf, int proplen, boolean_t literal)
2682 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2688 (void) snprintf(propbuf, proplen, "%llu", (long long unsigned int)propvalue);
2690 zfs_nicenum(propvalue, propbuf, proplen);
2697 * Returns the name of the given zfs handle.
2700 zfs_get_name(const zfs_handle_t *zhp)
2702 return (zhp->zfs_name);
2706 * Returns the type of the given zfs handle.
2709 zfs_get_type(const zfs_handle_t *zhp)
2711 return (zhp->zfs_type);
2715 * Is one dataset name a child dataset of another?
2717 * Needs to handle these cases:
2718 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2719 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2720 * Descendant? No. No. No. Yes.
2723 is_descendant(const char *ds1, const char *ds2)
2725 size_t d1len = strlen(ds1);
2727 /* ds2 can't be a descendant if it's smaller */
2728 if (strlen(ds2) < d1len)
2731 /* otherwise, compare strings and verify that there's a '/' char */
2732 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2736 * Given a complete name, return just the portion that refers to the parent.
2737 * Will return -1 if there is no parent (path is just the name of the
2741 parent_name(const char *path, char *buf, size_t buflen)
2745 (void) strlcpy(buf, path, buflen);
2747 if ((slashp = strrchr(buf, '/')) == NULL)
2755 * If accept_ancestor is false, then check to make sure that the given path has
2756 * a parent, and that it exists. If accept_ancestor is true, then find the
2757 * closest existing ancestor for the given path. In prefixlen return the
2758 * length of already existing prefix of the given path. We also fetch the
2759 * 'zoned' property, which is used to validate property settings when creating
2763 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2764 boolean_t accept_ancestor, int *prefixlen)
2766 zfs_cmd_t zc = {"\0"};
2767 char parent[ZFS_MAXNAMELEN];
2773 (void) snprintf(errbuf, sizeof (errbuf),
2774 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2776 /* get parent, and check to see if this is just a pool */
2777 if (parent_name(path, parent, sizeof (parent)) != 0) {
2778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2779 "missing dataset name"));
2780 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2783 /* check to see if the pool exists */
2784 if ((slash = strchr(parent, '/')) == NULL)
2785 slash = parent + strlen(parent);
2786 (void) strncpy(zc.zc_name, parent, slash - parent);
2787 zc.zc_name[slash - parent] = '\0';
2788 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2790 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2791 "no such pool '%s'"), zc.zc_name);
2792 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2795 /* check to see if the parent dataset exists */
2796 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2797 if (errno == ENOENT && accept_ancestor) {
2799 * Go deeper to find an ancestor, give up on top level.
2801 if (parent_name(parent, parent, sizeof (parent)) != 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));
2806 } else if (errno == ENOENT) {
2807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2808 "parent does not exist"));
2809 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2811 return (zfs_standard_error(hdl, errno, errbuf));
2814 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2818 /* we are in a non-global zone, but parent is in the global zone */
2819 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2820 (void) zfs_standard_error(hdl, EPERM, errbuf);
2825 /* make sure parent is a filesystem */
2826 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2827 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2828 "parent is not a filesystem"));
2829 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2835 if (prefixlen != NULL)
2836 *prefixlen = strlen(parent);
2841 * Finds whether the dataset of the given type(s) exists.
2844 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2848 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2852 * Try to get stats for the dataset, which will tell us if it exists.
2854 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2855 int ds_type = zhp->zfs_type;
2858 if (types & ds_type)
2865 * Given a path to 'target', create all the ancestors between
2866 * the prefixlen portion of the path, and the target itself.
2867 * Fail if the initial prefixlen-ancestor does not already exist.
2870 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2876 /* make sure prefix exists */
2877 cp = target + prefixlen;
2879 assert(strchr(cp, '/') == NULL);
2880 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2883 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2891 * Attempt to create, mount, and share any ancestor filesystems,
2892 * up to the prefixlen-long one.
2894 for (cp = target + prefixlen + 1;
2895 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2899 h = make_dataset_handle(hdl, target);
2901 /* it already exists, nothing to do here */
2906 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2908 opname = dgettext(TEXT_DOMAIN, "create");
2912 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2914 opname = dgettext(TEXT_DOMAIN, "open");
2918 if (zfs_mount(h, NULL, 0) != 0) {
2919 opname = dgettext(TEXT_DOMAIN, "mount");
2923 if (zfs_share(h) != 0) {
2924 opname = dgettext(TEXT_DOMAIN, "share");
2934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2935 "failed to %s ancestor '%s'"), opname, target);
2940 * Creates non-existing ancestors of the given path.
2943 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2949 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2952 if ((path_copy = strdup(path)) != NULL) {
2953 rc = create_parents(hdl, path_copy, prefix);
2956 if (path_copy == NULL || rc != 0)
2963 * Create a new filesystem or volume.
2966 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2971 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2974 dmu_objset_type_t ost;
2976 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2977 "cannot create '%s'"), path);
2979 /* validate the path, taking care to note the extended error message */
2980 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2981 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2983 /* validate parents exist */
2984 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2988 * The failure modes when creating a dataset of a different type over
2989 * one that already exists is a little strange. In particular, if you
2990 * try to create a dataset on top of an existing dataset, the ioctl()
2991 * will return ENOENT, not EEXIST. To prevent this from happening, we
2992 * first try to see if the dataset exists.
2994 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
2995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2996 "dataset already exists"));
2997 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3000 if (type == ZFS_TYPE_VOLUME)
3005 if (props && (props = zfs_valid_proplist(hdl, type, props,
3006 zoned, NULL, errbuf)) == 0)
3009 if (type == ZFS_TYPE_VOLUME) {
3011 * If we are creating a volume, the size and block size must
3012 * satisfy a few restraints. First, the blocksize must be a
3013 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3014 * volsize must be a multiple of the block size, and cannot be
3017 if (props == NULL || nvlist_lookup_uint64(props,
3018 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3020 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3021 "missing volume size"));
3022 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3025 if ((ret = nvlist_lookup_uint64(props,
3026 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3027 &blocksize)) != 0) {
3028 if (ret == ENOENT) {
3029 blocksize = zfs_prop_default_numeric(
3030 ZFS_PROP_VOLBLOCKSIZE);
3033 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3034 "missing volume block size"));
3035 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3041 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3042 "volume size cannot be zero"));
3043 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3046 if (size % blocksize != 0) {
3048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3049 "volume size must be a multiple of volume block "
3051 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3055 /* create the dataset */
3056 ret = lzc_create(path, ost, props);
3059 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
3060 ret = zvol_create_link(hdl, path);
3062 (void) zfs_standard_error(hdl, errno,
3063 dgettext(TEXT_DOMAIN,
3064 "Volume successfully created, but device links "
3065 "were not created"));
3070 /* check for failure */
3072 char parent[ZFS_MAXNAMELEN];
3073 (void) parent_name(path, parent, sizeof (parent));
3077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3078 "no such parent '%s'"), parent);
3079 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3082 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3083 "parent '%s' is not a filesystem"), parent);
3084 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3088 "volume block size must be power of 2 from "
3090 (uint_t)SPA_MINBLOCKSIZE,
3091 (uint_t)SPA_MAXBLOCKSIZE >> 10);
3093 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3097 "pool must be upgraded to set this "
3098 "property or value"));
3099 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3103 * This platform can't address a volume this big.
3105 if (type == ZFS_TYPE_VOLUME)
3106 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3111 return (zfs_standard_error(hdl, errno, errbuf));
3119 * Destroys the given dataset. The caller must make sure that the filesystem
3120 * isn't mounted, and that there are no active dependents. If the file system
3121 * does not exist this function does nothing.
3124 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3126 zfs_cmd_t zc = {"\0"};
3128 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3130 if (ZFS_IS_VOLUME(zhp)) {
3131 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3134 zc.zc_objset_type = DMU_OST_ZVOL;
3136 zc.zc_objset_type = DMU_OST_ZFS;
3139 zc.zc_defer_destroy = defer;
3140 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3142 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3143 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3147 remove_mountpoint(zhp);
3152 struct destroydata {
3154 const char *snapname;
3158 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3160 struct destroydata *dd = arg;
3161 char name[ZFS_MAXNAMELEN];
3164 (void) snprintf(name, sizeof (name),
3165 "%s@%s", zhp->zfs_name, dd->snapname);
3167 if (lzc_exists(name))
3168 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3170 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3171 (void) zvol_remove_link(zhp->zfs_hdl, name);
3173 * NB: this is simply a best-effort. We don't want to
3174 * return an error, because then we wouldn't visit all
3179 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3185 * Destroys all snapshots with the given name in zhp & descendants.
3188 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3191 struct destroydata dd = { 0 };
3193 dd.snapname = snapname;
3194 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3195 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3197 if (nvlist_empty(dd.nvl)) {
3198 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3199 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3200 zhp->zfs_name, snapname);
3202 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3204 nvlist_free(dd.nvl);
3209 * Destroys all the snapshots named in the nvlist.
3212 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3218 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3223 if (nvlist_empty(errlist)) {
3225 (void) snprintf(errbuf, sizeof (errbuf),
3226 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3228 ret = zfs_standard_error(hdl, ret, errbuf);
3230 for (pair = nvlist_next_nvpair(errlist, NULL);
3231 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3233 (void) snprintf(errbuf, sizeof (errbuf),
3234 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3237 switch (fnvpair_value_int32(pair)) {
3240 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3241 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3244 ret = zfs_standard_error(hdl, errno, errbuf);
3253 * Clones the given dataset. The target must be of the same type as the source.
3256 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3258 char parent[ZFS_MAXNAMELEN];
3261 libzfs_handle_t *hdl = zhp->zfs_hdl;
3264 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3266 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3267 "cannot create '%s'"), target);
3269 /* validate the target/clone name */
3270 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3271 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3273 /* validate parents exist */
3274 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3277 (void) parent_name(target, parent, sizeof (parent));
3283 if (ZFS_IS_VOLUME(zhp)) {
3284 type = ZFS_TYPE_VOLUME;
3286 type = ZFS_TYPE_FILESYSTEM;
3288 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3289 zhp, errbuf)) == NULL)
3293 ret = lzc_clone(target, zhp->zfs_name, props);
3301 * The parent doesn't exist. We should have caught this
3302 * above, but there may a race condition that has since
3303 * destroyed the parent.
3305 * At this point, we don't know whether it's the source
3306 * that doesn't exist anymore, or whether the target
3307 * dataset doesn't exist.
3309 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3310 "no such parent '%s'"), parent);
3311 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3314 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3315 "source and target pools differ"));
3316 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3320 return (zfs_standard_error(zhp->zfs_hdl, errno,
3323 } else if (ZFS_IS_VOLUME(zhp)) {
3324 ret = zvol_create_link(zhp->zfs_hdl, target);
3330 typedef struct promote_data {
3331 char cb_mountpoint[MAXPATHLEN];
3332 const char *cb_target;
3333 const char *cb_errbuf;
3334 uint64_t cb_pivot_txg;
3338 promote_snap_cb(zfs_handle_t *zhp, void *data)
3340 promote_data_t *pd = data;
3342 char snapname[MAXPATHLEN];
3345 /* We don't care about snapshots after the pivot point */
3346 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3351 /* Remove the device link if it's a zvol. */
3352 if (ZFS_IS_VOLUME(zhp))
3353 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3355 /* Check for conflicting names */
3356 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3357 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3358 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3361 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3362 "snapshot name '%s' from origin \n"
3363 "conflicts with '%s' from target"),
3364 zhp->zfs_name, snapname);
3365 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3372 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3374 promote_data_t *pd = data;
3376 /* We don't care about snapshots after the pivot point */
3377 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3378 /* Create the device link if it's a zvol. */
3379 if (ZFS_IS_VOLUME(zhp))
3380 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3388 * Promotes the given clone fs to be the clone parent.
3391 zfs_promote(zfs_handle_t *zhp)
3393 libzfs_handle_t *hdl = zhp->zfs_hdl;
3394 zfs_cmd_t zc = {"\0"};
3395 char parent[MAXPATHLEN];
3402 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3403 "cannot promote '%s'"), zhp->zfs_name);
3405 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3407 "snapshots can not be promoted"));
3408 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3411 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3412 if (parent[0] == '\0') {
3413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3414 "not a cloned filesystem"));
3415 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3417 cp = strchr(parent, '@');
3420 /* Walk the snapshots we will be moving */
3421 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3424 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3426 pd.cb_target = zhp->zfs_name;
3427 pd.cb_errbuf = errbuf;
3428 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3431 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3432 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3433 ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
3439 /* issue the ioctl */
3440 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3441 sizeof (zc.zc_value));
3442 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3443 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3446 int save_errno = errno;
3448 (void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3452 switch (save_errno) {
3455 * There is a conflicting snapshot name. We
3456 * should have caught this above, but they could
3457 * have renamed something in the mean time.
3459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3460 "conflicting snapshot '%s' from parent '%s'"),
3461 zc.zc_string, parent);
3462 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3465 return (zfs_standard_error(hdl, save_errno, errbuf));
3468 (void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3477 const char *cd_snapname;
3482 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3484 struct createdata *cd = arg;
3487 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3488 char name[MAXPATHLEN];
3490 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3491 (void) strlcat(name, "@", sizeof (name));
3492 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3493 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3496 * NB: this is simply a best-effort. We don't want to
3497 * return an error, because then we wouldn't visit all
3502 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3509 typedef struct snapdata {
3511 const char *sd_snapname;
3515 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3517 snapdata_t *sd = arg;
3518 char name[ZFS_MAXNAMELEN];
3521 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3522 (void) snprintf(name, sizeof (name),
3523 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3525 fnvlist_add_boolean(sd->sd_nvl, name);
3527 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3535 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3539 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3546 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3547 "cannot create snapshots "));
3550 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3551 const char *snapname = nvpair_name(elem);
3553 /* validate the target name */
3554 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3556 (void) snprintf(errbuf, sizeof (errbuf),
3557 dgettext(TEXT_DOMAIN,
3558 "cannot create snapshot '%s'"), snapname);
3559 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3563 if (props != NULL &&
3564 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3565 props, B_FALSE, NULL, errbuf)) == NULL) {
3569 ret = lzc_snapshot(snaps, props, &errors);
3572 boolean_t printed = B_FALSE;
3573 for (elem = nvlist_next_nvpair(errors, NULL);
3575 elem = nvlist_next_nvpair(errors, elem)) {
3576 (void) snprintf(errbuf, sizeof (errbuf),
3577 dgettext(TEXT_DOMAIN,
3578 "cannot create snapshot '%s'"), nvpair_name(elem));
3579 (void) zfs_standard_error(hdl,
3580 fnvpair_value_int32(elem), errbuf);
3586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3587 "multiple snapshots of same "
3589 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3593 (void) zfs_standard_error(hdl, ret, errbuf);
3598 int linktries = 0, linkok = 0, linkfail = 0;
3601 for (snap = nvlist_next_nvpair(snaps, NULL); snap != NULL;
3602 snap = nvlist_next_nvpair(snaps, snap)) {
3603 char *cp, *snapname;
3605 snapname = nvpair_name(snap);
3606 cp = strchr(snapname, '@');
3609 if ((zhp = zfs_open(hdl, snapname, ZFS_TYPE_FILESYSTEM |
3610 ZFS_TYPE_VOLUME)) != NULL) {
3611 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3614 if (zvol_create_link(zhp->zfs_hdl, nvpair_name(snap)))
3624 nvlist_free(errors);
3629 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3633 snapdata_t sd = { 0 };
3634 char fsname[ZFS_MAXNAMELEN];
3639 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3640 "cannot snapshot %s"), path);
3642 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3643 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3645 (void) strlcpy(fsname, path, sizeof (fsname));
3646 cp = strchr(fsname, '@');
3648 sd.sd_snapname = cp + 1;
3650 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3651 ZFS_TYPE_VOLUME)) == NULL) {
3655 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3657 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3659 fnvlist_add_boolean(sd.sd_nvl, path);
3662 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3663 nvlist_free(sd.sd_nvl);
3669 * Destroy any more recent snapshots. We invoke this callback on any dependents
3670 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3671 * is a dependent and we should just destroy it without checking the transaction
3674 typedef struct rollback_data {
3675 const char *cb_target; /* the snapshot */
3676 uint64_t cb_create; /* creation time reference */
3678 boolean_t cb_dependent;
3683 rollback_destroy(zfs_handle_t *zhp, void *data)
3685 rollback_data_t *cbp = data;
3687 if (!cbp->cb_dependent) {
3688 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3689 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3690 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3693 cbp->cb_dependent = B_TRUE;
3694 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3695 rollback_destroy, cbp);
3696 cbp->cb_dependent = B_FALSE;
3698 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3701 /* We must destroy this clone; first unmount it */
3702 prop_changelist_t *clp;
3704 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3705 cbp->cb_force ? MS_FORCE: 0);
3706 if (clp == NULL || changelist_prefix(clp) != 0) {
3707 cbp->cb_error = B_TRUE;
3711 if (zfs_destroy(zhp, B_FALSE) != 0)
3712 cbp->cb_error = B_TRUE;
3714 changelist_remove(clp, zhp->zfs_name);
3715 (void) changelist_postfix(clp);
3716 changelist_free(clp);
3724 * Given a dataset, rollback to a specific snapshot, discarding any
3725 * data changes since then and making it the active dataset.
3727 * Any snapshots more recent than the target are destroyed, along with
3731 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3733 rollback_data_t cb = { 0 };
3735 boolean_t restore_resv = 0;
3736 uint64_t old_volsize = 0, new_volsize;
3737 zfs_prop_t resv_prop = { 0 };
3739 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3740 zhp->zfs_type == ZFS_TYPE_VOLUME);
3743 * Destroy all recent snapshots and their dependents.
3745 cb.cb_force = force;
3746 cb.cb_target = snap->zfs_name;
3747 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3748 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3754 * Now that we have verified that the snapshot is the latest,
3755 * rollback to the given snapshot.
3758 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3759 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3761 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3763 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3765 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3769 * We rely on zfs_iter_children() to verify that there are no
3770 * newer snapshots for the given dataset. Therefore, we can
3771 * simply pass the name on to the ioctl() call. There is still
3772 * an unlikely race condition where the user has taken a
3773 * snapshot since we verified that this was the most recent.
3775 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3777 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3778 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3784 * For volumes, if the pre-rollback volsize matched the pre-
3785 * rollback reservation and the volsize has changed then set
3786 * the reservation property to the post-rollback volsize.
3787 * Make a new handle since the rollback closed the dataset.
3789 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3790 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3791 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3796 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3797 if (old_volsize != new_volsize)
3798 err = zfs_prop_set_int(zhp, resv_prop,
3807 * Renames the given dataset.
3810 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3811 boolean_t force_unmount)
3814 zfs_cmd_t zc = {"\0"};
3816 prop_changelist_t *cl = NULL;
3817 zfs_handle_t *zhrp = NULL;
3818 char *parentname = NULL;
3819 char parent[ZFS_MAXNAMELEN];
3820 libzfs_handle_t *hdl = zhp->zfs_hdl;
3823 /* if we have the same exact name, just return success */
3824 if (strcmp(zhp->zfs_name, target) == 0)
3827 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3828 "cannot rename to '%s'"), target);
3831 * Make sure the target name is valid
3833 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3834 if ((strchr(target, '@') == NULL) ||
3837 * Snapshot target name is abbreviated,
3838 * reconstruct full dataset name
3840 (void) strlcpy(parent, zhp->zfs_name,
3842 delim = strchr(parent, '@');
3843 if (strchr(target, '@') == NULL)
3847 (void) strlcat(parent, target, sizeof (parent));
3851 * Make sure we're renaming within the same dataset.
3853 delim = strchr(target, '@');
3854 if (strncmp(zhp->zfs_name, target, delim - target)
3855 != 0 || zhp->zfs_name[delim - target] != '@') {
3856 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3857 "snapshots must be part of same "
3859 return (zfs_error(hdl, EZFS_CROSSTARGET,
3863 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3864 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3867 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3868 "recursive rename must be a snapshot"));
3869 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3872 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3873 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3875 /* validate parents */
3876 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3879 /* make sure we're in the same pool */
3880 verify((delim = strchr(target, '/')) != NULL);
3881 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3882 zhp->zfs_name[delim - target] != '/') {
3883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3884 "datasets must be within same pool"));
3885 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3888 /* new name cannot be a child of the current dataset name */
3889 if (is_descendant(zhp->zfs_name, target)) {
3890 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3891 "New dataset name cannot be a descendant of "
3892 "current dataset name"));
3893 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3897 (void) snprintf(errbuf, sizeof (errbuf),
3898 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3900 if (getzoneid() == GLOBAL_ZONEID &&
3901 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3903 "dataset is used in a non-global zone"));
3904 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3908 struct destroydata dd;
3910 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3911 if (parentname == NULL) {
3915 delim = strchr(parentname, '@');
3917 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3923 dd.snapname = delim + 1;
3925 /* We remove any zvol links prior to renaming them */
3926 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3927 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3928 nvlist_free(dd.nvl);
3933 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3934 force_unmount ? MS_FORCE : 0)) == NULL)
3937 if (changelist_haszonedchild(cl)) {
3938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3939 "child dataset with inherited mountpoint is used "
3940 "in a non-global zone"));
3941 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3946 if ((ret = changelist_prefix(cl)) != 0)
3950 if (ZFS_IS_VOLUME(zhp))
3951 zc.zc_objset_type = DMU_OST_ZVOL;
3953 zc.zc_objset_type = DMU_OST_ZFS;
3955 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3956 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3958 zc.zc_cookie = recursive;
3960 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3962 * if it was recursive, the one that actually failed will
3965 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3966 "cannot rename '%s'"), zc.zc_name);
3968 if (recursive && errno == EEXIST) {
3969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3970 "a child dataset already has a snapshot "
3971 "with the new name"));
3972 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3974 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3978 * On failure, we still want to remount any filesystems that
3979 * were previously mounted, so we don't alter the system state.
3982 struct createdata cd;
3984 /* only create links for datasets that had existed */
3985 cd.cd_snapname = delim + 1;
3986 cd.cd_ifexists = B_TRUE;
3987 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3990 (void) changelist_postfix(cl);
3994 struct createdata cd;
3996 /* only create links for datasets that had existed */
3997 cd.cd_snapname = strchr(target, '@') + 1;
3998 cd.cd_ifexists = B_TRUE;
3999 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
4002 changelist_rename(cl, zfs_get_name(zhp), target);
4003 ret = changelist_postfix(cl);
4015 changelist_free(cl);
4021 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
4022 * and wait briefly for udev to create the /dev link.
4025 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
4027 return (zvol_create_link_common(hdl, dataset, B_FALSE));
4031 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
4033 zfs_cmd_t zc = {"\0"};
4034 char path[MAXPATHLEN];
4037 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4040 * Issue the appropriate ioctl.
4042 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
4046 * Silently ignore the case where the link already
4047 * exists. This allows 'zfs volinit' to be run multiple
4048 * times without errors.
4054 * snapdev set to hidden :
4055 * device creation was not permitted (see zvol.c)
4056 * ignore error quietly
4062 * Dataset does not exist in the kernel. If we
4063 * don't care (see zfs_rename), then ignore the
4073 return (zfs_standard_error_fmt(hdl, errno,
4074 dgettext(TEXT_DOMAIN, "cannot create device links "
4075 "for '%s'"), dataset));
4080 * Wait for udev to create the device.
4082 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
4083 error = zpool_label_disk_wait(path, DISK_LABEL_WAIT);
4085 (void) printf(gettext("%s may not be immediately "
4086 "available\n"), path);
4092 * Remove a minor node for the given zvol and the associated /dev links.
4095 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
4097 zfs_cmd_t zc = {"\0"};
4098 int timeout = 3000; /* in milliseconds */
4102 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4105 * Due to concurrent updates by udev the device may be reported as
4106 * busy. In this case don't immediately fail. Instead briefly delay
4107 * and retry the ioctl() which is now likely to succeed. If unable
4108 * remove the link after timeout milliseconds return the failure.
4110 for (i = 0; i < timeout; i++) {
4111 error = ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
4112 if (error && errno == EBUSY) {
4124 * Silently ignore the case where the link no longer
4125 * exists, so that 'zfs volfini' can be run multiple
4126 * times without errors.
4131 return (zfs_standard_error_fmt(hdl, errno,
4132 dgettext(TEXT_DOMAIN, "cannot remove device "
4133 "links for '%s': %s"), dataset, strerror(errno)));
4141 zfs_get_user_props(zfs_handle_t *zhp)
4143 return (zhp->zfs_user_props);
4147 * This function is used by 'zfs list' to determine the exact set of columns to
4148 * display, and their maximum widths. This does two main things:
4150 * - If this is a list of all properties, then expand the list to include
4151 * all native properties, and set a flag so that for each dataset we look
4152 * for new unique user properties and add them to the list.
4154 * - For non fixed-width properties, keep track of the maximum width seen
4155 * so that we can size the column appropriately. If the user has
4156 * requested received property values, we also need to compute the width
4157 * of the RECEIVED column.
4160 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
4162 libzfs_handle_t *hdl = zhp->zfs_hdl;
4163 zprop_list_t *entry;
4164 zprop_list_t **last, **start;
4165 nvlist_t *userprops, *propval;
4168 char buf[ZFS_MAXPROPLEN];
4170 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4173 userprops = zfs_get_user_props(zhp);
4176 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4178 * Go through and add any user properties as necessary. We
4179 * start by incrementing our list pointer to the first
4180 * non-native property.
4183 while (*start != NULL) {
4184 if ((*start)->pl_prop == ZPROP_INVAL)
4186 start = &(*start)->pl_next;
4190 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4192 * See if we've already found this property in our list.
4194 for (last = start; *last != NULL;
4195 last = &(*last)->pl_next) {
4196 if (strcmp((*last)->pl_user_prop,
4197 nvpair_name(elem)) == 0)
4201 if (*last == NULL) {
4202 if ((entry = zfs_alloc(hdl,
4203 sizeof (zprop_list_t))) == NULL ||
4204 ((entry->pl_user_prop = zfs_strdup(hdl,
4205 nvpair_name(elem)))) == NULL) {
4210 entry->pl_prop = ZPROP_INVAL;
4211 entry->pl_width = strlen(nvpair_name(elem));
4212 entry->pl_all = B_TRUE;
4219 * Now go through and check the width of any non-fixed columns
4221 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4222 if (entry->pl_fixed)
4225 if (entry->pl_prop != ZPROP_INVAL) {
4226 if (zfs_prop_get(zhp, entry->pl_prop,
4227 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4228 if (strlen(buf) > entry->pl_width)
4229 entry->pl_width = strlen(buf);
4231 if (received && zfs_prop_get_recvd(zhp,
4232 zfs_prop_to_name(entry->pl_prop),
4233 buf, sizeof (buf), B_FALSE) == 0)
4234 if (strlen(buf) > entry->pl_recvd_width)
4235 entry->pl_recvd_width = strlen(buf);
4237 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4239 verify(nvlist_lookup_string(propval,
4240 ZPROP_VALUE, &strval) == 0);
4241 if (strlen(strval) > entry->pl_width)
4242 entry->pl_width = strlen(strval);
4244 if (received && zfs_prop_get_recvd(zhp,
4245 entry->pl_user_prop,
4246 buf, sizeof (buf), B_FALSE) == 0)
4247 if (strlen(buf) > entry->pl_recvd_width)
4248 entry->pl_recvd_width = strlen(buf);
4256 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4261 * Keep a reference to the props-table against which we prune the
4264 zhp->zfs_props_table = props;
4266 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4269 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4270 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4273 * User properties will result in ZPROP_INVAL, and since we
4274 * only know how to prune standard ZFS properties, we always
4275 * leave these in the list. This can also happen if we
4276 * encounter an unknown DSL property (when running older
4277 * software, for example).
4279 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4280 (void) nvlist_remove(zhp->zfs_props,
4281 nvpair_name(curr), nvpair_type(curr));
4287 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4288 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4290 zfs_cmd_t zc = {"\0"};
4291 nvlist_t *nvlist = NULL;
4294 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4295 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4296 zc.zc_cookie = (uint64_t)cmd;
4298 if (cmd == ZFS_SMB_ACL_RENAME) {
4299 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4300 (void) no_memory(hdl);
4306 case ZFS_SMB_ACL_ADD:
4307 case ZFS_SMB_ACL_REMOVE:
4308 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4310 case ZFS_SMB_ACL_RENAME:
4311 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4313 (void) no_memory(hdl);
4316 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4318 (void) no_memory(hdl);
4321 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4322 nvlist_free(nvlist);
4326 case ZFS_SMB_ACL_PURGE:
4331 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4333 nvlist_free(nvlist);
4338 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4339 char *path, char *resource)
4341 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4346 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4347 char *path, char *resource)
4349 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4354 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4356 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4361 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4362 char *oldname, char *newname)
4364 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4369 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4370 zfs_userspace_cb_t func, void *arg)
4372 zfs_cmd_t zc = {"\0"};
4373 zfs_useracct_t buf[100];
4374 libzfs_handle_t *hdl = zhp->zfs_hdl;
4377 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4379 zc.zc_objset_type = type;
4380 zc.zc_nvlist_dst = (uintptr_t)buf;
4383 zfs_useracct_t *zua = buf;
4385 zc.zc_nvlist_dst_size = sizeof (buf);
4386 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4387 char errbuf[ZFS_MAXNAMELEN + 32];
4389 (void) snprintf(errbuf, sizeof (errbuf),
4390 dgettext(TEXT_DOMAIN,
4391 "cannot get used/quota for %s"), zc.zc_name);
4392 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4394 if (zc.zc_nvlist_dst_size == 0)
4397 while (zc.zc_nvlist_dst_size > 0) {
4398 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4399 zua->zu_space)) != 0)
4402 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4411 const char *snapname;
4413 boolean_t recursive;
4418 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4420 struct holdarg *ha = arg;
4421 char name[ZFS_MAXNAMELEN];
4424 (void) snprintf(name, sizeof (name),
4425 "%s@%s", zhp->zfs_name, ha->snapname);
4427 if (lzc_exists(name))
4428 fnvlist_add_string(ha->nvl, name, ha->tag);
4431 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4437 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4438 boolean_t recursive, int cleanup_fd)
4443 ha.nvl = fnvlist_alloc();
4444 ha.snapname = snapname;
4446 ha.recursive = recursive;
4447 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4449 if (nvlist_empty(ha.nvl)) {
4452 fnvlist_free(ha.nvl);
4454 (void) snprintf(errbuf, sizeof (errbuf),
4455 dgettext(TEXT_DOMAIN,
4456 "cannot hold snapshot '%s@%s'"),
4457 zhp->zfs_name, snapname);
4458 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4462 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4463 fnvlist_free(ha.nvl);
4469 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4473 libzfs_handle_t *hdl = zhp->zfs_hdl;
4478 ret = lzc_hold(holds, cleanup_fd, &errors);
4481 /* There may be errors even in the success case. */
4482 fnvlist_free(errors);
4486 if (nvlist_empty(errors)) {
4487 /* no hold-specific errors */
4488 (void) snprintf(errbuf, sizeof (errbuf),
4489 dgettext(TEXT_DOMAIN, "cannot hold"));
4492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4493 "pool must be upgraded"));
4494 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4497 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4500 (void) zfs_standard_error(hdl, ret, errbuf);
4504 for (elem = nvlist_next_nvpair(errors, NULL);
4506 elem = nvlist_next_nvpair(errors, elem)) {
4507 (void) snprintf(errbuf, sizeof (errbuf),
4508 dgettext(TEXT_DOMAIN,
4509 "cannot hold snapshot '%s'"), nvpair_name(elem));
4510 switch (fnvpair_value_int32(elem)) {
4513 * Temporary tags wind up having the ds object id
4514 * prepended. So even if we passed the length check
4515 * above, it's still possible for the tag to wind
4516 * up being slightly too long.
4518 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4521 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4524 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4527 (void) zfs_standard_error(hdl,
4528 fnvpair_value_int32(elem), errbuf);
4532 fnvlist_free(errors);
4537 zfs_release_one(zfs_handle_t *zhp, void *arg)
4539 struct holdarg *ha = arg;
4540 char name[ZFS_MAXNAMELEN];
4542 nvlist_t *existing_holds;
4544 (void) snprintf(name, sizeof (name),
4545 "%s@%s", zhp->zfs_name, ha->snapname);
4547 if (lzc_get_holds(name, &existing_holds) != 0) {
4549 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4552 nvlist_t *torelease = fnvlist_alloc();
4553 fnvlist_add_boolean(torelease, ha->tag);
4554 fnvlist_add_nvlist(ha->nvl, name, torelease);
4555 fnvlist_free(torelease);
4559 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4565 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4566 boolean_t recursive)
4570 nvlist_t *errors = NULL;
4572 libzfs_handle_t *hdl = zhp->zfs_hdl;
4575 ha.nvl = fnvlist_alloc();
4576 ha.snapname = snapname;
4578 ha.recursive = recursive;
4580 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4582 if (nvlist_empty(ha.nvl)) {
4583 fnvlist_free(ha.nvl);
4585 (void) snprintf(errbuf, sizeof (errbuf),
4586 dgettext(TEXT_DOMAIN,
4587 "cannot release hold from snapshot '%s@%s'"),
4588 zhp->zfs_name, snapname);
4590 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4592 (void) zfs_standard_error(hdl, ret, errbuf);
4597 ret = lzc_release(ha.nvl, &errors);
4598 fnvlist_free(ha.nvl);
4601 /* There may be errors even in the success case. */
4602 fnvlist_free(errors);
4606 if (nvlist_empty(errors)) {
4607 /* no hold-specific errors */
4608 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4613 "pool must be upgraded"));
4614 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4617 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4621 for (elem = nvlist_next_nvpair(errors, NULL);
4623 elem = nvlist_next_nvpair(errors, elem)) {
4624 (void) snprintf(errbuf, sizeof (errbuf),
4625 dgettext(TEXT_DOMAIN,
4626 "cannot release hold from snapshot '%s'"),
4628 switch (fnvpair_value_int32(elem)) {
4630 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4633 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4636 (void) zfs_standard_error_fmt(hdl,
4637 fnvpair_value_int32(elem), errbuf);
4641 fnvlist_free(errors);
4646 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4648 zfs_cmd_t zc = {"\0"};
4649 libzfs_handle_t *hdl = zhp->zfs_hdl;
4655 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4656 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4660 nvbuf = malloc(nvsz);
4661 if (nvbuf == NULL) {
4662 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4666 zc.zc_nvlist_dst_size = nvsz;
4667 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4669 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4671 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4672 (void) snprintf(errbuf, sizeof (errbuf),
4673 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4678 nvsz = zc.zc_nvlist_dst_size;
4682 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4683 "pool must be upgraded"));
4684 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4687 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4690 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4693 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4698 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4700 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4701 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4703 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4713 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4715 zfs_cmd_t zc = {"\0"};
4716 libzfs_handle_t *hdl = zhp->zfs_hdl;
4722 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4723 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4725 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4728 nvbuf = malloc(nvsz);
4730 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4733 zc.zc_nvlist_src_size = nvsz;
4734 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4735 zc.zc_perm_action = un;
4737 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4739 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4740 (void) snprintf(errbuf, sizeof (errbuf),
4741 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4745 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4746 "pool must be upgraded"));
4747 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4750 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4753 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4756 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4767 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4772 err = lzc_get_holds(zhp->zfs_name, nvl);
4775 libzfs_handle_t *hdl = zhp->zfs_hdl;
4777 (void) snprintf(errbuf, sizeof (errbuf),
4778 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4783 "pool must be upgraded"));
4784 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4787 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4790 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4793 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4802 * Convert the zvol's volume size to an appropriate reservation.
4803 * Note: If this routine is updated, it is necessary to update the ZFS test
4804 * suite's shell version in reservation.kshlib.
4807 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4810 uint64_t nblocks, volblocksize;
4814 if (nvlist_lookup_string(props,
4815 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4816 ncopies = atoi(strval);
4819 if (nvlist_lookup_uint64(props,
4820 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4821 &volblocksize) != 0)
4822 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4823 nblocks = volsize/volblocksize;
4824 /* start with metadnode L0-L6 */
4826 /* calculate number of indirects */
4827 while (nblocks > 1) {
4828 nblocks += DNODES_PER_LEVEL - 1;
4829 nblocks /= DNODES_PER_LEVEL;
4832 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4835 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4836 * compressed, but in practice they compress down to about
4839 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;