4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27 * Copyright (c) 2013 Martin Matuska. All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
43 #include <sys/mntent.h>
44 #include <sys/mount.h>
53 #include <directory.h>
54 #endif /* HAVE_IDMAP */
56 #include <sys/dnode.h>
61 #include "zfs_namecheck.h"
63 #include "libzfs_impl.h"
64 #include "zfs_deleg.h"
66 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
67 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
70 * Given a single type (not a mask of types), return the type in a human
74 zfs_type_to_name(zfs_type_t type
)
77 case ZFS_TYPE_FILESYSTEM
:
78 return (dgettext(TEXT_DOMAIN
, "filesystem"));
79 case ZFS_TYPE_SNAPSHOT
:
80 return (dgettext(TEXT_DOMAIN
, "snapshot"));
82 return (dgettext(TEXT_DOMAIN
, "volume"));
91 * Validate a ZFS path. This is used even before trying to open the dataset, to
92 * provide a more meaningful error message. We call zfs_error_aux() to
93 * explain exactly why the name was not valid.
96 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
102 (void) zfs_prop_get_table();
103 if (dataset_namecheck(path
, &why
, &what
) != 0) {
106 case NAME_ERR_TOOLONG
:
107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
108 "name is too long"));
111 case NAME_ERR_LEADING_SLASH
:
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "leading slash in name"));
116 case NAME_ERR_EMPTY_COMPONENT
:
117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
118 "empty component in name"));
121 case NAME_ERR_TRAILING_SLASH
:
122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
123 "trailing slash in name"));
126 case NAME_ERR_INVALCHAR
:
128 dgettext(TEXT_DOMAIN
, "invalid character "
129 "'%c' in name"), what
);
132 case NAME_ERR_MULTIPLE_AT
:
133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
134 "multiple '@' delimiters in name"));
137 case NAME_ERR_NOLETTER
:
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "pool doesn't begin with a letter"));
142 case NAME_ERR_RESERVED
:
143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
144 "name is reserved"));
147 case NAME_ERR_DISKLIKE
:
148 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
149 "reserved disk name"));
159 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
162 "snapshot delimiter '@' in filesystem name"));
166 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
168 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
169 "missing '@' delimiter in snapshot name"));
173 if (modifying
&& strchr(path
, '%') != NULL
) {
175 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
176 "invalid character %c in name"), '%');
184 zfs_name_valid(const char *name
, zfs_type_t type
)
186 if (type
== ZFS_TYPE_POOL
)
187 return (zpool_name_valid(NULL
, B_FALSE
, name
));
188 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
192 * This function takes the raw DSL properties, and filters out the user-defined
193 * properties into a separate nvlist.
196 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
198 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
203 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
204 (void) no_memory(hdl
);
209 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
210 if (!zfs_prop_user(nvpair_name(elem
)))
213 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
214 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
216 (void) no_memory(hdl
);
224 static zpool_handle_t
*
225 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
227 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
230 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
231 if (hdl
->libzfs_pool_handles
!= NULL
)
232 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
233 hdl
->libzfs_pool_handles
= zph
;
238 static zpool_handle_t
*
239 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
241 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
242 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
244 while ((zph
!= NULL
) &&
245 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
246 zph
= zph
->zpool_next
;
251 * Returns a handle to the pool that contains the provided dataset.
252 * If a handle to that pool already exists then that handle is returned.
253 * Otherwise, a new handle is created and added to the list of handles.
255 static zpool_handle_t
*
256 zpool_handle(zfs_handle_t
*zhp
)
262 len
= strcspn(zhp
->zfs_name
, "/@") + 1;
263 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
264 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
266 zph
= zpool_find_handle(zhp
, pool_name
, len
);
268 zph
= zpool_add_handle(zhp
, pool_name
);
275 zpool_free_handles(libzfs_handle_t
*hdl
)
277 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
279 while (zph
!= NULL
) {
280 next
= zph
->zpool_next
;
284 hdl
->libzfs_pool_handles
= NULL
;
288 * Utility function to gather stats (objset and zpl) for the given object.
291 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
293 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
295 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
297 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
298 if (errno
== ENOMEM
) {
299 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
310 * Utility function to get the received properties of the given object.
313 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
315 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
316 nvlist_t
*recvdprops
;
317 zfs_cmd_t zc
= {"\0"};
320 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
323 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
325 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
326 if (errno
== ENOMEM
) {
327 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
331 zcmd_free_nvlists(&zc
);
336 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
337 zcmd_free_nvlists(&zc
);
341 nvlist_free(zhp
->zfs_recvd_props
);
342 zhp
->zfs_recvd_props
= recvdprops
;
348 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
350 nvlist_t
*allprops
, *userprops
;
352 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
354 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
359 * XXX Why do we store the user props separately, in addition to
360 * storing them in zfs_props?
362 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
363 nvlist_free(allprops
);
367 nvlist_free(zhp
->zfs_props
);
368 nvlist_free(zhp
->zfs_user_props
);
370 zhp
->zfs_props
= allprops
;
371 zhp
->zfs_user_props
= userprops
;
377 get_stats(zfs_handle_t
*zhp
)
380 zfs_cmd_t zc
= {"\0"};
382 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
384 if (get_stats_ioctl(zhp
, &zc
) != 0)
386 else if (put_stats_zhdl(zhp
, &zc
) != 0)
388 zcmd_free_nvlists(&zc
);
393 * Refresh the properties currently stored in the handle.
396 zfs_refresh_properties(zfs_handle_t
*zhp
)
398 (void) get_stats(zhp
);
402 * Makes a handle from the given dataset name. Used by zfs_open() and
403 * zfs_iter_* to create child handles on the fly.
406 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
408 if (put_stats_zhdl(zhp
, zc
) != 0)
412 * We've managed to open the dataset and gather statistics. Determine
413 * the high-level type.
415 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
416 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
417 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
418 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
419 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
424 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
425 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
426 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
427 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
428 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
429 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
431 abort(); /* we should never see any other types */
433 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
440 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
442 zfs_cmd_t zc
= {"\0"};
444 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
450 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
451 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
455 if (get_stats_ioctl(zhp
, &zc
) == -1) {
456 zcmd_free_nvlists(&zc
);
460 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
464 zcmd_free_nvlists(&zc
);
469 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
471 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
477 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
478 if (make_dataset_handle_common(zhp
, zc
) == -1) {
486 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
488 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
493 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
494 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
495 zhp
->zfs_head_type
= pzhp
->zfs_type
;
496 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
497 zhp
->zpool_hdl
= zpool_handle(zhp
);
503 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
505 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
510 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
511 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
512 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
513 sizeof (zhp
->zfs_name
));
514 zhp
->zfs_type
= zhp_orig
->zfs_type
;
515 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
516 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
517 if (zhp_orig
->zfs_props
!= NULL
) {
518 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
519 (void) no_memory(zhp
->zfs_hdl
);
524 if (zhp_orig
->zfs_user_props
!= NULL
) {
525 if (nvlist_dup(zhp_orig
->zfs_user_props
,
526 &zhp
->zfs_user_props
, 0) != 0) {
527 (void) no_memory(zhp
->zfs_hdl
);
532 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
533 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
534 &zhp
->zfs_recvd_props
, 0)) {
535 (void) no_memory(zhp
->zfs_hdl
);
540 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
541 if (zhp_orig
->zfs_mntopts
!= NULL
) {
542 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
543 zhp_orig
->zfs_mntopts
);
545 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
550 * Opens the given snapshot, filesystem, or volume. The 'types'
551 * argument is a mask of acceptable types. The function will print an
552 * appropriate error message and return NULL if it can't be opened.
555 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
560 (void) snprintf(errbuf
, sizeof (errbuf
),
561 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
564 * Validate the name before we even try to open it.
566 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
567 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
568 "invalid dataset name"));
569 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
574 * Try to get stats for the dataset, which will tell us if it exists.
577 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
578 (void) zfs_standard_error(hdl
, errno
, errbuf
);
582 if (!(types
& zhp
->zfs_type
)) {
583 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
592 * Release a ZFS handle. Nothing to do but free the associated memory.
595 zfs_close(zfs_handle_t
*zhp
)
597 if (zhp
->zfs_mntopts
)
598 free(zhp
->zfs_mntopts
);
599 nvlist_free(zhp
->zfs_props
);
600 nvlist_free(zhp
->zfs_user_props
);
601 nvlist_free(zhp
->zfs_recvd_props
);
605 typedef struct mnttab_node
{
606 struct mnttab mtn_mt
;
611 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
613 const mnttab_node_t
*mtn1
= arg1
;
614 const mnttab_node_t
*mtn2
= arg2
;
617 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
621 return (rv
> 0 ? 1 : -1);
625 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
627 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
628 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
629 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
633 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
637 /* Reopen MNTTAB to prevent reading stale data from open file */
638 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
641 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
645 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
648 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
649 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
650 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
651 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
652 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
654 /* Exclude duplicate mounts */
655 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
656 free(mtn
->mtn_mt
.mnt_special
);
657 free(mtn
->mtn_mt
.mnt_mountp
);
658 free(mtn
->mtn_mt
.mnt_fstype
);
659 free(mtn
->mtn_mt
.mnt_mntopts
);
664 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
671 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
676 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
677 free(mtn
->mtn_mt
.mnt_special
);
678 free(mtn
->mtn_mt
.mnt_mountp
);
679 free(mtn
->mtn_mt
.mnt_fstype
);
680 free(mtn
->mtn_mt
.mnt_mntopts
);
683 avl_destroy(&hdl
->libzfs_mnttab_cache
);
687 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
689 hdl
->libzfs_mnttab_enable
= enable
;
693 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
694 struct mnttab
*entry
)
700 if (!hdl
->libzfs_mnttab_enable
) {
701 struct mnttab srch
= { 0 };
703 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
704 libzfs_mnttab_fini(hdl
);
706 /* Reopen MNTTAB to prevent reading stale data from open file */
707 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
710 srch
.mnt_special
= (char *)fsname
;
711 srch
.mnt_fstype
= MNTTYPE_ZFS
;
712 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
718 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
719 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
722 find
.mtn_mt
.mnt_special
= (char *)fsname
;
723 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
725 *entry
= mtn
->mtn_mt
;
732 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
733 const char *mountp
, const char *mntopts
)
737 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
739 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
740 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
741 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
742 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
743 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
744 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
748 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
753 find
.mtn_mt
.mnt_special
= (char *)fsname
;
754 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
755 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
756 free(ret
->mtn_mt
.mnt_special
);
757 free(ret
->mtn_mt
.mnt_mountp
);
758 free(ret
->mtn_mt
.mnt_fstype
);
759 free(ret
->mtn_mt
.mnt_mntopts
);
765 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
767 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
769 if (zpool_handle
== NULL
)
772 *spa_version
= zpool_get_prop_int(zpool_handle
,
773 ZPOOL_PROP_VERSION
, NULL
);
778 * The choice of reservation property depends on the SPA version.
781 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
785 if (zfs_spa_version(zhp
, &spa_version
) < 0)
788 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
789 *resv_prop
= ZFS_PROP_REFRESERVATION
;
791 *resv_prop
= ZFS_PROP_RESERVATION
;
797 * Given an nvlist of properties to set, validates that they are correct, and
798 * parses any numeric properties (index, boolean, etc) if they are specified as
802 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
803 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
810 int chosen_normal
= -1;
813 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
814 (void) no_memory(hdl
);
819 * Make sure this property is valid and applies to this type.
823 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
824 const char *propname
= nvpair_name(elem
);
826 prop
= zfs_name_to_prop(propname
);
827 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
829 * This is a user property: make sure it's a
830 * string, and that it's less than ZAP_MAXNAMELEN.
832 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
833 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
834 "'%s' must be a string"), propname
);
835 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
839 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
840 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
841 "property name '%s' is too long"),
843 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
847 (void) nvpair_value_string(elem
, &strval
);
848 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
849 (void) no_memory(hdl
);
856 * Currently, only user properties can be modified on
859 if (type
== ZFS_TYPE_SNAPSHOT
) {
860 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
861 "this property can not be modified for snapshots"));
862 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
866 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
867 zfs_userquota_prop_t uqtype
;
868 char newpropname
[128];
873 if (userquota_propname_decode(propname
, zoned
,
874 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
876 dgettext(TEXT_DOMAIN
,
877 "'%s' has an invalid user/group name"),
879 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
883 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
884 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
886 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
888 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
893 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
894 (void) nvpair_value_string(elem
, &strval
);
895 if (strcmp(strval
, "none") == 0) {
897 } else if (zfs_nicestrtonum(hdl
,
898 strval
, &intval
) != 0) {
899 (void) zfs_error(hdl
,
900 EZFS_BADPROP
, errbuf
);
903 } else if (nvpair_type(elem
) ==
905 (void) nvpair_value_uint64(elem
, &intval
);
907 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
908 "use 'none' to disable "
909 "userquota/groupquota"));
913 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
914 "'%s' must be a number"), propname
);
915 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
920 * Encode the prop name as
921 * userquota@<hex-rid>-domain, to make it easy
922 * for the kernel to decode.
924 (void) snprintf(newpropname
, sizeof (newpropname
),
925 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
926 (longlong_t
)rid
, domain
);
930 if (nvlist_add_uint64_array(ret
, newpropname
,
932 (void) no_memory(hdl
);
936 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
937 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
940 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
944 if (prop
== ZPROP_INVAL
) {
945 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
946 "invalid property '%s'"), propname
);
947 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
951 if (!zfs_prop_valid_for_type(prop
, type
)) {
953 dgettext(TEXT_DOMAIN
, "'%s' does not "
954 "apply to datasets of this type"), propname
);
955 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
959 if (zfs_prop_readonly(prop
) &&
960 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
962 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
964 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
968 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
969 &strval
, &intval
, errbuf
) != 0)
973 * Perform some additional checks for specific properties.
976 case ZFS_PROP_VERSION
:
982 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
983 if (intval
< version
) {
984 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
985 "Can not downgrade; already at version %u"),
987 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
993 case ZFS_PROP_RECORDSIZE
:
994 case ZFS_PROP_VOLBLOCKSIZE
:
995 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
996 if (intval
< SPA_MINBLOCKSIZE
||
997 intval
> SPA_MAXBLOCKSIZE
|| !ISP2(intval
)) {
998 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
999 "'%s' must be power of 2 from %u "
1000 "to %uk"), propname
,
1001 (uint_t
)SPA_MINBLOCKSIZE
,
1002 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
1003 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1008 case ZFS_PROP_MLSLABEL
:
1010 #ifdef HAVE_MLSLABEL
1012 * Verify the mlslabel string and convert to
1013 * internal hex label string.
1017 char *hex
= NULL
; /* internal label string */
1019 /* Default value is already OK. */
1020 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1023 /* Verify the label can be converted to binary form */
1024 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1025 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1026 L_NO_CORRECTION
, NULL
) == -1)) {
1030 /* Now translate to hex internal label string */
1031 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1037 m_label_free(new_sl
);
1039 /* If string is already in internal form, we're done. */
1040 if (strcmp(strval
, hex
) == 0) {
1045 /* Replace the label string with the internal form. */
1046 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1048 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1055 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1056 "invalid mlslabel '%s'"), strval
);
1057 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1058 m_label_free(new_sl
); /* OK if null */
1061 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1062 "mlslabels are unsupported"));
1063 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1065 #endif /* HAVE_MLSLABEL */
1068 case ZFS_PROP_MOUNTPOINT
:
1070 namecheck_err_t why
;
1072 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1073 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1076 if (mountpoint_namecheck(strval
, &why
)) {
1078 case NAME_ERR_LEADING_SLASH
:
1080 dgettext(TEXT_DOMAIN
,
1081 "'%s' must be an absolute path, "
1082 "'none', or 'legacy'"), propname
);
1084 case NAME_ERR_TOOLONG
:
1086 dgettext(TEXT_DOMAIN
,
1087 "component of '%s' is too long"),
1093 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1100 case ZFS_PROP_SHARESMB
:
1101 case ZFS_PROP_SHARENFS
:
1103 * For the mountpoint and sharenfs or sharesmb
1104 * properties, check if it can be set in a
1105 * global/non-global zone based on
1106 * the zoned property value:
1108 * global zone non-global zone
1109 * --------------------------------------------------
1110 * zoned=on mountpoint (no) mountpoint (yes)
1111 * sharenfs (no) sharenfs (no)
1112 * sharesmb (no) sharesmb (no)
1114 * zoned=off mountpoint (yes) N/A
1119 if (getzoneid() == GLOBAL_ZONEID
) {
1120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1121 "'%s' cannot be set on "
1122 "dataset in a non-global zone"),
1124 (void) zfs_error(hdl
, EZFS_ZONED
,
1127 } else if (prop
== ZFS_PROP_SHARENFS
||
1128 prop
== ZFS_PROP_SHARESMB
) {
1129 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1130 "'%s' cannot be set in "
1131 "a non-global zone"), propname
);
1132 (void) zfs_error(hdl
, EZFS_ZONED
,
1136 } else if (getzoneid() != GLOBAL_ZONEID
) {
1138 * If zoned property is 'off', this must be in
1139 * a global zone. If not, something is wrong.
1141 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1142 "'%s' cannot be set while dataset "
1143 "'zoned' property is set"), propname
);
1144 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1149 * At this point, it is legitimate to set the
1150 * property. Now we want to make sure that the
1151 * property value is valid if it is sharenfs.
1153 if ((prop
== ZFS_PROP_SHARENFS
||
1154 prop
== ZFS_PROP_SHARESMB
) &&
1155 strcmp(strval
, "on") != 0 &&
1156 strcmp(strval
, "off") != 0) {
1157 zfs_share_proto_t proto
;
1159 if (prop
== ZFS_PROP_SHARESMB
)
1165 * Must be an valid sharing protocol
1166 * option string so init the libshare
1167 * in order to enable the parser and
1168 * then parse the options. We use the
1169 * control API since we don't care about
1170 * the current configuration and don't
1171 * want the overhead of loading it
1172 * until we actually do something.
1175 if (zfs_init_libshare(hdl
,
1176 SA_INIT_CONTROL_API
) != SA_OK
) {
1178 * An error occurred so we can't do
1181 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1182 "'%s' cannot be set: problem "
1183 "in share initialization"),
1185 (void) zfs_error(hdl
, EZFS_BADPROP
,
1190 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1192 * There was an error in parsing so
1193 * deal with it by issuing an error
1194 * message and leaving after
1195 * uninitializing the the libshare
1198 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1199 "'%s' cannot be set to invalid "
1200 "options"), propname
);
1201 (void) zfs_error(hdl
, EZFS_BADPROP
,
1203 zfs_uninit_libshare(hdl
);
1206 zfs_uninit_libshare(hdl
);
1210 case ZFS_PROP_UTF8ONLY
:
1211 chosen_utf
= (int)intval
;
1213 case ZFS_PROP_NORMALIZE
:
1214 chosen_normal
= (int)intval
;
1221 * For changes to existing volumes, we have some additional
1222 * checks to enforce.
1224 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1225 uint64_t volsize
= zfs_prop_get_int(zhp
,
1227 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1228 ZFS_PROP_VOLBLOCKSIZE
);
1232 case ZFS_PROP_RESERVATION
:
1233 case ZFS_PROP_REFRESERVATION
:
1234 if (intval
> volsize
) {
1235 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1236 "'%s' is greater than current "
1237 "volume size"), propname
);
1238 (void) zfs_error(hdl
, EZFS_BADPROP
,
1244 case ZFS_PROP_VOLSIZE
:
1245 if (intval
% blocksize
!= 0) {
1246 zfs_nicenum(blocksize
, buf
,
1248 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1249 "'%s' must be a multiple of "
1250 "volume block size (%s)"),
1252 (void) zfs_error(hdl
, EZFS_BADPROP
,
1258 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1259 "'%s' cannot be zero"),
1261 (void) zfs_error(hdl
, EZFS_BADPROP
,
1273 * If normalization was chosen, but no UTF8 choice was made,
1274 * enforce rejection of non-UTF8 names.
1276 * If normalization was chosen, but rejecting non-UTF8 names
1277 * was explicitly not chosen, it is an error.
1279 if (chosen_normal
> 0 && chosen_utf
< 0) {
1280 if (nvlist_add_uint64(ret
,
1281 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1282 (void) no_memory(hdl
);
1285 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1286 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1287 "'%s' must be set 'on' if normalization chosen"),
1288 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1289 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1300 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1302 uint64_t old_volsize
;
1303 uint64_t new_volsize
;
1304 uint64_t old_reservation
;
1305 uint64_t new_reservation
;
1306 zfs_prop_t resv_prop
;
1309 * If this is an existing volume, and someone is setting the volsize,
1310 * make sure that it matches the reservation, or add it if necessary.
1312 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1313 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1315 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1316 if ((zvol_volsize_to_reservation(old_volsize
, zhp
->zfs_props
) !=
1317 old_reservation
) || nvlist_lookup_uint64(nvl
,
1318 zfs_prop_to_name(resv_prop
), &new_reservation
) != ENOENT
) {
1321 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1324 new_reservation
= zvol_volsize_to_reservation(new_volsize
,
1326 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1327 new_reservation
) != 0) {
1328 (void) no_memory(zhp
->zfs_hdl
);
1335 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1342 * For quotas and reservations, ENOSPC indicates
1343 * something different; setting a quota or reservation
1344 * doesn't use any disk space.
1347 case ZFS_PROP_QUOTA
:
1348 case ZFS_PROP_REFQUOTA
:
1349 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1350 "size is less than current used or "
1352 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1355 case ZFS_PROP_RESERVATION
:
1356 case ZFS_PROP_REFRESERVATION
:
1357 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1358 "size is greater than available space"));
1359 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1363 (void) zfs_standard_error(hdl
, err
, errbuf
);
1369 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1373 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1377 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1378 "pool and or dataset must be upgraded to set this "
1379 "property or value"));
1380 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1384 if (prop
== ZFS_PROP_COMPRESSION
) {
1385 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1386 "property setting is not allowed on "
1387 "bootable datasets"));
1388 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1390 (void) zfs_standard_error(hdl
, err
, errbuf
);
1395 if (prop
== ZPROP_INVAL
) {
1396 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1398 (void) zfs_standard_error(hdl
, err
, errbuf
);
1404 * This platform can't address a volume this big.
1407 if (prop
== ZFS_PROP_VOLSIZE
) {
1408 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1414 (void) zfs_standard_error(hdl
, err
, errbuf
);
1419 zfs_is_namespace_prop(zfs_prop_t prop
)
1423 case ZFS_PROP_ATIME
:
1424 case ZFS_PROP_RELATIME
:
1425 case ZFS_PROP_DEVICES
:
1427 case ZFS_PROP_SETUID
:
1428 case ZFS_PROP_READONLY
:
1429 case ZFS_PROP_XATTR
:
1430 case ZFS_PROP_NBMAND
:
1439 * Given a property name and value, set the property for the given dataset.
1442 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1444 zfs_cmd_t zc
= {"\0"};
1446 prop_changelist_t
*cl
= NULL
;
1448 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1449 nvlist_t
*nvl
= NULL
, *realprops
;
1451 boolean_t do_prefix
= B_TRUE
;
1454 (void) snprintf(errbuf
, sizeof (errbuf
),
1455 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1458 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1459 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1460 (void) no_memory(hdl
);
1464 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1465 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1471 prop
= zfs_name_to_prop(propname
);
1473 if (prop
== ZFS_PROP_VOLSIZE
) {
1474 if ((added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1)
1478 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1481 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1483 "child dataset with inherited mountpoint is used "
1484 "in a non-global zone"));
1485 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1490 * We don't want to unmount & remount the dataset when changing
1491 * its canmount property to 'on' or 'noauto'. We only use
1492 * the changelist logic to unmount when setting canmount=off.
1494 if (prop
== ZFS_PROP_CANMOUNT
) {
1496 int err
= zprop_string_to_index(prop
, propval
, &idx
,
1498 if (err
== 0 && idx
!= ZFS_CANMOUNT_OFF
)
1499 do_prefix
= B_FALSE
;
1502 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1506 * Execute the corresponding ioctl() to set this property.
1508 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1510 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1513 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1516 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1517 if (added_resv
&& errno
== ENOSPC
) {
1518 /* clean up the volsize property we tried to set */
1519 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1522 zcmd_free_nvlists(&zc
);
1523 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1525 if (nvlist_add_uint64(nvl
,
1526 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1529 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1531 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1535 ret
= changelist_postfix(cl
);
1539 * Refresh the statistics so the new property
1540 * value is reflected.
1542 (void) get_stats(zhp
);
1545 * Remount the filesystem to propagate the change
1546 * if one of the options handled by the generic
1547 * Linux namespace layer has been modified.
1549 if (zfs_is_namespace_prop(prop
) &&
1550 zfs_is_mounted(zhp
, NULL
))
1551 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1557 zcmd_free_nvlists(&zc
);
1559 changelist_free(cl
);
1564 * Given a property, inherit the value from the parent dataset, or if received
1565 * is TRUE, revert to the received value, if any.
1568 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1570 zfs_cmd_t zc
= {"\0"};
1572 prop_changelist_t
*cl
;
1573 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1577 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1578 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1580 zc
.zc_cookie
= received
;
1581 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1583 * For user properties, the amount of work we have to do is very
1584 * small, so just do it here.
1586 if (!zfs_prop_user(propname
)) {
1587 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1588 "invalid property"));
1589 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1592 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1593 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1595 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1596 return (zfs_standard_error(hdl
, errno
, errbuf
));
1602 * Verify that this property is inheritable.
1604 if (zfs_prop_readonly(prop
))
1605 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1607 if (!zfs_prop_inheritable(prop
) && !received
)
1608 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1611 * Check to see if the value applies to this type
1613 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1614 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1617 * Normalize the name, to get rid of shorthand abbreviations.
1619 propname
= zfs_prop_to_name(prop
);
1620 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1621 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1623 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1624 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1625 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1626 "dataset is used in a non-global zone"));
1627 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1631 * Determine datasets which will be affected by this change, if any.
1633 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1636 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1637 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1638 "child dataset with inherited mountpoint is used "
1639 "in a non-global zone"));
1640 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1644 if ((ret
= changelist_prefix(cl
)) != 0)
1647 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1648 return (zfs_standard_error(hdl
, errno
, errbuf
));
1651 if ((ret
= changelist_postfix(cl
)) != 0)
1655 * Refresh the statistics so the new property is reflected.
1657 (void) get_stats(zhp
);
1661 changelist_free(cl
);
1666 * True DSL properties are stored in an nvlist. The following two functions
1667 * extract them appropriately.
1670 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1676 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1677 zfs_prop_to_name(prop
), &nv
) == 0) {
1678 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1679 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1681 verify(!zhp
->zfs_props_table
||
1682 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1683 value
= zfs_prop_default_numeric(prop
);
1691 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1697 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1698 zfs_prop_to_name(prop
), &nv
) == 0) {
1699 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1700 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1702 verify(!zhp
->zfs_props_table
||
1703 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1704 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1713 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1715 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1719 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1721 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1722 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1726 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1728 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1733 * Internal function for getting a numeric property. Both zfs_prop_get() and
1734 * zfs_prop_get_int() are built using this interface.
1736 * Certain properties can be overridden using 'mount -o'. In this case, scan
1737 * the contents of the /etc/mtab entry, searching for the appropriate options.
1738 * If they differ from the on-disk values, report the current values and mark
1739 * the source "temporary".
1742 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1743 char **source
, uint64_t *val
)
1745 zfs_cmd_t zc
= {"\0"};
1746 nvlist_t
*zplprops
= NULL
;
1748 char *mntopt_on
= NULL
;
1749 char *mntopt_off
= NULL
;
1750 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1755 case ZFS_PROP_ATIME
:
1756 mntopt_on
= MNTOPT_ATIME
;
1757 mntopt_off
= MNTOPT_NOATIME
;
1760 case ZFS_PROP_RELATIME
:
1761 mntopt_on
= MNTOPT_RELATIME
;
1762 mntopt_off
= MNTOPT_NORELATIME
;
1765 case ZFS_PROP_DEVICES
:
1766 mntopt_on
= MNTOPT_DEVICES
;
1767 mntopt_off
= MNTOPT_NODEVICES
;
1771 mntopt_on
= MNTOPT_EXEC
;
1772 mntopt_off
= MNTOPT_NOEXEC
;
1775 case ZFS_PROP_READONLY
:
1776 mntopt_on
= MNTOPT_RO
;
1777 mntopt_off
= MNTOPT_RW
;
1780 case ZFS_PROP_SETUID
:
1781 mntopt_on
= MNTOPT_SETUID
;
1782 mntopt_off
= MNTOPT_NOSETUID
;
1785 case ZFS_PROP_XATTR
:
1786 mntopt_on
= MNTOPT_XATTR
;
1787 mntopt_off
= MNTOPT_NOXATTR
;
1790 case ZFS_PROP_NBMAND
:
1791 mntopt_on
= MNTOPT_NBMAND
;
1792 mntopt_off
= MNTOPT_NONBMAND
;
1799 * Because looking up the mount options is potentially expensive
1800 * (iterating over all of /etc/mtab), we defer its calculation until
1801 * we're looking up a property which requires its presence.
1803 if (!zhp
->zfs_mntcheck
&&
1804 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1805 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1806 struct mnttab entry
;
1808 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1809 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1811 if (zhp
->zfs_mntopts
== NULL
)
1815 zhp
->zfs_mntcheck
= B_TRUE
;
1818 if (zhp
->zfs_mntopts
== NULL
)
1819 mnt
.mnt_mntopts
= "";
1821 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
1824 case ZFS_PROP_ATIME
:
1825 case ZFS_PROP_RELATIME
:
1826 case ZFS_PROP_DEVICES
:
1828 case ZFS_PROP_READONLY
:
1829 case ZFS_PROP_SETUID
:
1830 case ZFS_PROP_XATTR
:
1831 case ZFS_PROP_NBMAND
:
1832 *val
= getprop_uint64(zhp
, prop
, source
);
1837 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
1840 *src
= ZPROP_SRC_TEMPORARY
;
1841 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
1844 *src
= ZPROP_SRC_TEMPORARY
;
1848 case ZFS_PROP_CANMOUNT
:
1849 case ZFS_PROP_VOLSIZE
:
1850 case ZFS_PROP_QUOTA
:
1851 case ZFS_PROP_REFQUOTA
:
1852 case ZFS_PROP_RESERVATION
:
1853 case ZFS_PROP_REFRESERVATION
:
1854 *val
= getprop_uint64(zhp
, prop
, source
);
1856 if (*source
== NULL
) {
1857 /* not default, must be local */
1858 *source
= zhp
->zfs_name
;
1862 case ZFS_PROP_MOUNTED
:
1863 *val
= (zhp
->zfs_mntopts
!= NULL
);
1866 case ZFS_PROP_NUMCLONES
:
1867 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
1870 case ZFS_PROP_VERSION
:
1871 case ZFS_PROP_NORMALIZE
:
1872 case ZFS_PROP_UTF8ONLY
:
1874 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
1875 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1877 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1878 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
1879 zcmd_free_nvlists(&zc
);
1882 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
1883 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
1885 zcmd_free_nvlists(&zc
);
1889 nvlist_free(zplprops
);
1890 zcmd_free_nvlists(&zc
);
1893 case ZFS_PROP_INCONSISTENT
:
1894 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
1898 switch (zfs_prop_get_type(prop
)) {
1899 case PROP_TYPE_NUMBER
:
1900 case PROP_TYPE_INDEX
:
1901 *val
= getprop_uint64(zhp
, prop
, source
);
1903 * If we tried to use a default value for a
1904 * readonly property, it means that it was not
1907 if (zfs_prop_readonly(prop
) &&
1908 *source
!= NULL
&& (*source
)[0] == '\0') {
1913 case PROP_TYPE_STRING
:
1915 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1916 "cannot get non-numeric property"));
1917 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
1918 dgettext(TEXT_DOMAIN
, "internal error")));
1926 * Calculate the source type, given the raw source string.
1929 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
1930 char *statbuf
, size_t statlen
)
1932 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
1935 if (source
== NULL
) {
1936 *srctype
= ZPROP_SRC_NONE
;
1937 } else if (source
[0] == '\0') {
1938 *srctype
= ZPROP_SRC_DEFAULT
;
1939 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
1940 *srctype
= ZPROP_SRC_RECEIVED
;
1942 if (strcmp(source
, zhp
->zfs_name
) == 0) {
1943 *srctype
= ZPROP_SRC_LOCAL
;
1945 (void) strlcpy(statbuf
, source
, statlen
);
1946 *srctype
= ZPROP_SRC_INHERITED
;
1953 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
1954 size_t proplen
, boolean_t literal
)
1959 if (zhp
->zfs_recvd_props
== NULL
)
1960 if (get_recvd_props_ioctl(zhp
) != 0)
1963 prop
= zfs_name_to_prop(propname
);
1965 if (prop
!= ZPROP_INVAL
) {
1967 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
1969 zfs_set_recvd_props_mode(zhp
, &cookie
);
1970 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
1971 NULL
, NULL
, 0, literal
);
1972 zfs_unset_recvd_props_mode(zhp
, &cookie
);
1976 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
1977 propname
, &propval
) != 0)
1979 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
1981 (void) strlcpy(propbuf
, recvdval
, proplen
);
1984 return (err
== 0 ? 0 : -1);
1988 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
1993 value
= zfs_get_clones_nvl(zhp
);
1998 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
1999 pair
= nvlist_next_nvpair(value
, pair
)) {
2000 if (propbuf
[0] != '\0')
2001 (void) strlcat(propbuf
, ",", proplen
);
2002 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2008 struct get_clones_arg
{
2012 char buf
[ZFS_MAXNAMELEN
];
2016 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2018 struct get_clones_arg
*gca
= arg
;
2020 if (gca
->numclones
== 0) {
2025 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2026 NULL
, NULL
, 0, B_TRUE
) != 0)
2028 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2029 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2034 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2040 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2042 nvlist_t
*nv
, *value
;
2044 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2045 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2046 struct get_clones_arg gca
;
2049 * if this is a snapshot, then the kernel wasn't able
2050 * to get the clones. Do it by slowly iterating.
2052 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2054 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2056 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2061 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2063 gca
.origin
= zhp
->zfs_name
;
2065 if (gca
.numclones
!= 0) {
2067 char pool
[ZFS_MAXNAMELEN
];
2070 /* get the pool name */
2071 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2072 (void) strsep(&cp
, "/@");
2073 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2074 ZFS_TYPE_FILESYSTEM
);
2076 (void) get_clones_cb(root
, &gca
);
2079 if (gca
.numclones
!= 0 ||
2080 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2081 nvlist_add_nvlist(zhp
->zfs_props
,
2082 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2089 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2090 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2093 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2099 * Retrieve a property from the given object. If 'literal' is specified, then
2100 * numbers are left as exact values. Otherwise, numbers are converted to a
2101 * human-readable form.
2103 * Returns 0 on success, or -1 on error.
2106 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2107 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2109 char *source
= NULL
;
2113 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2116 * Check to see if this property applies to our object
2118 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
2121 if (received
&& zfs_prop_readonly(prop
))
2125 *src
= ZPROP_SRC_NONE
;
2128 case ZFS_PROP_CREATION
:
2130 * 'creation' is a time_t stored in the statistics. We convert
2131 * this into a string unless 'literal' is specified.
2134 val
= getprop_uint64(zhp
, prop
, &source
);
2135 time_t time
= (time_t)val
;
2139 localtime_r(&time
, &t
) == NULL
||
2140 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2142 (void) snprintf(propbuf
, proplen
, "%llu",
2143 (u_longlong_t
) val
);
2147 case ZFS_PROP_MOUNTPOINT
:
2149 * Getting the precise mountpoint can be tricky.
2151 * - for 'none' or 'legacy', return those values.
2152 * - for inherited mountpoints, we want to take everything
2153 * after our ancestor and append it to the inherited value.
2155 * If the pool has an alternate root, we want to prepend that
2156 * root to any values we return.
2159 str
= getprop_string(zhp
, prop
, &source
);
2161 if (str
[0] == '/') {
2162 char buf
[MAXPATHLEN
];
2164 const char *relpath
;
2167 * If we inherit the mountpoint, even from a dataset
2168 * with a received value, the source will be the path of
2169 * the dataset we inherit from. If source is
2170 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2173 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2176 relpath
= zhp
->zfs_name
+ strlen(source
);
2177 if (relpath
[0] == '/')
2181 if ((zpool_get_prop(zhp
->zpool_hdl
,
2182 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
2183 (strcmp(root
, "-") == 0))
2186 * Special case an alternate root of '/'. This will
2187 * avoid having multiple leading slashes in the
2190 if (strcmp(root
, "/") == 0)
2194 * If the mountpoint is '/' then skip over this
2195 * if we are obtaining either an alternate root or
2196 * an inherited mountpoint.
2198 if (str
[1] == '\0' && (root
[0] != '\0' ||
2199 relpath
[0] != '\0'))
2202 if (relpath
[0] == '\0')
2203 (void) snprintf(propbuf
, proplen
, "%s%s",
2206 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2207 root
, str
, relpath
[0] == '@' ? "" : "/",
2210 /* 'legacy' or 'none' */
2211 (void) strlcpy(propbuf
, str
, proplen
);
2216 case ZFS_PROP_ORIGIN
:
2217 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2220 * If there is no parent at all, return failure to indicate that
2221 * it doesn't apply to this dataset.
2223 if (propbuf
[0] == '\0')
2227 case ZFS_PROP_CLONES
:
2228 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2232 case ZFS_PROP_QUOTA
:
2233 case ZFS_PROP_REFQUOTA
:
2234 case ZFS_PROP_RESERVATION
:
2235 case ZFS_PROP_REFRESERVATION
:
2237 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2241 * If quota or reservation is 0, we translate this into 'none'
2242 * (unless literal is set), and indicate that it's the default
2243 * value. Otherwise, we print the number nicely and indicate
2244 * that its set locally.
2248 (void) strlcpy(propbuf
, "0", proplen
);
2250 (void) strlcpy(propbuf
, "none", proplen
);
2253 (void) snprintf(propbuf
, proplen
, "%llu",
2256 zfs_nicenum(val
, propbuf
, proplen
);
2260 case ZFS_PROP_REFRATIO
:
2261 case ZFS_PROP_COMPRESSRATIO
:
2262 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2264 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2265 (u_longlong_t
)(val
/ 100),
2266 (u_longlong_t
)(val
% 100));
2270 switch (zhp
->zfs_type
) {
2271 case ZFS_TYPE_FILESYSTEM
:
2274 case ZFS_TYPE_VOLUME
:
2277 case ZFS_TYPE_SNAPSHOT
:
2283 (void) snprintf(propbuf
, proplen
, "%s", str
);
2286 case ZFS_PROP_MOUNTED
:
2288 * The 'mounted' property is a pseudo-property that described
2289 * whether the filesystem is currently mounted. Even though
2290 * it's a boolean value, the typical values of "on" and "off"
2291 * don't make sense, so we translate to "yes" and "no".
2293 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2294 src
, &source
, &val
) != 0)
2297 (void) strlcpy(propbuf
, "yes", proplen
);
2299 (void) strlcpy(propbuf
, "no", proplen
);
2304 * The 'name' property is a pseudo-property derived from the
2305 * dataset name. It is presented as a real property to simplify
2308 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2311 case ZFS_PROP_MLSLABEL
:
2313 #ifdef HAVE_MLSLABEL
2314 m_label_t
*new_sl
= NULL
;
2315 char *ascii
= NULL
; /* human readable label */
2317 (void) strlcpy(propbuf
,
2318 getprop_string(zhp
, prop
, &source
), proplen
);
2320 if (literal
|| (strcasecmp(propbuf
,
2321 ZFS_MLSLABEL_DEFAULT
) == 0))
2325 * Try to translate the internal hex string to
2326 * human-readable output. If there are any
2327 * problems just use the hex string.
2330 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2331 L_NO_CORRECTION
, NULL
) == -1) {
2332 m_label_free(new_sl
);
2336 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2340 m_label_free(new_sl
);
2343 m_label_free(new_sl
);
2345 (void) strlcpy(propbuf
, ascii
, proplen
);
2348 (void) strlcpy(propbuf
,
2349 getprop_string(zhp
, prop
, &source
), proplen
);
2350 #endif /* HAVE_MLSLABEL */
2356 * GUIDs are stored as numbers, but they are identifiers.
2357 * We don't want them to be pretty printed, because pretty
2358 * printing mangles the ID into a truncated and useless value.
2360 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2362 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2366 switch (zfs_prop_get_type(prop
)) {
2367 case PROP_TYPE_NUMBER
:
2368 if (get_numeric_property(zhp
, prop
, src
,
2369 &source
, &val
) != 0)
2372 (void) snprintf(propbuf
, proplen
, "%llu",
2375 zfs_nicenum(val
, propbuf
, proplen
);
2378 case PROP_TYPE_STRING
:
2379 (void) strlcpy(propbuf
,
2380 getprop_string(zhp
, prop
, &source
), proplen
);
2383 case PROP_TYPE_INDEX
:
2384 if (get_numeric_property(zhp
, prop
, src
,
2385 &source
, &val
) != 0)
2387 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2389 (void) strlcpy(propbuf
, strval
, proplen
);
2397 get_source(zhp
, src
, source
, statbuf
, statlen
);
2403 * Utility function to get the given numeric property. Does no validation that
2404 * the given property is the appropriate type; should only be used with
2405 * hard-coded property types.
2408 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2413 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2419 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2423 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2424 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2428 * Similar to zfs_prop_get(), but returns the value as an integer.
2431 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2432 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2437 * Check to see if this property applies to our object
2439 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2440 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2441 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2442 zfs_prop_to_name(prop
)));
2446 *src
= ZPROP_SRC_NONE
;
2448 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2451 get_source(zhp
, src
, source
, statbuf
, statlen
);
2458 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2459 char **domainp
, idmap_rid_t
*ridp
)
2461 idmap_get_handle_t
*get_hdl
= NULL
;
2465 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2469 err
= idmap_get_sidbyuid(get_hdl
, id
,
2470 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2472 err
= idmap_get_sidbygid(get_hdl
, id
,
2473 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2475 if (err
== IDMAP_SUCCESS
&&
2476 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2477 status
== IDMAP_SUCCESS
)
2483 idmap_get_destroy(get_hdl
);
2486 #endif /* HAVE_IDMAP */
2489 * convert the propname into parameters needed by kernel
2490 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2491 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2492 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2493 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2496 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2497 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2499 zfs_userquota_prop_t type
;
2508 /* Figure out the property type ({user|group}{quota|space}) */
2509 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2510 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2511 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2514 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2518 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
);
2519 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
);
2521 cp
= strchr(propname
, '@') + 1;
2523 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2524 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2527 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2528 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2531 } else if (strchr(cp
, '@')) {
2534 * It's a SID name (eg "user@domain") that needs to be
2535 * turned into S-1-domainID-RID.
2537 directory_error_t e
;
2538 char *numericsid
= NULL
;
2541 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2544 e
= directory_sid_from_user_name(NULL
,
2547 e
= directory_sid_from_group_name(NULL
,
2551 directory_error_free(e
);
2554 if (numericsid
== NULL
)
2557 (void) strlcpy(domain
, cp
, domainlen
);
2558 cp
= strrchr(domain
, '-');
2563 *ridp
= strtoull(cp
, &end
, 10);
2566 if (errno
!= 0 || *end
!= '\0')
2570 #endif /* HAVE_IDMAP */
2572 /* It's a user/group ID (eg "12345"). */
2575 id
= strtoul(cp
, &end
, 10);
2580 /* It's an ephemeral ID. */
2584 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2585 &mapdomain
, &rid
) != 0)
2587 (void) strlcpy(domain
, mapdomain
, domainlen
);
2591 #endif /* HAVE_IDMAP */
2601 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2602 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2605 zfs_cmd_t zc
= {"\0"};
2607 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2609 err
= userquota_propname_decode(propname
,
2610 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2611 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2612 zc
.zc_objset_type
= *typep
;
2616 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2620 *propvalue
= zc
.zc_cookie
;
2625 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2626 uint64_t *propvalue
)
2628 zfs_userquota_prop_t type
;
2630 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2635 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2636 char *propbuf
, int proplen
, boolean_t literal
)
2640 zfs_userquota_prop_t type
;
2642 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2649 (void) snprintf(propbuf
, proplen
, "%llu",
2650 (u_longlong_t
)propvalue
);
2651 } else if (propvalue
== 0 &&
2652 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2653 (void) strlcpy(propbuf
, "none", proplen
);
2655 zfs_nicenum(propvalue
, propbuf
, proplen
);
2661 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2662 uint64_t *propvalue
)
2665 zfs_cmd_t zc
= {"\0"};
2666 const char *snapname
;
2668 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2670 snapname
= strchr(propname
, '@') + 1;
2671 if (strchr(snapname
, '@')) {
2672 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2674 /* snapname is the short name, append it to zhp's fsname */
2677 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2678 sizeof (zc
.zc_value
));
2679 cp
= strchr(zc
.zc_value
, '@');
2682 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2683 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2686 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2690 *propvalue
= zc
.zc_cookie
;
2695 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2696 char *propbuf
, int proplen
, boolean_t literal
)
2701 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2707 (void) snprintf(propbuf
, proplen
, "%llu",
2708 (u_longlong_t
)propvalue
);
2710 zfs_nicenum(propvalue
, propbuf
, proplen
);
2717 * Returns the name of the given zfs handle.
2720 zfs_get_name(const zfs_handle_t
*zhp
)
2722 return (zhp
->zfs_name
);
2726 * Returns the type of the given zfs handle.
2729 zfs_get_type(const zfs_handle_t
*zhp
)
2731 return (zhp
->zfs_type
);
2735 * Is one dataset name a child dataset of another?
2737 * Needs to handle these cases:
2738 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2739 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2740 * Descendant? No. No. No. Yes.
2743 is_descendant(const char *ds1
, const char *ds2
)
2745 size_t d1len
= strlen(ds1
);
2747 /* ds2 can't be a descendant if it's smaller */
2748 if (strlen(ds2
) < d1len
)
2751 /* otherwise, compare strings and verify that there's a '/' char */
2752 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2756 * Given a complete name, return just the portion that refers to the parent.
2757 * Will return -1 if there is no parent (path is just the name of the
2761 parent_name(const char *path
, char *buf
, size_t buflen
)
2765 (void) strlcpy(buf
, path
, buflen
);
2767 if ((slashp
= strrchr(buf
, '/')) == NULL
)
2775 * If accept_ancestor is false, then check to make sure that the given path has
2776 * a parent, and that it exists. If accept_ancestor is true, then find the
2777 * closest existing ancestor for the given path. In prefixlen return the
2778 * length of already existing prefix of the given path. We also fetch the
2779 * 'zoned' property, which is used to validate property settings when creating
2783 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2784 boolean_t accept_ancestor
, int *prefixlen
)
2786 zfs_cmd_t zc
= {"\0"};
2787 char parent
[ZFS_MAXNAMELEN
];
2793 (void) snprintf(errbuf
, sizeof (errbuf
),
2794 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2796 /* get parent, and check to see if this is just a pool */
2797 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2798 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2799 "missing dataset name"));
2800 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2803 /* check to see if the pool exists */
2804 if ((slash
= strchr(parent
, '/')) == NULL
)
2805 slash
= parent
+ strlen(parent
);
2806 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2807 zc
.zc_name
[slash
- parent
] = '\0';
2808 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2810 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2811 "no such pool '%s'"), zc
.zc_name
);
2812 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2815 /* check to see if the parent dataset exists */
2816 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2817 if (errno
== ENOENT
&& accept_ancestor
) {
2819 * Go deeper to find an ancestor, give up on top level.
2821 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2822 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2823 "no such pool '%s'"), zc
.zc_name
);
2824 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2826 } else if (errno
== ENOENT
) {
2827 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2828 "parent does not exist"));
2829 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2831 return (zfs_standard_error(hdl
, errno
, errbuf
));
2834 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2838 /* we are in a non-global zone, but parent is in the global zone */
2839 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2840 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2845 /* make sure parent is a filesystem */
2846 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2847 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2848 "parent is not a filesystem"));
2849 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2855 if (prefixlen
!= NULL
)
2856 *prefixlen
= strlen(parent
);
2861 * Finds whether the dataset of the given type(s) exists.
2864 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2868 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2872 * Try to get stats for the dataset, which will tell us if it exists.
2874 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2875 int ds_type
= zhp
->zfs_type
;
2878 if (types
& ds_type
)
2885 * Given a path to 'target', create all the ancestors between
2886 * the prefixlen portion of the path, and the target itself.
2887 * Fail if the initial prefixlen-ancestor does not already exist.
2890 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2896 /* make sure prefix exists */
2897 cp
= target
+ prefixlen
;
2899 assert(strchr(cp
, '/') == NULL
);
2900 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2903 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2911 * Attempt to create, mount, and share any ancestor filesystems,
2912 * up to the prefixlen-long one.
2914 for (cp
= target
+ prefixlen
+ 1;
2915 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
2919 h
= make_dataset_handle(hdl
, target
);
2921 /* it already exists, nothing to do here */
2926 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2928 opname
= dgettext(TEXT_DOMAIN
, "create");
2932 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2934 opname
= dgettext(TEXT_DOMAIN
, "open");
2938 if (zfs_mount(h
, NULL
, 0) != 0) {
2939 opname
= dgettext(TEXT_DOMAIN
, "mount");
2943 if (zfs_share(h
) != 0) {
2944 opname
= dgettext(TEXT_DOMAIN
, "share");
2954 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2955 "failed to %s ancestor '%s'"), opname
, target
);
2960 * Creates non-existing ancestors of the given path.
2963 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2969 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
2972 if ((path_copy
= strdup(path
)) != NULL
) {
2973 rc
= create_parents(hdl
, path_copy
, prefix
);
2976 if (path_copy
== NULL
|| rc
!= 0)
2983 * Create a new filesystem or volume.
2986 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2991 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2994 dmu_objset_type_t ost
;
2996 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2997 "cannot create '%s'"), path
);
2999 /* validate the path, taking care to note the extended error message */
3000 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3001 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3003 /* validate parents exist */
3004 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3008 * The failure modes when creating a dataset of a different type over
3009 * one that already exists is a little strange. In particular, if you
3010 * try to create a dataset on top of an existing dataset, the ioctl()
3011 * will return ENOENT, not EEXIST. To prevent this from happening, we
3012 * first try to see if the dataset exists.
3014 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3015 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3016 "dataset already exists"));
3017 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3020 if (type
== ZFS_TYPE_VOLUME
)
3025 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3026 zoned
, NULL
, errbuf
)) == 0)
3029 if (type
== ZFS_TYPE_VOLUME
) {
3031 * If we are creating a volume, the size and block size must
3032 * satisfy a few restraints. First, the blocksize must be a
3033 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3034 * volsize must be a multiple of the block size, and cannot be
3037 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3038 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3040 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3041 "missing volume size"));
3042 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3045 if ((ret
= nvlist_lookup_uint64(props
,
3046 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3047 &blocksize
)) != 0) {
3048 if (ret
== ENOENT
) {
3049 blocksize
= zfs_prop_default_numeric(
3050 ZFS_PROP_VOLBLOCKSIZE
);
3053 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3054 "missing volume block size"));
3055 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3061 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3062 "volume size cannot be zero"));
3063 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3066 if (size
% blocksize
!= 0) {
3068 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3069 "volume size must be a multiple of volume block "
3071 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3075 /* create the dataset */
3076 ret
= lzc_create(path
, ost
, props
);
3079 /* check for failure */
3081 char parent
[ZFS_MAXNAMELEN
];
3082 (void) parent_name(path
, parent
, sizeof (parent
));
3086 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3087 "no such parent '%s'"), parent
);
3088 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3091 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3092 "parent '%s' is not a filesystem"), parent
);
3093 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3096 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3097 "volume block size must be power of 2 from "
3099 (uint_t
)SPA_MINBLOCKSIZE
,
3100 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
3102 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3105 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3106 "pool must be upgraded to set this "
3107 "property or value"));
3108 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3112 * This platform can't address a volume this big.
3114 if (type
== ZFS_TYPE_VOLUME
)
3115 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3120 return (zfs_standard_error(hdl
, errno
, errbuf
));
3128 * Destroys the given dataset. The caller must make sure that the filesystem
3129 * isn't mounted, and that there are no active dependents. If the file system
3130 * does not exist this function does nothing.
3133 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3135 zfs_cmd_t zc
= {"\0"};
3137 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3139 if (ZFS_IS_VOLUME(zhp
)) {
3140 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3142 zc
.zc_objset_type
= DMU_OST_ZFS
;
3145 zc
.zc_defer_destroy
= defer
;
3146 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3148 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3149 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3153 remove_mountpoint(zhp
);
3158 struct destroydata
{
3160 const char *snapname
;
3164 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3166 struct destroydata
*dd
= arg
;
3167 char name
[ZFS_MAXNAMELEN
];
3170 (void) snprintf(name
, sizeof (name
),
3171 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3173 if (lzc_exists(name
))
3174 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3176 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3182 * Destroys all snapshots with the given name in zhp & descendants.
3185 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3188 struct destroydata dd
= { 0 };
3190 dd
.snapname
= snapname
;
3191 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3192 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3194 if (nvlist_empty(dd
.nvl
)) {
3195 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3196 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3197 zhp
->zfs_name
, snapname
);
3199 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3201 nvlist_free(dd
.nvl
);
3206 * Destroys all the snapshots named in the nvlist.
3209 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3215 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3220 if (nvlist_empty(errlist
)) {
3222 (void) snprintf(errbuf
, sizeof (errbuf
),
3223 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3225 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3227 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3228 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3230 (void) snprintf(errbuf
, sizeof (errbuf
),
3231 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3234 switch (fnvpair_value_int32(pair
)) {
3237 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3238 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3241 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3250 * Clones the given dataset. The target must be of the same type as the source.
3253 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3255 char parent
[ZFS_MAXNAMELEN
];
3258 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3261 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3263 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3264 "cannot create '%s'"), target
);
3266 /* validate the target/clone name */
3267 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3268 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3270 /* validate parents exist */
3271 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3274 (void) parent_name(target
, parent
, sizeof (parent
));
3280 if (ZFS_IS_VOLUME(zhp
)) {
3281 type
= ZFS_TYPE_VOLUME
;
3283 type
= ZFS_TYPE_FILESYSTEM
;
3285 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3286 zhp
, errbuf
)) == NULL
)
3290 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3298 * The parent doesn't exist. We should have caught this
3299 * above, but there may a race condition that has since
3300 * destroyed the parent.
3302 * At this point, we don't know whether it's the source
3303 * that doesn't exist anymore, or whether the target
3304 * dataset doesn't exist.
3306 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3307 "no such parent '%s'"), parent
);
3308 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3311 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3312 "source and target pools differ"));
3313 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3317 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3326 * Promotes the given clone fs to be the clone parent.
3329 zfs_promote(zfs_handle_t
*zhp
)
3331 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3332 zfs_cmd_t zc
= {"\0"};
3333 char parent
[MAXPATHLEN
];
3337 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3338 "cannot promote '%s'"), zhp
->zfs_name
);
3340 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3341 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3342 "snapshots can not be promoted"));
3343 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3346 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3347 if (parent
[0] == '\0') {
3348 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3349 "not a cloned filesystem"));
3350 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3353 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3354 sizeof (zc
.zc_value
));
3355 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3356 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3359 int save_errno
= errno
;
3361 switch (save_errno
) {
3363 /* There is a conflicting snapshot name. */
3364 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3365 "conflicting snapshot '%s' from parent '%s'"),
3366 zc
.zc_string
, parent
);
3367 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3370 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3376 typedef struct snapdata
{
3378 const char *sd_snapname
;
3382 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3384 snapdata_t
*sd
= arg
;
3385 char name
[ZFS_MAXNAMELEN
];
3388 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3389 (void) snprintf(name
, sizeof (name
),
3390 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3392 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3394 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3402 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3406 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3413 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3414 "cannot create snapshots "));
3417 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3418 const char *snapname
= nvpair_name(elem
);
3420 /* validate the target name */
3421 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3423 (void) snprintf(errbuf
, sizeof (errbuf
),
3424 dgettext(TEXT_DOMAIN
,
3425 "cannot create snapshot '%s'"), snapname
);
3426 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3430 if (props
!= NULL
&&
3431 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3432 props
, B_FALSE
, NULL
, errbuf
)) == NULL
) {
3436 ret
= lzc_snapshot(snaps
, props
, &errors
);
3439 boolean_t printed
= B_FALSE
;
3440 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3442 elem
= nvlist_next_nvpair(errors
, elem
)) {
3443 (void) snprintf(errbuf
, sizeof (errbuf
),
3444 dgettext(TEXT_DOMAIN
,
3445 "cannot create snapshot '%s'"), nvpair_name(elem
));
3446 (void) zfs_standard_error(hdl
,
3447 fnvpair_value_int32(elem
), errbuf
);
3453 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3454 "multiple snapshots of same "
3456 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3460 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3466 nvlist_free(errors
);
3471 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3475 snapdata_t sd
= { 0 };
3476 char fsname
[ZFS_MAXNAMELEN
];
3481 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3482 "cannot snapshot %s"), path
);
3484 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3485 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3487 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3488 cp
= strchr(fsname
, '@');
3490 sd
.sd_snapname
= cp
+ 1;
3492 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3493 ZFS_TYPE_VOLUME
)) == NULL
) {
3497 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3499 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3501 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3504 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3505 nvlist_free(sd
.sd_nvl
);
3511 * Destroy any more recent snapshots. We invoke this callback on any dependents
3512 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3513 * is a dependent and we should just destroy it without checking the transaction
3516 typedef struct rollback_data
{
3517 const char *cb_target
; /* the snapshot */
3518 uint64_t cb_create
; /* creation time reference */
3520 boolean_t cb_dependent
;
3525 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3527 rollback_data_t
*cbp
= data
;
3529 if (!cbp
->cb_dependent
) {
3530 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3531 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3532 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3535 cbp
->cb_dependent
= B_TRUE
;
3536 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3537 rollback_destroy
, cbp
);
3538 cbp
->cb_dependent
= B_FALSE
;
3540 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3543 /* We must destroy this clone; first unmount it */
3544 prop_changelist_t
*clp
;
3546 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3547 cbp
->cb_force
? MS_FORCE
: 0);
3548 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3549 cbp
->cb_error
= B_TRUE
;
3553 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3554 cbp
->cb_error
= B_TRUE
;
3556 changelist_remove(clp
, zhp
->zfs_name
);
3557 (void) changelist_postfix(clp
);
3558 changelist_free(clp
);
3566 * Given a dataset, rollback to a specific snapshot, discarding any
3567 * data changes since then and making it the active dataset.
3569 * Any snapshots more recent than the target are destroyed, along with
3573 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3575 rollback_data_t cb
= { 0 };
3577 boolean_t restore_resv
= 0;
3578 uint64_t old_volsize
= 0, new_volsize
;
3579 zfs_prop_t resv_prop
= { 0 };
3581 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3582 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3585 * Destroy all recent snapshots and their dependents.
3587 cb
.cb_force
= force
;
3588 cb
.cb_target
= snap
->zfs_name
;
3589 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3590 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3596 * Now that we have verified that the snapshot is the latest,
3597 * rollback to the given snapshot.
3600 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3601 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3603 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3605 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3609 * We rely on zfs_iter_children() to verify that there are no
3610 * newer snapshots for the given dataset. Therefore, we can
3611 * simply pass the name on to the ioctl() call. There is still
3612 * an unlikely race condition where the user has taken a
3613 * snapshot since we verified that this was the most recent.
3615 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3617 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3618 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3624 * For volumes, if the pre-rollback volsize matched the pre-
3625 * rollback reservation and the volsize has changed then set
3626 * the reservation property to the post-rollback volsize.
3627 * Make a new handle since the rollback closed the dataset.
3629 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3630 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3632 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3633 if (old_volsize
!= new_volsize
)
3634 err
= zfs_prop_set_int(zhp
, resv_prop
,
3643 * Renames the given dataset.
3646 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
3647 boolean_t force_unmount
)
3650 zfs_cmd_t zc
= {"\0"};
3652 prop_changelist_t
*cl
= NULL
;
3653 zfs_handle_t
*zhrp
= NULL
;
3654 char *parentname
= NULL
;
3655 char parent
[ZFS_MAXNAMELEN
];
3656 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3659 /* if we have the same exact name, just return success */
3660 if (strcmp(zhp
->zfs_name
, target
) == 0)
3663 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3664 "cannot rename to '%s'"), target
);
3667 * Make sure the target name is valid
3669 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3670 if ((strchr(target
, '@') == NULL
) ||
3673 * Snapshot target name is abbreviated,
3674 * reconstruct full dataset name
3676 (void) strlcpy(parent
, zhp
->zfs_name
,
3678 delim
= strchr(parent
, '@');
3679 if (strchr(target
, '@') == NULL
)
3683 (void) strlcat(parent
, target
, sizeof (parent
));
3687 * Make sure we're renaming within the same dataset.
3689 delim
= strchr(target
, '@');
3690 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3691 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3692 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3693 "snapshots must be part of same "
3695 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3699 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3700 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3703 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3704 "recursive rename must be a snapshot"));
3705 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3708 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3709 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3711 /* validate parents */
3712 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3715 /* make sure we're in the same pool */
3716 verify((delim
= strchr(target
, '/')) != NULL
);
3717 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3718 zhp
->zfs_name
[delim
- target
] != '/') {
3719 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3720 "datasets must be within same pool"));
3721 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3724 /* new name cannot be a child of the current dataset name */
3725 if (is_descendant(zhp
->zfs_name
, target
)) {
3726 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3727 "New dataset name cannot be a descendant of "
3728 "current dataset name"));
3729 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3733 (void) snprintf(errbuf
, sizeof (errbuf
),
3734 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3736 if (getzoneid() == GLOBAL_ZONEID
&&
3737 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3738 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3739 "dataset is used in a non-global zone"));
3740 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3745 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3746 if (parentname
== NULL
) {
3750 delim
= strchr(parentname
, '@');
3752 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3759 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3760 force_unmount
? MS_FORCE
: 0)) == NULL
)
3763 if (changelist_haszonedchild(cl
)) {
3764 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3765 "child dataset with inherited mountpoint is used "
3766 "in a non-global zone"));
3767 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3772 if ((ret
= changelist_prefix(cl
)) != 0)
3776 if (ZFS_IS_VOLUME(zhp
))
3777 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3779 zc
.zc_objset_type
= DMU_OST_ZFS
;
3781 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3782 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3784 zc
.zc_cookie
= recursive
;
3786 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3788 * if it was recursive, the one that actually failed will
3791 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3792 "cannot rename '%s'"), zc
.zc_name
);
3794 if (recursive
&& errno
== EEXIST
) {
3795 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3796 "a child dataset already has a snapshot "
3797 "with the new name"));
3798 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3800 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3804 * On failure, we still want to remount any filesystems that
3805 * were previously mounted, so we don't alter the system state.
3808 (void) changelist_postfix(cl
);
3811 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3812 ret
= changelist_postfix(cl
);
3824 changelist_free(cl
);
3830 zfs_get_user_props(zfs_handle_t
*zhp
)
3832 return (zhp
->zfs_user_props
);
3836 * This function is used by 'zfs list' to determine the exact set of columns to
3837 * display, and their maximum widths. This does two main things:
3839 * - If this is a list of all properties, then expand the list to include
3840 * all native properties, and set a flag so that for each dataset we look
3841 * for new unique user properties and add them to the list.
3843 * - For non fixed-width properties, keep track of the maximum width seen
3844 * so that we can size the column appropriately. If the user has
3845 * requested received property values, we also need to compute the width
3846 * of the RECEIVED column.
3849 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
3852 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3853 zprop_list_t
*entry
;
3854 zprop_list_t
**last
, **start
;
3855 nvlist_t
*userprops
, *propval
;
3858 char buf
[ZFS_MAXPROPLEN
];
3860 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
3863 userprops
= zfs_get_user_props(zhp
);
3866 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
3868 * Go through and add any user properties as necessary. We
3869 * start by incrementing our list pointer to the first
3870 * non-native property.
3873 while (*start
!= NULL
) {
3874 if ((*start
)->pl_prop
== ZPROP_INVAL
)
3876 start
= &(*start
)->pl_next
;
3880 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
3882 * See if we've already found this property in our list.
3884 for (last
= start
; *last
!= NULL
;
3885 last
= &(*last
)->pl_next
) {
3886 if (strcmp((*last
)->pl_user_prop
,
3887 nvpair_name(elem
)) == 0)
3891 if (*last
== NULL
) {
3892 if ((entry
= zfs_alloc(hdl
,
3893 sizeof (zprop_list_t
))) == NULL
||
3894 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
3895 nvpair_name(elem
)))) == NULL
) {
3900 entry
->pl_prop
= ZPROP_INVAL
;
3901 entry
->pl_width
= strlen(nvpair_name(elem
));
3902 entry
->pl_all
= B_TRUE
;
3909 * Now go through and check the width of any non-fixed columns
3911 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
3912 if (entry
->pl_fixed
&& !literal
)
3915 if (entry
->pl_prop
!= ZPROP_INVAL
) {
3916 if (zfs_prop_get(zhp
, entry
->pl_prop
,
3917 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
3918 if (strlen(buf
) > entry
->pl_width
)
3919 entry
->pl_width
= strlen(buf
);
3921 if (received
&& zfs_prop_get_recvd(zhp
,
3922 zfs_prop_to_name(entry
->pl_prop
),
3923 buf
, sizeof (buf
), literal
) == 0)
3924 if (strlen(buf
) > entry
->pl_recvd_width
)
3925 entry
->pl_recvd_width
= strlen(buf
);
3927 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
3929 verify(nvlist_lookup_string(propval
,
3930 ZPROP_VALUE
, &strval
) == 0);
3931 if (strlen(strval
) > entry
->pl_width
)
3932 entry
->pl_width
= strlen(strval
);
3934 if (received
&& zfs_prop_get_recvd(zhp
,
3935 entry
->pl_user_prop
,
3936 buf
, sizeof (buf
), literal
) == 0)
3937 if (strlen(buf
) > entry
->pl_recvd_width
)
3938 entry
->pl_recvd_width
= strlen(buf
);
3946 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
3951 * Keep a reference to the props-table against which we prune the
3954 zhp
->zfs_props_table
= props
;
3956 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
3959 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
3960 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
3963 * User properties will result in ZPROP_INVAL, and since we
3964 * only know how to prune standard ZFS properties, we always
3965 * leave these in the list. This can also happen if we
3966 * encounter an unknown DSL property (when running older
3967 * software, for example).
3969 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
3970 (void) nvlist_remove(zhp
->zfs_props
,
3971 nvpair_name(curr
), nvpair_type(curr
));
3977 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
3978 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
3980 zfs_cmd_t zc
= {"\0"};
3981 nvlist_t
*nvlist
= NULL
;
3984 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3985 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3986 zc
.zc_cookie
= (uint64_t)cmd
;
3988 if (cmd
== ZFS_SMB_ACL_RENAME
) {
3989 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
3990 (void) no_memory(hdl
);
3996 case ZFS_SMB_ACL_ADD
:
3997 case ZFS_SMB_ACL_REMOVE
:
3998 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4000 case ZFS_SMB_ACL_RENAME
:
4001 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4003 (void) no_memory(hdl
);
4006 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4008 (void) no_memory(hdl
);
4011 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4012 nvlist_free(nvlist
);
4016 case ZFS_SMB_ACL_PURGE
:
4021 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4023 nvlist_free(nvlist
);
4028 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4029 char *path
, char *resource
)
4031 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4036 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4037 char *path
, char *resource
)
4039 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4044 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4046 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4051 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4052 char *oldname
, char *newname
)
4054 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4059 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4060 zfs_userspace_cb_t func
, void *arg
)
4062 zfs_cmd_t zc
= {"\0"};
4063 zfs_useracct_t buf
[100];
4064 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4067 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4069 zc
.zc_objset_type
= type
;
4070 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4073 zfs_useracct_t
*zua
= buf
;
4075 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4076 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4077 char errbuf
[ZFS_MAXNAMELEN
+ 32];
4079 (void) snprintf(errbuf
, sizeof (errbuf
),
4080 dgettext(TEXT_DOMAIN
,
4081 "cannot get used/quota for %s"), zc
.zc_name
);
4082 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4084 if (zc
.zc_nvlist_dst_size
== 0)
4087 while (zc
.zc_nvlist_dst_size
> 0) {
4088 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4089 zua
->zu_space
)) != 0)
4092 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4101 const char *snapname
;
4103 boolean_t recursive
;
4108 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4110 struct holdarg
*ha
= arg
;
4111 char name
[ZFS_MAXNAMELEN
];
4114 (void) snprintf(name
, sizeof (name
),
4115 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4117 if (lzc_exists(name
))
4118 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4121 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4127 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4128 boolean_t recursive
, int cleanup_fd
)
4133 ha
.nvl
= fnvlist_alloc();
4134 ha
.snapname
= snapname
;
4136 ha
.recursive
= recursive
;
4137 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4139 if (nvlist_empty(ha
.nvl
)) {
4142 fnvlist_free(ha
.nvl
);
4144 (void) snprintf(errbuf
, sizeof (errbuf
),
4145 dgettext(TEXT_DOMAIN
,
4146 "cannot hold snapshot '%s@%s'"),
4147 zhp
->zfs_name
, snapname
);
4148 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4152 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4153 fnvlist_free(ha
.nvl
);
4159 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4163 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4168 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4171 /* There may be errors even in the success case. */
4172 fnvlist_free(errors
);
4176 if (nvlist_empty(errors
)) {
4177 /* no hold-specific errors */
4178 (void) snprintf(errbuf
, sizeof (errbuf
),
4179 dgettext(TEXT_DOMAIN
, "cannot hold"));
4182 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4183 "pool must be upgraded"));
4184 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4187 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4190 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4194 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4196 elem
= nvlist_next_nvpair(errors
, elem
)) {
4197 (void) snprintf(errbuf
, sizeof (errbuf
),
4198 dgettext(TEXT_DOMAIN
,
4199 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4200 switch (fnvpair_value_int32(elem
)) {
4203 * Temporary tags wind up having the ds object id
4204 * prepended. So even if we passed the length check
4205 * above, it's still possible for the tag to wind
4206 * up being slightly too long.
4208 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4211 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4214 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4217 (void) zfs_standard_error(hdl
,
4218 fnvpair_value_int32(elem
), errbuf
);
4222 fnvlist_free(errors
);
4227 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4229 struct holdarg
*ha
= arg
;
4230 char name
[ZFS_MAXNAMELEN
];
4232 nvlist_t
*existing_holds
;
4234 (void) snprintf(name
, sizeof (name
),
4235 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4237 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4239 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4242 nvlist_t
*torelease
= fnvlist_alloc();
4243 fnvlist_add_boolean(torelease
, ha
->tag
);
4244 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4245 fnvlist_free(torelease
);
4249 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4255 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4256 boolean_t recursive
)
4260 nvlist_t
*errors
= NULL
;
4262 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4265 ha
.nvl
= fnvlist_alloc();
4266 ha
.snapname
= snapname
;
4268 ha
.recursive
= recursive
;
4270 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4272 if (nvlist_empty(ha
.nvl
)) {
4273 fnvlist_free(ha
.nvl
);
4275 (void) snprintf(errbuf
, sizeof (errbuf
),
4276 dgettext(TEXT_DOMAIN
,
4277 "cannot release hold from snapshot '%s@%s'"),
4278 zhp
->zfs_name
, snapname
);
4280 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4282 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4287 ret
= lzc_release(ha
.nvl
, &errors
);
4288 fnvlist_free(ha
.nvl
);
4291 /* There may be errors even in the success case. */
4292 fnvlist_free(errors
);
4296 if (nvlist_empty(errors
)) {
4297 /* no hold-specific errors */
4298 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4302 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4303 "pool must be upgraded"));
4304 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4307 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4311 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4313 elem
= nvlist_next_nvpair(errors
, elem
)) {
4314 (void) snprintf(errbuf
, sizeof (errbuf
),
4315 dgettext(TEXT_DOMAIN
,
4316 "cannot release hold from snapshot '%s'"),
4318 switch (fnvpair_value_int32(elem
)) {
4320 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4323 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4326 (void) zfs_standard_error_fmt(hdl
,
4327 fnvpair_value_int32(elem
), errbuf
);
4331 fnvlist_free(errors
);
4336 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4338 zfs_cmd_t zc
= {"\0"};
4339 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4345 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4346 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4350 nvbuf
= malloc(nvsz
);
4351 if (nvbuf
== NULL
) {
4352 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4356 zc
.zc_nvlist_dst_size
= nvsz
;
4357 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4359 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4361 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4362 (void) snprintf(errbuf
, sizeof (errbuf
),
4363 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4368 nvsz
= zc
.zc_nvlist_dst_size
;
4372 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4373 "pool must be upgraded"));
4374 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4377 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4380 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4383 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4388 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4390 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4391 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4393 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4403 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4405 zfs_cmd_t zc
= {"\0"};
4406 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4412 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4413 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4415 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4418 nvbuf
= malloc(nvsz
);
4420 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4423 zc
.zc_nvlist_src_size
= nvsz
;
4424 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4425 zc
.zc_perm_action
= un
;
4427 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4429 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4430 (void) snprintf(errbuf
, sizeof (errbuf
),
4431 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4435 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4436 "pool must be upgraded"));
4437 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4440 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4443 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4446 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4457 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4462 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4465 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4467 (void) snprintf(errbuf
, sizeof (errbuf
),
4468 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4473 "pool must be upgraded"));
4474 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4477 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4480 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4483 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4492 * Convert the zvol's volume size to an appropriate reservation.
4493 * Note: If this routine is updated, it is necessary to update the ZFS test
4494 * suite's shell version in reservation.kshlib.
4497 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4500 uint64_t nblocks
, volblocksize
;
4504 if (nvlist_lookup_string(props
,
4505 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4506 ncopies
= atoi(strval
);
4509 if (nvlist_lookup_uint64(props
,
4510 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4511 &volblocksize
) != 0)
4512 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4513 nblocks
= volsize
/volblocksize
;
4514 /* start with metadnode L0-L6 */
4516 /* calculate number of indirects */
4517 while (nblocks
> 1) {
4518 nblocks
+= DNODES_PER_LEVEL
- 1;
4519 nblocks
/= DNODES_PER_LEVEL
;
4522 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4525 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4526 * compressed, but in practice they compress down to about
4529 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;