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.
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
46 #include <directory.h>
48 #include <sys/dnode.h>
53 #include "zfs_namecheck.h"
55 #include "libzfs_impl.h"
56 #include "zfs_deleg.h"
58 static int userquota_propname_decode(const char *propname, boolean_t zoned,
59 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
62 * Given a single type (not a mask of types), return the type in a human
66 zfs_type_to_name(zfs_type_t type)
69 case ZFS_TYPE_FILESYSTEM:
70 return (dgettext(TEXT_DOMAIN, "filesystem"));
71 case ZFS_TYPE_SNAPSHOT:
72 return (dgettext(TEXT_DOMAIN, "snapshot"));
74 return (dgettext(TEXT_DOMAIN, "volume"));
83 * Validate a ZFS path. This is used even before trying to open the dataset, to
84 * provide a more meaningful error message. We call zfs_error_aux() to
85 * explain exactly why the name was not valid.
88 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
94 if (dataset_namecheck(path, &why, &what) != 0) {
97 case NAME_ERR_TOOLONG:
98 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
102 case NAME_ERR_LEADING_SLASH:
103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
104 "leading slash in name"));
107 case NAME_ERR_EMPTY_COMPONENT:
108 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
109 "empty component in name"));
112 case NAME_ERR_TRAILING_SLASH:
113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114 "trailing slash in name"));
117 case NAME_ERR_INVALCHAR:
119 dgettext(TEXT_DOMAIN, "invalid character "
120 "'%c' in name"), what);
123 case NAME_ERR_MULTIPLE_AT:
124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
125 "multiple '@' delimiters in name"));
128 case NAME_ERR_NOLETTER:
129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
130 "pool doesn't begin with a letter"));
133 case NAME_ERR_RESERVED:
134 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135 "name is reserved"));
138 case NAME_ERR_DISKLIKE:
139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
140 "reserved disk name"));
150 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
153 "snapshot delimiter '@' in filesystem name"));
157 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "missing '@' delimiter in snapshot name"));
164 if (modifying && strchr(path, '%') != NULL) {
166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
167 "invalid character %c in name"), '%');
175 zfs_name_valid(const char *name, zfs_type_t type)
177 if (type == ZFS_TYPE_POOL)
178 return (zpool_name_valid(NULL, B_FALSE, name));
179 return (zfs_validate_name(NULL, name, type, B_FALSE));
183 * This function takes the raw DSL properties, and filters out the user-defined
184 * properties into a separate nvlist.
187 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
189 libzfs_handle_t *hdl = zhp->zfs_hdl;
194 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
195 (void) no_memory(hdl);
200 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
201 if (!zfs_prop_user(nvpair_name(elem)))
204 verify(nvpair_value_nvlist(elem, &propval) == 0);
205 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
207 (void) no_memory(hdl);
215 static zpool_handle_t *
216 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
218 libzfs_handle_t *hdl = zhp->zfs_hdl;
221 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
222 if (hdl->libzfs_pool_handles != NULL)
223 zph->zpool_next = hdl->libzfs_pool_handles;
224 hdl->libzfs_pool_handles = zph;
229 static zpool_handle_t *
230 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
232 libzfs_handle_t *hdl = zhp->zfs_hdl;
233 zpool_handle_t *zph = hdl->libzfs_pool_handles;
235 while ((zph != NULL) &&
236 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
237 zph = zph->zpool_next;
242 * Returns a handle to the pool that contains the provided dataset.
243 * If a handle to that pool already exists then that handle is returned.
244 * Otherwise, a new handle is created and added to the list of handles.
246 static zpool_handle_t *
247 zpool_handle(zfs_handle_t *zhp)
253 len = strcspn(zhp->zfs_name, "/@") + 1;
254 pool_name = zfs_alloc(zhp->zfs_hdl, len);
255 (void) strlcpy(pool_name, zhp->zfs_name, len);
257 zph = zpool_find_handle(zhp, pool_name, len);
259 zph = zpool_add_handle(zhp, pool_name);
266 zpool_free_handles(libzfs_handle_t *hdl)
268 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
270 while (zph != NULL) {
271 next = zph->zpool_next;
275 hdl->libzfs_pool_handles = NULL;
279 * Utility function to gather stats (objset and zpl) for the given object.
282 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
284 libzfs_handle_t *hdl = zhp->zfs_hdl;
286 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
288 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
289 if (errno == ENOMEM) {
290 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
301 * Utility function to get the received properties of the given object.
304 get_recvd_props_ioctl(zfs_handle_t *zhp)
306 libzfs_handle_t *hdl = zhp->zfs_hdl;
307 nvlist_t *recvdprops;
308 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
311 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
314 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
316 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
317 if (errno == ENOMEM) {
318 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
322 zcmd_free_nvlists(&zc);
327 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
328 zcmd_free_nvlists(&zc);
332 nvlist_free(zhp->zfs_recvd_props);
333 zhp->zfs_recvd_props = recvdprops;
339 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
341 nvlist_t *allprops, *userprops;
343 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
345 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
350 * XXX Why do we store the user props separately, in addition to
351 * storing them in zfs_props?
353 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
354 nvlist_free(allprops);
358 nvlist_free(zhp->zfs_props);
359 nvlist_free(zhp->zfs_user_props);
361 zhp->zfs_props = allprops;
362 zhp->zfs_user_props = userprops;
368 get_stats(zfs_handle_t *zhp)
371 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
373 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
375 if (get_stats_ioctl(zhp, &zc) != 0)
377 else if (put_stats_zhdl(zhp, &zc) != 0)
379 zcmd_free_nvlists(&zc);
384 * Refresh the properties currently stored in the handle.
387 zfs_refresh_properties(zfs_handle_t *zhp)
389 (void) get_stats(zhp);
393 * Makes a handle from the given dataset name. Used by zfs_open() and
394 * zfs_iter_* to create child handles on the fly.
397 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
399 if (put_stats_zhdl(zhp, zc) != 0)
403 * We've managed to open the dataset and gather statistics. Determine
404 * the high-level type.
406 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
407 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
408 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
409 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
413 if (zhp->zfs_dmustats.dds_is_snapshot)
414 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
415 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
416 zhp->zfs_type = ZFS_TYPE_VOLUME;
417 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
418 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
420 abort(); /* we should never see any other types */
422 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
429 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
431 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
433 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
439 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
440 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
444 if (get_stats_ioctl(zhp, &zc) == -1) {
445 zcmd_free_nvlists(&zc);
449 if (make_dataset_handle_common(zhp, &zc) == -1) {
453 zcmd_free_nvlists(&zc);
457 static zfs_handle_t *
458 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
460 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
466 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
467 if (make_dataset_handle_common(zhp, zc) == -1) {
475 * Opens the given snapshot, filesystem, or volume. The 'types'
476 * argument is a mask of acceptable types. The function will print an
477 * appropriate error message and return NULL if it can't be opened.
480 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
485 (void) snprintf(errbuf, sizeof (errbuf),
486 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
489 * Validate the name before we even try to open it.
491 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
493 "invalid dataset name"));
494 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
499 * Try to get stats for the dataset, which will tell us if it exists.
502 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
503 (void) zfs_standard_error(hdl, errno, errbuf);
507 if (!(types & zhp->zfs_type)) {
508 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
517 * Release a ZFS handle. Nothing to do but free the associated memory.
520 zfs_close(zfs_handle_t *zhp)
522 if (zhp->zfs_mntopts)
523 free(zhp->zfs_mntopts);
524 nvlist_free(zhp->zfs_props);
525 nvlist_free(zhp->zfs_user_props);
526 nvlist_free(zhp->zfs_recvd_props);
530 typedef struct mnttab_node {
531 struct mnttab mtn_mt;
536 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
538 const mnttab_node_t *mtn1 = arg1;
539 const mnttab_node_t *mtn2 = arg2;
542 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
546 return (rv > 0 ? 1 : -1);
550 libzfs_mnttab_init(libzfs_handle_t *hdl)
552 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
553 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
554 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
558 libzfs_mnttab_update(libzfs_handle_t *hdl)
562 rewind(hdl->libzfs_mnttab);
563 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
566 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
568 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
569 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
570 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
571 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
572 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
573 avl_add(&hdl->libzfs_mnttab_cache, mtn);
578 libzfs_mnttab_fini(libzfs_handle_t *hdl)
583 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
584 free(mtn->mtn_mt.mnt_special);
585 free(mtn->mtn_mt.mnt_mountp);
586 free(mtn->mtn_mt.mnt_fstype);
587 free(mtn->mtn_mt.mnt_mntopts);
590 avl_destroy(&hdl->libzfs_mnttab_cache);
594 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
596 hdl->libzfs_mnttab_enable = enable;
600 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
601 struct mnttab *entry)
606 if (!hdl->libzfs_mnttab_enable) {
607 struct mnttab srch = { 0 };
609 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
610 libzfs_mnttab_fini(hdl);
611 rewind(hdl->libzfs_mnttab);
612 srch.mnt_special = (char *)fsname;
613 srch.mnt_fstype = MNTTYPE_ZFS;
614 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
620 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
621 libzfs_mnttab_update(hdl);
623 find.mtn_mt.mnt_special = (char *)fsname;
624 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
626 *entry = mtn->mtn_mt;
633 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
634 const char *mountp, const char *mntopts)
638 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
640 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
641 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
642 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
643 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
644 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
645 avl_add(&hdl->libzfs_mnttab_cache, mtn);
649 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
654 find.mtn_mt.mnt_special = (char *)fsname;
655 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
656 avl_remove(&hdl->libzfs_mnttab_cache, ret);
657 free(ret->mtn_mt.mnt_special);
658 free(ret->mtn_mt.mnt_mountp);
659 free(ret->mtn_mt.mnt_fstype);
660 free(ret->mtn_mt.mnt_mntopts);
666 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
668 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
670 if (zpool_handle == NULL)
673 *spa_version = zpool_get_prop_int(zpool_handle,
674 ZPOOL_PROP_VERSION, NULL);
679 * The choice of reservation property depends on the SPA version.
682 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
686 if (zfs_spa_version(zhp, &spa_version) < 0)
689 if (spa_version >= SPA_VERSION_REFRESERVATION)
690 *resv_prop = ZFS_PROP_REFRESERVATION;
692 *resv_prop = ZFS_PROP_RESERVATION;
698 * Given an nvlist of properties to set, validates that they are correct, and
699 * parses any numeric properties (index, boolean, etc) if they are specified as
703 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
704 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
711 int chosen_normal = -1;
714 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
715 (void) no_memory(hdl);
720 * Make sure this property is valid and applies to this type.
724 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
725 const char *propname = nvpair_name(elem);
727 prop = zfs_name_to_prop(propname);
728 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
730 * This is a user property: make sure it's a
731 * string, and that it's less than ZAP_MAXNAMELEN.
733 if (nvpair_type(elem) != DATA_TYPE_STRING) {
734 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
735 "'%s' must be a string"), propname);
736 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
740 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
741 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
742 "property name '%s' is too long"),
744 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
748 (void) nvpair_value_string(elem, &strval);
749 if (nvlist_add_string(ret, propname, strval) != 0) {
750 (void) no_memory(hdl);
757 * Currently, only user properties can be modified on
760 if (type == ZFS_TYPE_SNAPSHOT) {
761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
762 "this property can not be modified for snapshots"));
763 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
767 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
768 zfs_userquota_prop_t uqtype;
769 char newpropname[128];
774 if (userquota_propname_decode(propname, zoned,
775 &uqtype, domain, sizeof (domain), &rid) != 0) {
777 dgettext(TEXT_DOMAIN,
778 "'%s' has an invalid user/group name"),
780 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
784 if (uqtype != ZFS_PROP_USERQUOTA &&
785 uqtype != ZFS_PROP_GROUPQUOTA) {
787 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
789 (void) zfs_error(hdl, EZFS_PROPREADONLY,
794 if (nvpair_type(elem) == DATA_TYPE_STRING) {
795 (void) nvpair_value_string(elem, &strval);
796 if (strcmp(strval, "none") == 0) {
798 } else if (zfs_nicestrtonum(hdl,
799 strval, &intval) != 0) {
800 (void) zfs_error(hdl,
801 EZFS_BADPROP, errbuf);
804 } else if (nvpair_type(elem) ==
806 (void) nvpair_value_uint64(elem, &intval);
808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
809 "use 'none' to disable "
810 "userquota/groupquota"));
814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
815 "'%s' must be a number"), propname);
816 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
821 * Encode the prop name as
822 * userquota@<hex-rid>-domain, to make it easy
823 * for the kernel to decode.
825 (void) snprintf(newpropname, sizeof (newpropname),
826 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
827 (longlong_t)rid, domain);
831 if (nvlist_add_uint64_array(ret, newpropname,
833 (void) no_memory(hdl);
839 if (prop == ZPROP_INVAL) {
840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
841 "invalid property '%s'"), propname);
842 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
846 if (!zfs_prop_valid_for_type(prop, type)) {
848 dgettext(TEXT_DOMAIN, "'%s' does not "
849 "apply to datasets of this type"), propname);
850 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
854 if (zfs_prop_readonly(prop) &&
855 (!zfs_prop_setonce(prop) || zhp != NULL)) {
857 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
859 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
863 if (zprop_parse_value(hdl, elem, prop, type, ret,
864 &strval, &intval, errbuf) != 0)
868 * Perform some additional checks for specific properties.
871 case ZFS_PROP_VERSION:
877 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
878 if (intval < version) {
879 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
880 "Can not downgrade; already at version %u"),
882 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
888 case ZFS_PROP_RECORDSIZE:
889 case ZFS_PROP_VOLBLOCKSIZE:
890 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
891 if (intval < SPA_MINBLOCKSIZE ||
892 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
893 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
894 "'%s' must be power of 2 from %u "
896 (uint_t)SPA_MINBLOCKSIZE,
897 (uint_t)SPA_MAXBLOCKSIZE >> 10);
898 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
903 case ZFS_PROP_MLSLABEL:
906 * Verify the mlslabel string and convert to
907 * internal hex label string.
911 char *hex = NULL; /* internal label string */
913 /* Default value is already OK. */
914 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
917 /* Verify the label can be converted to binary form */
918 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
919 (str_to_label(strval, &new_sl, MAC_LABEL,
920 L_NO_CORRECTION, NULL) == -1)) {
924 /* Now translate to hex internal label string */
925 if (label_to_str(new_sl, &hex, M_INTERNAL,
931 m_label_free(new_sl);
933 /* If string is already in internal form, we're done. */
934 if (strcmp(strval, hex) == 0) {
939 /* Replace the label string with the internal form. */
940 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
942 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
949 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
950 "invalid mlslabel '%s'"), strval);
951 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
952 m_label_free(new_sl); /* OK if null */
957 case ZFS_PROP_MOUNTPOINT:
961 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
962 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
965 if (mountpoint_namecheck(strval, &why)) {
967 case NAME_ERR_LEADING_SLASH:
969 dgettext(TEXT_DOMAIN,
970 "'%s' must be an absolute path, "
971 "'none', or 'legacy'"), propname);
973 case NAME_ERR_TOOLONG:
975 dgettext(TEXT_DOMAIN,
976 "component of '%s' is too long"),
982 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
989 case ZFS_PROP_SHARESMB:
990 case ZFS_PROP_SHARENFS:
992 * For the mountpoint and sharenfs or sharesmb
993 * properties, check if it can be set in a
994 * global/non-global zone based on
995 * the zoned property value:
997 * global zone non-global zone
998 * --------------------------------------------------
999 * zoned=on mountpoint (no) mountpoint (yes)
1000 * sharenfs (no) sharenfs (no)
1001 * sharesmb (no) sharesmb (no)
1003 * zoned=off mountpoint (yes) N/A
1008 if (getzoneid() == GLOBAL_ZONEID) {
1009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1010 "'%s' cannot be set on "
1011 "dataset in a non-global zone"),
1013 (void) zfs_error(hdl, EZFS_ZONED,
1016 } else if (prop == ZFS_PROP_SHARENFS ||
1017 prop == ZFS_PROP_SHARESMB) {
1018 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1019 "'%s' cannot be set in "
1020 "a non-global zone"), propname);
1021 (void) zfs_error(hdl, EZFS_ZONED,
1025 } else if (getzoneid() != GLOBAL_ZONEID) {
1027 * If zoned property is 'off', this must be in
1028 * a global zone. If not, something is wrong.
1030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031 "'%s' cannot be set while dataset "
1032 "'zoned' property is set"), propname);
1033 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1038 * At this point, it is legitimate to set the
1039 * property. Now we want to make sure that the
1040 * property value is valid if it is sharenfs.
1042 if ((prop == ZFS_PROP_SHARENFS ||
1043 prop == ZFS_PROP_SHARESMB) &&
1044 strcmp(strval, "on") != 0 &&
1045 strcmp(strval, "off") != 0) {
1046 zfs_share_proto_t proto;
1048 if (prop == ZFS_PROP_SHARESMB)
1054 * Must be an valid sharing protocol
1055 * option string so init the libshare
1056 * in order to enable the parser and
1057 * then parse the options. We use the
1058 * control API since we don't care about
1059 * the current configuration and don't
1060 * want the overhead of loading it
1061 * until we actually do something.
1064 if (zfs_init_libshare(hdl,
1065 SA_INIT_CONTROL_API) != SA_OK) {
1067 * An error occurred so we can't do
1070 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1071 "'%s' cannot be set: problem "
1072 "in share initialization"),
1074 (void) zfs_error(hdl, EZFS_BADPROP,
1079 if (zfs_parse_options(strval, proto) != SA_OK) {
1081 * There was an error in parsing so
1082 * deal with it by issuing an error
1083 * message and leaving after
1084 * uninitializing the the libshare
1087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1088 "'%s' cannot be set to invalid "
1089 "options"), propname);
1090 (void) zfs_error(hdl, EZFS_BADPROP,
1092 zfs_uninit_libshare(hdl);
1095 zfs_uninit_libshare(hdl);
1099 case ZFS_PROP_UTF8ONLY:
1100 chosen_utf = (int)intval;
1102 case ZFS_PROP_NORMALIZE:
1103 chosen_normal = (int)intval;
1110 * For changes to existing volumes, we have some additional
1111 * checks to enforce.
1113 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1114 uint64_t volsize = zfs_prop_get_int(zhp,
1116 uint64_t blocksize = zfs_prop_get_int(zhp,
1117 ZFS_PROP_VOLBLOCKSIZE);
1121 case ZFS_PROP_RESERVATION:
1122 case ZFS_PROP_REFRESERVATION:
1123 if (intval > volsize) {
1124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1125 "'%s' is greater than current "
1126 "volume size"), propname);
1127 (void) zfs_error(hdl, EZFS_BADPROP,
1133 case ZFS_PROP_VOLSIZE:
1134 if (intval % blocksize != 0) {
1135 zfs_nicenum(blocksize, buf,
1137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1138 "'%s' must be a multiple of "
1139 "volume block size (%s)"),
1141 (void) zfs_error(hdl, EZFS_BADPROP,
1147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1148 "'%s' cannot be zero"),
1150 (void) zfs_error(hdl, EZFS_BADPROP,
1162 * If normalization was chosen, but no UTF8 choice was made,
1163 * enforce rejection of non-UTF8 names.
1165 * If normalization was chosen, but rejecting non-UTF8 names
1166 * was explicitly not chosen, it is an error.
1168 if (chosen_normal > 0 && chosen_utf < 0) {
1169 if (nvlist_add_uint64(ret,
1170 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1171 (void) no_memory(hdl);
1174 } else if (chosen_normal > 0 && chosen_utf == 0) {
1175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1176 "'%s' must be set 'on' if normalization chosen"),
1177 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1178 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1189 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1191 uint64_t old_volsize;
1192 uint64_t new_volsize;
1193 uint64_t old_reservation;
1194 uint64_t new_reservation;
1195 zfs_prop_t resv_prop;
1198 * If this is an existing volume, and someone is setting the volsize,
1199 * make sure that it matches the reservation, or add it if necessary.
1201 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1202 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1204 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1205 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1206 old_reservation) || nvlist_lookup_uint64(nvl,
1207 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1210 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1213 new_reservation = zvol_volsize_to_reservation(new_volsize,
1215 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1216 new_reservation) != 0) {
1217 (void) no_memory(zhp->zfs_hdl);
1224 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1231 * For quotas and reservations, ENOSPC indicates
1232 * something different; setting a quota or reservation
1233 * doesn't use any disk space.
1236 case ZFS_PROP_QUOTA:
1237 case ZFS_PROP_REFQUOTA:
1238 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1239 "size is less than current used or "
1241 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1244 case ZFS_PROP_RESERVATION:
1245 case ZFS_PROP_REFRESERVATION:
1246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1247 "size is greater than available space"));
1248 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1252 (void) zfs_standard_error(hdl, err, errbuf);
1258 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1262 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1266 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1267 "pool and or dataset must be upgraded to set this "
1268 "property or value"));
1269 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1273 if (prop == ZFS_PROP_COMPRESSION) {
1274 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1275 "property setting is not allowed on "
1276 "bootable datasets"));
1277 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1279 (void) zfs_standard_error(hdl, err, errbuf);
1284 if (prop == ZPROP_INVAL) {
1285 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1287 (void) zfs_standard_error(hdl, err, errbuf);
1293 * This platform can't address a volume this big.
1296 if (prop == ZFS_PROP_VOLSIZE) {
1297 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1303 (void) zfs_standard_error(hdl, err, errbuf);
1308 * Given a property name and value, set the property for the given dataset.
1311 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1313 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1315 prop_changelist_t *cl = NULL;
1317 libzfs_handle_t *hdl = zhp->zfs_hdl;
1318 nvlist_t *nvl = NULL, *realprops;
1320 boolean_t do_prefix;
1324 (void) snprintf(errbuf, sizeof (errbuf),
1325 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1328 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1329 nvlist_add_string(nvl, propname, propval) != 0) {
1330 (void) no_memory(hdl);
1334 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1335 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1341 prop = zfs_name_to_prop(propname);
1343 if (prop == ZFS_PROP_VOLSIZE) {
1344 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1348 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1351 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1353 "child dataset with inherited mountpoint is used "
1354 "in a non-global zone"));
1355 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1360 * If the dataset's canmount property is being set to noauto,
1361 * then we want to prevent unmounting & remounting it.
1363 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1364 (zprop_string_to_index(prop, propval, &idx,
1365 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1367 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1371 * Execute the corresponding ioctl() to set this property.
1373 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1375 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1378 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1381 zfs_setprop_error(hdl, prop, errno, errbuf);
1382 if (added_resv && errno == ENOSPC) {
1383 /* clean up the volsize property we tried to set */
1384 uint64_t old_volsize = zfs_prop_get_int(zhp,
1387 zcmd_free_nvlists(&zc);
1388 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1390 if (nvlist_add_uint64(nvl,
1391 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1394 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1396 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1400 ret = changelist_postfix(cl);
1403 * Refresh the statistics so the new property value
1407 (void) get_stats(zhp);
1412 zcmd_free_nvlists(&zc);
1414 changelist_free(cl);
1419 * Given a property, inherit the value from the parent dataset, or if received
1420 * is TRUE, revert to the received value, if any.
1423 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1425 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1427 prop_changelist_t *cl;
1428 libzfs_handle_t *hdl = zhp->zfs_hdl;
1432 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1433 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1435 zc.zc_cookie = received;
1436 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1438 * For user properties, the amount of work we have to do is very
1439 * small, so just do it here.
1441 if (!zfs_prop_user(propname)) {
1442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1443 "invalid property"));
1444 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1447 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1448 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1450 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1451 return (zfs_standard_error(hdl, errno, errbuf));
1457 * Verify that this property is inheritable.
1459 if (zfs_prop_readonly(prop))
1460 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1462 if (!zfs_prop_inheritable(prop) && !received)
1463 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1466 * Check to see if the value applies to this type
1468 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1469 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1472 * Normalize the name, to get rid of shorthand abbreviations.
1474 propname = zfs_prop_to_name(prop);
1475 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1476 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1478 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1479 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1481 "dataset is used in a non-global zone"));
1482 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1486 * Determine datasets which will be affected by this change, if any.
1488 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1491 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1493 "child dataset with inherited mountpoint is used "
1494 "in a non-global zone"));
1495 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1499 if ((ret = changelist_prefix(cl)) != 0)
1502 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1503 return (zfs_standard_error(hdl, errno, errbuf));
1506 if ((ret = changelist_postfix(cl)) != 0)
1510 * Refresh the statistics so the new property is reflected.
1512 (void) get_stats(zhp);
1516 changelist_free(cl);
1521 * True DSL properties are stored in an nvlist. The following two functions
1522 * extract them appropriately.
1525 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1531 if (nvlist_lookup_nvlist(zhp->zfs_props,
1532 zfs_prop_to_name(prop), &nv) == 0) {
1533 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1534 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1536 verify(!zhp->zfs_props_table ||
1537 zhp->zfs_props_table[prop] == B_TRUE);
1538 value = zfs_prop_default_numeric(prop);
1546 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1552 if (nvlist_lookup_nvlist(zhp->zfs_props,
1553 zfs_prop_to_name(prop), &nv) == 0) {
1554 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1555 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1557 verify(!zhp->zfs_props_table ||
1558 zhp->zfs_props_table[prop] == B_TRUE);
1559 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1568 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1570 return (zhp->zfs_props == zhp->zfs_recvd_props);
1574 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1576 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1577 zhp->zfs_props = zhp->zfs_recvd_props;
1581 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1583 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1588 * Internal function for getting a numeric property. Both zfs_prop_get() and
1589 * zfs_prop_get_int() are built using this interface.
1591 * Certain properties can be overridden using 'mount -o'. In this case, scan
1592 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1593 * If they differ from the on-disk values, report the current values and mark
1594 * the source "temporary".
1597 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1598 char **source, uint64_t *val)
1600 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1601 nvlist_t *zplprops = NULL;
1603 char *mntopt_on = NULL;
1604 char *mntopt_off = NULL;
1605 boolean_t received = zfs_is_recvd_props_mode(zhp);
1610 case ZFS_PROP_ATIME:
1611 mntopt_on = MNTOPT_ATIME;
1612 mntopt_off = MNTOPT_NOATIME;
1615 case ZFS_PROP_DEVICES:
1616 mntopt_on = MNTOPT_DEVICES;
1617 mntopt_off = MNTOPT_NODEVICES;
1621 mntopt_on = MNTOPT_EXEC;
1622 mntopt_off = MNTOPT_NOEXEC;
1625 case ZFS_PROP_READONLY:
1626 mntopt_on = MNTOPT_RO;
1627 mntopt_off = MNTOPT_RW;
1630 case ZFS_PROP_SETUID:
1631 mntopt_on = MNTOPT_SETUID;
1632 mntopt_off = MNTOPT_NOSETUID;
1635 case ZFS_PROP_XATTR:
1636 mntopt_on = MNTOPT_XATTR;
1637 mntopt_off = MNTOPT_NOXATTR;
1640 case ZFS_PROP_NBMAND:
1641 mntopt_on = MNTOPT_NBMAND;
1642 mntopt_off = MNTOPT_NONBMAND;
1649 * Because looking up the mount options is potentially expensive
1650 * (iterating over all of /etc/mnttab), we defer its calculation until
1651 * we're looking up a property which requires its presence.
1653 if (!zhp->zfs_mntcheck &&
1654 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1655 libzfs_handle_t *hdl = zhp->zfs_hdl;
1656 struct mnttab entry;
1658 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1659 zhp->zfs_mntopts = zfs_strdup(hdl,
1661 if (zhp->zfs_mntopts == NULL)
1665 zhp->zfs_mntcheck = B_TRUE;
1668 if (zhp->zfs_mntopts == NULL)
1669 mnt.mnt_mntopts = "";
1671 mnt.mnt_mntopts = zhp->zfs_mntopts;
1674 case ZFS_PROP_ATIME:
1675 case ZFS_PROP_DEVICES:
1677 case ZFS_PROP_READONLY:
1678 case ZFS_PROP_SETUID:
1679 case ZFS_PROP_XATTR:
1680 case ZFS_PROP_NBMAND:
1681 *val = getprop_uint64(zhp, prop, source);
1686 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1689 *src = ZPROP_SRC_TEMPORARY;
1690 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1693 *src = ZPROP_SRC_TEMPORARY;
1697 case ZFS_PROP_CANMOUNT:
1698 case ZFS_PROP_VOLSIZE:
1699 case ZFS_PROP_QUOTA:
1700 case ZFS_PROP_REFQUOTA:
1701 case ZFS_PROP_RESERVATION:
1702 case ZFS_PROP_REFRESERVATION:
1703 *val = getprop_uint64(zhp, prop, source);
1705 if (*source == NULL) {
1706 /* not default, must be local */
1707 *source = zhp->zfs_name;
1711 case ZFS_PROP_MOUNTED:
1712 *val = (zhp->zfs_mntopts != NULL);
1715 case ZFS_PROP_NUMCLONES:
1716 *val = zhp->zfs_dmustats.dds_num_clones;
1719 case ZFS_PROP_VERSION:
1720 case ZFS_PROP_NORMALIZE:
1721 case ZFS_PROP_UTF8ONLY:
1723 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1724 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1726 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1727 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1728 zcmd_free_nvlists(&zc);
1731 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1732 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1734 zcmd_free_nvlists(&zc);
1738 nvlist_free(zplprops);
1739 zcmd_free_nvlists(&zc);
1743 switch (zfs_prop_get_type(prop)) {
1744 case PROP_TYPE_NUMBER:
1745 case PROP_TYPE_INDEX:
1746 *val = getprop_uint64(zhp, prop, source);
1748 * If we tried to use a default value for a
1749 * readonly property, it means that it was not
1752 if (zfs_prop_readonly(prop) &&
1753 *source != NULL && (*source)[0] == '\0') {
1758 case PROP_TYPE_STRING:
1760 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1761 "cannot get non-numeric property"));
1762 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1763 dgettext(TEXT_DOMAIN, "internal error")));
1771 * Calculate the source type, given the raw source string.
1774 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1775 char *statbuf, size_t statlen)
1777 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1780 if (source == NULL) {
1781 *srctype = ZPROP_SRC_NONE;
1782 } else if (source[0] == '\0') {
1783 *srctype = ZPROP_SRC_DEFAULT;
1784 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1785 *srctype = ZPROP_SRC_RECEIVED;
1787 if (strcmp(source, zhp->zfs_name) == 0) {
1788 *srctype = ZPROP_SRC_LOCAL;
1790 (void) strlcpy(statbuf, source, statlen);
1791 *srctype = ZPROP_SRC_INHERITED;
1798 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1799 size_t proplen, boolean_t literal)
1804 if (zhp->zfs_recvd_props == NULL)
1805 if (get_recvd_props_ioctl(zhp) != 0)
1808 prop = zfs_name_to_prop(propname);
1810 if (prop != ZPROP_INVAL) {
1812 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1814 zfs_set_recvd_props_mode(zhp, &cookie);
1815 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1816 NULL, NULL, 0, literal);
1817 zfs_unset_recvd_props_mode(zhp, &cookie);
1818 } else if (zfs_prop_userquota(propname)) {
1823 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1824 propname, &propval) != 0)
1826 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1828 (void) strlcpy(propbuf, recvdval, proplen);
1831 return (err == 0 ? 0 : -1);
1835 * Retrieve a property from the given object. If 'literal' is specified, then
1836 * numbers are left as exact values. Otherwise, numbers are converted to a
1837 * human-readable form.
1839 * Returns 0 on success, or -1 on error.
1842 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1843 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1845 char *source = NULL;
1849 boolean_t received = zfs_is_recvd_props_mode(zhp);
1852 * Check to see if this property applies to our object
1854 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1857 if (received && zfs_prop_readonly(prop))
1861 *src = ZPROP_SRC_NONE;
1864 case ZFS_PROP_CREATION:
1866 * 'creation' is a time_t stored in the statistics. We convert
1867 * this into a string unless 'literal' is specified.
1870 val = getprop_uint64(zhp, prop, &source);
1871 time_t time = (time_t)val;
1875 localtime_r(&time, &t) == NULL ||
1876 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1878 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
1882 case ZFS_PROP_MOUNTPOINT:
1884 * Getting the precise mountpoint can be tricky.
1886 * - for 'none' or 'legacy', return those values.
1887 * - for inherited mountpoints, we want to take everything
1888 * after our ancestor and append it to the inherited value.
1890 * If the pool has an alternate root, we want to prepend that
1891 * root to any values we return.
1894 str = getprop_string(zhp, prop, &source);
1896 if (str[0] == '/') {
1897 char buf[MAXPATHLEN];
1899 const char *relpath;
1902 * If we inherit the mountpoint, even from a dataset
1903 * with a received value, the source will be the path of
1904 * the dataset we inherit from. If source is
1905 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1908 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1911 relpath = zhp->zfs_name + strlen(source);
1912 if (relpath[0] == '/')
1916 if ((zpool_get_prop(zhp->zpool_hdl,
1917 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1918 (strcmp(root, "-") == 0))
1921 * Special case an alternate root of '/'. This will
1922 * avoid having multiple leading slashes in the
1925 if (strcmp(root, "/") == 0)
1929 * If the mountpoint is '/' then skip over this
1930 * if we are obtaining either an alternate root or
1931 * an inherited mountpoint.
1933 if (str[1] == '\0' && (root[0] != '\0' ||
1934 relpath[0] != '\0'))
1937 if (relpath[0] == '\0')
1938 (void) snprintf(propbuf, proplen, "%s%s",
1941 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1942 root, str, relpath[0] == '@' ? "" : "/",
1945 /* 'legacy' or 'none' */
1946 (void) strlcpy(propbuf, str, proplen);
1951 case ZFS_PROP_ORIGIN:
1952 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1955 * If there is no parent at all, return failure to indicate that
1956 * it doesn't apply to this dataset.
1958 if (propbuf[0] == '\0')
1962 case ZFS_PROP_QUOTA:
1963 case ZFS_PROP_REFQUOTA:
1964 case ZFS_PROP_RESERVATION:
1965 case ZFS_PROP_REFRESERVATION:
1967 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1971 * If quota or reservation is 0, we translate this into 'none'
1972 * (unless literal is set), and indicate that it's the default
1973 * value. Otherwise, we print the number nicely and indicate
1974 * that its set locally.
1978 (void) strlcpy(propbuf, "0", proplen);
1980 (void) strlcpy(propbuf, "none", proplen);
1983 (void) snprintf(propbuf, proplen, "%llu",
1986 zfs_nicenum(val, propbuf, proplen);
1990 case ZFS_PROP_COMPRESSRATIO:
1991 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1993 (void) snprintf(propbuf, proplen, "%llu.%02llux",
1994 (u_longlong_t)(val / 100),
1995 (u_longlong_t)(val % 100));
1999 switch (zhp->zfs_type) {
2000 case ZFS_TYPE_FILESYSTEM:
2003 case ZFS_TYPE_VOLUME:
2006 case ZFS_TYPE_SNAPSHOT:
2012 (void) snprintf(propbuf, proplen, "%s", str);
2015 case ZFS_PROP_MOUNTED:
2017 * The 'mounted' property is a pseudo-property that described
2018 * whether the filesystem is currently mounted. Even though
2019 * it's a boolean value, the typical values of "on" and "off"
2020 * don't make sense, so we translate to "yes" and "no".
2022 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2023 src, &source, &val) != 0)
2026 (void) strlcpy(propbuf, "yes", proplen);
2028 (void) strlcpy(propbuf, "no", proplen);
2033 * The 'name' property is a pseudo-property derived from the
2034 * dataset name. It is presented as a real property to simplify
2037 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2040 case ZFS_PROP_MLSLABEL:
2042 m_label_t *new_sl = NULL;
2043 char *ascii = NULL; /* human readable label */
2045 (void) strlcpy(propbuf,
2046 getprop_string(zhp, prop, &source), proplen);
2048 if (literal || (strcasecmp(propbuf,
2049 ZFS_MLSLABEL_DEFAULT) == 0))
2053 * Try to translate the internal hex string to
2054 * human-readable output. If there are any
2055 * problems just use the hex string.
2058 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2059 L_NO_CORRECTION, NULL) == -1) {
2060 m_label_free(new_sl);
2064 if (label_to_str(new_sl, &ascii, M_LABEL,
2068 m_label_free(new_sl);
2071 m_label_free(new_sl);
2073 (void) strlcpy(propbuf, ascii, proplen);
2079 switch (zfs_prop_get_type(prop)) {
2080 case PROP_TYPE_NUMBER:
2081 if (get_numeric_property(zhp, prop, src,
2082 &source, &val) != 0)
2085 (void) snprintf(propbuf, proplen, "%llu",
2088 zfs_nicenum(val, propbuf, proplen);
2091 case PROP_TYPE_STRING:
2092 (void) strlcpy(propbuf,
2093 getprop_string(zhp, prop, &source), proplen);
2096 case PROP_TYPE_INDEX:
2097 if (get_numeric_property(zhp, prop, src,
2098 &source, &val) != 0)
2100 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2102 (void) strlcpy(propbuf, strval, proplen);
2110 get_source(zhp, src, source, statbuf, statlen);
2116 * Utility function to get the given numeric property. Does no validation that
2117 * the given property is the appropriate type; should only be used with
2118 * hard-coded property types.
2121 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2126 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2132 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2136 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2137 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2141 * Similar to zfs_prop_get(), but returns the value as an integer.
2144 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2145 zprop_source_t *src, char *statbuf, size_t statlen)
2150 * Check to see if this property applies to our object
2152 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2153 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2154 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2155 zfs_prop_to_name(prop)));
2159 *src = ZPROP_SRC_NONE;
2161 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2164 get_source(zhp, src, source, statbuf, statlen);
2170 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2171 char **domainp, idmap_rid_t *ridp)
2173 idmap_get_handle_t *get_hdl = NULL;
2177 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2181 err = idmap_get_sidbyuid(get_hdl, id,
2182 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2184 err = idmap_get_sidbygid(get_hdl, id,
2185 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2187 if (err == IDMAP_SUCCESS &&
2188 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2189 status == IDMAP_SUCCESS)
2195 idmap_get_destroy(get_hdl);
2200 * convert the propname into parameters needed by kernel
2201 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2202 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2205 userquota_propname_decode(const char *propname, boolean_t zoned,
2206 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2208 zfs_userquota_prop_t type;
2210 char *numericsid = NULL;
2215 /* Figure out the property type ({user|group}{quota|space}) */
2216 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2217 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2218 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2221 if (type == ZFS_NUM_USERQUOTA_PROPS)
2225 isuser = (type == ZFS_PROP_USERQUOTA ||
2226 type == ZFS_PROP_USERUSED);
2228 cp = strchr(propname, '@') + 1;
2230 if (strchr(cp, '@')) {
2232 * It's a SID name (eg "user@domain") that needs to be
2233 * turned into S-1-domainID-RID.
2235 directory_error_t e;
2236 if (zoned && getzoneid() == GLOBAL_ZONEID)
2239 e = directory_sid_from_user_name(NULL,
2242 e = directory_sid_from_group_name(NULL,
2246 directory_error_free(e);
2249 if (numericsid == NULL)
2252 /* will be further decoded below */
2255 if (strncmp(cp, "S-1-", 4) == 0) {
2256 /* It's a numeric SID (eg "S-1-234-567-89") */
2257 (void) strlcpy(domain, cp, domainlen);
2258 cp = strrchr(domain, '-');
2263 *ridp = strtoull(cp, &end, 10);
2268 if (errno != 0 || *end != '\0')
2270 } else if (!isdigit(*cp)) {
2272 * It's a user/group name (eg "user") that needs to be
2273 * turned into a uid/gid
2275 if (zoned && getzoneid() == GLOBAL_ZONEID)
2291 /* It's a user/group ID (eg "12345"). */
2292 uid_t id = strtoul(cp, &end, 10);
2299 /* It's an ephemeral ID. */
2300 if (idmap_id_to_numeric_domain_rid(id, isuser,
2301 &mapdomain, &rid) != 0)
2303 (void) strlcpy(domain, mapdomain, domainlen);
2310 ASSERT3P(numericsid, ==, NULL);
2315 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2316 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2319 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2321 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2323 err = userquota_propname_decode(propname,
2324 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2325 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2326 zc.zc_objset_type = *typep;
2330 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2334 *propvalue = zc.zc_cookie;
2339 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2340 uint64_t *propvalue)
2342 zfs_userquota_prop_t type;
2344 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2349 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2350 char *propbuf, int proplen, boolean_t literal)
2354 zfs_userquota_prop_t type;
2356 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2363 (void) snprintf(propbuf, proplen, "%llu",
2364 (u_longlong_t)propvalue);
2365 } else if (propvalue == 0 &&
2366 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2367 (void) strlcpy(propbuf, "none", proplen);
2369 zfs_nicenum(propvalue, propbuf, proplen);
2375 * Returns the name of the given zfs handle.
2378 zfs_get_name(const zfs_handle_t *zhp)
2380 return (zhp->zfs_name);
2384 * Returns the type of the given zfs handle.
2387 zfs_get_type(const zfs_handle_t *zhp)
2389 return (zhp->zfs_type);
2393 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2396 uint64_t orig_cookie;
2398 orig_cookie = zc->zc_cookie;
2400 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2401 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2406 /* expand nvlist memory and try again */
2407 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2408 zcmd_free_nvlists(zc);
2411 zc->zc_cookie = orig_cookie;
2414 * An errno value of ESRCH indicates normal completion.
2415 * If ENOENT is returned, then the underlying dataset
2416 * has been removed since we obtained the handle.
2423 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2424 dgettext(TEXT_DOMAIN,
2425 "cannot iterate filesystems"));
2433 * Iterate over all child filesystems
2436 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2438 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2442 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2445 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2448 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2451 * Silently ignore errors, as the only plausible explanation is
2452 * that the pool has since been removed.
2454 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2459 if ((ret = func(nzhp, data)) != 0) {
2460 zcmd_free_nvlists(&zc);
2464 zcmd_free_nvlists(&zc);
2465 return ((ret < 0) ? ret : 0);
2469 * Iterate over all snapshots
2472 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2474 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2478 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2481 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2483 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2486 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2491 if ((ret = func(nzhp, data)) != 0) {
2492 zcmd_free_nvlists(&zc);
2496 zcmd_free_nvlists(&zc);
2497 return ((ret < 0) ? ret : 0);
2501 * Iterate over all children, snapshots and filesystems
2504 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2508 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2511 return (zfs_iter_snapshots(zhp, func, data));
2515 * Is one dataset name a child dataset of another?
2517 * Needs to handle these cases:
2518 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2519 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2520 * Descendant? No. No. No. Yes.
2523 is_descendant(const char *ds1, const char *ds2)
2525 size_t d1len = strlen(ds1);
2527 /* ds2 can't be a descendant if it's smaller */
2528 if (strlen(ds2) < d1len)
2531 /* otherwise, compare strings and verify that there's a '/' char */
2532 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2536 * Given a complete name, return just the portion that refers to the parent.
2537 * Can return NULL if this is a pool.
2540 parent_name(const char *path, char *buf, size_t buflen)
2544 if ((loc = strrchr(path, '/')) == NULL)
2547 (void) strncpy(buf, path, MIN(buflen, loc - path));
2548 buf[loc - path] = '\0';
2554 * If accept_ancestor is false, then check to make sure that the given path has
2555 * a parent, and that it exists. If accept_ancestor is true, then find the
2556 * closest existing ancestor for the given path. In prefixlen return the
2557 * length of already existing prefix of the given path. We also fetch the
2558 * 'zoned' property, which is used to validate property settings when creating
2562 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2563 boolean_t accept_ancestor, int *prefixlen)
2565 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2566 char parent[ZFS_MAXNAMELEN];
2572 (void) snprintf(errbuf, sizeof (errbuf),
2573 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2575 /* get parent, and check to see if this is just a pool */
2576 if (parent_name(path, parent, sizeof (parent)) != 0) {
2577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2578 "missing dataset name"));
2579 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2582 /* check to see if the pool exists */
2583 if ((slash = strchr(parent, '/')) == NULL)
2584 slash = parent + strlen(parent);
2585 (void) strncpy(zc.zc_name, parent, slash - parent);
2586 zc.zc_name[slash - parent] = '\0';
2587 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2590 "no such pool '%s'"), zc.zc_name);
2591 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2594 /* check to see if the parent dataset exists */
2595 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2596 if (errno == ENOENT && accept_ancestor) {
2598 * Go deeper to find an ancestor, give up on top level.
2600 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2602 "no such pool '%s'"), zc.zc_name);
2603 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2605 } else if (errno == ENOENT) {
2606 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2607 "parent does not exist"));
2608 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2610 return (zfs_standard_error(hdl, errno, errbuf));
2613 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2617 /* we are in a non-global zone, but parent is in the global zone */
2618 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2619 (void) zfs_standard_error(hdl, EPERM, errbuf);
2624 /* make sure parent is a filesystem */
2625 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2627 "parent is not a filesystem"));
2628 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2634 if (prefixlen != NULL)
2635 *prefixlen = strlen(parent);
2640 * Finds whether the dataset of the given type(s) exists.
2643 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2647 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2651 * Try to get stats for the dataset, which will tell us if it exists.
2653 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2654 int ds_type = zhp->zfs_type;
2657 if (types & ds_type)
2664 * Given a path to 'target', create all the ancestors between
2665 * the prefixlen portion of the path, and the target itself.
2666 * Fail if the initial prefixlen-ancestor does not already exist.
2669 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2675 /* make sure prefix exists */
2676 cp = target + prefixlen;
2678 assert(strchr(cp, '/') == NULL);
2679 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2682 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2690 * Attempt to create, mount, and share any ancestor filesystems,
2691 * up to the prefixlen-long one.
2693 for (cp = target + prefixlen + 1;
2694 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2699 h = make_dataset_handle(hdl, target);
2701 /* it already exists, nothing to do here */
2706 logstr = hdl->libzfs_log_str;
2707 hdl->libzfs_log_str = NULL;
2708 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2710 hdl->libzfs_log_str = logstr;
2711 opname = dgettext(TEXT_DOMAIN, "create");
2715 hdl->libzfs_log_str = logstr;
2716 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2718 opname = dgettext(TEXT_DOMAIN, "open");
2722 if (zfs_mount(h, NULL, 0) != 0) {
2723 opname = dgettext(TEXT_DOMAIN, "mount");
2727 if (zfs_share(h) != 0) {
2728 opname = dgettext(TEXT_DOMAIN, "share");
2738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2739 "failed to %s ancestor '%s'"), opname, target);
2744 * Creates non-existing ancestors of the given path.
2747 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2753 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2756 if ((path_copy = strdup(path)) != NULL) {
2757 rc = create_parents(hdl, path_copy, prefix);
2760 if (path_copy == NULL || rc != 0)
2767 * Create a new filesystem or volume.
2770 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2773 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2776 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2780 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2781 "cannot create '%s'"), path);
2783 /* validate the path, taking care to note the extended error message */
2784 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2785 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2787 /* validate parents exist */
2788 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2792 * The failure modes when creating a dataset of a different type over
2793 * one that already exists is a little strange. In particular, if you
2794 * try to create a dataset on top of an existing dataset, the ioctl()
2795 * will return ENOENT, not EEXIST. To prevent this from happening, we
2796 * first try to see if the dataset exists.
2798 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2799 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2801 "dataset already exists"));
2802 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2805 if (type == ZFS_TYPE_VOLUME)
2806 zc.zc_objset_type = DMU_OST_ZVOL;
2808 zc.zc_objset_type = DMU_OST_ZFS;
2810 if (props && (props = zfs_valid_proplist(hdl, type, props,
2811 zoned, NULL, errbuf)) == 0)
2814 if (type == ZFS_TYPE_VOLUME) {
2816 * If we are creating a volume, the size and block size must
2817 * satisfy a few restraints. First, the blocksize must be a
2818 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2819 * volsize must be a multiple of the block size, and cannot be
2822 if (props == NULL || nvlist_lookup_uint64(props,
2823 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2825 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2826 "missing volume size"));
2827 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2830 if ((ret = nvlist_lookup_uint64(props,
2831 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2832 &blocksize)) != 0) {
2833 if (ret == ENOENT) {
2834 blocksize = zfs_prop_default_numeric(
2835 ZFS_PROP_VOLBLOCKSIZE);
2838 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2839 "missing volume block size"));
2840 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2846 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2847 "volume size cannot be zero"));
2848 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2851 if (size % blocksize != 0) {
2853 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2854 "volume size must be a multiple of volume block "
2856 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2860 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2864 /* create the dataset */
2865 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2867 zcmd_free_nvlists(&zc);
2869 /* check for failure */
2871 char parent[ZFS_MAXNAMELEN];
2872 (void) parent_name(path, parent, sizeof (parent));
2876 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2877 "no such parent '%s'"), parent);
2878 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2881 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2882 "parent '%s' is not a filesystem"), parent);
2883 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2886 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2887 "volume block size must be power of 2 from "
2889 (uint_t)SPA_MINBLOCKSIZE,
2890 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2892 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2895 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2896 "pool must be upgraded to set this "
2897 "property or value"));
2898 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2902 * This platform can't address a volume this big.
2904 if (type == ZFS_TYPE_VOLUME)
2905 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2910 return (zfs_standard_error(hdl, errno, errbuf));
2918 * Destroys the given dataset. The caller must make sure that the filesystem
2919 * isn't mounted, and that there are no active dependents.
2922 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2924 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2926 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2928 if (ZFS_IS_VOLUME(zhp)) {
2929 zc.zc_objset_type = DMU_OST_ZVOL;
2931 zc.zc_objset_type = DMU_OST_ZFS;
2934 zc.zc_defer_destroy = defer;
2935 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2936 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2937 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2941 remove_mountpoint(zhp);
2946 struct destroydata {
2953 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
2955 struct destroydata *dd = arg;
2957 char name[ZFS_MAXNAMELEN];
2958 boolean_t closezhp = dd->closezhp;
2961 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
2962 (void) strlcat(name, "@", sizeof (name));
2963 (void) strlcat(name, dd->snapname, sizeof (name));
2965 szhp = make_dataset_handle(zhp->zfs_hdl, name);
2967 dd->gotone = B_TRUE;
2971 dd->closezhp = B_TRUE;
2973 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
2980 * Destroys all snapshots with the given name in zhp & descendants.
2983 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
2985 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2987 struct destroydata dd = { 0 };
2989 dd.snapname = snapname;
2990 (void) zfs_check_snap_cb(zhp, &dd);
2993 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
2994 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
2995 zhp->zfs_name, snapname));
2998 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2999 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3000 zc.zc_defer_destroy = defer;
3002 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3006 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3007 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3011 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3012 "snapshot is cloned"));
3013 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3016 return (zfs_standard_error(zhp->zfs_hdl, errno,
3025 * Clones the given dataset. The target must be of the same type as the source.
3028 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3030 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3031 char parent[ZFS_MAXNAMELEN];
3034 libzfs_handle_t *hdl = zhp->zfs_hdl;
3038 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3040 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3041 "cannot create '%s'"), target);
3043 /* validate the target name */
3044 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3045 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3047 /* validate parents exist */
3048 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3051 (void) parent_name(target, parent, sizeof (parent));
3054 if (ZFS_IS_VOLUME(zhp)) {
3055 zc.zc_objset_type = DMU_OST_ZVOL;
3056 type = ZFS_TYPE_VOLUME;
3058 zc.zc_objset_type = DMU_OST_ZFS;
3059 type = ZFS_TYPE_FILESYSTEM;
3063 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3064 zhp, errbuf)) == NULL)
3067 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3075 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3076 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3077 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3079 zcmd_free_nvlists(&zc);
3086 * The parent doesn't exist. We should have caught this
3087 * above, but there may a race condition that has since
3088 * destroyed the parent.
3090 * At this point, we don't know whether it's the source
3091 * that doesn't exist anymore, or whether the target
3092 * dataset doesn't exist.
3094 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3095 "no such parent '%s'"), parent);
3096 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3099 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3100 "source and target pools differ"));
3101 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3105 return (zfs_standard_error(zhp->zfs_hdl, errno,
3114 * Promotes the given clone fs to be the clone parent.
3117 zfs_promote(zfs_handle_t *zhp)
3119 libzfs_handle_t *hdl = zhp->zfs_hdl;
3120 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3121 char parent[MAXPATHLEN];
3125 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3126 "cannot promote '%s'"), zhp->zfs_name);
3128 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3130 "snapshots can not be promoted"));
3131 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3134 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3135 if (parent[0] == '\0') {
3136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3137 "not a cloned filesystem"));
3138 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3141 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3142 sizeof (zc.zc_value));
3143 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3144 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3147 int save_errno = errno;
3149 switch (save_errno) {
3151 /* There is a conflicting snapshot name. */
3152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3153 "conflicting snapshot '%s' from parent '%s'"),
3154 zc.zc_string, parent);
3155 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3158 return (zfs_standard_error(hdl, save_errno, errbuf));
3165 * Takes a snapshot of the given dataset.
3168 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3172 char parent[ZFS_MAXNAMELEN];
3174 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3178 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3179 "cannot snapshot '%s'"), path);
3181 /* validate the target name */
3182 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3183 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3186 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3187 props, B_FALSE, NULL, errbuf)) == NULL)
3190 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3198 /* make sure the parent exists and is of the appropriate type */
3199 delim = strchr(path, '@');
3200 (void) strncpy(parent, path, delim - path);
3201 parent[delim - path] = '\0';
3203 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3204 ZFS_TYPE_VOLUME)) == NULL) {
3205 zcmd_free_nvlists(&zc);
3209 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3210 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3211 if (ZFS_IS_VOLUME(zhp))
3212 zc.zc_objset_type = DMU_OST_ZVOL;
3214 zc.zc_objset_type = DMU_OST_ZFS;
3215 zc.zc_cookie = recursive;
3216 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3218 zcmd_free_nvlists(&zc);
3221 * if it was recursive, the one that actually failed will be in
3225 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3226 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3227 (void) zfs_standard_error(hdl, errno, errbuf);
3236 * Destroy any more recent snapshots. We invoke this callback on any dependents
3237 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3238 * is a dependent and we should just destroy it without checking the transaction
3241 typedef struct rollback_data {
3242 const char *cb_target; /* the snapshot */
3243 uint64_t cb_create; /* creation time reference */
3245 boolean_t cb_dependent;
3250 rollback_destroy(zfs_handle_t *zhp, void *data)
3252 rollback_data_t *cbp = data;
3254 if (!cbp->cb_dependent) {
3255 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3256 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3257 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3261 cbp->cb_dependent = B_TRUE;
3262 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3263 rollback_destroy, cbp);
3264 cbp->cb_dependent = B_FALSE;
3266 logstr = zhp->zfs_hdl->libzfs_log_str;
3267 zhp->zfs_hdl->libzfs_log_str = NULL;
3268 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3269 zhp->zfs_hdl->libzfs_log_str = logstr;
3272 /* We must destroy this clone; first unmount it */
3273 prop_changelist_t *clp;
3275 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3276 cbp->cb_force ? MS_FORCE: 0);
3277 if (clp == NULL || changelist_prefix(clp) != 0) {
3278 cbp->cb_error = B_TRUE;
3282 if (zfs_destroy(zhp, B_FALSE) != 0)
3283 cbp->cb_error = B_TRUE;
3285 changelist_remove(clp, zhp->zfs_name);
3286 (void) changelist_postfix(clp);
3287 changelist_free(clp);
3295 * Given a dataset, rollback to a specific snapshot, discarding any
3296 * data changes since then and making it the active dataset.
3298 * Any snapshots more recent than the target are destroyed, along with
3302 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3304 rollback_data_t cb = { 0 };
3306 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3307 boolean_t restore_resv = 0;
3308 uint64_t old_volsize = 0, new_volsize;
3309 zfs_prop_t resv_prop = { 0 };
3311 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3312 zhp->zfs_type == ZFS_TYPE_VOLUME);
3315 * Destroy all recent snapshots and its dependends.
3317 cb.cb_force = force;
3318 cb.cb_target = snap->zfs_name;
3319 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3320 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3326 * Now that we have verified that the snapshot is the latest,
3327 * rollback to the given snapshot.
3330 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3331 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3333 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3335 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3338 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3340 if (ZFS_IS_VOLUME(zhp))
3341 zc.zc_objset_type = DMU_OST_ZVOL;
3343 zc.zc_objset_type = DMU_OST_ZFS;
3346 * We rely on zfs_iter_children() to verify that there are no
3347 * newer snapshots for the given dataset. Therefore, we can
3348 * simply pass the name on to the ioctl() call. There is still
3349 * an unlikely race condition where the user has taken a
3350 * snapshot since we verified that this was the most recent.
3353 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3354 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3355 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3361 * For volumes, if the pre-rollback volsize matched the pre-
3362 * rollback reservation and the volsize has changed then set
3363 * the reservation property to the post-rollback volsize.
3364 * Make a new handle since the rollback closed the dataset.
3366 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3367 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3369 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3370 if (old_volsize != new_volsize)
3371 err = zfs_prop_set_int(zhp, resv_prop,
3380 * Iterate over all dependents for a given dataset. This includes both
3381 * hierarchical dependents (children) and data dependents (snapshots and
3382 * clones). The bulk of the processing occurs in get_dependents() in
3386 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3387 zfs_iter_f func, void *data)
3392 zfs_handle_t *child;
3395 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3396 &dependents, &count) != 0)
3399 for (i = 0; i < count; i++) {
3400 if ((child = make_dataset_handle(zhp->zfs_hdl,
3401 dependents[i])) == NULL)
3404 if ((ret = func(child, data)) != 0)
3408 for (i = 0; i < count; i++)
3409 free(dependents[i]);
3416 * Renames the given dataset.
3419 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3422 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3424 prop_changelist_t *cl = NULL;
3425 zfs_handle_t *zhrp = NULL;
3426 char *parentname = NULL;
3427 char parent[ZFS_MAXNAMELEN];
3428 libzfs_handle_t *hdl = zhp->zfs_hdl;
3431 /* if we have the same exact name, just return success */
3432 if (strcmp(zhp->zfs_name, target) == 0)
3435 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3436 "cannot rename to '%s'"), target);
3439 * Make sure the target name is valid
3441 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3442 if ((strchr(target, '@') == NULL) ||
3445 * Snapshot target name is abbreviated,
3446 * reconstruct full dataset name
3448 (void) strlcpy(parent, zhp->zfs_name,
3450 delim = strchr(parent, '@');
3451 if (strchr(target, '@') == NULL)
3455 (void) strlcat(parent, target, sizeof (parent));
3459 * Make sure we're renaming within the same dataset.
3461 delim = strchr(target, '@');
3462 if (strncmp(zhp->zfs_name, target, delim - target)
3463 != 0 || zhp->zfs_name[delim - target] != '@') {
3464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3465 "snapshots must be part of same "
3467 return (zfs_error(hdl, EZFS_CROSSTARGET,
3471 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3472 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3475 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3476 "recursive rename must be a snapshot"));
3477 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3480 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3481 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3483 /* validate parents */
3484 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3487 /* make sure we're in the same pool */
3488 verify((delim = strchr(target, '/')) != NULL);
3489 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3490 zhp->zfs_name[delim - target] != '/') {
3491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3492 "datasets must be within same pool"));
3493 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3496 /* new name cannot be a child of the current dataset name */
3497 if (is_descendant(zhp->zfs_name, target)) {
3498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3499 "New dataset name cannot be a descendant of "
3500 "current dataset name"));
3501 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3505 (void) snprintf(errbuf, sizeof (errbuf),
3506 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3508 if (getzoneid() == GLOBAL_ZONEID &&
3509 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3511 "dataset is used in a non-global zone"));
3512 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3517 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3518 if (parentname == NULL) {
3522 delim = strchr(parentname, '@');
3524 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3531 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3534 if (changelist_haszonedchild(cl)) {
3535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3536 "child dataset with inherited mountpoint is used "
3537 "in a non-global zone"));
3538 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3543 if ((ret = changelist_prefix(cl)) != 0)
3547 if (ZFS_IS_VOLUME(zhp))
3548 zc.zc_objset_type = DMU_OST_ZVOL;
3550 zc.zc_objset_type = DMU_OST_ZFS;
3552 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3553 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3555 zc.zc_cookie = recursive;
3557 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3559 * if it was recursive, the one that actually failed will
3562 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3563 "cannot rename '%s'"), zc.zc_name);
3565 if (recursive && errno == EEXIST) {
3566 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3567 "a child dataset already has a snapshot "
3568 "with the new name"));
3569 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3571 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3575 * On failure, we still want to remount any filesystems that
3576 * were previously mounted, so we don't alter the system state.
3579 (void) changelist_postfix(cl);
3582 changelist_rename(cl, zfs_get_name(zhp), target);
3583 ret = changelist_postfix(cl);
3595 changelist_free(cl);
3601 zfs_get_user_props(zfs_handle_t *zhp)
3603 return (zhp->zfs_user_props);
3607 zfs_get_recvd_props(zfs_handle_t *zhp)
3609 if (zhp->zfs_recvd_props == NULL)
3610 if (get_recvd_props_ioctl(zhp) != 0)
3612 return (zhp->zfs_recvd_props);
3616 * This function is used by 'zfs list' to determine the exact set of columns to
3617 * display, and their maximum widths. This does two main things:
3619 * - If this is a list of all properties, then expand the list to include
3620 * all native properties, and set a flag so that for each dataset we look
3621 * for new unique user properties and add them to the list.
3623 * - For non fixed-width properties, keep track of the maximum width seen
3624 * so that we can size the column appropriately. If the user has
3625 * requested received property values, we also need to compute the width
3626 * of the RECEIVED column.
3629 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3631 libzfs_handle_t *hdl = zhp->zfs_hdl;
3632 zprop_list_t *entry;
3633 zprop_list_t **last, **start;
3634 nvlist_t *userprops, *propval;
3637 char buf[ZFS_MAXPROPLEN];
3639 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3642 userprops = zfs_get_user_props(zhp);
3645 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3647 * Go through and add any user properties as necessary. We
3648 * start by incrementing our list pointer to the first
3649 * non-native property.
3652 while (*start != NULL) {
3653 if ((*start)->pl_prop == ZPROP_INVAL)
3655 start = &(*start)->pl_next;
3659 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3661 * See if we've already found this property in our list.
3663 for (last = start; *last != NULL;
3664 last = &(*last)->pl_next) {
3665 if (strcmp((*last)->pl_user_prop,
3666 nvpair_name(elem)) == 0)
3670 if (*last == NULL) {
3671 if ((entry = zfs_alloc(hdl,
3672 sizeof (zprop_list_t))) == NULL ||
3673 ((entry->pl_user_prop = zfs_strdup(hdl,
3674 nvpair_name(elem)))) == NULL) {
3679 entry->pl_prop = ZPROP_INVAL;
3680 entry->pl_width = strlen(nvpair_name(elem));
3681 entry->pl_all = B_TRUE;
3688 * Now go through and check the width of any non-fixed columns
3690 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3691 if (entry->pl_fixed)
3694 if (entry->pl_prop != ZPROP_INVAL) {
3695 if (zfs_prop_get(zhp, entry->pl_prop,
3696 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3697 if (strlen(buf) > entry->pl_width)
3698 entry->pl_width = strlen(buf);
3700 if (received && zfs_prop_get_recvd(zhp,
3701 zfs_prop_to_name(entry->pl_prop),
3702 buf, sizeof (buf), B_FALSE) == 0)
3703 if (strlen(buf) > entry->pl_recvd_width)
3704 entry->pl_recvd_width = strlen(buf);
3706 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3708 verify(nvlist_lookup_string(propval,
3709 ZPROP_VALUE, &strval) == 0);
3710 if (strlen(strval) > entry->pl_width)
3711 entry->pl_width = strlen(strval);
3713 if (received && zfs_prop_get_recvd(zhp,
3714 entry->pl_user_prop,
3715 buf, sizeof (buf), B_FALSE) == 0)
3716 if (strlen(buf) > entry->pl_recvd_width)
3717 entry->pl_recvd_width = strlen(buf);
3725 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3726 char *resource, void *export, void *sharetab,
3727 int sharemax, zfs_share_op_t operation)
3729 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3732 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3733 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3735 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3736 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3737 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3738 zc.zc_share.z_sharetype = operation;
3739 zc.zc_share.z_sharemax = sharemax;
3740 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3745 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3750 * Keep a reference to the props-table against which we prune the
3753 zhp->zfs_props_table = props;
3755 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3758 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3759 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3762 * User properties will result in ZPROP_INVAL, and since we
3763 * only know how to prune standard ZFS properties, we always
3764 * leave these in the list. This can also happen if we
3765 * encounter an unknown DSL property (when running older
3766 * software, for example).
3768 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3769 (void) nvlist_remove(zhp->zfs_props,
3770 nvpair_name(curr), nvpair_type(curr));
3776 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3777 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3779 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3780 nvlist_t *nvlist = NULL;
3783 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3784 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3785 zc.zc_cookie = (uint64_t)cmd;
3787 if (cmd == ZFS_SMB_ACL_RENAME) {
3788 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3789 (void) no_memory(hdl);
3795 case ZFS_SMB_ACL_ADD:
3796 case ZFS_SMB_ACL_REMOVE:
3797 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3799 case ZFS_SMB_ACL_RENAME:
3800 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3802 (void) no_memory(hdl);
3805 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3807 (void) no_memory(hdl);
3810 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3811 nvlist_free(nvlist);
3815 case ZFS_SMB_ACL_PURGE:
3820 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3822 nvlist_free(nvlist);
3827 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
3828 char *path, char *resource)
3830 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
3835 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
3836 char *path, char *resource)
3838 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
3843 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
3845 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
3850 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
3851 char *oldname, char *newname)
3853 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
3858 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
3859 zfs_userspace_cb_t func, void *arg)
3861 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3863 zfs_useracct_t buf[100];
3865 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3867 zc.zc_objset_type = type;
3868 zc.zc_nvlist_dst = (uintptr_t)buf;
3872 zfs_useracct_t *zua = buf;
3874 zc.zc_nvlist_dst_size = sizeof (buf);
3875 error = ioctl(zhp->zfs_hdl->libzfs_fd,
3876 ZFS_IOC_USERSPACE_MANY, &zc);
3877 if (error || zc.zc_nvlist_dst_size == 0)
3880 while (zc.zc_nvlist_dst_size > 0) {
3881 error = func(arg, zua->zu_domain, zua->zu_rid,
3886 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
3894 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
3895 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
3896 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
3898 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3899 libzfs_handle_t *hdl = zhp->zfs_hdl;
3901 ASSERT(!recursive || dsobj == 0);
3903 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3904 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3905 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3906 >= sizeof (zc.zc_string))
3907 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3908 zc.zc_cookie = recursive;
3909 zc.zc_temphold = temphold;
3910 zc.zc_cleanup_fd = cleanup_fd;
3911 zc.zc_sendobj = dsobj;
3912 zc.zc_createtxg = createtxg;
3914 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
3915 char errbuf[ZFS_MAXNAMELEN+32];
3918 * if it was recursive, the one that actually failed will be in
3921 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3922 "cannot hold '%s@%s'"), zc.zc_name, snapname);
3926 * Temporary tags wind up having the ds object id
3927 * prepended. So even if we passed the length check
3928 * above, it's still possible for the tag to wind
3929 * up being slightly too long.
3931 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
3933 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3934 "pool must be upgraded"));
3935 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3937 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3939 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
3945 return (zfs_standard_error_fmt(hdl, errno, errbuf));
3953 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
3954 boolean_t recursive)
3956 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3957 libzfs_handle_t *hdl = zhp->zfs_hdl;
3959 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3960 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3961 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3962 >= sizeof (zc.zc_string))
3963 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3964 zc.zc_cookie = recursive;
3966 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
3967 char errbuf[ZFS_MAXNAMELEN+32];
3970 * if it was recursive, the one that actually failed will be in
3973 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3974 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
3978 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
3980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3981 "pool must be upgraded"));
3982 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3984 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3986 return (zfs_standard_error_fmt(hdl, errno, errbuf));
3994 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
3997 uint64_t nblocks, volblocksize;
4001 if (nvlist_lookup_string(props,
4002 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4003 ncopies = atoi(strval);
4006 if (nvlist_lookup_uint64(props,
4007 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4008 &volblocksize) != 0)
4009 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4010 nblocks = volsize/volblocksize;
4011 /* start with metadnode L0-L6 */
4013 /* calculate number of indirects */
4014 while (nblocks > 1) {
4015 nblocks += DNODES_PER_LEVEL - 1;
4016 nblocks /= DNODES_PER_LEVEL;
4019 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4022 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4023 * compressed, but in practice they compress down to about
4026 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;