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
)
1528 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1529 nvlist_t
*nvl
= NULL
;
1531 (void) snprintf(errbuf
, sizeof (errbuf
),
1532 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1535 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1536 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1537 (void) no_memory(hdl
);
1541 ret
= zfs_prop_set_list(zhp
, nvl
);
1551 * Given an nvlist of property names and values, set the properties for the
1555 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1557 zfs_cmd_t zc
= {"\0"};
1559 prop_changelist_t
**cls
= NULL
;
1562 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1566 zfs_prop_t prop
= 0;
1569 (void) snprintf(errbuf
, sizeof (errbuf
),
1570 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1573 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1574 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1578 * We have to check for any extra properties which need to be added
1579 * before computing the length of the nvlist.
1581 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1583 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1584 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1585 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1590 * Check how many properties we're setting and allocate an array to
1591 * store changelist pointers for postfix().
1594 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1596 elem
= nvlist_next_nvpair(nvl
, elem
))
1598 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1602 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1604 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1606 prop
= zfs_name_to_prop(nvpair_name(elem
));
1608 assert(cl_idx
< nvl_len
);
1610 * We don't want to unmount & remount the dataset when changing
1611 * its canmount property to 'on' or 'noauto'. We only use
1612 * the changelist logic to unmount when setting canmount=off.
1614 if (!(prop
== ZFS_PROP_CANMOUNT
&&
1615 fnvpair_value_uint64(elem
) != ZFS_CANMOUNT_OFF
)) {
1616 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1617 if (cls
[cl_idx
] == NULL
)
1621 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1622 changelist_haszonedchild(cls
[cl_idx
])) {
1623 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1624 "child dataset with inherited mountpoint is used "
1625 "in a non-global zone"));
1626 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1630 if (cls
[cl_idx
] != NULL
&&
1631 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1636 assert(cl_idx
== nvl_len
);
1639 * Execute the corresponding ioctl() to set this list of properties.
1641 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1643 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1644 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1647 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1650 /* Get the list of unset properties back and report them. */
1651 nvlist_t
*errorprops
= NULL
;
1652 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1654 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1656 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1657 prop
= zfs_name_to_prop(nvpair_name(elem
));
1658 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1660 nvlist_free(errorprops
);
1662 if (added_resv
&& errno
== ENOSPC
) {
1663 /* clean up the volsize property we tried to set */
1664 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1668 zcmd_free_nvlists(&zc
);
1670 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1672 if (nvlist_add_uint64(nvl
,
1673 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1676 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1678 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1681 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1682 if (cls
[cl_idx
] != NULL
) {
1683 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1691 * Refresh the statistics so the new property
1692 * value is reflected.
1694 (void) get_stats(zhp
);
1697 * Remount the filesystem to propagate the change
1698 * if one of the options handled by the generic
1699 * Linux namespace layer has been modified.
1701 if (zfs_is_namespace_prop(prop
) &&
1702 zfs_is_mounted(zhp
, NULL
))
1703 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1709 zcmd_free_nvlists(&zc
);
1711 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1712 if (cls
[cl_idx
] != NULL
)
1713 changelist_free(cls
[cl_idx
]);
1721 * Given a property, inherit the value from the parent dataset, or if received
1722 * is TRUE, revert to the received value, if any.
1725 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1727 zfs_cmd_t zc
= {"\0"};
1729 prop_changelist_t
*cl
;
1730 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1734 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1735 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1737 zc
.zc_cookie
= received
;
1738 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1740 * For user properties, the amount of work we have to do is very
1741 * small, so just do it here.
1743 if (!zfs_prop_user(propname
)) {
1744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1745 "invalid property"));
1746 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1749 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1750 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1752 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1753 return (zfs_standard_error(hdl
, errno
, errbuf
));
1759 * Verify that this property is inheritable.
1761 if (zfs_prop_readonly(prop
))
1762 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1764 if (!zfs_prop_inheritable(prop
) && !received
)
1765 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1768 * Check to see if the value applies to this type
1770 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1771 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1774 * Normalize the name, to get rid of shorthand abbreviations.
1776 propname
= zfs_prop_to_name(prop
);
1777 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1778 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1780 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1781 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1782 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1783 "dataset is used in a non-global zone"));
1784 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1788 * Determine datasets which will be affected by this change, if any.
1790 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1793 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1794 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1795 "child dataset with inherited mountpoint is used "
1796 "in a non-global zone"));
1797 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1801 if ((ret
= changelist_prefix(cl
)) != 0)
1804 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1805 return (zfs_standard_error(hdl
, errno
, errbuf
));
1808 if ((ret
= changelist_postfix(cl
)) != 0)
1812 * Refresh the statistics so the new property is reflected.
1814 (void) get_stats(zhp
);
1817 * Remount the filesystem to propagate the change
1818 * if one of the options handled by the generic
1819 * Linux namespace layer has been modified.
1821 if (zfs_is_namespace_prop(prop
) &&
1822 zfs_is_mounted(zhp
, NULL
))
1823 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1827 changelist_free(cl
);
1832 * True DSL properties are stored in an nvlist. The following two functions
1833 * extract them appropriately.
1836 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1842 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1843 zfs_prop_to_name(prop
), &nv
) == 0) {
1844 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1845 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1847 verify(!zhp
->zfs_props_table
||
1848 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1849 value
= zfs_prop_default_numeric(prop
);
1857 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1863 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1864 zfs_prop_to_name(prop
), &nv
) == 0) {
1865 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1866 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1868 verify(!zhp
->zfs_props_table
||
1869 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1870 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1879 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1881 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1885 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1887 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1888 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1892 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1894 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1899 * Internal function for getting a numeric property. Both zfs_prop_get() and
1900 * zfs_prop_get_int() are built using this interface.
1902 * Certain properties can be overridden using 'mount -o'. In this case, scan
1903 * the contents of the /etc/mtab entry, searching for the appropriate options.
1904 * If they differ from the on-disk values, report the current values and mark
1905 * the source "temporary".
1908 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1909 char **source
, uint64_t *val
)
1911 zfs_cmd_t zc
= {"\0"};
1912 nvlist_t
*zplprops
= NULL
;
1914 char *mntopt_on
= NULL
;
1915 char *mntopt_off
= NULL
;
1916 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1921 * If the property is being fetched for a snapshot, check whether
1922 * the property is valid for the snapshot's head dataset type.
1924 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
1925 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
1926 *val
= zfs_prop_default_numeric(prop
);
1931 case ZFS_PROP_ATIME
:
1932 mntopt_on
= MNTOPT_ATIME
;
1933 mntopt_off
= MNTOPT_NOATIME
;
1936 case ZFS_PROP_RELATIME
:
1937 mntopt_on
= MNTOPT_RELATIME
;
1938 mntopt_off
= MNTOPT_NORELATIME
;
1941 case ZFS_PROP_DEVICES
:
1942 mntopt_on
= MNTOPT_DEVICES
;
1943 mntopt_off
= MNTOPT_NODEVICES
;
1947 mntopt_on
= MNTOPT_EXEC
;
1948 mntopt_off
= MNTOPT_NOEXEC
;
1951 case ZFS_PROP_READONLY
:
1952 mntopt_on
= MNTOPT_RO
;
1953 mntopt_off
= MNTOPT_RW
;
1956 case ZFS_PROP_SETUID
:
1957 mntopt_on
= MNTOPT_SETUID
;
1958 mntopt_off
= MNTOPT_NOSETUID
;
1961 case ZFS_PROP_XATTR
:
1962 mntopt_on
= MNTOPT_XATTR
;
1963 mntopt_off
= MNTOPT_NOXATTR
;
1966 case ZFS_PROP_NBMAND
:
1967 mntopt_on
= MNTOPT_NBMAND
;
1968 mntopt_off
= MNTOPT_NONBMAND
;
1975 * Because looking up the mount options is potentially expensive
1976 * (iterating over all of /etc/mtab), we defer its calculation until
1977 * we're looking up a property which requires its presence.
1979 if (!zhp
->zfs_mntcheck
&&
1980 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1981 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1982 struct mnttab entry
;
1984 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1985 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1987 if (zhp
->zfs_mntopts
== NULL
)
1991 zhp
->zfs_mntcheck
= B_TRUE
;
1994 if (zhp
->zfs_mntopts
== NULL
)
1995 mnt
.mnt_mntopts
= "";
1997 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2000 case ZFS_PROP_ATIME
:
2001 case ZFS_PROP_RELATIME
:
2002 case ZFS_PROP_DEVICES
:
2004 case ZFS_PROP_READONLY
:
2005 case ZFS_PROP_SETUID
:
2006 case ZFS_PROP_XATTR
:
2007 case ZFS_PROP_NBMAND
:
2008 *val
= getprop_uint64(zhp
, prop
, source
);
2013 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2016 *src
= ZPROP_SRC_TEMPORARY
;
2017 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2020 *src
= ZPROP_SRC_TEMPORARY
;
2024 case ZFS_PROP_CANMOUNT
:
2025 case ZFS_PROP_VOLSIZE
:
2026 case ZFS_PROP_QUOTA
:
2027 case ZFS_PROP_REFQUOTA
:
2028 case ZFS_PROP_RESERVATION
:
2029 case ZFS_PROP_REFRESERVATION
:
2030 case ZFS_PROP_FILESYSTEM_LIMIT
:
2031 case ZFS_PROP_SNAPSHOT_LIMIT
:
2032 case ZFS_PROP_FILESYSTEM_COUNT
:
2033 case ZFS_PROP_SNAPSHOT_COUNT
:
2034 *val
= getprop_uint64(zhp
, prop
, source
);
2036 if (*source
== NULL
) {
2037 /* not default, must be local */
2038 *source
= zhp
->zfs_name
;
2042 case ZFS_PROP_MOUNTED
:
2043 *val
= (zhp
->zfs_mntopts
!= NULL
);
2046 case ZFS_PROP_NUMCLONES
:
2047 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2050 case ZFS_PROP_VERSION
:
2051 case ZFS_PROP_NORMALIZE
:
2052 case ZFS_PROP_UTF8ONLY
:
2054 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2056 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2057 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2058 zcmd_free_nvlists(&zc
);
2059 if (prop
== ZFS_PROP_VERSION
&&
2060 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2061 *val
= zfs_prop_default_numeric(prop
);
2064 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2065 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2067 zcmd_free_nvlists(&zc
);
2071 nvlist_free(zplprops
);
2072 zcmd_free_nvlists(&zc
);
2075 case ZFS_PROP_INCONSISTENT
:
2076 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2080 switch (zfs_prop_get_type(prop
)) {
2081 case PROP_TYPE_NUMBER
:
2082 case PROP_TYPE_INDEX
:
2083 *val
= getprop_uint64(zhp
, prop
, source
);
2085 * If we tried to use a default value for a
2086 * readonly property, it means that it was not
2089 if (zfs_prop_readonly(prop
) &&
2090 *source
!= NULL
&& (*source
)[0] == '\0') {
2095 case PROP_TYPE_STRING
:
2097 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2098 "cannot get non-numeric property"));
2099 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2100 dgettext(TEXT_DOMAIN
, "internal error")));
2108 * Calculate the source type, given the raw source string.
2111 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2112 char *statbuf
, size_t statlen
)
2114 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2117 if (source
== NULL
) {
2118 *srctype
= ZPROP_SRC_NONE
;
2119 } else if (source
[0] == '\0') {
2120 *srctype
= ZPROP_SRC_DEFAULT
;
2121 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2122 *srctype
= ZPROP_SRC_RECEIVED
;
2124 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2125 *srctype
= ZPROP_SRC_LOCAL
;
2127 (void) strlcpy(statbuf
, source
, statlen
);
2128 *srctype
= ZPROP_SRC_INHERITED
;
2135 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2136 size_t proplen
, boolean_t literal
)
2141 if (zhp
->zfs_recvd_props
== NULL
)
2142 if (get_recvd_props_ioctl(zhp
) != 0)
2145 prop
= zfs_name_to_prop(propname
);
2147 if (prop
!= ZPROP_INVAL
) {
2149 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2151 zfs_set_recvd_props_mode(zhp
, &cookie
);
2152 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2153 NULL
, NULL
, 0, literal
);
2154 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2158 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2159 propname
, &propval
) != 0)
2161 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2163 (void) strlcpy(propbuf
, recvdval
, proplen
);
2166 return (err
== 0 ? 0 : -1);
2170 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2175 value
= zfs_get_clones_nvl(zhp
);
2180 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2181 pair
= nvlist_next_nvpair(value
, pair
)) {
2182 if (propbuf
[0] != '\0')
2183 (void) strlcat(propbuf
, ",", proplen
);
2184 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2190 struct get_clones_arg
{
2194 char buf
[ZFS_MAXNAMELEN
];
2198 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2200 struct get_clones_arg
*gca
= arg
;
2202 if (gca
->numclones
== 0) {
2207 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2208 NULL
, NULL
, 0, B_TRUE
) != 0)
2210 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2211 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2216 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2222 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2224 nvlist_t
*nv
, *value
;
2226 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2227 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2228 struct get_clones_arg gca
;
2231 * if this is a snapshot, then the kernel wasn't able
2232 * to get the clones. Do it by slowly iterating.
2234 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2236 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2238 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2243 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2245 gca
.origin
= zhp
->zfs_name
;
2247 if (gca
.numclones
!= 0) {
2249 char pool
[ZFS_MAXNAMELEN
];
2252 /* get the pool name */
2253 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2254 (void) strsep(&cp
, "/@");
2255 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2256 ZFS_TYPE_FILESYSTEM
);
2258 (void) get_clones_cb(root
, &gca
);
2261 if (gca
.numclones
!= 0 ||
2262 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2263 nvlist_add_nvlist(zhp
->zfs_props
,
2264 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2271 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2272 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2275 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2281 * Retrieve a property from the given object. If 'literal' is specified, then
2282 * numbers are left as exact values. Otherwise, numbers are converted to a
2283 * human-readable form.
2285 * Returns 0 on success, or -1 on error.
2288 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2289 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2291 char *source
= NULL
;
2295 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2298 * Check to see if this property applies to our object
2300 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2303 if (received
&& zfs_prop_readonly(prop
))
2307 *src
= ZPROP_SRC_NONE
;
2310 case ZFS_PROP_CREATION
:
2312 * 'creation' is a time_t stored in the statistics. We convert
2313 * this into a string unless 'literal' is specified.
2316 val
= getprop_uint64(zhp
, prop
, &source
);
2317 time_t time
= (time_t)val
;
2321 localtime_r(&time
, &t
) == NULL
||
2322 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2324 (void) snprintf(propbuf
, proplen
, "%llu",
2325 (u_longlong_t
) val
);
2329 case ZFS_PROP_MOUNTPOINT
:
2331 * Getting the precise mountpoint can be tricky.
2333 * - for 'none' or 'legacy', return those values.
2334 * - for inherited mountpoints, we want to take everything
2335 * after our ancestor and append it to the inherited value.
2337 * If the pool has an alternate root, we want to prepend that
2338 * root to any values we return.
2341 str
= getprop_string(zhp
, prop
, &source
);
2343 if (str
[0] == '/') {
2344 char buf
[MAXPATHLEN
];
2346 const char *relpath
;
2349 * If we inherit the mountpoint, even from a dataset
2350 * with a received value, the source will be the path of
2351 * the dataset we inherit from. If source is
2352 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2355 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2358 relpath
= zhp
->zfs_name
+ strlen(source
);
2359 if (relpath
[0] == '/')
2363 if ((zpool_get_prop(zhp
->zpool_hdl
,
2364 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
2365 (strcmp(root
, "-") == 0))
2368 * Special case an alternate root of '/'. This will
2369 * avoid having multiple leading slashes in the
2372 if (strcmp(root
, "/") == 0)
2376 * If the mountpoint is '/' then skip over this
2377 * if we are obtaining either an alternate root or
2378 * an inherited mountpoint.
2380 if (str
[1] == '\0' && (root
[0] != '\0' ||
2381 relpath
[0] != '\0'))
2384 if (relpath
[0] == '\0')
2385 (void) snprintf(propbuf
, proplen
, "%s%s",
2388 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2389 root
, str
, relpath
[0] == '@' ? "" : "/",
2392 /* 'legacy' or 'none' */
2393 (void) strlcpy(propbuf
, str
, proplen
);
2398 case ZFS_PROP_ORIGIN
:
2399 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2402 * If there is no parent at all, return failure to indicate that
2403 * it doesn't apply to this dataset.
2405 if (propbuf
[0] == '\0')
2409 case ZFS_PROP_CLONES
:
2410 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2414 case ZFS_PROP_QUOTA
:
2415 case ZFS_PROP_REFQUOTA
:
2416 case ZFS_PROP_RESERVATION
:
2417 case ZFS_PROP_REFRESERVATION
:
2419 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2423 * If quota or reservation is 0, we translate this into 'none'
2424 * (unless literal is set), and indicate that it's the default
2425 * value. Otherwise, we print the number nicely and indicate
2426 * that its set locally.
2430 (void) strlcpy(propbuf
, "0", proplen
);
2432 (void) strlcpy(propbuf
, "none", proplen
);
2435 (void) snprintf(propbuf
, proplen
, "%llu",
2438 zfs_nicenum(val
, propbuf
, proplen
);
2442 case ZFS_PROP_FILESYSTEM_LIMIT
:
2443 case ZFS_PROP_SNAPSHOT_LIMIT
:
2444 case ZFS_PROP_FILESYSTEM_COUNT
:
2445 case ZFS_PROP_SNAPSHOT_COUNT
:
2447 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2451 * If limit is UINT64_MAX, we translate this into 'none' (unless
2452 * literal is set), and indicate that it's the default value.
2453 * Otherwise, we print the number nicely and indicate that it's
2457 (void) snprintf(propbuf
, proplen
, "%llu",
2459 } else if (val
== UINT64_MAX
) {
2460 (void) strlcpy(propbuf
, "none", proplen
);
2462 zfs_nicenum(val
, propbuf
, proplen
);
2466 case ZFS_PROP_REFRATIO
:
2467 case ZFS_PROP_COMPRESSRATIO
:
2468 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2470 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2471 (u_longlong_t
)(val
/ 100),
2472 (u_longlong_t
)(val
% 100));
2476 switch (zhp
->zfs_type
) {
2477 case ZFS_TYPE_FILESYSTEM
:
2480 case ZFS_TYPE_VOLUME
:
2483 case ZFS_TYPE_SNAPSHOT
:
2486 case ZFS_TYPE_BOOKMARK
:
2492 (void) snprintf(propbuf
, proplen
, "%s", str
);
2495 case ZFS_PROP_MOUNTED
:
2497 * The 'mounted' property is a pseudo-property that described
2498 * whether the filesystem is currently mounted. Even though
2499 * it's a boolean value, the typical values of "on" and "off"
2500 * don't make sense, so we translate to "yes" and "no".
2502 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2503 src
, &source
, &val
) != 0)
2506 (void) strlcpy(propbuf
, "yes", proplen
);
2508 (void) strlcpy(propbuf
, "no", proplen
);
2513 * The 'name' property is a pseudo-property derived from the
2514 * dataset name. It is presented as a real property to simplify
2517 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2520 case ZFS_PROP_MLSLABEL
:
2522 #ifdef HAVE_MLSLABEL
2523 m_label_t
*new_sl
= NULL
;
2524 char *ascii
= NULL
; /* human readable label */
2526 (void) strlcpy(propbuf
,
2527 getprop_string(zhp
, prop
, &source
), proplen
);
2529 if (literal
|| (strcasecmp(propbuf
,
2530 ZFS_MLSLABEL_DEFAULT
) == 0))
2534 * Try to translate the internal hex string to
2535 * human-readable output. If there are any
2536 * problems just use the hex string.
2539 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2540 L_NO_CORRECTION
, NULL
) == -1) {
2541 m_label_free(new_sl
);
2545 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2549 m_label_free(new_sl
);
2552 m_label_free(new_sl
);
2554 (void) strlcpy(propbuf
, ascii
, proplen
);
2557 (void) strlcpy(propbuf
,
2558 getprop_string(zhp
, prop
, &source
), proplen
);
2559 #endif /* HAVE_MLSLABEL */
2565 * GUIDs are stored as numbers, but they are identifiers.
2566 * We don't want them to be pretty printed, because pretty
2567 * printing mangles the ID into a truncated and useless value.
2569 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2571 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2575 switch (zfs_prop_get_type(prop
)) {
2576 case PROP_TYPE_NUMBER
:
2577 if (get_numeric_property(zhp
, prop
, src
,
2578 &source
, &val
) != 0)
2581 (void) snprintf(propbuf
, proplen
, "%llu",
2584 zfs_nicenum(val
, propbuf
, proplen
);
2587 case PROP_TYPE_STRING
:
2588 (void) strlcpy(propbuf
,
2589 getprop_string(zhp
, prop
, &source
), proplen
);
2592 case PROP_TYPE_INDEX
:
2593 if (get_numeric_property(zhp
, prop
, src
,
2594 &source
, &val
) != 0)
2596 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2598 (void) strlcpy(propbuf
, strval
, proplen
);
2606 get_source(zhp
, src
, source
, statbuf
, statlen
);
2612 * Utility function to get the given numeric property. Does no validation that
2613 * the given property is the appropriate type; should only be used with
2614 * hard-coded property types.
2617 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2622 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2628 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2632 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2633 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2637 * Similar to zfs_prop_get(), but returns the value as an integer.
2640 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2641 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2646 * Check to see if this property applies to our object
2648 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
2649 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2650 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2651 zfs_prop_to_name(prop
)));
2655 *src
= ZPROP_SRC_NONE
;
2657 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2660 get_source(zhp
, src
, source
, statbuf
, statlen
);
2667 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2668 char **domainp
, idmap_rid_t
*ridp
)
2670 idmap_get_handle_t
*get_hdl
= NULL
;
2674 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2678 err
= idmap_get_sidbyuid(get_hdl
, id
,
2679 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2681 err
= idmap_get_sidbygid(get_hdl
, id
,
2682 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2684 if (err
== IDMAP_SUCCESS
&&
2685 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2686 status
== IDMAP_SUCCESS
)
2692 idmap_get_destroy(get_hdl
);
2695 #endif /* HAVE_IDMAP */
2698 * convert the propname into parameters needed by kernel
2699 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2700 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2701 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2702 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2705 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2706 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2708 zfs_userquota_prop_t type
;
2717 /* Figure out the property type ({user|group}{quota|space}) */
2718 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2719 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2720 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2723 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2727 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
);
2728 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
);
2730 cp
= strchr(propname
, '@') + 1;
2732 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2733 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2736 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2737 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2740 } else if (strchr(cp
, '@')) {
2743 * It's a SID name (eg "user@domain") that needs to be
2744 * turned into S-1-domainID-RID.
2746 directory_error_t e
;
2747 char *numericsid
= NULL
;
2750 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2753 e
= directory_sid_from_user_name(NULL
,
2756 e
= directory_sid_from_group_name(NULL
,
2760 directory_error_free(e
);
2763 if (numericsid
== NULL
)
2766 (void) strlcpy(domain
, cp
, domainlen
);
2767 cp
= strrchr(domain
, '-');
2772 *ridp
= strtoull(cp
, &end
, 10);
2775 if (errno
!= 0 || *end
!= '\0')
2779 #endif /* HAVE_IDMAP */
2781 /* It's a user/group ID (eg "12345"). */
2784 id
= strtoul(cp
, &end
, 10);
2789 /* It's an ephemeral ID. */
2793 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2794 &mapdomain
, &rid
) != 0)
2796 (void) strlcpy(domain
, mapdomain
, domainlen
);
2800 #endif /* HAVE_IDMAP */
2810 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2811 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2814 zfs_cmd_t zc
= {"\0"};
2816 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2818 err
= userquota_propname_decode(propname
,
2819 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2820 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2821 zc
.zc_objset_type
= *typep
;
2825 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2829 *propvalue
= zc
.zc_cookie
;
2834 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2835 uint64_t *propvalue
)
2837 zfs_userquota_prop_t type
;
2839 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2844 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2845 char *propbuf
, int proplen
, boolean_t literal
)
2849 zfs_userquota_prop_t type
;
2851 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2858 (void) snprintf(propbuf
, proplen
, "%llu",
2859 (u_longlong_t
)propvalue
);
2860 } else if (propvalue
== 0 &&
2861 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2862 (void) strlcpy(propbuf
, "none", proplen
);
2864 zfs_nicenum(propvalue
, propbuf
, proplen
);
2870 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2871 uint64_t *propvalue
)
2874 zfs_cmd_t zc
= {"\0"};
2875 const char *snapname
;
2877 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2879 snapname
= strchr(propname
, '@') + 1;
2880 if (strchr(snapname
, '@')) {
2881 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2883 /* snapname is the short name, append it to zhp's fsname */
2886 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2887 sizeof (zc
.zc_value
));
2888 cp
= strchr(zc
.zc_value
, '@');
2891 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2892 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2895 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2899 *propvalue
= zc
.zc_cookie
;
2904 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2905 char *propbuf
, int proplen
, boolean_t literal
)
2910 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2916 (void) snprintf(propbuf
, proplen
, "%llu",
2917 (u_longlong_t
)propvalue
);
2919 zfs_nicenum(propvalue
, propbuf
, proplen
);
2926 * Returns the name of the given zfs handle.
2929 zfs_get_name(const zfs_handle_t
*zhp
)
2931 return (zhp
->zfs_name
);
2935 * Returns the type of the given zfs handle.
2938 zfs_get_type(const zfs_handle_t
*zhp
)
2940 return (zhp
->zfs_type
);
2944 * Is one dataset name a child dataset of another?
2946 * Needs to handle these cases:
2947 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2948 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2949 * Descendant? No. No. No. Yes.
2952 is_descendant(const char *ds1
, const char *ds2
)
2954 size_t d1len
= strlen(ds1
);
2956 /* ds2 can't be a descendant if it's smaller */
2957 if (strlen(ds2
) < d1len
)
2960 /* otherwise, compare strings and verify that there's a '/' char */
2961 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2965 * Given a complete name, return just the portion that refers to the parent.
2966 * Will return -1 if there is no parent (path is just the name of the
2970 parent_name(const char *path
, char *buf
, size_t buflen
)
2974 (void) strlcpy(buf
, path
, buflen
);
2976 if ((slashp
= strrchr(buf
, '/')) == NULL
)
2984 * If accept_ancestor is false, then check to make sure that the given path has
2985 * a parent, and that it exists. If accept_ancestor is true, then find the
2986 * closest existing ancestor for the given path. In prefixlen return the
2987 * length of already existing prefix of the given path. We also fetch the
2988 * 'zoned' property, which is used to validate property settings when creating
2992 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2993 boolean_t accept_ancestor
, int *prefixlen
)
2995 zfs_cmd_t zc
= {"\0"};
2996 char parent
[ZFS_MAXNAMELEN
];
3002 (void) snprintf(errbuf
, sizeof (errbuf
),
3003 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3005 /* get parent, and check to see if this is just a pool */
3006 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3007 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3008 "missing dataset name"));
3009 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3012 /* check to see if the pool exists */
3013 if ((slash
= strchr(parent
, '/')) == NULL
)
3014 slash
= parent
+ strlen(parent
);
3015 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3016 zc
.zc_name
[slash
- parent
] = '\0';
3017 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3019 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3020 "no such pool '%s'"), zc
.zc_name
);
3021 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3024 /* check to see if the parent dataset exists */
3025 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3026 if (errno
== ENOENT
&& accept_ancestor
) {
3028 * Go deeper to find an ancestor, give up on top level.
3030 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3031 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3032 "no such pool '%s'"), zc
.zc_name
);
3033 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3035 } else if (errno
== ENOENT
) {
3036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3037 "parent does not exist"));
3038 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3040 return (zfs_standard_error(hdl
, errno
, errbuf
));
3043 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3047 /* we are in a non-global zone, but parent is in the global zone */
3048 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3049 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3054 /* make sure parent is a filesystem */
3055 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3056 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3057 "parent is not a filesystem"));
3058 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3064 if (prefixlen
!= NULL
)
3065 *prefixlen
= strlen(parent
);
3070 * Finds whether the dataset of the given type(s) exists.
3073 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3077 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3081 * Try to get stats for the dataset, which will tell us if it exists.
3083 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3084 int ds_type
= zhp
->zfs_type
;
3087 if (types
& ds_type
)
3094 * Given a path to 'target', create all the ancestors between
3095 * the prefixlen portion of the path, and the target itself.
3096 * Fail if the initial prefixlen-ancestor does not already exist.
3099 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3105 /* make sure prefix exists */
3106 cp
= target
+ prefixlen
;
3108 assert(strchr(cp
, '/') == NULL
);
3109 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3112 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3120 * Attempt to create, mount, and share any ancestor filesystems,
3121 * up to the prefixlen-long one.
3123 for (cp
= target
+ prefixlen
+ 1;
3124 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
3128 h
= make_dataset_handle(hdl
, target
);
3130 /* it already exists, nothing to do here */
3135 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3137 opname
= dgettext(TEXT_DOMAIN
, "create");
3141 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3143 opname
= dgettext(TEXT_DOMAIN
, "open");
3147 if (zfs_mount(h
, NULL
, 0) != 0) {
3148 opname
= dgettext(TEXT_DOMAIN
, "mount");
3152 if (zfs_share(h
) != 0) {
3153 opname
= dgettext(TEXT_DOMAIN
, "share");
3163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3164 "failed to %s ancestor '%s'"), opname
, target
);
3169 * Creates non-existing ancestors of the given path.
3172 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3178 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3181 if ((path_copy
= strdup(path
)) != NULL
) {
3182 rc
= create_parents(hdl
, path_copy
, prefix
);
3185 if (path_copy
== NULL
|| rc
!= 0)
3192 * Create a new filesystem or volume.
3195 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3200 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3203 dmu_objset_type_t ost
;
3205 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3206 "cannot create '%s'"), path
);
3208 /* validate the path, taking care to note the extended error message */
3209 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3210 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3212 /* validate parents exist */
3213 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3217 * The failure modes when creating a dataset of a different type over
3218 * one that already exists is a little strange. In particular, if you
3219 * try to create a dataset on top of an existing dataset, the ioctl()
3220 * will return ENOENT, not EEXIST. To prevent this from happening, we
3221 * first try to see if the dataset exists.
3223 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3224 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3225 "dataset already exists"));
3226 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3229 if (type
== ZFS_TYPE_VOLUME
)
3234 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3235 zoned
, NULL
, errbuf
)) == 0)
3238 if (type
== ZFS_TYPE_VOLUME
) {
3240 * If we are creating a volume, the size and block size must
3241 * satisfy a few restraints. First, the blocksize must be a
3242 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3243 * volsize must be a multiple of the block size, and cannot be
3246 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3247 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3249 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3250 "missing volume size"));
3251 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3254 if ((ret
= nvlist_lookup_uint64(props
,
3255 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3256 &blocksize
)) != 0) {
3257 if (ret
== ENOENT
) {
3258 blocksize
= zfs_prop_default_numeric(
3259 ZFS_PROP_VOLBLOCKSIZE
);
3262 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3263 "missing volume block size"));
3264 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3270 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3271 "volume size cannot be zero"));
3272 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3275 if (size
% blocksize
!= 0) {
3277 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3278 "volume size must be a multiple of volume block "
3280 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3284 /* create the dataset */
3285 ret
= lzc_create(path
, ost
, props
);
3288 /* check for failure */
3290 char parent
[ZFS_MAXNAMELEN
];
3293 (void) parent_name(path
, parent
, sizeof (parent
));
3297 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3298 "no such parent '%s'"), parent
);
3299 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3302 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3303 "parent '%s' is not a filesystem"), parent
);
3304 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3307 zfs_nicenum(SPA_MAXBLOCKSIZE
, buf
, sizeof (buf
));
3308 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3309 "volume block size must be power of 2 from "
3310 "512B to %s"), buf
);
3312 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3315 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3316 "pool must be upgraded to set this "
3317 "property or value"));
3318 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3322 * This platform can't address a volume this big.
3324 if (type
== ZFS_TYPE_VOLUME
)
3325 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3330 return (zfs_standard_error(hdl
, errno
, errbuf
));
3338 * Destroys the given dataset. The caller must make sure that the filesystem
3339 * isn't mounted, and that there are no active dependents. If the file system
3340 * does not exist this function does nothing.
3343 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3345 zfs_cmd_t zc
= {"\0"};
3347 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3348 nvlist_t
*nv
= fnvlist_alloc();
3349 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3350 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3353 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3354 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3360 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3362 if (ZFS_IS_VOLUME(zhp
)) {
3363 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3365 zc
.zc_objset_type
= DMU_OST_ZFS
;
3368 zc
.zc_defer_destroy
= defer
;
3369 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3371 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3372 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3376 remove_mountpoint(zhp
);
3381 struct destroydata
{
3383 const char *snapname
;
3387 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3389 struct destroydata
*dd
= arg
;
3390 char name
[ZFS_MAXNAMELEN
];
3393 (void) snprintf(name
, sizeof (name
),
3394 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3396 if (lzc_exists(name
))
3397 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3399 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3405 * Destroys all snapshots with the given name in zhp & descendants.
3408 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3411 struct destroydata dd
= { 0 };
3413 dd
.snapname
= snapname
;
3414 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3415 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3417 if (nvlist_empty(dd
.nvl
)) {
3418 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3419 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3420 zhp
->zfs_name
, snapname
);
3422 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3424 nvlist_free(dd
.nvl
);
3429 * Destroys all the snapshots named in the nvlist.
3432 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3438 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3443 if (nvlist_empty(errlist
)) {
3445 (void) snprintf(errbuf
, sizeof (errbuf
),
3446 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3448 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3450 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3451 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3453 (void) snprintf(errbuf
, sizeof (errbuf
),
3454 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3457 switch (fnvpair_value_int32(pair
)) {
3460 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3461 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3464 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3473 * Clones the given dataset. The target must be of the same type as the source.
3476 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3478 char parent
[ZFS_MAXNAMELEN
];
3481 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3484 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3486 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3487 "cannot create '%s'"), target
);
3489 /* validate the target/clone name */
3490 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3491 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3493 /* validate parents exist */
3494 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3497 (void) parent_name(target
, parent
, sizeof (parent
));
3503 if (ZFS_IS_VOLUME(zhp
)) {
3504 type
= ZFS_TYPE_VOLUME
;
3506 type
= ZFS_TYPE_FILESYSTEM
;
3508 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3509 zhp
, errbuf
)) == NULL
)
3513 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3521 * The parent doesn't exist. We should have caught this
3522 * above, but there may a race condition that has since
3523 * destroyed the parent.
3525 * At this point, we don't know whether it's the source
3526 * that doesn't exist anymore, or whether the target
3527 * dataset doesn't exist.
3529 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3530 "no such parent '%s'"), parent
);
3531 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3534 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3535 "source and target pools differ"));
3536 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3540 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3549 * Promotes the given clone fs to be the clone parent.
3552 zfs_promote(zfs_handle_t
*zhp
)
3554 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3555 zfs_cmd_t zc
= {"\0"};
3556 char parent
[MAXPATHLEN
];
3560 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3561 "cannot promote '%s'"), zhp
->zfs_name
);
3563 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3565 "snapshots can not be promoted"));
3566 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3569 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3570 if (parent
[0] == '\0') {
3571 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3572 "not a cloned filesystem"));
3573 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3576 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3577 sizeof (zc
.zc_value
));
3578 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3579 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3582 int save_errno
= errno
;
3584 switch (save_errno
) {
3586 /* There is a conflicting snapshot name. */
3587 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3588 "conflicting snapshot '%s' from parent '%s'"),
3589 zc
.zc_string
, parent
);
3590 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3593 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3599 typedef struct snapdata
{
3601 const char *sd_snapname
;
3605 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3607 snapdata_t
*sd
= arg
;
3608 char name
[ZFS_MAXNAMELEN
];
3611 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3612 (void) snprintf(name
, sizeof (name
),
3613 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3615 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3617 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3625 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3629 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3636 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3637 "cannot create snapshots "));
3640 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3641 const char *snapname
= nvpair_name(elem
);
3643 /* validate the target name */
3644 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3646 (void) snprintf(errbuf
, sizeof (errbuf
),
3647 dgettext(TEXT_DOMAIN
,
3648 "cannot create snapshot '%s'"), snapname
);
3649 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3653 if (props
!= NULL
&&
3654 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3655 props
, B_FALSE
, NULL
, errbuf
)) == NULL
) {
3659 ret
= lzc_snapshot(snaps
, props
, &errors
);
3662 boolean_t printed
= B_FALSE
;
3663 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3665 elem
= nvlist_next_nvpair(errors
, elem
)) {
3666 (void) snprintf(errbuf
, sizeof (errbuf
),
3667 dgettext(TEXT_DOMAIN
,
3668 "cannot create snapshot '%s'"), nvpair_name(elem
));
3669 (void) zfs_standard_error(hdl
,
3670 fnvpair_value_int32(elem
), errbuf
);
3676 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3677 "multiple snapshots of same "
3679 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3683 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3689 nvlist_free(errors
);
3694 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3698 snapdata_t sd
= { 0 };
3699 char fsname
[ZFS_MAXNAMELEN
];
3704 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3705 "cannot snapshot %s"), path
);
3707 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3708 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3710 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3711 cp
= strchr(fsname
, '@');
3713 sd
.sd_snapname
= cp
+ 1;
3715 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3716 ZFS_TYPE_VOLUME
)) == NULL
) {
3720 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3722 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3724 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3727 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3728 nvlist_free(sd
.sd_nvl
);
3734 * Destroy any more recent snapshots. We invoke this callback on any dependents
3735 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3736 * is a dependent and we should just destroy it without checking the transaction
3739 typedef struct rollback_data
{
3740 const char *cb_target
; /* the snapshot */
3741 uint64_t cb_create
; /* creation time reference */
3747 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3749 rollback_data_t
*cbp
= data
;
3750 prop_changelist_t
*clp
;
3752 /* We must destroy this clone; first unmount it */
3753 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3754 cbp
->cb_force
? MS_FORCE
: 0);
3755 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3756 cbp
->cb_error
= B_TRUE
;
3760 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3761 cbp
->cb_error
= B_TRUE
;
3763 changelist_remove(clp
, zhp
->zfs_name
);
3764 (void) changelist_postfix(clp
);
3765 changelist_free(clp
);
3772 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3774 rollback_data_t
*cbp
= data
;
3776 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3777 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3778 rollback_destroy_dependent
, cbp
);
3780 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3788 * Given a dataset, rollback to a specific snapshot, discarding any
3789 * data changes since then and making it the active dataset.
3791 * Any snapshots and bookmarks more recent than the target are
3792 * destroyed, along with their dependents (i.e. clones).
3795 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3797 rollback_data_t cb
= { 0 };
3799 boolean_t restore_resv
= 0;
3800 uint64_t old_volsize
= 0, new_volsize
;
3801 zfs_prop_t resv_prop
= { 0 };
3803 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3804 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3807 * Destroy all recent snapshots and their dependents.
3809 cb
.cb_force
= force
;
3810 cb
.cb_target
= snap
->zfs_name
;
3811 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3812 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3813 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3819 * Now that we have verified that the snapshot is the latest,
3820 * rollback to the given snapshot.
3823 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3824 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3826 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3828 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3832 * We rely on zfs_iter_children() to verify that there are no
3833 * newer snapshots for the given dataset. Therefore, we can
3834 * simply pass the name on to the ioctl() call. There is still
3835 * an unlikely race condition where the user has taken a
3836 * snapshot since we verified that this was the most recent.
3838 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3840 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3841 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3847 * For volumes, if the pre-rollback volsize matched the pre-
3848 * rollback reservation and the volsize has changed then set
3849 * the reservation property to the post-rollback volsize.
3850 * Make a new handle since the rollback closed the dataset.
3852 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3853 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3855 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3856 if (old_volsize
!= new_volsize
)
3857 err
= zfs_prop_set_int(zhp
, resv_prop
,
3866 * Renames the given dataset.
3869 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
3870 boolean_t force_unmount
)
3873 zfs_cmd_t zc
= {"\0"};
3875 prop_changelist_t
*cl
= NULL
;
3876 zfs_handle_t
*zhrp
= NULL
;
3877 char *parentname
= NULL
;
3878 char parent
[ZFS_MAXNAMELEN
];
3879 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3882 /* if we have the same exact name, just return success */
3883 if (strcmp(zhp
->zfs_name
, target
) == 0)
3886 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3887 "cannot rename to '%s'"), target
);
3890 * Make sure the target name is valid
3892 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3893 if ((strchr(target
, '@') == NULL
) ||
3896 * Snapshot target name is abbreviated,
3897 * reconstruct full dataset name
3899 (void) strlcpy(parent
, zhp
->zfs_name
,
3901 delim
= strchr(parent
, '@');
3902 if (strchr(target
, '@') == NULL
)
3906 (void) strlcat(parent
, target
, sizeof (parent
));
3910 * Make sure we're renaming within the same dataset.
3912 delim
= strchr(target
, '@');
3913 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3914 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3915 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3916 "snapshots must be part of same "
3918 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3922 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3923 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3926 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3927 "recursive rename must be a snapshot"));
3928 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3931 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3932 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3934 /* validate parents */
3935 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3938 /* make sure we're in the same pool */
3939 verify((delim
= strchr(target
, '/')) != NULL
);
3940 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3941 zhp
->zfs_name
[delim
- target
] != '/') {
3942 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3943 "datasets must be within same pool"));
3944 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3947 /* new name cannot be a child of the current dataset name */
3948 if (is_descendant(zhp
->zfs_name
, target
)) {
3949 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3950 "New dataset name cannot be a descendant of "
3951 "current dataset name"));
3952 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3956 (void) snprintf(errbuf
, sizeof (errbuf
),
3957 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3959 if (getzoneid() == GLOBAL_ZONEID
&&
3960 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3961 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3962 "dataset is used in a non-global zone"));
3963 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3968 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3969 if (parentname
== NULL
) {
3973 delim
= strchr(parentname
, '@');
3975 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3982 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3983 force_unmount
? MS_FORCE
: 0)) == NULL
)
3986 if (changelist_haszonedchild(cl
)) {
3987 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3988 "child dataset with inherited mountpoint is used "
3989 "in a non-global zone"));
3990 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3995 if ((ret
= changelist_prefix(cl
)) != 0)
3999 if (ZFS_IS_VOLUME(zhp
))
4000 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4002 zc
.zc_objset_type
= DMU_OST_ZFS
;
4004 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4005 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4007 zc
.zc_cookie
= recursive
;
4009 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4011 * if it was recursive, the one that actually failed will
4014 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4015 "cannot rename '%s'"), zc
.zc_name
);
4017 if (recursive
&& errno
== EEXIST
) {
4018 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4019 "a child dataset already has a snapshot "
4020 "with the new name"));
4021 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4023 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4027 * On failure, we still want to remount any filesystems that
4028 * were previously mounted, so we don't alter the system state.
4031 (void) changelist_postfix(cl
);
4034 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4035 ret
= changelist_postfix(cl
);
4047 changelist_free(cl
);
4053 zfs_get_user_props(zfs_handle_t
*zhp
)
4055 return (zhp
->zfs_user_props
);
4059 * This function is used by 'zfs list' to determine the exact set of columns to
4060 * display, and their maximum widths. This does two main things:
4062 * - If this is a list of all properties, then expand the list to include
4063 * all native properties, and set a flag so that for each dataset we look
4064 * for new unique user properties and add them to the list.
4066 * - For non fixed-width properties, keep track of the maximum width seen
4067 * so that we can size the column appropriately. If the user has
4068 * requested received property values, we also need to compute the width
4069 * of the RECEIVED column.
4072 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4075 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4076 zprop_list_t
*entry
;
4077 zprop_list_t
**last
, **start
;
4078 nvlist_t
*userprops
, *propval
;
4081 char buf
[ZFS_MAXPROPLEN
];
4083 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4086 userprops
= zfs_get_user_props(zhp
);
4089 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4091 * Go through and add any user properties as necessary. We
4092 * start by incrementing our list pointer to the first
4093 * non-native property.
4096 while (*start
!= NULL
) {
4097 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4099 start
= &(*start
)->pl_next
;
4103 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4105 * See if we've already found this property in our list.
4107 for (last
= start
; *last
!= NULL
;
4108 last
= &(*last
)->pl_next
) {
4109 if (strcmp((*last
)->pl_user_prop
,
4110 nvpair_name(elem
)) == 0)
4114 if (*last
== NULL
) {
4115 if ((entry
= zfs_alloc(hdl
,
4116 sizeof (zprop_list_t
))) == NULL
||
4117 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4118 nvpair_name(elem
)))) == NULL
) {
4123 entry
->pl_prop
= ZPROP_INVAL
;
4124 entry
->pl_width
= strlen(nvpair_name(elem
));
4125 entry
->pl_all
= B_TRUE
;
4132 * Now go through and check the width of any non-fixed columns
4134 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4135 if (entry
->pl_fixed
&& !literal
)
4138 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4139 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4140 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4141 if (strlen(buf
) > entry
->pl_width
)
4142 entry
->pl_width
= strlen(buf
);
4144 if (received
&& zfs_prop_get_recvd(zhp
,
4145 zfs_prop_to_name(entry
->pl_prop
),
4146 buf
, sizeof (buf
), literal
) == 0)
4147 if (strlen(buf
) > entry
->pl_recvd_width
)
4148 entry
->pl_recvd_width
= strlen(buf
);
4150 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4152 verify(nvlist_lookup_string(propval
,
4153 ZPROP_VALUE
, &strval
) == 0);
4154 if (strlen(strval
) > entry
->pl_width
)
4155 entry
->pl_width
= strlen(strval
);
4157 if (received
&& zfs_prop_get_recvd(zhp
,
4158 entry
->pl_user_prop
,
4159 buf
, sizeof (buf
), literal
) == 0)
4160 if (strlen(buf
) > entry
->pl_recvd_width
)
4161 entry
->pl_recvd_width
= strlen(buf
);
4169 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4175 * Keep a reference to the props-table against which we prune the
4178 zhp
->zfs_props_table
= props
;
4180 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4183 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4184 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4187 * User properties will result in ZPROP_INVAL, and since we
4188 * only know how to prune standard ZFS properties, we always
4189 * leave these in the list. This can also happen if we
4190 * encounter an unknown DSL property (when running older
4191 * software, for example).
4193 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4194 (void) nvlist_remove(zhp
->zfs_props
,
4195 nvpair_name(curr
), nvpair_type(curr
));
4201 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4202 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4204 zfs_cmd_t zc
= {"\0"};
4205 nvlist_t
*nvlist
= NULL
;
4208 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4209 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4210 zc
.zc_cookie
= (uint64_t)cmd
;
4212 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4213 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4214 (void) no_memory(hdl
);
4220 case ZFS_SMB_ACL_ADD
:
4221 case ZFS_SMB_ACL_REMOVE
:
4222 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4224 case ZFS_SMB_ACL_RENAME
:
4225 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4227 (void) no_memory(hdl
);
4230 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4232 (void) no_memory(hdl
);
4235 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4236 nvlist_free(nvlist
);
4240 case ZFS_SMB_ACL_PURGE
:
4245 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4247 nvlist_free(nvlist
);
4252 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4253 char *path
, char *resource
)
4255 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4260 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4261 char *path
, char *resource
)
4263 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4268 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4270 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4275 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4276 char *oldname
, char *newname
)
4278 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4283 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4284 zfs_userspace_cb_t func
, void *arg
)
4286 zfs_cmd_t zc
= {"\0"};
4287 zfs_useracct_t buf
[100];
4288 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4291 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4293 zc
.zc_objset_type
= type
;
4294 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4297 zfs_useracct_t
*zua
= buf
;
4299 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4300 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4301 char errbuf
[ZFS_MAXNAMELEN
+ 32];
4303 (void) snprintf(errbuf
, sizeof (errbuf
),
4304 dgettext(TEXT_DOMAIN
,
4305 "cannot get used/quota for %s"), zc
.zc_name
);
4306 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4308 if (zc
.zc_nvlist_dst_size
== 0)
4311 while (zc
.zc_nvlist_dst_size
> 0) {
4312 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4313 zua
->zu_space
)) != 0)
4316 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4325 const char *snapname
;
4327 boolean_t recursive
;
4332 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4334 struct holdarg
*ha
= arg
;
4335 char name
[ZFS_MAXNAMELEN
];
4338 (void) snprintf(name
, sizeof (name
),
4339 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4341 if (lzc_exists(name
))
4342 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4345 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4351 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4352 boolean_t recursive
, int cleanup_fd
)
4357 ha
.nvl
= fnvlist_alloc();
4358 ha
.snapname
= snapname
;
4360 ha
.recursive
= recursive
;
4361 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4363 if (nvlist_empty(ha
.nvl
)) {
4366 fnvlist_free(ha
.nvl
);
4368 (void) snprintf(errbuf
, sizeof (errbuf
),
4369 dgettext(TEXT_DOMAIN
,
4370 "cannot hold snapshot '%s@%s'"),
4371 zhp
->zfs_name
, snapname
);
4372 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4376 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4377 fnvlist_free(ha
.nvl
);
4383 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4387 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4392 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4395 /* There may be errors even in the success case. */
4396 fnvlist_free(errors
);
4400 if (nvlist_empty(errors
)) {
4401 /* no hold-specific errors */
4402 (void) snprintf(errbuf
, sizeof (errbuf
),
4403 dgettext(TEXT_DOMAIN
, "cannot hold"));
4406 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4407 "pool must be upgraded"));
4408 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4411 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4414 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4418 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4420 elem
= nvlist_next_nvpair(errors
, elem
)) {
4421 (void) snprintf(errbuf
, sizeof (errbuf
),
4422 dgettext(TEXT_DOMAIN
,
4423 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4424 switch (fnvpair_value_int32(elem
)) {
4427 * Temporary tags wind up having the ds object id
4428 * prepended. So even if we passed the length check
4429 * above, it's still possible for the tag to wind
4430 * up being slightly too long.
4432 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4435 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4438 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4441 (void) zfs_standard_error(hdl
,
4442 fnvpair_value_int32(elem
), errbuf
);
4446 fnvlist_free(errors
);
4451 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4453 struct holdarg
*ha
= arg
;
4454 char name
[ZFS_MAXNAMELEN
];
4456 nvlist_t
*existing_holds
;
4458 (void) snprintf(name
, sizeof (name
),
4459 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4461 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4463 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4466 nvlist_t
*torelease
= fnvlist_alloc();
4467 fnvlist_add_boolean(torelease
, ha
->tag
);
4468 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4469 fnvlist_free(torelease
);
4473 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4479 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4480 boolean_t recursive
)
4484 nvlist_t
*errors
= NULL
;
4486 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4489 ha
.nvl
= fnvlist_alloc();
4490 ha
.snapname
= snapname
;
4492 ha
.recursive
= recursive
;
4494 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4496 if (nvlist_empty(ha
.nvl
)) {
4497 fnvlist_free(ha
.nvl
);
4499 (void) snprintf(errbuf
, sizeof (errbuf
),
4500 dgettext(TEXT_DOMAIN
,
4501 "cannot release hold from snapshot '%s@%s'"),
4502 zhp
->zfs_name
, snapname
);
4504 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4506 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4511 ret
= lzc_release(ha
.nvl
, &errors
);
4512 fnvlist_free(ha
.nvl
);
4515 /* There may be errors even in the success case. */
4516 fnvlist_free(errors
);
4520 if (nvlist_empty(errors
)) {
4521 /* no hold-specific errors */
4522 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4527 "pool must be upgraded"));
4528 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4531 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4535 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4537 elem
= nvlist_next_nvpair(errors
, elem
)) {
4538 (void) snprintf(errbuf
, sizeof (errbuf
),
4539 dgettext(TEXT_DOMAIN
,
4540 "cannot release hold from snapshot '%s'"),
4542 switch (fnvpair_value_int32(elem
)) {
4544 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4547 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4550 (void) zfs_standard_error_fmt(hdl
,
4551 fnvpair_value_int32(elem
), errbuf
);
4555 fnvlist_free(errors
);
4560 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4562 zfs_cmd_t zc
= {"\0"};
4563 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4569 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4570 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4574 nvbuf
= malloc(nvsz
);
4575 if (nvbuf
== NULL
) {
4576 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4580 zc
.zc_nvlist_dst_size
= nvsz
;
4581 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4583 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4585 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4586 (void) snprintf(errbuf
, sizeof (errbuf
),
4587 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4592 nvsz
= zc
.zc_nvlist_dst_size
;
4596 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4597 "pool must be upgraded"));
4598 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4601 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4604 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4607 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4612 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4614 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4615 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4617 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4627 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4629 zfs_cmd_t zc
= {"\0"};
4630 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4636 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4637 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4639 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4642 nvbuf
= malloc(nvsz
);
4644 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4647 zc
.zc_nvlist_src_size
= nvsz
;
4648 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4649 zc
.zc_perm_action
= un
;
4651 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4653 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4654 (void) snprintf(errbuf
, sizeof (errbuf
),
4655 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4659 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4660 "pool must be upgraded"));
4661 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4664 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4667 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4670 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4681 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4686 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4689 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4691 (void) snprintf(errbuf
, sizeof (errbuf
),
4692 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4696 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4697 "pool must be upgraded"));
4698 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4701 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4704 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4707 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4716 * Convert the zvol's volume size to an appropriate reservation.
4717 * Note: If this routine is updated, it is necessary to update the ZFS test
4718 * suite's shell version in reservation.kshlib.
4721 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4724 uint64_t nblocks
, volblocksize
;
4728 if (nvlist_lookup_string(props
,
4729 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4730 ncopies
= atoi(strval
);
4733 if (nvlist_lookup_uint64(props
,
4734 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4735 &volblocksize
) != 0)
4736 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4737 nblocks
= volsize
/volblocksize
;
4738 /* start with metadnode L0-L6 */
4740 /* calculate number of indirects */
4741 while (nblocks
> 1) {
4742 nblocks
+= DNODES_PER_LEVEL
- 1;
4743 nblocks
/= DNODES_PER_LEVEL
;
4746 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4749 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4750 * compressed, but in practice they compress down to about
4753 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;