4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2010 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
40 #include <sys/mntent.h>
41 #include <sys/mount.h>
50 #include <directory.h>
51 #endif /* HAVE_IDMAP */
53 #include <sys/dnode.h>
58 #include "zfs_namecheck.h"
60 #include "libzfs_impl.h"
61 #include "zfs_deleg.h"
63 static int zvol_create_link_common(libzfs_handle_t *, const char *, int);
64 static int userquota_propname_decode(const char *propname, boolean_t zoned,
65 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
68 * Given a single type (not a mask of types), return the type in a human
72 zfs_type_to_name(zfs_type_t type)
75 case ZFS_TYPE_FILESYSTEM:
76 return (dgettext(TEXT_DOMAIN, "filesystem"));
77 case ZFS_TYPE_SNAPSHOT:
78 return (dgettext(TEXT_DOMAIN, "snapshot"));
80 return (dgettext(TEXT_DOMAIN, "volume"));
89 * Validate a ZFS path. This is used even before trying to open the dataset, to
90 * provide a more meaningful error message. We call zfs_error_aux() to
91 * explain exactly why the name was not valid.
94 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
100 (void) zfs_prop_get_table();
101 if (dataset_namecheck(path, &why, &what) != 0) {
104 case NAME_ERR_TOOLONG:
105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106 "name is too long"));
109 case NAME_ERR_LEADING_SLASH:
110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
111 "leading slash in name"));
114 case NAME_ERR_EMPTY_COMPONENT:
115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
116 "empty component in name"));
119 case NAME_ERR_TRAILING_SLASH:
120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
121 "trailing slash in name"));
124 case NAME_ERR_INVALCHAR:
126 dgettext(TEXT_DOMAIN, "invalid character "
127 "'%c' in name"), what);
130 case NAME_ERR_MULTIPLE_AT:
131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132 "multiple '@' delimiters in name"));
135 case NAME_ERR_NOLETTER:
136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
137 "pool doesn't begin with a letter"));
140 case NAME_ERR_RESERVED:
141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
142 "name is reserved"));
145 case NAME_ERR_DISKLIKE:
146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
147 "reserved disk name"));
157 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "snapshot delimiter '@' in filesystem name"));
164 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
167 "missing '@' delimiter in snapshot name"));
171 if (modifying && strchr(path, '%') != NULL) {
173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
174 "invalid character %c in name"), '%');
182 zfs_name_valid(const char *name, zfs_type_t type)
184 if (type == ZFS_TYPE_POOL)
185 return (zpool_name_valid(NULL, B_FALSE, name));
186 return (zfs_validate_name(NULL, name, type, B_FALSE));
190 * This function takes the raw DSL properties, and filters out the user-defined
191 * properties into a separate nvlist.
194 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
196 libzfs_handle_t *hdl = zhp->zfs_hdl;
201 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
202 (void) no_memory(hdl);
207 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
208 if (!zfs_prop_user(nvpair_name(elem)))
211 verify(nvpair_value_nvlist(elem, &propval) == 0);
212 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
214 (void) no_memory(hdl);
222 static zpool_handle_t *
223 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
225 libzfs_handle_t *hdl = zhp->zfs_hdl;
228 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
229 if (hdl->libzfs_pool_handles != NULL)
230 zph->zpool_next = hdl->libzfs_pool_handles;
231 hdl->libzfs_pool_handles = zph;
236 static zpool_handle_t *
237 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
239 libzfs_handle_t *hdl = zhp->zfs_hdl;
240 zpool_handle_t *zph = hdl->libzfs_pool_handles;
242 while ((zph != NULL) &&
243 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
244 zph = zph->zpool_next;
249 * Returns a handle to the pool that contains the provided dataset.
250 * If a handle to that pool already exists then that handle is returned.
251 * Otherwise, a new handle is created and added to the list of handles.
253 static zpool_handle_t *
254 zpool_handle(zfs_handle_t *zhp)
260 len = strcspn(zhp->zfs_name, "/@") + 1;
261 pool_name = zfs_alloc(zhp->zfs_hdl, len);
262 (void) strlcpy(pool_name, zhp->zfs_name, len);
264 zph = zpool_find_handle(zhp, pool_name, len);
266 zph = zpool_add_handle(zhp, pool_name);
273 zpool_free_handles(libzfs_handle_t *hdl)
275 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
277 while (zph != NULL) {
278 next = zph->zpool_next;
282 hdl->libzfs_pool_handles = NULL;
286 * Utility function to gather stats (objset and zpl) for the given object.
289 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
291 libzfs_handle_t *hdl = zhp->zfs_hdl;
293 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
295 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
296 if (errno == ENOMEM) {
297 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
308 * Utility function to get the received properties of the given object.
311 get_recvd_props_ioctl(zfs_handle_t *zhp)
313 libzfs_handle_t *hdl = zhp->zfs_hdl;
314 nvlist_t *recvdprops;
315 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
318 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
321 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
323 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
324 if (errno == ENOMEM) {
325 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
329 zcmd_free_nvlists(&zc);
334 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
335 zcmd_free_nvlists(&zc);
339 nvlist_free(zhp->zfs_recvd_props);
340 zhp->zfs_recvd_props = recvdprops;
346 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
348 nvlist_t *allprops, *userprops;
350 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
352 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
357 * XXX Why do we store the user props separately, in addition to
358 * storing them in zfs_props?
360 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
361 nvlist_free(allprops);
365 nvlist_free(zhp->zfs_props);
366 nvlist_free(zhp->zfs_user_props);
368 zhp->zfs_props = allprops;
369 zhp->zfs_user_props = userprops;
375 get_stats(zfs_handle_t *zhp)
378 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
380 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
382 if (get_stats_ioctl(zhp, &zc) != 0)
384 else if (put_stats_zhdl(zhp, &zc) != 0)
386 zcmd_free_nvlists(&zc);
391 * Refresh the properties currently stored in the handle.
394 zfs_refresh_properties(zfs_handle_t *zhp)
396 (void) get_stats(zhp);
400 * Makes a handle from the given dataset name. Used by zfs_open() and
401 * zfs_iter_* to create child handles on the fly.
404 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
406 if (put_stats_zhdl(zhp, zc) != 0)
410 * We've managed to open the dataset and gather statistics. Determine
411 * the high-level type.
413 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
414 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
415 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
416 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
417 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
418 return (-1); /* zpios' and other testing datasets are
419 of this type, ignore if encountered */
423 if (zhp->zfs_dmustats.dds_is_snapshot)
424 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
425 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
426 zhp->zfs_type = ZFS_TYPE_VOLUME;
427 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
428 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
430 abort(); /* we should never see any other types */
432 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
439 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
441 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
443 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
449 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
450 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
454 if (get_stats_ioctl(zhp, &zc) == -1) {
455 zcmd_free_nvlists(&zc);
459 if (make_dataset_handle_common(zhp, &zc) == -1) {
463 zcmd_free_nvlists(&zc);
467 static zfs_handle_t *
468 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
470 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
476 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
477 if (make_dataset_handle_common(zhp, zc) == -1) {
484 static zfs_handle_t *
485 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
487 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
492 zhp->zfs_hdl = pzhp->zfs_hdl;
493 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
494 zhp->zfs_head_type = pzhp->zfs_type;
495 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
496 zhp->zpool_hdl = zpool_handle(zhp);
502 * Opens the given snapshot, filesystem, or volume. The 'types'
503 * argument is a mask of acceptable types. The function will print an
504 * appropriate error message and return NULL if it can't be opened.
507 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
512 (void) snprintf(errbuf, sizeof (errbuf),
513 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
516 * Validate the name before we even try to open it.
518 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) {
519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
520 "invalid dataset name"));
521 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
526 * Try to get stats for the dataset, which will tell us if it exists.
529 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
530 (void) zfs_standard_error(hdl, errno, errbuf);
534 if (!(types & zhp->zfs_type)) {
535 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
544 * Release a ZFS handle. Nothing to do but free the associated memory.
547 zfs_close(zfs_handle_t *zhp)
549 if (zhp->zfs_mntopts)
550 free(zhp->zfs_mntopts);
551 nvlist_free(zhp->zfs_props);
552 nvlist_free(zhp->zfs_user_props);
553 nvlist_free(zhp->zfs_recvd_props);
557 typedef struct mnttab_node {
558 struct mnttab mtn_mt;
563 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
565 const mnttab_node_t *mtn1 = arg1;
566 const mnttab_node_t *mtn2 = arg2;
569 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
573 return (rv > 0 ? 1 : -1);
577 libzfs_mnttab_init(libzfs_handle_t *hdl)
579 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
580 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
581 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
585 libzfs_mnttab_update(libzfs_handle_t *hdl)
589 rewind(hdl->libzfs_mnttab);
590 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
593 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
595 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
596 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
597 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
598 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
599 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
600 avl_add(&hdl->libzfs_mnttab_cache, mtn);
605 libzfs_mnttab_fini(libzfs_handle_t *hdl)
610 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))) {
611 free(mtn->mtn_mt.mnt_special);
612 free(mtn->mtn_mt.mnt_mountp);
613 free(mtn->mtn_mt.mnt_fstype);
614 free(mtn->mtn_mt.mnt_mntopts);
617 avl_destroy(&hdl->libzfs_mnttab_cache);
621 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
623 hdl->libzfs_mnttab_enable = enable;
627 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
628 struct mnttab *entry)
633 if (!hdl->libzfs_mnttab_enable) {
634 struct mnttab srch = { 0 };
636 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
637 libzfs_mnttab_fini(hdl);
638 rewind(hdl->libzfs_mnttab);
639 srch.mnt_special = (char *)fsname;
640 srch.mnt_fstype = MNTTYPE_ZFS;
641 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
647 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
648 libzfs_mnttab_update(hdl);
650 find.mtn_mt.mnt_special = (char *)fsname;
651 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
653 *entry = mtn->mtn_mt;
660 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
661 const char *mountp, const char *mntopts)
665 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
667 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
668 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
669 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
670 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
671 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
672 avl_add(&hdl->libzfs_mnttab_cache, mtn);
676 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
681 find.mtn_mt.mnt_special = (char *)fsname;
682 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))) {
683 avl_remove(&hdl->libzfs_mnttab_cache, ret);
684 free(ret->mtn_mt.mnt_special);
685 free(ret->mtn_mt.mnt_mountp);
686 free(ret->mtn_mt.mnt_fstype);
687 free(ret->mtn_mt.mnt_mntopts);
693 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
695 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
697 if (zpool_handle == NULL)
700 *spa_version = zpool_get_prop_int(zpool_handle,
701 ZPOOL_PROP_VERSION, NULL);
706 * The choice of reservation property depends on the SPA version.
709 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
713 if (zfs_spa_version(zhp, &spa_version) < 0)
716 if (spa_version >= SPA_VERSION_REFRESERVATION)
717 *resv_prop = ZFS_PROP_REFRESERVATION;
719 *resv_prop = ZFS_PROP_RESERVATION;
725 * Given an nvlist of properties to set, validates that they are correct, and
726 * parses any numeric properties (index, boolean, etc) if they are specified as
730 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
731 uint64_t zoned, zfs_handle_t *zhp, const char *errbuf)
738 int chosen_normal = -1;
741 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
742 (void) no_memory(hdl);
747 * Make sure this property is valid and applies to this type.
751 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
752 const char *propname = nvpair_name(elem);
754 prop = zfs_name_to_prop(propname);
755 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
757 * This is a user property: make sure it's a
758 * string, and that it's less than ZAP_MAXNAMELEN.
760 if (nvpair_type(elem) != DATA_TYPE_STRING) {
761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
762 "'%s' must be a string"), propname);
763 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
767 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
768 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
769 "property name '%s' is too long"),
771 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
775 (void) nvpair_value_string(elem, &strval);
776 if (nvlist_add_string(ret, propname, strval) != 0) {
777 (void) no_memory(hdl);
784 * Currently, only user properties can be modified on
787 if (type == ZFS_TYPE_SNAPSHOT) {
788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
789 "this property can not be modified for snapshots"));
790 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
794 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
795 zfs_userquota_prop_t uqtype;
796 char newpropname[128];
801 if (userquota_propname_decode(propname, zoned,
802 &uqtype, domain, sizeof (domain), &rid) != 0) {
804 dgettext(TEXT_DOMAIN,
805 "'%s' has an invalid user/group name"),
807 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
811 if (uqtype != ZFS_PROP_USERQUOTA &&
812 uqtype != ZFS_PROP_GROUPQUOTA) {
814 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
816 (void) zfs_error(hdl, EZFS_PROPREADONLY,
821 if (nvpair_type(elem) == DATA_TYPE_STRING) {
822 (void) nvpair_value_string(elem, &strval);
823 if (strcmp(strval, "none") == 0) {
825 } else if (zfs_nicestrtonum(hdl,
826 strval, &intval) != 0) {
827 (void) zfs_error(hdl,
828 EZFS_BADPROP, errbuf);
831 } else if (nvpair_type(elem) ==
833 (void) nvpair_value_uint64(elem, &intval);
835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
836 "use 'none' to disable "
837 "userquota/groupquota"));
841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
842 "'%s' must be a number"), propname);
843 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
848 * Encode the prop name as
849 * userquota@<hex-rid>-domain, to make it easy
850 * for the kernel to decode.
852 (void) snprintf(newpropname, sizeof (newpropname),
853 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
854 (longlong_t)rid, domain);
858 if (nvlist_add_uint64_array(ret, newpropname,
860 (void) no_memory(hdl);
866 if (prop == ZPROP_INVAL) {
867 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
868 "invalid property '%s'"), propname);
869 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
873 if (!zfs_prop_valid_for_type(prop, type)) {
875 dgettext(TEXT_DOMAIN, "'%s' does not "
876 "apply to datasets of this type"), propname);
877 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
881 if (zfs_prop_readonly(prop) &&
882 (!zfs_prop_setonce(prop) || zhp != NULL)) {
884 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
886 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
890 if (zprop_parse_value(hdl, elem, prop, type, ret,
891 &strval, &intval, errbuf) != 0)
895 * Perform some additional checks for specific properties.
898 case ZFS_PROP_VERSION:
904 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
905 if (intval < version) {
906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
907 "Can not downgrade; already at version %u"),
909 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
915 case ZFS_PROP_RECORDSIZE:
916 case ZFS_PROP_VOLBLOCKSIZE:
917 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
918 if (intval < SPA_MINBLOCKSIZE ||
919 intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) {
920 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
921 "'%s' must be power of 2 from %u "
923 (uint_t)SPA_MINBLOCKSIZE,
924 (uint_t)SPA_MAXBLOCKSIZE >> 10);
925 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
930 case ZFS_PROP_MLSLABEL:
934 * Verify the mlslabel string and convert to
935 * internal hex label string.
939 char *hex = NULL; /* internal label string */
941 /* Default value is already OK. */
942 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
945 /* Verify the label can be converted to binary form */
946 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
947 (str_to_label(strval, &new_sl, MAC_LABEL,
948 L_NO_CORRECTION, NULL) == -1)) {
952 /* Now translate to hex internal label string */
953 if (label_to_str(new_sl, &hex, M_INTERNAL,
959 m_label_free(new_sl);
961 /* If string is already in internal form, we're done. */
962 if (strcmp(strval, hex) == 0) {
967 /* Replace the label string with the internal form. */
968 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
970 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
977 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
978 "invalid mlslabel '%s'"), strval);
979 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
980 m_label_free(new_sl); /* OK if null */
983 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
984 "mlslabels are unsupported"));
985 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
987 #endif /* HAVE_MLSLABEL */
990 case ZFS_PROP_MOUNTPOINT:
994 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
995 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
998 if (mountpoint_namecheck(strval, &why)) {
1000 case NAME_ERR_LEADING_SLASH:
1002 dgettext(TEXT_DOMAIN,
1003 "'%s' must be an absolute path, "
1004 "'none', or 'legacy'"), propname);
1006 case NAME_ERR_TOOLONG:
1008 dgettext(TEXT_DOMAIN,
1009 "component of '%s' is too long"),
1015 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1022 case ZFS_PROP_SHARESMB:
1023 case ZFS_PROP_SHARENFS:
1025 * For the mountpoint and sharenfs or sharesmb
1026 * properties, check if it can be set in a
1027 * global/non-global zone based on
1028 * the zoned property value:
1030 * global zone non-global zone
1031 * --------------------------------------------------
1032 * zoned=on mountpoint (no) mountpoint (yes)
1033 * sharenfs (no) sharenfs (no)
1034 * sharesmb (no) sharesmb (no)
1036 * zoned=off mountpoint (yes) N/A
1041 if (getzoneid() == GLOBAL_ZONEID) {
1042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1043 "'%s' cannot be set on "
1044 "dataset in a non-global zone"),
1046 (void) zfs_error(hdl, EZFS_ZONED,
1049 } else if (prop == ZFS_PROP_SHARENFS ||
1050 prop == ZFS_PROP_SHARESMB) {
1051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1052 "'%s' cannot be set in "
1053 "a non-global zone"), propname);
1054 (void) zfs_error(hdl, EZFS_ZONED,
1058 } else if (getzoneid() != GLOBAL_ZONEID) {
1060 * If zoned property is 'off', this must be in
1061 * a global zone. If not, something is wrong.
1063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1064 "'%s' cannot be set while dataset "
1065 "'zoned' property is set"), propname);
1066 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1071 * At this point, it is legitimate to set the
1072 * property. Now we want to make sure that the
1073 * property value is valid if it is sharenfs.
1075 if ((prop == ZFS_PROP_SHARENFS ||
1076 prop == ZFS_PROP_SHARESMB) &&
1077 strcmp(strval, "on") != 0 &&
1078 strcmp(strval, "off") != 0) {
1079 zfs_share_proto_t proto;
1081 if (prop == ZFS_PROP_SHARESMB)
1087 * Must be an valid sharing protocol
1088 * option string so init the libshare
1089 * in order to enable the parser and
1090 * then parse the options. We use the
1091 * control API since we don't care about
1092 * the current configuration and don't
1093 * want the overhead of loading it
1094 * until we actually do something.
1097 if (zfs_init_libshare(hdl,
1098 SA_INIT_CONTROL_API) != SA_OK) {
1100 * An error occurred so we can't do
1103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1104 "'%s' cannot be set: problem "
1105 "in share initialization"),
1107 (void) zfs_error(hdl, EZFS_BADPROP,
1112 if (zfs_parse_options(strval, proto) != SA_OK) {
1114 * There was an error in parsing so
1115 * deal with it by issuing an error
1116 * message and leaving after
1117 * uninitializing the the libshare
1120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1121 "'%s' cannot be set to invalid "
1122 "options"), propname);
1123 (void) zfs_error(hdl, EZFS_BADPROP,
1125 zfs_uninit_libshare(hdl);
1128 zfs_uninit_libshare(hdl);
1132 case ZFS_PROP_UTF8ONLY:
1133 chosen_utf = (int)intval;
1135 case ZFS_PROP_NORMALIZE:
1136 chosen_normal = (int)intval;
1143 * For changes to existing volumes, we have some additional
1144 * checks to enforce.
1146 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1147 uint64_t volsize = zfs_prop_get_int(zhp,
1149 uint64_t blocksize = zfs_prop_get_int(zhp,
1150 ZFS_PROP_VOLBLOCKSIZE);
1154 case ZFS_PROP_RESERVATION:
1155 case ZFS_PROP_REFRESERVATION:
1156 if (intval > volsize) {
1157 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1158 "'%s' is greater than current "
1159 "volume size"), propname);
1160 (void) zfs_error(hdl, EZFS_BADPROP,
1166 case ZFS_PROP_VOLSIZE:
1167 if (intval % blocksize != 0) {
1168 zfs_nicenum(blocksize, buf,
1170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1171 "'%s' must be a multiple of "
1172 "volume block size (%s)"),
1174 (void) zfs_error(hdl, EZFS_BADPROP,
1180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1181 "'%s' cannot be zero"),
1183 (void) zfs_error(hdl, EZFS_BADPROP,
1195 * If normalization was chosen, but no UTF8 choice was made,
1196 * enforce rejection of non-UTF8 names.
1198 * If normalization was chosen, but rejecting non-UTF8 names
1199 * was explicitly not chosen, it is an error.
1201 if (chosen_normal > 0 && chosen_utf < 0) {
1202 if (nvlist_add_uint64(ret,
1203 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1204 (void) no_memory(hdl);
1207 } else if (chosen_normal > 0 && chosen_utf == 0) {
1208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1209 "'%s' must be set 'on' if normalization chosen"),
1210 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1211 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1222 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1224 uint64_t old_volsize;
1225 uint64_t new_volsize;
1226 uint64_t old_reservation;
1227 uint64_t new_reservation;
1228 zfs_prop_t resv_prop;
1231 * If this is an existing volume, and someone is setting the volsize,
1232 * make sure that it matches the reservation, or add it if necessary.
1234 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1235 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1237 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1238 if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) !=
1239 old_reservation) || nvlist_lookup_uint64(nvl,
1240 zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) {
1243 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1246 new_reservation = zvol_volsize_to_reservation(new_volsize,
1248 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1249 new_reservation) != 0) {
1250 (void) no_memory(zhp->zfs_hdl);
1257 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1264 * For quotas and reservations, ENOSPC indicates
1265 * something different; setting a quota or reservation
1266 * doesn't use any disk space.
1269 case ZFS_PROP_QUOTA:
1270 case ZFS_PROP_REFQUOTA:
1271 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1272 "size is less than current used or "
1274 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1277 case ZFS_PROP_RESERVATION:
1278 case ZFS_PROP_REFRESERVATION:
1279 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1280 "size is greater than available space"));
1281 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1285 (void) zfs_standard_error(hdl, err, errbuf);
1291 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1295 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1299 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1300 "pool and or dataset must be upgraded to set this "
1301 "property or value"));
1302 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1306 if (prop == ZFS_PROP_COMPRESSION) {
1307 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1308 "property setting is not allowed on "
1309 "bootable datasets"));
1310 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1312 (void) zfs_standard_error(hdl, err, errbuf);
1317 if (prop == ZPROP_INVAL) {
1318 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1320 (void) zfs_standard_error(hdl, err, errbuf);
1326 * This platform can't address a volume this big.
1329 if (prop == ZFS_PROP_VOLSIZE) {
1330 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1336 (void) zfs_standard_error(hdl, err, errbuf);
1341 zfs_is_namespace_prop(zfs_prop_t prop)
1345 case ZFS_PROP_ATIME:
1346 case ZFS_PROP_DEVICES:
1348 case ZFS_PROP_SETUID:
1349 case ZFS_PROP_READONLY:
1350 case ZFS_PROP_XATTR:
1351 case ZFS_PROP_NBMAND:
1360 * Given a property name and value, set the property for the given dataset.
1363 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1365 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1367 prop_changelist_t *cl = NULL;
1369 libzfs_handle_t *hdl = zhp->zfs_hdl;
1370 nvlist_t *nvl = NULL, *realprops;
1372 boolean_t do_prefix;
1376 (void) snprintf(errbuf, sizeof (errbuf),
1377 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1380 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1381 nvlist_add_string(nvl, propname, propval) != 0) {
1382 (void) no_memory(hdl);
1386 if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl,
1387 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL)
1393 prop = zfs_name_to_prop(propname);
1395 if (prop == ZFS_PROP_VOLSIZE) {
1396 if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1)
1400 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1403 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1405 "child dataset with inherited mountpoint is used "
1406 "in a non-global zone"));
1407 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1412 * If the dataset's canmount property is being set to noauto,
1413 * then we want to prevent unmounting & remounting it.
1415 do_prefix = !((prop == ZFS_PROP_CANMOUNT) &&
1416 (zprop_string_to_index(prop, propval, &idx,
1417 ZFS_TYPE_DATASET) == 0) && (idx == ZFS_CANMOUNT_NOAUTO));
1419 if (do_prefix && (ret = changelist_prefix(cl)) != 0)
1423 * Execute the corresponding ioctl() to set this property.
1425 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1427 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1430 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1433 zfs_setprop_error(hdl, prop, errno, errbuf);
1434 if (added_resv && errno == ENOSPC) {
1435 /* clean up the volsize property we tried to set */
1436 uint64_t old_volsize = zfs_prop_get_int(zhp,
1439 zcmd_free_nvlists(&zc);
1440 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1442 if (nvlist_add_uint64(nvl,
1443 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1446 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1448 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1452 ret = changelist_postfix(cl);
1456 * Refresh the statistics so the new property
1457 * value is reflected.
1459 (void) get_stats(zhp);
1462 * Remount the filesystem to propagate the change
1463 * if one of the options handled by the generic
1464 * Linux namespace layer has been modified.
1466 if (zfs_is_namespace_prop(prop) &&
1467 zfs_is_mounted(zhp, NULL))
1468 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1474 zcmd_free_nvlists(&zc);
1476 changelist_free(cl);
1481 * Given a property, inherit the value from the parent dataset, or if received
1482 * is TRUE, revert to the received value, if any.
1485 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1487 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1489 prop_changelist_t *cl;
1490 libzfs_handle_t *hdl = zhp->zfs_hdl;
1494 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1495 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1497 zc.zc_cookie = received;
1498 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1500 * For user properties, the amount of work we have to do is very
1501 * small, so just do it here.
1503 if (!zfs_prop_user(propname)) {
1504 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1505 "invalid property"));
1506 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1509 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1510 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1512 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1513 return (zfs_standard_error(hdl, errno, errbuf));
1519 * Verify that this property is inheritable.
1521 if (zfs_prop_readonly(prop))
1522 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1524 if (!zfs_prop_inheritable(prop) && !received)
1525 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1528 * Check to see if the value applies to this type
1530 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1531 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1534 * Normalize the name, to get rid of shorthand abbreviations.
1536 propname = zfs_prop_to_name(prop);
1537 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1538 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1540 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1541 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1543 "dataset is used in a non-global zone"));
1544 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1548 * Determine datasets which will be affected by this change, if any.
1550 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1553 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1555 "child dataset with inherited mountpoint is used "
1556 "in a non-global zone"));
1557 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1561 if ((ret = changelist_prefix(cl)) != 0)
1564 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1565 return (zfs_standard_error(hdl, errno, errbuf));
1568 if ((ret = changelist_postfix(cl)) != 0)
1572 * Refresh the statistics so the new property is reflected.
1574 (void) get_stats(zhp);
1578 changelist_free(cl);
1583 * True DSL properties are stored in an nvlist. The following two functions
1584 * extract them appropriately.
1587 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1593 if (nvlist_lookup_nvlist(zhp->zfs_props,
1594 zfs_prop_to_name(prop), &nv) == 0) {
1595 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1596 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1598 verify(!zhp->zfs_props_table ||
1599 zhp->zfs_props_table[prop] == B_TRUE);
1600 value = zfs_prop_default_numeric(prop);
1608 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1614 if (nvlist_lookup_nvlist(zhp->zfs_props,
1615 zfs_prop_to_name(prop), &nv) == 0) {
1616 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
1617 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1619 verify(!zhp->zfs_props_table ||
1620 zhp->zfs_props_table[prop] == B_TRUE);
1621 if ((value = (char *)zfs_prop_default_string(prop)) == NULL)
1630 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1632 return (zhp->zfs_props == zhp->zfs_recvd_props);
1636 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1638 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1639 zhp->zfs_props = zhp->zfs_recvd_props;
1643 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1645 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1650 * Internal function for getting a numeric property. Both zfs_prop_get() and
1651 * zfs_prop_get_int() are built using this interface.
1653 * Certain properties can be overridden using 'mount -o'. In this case, scan
1654 * the contents of the /etc/mtab entry, searching for the appropriate options.
1655 * If they differ from the on-disk values, report the current values and mark
1656 * the source "temporary".
1659 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1660 char **source, uint64_t *val)
1662 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1663 nvlist_t *zplprops = NULL;
1665 char *mntopt_on = NULL;
1666 char *mntopt_off = NULL;
1667 boolean_t received = zfs_is_recvd_props_mode(zhp);
1672 case ZFS_PROP_ATIME:
1673 mntopt_on = MNTOPT_ATIME;
1674 mntopt_off = MNTOPT_NOATIME;
1677 case ZFS_PROP_DEVICES:
1678 mntopt_on = MNTOPT_DEVICES;
1679 mntopt_off = MNTOPT_NODEVICES;
1683 mntopt_on = MNTOPT_EXEC;
1684 mntopt_off = MNTOPT_NOEXEC;
1687 case ZFS_PROP_READONLY:
1688 mntopt_on = MNTOPT_RO;
1689 mntopt_off = MNTOPT_RW;
1692 case ZFS_PROP_SETUID:
1693 mntopt_on = MNTOPT_SETUID;
1694 mntopt_off = MNTOPT_NOSETUID;
1697 case ZFS_PROP_XATTR:
1698 mntopt_on = MNTOPT_XATTR;
1699 mntopt_off = MNTOPT_NOXATTR;
1702 case ZFS_PROP_NBMAND:
1703 mntopt_on = MNTOPT_NBMAND;
1704 mntopt_off = MNTOPT_NONBMAND;
1711 * Because looking up the mount options is potentially expensive
1712 * (iterating over all of /etc/mtab), we defer its calculation until
1713 * we're looking up a property which requires its presence.
1715 if (!zhp->zfs_mntcheck &&
1716 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1717 libzfs_handle_t *hdl = zhp->zfs_hdl;
1718 struct mnttab entry;
1720 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1721 zhp->zfs_mntopts = zfs_strdup(hdl,
1723 if (zhp->zfs_mntopts == NULL)
1727 zhp->zfs_mntcheck = B_TRUE;
1730 if (zhp->zfs_mntopts == NULL)
1731 mnt.mnt_mntopts = "";
1733 mnt.mnt_mntopts = zhp->zfs_mntopts;
1736 case ZFS_PROP_ATIME:
1737 case ZFS_PROP_DEVICES:
1739 case ZFS_PROP_READONLY:
1740 case ZFS_PROP_SETUID:
1741 case ZFS_PROP_XATTR:
1742 case ZFS_PROP_NBMAND:
1743 *val = getprop_uint64(zhp, prop, source);
1748 if (hasmntopt(&mnt, mntopt_on) && !*val) {
1751 *src = ZPROP_SRC_TEMPORARY;
1752 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
1755 *src = ZPROP_SRC_TEMPORARY;
1759 case ZFS_PROP_CANMOUNT:
1760 case ZFS_PROP_VOLSIZE:
1761 case ZFS_PROP_QUOTA:
1762 case ZFS_PROP_REFQUOTA:
1763 case ZFS_PROP_RESERVATION:
1764 case ZFS_PROP_REFRESERVATION:
1765 *val = getprop_uint64(zhp, prop, source);
1767 if (*source == NULL) {
1768 /* not default, must be local */
1769 *source = zhp->zfs_name;
1773 case ZFS_PROP_MOUNTED:
1774 *val = (zhp->zfs_mntopts != NULL);
1777 case ZFS_PROP_NUMCLONES:
1778 *val = zhp->zfs_dmustats.dds_num_clones;
1781 case ZFS_PROP_VERSION:
1782 case ZFS_PROP_NORMALIZE:
1783 case ZFS_PROP_UTF8ONLY:
1785 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
1786 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
1788 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1789 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
1790 zcmd_free_nvlists(&zc);
1793 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
1794 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
1796 zcmd_free_nvlists(&zc);
1800 nvlist_free(zplprops);
1801 zcmd_free_nvlists(&zc);
1805 switch (zfs_prop_get_type(prop)) {
1806 case PROP_TYPE_NUMBER:
1807 case PROP_TYPE_INDEX:
1808 *val = getprop_uint64(zhp, prop, source);
1810 * If we tried to use a default value for a
1811 * readonly property, it means that it was not
1814 if (zfs_prop_readonly(prop) &&
1815 *source != NULL && (*source)[0] == '\0') {
1820 case PROP_TYPE_STRING:
1822 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1823 "cannot get non-numeric property"));
1824 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
1825 dgettext(TEXT_DOMAIN, "internal error")));
1833 * Calculate the source type, given the raw source string.
1836 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
1837 char *statbuf, size_t statlen)
1839 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
1842 if (source == NULL) {
1843 *srctype = ZPROP_SRC_NONE;
1844 } else if (source[0] == '\0') {
1845 *srctype = ZPROP_SRC_DEFAULT;
1846 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
1847 *srctype = ZPROP_SRC_RECEIVED;
1849 if (strcmp(source, zhp->zfs_name) == 0) {
1850 *srctype = ZPROP_SRC_LOCAL;
1852 (void) strlcpy(statbuf, source, statlen);
1853 *srctype = ZPROP_SRC_INHERITED;
1860 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
1861 size_t proplen, boolean_t literal)
1866 if (zhp->zfs_recvd_props == NULL)
1867 if (get_recvd_props_ioctl(zhp) != 0)
1870 prop = zfs_name_to_prop(propname);
1872 if (prop != ZPROP_INVAL) {
1874 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
1876 zfs_set_recvd_props_mode(zhp, &cookie);
1877 err = zfs_prop_get(zhp, prop, propbuf, proplen,
1878 NULL, NULL, 0, literal);
1879 zfs_unset_recvd_props_mode(zhp, &cookie);
1880 } else if (zfs_prop_userquota(propname)) {
1885 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
1886 propname, &propval) != 0)
1888 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
1890 (void) strlcpy(propbuf, recvdval, proplen);
1893 return (err == 0 ? 0 : -1);
1897 * Retrieve a property from the given object. If 'literal' is specified, then
1898 * numbers are left as exact values. Otherwise, numbers are converted to a
1899 * human-readable form.
1901 * Returns 0 on success, or -1 on error.
1904 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
1905 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
1907 char *source = NULL;
1911 boolean_t received = zfs_is_recvd_props_mode(zhp);
1914 * Check to see if this property applies to our object
1916 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1919 if (received && zfs_prop_readonly(prop))
1923 *src = ZPROP_SRC_NONE;
1926 case ZFS_PROP_CREATION:
1928 * 'creation' is a time_t stored in the statistics. We convert
1929 * this into a string unless 'literal' is specified.
1932 val = getprop_uint64(zhp, prop, &source);
1933 time_t time = (time_t)val;
1937 localtime_r(&time, &t) == NULL ||
1938 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
1940 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t) val);
1944 case ZFS_PROP_MOUNTPOINT:
1946 * Getting the precise mountpoint can be tricky.
1948 * - for 'none' or 'legacy', return those values.
1949 * - for inherited mountpoints, we want to take everything
1950 * after our ancestor and append it to the inherited value.
1952 * If the pool has an alternate root, we want to prepend that
1953 * root to any values we return.
1956 str = getprop_string(zhp, prop, &source);
1958 if (str[0] == '/') {
1959 char buf[MAXPATHLEN];
1961 const char *relpath;
1964 * If we inherit the mountpoint, even from a dataset
1965 * with a received value, the source will be the path of
1966 * the dataset we inherit from. If source is
1967 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1970 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
1973 relpath = zhp->zfs_name + strlen(source);
1974 if (relpath[0] == '/')
1978 if ((zpool_get_prop(zhp->zpool_hdl,
1979 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) ||
1980 (strcmp(root, "-") == 0))
1983 * Special case an alternate root of '/'. This will
1984 * avoid having multiple leading slashes in the
1987 if (strcmp(root, "/") == 0)
1991 * If the mountpoint is '/' then skip over this
1992 * if we are obtaining either an alternate root or
1993 * an inherited mountpoint.
1995 if (str[1] == '\0' && (root[0] != '\0' ||
1996 relpath[0] != '\0'))
1999 if (relpath[0] == '\0')
2000 (void) snprintf(propbuf, proplen, "%s%s",
2003 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2004 root, str, relpath[0] == '@' ? "" : "/",
2007 /* 'legacy' or 'none' */
2008 (void) strlcpy(propbuf, str, proplen);
2013 case ZFS_PROP_ORIGIN:
2014 (void) strlcpy(propbuf, getprop_string(zhp, prop, &source),
2017 * If there is no parent at all, return failure to indicate that
2018 * it doesn't apply to this dataset.
2020 if (propbuf[0] == '\0')
2024 case ZFS_PROP_QUOTA:
2025 case ZFS_PROP_REFQUOTA:
2026 case ZFS_PROP_RESERVATION:
2027 case ZFS_PROP_REFRESERVATION:
2029 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2033 * If quota or reservation is 0, we translate this into 'none'
2034 * (unless literal is set), and indicate that it's the default
2035 * value. Otherwise, we print the number nicely and indicate
2036 * that its set locally.
2040 (void) strlcpy(propbuf, "0", proplen);
2042 (void) strlcpy(propbuf, "none", proplen);
2045 (void) snprintf(propbuf, proplen, "%llu",
2048 zfs_nicenum(val, propbuf, proplen);
2052 case ZFS_PROP_REFRATIO:
2053 case ZFS_PROP_COMPRESSRATIO:
2054 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2056 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2057 (u_longlong_t)(val / 100),
2058 (u_longlong_t)(val % 100));
2062 switch (zhp->zfs_type) {
2063 case ZFS_TYPE_FILESYSTEM:
2066 case ZFS_TYPE_VOLUME:
2069 case ZFS_TYPE_SNAPSHOT:
2075 (void) snprintf(propbuf, proplen, "%s", str);
2078 case ZFS_PROP_MOUNTED:
2080 * The 'mounted' property is a pseudo-property that described
2081 * whether the filesystem is currently mounted. Even though
2082 * it's a boolean value, the typical values of "on" and "off"
2083 * don't make sense, so we translate to "yes" and "no".
2085 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2086 src, &source, &val) != 0)
2089 (void) strlcpy(propbuf, "yes", proplen);
2091 (void) strlcpy(propbuf, "no", proplen);
2096 * The 'name' property is a pseudo-property derived from the
2097 * dataset name. It is presented as a real property to simplify
2100 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2103 case ZFS_PROP_MLSLABEL:
2105 #ifdef HAVE_MLSLABEL
2106 m_label_t *new_sl = NULL;
2107 char *ascii = NULL; /* human readable label */
2109 (void) strlcpy(propbuf,
2110 getprop_string(zhp, prop, &source), proplen);
2112 if (literal || (strcasecmp(propbuf,
2113 ZFS_MLSLABEL_DEFAULT) == 0))
2117 * Try to translate the internal hex string to
2118 * human-readable output. If there are any
2119 * problems just use the hex string.
2122 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2123 L_NO_CORRECTION, NULL) == -1) {
2124 m_label_free(new_sl);
2128 if (label_to_str(new_sl, &ascii, M_LABEL,
2132 m_label_free(new_sl);
2135 m_label_free(new_sl);
2137 (void) strlcpy(propbuf, ascii, proplen);
2140 (void) strlcpy(propbuf,
2141 getprop_string(zhp, prop, &source), proplen);
2142 #endif /* HAVE_MLSLABEL */
2147 switch (zfs_prop_get_type(prop)) {
2148 case PROP_TYPE_NUMBER:
2149 if (get_numeric_property(zhp, prop, src,
2150 &source, &val) != 0)
2153 (void) snprintf(propbuf, proplen, "%llu",
2156 zfs_nicenum(val, propbuf, proplen);
2159 case PROP_TYPE_STRING:
2160 (void) strlcpy(propbuf,
2161 getprop_string(zhp, prop, &source), proplen);
2164 case PROP_TYPE_INDEX:
2165 if (get_numeric_property(zhp, prop, src,
2166 &source, &val) != 0)
2168 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2170 (void) strlcpy(propbuf, strval, proplen);
2178 get_source(zhp, src, source, statbuf, statlen);
2184 * Utility function to get the given numeric property. Does no validation that
2185 * the given property is the appropriate type; should only be used with
2186 * hard-coded property types.
2189 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2194 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2200 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2204 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2205 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2209 * Similar to zfs_prop_get(), but returns the value as an integer.
2212 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2213 zprop_source_t *src, char *statbuf, size_t statlen)
2218 * Check to see if this property applies to our object
2220 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2221 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2222 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2223 zfs_prop_to_name(prop)));
2227 *src = ZPROP_SRC_NONE;
2229 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2232 get_source(zhp, src, source, statbuf, statlen);
2239 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2240 char **domainp, idmap_rid_t *ridp)
2242 idmap_get_handle_t *get_hdl = NULL;
2246 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2250 err = idmap_get_sidbyuid(get_hdl, id,
2251 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2253 err = idmap_get_sidbygid(get_hdl, id,
2254 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2256 if (err == IDMAP_SUCCESS &&
2257 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2258 status == IDMAP_SUCCESS)
2264 idmap_get_destroy(get_hdl);
2267 #endif /* HAVE_IDMAP */
2270 * convert the propname into parameters needed by kernel
2271 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2272 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2273 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2274 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2277 userquota_propname_decode(const char *propname, boolean_t zoned,
2278 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2280 zfs_userquota_prop_t type;
2289 /* Figure out the property type ({user|group}{quota|space}) */
2290 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2291 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2292 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2295 if (type == ZFS_NUM_USERQUOTA_PROPS)
2299 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED);
2300 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED);
2302 cp = strchr(propname, '@') + 1;
2304 if (isuser && (pw = getpwnam(cp)) != NULL) {
2305 if (zoned && getzoneid() == GLOBAL_ZONEID)
2308 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
2309 if (zoned && getzoneid() == GLOBAL_ZONEID)
2312 } else if (strchr(cp, '@')) {
2315 * It's a SID name (eg "user@domain") that needs to be
2316 * turned into S-1-domainID-RID.
2318 directory_error_t e;
2319 char *numericsid = NULL;
2322 if (zoned && getzoneid() == GLOBAL_ZONEID)
2325 e = directory_sid_from_user_name(NULL,
2328 e = directory_sid_from_group_name(NULL,
2332 directory_error_free(e);
2335 if (numericsid == NULL)
2338 (void) strlcpy(domain, cp, domainlen);
2339 cp = strrchr(domain, '-');
2344 *ridp = strtoull(cp, &end, 10);
2347 if (errno != 0 || *end != '\0')
2351 #endif /* HAVE_IDMAP */
2354 /* It's a user/group ID (eg "12345"). */
2360 id = strtoul(cp, &end, 10);
2364 /* It's an ephemeral ID. */
2365 if (idmap_id_to_numeric_domain_rid(id, isuser,
2366 &mapdomain, &rid) != 0)
2368 (void) strlcpy(domain, mapdomain, domainlen);
2375 #endif /* HAVE_IDMAP */
2382 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2383 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2386 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2388 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2390 err = userquota_propname_decode(propname,
2391 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2392 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2393 zc.zc_objset_type = *typep;
2397 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2401 *propvalue = zc.zc_cookie;
2406 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2407 uint64_t *propvalue)
2409 zfs_userquota_prop_t type;
2411 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2416 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2417 char *propbuf, int proplen, boolean_t literal)
2421 zfs_userquota_prop_t type;
2423 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2430 (void) snprintf(propbuf, proplen, "%llu",
2431 (u_longlong_t)propvalue);
2432 } else if (propvalue == 0 &&
2433 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2434 (void) strlcpy(propbuf, "none", proplen);
2436 zfs_nicenum(propvalue, propbuf, proplen);
2442 * Returns the name of the given zfs handle.
2445 zfs_get_name(const zfs_handle_t *zhp)
2447 return (zhp->zfs_name);
2451 * Returns the type of the given zfs handle.
2454 zfs_get_type(const zfs_handle_t *zhp)
2456 return (zhp->zfs_type);
2460 zfs_do_list_ioctl(zfs_handle_t *zhp, int arg, zfs_cmd_t *zc)
2463 uint64_t orig_cookie;
2465 orig_cookie = zc->zc_cookie;
2467 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
2468 rc = ioctl(zhp->zfs_hdl->libzfs_fd, arg, zc);
2473 /* expand nvlist memory and try again */
2474 if (zcmd_expand_dst_nvlist(zhp->zfs_hdl, zc) != 0) {
2475 zcmd_free_nvlists(zc);
2478 zc->zc_cookie = orig_cookie;
2481 * An errno value of ESRCH indicates normal completion.
2482 * If ENOENT is returned, then the underlying dataset
2483 * has been removed since we obtained the handle.
2490 rc = zfs_standard_error(zhp->zfs_hdl, errno,
2491 dgettext(TEXT_DOMAIN,
2492 "cannot iterate filesystems"));
2500 * Iterate over all child filesystems
2503 zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2505 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2509 if (zhp->zfs_type != ZFS_TYPE_FILESYSTEM)
2512 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2515 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_DATASET_LIST_NEXT,
2518 * Silently ignore errors, as the only plausible explanation is
2519 * that the pool has since been removed.
2521 if ((nzhp = make_dataset_handle_zc(zhp->zfs_hdl,
2526 if ((ret = func(nzhp, data)) != 0) {
2527 zcmd_free_nvlists(&zc);
2531 zcmd_free_nvlists(&zc);
2532 return ((ret < 0) ? ret : 0);
2536 * Iterate over all snapshots
2539 zfs_iter_snapshots(zfs_handle_t *zhp, boolean_t simple, zfs_iter_f func,
2542 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2546 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
2549 zc.zc_simple = simple;
2551 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2553 while ((ret = zfs_do_list_ioctl(zhp, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2557 nzhp = make_dataset_simple_handle_zc(zhp, &zc);
2559 nzhp = make_dataset_handle_zc(zhp->zfs_hdl, &zc);
2563 if ((ret = func(nzhp, data)) != 0) {
2564 zcmd_free_nvlists(&zc);
2568 zcmd_free_nvlists(&zc);
2569 return ((ret < 0) ? ret : 0);
2573 * Iterate over all children, snapshots and filesystems
2576 zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data)
2580 if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0)
2583 return (zfs_iter_snapshots(zhp, B_FALSE, func, data));
2587 * Is one dataset name a child dataset of another?
2589 * Needs to handle these cases:
2590 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2591 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2592 * Descendant? No. No. No. Yes.
2595 is_descendant(const char *ds1, const char *ds2)
2597 size_t d1len = strlen(ds1);
2599 /* ds2 can't be a descendant if it's smaller */
2600 if (strlen(ds2) < d1len)
2603 /* otherwise, compare strings and verify that there's a '/' char */
2604 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
2608 * Given a complete name, return just the portion that refers to the parent.
2609 * Can return NULL if this is a pool.
2612 parent_name(const char *path, char *buf, size_t buflen)
2616 if ((loc = strrchr(path, '/')) == NULL)
2619 (void) strncpy(buf, path, MIN(buflen, loc - path));
2620 buf[loc - path] = '\0';
2626 * If accept_ancestor is false, then check to make sure that the given path has
2627 * a parent, and that it exists. If accept_ancestor is true, then find the
2628 * closest existing ancestor for the given path. In prefixlen return the
2629 * length of already existing prefix of the given path. We also fetch the
2630 * 'zoned' property, which is used to validate property settings when creating
2634 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
2635 boolean_t accept_ancestor, int *prefixlen)
2637 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2638 char parent[ZFS_MAXNAMELEN];
2644 (void) snprintf(errbuf, sizeof (errbuf),
2645 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
2647 /* get parent, and check to see if this is just a pool */
2648 if (parent_name(path, parent, sizeof (parent)) != 0) {
2649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2650 "missing dataset name"));
2651 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2654 /* check to see if the pool exists */
2655 if ((slash = strchr(parent, '/')) == NULL)
2656 slash = parent + strlen(parent);
2657 (void) strncpy(zc.zc_name, parent, slash - parent);
2658 zc.zc_name[slash - parent] = '\0';
2659 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
2661 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2662 "no such pool '%s'"), zc.zc_name);
2663 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2666 /* check to see if the parent dataset exists */
2667 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
2668 if (errno == ENOENT && accept_ancestor) {
2670 * Go deeper to find an ancestor, give up on top level.
2672 if (parent_name(parent, parent, sizeof (parent)) != 0) {
2673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2674 "no such pool '%s'"), zc.zc_name);
2675 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2677 } else if (errno == ENOENT) {
2678 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2679 "parent does not exist"));
2680 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2682 return (zfs_standard_error(hdl, errno, errbuf));
2685 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
2689 /* we are in a non-global zone, but parent is in the global zone */
2690 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
2691 (void) zfs_standard_error(hdl, EPERM, errbuf);
2696 /* make sure parent is a filesystem */
2697 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
2698 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2699 "parent is not a filesystem"));
2700 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
2706 if (prefixlen != NULL)
2707 *prefixlen = strlen(parent);
2712 * Finds whether the dataset of the given type(s) exists.
2715 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
2719 if (!zfs_validate_name(hdl, path, types, B_FALSE))
2723 * Try to get stats for the dataset, which will tell us if it exists.
2725 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
2726 int ds_type = zhp->zfs_type;
2729 if (types & ds_type)
2736 * Given a path to 'target', create all the ancestors between
2737 * the prefixlen portion of the path, and the target itself.
2738 * Fail if the initial prefixlen-ancestor does not already exist.
2741 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
2747 /* make sure prefix exists */
2748 cp = target + prefixlen;
2750 assert(strchr(cp, '/') == NULL);
2751 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2754 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2762 * Attempt to create, mount, and share any ancestor filesystems,
2763 * up to the prefixlen-long one.
2765 for (cp = target + prefixlen + 1;
2766 (cp = strchr(cp, '/')); *cp = '/', cp++) {
2771 h = make_dataset_handle(hdl, target);
2773 /* it already exists, nothing to do here */
2778 logstr = hdl->libzfs_log_str;
2779 hdl->libzfs_log_str = NULL;
2780 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
2782 hdl->libzfs_log_str = logstr;
2783 opname = dgettext(TEXT_DOMAIN, "create");
2787 hdl->libzfs_log_str = logstr;
2788 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
2790 opname = dgettext(TEXT_DOMAIN, "open");
2794 if (zfs_mount(h, NULL, 0) != 0) {
2795 opname = dgettext(TEXT_DOMAIN, "mount");
2799 if (zfs_share(h) != 0) {
2800 opname = dgettext(TEXT_DOMAIN, "share");
2810 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2811 "failed to %s ancestor '%s'"), opname, target);
2816 * Creates non-existing ancestors of the given path.
2819 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
2825 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
2828 if ((path_copy = strdup(path)) != NULL) {
2829 rc = create_parents(hdl, path_copy, prefix);
2832 if (path_copy == NULL || rc != 0)
2839 * Create a new filesystem or volume.
2842 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
2845 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2848 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
2852 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2853 "cannot create '%s'"), path);
2855 /* validate the path, taking care to note the extended error message */
2856 if (!zfs_validate_name(hdl, path, type, B_TRUE))
2857 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2859 /* validate parents exist */
2860 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
2864 * The failure modes when creating a dataset of a different type over
2865 * one that already exists is a little strange. In particular, if you
2866 * try to create a dataset on top of an existing dataset, the ioctl()
2867 * will return ENOENT, not EEXIST. To prevent this from happening, we
2868 * first try to see if the dataset exists.
2870 (void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name));
2871 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2872 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2873 "dataset already exists"));
2874 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2877 if (type == ZFS_TYPE_VOLUME)
2878 zc.zc_objset_type = DMU_OST_ZVOL;
2880 zc.zc_objset_type = DMU_OST_ZFS;
2882 if (props && (props = zfs_valid_proplist(hdl, type, props,
2883 zoned, NULL, errbuf)) == 0)
2886 if (type == ZFS_TYPE_VOLUME) {
2888 * If we are creating a volume, the size and block size must
2889 * satisfy a few restraints. First, the blocksize must be a
2890 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2891 * volsize must be a multiple of the block size, and cannot be
2894 if (props == NULL || nvlist_lookup_uint64(props,
2895 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
2897 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2898 "missing volume size"));
2899 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2902 if ((ret = nvlist_lookup_uint64(props,
2903 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
2904 &blocksize)) != 0) {
2905 if (ret == ENOENT) {
2906 blocksize = zfs_prop_default_numeric(
2907 ZFS_PROP_VOLBLOCKSIZE);
2910 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2911 "missing volume block size"));
2912 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2918 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2919 "volume size cannot be zero"));
2920 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2923 if (size % blocksize != 0) {
2925 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2926 "volume size must be a multiple of volume block "
2928 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2932 if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
2936 /* create the dataset */
2937 ret = zfs_ioctl(hdl, ZFS_IOC_CREATE, &zc);
2939 if (ret == 0 && type == ZFS_TYPE_VOLUME) {
2940 ret = zvol_create_link(hdl, path);
2942 (void) zfs_standard_error(hdl, errno,
2943 dgettext(TEXT_DOMAIN,
2944 "Volume successfully created, but device links "
2945 "were not created"));
2946 zcmd_free_nvlists(&zc);
2951 zcmd_free_nvlists(&zc);
2953 /* check for failure */
2955 char parent[ZFS_MAXNAMELEN];
2956 (void) parent_name(path, parent, sizeof (parent));
2960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2961 "no such parent '%s'"), parent);
2962 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2965 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2966 "parent '%s' is not a filesystem"), parent);
2967 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2970 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2971 "volume block size must be power of 2 from "
2973 (uint_t)SPA_MINBLOCKSIZE,
2974 (uint_t)SPA_MAXBLOCKSIZE >> 10);
2976 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2980 "pool must be upgraded to set this "
2981 "property or value"));
2982 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
2986 * This platform can't address a volume this big.
2988 if (type == ZFS_TYPE_VOLUME)
2989 return (zfs_error(hdl, EZFS_VOLTOOBIG,
2994 return (zfs_standard_error(hdl, errno, errbuf));
3002 * Destroys the given dataset. The caller must make sure that the filesystem
3003 * isn't mounted, and that there are no active dependents.
3006 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3008 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3010 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3012 if (ZFS_IS_VOLUME(zhp)) {
3013 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3016 zc.zc_objset_type = DMU_OST_ZVOL;
3018 zc.zc_objset_type = DMU_OST_ZFS;
3021 zc.zc_defer_destroy = defer;
3022 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0) {
3023 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3024 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3028 remove_mountpoint(zhp);
3033 struct destroydata {
3040 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3042 struct destroydata *dd = arg;
3044 char name[ZFS_MAXNAMELEN];
3045 boolean_t closezhp = dd->closezhp;
3048 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3049 (void) strlcat(name, "@", sizeof (name));
3050 (void) strlcat(name, dd->snapname, sizeof (name));
3052 szhp = make_dataset_handle(zhp->zfs_hdl, name);
3054 dd->gotone = B_TRUE;
3058 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3059 (void) zvol_remove_link(zhp->zfs_hdl, name);
3061 * NB: this is simply a best-effort. We don't want to
3062 * return an error, because then we wouldn't visit all
3067 dd->closezhp = B_TRUE;
3068 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, arg);
3075 * Destroys all snapshots with the given name in zhp & descendants.
3078 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3080 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3082 struct destroydata dd = { 0 };
3084 dd.snapname = snapname;
3085 (void) zfs_check_snap_cb(zhp, &dd);
3088 return (zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3089 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3090 zhp->zfs_name, snapname));
3093 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3094 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3095 zc.zc_defer_destroy = defer;
3097 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY_SNAPS, &zc);
3101 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3102 "cannot destroy '%s@%s'"), zc.zc_name, snapname);
3106 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3107 "snapshot is cloned"));
3108 return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf));
3111 return (zfs_standard_error(zhp->zfs_hdl, errno,
3120 * Clones the given dataset. The target must be of the same type as the source.
3123 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3125 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3126 char parent[ZFS_MAXNAMELEN];
3129 libzfs_handle_t *hdl = zhp->zfs_hdl;
3133 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3135 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3136 "cannot create '%s'"), target);
3138 /* validate the target name */
3139 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3140 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3142 /* validate parents exist */
3143 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3146 (void) parent_name(target, parent, sizeof (parent));
3149 if (ZFS_IS_VOLUME(zhp)) {
3150 zc.zc_objset_type = DMU_OST_ZVOL;
3151 type = ZFS_TYPE_VOLUME;
3153 zc.zc_objset_type = DMU_OST_ZFS;
3154 type = ZFS_TYPE_FILESYSTEM;
3158 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3159 zhp, errbuf)) == NULL)
3162 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3170 (void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name));
3171 (void) strlcpy(zc.zc_value, zhp->zfs_name, sizeof (zc.zc_value));
3172 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_CREATE, &zc);
3174 zcmd_free_nvlists(&zc);
3181 * The parent doesn't exist. We should have caught this
3182 * above, but there may a race condition that has since
3183 * destroyed the parent.
3185 * At this point, we don't know whether it's the source
3186 * that doesn't exist anymore, or whether the target
3187 * dataset doesn't exist.
3189 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3190 "no such parent '%s'"), parent);
3191 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3194 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3195 "source and target pools differ"));
3196 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3200 return (zfs_standard_error(zhp->zfs_hdl, errno,
3203 } else if (ZFS_IS_VOLUME(zhp)) {
3204 ret = zvol_create_link(zhp->zfs_hdl, target);
3210 typedef struct promote_data {
3211 char cb_mountpoint[MAXPATHLEN];
3212 const char *cb_target;
3213 const char *cb_errbuf;
3214 uint64_t cb_pivot_txg;
3218 promote_snap_cb(zfs_handle_t *zhp, void *data)
3220 promote_data_t *pd = data;
3222 char snapname[MAXPATHLEN];
3225 /* We don't care about snapshots after the pivot point */
3226 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) {
3231 /* Remove the device link if it's a zvol. */
3232 if (ZFS_IS_VOLUME(zhp))
3233 (void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name);
3235 /* Check for conflicting names */
3236 (void) strlcpy(snapname, pd->cb_target, sizeof (snapname));
3237 (void) strlcat(snapname, strchr(zhp->zfs_name, '@'), sizeof (snapname));
3238 szhp = make_dataset_handle(zhp->zfs_hdl, snapname);
3241 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3242 "snapshot name '%s' from origin \n"
3243 "conflicts with '%s' from target"),
3244 zhp->zfs_name, snapname);
3245 rv = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf);
3252 promote_snap_done_cb(zfs_handle_t *zhp, void *data)
3254 promote_data_t *pd = data;
3256 /* We don't care about snapshots after the pivot point */
3257 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) <= pd->cb_pivot_txg) {
3258 /* Create the device link if it's a zvol. */
3259 if (ZFS_IS_VOLUME(zhp))
3260 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
3268 * Promotes the given clone fs to be the clone parent.
3271 zfs_promote(zfs_handle_t *zhp)
3273 libzfs_handle_t *hdl = zhp->zfs_hdl;
3274 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3275 char parent[MAXPATHLEN];
3282 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3283 "cannot promote '%s'"), zhp->zfs_name);
3285 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3287 "snapshots can not be promoted"));
3288 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3291 (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent));
3292 if (parent[0] == '\0') {
3293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3294 "not a cloned filesystem"));
3295 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3297 cp = strchr(parent, '@');
3300 /* Walk the snapshots we will be moving */
3301 pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
3304 pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG);
3306 pd.cb_target = zhp->zfs_name;
3307 pd.cb_errbuf = errbuf;
3308 pzhp = zfs_open(hdl, parent, ZFS_TYPE_DATASET);
3311 (void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint,
3312 sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE);
3313 ret = zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_cb, &pd);
3319 /* issue the ioctl */
3320 (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin,
3321 sizeof (zc.zc_value));
3322 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3323 ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3326 int save_errno = errno;
3328 (void) zfs_iter_snapshots(pzhp, B_FALSE, promote_snap_done_cb,
3332 switch (save_errno) {
3335 * There is a conflicting snapshot name. We
3336 * should have caught this above, but they could
3337 * have renamed something in the mean time.
3339 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3340 "conflicting snapshot '%s' from parent '%s'"),
3341 zc.zc_string, parent);
3342 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3345 return (zfs_standard_error(hdl, save_errno, errbuf));
3348 (void) zfs_iter_snapshots(zhp, B_FALSE, promote_snap_done_cb,
3357 const char *cd_snapname;
3362 zfs_create_link_cb(zfs_handle_t *zhp, void *arg)
3364 struct createdata *cd = arg;
3367 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3368 char name[MAXPATHLEN];
3370 (void) strlcpy(name, zhp->zfs_name, sizeof (name));
3371 (void) strlcat(name, "@", sizeof (name));
3372 (void) strlcat(name, cd->cd_snapname, sizeof (name));
3373 (void) zvol_create_link_common(zhp->zfs_hdl, name,
3376 * NB: this is simply a best-effort. We don't want to
3377 * return an error, because then we wouldn't visit all
3382 ret = zfs_iter_filesystems(zhp, zfs_create_link_cb, cd);
3390 * Takes a snapshot of the given dataset.
3393 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3397 char parent[ZFS_MAXNAMELEN];
3399 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3403 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3404 "cannot snapshot '%s'"), path);
3406 /* validate the target name */
3407 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3408 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3411 if ((props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3412 props, B_FALSE, NULL, errbuf)) == NULL)
3415 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
3423 /* make sure the parent exists and is of the appropriate type */
3424 delim = strchr(path, '@');
3425 (void) strncpy(parent, path, delim - path);
3426 parent[delim - path] = '\0';
3428 if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM |
3429 ZFS_TYPE_VOLUME)) == NULL) {
3430 zcmd_free_nvlists(&zc);
3434 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3435 (void) strlcpy(zc.zc_value, delim+1, sizeof (zc.zc_value));
3436 if (ZFS_IS_VOLUME(zhp))
3437 zc.zc_objset_type = DMU_OST_ZVOL;
3439 zc.zc_objset_type = DMU_OST_ZFS;
3440 zc.zc_cookie = recursive;
3441 ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SNAPSHOT, &zc);
3443 zcmd_free_nvlists(&zc);
3446 * if it was recursive, the one that actually failed will be in
3450 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3451 "cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_value);
3453 if (ret == 0 && recursive) {
3454 struct createdata cd;
3456 cd.cd_snapname = delim + 1;
3457 cd.cd_ifexists = B_FALSE;
3458 (void) zfs_iter_filesystems(zhp, zfs_create_link_cb, &cd);
3460 if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) {
3461 ret = zvol_create_link(zhp->zfs_hdl, path);
3463 (void) zfs_standard_error(hdl, errno,
3464 dgettext(TEXT_DOMAIN,
3465 "Volume successfully snapshotted, but device links "
3466 "were not created"));
3473 (void) zfs_standard_error(hdl, errno, errbuf);
3481 * Destroy any more recent snapshots. We invoke this callback on any dependents
3482 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3483 * is a dependent and we should just destroy it without checking the transaction
3486 typedef struct rollback_data {
3487 const char *cb_target; /* the snapshot */
3488 uint64_t cb_create; /* creation time reference */
3490 boolean_t cb_dependent;
3495 rollback_destroy(zfs_handle_t *zhp, void *data)
3497 rollback_data_t *cbp = data;
3499 if (!cbp->cb_dependent) {
3500 if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 &&
3501 zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
3502 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) >
3506 cbp->cb_dependent = B_TRUE;
3507 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3508 rollback_destroy, cbp);
3509 cbp->cb_dependent = B_FALSE;
3511 logstr = zhp->zfs_hdl->libzfs_log_str;
3512 zhp->zfs_hdl->libzfs_log_str = NULL;
3513 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3514 zhp->zfs_hdl->libzfs_log_str = logstr;
3517 /* We must destroy this clone; first unmount it */
3518 prop_changelist_t *clp;
3520 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3521 cbp->cb_force ? MS_FORCE: 0);
3522 if (clp == NULL || changelist_prefix(clp) != 0) {
3523 cbp->cb_error = B_TRUE;
3527 if (zfs_destroy(zhp, B_FALSE) != 0)
3528 cbp->cb_error = B_TRUE;
3530 changelist_remove(clp, zhp->zfs_name);
3531 (void) changelist_postfix(clp);
3532 changelist_free(clp);
3540 * Given a dataset, rollback to a specific snapshot, discarding any
3541 * data changes since then and making it the active dataset.
3543 * Any snapshots more recent than the target are destroyed, along with
3547 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3549 rollback_data_t cb = { 0 };
3551 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3552 boolean_t restore_resv = 0;
3553 uint64_t old_volsize = 0, new_volsize;
3554 zfs_prop_t resv_prop = { 0 };
3556 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3557 zhp->zfs_type == ZFS_TYPE_VOLUME);
3560 * Destroy all recent snapshots and its dependends.
3562 cb.cb_force = force;
3563 cb.cb_target = snap->zfs_name;
3564 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3565 (void) zfs_iter_children(zhp, rollback_destroy, &cb);
3571 * Now that we have verified that the snapshot is the latest,
3572 * rollback to the given snapshot.
3575 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3576 if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0)
3578 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3580 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3582 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3585 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3587 if (ZFS_IS_VOLUME(zhp))
3588 zc.zc_objset_type = DMU_OST_ZVOL;
3590 zc.zc_objset_type = DMU_OST_ZFS;
3593 * We rely on zfs_iter_children() to verify that there are no
3594 * newer snapshots for the given dataset. Therefore, we can
3595 * simply pass the name on to the ioctl() call. There is still
3596 * an unlikely race condition where the user has taken a
3597 * snapshot since we verified that this was the most recent.
3600 if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) {
3601 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3602 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3608 * For volumes, if the pre-rollback volsize matched the pre-
3609 * rollback reservation and the volsize has changed then set
3610 * the reservation property to the post-rollback volsize.
3611 * Make a new handle since the rollback closed the dataset.
3613 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3614 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3615 if ((err = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name))) {
3620 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3621 if (old_volsize != new_volsize)
3622 err = zfs_prop_set_int(zhp, resv_prop,
3631 * Iterate over all dependents for a given dataset. This includes both
3632 * hierarchical dependents (children) and data dependents (snapshots and
3633 * clones). The bulk of the processing occurs in get_dependents() in
3637 zfs_iter_dependents(zfs_handle_t *zhp, boolean_t allowrecursion,
3638 zfs_iter_f func, void *data)
3643 zfs_handle_t *child;
3646 if (get_dependents(zhp->zfs_hdl, allowrecursion, zhp->zfs_name,
3647 &dependents, &count) != 0)
3650 for (i = 0; i < count; i++) {
3651 if ((child = make_dataset_handle(zhp->zfs_hdl,
3652 dependents[i])) == NULL)
3655 if ((ret = func(child, data)) != 0)
3659 for (i = 0; i < count; i++)
3660 free(dependents[i]);
3667 * Renames the given dataset.
3670 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive)
3673 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3675 prop_changelist_t *cl = NULL;
3676 zfs_handle_t *zhrp = NULL;
3677 char *parentname = NULL;
3678 char parent[ZFS_MAXNAMELEN];
3679 libzfs_handle_t *hdl = zhp->zfs_hdl;
3682 /* if we have the same exact name, just return success */
3683 if (strcmp(zhp->zfs_name, target) == 0)
3686 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3687 "cannot rename to '%s'"), target);
3690 * Make sure the target name is valid
3692 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3693 if ((strchr(target, '@') == NULL) ||
3696 * Snapshot target name is abbreviated,
3697 * reconstruct full dataset name
3699 (void) strlcpy(parent, zhp->zfs_name,
3701 delim = strchr(parent, '@');
3702 if (strchr(target, '@') == NULL)
3706 (void) strlcat(parent, target, sizeof (parent));
3710 * Make sure we're renaming within the same dataset.
3712 delim = strchr(target, '@');
3713 if (strncmp(zhp->zfs_name, target, delim - target)
3714 != 0 || zhp->zfs_name[delim - target] != '@') {
3715 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3716 "snapshots must be part of same "
3718 return (zfs_error(hdl, EZFS_CROSSTARGET,
3722 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3723 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3727 "recursive rename must be a snapshot"));
3728 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3731 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3732 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3734 /* validate parents */
3735 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
3738 /* make sure we're in the same pool */
3739 verify((delim = strchr(target, '/')) != NULL);
3740 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
3741 zhp->zfs_name[delim - target] != '/') {
3742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3743 "datasets must be within same pool"));
3744 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
3747 /* new name cannot be a child of the current dataset name */
3748 if (is_descendant(zhp->zfs_name, target)) {
3749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3750 "New dataset name cannot be a descendant of "
3751 "current dataset name"));
3752 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3756 (void) snprintf(errbuf, sizeof (errbuf),
3757 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
3759 if (getzoneid() == GLOBAL_ZONEID &&
3760 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
3761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3762 "dataset is used in a non-global zone"));
3763 return (zfs_error(hdl, EZFS_ZONED, errbuf));
3767 struct destroydata dd;
3769 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
3770 if (parentname == NULL) {
3774 delim = strchr(parentname, '@');
3776 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
3782 dd.snapname = delim + 1;
3783 dd.gotone = B_FALSE;
3784 dd.closezhp = B_TRUE;
3786 /* We remove any zvol links prior to renaming them */
3787 ret = zfs_iter_filesystems(zhrp, zfs_check_snap_cb, &dd);
3792 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0)) == NULL)
3795 if (changelist_haszonedchild(cl)) {
3796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3797 "child dataset with inherited mountpoint is used "
3798 "in a non-global zone"));
3799 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
3804 if ((ret = changelist_prefix(cl)) != 0)
3808 if (ZFS_IS_VOLUME(zhp))
3809 zc.zc_objset_type = DMU_OST_ZVOL;
3811 zc.zc_objset_type = DMU_OST_ZFS;
3813 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3814 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
3816 zc.zc_cookie = recursive;
3818 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
3820 * if it was recursive, the one that actually failed will
3823 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3824 "cannot rename '%s'"), zc.zc_name);
3826 if (recursive && errno == EEXIST) {
3827 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3828 "a child dataset already has a snapshot "
3829 "with the new name"));
3830 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3832 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
3836 * On failure, we still want to remount any filesystems that
3837 * were previously mounted, so we don't alter the system state.
3840 struct createdata cd;
3842 /* only create links for datasets that had existed */
3843 cd.cd_snapname = delim + 1;
3844 cd.cd_ifexists = B_TRUE;
3845 (void) zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3848 (void) changelist_postfix(cl);
3852 struct createdata cd;
3854 /* only create links for datasets that had existed */
3855 cd.cd_snapname = strchr(target, '@') + 1;
3856 cd.cd_ifexists = B_TRUE;
3857 ret = zfs_iter_filesystems(zhrp, zfs_create_link_cb,
3860 changelist_rename(cl, zfs_get_name(zhp), target);
3861 ret = changelist_postfix(cl);
3873 changelist_free(cl);
3879 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3880 * and wait briefly for udev to create the /dev link.
3883 zvol_create_link(libzfs_handle_t *hdl, const char *dataset)
3885 return (zvol_create_link_common(hdl, dataset, B_FALSE));
3889 zvol_create_link_common(libzfs_handle_t *hdl, const char *dataset, int ifexists)
3891 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3892 char path[MAXPATHLEN];
3895 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3898 * Issue the appropriate ioctl.
3900 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) {
3904 * Silently ignore the case where the link already
3905 * exists. This allows 'zfs volinit' to be run multiple
3906 * times without errors.
3912 * Dataset does not exist in the kernel. If we
3913 * don't care (see zfs_rename), then ignore the
3923 return (zfs_standard_error_fmt(hdl, errno,
3924 dgettext(TEXT_DOMAIN, "cannot create device links "
3925 "for '%s'"), dataset));
3930 * Wait up to 10 seconds for udev to create the device.
3932 (void) snprintf(path, sizeof (path), "%s/%s", ZVOL_DIR, dataset);
3933 error = zpool_label_disk_wait(path, 10000);
3935 (void) printf(gettext("%s may not be immediately "
3936 "available\n"), path);
3942 * Remove a minor node for the given zvol and the associated /dev links.
3945 zvol_remove_link(libzfs_handle_t *hdl, const char *dataset)
3947 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3948 int timeout = 3000; /* in milliseconds */
3952 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
3955 * Due to concurrent updates by udev the device may be reported as
3956 * busy. In this case don't immediately fail. Instead briefly delay
3957 * and retry the ioctl() which is now likely to succeed. If unable
3958 * remove the link after timeout milliseconds return the failure.
3960 for (i = 0; i < timeout; i++) {
3961 error = ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc);
3962 if (error && errno == EBUSY) {
3974 * Silently ignore the case where the link no longer
3975 * exists, so that 'zfs volfini' can be run multiple
3976 * times without errors.
3981 return (zfs_standard_error_fmt(hdl, errno,
3982 dgettext(TEXT_DOMAIN, "cannot remove device "
3983 "links for '%s': %s"), dataset, strerror(errno)));
3991 zfs_get_user_props(zfs_handle_t *zhp)
3993 return (zhp->zfs_user_props);
3997 * This function is used by 'zfs list' to determine the exact set of columns to
3998 * display, and their maximum widths. This does two main things:
4000 * - If this is a list of all properties, then expand the list to include
4001 * all native properties, and set a flag so that for each dataset we look
4002 * for new unique user properties and add them to the list.
4004 * - For non fixed-width properties, keep track of the maximum width seen
4005 * so that we can size the column appropriately. If the user has
4006 * requested received property values, we also need to compute the width
4007 * of the RECEIVED column.
4010 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received)
4012 libzfs_handle_t *hdl = zhp->zfs_hdl;
4013 zprop_list_t *entry;
4014 zprop_list_t **last, **start;
4015 nvlist_t *userprops, *propval;
4018 char buf[ZFS_MAXPROPLEN];
4020 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4023 userprops = zfs_get_user_props(zhp);
4026 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4028 * Go through and add any user properties as necessary. We
4029 * start by incrementing our list pointer to the first
4030 * non-native property.
4033 while (*start != NULL) {
4034 if ((*start)->pl_prop == ZPROP_INVAL)
4036 start = &(*start)->pl_next;
4040 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4042 * See if we've already found this property in our list.
4044 for (last = start; *last != NULL;
4045 last = &(*last)->pl_next) {
4046 if (strcmp((*last)->pl_user_prop,
4047 nvpair_name(elem)) == 0)
4051 if (*last == NULL) {
4052 if ((entry = zfs_alloc(hdl,
4053 sizeof (zprop_list_t))) == NULL ||
4054 ((entry->pl_user_prop = zfs_strdup(hdl,
4055 nvpair_name(elem)))) == NULL) {
4060 entry->pl_prop = ZPROP_INVAL;
4061 entry->pl_width = strlen(nvpair_name(elem));
4062 entry->pl_all = B_TRUE;
4069 * Now go through and check the width of any non-fixed columns
4071 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4072 if (entry->pl_fixed)
4075 if (entry->pl_prop != ZPROP_INVAL) {
4076 if (zfs_prop_get(zhp, entry->pl_prop,
4077 buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) {
4078 if (strlen(buf) > entry->pl_width)
4079 entry->pl_width = strlen(buf);
4081 if (received && zfs_prop_get_recvd(zhp,
4082 zfs_prop_to_name(entry->pl_prop),
4083 buf, sizeof (buf), B_FALSE) == 0)
4084 if (strlen(buf) > entry->pl_recvd_width)
4085 entry->pl_recvd_width = strlen(buf);
4087 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4089 verify(nvlist_lookup_string(propval,
4090 ZPROP_VALUE, &strval) == 0);
4091 if (strlen(strval) > entry->pl_width)
4092 entry->pl_width = strlen(strval);
4094 if (received && zfs_prop_get_recvd(zhp,
4095 entry->pl_user_prop,
4096 buf, sizeof (buf), B_FALSE) == 0)
4097 if (strlen(buf) > entry->pl_recvd_width)
4098 entry->pl_recvd_width = strlen(buf);
4106 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4111 * Keep a reference to the props-table against which we prune the
4114 zhp->zfs_props_table = props;
4116 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4119 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4120 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4123 * User properties will result in ZPROP_INVAL, and since we
4124 * only know how to prune standard ZFS properties, we always
4125 * leave these in the list. This can also happen if we
4126 * encounter an unknown DSL property (when running older
4127 * software, for example).
4129 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4130 (void) nvlist_remove(zhp->zfs_props,
4131 nvpair_name(curr), nvpair_type(curr));
4137 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4138 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4140 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4141 nvlist_t *nvlist = NULL;
4144 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4145 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4146 zc.zc_cookie = (uint64_t)cmd;
4148 if (cmd == ZFS_SMB_ACL_RENAME) {
4149 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4150 (void) no_memory(hdl);
4156 case ZFS_SMB_ACL_ADD:
4157 case ZFS_SMB_ACL_REMOVE:
4158 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4160 case ZFS_SMB_ACL_RENAME:
4161 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4163 (void) no_memory(hdl);
4166 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4168 (void) no_memory(hdl);
4171 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4172 nvlist_free(nvlist);
4176 case ZFS_SMB_ACL_PURGE:
4181 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4183 nvlist_free(nvlist);
4188 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4189 char *path, char *resource)
4191 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4196 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4197 char *path, char *resource)
4199 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4204 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4206 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4211 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4212 char *oldname, char *newname)
4214 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4219 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4220 zfs_userspace_cb_t func, void *arg)
4222 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4224 zfs_useracct_t buf[100];
4226 (void) strncpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4228 zc.zc_objset_type = type;
4229 zc.zc_nvlist_dst = (uintptr_t)buf;
4233 zfs_useracct_t *zua = buf;
4235 zc.zc_nvlist_dst_size = sizeof (buf);
4236 error = ioctl(zhp->zfs_hdl->libzfs_fd,
4237 ZFS_IOC_USERSPACE_MANY, &zc);
4238 if (error || zc.zc_nvlist_dst_size == 0)
4241 while (zc.zc_nvlist_dst_size > 0) {
4242 error = func(arg, zua->zu_domain, zua->zu_rid,
4247 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4255 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4256 boolean_t recursive, boolean_t temphold, boolean_t enoent_ok,
4257 int cleanup_fd, uint64_t dsobj, uint64_t createtxg)
4259 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4260 libzfs_handle_t *hdl = zhp->zfs_hdl;
4262 ASSERT(!recursive || dsobj == 0);
4264 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4265 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4266 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4267 >= sizeof (zc.zc_string))
4268 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4269 zc.zc_cookie = recursive;
4270 zc.zc_temphold = temphold;
4271 zc.zc_cleanup_fd = cleanup_fd;
4272 zc.zc_sendobj = dsobj;
4273 zc.zc_createtxg = createtxg;
4275 if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) {
4276 char errbuf[ZFS_MAXNAMELEN+32];
4279 * if it was recursive, the one that actually failed will be in
4282 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4283 "cannot hold '%s@%s'"), zc.zc_name, snapname);
4287 * Temporary tags wind up having the ds object id
4288 * prepended. So even if we passed the length check
4289 * above, it's still possible for the tag to wind
4290 * up being slightly too long.
4292 return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf));
4294 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4295 "pool must be upgraded"));
4296 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4298 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4300 return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf));
4306 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4314 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4315 boolean_t recursive)
4317 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4318 libzfs_handle_t *hdl = zhp->zfs_hdl;
4320 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4321 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
4322 if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string))
4323 >= sizeof (zc.zc_string))
4324 return (zfs_error(hdl, EZFS_TAGTOOLONG, tag));
4325 zc.zc_cookie = recursive;
4327 if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) {
4328 char errbuf[ZFS_MAXNAMELEN+32];
4331 * if it was recursive, the one that actually failed will be in
4334 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4335 "cannot release '%s' from '%s@%s'"), tag, zc.zc_name,
4339 return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf));
4341 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4342 "pool must be upgraded"));
4343 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
4345 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4347 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4355 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4357 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4358 libzfs_handle_t *hdl = zhp->zfs_hdl;
4362 char errbuf[ZFS_MAXNAMELEN+32];
4364 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4365 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4369 nvbuf = malloc(nvsz);
4370 if (nvbuf == NULL) {
4371 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4375 zc.zc_nvlist_dst_size = nvsz;
4376 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4378 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4380 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4381 (void) snprintf(errbuf, sizeof (errbuf),
4382 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4387 nvsz = zc.zc_nvlist_dst_size;
4391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4392 "pool must be upgraded"));
4393 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4396 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4399 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4402 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4407 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4409 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4410 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4412 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4422 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4424 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4425 libzfs_handle_t *hdl = zhp->zfs_hdl;
4427 char errbuf[ZFS_MAXNAMELEN+32];
4431 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4432 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4434 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4437 nvbuf = malloc(nvsz);
4439 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4442 zc.zc_nvlist_src_size = nvsz;
4443 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4444 zc.zc_perm_action = un;
4446 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4448 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4449 (void) snprintf(errbuf, sizeof (errbuf),
4450 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4455 "pool must be upgraded"));
4456 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4459 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4462 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4465 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4476 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4478 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
4479 libzfs_handle_t *hdl = zhp->zfs_hdl;
4483 char errbuf[ZFS_MAXNAMELEN+32];
4485 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4489 nvbuf = malloc(nvsz);
4490 if (nvbuf == NULL) {
4491 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4495 zc.zc_nvlist_dst_size = nvsz;
4496 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4498 (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN);
4500 if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) {
4501 (void) snprintf(errbuf, sizeof (errbuf),
4502 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4507 nvsz = zc.zc_nvlist_dst_size;
4511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4512 "pool must be upgraded"));
4513 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4516 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4519 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4522 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4527 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4529 (void) snprintf(errbuf, sizeof (errbuf),
4530 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4532 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4542 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4545 uint64_t nblocks, volblocksize;
4549 if (nvlist_lookup_string(props,
4550 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4551 ncopies = atoi(strval);
4554 if (nvlist_lookup_uint64(props,
4555 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4556 &volblocksize) != 0)
4557 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4558 nblocks = volsize/volblocksize;
4559 /* start with metadnode L0-L6 */
4561 /* calculate number of indirects */
4562 while (nblocks > 1) {
4563 nblocks += DNODES_PER_LEVEL - 1;
4564 nblocks /= DNODES_PER_LEVEL;
4567 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4570 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4571 * compressed, but in practice they compress down to about
4574 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;