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>
47 #include <directory.h>
48 #endif /* HAVE_IDMAP */
50 #include <sys/dnode.h>
55 #include "zfs_namecheck.h"
57 #include "libzfs_impl.h"
58 #include "zfs_deleg.h"
60 static int userquota_propname_decode(const char *propname, boolean_t zoned,
61 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
64 * Given a single type (not a mask of types), return the type in a human
68 zfs_type_to_name(zfs_type_t type)
71 case ZFS_TYPE_FILESYSTEM:
72 return (dgettext(TEXT_DOMAIN, "filesystem"));
73 case ZFS_TYPE_SNAPSHOT:
74 return (dgettext(TEXT_DOMAIN, "snapshot"));
76 return (dgettext(TEXT_DOMAIN, "volume"));
85 * Validate a ZFS path. This is used even before trying to open the dataset, to
86 * provide a more meaningful error message. We call zfs_error_aux() to
87 * explain exactly why the name was not valid.
90 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
96 if (dataset_namecheck(path, &why, &what) != 0) {
99 case NAME_ERR_TOOLONG:
100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
101 "name is too long"));
104 case NAME_ERR_LEADING_SLASH:
105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106 "leading slash in name"));
109 case NAME_ERR_EMPTY_COMPONENT:
110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
111 "empty component in name"));
114 case NAME_ERR_TRAILING_SLASH:
115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116 "trailing slash in name"));
119 case NAME_ERR_INVALCHAR:
121 dgettext(TEXT_DOMAIN, "invalid character "
122 "'%c' in name"), what);
125 case NAME_ERR_MULTIPLE_AT:
126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
127 "multiple '@' delimiters in name"));
130 case NAME_ERR_NOLETTER:
131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132 "pool doesn't begin with a letter"));
135 case NAME_ERR_RESERVED:
136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
137 "name is reserved"));
140 case NAME_ERR_DISKLIKE:
141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
142 "reserved disk name"));
152 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "snapshot delimiter '@' in filesystem name"));
159 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
162 "missing '@' delimiter in snapshot name"));
166 if (modifying && strchr(path, '%') != NULL) {
168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
169 "invalid character %c in name"), '%');
177 zfs_name_valid(const char *name, zfs_type_t type)
179 if (type == ZFS_TYPE_POOL)
180 return (zpool_name_valid(NULL, B_FALSE, name));
181 return (zfs_validate_name(NULL, name, type, B_FALSE));
185 * This function takes the raw DSL properties, and filters out the user-defined
186 * properties into a separate nvlist.
189 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
191 libzfs_handle_t *hdl = zhp->zfs_hdl;
196 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
197 (void) no_memory(hdl);
202 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
203 if (!zfs_prop_user(nvpair_name(elem)))
206 verify(nvpair_value_nvlist(elem, &propval) == 0);
207 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
209 (void) no_memory(hdl);
217 static zpool_handle_t *
218 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
220 libzfs_handle_t *hdl = zhp->zfs_hdl;
223 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
224 if (hdl->libzfs_pool_handles != NULL)
225 zph->zpool_next = hdl->libzfs_pool_handles;
226 hdl->libzfs_pool_handles = zph;
231 static zpool_handle_t *
232 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
234 libzfs_handle_t *hdl = zhp->zfs_hdl;
235 zpool_handle_t *zph = hdl->libzfs_pool_handles;
237 while ((zph != NULL) &&
238 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
239 zph = zph->zpool_next;
244 * Returns a handle to the pool that contains the provided dataset.
245 * If a handle to that pool already exists then that handle is returned.
246 * Otherwise, a new handle is created and added to the list of handles.
248 static zpool_handle_t *
249 zpool_handle(zfs_handle_t *zhp)
255 len = strcspn(zhp->zfs_name, "/@") + 1;
256 pool_name = zfs_alloc(zhp->zfs_hdl, len);
257 (void) strlcpy(pool_name, zhp->zfs_name, len);
259 zph = zpool_find_handle(zhp, pool_name, len);
261 zph = zpool_add_handle(zhp, pool_name);
268 zpool_free_handles(libzfs_handle_t *hdl)
270 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
272 while (zph != NULL) {
273 next = zph->zpool_next;
277 hdl->libzfs_pool_handles = NULL;
281 * Utility function to gather stats (objset and zpl) for the given object.
284 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
286 libzfs_handle_t *hdl = zhp->zfs_hdl;
288 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
290 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
291 if (errno == ENOMEM) {
292 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
303 * Utility function to get the received properties of the given object.
306 get_recvd_props_ioctl(zfs_handle_t *zhp)
308 libzfs_handle_t *hdl = zhp->zfs_hdl;
309 nvlist_t *recvdprops;
310 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
313 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
316 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
318 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
319 if (errno == ENOMEM) {
320 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
324 zcmd_free_nvlists(&zc);
329 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
330 zcmd_free_nvlists(&zc);
334 nvlist_free(zhp->zfs_recvd_props);
335 zhp->zfs_recvd_props = recvdprops;
341 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
343 nvlist_t *allprops, *userprops;
345 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
347 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
352 * XXX Why do we store the user props separately, in addition to
353 * storing them in zfs_props?
355 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
356 nvlist_free(allprops);
360 nvlist_free(zhp->zfs_props);
361 nvlist_free(zhp->zfs_user_props);
363 zhp->zfs_props = allprops;
364 zhp->zfs_user_props = userprops;
370 get_stats(zfs_handle_t *zhp)
373 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
375 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
377 if (get_stats_ioctl(zhp, &zc) != 0)
379 else if (put_stats_zhdl(zhp, &zc) != 0)
381 zcmd_free_nvlists(&zc);
386 * Refresh the properties currently stored in the handle.
389 zfs_refresh_properties(zfs_handle_t *zhp)
391 (void) get_stats(zhp);
395 * Makes a handle from the given dataset name. Used by zfs_open() and
396 * zfs_iter_* to create child handles on the fly.
399 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
401 if (put_stats_zhdl(zhp, zc) != 0)
405 * We've managed to open the dataset and gather statistics. Determine
406 * the high-level type.
408 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
409 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
410 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
411 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
415 if (zhp->zfs_dmustats.dds_is_snapshot)
416 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
417 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
418 zhp->zfs_type = ZFS_TYPE_VOLUME;
419 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
420 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
422 abort(); /* we should never see any other types */
424 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
431 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
433 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
435 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
441 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
442 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
446 if (get_stats_ioctl(zhp, &zc) == -1) {
447 zcmd_free_nvlists(&zc);
451 if (make_dataset_handle_common(zhp, &zc) == -1) {
455 zcmd_free_nvlists(&zc);
459 static zfs_handle_t *
460 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
462 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
468 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
469 if (make_dataset_handle_common(zhp, zc) == -1) {
477 * Opens the given snapshot, filesystem, or volume. The 'types'
478 * argument is a mask of acceptable types. The function will print an
479 * appropriate error message and return NULL if it can't be opened.
482 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
487 (void) snprintf(errbuf, sizeof (errbuf),
488 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
491 * Validate the name before we even try to open it.
493 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
495 "invalid dataset name"));
496 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
501 * Try to get stats for the dataset, which will tell us if it exists.
504 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
505 (void) zfs_standard_error(hdl, errno, errbuf);
509 if (!(types & zhp->zfs_type)) {
510 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
519 * Release a ZFS handle. Nothing to do but free the associated memory.
522 zfs_close(zfs_handle_t *zhp)
524 if (zhp->zfs_mntopts)
525 free(zhp->zfs_mntopts);
526 nvlist_free(zhp->zfs_props);
527 nvlist_free(zhp->zfs_user_props);
528 nvlist_free(zhp->zfs_recvd_props);
532 typedef struct mnttab_node {
533 struct mnttab mtn_mt;
538 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
540 const mnttab_node_t *mtn1 = arg1;
541 const mnttab_node_t *mtn2 = arg2;
544 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
548 return (rv > 0 ? 1 : -1);
552 libzfs_mnttab_init(libzfs_handle_t *hdl)
554 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
555 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
556 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
560 libzfs_mnttab_update(libzfs_handle_t *hdl)
564 rewind(hdl->libzfs_mnttab);
565 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
568 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
570 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
571 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
572 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
573 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
574 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
575 avl_add(&hdl->libzfs_mnttab_cache, mtn);
580 libzfs_mnttab_fini(libzfs_handle_t *hdl)
585 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
586 free(mtn->mtn_mt.mnt_special);
587 free(mtn->mtn_mt.mnt_mountp);
588 free(mtn->mtn_mt.mnt_fstype);
589 free(mtn->mtn_mt.mnt_mntopts);
592 avl_destroy(&hdl->libzfs_mnttab_cache);
596 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
598 hdl->libzfs_mnttab_enable = enable;
602 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
603 struct mnttab *entry)
608 if (!hdl->libzfs_mnttab_enable) {
609 struct mnttab srch = { 0 };
611 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
612 libzfs_mnttab_fini(hdl);
613 rewind(hdl->libzfs_mnttab);
614 srch.mnt_special = (char *)fsname;
615 srch.mnt_fstype = MNTTYPE_ZFS;
616 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
622 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
623 libzfs_mnttab_update(hdl);
625 find.mtn_mt.mnt_special = (char *)fsname;
626 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
628 *entry = mtn->mtn_mt;
635 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
636 const char *mountp, const char *mntopts)
640 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
642 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
643 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
644 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
645 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
646 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
647 avl_add(&hdl->libzfs_mnttab_cache, mtn);
651 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
656 find.mtn_mt.mnt_special = (char *)fsname;
657 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
658 avl_remove(&hdl->libzfs_mnttab_cache, ret);
659 free(ret->mtn_mt.mnt_special);
660 free(ret->mtn_mt.mnt_mountp);
661 free(ret->mtn_mt.mnt_fstype);
662 free(ret->mtn_mt.mnt_mntopts);
668 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
670 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
672 if (zpool_handle == NULL)
675 *spa_version = zpool_get_prop_int(zpool_handle,
676 ZPOOL_PROP_VERSION, NULL);
681 * The choice of reservation property depends on the SPA version.
684 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
688 if (zfs_spa_version(zhp, &spa_version) < 0)
691 if (spa_version >= SPA_VERSION_REFRESERVATION)
692 *resv_prop = ZFS_PROP_REFRESERVATION;
694 *resv_prop = ZFS_PROP_RESERVATION;
700 * Given an nvlist of properties to set, validates that they are correct, and
701 * parses any numeric properties (index, boolean, etc) if they are specified as
705 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
706 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
713 int chosen_normal = -1;
716 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
717 (void) no_memory(hdl);
722 * Make sure this property is valid and applies to this type.
726 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
727 const char *propname = nvpair_name(elem);
729 prop = zfs_name_to_prop(propname);
730 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
732 * This is a user property: make sure it's a
733 * string, and that it's less than ZAP_MAXNAMELEN.
735 if (nvpair_type(elem) != DATA_TYPE_STRING) {
736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
737 "'%s' must be a string"), propname);
738 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
742 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
744 "property name '%s' is too long"),
746 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
750 (void) nvpair_value_string(elem, &strval);
751 if (nvlist_add_string(ret, propname, strval) != 0) {
752 (void) no_memory(hdl);
759 * Currently, only user properties can be modified on
762 if (type == ZFS_TYPE_SNAPSHOT) {
763 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
764 "this property can not be modified for snapshots"));
765 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
769 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
770 zfs_userquota_prop_t uqtype;
771 char newpropname[128];
776 if (userquota_propname_decode(propname, zoned,
777 &uqtype, domain, sizeof (domain), &rid) != 0) {
779 dgettext(TEXT_DOMAIN,
780 "'%s' has an invalid user/group name"),
782 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
786 if (uqtype != ZFS_PROP_USERQUOTA &&
787 uqtype != ZFS_PROP_GROUPQUOTA) {
789 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
791 (void) zfs_error(hdl, EZFS_PROPREADONLY,
796 if (nvpair_type(elem) == DATA_TYPE_STRING) {
797 (void) nvpair_value_string(elem, &strval);
798 if (strcmp(strval, "none") == 0) {
800 } else if (zfs_nicestrtonum(hdl,
801 strval, &intval) != 0) {
802 (void) zfs_error(hdl,
803 EZFS_BADPROP, errbuf);
806 } else if (nvpair_type(elem) ==
808 (void) nvpair_value_uint64(elem, &intval);
810 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
811 "use 'none' to disable "
812 "userquota/groupquota"));
816 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
817 "'%s' must be a number"), propname);
818 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
823 * Encode the prop name as
824 * userquota@<hex-rid>-domain, to make it easy
825 * for the kernel to decode.
827 (void) snprintf(newpropname, sizeof (newpropname),
828 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
829 (longlong_t)rid, domain);
833 if (nvlist_add_uint64_array(ret, newpropname,
835 (void) no_memory(hdl);
841 if (prop == ZPROP_INVAL) {
842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
843 "invalid property '%s'"), propname);
844 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
848 if (!zfs_prop_valid_for_type(prop, type)) {
850 dgettext(TEXT_DOMAIN, "'%s' does not "
851 "apply to datasets of this type"), propname);
852 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
856 if (zfs_prop_readonly(prop) &&
857 (!zfs_prop_setonce(prop) || zhp != NULL)) {
859 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
861 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
865 if (zprop_parse_value(hdl, elem, prop, type, ret,
866 &strval, &intval, errbuf) != 0)
870 * Perform some additional checks for specific properties.
873 case ZFS_PROP_VERSION:
879 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
880 if (intval < version) {
881 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
882 "Can not downgrade; already at version %u"),
884 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
890 case ZFS_PROP_RECORDSIZE:
891 case ZFS_PROP_VOLBLOCKSIZE:
892 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
893 if (intval < SPA_MINBLOCKSIZE ||
894 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
895 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
896 "'%s' must be power of 2 from %u "
898 (uint_t)SPA_MINBLOCKSIZE,
899 (uint_t)SPA_MAXBLOCKSIZE >> 10);
900 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
905 case ZFS_PROP_MLSLABEL:
909 * Verify the mlslabel string and convert to
910 * internal hex label string.
914 char *hex = NULL; /* internal label string */
916 /* Default value is already OK. */
917 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
920 /* Verify the label can be converted to binary form */
921 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
922 (str_to_label(strval, &new_sl, MAC_LABEL,
923 L_NO_CORRECTION, NULL) == -1)) {
927 /* Now translate to hex internal label string */
928 if (label_to_str(new_sl, &hex, M_INTERNAL,
934 m_label_free(new_sl);
936 /* If string is already in internal form, we're done. */
937 if (strcmp(strval, hex) == 0) {
942 /* Replace the label string with the internal form. */
943 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
945 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953 "invalid mlslabel '%s'"), strval);
954 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
955 m_label_free(new_sl); /* OK if null */
958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
959 "mlslabels are unsupported"));
960 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
962 #endif /* HAVE_MLSLABEL */
965 case ZFS_PROP_MOUNTPOINT:
969 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
970 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
973 if (mountpoint_namecheck(strval, &why)) {
975 case NAME_ERR_LEADING_SLASH:
977 dgettext(TEXT_DOMAIN,
978 "'%s' must be an absolute path, "
979 "'none', or 'legacy'"), propname);
981 case NAME_ERR_TOOLONG:
983 dgettext(TEXT_DOMAIN,
984 "component of '%s' is too long"),
990 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
997 case ZFS_PROP_SHARESMB:
998 case ZFS_PROP_SHARENFS:
1000 * For the mountpoint and sharenfs or sharesmb
1001 * properties, check if it can be set in a
1002 * global/non-global zone based on
1003 * the zoned property value:
1005 * global zone non-global zone
1006 * --------------------------------------------------
1007 * zoned=on mountpoint (no) mountpoint (yes)
1008 * sharenfs (no) sharenfs (no)
1009 * sharesmb (no) sharesmb (no)
1011 * zoned=off mountpoint (yes) N/A
1016 if (getzoneid() == GLOBAL_ZONEID) {
1017 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 "'%s' cannot be set on "
1019 "dataset in a non-global zone"),
1021 (void) zfs_error(hdl, EZFS_ZONED,
1024 } else if (prop == ZFS_PROP_SHARENFS ||
1025 prop == ZFS_PROP_SHARESMB) {
1026 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1027 "'%s' cannot be set in "
1028 "a non-global zone"), propname);
1029 (void) zfs_error(hdl, EZFS_ZONED,
1033 } else if (getzoneid() != GLOBAL_ZONEID) {
1035 * If zoned property is 'off', this must be in
1036 * a global zone. If not, something is wrong.
1038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1039 "'%s' cannot be set while dataset "
1040 "'zoned' property is set"), propname);
1041 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1046 * At this point, it is legitimate to set the
1047 * property. Now we want to make sure that the
1048 * property value is valid if it is sharenfs.
1050 if ((prop == ZFS_PROP_SHARENFS ||
1051 prop == ZFS_PROP_SHARESMB) &&
1052 strcmp(strval, "on") != 0 &&
1053 strcmp(strval, "off") != 0) {
1054 zfs_share_proto_t proto;
1056 if (prop == ZFS_PROP_SHARESMB)
1062 * Must be an valid sharing protocol
1063 * option string so init the libshare
1064 * in order to enable the parser and
1065 * then parse the options. We use the
1066 * control API since we don't care about
1067 * the current configuration and don't
1068 * want the overhead of loading it
1069 * until we actually do something.
1072 if (zfs_init_libshare(hdl,
1073 SA_INIT_CONTROL_API) != SA_OK) {
1075 * An error occurred so we can't do
1078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1079 "'%s' cannot be set: problem "
1080 "in share initialization"),
1082 (void) zfs_error(hdl, EZFS_BADPROP,
1087 if (zfs_parse_options(strval, proto) != SA_OK) {
1089 * There was an error in parsing so
1090 * deal with it by issuing an error
1091 * message and leaving after
1092 * uninitializing the the libshare
1095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1096 "'%s' cannot be set to invalid "
1097 "options"), propname);
1098 (void) zfs_error(hdl, EZFS_BADPROP,
1100 zfs_uninit_libshare(hdl);
1103 zfs_uninit_libshare(hdl);
1107 case ZFS_PROP_UTF8ONLY:
1108 chosen_utf = (int)intval;
1110 case ZFS_PROP_NORMALIZE:
1111 chosen_normal = (int)intval;
1118 * For changes to existing volumes, we have some additional
1119 * checks to enforce.
1121 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1122 uint64_t volsize = zfs_prop_get_int(zhp,
1124 uint64_t blocksize = zfs_prop_get_int(zhp,
1125 ZFS_PROP_VOLBLOCKSIZE);
1129 case ZFS_PROP_RESERVATION:
1130 case ZFS_PROP_REFRESERVATION:
1131 if (intval > volsize) {
1132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1133 "'%s' is greater than current "
1134 "volume size"), propname);
1135 (void) zfs_error(hdl, EZFS_BADPROP,
1141 case ZFS_PROP_VOLSIZE:
1142 if (intval % blocksize != 0) {
1143 zfs_nicenum(blocksize, buf,
1145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1146 "'%s' must be a multiple of "
1147 "volume block size (%s)"),
1149 (void) zfs_error(hdl, EZFS_BADPROP,
1155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1156 "'%s' cannot be zero"),
1158 (void) zfs_error(hdl, EZFS_BADPROP,
1170 * If normalization was chosen, but no UTF8 choice was made,
1171 * enforce rejection of non-UTF8 names.
1173 * If normalization was chosen, but rejecting non-UTF8 names
1174 * was explicitly not chosen, it is an error.
1176 if (chosen_normal > 0 && chosen_utf < 0) {
1177 if (nvlist_add_uint64(ret,
1178 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1179 (void) no_memory(hdl);
1182 } else if (chosen_normal > 0 && chosen_utf == 0) {
1183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1184 "'%s' must be set 'on' if normalization chosen"),
1185 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1186 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1197 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1199 uint64_t old_volsize;
1200 uint64_t new_volsize;
1201 uint64_t old_reservation;
1202 uint64_t new_reservation;
1203 zfs_prop_t resv_prop;
1206 * If this is an existing volume, and someone is setting the volsize,
1207 * make sure that it matches the reservation, or add it if necessary.
1209 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1210 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1212 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1213 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1214 old_reservation) || nvlist_lookup_uint64(nvl,
1215 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1218 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1221 new_reservation = zvol_volsize_to_reservation(new_volsize,
1223 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1224 new_reservation) != 0) {
1225 (void) no_memory(zhp->zfs_hdl);
1232 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1239 * For quotas and reservations, ENOSPC indicates
1240 * something different; setting a quota or reservation
1241 * doesn't use any disk space.
1244 case ZFS_PROP_QUOTA:
1245 case ZFS_PROP_REFQUOTA:
1246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1247 "size is less than current used or "
1249 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1252 case ZFS_PROP_RESERVATION:
1253 case ZFS_PROP_REFRESERVATION:
1254 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1255 "size is greater than available space"));
1256 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1260 (void) zfs_standard_error(hdl, err, errbuf);
1266 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1270 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1275 "pool and or dataset must be upgraded to set this "
1276 "property or value"));
1277 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1281 if (prop == ZFS_PROP_COMPRESSION) {
1282 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1283 "property setting is not allowed on "
1284 "bootable datasets"));
1285 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1287 (void) zfs_standard_error(hdl, err, errbuf);
1292 if (prop == ZPROP_INVAL) {
1293 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1295 (void) zfs_standard_error(hdl, err, errbuf);
1301 * This platform can't address a volume this big.
1304 if (prop == ZFS_PROP_VOLSIZE) {
1305 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1311 (void) zfs_standard_error(hdl, err, errbuf);
1316 * Given a property name and value, set the property for the given dataset.
1319 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1321 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1323 prop_changelist_t *cl = NULL;
1325 libzfs_handle_t *hdl = zhp->zfs_hdl;
1326 nvlist_t *nvl = NULL, *realprops;
1328 boolean_t do_prefix;
1332 (void) snprintf(errbuf, sizeof (errbuf),
1333 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1336 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1337 nvlist_add_string(nvl, propname, propval) != 0) {
1338 (void) no_memory(hdl);
1342 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1343 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1349 prop = zfs_name_to_prop(propname);
1351 if (prop == ZFS_PROP_VOLSIZE) {
1352 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1356 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1359 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1361 "child dataset with inherited mountpoint is used "
1362 "in a non-global zone"));
1363 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1368 * If the dataset's canmount property is being set to noauto,
1369 * then we want to prevent unmounting & remounting it.
1371 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1372 (zprop_string_to_index(prop, propval, &idx,
1373 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1375 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1379 * Execute the corresponding ioctl() to set this property.
1381 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1383 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1386 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1389 zfs_setprop_error(hdl, prop, errno, errbuf);
1390 if (added_resv && errno == ENOSPC) {
1391 /* clean up the volsize property we tried to set */
1392 uint64_t old_volsize = zfs_prop_get_int(zhp,
1395 zcmd_free_nvlists(&zc);
1396 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1398 if (nvlist_add_uint64(nvl,
1399 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1402 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1404 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1408 ret = changelist_postfix(cl);
1411 * Refresh the statistics so the new property value
1415 (void) get_stats(zhp);
1420 zcmd_free_nvlists(&zc);
1422 changelist_free(cl);
1427 * Given a property, inherit the value from the parent dataset, or if received
1428 * is TRUE, revert to the received value, if any.
1431 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1433 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1435 prop_changelist_t *cl;
1436 libzfs_handle_t *hdl = zhp->zfs_hdl;
1440 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1441 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1443 zc.zc_cookie = received;
1444 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1446 * For user properties, the amount of work we have to do is very
1447 * small, so just do it here.
1449 if (!zfs_prop_user(propname)) {
1450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1451 "invalid property"));
1452 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1455 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1456 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1458 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1459 return (zfs_standard_error(hdl, errno, errbuf));
1465 * Verify that this property is inheritable.
1467 if (zfs_prop_readonly(prop))
1468 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1470 if (!zfs_prop_inheritable(prop) && !received)
1471 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1474 * Check to see if the value applies to this type
1476 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1477 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1480 * Normalize the name, to get rid of shorthand abbreviations.
1482 propname = zfs_prop_to_name(prop);
1483 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1484 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1486 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1487 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1489 "dataset is used in a non-global zone"));
1490 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1494 * Determine datasets which will be affected by this change, if any.
1496 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1499 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1501 "child dataset with inherited mountpoint is used "
1502 "in a non-global zone"));
1503 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1507 if ((ret = changelist_prefix(cl)) != 0)
1510 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1511 return (zfs_standard_error(hdl, errno, errbuf));
1514 if ((ret = changelist_postfix(cl)) != 0)
1518 * Refresh the statistics so the new property is reflected.
1520 (void) get_stats(zhp);
1524 changelist_free(cl);
1529 * True DSL properties are stored in an nvlist. The following two functions
1530 * extract them appropriately.
1533 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1539 if (nvlist_lookup_nvlist(zhp->zfs_props,
1540 zfs_prop_to_name(prop), &nv) == 0) {
1541 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1542 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1544 verify(!zhp->zfs_props_table ||
1545 zhp->zfs_props_table[prop] == B_TRUE);
1546 value = zfs_prop_default_numeric(prop);
1554 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1560 if (nvlist_lookup_nvlist(zhp->zfs_props,
1561 zfs_prop_to_name(prop), &nv) == 0) {
1562 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1563 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1565 verify(!zhp->zfs_props_table ||
1566 zhp->zfs_props_table[prop] == B_TRUE);
1567 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1576 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1578 return (zhp->zfs_props == zhp->zfs_recvd_props);
1582 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1584 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1585 zhp->zfs_props = zhp->zfs_recvd_props;
1589 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1591 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1596 * Internal function for getting a numeric property. Both zfs_prop_get() and
1597 * zfs_prop_get_int() are built using this interface.
1599 * Certain properties can be overridden using 'mount -o'. In this case, scan
1600 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1601 * If they differ from the on-disk values, report the current values and mark
1602 * the source "temporary".
1605 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1606 char **source, uint64_t *val)
1608 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1609 nvlist_t *zplprops = NULL;
1611 char *mntopt_on = NULL;
1612 char *mntopt_off = NULL;
1613 boolean_t received = zfs_is_recvd_props_mode(zhp);
1618 case ZFS_PROP_ATIME:
1619 mntopt_on = MNTOPT_ATIME;
1620 mntopt_off = MNTOPT_NOATIME;
1623 case ZFS_PROP_DEVICES:
1624 mntopt_on = MNTOPT_DEVICES;
1625 mntopt_off = MNTOPT_NODEVICES;
1629 mntopt_on = MNTOPT_EXEC;
1630 mntopt_off = MNTOPT_NOEXEC;
1633 case ZFS_PROP_READONLY:
1634 mntopt_on = MNTOPT_RO;
1635 mntopt_off = MNTOPT_RW;
1638 case ZFS_PROP_SETUID:
1639 mntopt_on = MNTOPT_SETUID;
1640 mntopt_off = MNTOPT_NOSETUID;
1643 case ZFS_PROP_XATTR:
1644 mntopt_on = MNTOPT_XATTR;
1645 mntopt_off = MNTOPT_NOXATTR;
1648 case ZFS_PROP_NBMAND:
1649 mntopt_on = MNTOPT_NBMAND;
1650 mntopt_off = MNTOPT_NONBMAND;
1657 * Because looking up the mount options is potentially expensive
1658 * (iterating over all of /etc/mnttab), we defer its calculation until
1659 * we're looking up a property which requires its presence.
1661 if (!zhp->zfs_mntcheck &&
1662 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1663 libzfs_handle_t *hdl = zhp->zfs_hdl;
1664 struct mnttab entry;
1666 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1667 zhp->zfs_mntopts = zfs_strdup(hdl,
1669 if (zhp->zfs_mntopts == NULL)
1673 zhp->zfs_mntcheck = B_TRUE;
1676 if (zhp->zfs_mntopts == NULL)
1677 mnt.mnt_mntopts = "";
1679 mnt.mnt_mntopts = zhp->zfs_mntopts;
1682 case ZFS_PROP_ATIME:
1683 case ZFS_PROP_DEVICES:
1685 case ZFS_PROP_READONLY:
1686 case ZFS_PROP_SETUID:
1687 case ZFS_PROP_XATTR:
1688 case ZFS_PROP_NBMAND:
1689 *val = getprop_uint64(zhp, prop, source);
1694 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1697 *src = ZPROP_SRC_TEMPORARY;
1698 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1701 *src = ZPROP_SRC_TEMPORARY;
1705 case ZFS_PROP_CANMOUNT:
1706 case ZFS_PROP_VOLSIZE:
1707 case ZFS_PROP_QUOTA:
1708 case ZFS_PROP_REFQUOTA:
1709 case ZFS_PROP_RESERVATION:
1710 case ZFS_PROP_REFRESERVATION:
1711 *val = getprop_uint64(zhp, prop, source);
1713 if (*source == NULL) {
1714 /* not default, must be local */
1715 *source = zhp->zfs_name;
1719 case ZFS_PROP_MOUNTED:
1720 *val = (zhp->zfs_mntopts != NULL);
1723 case ZFS_PROP_NUMCLONES:
1724 *val = zhp->zfs_dmustats.dds_num_clones;
1727 case ZFS_PROP_VERSION:
1728 case ZFS_PROP_NORMALIZE:
1729 case ZFS_PROP_UTF8ONLY:
1731 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1732 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1734 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1735 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1736 zcmd_free_nvlists(&zc);
1739 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1740 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1742 zcmd_free_nvlists(&zc);
1746 nvlist_free(zplprops);
1747 zcmd_free_nvlists(&zc);
1751 switch (zfs_prop_get_type(prop)) {
1752 case PROP_TYPE_NUMBER:
1753 case PROP_TYPE_INDEX:
1754 *val = getprop_uint64(zhp, prop, source);
1756 * If we tried to use a default value for a
1757 * readonly property, it means that it was not
1760 if (zfs_prop_readonly(prop) &&
1761 *source != NULL && (*source)[0] == '\0') {
1766 case PROP_TYPE_STRING:
1768 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1769 "cannot get non-numeric property"));
1770 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1771 dgettext(TEXT_DOMAIN, "internal error")));
1779 * Calculate the source type, given the raw source string.
1782 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1783 char *statbuf, size_t statlen)
1785 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1788 if (source == NULL) {
1789 *srctype = ZPROP_SRC_NONE;
1790 } else if (source[0] == '\0') {
1791 *srctype = ZPROP_SRC_DEFAULT;
1792 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1793 *srctype = ZPROP_SRC_RECEIVED;
1795 if (strcmp(source, zhp->zfs_name) == 0) {
1796 *srctype = ZPROP_SRC_LOCAL;
1798 (void) strlcpy(statbuf, source, statlen);
1799 *srctype = ZPROP_SRC_INHERITED;
1806 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1807 size_t proplen, boolean_t literal)
1812 if (zhp->zfs_recvd_props == NULL)
1813 if (get_recvd_props_ioctl(zhp) != 0)
1816 prop = zfs_name_to_prop(propname);
1818 if (prop != ZPROP_INVAL) {
1820 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1822 zfs_set_recvd_props_mode(zhp, &cookie);
1823 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1824 NULL, NULL, 0, literal);
1825 zfs_unset_recvd_props_mode(zhp, &cookie);
1826 } else if (zfs_prop_userquota(propname)) {
1831 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1832 propname, &propval) != 0)
1834 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1836 (void) strlcpy(propbuf, recvdval, proplen);
1839 return (err == 0 ? 0 : -1);
1843 * Retrieve a property from the given object. If 'literal' is specified, then
1844 * numbers are left as exact values. Otherwise, numbers are converted to a
1845 * human-readable form.
1847 * Returns 0 on success, or -1 on error.
1850 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1851 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1853 char *source = NULL;
1857 boolean_t received = zfs_is_recvd_props_mode(zhp);
1860 * Check to see if this property applies to our object
1862 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1865 if (received && zfs_prop_readonly(prop))
1869 *src = ZPROP_SRC_NONE;
1872 case ZFS_PROP_CREATION:
1874 * 'creation' is a time_t stored in the statistics. We convert
1875 * this into a string unless 'literal' is specified.
1878 val = getprop_uint64(zhp, prop, &source);
1879 time_t time = (time_t)val;
1883 localtime_r(&time, &t) == NULL ||
1884 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1886 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
1890 case ZFS_PROP_MOUNTPOINT:
1892 * Getting the precise mountpoint can be tricky.
1894 * - for 'none' or 'legacy', return those values.
1895 * - for inherited mountpoints, we want to take everything
1896 * after our ancestor and append it to the inherited value.
1898 * If the pool has an alternate root, we want to prepend that
1899 * root to any values we return.
1902 str = getprop_string(zhp, prop, &source);
1904 if (str[0] == '/') {
1905 char buf[MAXPATHLEN];
1907 const char *relpath;
1910 * If we inherit the mountpoint, even from a dataset
1911 * with a received value, the source will be the path of
1912 * the dataset we inherit from. If source is
1913 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1916 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1919 relpath = zhp->zfs_name + strlen(source);
1920 if (relpath[0] == '/')
1924 if ((zpool_get_prop(zhp->zpool_hdl,
1925 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1926 (strcmp(root, "-") == 0))
1929 * Special case an alternate root of '/'. This will
1930 * avoid having multiple leading slashes in the
1933 if (strcmp(root, "/") == 0)
1937 * If the mountpoint is '/' then skip over this
1938 * if we are obtaining either an alternate root or
1939 * an inherited mountpoint.
1941 if (str[1] == '\0' && (root[0] != '\0' ||
1942 relpath[0] != '\0'))
1945 if (relpath[0] == '\0')
1946 (void) snprintf(propbuf, proplen, "%s%s",
1949 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1950 root, str, relpath[0] == '@' ? "" : "/",
1953 /* 'legacy' or 'none' */
1954 (void) strlcpy(propbuf, str, proplen);
1959 case ZFS_PROP_ORIGIN:
1960 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1963 * If there is no parent at all, return failure to indicate that
1964 * it doesn't apply to this dataset.
1966 if (propbuf[0] == '\0')
1970 case ZFS_PROP_QUOTA:
1971 case ZFS_PROP_REFQUOTA:
1972 case ZFS_PROP_RESERVATION:
1973 case ZFS_PROP_REFRESERVATION:
1975 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1979 * If quota or reservation is 0, we translate this into 'none'
1980 * (unless literal is set), and indicate that it's the default
1981 * value. Otherwise, we print the number nicely and indicate
1982 * that its set locally.
1986 (void) strlcpy(propbuf, "0", proplen);
1988 (void) strlcpy(propbuf, "none", proplen);
1991 (void) snprintf(propbuf, proplen, "%llu",
1994 zfs_nicenum(val, propbuf, proplen);
1998 case ZFS_PROP_COMPRESSRATIO:
1999 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2001 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2002 (u_longlong_t)(val / 100),
2003 (u_longlong_t)(val % 100));
2007 switch (zhp->zfs_type) {
2008 case ZFS_TYPE_FILESYSTEM:
2011 case ZFS_TYPE_VOLUME:
2014 case ZFS_TYPE_SNAPSHOT:
2020 (void) snprintf(propbuf, proplen, "%s", str);
2023 case ZFS_PROP_MOUNTED:
2025 * The 'mounted' property is a pseudo-property that described
2026 * whether the filesystem is currently mounted. Even though
2027 * it's a boolean value, the typical values of "on" and "off"
2028 * don't make sense, so we translate to "yes" and "no".
2030 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2031 src, &source, &val) != 0)
2034 (void) strlcpy(propbuf, "yes", proplen);
2036 (void) strlcpy(propbuf, "no", proplen);
2041 * The 'name' property is a pseudo-property derived from the
2042 * dataset name. It is presented as a real property to simplify
2045 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2048 case ZFS_PROP_MLSLABEL:
2050 #ifdef HAVE_MLSLABEL
2051 m_label_t *new_sl = NULL;
2052 char *ascii = NULL; /* human readable label */
2054 (void) strlcpy(propbuf,
2055 getprop_string(zhp, prop, &source), proplen);
2057 if (literal || (strcasecmp(propbuf,
2058 ZFS_MLSLABEL_DEFAULT) == 0))
2062 * Try to translate the internal hex string to
2063 * human-readable output. If there are any
2064 * problems just use the hex string.
2067 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2068 L_NO_CORRECTION, NULL) == -1) {
2069 m_label_free(new_sl);
2073 if (label_to_str(new_sl, &ascii, M_LABEL,
2077 m_label_free(new_sl);
2080 m_label_free(new_sl);
2082 (void) strlcpy(propbuf, ascii, proplen);
2085 (void) strlcpy(propbuf,
2086 getprop_string(zhp, prop, &source), proplen);
2087 #endif /* HAVE_MLSLABEL */
2092 switch (zfs_prop_get_type(prop)) {
2093 case PROP_TYPE_NUMBER:
2094 if (get_numeric_property(zhp, prop, src,
2095 &source, &val) != 0)
2098 (void) snprintf(propbuf, proplen, "%llu",
2101 zfs_nicenum(val, propbuf, proplen);
2104 case PROP_TYPE_STRING:
2105 (void) strlcpy(propbuf,
2106 getprop_string(zhp, prop, &source), proplen);
2109 case PROP_TYPE_INDEX:
2110 if (get_numeric_property(zhp, prop, src,
2111 &source, &val) != 0)
2113 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2115 (void) strlcpy(propbuf, strval, proplen);
2123 get_source(zhp, src, source, statbuf, statlen);
2129 * Utility function to get the given numeric property. Does no validation that
2130 * the given property is the appropriate type; should only be used with
2131 * hard-coded property types.
2134 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2139 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2145 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2149 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2150 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2154 * Similar to zfs_prop_get(), but returns the value as an integer.
2157 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2158 zprop_source_t *src, char *statbuf, size_t statlen)
2163 * Check to see if this property applies to our object
2165 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2166 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2167 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2168 zfs_prop_to_name(prop)));
2172 *src = ZPROP_SRC_NONE;
2174 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2177 get_source(zhp, src, source, statbuf, statlen);
2184 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2185 char **domainp, idmap_rid_t *ridp)
2187 idmap_get_handle_t *get_hdl = NULL;
2191 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2195 err = idmap_get_sidbyuid(get_hdl, id,
2196 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2198 err = idmap_get_sidbygid(get_hdl, id,
2199 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2201 if (err == IDMAP_SUCCESS &&
2202 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2203 status == IDMAP_SUCCESS)
2209 idmap_get_destroy(get_hdl);
2212 #endif /* HAVE_IDMAP */
2215 * convert the propname into parameters needed by kernel
2216 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2217 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2220 userquota_propname_decode(const char *propname, boolean_t zoned,
2221 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2223 zfs_userquota_prop_t type;
2225 char *numericsid = NULL;
2230 /* Figure out the property type ({user|group}{quota|space}) */
2231 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2232 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2233 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2236 if (type == ZFS_NUM_USERQUOTA_PROPS)
2240 isuser = (type == ZFS_PROP_USERQUOTA ||
2241 type == ZFS_PROP_USERUSED);
2243 cp = strchr(propname, '@') + 1;
2245 if (strchr(cp, '@')) {
2248 * It's a SID name (eg "user@domain") that needs to be
2249 * turned into S-1-domainID-RID.
2251 directory_error_t e;
2252 if (zoned && getzoneid() == GLOBAL_ZONEID)
2255 e = directory_sid_from_user_name(NULL,
2258 e = directory_sid_from_group_name(NULL,
2262 directory_error_free(e);
2265 if (numericsid == NULL)
2268 /* will be further decoded below */
2271 #endif /* HAVE_IDMAP */
2274 if (strncmp(cp, "S-1-", 4) == 0) {
2275 /* It's a numeric SID (eg "S-1-234-567-89") */
2276 (void) strlcpy(domain, cp, domainlen);
2277 cp = strrchr(domain, '-');
2282 *ridp = strtoull(cp, &end, 10);
2287 if (errno != 0 || *end != '\0')
2289 } else if (!isdigit(*cp)) {
2291 * It's a user/group name (eg "user") that needs to be
2292 * turned into a uid/gid
2294 if (zoned && getzoneid() == GLOBAL_ZONEID)
2311 /* It's a user/group ID (eg "12345"). */
2312 uid_t id = strtoul(cp, &end, 10);
2319 /* It's an ephemeral ID. */
2320 if (idmap_id_to_numeric_domain_rid(id, isuser,
2321 &mapdomain, &rid) != 0)
2323 (void) strlcpy(domain, mapdomain, domainlen);
2330 #endif /* HAVE_IDMAP */
2333 ASSERT3P(numericsid, ==, NULL);
2338 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2339 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2342 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2344 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2346 err = userquota_propname_decode(propname,
2347 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2348 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2349 zc.zc_objset_type = *typep;
2353 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2357 *propvalue = zc.zc_cookie;
2362 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2363 uint64_t *propvalue)
2365 zfs_userquota_prop_t type;
2367 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2372 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2373 char *propbuf, int proplen, boolean_t literal)
2377 zfs_userquota_prop_t type;
2379 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2386 (void) snprintf(propbuf, proplen, "%llu",
2387 (u_longlong_t)propvalue);
2388 } else if (propvalue == 0 &&
2389 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2390 (void) strlcpy(propbuf, "none", proplen);
2392 zfs_nicenum(propvalue, propbuf, proplen);
2398 * Returns the name of the given zfs handle.
2401 zfs_get_name(const zfs_handle_t *zhp)
2403 return (zhp->zfs_name);
2407 * Returns the type of the given zfs handle.
2410 zfs_get_type(const zfs_handle_t *zhp)
2412 return (zhp->zfs_type);
2416 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2419 uint64_t orig_cookie;
2421 orig_cookie = zc->zc_cookie;
2423 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2424 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2429 /* expand nvlist memory and try again */
2430 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2431 zcmd_free_nvlists(zc);
2434 zc->zc_cookie = orig_cookie;
2437 * An errno value of ESRCH indicates normal completion.
2438 * If ENOENT is returned, then the underlying dataset
2439 * has been removed since we obtained the handle.
2446 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2447 dgettext(TEXT_DOMAIN,
2448 "cannot iterate filesystems"));
2456 * Iterate over all child filesystems
2459 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2461 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2465 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2468 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2471 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2474 * Silently ignore errors, as the only plausible explanation is
2475 * that the pool has since been removed.
2477 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2482 if ((ret = func(nzhp, data)) != 0) {
2483 zcmd_free_nvlists(&zc);
2487 zcmd_free_nvlists(&zc);
2488 return ((ret < 0) ? ret : 0);
2492 * Iterate over all snapshots
2495 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2497 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2501 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2504 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2506 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2509 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2514 if ((ret = func(nzhp, data)) != 0) {
2515 zcmd_free_nvlists(&zc);
2519 zcmd_free_nvlists(&zc);
2520 return ((ret < 0) ? ret : 0);
2524 * Iterate over all children, snapshots and filesystems
2527 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2531 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2534 return (zfs_iter_snapshots(zhp, func, data));
2538 * Is one dataset name a child dataset of another?
2540 * Needs to handle these cases:
2541 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2542 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2543 * Descendant? No. No. No. Yes.
2546 is_descendant(const char *ds1, const char *ds2)
2548 size_t d1len = strlen(ds1);
2550 /* ds2 can't be a descendant if it's smaller */
2551 if (strlen(ds2) < d1len)
2554 /* otherwise, compare strings and verify that there's a '/' char */
2555 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2559 * Given a complete name, return just the portion that refers to the parent.
2560 * Can return NULL if this is a pool.
2563 parent_name(const char *path, char *buf, size_t buflen)
2567 if ((loc = strrchr(path, '/')) == NULL)
2570 (void) strncpy(buf, path, MIN(buflen, loc - path));
2571 buf[loc - path] = '\0';
2577 * If accept_ancestor is false, then check to make sure that the given path has
2578 * a parent, and that it exists. If accept_ancestor is true, then find the
2579 * closest existing ancestor for the given path. In prefixlen return the
2580 * length of already existing prefix of the given path. We also fetch the
2581 * 'zoned' property, which is used to validate property settings when creating
2585 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2586 boolean_t accept_ancestor, int *prefixlen)
2588 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2589 char parent[ZFS_MAXNAMELEN];
2595 (void) snprintf(errbuf, sizeof (errbuf),
2596 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2598 /* get parent, and check to see if this is just a pool */
2599 if (parent_name(path, parent, sizeof (parent)) != 0) {
2600 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2601 "missing dataset name"));
2602 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2605 /* check to see if the pool exists */
2606 if ((slash = strchr(parent, '/')) == NULL)
2607 slash = parent + strlen(parent);
2608 (void) strncpy(zc.zc_name, parent, slash - parent);
2609 zc.zc_name[slash - parent] = '\0';
2610 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2613 "no such pool '%s'"), zc.zc_name);
2614 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2617 /* check to see if the parent dataset exists */
2618 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2619 if (errno == ENOENT && accept_ancestor) {
2621 * Go deeper to find an ancestor, give up on top level.
2623 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2625 "no such pool '%s'"), zc.zc_name);
2626 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2628 } else if (errno == ENOENT) {
2629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2630 "parent does not exist"));
2631 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2633 return (zfs_standard_error(hdl, errno, errbuf));
2636 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2640 /* we are in a non-global zone, but parent is in the global zone */
2641 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2642 (void) zfs_standard_error(hdl, EPERM, errbuf);
2647 /* make sure parent is a filesystem */
2648 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2650 "parent is not a filesystem"));
2651 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2657 if (prefixlen != NULL)
2658 *prefixlen = strlen(parent);
2663 * Finds whether the dataset of the given type(s) exists.
2666 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2670 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2674 * Try to get stats for the dataset, which will tell us if it exists.
2676 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2677 int ds_type = zhp->zfs_type;
2680 if (types & ds_type)
2687 * Given a path to 'target', create all the ancestors between
2688 * the prefixlen portion of the path, and the target itself.
2689 * Fail if the initial prefixlen-ancestor does not already exist.
2692 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2698 /* make sure prefix exists */
2699 cp = target + prefixlen;
2701 assert(strchr(cp, '/') == NULL);
2702 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2705 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2713 * Attempt to create, mount, and share any ancestor filesystems,
2714 * up to the prefixlen-long one.
2716 for (cp = target + prefixlen + 1;
2717 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2722 h = make_dataset_handle(hdl, target);
2724 /* it already exists, nothing to do here */
2729 logstr = hdl->libzfs_log_str;
2730 hdl->libzfs_log_str = NULL;
2731 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2733 hdl->libzfs_log_str = logstr;
2734 opname = dgettext(TEXT_DOMAIN, "create");
2738 hdl->libzfs_log_str = logstr;
2739 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2741 opname = dgettext(TEXT_DOMAIN, "open");
2745 if (zfs_mount(h, NULL, 0) != 0) {
2746 opname = dgettext(TEXT_DOMAIN, "mount");
2750 if (zfs_share(h) != 0) {
2751 opname = dgettext(TEXT_DOMAIN, "share");
2761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2762 "failed to %s ancestor '%s'"), opname, target);
2767 * Creates non-existing ancestors of the given path.
2770 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2776 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2779 if ((path_copy = strdup(path)) != NULL) {
2780 rc = create_parents(hdl, path_copy, prefix);
2783 if (path_copy == NULL || rc != 0)
2790 * Create a new filesystem or volume.
2793 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2796 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2799 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2803 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2804 "cannot create '%s'"), path);
2806 /* validate the path, taking care to note the extended error message */
2807 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2808 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2810 /* validate parents exist */
2811 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2815 * The failure modes when creating a dataset of a different type over
2816 * one that already exists is a little strange. In particular, if you
2817 * try to create a dataset on top of an existing dataset, the ioctl()
2818 * will return ENOENT, not EEXIST. To prevent this from happening, we
2819 * first try to see if the dataset exists.
2821 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2822 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2824 "dataset already exists"));
2825 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2828 if (type == ZFS_TYPE_VOLUME)
2829 zc.zc_objset_type = DMU_OST_ZVOL;
2831 zc.zc_objset_type = DMU_OST_ZFS;
2833 if (props && (props = zfs_valid_proplist(hdl, type, props,
2834 zoned, NULL, errbuf)) == 0)
2837 if (type == ZFS_TYPE_VOLUME) {
2839 * If we are creating a volume, the size and block size must
2840 * satisfy a few restraints. First, the blocksize must be a
2841 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2842 * volsize must be a multiple of the block size, and cannot be
2845 if (props == NULL || nvlist_lookup_uint64(props,
2846 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2849 "missing volume size"));
2850 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2853 if ((ret = nvlist_lookup_uint64(props,
2854 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2855 &blocksize)) != 0) {
2856 if (ret == ENOENT) {
2857 blocksize = zfs_prop_default_numeric(
2858 ZFS_PROP_VOLBLOCKSIZE);
2861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2862 "missing volume block size"));
2863 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2869 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2870 "volume size cannot be zero"));
2871 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2874 if (size % blocksize != 0) {
2876 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2877 "volume size must be a multiple of volume block "
2879 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2883 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2887 /* create the dataset */
2888 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2890 zcmd_free_nvlists(&zc);
2892 /* check for failure */
2894 char parent[ZFS_MAXNAMELEN];
2895 (void) parent_name(path, parent, sizeof (parent));
2899 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2900 "no such parent '%s'"), parent);
2901 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2904 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2905 "parent '%s' is not a filesystem"), parent);
2906 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2910 "volume block size must be power of 2 from "
2912 (uint_t)SPA_MINBLOCKSIZE,
2913 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2915 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2918 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2919 "pool must be upgraded to set this "
2920 "property or value"));
2921 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2925 * This platform can't address a volume this big.
2927 if (type == ZFS_TYPE_VOLUME)
2928 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2933 return (zfs_standard_error(hdl, errno, errbuf));
2941 * Destroys the given dataset. The caller must make sure that the filesystem
2942 * isn't mounted, and that there are no active dependents.
2945 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2947 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2949 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2951 if (ZFS_IS_VOLUME(zhp)) {
2952 zc.zc_objset_type = DMU_OST_ZVOL;
2954 zc.zc_objset_type = DMU_OST_ZFS;
2957 zc.zc_defer_destroy = defer;
2958 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2959 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2960 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2964 remove_mountpoint(zhp);
2969 struct destroydata {
2976 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
2978 struct destroydata *dd = arg;
2980 char name[ZFS_MAXNAMELEN];
2981 boolean_t closezhp = dd->closezhp;
2984 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
2985 (void) strlcat(name, "@", sizeof (name));
2986 (void) strlcat(name, dd->snapname, sizeof (name));
2988 szhp = make_dataset_handle(zhp->zfs_hdl, name);
2990 dd->gotone = B_TRUE;
2994 dd->closezhp = B_TRUE;
2996 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
3003 * Destroys all snapshots with the given name in zhp & descendants.
3006 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3008 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3010 struct destroydata dd = { 0 };
3012 dd.snapname = snapname;
3013 (void) zfs_check_snap_cb(zhp, &dd);
3016 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3017 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3018 zhp->zfs_name, snapname));
3021 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3022 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3023 zc.zc_defer_destroy = defer;
3025 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3029 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3030 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3034 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3035 "snapshot is cloned"));
3036 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3039 return (zfs_standard_error(zhp->zfs_hdl, errno,
3048 * Clones the given dataset. The target must be of the same type as the source.
3051 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3053 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3054 char parent[ZFS_MAXNAMELEN];
3057 libzfs_handle_t *hdl = zhp->zfs_hdl;
3061 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3063 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3064 "cannot create '%s'"), target);
3066 /* validate the target name */
3067 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3068 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3070 /* validate parents exist */
3071 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3074 (void) parent_name(target, parent, sizeof (parent));
3077 if (ZFS_IS_VOLUME(zhp)) {
3078 zc.zc_objset_type = DMU_OST_ZVOL;
3079 type = ZFS_TYPE_VOLUME;
3081 zc.zc_objset_type = DMU_OST_ZFS;
3082 type = ZFS_TYPE_FILESYSTEM;
3086 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3087 zhp, errbuf)) == NULL)
3090 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3098 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3099 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3100 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3102 zcmd_free_nvlists(&zc);
3109 * The parent doesn't exist. We should have caught this
3110 * above, but there may a race condition that has since
3111 * destroyed the parent.
3113 * At this point, we don't know whether it's the source
3114 * that doesn't exist anymore, or whether the target
3115 * dataset doesn't exist.
3117 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3118 "no such parent '%s'"), parent);
3119 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3122 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3123 "source and target pools differ"));
3124 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3128 return (zfs_standard_error(zhp->zfs_hdl, errno,
3137 * Promotes the given clone fs to be the clone parent.
3140 zfs_promote(zfs_handle_t *zhp)
3142 libzfs_handle_t *hdl = zhp->zfs_hdl;
3143 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3144 char parent[MAXPATHLEN];
3148 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3149 "cannot promote '%s'"), zhp->zfs_name);
3151 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3153 "snapshots can not be promoted"));
3154 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3157 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3158 if (parent[0] == '\0') {
3159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3160 "not a cloned filesystem"));
3161 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3164 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3165 sizeof (zc.zc_value));
3166 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3167 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3170 int save_errno = errno;
3172 switch (save_errno) {
3174 /* There is a conflicting snapshot name. */
3175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3176 "conflicting snapshot '%s' from parent '%s'"),
3177 zc.zc_string, parent);
3178 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3181 return (zfs_standard_error(hdl, save_errno, errbuf));
3188 * Takes a snapshot of the given dataset.
3191 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3195 char parent[ZFS_MAXNAMELEN];
3197 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3201 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3202 "cannot snapshot '%s'"), path);
3204 /* validate the target name */
3205 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3206 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3209 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3210 props, B_FALSE, NULL, errbuf)) == NULL)
3213 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3221 /* make sure the parent exists and is of the appropriate type */
3222 delim = strchr(path, '@');
3223 (void) strncpy(parent, path, delim - path);
3224 parent[delim - path] = '\0';
3226 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3227 ZFS_TYPE_VOLUME)) == NULL) {
3228 zcmd_free_nvlists(&zc);
3232 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3233 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3234 if (ZFS_IS_VOLUME(zhp))
3235 zc.zc_objset_type = DMU_OST_ZVOL;
3237 zc.zc_objset_type = DMU_OST_ZFS;
3238 zc.zc_cookie = recursive;
3239 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3241 zcmd_free_nvlists(&zc);
3244 * if it was recursive, the one that actually failed will be in
3248 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3249 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3250 (void) zfs_standard_error(hdl, errno, errbuf);
3259 * Destroy any more recent snapshots. We invoke this callback on any dependents
3260 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3261 * is a dependent and we should just destroy it without checking the transaction
3264 typedef struct rollback_data {
3265 const char *cb_target; /* the snapshot */
3266 uint64_t cb_create; /* creation time reference */
3268 boolean_t cb_dependent;
3273 rollback_destroy(zfs_handle_t *zhp, void *data)
3275 rollback_data_t *cbp = data;
3277 if (!cbp->cb_dependent) {
3278 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3279 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3280 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3284 cbp->cb_dependent = B_TRUE;
3285 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3286 rollback_destroy, cbp);
3287 cbp->cb_dependent = B_FALSE;
3289 logstr = zhp->zfs_hdl->libzfs_log_str;
3290 zhp->zfs_hdl->libzfs_log_str = NULL;
3291 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3292 zhp->zfs_hdl->libzfs_log_str = logstr;
3295 /* We must destroy this clone; first unmount it */
3296 prop_changelist_t *clp;
3298 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3299 cbp->cb_force ? MS_FORCE: 0);
3300 if (clp == NULL || changelist_prefix(clp) != 0) {
3301 cbp->cb_error = B_TRUE;
3305 if (zfs_destroy(zhp, B_FALSE) != 0)
3306 cbp->cb_error = B_TRUE;
3308 changelist_remove(clp, zhp->zfs_name);
3309 (void) changelist_postfix(clp);
3310 changelist_free(clp);
3318 * Given a dataset, rollback to a specific snapshot, discarding any
3319 * data changes since then and making it the active dataset.
3321 * Any snapshots more recent than the target are destroyed, along with
3325 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3327 rollback_data_t cb = { 0 };
3329 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3330 boolean_t restore_resv = 0;
3331 uint64_t old_volsize = 0, new_volsize;
3332 zfs_prop_t resv_prop = { 0 };
3334 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3335 zhp->zfs_type == ZFS_TYPE_VOLUME);
3338 * Destroy all recent snapshots and its dependends.
3340 cb.cb_force = force;
3341 cb.cb_target = snap->zfs_name;
3342 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3343 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3349 * Now that we have verified that the snapshot is the latest,
3350 * rollback to the given snapshot.
3353 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3354 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3356 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3358 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3361 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3363 if (ZFS_IS_VOLUME(zhp))
3364 zc.zc_objset_type = DMU_OST_ZVOL;
3366 zc.zc_objset_type = DMU_OST_ZFS;
3369 * We rely on zfs_iter_children() to verify that there are no
3370 * newer snapshots for the given dataset. Therefore, we can
3371 * simply pass the name on to the ioctl() call. There is still
3372 * an unlikely race condition where the user has taken a
3373 * snapshot since we verified that this was the most recent.
3376 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3377 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3378 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3384 * For volumes, if the pre-rollback volsize matched the pre-
3385 * rollback reservation and the volsize has changed then set
3386 * the reservation property to the post-rollback volsize.
3387 * Make a new handle since the rollback closed the dataset.
3389 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3390 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3392 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3393 if (old_volsize != new_volsize)
3394 err = zfs_prop_set_int(zhp, resv_prop,
3403 * Iterate over all dependents for a given dataset. This includes both
3404 * hierarchical dependents (children) and data dependents (snapshots and
3405 * clones). The bulk of the processing occurs in get_dependents() in
3409 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3410 zfs_iter_f func, void *data)
3415 zfs_handle_t *child;
3418 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3419 &dependents, &count) != 0)
3422 for (i = 0; i < count; i++) {
3423 if ((child = make_dataset_handle(zhp->zfs_hdl,
3424 dependents[i])) == NULL)
3427 if ((ret = func(child, data)) != 0)
3431 for (i = 0; i < count; i++)
3432 free(dependents[i]);
3439 * Renames the given dataset.
3442 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3445 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3447 prop_changelist_t *cl = NULL;
3448 zfs_handle_t *zhrp = NULL;
3449 char *parentname = NULL;
3450 char parent[ZFS_MAXNAMELEN];
3451 libzfs_handle_t *hdl = zhp->zfs_hdl;
3454 /* if we have the same exact name, just return success */
3455 if (strcmp(zhp->zfs_name, target) == 0)
3458 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3459 "cannot rename to '%s'"), target);
3462 * Make sure the target name is valid
3464 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3465 if ((strchr(target, '@') == NULL) ||
3468 * Snapshot target name is abbreviated,
3469 * reconstruct full dataset name
3471 (void) strlcpy(parent, zhp->zfs_name,
3473 delim = strchr(parent, '@');
3474 if (strchr(target, '@') == NULL)
3478 (void) strlcat(parent, target, sizeof (parent));
3482 * Make sure we're renaming within the same dataset.
3484 delim = strchr(target, '@');
3485 if (strncmp(zhp->zfs_name, target, delim - target)
3486 != 0 || zhp->zfs_name[delim - target] != '@') {
3487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3488 "snapshots must be part of same "
3490 return (zfs_error(hdl, EZFS_CROSSTARGET,
3494 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3495 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3499 "recursive rename must be a snapshot"));
3500 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3503 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3504 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3506 /* validate parents */
3507 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3510 /* make sure we're in the same pool */
3511 verify((delim = strchr(target, '/')) != NULL);
3512 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3513 zhp->zfs_name[delim - target] != '/') {
3514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3515 "datasets must be within same pool"));
3516 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3519 /* new name cannot be a child of the current dataset name */
3520 if (is_descendant(zhp->zfs_name, target)) {
3521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3522 "New dataset name cannot be a descendant of "
3523 "current dataset name"));
3524 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3528 (void) snprintf(errbuf, sizeof (errbuf),
3529 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3531 if (getzoneid() == GLOBAL_ZONEID &&
3532 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3534 "dataset is used in a non-global zone"));
3535 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3540 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3541 if (parentname == NULL) {
3545 delim = strchr(parentname, '@');
3547 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3554 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3557 if (changelist_haszonedchild(cl)) {
3558 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3559 "child dataset with inherited mountpoint is used "
3560 "in a non-global zone"));
3561 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3566 if ((ret = changelist_prefix(cl)) != 0)
3570 if (ZFS_IS_VOLUME(zhp))
3571 zc.zc_objset_type = DMU_OST_ZVOL;
3573 zc.zc_objset_type = DMU_OST_ZFS;
3575 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3576 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3578 zc.zc_cookie = recursive;
3580 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3582 * if it was recursive, the one that actually failed will
3585 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3586 "cannot rename '%s'"), zc.zc_name);
3588 if (recursive && errno == EEXIST) {
3589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3590 "a child dataset already has a snapshot "
3591 "with the new name"));
3592 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3594 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3598 * On failure, we still want to remount any filesystems that
3599 * were previously mounted, so we don't alter the system state.
3602 (void) changelist_postfix(cl);
3605 changelist_rename(cl, zfs_get_name(zhp), target);
3606 ret = changelist_postfix(cl);
3618 changelist_free(cl);
3624 zfs_get_user_props(zfs_handle_t *zhp)
3626 return (zhp->zfs_user_props);
3630 zfs_get_recvd_props(zfs_handle_t *zhp)
3632 if (zhp->zfs_recvd_props == NULL)
3633 if (get_recvd_props_ioctl(zhp) != 0)
3635 return (zhp->zfs_recvd_props);
3639 * This function is used by 'zfs list' to determine the exact set of columns to
3640 * display, and their maximum widths. This does two main things:
3642 * - If this is a list of all properties, then expand the list to include
3643 * all native properties, and set a flag so that for each dataset we look
3644 * for new unique user properties and add them to the list.
3646 * - For non fixed-width properties, keep track of the maximum width seen
3647 * so that we can size the column appropriately. If the user has
3648 * requested received property values, we also need to compute the width
3649 * of the RECEIVED column.
3652 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3654 libzfs_handle_t *hdl = zhp->zfs_hdl;
3655 zprop_list_t *entry;
3656 zprop_list_t **last, **start;
3657 nvlist_t *userprops, *propval;
3660 char buf[ZFS_MAXPROPLEN];
3662 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3665 userprops = zfs_get_user_props(zhp);
3668 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3670 * Go through and add any user properties as necessary. We
3671 * start by incrementing our list pointer to the first
3672 * non-native property.
3675 while (*start != NULL) {
3676 if ((*start)->pl_prop == ZPROP_INVAL)
3678 start = &(*start)->pl_next;
3682 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3684 * See if we've already found this property in our list.
3686 for (last = start; *last != NULL;
3687 last = &(*last)->pl_next) {
3688 if (strcmp((*last)->pl_user_prop,
3689 nvpair_name(elem)) == 0)
3693 if (*last == NULL) {
3694 if ((entry = zfs_alloc(hdl,
3695 sizeof (zprop_list_t))) == NULL ||
3696 ((entry->pl_user_prop = zfs_strdup(hdl,
3697 nvpair_name(elem)))) == NULL) {
3702 entry->pl_prop = ZPROP_INVAL;
3703 entry->pl_width = strlen(nvpair_name(elem));
3704 entry->pl_all = B_TRUE;
3711 * Now go through and check the width of any non-fixed columns
3713 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3714 if (entry->pl_fixed)
3717 if (entry->pl_prop != ZPROP_INVAL) {
3718 if (zfs_prop_get(zhp, entry->pl_prop,
3719 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3720 if (strlen(buf) > entry->pl_width)
3721 entry->pl_width = strlen(buf);
3723 if (received && zfs_prop_get_recvd(zhp,
3724 zfs_prop_to_name(entry->pl_prop),
3725 buf, sizeof (buf), B_FALSE) == 0)
3726 if (strlen(buf) > entry->pl_recvd_width)
3727 entry->pl_recvd_width = strlen(buf);
3729 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3731 verify(nvlist_lookup_string(propval,
3732 ZPROP_VALUE, &strval) == 0);
3733 if (strlen(strval) > entry->pl_width)
3734 entry->pl_width = strlen(strval);
3736 if (received && zfs_prop_get_recvd(zhp,
3737 entry->pl_user_prop,
3738 buf, sizeof (buf), B_FALSE) == 0)
3739 if (strlen(buf) > entry->pl_recvd_width)
3740 entry->pl_recvd_width = strlen(buf);
3748 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3749 char *resource, void *export, void *sharetab,
3750 int sharemax, zfs_share_op_t operation)
3752 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3755 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3756 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3758 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3759 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3760 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3761 zc.zc_share.z_sharetype = operation;
3762 zc.zc_share.z_sharemax = sharemax;
3763 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3768 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3773 * Keep a reference to the props-table against which we prune the
3776 zhp->zfs_props_table = props;
3778 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3781 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3782 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3785 * User properties will result in ZPROP_INVAL, and since we
3786 * only know how to prune standard ZFS properties, we always
3787 * leave these in the list. This can also happen if we
3788 * encounter an unknown DSL property (when running older
3789 * software, for example).
3791 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3792 (void) nvlist_remove(zhp->zfs_props,
3793 nvpair_name(curr), nvpair_type(curr));
3799 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3800 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3802 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3803 nvlist_t *nvlist = NULL;
3806 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3807 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3808 zc.zc_cookie = (uint64_t)cmd;
3810 if (cmd == ZFS_SMB_ACL_RENAME) {
3811 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3812 (void) no_memory(hdl);
3818 case ZFS_SMB_ACL_ADD:
3819 case ZFS_SMB_ACL_REMOVE:
3820 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3822 case ZFS_SMB_ACL_RENAME:
3823 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3825 (void) no_memory(hdl);
3828 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3830 (void) no_memory(hdl);
3833 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3834 nvlist_free(nvlist);
3838 case ZFS_SMB_ACL_PURGE:
3843 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3845 nvlist_free(nvlist);
3850 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
3851 char *path, char *resource)
3853 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
3858 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
3859 char *path, char *resource)
3861 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
3866 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
3868 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
3873 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
3874 char *oldname, char *newname)
3876 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
3881 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
3882 zfs_userspace_cb_t func, void *arg)
3884 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3886 zfs_useracct_t buf[100];
3888 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3890 zc.zc_objset_type = type;
3891 zc.zc_nvlist_dst = (uintptr_t)buf;
3895 zfs_useracct_t *zua = buf;
3897 zc.zc_nvlist_dst_size = sizeof (buf);
3898 error = ioctl(zhp->zfs_hdl->libzfs_fd,
3899 ZFS_IOC_USERSPACE_MANY, &zc);
3900 if (error || zc.zc_nvlist_dst_size == 0)
3903 while (zc.zc_nvlist_dst_size > 0) {
3904 error = func(arg, zua->zu_domain, zua->zu_rid,
3909 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
3917 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
3918 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
3919 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
3921 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3922 libzfs_handle_t *hdl = zhp->zfs_hdl;
3924 ASSERT(!recursive || dsobj == 0);
3926 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3927 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3928 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3929 >= sizeof (zc.zc_string))
3930 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3931 zc.zc_cookie = recursive;
3932 zc.zc_temphold = temphold;
3933 zc.zc_cleanup_fd = cleanup_fd;
3934 zc.zc_sendobj = dsobj;
3935 zc.zc_createtxg = createtxg;
3937 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
3938 char errbuf[ZFS_MAXNAMELEN+32];
3941 * if it was recursive, the one that actually failed will be in
3944 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3945 "cannot hold '%s@%s'"), zc.zc_name, snapname);
3949 * Temporary tags wind up having the ds object id
3950 * prepended. So even if we passed the length check
3951 * above, it's still possible for the tag to wind
3952 * up being slightly too long.
3954 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
3956 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3957 "pool must be upgraded"));
3958 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3960 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3962 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
3968 return (zfs_standard_error_fmt(hdl, errno, errbuf));
3976 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
3977 boolean_t recursive)
3979 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3980 libzfs_handle_t *hdl = zhp->zfs_hdl;
3982 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3983 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3984 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3985 >= sizeof (zc.zc_string))
3986 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3987 zc.zc_cookie = recursive;
3989 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
3990 char errbuf[ZFS_MAXNAMELEN+32];
3993 * if it was recursive, the one that actually failed will be in
3996 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3997 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4001 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4003 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4004 "pool must be upgraded"));
4005 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4007 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4009 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4017 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4020 uint64_t nblocks, volblocksize;
4024 if (nvlist_lookup_string(props,
4025 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4026 ncopies = atoi(strval);
4029 if (nvlist_lookup_uint64(props,
4030 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4031 &volblocksize) != 0)
4032 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4033 nblocks = volsize/volblocksize;
4034 /* start with metadnode L0-L6 */
4036 /* calculate number of indirects */
4037 while (nblocks > 1) {
4038 nblocks += DNODES_PER_LEVEL - 1;
4039 nblocks /= DNODES_PER_LEVEL;
4042 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4045 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4046 * compressed, but in practice they compress down to about
4049 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;