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:
908 * Verify the mlslabel string and convert to
909 * internal hex label string.
913 char *hex = NULL; /* internal label string */
915 /* Default value is already OK. */
916 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
919 /* Verify the label can be converted to binary form */
920 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
921 (str_to_label(strval, &new_sl, MAC_LABEL,
922 L_NO_CORRECTION, NULL) == -1)) {
926 /* Now translate to hex internal label string */
927 if (label_to_str(new_sl, &hex, M_INTERNAL,
933 m_label_free(new_sl);
935 /* If string is already in internal form, we're done. */
936 if (strcmp(strval, hex) == 0) {
941 /* Replace the label string with the internal form. */
942 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
944 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
951 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
952 "invalid mlslabel '%s'"), strval);
953 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
954 m_label_free(new_sl); /* OK if null */
959 case ZFS_PROP_MOUNTPOINT:
963 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
964 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
967 if (mountpoint_namecheck(strval, &why)) {
969 case NAME_ERR_LEADING_SLASH:
971 dgettext(TEXT_DOMAIN,
972 "'%s' must be an absolute path, "
973 "'none', or 'legacy'"), propname);
975 case NAME_ERR_TOOLONG:
977 dgettext(TEXT_DOMAIN,
978 "component of '%s' is too long"),
984 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
991 case ZFS_PROP_SHARESMB:
992 case ZFS_PROP_SHARENFS:
994 * For the mountpoint and sharenfs or sharesmb
995 * properties, check if it can be set in a
996 * global/non-global zone based on
997 * the zoned property value:
999 * global zone non-global zone
1000 * --------------------------------------------------
1001 * zoned=on mountpoint (no) mountpoint (yes)
1002 * sharenfs (no) sharenfs (no)
1003 * sharesmb (no) sharesmb (no)
1005 * zoned=off mountpoint (yes) N/A
1010 if (getzoneid() == GLOBAL_ZONEID) {
1011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1012 "'%s' cannot be set on "
1013 "dataset in a non-global zone"),
1015 (void) zfs_error(hdl, EZFS_ZONED,
1018 } else if (prop == ZFS_PROP_SHARENFS ||
1019 prop == ZFS_PROP_SHARESMB) {
1020 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1021 "'%s' cannot be set in "
1022 "a non-global zone"), propname);
1023 (void) zfs_error(hdl, EZFS_ZONED,
1027 } else if (getzoneid() != GLOBAL_ZONEID) {
1029 * If zoned property is 'off', this must be in
1030 * a global zone. If not, something is wrong.
1032 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1033 "'%s' cannot be set while dataset "
1034 "'zoned' property is set"), propname);
1035 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1040 * At this point, it is legitimate to set the
1041 * property. Now we want to make sure that the
1042 * property value is valid if it is sharenfs.
1044 if ((prop == ZFS_PROP_SHARENFS ||
1045 prop == ZFS_PROP_SHARESMB) &&
1046 strcmp(strval, "on") != 0 &&
1047 strcmp(strval, "off") != 0) {
1048 zfs_share_proto_t proto;
1050 if (prop == ZFS_PROP_SHARESMB)
1056 * Must be an valid sharing protocol
1057 * option string so init the libshare
1058 * in order to enable the parser and
1059 * then parse the options. We use the
1060 * control API since we don't care about
1061 * the current configuration and don't
1062 * want the overhead of loading it
1063 * until we actually do something.
1066 if (zfs_init_libshare(hdl,
1067 SA_INIT_CONTROL_API) != SA_OK) {
1069 * An error occurred so we can't do
1072 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1073 "'%s' cannot be set: problem "
1074 "in share initialization"),
1076 (void) zfs_error(hdl, EZFS_BADPROP,
1081 if (zfs_parse_options(strval, proto) != SA_OK) {
1083 * There was an error in parsing so
1084 * deal with it by issuing an error
1085 * message and leaving after
1086 * uninitializing the the libshare
1089 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1090 "'%s' cannot be set to invalid "
1091 "options"), propname);
1092 (void) zfs_error(hdl, EZFS_BADPROP,
1094 zfs_uninit_libshare(hdl);
1097 zfs_uninit_libshare(hdl);
1101 case ZFS_PROP_UTF8ONLY:
1102 chosen_utf = (int)intval;
1104 case ZFS_PROP_NORMALIZE:
1105 chosen_normal = (int)intval;
1112 * For changes to existing volumes, we have some additional
1113 * checks to enforce.
1115 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1116 uint64_t volsize = zfs_prop_get_int(zhp,
1118 uint64_t blocksize = zfs_prop_get_int(zhp,
1119 ZFS_PROP_VOLBLOCKSIZE);
1123 case ZFS_PROP_RESERVATION:
1124 case ZFS_PROP_REFRESERVATION:
1125 if (intval > volsize) {
1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127 "'%s' is greater than current "
1128 "volume size"), propname);
1129 (void) zfs_error(hdl, EZFS_BADPROP,
1135 case ZFS_PROP_VOLSIZE:
1136 if (intval % blocksize != 0) {
1137 zfs_nicenum(blocksize, buf,
1139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1140 "'%s' must be a multiple of "
1141 "volume block size (%s)"),
1143 (void) zfs_error(hdl, EZFS_BADPROP,
1149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1150 "'%s' cannot be zero"),
1152 (void) zfs_error(hdl, EZFS_BADPROP,
1164 * If normalization was chosen, but no UTF8 choice was made,
1165 * enforce rejection of non-UTF8 names.
1167 * If normalization was chosen, but rejecting non-UTF8 names
1168 * was explicitly not chosen, it is an error.
1170 if (chosen_normal > 0 && chosen_utf < 0) {
1171 if (nvlist_add_uint64(ret,
1172 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1173 (void) no_memory(hdl);
1176 } else if (chosen_normal > 0 && chosen_utf == 0) {
1177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1178 "'%s' must be set 'on' if normalization chosen"),
1179 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1180 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1191 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1193 uint64_t old_volsize;
1194 uint64_t new_volsize;
1195 uint64_t old_reservation;
1196 uint64_t new_reservation;
1197 zfs_prop_t resv_prop;
1200 * If this is an existing volume, and someone is setting the volsize,
1201 * make sure that it matches the reservation, or add it if necessary.
1203 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1204 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1206 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1207 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1208 old_reservation) || nvlist_lookup_uint64(nvl,
1209 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1212 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1215 new_reservation = zvol_volsize_to_reservation(new_volsize,
1217 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1218 new_reservation) != 0) {
1219 (void) no_memory(zhp->zfs_hdl);
1226 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1233 * For quotas and reservations, ENOSPC indicates
1234 * something different; setting a quota or reservation
1235 * doesn't use any disk space.
1238 case ZFS_PROP_QUOTA:
1239 case ZFS_PROP_REFQUOTA:
1240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1241 "size is less than current used or "
1243 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1246 case ZFS_PROP_RESERVATION:
1247 case ZFS_PROP_REFRESERVATION:
1248 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1249 "size is greater than available space"));
1250 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1254 (void) zfs_standard_error(hdl, err, errbuf);
1260 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1264 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1269 "pool and or dataset must be upgraded to set this "
1270 "property or value"));
1271 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1275 if (prop == ZFS_PROP_COMPRESSION) {
1276 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1277 "property setting is not allowed on "
1278 "bootable datasets"));
1279 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1281 (void) zfs_standard_error(hdl, err, errbuf);
1286 if (prop == ZPROP_INVAL) {
1287 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1289 (void) zfs_standard_error(hdl, err, errbuf);
1295 * This platform can't address a volume this big.
1298 if (prop == ZFS_PROP_VOLSIZE) {
1299 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1305 (void) zfs_standard_error(hdl, err, errbuf);
1310 * Given a property name and value, set the property for the given dataset.
1313 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1315 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1317 prop_changelist_t *cl = NULL;
1319 libzfs_handle_t *hdl = zhp->zfs_hdl;
1320 nvlist_t *nvl = NULL, *realprops;
1322 boolean_t do_prefix;
1326 (void) snprintf(errbuf, sizeof (errbuf),
1327 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1330 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1331 nvlist_add_string(nvl, propname, propval) != 0) {
1332 (void) no_memory(hdl);
1336 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1337 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1343 prop = zfs_name_to_prop(propname);
1345 if (prop == ZFS_PROP_VOLSIZE) {
1346 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1350 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1353 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1354 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1355 "child dataset with inherited mountpoint is used "
1356 "in a non-global zone"));
1357 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1362 * If the dataset's canmount property is being set to noauto,
1363 * then we want to prevent unmounting & remounting it.
1365 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1366 (zprop_string_to_index(prop, propval, &idx,
1367 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1369 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1373 * Execute the corresponding ioctl() to set this property.
1375 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1377 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1380 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1383 zfs_setprop_error(hdl, prop, errno, errbuf);
1384 if (added_resv && errno == ENOSPC) {
1385 /* clean up the volsize property we tried to set */
1386 uint64_t old_volsize = zfs_prop_get_int(zhp,
1389 zcmd_free_nvlists(&zc);
1390 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1392 if (nvlist_add_uint64(nvl,
1393 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1396 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1398 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1402 ret = changelist_postfix(cl);
1405 * Refresh the statistics so the new property value
1409 (void) get_stats(zhp);
1414 zcmd_free_nvlists(&zc);
1416 changelist_free(cl);
1421 * Given a property, inherit the value from the parent dataset, or if received
1422 * is TRUE, revert to the received value, if any.
1425 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1427 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1429 prop_changelist_t *cl;
1430 libzfs_handle_t *hdl = zhp->zfs_hdl;
1434 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1435 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1437 zc.zc_cookie = received;
1438 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1440 * For user properties, the amount of work we have to do is very
1441 * small, so just do it here.
1443 if (!zfs_prop_user(propname)) {
1444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1445 "invalid property"));
1446 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1449 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1450 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1452 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1453 return (zfs_standard_error(hdl, errno, errbuf));
1459 * Verify that this property is inheritable.
1461 if (zfs_prop_readonly(prop))
1462 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1464 if (!zfs_prop_inheritable(prop) && !received)
1465 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1468 * Check to see if the value applies to this type
1470 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1471 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1474 * Normalize the name, to get rid of shorthand abbreviations.
1476 propname = zfs_prop_to_name(prop);
1477 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1478 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1480 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1481 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1483 "dataset is used in a non-global zone"));
1484 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1488 * Determine datasets which will be affected by this change, if any.
1490 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1493 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1495 "child dataset with inherited mountpoint is used "
1496 "in a non-global zone"));
1497 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1501 if ((ret = changelist_prefix(cl)) != 0)
1504 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1505 return (zfs_standard_error(hdl, errno, errbuf));
1508 if ((ret = changelist_postfix(cl)) != 0)
1512 * Refresh the statistics so the new property is reflected.
1514 (void) get_stats(zhp);
1518 changelist_free(cl);
1523 * True DSL properties are stored in an nvlist. The following two functions
1524 * extract them appropriately.
1527 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1533 if (nvlist_lookup_nvlist(zhp->zfs_props,
1534 zfs_prop_to_name(prop), &nv) == 0) {
1535 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1536 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1538 verify(!zhp->zfs_props_table ||
1539 zhp->zfs_props_table[prop] == B_TRUE);
1540 value = zfs_prop_default_numeric(prop);
1548 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1554 if (nvlist_lookup_nvlist(zhp->zfs_props,
1555 zfs_prop_to_name(prop), &nv) == 0) {
1556 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1557 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1559 verify(!zhp->zfs_props_table ||
1560 zhp->zfs_props_table[prop] == B_TRUE);
1561 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1570 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1572 return (zhp->zfs_props == zhp->zfs_recvd_props);
1576 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1578 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1579 zhp->zfs_props = zhp->zfs_recvd_props;
1583 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1585 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1590 * Internal function for getting a numeric property. Both zfs_prop_get() and
1591 * zfs_prop_get_int() are built using this interface.
1593 * Certain properties can be overridden using 'mount -o'. In this case, scan
1594 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1595 * If they differ from the on-disk values, report the current values and mark
1596 * the source "temporary".
1599 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1600 char **source, uint64_t *val)
1602 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1603 nvlist_t *zplprops = NULL;
1605 char *mntopt_on = NULL;
1606 char *mntopt_off = NULL;
1607 boolean_t received = zfs_is_recvd_props_mode(zhp);
1612 case ZFS_PROP_ATIME:
1613 mntopt_on = MNTOPT_ATIME;
1614 mntopt_off = MNTOPT_NOATIME;
1617 case ZFS_PROP_DEVICES:
1618 mntopt_on = MNTOPT_DEVICES;
1619 mntopt_off = MNTOPT_NODEVICES;
1623 mntopt_on = MNTOPT_EXEC;
1624 mntopt_off = MNTOPT_NOEXEC;
1627 case ZFS_PROP_READONLY:
1628 mntopt_on = MNTOPT_RO;
1629 mntopt_off = MNTOPT_RW;
1632 case ZFS_PROP_SETUID:
1633 mntopt_on = MNTOPT_SETUID;
1634 mntopt_off = MNTOPT_NOSETUID;
1637 case ZFS_PROP_XATTR:
1638 mntopt_on = MNTOPT_XATTR;
1639 mntopt_off = MNTOPT_NOXATTR;
1642 case ZFS_PROP_NBMAND:
1643 mntopt_on = MNTOPT_NBMAND;
1644 mntopt_off = MNTOPT_NONBMAND;
1651 * Because looking up the mount options is potentially expensive
1652 * (iterating over all of /etc/mnttab), we defer its calculation until
1653 * we're looking up a property which requires its presence.
1655 if (!zhp->zfs_mntcheck &&
1656 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1657 libzfs_handle_t *hdl = zhp->zfs_hdl;
1658 struct mnttab entry;
1660 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1661 zhp->zfs_mntopts = zfs_strdup(hdl,
1663 if (zhp->zfs_mntopts == NULL)
1667 zhp->zfs_mntcheck = B_TRUE;
1670 if (zhp->zfs_mntopts == NULL)
1671 mnt.mnt_mntopts = "";
1673 mnt.mnt_mntopts = zhp->zfs_mntopts;
1676 case ZFS_PROP_ATIME:
1677 case ZFS_PROP_DEVICES:
1679 case ZFS_PROP_READONLY:
1680 case ZFS_PROP_SETUID:
1681 case ZFS_PROP_XATTR:
1682 case ZFS_PROP_NBMAND:
1683 *val = getprop_uint64(zhp, prop, source);
1688 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1691 *src = ZPROP_SRC_TEMPORARY;
1692 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1695 *src = ZPROP_SRC_TEMPORARY;
1699 case ZFS_PROP_CANMOUNT:
1700 case ZFS_PROP_VOLSIZE:
1701 case ZFS_PROP_QUOTA:
1702 case ZFS_PROP_REFQUOTA:
1703 case ZFS_PROP_RESERVATION:
1704 case ZFS_PROP_REFRESERVATION:
1705 *val = getprop_uint64(zhp, prop, source);
1707 if (*source == NULL) {
1708 /* not default, must be local */
1709 *source = zhp->zfs_name;
1713 case ZFS_PROP_MOUNTED:
1714 *val = (zhp->zfs_mntopts != NULL);
1717 case ZFS_PROP_NUMCLONES:
1718 *val = zhp->zfs_dmustats.dds_num_clones;
1721 case ZFS_PROP_VERSION:
1722 case ZFS_PROP_NORMALIZE:
1723 case ZFS_PROP_UTF8ONLY:
1725 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1726 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1728 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1729 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1730 zcmd_free_nvlists(&zc);
1733 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1734 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1736 zcmd_free_nvlists(&zc);
1740 nvlist_free(zplprops);
1741 zcmd_free_nvlists(&zc);
1745 switch (zfs_prop_get_type(prop)) {
1746 case PROP_TYPE_NUMBER:
1747 case PROP_TYPE_INDEX:
1748 *val = getprop_uint64(zhp, prop, source);
1750 * If we tried to use a default value for a
1751 * readonly property, it means that it was not
1754 if (zfs_prop_readonly(prop) &&
1755 *source != NULL && (*source)[0] == '\0') {
1760 case PROP_TYPE_STRING:
1762 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1763 "cannot get non-numeric property"));
1764 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1765 dgettext(TEXT_DOMAIN, "internal error")));
1773 * Calculate the source type, given the raw source string.
1776 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1777 char *statbuf, size_t statlen)
1779 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1782 if (source == NULL) {
1783 *srctype = ZPROP_SRC_NONE;
1784 } else if (source[0] == '\0') {
1785 *srctype = ZPROP_SRC_DEFAULT;
1786 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1787 *srctype = ZPROP_SRC_RECEIVED;
1789 if (strcmp(source, zhp->zfs_name) == 0) {
1790 *srctype = ZPROP_SRC_LOCAL;
1792 (void) strlcpy(statbuf, source, statlen);
1793 *srctype = ZPROP_SRC_INHERITED;
1800 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1801 size_t proplen, boolean_t literal)
1806 if (zhp->zfs_recvd_props == NULL)
1807 if (get_recvd_props_ioctl(zhp) != 0)
1810 prop = zfs_name_to_prop(propname);
1812 if (prop != ZPROP_INVAL) {
1814 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1816 zfs_set_recvd_props_mode(zhp, &cookie);
1817 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1818 NULL, NULL, 0, literal);
1819 zfs_unset_recvd_props_mode(zhp, &cookie);
1820 } else if (zfs_prop_userquota(propname)) {
1825 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1826 propname, &propval) != 0)
1828 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1830 (void) strlcpy(propbuf, recvdval, proplen);
1833 return (err == 0 ? 0 : -1);
1837 * Retrieve a property from the given object. If 'literal' is specified, then
1838 * numbers are left as exact values. Otherwise, numbers are converted to a
1839 * human-readable form.
1841 * Returns 0 on success, or -1 on error.
1844 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1845 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1847 char *source = NULL;
1851 boolean_t received = zfs_is_recvd_props_mode(zhp);
1854 * Check to see if this property applies to our object
1856 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1859 if (received && zfs_prop_readonly(prop))
1863 *src = ZPROP_SRC_NONE;
1866 case ZFS_PROP_CREATION:
1868 * 'creation' is a time_t stored in the statistics. We convert
1869 * this into a string unless 'literal' is specified.
1872 val = getprop_uint64(zhp, prop, &source);
1873 time_t time = (time_t)val;
1877 localtime_r(&time, &t) == NULL ||
1878 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1880 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
1884 case ZFS_PROP_MOUNTPOINT:
1886 * Getting the precise mountpoint can be tricky.
1888 * - for 'none' or 'legacy', return those values.
1889 * - for inherited mountpoints, we want to take everything
1890 * after our ancestor and append it to the inherited value.
1892 * If the pool has an alternate root, we want to prepend that
1893 * root to any values we return.
1896 str = getprop_string(zhp, prop, &source);
1898 if (str[0] == '/') {
1899 char buf[MAXPATHLEN];
1901 const char *relpath;
1904 * If we inherit the mountpoint, even from a dataset
1905 * with a received value, the source will be the path of
1906 * the dataset we inherit from. If source is
1907 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1910 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1913 relpath = zhp->zfs_name + strlen(source);
1914 if (relpath[0] == '/')
1918 if ((zpool_get_prop(zhp->zpool_hdl,
1919 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1920 (strcmp(root, "-") == 0))
1923 * Special case an alternate root of '/'. This will
1924 * avoid having multiple leading slashes in the
1927 if (strcmp(root, "/") == 0)
1931 * If the mountpoint is '/' then skip over this
1932 * if we are obtaining either an alternate root or
1933 * an inherited mountpoint.
1935 if (str[1] == '\0' && (root[0] != '\0' ||
1936 relpath[0] != '\0'))
1939 if (relpath[0] == '\0')
1940 (void) snprintf(propbuf, proplen, "%s%s",
1943 (void) snprintf(propbuf, proplen, "%s%s%s%s",
1944 root, str, relpath[0] == '@' ? "" : "/",
1947 /* 'legacy' or 'none' */
1948 (void) strlcpy(propbuf, str, proplen);
1953 case ZFS_PROP_ORIGIN:
1954 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
1957 * If there is no parent at all, return failure to indicate that
1958 * it doesn't apply to this dataset.
1960 if (propbuf[0] == '\0')
1964 case ZFS_PROP_QUOTA:
1965 case ZFS_PROP_REFQUOTA:
1966 case ZFS_PROP_RESERVATION:
1967 case ZFS_PROP_REFRESERVATION:
1969 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1973 * If quota or reservation is 0, we translate this into 'none'
1974 * (unless literal is set), and indicate that it's the default
1975 * value. Otherwise, we print the number nicely and indicate
1976 * that its set locally.
1980 (void) strlcpy(propbuf, "0", proplen);
1982 (void) strlcpy(propbuf, "none", proplen);
1985 (void) snprintf(propbuf, proplen, "%llu",
1988 zfs_nicenum(val, propbuf, proplen);
1992 case ZFS_PROP_COMPRESSRATIO:
1993 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
1995 (void) snprintf(propbuf, proplen, "%llu.%02llux",
1996 (u_longlong_t)(val / 100),
1997 (u_longlong_t)(val % 100));
2001 switch (zhp->zfs_type) {
2002 case ZFS_TYPE_FILESYSTEM:
2005 case ZFS_TYPE_VOLUME:
2008 case ZFS_TYPE_SNAPSHOT:
2014 (void) snprintf(propbuf, proplen, "%s", str);
2017 case ZFS_PROP_MOUNTED:
2019 * The 'mounted' property is a pseudo-property that described
2020 * whether the filesystem is currently mounted. Even though
2021 * it's a boolean value, the typical values of "on" and "off"
2022 * don't make sense, so we translate to "yes" and "no".
2024 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2025 src, &source, &val) != 0)
2028 (void) strlcpy(propbuf, "yes", proplen);
2030 (void) strlcpy(propbuf, "no", proplen);
2035 * The 'name' property is a pseudo-property derived from the
2036 * dataset name. It is presented as a real property to simplify
2039 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2042 case ZFS_PROP_MLSLABEL:
2044 m_label_t *new_sl = NULL;
2045 char *ascii = NULL; /* human readable label */
2047 (void) strlcpy(propbuf,
2048 getprop_string(zhp, prop, &source), proplen);
2050 if (literal || (strcasecmp(propbuf,
2051 ZFS_MLSLABEL_DEFAULT) == 0))
2055 * Try to translate the internal hex string to
2056 * human-readable output. If there are any
2057 * problems just use the hex string.
2060 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2061 L_NO_CORRECTION, NULL) == -1) {
2062 m_label_free(new_sl);
2066 if (label_to_str(new_sl, &ascii, M_LABEL,
2070 m_label_free(new_sl);
2073 m_label_free(new_sl);
2075 (void) strlcpy(propbuf, ascii, proplen);
2081 switch (zfs_prop_get_type(prop)) {
2082 case PROP_TYPE_NUMBER:
2083 if (get_numeric_property(zhp, prop, src,
2084 &source, &val) != 0)
2087 (void) snprintf(propbuf, proplen, "%llu",
2090 zfs_nicenum(val, propbuf, proplen);
2093 case PROP_TYPE_STRING:
2094 (void) strlcpy(propbuf,
2095 getprop_string(zhp, prop, &source), proplen);
2098 case PROP_TYPE_INDEX:
2099 if (get_numeric_property(zhp, prop, src,
2100 &source, &val) != 0)
2102 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2104 (void) strlcpy(propbuf, strval, proplen);
2112 get_source(zhp, src, source, statbuf, statlen);
2118 * Utility function to get the given numeric property. Does no validation that
2119 * the given property is the appropriate type; should only be used with
2120 * hard-coded property types.
2123 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2128 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2134 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2138 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2139 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2143 * Similar to zfs_prop_get(), but returns the value as an integer.
2146 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2147 zprop_source_t *src, char *statbuf, size_t statlen)
2152 * Check to see if this property applies to our object
2154 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2155 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2156 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2157 zfs_prop_to_name(prop)));
2161 *src = ZPROP_SRC_NONE;
2163 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2166 get_source(zhp, src, source, statbuf, statlen);
2173 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2174 char **domainp, idmap_rid_t *ridp)
2176 idmap_get_handle_t *get_hdl = NULL;
2180 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2184 err = idmap_get_sidbyuid(get_hdl, id,
2185 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2187 err = idmap_get_sidbygid(get_hdl, id,
2188 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2190 if (err == IDMAP_SUCCESS &&
2191 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2192 status == IDMAP_SUCCESS)
2198 idmap_get_destroy(get_hdl);
2201 #endif /* HAVE_IDMAP */
2204 * convert the propname into parameters needed by kernel
2205 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2206 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2209 userquota_propname_decode(const char *propname, boolean_t zoned,
2210 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2212 zfs_userquota_prop_t type;
2214 char *numericsid = NULL;
2219 /* Figure out the property type ({user|group}{quota|space}) */
2220 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2221 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2222 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2225 if (type == ZFS_NUM_USERQUOTA_PROPS)
2229 isuser = (type == ZFS_PROP_USERQUOTA ||
2230 type == ZFS_PROP_USERUSED);
2232 cp = strchr(propname, '@') + 1;
2234 if (strchr(cp, '@')) {
2237 * It's a SID name (eg "user@domain") that needs to be
2238 * turned into S-1-domainID-RID.
2240 directory_error_t e;
2241 if (zoned && getzoneid() == GLOBAL_ZONEID)
2244 e = directory_sid_from_user_name(NULL,
2247 e = directory_sid_from_group_name(NULL,
2251 directory_error_free(e);
2254 if (numericsid == NULL)
2257 /* will be further decoded below */
2260 #endif /* HAVE_IDMAP */
2263 if (strncmp(cp, "S-1-", 4) == 0) {
2264 /* It's a numeric SID (eg "S-1-234-567-89") */
2265 (void) strlcpy(domain, cp, domainlen);
2266 cp = strrchr(domain, '-');
2271 *ridp = strtoull(cp, &end, 10);
2276 if (errno != 0 || *end != '\0')
2278 } else if (!isdigit(*cp)) {
2280 * It's a user/group name (eg "user") that needs to be
2281 * turned into a uid/gid
2283 if (zoned && getzoneid() == GLOBAL_ZONEID)
2300 /* It's a user/group ID (eg "12345"). */
2301 uid_t id = strtoul(cp, &end, 10);
2308 /* It's an ephemeral ID. */
2309 if (idmap_id_to_numeric_domain_rid(id, isuser,
2310 &mapdomain, &rid) != 0)
2312 (void) strlcpy(domain, mapdomain, domainlen);
2319 #endif /* HAVE_IDMAP */
2322 ASSERT3P(numericsid, ==, NULL);
2327 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2328 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2331 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2333 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2335 err = userquota_propname_decode(propname,
2336 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2337 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2338 zc.zc_objset_type = *typep;
2342 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2346 *propvalue = zc.zc_cookie;
2351 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2352 uint64_t *propvalue)
2354 zfs_userquota_prop_t type;
2356 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2361 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2362 char *propbuf, int proplen, boolean_t literal)
2366 zfs_userquota_prop_t type;
2368 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2375 (void) snprintf(propbuf, proplen, "%llu",
2376 (u_longlong_t)propvalue);
2377 } else if (propvalue == 0 &&
2378 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2379 (void) strlcpy(propbuf, "none", proplen);
2381 zfs_nicenum(propvalue, propbuf, proplen);
2387 * Returns the name of the given zfs handle.
2390 zfs_get_name(const zfs_handle_t *zhp)
2392 return (zhp->zfs_name);
2396 * Returns the type of the given zfs handle.
2399 zfs_get_type(const zfs_handle_t *zhp)
2401 return (zhp->zfs_type);
2405 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2408 uint64_t orig_cookie;
2410 orig_cookie = zc->zc_cookie;
2412 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2413 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2418 /* expand nvlist memory and try again */
2419 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2420 zcmd_free_nvlists(zc);
2423 zc->zc_cookie = orig_cookie;
2426 * An errno value of ESRCH indicates normal completion.
2427 * If ENOENT is returned, then the underlying dataset
2428 * has been removed since we obtained the handle.
2435 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2436 dgettext(TEXT_DOMAIN,
2437 "cannot iterate filesystems"));
2445 * Iterate over all child filesystems
2448 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2450 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2454 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2457 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2460 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2463 * Silently ignore errors, as the only plausible explanation is
2464 * that the pool has since been removed.
2466 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2471 if ((ret = func(nzhp, data)) != 0) {
2472 zcmd_free_nvlists(&zc);
2476 zcmd_free_nvlists(&zc);
2477 return ((ret < 0) ? ret : 0);
2481 * Iterate over all snapshots
2484 zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2486 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2490 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2493 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2495 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2498 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2503 if ((ret = func(nzhp, data)) != 0) {
2504 zcmd_free_nvlists(&zc);
2508 zcmd_free_nvlists(&zc);
2509 return ((ret < 0) ? ret : 0);
2513 * Iterate over all children, snapshots and filesystems
2516 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2520 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2523 return (zfs_iter_snapshots(zhp, func, data));
2527 * Is one dataset name a child dataset of another?
2529 * Needs to handle these cases:
2530 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2531 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2532 * Descendant? No. No. No. Yes.
2535 is_descendant(const char *ds1, const char *ds2)
2537 size_t d1len = strlen(ds1);
2539 /* ds2 can't be a descendant if it's smaller */
2540 if (strlen(ds2) < d1len)
2543 /* otherwise, compare strings and verify that there's a '/' char */
2544 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2548 * Given a complete name, return just the portion that refers to the parent.
2549 * Can return NULL if this is a pool.
2552 parent_name(const char *path, char *buf, size_t buflen)
2556 if ((loc = strrchr(path, '/')) == NULL)
2559 (void) strncpy(buf, path, MIN(buflen, loc - path));
2560 buf[loc - path] = '\0';
2566 * If accept_ancestor is false, then check to make sure that the given path has
2567 * a parent, and that it exists. If accept_ancestor is true, then find the
2568 * closest existing ancestor for the given path. In prefixlen return the
2569 * length of already existing prefix of the given path. We also fetch the
2570 * 'zoned' property, which is used to validate property settings when creating
2574 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2575 boolean_t accept_ancestor, int *prefixlen)
2577 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2578 char parent[ZFS_MAXNAMELEN];
2584 (void) snprintf(errbuf, sizeof (errbuf),
2585 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2587 /* get parent, and check to see if this is just a pool */
2588 if (parent_name(path, parent, sizeof (parent)) != 0) {
2589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2590 "missing dataset name"));
2591 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2594 /* check to see if the pool exists */
2595 if ((slash = strchr(parent, '/')) == NULL)
2596 slash = parent + strlen(parent);
2597 (void) strncpy(zc.zc_name, parent, slash - parent);
2598 zc.zc_name[slash - parent] = '\0';
2599 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 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));
2606 /* check to see if the parent dataset exists */
2607 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2608 if (errno == ENOENT && accept_ancestor) {
2610 * Go deeper to find an ancestor, give up on top level.
2612 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2614 "no such pool '%s'"), zc.zc_name);
2615 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2617 } else if (errno == ENOENT) {
2618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2619 "parent does not exist"));
2620 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2622 return (zfs_standard_error(hdl, errno, errbuf));
2625 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2629 /* we are in a non-global zone, but parent is in the global zone */
2630 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2631 (void) zfs_standard_error(hdl, EPERM, errbuf);
2636 /* make sure parent is a filesystem */
2637 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2638 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2639 "parent is not a filesystem"));
2640 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2646 if (prefixlen != NULL)
2647 *prefixlen = strlen(parent);
2652 * Finds whether the dataset of the given type(s) exists.
2655 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2659 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2663 * Try to get stats for the dataset, which will tell us if it exists.
2665 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2666 int ds_type = zhp->zfs_type;
2669 if (types & ds_type)
2676 * Given a path to 'target', create all the ancestors between
2677 * the prefixlen portion of the path, and the target itself.
2678 * Fail if the initial prefixlen-ancestor does not already exist.
2681 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2687 /* make sure prefix exists */
2688 cp = target + prefixlen;
2690 assert(strchr(cp, '/') == NULL);
2691 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2694 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2702 * Attempt to create, mount, and share any ancestor filesystems,
2703 * up to the prefixlen-long one.
2705 for (cp = target + prefixlen + 1;
2706 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2711 h = make_dataset_handle(hdl, target);
2713 /* it already exists, nothing to do here */
2718 logstr = hdl->libzfs_log_str;
2719 hdl->libzfs_log_str = NULL;
2720 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2722 hdl->libzfs_log_str = logstr;
2723 opname = dgettext(TEXT_DOMAIN, "create");
2727 hdl->libzfs_log_str = logstr;
2728 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2730 opname = dgettext(TEXT_DOMAIN, "open");
2734 if (zfs_mount(h, NULL, 0) != 0) {
2735 opname = dgettext(TEXT_DOMAIN, "mount");
2739 if (zfs_share(h) != 0) {
2740 opname = dgettext(TEXT_DOMAIN, "share");
2750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2751 "failed to %s ancestor '%s'"), opname, target);
2756 * Creates non-existing ancestors of the given path.
2759 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2765 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2768 if ((path_copy = strdup(path)) != NULL) {
2769 rc = create_parents(hdl, path_copy, prefix);
2772 if (path_copy == NULL || rc != 0)
2779 * Create a new filesystem or volume.
2782 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2785 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2788 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2792 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2793 "cannot create '%s'"), path);
2795 /* validate the path, taking care to note the extended error message */
2796 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2797 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2799 /* validate parents exist */
2800 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2804 * The failure modes when creating a dataset of a different type over
2805 * one that already exists is a little strange. In particular, if you
2806 * try to create a dataset on top of an existing dataset, the ioctl()
2807 * will return ENOENT, not EEXIST. To prevent this from happening, we
2808 * first try to see if the dataset exists.
2810 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2811 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2812 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2813 "dataset already exists"));
2814 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2817 if (type == ZFS_TYPE_VOLUME)
2818 zc.zc_objset_type = DMU_OST_ZVOL;
2820 zc.zc_objset_type = DMU_OST_ZFS;
2822 if (props && (props = zfs_valid_proplist(hdl, type, props,
2823 zoned, NULL, errbuf)) == 0)
2826 if (type == ZFS_TYPE_VOLUME) {
2828 * If we are creating a volume, the size and block size must
2829 * satisfy a few restraints. First, the blocksize must be a
2830 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2831 * volsize must be a multiple of the block size, and cannot be
2834 if (props == NULL || nvlist_lookup_uint64(props,
2835 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2837 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2838 "missing volume size"));
2839 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2842 if ((ret = nvlist_lookup_uint64(props,
2843 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2844 &blocksize)) != 0) {
2845 if (ret == ENOENT) {
2846 blocksize = zfs_prop_default_numeric(
2847 ZFS_PROP_VOLBLOCKSIZE);
2850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2851 "missing volume block size"));
2852 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2858 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2859 "volume size cannot be zero"));
2860 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2863 if (size % blocksize != 0) {
2865 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2866 "volume size must be a multiple of volume block "
2868 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2872 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2876 /* create the dataset */
2877 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2879 zcmd_free_nvlists(&zc);
2881 /* check for failure */
2883 char parent[ZFS_MAXNAMELEN];
2884 (void) parent_name(path, parent, sizeof (parent));
2888 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2889 "no such parent '%s'"), parent);
2890 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2893 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2894 "parent '%s' is not a filesystem"), parent);
2895 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2898 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2899 "volume block size must be power of 2 from "
2901 (uint_t)SPA_MINBLOCKSIZE,
2902 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2904 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2907 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2908 "pool must be upgraded to set this "
2909 "property or value"));
2910 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2914 * This platform can't address a volume this big.
2916 if (type == ZFS_TYPE_VOLUME)
2917 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2922 return (zfs_standard_error(hdl, errno, errbuf));
2930 * Destroys the given dataset. The caller must make sure that the filesystem
2931 * isn't mounted, and that there are no active dependents.
2934 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
2936 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2938 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2940 if (ZFS_IS_VOLUME(zhp)) {
2941 zc.zc_objset_type = DMU_OST_ZVOL;
2943 zc.zc_objset_type = DMU_OST_ZFS;
2946 zc.zc_defer_destroy = defer;
2947 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
2948 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
2949 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
2953 remove_mountpoint(zhp);
2958 struct destroydata {
2965 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
2967 struct destroydata *dd = arg;
2969 char name[ZFS_MAXNAMELEN];
2970 boolean_t closezhp = dd->closezhp;
2973 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
2974 (void) strlcat(name, "@", sizeof (name));
2975 (void) strlcat(name, dd->snapname, sizeof (name));
2977 szhp = make_dataset_handle(zhp->zfs_hdl, name);
2979 dd->gotone = B_TRUE;
2983 dd->closezhp = B_TRUE;
2985 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
2992 * Destroys all snapshots with the given name in zhp & descendants.
2995 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
2997 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2999 struct destroydata dd = { 0 };
3001 dd.snapname = snapname;
3002 (void) zfs_check_snap_cb(zhp, &dd);
3005 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3006 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3007 zhp->zfs_name, snapname));
3010 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3011 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3012 zc.zc_defer_destroy = defer;
3014 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3018 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3019 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3023 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3024 "snapshot is cloned"));
3025 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3028 return (zfs_standard_error(zhp->zfs_hdl, errno,
3037 * Clones the given dataset. The target must be of the same type as the source.
3040 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3042 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3043 char parent[ZFS_MAXNAMELEN];
3046 libzfs_handle_t *hdl = zhp->zfs_hdl;
3050 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3052 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3053 "cannot create '%s'"), target);
3055 /* validate the target name */
3056 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3057 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3059 /* validate parents exist */
3060 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3063 (void) parent_name(target, parent, sizeof (parent));
3066 if (ZFS_IS_VOLUME(zhp)) {
3067 zc.zc_objset_type = DMU_OST_ZVOL;
3068 type = ZFS_TYPE_VOLUME;
3070 zc.zc_objset_type = DMU_OST_ZFS;
3071 type = ZFS_TYPE_FILESYSTEM;
3075 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3076 zhp, errbuf)) == NULL)
3079 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3087 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3088 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3089 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3091 zcmd_free_nvlists(&zc);
3098 * The parent doesn't exist. We should have caught this
3099 * above, but there may a race condition that has since
3100 * destroyed the parent.
3102 * At this point, we don't know whether it's the source
3103 * that doesn't exist anymore, or whether the target
3104 * dataset doesn't exist.
3106 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3107 "no such parent '%s'"), parent);
3108 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3111 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3112 "source and target pools differ"));
3113 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3117 return (zfs_standard_error(zhp->zfs_hdl, errno,
3126 * Promotes the given clone fs to be the clone parent.
3129 zfs_promote(zfs_handle_t *zhp)
3131 libzfs_handle_t *hdl = zhp->zfs_hdl;
3132 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3133 char parent[MAXPATHLEN];
3137 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3138 "cannot promote '%s'"), zhp->zfs_name);
3140 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3142 "snapshots can not be promoted"));
3143 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3146 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3147 if (parent[0] == '\0') {
3148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3149 "not a cloned filesystem"));
3150 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3153 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3154 sizeof (zc.zc_value));
3155 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3156 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3159 int save_errno = errno;
3161 switch (save_errno) {
3163 /* There is a conflicting snapshot name. */
3164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3165 "conflicting snapshot '%s' from parent '%s'"),
3166 zc.zc_string, parent);
3167 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3170 return (zfs_standard_error(hdl, save_errno, errbuf));
3177 * Takes a snapshot of the given dataset.
3180 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3184 char parent[ZFS_MAXNAMELEN];
3186 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3190 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3191 "cannot snapshot '%s'"), path);
3193 /* validate the target name */
3194 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3195 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3198 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3199 props, B_FALSE, NULL, errbuf)) == NULL)
3202 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3210 /* make sure the parent exists and is of the appropriate type */
3211 delim = strchr(path, '@');
3212 (void) strncpy(parent, path, delim - path);
3213 parent[delim - path] = '\0';
3215 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3216 ZFS_TYPE_VOLUME)) == NULL) {
3217 zcmd_free_nvlists(&zc);
3221 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3222 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3223 if (ZFS_IS_VOLUME(zhp))
3224 zc.zc_objset_type = DMU_OST_ZVOL;
3226 zc.zc_objset_type = DMU_OST_ZFS;
3227 zc.zc_cookie = recursive;
3228 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3230 zcmd_free_nvlists(&zc);
3233 * if it was recursive, the one that actually failed will be in
3237 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3238 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3239 (void) zfs_standard_error(hdl, errno, errbuf);
3248 * Destroy any more recent snapshots. We invoke this callback on any dependents
3249 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3250 * is a dependent and we should just destroy it without checking the transaction
3253 typedef struct rollback_data {
3254 const char *cb_target; /* the snapshot */
3255 uint64_t cb_create; /* creation time reference */
3257 boolean_t cb_dependent;
3262 rollback_destroy(zfs_handle_t *zhp, void *data)
3264 rollback_data_t *cbp = data;
3266 if (!cbp->cb_dependent) {
3267 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3268 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3269 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3273 cbp->cb_dependent = B_TRUE;
3274 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3275 rollback_destroy, cbp);
3276 cbp->cb_dependent = B_FALSE;
3278 logstr = zhp->zfs_hdl->libzfs_log_str;
3279 zhp->zfs_hdl->libzfs_log_str = NULL;
3280 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3281 zhp->zfs_hdl->libzfs_log_str = logstr;
3284 /* We must destroy this clone; first unmount it */
3285 prop_changelist_t *clp;
3287 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3288 cbp->cb_force ? MS_FORCE: 0);
3289 if (clp == NULL || changelist_prefix(clp) != 0) {
3290 cbp->cb_error = B_TRUE;
3294 if (zfs_destroy(zhp, B_FALSE) != 0)
3295 cbp->cb_error = B_TRUE;
3297 changelist_remove(clp, zhp->zfs_name);
3298 (void) changelist_postfix(clp);
3299 changelist_free(clp);
3307 * Given a dataset, rollback to a specific snapshot, discarding any
3308 * data changes since then and making it the active dataset.
3310 * Any snapshots more recent than the target are destroyed, along with
3314 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3316 rollback_data_t cb = { 0 };
3318 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3319 boolean_t restore_resv = 0;
3320 uint64_t old_volsize = 0, new_volsize;
3321 zfs_prop_t resv_prop = { 0 };
3323 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3324 zhp->zfs_type == ZFS_TYPE_VOLUME);
3327 * Destroy all recent snapshots and its dependends.
3329 cb.cb_force = force;
3330 cb.cb_target = snap->zfs_name;
3331 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3332 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3338 * Now that we have verified that the snapshot is the latest,
3339 * rollback to the given snapshot.
3342 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3343 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3345 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3347 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3350 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3352 if (ZFS_IS_VOLUME(zhp))
3353 zc.zc_objset_type = DMU_OST_ZVOL;
3355 zc.zc_objset_type = DMU_OST_ZFS;
3358 * We rely on zfs_iter_children() to verify that there are no
3359 * newer snapshots for the given dataset. Therefore, we can
3360 * simply pass the name on to the ioctl() call. There is still
3361 * an unlikely race condition where the user has taken a
3362 * snapshot since we verified that this was the most recent.
3365 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3366 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3367 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3373 * For volumes, if the pre-rollback volsize matched the pre-
3374 * rollback reservation and the volsize has changed then set
3375 * the reservation property to the post-rollback volsize.
3376 * Make a new handle since the rollback closed the dataset.
3378 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3379 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3381 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3382 if (old_volsize != new_volsize)
3383 err = zfs_prop_set_int(zhp, resv_prop,
3392 * Iterate over all dependents for a given dataset. This includes both
3393 * hierarchical dependents (children) and data dependents (snapshots and
3394 * clones). The bulk of the processing occurs in get_dependents() in
3398 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3399 zfs_iter_f func, void *data)
3404 zfs_handle_t *child;
3407 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3408 &dependents, &count) != 0)
3411 for (i = 0; i < count; i++) {
3412 if ((child = make_dataset_handle(zhp->zfs_hdl,
3413 dependents[i])) == NULL)
3416 if ((ret = func(child, data)) != 0)
3420 for (i = 0; i < count; i++)
3421 free(dependents[i]);
3428 * Renames the given dataset.
3431 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3434 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3436 prop_changelist_t *cl = NULL;
3437 zfs_handle_t *zhrp = NULL;
3438 char *parentname = NULL;
3439 char parent[ZFS_MAXNAMELEN];
3440 libzfs_handle_t *hdl = zhp->zfs_hdl;
3443 /* if we have the same exact name, just return success */
3444 if (strcmp(zhp->zfs_name, target) == 0)
3447 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3448 "cannot rename to '%s'"), target);
3451 * Make sure the target name is valid
3453 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3454 if ((strchr(target, '@') == NULL) ||
3457 * Snapshot target name is abbreviated,
3458 * reconstruct full dataset name
3460 (void) strlcpy(parent, zhp->zfs_name,
3462 delim = strchr(parent, '@');
3463 if (strchr(target, '@') == NULL)
3467 (void) strlcat(parent, target, sizeof (parent));
3471 * Make sure we're renaming within the same dataset.
3473 delim = strchr(target, '@');
3474 if (strncmp(zhp->zfs_name, target, delim - target)
3475 != 0 || zhp->zfs_name[delim - target] != '@') {
3476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3477 "snapshots must be part of same "
3479 return (zfs_error(hdl, EZFS_CROSSTARGET,
3483 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3484 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3488 "recursive rename must be a snapshot"));
3489 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3492 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3493 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3495 /* validate parents */
3496 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3499 /* make sure we're in the same pool */
3500 verify((delim = strchr(target, '/')) != NULL);
3501 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3502 zhp->zfs_name[delim - target] != '/') {
3503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3504 "datasets must be within same pool"));
3505 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3508 /* new name cannot be a child of the current dataset name */
3509 if (is_descendant(zhp->zfs_name, target)) {
3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3511 "New dataset name cannot be a descendant of "
3512 "current dataset name"));
3513 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3517 (void) snprintf(errbuf, sizeof (errbuf),
3518 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3520 if (getzoneid() == GLOBAL_ZONEID &&
3521 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3523 "dataset is used in a non-global zone"));
3524 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3529 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3530 if (parentname == NULL) {
3534 delim = strchr(parentname, '@');
3536 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3543 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3546 if (changelist_haszonedchild(cl)) {
3547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3548 "child dataset with inherited mountpoint is used "
3549 "in a non-global zone"));
3550 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3555 if ((ret = changelist_prefix(cl)) != 0)
3559 if (ZFS_IS_VOLUME(zhp))
3560 zc.zc_objset_type = DMU_OST_ZVOL;
3562 zc.zc_objset_type = DMU_OST_ZFS;
3564 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3565 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3567 zc.zc_cookie = recursive;
3569 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3571 * if it was recursive, the one that actually failed will
3574 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3575 "cannot rename '%s'"), zc.zc_name);
3577 if (recursive && errno == EEXIST) {
3578 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3579 "a child dataset already has a snapshot "
3580 "with the new name"));
3581 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3583 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3587 * On failure, we still want to remount any filesystems that
3588 * were previously mounted, so we don't alter the system state.
3591 (void) changelist_postfix(cl);
3594 changelist_rename(cl, zfs_get_name(zhp), target);
3595 ret = changelist_postfix(cl);
3607 changelist_free(cl);
3613 zfs_get_user_props(zfs_handle_t *zhp)
3615 return (zhp->zfs_user_props);
3619 zfs_get_recvd_props(zfs_handle_t *zhp)
3621 if (zhp->zfs_recvd_props == NULL)
3622 if (get_recvd_props_ioctl(zhp) != 0)
3624 return (zhp->zfs_recvd_props);
3628 * This function is used by 'zfs list' to determine the exact set of columns to
3629 * display, and their maximum widths. This does two main things:
3631 * - If this is a list of all properties, then expand the list to include
3632 * all native properties, and set a flag so that for each dataset we look
3633 * for new unique user properties and add them to the list.
3635 * - For non fixed-width properties, keep track of the maximum width seen
3636 * so that we can size the column appropriately. If the user has
3637 * requested received property values, we also need to compute the width
3638 * of the RECEIVED column.
3641 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
3643 libzfs_handle_t *hdl = zhp->zfs_hdl;
3644 zprop_list_t *entry;
3645 zprop_list_t **last, **start;
3646 nvlist_t *userprops, *propval;
3649 char buf[ZFS_MAXPROPLEN];
3651 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
3654 userprops = zfs_get_user_props(zhp);
3657 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
3659 * Go through and add any user properties as necessary. We
3660 * start by incrementing our list pointer to the first
3661 * non-native property.
3664 while (*start != NULL) {
3665 if ((*start)->pl_prop == ZPROP_INVAL)
3667 start = &(*start)->pl_next;
3671 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
3673 * See if we've already found this property in our list.
3675 for (last = start; *last != NULL;
3676 last = &(*last)->pl_next) {
3677 if (strcmp((*last)->pl_user_prop,
3678 nvpair_name(elem)) == 0)
3682 if (*last == NULL) {
3683 if ((entry = zfs_alloc(hdl,
3684 sizeof (zprop_list_t))) == NULL ||
3685 ((entry->pl_user_prop = zfs_strdup(hdl,
3686 nvpair_name(elem)))) == NULL) {
3691 entry->pl_prop = ZPROP_INVAL;
3692 entry->pl_width = strlen(nvpair_name(elem));
3693 entry->pl_all = B_TRUE;
3700 * Now go through and check the width of any non-fixed columns
3702 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
3703 if (entry->pl_fixed)
3706 if (entry->pl_prop != ZPROP_INVAL) {
3707 if (zfs_prop_get(zhp, entry->pl_prop,
3708 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
3709 if (strlen(buf) > entry->pl_width)
3710 entry->pl_width = strlen(buf);
3712 if (received && zfs_prop_get_recvd(zhp,
3713 zfs_prop_to_name(entry->pl_prop),
3714 buf, sizeof (buf), B_FALSE) == 0)
3715 if (strlen(buf) > entry->pl_recvd_width)
3716 entry->pl_recvd_width = strlen(buf);
3718 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
3720 verify(nvlist_lookup_string(propval,
3721 ZPROP_VALUE, &strval) == 0);
3722 if (strlen(strval) > entry->pl_width)
3723 entry->pl_width = strlen(strval);
3725 if (received && zfs_prop_get_recvd(zhp,
3726 entry->pl_user_prop,
3727 buf, sizeof (buf), B_FALSE) == 0)
3728 if (strlen(buf) > entry->pl_recvd_width)
3729 entry->pl_recvd_width = strlen(buf);
3737 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
3738 char *resource, void *export, void *sharetab,
3739 int sharemax, zfs_share_op_t operation)
3741 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3744 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3745 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3747 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
3748 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
3749 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
3750 zc.zc_share.z_sharetype = operation;
3751 zc.zc_share.z_sharemax = sharemax;
3752 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
3757 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
3762 * Keep a reference to the props-table against which we prune the
3765 zhp->zfs_props_table = props;
3767 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
3770 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
3771 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
3774 * User properties will result in ZPROP_INVAL, and since we
3775 * only know how to prune standard ZFS properties, we always
3776 * leave these in the list. This can also happen if we
3777 * encounter an unknown DSL property (when running older
3778 * software, for example).
3780 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
3781 (void) nvlist_remove(zhp->zfs_props,
3782 nvpair_name(curr), nvpair_type(curr));
3788 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
3789 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
3791 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3792 nvlist_t *nvlist = NULL;
3795 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3796 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
3797 zc.zc_cookie = (uint64_t)cmd;
3799 if (cmd == ZFS_SMB_ACL_RENAME) {
3800 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
3801 (void) no_memory(hdl);
3807 case ZFS_SMB_ACL_ADD:
3808 case ZFS_SMB_ACL_REMOVE:
3809 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
3811 case ZFS_SMB_ACL_RENAME:
3812 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
3814 (void) no_memory(hdl);
3817 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
3819 (void) no_memory(hdl);
3822 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
3823 nvlist_free(nvlist);
3827 case ZFS_SMB_ACL_PURGE:
3832 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
3834 nvlist_free(nvlist);
3839 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
3840 char *path, char *resource)
3842 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
3847 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
3848 char *path, char *resource)
3850 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
3855 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
3857 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
3862 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
3863 char *oldname, char *newname)
3865 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
3870 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
3871 zfs_userspace_cb_t func, void *arg)
3873 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3875 zfs_useracct_t buf[100];
3877 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3879 zc.zc_objset_type = type;
3880 zc.zc_nvlist_dst = (uintptr_t)buf;
3884 zfs_useracct_t *zua = buf;
3886 zc.zc_nvlist_dst_size = sizeof (buf);
3887 error = ioctl(zhp->zfs_hdl->libzfs_fd,
3888 ZFS_IOC_USERSPACE_MANY, &zc);
3889 if (error || zc.zc_nvlist_dst_size == 0)
3892 while (zc.zc_nvlist_dst_size > 0) {
3893 error = func(arg, zua->zu_domain, zua->zu_rid,
3898 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
3906 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
3907 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
3908 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
3910 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3911 libzfs_handle_t *hdl = zhp->zfs_hdl;
3913 ASSERT(!recursive || dsobj == 0);
3915 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3916 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3917 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3918 >= sizeof (zc.zc_string))
3919 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3920 zc.zc_cookie = recursive;
3921 zc.zc_temphold = temphold;
3922 zc.zc_cleanup_fd = cleanup_fd;
3923 zc.zc_sendobj = dsobj;
3924 zc.zc_createtxg = createtxg;
3926 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
3927 char errbuf[ZFS_MAXNAMELEN+32];
3930 * if it was recursive, the one that actually failed will be in
3933 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3934 "cannot hold '%s@%s'"), zc.zc_name, snapname);
3938 * Temporary tags wind up having the ds object id
3939 * prepended. So even if we passed the length check
3940 * above, it's still possible for the tag to wind
3941 * up being slightly too long.
3943 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
3945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3946 "pool must be upgraded"));
3947 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3949 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3951 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
3957 return (zfs_standard_error_fmt(hdl, errno, errbuf));
3965 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
3966 boolean_t recursive)
3968 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3969 libzfs_handle_t *hdl = zhp->zfs_hdl;
3971 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3972 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3973 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
3974 >= sizeof (zc.zc_string))
3975 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
3976 zc.zc_cookie = recursive;
3978 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
3979 char errbuf[ZFS_MAXNAMELEN+32];
3982 * if it was recursive, the one that actually failed will be in
3985 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3986 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
3990 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
3992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3993 "pool must be upgraded"));
3994 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3996 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3998 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4006 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4009 uint64_t nblocks, volblocksize;
4013 if (nvlist_lookup_string(props,
4014 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4015 ncopies = atoi(strval);
4018 if (nvlist_lookup_uint64(props,
4019 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4020 &volblocksize) != 0)
4021 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4022 nblocks = volsize/volblocksize;
4023 /* start with metadnode L0-L6 */
4025 /* calculate number of indirects */
4026 while (nblocks > 1) {
4027 nblocks += DNODES_PER_LEVEL - 1;
4028 nblocks /= DNODES_PER_LEVEL;
4031 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4034 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4035 * compressed, but in practice they compress down to about
4038 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;