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, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
44 #include <sys/mntent.h>
45 #include <sys/mount.h>
54 #include <directory.h>
55 #endif /* HAVE_IDMAP */
57 #include <sys/dnode.h>
62 #include "zfs_namecheck.h"
64 #include "libzfs_impl.h"
65 #include "zfs_deleg.h"
67 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
68 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
71 * Given a single type (not a mask of types), return the type in a human
75 zfs_type_to_name(zfs_type_t type
)
78 case ZFS_TYPE_FILESYSTEM
:
79 return (dgettext(TEXT_DOMAIN
, "filesystem"));
80 case ZFS_TYPE_SNAPSHOT
:
81 return (dgettext(TEXT_DOMAIN
, "snapshot"));
83 return (dgettext(TEXT_DOMAIN
, "volume"));
92 * Validate a ZFS path. This is used even before trying to open the dataset, to
93 * provide a more meaningful error message. We call zfs_error_aux() to
94 * explain exactly why the name was not valid.
97 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
103 (void) zfs_prop_get_table();
104 if (dataset_namecheck(path
, &why
, &what
) != 0) {
107 case NAME_ERR_TOOLONG
:
108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
109 "name is too long"));
112 case NAME_ERR_LEADING_SLASH
:
113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
114 "leading slash in name"));
117 case NAME_ERR_EMPTY_COMPONENT
:
118 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
119 "empty component in name"));
122 case NAME_ERR_TRAILING_SLASH
:
123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
124 "trailing slash in name"));
127 case NAME_ERR_INVALCHAR
:
129 dgettext(TEXT_DOMAIN
, "invalid character "
130 "'%c' in name"), what
);
133 case NAME_ERR_MULTIPLE_AT
:
134 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
135 "multiple '@' delimiters in name"));
138 case NAME_ERR_NOLETTER
:
139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
140 "pool doesn't begin with a letter"));
143 case NAME_ERR_RESERVED
:
144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
145 "name is reserved"));
148 case NAME_ERR_DISKLIKE
:
149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
150 "reserved disk name"));
160 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
162 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
163 "snapshot delimiter '@' in filesystem name"));
167 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
169 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
170 "missing '@' delimiter in snapshot name"));
174 if (modifying
&& strchr(path
, '%') != NULL
) {
176 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
177 "invalid character %c in name"), '%');
185 zfs_name_valid(const char *name
, zfs_type_t type
)
187 if (type
== ZFS_TYPE_POOL
)
188 return (zpool_name_valid(NULL
, B_FALSE
, name
));
189 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
193 * This function takes the raw DSL properties, and filters out the user-defined
194 * properties into a separate nvlist.
197 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
199 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
204 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
205 (void) no_memory(hdl
);
210 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
211 if (!zfs_prop_user(nvpair_name(elem
)))
214 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
215 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
217 (void) no_memory(hdl
);
225 static zpool_handle_t
*
226 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
228 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
231 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
232 if (hdl
->libzfs_pool_handles
!= NULL
)
233 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
234 hdl
->libzfs_pool_handles
= zph
;
239 static zpool_handle_t
*
240 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
242 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
243 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
245 while ((zph
!= NULL
) &&
246 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
247 zph
= zph
->zpool_next
;
252 * Returns a handle to the pool that contains the provided dataset.
253 * If a handle to that pool already exists then that handle is returned.
254 * Otherwise, a new handle is created and added to the list of handles.
256 static zpool_handle_t
*
257 zpool_handle(zfs_handle_t
*zhp
)
263 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
264 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
265 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
267 zph
= zpool_find_handle(zhp
, pool_name
, len
);
269 zph
= zpool_add_handle(zhp
, pool_name
);
276 zpool_free_handles(libzfs_handle_t
*hdl
)
278 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
280 while (zph
!= NULL
) {
281 next
= zph
->zpool_next
;
285 hdl
->libzfs_pool_handles
= NULL
;
289 * Utility function to gather stats (objset and zpl) for the given object.
292 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
294 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
296 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
298 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
299 if (errno
== ENOMEM
) {
300 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
311 * Utility function to get the received properties of the given object.
314 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
316 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
317 nvlist_t
*recvdprops
;
318 zfs_cmd_t zc
= {"\0"};
321 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
324 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
326 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
327 if (errno
== ENOMEM
) {
328 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
332 zcmd_free_nvlists(&zc
);
337 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
338 zcmd_free_nvlists(&zc
);
342 nvlist_free(zhp
->zfs_recvd_props
);
343 zhp
->zfs_recvd_props
= recvdprops
;
349 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
351 nvlist_t
*allprops
, *userprops
;
353 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
355 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
360 * XXX Why do we store the user props separately, in addition to
361 * storing them in zfs_props?
363 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
364 nvlist_free(allprops
);
368 nvlist_free(zhp
->zfs_props
);
369 nvlist_free(zhp
->zfs_user_props
);
371 zhp
->zfs_props
= allprops
;
372 zhp
->zfs_user_props
= userprops
;
378 get_stats(zfs_handle_t
*zhp
)
381 zfs_cmd_t zc
= {"\0"};
383 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
385 if (get_stats_ioctl(zhp
, &zc
) != 0)
387 else if (put_stats_zhdl(zhp
, &zc
) != 0)
389 zcmd_free_nvlists(&zc
);
394 * Refresh the properties currently stored in the handle.
397 zfs_refresh_properties(zfs_handle_t
*zhp
)
399 (void) get_stats(zhp
);
403 * Makes a handle from the given dataset name. Used by zfs_open() and
404 * zfs_iter_* to create child handles on the fly.
407 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
409 if (put_stats_zhdl(zhp
, zc
) != 0)
413 * We've managed to open the dataset and gather statistics. Determine
414 * the high-level type.
416 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
417 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
418 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
419 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
420 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
425 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
426 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
427 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
428 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
429 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
430 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
432 abort(); /* we should never see any other types */
434 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
441 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
443 zfs_cmd_t zc
= {"\0"};
445 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
451 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
452 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
456 if (get_stats_ioctl(zhp
, &zc
) == -1) {
457 zcmd_free_nvlists(&zc
);
461 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
465 zcmd_free_nvlists(&zc
);
470 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
472 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
478 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
479 if (make_dataset_handle_common(zhp
, zc
) == -1) {
487 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
489 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
494 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
495 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
496 zhp
->zfs_head_type
= pzhp
->zfs_type
;
497 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
498 zhp
->zpool_hdl
= zpool_handle(zhp
);
504 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
506 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
511 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
512 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
513 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
514 sizeof (zhp
->zfs_name
));
515 zhp
->zfs_type
= zhp_orig
->zfs_type
;
516 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
517 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
518 if (zhp_orig
->zfs_props
!= NULL
) {
519 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
520 (void) no_memory(zhp
->zfs_hdl
);
525 if (zhp_orig
->zfs_user_props
!= NULL
) {
526 if (nvlist_dup(zhp_orig
->zfs_user_props
,
527 &zhp
->zfs_user_props
, 0) != 0) {
528 (void) no_memory(zhp
->zfs_hdl
);
533 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
534 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
535 &zhp
->zfs_recvd_props
, 0)) {
536 (void) no_memory(zhp
->zfs_hdl
);
541 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
542 if (zhp_orig
->zfs_mntopts
!= NULL
) {
543 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
544 zhp_orig
->zfs_mntopts
);
546 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
551 zfs_bookmark_exists(const char *path
)
555 char fsname
[ZFS_MAXNAMELEN
];
562 (void) strlcpy(fsname
, path
, sizeof (fsname
));
563 pound
= strchr(fsname
, '#');
568 bmark_name
= pound
+ 1;
569 props
= fnvlist_alloc();
570 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
577 rv
= nvlist_exists(bmarks
, bmark_name
);
583 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
584 nvlist_t
*bmark_props
)
586 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
591 /* Fill in the name. */
592 zhp
->zfs_hdl
= parent
->zfs_hdl
;
593 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
595 /* Set the property lists. */
596 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
602 zhp
->zfs_head_type
= parent
->zfs_head_type
;
603 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
605 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
606 nvlist_free(zhp
->zfs_props
);
615 * Opens the given snapshot, filesystem, or volume. The 'types'
616 * argument is a mask of acceptable types. The function will print an
617 * appropriate error message and return NULL if it can't be opened.
620 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
625 (void) snprintf(errbuf
, sizeof (errbuf
),
626 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
629 * Validate the name before we even try to open it.
631 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
633 "invalid dataset name"));
634 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
639 * Try to get stats for the dataset, which will tell us if it exists.
642 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
643 (void) zfs_standard_error(hdl
, errno
, errbuf
);
647 if (!(types
& zhp
->zfs_type
)) {
648 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
657 * Release a ZFS handle. Nothing to do but free the associated memory.
660 zfs_close(zfs_handle_t
*zhp
)
662 if (zhp
->zfs_mntopts
)
663 free(zhp
->zfs_mntopts
);
664 nvlist_free(zhp
->zfs_props
);
665 nvlist_free(zhp
->zfs_user_props
);
666 nvlist_free(zhp
->zfs_recvd_props
);
670 typedef struct mnttab_node
{
671 struct mnttab mtn_mt
;
676 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
678 const mnttab_node_t
*mtn1
= arg1
;
679 const mnttab_node_t
*mtn2
= arg2
;
682 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
686 return (rv
> 0 ? 1 : -1);
690 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
692 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
693 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
694 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
698 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
702 /* Reopen MNTTAB to prevent reading stale data from open file */
703 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
706 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
710 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
713 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
714 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
715 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
716 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
717 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
719 /* Exclude duplicate mounts */
720 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
721 free(mtn
->mtn_mt
.mnt_special
);
722 free(mtn
->mtn_mt
.mnt_mountp
);
723 free(mtn
->mtn_mt
.mnt_fstype
);
724 free(mtn
->mtn_mt
.mnt_mntopts
);
729 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
736 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
741 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
742 free(mtn
->mtn_mt
.mnt_special
);
743 free(mtn
->mtn_mt
.mnt_mountp
);
744 free(mtn
->mtn_mt
.mnt_fstype
);
745 free(mtn
->mtn_mt
.mnt_mntopts
);
748 avl_destroy(&hdl
->libzfs_mnttab_cache
);
752 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
754 hdl
->libzfs_mnttab_enable
= enable
;
758 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
759 struct mnttab
*entry
)
765 if (!hdl
->libzfs_mnttab_enable
) {
766 struct mnttab srch
= { 0 };
768 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
769 libzfs_mnttab_fini(hdl
);
771 /* Reopen MNTTAB to prevent reading stale data from open file */
772 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
775 srch
.mnt_special
= (char *)fsname
;
776 srch
.mnt_fstype
= MNTTYPE_ZFS
;
777 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
783 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
784 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
787 find
.mtn_mt
.mnt_special
= (char *)fsname
;
788 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
790 *entry
= mtn
->mtn_mt
;
797 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
798 const char *mountp
, const char *mntopts
)
802 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
804 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
805 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
806 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
807 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
808 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
809 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
813 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
818 find
.mtn_mt
.mnt_special
= (char *)fsname
;
819 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
820 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
821 free(ret
->mtn_mt
.mnt_special
);
822 free(ret
->mtn_mt
.mnt_mountp
);
823 free(ret
->mtn_mt
.mnt_fstype
);
824 free(ret
->mtn_mt
.mnt_mntopts
);
830 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
832 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
834 if (zpool_handle
== NULL
)
837 *spa_version
= zpool_get_prop_int(zpool_handle
,
838 ZPOOL_PROP_VERSION
, NULL
);
843 * The choice of reservation property depends on the SPA version.
846 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
850 if (zfs_spa_version(zhp
, &spa_version
) < 0)
853 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
854 *resv_prop
= ZFS_PROP_REFRESERVATION
;
856 *resv_prop
= ZFS_PROP_RESERVATION
;
862 * Given an nvlist of properties to set, validates that they are correct, and
863 * parses any numeric properties (index, boolean, etc) if they are specified as
867 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
868 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
875 int chosen_normal
= -1;
878 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
879 (void) no_memory(hdl
);
884 * Make sure this property is valid and applies to this type.
888 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
889 const char *propname
= nvpair_name(elem
);
891 prop
= zfs_name_to_prop(propname
);
892 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
894 * This is a user property: make sure it's a
895 * string, and that it's less than ZAP_MAXNAMELEN.
897 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
898 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
899 "'%s' must be a string"), propname
);
900 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
904 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
905 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
906 "property name '%s' is too long"),
908 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
912 (void) nvpair_value_string(elem
, &strval
);
913 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
914 (void) no_memory(hdl
);
921 * Currently, only user properties can be modified on
924 if (type
== ZFS_TYPE_SNAPSHOT
) {
925 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
926 "this property can not be modified for snapshots"));
927 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
931 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
932 zfs_userquota_prop_t uqtype
;
933 char newpropname
[128];
938 if (userquota_propname_decode(propname
, zoned
,
939 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
941 dgettext(TEXT_DOMAIN
,
942 "'%s' has an invalid user/group name"),
944 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
948 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
949 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
951 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
953 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
958 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
959 (void) nvpair_value_string(elem
, &strval
);
960 if (strcmp(strval
, "none") == 0) {
962 } else if (zfs_nicestrtonum(hdl
,
963 strval
, &intval
) != 0) {
964 (void) zfs_error(hdl
,
965 EZFS_BADPROP
, errbuf
);
968 } else if (nvpair_type(elem
) ==
970 (void) nvpair_value_uint64(elem
, &intval
);
972 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
973 "use 'none' to disable "
974 "userquota/groupquota"));
978 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
979 "'%s' must be a number"), propname
);
980 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
985 * Encode the prop name as
986 * userquota@<hex-rid>-domain, to make it easy
987 * for the kernel to decode.
989 (void) snprintf(newpropname
, sizeof (newpropname
),
990 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
991 (longlong_t
)rid
, domain
);
995 if (nvlist_add_uint64_array(ret
, newpropname
,
997 (void) no_memory(hdl
);
1001 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1002 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1003 "'%s' is readonly"),
1005 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1009 if (prop
== ZPROP_INVAL
) {
1010 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1011 "invalid property '%s'"), propname
);
1012 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1016 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1018 dgettext(TEXT_DOMAIN
, "'%s' does not "
1019 "apply to datasets of this type"), propname
);
1020 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1024 if (zfs_prop_readonly(prop
) &&
1025 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
1027 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1029 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1033 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1034 &strval
, &intval
, errbuf
) != 0)
1038 * Perform some additional checks for specific properties.
1041 case ZFS_PROP_VERSION
:
1047 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1048 if (intval
< version
) {
1049 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1050 "Can not downgrade; already at version %u"),
1052 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1058 case ZFS_PROP_VOLBLOCKSIZE
:
1059 case ZFS_PROP_RECORDSIZE
:
1061 int maxbs
= SPA_MAXBLOCKSIZE
;
1065 maxbs
= zpool_get_prop_int(zhp
->zpool_hdl
,
1066 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1069 * The value must be a power of two between
1070 * SPA_MINBLOCKSIZE and maxbs.
1072 if (intval
< SPA_MINBLOCKSIZE
||
1073 intval
> maxbs
|| !ISP2(intval
)) {
1074 zfs_nicenum(maxbs
, buf
, sizeof (buf
));
1075 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1076 "'%s' must be power of 2 from 512B "
1077 "to %s"), propname
, buf
);
1078 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1083 case ZFS_PROP_MLSLABEL
:
1085 #ifdef HAVE_MLSLABEL
1087 * Verify the mlslabel string and convert to
1088 * internal hex label string.
1092 char *hex
= NULL
; /* internal label string */
1094 /* Default value is already OK. */
1095 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1098 /* Verify the label can be converted to binary form */
1099 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1100 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1101 L_NO_CORRECTION
, NULL
) == -1)) {
1105 /* Now translate to hex internal label string */
1106 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1112 m_label_free(new_sl
);
1114 /* If string is already in internal form, we're done. */
1115 if (strcmp(strval
, hex
) == 0) {
1120 /* Replace the label string with the internal form. */
1121 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1123 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1130 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1131 "invalid mlslabel '%s'"), strval
);
1132 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1133 m_label_free(new_sl
); /* OK if null */
1136 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1137 "mlslabels are unsupported"));
1138 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1140 #endif /* HAVE_MLSLABEL */
1143 case ZFS_PROP_MOUNTPOINT
:
1145 namecheck_err_t why
;
1147 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1148 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1151 if (mountpoint_namecheck(strval
, &why
)) {
1153 case NAME_ERR_LEADING_SLASH
:
1155 dgettext(TEXT_DOMAIN
,
1156 "'%s' must be an absolute path, "
1157 "'none', or 'legacy'"), propname
);
1159 case NAME_ERR_TOOLONG
:
1161 dgettext(TEXT_DOMAIN
,
1162 "component of '%s' is too long"),
1168 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1175 case ZFS_PROP_SHARESMB
:
1176 case ZFS_PROP_SHARENFS
:
1178 * For the mountpoint and sharenfs or sharesmb
1179 * properties, check if it can be set in a
1180 * global/non-global zone based on
1181 * the zoned property value:
1183 * global zone non-global zone
1184 * --------------------------------------------------
1185 * zoned=on mountpoint (no) mountpoint (yes)
1186 * sharenfs (no) sharenfs (no)
1187 * sharesmb (no) sharesmb (no)
1189 * zoned=off mountpoint (yes) N/A
1194 if (getzoneid() == GLOBAL_ZONEID
) {
1195 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1196 "'%s' cannot be set on "
1197 "dataset in a non-global zone"),
1199 (void) zfs_error(hdl
, EZFS_ZONED
,
1202 } else if (prop
== ZFS_PROP_SHARENFS
||
1203 prop
== ZFS_PROP_SHARESMB
) {
1204 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1205 "'%s' cannot be set in "
1206 "a non-global zone"), propname
);
1207 (void) zfs_error(hdl
, EZFS_ZONED
,
1211 } else if (getzoneid() != GLOBAL_ZONEID
) {
1213 * If zoned property is 'off', this must be in
1214 * a global zone. If not, something is wrong.
1216 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1217 "'%s' cannot be set while dataset "
1218 "'zoned' property is set"), propname
);
1219 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1224 * At this point, it is legitimate to set the
1225 * property. Now we want to make sure that the
1226 * property value is valid if it is sharenfs.
1228 if ((prop
== ZFS_PROP_SHARENFS
||
1229 prop
== ZFS_PROP_SHARESMB
) &&
1230 strcmp(strval
, "on") != 0 &&
1231 strcmp(strval
, "off") != 0) {
1232 zfs_share_proto_t proto
;
1234 if (prop
== ZFS_PROP_SHARESMB
)
1240 * Must be an valid sharing protocol
1241 * option string so init the libshare
1242 * in order to enable the parser and
1243 * then parse the options. We use the
1244 * control API since we don't care about
1245 * the current configuration and don't
1246 * want the overhead of loading it
1247 * until we actually do something.
1250 if (zfs_init_libshare(hdl
,
1251 SA_INIT_CONTROL_API
) != SA_OK
) {
1253 * An error occurred so we can't do
1256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1257 "'%s' cannot be set: problem "
1258 "in share initialization"),
1260 (void) zfs_error(hdl
, EZFS_BADPROP
,
1265 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1267 * There was an error in parsing so
1268 * deal with it by issuing an error
1269 * message and leaving after
1270 * uninitializing the the libshare
1273 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1274 "'%s' cannot be set to invalid "
1275 "options"), propname
);
1276 (void) zfs_error(hdl
, EZFS_BADPROP
,
1278 zfs_uninit_libshare(hdl
);
1281 zfs_uninit_libshare(hdl
);
1285 case ZFS_PROP_UTF8ONLY
:
1286 chosen_utf
= (int)intval
;
1288 case ZFS_PROP_NORMALIZE
:
1289 chosen_normal
= (int)intval
;
1296 * For changes to existing volumes, we have some additional
1297 * checks to enforce.
1299 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1300 uint64_t volsize
= zfs_prop_get_int(zhp
,
1302 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1303 ZFS_PROP_VOLBLOCKSIZE
);
1307 case ZFS_PROP_RESERVATION
:
1308 case ZFS_PROP_REFRESERVATION
:
1309 if (intval
> volsize
) {
1310 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1311 "'%s' is greater than current "
1312 "volume size"), propname
);
1313 (void) zfs_error(hdl
, EZFS_BADPROP
,
1319 case ZFS_PROP_VOLSIZE
:
1320 if (intval
% blocksize
!= 0) {
1321 zfs_nicenum(blocksize
, buf
,
1323 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1324 "'%s' must be a multiple of "
1325 "volume block size (%s)"),
1327 (void) zfs_error(hdl
, EZFS_BADPROP
,
1333 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1334 "'%s' cannot be zero"),
1336 (void) zfs_error(hdl
, EZFS_BADPROP
,
1348 * If normalization was chosen, but no UTF8 choice was made,
1349 * enforce rejection of non-UTF8 names.
1351 * If normalization was chosen, but rejecting non-UTF8 names
1352 * was explicitly not chosen, it is an error.
1354 if (chosen_normal
> 0 && chosen_utf
< 0) {
1355 if (nvlist_add_uint64(ret
,
1356 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1357 (void) no_memory(hdl
);
1360 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1361 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1362 "'%s' must be set 'on' if normalization chosen"),
1363 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1364 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1375 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1377 uint64_t old_volsize
;
1378 uint64_t new_volsize
;
1379 uint64_t old_reservation
;
1380 uint64_t new_reservation
;
1381 zfs_prop_t resv_prop
;
1384 * If this is an existing volume, and someone is setting the volsize,
1385 * make sure that it matches the reservation, or add it if necessary.
1387 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1388 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1390 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1391 if ((zvol_volsize_to_reservation(old_volsize
, zhp
->zfs_props
) !=
1392 old_reservation
) || nvlist_lookup_uint64(nvl
,
1393 zfs_prop_to_name(resv_prop
), &new_reservation
) != ENOENT
) {
1396 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1399 new_reservation
= zvol_volsize_to_reservation(new_volsize
,
1401 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1402 new_reservation
) != 0) {
1403 (void) no_memory(zhp
->zfs_hdl
);
1410 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1417 * For quotas and reservations, ENOSPC indicates
1418 * something different; setting a quota or reservation
1419 * doesn't use any disk space.
1422 case ZFS_PROP_QUOTA
:
1423 case ZFS_PROP_REFQUOTA
:
1424 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1425 "size is less than current used or "
1427 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1430 case ZFS_PROP_RESERVATION
:
1431 case ZFS_PROP_REFRESERVATION
:
1432 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1433 "size is greater than available space"));
1434 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1438 (void) zfs_standard_error(hdl
, err
, errbuf
);
1444 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1448 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1452 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1453 "property value too long"));
1454 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1458 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1459 "pool and or dataset must be upgraded to set this "
1460 "property or value"));
1461 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1465 if (prop
== ZFS_PROP_COMPRESSION
||
1466 prop
== ZFS_PROP_RECORDSIZE
) {
1467 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1468 "property setting is not allowed on "
1469 "bootable datasets"));
1470 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1472 (void) zfs_standard_error(hdl
, err
, errbuf
);
1477 if (prop
== ZPROP_INVAL
) {
1478 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1480 (void) zfs_standard_error(hdl
, err
, errbuf
);
1486 * This platform can't address a volume this big.
1489 if (prop
== ZFS_PROP_VOLSIZE
) {
1490 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1496 (void) zfs_standard_error(hdl
, err
, errbuf
);
1501 zfs_is_namespace_prop(zfs_prop_t prop
)
1505 case ZFS_PROP_ATIME
:
1506 case ZFS_PROP_RELATIME
:
1507 case ZFS_PROP_DEVICES
:
1509 case ZFS_PROP_SETUID
:
1510 case ZFS_PROP_READONLY
:
1511 case ZFS_PROP_XATTR
:
1512 case ZFS_PROP_NBMAND
:
1521 * Given a property name and value, set the property for the given dataset.
1524 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1526 zfs_cmd_t zc
= {"\0"};
1528 prop_changelist_t
*cl
= NULL
;
1530 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1531 nvlist_t
*nvl
= NULL
, *realprops
;
1533 boolean_t do_prefix
= B_TRUE
;
1536 (void) snprintf(errbuf
, sizeof (errbuf
),
1537 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1540 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1541 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1542 (void) no_memory(hdl
);
1546 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1547 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1553 prop
= zfs_name_to_prop(propname
);
1555 if (prop
== ZFS_PROP_VOLSIZE
) {
1556 if ((added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1)
1560 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1563 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1565 "child dataset with inherited mountpoint is used "
1566 "in a non-global zone"));
1567 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1572 * We don't want to unmount & remount the dataset when changing
1573 * its canmount property to 'on' or 'noauto'. We only use
1574 * the changelist logic to unmount when setting canmount=off.
1576 if (prop
== ZFS_PROP_CANMOUNT
) {
1578 int err
= zprop_string_to_index(prop
, propval
, &idx
,
1580 if (err
== 0 && idx
!= ZFS_CANMOUNT_OFF
)
1581 do_prefix
= B_FALSE
;
1584 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1588 * Execute the corresponding ioctl() to set this property.
1590 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1592 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1595 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1598 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1599 if (added_resv
&& errno
== ENOSPC
) {
1600 /* clean up the volsize property we tried to set */
1601 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1604 zcmd_free_nvlists(&zc
);
1605 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1607 if (nvlist_add_uint64(nvl
,
1608 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1611 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1613 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1617 ret
= changelist_postfix(cl
);
1621 * Refresh the statistics so the new property
1622 * value is reflected.
1624 (void) get_stats(zhp
);
1627 * Remount the filesystem to propagate the change
1628 * if one of the options handled by the generic
1629 * Linux namespace layer has been modified.
1631 if (zfs_is_namespace_prop(prop
) &&
1632 zfs_is_mounted(zhp
, NULL
))
1633 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1639 zcmd_free_nvlists(&zc
);
1641 changelist_free(cl
);
1646 * Given a property, inherit the value from the parent dataset, or if received
1647 * is TRUE, revert to the received value, if any.
1650 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1652 zfs_cmd_t zc
= {"\0"};
1654 prop_changelist_t
*cl
;
1655 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1659 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1660 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1662 zc
.zc_cookie
= received
;
1663 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1665 * For user properties, the amount of work we have to do is very
1666 * small, so just do it here.
1668 if (!zfs_prop_user(propname
)) {
1669 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1670 "invalid property"));
1671 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1674 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1675 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1677 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1678 return (zfs_standard_error(hdl
, errno
, errbuf
));
1684 * Verify that this property is inheritable.
1686 if (zfs_prop_readonly(prop
))
1687 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1689 if (!zfs_prop_inheritable(prop
) && !received
)
1690 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1693 * Check to see if the value applies to this type
1695 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1696 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1699 * Normalize the name, to get rid of shorthand abbreviations.
1701 propname
= zfs_prop_to_name(prop
);
1702 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1703 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1705 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1706 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1707 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1708 "dataset is used in a non-global zone"));
1709 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1713 * Determine datasets which will be affected by this change, if any.
1715 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1718 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1719 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1720 "child dataset with inherited mountpoint is used "
1721 "in a non-global zone"));
1722 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1726 if ((ret
= changelist_prefix(cl
)) != 0)
1729 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1730 return (zfs_standard_error(hdl
, errno
, errbuf
));
1733 if ((ret
= changelist_postfix(cl
)) != 0)
1737 * Refresh the statistics so the new property is reflected.
1739 (void) get_stats(zhp
);
1742 * Remount the filesystem to propagate the change
1743 * if one of the options handled by the generic
1744 * Linux namespace layer has been modified.
1746 if (zfs_is_namespace_prop(prop
) &&
1747 zfs_is_mounted(zhp
, NULL
))
1748 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1752 changelist_free(cl
);
1757 * True DSL properties are stored in an nvlist. The following two functions
1758 * extract them appropriately.
1761 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1767 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1768 zfs_prop_to_name(prop
), &nv
) == 0) {
1769 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1770 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1772 verify(!zhp
->zfs_props_table
||
1773 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1774 value
= zfs_prop_default_numeric(prop
);
1782 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1788 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1789 zfs_prop_to_name(prop
), &nv
) == 0) {
1790 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1791 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1793 verify(!zhp
->zfs_props_table
||
1794 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1795 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1804 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1806 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1810 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1812 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1813 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1817 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1819 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1824 * Internal function for getting a numeric property. Both zfs_prop_get() and
1825 * zfs_prop_get_int() are built using this interface.
1827 * Certain properties can be overridden using 'mount -o'. In this case, scan
1828 * the contents of the /etc/mtab entry, searching for the appropriate options.
1829 * If they differ from the on-disk values, report the current values and mark
1830 * the source "temporary".
1833 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1834 char **source
, uint64_t *val
)
1836 zfs_cmd_t zc
= {"\0"};
1837 nvlist_t
*zplprops
= NULL
;
1839 char *mntopt_on
= NULL
;
1840 char *mntopt_off
= NULL
;
1841 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1846 * If the property is being fetched for a snapshot, check whether
1847 * the property is valid for the snapshot's head dataset type.
1849 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
1850 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
1851 *val
= zfs_prop_default_numeric(prop
);
1856 case ZFS_PROP_ATIME
:
1857 mntopt_on
= MNTOPT_ATIME
;
1858 mntopt_off
= MNTOPT_NOATIME
;
1861 case ZFS_PROP_RELATIME
:
1862 mntopt_on
= MNTOPT_RELATIME
;
1863 mntopt_off
= MNTOPT_NORELATIME
;
1866 case ZFS_PROP_DEVICES
:
1867 mntopt_on
= MNTOPT_DEVICES
;
1868 mntopt_off
= MNTOPT_NODEVICES
;
1872 mntopt_on
= MNTOPT_EXEC
;
1873 mntopt_off
= MNTOPT_NOEXEC
;
1876 case ZFS_PROP_READONLY
:
1877 mntopt_on
= MNTOPT_RO
;
1878 mntopt_off
= MNTOPT_RW
;
1881 case ZFS_PROP_SETUID
:
1882 mntopt_on
= MNTOPT_SETUID
;
1883 mntopt_off
= MNTOPT_NOSETUID
;
1886 case ZFS_PROP_XATTR
:
1887 mntopt_on
= MNTOPT_XATTR
;
1888 mntopt_off
= MNTOPT_NOXATTR
;
1891 case ZFS_PROP_NBMAND
:
1892 mntopt_on
= MNTOPT_NBMAND
;
1893 mntopt_off
= MNTOPT_NONBMAND
;
1900 * Because looking up the mount options is potentially expensive
1901 * (iterating over all of /etc/mtab), we defer its calculation until
1902 * we're looking up a property which requires its presence.
1904 if (!zhp
->zfs_mntcheck
&&
1905 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1906 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1907 struct mnttab entry
;
1909 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1910 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1912 if (zhp
->zfs_mntopts
== NULL
)
1916 zhp
->zfs_mntcheck
= B_TRUE
;
1919 if (zhp
->zfs_mntopts
== NULL
)
1920 mnt
.mnt_mntopts
= "";
1922 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
1925 case ZFS_PROP_ATIME
:
1926 case ZFS_PROP_RELATIME
:
1927 case ZFS_PROP_DEVICES
:
1929 case ZFS_PROP_READONLY
:
1930 case ZFS_PROP_SETUID
:
1931 case ZFS_PROP_XATTR
:
1932 case ZFS_PROP_NBMAND
:
1933 *val
= getprop_uint64(zhp
, prop
, source
);
1938 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
1941 *src
= ZPROP_SRC_TEMPORARY
;
1942 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
1945 *src
= ZPROP_SRC_TEMPORARY
;
1949 case ZFS_PROP_CANMOUNT
:
1950 case ZFS_PROP_VOLSIZE
:
1951 case ZFS_PROP_QUOTA
:
1952 case ZFS_PROP_REFQUOTA
:
1953 case ZFS_PROP_RESERVATION
:
1954 case ZFS_PROP_REFRESERVATION
:
1955 case ZFS_PROP_FILESYSTEM_LIMIT
:
1956 case ZFS_PROP_SNAPSHOT_LIMIT
:
1957 case ZFS_PROP_FILESYSTEM_COUNT
:
1958 case ZFS_PROP_SNAPSHOT_COUNT
:
1959 *val
= getprop_uint64(zhp
, prop
, source
);
1961 if (*source
== NULL
) {
1962 /* not default, must be local */
1963 *source
= zhp
->zfs_name
;
1967 case ZFS_PROP_MOUNTED
:
1968 *val
= (zhp
->zfs_mntopts
!= NULL
);
1971 case ZFS_PROP_NUMCLONES
:
1972 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
1975 case ZFS_PROP_VERSION
:
1976 case ZFS_PROP_NORMALIZE
:
1977 case ZFS_PROP_UTF8ONLY
:
1979 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1981 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1982 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
1983 zcmd_free_nvlists(&zc
);
1984 if (prop
== ZFS_PROP_VERSION
&&
1985 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
1986 *val
= zfs_prop_default_numeric(prop
);
1989 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
1990 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
1992 zcmd_free_nvlists(&zc
);
1996 nvlist_free(zplprops
);
1997 zcmd_free_nvlists(&zc
);
2000 case ZFS_PROP_INCONSISTENT
:
2001 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2005 switch (zfs_prop_get_type(prop
)) {
2006 case PROP_TYPE_NUMBER
:
2007 case PROP_TYPE_INDEX
:
2008 *val
= getprop_uint64(zhp
, prop
, source
);
2010 * If we tried to use a default value for a
2011 * readonly property, it means that it was not
2014 if (zfs_prop_readonly(prop
) &&
2015 *source
!= NULL
&& (*source
)[0] == '\0') {
2020 case PROP_TYPE_STRING
:
2022 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2023 "cannot get non-numeric property"));
2024 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2025 dgettext(TEXT_DOMAIN
, "internal error")));
2033 * Calculate the source type, given the raw source string.
2036 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2037 char *statbuf
, size_t statlen
)
2039 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2042 if (source
== NULL
) {
2043 *srctype
= ZPROP_SRC_NONE
;
2044 } else if (source
[0] == '\0') {
2045 *srctype
= ZPROP_SRC_DEFAULT
;
2046 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2047 *srctype
= ZPROP_SRC_RECEIVED
;
2049 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2050 *srctype
= ZPROP_SRC_LOCAL
;
2052 (void) strlcpy(statbuf
, source
, statlen
);
2053 *srctype
= ZPROP_SRC_INHERITED
;
2060 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2061 size_t proplen
, boolean_t literal
)
2066 if (zhp
->zfs_recvd_props
== NULL
)
2067 if (get_recvd_props_ioctl(zhp
) != 0)
2070 prop
= zfs_name_to_prop(propname
);
2072 if (prop
!= ZPROP_INVAL
) {
2074 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2076 zfs_set_recvd_props_mode(zhp
, &cookie
);
2077 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2078 NULL
, NULL
, 0, literal
);
2079 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2083 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2084 propname
, &propval
) != 0)
2086 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2088 (void) strlcpy(propbuf
, recvdval
, proplen
);
2091 return (err
== 0 ? 0 : -1);
2095 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2100 value
= zfs_get_clones_nvl(zhp
);
2105 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2106 pair
= nvlist_next_nvpair(value
, pair
)) {
2107 if (propbuf
[0] != '\0')
2108 (void) strlcat(propbuf
, ",", proplen
);
2109 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2115 struct get_clones_arg
{
2119 char buf
[ZFS_MAXNAMELEN
];
2123 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2125 struct get_clones_arg
*gca
= arg
;
2127 if (gca
->numclones
== 0) {
2132 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2133 NULL
, NULL
, 0, B_TRUE
) != 0)
2135 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2136 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2141 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2147 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2149 nvlist_t
*nv
, *value
;
2151 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2152 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2153 struct get_clones_arg gca
;
2156 * if this is a snapshot, then the kernel wasn't able
2157 * to get the clones. Do it by slowly iterating.
2159 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2161 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2163 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2168 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2170 gca
.origin
= zhp
->zfs_name
;
2172 if (gca
.numclones
!= 0) {
2174 char pool
[ZFS_MAXNAMELEN
];
2177 /* get the pool name */
2178 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2179 (void) strsep(&cp
, "/@");
2180 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2181 ZFS_TYPE_FILESYSTEM
);
2183 (void) get_clones_cb(root
, &gca
);
2186 if (gca
.numclones
!= 0 ||
2187 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2188 nvlist_add_nvlist(zhp
->zfs_props
,
2189 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2196 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2197 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2200 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2206 * Retrieve a property from the given object. If 'literal' is specified, then
2207 * numbers are left as exact values. Otherwise, numbers are converted to a
2208 * human-readable form.
2210 * Returns 0 on success, or -1 on error.
2213 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2214 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2216 char *source
= NULL
;
2220 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2223 * Check to see if this property applies to our object
2225 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2228 if (received
&& zfs_prop_readonly(prop
))
2232 *src
= ZPROP_SRC_NONE
;
2235 case ZFS_PROP_CREATION
:
2237 * 'creation' is a time_t stored in the statistics. We convert
2238 * this into a string unless 'literal' is specified.
2241 val
= getprop_uint64(zhp
, prop
, &source
);
2242 time_t time
= (time_t)val
;
2246 localtime_r(&time
, &t
) == NULL
||
2247 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2249 (void) snprintf(propbuf
, proplen
, "%llu",
2250 (u_longlong_t
) val
);
2254 case ZFS_PROP_MOUNTPOINT
:
2256 * Getting the precise mountpoint can be tricky.
2258 * - for 'none' or 'legacy', return those values.
2259 * - for inherited mountpoints, we want to take everything
2260 * after our ancestor and append it to the inherited value.
2262 * If the pool has an alternate root, we want to prepend that
2263 * root to any values we return.
2266 str
= getprop_string(zhp
, prop
, &source
);
2268 if (str
[0] == '/') {
2269 char buf
[MAXPATHLEN
];
2271 const char *relpath
;
2274 * If we inherit the mountpoint, even from a dataset
2275 * with a received value, the source will be the path of
2276 * the dataset we inherit from. If source is
2277 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2280 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2283 relpath
= zhp
->zfs_name
+ strlen(source
);
2284 if (relpath
[0] == '/')
2288 if ((zpool_get_prop(zhp
->zpool_hdl
,
2289 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
2290 (strcmp(root
, "-") == 0))
2293 * Special case an alternate root of '/'. This will
2294 * avoid having multiple leading slashes in the
2297 if (strcmp(root
, "/") == 0)
2301 * If the mountpoint is '/' then skip over this
2302 * if we are obtaining either an alternate root or
2303 * an inherited mountpoint.
2305 if (str
[1] == '\0' && (root
[0] != '\0' ||
2306 relpath
[0] != '\0'))
2309 if (relpath
[0] == '\0')
2310 (void) snprintf(propbuf
, proplen
, "%s%s",
2313 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2314 root
, str
, relpath
[0] == '@' ? "" : "/",
2317 /* 'legacy' or 'none' */
2318 (void) strlcpy(propbuf
, str
, proplen
);
2323 case ZFS_PROP_ORIGIN
:
2324 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2327 * If there is no parent at all, return failure to indicate that
2328 * it doesn't apply to this dataset.
2330 if (propbuf
[0] == '\0')
2334 case ZFS_PROP_CLONES
:
2335 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2339 case ZFS_PROP_QUOTA
:
2340 case ZFS_PROP_REFQUOTA
:
2341 case ZFS_PROP_RESERVATION
:
2342 case ZFS_PROP_REFRESERVATION
:
2344 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2348 * If quota or reservation is 0, we translate this into 'none'
2349 * (unless literal is set), and indicate that it's the default
2350 * value. Otherwise, we print the number nicely and indicate
2351 * that its set locally.
2355 (void) strlcpy(propbuf
, "0", proplen
);
2357 (void) strlcpy(propbuf
, "none", proplen
);
2360 (void) snprintf(propbuf
, proplen
, "%llu",
2363 zfs_nicenum(val
, propbuf
, proplen
);
2367 case ZFS_PROP_FILESYSTEM_LIMIT
:
2368 case ZFS_PROP_SNAPSHOT_LIMIT
:
2369 case ZFS_PROP_FILESYSTEM_COUNT
:
2370 case ZFS_PROP_SNAPSHOT_COUNT
:
2372 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2376 * If limit is UINT64_MAX, we translate this into 'none' (unless
2377 * literal is set), and indicate that it's the default value.
2378 * Otherwise, we print the number nicely and indicate that it's
2382 (void) snprintf(propbuf
, proplen
, "%llu",
2384 } else if (val
== UINT64_MAX
) {
2385 (void) strlcpy(propbuf
, "none", proplen
);
2387 zfs_nicenum(val
, propbuf
, proplen
);
2391 case ZFS_PROP_REFRATIO
:
2392 case ZFS_PROP_COMPRESSRATIO
:
2393 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2395 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2396 (u_longlong_t
)(val
/ 100),
2397 (u_longlong_t
)(val
% 100));
2401 switch (zhp
->zfs_type
) {
2402 case ZFS_TYPE_FILESYSTEM
:
2405 case ZFS_TYPE_VOLUME
:
2408 case ZFS_TYPE_SNAPSHOT
:
2411 case ZFS_TYPE_BOOKMARK
:
2417 (void) snprintf(propbuf
, proplen
, "%s", str
);
2420 case ZFS_PROP_MOUNTED
:
2422 * The 'mounted' property is a pseudo-property that described
2423 * whether the filesystem is currently mounted. Even though
2424 * it's a boolean value, the typical values of "on" and "off"
2425 * don't make sense, so we translate to "yes" and "no".
2427 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2428 src
, &source
, &val
) != 0)
2431 (void) strlcpy(propbuf
, "yes", proplen
);
2433 (void) strlcpy(propbuf
, "no", proplen
);
2438 * The 'name' property is a pseudo-property derived from the
2439 * dataset name. It is presented as a real property to simplify
2442 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2445 case ZFS_PROP_MLSLABEL
:
2447 #ifdef HAVE_MLSLABEL
2448 m_label_t
*new_sl
= NULL
;
2449 char *ascii
= NULL
; /* human readable label */
2451 (void) strlcpy(propbuf
,
2452 getprop_string(zhp
, prop
, &source
), proplen
);
2454 if (literal
|| (strcasecmp(propbuf
,
2455 ZFS_MLSLABEL_DEFAULT
) == 0))
2459 * Try to translate the internal hex string to
2460 * human-readable output. If there are any
2461 * problems just use the hex string.
2464 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2465 L_NO_CORRECTION
, NULL
) == -1) {
2466 m_label_free(new_sl
);
2470 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2474 m_label_free(new_sl
);
2477 m_label_free(new_sl
);
2479 (void) strlcpy(propbuf
, ascii
, proplen
);
2482 (void) strlcpy(propbuf
,
2483 getprop_string(zhp
, prop
, &source
), proplen
);
2484 #endif /* HAVE_MLSLABEL */
2490 * GUIDs are stored as numbers, but they are identifiers.
2491 * We don't want them to be pretty printed, because pretty
2492 * printing mangles the ID into a truncated and useless value.
2494 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2496 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2500 switch (zfs_prop_get_type(prop
)) {
2501 case PROP_TYPE_NUMBER
:
2502 if (get_numeric_property(zhp
, prop
, src
,
2503 &source
, &val
) != 0)
2506 (void) snprintf(propbuf
, proplen
, "%llu",
2509 zfs_nicenum(val
, propbuf
, proplen
);
2512 case PROP_TYPE_STRING
:
2513 (void) strlcpy(propbuf
,
2514 getprop_string(zhp
, prop
, &source
), proplen
);
2517 case PROP_TYPE_INDEX
:
2518 if (get_numeric_property(zhp
, prop
, src
,
2519 &source
, &val
) != 0)
2521 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2523 (void) strlcpy(propbuf
, strval
, proplen
);
2531 get_source(zhp
, src
, source
, statbuf
, statlen
);
2537 * Utility function to get the given numeric property. Does no validation that
2538 * the given property is the appropriate type; should only be used with
2539 * hard-coded property types.
2542 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2547 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2553 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2557 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2558 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2562 * Similar to zfs_prop_get(), but returns the value as an integer.
2565 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2566 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2571 * Check to see if this property applies to our object
2573 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
2574 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2575 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2576 zfs_prop_to_name(prop
)));
2580 *src
= ZPROP_SRC_NONE
;
2582 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2585 get_source(zhp
, src
, source
, statbuf
, statlen
);
2592 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2593 char **domainp
, idmap_rid_t
*ridp
)
2595 idmap_get_handle_t
*get_hdl
= NULL
;
2599 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2603 err
= idmap_get_sidbyuid(get_hdl
, id
,
2604 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2606 err
= idmap_get_sidbygid(get_hdl
, id
,
2607 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2609 if (err
== IDMAP_SUCCESS
&&
2610 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2611 status
== IDMAP_SUCCESS
)
2617 idmap_get_destroy(get_hdl
);
2620 #endif /* HAVE_IDMAP */
2623 * convert the propname into parameters needed by kernel
2624 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2625 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2626 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2627 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2630 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2631 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2633 zfs_userquota_prop_t type
;
2642 /* Figure out the property type ({user|group}{quota|space}) */
2643 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2644 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2645 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2648 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2652 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
);
2653 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
);
2655 cp
= strchr(propname
, '@') + 1;
2657 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2658 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2661 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2662 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2665 } else if (strchr(cp
, '@')) {
2668 * It's a SID name (eg "user@domain") that needs to be
2669 * turned into S-1-domainID-RID.
2671 directory_error_t e
;
2672 char *numericsid
= NULL
;
2675 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2678 e
= directory_sid_from_user_name(NULL
,
2681 e
= directory_sid_from_group_name(NULL
,
2685 directory_error_free(e
);
2688 if (numericsid
== NULL
)
2691 (void) strlcpy(domain
, cp
, domainlen
);
2692 cp
= strrchr(domain
, '-');
2697 *ridp
= strtoull(cp
, &end
, 10);
2700 if (errno
!= 0 || *end
!= '\0')
2704 #endif /* HAVE_IDMAP */
2706 /* It's a user/group ID (eg "12345"). */
2709 id
= strtoul(cp
, &end
, 10);
2714 /* It's an ephemeral ID. */
2718 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2719 &mapdomain
, &rid
) != 0)
2721 (void) strlcpy(domain
, mapdomain
, domainlen
);
2725 #endif /* HAVE_IDMAP */
2735 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2736 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2739 zfs_cmd_t zc
= {"\0"};
2741 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2743 err
= userquota_propname_decode(propname
,
2744 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2745 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2746 zc
.zc_objset_type
= *typep
;
2750 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2754 *propvalue
= zc
.zc_cookie
;
2759 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2760 uint64_t *propvalue
)
2762 zfs_userquota_prop_t type
;
2764 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2769 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2770 char *propbuf
, int proplen
, boolean_t literal
)
2774 zfs_userquota_prop_t type
;
2776 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2783 (void) snprintf(propbuf
, proplen
, "%llu",
2784 (u_longlong_t
)propvalue
);
2785 } else if (propvalue
== 0 &&
2786 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2787 (void) strlcpy(propbuf
, "none", proplen
);
2789 zfs_nicenum(propvalue
, propbuf
, proplen
);
2795 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2796 uint64_t *propvalue
)
2799 zfs_cmd_t zc
= {"\0"};
2800 const char *snapname
;
2802 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2804 snapname
= strchr(propname
, '@') + 1;
2805 if (strchr(snapname
, '@')) {
2806 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2808 /* snapname is the short name, append it to zhp's fsname */
2811 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2812 sizeof (zc
.zc_value
));
2813 cp
= strchr(zc
.zc_value
, '@');
2816 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2817 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2820 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2824 *propvalue
= zc
.zc_cookie
;
2829 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2830 char *propbuf
, int proplen
, boolean_t literal
)
2835 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2841 (void) snprintf(propbuf
, proplen
, "%llu",
2842 (u_longlong_t
)propvalue
);
2844 zfs_nicenum(propvalue
, propbuf
, proplen
);
2851 * Returns the name of the given zfs handle.
2854 zfs_get_name(const zfs_handle_t
*zhp
)
2856 return (zhp
->zfs_name
);
2860 * Returns the type of the given zfs handle.
2863 zfs_get_type(const zfs_handle_t
*zhp
)
2865 return (zhp
->zfs_type
);
2869 * Is one dataset name a child dataset of another?
2871 * Needs to handle these cases:
2872 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2873 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2874 * Descendant? No. No. No. Yes.
2877 is_descendant(const char *ds1
, const char *ds2
)
2879 size_t d1len
= strlen(ds1
);
2881 /* ds2 can't be a descendant if it's smaller */
2882 if (strlen(ds2
) < d1len
)
2885 /* otherwise, compare strings and verify that there's a '/' char */
2886 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2890 * Given a complete name, return just the portion that refers to the parent.
2891 * Will return -1 if there is no parent (path is just the name of the
2895 parent_name(const char *path
, char *buf
, size_t buflen
)
2899 (void) strlcpy(buf
, path
, buflen
);
2901 if ((slashp
= strrchr(buf
, '/')) == NULL
)
2909 * If accept_ancestor is false, then check to make sure that the given path has
2910 * a parent, and that it exists. If accept_ancestor is true, then find the
2911 * closest existing ancestor for the given path. In prefixlen return the
2912 * length of already existing prefix of the given path. We also fetch the
2913 * 'zoned' property, which is used to validate property settings when creating
2917 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2918 boolean_t accept_ancestor
, int *prefixlen
)
2920 zfs_cmd_t zc
= {"\0"};
2921 char parent
[ZFS_MAXNAMELEN
];
2927 (void) snprintf(errbuf
, sizeof (errbuf
),
2928 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2930 /* get parent, and check to see if this is just a pool */
2931 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2932 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2933 "missing dataset name"));
2934 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2937 /* check to see if the pool exists */
2938 if ((slash
= strchr(parent
, '/')) == NULL
)
2939 slash
= parent
+ strlen(parent
);
2940 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2941 zc
.zc_name
[slash
- parent
] = '\0';
2942 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2944 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2945 "no such pool '%s'"), zc
.zc_name
);
2946 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2949 /* check to see if the parent dataset exists */
2950 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2951 if (errno
== ENOENT
&& accept_ancestor
) {
2953 * Go deeper to find an ancestor, give up on top level.
2955 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2956 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2957 "no such pool '%s'"), zc
.zc_name
);
2958 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2960 } else if (errno
== ENOENT
) {
2961 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2962 "parent does not exist"));
2963 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2965 return (zfs_standard_error(hdl
, errno
, errbuf
));
2968 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2972 /* we are in a non-global zone, but parent is in the global zone */
2973 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2974 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2979 /* make sure parent is a filesystem */
2980 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2981 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2982 "parent is not a filesystem"));
2983 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2989 if (prefixlen
!= NULL
)
2990 *prefixlen
= strlen(parent
);
2995 * Finds whether the dataset of the given type(s) exists.
2998 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3002 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3006 * Try to get stats for the dataset, which will tell us if it exists.
3008 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3009 int ds_type
= zhp
->zfs_type
;
3012 if (types
& ds_type
)
3019 * Given a path to 'target', create all the ancestors between
3020 * the prefixlen portion of the path, and the target itself.
3021 * Fail if the initial prefixlen-ancestor does not already exist.
3024 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3030 /* make sure prefix exists */
3031 cp
= target
+ prefixlen
;
3033 assert(strchr(cp
, '/') == NULL
);
3034 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3037 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3045 * Attempt to create, mount, and share any ancestor filesystems,
3046 * up to the prefixlen-long one.
3048 for (cp
= target
+ prefixlen
+ 1;
3049 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
3053 h
= make_dataset_handle(hdl
, target
);
3055 /* it already exists, nothing to do here */
3060 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3062 opname
= dgettext(TEXT_DOMAIN
, "create");
3066 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3068 opname
= dgettext(TEXT_DOMAIN
, "open");
3072 if (zfs_mount(h
, NULL
, 0) != 0) {
3073 opname
= dgettext(TEXT_DOMAIN
, "mount");
3077 if (zfs_share(h
) != 0) {
3078 opname
= dgettext(TEXT_DOMAIN
, "share");
3088 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3089 "failed to %s ancestor '%s'"), opname
, target
);
3094 * Creates non-existing ancestors of the given path.
3097 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3103 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3106 if ((path_copy
= strdup(path
)) != NULL
) {
3107 rc
= create_parents(hdl
, path_copy
, prefix
);
3110 if (path_copy
== NULL
|| rc
!= 0)
3117 * Create a new filesystem or volume.
3120 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3125 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3128 dmu_objset_type_t ost
;
3130 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3131 "cannot create '%s'"), path
);
3133 /* validate the path, taking care to note the extended error message */
3134 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3135 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3137 /* validate parents exist */
3138 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3142 * The failure modes when creating a dataset of a different type over
3143 * one that already exists is a little strange. In particular, if you
3144 * try to create a dataset on top of an existing dataset, the ioctl()
3145 * will return ENOENT, not EEXIST. To prevent this from happening, we
3146 * first try to see if the dataset exists.
3148 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3150 "dataset already exists"));
3151 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3154 if (type
== ZFS_TYPE_VOLUME
)
3159 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3160 zoned
, NULL
, errbuf
)) == 0)
3163 if (type
== ZFS_TYPE_VOLUME
) {
3165 * If we are creating a volume, the size and block size must
3166 * satisfy a few restraints. First, the blocksize must be a
3167 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3168 * volsize must be a multiple of the block size, and cannot be
3171 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3172 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3174 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3175 "missing volume size"));
3176 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3179 if ((ret
= nvlist_lookup_uint64(props
,
3180 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3181 &blocksize
)) != 0) {
3182 if (ret
== ENOENT
) {
3183 blocksize
= zfs_prop_default_numeric(
3184 ZFS_PROP_VOLBLOCKSIZE
);
3187 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3188 "missing volume block size"));
3189 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3195 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3196 "volume size cannot be zero"));
3197 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3200 if (size
% blocksize
!= 0) {
3202 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3203 "volume size must be a multiple of volume block "
3205 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3209 /* create the dataset */
3210 ret
= lzc_create(path
, ost
, props
);
3213 /* check for failure */
3215 char parent
[ZFS_MAXNAMELEN
];
3218 (void) parent_name(path
, parent
, sizeof (parent
));
3222 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3223 "no such parent '%s'"), parent
);
3224 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3227 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3228 "parent '%s' is not a filesystem"), parent
);
3229 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3232 zfs_nicenum(SPA_MAXBLOCKSIZE
, buf
, sizeof (buf
));
3233 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3234 "volume block size must be power of 2 from "
3235 "512B to %s"), buf
);
3237 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3240 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3241 "pool must be upgraded to set this "
3242 "property or value"));
3243 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3247 * This platform can't address a volume this big.
3249 if (type
== ZFS_TYPE_VOLUME
)
3250 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3255 return (zfs_standard_error(hdl
, errno
, errbuf
));
3263 * Destroys the given dataset. The caller must make sure that the filesystem
3264 * isn't mounted, and that there are no active dependents. If the file system
3265 * does not exist this function does nothing.
3268 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3270 zfs_cmd_t zc
= {"\0"};
3272 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3273 nvlist_t
*nv
= fnvlist_alloc();
3274 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3275 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3278 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3279 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3285 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3287 if (ZFS_IS_VOLUME(zhp
)) {
3288 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3290 zc
.zc_objset_type
= DMU_OST_ZFS
;
3293 zc
.zc_defer_destroy
= defer
;
3294 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3296 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3297 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3301 remove_mountpoint(zhp
);
3306 struct destroydata
{
3308 const char *snapname
;
3312 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3314 struct destroydata
*dd
= arg
;
3315 char name
[ZFS_MAXNAMELEN
];
3318 (void) snprintf(name
, sizeof (name
),
3319 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3321 if (lzc_exists(name
))
3322 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3324 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3330 * Destroys all snapshots with the given name in zhp & descendants.
3333 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3336 struct destroydata dd
= { 0 };
3338 dd
.snapname
= snapname
;
3339 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3340 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3342 if (nvlist_empty(dd
.nvl
)) {
3343 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3344 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3345 zhp
->zfs_name
, snapname
);
3347 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3349 nvlist_free(dd
.nvl
);
3354 * Destroys all the snapshots named in the nvlist.
3357 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3363 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3368 if (nvlist_empty(errlist
)) {
3370 (void) snprintf(errbuf
, sizeof (errbuf
),
3371 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3373 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3375 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3376 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3378 (void) snprintf(errbuf
, sizeof (errbuf
),
3379 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3382 switch (fnvpair_value_int32(pair
)) {
3385 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3386 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3389 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3398 * Clones the given dataset. The target must be of the same type as the source.
3401 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3403 char parent
[ZFS_MAXNAMELEN
];
3406 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3409 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3411 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3412 "cannot create '%s'"), target
);
3414 /* validate the target/clone name */
3415 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3416 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3418 /* validate parents exist */
3419 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3422 (void) parent_name(target
, parent
, sizeof (parent
));
3428 if (ZFS_IS_VOLUME(zhp
)) {
3429 type
= ZFS_TYPE_VOLUME
;
3431 type
= ZFS_TYPE_FILESYSTEM
;
3433 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3434 zhp
, errbuf
)) == NULL
)
3438 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3446 * The parent doesn't exist. We should have caught this
3447 * above, but there may a race condition that has since
3448 * destroyed the parent.
3450 * At this point, we don't know whether it's the source
3451 * that doesn't exist anymore, or whether the target
3452 * dataset doesn't exist.
3454 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3455 "no such parent '%s'"), parent
);
3456 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3459 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3460 "source and target pools differ"));
3461 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3465 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3474 * Promotes the given clone fs to be the clone parent.
3477 zfs_promote(zfs_handle_t
*zhp
)
3479 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3480 zfs_cmd_t zc
= {"\0"};
3481 char parent
[MAXPATHLEN
];
3485 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3486 "cannot promote '%s'"), zhp
->zfs_name
);
3488 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3489 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3490 "snapshots can not be promoted"));
3491 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3494 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3495 if (parent
[0] == '\0') {
3496 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3497 "not a cloned filesystem"));
3498 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3501 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3502 sizeof (zc
.zc_value
));
3503 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3504 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3507 int save_errno
= errno
;
3509 switch (save_errno
) {
3511 /* There is a conflicting snapshot name. */
3512 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3513 "conflicting snapshot '%s' from parent '%s'"),
3514 zc
.zc_string
, parent
);
3515 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3518 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3524 typedef struct snapdata
{
3526 const char *sd_snapname
;
3530 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3532 snapdata_t
*sd
= arg
;
3533 char name
[ZFS_MAXNAMELEN
];
3536 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3537 (void) snprintf(name
, sizeof (name
),
3538 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3540 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3542 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3550 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3554 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3561 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3562 "cannot create snapshots "));
3565 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3566 const char *snapname
= nvpair_name(elem
);
3568 /* validate the target name */
3569 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3571 (void) snprintf(errbuf
, sizeof (errbuf
),
3572 dgettext(TEXT_DOMAIN
,
3573 "cannot create snapshot '%s'"), snapname
);
3574 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3578 if (props
!= NULL
&&
3579 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3580 props
, B_FALSE
, NULL
, errbuf
)) == NULL
) {
3584 ret
= lzc_snapshot(snaps
, props
, &errors
);
3587 boolean_t printed
= B_FALSE
;
3588 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3590 elem
= nvlist_next_nvpair(errors
, elem
)) {
3591 (void) snprintf(errbuf
, sizeof (errbuf
),
3592 dgettext(TEXT_DOMAIN
,
3593 "cannot create snapshot '%s'"), nvpair_name(elem
));
3594 (void) zfs_standard_error(hdl
,
3595 fnvpair_value_int32(elem
), errbuf
);
3601 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3602 "multiple snapshots of same "
3604 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3608 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3614 nvlist_free(errors
);
3619 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3623 snapdata_t sd
= { 0 };
3624 char fsname
[ZFS_MAXNAMELEN
];
3629 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3630 "cannot snapshot %s"), path
);
3632 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3633 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3635 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3636 cp
= strchr(fsname
, '@');
3638 sd
.sd_snapname
= cp
+ 1;
3640 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3641 ZFS_TYPE_VOLUME
)) == NULL
) {
3645 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3647 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3649 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3652 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3653 nvlist_free(sd
.sd_nvl
);
3659 * Destroy any more recent snapshots. We invoke this callback on any dependents
3660 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3661 * is a dependent and we should just destroy it without checking the transaction
3664 typedef struct rollback_data
{
3665 const char *cb_target
; /* the snapshot */
3666 uint64_t cb_create
; /* creation time reference */
3672 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3674 rollback_data_t
*cbp
= data
;
3675 prop_changelist_t
*clp
;
3677 /* We must destroy this clone; first unmount it */
3678 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3679 cbp
->cb_force
? MS_FORCE
: 0);
3680 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3681 cbp
->cb_error
= B_TRUE
;
3685 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3686 cbp
->cb_error
= B_TRUE
;
3688 changelist_remove(clp
, zhp
->zfs_name
);
3689 (void) changelist_postfix(clp
);
3690 changelist_free(clp
);
3697 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3699 rollback_data_t
*cbp
= data
;
3701 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3702 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3703 rollback_destroy_dependent
, cbp
);
3705 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3713 * Given a dataset, rollback to a specific snapshot, discarding any
3714 * data changes since then and making it the active dataset.
3716 * Any snapshots and bookmarks more recent than the target are
3717 * destroyed, along with their dependents (i.e. clones).
3720 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3722 rollback_data_t cb
= { 0 };
3724 boolean_t restore_resv
= 0;
3725 uint64_t old_volsize
= 0, new_volsize
;
3726 zfs_prop_t resv_prop
= { 0 };
3728 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3729 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3732 * Destroy all recent snapshots and their dependents.
3734 cb
.cb_force
= force
;
3735 cb
.cb_target
= snap
->zfs_name
;
3736 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3737 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3738 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3744 * Now that we have verified that the snapshot is the latest,
3745 * rollback to the given snapshot.
3748 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3749 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3751 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3753 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3757 * We rely on zfs_iter_children() to verify that there are no
3758 * newer snapshots for the given dataset. Therefore, we can
3759 * simply pass the name on to the ioctl() call. There is still
3760 * an unlikely race condition where the user has taken a
3761 * snapshot since we verified that this was the most recent.
3763 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3765 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3766 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3772 * For volumes, if the pre-rollback volsize matched the pre-
3773 * rollback reservation and the volsize has changed then set
3774 * the reservation property to the post-rollback volsize.
3775 * Make a new handle since the rollback closed the dataset.
3777 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3778 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3780 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3781 if (old_volsize
!= new_volsize
)
3782 err
= zfs_prop_set_int(zhp
, resv_prop
,
3791 * Renames the given dataset.
3794 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
3795 boolean_t force_unmount
)
3798 zfs_cmd_t zc
= {"\0"};
3800 prop_changelist_t
*cl
= NULL
;
3801 zfs_handle_t
*zhrp
= NULL
;
3802 char *parentname
= NULL
;
3803 char parent
[ZFS_MAXNAMELEN
];
3804 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3807 /* if we have the same exact name, just return success */
3808 if (strcmp(zhp
->zfs_name
, target
) == 0)
3811 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3812 "cannot rename to '%s'"), target
);
3815 * Make sure the target name is valid
3817 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3818 if ((strchr(target
, '@') == NULL
) ||
3821 * Snapshot target name is abbreviated,
3822 * reconstruct full dataset name
3824 (void) strlcpy(parent
, zhp
->zfs_name
,
3826 delim
= strchr(parent
, '@');
3827 if (strchr(target
, '@') == NULL
)
3831 (void) strlcat(parent
, target
, sizeof (parent
));
3835 * Make sure we're renaming within the same dataset.
3837 delim
= strchr(target
, '@');
3838 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3839 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3840 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3841 "snapshots must be part of same "
3843 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3847 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3848 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3851 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3852 "recursive rename must be a snapshot"));
3853 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3856 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3857 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3859 /* validate parents */
3860 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3863 /* make sure we're in the same pool */
3864 verify((delim
= strchr(target
, '/')) != NULL
);
3865 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3866 zhp
->zfs_name
[delim
- target
] != '/') {
3867 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3868 "datasets must be within same pool"));
3869 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3872 /* new name cannot be a child of the current dataset name */
3873 if (is_descendant(zhp
->zfs_name
, target
)) {
3874 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3875 "New dataset name cannot be a descendant of "
3876 "current dataset name"));
3877 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3881 (void) snprintf(errbuf
, sizeof (errbuf
),
3882 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3884 if (getzoneid() == GLOBAL_ZONEID
&&
3885 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3886 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3887 "dataset is used in a non-global zone"));
3888 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3892 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3893 if (parentname
== NULL
) {
3897 delim
= strchr(parentname
, '@');
3899 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3904 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
3905 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3906 force_unmount
? MS_FORCE
: 0)) == NULL
)
3909 if (changelist_haszonedchild(cl
)) {
3910 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3911 "child dataset with inherited mountpoint is used "
3912 "in a non-global zone"));
3913 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3918 if ((ret
= changelist_prefix(cl
)) != 0)
3922 if (ZFS_IS_VOLUME(zhp
))
3923 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3925 zc
.zc_objset_type
= DMU_OST_ZFS
;
3927 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3928 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3930 zc
.zc_cookie
= recursive
;
3932 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3934 * if it was recursive, the one that actually failed will
3937 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3938 "cannot rename '%s'"), zc
.zc_name
);
3940 if (recursive
&& errno
== EEXIST
) {
3941 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3942 "a child dataset already has a snapshot "
3943 "with the new name"));
3944 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3946 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3950 * On failure, we still want to remount any filesystems that
3951 * were previously mounted, so we don't alter the system state.
3954 (void) changelist_postfix(cl
);
3957 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3958 ret
= changelist_postfix(cl
);
3963 if (parentname
!= NULL
) {
3970 changelist_free(cl
);
3976 zfs_get_user_props(zfs_handle_t
*zhp
)
3978 return (zhp
->zfs_user_props
);
3982 * This function is used by 'zfs list' to determine the exact set of columns to
3983 * display, and their maximum widths. This does two main things:
3985 * - If this is a list of all properties, then expand the list to include
3986 * all native properties, and set a flag so that for each dataset we look
3987 * for new unique user properties and add them to the list.
3989 * - For non fixed-width properties, keep track of the maximum width seen
3990 * so that we can size the column appropriately. If the user has
3991 * requested received property values, we also need to compute the width
3992 * of the RECEIVED column.
3995 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
3998 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3999 zprop_list_t
*entry
;
4000 zprop_list_t
**last
, **start
;
4001 nvlist_t
*userprops
, *propval
;
4004 char buf
[ZFS_MAXPROPLEN
];
4006 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4009 userprops
= zfs_get_user_props(zhp
);
4012 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4014 * Go through and add any user properties as necessary. We
4015 * start by incrementing our list pointer to the first
4016 * non-native property.
4019 while (*start
!= NULL
) {
4020 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4022 start
= &(*start
)->pl_next
;
4026 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4028 * See if we've already found this property in our list.
4030 for (last
= start
; *last
!= NULL
;
4031 last
= &(*last
)->pl_next
) {
4032 if (strcmp((*last
)->pl_user_prop
,
4033 nvpair_name(elem
)) == 0)
4037 if (*last
== NULL
) {
4038 if ((entry
= zfs_alloc(hdl
,
4039 sizeof (zprop_list_t
))) == NULL
||
4040 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4041 nvpair_name(elem
)))) == NULL
) {
4046 entry
->pl_prop
= ZPROP_INVAL
;
4047 entry
->pl_width
= strlen(nvpair_name(elem
));
4048 entry
->pl_all
= B_TRUE
;
4055 * Now go through and check the width of any non-fixed columns
4057 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4058 if (entry
->pl_fixed
&& !literal
)
4061 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4062 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4063 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4064 if (strlen(buf
) > entry
->pl_width
)
4065 entry
->pl_width
= strlen(buf
);
4067 if (received
&& zfs_prop_get_recvd(zhp
,
4068 zfs_prop_to_name(entry
->pl_prop
),
4069 buf
, sizeof (buf
), literal
) == 0)
4070 if (strlen(buf
) > entry
->pl_recvd_width
)
4071 entry
->pl_recvd_width
= strlen(buf
);
4073 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4075 verify(nvlist_lookup_string(propval
,
4076 ZPROP_VALUE
, &strval
) == 0);
4077 if (strlen(strval
) > entry
->pl_width
)
4078 entry
->pl_width
= strlen(strval
);
4080 if (received
&& zfs_prop_get_recvd(zhp
,
4081 entry
->pl_user_prop
,
4082 buf
, sizeof (buf
), literal
) == 0)
4083 if (strlen(buf
) > entry
->pl_recvd_width
)
4084 entry
->pl_recvd_width
= strlen(buf
);
4092 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4097 * Keep a reference to the props-table against which we prune the
4100 zhp
->zfs_props_table
= props
;
4102 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4105 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4106 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4109 * User properties will result in ZPROP_INVAL, and since we
4110 * only know how to prune standard ZFS properties, we always
4111 * leave these in the list. This can also happen if we
4112 * encounter an unknown DSL property (when running older
4113 * software, for example).
4115 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4116 (void) nvlist_remove(zhp
->zfs_props
,
4117 nvpair_name(curr
), nvpair_type(curr
));
4123 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4124 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4126 zfs_cmd_t zc
= {"\0"};
4127 nvlist_t
*nvlist
= NULL
;
4130 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4131 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4132 zc
.zc_cookie
= (uint64_t)cmd
;
4134 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4135 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4136 (void) no_memory(hdl
);
4142 case ZFS_SMB_ACL_ADD
:
4143 case ZFS_SMB_ACL_REMOVE
:
4144 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4146 case ZFS_SMB_ACL_RENAME
:
4147 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4149 (void) no_memory(hdl
);
4152 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4154 (void) no_memory(hdl
);
4157 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4158 nvlist_free(nvlist
);
4162 case ZFS_SMB_ACL_PURGE
:
4167 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4169 nvlist_free(nvlist
);
4174 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4175 char *path
, char *resource
)
4177 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4182 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4183 char *path
, char *resource
)
4185 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4190 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4192 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4197 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4198 char *oldname
, char *newname
)
4200 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4205 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4206 zfs_userspace_cb_t func
, void *arg
)
4208 zfs_cmd_t zc
= {"\0"};
4209 zfs_useracct_t buf
[100];
4210 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4213 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4215 zc
.zc_objset_type
= type
;
4216 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4219 zfs_useracct_t
*zua
= buf
;
4221 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4222 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4223 char errbuf
[ZFS_MAXNAMELEN
+ 32];
4225 (void) snprintf(errbuf
, sizeof (errbuf
),
4226 dgettext(TEXT_DOMAIN
,
4227 "cannot get used/quota for %s"), zc
.zc_name
);
4228 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4230 if (zc
.zc_nvlist_dst_size
== 0)
4233 while (zc
.zc_nvlist_dst_size
> 0) {
4234 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4235 zua
->zu_space
)) != 0)
4238 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4247 const char *snapname
;
4249 boolean_t recursive
;
4254 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4256 struct holdarg
*ha
= arg
;
4257 char name
[ZFS_MAXNAMELEN
];
4260 (void) snprintf(name
, sizeof (name
),
4261 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4263 if (lzc_exists(name
))
4264 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4267 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4273 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4274 boolean_t recursive
, int cleanup_fd
)
4279 ha
.nvl
= fnvlist_alloc();
4280 ha
.snapname
= snapname
;
4282 ha
.recursive
= recursive
;
4283 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4285 if (nvlist_empty(ha
.nvl
)) {
4288 fnvlist_free(ha
.nvl
);
4290 (void) snprintf(errbuf
, sizeof (errbuf
),
4291 dgettext(TEXT_DOMAIN
,
4292 "cannot hold snapshot '%s@%s'"),
4293 zhp
->zfs_name
, snapname
);
4294 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4298 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4299 fnvlist_free(ha
.nvl
);
4305 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4309 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4314 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4317 /* There may be errors even in the success case. */
4318 fnvlist_free(errors
);
4322 if (nvlist_empty(errors
)) {
4323 /* no hold-specific errors */
4324 (void) snprintf(errbuf
, sizeof (errbuf
),
4325 dgettext(TEXT_DOMAIN
, "cannot hold"));
4328 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4329 "pool must be upgraded"));
4330 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4333 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4336 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4340 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4342 elem
= nvlist_next_nvpair(errors
, elem
)) {
4343 (void) snprintf(errbuf
, sizeof (errbuf
),
4344 dgettext(TEXT_DOMAIN
,
4345 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4346 switch (fnvpair_value_int32(elem
)) {
4349 * Temporary tags wind up having the ds object id
4350 * prepended. So even if we passed the length check
4351 * above, it's still possible for the tag to wind
4352 * up being slightly too long.
4354 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4357 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4360 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4363 (void) zfs_standard_error(hdl
,
4364 fnvpair_value_int32(elem
), errbuf
);
4368 fnvlist_free(errors
);
4373 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4375 struct holdarg
*ha
= arg
;
4376 char name
[ZFS_MAXNAMELEN
];
4378 nvlist_t
*existing_holds
;
4380 (void) snprintf(name
, sizeof (name
),
4381 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4383 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4385 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4388 nvlist_t
*torelease
= fnvlist_alloc();
4389 fnvlist_add_boolean(torelease
, ha
->tag
);
4390 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4391 fnvlist_free(torelease
);
4395 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4401 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4402 boolean_t recursive
)
4406 nvlist_t
*errors
= NULL
;
4408 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4411 ha
.nvl
= fnvlist_alloc();
4412 ha
.snapname
= snapname
;
4414 ha
.recursive
= recursive
;
4416 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4418 if (nvlist_empty(ha
.nvl
)) {
4419 fnvlist_free(ha
.nvl
);
4421 (void) snprintf(errbuf
, sizeof (errbuf
),
4422 dgettext(TEXT_DOMAIN
,
4423 "cannot release hold from snapshot '%s@%s'"),
4424 zhp
->zfs_name
, snapname
);
4426 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4428 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4433 ret
= lzc_release(ha
.nvl
, &errors
);
4434 fnvlist_free(ha
.nvl
);
4437 /* There may be errors even in the success case. */
4438 fnvlist_free(errors
);
4442 if (nvlist_empty(errors
)) {
4443 /* no hold-specific errors */
4444 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4448 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4449 "pool must be upgraded"));
4450 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4453 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4457 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4459 elem
= nvlist_next_nvpair(errors
, elem
)) {
4460 (void) snprintf(errbuf
, sizeof (errbuf
),
4461 dgettext(TEXT_DOMAIN
,
4462 "cannot release hold from snapshot '%s'"),
4464 switch (fnvpair_value_int32(elem
)) {
4466 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4469 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4472 (void) zfs_standard_error_fmt(hdl
,
4473 fnvpair_value_int32(elem
), errbuf
);
4477 fnvlist_free(errors
);
4482 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4484 zfs_cmd_t zc
= {"\0"};
4485 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4491 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4492 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4496 nvbuf
= malloc(nvsz
);
4497 if (nvbuf
== NULL
) {
4498 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4502 zc
.zc_nvlist_dst_size
= nvsz
;
4503 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4505 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4507 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4508 (void) snprintf(errbuf
, sizeof (errbuf
),
4509 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4514 nvsz
= zc
.zc_nvlist_dst_size
;
4518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4519 "pool must be upgraded"));
4520 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4523 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4526 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4529 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4534 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4536 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4537 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4539 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4549 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4551 zfs_cmd_t zc
= {"\0"};
4552 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4558 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4559 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4561 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4564 nvbuf
= malloc(nvsz
);
4566 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4569 zc
.zc_nvlist_src_size
= nvsz
;
4570 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4571 zc
.zc_perm_action
= un
;
4573 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4575 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4576 (void) snprintf(errbuf
, sizeof (errbuf
),
4577 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4582 "pool must be upgraded"));
4583 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4586 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4589 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4592 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4603 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4608 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4611 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4613 (void) snprintf(errbuf
, sizeof (errbuf
),
4614 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4618 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4619 "pool must be upgraded"));
4620 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4623 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4626 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4629 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4638 * Convert the zvol's volume size to an appropriate reservation.
4639 * Note: If this routine is updated, it is necessary to update the ZFS test
4640 * suite's shell version in reservation.kshlib.
4643 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4646 uint64_t nblocks
, volblocksize
;
4650 if (nvlist_lookup_string(props
,
4651 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4652 ncopies
= atoi(strval
);
4655 if (nvlist_lookup_uint64(props
,
4656 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4657 &volblocksize
) != 0)
4658 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4659 nblocks
= volsize
/volblocksize
;
4660 /* start with metadnode L0-L6 */
4662 /* calculate number of indirects */
4663 while (nblocks
> 1) {
4664 nblocks
+= DNODES_PER_LEVEL
- 1;
4665 nblocks
/= DNODES_PER_LEVEL
;
4668 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4671 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4672 * compressed, but in practice they compress down to about
4675 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;