4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
37 #include <sys/mntent.h>
38 #include <sys/mount.h>
46 #include <directory.h>
48 #include <sys/dnode.h>
53 #include "zfs_namecheck.h"
55 #include "libzfs_impl.h"
56 #include "zfs_deleg.h"
58 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
59 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
62 * Given a single type (not a mask of types), return the type in a human
66 zfs_type_to_name(zfs_type_t type
)
69 case ZFS_TYPE_FILESYSTEM
:
70 return (dgettext(TEXT_DOMAIN
, "filesystem"));
71 case ZFS_TYPE_SNAPSHOT
:
72 return (dgettext(TEXT_DOMAIN
, "snapshot"));
74 return (dgettext(TEXT_DOMAIN
, "volume"));
83 * Given a path and mask of ZFS types, return a string describing this dataset.
84 * This is used when we fail to open a dataset and we cannot get an exact type.
85 * We guess what the type would have been based on the path and the mask of
89 path_to_str(const char *path
, int types
)
92 * When given a single type, always report the exact type.
94 if (types
== ZFS_TYPE_SNAPSHOT
)
95 return (dgettext(TEXT_DOMAIN
, "snapshot"));
96 if (types
== ZFS_TYPE_FILESYSTEM
)
97 return (dgettext(TEXT_DOMAIN
, "filesystem"));
98 if (types
== ZFS_TYPE_VOLUME
)
99 return (dgettext(TEXT_DOMAIN
, "volume"));
102 * The user is requesting more than one type of dataset. If this is the
103 * case, consult the path itself. If we're looking for a snapshot, and
104 * a '@' is found, then report it as "snapshot". Otherwise, remove the
105 * snapshot attribute and try again.
107 if (types
& ZFS_TYPE_SNAPSHOT
) {
108 if (strchr(path
, '@') != NULL
)
109 return (dgettext(TEXT_DOMAIN
, "snapshot"));
110 return (path_to_str(path
, types
& ~ZFS_TYPE_SNAPSHOT
));
114 * The user has requested either filesystems or volumes.
115 * We have no way of knowing a priori what type this would be, so always
116 * report it as "filesystem" or "volume", our two primitive types.
118 if (types
& ZFS_TYPE_FILESYSTEM
)
119 return (dgettext(TEXT_DOMAIN
, "filesystem"));
121 assert(types
& ZFS_TYPE_VOLUME
);
122 return (dgettext(TEXT_DOMAIN
, "volume"));
126 * Validate a ZFS path. This is used even before trying to open the dataset, to
127 * provide a more meaningful error message. We call zfs_error_aux() to
128 * explain exactly why the name was not valid.
131 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
137 if (dataset_namecheck(path
, &why
, &what
) != 0) {
140 case NAME_ERR_TOOLONG
:
141 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
142 "name is too long"));
145 case NAME_ERR_LEADING_SLASH
:
146 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
147 "leading slash in name"));
150 case NAME_ERR_EMPTY_COMPONENT
:
151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
152 "empty component in name"));
155 case NAME_ERR_TRAILING_SLASH
:
156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
157 "trailing slash in name"));
160 case NAME_ERR_INVALCHAR
:
162 dgettext(TEXT_DOMAIN
, "invalid character "
163 "'%c' in name"), what
);
166 case NAME_ERR_MULTIPLE_AT
:
167 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
168 "multiple '@' delimiters in name"));
171 case NAME_ERR_NOLETTER
:
172 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
173 "pool doesn't begin with a letter"));
176 case NAME_ERR_RESERVED
:
177 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
178 "name is reserved"));
181 case NAME_ERR_DISKLIKE
:
182 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
183 "reserved disk name"));
193 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
195 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
196 "snapshot delimiter '@' in filesystem name"));
200 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
202 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
203 "missing '@' delimiter in snapshot name"));
207 if (modifying
&& strchr(path
, '%') != NULL
) {
209 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
210 "invalid character %c in name"), '%');
218 zfs_name_valid(const char *name
, zfs_type_t type
)
220 if (type
== ZFS_TYPE_POOL
)
221 return (zpool_name_valid(NULL
, B_FALSE
, name
));
222 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
226 * This function takes the raw DSL properties, and filters out the user-defined
227 * properties into a separate nvlist.
230 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
232 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
237 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
238 (void) no_memory(hdl
);
243 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
244 if (!zfs_prop_user(nvpair_name(elem
)))
247 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
248 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
250 (void) no_memory(hdl
);
258 static zpool_handle_t
*
259 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
261 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
264 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
265 if (hdl
->libzfs_pool_handles
!= NULL
)
266 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
267 hdl
->libzfs_pool_handles
= zph
;
272 static zpool_handle_t
*
273 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
275 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
276 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
278 while ((zph
!= NULL
) &&
279 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
280 zph
= zph
->zpool_next
;
285 * Returns a handle to the pool that contains the provided dataset.
286 * If a handle to that pool already exists then that handle is returned.
287 * Otherwise, a new handle is created and added to the list of handles.
289 static zpool_handle_t
*
290 zpool_handle(zfs_handle_t
*zhp
)
296 len
= strcspn(zhp
->zfs_name
, "/@") + 1;
297 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
298 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
300 zph
= zpool_find_handle(zhp
, pool_name
, len
);
302 zph
= zpool_add_handle(zhp
, pool_name
);
309 zpool_free_handles(libzfs_handle_t
*hdl
)
311 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
313 while (zph
!= NULL
) {
314 next
= zph
->zpool_next
;
318 hdl
->libzfs_pool_handles
= NULL
;
322 * Utility function to gather stats (objset and zpl) for the given object.
325 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
327 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
329 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
331 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
332 if (errno
== ENOMEM
) {
333 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
344 * Utility function to get the received properties of the given object.
347 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
349 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
350 nvlist_t
*recvdprops
;
351 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
354 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
357 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
359 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
360 if (errno
== ENOMEM
) {
361 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
365 zcmd_free_nvlists(&zc
);
370 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
371 zcmd_free_nvlists(&zc
);
375 nvlist_free(zhp
->zfs_recvd_props
);
376 zhp
->zfs_recvd_props
= recvdprops
;
382 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
384 nvlist_t
*allprops
, *userprops
;
386 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
388 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
393 * XXX Why do we store the user props separately, in addition to
394 * storing them in zfs_props?
396 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
397 nvlist_free(allprops
);
401 nvlist_free(zhp
->zfs_props
);
402 nvlist_free(zhp
->zfs_user_props
);
404 zhp
->zfs_props
= allprops
;
405 zhp
->zfs_user_props
= userprops
;
411 get_stats(zfs_handle_t
*zhp
)
414 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
416 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
418 if (get_stats_ioctl(zhp
, &zc
) != 0)
420 else if (put_stats_zhdl(zhp
, &zc
) != 0)
422 zcmd_free_nvlists(&zc
);
427 * Refresh the properties currently stored in the handle.
430 zfs_refresh_properties(zfs_handle_t
*zhp
)
432 (void) get_stats(zhp
);
436 * Makes a handle from the given dataset name. Used by zfs_open() and
437 * zfs_iter_* to create child handles on the fly.
440 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
442 if (put_stats_zhdl(zhp
, zc
) != 0)
446 * We've managed to open the dataset and gather statistics. Determine
447 * the high-level type.
449 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
450 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
451 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
452 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
456 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
457 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
458 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
459 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
460 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
461 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
463 abort(); /* we should never see any other types */
465 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
472 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
474 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
476 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
482 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
483 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
487 if (get_stats_ioctl(zhp
, &zc
) == -1) {
488 zcmd_free_nvlists(&zc
);
492 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
496 zcmd_free_nvlists(&zc
);
500 static zfs_handle_t
*
501 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
503 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
509 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
510 if (make_dataset_handle_common(zhp
, zc
) == -1) {
518 * Opens the given snapshot, filesystem, or volume. The 'types'
519 * argument is a mask of acceptable types. The function will print an
520 * appropriate error message and return NULL if it can't be opened.
523 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
528 (void) snprintf(errbuf
, sizeof (errbuf
),
529 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
532 * Validate the name before we even try to open it.
534 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
535 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
536 "invalid dataset name"));
537 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
542 * Try to get stats for the dataset, which will tell us if it exists.
545 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
546 (void) zfs_standard_error(hdl
, errno
, errbuf
);
550 if (!(types
& zhp
->zfs_type
)) {
551 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
560 * Release a ZFS handle. Nothing to do but free the associated memory.
563 zfs_close(zfs_handle_t
*zhp
)
565 if (zhp
->zfs_mntopts
)
566 free(zhp
->zfs_mntopts
);
567 nvlist_free(zhp
->zfs_props
);
568 nvlist_free(zhp
->zfs_user_props
);
569 nvlist_free(zhp
->zfs_recvd_props
);
573 typedef struct mnttab_node
{
574 struct mnttab mtn_mt
;
579 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
581 const mnttab_node_t
*mtn1
= arg1
;
582 const mnttab_node_t
*mtn2
= arg2
;
585 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
589 return (rv
> 0 ? 1 : -1);
593 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
595 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
596 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
597 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
601 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
605 rewind(hdl
->libzfs_mnttab
);
606 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
609 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
611 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
612 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
613 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
614 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
615 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
616 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
621 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
626 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
627 free(mtn
->mtn_mt
.mnt_special
);
628 free(mtn
->mtn_mt
.mnt_mountp
);
629 free(mtn
->mtn_mt
.mnt_fstype
);
630 free(mtn
->mtn_mt
.mnt_mntopts
);
633 avl_destroy(&hdl
->libzfs_mnttab_cache
);
637 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
639 hdl
->libzfs_mnttab_enable
= enable
;
643 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
644 struct mnttab
*entry
)
649 if (!hdl
->libzfs_mnttab_enable
) {
650 struct mnttab srch
= { 0 };
652 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
653 libzfs_mnttab_fini(hdl
);
654 rewind(hdl
->libzfs_mnttab
);
655 srch
.mnt_special
= (char *)fsname
;
656 srch
.mnt_fstype
= MNTTYPE_ZFS
;
657 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
663 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
664 libzfs_mnttab_update(hdl
);
666 find
.mtn_mt
.mnt_special
= (char *)fsname
;
667 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
669 *entry
= mtn
->mtn_mt
;
676 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
677 const char *mountp
, const char *mntopts
)
681 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
683 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
684 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
685 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
686 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
687 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
688 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
692 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
697 find
.mtn_mt
.mnt_special
= (char *)fsname
;
698 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
699 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
700 free(ret
->mtn_mt
.mnt_special
);
701 free(ret
->mtn_mt
.mnt_mountp
);
702 free(ret
->mtn_mt
.mnt_fstype
);
703 free(ret
->mtn_mt
.mnt_mntopts
);
709 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
711 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
713 if (zpool_handle
== NULL
)
716 *spa_version
= zpool_get_prop_int(zpool_handle
,
717 ZPOOL_PROP_VERSION
, NULL
);
722 * The choice of reservation property depends on the SPA version.
725 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
729 if (zfs_spa_version(zhp
, &spa_version
) < 0)
732 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
733 *resv_prop
= ZFS_PROP_REFRESERVATION
;
735 *resv_prop
= ZFS_PROP_RESERVATION
;
741 * Given an nvlist of properties to set, validates that they are correct, and
742 * parses any numeric properties (index, boolean, etc) if they are specified as
746 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
747 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
754 int chosen_normal
= -1;
757 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
758 (void) no_memory(hdl
);
763 * Make sure this property is valid and applies to this type.
767 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
768 const char *propname
= nvpair_name(elem
);
770 prop
= zfs_name_to_prop(propname
);
771 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
773 * This is a user property: make sure it's a
774 * string, and that it's less than ZAP_MAXNAMELEN.
776 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
778 "'%s' must be a string"), propname
);
779 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
783 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
784 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
785 "property name '%s' is too long"),
787 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
791 (void) nvpair_value_string(elem
, &strval
);
792 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
793 (void) no_memory(hdl
);
800 * Currently, only user properties can be modified on
803 if (type
== ZFS_TYPE_SNAPSHOT
) {
804 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
805 "this property can not be modified for snapshots"));
806 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
810 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
811 zfs_userquota_prop_t uqtype
;
812 char newpropname
[128];
817 if (userquota_propname_decode(propname
, zoned
,
818 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
820 dgettext(TEXT_DOMAIN
,
821 "'%s' has an invalid user/group name"),
823 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
827 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
828 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
830 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
832 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
837 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
838 (void) nvpair_value_string(elem
, &strval
);
839 if (strcmp(strval
, "none") == 0) {
841 } else if (zfs_nicestrtonum(hdl
,
842 strval
, &intval
) != 0) {
843 (void) zfs_error(hdl
,
844 EZFS_BADPROP
, errbuf
);
847 } else if (nvpair_type(elem
) ==
849 (void) nvpair_value_uint64(elem
, &intval
);
851 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
852 "use 'none' to disable "
853 "userquota/groupquota"));
857 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
858 "'%s' must be a number"), propname
);
859 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
864 * Encode the prop name as
865 * userquota@<hex-rid>-domain, to make it easy
866 * for the kernel to decode.
868 (void) snprintf(newpropname
, sizeof (newpropname
),
869 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
870 (longlong_t
)rid
, domain
);
874 if (nvlist_add_uint64_array(ret
, newpropname
,
876 (void) no_memory(hdl
);
882 if (prop
== ZPROP_INVAL
) {
883 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
884 "invalid property '%s'"), propname
);
885 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
889 if (!zfs_prop_valid_for_type(prop
, type
)) {
891 dgettext(TEXT_DOMAIN
, "'%s' does not "
892 "apply to datasets of this type"), propname
);
893 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
897 if (zfs_prop_readonly(prop
) &&
898 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
900 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
902 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
906 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
907 &strval
, &intval
, errbuf
) != 0)
911 * Perform some additional checks for specific properties.
914 case ZFS_PROP_VERSION
:
920 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
921 if (intval
< version
) {
922 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
923 "Can not downgrade; already at version %u"),
925 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
931 case ZFS_PROP_RECORDSIZE
:
932 case ZFS_PROP_VOLBLOCKSIZE
:
933 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
934 if (intval
< SPA_MINBLOCKSIZE
||
935 intval
> SPA_MAXBLOCKSIZE
|| !ISP2(intval
)) {
936 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
937 "'%s' must be power of 2 from %u "
939 (uint_t
)SPA_MINBLOCKSIZE
,
940 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
941 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
946 case ZFS_PROP_MLSLABEL
:
949 * Verify the mlslabel string and convert to
950 * internal hex label string.
954 char *hex
= NULL
; /* internal label string */
956 /* Default value is already OK. */
957 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
960 /* Verify the label can be converted to binary form */
961 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
962 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
963 L_NO_CORRECTION
, NULL
) == -1)) {
967 /* Now translate to hex internal label string */
968 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
974 m_label_free(new_sl
);
976 /* If string is already in internal form, we're done. */
977 if (strcmp(strval
, hex
) == 0) {
982 /* Replace the label string with the internal form. */
983 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
985 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
992 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
993 "invalid mlslabel '%s'"), strval
);
994 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
995 m_label_free(new_sl
); /* OK if null */
1000 case ZFS_PROP_MOUNTPOINT
:
1002 namecheck_err_t why
;
1004 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1005 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1008 if (mountpoint_namecheck(strval
, &why
)) {
1010 case NAME_ERR_LEADING_SLASH
:
1012 dgettext(TEXT_DOMAIN
,
1013 "'%s' must be an absolute path, "
1014 "'none', or 'legacy'"), propname
);
1016 case NAME_ERR_TOOLONG
:
1018 dgettext(TEXT_DOMAIN
,
1019 "component of '%s' is too long"),
1025 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1032 case ZFS_PROP_SHARESMB
:
1033 case ZFS_PROP_SHARENFS
:
1035 * For the mountpoint and sharenfs or sharesmb
1036 * properties, check if it can be set in a
1037 * global/non-global zone based on
1038 * the zoned property value:
1040 * global zone non-global zone
1041 * --------------------------------------------------
1042 * zoned=on mountpoint (no) mountpoint (yes)
1043 * sharenfs (no) sharenfs (no)
1044 * sharesmb (no) sharesmb (no)
1046 * zoned=off mountpoint (yes) N/A
1051 if (getzoneid() == GLOBAL_ZONEID
) {
1052 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1053 "'%s' cannot be set on "
1054 "dataset in a non-global zone"),
1056 (void) zfs_error(hdl
, EZFS_ZONED
,
1059 } else if (prop
== ZFS_PROP_SHARENFS
||
1060 prop
== ZFS_PROP_SHARESMB
) {
1061 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1062 "'%s' cannot be set in "
1063 "a non-global zone"), propname
);
1064 (void) zfs_error(hdl
, EZFS_ZONED
,
1068 } else if (getzoneid() != GLOBAL_ZONEID
) {
1070 * If zoned property is 'off', this must be in
1071 * a global zone. If not, something is wrong.
1073 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1074 "'%s' cannot be set while dataset "
1075 "'zoned' property is set"), propname
);
1076 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1081 * At this point, it is legitimate to set the
1082 * property. Now we want to make sure that the
1083 * property value is valid if it is sharenfs.
1085 if ((prop
== ZFS_PROP_SHARENFS
||
1086 prop
== ZFS_PROP_SHARESMB
) &&
1087 strcmp(strval
, "on") != 0 &&
1088 strcmp(strval
, "off") != 0) {
1089 zfs_share_proto_t proto
;
1091 if (prop
== ZFS_PROP_SHARESMB
)
1097 * Must be an valid sharing protocol
1098 * option string so init the libshare
1099 * in order to enable the parser and
1100 * then parse the options. We use the
1101 * control API since we don't care about
1102 * the current configuration and don't
1103 * want the overhead of loading it
1104 * until we actually do something.
1107 if (zfs_init_libshare(hdl
,
1108 SA_INIT_CONTROL_API
) != SA_OK
) {
1110 * An error occurred so we can't do
1113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1114 "'%s' cannot be set: problem "
1115 "in share initialization"),
1117 (void) zfs_error(hdl
, EZFS_BADPROP
,
1122 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1124 * There was an error in parsing so
1125 * deal with it by issuing an error
1126 * message and leaving after
1127 * uninitializing the the libshare
1130 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1131 "'%s' cannot be set to invalid "
1132 "options"), propname
);
1133 (void) zfs_error(hdl
, EZFS_BADPROP
,
1135 zfs_uninit_libshare(hdl
);
1138 zfs_uninit_libshare(hdl
);
1142 case ZFS_PROP_UTF8ONLY
:
1143 chosen_utf
= (int)intval
;
1145 case ZFS_PROP_NORMALIZE
:
1146 chosen_normal
= (int)intval
;
1153 * For changes to existing volumes, we have some additional
1154 * checks to enforce.
1156 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1157 uint64_t volsize
= zfs_prop_get_int(zhp
,
1159 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1160 ZFS_PROP_VOLBLOCKSIZE
);
1164 case ZFS_PROP_RESERVATION
:
1165 case ZFS_PROP_REFRESERVATION
:
1166 if (intval
> volsize
) {
1167 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1168 "'%s' is greater than current "
1169 "volume size"), propname
);
1170 (void) zfs_error(hdl
, EZFS_BADPROP
,
1176 case ZFS_PROP_VOLSIZE
:
1177 if (intval
% blocksize
!= 0) {
1178 zfs_nicenum(blocksize
, buf
,
1180 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1181 "'%s' must be a multiple of "
1182 "volume block size (%s)"),
1184 (void) zfs_error(hdl
, EZFS_BADPROP
,
1190 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1191 "'%s' cannot be zero"),
1193 (void) zfs_error(hdl
, EZFS_BADPROP
,
1205 * If normalization was chosen, but no UTF8 choice was made,
1206 * enforce rejection of non-UTF8 names.
1208 * If normalization was chosen, but rejecting non-UTF8 names
1209 * was explicitly not chosen, it is an error.
1211 if (chosen_normal
> 0 && chosen_utf
< 0) {
1212 if (nvlist_add_uint64(ret
,
1213 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1214 (void) no_memory(hdl
);
1217 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1218 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1219 "'%s' must be set 'on' if normalization chosen"),
1220 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1221 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1232 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1234 uint64_t old_volsize
;
1235 uint64_t new_volsize
;
1236 uint64_t old_reservation
;
1237 uint64_t new_reservation
;
1238 zfs_prop_t resv_prop
;
1241 * If this is an existing volume, and someone is setting the volsize,
1242 * make sure that it matches the reservation, or add it if necessary.
1244 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1245 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1247 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1248 if ((zvol_volsize_to_reservation(old_volsize
, zhp
->zfs_props
) !=
1249 old_reservation
) || nvlist_lookup_uint64(nvl
,
1250 zfs_prop_to_name(resv_prop
), &new_reservation
) != ENOENT
) {
1253 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1256 new_reservation
= zvol_volsize_to_reservation(new_volsize
,
1258 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1259 new_reservation
) != 0) {
1260 (void) no_memory(zhp
->zfs_hdl
);
1267 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1274 * For quotas and reservations, ENOSPC indicates
1275 * something different; setting a quota or reservation
1276 * doesn't use any disk space.
1279 case ZFS_PROP_QUOTA
:
1280 case ZFS_PROP_REFQUOTA
:
1281 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1282 "size is less than current used or "
1284 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1287 case ZFS_PROP_RESERVATION
:
1288 case ZFS_PROP_REFRESERVATION
:
1289 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1290 "size is greater than available space"));
1291 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1295 (void) zfs_standard_error(hdl
, err
, errbuf
);
1301 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1305 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1309 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1310 "pool and or dataset must be upgraded to set this "
1311 "property or value"));
1312 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1316 if (prop
== ZFS_PROP_COMPRESSION
) {
1317 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1318 "property setting is not allowed on "
1319 "bootable datasets"));
1320 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1322 (void) zfs_standard_error(hdl
, err
, errbuf
);
1327 if (prop
== ZPROP_INVAL
) {
1328 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1330 (void) zfs_standard_error(hdl
, err
, errbuf
);
1336 * This platform can't address a volume this big.
1339 if (prop
== ZFS_PROP_VOLSIZE
) {
1340 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1346 (void) zfs_standard_error(hdl
, err
, errbuf
);
1351 * Given a property name and value, set the property for the given dataset.
1354 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1356 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1358 prop_changelist_t
*cl
= NULL
;
1360 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1361 nvlist_t
*nvl
= NULL
, *realprops
;
1363 boolean_t do_prefix
;
1367 (void) snprintf(errbuf
, sizeof (errbuf
),
1368 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1371 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1372 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1373 (void) no_memory(hdl
);
1377 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1378 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1384 prop
= zfs_name_to_prop(propname
);
1386 if (prop
== ZFS_PROP_VOLSIZE
) {
1387 if ((added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1)
1391 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1394 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1395 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1396 "child dataset with inherited mountpoint is used "
1397 "in a non-global zone"));
1398 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1403 * If the dataset's canmount property is being set to noauto,
1404 * then we want to prevent unmounting & remounting it.
1406 do_prefix
= !((prop
== ZFS_PROP_CANMOUNT
) &&
1407 (zprop_string_to_index(prop
, propval
, &idx
,
1408 ZFS_TYPE_DATASET
) == 0) && (idx
== ZFS_CANMOUNT_NOAUTO
));
1410 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1414 * Execute the corresponding ioctl() to set this property.
1416 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1418 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1421 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1424 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1425 if (added_resv
&& errno
== ENOSPC
) {
1426 /* clean up the volsize property we tried to set */
1427 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1430 zcmd_free_nvlists(&zc
);
1431 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1433 if (nvlist_add_uint64(nvl
,
1434 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1437 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1439 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1443 ret
= changelist_postfix(cl
);
1446 * Refresh the statistics so the new property value
1450 (void) get_stats(zhp
);
1455 zcmd_free_nvlists(&zc
);
1457 changelist_free(cl
);
1462 * Given a property, inherit the value from the parent dataset, or if received
1463 * is TRUE, revert to the received value, if any.
1466 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1468 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1470 prop_changelist_t
*cl
;
1471 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1475 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1476 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1478 zc
.zc_cookie
= received
;
1479 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1481 * For user properties, the amount of work we have to do is very
1482 * small, so just do it here.
1484 if (!zfs_prop_user(propname
)) {
1485 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1486 "invalid property"));
1487 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1490 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1491 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1493 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1494 return (zfs_standard_error(hdl
, errno
, errbuf
));
1500 * Verify that this property is inheritable.
1502 if (zfs_prop_readonly(prop
))
1503 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1505 if (!zfs_prop_inheritable(prop
) && !received
)
1506 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1509 * Check to see if the value applies to this type
1511 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1512 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1515 * Normalize the name, to get rid of shorthand abbreviations.
1517 propname
= zfs_prop_to_name(prop
);
1518 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1519 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1521 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1522 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1523 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1524 "dataset is used in a non-global zone"));
1525 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1529 * Determine datasets which will be affected by this change, if any.
1531 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1534 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1535 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1536 "child dataset with inherited mountpoint is used "
1537 "in a non-global zone"));
1538 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1542 if ((ret
= changelist_prefix(cl
)) != 0)
1545 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1546 return (zfs_standard_error(hdl
, errno
, errbuf
));
1549 if ((ret
= changelist_postfix(cl
)) != 0)
1553 * Refresh the statistics so the new property is reflected.
1555 (void) get_stats(zhp
);
1559 changelist_free(cl
);
1564 * True DSL properties are stored in an nvlist. The following two functions
1565 * extract them appropriately.
1568 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1574 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1575 zfs_prop_to_name(prop
), &nv
) == 0) {
1576 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1577 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1579 verify(!zhp
->zfs_props_table
||
1580 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1581 value
= zfs_prop_default_numeric(prop
);
1589 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1595 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1596 zfs_prop_to_name(prop
), &nv
) == 0) {
1597 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1598 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1600 verify(!zhp
->zfs_props_table
||
1601 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1602 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1611 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1613 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1617 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1619 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1620 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1624 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1626 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1631 * Internal function for getting a numeric property. Both zfs_prop_get() and
1632 * zfs_prop_get_int() are built using this interface.
1634 * Certain properties can be overridden using 'mount -o'. In this case, scan
1635 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1636 * If they differ from the on-disk values, report the current values and mark
1637 * the source "temporary".
1640 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1641 char **source
, uint64_t *val
)
1643 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1644 nvlist_t
*zplprops
= NULL
;
1646 char *mntopt_on
= NULL
;
1647 char *mntopt_off
= NULL
;
1648 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1653 case ZFS_PROP_ATIME
:
1654 mntopt_on
= MNTOPT_ATIME
;
1655 mntopt_off
= MNTOPT_NOATIME
;
1658 case ZFS_PROP_DEVICES
:
1659 mntopt_on
= MNTOPT_DEVICES
;
1660 mntopt_off
= MNTOPT_NODEVICES
;
1664 mntopt_on
= MNTOPT_EXEC
;
1665 mntopt_off
= MNTOPT_NOEXEC
;
1668 case ZFS_PROP_READONLY
:
1669 mntopt_on
= MNTOPT_RO
;
1670 mntopt_off
= MNTOPT_RW
;
1673 case ZFS_PROP_SETUID
:
1674 mntopt_on
= MNTOPT_SETUID
;
1675 mntopt_off
= MNTOPT_NOSETUID
;
1678 case ZFS_PROP_XATTR
:
1679 mntopt_on
= MNTOPT_XATTR
;
1680 mntopt_off
= MNTOPT_NOXATTR
;
1683 case ZFS_PROP_NBMAND
:
1684 mntopt_on
= MNTOPT_NBMAND
;
1685 mntopt_off
= MNTOPT_NONBMAND
;
1692 * Because looking up the mount options is potentially expensive
1693 * (iterating over all of /etc/mnttab), we defer its calculation until
1694 * we're looking up a property which requires its presence.
1696 if (!zhp
->zfs_mntcheck
&&
1697 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1698 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1699 struct mnttab entry
;
1701 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1702 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1704 if (zhp
->zfs_mntopts
== NULL
)
1708 zhp
->zfs_mntcheck
= B_TRUE
;
1711 if (zhp
->zfs_mntopts
== NULL
)
1712 mnt
.mnt_mntopts
= "";
1714 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
1717 case ZFS_PROP_ATIME
:
1718 case ZFS_PROP_DEVICES
:
1720 case ZFS_PROP_READONLY
:
1721 case ZFS_PROP_SETUID
:
1722 case ZFS_PROP_XATTR
:
1723 case ZFS_PROP_NBMAND
:
1724 *val
= getprop_uint64(zhp
, prop
, source
);
1729 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
1732 *src
= ZPROP_SRC_TEMPORARY
;
1733 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
1736 *src
= ZPROP_SRC_TEMPORARY
;
1740 case ZFS_PROP_CANMOUNT
:
1741 case ZFS_PROP_VOLSIZE
:
1742 case ZFS_PROP_QUOTA
:
1743 case ZFS_PROP_REFQUOTA
:
1744 case ZFS_PROP_RESERVATION
:
1745 case ZFS_PROP_REFRESERVATION
:
1746 *val
= getprop_uint64(zhp
, prop
, source
);
1748 if (*source
== NULL
) {
1749 /* not default, must be local */
1750 *source
= zhp
->zfs_name
;
1754 case ZFS_PROP_MOUNTED
:
1755 *val
= (zhp
->zfs_mntopts
!= NULL
);
1758 case ZFS_PROP_NUMCLONES
:
1759 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
1762 case ZFS_PROP_VERSION
:
1763 case ZFS_PROP_NORMALIZE
:
1764 case ZFS_PROP_UTF8ONLY
:
1766 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
1767 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1769 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1770 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
1771 zcmd_free_nvlists(&zc
);
1774 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
1775 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
1777 zcmd_free_nvlists(&zc
);
1781 nvlist_free(zplprops
);
1782 zcmd_free_nvlists(&zc
);
1786 switch (zfs_prop_get_type(prop
)) {
1787 case PROP_TYPE_NUMBER
:
1788 case PROP_TYPE_INDEX
:
1789 *val
= getprop_uint64(zhp
, prop
, source
);
1791 * If we tried to use a default value for a
1792 * readonly property, it means that it was not
1795 if (zfs_prop_readonly(prop
) &&
1796 *source
!= NULL
&& (*source
)[0] == '\0') {
1801 case PROP_TYPE_STRING
:
1803 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1804 "cannot get non-numeric property"));
1805 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
1806 dgettext(TEXT_DOMAIN
, "internal error")));
1814 * Calculate the source type, given the raw source string.
1817 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
1818 char *statbuf
, size_t statlen
)
1820 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
1823 if (source
== NULL
) {
1824 *srctype
= ZPROP_SRC_NONE
;
1825 } else if (source
[0] == '\0') {
1826 *srctype
= ZPROP_SRC_DEFAULT
;
1827 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
1828 *srctype
= ZPROP_SRC_RECEIVED
;
1830 if (strcmp(source
, zhp
->zfs_name
) == 0) {
1831 *srctype
= ZPROP_SRC_LOCAL
;
1833 (void) strlcpy(statbuf
, source
, statlen
);
1834 *srctype
= ZPROP_SRC_INHERITED
;
1841 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
1842 size_t proplen
, boolean_t literal
)
1847 if (zhp
->zfs_recvd_props
== NULL
)
1848 if (get_recvd_props_ioctl(zhp
) != 0)
1851 prop
= zfs_name_to_prop(propname
);
1853 if (prop
!= ZPROP_INVAL
) {
1855 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
1857 zfs_set_recvd_props_mode(zhp
, &cookie
);
1858 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
1859 NULL
, NULL
, 0, literal
);
1860 zfs_unset_recvd_props_mode(zhp
, &cookie
);
1861 } else if (zfs_prop_userquota(propname
)) {
1866 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
1867 propname
, &propval
) != 0)
1869 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
1871 (void) strlcpy(propbuf
, recvdval
, proplen
);
1874 return (err
== 0 ? 0 : -1);
1878 * Retrieve a property from the given object. If 'literal' is specified, then
1879 * numbers are left as exact values. Otherwise, numbers are converted to a
1880 * human-readable form.
1882 * Returns 0 on success, or -1 on error.
1885 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
1886 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
1888 char *source
= NULL
;
1892 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1895 * Check to see if this property applies to our object
1897 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1900 if (received
&& zfs_prop_readonly(prop
))
1904 *src
= ZPROP_SRC_NONE
;
1907 case ZFS_PROP_CREATION
:
1909 * 'creation' is a time_t stored in the statistics. We convert
1910 * this into a string unless 'literal' is specified.
1913 val
= getprop_uint64(zhp
, prop
, &source
);
1914 time_t time
= (time_t)val
;
1918 localtime_r(&time
, &t
) == NULL
||
1919 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
1921 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
) val
);
1925 case ZFS_PROP_MOUNTPOINT
:
1927 * Getting the precise mountpoint can be tricky.
1929 * - for 'none' or 'legacy', return those values.
1930 * - for inherited mountpoints, we want to take everything
1931 * after our ancestor and append it to the inherited value.
1933 * If the pool has an alternate root, we want to prepend that
1934 * root to any values we return.
1937 str
= getprop_string(zhp
, prop
, &source
);
1939 if (str
[0] == '/') {
1940 char buf
[MAXPATHLEN
];
1942 const char *relpath
;
1945 * If we inherit the mountpoint, even from a dataset
1946 * with a received value, the source will be the path of
1947 * the dataset we inherit from. If source is
1948 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1951 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
1954 relpath
= zhp
->zfs_name
+ strlen(source
);
1955 if (relpath
[0] == '/')
1959 if ((zpool_get_prop(zhp
->zpool_hdl
,
1960 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
1961 (strcmp(root
, "-") == 0))
1964 * Special case an alternate root of '/'. This will
1965 * avoid having multiple leading slashes in the
1968 if (strcmp(root
, "/") == 0)
1972 * If the mountpoint is '/' then skip over this
1973 * if we are obtaining either an alternate root or
1974 * an inherited mountpoint.
1976 if (str
[1] == '\0' && (root
[0] != '\0' ||
1977 relpath
[0] != '\0'))
1980 if (relpath
[0] == '\0')
1981 (void) snprintf(propbuf
, proplen
, "%s%s",
1984 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
1985 root
, str
, relpath
[0] == '@' ? "" : "/",
1988 /* 'legacy' or 'none' */
1989 (void) strlcpy(propbuf
, str
, proplen
);
1994 case ZFS_PROP_ORIGIN
:
1995 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
1998 * If there is no parent at all, return failure to indicate that
1999 * it doesn't apply to this dataset.
2001 if (propbuf
[0] == '\0')
2005 case ZFS_PROP_QUOTA
:
2006 case ZFS_PROP_REFQUOTA
:
2007 case ZFS_PROP_RESERVATION
:
2008 case ZFS_PROP_REFRESERVATION
:
2010 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2014 * If quota or reservation is 0, we translate this into 'none'
2015 * (unless literal is set), and indicate that it's the default
2016 * value. Otherwise, we print the number nicely and indicate
2017 * that its set locally.
2021 (void) strlcpy(propbuf
, "0", proplen
);
2023 (void) strlcpy(propbuf
, "none", proplen
);
2026 (void) snprintf(propbuf
, proplen
, "%llu",
2029 zfs_nicenum(val
, propbuf
, proplen
);
2033 case ZFS_PROP_COMPRESSRATIO
:
2034 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2036 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2037 (u_longlong_t
)(val
/ 100),
2038 (u_longlong_t
)(val
% 100));
2042 switch (zhp
->zfs_type
) {
2043 case ZFS_TYPE_FILESYSTEM
:
2046 case ZFS_TYPE_VOLUME
:
2049 case ZFS_TYPE_SNAPSHOT
:
2055 (void) snprintf(propbuf
, proplen
, "%s", str
);
2058 case ZFS_PROP_MOUNTED
:
2060 * The 'mounted' property is a pseudo-property that described
2061 * whether the filesystem is currently mounted. Even though
2062 * it's a boolean value, the typical values of "on" and "off"
2063 * don't make sense, so we translate to "yes" and "no".
2065 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2066 src
, &source
, &val
) != 0)
2069 (void) strlcpy(propbuf
, "yes", proplen
);
2071 (void) strlcpy(propbuf
, "no", proplen
);
2076 * The 'name' property is a pseudo-property derived from the
2077 * dataset name. It is presented as a real property to simplify
2080 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2083 case ZFS_PROP_MLSLABEL
:
2085 m_label_t
*new_sl
= NULL
;
2086 char *ascii
= NULL
; /* human readable label */
2088 (void) strlcpy(propbuf
,
2089 getprop_string(zhp
, prop
, &source
), proplen
);
2091 if (literal
|| (strcasecmp(propbuf
,
2092 ZFS_MLSLABEL_DEFAULT
) == 0))
2096 * Try to translate the internal hex string to
2097 * human-readable output. If there are any
2098 * problems just use the hex string.
2101 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2102 L_NO_CORRECTION
, NULL
) == -1) {
2103 m_label_free(new_sl
);
2107 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2111 m_label_free(new_sl
);
2114 m_label_free(new_sl
);
2116 (void) strlcpy(propbuf
, ascii
, proplen
);
2122 switch (zfs_prop_get_type(prop
)) {
2123 case PROP_TYPE_NUMBER
:
2124 if (get_numeric_property(zhp
, prop
, src
,
2125 &source
, &val
) != 0)
2128 (void) snprintf(propbuf
, proplen
, "%llu",
2131 zfs_nicenum(val
, propbuf
, proplen
);
2134 case PROP_TYPE_STRING
:
2135 (void) strlcpy(propbuf
,
2136 getprop_string(zhp
, prop
, &source
), proplen
);
2139 case PROP_TYPE_INDEX
:
2140 if (get_numeric_property(zhp
, prop
, src
,
2141 &source
, &val
) != 0)
2143 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2145 (void) strlcpy(propbuf
, strval
, proplen
);
2153 get_source(zhp
, src
, source
, statbuf
, statlen
);
2159 * Utility function to get the given numeric property. Does no validation that
2160 * the given property is the appropriate type; should only be used with
2161 * hard-coded property types.
2164 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2169 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2175 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2179 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2180 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2184 * Similar to zfs_prop_get(), but returns the value as an integer.
2187 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2188 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2193 * Check to see if this property applies to our object
2195 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2196 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2197 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2198 zfs_prop_to_name(prop
)));
2202 *src
= ZPROP_SRC_NONE
;
2204 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2207 get_source(zhp
, src
, source
, statbuf
, statlen
);
2213 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2214 char **domainp
, idmap_rid_t
*ridp
)
2216 idmap_get_handle_t
*get_hdl
= NULL
;
2220 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2224 err
= idmap_get_sidbyuid(get_hdl
, id
,
2225 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2227 err
= idmap_get_sidbygid(get_hdl
, id
,
2228 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2230 if (err
== IDMAP_SUCCESS
&&
2231 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2232 status
== IDMAP_SUCCESS
)
2238 idmap_get_destroy(get_hdl
);
2243 * convert the propname into parameters needed by kernel
2244 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2245 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2248 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2249 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2251 zfs_userquota_prop_t type
;
2253 char *numericsid
= NULL
;
2258 /* Figure out the property type ({user|group}{quota|space}) */
2259 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2260 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2261 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2264 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2268 isuser
= (type
== ZFS_PROP_USERQUOTA
||
2269 type
== ZFS_PROP_USERUSED
);
2271 cp
= strchr(propname
, '@') + 1;
2273 if (strchr(cp
, '@')) {
2275 * It's a SID name (eg "user@domain") that needs to be
2276 * turned into S-1-domainID-RID.
2278 directory_error_t e
;
2279 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2282 e
= directory_sid_from_user_name(NULL
,
2285 e
= directory_sid_from_group_name(NULL
,
2289 directory_error_free(e
);
2292 if (numericsid
== NULL
)
2295 /* will be further decoded below */
2298 if (strncmp(cp
, "S-1-", 4) == 0) {
2299 /* It's a numeric SID (eg "S-1-234-567-89") */
2300 (void) strlcpy(domain
, cp
, domainlen
);
2301 cp
= strrchr(domain
, '-');
2306 *ridp
= strtoull(cp
, &end
, 10);
2311 if (errno
!= 0 || *end
!= '\0')
2313 } else if (!isdigit(*cp
)) {
2315 * It's a user/group name (eg "user") that needs to be
2316 * turned into a uid/gid
2318 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2334 /* It's a user/group ID (eg "12345"). */
2335 uid_t id
= strtoul(cp
, &end
, 10);
2342 /* It's an ephemeral ID. */
2343 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2344 &mapdomain
, &rid
) != 0)
2346 (void) strlcpy(domain
, mapdomain
, domainlen
);
2353 ASSERT3P(numericsid
, ==, NULL
);
2358 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2359 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2362 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2364 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2366 err
= userquota_propname_decode(propname
,
2367 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2368 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2369 zc
.zc_objset_type
= *typep
;
2373 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2377 *propvalue
= zc
.zc_cookie
;
2382 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2383 uint64_t *propvalue
)
2385 zfs_userquota_prop_t type
;
2387 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2392 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2393 char *propbuf
, int proplen
, boolean_t literal
)
2397 zfs_userquota_prop_t type
;
2399 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2406 (void) snprintf(propbuf
, proplen
, "%llu",
2407 (u_longlong_t
)propvalue
);
2408 } else if (propvalue
== 0 &&
2409 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2410 (void) strlcpy(propbuf
, "none", proplen
);
2412 zfs_nicenum(propvalue
, propbuf
, proplen
);
2418 * Returns the name of the given zfs handle.
2421 zfs_get_name(const zfs_handle_t
*zhp
)
2423 return (zhp
->zfs_name
);
2427 * Returns the type of the given zfs handle.
2430 zfs_get_type(const zfs_handle_t
*zhp
)
2432 return (zhp
->zfs_type
);
2436 zfs_do_list_ioctl(zfs_handle_t
*zhp
, int arg
, zfs_cmd_t
*zc
)
2439 uint64_t orig_cookie
;
2441 orig_cookie
= zc
->zc_cookie
;
2443 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
2444 rc
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, arg
, zc
);
2449 /* expand nvlist memory and try again */
2450 if (zcmd_expand_dst_nvlist(zhp
->zfs_hdl
, zc
) != 0) {
2451 zcmd_free_nvlists(zc
);
2454 zc
->zc_cookie
= orig_cookie
;
2457 * An errno value of ESRCH indicates normal completion.
2458 * If ENOENT is returned, then the underlying dataset
2459 * has been removed since we obtained the handle.
2466 rc
= zfs_standard_error(zhp
->zfs_hdl
, errno
,
2467 dgettext(TEXT_DOMAIN
,
2468 "cannot iterate filesystems"));
2476 * Iterate over all child filesystems
2479 zfs_iter_filesystems(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2481 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2485 if (zhp
->zfs_type
!= ZFS_TYPE_FILESYSTEM
)
2488 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2491 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_DATASET_LIST_NEXT
,
2494 * Silently ignore errors, as the only plausible explanation is
2495 * that the pool has since been removed.
2497 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2502 if ((ret
= func(nzhp
, data
)) != 0) {
2503 zcmd_free_nvlists(&zc
);
2507 zcmd_free_nvlists(&zc
);
2508 return ((ret
< 0) ? ret
: 0);
2512 * Iterate over all snapshots
2515 zfs_iter_snapshots(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2517 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2521 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
2524 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2526 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2529 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2534 if ((ret
= func(nzhp
, data
)) != 0) {
2535 zcmd_free_nvlists(&zc
);
2539 zcmd_free_nvlists(&zc
);
2540 return ((ret
< 0) ? ret
: 0);
2544 * Iterate over all children, snapshots and filesystems
2547 zfs_iter_children(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2551 if ((ret
= zfs_iter_filesystems(zhp
, func
, data
)) != 0)
2554 return (zfs_iter_snapshots(zhp
, func
, data
));
2558 * Is one dataset name a child dataset of another?
2560 * Needs to handle these cases:
2561 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2562 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2563 * Descendant? No. No. No. Yes.
2566 is_descendant(const char *ds1
, const char *ds2
)
2568 size_t d1len
= strlen(ds1
);
2570 /* ds2 can't be a descendant if it's smaller */
2571 if (strlen(ds2
) < d1len
)
2574 /* otherwise, compare strings and verify that there's a '/' char */
2575 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2579 * Given a complete name, return just the portion that refers to the parent.
2580 * Can return NULL if this is a pool.
2583 parent_name(const char *path
, char *buf
, size_t buflen
)
2587 if ((loc
= strrchr(path
, '/')) == NULL
)
2590 (void) strncpy(buf
, path
, MIN(buflen
, loc
- path
));
2591 buf
[loc
- path
] = '\0';
2597 * If accept_ancestor is false, then check to make sure that the given path has
2598 * a parent, and that it exists. If accept_ancestor is true, then find the
2599 * closest existing ancestor for the given path. In prefixlen return the
2600 * length of already existing prefix of the given path. We also fetch the
2601 * 'zoned' property, which is used to validate property settings when creating
2605 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2606 boolean_t accept_ancestor
, int *prefixlen
)
2608 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2609 char parent
[ZFS_MAXNAMELEN
];
2615 (void) snprintf(errbuf
, sizeof (errbuf
),
2616 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2618 /* get parent, and check to see if this is just a pool */
2619 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2620 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2621 "missing dataset name"));
2622 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2625 /* check to see if the pool exists */
2626 if ((slash
= strchr(parent
, '/')) == NULL
)
2627 slash
= parent
+ strlen(parent
);
2628 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2629 zc
.zc_name
[slash
- parent
] = '\0';
2630 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2633 "no such pool '%s'"), zc
.zc_name
);
2634 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2637 /* check to see if the parent dataset exists */
2638 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2639 if (errno
== ENOENT
&& accept_ancestor
) {
2641 * Go deeper to find an ancestor, give up on top level.
2643 if (parent_name(parent
, parent
, sizeof (parent
)) != 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
));
2648 } else if (errno
== ENOENT
) {
2649 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2650 "parent does not exist"));
2651 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2653 return (zfs_standard_error(hdl
, errno
, errbuf
));
2656 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2660 /* we are in a non-global zone, but parent is in the global zone */
2661 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2662 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2667 /* make sure parent is a filesystem */
2668 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2669 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2670 "parent is not a filesystem"));
2671 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2677 if (prefixlen
!= NULL
)
2678 *prefixlen
= strlen(parent
);
2683 * Finds whether the dataset of the given type(s) exists.
2686 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2690 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2694 * Try to get stats for the dataset, which will tell us if it exists.
2696 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2697 int ds_type
= zhp
->zfs_type
;
2700 if (types
& ds_type
)
2707 * Given a path to 'target', create all the ancestors between
2708 * the prefixlen portion of the path, and the target itself.
2709 * Fail if the initial prefixlen-ancestor does not already exist.
2712 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2718 /* make sure prefix exists */
2719 cp
= target
+ prefixlen
;
2721 assert(strchr(cp
, '/') == NULL
);
2722 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2725 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2733 * Attempt to create, mount, and share any ancestor filesystems,
2734 * up to the prefixlen-long one.
2736 for (cp
= target
+ prefixlen
+ 1;
2737 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
2742 h
= make_dataset_handle(hdl
, target
);
2744 /* it already exists, nothing to do here */
2749 logstr
= hdl
->libzfs_log_str
;
2750 hdl
->libzfs_log_str
= NULL
;
2751 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2753 hdl
->libzfs_log_str
= logstr
;
2754 opname
= dgettext(TEXT_DOMAIN
, "create");
2758 hdl
->libzfs_log_str
= logstr
;
2759 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2761 opname
= dgettext(TEXT_DOMAIN
, "open");
2765 if (zfs_mount(h
, NULL
, 0) != 0) {
2766 opname
= dgettext(TEXT_DOMAIN
, "mount");
2770 if (zfs_share(h
) != 0) {
2771 opname
= dgettext(TEXT_DOMAIN
, "share");
2781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2782 "failed to %s ancestor '%s'"), opname
, target
);
2787 * Creates non-existing ancestors of the given path.
2790 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2796 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
2799 if ((path_copy
= strdup(path
)) != NULL
) {
2800 rc
= create_parents(hdl
, path_copy
, prefix
);
2803 if (path_copy
== NULL
|| rc
!= 0)
2810 * Create a new filesystem or volume.
2813 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2816 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2819 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2823 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2824 "cannot create '%s'"), path
);
2826 /* validate the path, taking care to note the extended error message */
2827 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
2828 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2830 /* validate parents exist */
2831 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
2835 * The failure modes when creating a dataset of a different type over
2836 * one that already exists is a little strange. In particular, if you
2837 * try to create a dataset on top of an existing dataset, the ioctl()
2838 * will return ENOENT, not EEXIST. To prevent this from happening, we
2839 * first try to see if the dataset exists.
2841 (void) strlcpy(zc
.zc_name
, path
, sizeof (zc
.zc_name
));
2842 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2843 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2844 "dataset already exists"));
2845 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2848 if (type
== ZFS_TYPE_VOLUME
)
2849 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2851 zc
.zc_objset_type
= DMU_OST_ZFS
;
2853 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
2854 zoned
, NULL
, errbuf
)) == 0)
2857 if (type
== ZFS_TYPE_VOLUME
) {
2859 * If we are creating a volume, the size and block size must
2860 * satisfy a few restraints. First, the blocksize must be a
2861 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2862 * volsize must be a multiple of the block size, and cannot be
2865 if (props
== NULL
|| nvlist_lookup_uint64(props
,
2866 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
2868 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2869 "missing volume size"));
2870 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2873 if ((ret
= nvlist_lookup_uint64(props
,
2874 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2875 &blocksize
)) != 0) {
2876 if (ret
== ENOENT
) {
2877 blocksize
= zfs_prop_default_numeric(
2878 ZFS_PROP_VOLBLOCKSIZE
);
2881 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2882 "missing volume block size"));
2883 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2889 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2890 "volume size cannot be zero"));
2891 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2894 if (size
% blocksize
!= 0) {
2896 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2897 "volume size must be a multiple of volume block "
2899 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2903 if (props
&& zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0)
2907 /* create the dataset */
2908 ret
= zfs_ioctl(hdl
, ZFS_IOC_CREATE
, &zc
);
2910 zcmd_free_nvlists(&zc
);
2912 /* check for failure */
2914 char parent
[ZFS_MAXNAMELEN
];
2915 (void) parent_name(path
, parent
, sizeof (parent
));
2919 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2920 "no such parent '%s'"), parent
);
2921 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2924 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2925 "parent '%s' is not a filesystem"), parent
);
2926 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
2929 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2930 "volume block size must be power of 2 from "
2932 (uint_t
)SPA_MINBLOCKSIZE
,
2933 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
2935 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2938 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2939 "pool must be upgraded to set this "
2940 "property or value"));
2941 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
2945 * This platform can't address a volume this big.
2947 if (type
== ZFS_TYPE_VOLUME
)
2948 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
2953 return (zfs_standard_error(hdl
, errno
, errbuf
));
2961 * Destroys the given dataset. The caller must make sure that the filesystem
2962 * isn't mounted, and that there are no active dependents.
2965 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
2967 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2969 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2971 if (ZFS_IS_VOLUME(zhp
)) {
2972 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2974 zc
.zc_objset_type
= DMU_OST_ZFS
;
2977 zc
.zc_defer_destroy
= defer
;
2978 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0) {
2979 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
2980 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
2984 remove_mountpoint(zhp
);
2989 struct destroydata
{
2996 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
2998 struct destroydata
*dd
= arg
;
3000 char name
[ZFS_MAXNAMELEN
];
3001 boolean_t closezhp
= dd
->closezhp
;
3004 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3005 (void) strlcat(name
, "@", sizeof (name
));
3006 (void) strlcat(name
, dd
->snapname
, sizeof (name
));
3008 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
3010 dd
->gotone
= B_TRUE
;
3014 dd
->closezhp
= B_TRUE
;
3016 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, arg
);
3023 * Destroys all snapshots with the given name in zhp & descendants.
3026 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3028 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3030 struct destroydata dd
= { 0 };
3032 dd
.snapname
= snapname
;
3033 (void) zfs_check_snap_cb(zhp
, &dd
);
3036 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3037 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3038 zhp
->zfs_name
, snapname
));
3041 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3042 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3043 zc
.zc_defer_destroy
= defer
;
3045 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY_SNAPS
, &zc
);
3049 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3050 "cannot destroy '%s@%s'"), zc
.zc_name
, snapname
);
3054 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3055 "snapshot is cloned"));
3056 return (zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
));
3059 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3068 * Clones the given dataset. The target must be of the same type as the source.
3071 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3073 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3074 char parent
[ZFS_MAXNAMELEN
];
3077 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3081 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3083 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3084 "cannot create '%s'"), target
);
3086 /* validate the target name */
3087 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3088 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3090 /* validate parents exist */
3091 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3094 (void) parent_name(target
, parent
, sizeof (parent
));
3097 if (ZFS_IS_VOLUME(zhp
)) {
3098 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3099 type
= ZFS_TYPE_VOLUME
;
3101 zc
.zc_objset_type
= DMU_OST_ZFS
;
3102 type
= ZFS_TYPE_FILESYSTEM
;
3106 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3107 zhp
, errbuf
)) == NULL
)
3110 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3118 (void) strlcpy(zc
.zc_name
, target
, sizeof (zc
.zc_name
));
3119 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
, sizeof (zc
.zc_value
));
3120 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_CREATE
, &zc
);
3122 zcmd_free_nvlists(&zc
);
3129 * The parent doesn't exist. We should have caught this
3130 * above, but there may a race condition that has since
3131 * destroyed the parent.
3133 * At this point, we don't know whether it's the source
3134 * that doesn't exist anymore, or whether the target
3135 * dataset doesn't exist.
3137 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3138 "no such parent '%s'"), parent
);
3139 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3142 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3143 "source and target pools differ"));
3144 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3148 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3157 * Promotes the given clone fs to be the clone parent.
3160 zfs_promote(zfs_handle_t
*zhp
)
3162 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3163 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3164 char parent
[MAXPATHLEN
];
3168 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3169 "cannot promote '%s'"), zhp
->zfs_name
);
3171 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3172 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3173 "snapshots can not be promoted"));
3174 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3177 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3178 if (parent
[0] == '\0') {
3179 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3180 "not a cloned filesystem"));
3181 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3184 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3185 sizeof (zc
.zc_value
));
3186 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3187 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3190 int save_errno
= errno
;
3192 switch (save_errno
) {
3194 /* There is a conflicting snapshot name. */
3195 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3196 "conflicting snapshot '%s' from parent '%s'"),
3197 zc
.zc_string
, parent
);
3198 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3201 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3208 * Takes a snapshot of the given dataset.
3211 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3215 char parent
[ZFS_MAXNAMELEN
];
3217 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3221 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3222 "cannot snapshot '%s'"), path
);
3224 /* validate the target name */
3225 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3226 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3229 if ((props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3230 props
, B_FALSE
, NULL
, errbuf
)) == NULL
)
3233 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3241 /* make sure the parent exists and is of the appropriate type */
3242 delim
= strchr(path
, '@');
3243 (void) strncpy(parent
, path
, delim
- path
);
3244 parent
[delim
- path
] = '\0';
3246 if ((zhp
= zfs_open(hdl
, parent
, ZFS_TYPE_FILESYSTEM
|
3247 ZFS_TYPE_VOLUME
)) == NULL
) {
3248 zcmd_free_nvlists(&zc
);
3252 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3253 (void) strlcpy(zc
.zc_value
, delim
+1, sizeof (zc
.zc_value
));
3254 if (ZFS_IS_VOLUME(zhp
))
3255 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3257 zc
.zc_objset_type
= DMU_OST_ZFS
;
3258 zc
.zc_cookie
= recursive
;
3259 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SNAPSHOT
, &zc
);
3261 zcmd_free_nvlists(&zc
);
3264 * if it was recursive, the one that actually failed will be in
3268 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3269 "cannot create snapshot '%s@%s'"), zc
.zc_name
, zc
.zc_value
);
3270 (void) zfs_standard_error(hdl
, errno
, errbuf
);
3279 * Destroy any more recent snapshots. We invoke this callback on any dependents
3280 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3281 * is a dependent and we should just destroy it without checking the transaction
3284 typedef struct rollback_data
{
3285 const char *cb_target
; /* the snapshot */
3286 uint64_t cb_create
; /* creation time reference */
3288 boolean_t cb_dependent
;
3293 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3295 rollback_data_t
*cbp
= data
;
3297 if (!cbp
->cb_dependent
) {
3298 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3299 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3300 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3304 cbp
->cb_dependent
= B_TRUE
;
3305 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3306 rollback_destroy
, cbp
);
3307 cbp
->cb_dependent
= B_FALSE
;
3309 logstr
= zhp
->zfs_hdl
->libzfs_log_str
;
3310 zhp
->zfs_hdl
->libzfs_log_str
= NULL
;
3311 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3312 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
3315 /* We must destroy this clone; first unmount it */
3316 prop_changelist_t
*clp
;
3318 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3319 cbp
->cb_force
? MS_FORCE
: 0);
3320 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3321 cbp
->cb_error
= B_TRUE
;
3325 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3326 cbp
->cb_error
= B_TRUE
;
3328 changelist_remove(clp
, zhp
->zfs_name
);
3329 (void) changelist_postfix(clp
);
3330 changelist_free(clp
);
3338 * Given a dataset, rollback to a specific snapshot, discarding any
3339 * data changes since then and making it the active dataset.
3341 * Any snapshots more recent than the target are destroyed, along with
3345 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3347 rollback_data_t cb
= { 0 };
3349 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3350 boolean_t restore_resv
= 0;
3351 uint64_t old_volsize
, new_volsize
;
3352 zfs_prop_t resv_prop
;
3354 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3355 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3358 * Destroy all recent snapshots and its dependends.
3360 cb
.cb_force
= force
;
3361 cb
.cb_target
= snap
->zfs_name
;
3362 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3363 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3369 * Now that we have verified that the snapshot is the latest,
3370 * rollback to the given snapshot.
3373 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3374 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3376 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3378 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3381 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3383 if (ZFS_IS_VOLUME(zhp
))
3384 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3386 zc
.zc_objset_type
= DMU_OST_ZFS
;
3389 * We rely on zfs_iter_children() to verify that there are no
3390 * newer snapshots for the given dataset. Therefore, we can
3391 * simply pass the name on to the ioctl() call. There is still
3392 * an unlikely race condition where the user has taken a
3393 * snapshot since we verified that this was the most recent.
3396 if ((err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_ROLLBACK
, &zc
)) != 0) {
3397 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3398 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3404 * For volumes, if the pre-rollback volsize matched the pre-
3405 * rollback reservation and the volsize has changed then set
3406 * the reservation property to the post-rollback volsize.
3407 * Make a new handle since the rollback closed the dataset.
3409 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3410 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3412 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3413 if (old_volsize
!= new_volsize
)
3414 err
= zfs_prop_set_int(zhp
, resv_prop
,
3423 * Iterate over all dependents for a given dataset. This includes both
3424 * hierarchical dependents (children) and data dependents (snapshots and
3425 * clones). The bulk of the processing occurs in get_dependents() in
3429 zfs_iter_dependents(zfs_handle_t
*zhp
, boolean_t allowrecursion
,
3430 zfs_iter_f func
, void *data
)
3435 zfs_handle_t
*child
;
3438 if (get_dependents(zhp
->zfs_hdl
, allowrecursion
, zhp
->zfs_name
,
3439 &dependents
, &count
) != 0)
3442 for (i
= 0; i
< count
; i
++) {
3443 if ((child
= make_dataset_handle(zhp
->zfs_hdl
,
3444 dependents
[i
])) == NULL
)
3447 if ((ret
= func(child
, data
)) != 0)
3451 for (i
= 0; i
< count
; i
++)
3452 free(dependents
[i
]);
3459 * Renames the given dataset.
3462 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
)
3465 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3467 prop_changelist_t
*cl
= NULL
;
3468 zfs_handle_t
*zhrp
= NULL
;
3469 char *parentname
= NULL
;
3470 char parent
[ZFS_MAXNAMELEN
];
3471 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3474 /* if we have the same exact name, just return success */
3475 if (strcmp(zhp
->zfs_name
, target
) == 0)
3478 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3479 "cannot rename to '%s'"), target
);
3482 * Make sure the target name is valid
3484 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3485 if ((strchr(target
, '@') == NULL
) ||
3488 * Snapshot target name is abbreviated,
3489 * reconstruct full dataset name
3491 (void) strlcpy(parent
, zhp
->zfs_name
,
3493 delim
= strchr(parent
, '@');
3494 if (strchr(target
, '@') == NULL
)
3498 (void) strlcat(parent
, target
, sizeof (parent
));
3502 * Make sure we're renaming within the same dataset.
3504 delim
= strchr(target
, '@');
3505 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3506 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3507 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3508 "snapshots must be part of same "
3510 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3514 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3515 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3519 "recursive rename must be a snapshot"));
3520 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3523 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3524 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3526 /* validate parents */
3527 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3530 /* make sure we're in the same pool */
3531 verify((delim
= strchr(target
, '/')) != NULL
);
3532 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3533 zhp
->zfs_name
[delim
- target
] != '/') {
3534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3535 "datasets must be within same pool"));
3536 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3539 /* new name cannot be a child of the current dataset name */
3540 if (is_descendant(zhp
->zfs_name
, target
)) {
3541 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3542 "New dataset name cannot be a descendant of "
3543 "current dataset name"));
3544 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3548 (void) snprintf(errbuf
, sizeof (errbuf
),
3549 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3551 if (getzoneid() == GLOBAL_ZONEID
&&
3552 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3553 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3554 "dataset is used in a non-global zone"));
3555 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3560 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3561 if (parentname
== NULL
) {
3565 delim
= strchr(parentname
, '@');
3567 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3574 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0)) == NULL
)
3577 if (changelist_haszonedchild(cl
)) {
3578 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3579 "child dataset with inherited mountpoint is used "
3580 "in a non-global zone"));
3581 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3585 if ((ret
= changelist_prefix(cl
)) != 0)
3589 if (ZFS_IS_VOLUME(zhp
))
3590 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3592 zc
.zc_objset_type
= DMU_OST_ZFS
;
3594 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3595 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3597 zc
.zc_cookie
= recursive
;
3599 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3601 * if it was recursive, the one that actually failed will
3604 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3605 "cannot rename '%s'"), zc
.zc_name
);
3607 if (recursive
&& errno
== EEXIST
) {
3608 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3609 "a child dataset already has a snapshot "
3610 "with the new name"));
3611 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3613 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3617 * On failure, we still want to remount any filesystems that
3618 * were previously mounted, so we don't alter the system state.
3621 (void) changelist_postfix(cl
);
3624 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3625 ret
= changelist_postfix(cl
);
3637 changelist_free(cl
);
3643 zfs_get_user_props(zfs_handle_t
*zhp
)
3645 return (zhp
->zfs_user_props
);
3649 zfs_get_recvd_props(zfs_handle_t
*zhp
)
3651 if (zhp
->zfs_recvd_props
== NULL
)
3652 if (get_recvd_props_ioctl(zhp
) != 0)
3654 return (zhp
->zfs_recvd_props
);
3658 * This function is used by 'zfs list' to determine the exact set of columns to
3659 * display, and their maximum widths. This does two main things:
3661 * - If this is a list of all properties, then expand the list to include
3662 * all native properties, and set a flag so that for each dataset we look
3663 * for new unique user properties and add them to the list.
3665 * - For non fixed-width properties, keep track of the maximum width seen
3666 * so that we can size the column appropriately. If the user has
3667 * requested received property values, we also need to compute the width
3668 * of the RECEIVED column.
3671 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
)
3673 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3674 zprop_list_t
*entry
;
3675 zprop_list_t
**last
, **start
;
3676 nvlist_t
*userprops
, *propval
;
3679 char buf
[ZFS_MAXPROPLEN
];
3681 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
3684 userprops
= zfs_get_user_props(zhp
);
3687 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
3689 * Go through and add any user properties as necessary. We
3690 * start by incrementing our list pointer to the first
3691 * non-native property.
3694 while (*start
!= NULL
) {
3695 if ((*start
)->pl_prop
== ZPROP_INVAL
)
3697 start
= &(*start
)->pl_next
;
3701 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
3703 * See if we've already found this property in our list.
3705 for (last
= start
; *last
!= NULL
;
3706 last
= &(*last
)->pl_next
) {
3707 if (strcmp((*last
)->pl_user_prop
,
3708 nvpair_name(elem
)) == 0)
3712 if (*last
== NULL
) {
3713 if ((entry
= zfs_alloc(hdl
,
3714 sizeof (zprop_list_t
))) == NULL
||
3715 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
3716 nvpair_name(elem
)))) == NULL
) {
3721 entry
->pl_prop
= ZPROP_INVAL
;
3722 entry
->pl_width
= strlen(nvpair_name(elem
));
3723 entry
->pl_all
= B_TRUE
;
3730 * Now go through and check the width of any non-fixed columns
3732 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
3733 if (entry
->pl_fixed
)
3736 if (entry
->pl_prop
!= ZPROP_INVAL
) {
3737 if (zfs_prop_get(zhp
, entry
->pl_prop
,
3738 buf
, sizeof (buf
), NULL
, NULL
, 0, B_FALSE
) == 0) {
3739 if (strlen(buf
) > entry
->pl_width
)
3740 entry
->pl_width
= strlen(buf
);
3742 if (received
&& zfs_prop_get_recvd(zhp
,
3743 zfs_prop_to_name(entry
->pl_prop
),
3744 buf
, sizeof (buf
), B_FALSE
) == 0)
3745 if (strlen(buf
) > entry
->pl_recvd_width
)
3746 entry
->pl_recvd_width
= strlen(buf
);
3748 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
3750 verify(nvlist_lookup_string(propval
,
3751 ZPROP_VALUE
, &strval
) == 0);
3752 if (strlen(strval
) > entry
->pl_width
)
3753 entry
->pl_width
= strlen(strval
);
3755 if (received
&& zfs_prop_get_recvd(zhp
,
3756 entry
->pl_user_prop
,
3757 buf
, sizeof (buf
), B_FALSE
) == 0)
3758 if (strlen(buf
) > entry
->pl_recvd_width
)
3759 entry
->pl_recvd_width
= strlen(buf
);
3767 zfs_deleg_share_nfs(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
3768 char *resource
, void *export
, void *sharetab
,
3769 int sharemax
, zfs_share_op_t operation
)
3771 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3774 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3775 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3777 (void) strlcpy(zc
.zc_string
, resource
, sizeof (zc
.zc_string
));
3778 zc
.zc_share
.z_sharedata
= (uint64_t)(uintptr_t)sharetab
;
3779 zc
.zc_share
.z_exportdata
= (uint64_t)(uintptr_t)export
;
3780 zc
.zc_share
.z_sharetype
= operation
;
3781 zc
.zc_share
.z_sharemax
= sharemax
;
3782 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SHARE
, &zc
);
3787 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
3792 * Keep a reference to the props-table against which we prune the
3795 zhp
->zfs_props_table
= props
;
3797 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
3800 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
3801 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
3804 * User properties will result in ZPROP_INVAL, and since we
3805 * only know how to prune standard ZFS properties, we always
3806 * leave these in the list. This can also happen if we
3807 * encounter an unknown DSL property (when running older
3808 * software, for example).
3810 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
3811 (void) nvlist_remove(zhp
->zfs_props
,
3812 nvpair_name(curr
), nvpair_type(curr
));
3818 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
3819 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
3821 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3822 nvlist_t
*nvlist
= NULL
;
3825 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3826 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3827 zc
.zc_cookie
= (uint64_t)cmd
;
3829 if (cmd
== ZFS_SMB_ACL_RENAME
) {
3830 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
3831 (void) no_memory(hdl
);
3837 case ZFS_SMB_ACL_ADD
:
3838 case ZFS_SMB_ACL_REMOVE
:
3839 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
3841 case ZFS_SMB_ACL_RENAME
:
3842 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
3844 (void) no_memory(hdl
);
3847 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
3849 (void) no_memory(hdl
);
3852 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
3853 nvlist_free(nvlist
);
3857 case ZFS_SMB_ACL_PURGE
:
3862 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
3864 nvlist_free(nvlist
);
3869 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
3870 char *path
, char *resource
)
3872 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
3877 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
3878 char *path
, char *resource
)
3880 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
3885 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
3887 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
3892 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
3893 char *oldname
, char *newname
)
3895 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
3900 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
3901 zfs_userspace_cb_t func
, void *arg
)
3903 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3905 zfs_useracct_t buf
[100];
3907 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3909 zc
.zc_objset_type
= type
;
3910 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
3914 zfs_useracct_t
*zua
= buf
;
3916 zc
.zc_nvlist_dst_size
= sizeof (buf
);
3917 error
= ioctl(zhp
->zfs_hdl
->libzfs_fd
,
3918 ZFS_IOC_USERSPACE_MANY
, &zc
);
3919 if (error
|| zc
.zc_nvlist_dst_size
== 0)
3922 while (zc
.zc_nvlist_dst_size
> 0) {
3923 error
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
3928 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
3936 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
3937 boolean_t recursive
, boolean_t temphold
, boolean_t enoent_ok
,
3938 int cleanup_fd
, uint64_t dsobj
, uint64_t createtxg
)
3940 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3941 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3943 ASSERT(!recursive
|| dsobj
== 0);
3945 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3946 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3947 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
3948 >= sizeof (zc
.zc_string
))
3949 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
3950 zc
.zc_cookie
= recursive
;
3951 zc
.zc_temphold
= temphold
;
3952 zc
.zc_cleanup_fd
= cleanup_fd
;
3953 zc
.zc_sendobj
= dsobj
;
3954 zc
.zc_createtxg
= createtxg
;
3956 if (zfs_ioctl(hdl
, ZFS_IOC_HOLD
, &zc
) != 0) {
3957 char errbuf
[ZFS_MAXNAMELEN
+32];
3960 * if it was recursive, the one that actually failed will be in
3963 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3964 "cannot hold '%s@%s'"), zc
.zc_name
, snapname
);
3968 * Temporary tags wind up having the ds object id
3969 * prepended. So even if we passed the length check
3970 * above, it's still possible for the tag to wind
3971 * up being slightly too long.
3973 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
));
3975 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3976 "pool must be upgraded"));
3977 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3979 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3981 return (zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
));
3987 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
3995 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
3996 boolean_t recursive
)
3998 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3999 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4001 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4002 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4003 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4004 >= sizeof (zc
.zc_string
))
4005 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4006 zc
.zc_cookie
= recursive
;
4008 if (zfs_ioctl(hdl
, ZFS_IOC_RELEASE
, &zc
) != 0) {
4009 char errbuf
[ZFS_MAXNAMELEN
+32];
4012 * if it was recursive, the one that actually failed will be in
4015 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4016 "cannot release '%s' from '%s@%s'"), tag
, zc
.zc_name
,
4020 return (zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
));
4022 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4023 "pool must be upgraded"));
4024 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4026 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4028 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4036 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4039 uint64_t nblocks
, volblocksize
;
4043 if (nvlist_lookup_string(props
,
4044 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4045 ncopies
= atoi(strval
);
4048 if (nvlist_lookup_uint64(props
,
4049 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4050 &volblocksize
) != 0)
4051 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4052 nblocks
= volsize
/volblocksize
;
4053 /* start with metadnode L0-L6 */
4055 /* calculate number of indirects */
4056 while (nblocks
> 1) {
4057 nblocks
+= DNODES_PER_LEVEL
- 1;
4058 nblocks
/= DNODES_PER_LEVEL
;
4061 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4064 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4065 * compressed, but in practice they compress down to about
4068 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;