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) 2011, 2015 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>
53 #include <directory.h>
54 #endif /* HAVE_IDMAP */
56 #include <sys/dnode.h>
61 #include "zfs_namecheck.h"
63 #include "libzfs_impl.h"
64 #include "zfs_deleg.h"
66 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
67 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
70 * Given a single type (not a mask of types), return the type in a human
74 zfs_type_to_name(zfs_type_t type
)
77 case ZFS_TYPE_FILESYSTEM
:
78 return (dgettext(TEXT_DOMAIN
, "filesystem"));
79 case ZFS_TYPE_SNAPSHOT
:
80 return (dgettext(TEXT_DOMAIN
, "snapshot"));
82 return (dgettext(TEXT_DOMAIN
, "volume"));
91 * Validate a ZFS path. This is used even before trying to open the dataset, to
92 * provide a more meaningful error message. We call zfs_error_aux() to
93 * explain exactly why the name was not valid.
96 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
102 (void) zfs_prop_get_table();
103 if (dataset_namecheck(path
, &why
, &what
) != 0) {
106 case NAME_ERR_TOOLONG
:
107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
108 "name is too long"));
111 case NAME_ERR_LEADING_SLASH
:
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "leading slash in name"));
116 case NAME_ERR_EMPTY_COMPONENT
:
117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
118 "empty component in name"));
121 case NAME_ERR_TRAILING_SLASH
:
122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
123 "trailing slash in name"));
126 case NAME_ERR_INVALCHAR
:
128 dgettext(TEXT_DOMAIN
, "invalid character "
129 "'%c' in name"), what
);
132 case NAME_ERR_MULTIPLE_AT
:
133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
134 "multiple '@' delimiters in name"));
137 case NAME_ERR_NOLETTER
:
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "pool doesn't begin with a letter"));
142 case NAME_ERR_RESERVED
:
143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
144 "name is reserved"));
147 case NAME_ERR_DISKLIKE
:
148 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
149 "reserved disk name"));
159 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
162 "snapshot delimiter '@' in filesystem name"));
166 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
168 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
169 "missing '@' delimiter in snapshot name"));
173 if (modifying
&& strchr(path
, '%') != NULL
) {
175 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
176 "invalid character %c in name"), '%');
184 zfs_name_valid(const char *name
, zfs_type_t type
)
186 if (type
== ZFS_TYPE_POOL
)
187 return (zpool_name_valid(NULL
, B_FALSE
, name
));
188 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
192 * This function takes the raw DSL properties, and filters out the user-defined
193 * properties into a separate nvlist.
196 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
198 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
203 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
204 (void) no_memory(hdl
);
209 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
210 if (!zfs_prop_user(nvpair_name(elem
)))
213 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
214 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
216 (void) no_memory(hdl
);
224 static zpool_handle_t
*
225 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
227 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
230 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
231 if (hdl
->libzfs_pool_handles
!= NULL
)
232 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
233 hdl
->libzfs_pool_handles
= zph
;
238 static zpool_handle_t
*
239 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
241 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
242 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
244 while ((zph
!= NULL
) &&
245 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
246 zph
= zph
->zpool_next
;
251 * Returns a handle to the pool that contains the provided dataset.
252 * If a handle to that pool already exists then that handle is returned.
253 * Otherwise, a new handle is created and added to the list of handles.
255 static zpool_handle_t
*
256 zpool_handle(zfs_handle_t
*zhp
)
262 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
263 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
264 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
266 zph
= zpool_find_handle(zhp
, pool_name
, len
);
268 zph
= zpool_add_handle(zhp
, pool_name
);
275 zpool_free_handles(libzfs_handle_t
*hdl
)
277 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
279 while (zph
!= NULL
) {
280 next
= zph
->zpool_next
;
284 hdl
->libzfs_pool_handles
= NULL
;
288 * Utility function to gather stats (objset and zpl) for the given object.
291 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
293 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
295 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
297 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
298 if (errno
== ENOMEM
) {
299 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
310 * Utility function to get the received properties of the given object.
313 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
315 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
316 nvlist_t
*recvdprops
;
317 zfs_cmd_t zc
= {"\0"};
320 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
323 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
325 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
326 if (errno
== ENOMEM
) {
327 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
331 zcmd_free_nvlists(&zc
);
336 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
337 zcmd_free_nvlists(&zc
);
341 nvlist_free(zhp
->zfs_recvd_props
);
342 zhp
->zfs_recvd_props
= recvdprops
;
348 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
350 nvlist_t
*allprops
, *userprops
;
352 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
354 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
359 * XXX Why do we store the user props separately, in addition to
360 * storing them in zfs_props?
362 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
363 nvlist_free(allprops
);
367 nvlist_free(zhp
->zfs_props
);
368 nvlist_free(zhp
->zfs_user_props
);
370 zhp
->zfs_props
= allprops
;
371 zhp
->zfs_user_props
= userprops
;
377 get_stats(zfs_handle_t
*zhp
)
380 zfs_cmd_t zc
= {"\0"};
382 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
384 if (get_stats_ioctl(zhp
, &zc
) != 0)
386 else if (put_stats_zhdl(zhp
, &zc
) != 0)
388 zcmd_free_nvlists(&zc
);
393 * Refresh the properties currently stored in the handle.
396 zfs_refresh_properties(zfs_handle_t
*zhp
)
398 (void) get_stats(zhp
);
402 * Makes a handle from the given dataset name. Used by zfs_open() and
403 * zfs_iter_* to create child handles on the fly.
406 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
408 if (put_stats_zhdl(zhp
, zc
) != 0)
412 * We've managed to open the dataset and gather statistics. Determine
413 * the high-level type.
415 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
416 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
417 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
418 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
419 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
424 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
425 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
426 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
427 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
428 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
429 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
431 abort(); /* we should never see any other types */
433 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
440 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
442 zfs_cmd_t zc
= {"\0"};
444 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
450 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
451 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
455 if (get_stats_ioctl(zhp
, &zc
) == -1) {
456 zcmd_free_nvlists(&zc
);
460 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
464 zcmd_free_nvlists(&zc
);
469 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
471 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
477 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
478 if (make_dataset_handle_common(zhp
, zc
) == -1) {
486 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
488 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
493 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
494 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
495 zhp
->zfs_head_type
= pzhp
->zfs_type
;
496 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
497 zhp
->zpool_hdl
= zpool_handle(zhp
);
503 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
505 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
510 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
511 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
512 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
513 sizeof (zhp
->zfs_name
));
514 zhp
->zfs_type
= zhp_orig
->zfs_type
;
515 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
516 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
517 if (zhp_orig
->zfs_props
!= NULL
) {
518 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
519 (void) no_memory(zhp
->zfs_hdl
);
524 if (zhp_orig
->zfs_user_props
!= NULL
) {
525 if (nvlist_dup(zhp_orig
->zfs_user_props
,
526 &zhp
->zfs_user_props
, 0) != 0) {
527 (void) no_memory(zhp
->zfs_hdl
);
532 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
533 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
534 &zhp
->zfs_recvd_props
, 0)) {
535 (void) no_memory(zhp
->zfs_hdl
);
540 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
541 if (zhp_orig
->zfs_mntopts
!= NULL
) {
542 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
543 zhp_orig
->zfs_mntopts
);
545 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
550 zfs_bookmark_exists(const char *path
)
554 char fsname
[ZFS_MAXNAMELEN
];
561 (void) strlcpy(fsname
, path
, sizeof (fsname
));
562 pound
= strchr(fsname
, '#');
567 bmark_name
= pound
+ 1;
568 props
= fnvlist_alloc();
569 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
576 rv
= nvlist_exists(bmarks
, bmark_name
);
582 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
583 nvlist_t
*bmark_props
)
585 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
590 /* Fill in the name. */
591 zhp
->zfs_hdl
= parent
->zfs_hdl
;
592 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
594 /* Set the property lists. */
595 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
601 zhp
->zfs_head_type
= parent
->zfs_head_type
;
602 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
604 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
605 nvlist_free(zhp
->zfs_props
);
614 * Opens the given snapshot, filesystem, or volume. The 'types'
615 * argument is a mask of acceptable types. The function will print an
616 * appropriate error message and return NULL if it can't be opened.
619 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
624 (void) snprintf(errbuf
, sizeof (errbuf
),
625 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
628 * Validate the name before we even try to open it.
630 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
632 "invalid dataset name"));
633 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
638 * Try to get stats for the dataset, which will tell us if it exists.
641 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
642 (void) zfs_standard_error(hdl
, errno
, errbuf
);
646 if (!(types
& zhp
->zfs_type
)) {
647 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
656 * Release a ZFS handle. Nothing to do but free the associated memory.
659 zfs_close(zfs_handle_t
*zhp
)
661 if (zhp
->zfs_mntopts
)
662 free(zhp
->zfs_mntopts
);
663 nvlist_free(zhp
->zfs_props
);
664 nvlist_free(zhp
->zfs_user_props
);
665 nvlist_free(zhp
->zfs_recvd_props
);
669 typedef struct mnttab_node
{
670 struct mnttab mtn_mt
;
675 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
677 const mnttab_node_t
*mtn1
= arg1
;
678 const mnttab_node_t
*mtn2
= arg2
;
681 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
685 return (rv
> 0 ? 1 : -1);
689 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
691 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
692 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
693 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
697 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
701 /* Reopen MNTTAB to prevent reading stale data from open file */
702 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
705 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
709 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
712 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
713 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
714 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
715 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
716 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
718 /* Exclude duplicate mounts */
719 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
720 free(mtn
->mtn_mt
.mnt_special
);
721 free(mtn
->mtn_mt
.mnt_mountp
);
722 free(mtn
->mtn_mt
.mnt_fstype
);
723 free(mtn
->mtn_mt
.mnt_mntopts
);
728 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
735 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
740 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
741 free(mtn
->mtn_mt
.mnt_special
);
742 free(mtn
->mtn_mt
.mnt_mountp
);
743 free(mtn
->mtn_mt
.mnt_fstype
);
744 free(mtn
->mtn_mt
.mnt_mntopts
);
747 avl_destroy(&hdl
->libzfs_mnttab_cache
);
751 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
753 hdl
->libzfs_mnttab_enable
= enable
;
757 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
758 struct mnttab
*entry
)
764 if (!hdl
->libzfs_mnttab_enable
) {
765 struct mnttab srch
= { 0 };
767 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
768 libzfs_mnttab_fini(hdl
);
770 /* Reopen MNTTAB to prevent reading stale data from open file */
771 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
774 srch
.mnt_special
= (char *)fsname
;
775 srch
.mnt_fstype
= MNTTYPE_ZFS
;
776 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
782 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
783 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
786 find
.mtn_mt
.mnt_special
= (char *)fsname
;
787 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
789 *entry
= mtn
->mtn_mt
;
796 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
797 const char *mountp
, const char *mntopts
)
801 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
803 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
804 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
805 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
806 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
807 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
808 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
812 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
817 find
.mtn_mt
.mnt_special
= (char *)fsname
;
818 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
819 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
820 free(ret
->mtn_mt
.mnt_special
);
821 free(ret
->mtn_mt
.mnt_mountp
);
822 free(ret
->mtn_mt
.mnt_fstype
);
823 free(ret
->mtn_mt
.mnt_mntopts
);
829 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
831 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
833 if (zpool_handle
== NULL
)
836 *spa_version
= zpool_get_prop_int(zpool_handle
,
837 ZPOOL_PROP_VERSION
, NULL
);
842 * The choice of reservation property depends on the SPA version.
845 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
849 if (zfs_spa_version(zhp
, &spa_version
) < 0)
852 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
853 *resv_prop
= ZFS_PROP_REFRESERVATION
;
855 *resv_prop
= ZFS_PROP_RESERVATION
;
861 * Given an nvlist of properties to set, validates that they are correct, and
862 * parses any numeric properties (index, boolean, etc) if they are specified as
866 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
867 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
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
;
1064 if (zpool_hdl
!= NULL
) {
1065 maxbs
= zpool_get_prop_int(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
;
1385 * If this is an existing volume, and someone is setting the volsize,
1386 * make sure that it matches the reservation, or add it if necessary.
1388 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1389 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1391 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1393 props
= fnvlist_alloc();
1394 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1395 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1397 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1398 old_reservation
) || nvlist_exists(nvl
,
1399 zfs_prop_to_name(resv_prop
))) {
1400 fnvlist_free(props
);
1403 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1404 &new_volsize
) != 0) {
1405 fnvlist_free(props
);
1408 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1409 fnvlist_free(props
);
1411 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1412 new_reservation
) != 0) {
1413 (void) no_memory(zhp
->zfs_hdl
);
1420 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1427 * For quotas and reservations, ENOSPC indicates
1428 * something different; setting a quota or reservation
1429 * doesn't use any disk space.
1432 case ZFS_PROP_QUOTA
:
1433 case ZFS_PROP_REFQUOTA
:
1434 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1435 "size is less than current used or "
1437 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1440 case ZFS_PROP_RESERVATION
:
1441 case ZFS_PROP_REFRESERVATION
:
1442 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1443 "size is greater than available space"));
1444 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1448 (void) zfs_standard_error(hdl
, err
, errbuf
);
1454 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1458 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1462 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1463 "property value too long"));
1464 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1468 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1469 "pool and or dataset must be upgraded to set this "
1470 "property or value"));
1471 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1475 if (prop
== ZFS_PROP_COMPRESSION
||
1476 prop
== ZFS_PROP_RECORDSIZE
) {
1477 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1478 "property setting is not allowed on "
1479 "bootable datasets"));
1480 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1482 (void) zfs_standard_error(hdl
, err
, errbuf
);
1487 if (prop
== ZPROP_INVAL
) {
1488 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1490 (void) zfs_standard_error(hdl
, err
, errbuf
);
1496 * This platform can't address a volume this big.
1499 if (prop
== ZFS_PROP_VOLSIZE
) {
1500 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1506 (void) zfs_standard_error(hdl
, err
, errbuf
);
1511 zfs_is_namespace_prop(zfs_prop_t prop
)
1515 case ZFS_PROP_ATIME
:
1516 case ZFS_PROP_RELATIME
:
1517 case ZFS_PROP_DEVICES
:
1519 case ZFS_PROP_SETUID
:
1520 case ZFS_PROP_READONLY
:
1521 case ZFS_PROP_XATTR
:
1522 case ZFS_PROP_NBMAND
:
1531 * Given a property name and value, set the property for the given dataset.
1534 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1538 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1539 nvlist_t
*nvl
= NULL
;
1541 (void) snprintf(errbuf
, sizeof (errbuf
),
1542 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1545 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1546 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1547 (void) no_memory(hdl
);
1551 ret
= zfs_prop_set_list(zhp
, nvl
);
1561 * Given an nvlist of property names and values, set the properties for the
1565 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1567 zfs_cmd_t zc
= {"\0"};
1569 prop_changelist_t
**cls
= NULL
;
1572 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1576 zfs_prop_t prop
= 0;
1579 (void) snprintf(errbuf
, sizeof (errbuf
),
1580 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1583 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1584 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1589 * We have to check for any extra properties which need to be added
1590 * before computing the length of the nvlist.
1592 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1594 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1595 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1596 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1601 * Check how many properties we're setting and allocate an array to
1602 * store changelist pointers for postfix().
1605 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1607 elem
= nvlist_next_nvpair(nvl
, elem
))
1609 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1613 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1615 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1617 prop
= zfs_name_to_prop(nvpair_name(elem
));
1619 assert(cl_idx
< nvl_len
);
1621 * We don't want to unmount & remount the dataset when changing
1622 * its canmount property to 'on' or 'noauto'. We only use
1623 * the changelist logic to unmount when setting canmount=off.
1625 if (!(prop
== ZFS_PROP_CANMOUNT
&&
1626 fnvpair_value_uint64(elem
) != ZFS_CANMOUNT_OFF
)) {
1627 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1628 if (cls
[cl_idx
] == NULL
)
1632 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1633 changelist_haszonedchild(cls
[cl_idx
])) {
1634 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1635 "child dataset with inherited mountpoint is used "
1636 "in a non-global zone"));
1637 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1641 if (cls
[cl_idx
] != NULL
&&
1642 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1647 assert(cl_idx
== nvl_len
);
1650 * Execute the corresponding ioctl() to set this list of properties.
1652 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1654 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1655 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1658 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1661 /* Get the list of unset properties back and report them. */
1662 nvlist_t
*errorprops
= NULL
;
1663 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1665 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1667 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1668 prop
= zfs_name_to_prop(nvpair_name(elem
));
1669 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1671 nvlist_free(errorprops
);
1673 if (added_resv
&& errno
== ENOSPC
) {
1674 /* clean up the volsize property we tried to set */
1675 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1679 zcmd_free_nvlists(&zc
);
1681 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1683 if (nvlist_add_uint64(nvl
,
1684 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1687 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1689 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1692 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1693 if (cls
[cl_idx
] != NULL
) {
1694 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1702 * Refresh the statistics so the new property
1703 * value is reflected.
1705 (void) get_stats(zhp
);
1708 * Remount the filesystem to propagate the change
1709 * if one of the options handled by the generic
1710 * Linux namespace layer has been modified.
1712 if (zfs_is_namespace_prop(prop
) &&
1713 zfs_is_mounted(zhp
, NULL
))
1714 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1720 zcmd_free_nvlists(&zc
);
1722 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1723 if (cls
[cl_idx
] != NULL
)
1724 changelist_free(cls
[cl_idx
]);
1732 * Given a property, inherit the value from the parent dataset, or if received
1733 * is TRUE, revert to the received value, if any.
1736 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1738 zfs_cmd_t zc
= {"\0"};
1740 prop_changelist_t
*cl
;
1741 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1745 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1746 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1748 zc
.zc_cookie
= received
;
1749 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1751 * For user properties, the amount of work we have to do is very
1752 * small, so just do it here.
1754 if (!zfs_prop_user(propname
)) {
1755 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1756 "invalid property"));
1757 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1760 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1761 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1763 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1764 return (zfs_standard_error(hdl
, errno
, errbuf
));
1770 * Verify that this property is inheritable.
1772 if (zfs_prop_readonly(prop
))
1773 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1775 if (!zfs_prop_inheritable(prop
) && !received
)
1776 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1779 * Check to see if the value applies to this type
1781 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1782 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1785 * Normalize the name, to get rid of shorthand abbreviations.
1787 propname
= zfs_prop_to_name(prop
);
1788 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1789 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1791 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1792 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1793 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1794 "dataset is used in a non-global zone"));
1795 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1799 * Determine datasets which will be affected by this change, if any.
1801 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1804 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1805 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1806 "child dataset with inherited mountpoint is used "
1807 "in a non-global zone"));
1808 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1812 if ((ret
= changelist_prefix(cl
)) != 0)
1815 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1816 return (zfs_standard_error(hdl
, errno
, errbuf
));
1819 if ((ret
= changelist_postfix(cl
)) != 0)
1823 * Refresh the statistics so the new property is reflected.
1825 (void) get_stats(zhp
);
1828 * Remount the filesystem to propagate the change
1829 * if one of the options handled by the generic
1830 * Linux namespace layer has been modified.
1832 if (zfs_is_namespace_prop(prop
) &&
1833 zfs_is_mounted(zhp
, NULL
))
1834 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1838 changelist_free(cl
);
1843 * True DSL properties are stored in an nvlist. The following two functions
1844 * extract them appropriately.
1847 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1853 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1854 zfs_prop_to_name(prop
), &nv
) == 0) {
1855 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1856 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1858 verify(!zhp
->zfs_props_table
||
1859 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1860 value
= zfs_prop_default_numeric(prop
);
1868 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1874 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1875 zfs_prop_to_name(prop
), &nv
) == 0) {
1876 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1877 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1879 verify(!zhp
->zfs_props_table
||
1880 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1881 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1890 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1892 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1896 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1898 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1899 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1903 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1905 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1910 * Internal function for getting a numeric property. Both zfs_prop_get() and
1911 * zfs_prop_get_int() are built using this interface.
1913 * Certain properties can be overridden using 'mount -o'. In this case, scan
1914 * the contents of the /etc/mtab entry, searching for the appropriate options.
1915 * If they differ from the on-disk values, report the current values and mark
1916 * the source "temporary".
1919 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1920 char **source
, uint64_t *val
)
1922 zfs_cmd_t zc
= {"\0"};
1923 nvlist_t
*zplprops
= NULL
;
1925 char *mntopt_on
= NULL
;
1926 char *mntopt_off
= NULL
;
1927 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1932 * If the property is being fetched for a snapshot, check whether
1933 * the property is valid for the snapshot's head dataset type.
1935 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
1936 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
1937 *val
= zfs_prop_default_numeric(prop
);
1942 case ZFS_PROP_ATIME
:
1943 mntopt_on
= MNTOPT_ATIME
;
1944 mntopt_off
= MNTOPT_NOATIME
;
1947 case ZFS_PROP_RELATIME
:
1948 mntopt_on
= MNTOPT_RELATIME
;
1949 mntopt_off
= MNTOPT_NORELATIME
;
1952 case ZFS_PROP_DEVICES
:
1953 mntopt_on
= MNTOPT_DEVICES
;
1954 mntopt_off
= MNTOPT_NODEVICES
;
1958 mntopt_on
= MNTOPT_EXEC
;
1959 mntopt_off
= MNTOPT_NOEXEC
;
1962 case ZFS_PROP_READONLY
:
1963 mntopt_on
= MNTOPT_RO
;
1964 mntopt_off
= MNTOPT_RW
;
1967 case ZFS_PROP_SETUID
:
1968 mntopt_on
= MNTOPT_SETUID
;
1969 mntopt_off
= MNTOPT_NOSETUID
;
1972 case ZFS_PROP_XATTR
:
1973 mntopt_on
= MNTOPT_XATTR
;
1974 mntopt_off
= MNTOPT_NOXATTR
;
1977 case ZFS_PROP_NBMAND
:
1978 mntopt_on
= MNTOPT_NBMAND
;
1979 mntopt_off
= MNTOPT_NONBMAND
;
1986 * Because looking up the mount options is potentially expensive
1987 * (iterating over all of /etc/mtab), we defer its calculation until
1988 * we're looking up a property which requires its presence.
1990 if (!zhp
->zfs_mntcheck
&&
1991 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1992 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1993 struct mnttab entry
;
1995 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1996 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1998 if (zhp
->zfs_mntopts
== NULL
)
2002 zhp
->zfs_mntcheck
= B_TRUE
;
2005 if (zhp
->zfs_mntopts
== NULL
)
2006 mnt
.mnt_mntopts
= "";
2008 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2011 case ZFS_PROP_ATIME
:
2012 case ZFS_PROP_RELATIME
:
2013 case ZFS_PROP_DEVICES
:
2015 case ZFS_PROP_READONLY
:
2016 case ZFS_PROP_SETUID
:
2017 case ZFS_PROP_XATTR
:
2018 case ZFS_PROP_NBMAND
:
2019 *val
= getprop_uint64(zhp
, prop
, source
);
2024 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2027 *src
= ZPROP_SRC_TEMPORARY
;
2028 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2031 *src
= ZPROP_SRC_TEMPORARY
;
2035 case ZFS_PROP_CANMOUNT
:
2036 case ZFS_PROP_VOLSIZE
:
2037 case ZFS_PROP_QUOTA
:
2038 case ZFS_PROP_REFQUOTA
:
2039 case ZFS_PROP_RESERVATION
:
2040 case ZFS_PROP_REFRESERVATION
:
2041 case ZFS_PROP_FILESYSTEM_LIMIT
:
2042 case ZFS_PROP_SNAPSHOT_LIMIT
:
2043 case ZFS_PROP_FILESYSTEM_COUNT
:
2044 case ZFS_PROP_SNAPSHOT_COUNT
:
2045 *val
= getprop_uint64(zhp
, prop
, source
);
2047 if (*source
== NULL
) {
2048 /* not default, must be local */
2049 *source
= zhp
->zfs_name
;
2053 case ZFS_PROP_MOUNTED
:
2054 *val
= (zhp
->zfs_mntopts
!= NULL
);
2057 case ZFS_PROP_NUMCLONES
:
2058 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2061 case ZFS_PROP_VERSION
:
2062 case ZFS_PROP_NORMALIZE
:
2063 case ZFS_PROP_UTF8ONLY
:
2065 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2067 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2068 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2069 zcmd_free_nvlists(&zc
);
2070 if (prop
== ZFS_PROP_VERSION
&&
2071 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2072 *val
= zfs_prop_default_numeric(prop
);
2075 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2076 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2078 zcmd_free_nvlists(&zc
);
2081 nvlist_free(zplprops
);
2082 zcmd_free_nvlists(&zc
);
2085 case ZFS_PROP_INCONSISTENT
:
2086 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2090 switch (zfs_prop_get_type(prop
)) {
2091 case PROP_TYPE_NUMBER
:
2092 case PROP_TYPE_INDEX
:
2093 *val
= getprop_uint64(zhp
, prop
, source
);
2095 * If we tried to use a default value for a
2096 * readonly property, it means that it was not
2099 if (zfs_prop_readonly(prop
) &&
2100 *source
!= NULL
&& (*source
)[0] == '\0') {
2105 case PROP_TYPE_STRING
:
2107 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2108 "cannot get non-numeric property"));
2109 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2110 dgettext(TEXT_DOMAIN
, "internal error")));
2118 * Calculate the source type, given the raw source string.
2121 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2122 char *statbuf
, size_t statlen
)
2124 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2127 if (source
== NULL
) {
2128 *srctype
= ZPROP_SRC_NONE
;
2129 } else if (source
[0] == '\0') {
2130 *srctype
= ZPROP_SRC_DEFAULT
;
2131 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2132 *srctype
= ZPROP_SRC_RECEIVED
;
2134 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2135 *srctype
= ZPROP_SRC_LOCAL
;
2137 (void) strlcpy(statbuf
, source
, statlen
);
2138 *srctype
= ZPROP_SRC_INHERITED
;
2145 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2146 size_t proplen
, boolean_t literal
)
2151 if (zhp
->zfs_recvd_props
== NULL
)
2152 if (get_recvd_props_ioctl(zhp
) != 0)
2155 prop
= zfs_name_to_prop(propname
);
2157 if (prop
!= ZPROP_INVAL
) {
2159 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2161 zfs_set_recvd_props_mode(zhp
, &cookie
);
2162 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2163 NULL
, NULL
, 0, literal
);
2164 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2168 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2169 propname
, &propval
) != 0)
2171 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2173 (void) strlcpy(propbuf
, recvdval
, proplen
);
2176 return (err
== 0 ? 0 : -1);
2180 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2185 value
= zfs_get_clones_nvl(zhp
);
2190 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2191 pair
= nvlist_next_nvpair(value
, pair
)) {
2192 if (propbuf
[0] != '\0')
2193 (void) strlcat(propbuf
, ",", proplen
);
2194 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2200 struct get_clones_arg
{
2204 char buf
[ZFS_MAXNAMELEN
];
2208 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2210 struct get_clones_arg
*gca
= arg
;
2212 if (gca
->numclones
== 0) {
2217 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2218 NULL
, NULL
, 0, B_TRUE
) != 0)
2220 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2221 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2226 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2232 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2234 nvlist_t
*nv
, *value
;
2236 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2237 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2238 struct get_clones_arg gca
;
2241 * if this is a snapshot, then the kernel wasn't able
2242 * to get the clones. Do it by slowly iterating.
2244 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2246 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2248 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2253 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2255 gca
.origin
= zhp
->zfs_name
;
2257 if (gca
.numclones
!= 0) {
2259 char pool
[ZFS_MAXNAMELEN
];
2262 /* get the pool name */
2263 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2264 (void) strsep(&cp
, "/@");
2265 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2266 ZFS_TYPE_FILESYSTEM
);
2268 (void) get_clones_cb(root
, &gca
);
2271 if (gca
.numclones
!= 0 ||
2272 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2273 nvlist_add_nvlist(zhp
->zfs_props
,
2274 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2281 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2282 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2285 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2291 * Retrieve a property from the given object. If 'literal' is specified, then
2292 * numbers are left as exact values. Otherwise, numbers are converted to a
2293 * human-readable form.
2295 * Returns 0 on success, or -1 on error.
2298 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2299 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2301 char *source
= NULL
;
2305 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2308 * Check to see if this property applies to our object
2310 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2313 if (received
&& zfs_prop_readonly(prop
))
2317 *src
= ZPROP_SRC_NONE
;
2320 case ZFS_PROP_CREATION
:
2322 * 'creation' is a time_t stored in the statistics. We convert
2323 * this into a string unless 'literal' is specified.
2326 val
= getprop_uint64(zhp
, prop
, &source
);
2327 time_t time
= (time_t)val
;
2331 localtime_r(&time
, &t
) == NULL
||
2332 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2334 (void) snprintf(propbuf
, proplen
, "%llu",
2335 (u_longlong_t
) val
);
2339 case ZFS_PROP_MOUNTPOINT
:
2341 * Getting the precise mountpoint can be tricky.
2343 * - for 'none' or 'legacy', return those values.
2344 * - for inherited mountpoints, we want to take everything
2345 * after our ancestor and append it to the inherited value.
2347 * If the pool has an alternate root, we want to prepend that
2348 * root to any values we return.
2351 str
= getprop_string(zhp
, prop
, &source
);
2353 if (str
[0] == '/') {
2354 char buf
[MAXPATHLEN
];
2356 const char *relpath
;
2359 * If we inherit the mountpoint, even from a dataset
2360 * with a received value, the source will be the path of
2361 * the dataset we inherit from. If source is
2362 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2365 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2368 relpath
= zhp
->zfs_name
+ strlen(source
);
2369 if (relpath
[0] == '/')
2373 if ((zpool_get_prop(zhp
->zpool_hdl
,
2374 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2375 B_FALSE
)) || (strcmp(root
, "-") == 0))
2378 * Special case an alternate root of '/'. This will
2379 * avoid having multiple leading slashes in the
2382 if (strcmp(root
, "/") == 0)
2386 * If the mountpoint is '/' then skip over this
2387 * if we are obtaining either an alternate root or
2388 * an inherited mountpoint.
2390 if (str
[1] == '\0' && (root
[0] != '\0' ||
2391 relpath
[0] != '\0'))
2394 if (relpath
[0] == '\0')
2395 (void) snprintf(propbuf
, proplen
, "%s%s",
2398 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2399 root
, str
, relpath
[0] == '@' ? "" : "/",
2402 /* 'legacy' or 'none' */
2403 (void) strlcpy(propbuf
, str
, proplen
);
2408 case ZFS_PROP_ORIGIN
:
2409 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2412 * If there is no parent at all, return failure to indicate that
2413 * it doesn't apply to this dataset.
2415 if (propbuf
[0] == '\0')
2419 case ZFS_PROP_CLONES
:
2420 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2424 case ZFS_PROP_QUOTA
:
2425 case ZFS_PROP_REFQUOTA
:
2426 case ZFS_PROP_RESERVATION
:
2427 case ZFS_PROP_REFRESERVATION
:
2429 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2433 * If quota or reservation is 0, we translate this into 'none'
2434 * (unless literal is set), and indicate that it's the default
2435 * value. Otherwise, we print the number nicely and indicate
2436 * that its set locally.
2440 (void) strlcpy(propbuf
, "0", proplen
);
2442 (void) strlcpy(propbuf
, "none", proplen
);
2445 (void) snprintf(propbuf
, proplen
, "%llu",
2448 zfs_nicenum(val
, propbuf
, proplen
);
2452 case ZFS_PROP_FILESYSTEM_LIMIT
:
2453 case ZFS_PROP_SNAPSHOT_LIMIT
:
2454 case ZFS_PROP_FILESYSTEM_COUNT
:
2455 case ZFS_PROP_SNAPSHOT_COUNT
:
2457 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2461 * If limit is UINT64_MAX, we translate this into 'none' (unless
2462 * literal is set), and indicate that it's the default value.
2463 * Otherwise, we print the number nicely and indicate that it's
2467 (void) snprintf(propbuf
, proplen
, "%llu",
2469 } else if (val
== UINT64_MAX
) {
2470 (void) strlcpy(propbuf
, "none", proplen
);
2472 zfs_nicenum(val
, propbuf
, proplen
);
2476 case ZFS_PROP_REFRATIO
:
2477 case ZFS_PROP_COMPRESSRATIO
:
2478 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2480 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2481 (u_longlong_t
)(val
/ 100),
2482 (u_longlong_t
)(val
% 100));
2486 switch (zhp
->zfs_type
) {
2487 case ZFS_TYPE_FILESYSTEM
:
2490 case ZFS_TYPE_VOLUME
:
2493 case ZFS_TYPE_SNAPSHOT
:
2496 case ZFS_TYPE_BOOKMARK
:
2502 (void) snprintf(propbuf
, proplen
, "%s", str
);
2505 case ZFS_PROP_MOUNTED
:
2507 * The 'mounted' property is a pseudo-property that described
2508 * whether the filesystem is currently mounted. Even though
2509 * it's a boolean value, the typical values of "on" and "off"
2510 * don't make sense, so we translate to "yes" and "no".
2512 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2513 src
, &source
, &val
) != 0)
2516 (void) strlcpy(propbuf
, "yes", proplen
);
2518 (void) strlcpy(propbuf
, "no", proplen
);
2523 * The 'name' property is a pseudo-property derived from the
2524 * dataset name. It is presented as a real property to simplify
2527 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2530 case ZFS_PROP_MLSLABEL
:
2532 #ifdef HAVE_MLSLABEL
2533 m_label_t
*new_sl
= NULL
;
2534 char *ascii
= NULL
; /* human readable label */
2536 (void) strlcpy(propbuf
,
2537 getprop_string(zhp
, prop
, &source
), proplen
);
2539 if (literal
|| (strcasecmp(propbuf
,
2540 ZFS_MLSLABEL_DEFAULT
) == 0))
2544 * Try to translate the internal hex string to
2545 * human-readable output. If there are any
2546 * problems just use the hex string.
2549 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2550 L_NO_CORRECTION
, NULL
) == -1) {
2551 m_label_free(new_sl
);
2555 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2559 m_label_free(new_sl
);
2562 m_label_free(new_sl
);
2564 (void) strlcpy(propbuf
, ascii
, proplen
);
2567 (void) strlcpy(propbuf
,
2568 getprop_string(zhp
, prop
, &source
), proplen
);
2569 #endif /* HAVE_MLSLABEL */
2575 * GUIDs are stored as numbers, but they are identifiers.
2576 * We don't want them to be pretty printed, because pretty
2577 * printing mangles the ID into a truncated and useless value.
2579 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2581 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2585 switch (zfs_prop_get_type(prop
)) {
2586 case PROP_TYPE_NUMBER
:
2587 if (get_numeric_property(zhp
, prop
, src
,
2588 &source
, &val
) != 0)
2591 (void) snprintf(propbuf
, proplen
, "%llu",
2594 zfs_nicenum(val
, propbuf
, proplen
);
2597 case PROP_TYPE_STRING
:
2598 (void) strlcpy(propbuf
,
2599 getprop_string(zhp
, prop
, &source
), proplen
);
2602 case PROP_TYPE_INDEX
:
2603 if (get_numeric_property(zhp
, prop
, src
,
2604 &source
, &val
) != 0)
2606 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2608 (void) strlcpy(propbuf
, strval
, proplen
);
2616 get_source(zhp
, src
, source
, statbuf
, statlen
);
2622 * Utility function to get the given numeric property. Does no validation that
2623 * the given property is the appropriate type; should only be used with
2624 * hard-coded property types.
2627 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2632 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2638 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2642 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2643 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2647 * Similar to zfs_prop_get(), but returns the value as an integer.
2650 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2651 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2656 * Check to see if this property applies to our object
2658 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
2659 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2660 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2661 zfs_prop_to_name(prop
)));
2665 *src
= ZPROP_SRC_NONE
;
2667 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2670 get_source(zhp
, src
, source
, statbuf
, statlen
);
2677 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2678 char **domainp
, idmap_rid_t
*ridp
)
2680 idmap_get_handle_t
*get_hdl
= NULL
;
2684 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2688 err
= idmap_get_sidbyuid(get_hdl
, id
,
2689 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2691 err
= idmap_get_sidbygid(get_hdl
, id
,
2692 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2694 if (err
== IDMAP_SUCCESS
&&
2695 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2696 status
== IDMAP_SUCCESS
)
2702 idmap_get_destroy(get_hdl
);
2705 #endif /* HAVE_IDMAP */
2708 * convert the propname into parameters needed by kernel
2709 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2710 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2711 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2712 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2715 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2716 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2718 zfs_userquota_prop_t type
;
2727 /* Figure out the property type ({user|group}{quota|space}) */
2728 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2729 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2730 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2733 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2737 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
);
2738 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
);
2740 cp
= strchr(propname
, '@') + 1;
2742 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2743 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2746 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2747 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2750 } else if (strchr(cp
, '@')) {
2753 * It's a SID name (eg "user@domain") that needs to be
2754 * turned into S-1-domainID-RID.
2756 directory_error_t e
;
2757 char *numericsid
= NULL
;
2760 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2763 e
= directory_sid_from_user_name(NULL
,
2766 e
= directory_sid_from_group_name(NULL
,
2770 directory_error_free(e
);
2773 if (numericsid
== NULL
)
2776 (void) strlcpy(domain
, cp
, domainlen
);
2777 cp
= strrchr(domain
, '-');
2782 *ridp
= strtoull(cp
, &end
, 10);
2785 if (errno
!= 0 || *end
!= '\0')
2789 #endif /* HAVE_IDMAP */
2791 /* It's a user/group ID (eg "12345"). */
2794 id
= strtoul(cp
, &end
, 10);
2799 /* It's an ephemeral ID. */
2803 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2804 &mapdomain
, &rid
) != 0)
2806 (void) strlcpy(domain
, mapdomain
, domainlen
);
2810 #endif /* HAVE_IDMAP */
2820 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2821 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2824 zfs_cmd_t zc
= {"\0"};
2826 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2828 err
= userquota_propname_decode(propname
,
2829 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2830 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2831 zc
.zc_objset_type
= *typep
;
2835 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2839 *propvalue
= zc
.zc_cookie
;
2844 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2845 uint64_t *propvalue
)
2847 zfs_userquota_prop_t type
;
2849 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2854 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2855 char *propbuf
, int proplen
, boolean_t literal
)
2859 zfs_userquota_prop_t type
;
2861 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2868 (void) snprintf(propbuf
, proplen
, "%llu",
2869 (u_longlong_t
)propvalue
);
2870 } else if (propvalue
== 0 &&
2871 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2872 (void) strlcpy(propbuf
, "none", proplen
);
2874 zfs_nicenum(propvalue
, propbuf
, proplen
);
2880 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2881 uint64_t *propvalue
)
2884 zfs_cmd_t zc
= {"\0"};
2885 const char *snapname
;
2887 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2889 snapname
= strchr(propname
, '@') + 1;
2890 if (strchr(snapname
, '@')) {
2891 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2893 /* snapname is the short name, append it to zhp's fsname */
2896 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2897 sizeof (zc
.zc_value
));
2898 cp
= strchr(zc
.zc_value
, '@');
2901 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2902 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2905 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2909 *propvalue
= zc
.zc_cookie
;
2914 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2915 char *propbuf
, int proplen
, boolean_t literal
)
2920 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2926 (void) snprintf(propbuf
, proplen
, "%llu",
2927 (u_longlong_t
)propvalue
);
2929 zfs_nicenum(propvalue
, propbuf
, proplen
);
2936 * Returns the name of the given zfs handle.
2939 zfs_get_name(const zfs_handle_t
*zhp
)
2941 return (zhp
->zfs_name
);
2945 * Returns the type of the given zfs handle.
2948 zfs_get_type(const zfs_handle_t
*zhp
)
2950 return (zhp
->zfs_type
);
2954 * Is one dataset name a child dataset of another?
2956 * Needs to handle these cases:
2957 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2958 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2959 * Descendant? No. No. No. Yes.
2962 is_descendant(const char *ds1
, const char *ds2
)
2964 size_t d1len
= strlen(ds1
);
2966 /* ds2 can't be a descendant if it's smaller */
2967 if (strlen(ds2
) < d1len
)
2970 /* otherwise, compare strings and verify that there's a '/' char */
2971 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2975 * Given a complete name, return just the portion that refers to the parent.
2976 * Will return -1 if there is no parent (path is just the name of the
2980 parent_name(const char *path
, char *buf
, size_t buflen
)
2984 (void) strlcpy(buf
, path
, buflen
);
2986 if ((slashp
= strrchr(buf
, '/')) == NULL
)
2994 * If accept_ancestor is false, then check to make sure that the given path has
2995 * a parent, and that it exists. If accept_ancestor is true, then find the
2996 * closest existing ancestor for the given path. In prefixlen return the
2997 * length of already existing prefix of the given path. We also fetch the
2998 * 'zoned' property, which is used to validate property settings when creating
3002 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3003 boolean_t accept_ancestor
, int *prefixlen
)
3005 zfs_cmd_t zc
= {"\0"};
3006 char parent
[ZFS_MAXNAMELEN
];
3012 (void) snprintf(errbuf
, sizeof (errbuf
),
3013 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3015 /* get parent, and check to see if this is just a pool */
3016 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3017 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3018 "missing dataset name"));
3019 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3022 /* check to see if the pool exists */
3023 if ((slash
= strchr(parent
, '/')) == NULL
)
3024 slash
= parent
+ strlen(parent
);
3025 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3026 zc
.zc_name
[slash
- parent
] = '\0';
3027 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3029 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3030 "no such pool '%s'"), zc
.zc_name
);
3031 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3034 /* check to see if the parent dataset exists */
3035 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3036 if (errno
== ENOENT
&& accept_ancestor
) {
3038 * Go deeper to find an ancestor, give up on top level.
3040 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3042 "no such pool '%s'"), zc
.zc_name
);
3043 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3045 } else if (errno
== ENOENT
) {
3046 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3047 "parent does not exist"));
3048 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3050 return (zfs_standard_error(hdl
, errno
, errbuf
));
3053 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3057 /* we are in a non-global zone, but parent is in the global zone */
3058 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3059 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3064 /* make sure parent is a filesystem */
3065 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3066 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3067 "parent is not a filesystem"));
3068 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3074 if (prefixlen
!= NULL
)
3075 *prefixlen
= strlen(parent
);
3080 * Finds whether the dataset of the given type(s) exists.
3083 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3087 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3091 * Try to get stats for the dataset, which will tell us if it exists.
3093 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3094 int ds_type
= zhp
->zfs_type
;
3097 if (types
& ds_type
)
3104 * Given a path to 'target', create all the ancestors between
3105 * the prefixlen portion of the path, and the target itself.
3106 * Fail if the initial prefixlen-ancestor does not already exist.
3109 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3115 /* make sure prefix exists */
3116 cp
= target
+ prefixlen
;
3118 assert(strchr(cp
, '/') == NULL
);
3119 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3122 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3130 * Attempt to create, mount, and share any ancestor filesystems,
3131 * up to the prefixlen-long one.
3133 for (cp
= target
+ prefixlen
+ 1;
3134 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
3138 h
= make_dataset_handle(hdl
, target
);
3140 /* it already exists, nothing to do here */
3145 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3147 opname
= dgettext(TEXT_DOMAIN
, "create");
3151 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3153 opname
= dgettext(TEXT_DOMAIN
, "open");
3157 if (zfs_mount(h
, NULL
, 0) != 0) {
3158 opname
= dgettext(TEXT_DOMAIN
, "mount");
3162 if (zfs_share(h
) != 0) {
3163 opname
= dgettext(TEXT_DOMAIN
, "share");
3173 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3174 "failed to %s ancestor '%s'"), opname
, target
);
3179 * Creates non-existing ancestors of the given path.
3182 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3188 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3191 if ((path_copy
= strdup(path
)) != NULL
) {
3192 rc
= create_parents(hdl
, path_copy
, prefix
);
3195 if (path_copy
== NULL
|| rc
!= 0)
3202 * Create a new filesystem or volume.
3205 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3210 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3213 dmu_objset_type_t ost
;
3215 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3216 "cannot create '%s'"), path
);
3218 /* validate the path, taking care to note the extended error message */
3219 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3220 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3222 /* validate parents exist */
3223 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3227 * The failure modes when creating a dataset of a different type over
3228 * one that already exists is a little strange. In particular, if you
3229 * try to create a dataset on top of an existing dataset, the ioctl()
3230 * will return ENOENT, not EEXIST. To prevent this from happening, we
3231 * first try to see if the dataset exists.
3233 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3234 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3235 "dataset already exists"));
3236 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3239 if (type
== ZFS_TYPE_VOLUME
)
3244 /* open zpool handle for prop validation */
3245 char pool_path
[MAXNAMELEN
];
3246 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3248 /* truncate pool_path at first slash */
3249 char *p
= strchr(pool_path
, '/');
3253 zpool_handle_t
*zpool_handle
= zpool_open(hdl
, pool_path
);
3255 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3256 zoned
, NULL
, zpool_handle
, errbuf
)) == 0) {
3257 zpool_close(zpool_handle
);
3260 zpool_close(zpool_handle
);
3262 if (type
== ZFS_TYPE_VOLUME
) {
3264 * If we are creating a volume, the size and block size must
3265 * satisfy a few restraints. First, the blocksize must be a
3266 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3267 * volsize must be a multiple of the block size, and cannot be
3270 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3271 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3273 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3274 "missing volume size"));
3275 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3278 if ((ret
= nvlist_lookup_uint64(props
,
3279 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3280 &blocksize
)) != 0) {
3281 if (ret
== ENOENT
) {
3282 blocksize
= zfs_prop_default_numeric(
3283 ZFS_PROP_VOLBLOCKSIZE
);
3286 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3287 "missing volume block size"));
3288 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3294 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3295 "volume size cannot be zero"));
3296 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3299 if (size
% blocksize
!= 0) {
3301 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3302 "volume size must be a multiple of volume block "
3304 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3308 /* create the dataset */
3309 ret
= lzc_create(path
, ost
, props
);
3312 /* check for failure */
3314 char parent
[ZFS_MAXNAMELEN
];
3315 (void) parent_name(path
, parent
, sizeof (parent
));
3319 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3320 "no such parent '%s'"), parent
);
3321 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3324 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3325 "parent '%s' is not a filesystem"), parent
);
3326 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3329 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3330 "pool must be upgraded to set this "
3331 "property or value"));
3332 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3336 * This platform can't address a volume this big.
3338 if (type
== ZFS_TYPE_VOLUME
)
3339 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3344 return (zfs_standard_error(hdl
, errno
, errbuf
));
3352 * Destroys the given dataset. The caller must make sure that the filesystem
3353 * isn't mounted, and that there are no active dependents. If the file system
3354 * does not exist this function does nothing.
3357 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3359 zfs_cmd_t zc
= {"\0"};
3361 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3362 nvlist_t
*nv
= fnvlist_alloc();
3363 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3364 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3367 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3368 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3374 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3376 if (ZFS_IS_VOLUME(zhp
)) {
3377 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3379 zc
.zc_objset_type
= DMU_OST_ZFS
;
3382 zc
.zc_defer_destroy
= defer
;
3383 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3385 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3386 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3390 remove_mountpoint(zhp
);
3395 struct destroydata
{
3397 const char *snapname
;
3401 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3403 struct destroydata
*dd
= arg
;
3404 char name
[ZFS_MAXNAMELEN
];
3407 (void) snprintf(name
, sizeof (name
),
3408 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3410 if (lzc_exists(name
))
3411 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3413 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3419 * Destroys all snapshots with the given name in zhp & descendants.
3422 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3425 struct destroydata dd
= { 0 };
3427 dd
.snapname
= snapname
;
3428 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3429 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3431 if (nvlist_empty(dd
.nvl
)) {
3432 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3433 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3434 zhp
->zfs_name
, snapname
);
3436 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3438 nvlist_free(dd
.nvl
);
3443 * Destroys all the snapshots named in the nvlist.
3446 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3449 nvlist_t
*errlist
= NULL
;
3452 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3455 nvlist_free(errlist
);
3459 if (nvlist_empty(errlist
)) {
3461 (void) snprintf(errbuf
, sizeof (errbuf
),
3462 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3464 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3466 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3467 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3469 (void) snprintf(errbuf
, sizeof (errbuf
),
3470 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3473 switch (fnvpair_value_int32(pair
)) {
3476 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3477 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3480 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3485 nvlist_free(errlist
);
3490 * Clones the given dataset. The target must be of the same type as the source.
3493 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3495 char parent
[ZFS_MAXNAMELEN
];
3498 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3501 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3503 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3504 "cannot create '%s'"), target
);
3506 /* validate the target/clone name */
3507 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3508 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3510 /* validate parents exist */
3511 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3514 (void) parent_name(target
, parent
, sizeof (parent
));
3520 if (ZFS_IS_VOLUME(zhp
)) {
3521 type
= ZFS_TYPE_VOLUME
;
3523 type
= ZFS_TYPE_FILESYSTEM
;
3525 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3526 zhp
, zhp
->zpool_hdl
, errbuf
)) == NULL
)
3530 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3538 * The parent doesn't exist. We should have caught this
3539 * above, but there may a race condition that has since
3540 * destroyed the parent.
3542 * At this point, we don't know whether it's the source
3543 * that doesn't exist anymore, or whether the target
3544 * dataset doesn't exist.
3546 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3547 "no such parent '%s'"), parent
);
3548 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3551 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3552 "source and target pools differ"));
3553 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3557 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3566 * Promotes the given clone fs to be the clone parent.
3569 zfs_promote(zfs_handle_t
*zhp
)
3571 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3572 zfs_cmd_t zc
= {"\0"};
3573 char parent
[MAXPATHLEN
];
3577 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3578 "cannot promote '%s'"), zhp
->zfs_name
);
3580 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3582 "snapshots can not be promoted"));
3583 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3586 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3587 if (parent
[0] == '\0') {
3588 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3589 "not a cloned filesystem"));
3590 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3593 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3594 sizeof (zc
.zc_value
));
3595 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3596 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3599 int save_errno
= errno
;
3601 switch (save_errno
) {
3603 /* There is a conflicting snapshot name. */
3604 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3605 "conflicting snapshot '%s' from parent '%s'"),
3606 zc
.zc_string
, parent
);
3607 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3610 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3616 typedef struct snapdata
{
3618 const char *sd_snapname
;
3622 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3624 snapdata_t
*sd
= arg
;
3625 char name
[ZFS_MAXNAMELEN
];
3628 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3629 (void) snprintf(name
, sizeof (name
),
3630 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3632 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3634 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3642 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3646 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3653 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3654 "cannot create snapshots "));
3657 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3658 const char *snapname
= nvpair_name(elem
);
3660 /* validate the target name */
3661 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3663 (void) snprintf(errbuf
, sizeof (errbuf
),
3664 dgettext(TEXT_DOMAIN
,
3665 "cannot create snapshot '%s'"), snapname
);
3666 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3671 * get pool handle for prop validation. assumes all snaps are in the
3672 * same pool, as does lzc_snapshot (below).
3674 char pool
[MAXNAMELEN
];
3675 elem
= nvlist_next_nvpair(snaps
, NULL
);
3676 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
3677 pool
[strcspn(pool
, "/@")] = '\0';
3678 zpool_handle_t
*zpool_hdl
= zpool_open(hdl
, pool
);
3680 if (props
!= NULL
&&
3681 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3682 props
, B_FALSE
, NULL
, zpool_hdl
, errbuf
)) == NULL
) {
3683 zpool_close(zpool_hdl
);
3686 zpool_close(zpool_hdl
);
3688 ret
= lzc_snapshot(snaps
, props
, &errors
);
3691 boolean_t printed
= B_FALSE
;
3692 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3694 elem
= nvlist_next_nvpair(errors
, elem
)) {
3695 (void) snprintf(errbuf
, sizeof (errbuf
),
3696 dgettext(TEXT_DOMAIN
,
3697 "cannot create snapshot '%s'"), nvpair_name(elem
));
3698 (void) zfs_standard_error(hdl
,
3699 fnvpair_value_int32(elem
), errbuf
);
3705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3706 "multiple snapshots of same "
3708 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3712 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3718 nvlist_free(errors
);
3723 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3727 snapdata_t sd
= { 0 };
3728 char fsname
[ZFS_MAXNAMELEN
];
3733 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3734 "cannot snapshot %s"), path
);
3736 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3737 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3739 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3740 cp
= strchr(fsname
, '@');
3742 sd
.sd_snapname
= cp
+ 1;
3744 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3745 ZFS_TYPE_VOLUME
)) == NULL
) {
3749 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3751 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3753 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3756 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3757 nvlist_free(sd
.sd_nvl
);
3763 * Destroy any more recent snapshots. We invoke this callback on any dependents
3764 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3765 * is a dependent and we should just destroy it without checking the transaction
3768 typedef struct rollback_data
{
3769 const char *cb_target
; /* the snapshot */
3770 uint64_t cb_create
; /* creation time reference */
3776 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3778 rollback_data_t
*cbp
= data
;
3779 prop_changelist_t
*clp
;
3781 /* We must destroy this clone; first unmount it */
3782 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3783 cbp
->cb_force
? MS_FORCE
: 0);
3784 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3785 cbp
->cb_error
= B_TRUE
;
3789 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3790 cbp
->cb_error
= B_TRUE
;
3792 changelist_remove(clp
, zhp
->zfs_name
);
3793 (void) changelist_postfix(clp
);
3794 changelist_free(clp
);
3801 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3803 rollback_data_t
*cbp
= data
;
3805 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3806 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3807 rollback_destroy_dependent
, cbp
);
3809 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3817 * Given a dataset, rollback to a specific snapshot, discarding any
3818 * data changes since then and making it the active dataset.
3820 * Any snapshots and bookmarks more recent than the target are
3821 * destroyed, along with their dependents (i.e. clones).
3824 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3826 rollback_data_t cb
= { 0 };
3828 boolean_t restore_resv
= 0;
3829 uint64_t old_volsize
= 0, new_volsize
;
3830 zfs_prop_t resv_prop
= { 0 };
3832 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3833 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3836 * Destroy all recent snapshots and their dependents.
3838 cb
.cb_force
= force
;
3839 cb
.cb_target
= snap
->zfs_name
;
3840 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3841 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3842 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3848 * Now that we have verified that the snapshot is the latest,
3849 * rollback to the given snapshot.
3852 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3853 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3855 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3857 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3861 * We rely on zfs_iter_children() to verify that there are no
3862 * newer snapshots for the given dataset. Therefore, we can
3863 * simply pass the name on to the ioctl() call. There is still
3864 * an unlikely race condition where the user has taken a
3865 * snapshot since we verified that this was the most recent.
3867 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3869 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3870 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3876 * For volumes, if the pre-rollback volsize matched the pre-
3877 * rollback reservation and the volsize has changed then set
3878 * the reservation property to the post-rollback volsize.
3879 * Make a new handle since the rollback closed the dataset.
3881 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3882 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3884 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3885 if (old_volsize
!= new_volsize
)
3886 err
= zfs_prop_set_int(zhp
, resv_prop
,
3895 * Renames the given dataset.
3898 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
3899 boolean_t force_unmount
)
3902 zfs_cmd_t zc
= {"\0"};
3904 prop_changelist_t
*cl
= NULL
;
3905 zfs_handle_t
*zhrp
= NULL
;
3906 char *parentname
= NULL
;
3907 char parent
[ZFS_MAXNAMELEN
];
3908 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3911 /* if we have the same exact name, just return success */
3912 if (strcmp(zhp
->zfs_name
, target
) == 0)
3915 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3916 "cannot rename to '%s'"), target
);
3919 * Make sure the target name is valid
3921 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3922 if ((strchr(target
, '@') == NULL
) ||
3925 * Snapshot target name is abbreviated,
3926 * reconstruct full dataset name
3928 (void) strlcpy(parent
, zhp
->zfs_name
,
3930 delim
= strchr(parent
, '@');
3931 if (strchr(target
, '@') == NULL
)
3935 (void) strlcat(parent
, target
, sizeof (parent
));
3939 * Make sure we're renaming within the same dataset.
3941 delim
= strchr(target
, '@');
3942 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3943 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3944 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3945 "snapshots must be part of same "
3947 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3951 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3952 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3955 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3956 "recursive rename must be a snapshot"));
3957 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3960 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3961 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3963 /* validate parents */
3964 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3967 /* make sure we're in the same pool */
3968 verify((delim
= strchr(target
, '/')) != NULL
);
3969 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3970 zhp
->zfs_name
[delim
- target
] != '/') {
3971 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3972 "datasets must be within same pool"));
3973 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3976 /* new name cannot be a child of the current dataset name */
3977 if (is_descendant(zhp
->zfs_name
, target
)) {
3978 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3979 "New dataset name cannot be a descendant of "
3980 "current dataset name"));
3981 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3985 (void) snprintf(errbuf
, sizeof (errbuf
),
3986 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3988 if (getzoneid() == GLOBAL_ZONEID
&&
3989 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3990 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3991 "dataset is used in a non-global zone"));
3992 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3996 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3997 if (parentname
== NULL
) {
4001 delim
= strchr(parentname
, '@');
4003 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4008 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4009 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4010 force_unmount
? MS_FORCE
: 0)) == NULL
)
4013 if (changelist_haszonedchild(cl
)) {
4014 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4015 "child dataset with inherited mountpoint is used "
4016 "in a non-global zone"));
4017 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4022 if ((ret
= changelist_prefix(cl
)) != 0)
4026 if (ZFS_IS_VOLUME(zhp
))
4027 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4029 zc
.zc_objset_type
= DMU_OST_ZFS
;
4031 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4032 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4034 zc
.zc_cookie
= recursive
;
4036 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4038 * if it was recursive, the one that actually failed will
4041 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4042 "cannot rename '%s'"), zc
.zc_name
);
4044 if (recursive
&& errno
== EEXIST
) {
4045 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4046 "a child dataset already has a snapshot "
4047 "with the new name"));
4048 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4050 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4054 * On failure, we still want to remount any filesystems that
4055 * were previously mounted, so we don't alter the system state.
4058 (void) changelist_postfix(cl
);
4061 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4062 ret
= changelist_postfix(cl
);
4067 if (parentname
!= NULL
) {
4074 changelist_free(cl
);
4080 zfs_get_user_props(zfs_handle_t
*zhp
)
4082 return (zhp
->zfs_user_props
);
4086 * This function is used by 'zfs list' to determine the exact set of columns to
4087 * display, and their maximum widths. This does two main things:
4089 * - If this is a list of all properties, then expand the list to include
4090 * all native properties, and set a flag so that for each dataset we look
4091 * for new unique user properties and add them to the list.
4093 * - For non fixed-width properties, keep track of the maximum width seen
4094 * so that we can size the column appropriately. If the user has
4095 * requested received property values, we also need to compute the width
4096 * of the RECEIVED column.
4099 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4102 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4103 zprop_list_t
*entry
;
4104 zprop_list_t
**last
, **start
;
4105 nvlist_t
*userprops
, *propval
;
4108 char buf
[ZFS_MAXPROPLEN
];
4110 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4113 userprops
= zfs_get_user_props(zhp
);
4116 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4118 * Go through and add any user properties as necessary. We
4119 * start by incrementing our list pointer to the first
4120 * non-native property.
4123 while (*start
!= NULL
) {
4124 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4126 start
= &(*start
)->pl_next
;
4130 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4132 * See if we've already found this property in our list.
4134 for (last
= start
; *last
!= NULL
;
4135 last
= &(*last
)->pl_next
) {
4136 if (strcmp((*last
)->pl_user_prop
,
4137 nvpair_name(elem
)) == 0)
4141 if (*last
== NULL
) {
4142 if ((entry
= zfs_alloc(hdl
,
4143 sizeof (zprop_list_t
))) == NULL
||
4144 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4145 nvpair_name(elem
)))) == NULL
) {
4150 entry
->pl_prop
= ZPROP_INVAL
;
4151 entry
->pl_width
= strlen(nvpair_name(elem
));
4152 entry
->pl_all
= B_TRUE
;
4159 * Now go through and check the width of any non-fixed columns
4161 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4162 if (entry
->pl_fixed
&& !literal
)
4165 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4166 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4167 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4168 if (strlen(buf
) > entry
->pl_width
)
4169 entry
->pl_width
= strlen(buf
);
4171 if (received
&& zfs_prop_get_recvd(zhp
,
4172 zfs_prop_to_name(entry
->pl_prop
),
4173 buf
, sizeof (buf
), literal
) == 0)
4174 if (strlen(buf
) > entry
->pl_recvd_width
)
4175 entry
->pl_recvd_width
= strlen(buf
);
4177 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4179 verify(nvlist_lookup_string(propval
,
4180 ZPROP_VALUE
, &strval
) == 0);
4181 if (strlen(strval
) > entry
->pl_width
)
4182 entry
->pl_width
= strlen(strval
);
4184 if (received
&& zfs_prop_get_recvd(zhp
,
4185 entry
->pl_user_prop
,
4186 buf
, sizeof (buf
), literal
) == 0)
4187 if (strlen(buf
) > entry
->pl_recvd_width
)
4188 entry
->pl_recvd_width
= strlen(buf
);
4196 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4202 * Keep a reference to the props-table against which we prune the
4205 zhp
->zfs_props_table
= props
;
4207 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4210 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4211 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4214 * User properties will result in ZPROP_INVAL, and since we
4215 * only know how to prune standard ZFS properties, we always
4216 * leave these in the list. This can also happen if we
4217 * encounter an unknown DSL property (when running older
4218 * software, for example).
4220 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4221 (void) nvlist_remove(zhp
->zfs_props
,
4222 nvpair_name(curr
), nvpair_type(curr
));
4228 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4229 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4231 zfs_cmd_t zc
= {"\0"};
4232 nvlist_t
*nvlist
= NULL
;
4235 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4236 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4237 zc
.zc_cookie
= (uint64_t)cmd
;
4239 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4240 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4241 (void) no_memory(hdl
);
4247 case ZFS_SMB_ACL_ADD
:
4248 case ZFS_SMB_ACL_REMOVE
:
4249 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4251 case ZFS_SMB_ACL_RENAME
:
4252 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4254 (void) no_memory(hdl
);
4257 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4259 (void) no_memory(hdl
);
4262 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4263 nvlist_free(nvlist
);
4267 case ZFS_SMB_ACL_PURGE
:
4272 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4273 nvlist_free(nvlist
);
4278 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4279 char *path
, char *resource
)
4281 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4286 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4287 char *path
, char *resource
)
4289 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4294 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4296 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4301 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4302 char *oldname
, char *newname
)
4304 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4309 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4310 zfs_userspace_cb_t func
, void *arg
)
4312 zfs_cmd_t zc
= {"\0"};
4313 zfs_useracct_t buf
[100];
4314 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4317 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4319 zc
.zc_objset_type
= type
;
4320 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4323 zfs_useracct_t
*zua
= buf
;
4325 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4326 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4327 char errbuf
[ZFS_MAXNAMELEN
+ 32];
4329 (void) snprintf(errbuf
, sizeof (errbuf
),
4330 dgettext(TEXT_DOMAIN
,
4331 "cannot get used/quota for %s"), zc
.zc_name
);
4332 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4334 if (zc
.zc_nvlist_dst_size
== 0)
4337 while (zc
.zc_nvlist_dst_size
> 0) {
4338 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4339 zua
->zu_space
)) != 0)
4342 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4351 const char *snapname
;
4353 boolean_t recursive
;
4358 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4360 struct holdarg
*ha
= arg
;
4361 char name
[ZFS_MAXNAMELEN
];
4364 (void) snprintf(name
, sizeof (name
),
4365 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4367 if (lzc_exists(name
))
4368 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4371 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4377 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4378 boolean_t recursive
, int cleanup_fd
)
4383 ha
.nvl
= fnvlist_alloc();
4384 ha
.snapname
= snapname
;
4386 ha
.recursive
= recursive
;
4387 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4389 if (nvlist_empty(ha
.nvl
)) {
4392 fnvlist_free(ha
.nvl
);
4394 (void) snprintf(errbuf
, sizeof (errbuf
),
4395 dgettext(TEXT_DOMAIN
,
4396 "cannot hold snapshot '%s@%s'"),
4397 zhp
->zfs_name
, snapname
);
4398 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4402 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4403 fnvlist_free(ha
.nvl
);
4409 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4413 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4418 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4421 /* There may be errors even in the success case. */
4422 fnvlist_free(errors
);
4426 if (nvlist_empty(errors
)) {
4427 /* no hold-specific errors */
4428 (void) snprintf(errbuf
, sizeof (errbuf
),
4429 dgettext(TEXT_DOMAIN
, "cannot hold"));
4432 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4433 "pool must be upgraded"));
4434 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4437 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4440 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4444 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4446 elem
= nvlist_next_nvpair(errors
, elem
)) {
4447 (void) snprintf(errbuf
, sizeof (errbuf
),
4448 dgettext(TEXT_DOMAIN
,
4449 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4450 switch (fnvpair_value_int32(elem
)) {
4453 * Temporary tags wind up having the ds object id
4454 * prepended. So even if we passed the length check
4455 * above, it's still possible for the tag to wind
4456 * up being slightly too long.
4458 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4461 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4464 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4467 (void) zfs_standard_error(hdl
,
4468 fnvpair_value_int32(elem
), errbuf
);
4472 fnvlist_free(errors
);
4477 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4479 struct holdarg
*ha
= arg
;
4480 char name
[ZFS_MAXNAMELEN
];
4482 nvlist_t
*existing_holds
;
4484 (void) snprintf(name
, sizeof (name
),
4485 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4487 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4489 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4492 nvlist_t
*torelease
= fnvlist_alloc();
4493 fnvlist_add_boolean(torelease
, ha
->tag
);
4494 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4495 fnvlist_free(torelease
);
4499 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4505 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4506 boolean_t recursive
)
4510 nvlist_t
*errors
= NULL
;
4512 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4515 ha
.nvl
= fnvlist_alloc();
4516 ha
.snapname
= snapname
;
4518 ha
.recursive
= recursive
;
4520 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4522 if (nvlist_empty(ha
.nvl
)) {
4523 fnvlist_free(ha
.nvl
);
4525 (void) snprintf(errbuf
, sizeof (errbuf
),
4526 dgettext(TEXT_DOMAIN
,
4527 "cannot release hold from snapshot '%s@%s'"),
4528 zhp
->zfs_name
, snapname
);
4530 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4532 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4537 ret
= lzc_release(ha
.nvl
, &errors
);
4538 fnvlist_free(ha
.nvl
);
4541 /* There may be errors even in the success case. */
4542 fnvlist_free(errors
);
4546 if (nvlist_empty(errors
)) {
4547 /* no hold-specific errors */
4548 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4552 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4553 "pool must be upgraded"));
4554 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4557 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4561 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4563 elem
= nvlist_next_nvpair(errors
, elem
)) {
4564 (void) snprintf(errbuf
, sizeof (errbuf
),
4565 dgettext(TEXT_DOMAIN
,
4566 "cannot release hold from snapshot '%s'"),
4568 switch (fnvpair_value_int32(elem
)) {
4570 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4573 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4576 (void) zfs_standard_error_fmt(hdl
,
4577 fnvpair_value_int32(elem
), errbuf
);
4581 fnvlist_free(errors
);
4586 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4588 zfs_cmd_t zc
= {"\0"};
4589 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4595 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4596 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4600 nvbuf
= malloc(nvsz
);
4601 if (nvbuf
== NULL
) {
4602 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4606 zc
.zc_nvlist_dst_size
= nvsz
;
4607 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4609 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4611 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4612 (void) snprintf(errbuf
, sizeof (errbuf
),
4613 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4618 nvsz
= zc
.zc_nvlist_dst_size
;
4622 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4623 "pool must be upgraded"));
4624 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4627 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4630 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4633 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4638 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4640 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4641 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4643 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4653 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4655 zfs_cmd_t zc
= {"\0"};
4656 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4662 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4663 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4665 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4668 nvbuf
= malloc(nvsz
);
4670 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4673 zc
.zc_nvlist_src_size
= nvsz
;
4674 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4675 zc
.zc_perm_action
= un
;
4677 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4679 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4680 (void) snprintf(errbuf
, sizeof (errbuf
),
4681 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4685 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4686 "pool must be upgraded"));
4687 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4690 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4693 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4696 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4707 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4712 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4715 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4717 (void) snprintf(errbuf
, sizeof (errbuf
),
4718 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4722 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4723 "pool must be upgraded"));
4724 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4727 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4730 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4733 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4742 * Convert the zvol's volume size to an appropriate reservation.
4743 * Note: If this routine is updated, it is necessary to update the ZFS test
4744 * suite's shell version in reservation.kshlib.
4747 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4750 uint64_t nblocks
, volblocksize
;
4754 if (nvlist_lookup_string(props
,
4755 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4756 ncopies
= atoi(strval
);
4759 if (nvlist_lookup_uint64(props
,
4760 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4761 &volblocksize
) != 0)
4762 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4763 nblocks
= volsize
/volblocksize
;
4764 /* start with metadnode L0-L6 */
4766 /* calculate number of indirects */
4767 while (nblocks
> 1) {
4768 nblocks
+= DNODES_PER_LEVEL
- 1;
4769 nblocks
/= DNODES_PER_LEVEL
;
4772 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4775 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4776 * compressed, but in practice they compress down to about
4779 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;