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 2010 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
39 #include <sys/mntent.h>
40 #include <sys/mount.h>
48 #include <directory.h>
50 #include <sys/dnode.h>
55 #include "zfs_namecheck.h"
57 #include "libzfs_impl.h"
58 #include "zfs_deleg.h"
60 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
61 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
64 * Given a single type (not a mask of types), return the type in a human
68 zfs_type_to_name(zfs_type_t type
)
71 case ZFS_TYPE_FILESYSTEM
:
72 return (dgettext(TEXT_DOMAIN
, "filesystem"));
73 case ZFS_TYPE_SNAPSHOT
:
74 return (dgettext(TEXT_DOMAIN
, "snapshot"));
76 return (dgettext(TEXT_DOMAIN
, "volume"));
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 (void) zfs_prop_get_table();
138 if (dataset_namecheck(path
, &why
, &what
) != 0) {
141 case NAME_ERR_TOOLONG
:
142 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
143 "name is too long"));
146 case NAME_ERR_LEADING_SLASH
:
147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
148 "leading slash in name"));
151 case NAME_ERR_EMPTY_COMPONENT
:
152 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
153 "empty component in name"));
156 case NAME_ERR_TRAILING_SLASH
:
157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
158 "trailing slash in name"));
161 case NAME_ERR_INVALCHAR
:
163 dgettext(TEXT_DOMAIN
, "invalid character "
164 "'%c' in name"), what
);
167 case NAME_ERR_MULTIPLE_AT
:
168 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
169 "multiple '@' delimiters in name"));
172 case NAME_ERR_NOLETTER
:
173 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
174 "pool doesn't begin with a letter"));
177 case NAME_ERR_RESERVED
:
178 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
179 "name is reserved"));
182 case NAME_ERR_DISKLIKE
:
183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
184 "reserved disk name"));
192 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
194 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
195 "snapshot delimiter '@' in filesystem name"));
199 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
201 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
202 "missing '@' delimiter in snapshot name"));
206 if (modifying
&& strchr(path
, '%') != NULL
) {
208 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
209 "invalid character %c in name"), '%');
217 zfs_name_valid(const char *name
, zfs_type_t type
)
219 if (type
== ZFS_TYPE_POOL
)
220 return (zpool_name_valid(NULL
, B_FALSE
, name
));
221 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
225 * This function takes the raw DSL properties, and filters out the user-defined
226 * properties into a separate nvlist.
229 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
231 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
236 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
237 (void) no_memory(hdl
);
242 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
243 if (!zfs_prop_user(nvpair_name(elem
)))
246 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
247 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
249 (void) no_memory(hdl
);
257 static zpool_handle_t
*
258 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
260 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
263 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
264 if (hdl
->libzfs_pool_handles
!= NULL
)
265 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
266 hdl
->libzfs_pool_handles
= zph
;
271 static zpool_handle_t
*
272 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
274 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
275 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
277 while ((zph
!= NULL
) &&
278 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
279 zph
= zph
->zpool_next
;
284 * Returns a handle to the pool that contains the provided dataset.
285 * If a handle to that pool already exists then that handle is returned.
286 * Otherwise, a new handle is created and added to the list of handles.
288 static zpool_handle_t
*
289 zpool_handle(zfs_handle_t
*zhp
)
295 len
= strcspn(zhp
->zfs_name
, "/@") + 1;
296 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
297 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
299 zph
= zpool_find_handle(zhp
, pool_name
, len
);
301 zph
= zpool_add_handle(zhp
, pool_name
);
308 zpool_free_handles(libzfs_handle_t
*hdl
)
310 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
312 while (zph
!= NULL
) {
313 next
= zph
->zpool_next
;
317 hdl
->libzfs_pool_handles
= NULL
;
321 * Utility function to gather stats (objset and zpl) for the given object.
324 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
326 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
328 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
330 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
331 if (errno
== ENOMEM
) {
332 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
343 * Utility function to get the received properties of the given object.
346 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
348 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
349 nvlist_t
*recvdprops
;
350 zfs_cmd_t zc
= { 0 };
353 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
356 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
358 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
359 if (errno
== ENOMEM
) {
360 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
364 zcmd_free_nvlists(&zc
);
369 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
370 zcmd_free_nvlists(&zc
);
374 nvlist_free(zhp
->zfs_recvd_props
);
375 zhp
->zfs_recvd_props
= recvdprops
;
381 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
383 nvlist_t
*allprops
, *userprops
;
385 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
387 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
392 * XXX Why do we store the user props separately, in addition to
393 * storing them in zfs_props?
395 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
396 nvlist_free(allprops
);
400 nvlist_free(zhp
->zfs_props
);
401 nvlist_free(zhp
->zfs_user_props
);
403 zhp
->zfs_props
= allprops
;
404 zhp
->zfs_user_props
= userprops
;
410 get_stats(zfs_handle_t
*zhp
)
413 zfs_cmd_t zc
= { 0 };
415 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
417 if (get_stats_ioctl(zhp
, &zc
) != 0)
419 else if (put_stats_zhdl(zhp
, &zc
) != 0)
421 zcmd_free_nvlists(&zc
);
426 * Refresh the properties currently stored in the handle.
429 zfs_refresh_properties(zfs_handle_t
*zhp
)
431 (void) get_stats(zhp
);
435 * Makes a handle from the given dataset name. Used by zfs_open() and
436 * zfs_iter_* to create child handles on the fly.
439 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
441 if (put_stats_zhdl(zhp
, zc
) != 0)
445 * We've managed to open the dataset and gather statistics. Determine
446 * the high-level type.
448 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
449 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
450 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
451 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
455 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
456 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
457 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
458 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
459 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
460 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
462 abort(); /* we should never see any other types */
464 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
471 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
473 zfs_cmd_t zc
= { 0 };
475 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
481 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
482 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
486 if (get_stats_ioctl(zhp
, &zc
) == -1) {
487 zcmd_free_nvlists(&zc
);
491 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
495 zcmd_free_nvlists(&zc
);
500 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
502 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
508 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
509 if (make_dataset_handle_common(zhp
, zc
) == -1) {
517 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
519 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
524 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
525 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
526 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
527 sizeof (zhp
->zfs_name
));
528 zhp
->zfs_type
= zhp_orig
->zfs_type
;
529 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
530 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
531 if (zhp_orig
->zfs_props
!= NULL
) {
532 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
533 (void) no_memory(zhp
->zfs_hdl
);
538 if (zhp_orig
->zfs_user_props
!= NULL
) {
539 if (nvlist_dup(zhp_orig
->zfs_user_props
,
540 &zhp
->zfs_user_props
, 0) != 0) {
541 (void) no_memory(zhp
->zfs_hdl
);
546 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
547 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
548 &zhp
->zfs_recvd_props
, 0)) {
549 (void) no_memory(zhp
->zfs_hdl
);
554 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
555 if (zhp_orig
->zfs_mntopts
!= NULL
) {
556 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
557 zhp_orig
->zfs_mntopts
);
559 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
564 * Opens the given snapshot, filesystem, or volume. The 'types'
565 * argument is a mask of acceptable types. The function will print an
566 * appropriate error message and return NULL if it can't be opened.
569 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
574 (void) snprintf(errbuf
, sizeof (errbuf
),
575 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
578 * Validate the name before we even try to open it.
580 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
582 "invalid dataset name"));
583 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
588 * Try to get stats for the dataset, which will tell us if it exists.
591 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
592 (void) zfs_standard_error(hdl
, errno
, errbuf
);
596 if (!(types
& zhp
->zfs_type
)) {
597 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
606 * Release a ZFS handle. Nothing to do but free the associated memory.
609 zfs_close(zfs_handle_t
*zhp
)
611 if (zhp
->zfs_mntopts
)
612 free(zhp
->zfs_mntopts
);
613 nvlist_free(zhp
->zfs_props
);
614 nvlist_free(zhp
->zfs_user_props
);
615 nvlist_free(zhp
->zfs_recvd_props
);
619 typedef struct mnttab_node
{
620 struct mnttab mtn_mt
;
625 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
627 const mnttab_node_t
*mtn1
= arg1
;
628 const mnttab_node_t
*mtn2
= arg2
;
631 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
635 return (rv
> 0 ? 1 : -1);
639 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
641 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
642 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
643 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
647 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
651 rewind(hdl
->libzfs_mnttab
);
652 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
655 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
657 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
658 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
659 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
660 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
661 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
662 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
667 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
672 while (mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
)) {
673 free(mtn
->mtn_mt
.mnt_special
);
674 free(mtn
->mtn_mt
.mnt_mountp
);
675 free(mtn
->mtn_mt
.mnt_fstype
);
676 free(mtn
->mtn_mt
.mnt_mntopts
);
679 avl_destroy(&hdl
->libzfs_mnttab_cache
);
683 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
685 hdl
->libzfs_mnttab_enable
= enable
;
689 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
690 struct mnttab
*entry
)
695 if (!hdl
->libzfs_mnttab_enable
) {
696 struct mnttab srch
= { 0 };
698 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
699 libzfs_mnttab_fini(hdl
);
700 rewind(hdl
->libzfs_mnttab
);
701 srch
.mnt_special
= (char *)fsname
;
702 srch
.mnt_fstype
= MNTTYPE_ZFS
;
703 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
709 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
710 libzfs_mnttab_update(hdl
);
712 find
.mtn_mt
.mnt_special
= (char *)fsname
;
713 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
715 *entry
= mtn
->mtn_mt
;
722 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
723 const char *mountp
, const char *mntopts
)
727 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
729 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
730 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
731 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
732 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
733 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
734 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
738 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
743 find
.mtn_mt
.mnt_special
= (char *)fsname
;
744 if (ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
)) {
745 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
746 free(ret
->mtn_mt
.mnt_special
);
747 free(ret
->mtn_mt
.mnt_mountp
);
748 free(ret
->mtn_mt
.mnt_fstype
);
749 free(ret
->mtn_mt
.mnt_mntopts
);
755 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
757 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
759 if (zpool_handle
== NULL
)
762 *spa_version
= zpool_get_prop_int(zpool_handle
,
763 ZPOOL_PROP_VERSION
, NULL
);
768 * The choice of reservation property depends on the SPA version.
771 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
775 if (zfs_spa_version(zhp
, &spa_version
) < 0)
778 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
779 *resv_prop
= ZFS_PROP_REFRESERVATION
;
781 *resv_prop
= ZFS_PROP_RESERVATION
;
787 * Given an nvlist of properties to set, validates that they are correct, and
788 * parses any numeric properties (index, boolean, etc) if they are specified as
792 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
793 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
800 int chosen_normal
= -1;
803 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
804 (void) no_memory(hdl
);
809 * Make sure this property is valid and applies to this type.
813 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
814 const char *propname
= nvpair_name(elem
);
816 prop
= zfs_name_to_prop(propname
);
817 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
819 * This is a user property: make sure it's a
820 * string, and that it's less than ZAP_MAXNAMELEN.
822 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
823 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
824 "'%s' must be a string"), propname
);
825 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
829 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
830 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
831 "property name '%s' is too long"),
833 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
837 (void) nvpair_value_string(elem
, &strval
);
838 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
839 (void) no_memory(hdl
);
846 * Currently, only user properties can be modified on
849 if (type
== ZFS_TYPE_SNAPSHOT
) {
850 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
851 "this property can not be modified for snapshots"));
852 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
856 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
857 zfs_userquota_prop_t uqtype
;
858 char newpropname
[128];
863 if (userquota_propname_decode(propname
, zoned
,
864 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
866 dgettext(TEXT_DOMAIN
,
867 "'%s' has an invalid user/group name"),
869 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
873 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
874 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
876 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
878 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
883 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
884 (void) nvpair_value_string(elem
, &strval
);
885 if (strcmp(strval
, "none") == 0) {
887 } else if (zfs_nicestrtonum(hdl
,
888 strval
, &intval
) != 0) {
889 (void) zfs_error(hdl
,
890 EZFS_BADPROP
, errbuf
);
893 } else if (nvpair_type(elem
) ==
895 (void) nvpair_value_uint64(elem
, &intval
);
897 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
898 "use 'none' to disable "
899 "userquota/groupquota"));
903 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
904 "'%s' must be a number"), propname
);
905 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
910 * Encode the prop name as
911 * userquota@<hex-rid>-domain, to make it easy
912 * for the kernel to decode.
914 (void) snprintf(newpropname
, sizeof (newpropname
),
915 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
916 (longlong_t
)rid
, domain
);
920 if (nvlist_add_uint64_array(ret
, newpropname
,
922 (void) no_memory(hdl
);
926 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
927 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
930 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
934 if (prop
== ZPROP_INVAL
) {
935 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
936 "invalid property '%s'"), propname
);
937 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
941 if (!zfs_prop_valid_for_type(prop
, type
)) {
943 dgettext(TEXT_DOMAIN
, "'%s' does not "
944 "apply to datasets of this type"), propname
);
945 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
949 if (zfs_prop_readonly(prop
) &&
950 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
952 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
954 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
958 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
959 &strval
, &intval
, errbuf
) != 0)
963 * Perform some additional checks for specific properties.
966 case ZFS_PROP_VERSION
:
972 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
973 if (intval
< version
) {
974 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
975 "Can not downgrade; already at version %u"),
977 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
983 case ZFS_PROP_RECORDSIZE
:
984 case ZFS_PROP_VOLBLOCKSIZE
:
985 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
986 if (intval
< SPA_MINBLOCKSIZE
||
987 intval
> SPA_MAXBLOCKSIZE
|| !ISP2(intval
)) {
988 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
989 "'%s' must be power of 2 from %u "
991 (uint_t
)SPA_MINBLOCKSIZE
,
992 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
993 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
998 case ZFS_PROP_MLSLABEL
:
1001 * Verify the mlslabel string and convert to
1002 * internal hex label string.
1006 char *hex
= NULL
; /* internal label string */
1008 /* Default value is already OK. */
1009 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1012 /* Verify the label can be converted to binary form */
1013 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1014 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1015 L_NO_CORRECTION
, NULL
) == -1)) {
1019 /* Now translate to hex internal label string */
1020 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1026 m_label_free(new_sl
);
1028 /* If string is already in internal form, we're done. */
1029 if (strcmp(strval
, hex
) == 0) {
1034 /* Replace the label string with the internal form. */
1035 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1037 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1044 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1045 "invalid mlslabel '%s'"), strval
);
1046 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1047 m_label_free(new_sl
); /* OK if null */
1052 case ZFS_PROP_MOUNTPOINT
:
1054 namecheck_err_t why
;
1056 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1057 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1060 if (mountpoint_namecheck(strval
, &why
)) {
1062 case NAME_ERR_LEADING_SLASH
:
1064 dgettext(TEXT_DOMAIN
,
1065 "'%s' must be an absolute path, "
1066 "'none', or 'legacy'"), propname
);
1068 case NAME_ERR_TOOLONG
:
1070 dgettext(TEXT_DOMAIN
,
1071 "component of '%s' is too long"),
1075 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1082 case ZFS_PROP_SHARESMB
:
1083 case ZFS_PROP_SHARENFS
:
1085 * For the mountpoint and sharenfs or sharesmb
1086 * properties, check if it can be set in a
1087 * global/non-global zone based on
1088 * the zoned property value:
1090 * global zone non-global zone
1091 * --------------------------------------------------
1092 * zoned=on mountpoint (no) mountpoint (yes)
1093 * sharenfs (no) sharenfs (no)
1094 * sharesmb (no) sharesmb (no)
1096 * zoned=off mountpoint (yes) N/A
1101 if (getzoneid() == GLOBAL_ZONEID
) {
1102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1103 "'%s' cannot be set on "
1104 "dataset in a non-global zone"),
1106 (void) zfs_error(hdl
, EZFS_ZONED
,
1109 } else if (prop
== ZFS_PROP_SHARENFS
||
1110 prop
== ZFS_PROP_SHARESMB
) {
1111 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1112 "'%s' cannot be set in "
1113 "a non-global zone"), propname
);
1114 (void) zfs_error(hdl
, EZFS_ZONED
,
1118 } else if (getzoneid() != GLOBAL_ZONEID
) {
1120 * If zoned property is 'off', this must be in
1121 * a global zone. If not, something is wrong.
1123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1124 "'%s' cannot be set while dataset "
1125 "'zoned' property is set"), propname
);
1126 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1131 * At this point, it is legitimate to set the
1132 * property. Now we want to make sure that the
1133 * property value is valid if it is sharenfs.
1135 if ((prop
== ZFS_PROP_SHARENFS
||
1136 prop
== ZFS_PROP_SHARESMB
) &&
1137 strcmp(strval
, "on") != 0 &&
1138 strcmp(strval
, "off") != 0) {
1139 zfs_share_proto_t proto
;
1141 if (prop
== ZFS_PROP_SHARESMB
)
1147 * Must be an valid sharing protocol
1148 * option string so init the libshare
1149 * in order to enable the parser and
1150 * then parse the options. We use the
1151 * control API since we don't care about
1152 * the current configuration and don't
1153 * want the overhead of loading it
1154 * until we actually do something.
1157 if (zfs_init_libshare(hdl
,
1158 SA_INIT_CONTROL_API
) != SA_OK
) {
1160 * An error occurred so we can't do
1163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1164 "'%s' cannot be set: problem "
1165 "in share initialization"),
1167 (void) zfs_error(hdl
, EZFS_BADPROP
,
1172 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1174 * There was an error in parsing so
1175 * deal with it by issuing an error
1176 * message and leaving after
1177 * uninitializing the the libshare
1180 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1181 "'%s' cannot be set to invalid "
1182 "options"), propname
);
1183 (void) zfs_error(hdl
, EZFS_BADPROP
,
1185 zfs_uninit_libshare(hdl
);
1188 zfs_uninit_libshare(hdl
);
1192 case ZFS_PROP_UTF8ONLY
:
1193 chosen_utf
= (int)intval
;
1195 case ZFS_PROP_NORMALIZE
:
1196 chosen_normal
= (int)intval
;
1201 * For changes to existing volumes, we have some additional
1202 * checks to enforce.
1204 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1205 uint64_t volsize
= zfs_prop_get_int(zhp
,
1207 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1208 ZFS_PROP_VOLBLOCKSIZE
);
1212 case ZFS_PROP_RESERVATION
:
1213 case ZFS_PROP_REFRESERVATION
:
1214 if (intval
> volsize
) {
1215 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1216 "'%s' is greater than current "
1217 "volume size"), propname
);
1218 (void) zfs_error(hdl
, EZFS_BADPROP
,
1224 case ZFS_PROP_VOLSIZE
:
1225 if (intval
% blocksize
!= 0) {
1226 zfs_nicenum(blocksize
, buf
,
1228 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1229 "'%s' must be a multiple of "
1230 "volume block size (%s)"),
1232 (void) zfs_error(hdl
, EZFS_BADPROP
,
1238 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1239 "'%s' cannot be zero"),
1241 (void) zfs_error(hdl
, EZFS_BADPROP
,
1251 * If normalization was chosen, but no UTF8 choice was made,
1252 * enforce rejection of non-UTF8 names.
1254 * If normalization was chosen, but rejecting non-UTF8 names
1255 * was explicitly not chosen, it is an error.
1257 if (chosen_normal
> 0 && chosen_utf
< 0) {
1258 if (nvlist_add_uint64(ret
,
1259 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1260 (void) no_memory(hdl
);
1263 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1264 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1265 "'%s' must be set 'on' if normalization chosen"),
1266 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1267 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1278 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1280 uint64_t old_volsize
;
1281 uint64_t new_volsize
;
1282 uint64_t old_reservation
;
1283 uint64_t new_reservation
;
1284 zfs_prop_t resv_prop
;
1287 * If this is an existing volume, and someone is setting the volsize,
1288 * make sure that it matches the reservation, or add it if necessary.
1290 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1291 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1293 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1294 if ((zvol_volsize_to_reservation(old_volsize
, zhp
->zfs_props
) !=
1295 old_reservation
) || nvlist_lookup_uint64(nvl
,
1296 zfs_prop_to_name(resv_prop
), &new_reservation
) != ENOENT
) {
1299 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1302 new_reservation
= zvol_volsize_to_reservation(new_volsize
,
1304 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1305 new_reservation
) != 0) {
1306 (void) no_memory(zhp
->zfs_hdl
);
1313 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1320 * For quotas and reservations, ENOSPC indicates
1321 * something different; setting a quota or reservation
1322 * doesn't use any disk space.
1325 case ZFS_PROP_QUOTA
:
1326 case ZFS_PROP_REFQUOTA
:
1327 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1328 "size is less than current used or "
1330 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1333 case ZFS_PROP_RESERVATION
:
1334 case ZFS_PROP_REFRESERVATION
:
1335 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1336 "size is greater than available space"));
1337 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1341 (void) zfs_standard_error(hdl
, err
, errbuf
);
1347 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1351 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1355 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1356 "pool and or dataset must be upgraded to set this "
1357 "property or value"));
1358 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1362 if (prop
== ZFS_PROP_COMPRESSION
) {
1363 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1364 "property setting is not allowed on "
1365 "bootable datasets"));
1366 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1368 (void) zfs_standard_error(hdl
, err
, errbuf
);
1373 if (prop
== ZPROP_INVAL
) {
1374 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1376 (void) zfs_standard_error(hdl
, err
, errbuf
);
1382 * This platform can't address a volume this big.
1385 if (prop
== ZFS_PROP_VOLSIZE
) {
1386 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1392 (void) zfs_standard_error(hdl
, err
, errbuf
);
1397 * Given a property name and value, set the property for the given dataset.
1400 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1402 zfs_cmd_t zc
= { 0 };
1404 prop_changelist_t
*cl
= NULL
;
1406 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1407 nvlist_t
*nvl
= NULL
, *realprops
;
1409 boolean_t do_prefix
;
1413 (void) snprintf(errbuf
, sizeof (errbuf
),
1414 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1417 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1418 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1419 (void) no_memory(hdl
);
1423 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1424 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1430 prop
= zfs_name_to_prop(propname
);
1432 if (prop
== ZFS_PROP_VOLSIZE
) {
1433 if ((added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1)
1437 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1440 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1441 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1442 "child dataset with inherited mountpoint is used "
1443 "in a non-global zone"));
1444 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1449 * If the dataset's canmount property is being set to noauto,
1450 * then we want to prevent unmounting & remounting it.
1452 do_prefix
= !((prop
== ZFS_PROP_CANMOUNT
) &&
1453 (zprop_string_to_index(prop
, propval
, &idx
,
1454 ZFS_TYPE_DATASET
) == 0) && (idx
== ZFS_CANMOUNT_NOAUTO
));
1456 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1460 * Execute the corresponding ioctl() to set this property.
1462 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1464 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1467 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1470 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1471 if (added_resv
&& errno
== ENOSPC
) {
1472 /* clean up the volsize property we tried to set */
1473 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1476 zcmd_free_nvlists(&zc
);
1477 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1479 if (nvlist_add_uint64(nvl
,
1480 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1483 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1485 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1489 ret
= changelist_postfix(cl
);
1492 * Refresh the statistics so the new property value
1496 (void) get_stats(zhp
);
1501 zcmd_free_nvlists(&zc
);
1503 changelist_free(cl
);
1508 * Given a property, inherit the value from the parent dataset, or if received
1509 * is TRUE, revert to the received value, if any.
1512 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1514 zfs_cmd_t zc
= { 0 };
1516 prop_changelist_t
*cl
;
1517 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1521 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1522 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1524 zc
.zc_cookie
= received
;
1525 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1527 * For user properties, the amount of work we have to do is very
1528 * small, so just do it here.
1530 if (!zfs_prop_user(propname
)) {
1531 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1532 "invalid property"));
1533 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1536 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1537 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1539 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1540 return (zfs_standard_error(hdl
, errno
, errbuf
));
1546 * Verify that this property is inheritable.
1548 if (zfs_prop_readonly(prop
))
1549 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1551 if (!zfs_prop_inheritable(prop
) && !received
)
1552 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1555 * Check to see if the value applies to this type
1557 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1558 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1561 * Normalize the name, to get rid of shorthand abbreviations.
1563 propname
= zfs_prop_to_name(prop
);
1564 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1565 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1567 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1568 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1569 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1570 "dataset is used in a non-global zone"));
1571 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1575 * Determine datasets which will be affected by this change, if any.
1577 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1580 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1582 "child dataset with inherited mountpoint is used "
1583 "in a non-global zone"));
1584 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1588 if ((ret
= changelist_prefix(cl
)) != 0)
1591 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1592 return (zfs_standard_error(hdl
, errno
, errbuf
));
1595 if ((ret
= changelist_postfix(cl
)) != 0)
1599 * Refresh the statistics so the new property is reflected.
1601 (void) get_stats(zhp
);
1605 changelist_free(cl
);
1610 * True DSL properties are stored in an nvlist. The following two functions
1611 * extract them appropriately.
1614 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1620 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1621 zfs_prop_to_name(prop
), &nv
) == 0) {
1622 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1623 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1625 verify(!zhp
->zfs_props_table
||
1626 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1627 value
= zfs_prop_default_numeric(prop
);
1635 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1641 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1642 zfs_prop_to_name(prop
), &nv
) == 0) {
1643 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1644 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1646 verify(!zhp
->zfs_props_table
||
1647 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1648 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1657 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1659 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1663 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1665 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1666 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1670 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1672 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1677 * Internal function for getting a numeric property. Both zfs_prop_get() and
1678 * zfs_prop_get_int() are built using this interface.
1680 * Certain properties can be overridden using 'mount -o'. In this case, scan
1681 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1682 * If they differ from the on-disk values, report the current values and mark
1683 * the source "temporary".
1686 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1687 char **source
, uint64_t *val
)
1689 zfs_cmd_t zc
= { 0 };
1690 nvlist_t
*zplprops
= NULL
;
1692 char *mntopt_on
= NULL
;
1693 char *mntopt_off
= NULL
;
1694 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1699 case ZFS_PROP_ATIME
:
1700 mntopt_on
= MNTOPT_ATIME
;
1701 mntopt_off
= MNTOPT_NOATIME
;
1704 case ZFS_PROP_DEVICES
:
1705 mntopt_on
= MNTOPT_DEVICES
;
1706 mntopt_off
= MNTOPT_NODEVICES
;
1710 mntopt_on
= MNTOPT_EXEC
;
1711 mntopt_off
= MNTOPT_NOEXEC
;
1714 case ZFS_PROP_READONLY
:
1715 mntopt_on
= MNTOPT_RO
;
1716 mntopt_off
= MNTOPT_RW
;
1719 case ZFS_PROP_SETUID
:
1720 mntopt_on
= MNTOPT_SETUID
;
1721 mntopt_off
= MNTOPT_NOSETUID
;
1724 case ZFS_PROP_XATTR
:
1725 mntopt_on
= MNTOPT_XATTR
;
1726 mntopt_off
= MNTOPT_NOXATTR
;
1729 case ZFS_PROP_NBMAND
:
1730 mntopt_on
= MNTOPT_NBMAND
;
1731 mntopt_off
= MNTOPT_NONBMAND
;
1736 * Because looking up the mount options is potentially expensive
1737 * (iterating over all of /etc/mnttab), we defer its calculation until
1738 * we're looking up a property which requires its presence.
1740 if (!zhp
->zfs_mntcheck
&&
1741 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1742 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1743 struct mnttab entry
;
1745 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1746 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1748 if (zhp
->zfs_mntopts
== NULL
)
1752 zhp
->zfs_mntcheck
= B_TRUE
;
1755 if (zhp
->zfs_mntopts
== NULL
)
1756 mnt
.mnt_mntopts
= "";
1758 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
1761 case ZFS_PROP_ATIME
:
1762 case ZFS_PROP_DEVICES
:
1764 case ZFS_PROP_READONLY
:
1765 case ZFS_PROP_SETUID
:
1766 case ZFS_PROP_XATTR
:
1767 case ZFS_PROP_NBMAND
:
1768 *val
= getprop_uint64(zhp
, prop
, source
);
1773 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
1776 *src
= ZPROP_SRC_TEMPORARY
;
1777 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
1780 *src
= ZPROP_SRC_TEMPORARY
;
1784 case ZFS_PROP_CANMOUNT
:
1785 case ZFS_PROP_VOLSIZE
:
1786 case ZFS_PROP_QUOTA
:
1787 case ZFS_PROP_REFQUOTA
:
1788 case ZFS_PROP_RESERVATION
:
1789 case ZFS_PROP_REFRESERVATION
:
1790 *val
= getprop_uint64(zhp
, prop
, source
);
1792 if (*source
== NULL
) {
1793 /* not default, must be local */
1794 *source
= zhp
->zfs_name
;
1798 case ZFS_PROP_MOUNTED
:
1799 *val
= (zhp
->zfs_mntopts
!= NULL
);
1802 case ZFS_PROP_NUMCLONES
:
1803 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
1806 case ZFS_PROP_VERSION
:
1807 case ZFS_PROP_NORMALIZE
:
1808 case ZFS_PROP_UTF8ONLY
:
1810 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
1811 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1813 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1814 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
1815 zcmd_free_nvlists(&zc
);
1818 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
1819 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
1821 zcmd_free_nvlists(&zc
);
1825 nvlist_free(zplprops
);
1826 zcmd_free_nvlists(&zc
);
1830 switch (zfs_prop_get_type(prop
)) {
1831 case PROP_TYPE_NUMBER
:
1832 case PROP_TYPE_INDEX
:
1833 *val
= getprop_uint64(zhp
, prop
, source
);
1835 * If we tried to use a default value for a
1836 * readonly property, it means that it was not
1839 if (zfs_prop_readonly(prop
) &&
1840 *source
!= NULL
&& (*source
)[0] == '\0') {
1845 case PROP_TYPE_STRING
:
1847 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1848 "cannot get non-numeric property"));
1849 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
1850 dgettext(TEXT_DOMAIN
, "internal error")));
1858 * Calculate the source type, given the raw source string.
1861 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
1862 char *statbuf
, size_t statlen
)
1864 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
1867 if (source
== NULL
) {
1868 *srctype
= ZPROP_SRC_NONE
;
1869 } else if (source
[0] == '\0') {
1870 *srctype
= ZPROP_SRC_DEFAULT
;
1871 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
1872 *srctype
= ZPROP_SRC_RECEIVED
;
1874 if (strcmp(source
, zhp
->zfs_name
) == 0) {
1875 *srctype
= ZPROP_SRC_LOCAL
;
1877 (void) strlcpy(statbuf
, source
, statlen
);
1878 *srctype
= ZPROP_SRC_INHERITED
;
1885 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
1886 size_t proplen
, boolean_t literal
)
1891 if (zhp
->zfs_recvd_props
== NULL
)
1892 if (get_recvd_props_ioctl(zhp
) != 0)
1895 prop
= zfs_name_to_prop(propname
);
1897 if (prop
!= ZPROP_INVAL
) {
1899 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
1901 zfs_set_recvd_props_mode(zhp
, &cookie
);
1902 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
1903 NULL
, NULL
, 0, literal
);
1904 zfs_unset_recvd_props_mode(zhp
, &cookie
);
1908 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
1909 propname
, &propval
) != 0)
1911 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
1913 (void) strlcpy(propbuf
, recvdval
, proplen
);
1916 return (err
== 0 ? 0 : -1);
1920 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
1925 value
= zfs_get_clones_nvl(zhp
);
1930 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
1931 pair
= nvlist_next_nvpair(value
, pair
)) {
1932 if (propbuf
[0] != '\0')
1933 (void) strlcat(propbuf
, ",", proplen
);
1934 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
1940 struct get_clones_arg
{
1944 char buf
[ZFS_MAXNAMELEN
];
1948 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
1950 struct get_clones_arg
*gca
= arg
;
1952 if (gca
->numclones
== 0) {
1957 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
1958 NULL
, NULL
, 0, B_TRUE
) != 0)
1960 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
1961 if (nvlist_add_boolean(gca
->value
, zfs_get_name(zhp
)) != 0) {
1963 return (no_memory(zhp
->zfs_hdl
));
1969 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
1975 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
1977 nvlist_t
*nv
, *value
;
1979 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1980 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
1981 struct get_clones_arg gca
;
1984 * if this is a snapshot, then the kernel wasn't able
1985 * to get the clones. Do it by slowly iterating.
1987 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
1989 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
1991 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
1996 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
1998 gca
.origin
= zhp
->zfs_name
;
2000 if (gca
.numclones
!= 0) {
2002 char pool
[ZFS_MAXNAMELEN
];
2005 /* get the pool name */
2006 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2007 (void) strsep(&cp
, "/@");
2008 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2009 ZFS_TYPE_FILESYSTEM
);
2011 (void) get_clones_cb(root
, &gca
);
2014 if (gca
.numclones
!= 0 ||
2015 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2016 nvlist_add_nvlist(zhp
->zfs_props
,
2017 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2024 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2025 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2028 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2034 * Retrieve a property from the given object. If 'literal' is specified, then
2035 * numbers are left as exact values. Otherwise, numbers are converted to a
2036 * human-readable form.
2038 * Returns 0 on success, or -1 on error.
2041 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2042 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2044 char *source
= NULL
;
2048 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2051 * Check to see if this property applies to our object
2053 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
2056 if (received
&& zfs_prop_readonly(prop
))
2060 *src
= ZPROP_SRC_NONE
;
2063 case ZFS_PROP_CREATION
:
2065 * 'creation' is a time_t stored in the statistics. We convert
2066 * this into a string unless 'literal' is specified.
2069 val
= getprop_uint64(zhp
, prop
, &source
);
2070 time_t time
= (time_t)val
;
2074 localtime_r(&time
, &t
) == NULL
||
2075 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2077 (void) snprintf(propbuf
, proplen
, "%llu", val
);
2081 case ZFS_PROP_MOUNTPOINT
:
2083 * Getting the precise mountpoint can be tricky.
2085 * - for 'none' or 'legacy', return those values.
2086 * - for inherited mountpoints, we want to take everything
2087 * after our ancestor and append it to the inherited value.
2089 * If the pool has an alternate root, we want to prepend that
2090 * root to any values we return.
2093 str
= getprop_string(zhp
, prop
, &source
);
2095 if (str
[0] == '/') {
2096 char buf
[MAXPATHLEN
];
2098 const char *relpath
;
2101 * If we inherit the mountpoint, even from a dataset
2102 * with a received value, the source will be the path of
2103 * the dataset we inherit from. If source is
2104 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2107 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2110 relpath
= zhp
->zfs_name
+ strlen(source
);
2111 if (relpath
[0] == '/')
2115 if ((zpool_get_prop(zhp
->zpool_hdl
,
2116 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
2117 (strcmp(root
, "-") == 0))
2120 * Special case an alternate root of '/'. This will
2121 * avoid having multiple leading slashes in the
2124 if (strcmp(root
, "/") == 0)
2128 * If the mountpoint is '/' then skip over this
2129 * if we are obtaining either an alternate root or
2130 * an inherited mountpoint.
2132 if (str
[1] == '\0' && (root
[0] != '\0' ||
2133 relpath
[0] != '\0'))
2136 if (relpath
[0] == '\0')
2137 (void) snprintf(propbuf
, proplen
, "%s%s",
2140 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2141 root
, str
, relpath
[0] == '@' ? "" : "/",
2144 /* 'legacy' or 'none' */
2145 (void) strlcpy(propbuf
, str
, proplen
);
2150 case ZFS_PROP_ORIGIN
:
2151 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2154 * If there is no parent at all, return failure to indicate that
2155 * it doesn't apply to this dataset.
2157 if (propbuf
[0] == '\0')
2161 case ZFS_PROP_CLONES
:
2162 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2166 case ZFS_PROP_QUOTA
:
2167 case ZFS_PROP_REFQUOTA
:
2168 case ZFS_PROP_RESERVATION
:
2169 case ZFS_PROP_REFRESERVATION
:
2171 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2175 * If quota or reservation is 0, we translate this into 'none'
2176 * (unless literal is set), and indicate that it's the default
2177 * value. Otherwise, we print the number nicely and indicate
2178 * that its set locally.
2182 (void) strlcpy(propbuf
, "0", proplen
);
2184 (void) strlcpy(propbuf
, "none", proplen
);
2187 (void) snprintf(propbuf
, proplen
, "%llu",
2190 zfs_nicenum(val
, propbuf
, proplen
);
2194 case ZFS_PROP_REFRATIO
:
2195 case ZFS_PROP_COMPRESSRATIO
:
2196 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2198 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2199 (u_longlong_t
)(val
/ 100),
2200 (u_longlong_t
)(val
% 100));
2204 switch (zhp
->zfs_type
) {
2205 case ZFS_TYPE_FILESYSTEM
:
2208 case ZFS_TYPE_VOLUME
:
2211 case ZFS_TYPE_SNAPSHOT
:
2217 (void) snprintf(propbuf
, proplen
, "%s", str
);
2220 case ZFS_PROP_MOUNTED
:
2222 * The 'mounted' property is a pseudo-property that described
2223 * whether the filesystem is currently mounted. Even though
2224 * it's a boolean value, the typical values of "on" and "off"
2225 * don't make sense, so we translate to "yes" and "no".
2227 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2228 src
, &source
, &val
) != 0)
2231 (void) strlcpy(propbuf
, "yes", proplen
);
2233 (void) strlcpy(propbuf
, "no", proplen
);
2238 * The 'name' property is a pseudo-property derived from the
2239 * dataset name. It is presented as a real property to simplify
2242 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2245 case ZFS_PROP_MLSLABEL
:
2247 m_label_t
*new_sl
= NULL
;
2248 char *ascii
= NULL
; /* human readable label */
2250 (void) strlcpy(propbuf
,
2251 getprop_string(zhp
, prop
, &source
), proplen
);
2253 if (literal
|| (strcasecmp(propbuf
,
2254 ZFS_MLSLABEL_DEFAULT
) == 0))
2258 * Try to translate the internal hex string to
2259 * human-readable output. If there are any
2260 * problems just use the hex string.
2263 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2264 L_NO_CORRECTION
, NULL
) == -1) {
2265 m_label_free(new_sl
);
2269 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2273 m_label_free(new_sl
);
2276 m_label_free(new_sl
);
2278 (void) strlcpy(propbuf
, ascii
, proplen
);
2284 switch (zfs_prop_get_type(prop
)) {
2285 case PROP_TYPE_NUMBER
:
2286 if (get_numeric_property(zhp
, prop
, src
,
2287 &source
, &val
) != 0)
2290 (void) snprintf(propbuf
, proplen
, "%llu",
2293 zfs_nicenum(val
, propbuf
, proplen
);
2296 case PROP_TYPE_STRING
:
2297 (void) strlcpy(propbuf
,
2298 getprop_string(zhp
, prop
, &source
), proplen
);
2301 case PROP_TYPE_INDEX
:
2302 if (get_numeric_property(zhp
, prop
, src
,
2303 &source
, &val
) != 0)
2305 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2307 (void) strlcpy(propbuf
, strval
, proplen
);
2315 get_source(zhp
, src
, source
, statbuf
, statlen
);
2321 * Utility function to get the given numeric property. Does no validation that
2322 * the given property is the appropriate type; should only be used with
2323 * hard-coded property types.
2326 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2331 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2337 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2341 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2342 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2346 * Similar to zfs_prop_get(), but returns the value as an integer.
2349 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2350 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2355 * Check to see if this property applies to our object
2357 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2358 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2359 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2360 zfs_prop_to_name(prop
)));
2364 *src
= ZPROP_SRC_NONE
;
2366 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2369 get_source(zhp
, src
, source
, statbuf
, statlen
);
2375 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2376 char **domainp
, idmap_rid_t
*ridp
)
2378 idmap_get_handle_t
*get_hdl
= NULL
;
2382 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2386 err
= idmap_get_sidbyuid(get_hdl
, id
,
2387 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2389 err
= idmap_get_sidbygid(get_hdl
, id
,
2390 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2392 if (err
== IDMAP_SUCCESS
&&
2393 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2394 status
== IDMAP_SUCCESS
)
2400 idmap_get_destroy(get_hdl
);
2405 * convert the propname into parameters needed by kernel
2406 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2407 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2410 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2411 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2413 zfs_userquota_prop_t type
;
2415 char *numericsid
= NULL
;
2420 /* Figure out the property type ({user|group}{quota|space}) */
2421 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2422 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2423 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2426 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2430 isuser
= (type
== ZFS_PROP_USERQUOTA
||
2431 type
== ZFS_PROP_USERUSED
);
2433 cp
= strchr(propname
, '@') + 1;
2435 if (strchr(cp
, '@')) {
2437 * It's a SID name (eg "user@domain") that needs to be
2438 * turned into S-1-domainID-RID.
2440 directory_error_t e
;
2441 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2444 e
= directory_sid_from_user_name(NULL
,
2447 e
= directory_sid_from_group_name(NULL
,
2451 directory_error_free(e
);
2454 if (numericsid
== NULL
)
2457 /* will be further decoded below */
2460 if (strncmp(cp
, "S-1-", 4) == 0) {
2461 /* It's a numeric SID (eg "S-1-234-567-89") */
2462 (void) strlcpy(domain
, cp
, domainlen
);
2463 cp
= strrchr(domain
, '-');
2468 *ridp
= strtoull(cp
, &end
, 10);
2473 if (errno
!= 0 || *end
!= '\0')
2475 } else if (!isdigit(*cp
)) {
2477 * It's a user/group name (eg "user") that needs to be
2478 * turned into a uid/gid
2480 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2496 /* It's a user/group ID (eg "12345"). */
2497 uid_t id
= strtoul(cp
, &end
, 10);
2504 /* It's an ephemeral ID. */
2505 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2506 &mapdomain
, &rid
) != 0)
2508 (void) strlcpy(domain
, mapdomain
, domainlen
);
2515 ASSERT3P(numericsid
, ==, NULL
);
2520 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2521 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2524 zfs_cmd_t zc
= { 0 };
2526 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2528 err
= userquota_propname_decode(propname
,
2529 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2530 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2531 zc
.zc_objset_type
= *typep
;
2535 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2539 *propvalue
= zc
.zc_cookie
;
2544 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2545 uint64_t *propvalue
)
2547 zfs_userquota_prop_t type
;
2549 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2554 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2555 char *propbuf
, int proplen
, boolean_t literal
)
2559 zfs_userquota_prop_t type
;
2561 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2568 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
2569 } else if (propvalue
== 0 &&
2570 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2571 (void) strlcpy(propbuf
, "none", proplen
);
2573 zfs_nicenum(propvalue
, propbuf
, proplen
);
2579 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2580 uint64_t *propvalue
)
2583 zfs_cmd_t zc
= { 0 };
2584 const char *snapname
;
2586 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2588 snapname
= strchr(propname
, '@') + 1;
2589 if (strchr(snapname
, '@')) {
2590 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2592 /* snapname is the short name, append it to zhp's fsname */
2595 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2596 sizeof (zc
.zc_value
));
2597 cp
= strchr(zc
.zc_value
, '@');
2600 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2601 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2604 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2608 *propvalue
= zc
.zc_cookie
;
2613 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2614 char *propbuf
, int proplen
, boolean_t literal
)
2619 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2625 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
2627 zfs_nicenum(propvalue
, propbuf
, proplen
);
2633 zfs_get_snapused_int(zfs_handle_t
*firstsnap
, zfs_handle_t
*lastsnap
,
2637 zfs_cmd_t zc
= { 0 };
2639 (void) strlcpy(zc
.zc_name
, lastsnap
->zfs_name
, sizeof (zc
.zc_name
));
2640 (void) strlcpy(zc
.zc_value
, firstsnap
->zfs_name
, sizeof (zc
.zc_value
));
2642 err
= ioctl(lastsnap
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_SNAPS
, &zc
);
2646 *usedp
= zc
.zc_cookie
;
2652 * Returns the name of the given zfs handle.
2655 zfs_get_name(const zfs_handle_t
*zhp
)
2657 return (zhp
->zfs_name
);
2661 * Returns the type of the given zfs handle.
2664 zfs_get_type(const zfs_handle_t
*zhp
)
2666 return (zhp
->zfs_type
);
2670 * Is one dataset name a child dataset of another?
2672 * Needs to handle these cases:
2673 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2674 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2675 * Descendant? No. No. No. Yes.
2678 is_descendant(const char *ds1
, const char *ds2
)
2680 size_t d1len
= strlen(ds1
);
2682 /* ds2 can't be a descendant if it's smaller */
2683 if (strlen(ds2
) < d1len
)
2686 /* otherwise, compare strings and verify that there's a '/' char */
2687 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2691 * Given a complete name, return just the portion that refers to the parent.
2692 * Will return -1 if there is no parent (path is just the name of the
2696 parent_name(const char *path
, char *buf
, size_t buflen
)
2700 (void) strlcpy(buf
, path
, buflen
);
2702 if ((slashp
= strrchr(buf
, '/')) == NULL
)
2710 * If accept_ancestor is false, then check to make sure that the given path has
2711 * a parent, and that it exists. If accept_ancestor is true, then find the
2712 * closest existing ancestor for the given path. In prefixlen return the
2713 * length of already existing prefix of the given path. We also fetch the
2714 * 'zoned' property, which is used to validate property settings when creating
2718 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2719 boolean_t accept_ancestor
, int *prefixlen
)
2721 zfs_cmd_t zc
= { 0 };
2722 char parent
[ZFS_MAXNAMELEN
];
2728 (void) snprintf(errbuf
, sizeof (errbuf
),
2729 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2731 /* get parent, and check to see if this is just a pool */
2732 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2733 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2734 "missing dataset name"));
2735 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2738 /* check to see if the pool exists */
2739 if ((slash
= strchr(parent
, '/')) == NULL
)
2740 slash
= parent
+ strlen(parent
);
2741 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2742 zc
.zc_name
[slash
- parent
] = '\0';
2743 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2745 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2746 "no such pool '%s'"), zc
.zc_name
);
2747 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2750 /* check to see if the parent dataset exists */
2751 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2752 if (errno
== ENOENT
&& accept_ancestor
) {
2754 * Go deeper to find an ancestor, give up on top level.
2756 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2757 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2758 "no such pool '%s'"), zc
.zc_name
);
2759 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2761 } else if (errno
== ENOENT
) {
2762 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2763 "parent does not exist"));
2764 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2766 return (zfs_standard_error(hdl
, errno
, errbuf
));
2769 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2773 /* we are in a non-global zone, but parent is in the global zone */
2774 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2775 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2780 /* make sure parent is a filesystem */
2781 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2782 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2783 "parent is not a filesystem"));
2784 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2790 if (prefixlen
!= NULL
)
2791 *prefixlen
= strlen(parent
);
2796 * Finds whether the dataset of the given type(s) exists.
2799 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2803 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2807 * Try to get stats for the dataset, which will tell us if it exists.
2809 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2810 int ds_type
= zhp
->zfs_type
;
2813 if (types
& ds_type
)
2820 * Given a path to 'target', create all the ancestors between
2821 * the prefixlen portion of the path, and the target itself.
2822 * Fail if the initial prefixlen-ancestor does not already exist.
2825 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2831 /* make sure prefix exists */
2832 cp
= target
+ prefixlen
;
2834 assert(strchr(cp
, '/') == NULL
);
2835 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2838 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2846 * Attempt to create, mount, and share any ancestor filesystems,
2847 * up to the prefixlen-long one.
2849 for (cp
= target
+ prefixlen
+ 1;
2850 cp
= strchr(cp
, '/'); *cp
= '/', cp
++) {
2855 h
= make_dataset_handle(hdl
, target
);
2857 /* it already exists, nothing to do here */
2862 logstr
= hdl
->libzfs_log_str
;
2863 hdl
->libzfs_log_str
= NULL
;
2864 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2866 hdl
->libzfs_log_str
= logstr
;
2867 opname
= dgettext(TEXT_DOMAIN
, "create");
2871 hdl
->libzfs_log_str
= logstr
;
2872 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2874 opname
= dgettext(TEXT_DOMAIN
, "open");
2878 if (zfs_mount(h
, NULL
, 0) != 0) {
2879 opname
= dgettext(TEXT_DOMAIN
, "mount");
2883 if (zfs_share(h
) != 0) {
2884 opname
= dgettext(TEXT_DOMAIN
, "share");
2894 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2895 "failed to %s ancestor '%s'"), opname
, target
);
2900 * Creates non-existing ancestors of the given path.
2903 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2909 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
2912 if ((path_copy
= strdup(path
)) != NULL
) {
2913 rc
= create_parents(hdl
, path_copy
, prefix
);
2916 if (path_copy
== NULL
|| rc
!= 0)
2923 * Create a new filesystem or volume.
2926 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2929 zfs_cmd_t zc
= { 0 };
2932 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2936 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2937 "cannot create '%s'"), path
);
2939 /* validate the path, taking care to note the extended error message */
2940 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
2941 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2943 /* validate parents exist */
2944 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
2948 * The failure modes when creating a dataset of a different type over
2949 * one that already exists is a little strange. In particular, if you
2950 * try to create a dataset on top of an existing dataset, the ioctl()
2951 * will return ENOENT, not EEXIST. To prevent this from happening, we
2952 * first try to see if the dataset exists.
2954 (void) strlcpy(zc
.zc_name
, path
, sizeof (zc
.zc_name
));
2955 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2956 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2957 "dataset already exists"));
2958 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2961 if (type
== ZFS_TYPE_VOLUME
)
2962 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2964 zc
.zc_objset_type
= DMU_OST_ZFS
;
2966 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
2967 zoned
, NULL
, errbuf
)) == 0)
2970 if (type
== ZFS_TYPE_VOLUME
) {
2972 * If we are creating a volume, the size and block size must
2973 * satisfy a few restraints. First, the blocksize must be a
2974 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2975 * volsize must be a multiple of the block size, and cannot be
2978 if (props
== NULL
|| nvlist_lookup_uint64(props
,
2979 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
2981 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2982 "missing volume size"));
2983 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2986 if ((ret
= nvlist_lookup_uint64(props
,
2987 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2988 &blocksize
)) != 0) {
2989 if (ret
== ENOENT
) {
2990 blocksize
= zfs_prop_default_numeric(
2991 ZFS_PROP_VOLBLOCKSIZE
);
2994 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2995 "missing volume block size"));
2996 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3002 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3003 "volume size cannot be zero"));
3004 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3007 if (size
% blocksize
!= 0) {
3009 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3010 "volume size must be a multiple of volume block "
3012 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3016 if (props
&& zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0)
3020 /* create the dataset */
3021 ret
= zfs_ioctl(hdl
, ZFS_IOC_CREATE
, &zc
);
3023 zcmd_free_nvlists(&zc
);
3025 /* check for failure */
3027 char parent
[ZFS_MAXNAMELEN
];
3028 (void) parent_name(path
, parent
, sizeof (parent
));
3032 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3033 "no such parent '%s'"), parent
);
3034 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3037 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3038 "parent '%s' is not a filesystem"), parent
);
3039 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3043 "volume block size must be power of 2 from "
3045 (uint_t
)SPA_MINBLOCKSIZE
,
3046 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
3048 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3051 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3052 "pool must be upgraded to set this "
3053 "property or value"));
3054 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3058 * This platform can't address a volume this big.
3060 if (type
== ZFS_TYPE_VOLUME
)
3061 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3066 return (zfs_standard_error(hdl
, errno
, errbuf
));
3074 * Destroys the given dataset. The caller must make sure that the filesystem
3075 * isn't mounted, and that there are no active dependents.
3078 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3080 zfs_cmd_t zc
= { 0 };
3082 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3084 if (ZFS_IS_VOLUME(zhp
)) {
3085 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3087 zc
.zc_objset_type
= DMU_OST_ZFS
;
3090 zc
.zc_defer_destroy
= defer
;
3091 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0) {
3092 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3093 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3097 remove_mountpoint(zhp
);
3102 struct destroydata
{
3104 const char *snapname
;
3108 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3110 struct destroydata
*dd
= arg
;
3112 char name
[ZFS_MAXNAMELEN
];
3115 (void) snprintf(name
, sizeof (name
),
3116 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3118 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
3120 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3124 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3130 * Destroys all snapshots with the given name in zhp & descendants.
3133 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3136 struct destroydata dd
= { 0 };
3138 dd
.snapname
= snapname
;
3139 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3140 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3142 if (nvlist_next_nvpair(dd
.nvl
, NULL
) == NULL
) {
3143 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3144 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3145 zhp
->zfs_name
, snapname
);
3147 ret
= zfs_destroy_snaps_nvl(zhp
, dd
.nvl
, defer
);
3149 nvlist_free(dd
.nvl
);
3154 * Destroys all the snapshots named in the nvlist. They must be underneath
3155 * the zhp (either snapshots of it, or snapshots of its descendants).
3158 zfs_destroy_snaps_nvl(zfs_handle_t
*zhp
, nvlist_t
*snaps
, boolean_t defer
)
3161 zfs_cmd_t zc
= { 0 };
3163 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3164 if (zcmd_write_src_nvlist(zhp
->zfs_hdl
, &zc
, snaps
) != 0)
3166 zc
.zc_defer_destroy
= defer
;
3168 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY_SNAPS_NVL
, &zc
);
3172 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3173 "cannot destroy snapshots in %s"), zc
.zc_name
);
3177 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3178 "snapshot is cloned"));
3179 return (zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
));
3182 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3191 * Clones the given dataset. The target must be of the same type as the source.
3194 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3196 zfs_cmd_t zc
= { 0 };
3197 char parent
[ZFS_MAXNAMELEN
];
3200 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3204 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3206 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3207 "cannot create '%s'"), target
);
3209 /* validate the target/clone name */
3210 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3211 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3213 /* validate parents exist */
3214 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3217 (void) parent_name(target
, parent
, sizeof (parent
));
3220 if (ZFS_IS_VOLUME(zhp
)) {
3221 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3222 type
= ZFS_TYPE_VOLUME
;
3224 zc
.zc_objset_type
= DMU_OST_ZFS
;
3225 type
= ZFS_TYPE_FILESYSTEM
;
3229 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3230 zhp
, errbuf
)) == NULL
)
3233 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3241 (void) strlcpy(zc
.zc_name
, target
, sizeof (zc
.zc_name
));
3242 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
, sizeof (zc
.zc_value
));
3243 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_CREATE
, &zc
);
3245 zcmd_free_nvlists(&zc
);
3252 * The parent doesn't exist. We should have caught this
3253 * above, but there may a race condition that has since
3254 * destroyed the parent.
3256 * At this point, we don't know whether it's the source
3257 * that doesn't exist anymore, or whether the target
3258 * dataset doesn't exist.
3260 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3261 "no such parent '%s'"), parent
);
3262 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3265 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3266 "source and target pools differ"));
3267 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3271 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3280 * Promotes the given clone fs to be the clone parent.
3283 zfs_promote(zfs_handle_t
*zhp
)
3285 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3286 zfs_cmd_t zc
= { 0 };
3287 char parent
[MAXPATHLEN
];
3291 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3292 "cannot promote '%s'"), zhp
->zfs_name
);
3294 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3295 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3296 "snapshots can not be promoted"));
3297 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3300 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3301 if (parent
[0] == '\0') {
3302 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3303 "not a cloned filesystem"));
3304 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3307 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3308 sizeof (zc
.zc_value
));
3309 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3310 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3313 int save_errno
= errno
;
3315 switch (save_errno
) {
3317 /* There is a conflicting snapshot name. */
3318 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3319 "conflicting snapshot '%s' from parent '%s'"),
3320 zc
.zc_string
, parent
);
3321 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3324 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3331 * Takes a snapshot of the given dataset.
3334 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3338 char parent
[ZFS_MAXNAMELEN
];
3340 zfs_cmd_t zc
= { 0 };
3344 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3345 "cannot snapshot '%s'"), path
);
3347 /* validate the target name */
3348 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3349 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3352 if ((props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3353 props
, B_FALSE
, NULL
, errbuf
)) == NULL
)
3356 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3364 /* make sure the parent exists and is of the appropriate type */
3365 delim
= strchr(path
, '@');
3366 (void) strncpy(parent
, path
, delim
- path
);
3367 parent
[delim
- path
] = '\0';
3369 if ((zhp
= zfs_open(hdl
, parent
, ZFS_TYPE_FILESYSTEM
|
3370 ZFS_TYPE_VOLUME
)) == NULL
) {
3371 zcmd_free_nvlists(&zc
);
3375 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3376 (void) strlcpy(zc
.zc_value
, delim
+1, sizeof (zc
.zc_value
));
3377 if (ZFS_IS_VOLUME(zhp
))
3378 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3380 zc
.zc_objset_type
= DMU_OST_ZFS
;
3381 zc
.zc_cookie
= recursive
;
3382 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SNAPSHOT
, &zc
);
3384 zcmd_free_nvlists(&zc
);
3387 * if it was recursive, the one that actually failed will be in
3391 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3392 "cannot create snapshot '%s@%s'"), zc
.zc_name
, zc
.zc_value
);
3393 (void) zfs_standard_error(hdl
, errno
, errbuf
);
3402 * Destroy any more recent snapshots. We invoke this callback on any dependents
3403 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3404 * is a dependent and we should just destroy it without checking the transaction
3407 typedef struct rollback_data
{
3408 const char *cb_target
; /* the snapshot */
3409 uint64_t cb_create
; /* creation time reference */
3411 boolean_t cb_dependent
;
3416 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3418 rollback_data_t
*cbp
= data
;
3420 if (!cbp
->cb_dependent
) {
3421 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3422 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3423 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3427 cbp
->cb_dependent
= B_TRUE
;
3428 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3429 rollback_destroy
, cbp
);
3430 cbp
->cb_dependent
= B_FALSE
;
3432 logstr
= zhp
->zfs_hdl
->libzfs_log_str
;
3433 zhp
->zfs_hdl
->libzfs_log_str
= NULL
;
3434 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3435 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
3438 /* We must destroy this clone; first unmount it */
3439 prop_changelist_t
*clp
;
3441 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3442 cbp
->cb_force
? MS_FORCE
: 0);
3443 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3444 cbp
->cb_error
= B_TRUE
;
3448 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3449 cbp
->cb_error
= B_TRUE
;
3451 changelist_remove(clp
, zhp
->zfs_name
);
3452 (void) changelist_postfix(clp
);
3453 changelist_free(clp
);
3461 * Given a dataset, rollback to a specific snapshot, discarding any
3462 * data changes since then and making it the active dataset.
3464 * Any snapshots more recent than the target are destroyed, along with
3468 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3470 rollback_data_t cb
= { 0 };
3472 zfs_cmd_t zc
= { 0 };
3473 boolean_t restore_resv
= 0;
3474 uint64_t old_volsize
, new_volsize
;
3475 zfs_prop_t resv_prop
;
3477 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3478 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3481 * Destroy all recent snapshots and its dependends.
3483 cb
.cb_force
= force
;
3484 cb
.cb_target
= snap
->zfs_name
;
3485 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3486 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3492 * Now that we have verified that the snapshot is the latest,
3493 * rollback to the given snapshot.
3496 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3497 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3499 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3501 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3504 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3506 if (ZFS_IS_VOLUME(zhp
))
3507 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3509 zc
.zc_objset_type
= DMU_OST_ZFS
;
3512 * We rely on zfs_iter_children() to verify that there are no
3513 * newer snapshots for the given dataset. Therefore, we can
3514 * simply pass the name on to the ioctl() call. There is still
3515 * an unlikely race condition where the user has taken a
3516 * snapshot since we verified that this was the most recent.
3519 if ((err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_ROLLBACK
, &zc
)) != 0) {
3520 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3521 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3527 * For volumes, if the pre-rollback volsize matched the pre-
3528 * rollback reservation and the volsize has changed then set
3529 * the reservation property to the post-rollback volsize.
3530 * Make a new handle since the rollback closed the dataset.
3532 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3533 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3535 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3536 if (old_volsize
!= new_volsize
)
3537 err
= zfs_prop_set_int(zhp
, resv_prop
,
3546 * Renames the given dataset.
3549 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
)
3552 zfs_cmd_t zc
= { 0 };
3554 prop_changelist_t
*cl
= NULL
;
3555 zfs_handle_t
*zhrp
= NULL
;
3556 char *parentname
= NULL
;
3557 char parent
[ZFS_MAXNAMELEN
];
3558 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3561 /* if we have the same exact name, just return success */
3562 if (strcmp(zhp
->zfs_name
, target
) == 0)
3565 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3566 "cannot rename to '%s'"), target
);
3569 * Make sure the target name is valid
3571 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3572 if ((strchr(target
, '@') == NULL
) ||
3575 * Snapshot target name is abbreviated,
3576 * reconstruct full dataset name
3578 (void) strlcpy(parent
, zhp
->zfs_name
,
3580 delim
= strchr(parent
, '@');
3581 if (strchr(target
, '@') == NULL
)
3585 (void) strlcat(parent
, target
, sizeof (parent
));
3589 * Make sure we're renaming within the same dataset.
3591 delim
= strchr(target
, '@');
3592 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3593 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3594 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3595 "snapshots must be part of same "
3597 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3601 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3602 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3605 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3606 "recursive rename must be a snapshot"));
3607 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3610 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3611 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3613 /* validate parents */
3614 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3617 /* make sure we're in the same pool */
3618 verify((delim
= strchr(target
, '/')) != NULL
);
3619 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3620 zhp
->zfs_name
[delim
- target
] != '/') {
3621 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3622 "datasets must be within same pool"));
3623 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3626 /* new name cannot be a child of the current dataset name */
3627 if (is_descendant(zhp
->zfs_name
, target
)) {
3628 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3629 "New dataset name cannot be a descendant of "
3630 "current dataset name"));
3631 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3635 (void) snprintf(errbuf
, sizeof (errbuf
),
3636 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3638 if (getzoneid() == GLOBAL_ZONEID
&&
3639 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3640 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3641 "dataset is used in a non-global zone"));
3642 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3647 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3648 if (parentname
== NULL
) {
3652 delim
= strchr(parentname
, '@');
3654 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3661 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0)) == NULL
)
3664 if (changelist_haszonedchild(cl
)) {
3665 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3666 "child dataset with inherited mountpoint is used "
3667 "in a non-global zone"));
3668 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3672 if ((ret
= changelist_prefix(cl
)) != 0)
3676 if (ZFS_IS_VOLUME(zhp
))
3677 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3679 zc
.zc_objset_type
= DMU_OST_ZFS
;
3681 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3682 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3684 zc
.zc_cookie
= recursive
;
3686 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3688 * if it was recursive, the one that actually failed will
3691 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3692 "cannot rename '%s'"), zc
.zc_name
);
3694 if (recursive
&& errno
== EEXIST
) {
3695 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3696 "a child dataset already has a snapshot "
3697 "with the new name"));
3698 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3700 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3704 * On failure, we still want to remount any filesystems that
3705 * were previously mounted, so we don't alter the system state.
3708 (void) changelist_postfix(cl
);
3711 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3712 ret
= changelist_postfix(cl
);
3724 changelist_free(cl
);
3730 zfs_get_user_props(zfs_handle_t
*zhp
)
3732 return (zhp
->zfs_user_props
);
3736 zfs_get_recvd_props(zfs_handle_t
*zhp
)
3738 if (zhp
->zfs_recvd_props
== NULL
)
3739 if (get_recvd_props_ioctl(zhp
) != 0)
3741 return (zhp
->zfs_recvd_props
);
3745 * This function is used by 'zfs list' to determine the exact set of columns to
3746 * display, and their maximum widths. This does two main things:
3748 * - If this is a list of all properties, then expand the list to include
3749 * all native properties, and set a flag so that for each dataset we look
3750 * for new unique user properties and add them to the list.
3752 * - For non fixed-width properties, keep track of the maximum width seen
3753 * so that we can size the column appropriately. If the user has
3754 * requested received property values, we also need to compute the width
3755 * of the RECEIVED column.
3758 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
)
3760 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3761 zprop_list_t
*entry
;
3762 zprop_list_t
**last
, **start
;
3763 nvlist_t
*userprops
, *propval
;
3766 char buf
[ZFS_MAXPROPLEN
];
3768 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
3771 userprops
= zfs_get_user_props(zhp
);
3774 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
3776 * Go through and add any user properties as necessary. We
3777 * start by incrementing our list pointer to the first
3778 * non-native property.
3781 while (*start
!= NULL
) {
3782 if ((*start
)->pl_prop
== ZPROP_INVAL
)
3784 start
= &(*start
)->pl_next
;
3788 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
3790 * See if we've already found this property in our list.
3792 for (last
= start
; *last
!= NULL
;
3793 last
= &(*last
)->pl_next
) {
3794 if (strcmp((*last
)->pl_user_prop
,
3795 nvpair_name(elem
)) == 0)
3799 if (*last
== NULL
) {
3800 if ((entry
= zfs_alloc(hdl
,
3801 sizeof (zprop_list_t
))) == NULL
||
3802 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
3803 nvpair_name(elem
)))) == NULL
) {
3808 entry
->pl_prop
= ZPROP_INVAL
;
3809 entry
->pl_width
= strlen(nvpair_name(elem
));
3810 entry
->pl_all
= B_TRUE
;
3817 * Now go through and check the width of any non-fixed columns
3819 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
3820 if (entry
->pl_fixed
)
3823 if (entry
->pl_prop
!= ZPROP_INVAL
) {
3824 if (zfs_prop_get(zhp
, entry
->pl_prop
,
3825 buf
, sizeof (buf
), NULL
, NULL
, 0, B_FALSE
) == 0) {
3826 if (strlen(buf
) > entry
->pl_width
)
3827 entry
->pl_width
= strlen(buf
);
3829 if (received
&& zfs_prop_get_recvd(zhp
,
3830 zfs_prop_to_name(entry
->pl_prop
),
3831 buf
, sizeof (buf
), B_FALSE
) == 0)
3832 if (strlen(buf
) > entry
->pl_recvd_width
)
3833 entry
->pl_recvd_width
= strlen(buf
);
3835 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
3837 verify(nvlist_lookup_string(propval
,
3838 ZPROP_VALUE
, &strval
) == 0);
3839 if (strlen(strval
) > entry
->pl_width
)
3840 entry
->pl_width
= strlen(strval
);
3842 if (received
&& zfs_prop_get_recvd(zhp
,
3843 entry
->pl_user_prop
,
3844 buf
, sizeof (buf
), B_FALSE
) == 0)
3845 if (strlen(buf
) > entry
->pl_recvd_width
)
3846 entry
->pl_recvd_width
= strlen(buf
);
3854 zfs_deleg_share_nfs(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
3855 char *resource
, void *export
, void *sharetab
,
3856 int sharemax
, zfs_share_op_t operation
)
3858 zfs_cmd_t zc
= { 0 };
3861 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3862 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3864 (void) strlcpy(zc
.zc_string
, resource
, sizeof (zc
.zc_string
));
3865 zc
.zc_share
.z_sharedata
= (uint64_t)(uintptr_t)sharetab
;
3866 zc
.zc_share
.z_exportdata
= (uint64_t)(uintptr_t)export
;
3867 zc
.zc_share
.z_sharetype
= operation
;
3868 zc
.zc_share
.z_sharemax
= sharemax
;
3869 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SHARE
, &zc
);
3874 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
3879 * Keep a reference to the props-table against which we prune the
3882 zhp
->zfs_props_table
= props
;
3884 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
3887 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
3888 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
3891 * User properties will result in ZPROP_INVAL, and since we
3892 * only know how to prune standard ZFS properties, we always
3893 * leave these in the list. This can also happen if we
3894 * encounter an unknown DSL property (when running older
3895 * software, for example).
3897 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
3898 (void) nvlist_remove(zhp
->zfs_props
,
3899 nvpair_name(curr
), nvpair_type(curr
));
3905 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
3906 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
3908 zfs_cmd_t zc
= { 0 };
3909 nvlist_t
*nvlist
= NULL
;
3912 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3913 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3914 zc
.zc_cookie
= (uint64_t)cmd
;
3916 if (cmd
== ZFS_SMB_ACL_RENAME
) {
3917 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
3918 (void) no_memory(hdl
);
3924 case ZFS_SMB_ACL_ADD
:
3925 case ZFS_SMB_ACL_REMOVE
:
3926 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
3928 case ZFS_SMB_ACL_RENAME
:
3929 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
3931 (void) no_memory(hdl
);
3934 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
3936 (void) no_memory(hdl
);
3939 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
3940 nvlist_free(nvlist
);
3944 case ZFS_SMB_ACL_PURGE
:
3949 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
3951 nvlist_free(nvlist
);
3956 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
3957 char *path
, char *resource
)
3959 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
3964 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
3965 char *path
, char *resource
)
3967 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
3972 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
3974 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
3979 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
3980 char *oldname
, char *newname
)
3982 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
3987 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
3988 zfs_userspace_cb_t func
, void *arg
)
3990 zfs_cmd_t zc
= { 0 };
3992 zfs_useracct_t buf
[100];
3994 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3996 zc
.zc_objset_type
= type
;
3997 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4001 zfs_useracct_t
*zua
= buf
;
4003 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4004 error
= ioctl(zhp
->zfs_hdl
->libzfs_fd
,
4005 ZFS_IOC_USERSPACE_MANY
, &zc
);
4006 if (error
|| zc
.zc_nvlist_dst_size
== 0)
4009 while (zc
.zc_nvlist_dst_size
> 0) {
4010 error
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4015 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4023 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4024 boolean_t recursive
, boolean_t temphold
, boolean_t enoent_ok
,
4025 int cleanup_fd
, uint64_t dsobj
, uint64_t createtxg
)
4027 zfs_cmd_t zc
= { 0 };
4028 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4030 ASSERT(!recursive
|| dsobj
== 0);
4032 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4033 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4034 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4035 >= sizeof (zc
.zc_string
))
4036 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4037 zc
.zc_cookie
= recursive
;
4038 zc
.zc_temphold
= temphold
;
4039 zc
.zc_cleanup_fd
= cleanup_fd
;
4040 zc
.zc_sendobj
= dsobj
;
4041 zc
.zc_createtxg
= createtxg
;
4043 if (zfs_ioctl(hdl
, ZFS_IOC_HOLD
, &zc
) != 0) {
4044 char errbuf
[ZFS_MAXNAMELEN
+32];
4047 * if it was recursive, the one that actually failed will be in
4050 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4051 "cannot hold '%s@%s'"), zc
.zc_name
, snapname
);
4055 * Temporary tags wind up having the ds object id
4056 * prepended. So even if we passed the length check
4057 * above, it's still possible for the tag to wind
4058 * up being slightly too long.
4060 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
));
4062 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4063 "pool must be upgraded"));
4064 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4066 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4068 return (zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
));
4074 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4082 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4083 boolean_t recursive
)
4085 zfs_cmd_t zc
= { 0 };
4086 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4088 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4089 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4090 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4091 >= sizeof (zc
.zc_string
))
4092 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4093 zc
.zc_cookie
= recursive
;
4095 if (zfs_ioctl(hdl
, ZFS_IOC_RELEASE
, &zc
) != 0) {
4096 char errbuf
[ZFS_MAXNAMELEN
+32];
4099 * if it was recursive, the one that actually failed will be in
4102 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4103 "cannot release '%s' from '%s@%s'"), tag
, zc
.zc_name
,
4107 return (zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
));
4109 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4110 "pool must be upgraded"));
4111 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4113 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4115 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4123 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4125 zfs_cmd_t zc
= { 0 };
4126 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4130 char errbuf
[ZFS_MAXNAMELEN
+32];
4132 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4133 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4137 nvbuf
= malloc(nvsz
);
4138 if (nvbuf
== NULL
) {
4139 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4143 zc
.zc_nvlist_dst_size
= nvsz
;
4144 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4146 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4148 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4149 (void) snprintf(errbuf
, sizeof (errbuf
),
4150 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4155 nvsz
= zc
.zc_nvlist_dst_size
;
4159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4160 "pool must be upgraded"));
4161 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4164 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4167 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4170 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4175 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4177 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4178 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4180 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4190 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4192 zfs_cmd_t zc
= { 0 };
4193 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4195 char errbuf
[ZFS_MAXNAMELEN
+32];
4199 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4200 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4202 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4205 nvbuf
= malloc(nvsz
);
4207 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4210 zc
.zc_nvlist_src_size
= nvsz
;
4211 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4212 zc
.zc_perm_action
= un
;
4214 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4216 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4217 (void) snprintf(errbuf
, sizeof (errbuf
),
4218 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4222 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4223 "pool must be upgraded"));
4224 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4227 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4230 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4233 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4244 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4246 zfs_cmd_t zc
= { 0 };
4247 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4251 char errbuf
[ZFS_MAXNAMELEN
+32];
4253 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
4257 nvbuf
= malloc(nvsz
);
4258 if (nvbuf
== NULL
) {
4259 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4263 zc
.zc_nvlist_dst_size
= nvsz
;
4264 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4266 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4268 if (zfs_ioctl(hdl
, ZFS_IOC_GET_HOLDS
, &zc
) != 0) {
4269 (void) snprintf(errbuf
, sizeof (errbuf
),
4270 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4275 nvsz
= zc
.zc_nvlist_dst_size
;
4279 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4280 "pool must be upgraded"));
4281 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4284 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4287 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4290 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4295 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4297 (void) snprintf(errbuf
, sizeof (errbuf
),
4298 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4300 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4310 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4313 uint64_t nblocks
, volblocksize
;
4317 if (nvlist_lookup_string(props
,
4318 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4319 ncopies
= atoi(strval
);
4322 if (nvlist_lookup_uint64(props
,
4323 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4324 &volblocksize
) != 0)
4325 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4326 nblocks
= volsize
/volblocksize
;
4327 /* start with metadnode L0-L6 */
4329 /* calculate number of indirects */
4330 while (nblocks
> 1) {
4331 nblocks
+= DNODES_PER_LEVEL
- 1;
4332 nblocks
/= DNODES_PER_LEVEL
;
4335 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4338 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4339 * compressed, but in practice they compress down to about
4342 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;