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) 2011 by Delphix. All rights reserved.
38 #include <sys/mntent.h>
39 #include <sys/mount.h>
48 #include <directory.h>
49 #endif /* HAVE_IDMAP */
51 #include <sys/dnode.h>
56 #include "zfs_namecheck.h"
58 #include "libzfs_impl.h"
59 #include "zfs_deleg.h"
61 static int zvol_create_link_common(libzfs_handle_t
*, const char *, int);
62 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
63 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
66 * Given a single type (not a mask of types), return the type in a human
70 zfs_type_to_name(zfs_type_t type
)
73 case ZFS_TYPE_FILESYSTEM
:
74 return (dgettext(TEXT_DOMAIN
, "filesystem"));
75 case ZFS_TYPE_SNAPSHOT
:
76 return (dgettext(TEXT_DOMAIN
, "snapshot"));
78 return (dgettext(TEXT_DOMAIN
, "volume"));
87 * Validate a ZFS path. This is used even before trying to open the dataset, to
88 * provide a more meaningful error message. We call zfs_error_aux() to
89 * explain exactly why the name was not valid.
92 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
98 if (dataset_namecheck(path
, &why
, &what
) != 0) {
101 case NAME_ERR_TOOLONG
:
102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
103 "name is too long"));
106 case NAME_ERR_LEADING_SLASH
:
107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
108 "leading slash in name"));
111 case NAME_ERR_EMPTY_COMPONENT
:
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "empty component in name"));
116 case NAME_ERR_TRAILING_SLASH
:
117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
118 "trailing slash in name"));
121 case NAME_ERR_INVALCHAR
:
123 dgettext(TEXT_DOMAIN
, "invalid character "
124 "'%c' in name"), what
);
127 case NAME_ERR_MULTIPLE_AT
:
128 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
129 "multiple '@' delimiters in name"));
132 case NAME_ERR_NOLETTER
:
133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
134 "pool doesn't begin with a letter"));
137 case NAME_ERR_RESERVED
:
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "name is reserved"));
142 case NAME_ERR_DISKLIKE
:
143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
144 "reserved disk name"));
154 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
157 "snapshot delimiter '@' in filesystem name"));
161 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
164 "missing '@' delimiter in snapshot name"));
168 if (modifying
&& strchr(path
, '%') != NULL
) {
170 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
171 "invalid character %c in name"), '%');
179 zfs_name_valid(const char *name
, zfs_type_t type
)
181 if (type
== ZFS_TYPE_POOL
)
182 return (zpool_name_valid(NULL
, B_FALSE
, name
));
183 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
187 * This function takes the raw DSL properties, and filters out the user-defined
188 * properties into a separate nvlist.
191 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
193 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
198 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
199 (void) no_memory(hdl
);
204 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
205 if (!zfs_prop_user(nvpair_name(elem
)))
208 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
209 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
211 (void) no_memory(hdl
);
219 static zpool_handle_t
*
220 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
222 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
225 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
226 if (hdl
->libzfs_pool_handles
!= NULL
)
227 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
228 hdl
->libzfs_pool_handles
= zph
;
233 static zpool_handle_t
*
234 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
236 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
237 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
239 while ((zph
!= NULL
) &&
240 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
241 zph
= zph
->zpool_next
;
246 * Returns a handle to the pool that contains the provided dataset.
247 * If a handle to that pool already exists then that handle is returned.
248 * Otherwise, a new handle is created and added to the list of handles.
250 static zpool_handle_t
*
251 zpool_handle(zfs_handle_t
*zhp
)
257 len
= strcspn(zhp
->zfs_name
, "/@") + 1;
258 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
259 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
261 zph
= zpool_find_handle(zhp
, pool_name
, len
);
263 zph
= zpool_add_handle(zhp
, pool_name
);
270 zpool_free_handles(libzfs_handle_t
*hdl
)
272 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
274 while (zph
!= NULL
) {
275 next
= zph
->zpool_next
;
279 hdl
->libzfs_pool_handles
= NULL
;
283 * Utility function to gather stats (objset and zpl) for the given object.
286 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
288 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
290 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
292 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
293 if (errno
== ENOMEM
) {
294 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
305 * Utility function to get the received properties of the given object.
308 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
310 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
311 nvlist_t
*recvdprops
;
312 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
315 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
318 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
320 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
321 if (errno
== ENOMEM
) {
322 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
326 zcmd_free_nvlists(&zc
);
331 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
332 zcmd_free_nvlists(&zc
);
336 nvlist_free(zhp
->zfs_recvd_props
);
337 zhp
->zfs_recvd_props
= recvdprops
;
343 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
345 nvlist_t
*allprops
, *userprops
;
347 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
349 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
354 * XXX Why do we store the user props separately, in addition to
355 * storing them in zfs_props?
357 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
358 nvlist_free(allprops
);
362 nvlist_free(zhp
->zfs_props
);
363 nvlist_free(zhp
->zfs_user_props
);
365 zhp
->zfs_props
= allprops
;
366 zhp
->zfs_user_props
= userprops
;
372 get_stats(zfs_handle_t
*zhp
)
375 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
377 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
379 if (get_stats_ioctl(zhp
, &zc
) != 0)
381 else if (put_stats_zhdl(zhp
, &zc
) != 0)
383 zcmd_free_nvlists(&zc
);
388 * Refresh the properties currently stored in the handle.
391 zfs_refresh_properties(zfs_handle_t
*zhp
)
393 (void) get_stats(zhp
);
397 * Makes a handle from the given dataset name. Used by zfs_open() and
398 * zfs_iter_* to create child handles on the fly.
401 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
403 if (put_stats_zhdl(zhp
, zc
) != 0)
407 * We've managed to open the dataset and gather statistics. Determine
408 * the high-level type.
410 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
411 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
412 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
413 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
417 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
418 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
419 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
420 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
421 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
422 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
424 abort(); /* we should never see any other types */
426 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
433 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
435 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
437 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
443 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
444 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
448 if (get_stats_ioctl(zhp
, &zc
) == -1) {
449 zcmd_free_nvlists(&zc
);
453 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
457 zcmd_free_nvlists(&zc
);
461 static zfs_handle_t
*
462 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
464 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
470 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
471 if (make_dataset_handle_common(zhp
, zc
) == -1) {
479 * Opens the given snapshot, filesystem, or volume. The 'types'
480 * argument is a mask of acceptable types. The function will print an
481 * appropriate error message and return NULL if it can't be opened.
484 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
489 (void) snprintf(errbuf
, sizeof (errbuf
),
490 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
493 * Validate the name before we even try to open it.
495 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
496 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
497 "invalid dataset name"));
498 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
503 * Try to get stats for the dataset, which will tell us if it exists.
506 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
507 (void) zfs_standard_error(hdl
, errno
, errbuf
);
511 if (!(types
& zhp
->zfs_type
)) {
512 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
521 * Release a ZFS handle. Nothing to do but free the associated memory.
524 zfs_close(zfs_handle_t
*zhp
)
526 if (zhp
->zfs_mntopts
)
527 free(zhp
->zfs_mntopts
);
528 nvlist_free(zhp
->zfs_props
);
529 nvlist_free(zhp
->zfs_user_props
);
530 nvlist_free(zhp
->zfs_recvd_props
);
534 typedef struct mnttab_node
{
535 struct mnttab mtn_mt
;
540 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
542 const mnttab_node_t
*mtn1
= arg1
;
543 const mnttab_node_t
*mtn2
= arg2
;
546 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
550 return (rv
> 0 ? 1 : -1);
554 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
556 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
557 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
558 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
562 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
566 rewind(hdl
->libzfs_mnttab
);
567 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
570 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
572 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
573 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
574 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
575 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
576 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
577 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
582 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
587 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
588 free(mtn
->mtn_mt
.mnt_special
);
589 free(mtn
->mtn_mt
.mnt_mountp
);
590 free(mtn
->mtn_mt
.mnt_fstype
);
591 free(mtn
->mtn_mt
.mnt_mntopts
);
594 avl_destroy(&hdl
->libzfs_mnttab_cache
);
598 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
600 hdl
->libzfs_mnttab_enable
= enable
;
604 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
605 struct mnttab
*entry
)
610 if (!hdl
->libzfs_mnttab_enable
) {
611 struct mnttab srch
= { 0 };
613 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
614 libzfs_mnttab_fini(hdl
);
615 rewind(hdl
->libzfs_mnttab
);
616 srch
.mnt_special
= (char *)fsname
;
617 srch
.mnt_fstype
= MNTTYPE_ZFS
;
618 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
624 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
625 libzfs_mnttab_update(hdl
);
627 find
.mtn_mt
.mnt_special
= (char *)fsname
;
628 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
630 *entry
= mtn
->mtn_mt
;
637 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
638 const char *mountp
, const char *mntopts
)
642 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
644 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
645 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
646 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
647 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
648 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
649 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
653 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
658 find
.mtn_mt
.mnt_special
= (char *)fsname
;
659 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
660 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
661 free(ret
->mtn_mt
.mnt_special
);
662 free(ret
->mtn_mt
.mnt_mountp
);
663 free(ret
->mtn_mt
.mnt_fstype
);
664 free(ret
->mtn_mt
.mnt_mntopts
);
670 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
672 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
674 if (zpool_handle
== NULL
)
677 *spa_version
= zpool_get_prop_int(zpool_handle
,
678 ZPOOL_PROP_VERSION
, NULL
);
683 * The choice of reservation property depends on the SPA version.
686 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
690 if (zfs_spa_version(zhp
, &spa_version
) < 0)
693 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
694 *resv_prop
= ZFS_PROP_REFRESERVATION
;
696 *resv_prop
= ZFS_PROP_RESERVATION
;
702 * Given an nvlist of properties to set, validates that they are correct, and
703 * parses any numeric properties (index, boolean, etc) if they are specified as
707 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
708 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
715 int chosen_normal
= -1;
718 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
719 (void) no_memory(hdl
);
724 * Make sure this property is valid and applies to this type.
728 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
729 const char *propname
= nvpair_name(elem
);
731 prop
= zfs_name_to_prop(propname
);
732 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
734 * This is a user property: make sure it's a
735 * string, and that it's less than ZAP_MAXNAMELEN.
737 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
738 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
739 "'%s' must be a string"), propname
);
740 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
744 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
745 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
746 "property name '%s' is too long"),
748 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
752 (void) nvpair_value_string(elem
, &strval
);
753 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
754 (void) no_memory(hdl
);
761 * Currently, only user properties can be modified on
764 if (type
== ZFS_TYPE_SNAPSHOT
) {
765 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
766 "this property can not be modified for snapshots"));
767 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
771 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
772 zfs_userquota_prop_t uqtype
;
773 char newpropname
[128];
778 if (userquota_propname_decode(propname
, zoned
,
779 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
781 dgettext(TEXT_DOMAIN
,
782 "'%s' has an invalid user/group name"),
784 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
788 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
789 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
791 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
793 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
798 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
799 (void) nvpair_value_string(elem
, &strval
);
800 if (strcmp(strval
, "none") == 0) {
802 } else if (zfs_nicestrtonum(hdl
,
803 strval
, &intval
) != 0) {
804 (void) zfs_error(hdl
,
805 EZFS_BADPROP
, errbuf
);
808 } else if (nvpair_type(elem
) ==
810 (void) nvpair_value_uint64(elem
, &intval
);
812 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
813 "use 'none' to disable "
814 "userquota/groupquota"));
818 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
819 "'%s' must be a number"), propname
);
820 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
825 * Encode the prop name as
826 * userquota@<hex-rid>-domain, to make it easy
827 * for the kernel to decode.
829 (void) snprintf(newpropname
, sizeof (newpropname
),
830 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
831 (longlong_t
)rid
, domain
);
835 if (nvlist_add_uint64_array(ret
, newpropname
,
837 (void) no_memory(hdl
);
843 if (prop
== ZPROP_INVAL
) {
844 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
845 "invalid property '%s'"), propname
);
846 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
850 if (!zfs_prop_valid_for_type(prop
, type
)) {
852 dgettext(TEXT_DOMAIN
, "'%s' does not "
853 "apply to datasets of this type"), propname
);
854 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
858 if (zfs_prop_readonly(prop
) &&
859 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
861 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
863 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
867 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
868 &strval
, &intval
, errbuf
) != 0)
872 * Perform some additional checks for specific properties.
875 case ZFS_PROP_VERSION
:
881 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
882 if (intval
< version
) {
883 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
884 "Can not downgrade; already at version %u"),
886 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
892 case ZFS_PROP_RECORDSIZE
:
893 case ZFS_PROP_VOLBLOCKSIZE
:
894 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
895 if (intval
< SPA_MINBLOCKSIZE
||
896 intval
> SPA_MAXBLOCKSIZE
|| !ISP2(intval
)) {
897 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
898 "'%s' must be power of 2 from %u "
900 (uint_t
)SPA_MINBLOCKSIZE
,
901 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
902 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
907 case ZFS_PROP_MLSLABEL
:
911 * Verify the mlslabel string and convert to
912 * internal hex label string.
916 char *hex
= NULL
; /* internal label string */
918 /* Default value is already OK. */
919 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
922 /* Verify the label can be converted to binary form */
923 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
924 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
925 L_NO_CORRECTION
, NULL
) == -1)) {
929 /* Now translate to hex internal label string */
930 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
936 m_label_free(new_sl
);
938 /* If string is already in internal form, we're done. */
939 if (strcmp(strval
, hex
) == 0) {
944 /* Replace the label string with the internal form. */
945 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
947 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
954 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
955 "invalid mlslabel '%s'"), strval
);
956 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
957 m_label_free(new_sl
); /* OK if null */
960 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
961 "mlslabels are unsupported"));
962 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
964 #endif /* HAVE_MLSLABEL */
967 case ZFS_PROP_MOUNTPOINT
:
971 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
972 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
975 if (mountpoint_namecheck(strval
, &why
)) {
977 case NAME_ERR_LEADING_SLASH
:
979 dgettext(TEXT_DOMAIN
,
980 "'%s' must be an absolute path, "
981 "'none', or 'legacy'"), propname
);
983 case NAME_ERR_TOOLONG
:
985 dgettext(TEXT_DOMAIN
,
986 "component of '%s' is too long"),
992 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
999 case ZFS_PROP_SHARESMB
:
1000 case ZFS_PROP_SHARENFS
:
1002 * For the mountpoint and sharenfs or sharesmb
1003 * properties, check if it can be set in a
1004 * global/non-global zone based on
1005 * the zoned property value:
1007 * global zone non-global zone
1008 * --------------------------------------------------
1009 * zoned=on mountpoint (no) mountpoint (yes)
1010 * sharenfs (no) sharenfs (no)
1011 * sharesmb (no) sharesmb (no)
1013 * zoned=off mountpoint (yes) N/A
1018 if (getzoneid() == GLOBAL_ZONEID
) {
1019 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1020 "'%s' cannot be set on "
1021 "dataset in a non-global zone"),
1023 (void) zfs_error(hdl
, EZFS_ZONED
,
1026 } else if (prop
== ZFS_PROP_SHARENFS
||
1027 prop
== ZFS_PROP_SHARESMB
) {
1028 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1029 "'%s' cannot be set in "
1030 "a non-global zone"), propname
);
1031 (void) zfs_error(hdl
, EZFS_ZONED
,
1035 } else if (getzoneid() != GLOBAL_ZONEID
) {
1037 * If zoned property is 'off', this must be in
1038 * a global zone. If not, something is wrong.
1040 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1041 "'%s' cannot be set while dataset "
1042 "'zoned' property is set"), propname
);
1043 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1048 * At this point, it is legitimate to set the
1049 * property. Now we want to make sure that the
1050 * property value is valid if it is sharenfs.
1052 if ((prop
== ZFS_PROP_SHARENFS
||
1053 prop
== ZFS_PROP_SHARESMB
) &&
1054 strcmp(strval
, "on") != 0 &&
1055 strcmp(strval
, "off") != 0) {
1056 zfs_share_proto_t proto
;
1058 if (prop
== ZFS_PROP_SHARESMB
)
1064 * Must be an valid sharing protocol
1065 * option string so init the libshare
1066 * in order to enable the parser and
1067 * then parse the options. We use the
1068 * control API since we don't care about
1069 * the current configuration and don't
1070 * want the overhead of loading it
1071 * until we actually do something.
1074 if (zfs_init_libshare(hdl
,
1075 SA_INIT_CONTROL_API
) != SA_OK
) {
1077 * An error occurred so we can't do
1080 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1081 "'%s' cannot be set: problem "
1082 "in share initialization"),
1084 (void) zfs_error(hdl
, EZFS_BADPROP
,
1089 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1091 * There was an error in parsing so
1092 * deal with it by issuing an error
1093 * message and leaving after
1094 * uninitializing the the libshare
1097 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1098 "'%s' cannot be set to invalid "
1099 "options"), propname
);
1100 (void) zfs_error(hdl
, EZFS_BADPROP
,
1102 zfs_uninit_libshare(hdl
);
1105 zfs_uninit_libshare(hdl
);
1109 case ZFS_PROP_UTF8ONLY
:
1110 chosen_utf
= (int)intval
;
1112 case ZFS_PROP_NORMALIZE
:
1113 chosen_normal
= (int)intval
;
1120 * For changes to existing volumes, we have some additional
1121 * checks to enforce.
1123 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1124 uint64_t volsize
= zfs_prop_get_int(zhp
,
1126 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1127 ZFS_PROP_VOLBLOCKSIZE
);
1131 case ZFS_PROP_RESERVATION
:
1132 case ZFS_PROP_REFRESERVATION
:
1133 if (intval
> volsize
) {
1134 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1135 "'%s' is greater than current "
1136 "volume size"), propname
);
1137 (void) zfs_error(hdl
, EZFS_BADPROP
,
1143 case ZFS_PROP_VOLSIZE
:
1144 if (intval
% blocksize
!= 0) {
1145 zfs_nicenum(blocksize
, buf
,
1147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1148 "'%s' must be a multiple of "
1149 "volume block size (%s)"),
1151 (void) zfs_error(hdl
, EZFS_BADPROP
,
1157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1158 "'%s' cannot be zero"),
1160 (void) zfs_error(hdl
, EZFS_BADPROP
,
1172 * If normalization was chosen, but no UTF8 choice was made,
1173 * enforce rejection of non-UTF8 names.
1175 * If normalization was chosen, but rejecting non-UTF8 names
1176 * was explicitly not chosen, it is an error.
1178 if (chosen_normal
> 0 && chosen_utf
< 0) {
1179 if (nvlist_add_uint64(ret
,
1180 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1181 (void) no_memory(hdl
);
1184 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1185 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1186 "'%s' must be set 'on' if normalization chosen"),
1187 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1188 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1199 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1201 uint64_t old_volsize
;
1202 uint64_t new_volsize
;
1203 uint64_t old_reservation
;
1204 uint64_t new_reservation
;
1205 zfs_prop_t resv_prop
;
1208 * If this is an existing volume, and someone is setting the volsize,
1209 * make sure that it matches the reservation, or add it if necessary.
1211 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1212 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1214 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1215 if ((zvol_volsize_to_reservation(old_volsize
, zhp
->zfs_props
) !=
1216 old_reservation
) || nvlist_lookup_uint64(nvl
,
1217 zfs_prop_to_name(resv_prop
), &new_reservation
) != ENOENT
) {
1220 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1223 new_reservation
= zvol_volsize_to_reservation(new_volsize
,
1225 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1226 new_reservation
) != 0) {
1227 (void) no_memory(zhp
->zfs_hdl
);
1234 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1241 * For quotas and reservations, ENOSPC indicates
1242 * something different; setting a quota or reservation
1243 * doesn't use any disk space.
1246 case ZFS_PROP_QUOTA
:
1247 case ZFS_PROP_REFQUOTA
:
1248 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1249 "size is less than current used or "
1251 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1254 case ZFS_PROP_RESERVATION
:
1255 case ZFS_PROP_REFRESERVATION
:
1256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1257 "size is greater than available space"));
1258 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1262 (void) zfs_standard_error(hdl
, err
, errbuf
);
1268 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1272 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1276 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1277 "pool and or dataset must be upgraded to set this "
1278 "property or value"));
1279 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1283 if (prop
== ZFS_PROP_COMPRESSION
) {
1284 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1285 "property setting is not allowed on "
1286 "bootable datasets"));
1287 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1289 (void) zfs_standard_error(hdl
, err
, errbuf
);
1294 if (prop
== ZPROP_INVAL
) {
1295 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1297 (void) zfs_standard_error(hdl
, err
, errbuf
);
1303 * This platform can't address a volume this big.
1306 if (prop
== ZFS_PROP_VOLSIZE
) {
1307 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1313 (void) zfs_standard_error(hdl
, err
, errbuf
);
1318 zfs_is_namespace_prop(zfs_prop_t prop
)
1322 case ZFS_PROP_ATIME
:
1323 case ZFS_PROP_DEVICES
:
1325 case ZFS_PROP_SETUID
:
1326 case ZFS_PROP_READONLY
:
1327 case ZFS_PROP_XATTR
:
1328 case ZFS_PROP_NBMAND
:
1337 * Given a property name and value, set the property for the given dataset.
1340 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1342 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1344 prop_changelist_t
*cl
= NULL
;
1346 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1347 nvlist_t
*nvl
= NULL
, *realprops
;
1349 boolean_t do_prefix
;
1353 (void) snprintf(errbuf
, sizeof (errbuf
),
1354 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1357 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1358 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1359 (void) no_memory(hdl
);
1363 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1364 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1370 prop
= zfs_name_to_prop(propname
);
1372 if (prop
== ZFS_PROP_VOLSIZE
) {
1373 if ((added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1)
1377 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1380 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1381 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1382 "child dataset with inherited mountpoint is used "
1383 "in a non-global zone"));
1384 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1389 * If the dataset's canmount property is being set to noauto,
1390 * then we want to prevent unmounting & remounting it.
1392 do_prefix
= !((prop
== ZFS_PROP_CANMOUNT
) &&
1393 (zprop_string_to_index(prop
, propval
, &idx
,
1394 ZFS_TYPE_DATASET
) == 0) && (idx
== ZFS_CANMOUNT_NOAUTO
));
1396 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1400 * Execute the corresponding ioctl() to set this property.
1402 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1404 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1407 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1410 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1411 if (added_resv
&& errno
== ENOSPC
) {
1412 /* clean up the volsize property we tried to set */
1413 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1416 zcmd_free_nvlists(&zc
);
1417 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1419 if (nvlist_add_uint64(nvl
,
1420 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1423 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1425 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1429 ret
= changelist_postfix(cl
);
1433 * Refresh the statistics so the new property
1434 * value is reflected.
1436 (void) get_stats(zhp
);
1439 * Remount the filesystem to propagate the change
1440 * if one of the options handled by the generic
1441 * Linux namespace layer has been modified.
1443 if (zfs_is_namespace_prop(prop
) &&
1444 zfs_is_mounted(zhp
, NULL
))
1445 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1451 zcmd_free_nvlists(&zc
);
1453 changelist_free(cl
);
1458 * Given a property, inherit the value from the parent dataset, or if received
1459 * is TRUE, revert to the received value, if any.
1462 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1464 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1466 prop_changelist_t
*cl
;
1467 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1471 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1472 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1474 zc
.zc_cookie
= received
;
1475 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1477 * For user properties, the amount of work we have to do is very
1478 * small, so just do it here.
1480 if (!zfs_prop_user(propname
)) {
1481 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1482 "invalid property"));
1483 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1486 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1487 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1489 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1490 return (zfs_standard_error(hdl
, errno
, errbuf
));
1496 * Verify that this property is inheritable.
1498 if (zfs_prop_readonly(prop
))
1499 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1501 if (!zfs_prop_inheritable(prop
) && !received
)
1502 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1505 * Check to see if the value applies to this type
1507 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1508 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1511 * Normalize the name, to get rid of shorthand abbreviations.
1513 propname
= zfs_prop_to_name(prop
);
1514 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1515 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1517 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1518 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1519 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1520 "dataset is used in a non-global zone"));
1521 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1525 * Determine datasets which will be affected by this change, if any.
1527 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1530 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1531 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1532 "child dataset with inherited mountpoint is used "
1533 "in a non-global zone"));
1534 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1538 if ((ret
= changelist_prefix(cl
)) != 0)
1541 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1542 return (zfs_standard_error(hdl
, errno
, errbuf
));
1545 if ((ret
= changelist_postfix(cl
)) != 0)
1549 * Refresh the statistics so the new property is reflected.
1551 (void) get_stats(zhp
);
1555 changelist_free(cl
);
1560 * True DSL properties are stored in an nvlist. The following two functions
1561 * extract them appropriately.
1564 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1570 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1571 zfs_prop_to_name(prop
), &nv
) == 0) {
1572 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1573 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1575 verify(!zhp
->zfs_props_table
||
1576 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1577 value
= zfs_prop_default_numeric(prop
);
1585 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1591 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1592 zfs_prop_to_name(prop
), &nv
) == 0) {
1593 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1594 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1596 verify(!zhp
->zfs_props_table
||
1597 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1598 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1607 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1609 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1613 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1615 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1616 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1620 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1622 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1627 * Internal function for getting a numeric property. Both zfs_prop_get() and
1628 * zfs_prop_get_int() are built using this interface.
1630 * Certain properties can be overridden using 'mount -o'. In this case, scan
1631 * the contents of the /etc/mtab entry, searching for the appropriate options.
1632 * If they differ from the on-disk values, report the current values and mark
1633 * the source "temporary".
1636 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1637 char **source
, uint64_t *val
)
1639 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1640 nvlist_t
*zplprops
= NULL
;
1642 char *mntopt_on
= NULL
;
1643 char *mntopt_off
= NULL
;
1644 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1649 case ZFS_PROP_ATIME
:
1650 mntopt_on
= MNTOPT_ATIME
;
1651 mntopt_off
= MNTOPT_NOATIME
;
1654 case ZFS_PROP_DEVICES
:
1655 mntopt_on
= MNTOPT_DEVICES
;
1656 mntopt_off
= MNTOPT_NODEVICES
;
1660 mntopt_on
= MNTOPT_EXEC
;
1661 mntopt_off
= MNTOPT_NOEXEC
;
1664 case ZFS_PROP_READONLY
:
1665 mntopt_on
= MNTOPT_RO
;
1666 mntopt_off
= MNTOPT_RW
;
1669 case ZFS_PROP_SETUID
:
1670 mntopt_on
= MNTOPT_SETUID
;
1671 mntopt_off
= MNTOPT_NOSETUID
;
1674 case ZFS_PROP_XATTR
:
1675 mntopt_on
= MNTOPT_XATTR
;
1676 mntopt_off
= MNTOPT_NOXATTR
;
1679 case ZFS_PROP_NBMAND
:
1680 mntopt_on
= MNTOPT_NBMAND
;
1681 mntopt_off
= MNTOPT_NONBMAND
;
1688 * Because looking up the mount options is potentially expensive
1689 * (iterating over all of /etc/mtab), we defer its calculation until
1690 * we're looking up a property which requires its presence.
1692 if (!zhp
->zfs_mntcheck
&&
1693 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1694 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1695 struct mnttab entry
;
1697 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1698 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1700 if (zhp
->zfs_mntopts
== NULL
)
1704 zhp
->zfs_mntcheck
= B_TRUE
;
1707 if (zhp
->zfs_mntopts
== NULL
)
1708 mnt
.mnt_mntopts
= "";
1710 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
1713 case ZFS_PROP_ATIME
:
1714 case ZFS_PROP_DEVICES
:
1716 case ZFS_PROP_READONLY
:
1717 case ZFS_PROP_SETUID
:
1718 case ZFS_PROP_XATTR
:
1719 case ZFS_PROP_NBMAND
:
1720 *val
= getprop_uint64(zhp
, prop
, source
);
1725 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
1728 *src
= ZPROP_SRC_TEMPORARY
;
1729 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
1732 *src
= ZPROP_SRC_TEMPORARY
;
1736 case ZFS_PROP_CANMOUNT
:
1737 case ZFS_PROP_VOLSIZE
:
1738 case ZFS_PROP_QUOTA
:
1739 case ZFS_PROP_REFQUOTA
:
1740 case ZFS_PROP_RESERVATION
:
1741 case ZFS_PROP_REFRESERVATION
:
1742 *val
= getprop_uint64(zhp
, prop
, source
);
1744 if (*source
== NULL
) {
1745 /* not default, must be local */
1746 *source
= zhp
->zfs_name
;
1750 case ZFS_PROP_MOUNTED
:
1751 *val
= (zhp
->zfs_mntopts
!= NULL
);
1754 case ZFS_PROP_NUMCLONES
:
1755 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
1758 case ZFS_PROP_VERSION
:
1759 case ZFS_PROP_NORMALIZE
:
1760 case ZFS_PROP_UTF8ONLY
:
1762 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
1763 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1765 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1766 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
1767 zcmd_free_nvlists(&zc
);
1770 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
1771 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
1773 zcmd_free_nvlists(&zc
);
1777 nvlist_free(zplprops
);
1778 zcmd_free_nvlists(&zc
);
1782 switch (zfs_prop_get_type(prop
)) {
1783 case PROP_TYPE_NUMBER
:
1784 case PROP_TYPE_INDEX
:
1785 *val
= getprop_uint64(zhp
, prop
, source
);
1787 * If we tried to use a default value for a
1788 * readonly property, it means that it was not
1791 if (zfs_prop_readonly(prop
) &&
1792 *source
!= NULL
&& (*source
)[0] == '\0') {
1797 case PROP_TYPE_STRING
:
1799 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1800 "cannot get non-numeric property"));
1801 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
1802 dgettext(TEXT_DOMAIN
, "internal error")));
1810 * Calculate the source type, given the raw source string.
1813 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
1814 char *statbuf
, size_t statlen
)
1816 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
1819 if (source
== NULL
) {
1820 *srctype
= ZPROP_SRC_NONE
;
1821 } else if (source
[0] == '\0') {
1822 *srctype
= ZPROP_SRC_DEFAULT
;
1823 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
1824 *srctype
= ZPROP_SRC_RECEIVED
;
1826 if (strcmp(source
, zhp
->zfs_name
) == 0) {
1827 *srctype
= ZPROP_SRC_LOCAL
;
1829 (void) strlcpy(statbuf
, source
, statlen
);
1830 *srctype
= ZPROP_SRC_INHERITED
;
1837 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
1838 size_t proplen
, boolean_t literal
)
1843 if (zhp
->zfs_recvd_props
== NULL
)
1844 if (get_recvd_props_ioctl(zhp
) != 0)
1847 prop
= zfs_name_to_prop(propname
);
1849 if (prop
!= ZPROP_INVAL
) {
1851 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
1853 zfs_set_recvd_props_mode(zhp
, &cookie
);
1854 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
1855 NULL
, NULL
, 0, literal
);
1856 zfs_unset_recvd_props_mode(zhp
, &cookie
);
1857 } else if (zfs_prop_userquota(propname
)) {
1862 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
1863 propname
, &propval
) != 0)
1865 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
1867 (void) strlcpy(propbuf
, recvdval
, proplen
);
1870 return (err
== 0 ? 0 : -1);
1874 * Retrieve a property from the given object. If 'literal' is specified, then
1875 * numbers are left as exact values. Otherwise, numbers are converted to a
1876 * human-readable form.
1878 * Returns 0 on success, or -1 on error.
1881 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
1882 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
1884 char *source
= NULL
;
1888 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1891 * Check to see if this property applies to our object
1893 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1896 if (received
&& zfs_prop_readonly(prop
))
1900 *src
= ZPROP_SRC_NONE
;
1903 case ZFS_PROP_CREATION
:
1905 * 'creation' is a time_t stored in the statistics. We convert
1906 * this into a string unless 'literal' is specified.
1909 val
= getprop_uint64(zhp
, prop
, &source
);
1910 time_t time
= (time_t)val
;
1914 localtime_r(&time
, &t
) == NULL
||
1915 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
1917 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
) val
);
1921 case ZFS_PROP_MOUNTPOINT
:
1923 * Getting the precise mountpoint can be tricky.
1925 * - for 'none' or 'legacy', return those values.
1926 * - for inherited mountpoints, we want to take everything
1927 * after our ancestor and append it to the inherited value.
1929 * If the pool has an alternate root, we want to prepend that
1930 * root to any values we return.
1933 str
= getprop_string(zhp
, prop
, &source
);
1935 if (str
[0] == '/') {
1936 char buf
[MAXPATHLEN
];
1938 const char *relpath
;
1941 * If we inherit the mountpoint, even from a dataset
1942 * with a received value, the source will be the path of
1943 * the dataset we inherit from. If source is
1944 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1947 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
1950 relpath
= zhp
->zfs_name
+ strlen(source
);
1951 if (relpath
[0] == '/')
1955 if ((zpool_get_prop(zhp
->zpool_hdl
,
1956 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
1957 (strcmp(root
, "-") == 0))
1960 * Special case an alternate root of '/'. This will
1961 * avoid having multiple leading slashes in the
1964 if (strcmp(root
, "/") == 0)
1968 * If the mountpoint is '/' then skip over this
1969 * if we are obtaining either an alternate root or
1970 * an inherited mountpoint.
1972 if (str
[1] == '\0' && (root
[0] != '\0' ||
1973 relpath
[0] != '\0'))
1976 if (relpath
[0] == '\0')
1977 (void) snprintf(propbuf
, proplen
, "%s%s",
1980 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
1981 root
, str
, relpath
[0] == '@' ? "" : "/",
1984 /* 'legacy' or 'none' */
1985 (void) strlcpy(propbuf
, str
, proplen
);
1990 case ZFS_PROP_ORIGIN
:
1991 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
1994 * If there is no parent at all, return failure to indicate that
1995 * it doesn't apply to this dataset.
1997 if (propbuf
[0] == '\0')
2001 case ZFS_PROP_QUOTA
:
2002 case ZFS_PROP_REFQUOTA
:
2003 case ZFS_PROP_RESERVATION
:
2004 case ZFS_PROP_REFRESERVATION
:
2006 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2010 * If quota or reservation is 0, we translate this into 'none'
2011 * (unless literal is set), and indicate that it's the default
2012 * value. Otherwise, we print the number nicely and indicate
2013 * that its set locally.
2017 (void) strlcpy(propbuf
, "0", proplen
);
2019 (void) strlcpy(propbuf
, "none", proplen
);
2022 (void) snprintf(propbuf
, proplen
, "%llu",
2025 zfs_nicenum(val
, propbuf
, proplen
);
2029 case ZFS_PROP_REFRATIO
:
2030 case ZFS_PROP_COMPRESSRATIO
:
2031 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2033 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2034 (u_longlong_t
)(val
/ 100),
2035 (u_longlong_t
)(val
% 100));
2039 switch (zhp
->zfs_type
) {
2040 case ZFS_TYPE_FILESYSTEM
:
2043 case ZFS_TYPE_VOLUME
:
2046 case ZFS_TYPE_SNAPSHOT
:
2052 (void) snprintf(propbuf
, proplen
, "%s", str
);
2055 case ZFS_PROP_MOUNTED
:
2057 * The 'mounted' property is a pseudo-property that described
2058 * whether the filesystem is currently mounted. Even though
2059 * it's a boolean value, the typical values of "on" and "off"
2060 * don't make sense, so we translate to "yes" and "no".
2062 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2063 src
, &source
, &val
) != 0)
2066 (void) strlcpy(propbuf
, "yes", proplen
);
2068 (void) strlcpy(propbuf
, "no", proplen
);
2073 * The 'name' property is a pseudo-property derived from the
2074 * dataset name. It is presented as a real property to simplify
2077 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2080 case ZFS_PROP_MLSLABEL
:
2082 #ifdef HAVE_MLSLABEL
2083 m_label_t
*new_sl
= NULL
;
2084 char *ascii
= NULL
; /* human readable label */
2086 (void) strlcpy(propbuf
,
2087 getprop_string(zhp
, prop
, &source
), proplen
);
2089 if (literal
|| (strcasecmp(propbuf
,
2090 ZFS_MLSLABEL_DEFAULT
) == 0))
2094 * Try to translate the internal hex string to
2095 * human-readable output. If there are any
2096 * problems just use the hex string.
2099 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2100 L_NO_CORRECTION
, NULL
) == -1) {
2101 m_label_free(new_sl
);
2105 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2109 m_label_free(new_sl
);
2112 m_label_free(new_sl
);
2114 (void) strlcpy(propbuf
, ascii
, proplen
);
2117 (void) strlcpy(propbuf
,
2118 getprop_string(zhp
, prop
, &source
), proplen
);
2119 #endif /* HAVE_MLSLABEL */
2124 switch (zfs_prop_get_type(prop
)) {
2125 case PROP_TYPE_NUMBER
:
2126 if (get_numeric_property(zhp
, prop
, src
,
2127 &source
, &val
) != 0)
2130 (void) snprintf(propbuf
, proplen
, "%llu",
2133 zfs_nicenum(val
, propbuf
, proplen
);
2136 case PROP_TYPE_STRING
:
2137 (void) strlcpy(propbuf
,
2138 getprop_string(zhp
, prop
, &source
), proplen
);
2141 case PROP_TYPE_INDEX
:
2142 if (get_numeric_property(zhp
, prop
, src
,
2143 &source
, &val
) != 0)
2145 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2147 (void) strlcpy(propbuf
, strval
, proplen
);
2155 get_source(zhp
, src
, source
, statbuf
, statlen
);
2161 * Utility function to get the given numeric property. Does no validation that
2162 * the given property is the appropriate type; should only be used with
2163 * hard-coded property types.
2166 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2171 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2177 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2181 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2182 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2186 * Similar to zfs_prop_get(), but returns the value as an integer.
2189 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2190 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2195 * Check to see if this property applies to our object
2197 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2198 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2199 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2200 zfs_prop_to_name(prop
)));
2204 *src
= ZPROP_SRC_NONE
;
2206 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2209 get_source(zhp
, src
, source
, statbuf
, statlen
);
2216 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2217 char **domainp
, idmap_rid_t
*ridp
)
2219 idmap_get_handle_t
*get_hdl
= NULL
;
2223 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2227 err
= idmap_get_sidbyuid(get_hdl
, id
,
2228 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2230 err
= idmap_get_sidbygid(get_hdl
, id
,
2231 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2233 if (err
== IDMAP_SUCCESS
&&
2234 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2235 status
== IDMAP_SUCCESS
)
2241 idmap_get_destroy(get_hdl
);
2244 #endif /* HAVE_IDMAP */
2247 * convert the propname into parameters needed by kernel
2248 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2249 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2252 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2253 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2255 zfs_userquota_prop_t type
;
2257 char *numericsid
= NULL
;
2262 /* Figure out the property type ({user|group}{quota|space}) */
2263 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2264 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2265 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2268 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2272 isuser
= (type
== ZFS_PROP_USERQUOTA
||
2273 type
== ZFS_PROP_USERUSED
);
2275 cp
= strchr(propname
, '@') + 1;
2277 if (strchr(cp
, '@')) {
2280 * It's a SID name (eg "user@domain") that needs to be
2281 * turned into S-1-domainID-RID.
2283 directory_error_t e
;
2284 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2287 e
= directory_sid_from_user_name(NULL
,
2290 e
= directory_sid_from_group_name(NULL
,
2294 directory_error_free(e
);
2297 if (numericsid
== NULL
)
2300 /* will be further decoded below */
2303 #endif /* HAVE_IDMAP */
2306 if (strncmp(cp
, "S-1-", 4) == 0) {
2307 /* It's a numeric SID (eg "S-1-234-567-89") */
2308 (void) strlcpy(domain
, cp
, domainlen
);
2309 cp
= strrchr(domain
, '-');
2314 *ridp
= strtoull(cp
, &end
, 10);
2319 if (errno
!= 0 || *end
!= '\0')
2321 } else if (!isdigit(*cp
)) {
2323 * It's a user/group name (eg "user") that needs to be
2324 * turned into a uid/gid
2326 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2343 /* It's a user/group ID (eg "12345"). */
2344 uid_t id
= strtoul(cp
, &end
, 10);
2351 /* It's an ephemeral ID. */
2352 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2353 &mapdomain
, &rid
) != 0)
2355 (void) strlcpy(domain
, mapdomain
, domainlen
);
2362 #endif /* HAVE_IDMAP */
2365 ASSERT3P(numericsid
, ==, NULL
);
2370 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2371 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2374 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2376 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2378 err
= userquota_propname_decode(propname
,
2379 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2380 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2381 zc
.zc_objset_type
= *typep
;
2385 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2389 *propvalue
= zc
.zc_cookie
;
2394 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2395 uint64_t *propvalue
)
2397 zfs_userquota_prop_t type
;
2399 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2404 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2405 char *propbuf
, int proplen
, boolean_t literal
)
2409 zfs_userquota_prop_t type
;
2411 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2418 (void) snprintf(propbuf
, proplen
, "%llu",
2419 (u_longlong_t
)propvalue
);
2420 } else if (propvalue
== 0 &&
2421 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2422 (void) strlcpy(propbuf
, "none", proplen
);
2424 zfs_nicenum(propvalue
, propbuf
, proplen
);
2430 * Returns the name of the given zfs handle.
2433 zfs_get_name(const zfs_handle_t
*zhp
)
2435 return (zhp
->zfs_name
);
2439 * Returns the type of the given zfs handle.
2442 zfs_get_type(const zfs_handle_t
*zhp
)
2444 return (zhp
->zfs_type
);
2448 zfs_do_list_ioctl(zfs_handle_t
*zhp
, int arg
, zfs_cmd_t
*zc
)
2451 uint64_t orig_cookie
;
2453 orig_cookie
= zc
->zc_cookie
;
2455 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
2456 rc
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, arg
, zc
);
2461 /* expand nvlist memory and try again */
2462 if (zcmd_expand_dst_nvlist(zhp
->zfs_hdl
, zc
) != 0) {
2463 zcmd_free_nvlists(zc
);
2466 zc
->zc_cookie
= orig_cookie
;
2469 * An errno value of ESRCH indicates normal completion.
2470 * If ENOENT is returned, then the underlying dataset
2471 * has been removed since we obtained the handle.
2478 rc
= zfs_standard_error(zhp
->zfs_hdl
, errno
,
2479 dgettext(TEXT_DOMAIN
,
2480 "cannot iterate filesystems"));
2488 * Iterate over all child filesystems
2491 zfs_iter_filesystems(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2493 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2497 if (zhp
->zfs_type
!= ZFS_TYPE_FILESYSTEM
)
2500 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2503 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_DATASET_LIST_NEXT
,
2506 * Silently ignore errors, as the only plausible explanation is
2507 * that the pool has since been removed.
2509 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2514 if ((ret
= func(nzhp
, data
)) != 0) {
2515 zcmd_free_nvlists(&zc
);
2519 zcmd_free_nvlists(&zc
);
2520 return ((ret
< 0) ? ret
: 0);
2524 * Iterate over all snapshots
2527 zfs_iter_snapshots(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2529 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2533 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
2536 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2538 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2541 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2546 if ((ret
= func(nzhp
, data
)) != 0) {
2547 zcmd_free_nvlists(&zc
);
2551 zcmd_free_nvlists(&zc
);
2552 return ((ret
< 0) ? ret
: 0);
2556 * Iterate over all children, snapshots and filesystems
2559 zfs_iter_children(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2563 if ((ret
= zfs_iter_filesystems(zhp
, func
, data
)) != 0)
2566 return (zfs_iter_snapshots(zhp
, func
, data
));
2570 * Is one dataset name a child dataset of another?
2572 * Needs to handle these cases:
2573 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2574 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2575 * Descendant? No. No. No. Yes.
2578 is_descendant(const char *ds1
, const char *ds2
)
2580 size_t d1len
= strlen(ds1
);
2582 /* ds2 can't be a descendant if it's smaller */
2583 if (strlen(ds2
) < d1len
)
2586 /* otherwise, compare strings and verify that there's a '/' char */
2587 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2591 * Given a complete name, return just the portion that refers to the parent.
2592 * Can return NULL if this is a pool.
2595 parent_name(const char *path
, char *buf
, size_t buflen
)
2599 if ((loc
= strrchr(path
, '/')) == NULL
)
2602 (void) strncpy(buf
, path
, MIN(buflen
, loc
- path
));
2603 buf
[loc
- path
] = '\0';
2609 * If accept_ancestor is false, then check to make sure that the given path has
2610 * a parent, and that it exists. If accept_ancestor is true, then find the
2611 * closest existing ancestor for the given path. In prefixlen return the
2612 * length of already existing prefix of the given path. We also fetch the
2613 * 'zoned' property, which is used to validate property settings when creating
2617 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2618 boolean_t accept_ancestor
, int *prefixlen
)
2620 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2621 char parent
[ZFS_MAXNAMELEN
];
2627 (void) snprintf(errbuf
, sizeof (errbuf
),
2628 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2630 /* get parent, and check to see if this is just a pool */
2631 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2633 "missing dataset name"));
2634 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2637 /* check to see if the pool exists */
2638 if ((slash
= strchr(parent
, '/')) == NULL
)
2639 slash
= parent
+ strlen(parent
);
2640 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2641 zc
.zc_name
[slash
- parent
] = '\0';
2642 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2644 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2645 "no such pool '%s'"), zc
.zc_name
);
2646 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2649 /* check to see if the parent dataset exists */
2650 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2651 if (errno
== ENOENT
&& accept_ancestor
) {
2653 * Go deeper to find an ancestor, give up on top level.
2655 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2656 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2657 "no such pool '%s'"), zc
.zc_name
);
2658 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2660 } else if (errno
== ENOENT
) {
2661 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2662 "parent does not exist"));
2663 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2665 return (zfs_standard_error(hdl
, errno
, errbuf
));
2668 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2672 /* we are in a non-global zone, but parent is in the global zone */
2673 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2674 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2679 /* make sure parent is a filesystem */
2680 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2681 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2682 "parent is not a filesystem"));
2683 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2689 if (prefixlen
!= NULL
)
2690 *prefixlen
= strlen(parent
);
2695 * Finds whether the dataset of the given type(s) exists.
2698 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2702 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2706 * Try to get stats for the dataset, which will tell us if it exists.
2708 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2709 int ds_type
= zhp
->zfs_type
;
2712 if (types
& ds_type
)
2719 * Given a path to 'target', create all the ancestors between
2720 * the prefixlen portion of the path, and the target itself.
2721 * Fail if the initial prefixlen-ancestor does not already exist.
2724 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2730 /* make sure prefix exists */
2731 cp
= target
+ prefixlen
;
2733 assert(strchr(cp
, '/') == NULL
);
2734 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2737 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2745 * Attempt to create, mount, and share any ancestor filesystems,
2746 * up to the prefixlen-long one.
2748 for (cp
= target
+ prefixlen
+ 1;
2749 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
2754 h
= make_dataset_handle(hdl
, target
);
2756 /* it already exists, nothing to do here */
2761 logstr
= hdl
->libzfs_log_str
;
2762 hdl
->libzfs_log_str
= NULL
;
2763 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2765 hdl
->libzfs_log_str
= logstr
;
2766 opname
= dgettext(TEXT_DOMAIN
, "create");
2770 hdl
->libzfs_log_str
= logstr
;
2771 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2773 opname
= dgettext(TEXT_DOMAIN
, "open");
2777 if (zfs_mount(h
, NULL
, 0) != 0) {
2778 opname
= dgettext(TEXT_DOMAIN
, "mount");
2782 if (zfs_share(h
) != 0) {
2783 opname
= dgettext(TEXT_DOMAIN
, "share");
2793 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2794 "failed to %s ancestor '%s'"), opname
, target
);
2799 * Creates non-existing ancestors of the given path.
2802 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2808 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
2811 if ((path_copy
= strdup(path
)) != NULL
) {
2812 rc
= create_parents(hdl
, path_copy
, prefix
);
2815 if (path_copy
== NULL
|| rc
!= 0)
2822 * Create a new filesystem or volume.
2825 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2828 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2831 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2835 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2836 "cannot create '%s'"), path
);
2838 /* validate the path, taking care to note the extended error message */
2839 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
2840 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2842 /* validate parents exist */
2843 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
2847 * The failure modes when creating a dataset of a different type over
2848 * one that already exists is a little strange. In particular, if you
2849 * try to create a dataset on top of an existing dataset, the ioctl()
2850 * will return ENOENT, not EEXIST. To prevent this from happening, we
2851 * first try to see if the dataset exists.
2853 (void) strlcpy(zc
.zc_name
, path
, sizeof (zc
.zc_name
));
2854 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2855 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2856 "dataset already exists"));
2857 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2860 if (type
== ZFS_TYPE_VOLUME
)
2861 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2863 zc
.zc_objset_type
= DMU_OST_ZFS
;
2865 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
2866 zoned
, NULL
, errbuf
)) == 0)
2869 if (type
== ZFS_TYPE_VOLUME
) {
2871 * If we are creating a volume, the size and block size must
2872 * satisfy a few restraints. First, the blocksize must be a
2873 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2874 * volsize must be a multiple of the block size, and cannot be
2877 if (props
== NULL
|| nvlist_lookup_uint64(props
,
2878 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
2880 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2881 "missing volume size"));
2882 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2885 if ((ret
= nvlist_lookup_uint64(props
,
2886 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2887 &blocksize
)) != 0) {
2888 if (ret
== ENOENT
) {
2889 blocksize
= zfs_prop_default_numeric(
2890 ZFS_PROP_VOLBLOCKSIZE
);
2893 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2894 "missing volume block size"));
2895 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2901 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2902 "volume size cannot be zero"));
2903 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2906 if (size
% blocksize
!= 0) {
2908 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2909 "volume size must be a multiple of volume block "
2911 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2915 if (props
&& zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0)
2919 /* create the dataset */
2920 ret
= zfs_ioctl(hdl
, ZFS_IOC_CREATE
, &zc
);
2922 if (ret
== 0 && type
== ZFS_TYPE_VOLUME
) {
2923 ret
= zvol_create_link(hdl
, path
);
2925 (void) zfs_standard_error(hdl
, errno
,
2926 dgettext(TEXT_DOMAIN
,
2927 "Volume successfully created, but device links "
2928 "were not created"));
2929 zcmd_free_nvlists(&zc
);
2934 zcmd_free_nvlists(&zc
);
2936 /* check for failure */
2938 char parent
[ZFS_MAXNAMELEN
];
2939 (void) parent_name(path
, parent
, sizeof (parent
));
2943 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2944 "no such parent '%s'"), parent
);
2945 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2948 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2949 "parent '%s' is not a filesystem"), parent
);
2950 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
2953 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2954 "volume block size must be power of 2 from "
2956 (uint_t
)SPA_MINBLOCKSIZE
,
2957 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
2959 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2962 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2963 "pool must be upgraded to set this "
2964 "property or value"));
2965 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
2969 * This platform can't address a volume this big.
2971 if (type
== ZFS_TYPE_VOLUME
)
2972 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
2977 return (zfs_standard_error(hdl
, errno
, errbuf
));
2985 * Destroys the given dataset. The caller must make sure that the filesystem
2986 * isn't mounted, and that there are no active dependents.
2989 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
2991 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2993 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2995 if (ZFS_IS_VOLUME(zhp
)) {
2996 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
2999 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3001 zc
.zc_objset_type
= DMU_OST_ZFS
;
3004 zc
.zc_defer_destroy
= defer
;
3005 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0) {
3006 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3007 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3011 remove_mountpoint(zhp
);
3016 struct destroydata
{
3023 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3025 struct destroydata
*dd
= arg
;
3027 char name
[ZFS_MAXNAMELEN
];
3028 boolean_t closezhp
= dd
->closezhp
;
3031 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3032 (void) strlcat(name
, "@", sizeof (name
));
3033 (void) strlcat(name
, dd
->snapname
, sizeof (name
));
3035 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
3037 dd
->gotone
= B_TRUE
;
3041 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3042 (void) zvol_remove_link(zhp
->zfs_hdl
, name
);
3044 * NB: this is simply a best-effort. We don't want to
3045 * return an error, because then we wouldn't visit all
3050 dd
->closezhp
= B_TRUE
;
3051 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, arg
);
3058 * Destroys all snapshots with the given name in zhp & descendants.
3061 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3063 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3065 struct destroydata dd
= { 0 };
3067 dd
.snapname
= snapname
;
3068 (void) zfs_check_snap_cb(zhp
, &dd
);
3071 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3072 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3073 zhp
->zfs_name
, snapname
));
3076 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3077 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3078 zc
.zc_defer_destroy
= defer
;
3080 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY_SNAPS
, &zc
);
3084 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3085 "cannot destroy '%s@%s'"), zc
.zc_name
, snapname
);
3089 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3090 "snapshot is cloned"));
3091 return (zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
));
3094 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3103 * Clones the given dataset. The target must be of the same type as the source.
3106 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3108 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3109 char parent
[ZFS_MAXNAMELEN
];
3112 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3116 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3118 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3119 "cannot create '%s'"), target
);
3121 /* validate the target name */
3122 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3123 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3125 /* validate parents exist */
3126 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3129 (void) parent_name(target
, parent
, sizeof (parent
));
3132 if (ZFS_IS_VOLUME(zhp
)) {
3133 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3134 type
= ZFS_TYPE_VOLUME
;
3136 zc
.zc_objset_type
= DMU_OST_ZFS
;
3137 type
= ZFS_TYPE_FILESYSTEM
;
3141 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3142 zhp
, errbuf
)) == NULL
)
3145 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3153 (void) strlcpy(zc
.zc_name
, target
, sizeof (zc
.zc_name
));
3154 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
, sizeof (zc
.zc_value
));
3155 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_CREATE
, &zc
);
3157 zcmd_free_nvlists(&zc
);
3164 * The parent doesn't exist. We should have caught this
3165 * above, but there may a race condition that has since
3166 * destroyed the parent.
3168 * At this point, we don't know whether it's the source
3169 * that doesn't exist anymore, or whether the target
3170 * dataset doesn't exist.
3172 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3173 "no such parent '%s'"), parent
);
3174 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3177 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3178 "source and target pools differ"));
3179 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3183 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3186 } else if (ZFS_IS_VOLUME(zhp
)) {
3187 ret
= zvol_create_link(zhp
->zfs_hdl
, target
);
3193 typedef struct promote_data
{
3194 char cb_mountpoint
[MAXPATHLEN
];
3195 const char *cb_target
;
3196 const char *cb_errbuf
;
3197 uint64_t cb_pivot_txg
;
3201 promote_snap_cb(zfs_handle_t
*zhp
, void *data
)
3203 promote_data_t
*pd
= data
;
3205 char snapname
[MAXPATHLEN
];
3208 /* We don't care about snapshots after the pivot point */
3209 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > pd
->cb_pivot_txg
) {
3214 /* Remove the device link if it's a zvol. */
3215 if (ZFS_IS_VOLUME(zhp
))
3216 (void) zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3218 /* Check for conflicting names */
3219 (void) strlcpy(snapname
, pd
->cb_target
, sizeof (snapname
));
3220 (void) strlcat(snapname
, strchr(zhp
->zfs_name
, '@'), sizeof (snapname
));
3221 szhp
= make_dataset_handle(zhp
->zfs_hdl
, snapname
);
3224 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3225 "snapshot name '%s' from origin \n"
3226 "conflicts with '%s' from target"),
3227 zhp
->zfs_name
, snapname
);
3228 rv
= zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, pd
->cb_errbuf
);
3235 promote_snap_done_cb(zfs_handle_t
*zhp
, void *data
)
3237 promote_data_t
*pd
= data
;
3239 /* We don't care about snapshots after the pivot point */
3240 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) <= pd
->cb_pivot_txg
) {
3241 /* Create the device link if it's a zvol. */
3242 if (ZFS_IS_VOLUME(zhp
))
3243 (void) zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3251 * Promotes the given clone fs to be the clone parent.
3254 zfs_promote(zfs_handle_t
*zhp
)
3256 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3257 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3258 char parent
[MAXPATHLEN
];
3265 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3266 "cannot promote '%s'"), zhp
->zfs_name
);
3268 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3269 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3270 "snapshots can not be promoted"));
3271 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3274 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3275 if (parent
[0] == '\0') {
3276 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3277 "not a cloned filesystem"));
3278 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3280 cp
= strchr(parent
, '@');
3283 /* Walk the snapshots we will be moving */
3284 pzhp
= zfs_open(hdl
, zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
3287 pd
.cb_pivot_txg
= zfs_prop_get_int(pzhp
, ZFS_PROP_CREATETXG
);
3289 pd
.cb_target
= zhp
->zfs_name
;
3290 pd
.cb_errbuf
= errbuf
;
3291 pzhp
= zfs_open(hdl
, parent
, ZFS_TYPE_DATASET
);
3294 (void) zfs_prop_get(pzhp
, ZFS_PROP_MOUNTPOINT
, pd
.cb_mountpoint
,
3295 sizeof (pd
.cb_mountpoint
), NULL
, NULL
, 0, FALSE
);
3296 ret
= zfs_iter_snapshots(pzhp
, promote_snap_cb
, &pd
);
3302 /* issue the ioctl */
3303 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3304 sizeof (zc
.zc_value
));
3305 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3306 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3309 int save_errno
= errno
;
3311 (void) zfs_iter_snapshots(pzhp
, promote_snap_done_cb
, &pd
);
3314 switch (save_errno
) {
3317 * There is a conflicting snapshot name. We
3318 * should have caught this above, but they could
3319 * have renamed something in the mean time.
3321 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3322 "conflicting snapshot '%s' from parent '%s'"),
3323 zc
.zc_string
, parent
);
3324 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3327 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3330 (void) zfs_iter_snapshots(zhp
, promote_snap_done_cb
, &pd
);
3338 const char *cd_snapname
;
3343 zfs_create_link_cb(zfs_handle_t
*zhp
, void *arg
)
3345 struct createdata
*cd
= arg
;
3348 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3349 char name
[MAXPATHLEN
];
3351 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3352 (void) strlcat(name
, "@", sizeof (name
));
3353 (void) strlcat(name
, cd
->cd_snapname
, sizeof (name
));
3354 (void) zvol_create_link_common(zhp
->zfs_hdl
, name
,
3357 * NB: this is simply a best-effort. We don't want to
3358 * return an error, because then we wouldn't visit all
3363 ret
= zfs_iter_filesystems(zhp
, zfs_create_link_cb
, cd
);
3371 * Takes a snapshot of the given dataset.
3374 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3378 char parent
[ZFS_MAXNAMELEN
];
3380 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3384 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3385 "cannot snapshot '%s'"), path
);
3387 /* validate the target name */
3388 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3389 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3392 if ((props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3393 props
, B_FALSE
, NULL
, errbuf
)) == NULL
)
3396 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3404 /* make sure the parent exists and is of the appropriate type */
3405 delim
= strchr(path
, '@');
3406 (void) strncpy(parent
, path
, delim
- path
);
3407 parent
[delim
- path
] = '\0';
3409 if ((zhp
= zfs_open(hdl
, parent
, ZFS_TYPE_FILESYSTEM
|
3410 ZFS_TYPE_VOLUME
)) == NULL
) {
3411 zcmd_free_nvlists(&zc
);
3415 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3416 (void) strlcpy(zc
.zc_value
, delim
+1, sizeof (zc
.zc_value
));
3417 if (ZFS_IS_VOLUME(zhp
))
3418 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3420 zc
.zc_objset_type
= DMU_OST_ZFS
;
3421 zc
.zc_cookie
= recursive
;
3422 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SNAPSHOT
, &zc
);
3424 zcmd_free_nvlists(&zc
);
3427 * if it was recursive, the one that actually failed will be in
3431 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3432 "cannot create snapshot '%s@%s'"), zc
.zc_name
, zc
.zc_value
);
3434 if (ret
== 0 && recursive
) {
3435 struct createdata cd
;
3437 cd
.cd_snapname
= delim
+ 1;
3438 cd
.cd_ifexists
= B_FALSE
;
3439 (void) zfs_iter_filesystems(zhp
, zfs_create_link_cb
, &cd
);
3441 if (ret
== 0 && zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3442 ret
= zvol_create_link(zhp
->zfs_hdl
, path
);
3444 (void) zfs_standard_error(hdl
, errno
,
3445 dgettext(TEXT_DOMAIN
,
3446 "Volume successfully snapshotted, but device links "
3447 "were not created"));
3454 (void) zfs_standard_error(hdl
, errno
, errbuf
);
3462 * Destroy any more recent snapshots. We invoke this callback on any dependents
3463 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3464 * is a dependent and we should just destroy it without checking the transaction
3467 typedef struct rollback_data
{
3468 const char *cb_target
; /* the snapshot */
3469 uint64_t cb_create
; /* creation time reference */
3471 boolean_t cb_dependent
;
3476 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3478 rollback_data_t
*cbp
= data
;
3480 if (!cbp
->cb_dependent
) {
3481 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3482 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3483 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3487 cbp
->cb_dependent
= B_TRUE
;
3488 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3489 rollback_destroy
, cbp
);
3490 cbp
->cb_dependent
= B_FALSE
;
3492 logstr
= zhp
->zfs_hdl
->libzfs_log_str
;
3493 zhp
->zfs_hdl
->libzfs_log_str
= NULL
;
3494 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3495 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
3498 /* We must destroy this clone; first unmount it */
3499 prop_changelist_t
*clp
;
3501 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3502 cbp
->cb_force
? MS_FORCE
: 0);
3503 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3504 cbp
->cb_error
= B_TRUE
;
3508 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3509 cbp
->cb_error
= B_TRUE
;
3511 changelist_remove(clp
, zhp
->zfs_name
);
3512 (void) changelist_postfix(clp
);
3513 changelist_free(clp
);
3521 * Given a dataset, rollback to a specific snapshot, discarding any
3522 * data changes since then and making it the active dataset.
3524 * Any snapshots more recent than the target are destroyed, along with
3528 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3530 rollback_data_t cb
= { 0 };
3532 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3533 boolean_t restore_resv
= 0;
3534 uint64_t old_volsize
= 0, new_volsize
;
3535 zfs_prop_t resv_prop
= { 0 };
3537 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3538 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3541 * Destroy all recent snapshots and its dependends.
3543 cb
.cb_force
= force
;
3544 cb
.cb_target
= snap
->zfs_name
;
3545 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3546 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3552 * Now that we have verified that the snapshot is the latest,
3553 * rollback to the given snapshot.
3556 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3557 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3559 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3561 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3563 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3566 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3568 if (ZFS_IS_VOLUME(zhp
))
3569 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3571 zc
.zc_objset_type
= DMU_OST_ZFS
;
3574 * We rely on zfs_iter_children() to verify that there are no
3575 * newer snapshots for the given dataset. Therefore, we can
3576 * simply pass the name on to the ioctl() call. There is still
3577 * an unlikely race condition where the user has taken a
3578 * snapshot since we verified that this was the most recent.
3581 if ((err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_ROLLBACK
, &zc
)) != 0) {
3582 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3583 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3589 * For volumes, if the pre-rollback volsize matched the pre-
3590 * rollback reservation and the volsize has changed then set
3591 * the reservation property to the post-rollback volsize.
3592 * Make a new handle since the rollback closed the dataset.
3594 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3595 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3596 if ((err
= zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3601 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3602 if (old_volsize
!= new_volsize
)
3603 err
= zfs_prop_set_int(zhp
, resv_prop
,
3612 * Iterate over all dependents for a given dataset. This includes both
3613 * hierarchical dependents (children) and data dependents (snapshots and
3614 * clones). The bulk of the processing occurs in get_dependents() in
3618 zfs_iter_dependents(zfs_handle_t
*zhp
, boolean_t allowrecursion
,
3619 zfs_iter_f func
, void *data
)
3624 zfs_handle_t
*child
;
3627 if (get_dependents(zhp
->zfs_hdl
, allowrecursion
, zhp
->zfs_name
,
3628 &dependents
, &count
) != 0)
3631 for (i
= 0; i
< count
; i
++) {
3632 if ((child
= make_dataset_handle(zhp
->zfs_hdl
,
3633 dependents
[i
])) == NULL
)
3636 if ((ret
= func(child
, data
)) != 0)
3640 for (i
= 0; i
< count
; i
++)
3641 free(dependents
[i
]);
3648 * Renames the given dataset.
3651 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
)
3654 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3656 prop_changelist_t
*cl
= NULL
;
3657 zfs_handle_t
*zhrp
= NULL
;
3658 char *parentname
= NULL
;
3659 char parent
[ZFS_MAXNAMELEN
];
3660 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3663 /* if we have the same exact name, just return success */
3664 if (strcmp(zhp
->zfs_name
, target
) == 0)
3667 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3668 "cannot rename to '%s'"), target
);
3671 * Make sure the target name is valid
3673 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3674 if ((strchr(target
, '@') == NULL
) ||
3677 * Snapshot target name is abbreviated,
3678 * reconstruct full dataset name
3680 (void) strlcpy(parent
, zhp
->zfs_name
,
3682 delim
= strchr(parent
, '@');
3683 if (strchr(target
, '@') == NULL
)
3687 (void) strlcat(parent
, target
, sizeof (parent
));
3691 * Make sure we're renaming within the same dataset.
3693 delim
= strchr(target
, '@');
3694 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3695 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3696 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3697 "snapshots must be part of same "
3699 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3703 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3704 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3707 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3708 "recursive rename must be a snapshot"));
3709 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3712 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3713 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3715 /* validate parents */
3716 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3719 /* make sure we're in the same pool */
3720 verify((delim
= strchr(target
, '/')) != NULL
);
3721 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3722 zhp
->zfs_name
[delim
- target
] != '/') {
3723 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3724 "datasets must be within same pool"));
3725 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3728 /* new name cannot be a child of the current dataset name */
3729 if (is_descendant(zhp
->zfs_name
, target
)) {
3730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3731 "New dataset name cannot be a descendant of "
3732 "current dataset name"));
3733 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3737 (void) snprintf(errbuf
, sizeof (errbuf
),
3738 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3740 if (getzoneid() == GLOBAL_ZONEID
&&
3741 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3742 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3743 "dataset is used in a non-global zone"));
3744 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3748 struct destroydata dd
;
3750 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3751 if (parentname
== NULL
) {
3755 delim
= strchr(parentname
, '@');
3757 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3763 dd
.snapname
= delim
+ 1;
3764 dd
.gotone
= B_FALSE
;
3765 dd
.closezhp
= B_TRUE
;
3767 /* We remove any zvol links prior to renaming them */
3768 ret
= zfs_iter_filesystems(zhrp
, zfs_check_snap_cb
, &dd
);
3773 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0)) == NULL
)
3776 if (changelist_haszonedchild(cl
)) {
3777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3778 "child dataset with inherited mountpoint is used "
3779 "in a non-global zone"));
3780 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3785 if ((ret
= changelist_prefix(cl
)) != 0)
3789 if (ZFS_IS_VOLUME(zhp
))
3790 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3792 zc
.zc_objset_type
= DMU_OST_ZFS
;
3794 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3795 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3797 zc
.zc_cookie
= recursive
;
3799 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3801 * if it was recursive, the one that actually failed will
3804 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3805 "cannot rename '%s'"), zc
.zc_name
);
3807 if (recursive
&& errno
== EEXIST
) {
3808 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3809 "a child dataset already has a snapshot "
3810 "with the new name"));
3811 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3813 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3817 * On failure, we still want to remount any filesystems that
3818 * were previously mounted, so we don't alter the system state.
3821 struct createdata cd
;
3823 /* only create links for datasets that had existed */
3824 cd
.cd_snapname
= delim
+ 1;
3825 cd
.cd_ifexists
= B_TRUE
;
3826 (void) zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3829 (void) changelist_postfix(cl
);
3833 struct createdata cd
;
3835 /* only create links for datasets that had existed */
3836 cd
.cd_snapname
= strchr(target
, '@') + 1;
3837 cd
.cd_ifexists
= B_TRUE
;
3838 ret
= zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3841 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3842 ret
= changelist_postfix(cl
);
3854 changelist_free(cl
);
3860 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3861 * and wait briefly for udev to create the /dev link.
3864 zvol_create_link(libzfs_handle_t
*hdl
, const char *dataset
)
3866 return (zvol_create_link_common(hdl
, dataset
, B_FALSE
));
3870 zvol_create_link_common(libzfs_handle_t
*hdl
, const char *dataset
, int ifexists
)
3872 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3873 char path
[MAXPATHLEN
];
3876 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3879 * Issue the appropriate ioctl.
3881 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CREATE_MINOR
, &zc
) != 0) {
3885 * Silently ignore the case where the link already
3886 * exists. This allows 'zfs volinit' to be run multiple
3887 * times without errors.
3893 * Dataset does not exist in the kernel. If we
3894 * don't care (see zfs_rename), then ignore the
3904 return (zfs_standard_error_fmt(hdl
, errno
,
3905 dgettext(TEXT_DOMAIN
, "cannot create device links "
3906 "for '%s'"), dataset
));
3911 * Wait up to 10 seconds for udev to create the device.
3913 (void) snprintf(path
, sizeof (path
), "%s/%s", ZVOL_DIR
, dataset
);
3914 error
= zpool_label_disk_wait(path
, 10000);
3916 (void) printf(gettext("%s may not be immediately "
3917 "available\n"), path
);
3923 * Remove a minor node for the given zvol and the associated /dev links.
3926 zvol_remove_link(libzfs_handle_t
*hdl
, const char *dataset
)
3928 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3930 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3932 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_REMOVE_MINOR
, &zc
) != 0) {
3936 * Silently ignore the case where the link no longer
3937 * exists, so that 'zfs volfini' can be run multiple
3938 * times without errors.
3943 return (zfs_standard_error_fmt(hdl
, errno
,
3944 dgettext(TEXT_DOMAIN
, "cannot remove device "
3945 "links for '%s'"), dataset
));
3953 zfs_get_user_props(zfs_handle_t
*zhp
)
3955 return (zhp
->zfs_user_props
);
3959 * This function is used by 'zfs list' to determine the exact set of columns to
3960 * display, and their maximum widths. This does two main things:
3962 * - If this is a list of all properties, then expand the list to include
3963 * all native properties, and set a flag so that for each dataset we look
3964 * for new unique user properties and add them to the list.
3966 * - For non fixed-width properties, keep track of the maximum width seen
3967 * so that we can size the column appropriately. If the user has
3968 * requested received property values, we also need to compute the width
3969 * of the RECEIVED column.
3972 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
)
3974 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3975 zprop_list_t
*entry
;
3976 zprop_list_t
**last
, **start
;
3977 nvlist_t
*userprops
, *propval
;
3980 char buf
[ZFS_MAXPROPLEN
];
3982 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
3985 userprops
= zfs_get_user_props(zhp
);
3988 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
3990 * Go through and add any user properties as necessary. We
3991 * start by incrementing our list pointer to the first
3992 * non-native property.
3995 while (*start
!= NULL
) {
3996 if ((*start
)->pl_prop
== ZPROP_INVAL
)
3998 start
= &(*start
)->pl_next
;
4002 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4004 * See if we've already found this property in our list.
4006 for (last
= start
; *last
!= NULL
;
4007 last
= &(*last
)->pl_next
) {
4008 if (strcmp((*last
)->pl_user_prop
,
4009 nvpair_name(elem
)) == 0)
4013 if (*last
== NULL
) {
4014 if ((entry
= zfs_alloc(hdl
,
4015 sizeof (zprop_list_t
))) == NULL
||
4016 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4017 nvpair_name(elem
)))) == NULL
) {
4022 entry
->pl_prop
= ZPROP_INVAL
;
4023 entry
->pl_width
= strlen(nvpair_name(elem
));
4024 entry
->pl_all
= B_TRUE
;
4031 * Now go through and check the width of any non-fixed columns
4033 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4034 if (entry
->pl_fixed
)
4037 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4038 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4039 buf
, sizeof (buf
), NULL
, NULL
, 0, B_FALSE
) == 0) {
4040 if (strlen(buf
) > entry
->pl_width
)
4041 entry
->pl_width
= strlen(buf
);
4043 if (received
&& zfs_prop_get_recvd(zhp
,
4044 zfs_prop_to_name(entry
->pl_prop
),
4045 buf
, sizeof (buf
), B_FALSE
) == 0)
4046 if (strlen(buf
) > entry
->pl_recvd_width
)
4047 entry
->pl_recvd_width
= strlen(buf
);
4049 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4051 verify(nvlist_lookup_string(propval
,
4052 ZPROP_VALUE
, &strval
) == 0);
4053 if (strlen(strval
) > entry
->pl_width
)
4054 entry
->pl_width
= strlen(strval
);
4056 if (received
&& zfs_prop_get_recvd(zhp
,
4057 entry
->pl_user_prop
,
4058 buf
, sizeof (buf
), B_FALSE
) == 0)
4059 if (strlen(buf
) > entry
->pl_recvd_width
)
4060 entry
->pl_recvd_width
= strlen(buf
);
4068 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4073 * Keep a reference to the props-table against which we prune the
4076 zhp
->zfs_props_table
= props
;
4078 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4081 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4082 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4085 * User properties will result in ZPROP_INVAL, and since we
4086 * only know how to prune standard ZFS properties, we always
4087 * leave these in the list. This can also happen if we
4088 * encounter an unknown DSL property (when running older
4089 * software, for example).
4091 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4092 (void) nvlist_remove(zhp
->zfs_props
,
4093 nvpair_name(curr
), nvpair_type(curr
));
4099 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4100 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4102 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4103 nvlist_t
*nvlist
= NULL
;
4106 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4107 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4108 zc
.zc_cookie
= (uint64_t)cmd
;
4110 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4111 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4112 (void) no_memory(hdl
);
4118 case ZFS_SMB_ACL_ADD
:
4119 case ZFS_SMB_ACL_REMOVE
:
4120 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4122 case ZFS_SMB_ACL_RENAME
:
4123 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4125 (void) no_memory(hdl
);
4128 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4130 (void) no_memory(hdl
);
4133 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4134 nvlist_free(nvlist
);
4138 case ZFS_SMB_ACL_PURGE
:
4143 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4145 nvlist_free(nvlist
);
4150 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4151 char *path
, char *resource
)
4153 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4158 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4159 char *path
, char *resource
)
4161 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4166 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4168 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4173 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4174 char *oldname
, char *newname
)
4176 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4181 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4182 zfs_userspace_cb_t func
, void *arg
)
4184 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4186 zfs_useracct_t buf
[100];
4188 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4190 zc
.zc_objset_type
= type
;
4191 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4195 zfs_useracct_t
*zua
= buf
;
4197 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4198 error
= ioctl(zhp
->zfs_hdl
->libzfs_fd
,
4199 ZFS_IOC_USERSPACE_MANY
, &zc
);
4200 if (error
|| zc
.zc_nvlist_dst_size
== 0)
4203 while (zc
.zc_nvlist_dst_size
> 0) {
4204 error
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4209 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4217 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4218 boolean_t recursive
, boolean_t temphold
, boolean_t enoent_ok
,
4219 int cleanup_fd
, uint64_t dsobj
, uint64_t createtxg
)
4221 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4222 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4224 ASSERT(!recursive
|| dsobj
== 0);
4226 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4227 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4228 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4229 >= sizeof (zc
.zc_string
))
4230 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4231 zc
.zc_cookie
= recursive
;
4232 zc
.zc_temphold
= temphold
;
4233 zc
.zc_cleanup_fd
= cleanup_fd
;
4234 zc
.zc_sendobj
= dsobj
;
4235 zc
.zc_createtxg
= createtxg
;
4237 if (zfs_ioctl(hdl
, ZFS_IOC_HOLD
, &zc
) != 0) {
4238 char errbuf
[ZFS_MAXNAMELEN
+32];
4241 * if it was recursive, the one that actually failed will be in
4244 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4245 "cannot hold '%s@%s'"), zc
.zc_name
, snapname
);
4249 * Temporary tags wind up having the ds object id
4250 * prepended. So even if we passed the length check
4251 * above, it's still possible for the tag to wind
4252 * up being slightly too long.
4254 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
));
4256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4257 "pool must be upgraded"));
4258 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4260 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4262 return (zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
));
4268 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4276 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4277 boolean_t recursive
)
4279 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4280 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4282 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4283 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4284 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4285 >= sizeof (zc
.zc_string
))
4286 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4287 zc
.zc_cookie
= recursive
;
4289 if (zfs_ioctl(hdl
, ZFS_IOC_RELEASE
, &zc
) != 0) {
4290 char errbuf
[ZFS_MAXNAMELEN
+32];
4293 * if it was recursive, the one that actually failed will be in
4296 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4297 "cannot release '%s' from '%s@%s'"), tag
, zc
.zc_name
,
4301 return (zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
));
4303 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4304 "pool must be upgraded"));
4305 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4307 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4309 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4317 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4320 uint64_t nblocks
, volblocksize
;
4324 if (nvlist_lookup_string(props
,
4325 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4326 ncopies
= atoi(strval
);
4329 if (nvlist_lookup_uint64(props
,
4330 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4331 &volblocksize
) != 0)
4332 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4333 nblocks
= volsize
/volblocksize
;
4334 /* start with metadnode L0-L6 */
4336 /* calculate number of indirects */
4337 while (nblocks
> 1) {
4338 nblocks
+= DNODES_PER_LEVEL
- 1;
4339 nblocks
/= DNODES_PER_LEVEL
;
4342 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4345 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4346 * compressed, but in practice they compress down to about
4349 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;