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) 2018, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2017 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 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017 RackTop Systems.
46 #include <sys/mntent.h>
47 #include <sys/mount.h>
55 #include <directory.h>
56 #endif /* HAVE_IDMAP */
58 #include <sys/dnode.h>
61 #include <sys/dsl_crypt.h>
64 #include "zfs_namecheck.h"
66 #include "libzfs_impl.h"
68 #include "zfs_deleg.h"
70 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
71 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
74 * Given a single type (not a mask of types), return the type in a human
78 zfs_type_to_name(zfs_type_t type
)
81 case ZFS_TYPE_FILESYSTEM
:
82 return (dgettext(TEXT_DOMAIN
, "filesystem"));
83 case ZFS_TYPE_SNAPSHOT
:
84 return (dgettext(TEXT_DOMAIN
, "snapshot"));
86 return (dgettext(TEXT_DOMAIN
, "volume"));
88 return (dgettext(TEXT_DOMAIN
, "pool"));
89 case ZFS_TYPE_BOOKMARK
:
90 return (dgettext(TEXT_DOMAIN
, "bookmark"));
92 assert(!"unhandled zfs_type_t");
99 * Validate a ZFS path. This is used even before trying to open the dataset, to
100 * provide a more meaningful error message. We call zfs_error_aux() to
101 * explain exactly why the name was not valid.
104 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
110 if (entity_namecheck(path
, &why
, &what
) != 0) {
113 case NAME_ERR_TOOLONG
:
114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
115 "name is too long"));
118 case NAME_ERR_LEADING_SLASH
:
119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
120 "leading slash in name"));
123 case NAME_ERR_EMPTY_COMPONENT
:
124 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
125 "empty component in name"));
128 case NAME_ERR_TRAILING_SLASH
:
129 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
130 "trailing slash in name"));
133 case NAME_ERR_INVALCHAR
:
135 dgettext(TEXT_DOMAIN
, "invalid character "
136 "'%c' in name"), what
);
139 case NAME_ERR_MULTIPLE_DELIMITERS
:
140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
141 "multiple '@' and/or '#' delimiters in "
145 case NAME_ERR_NOLETTER
:
146 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
147 "pool doesn't begin with a letter"));
150 case NAME_ERR_RESERVED
:
151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
152 "name is reserved"));
155 case NAME_ERR_DISKLIKE
:
156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
157 "reserved disk name"));
161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
162 "(%d) not defined"), why
);
170 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
172 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
173 "snapshot delimiter '@' is not expected here"));
177 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
179 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
180 "missing '@' delimiter in snapshot name"));
184 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
187 "bookmark delimiter '#' is not expected here"));
191 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
193 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
194 "missing '#' delimiter in bookmark name"));
198 if (modifying
&& strchr(path
, '%') != NULL
) {
200 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
201 "invalid character %c in name"), '%');
209 zfs_name_valid(const char *name
, zfs_type_t type
)
211 if (type
== ZFS_TYPE_POOL
)
212 return (zpool_name_valid(NULL
, B_FALSE
, name
));
213 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
217 * This function takes the raw DSL properties, and filters out the user-defined
218 * properties into a separate nvlist.
221 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
223 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
228 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
229 (void) no_memory(hdl
);
234 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
235 if (!zfs_prop_user(nvpair_name(elem
)))
238 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
239 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
241 (void) no_memory(hdl
);
249 static zpool_handle_t
*
250 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
252 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
255 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
256 if (hdl
->libzfs_pool_handles
!= NULL
)
257 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
258 hdl
->libzfs_pool_handles
= zph
;
263 static zpool_handle_t
*
264 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
266 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
267 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
269 while ((zph
!= NULL
) &&
270 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
271 zph
= zph
->zpool_next
;
276 * Returns a handle to the pool that contains the provided dataset.
277 * If a handle to that pool already exists then that handle is returned.
278 * Otherwise, a new handle is created and added to the list of handles.
280 static zpool_handle_t
*
281 zpool_handle(zfs_handle_t
*zhp
)
287 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
288 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
289 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
291 zph
= zpool_find_handle(zhp
, pool_name
, len
);
293 zph
= zpool_add_handle(zhp
, pool_name
);
300 zpool_free_handles(libzfs_handle_t
*hdl
)
302 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
304 while (zph
!= NULL
) {
305 next
= zph
->zpool_next
;
309 hdl
->libzfs_pool_handles
= NULL
;
313 * Utility function to gather stats (objset and zpl) for the given object.
316 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
318 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
320 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
322 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
323 if (errno
== ENOMEM
) {
324 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
335 * Utility function to get the received properties of the given object.
338 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
340 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
341 nvlist_t
*recvdprops
;
342 zfs_cmd_t zc
= {"\0"};
345 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
348 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
350 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
351 if (errno
== ENOMEM
) {
352 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
356 zcmd_free_nvlists(&zc
);
361 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
362 zcmd_free_nvlists(&zc
);
366 nvlist_free(zhp
->zfs_recvd_props
);
367 zhp
->zfs_recvd_props
= recvdprops
;
373 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
375 nvlist_t
*allprops
, *userprops
;
377 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
379 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
384 * XXX Why do we store the user props separately, in addition to
385 * storing them in zfs_props?
387 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
388 nvlist_free(allprops
);
392 nvlist_free(zhp
->zfs_props
);
393 nvlist_free(zhp
->zfs_user_props
);
395 zhp
->zfs_props
= allprops
;
396 zhp
->zfs_user_props
= userprops
;
402 get_stats(zfs_handle_t
*zhp
)
405 zfs_cmd_t zc
= {"\0"};
407 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
409 if (get_stats_ioctl(zhp
, &zc
) != 0)
411 else if (put_stats_zhdl(zhp
, &zc
) != 0)
413 zcmd_free_nvlists(&zc
);
418 * Refresh the properties currently stored in the handle.
421 zfs_refresh_properties(zfs_handle_t
*zhp
)
423 (void) get_stats(zhp
);
427 * Makes a handle from the given dataset name. Used by zfs_open() and
428 * zfs_iter_* to create child handles on the fly.
431 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
433 if (put_stats_zhdl(zhp
, zc
) != 0)
437 * We've managed to open the dataset and gather statistics. Determine
438 * the high-level type.
440 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
441 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
442 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
443 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
444 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
449 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
450 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
451 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
452 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
453 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
454 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
456 abort(); /* we should never see any other types */
458 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
465 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
467 zfs_cmd_t zc
= {"\0"};
469 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
475 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
476 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
480 if (get_stats_ioctl(zhp
, &zc
) == -1) {
481 zcmd_free_nvlists(&zc
);
485 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
489 zcmd_free_nvlists(&zc
);
494 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
496 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
502 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
503 if (make_dataset_handle_common(zhp
, zc
) == -1) {
511 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
513 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
518 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
519 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
520 zhp
->zfs_head_type
= pzhp
->zfs_type
;
521 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
522 zhp
->zpool_hdl
= zpool_handle(zhp
);
528 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
530 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
535 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
536 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
537 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
538 sizeof (zhp
->zfs_name
));
539 zhp
->zfs_type
= zhp_orig
->zfs_type
;
540 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
541 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
542 if (zhp_orig
->zfs_props
!= NULL
) {
543 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
544 (void) no_memory(zhp
->zfs_hdl
);
549 if (zhp_orig
->zfs_user_props
!= NULL
) {
550 if (nvlist_dup(zhp_orig
->zfs_user_props
,
551 &zhp
->zfs_user_props
, 0) != 0) {
552 (void) no_memory(zhp
->zfs_hdl
);
557 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
558 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
559 &zhp
->zfs_recvd_props
, 0)) {
560 (void) no_memory(zhp
->zfs_hdl
);
565 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
566 if (zhp_orig
->zfs_mntopts
!= NULL
) {
567 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
568 zhp_orig
->zfs_mntopts
);
570 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
575 zfs_bookmark_exists(const char *path
)
579 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
586 (void) strlcpy(fsname
, path
, sizeof (fsname
));
587 pound
= strchr(fsname
, '#');
592 bmark_name
= pound
+ 1;
593 props
= fnvlist_alloc();
594 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
601 rv
= nvlist_exists(bmarks
, bmark_name
);
607 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
608 nvlist_t
*bmark_props
)
610 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
615 /* Fill in the name. */
616 zhp
->zfs_hdl
= parent
->zfs_hdl
;
617 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
619 /* Set the property lists. */
620 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
626 zhp
->zfs_head_type
= parent
->zfs_head_type
;
627 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
629 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
630 nvlist_free(zhp
->zfs_props
);
638 struct zfs_open_bookmarks_cb_data
{
644 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
646 struct zfs_open_bookmarks_cb_data
*dp
= data
;
649 * Is it the one we are looking for?
651 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
653 * We found it. Save it and let the caller know we are done.
660 * Not found. Close the handle and ask for another one.
667 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
668 * argument is a mask of acceptable types. The function will print an
669 * appropriate error message and return NULL if it can't be opened.
672 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
678 (void) snprintf(errbuf
, sizeof (errbuf
),
679 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
682 * Validate the name before we even try to open it.
684 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
685 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
690 * Bookmarks needs to be handled separately.
692 bookp
= strchr(path
, '#');
695 * Try to get stats for the dataset, which will tell us if it
699 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
700 (void) zfs_standard_error(hdl
, errno
, errbuf
);
704 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
706 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
709 * We need to cut out '#' and everything after '#'
710 * to get the parent dataset name only.
712 assert(bookp
- path
< sizeof (dsname
));
713 (void) strncpy(dsname
, path
, bookp
- path
);
714 dsname
[bookp
- path
] = '\0';
717 * Create handle for the parent dataset.
720 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
721 (void) zfs_standard_error(hdl
, errno
, errbuf
);
726 * Iterate bookmarks to find the right one.
729 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
730 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
731 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
735 if (cb_data
.zhp
== NULL
) {
736 (void) zfs_standard_error(hdl
, errno
, errbuf
);
748 if (!(types
& zhp
->zfs_type
)) {
749 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
758 * Release a ZFS handle. Nothing to do but free the associated memory.
761 zfs_close(zfs_handle_t
*zhp
)
763 if (zhp
->zfs_mntopts
)
764 free(zhp
->zfs_mntopts
);
765 nvlist_free(zhp
->zfs_props
);
766 nvlist_free(zhp
->zfs_user_props
);
767 nvlist_free(zhp
->zfs_recvd_props
);
771 typedef struct mnttab_node
{
772 struct mnttab mtn_mt
;
777 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
779 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
780 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
783 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
785 return (AVL_ISIGN(rv
));
789 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
791 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
792 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
793 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
797 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
801 /* Reopen MNTTAB to prevent reading stale data from open file */
802 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
805 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
809 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
812 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
813 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
814 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
815 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
816 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
818 /* Exclude duplicate mounts */
819 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
820 free(mtn
->mtn_mt
.mnt_special
);
821 free(mtn
->mtn_mt
.mnt_mountp
);
822 free(mtn
->mtn_mt
.mnt_fstype
);
823 free(mtn
->mtn_mt
.mnt_mntopts
);
828 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
835 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
840 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
842 free(mtn
->mtn_mt
.mnt_special
);
843 free(mtn
->mtn_mt
.mnt_mountp
);
844 free(mtn
->mtn_mt
.mnt_fstype
);
845 free(mtn
->mtn_mt
.mnt_mntopts
);
848 avl_destroy(&hdl
->libzfs_mnttab_cache
);
852 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
854 hdl
->libzfs_mnttab_enable
= enable
;
858 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
859 struct mnttab
*entry
)
865 if (!hdl
->libzfs_mnttab_enable
) {
866 struct mnttab srch
= { 0 };
868 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
869 libzfs_mnttab_fini(hdl
);
871 /* Reopen MNTTAB to prevent reading stale data from open file */
872 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
875 srch
.mnt_special
= (char *)fsname
;
876 srch
.mnt_fstype
= MNTTYPE_ZFS
;
877 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
883 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
884 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
887 find
.mtn_mt
.mnt_special
= (char *)fsname
;
888 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
890 *entry
= mtn
->mtn_mt
;
897 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
898 const char *mountp
, const char *mntopts
)
902 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
904 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
905 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
906 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
907 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
908 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
909 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
913 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
918 find
.mtn_mt
.mnt_special
= (char *)fsname
;
919 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
921 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
922 free(ret
->mtn_mt
.mnt_special
);
923 free(ret
->mtn_mt
.mnt_mountp
);
924 free(ret
->mtn_mt
.mnt_fstype
);
925 free(ret
->mtn_mt
.mnt_mntopts
);
931 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
933 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
935 if (zpool_handle
== NULL
)
938 *spa_version
= zpool_get_prop_int(zpool_handle
,
939 ZPOOL_PROP_VERSION
, NULL
);
944 * The choice of reservation property depends on the SPA version.
947 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
951 if (zfs_spa_version(zhp
, &spa_version
) < 0)
954 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
955 *resv_prop
= ZFS_PROP_REFRESERVATION
;
957 *resv_prop
= ZFS_PROP_RESERVATION
;
963 * Given an nvlist of properties to set, validates that they are correct, and
964 * parses any numeric properties (index, boolean, etc) if they are specified as
968 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
969 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
970 boolean_t key_params_ok
, const char *errbuf
)
977 int chosen_normal
= -1;
980 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
981 (void) no_memory(hdl
);
986 * Make sure this property is valid and applies to this type.
990 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
991 const char *propname
= nvpair_name(elem
);
993 prop
= zfs_name_to_prop(propname
);
994 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
996 * This is a user property: make sure it's a
997 * string, and that it's less than ZAP_MAXNAMELEN.
999 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1000 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1001 "'%s' must be a string"), propname
);
1002 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1006 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1007 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1008 "property name '%s' is too long"),
1010 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1014 (void) nvpair_value_string(elem
, &strval
);
1015 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1016 (void) no_memory(hdl
);
1023 * Currently, only user properties can be modified on
1026 if (type
== ZFS_TYPE_SNAPSHOT
) {
1027 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1028 "this property can not be modified for snapshots"));
1029 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1033 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1034 zfs_userquota_prop_t uqtype
;
1035 char *newpropname
= NULL
;
1041 if (userquota_propname_decode(propname
, zoned
,
1042 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1044 dgettext(TEXT_DOMAIN
,
1045 "'%s' has an invalid user/group name"),
1047 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1051 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1052 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1053 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1054 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1055 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1056 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1058 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1060 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1065 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1066 (void) nvpair_value_string(elem
, &strval
);
1067 if (strcmp(strval
, "none") == 0) {
1069 } else if (zfs_nicestrtonum(hdl
,
1070 strval
, &intval
) != 0) {
1071 (void) zfs_error(hdl
,
1072 EZFS_BADPROP
, errbuf
);
1075 } else if (nvpair_type(elem
) ==
1077 (void) nvpair_value_uint64(elem
, &intval
);
1079 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1080 "use 'none' to disable "
1081 "{user|group|project}quota"));
1085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1086 "'%s' must be a number"), propname
);
1087 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1092 * Encode the prop name as
1093 * userquota@<hex-rid>-domain, to make it easy
1094 * for the kernel to decode.
1096 rc
= asprintf(&newpropname
, "%s%llx-%s",
1097 zfs_userquota_prop_prefixes
[uqtype
],
1098 (longlong_t
)rid
, domain
);
1099 if (rc
== -1 || newpropname
== NULL
) {
1100 (void) no_memory(hdl
);
1107 if (nvlist_add_uint64_array(ret
, newpropname
,
1110 (void) no_memory(hdl
);
1115 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1116 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1117 "'%s' is readonly"),
1119 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1123 if (prop
== ZPROP_INVAL
) {
1124 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1125 "invalid property '%s'"), propname
);
1126 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1130 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1132 dgettext(TEXT_DOMAIN
, "'%s' does not "
1133 "apply to datasets of this type"), propname
);
1134 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1138 if (zfs_prop_readonly(prop
) &&
1139 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1140 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1142 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1144 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1148 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1149 &strval
, &intval
, errbuf
) != 0)
1153 * Perform some additional checks for specific properties.
1156 case ZFS_PROP_VERSION
:
1162 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1163 if (intval
< version
) {
1164 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1165 "Can not downgrade; already at version %u"),
1167 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1173 case ZFS_PROP_VOLBLOCKSIZE
:
1174 case ZFS_PROP_RECORDSIZE
:
1176 int maxbs
= SPA_MAXBLOCKSIZE
;
1179 if (zpool_hdl
!= NULL
) {
1180 maxbs
= zpool_get_prop_int(zpool_hdl
,
1181 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1184 * The value must be a power of two between
1185 * SPA_MINBLOCKSIZE and maxbs.
1187 if (intval
< SPA_MINBLOCKSIZE
||
1188 intval
> maxbs
|| !ISP2(intval
)) {
1189 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1190 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1191 "'%s' must be power of 2 from 512B "
1192 "to %s"), propname
, buf
);
1193 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1199 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1200 if (zpool_hdl
!= NULL
) {
1201 char state
[64] = "";
1204 * Issue a warning but do not fail so that
1205 * tests for setable properties succeed.
1207 if (zpool_prop_get_feature(zpool_hdl
,
1208 "feature@allocation_classes", state
,
1209 sizeof (state
)) != 0 ||
1210 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1211 (void) fprintf(stderr
, gettext(
1212 "%s: property requires a special "
1213 "device in the pool\n"), propname
);
1217 (intval
< SPA_MINBLOCKSIZE
||
1218 intval
> SPA_OLD_MAXBLOCKSIZE
|| !ISP2(intval
))) {
1219 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1220 "invalid '%s=%d' property: must be zero or "
1221 "a power of 2 from 512B to 128K"), propname
,
1223 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1228 case ZFS_PROP_MLSLABEL
:
1230 #ifdef HAVE_MLSLABEL
1232 * Verify the mlslabel string and convert to
1233 * internal hex label string.
1237 char *hex
= NULL
; /* internal label string */
1239 /* Default value is already OK. */
1240 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1243 /* Verify the label can be converted to binary form */
1244 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1245 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1246 L_NO_CORRECTION
, NULL
) == -1)) {
1250 /* Now translate to hex internal label string */
1251 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1257 m_label_free(new_sl
);
1259 /* If string is already in internal form, we're done. */
1260 if (strcmp(strval
, hex
) == 0) {
1265 /* Replace the label string with the internal form. */
1266 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1268 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1275 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1276 "invalid mlslabel '%s'"), strval
);
1277 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1278 m_label_free(new_sl
); /* OK if null */
1281 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1282 "mlslabels are unsupported"));
1283 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1285 #endif /* HAVE_MLSLABEL */
1288 case ZFS_PROP_MOUNTPOINT
:
1290 namecheck_err_t why
;
1292 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1293 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1296 if (mountpoint_namecheck(strval
, &why
)) {
1298 case NAME_ERR_LEADING_SLASH
:
1300 dgettext(TEXT_DOMAIN
,
1301 "'%s' must be an absolute path, "
1302 "'none', or 'legacy'"), propname
);
1304 case NAME_ERR_TOOLONG
:
1306 dgettext(TEXT_DOMAIN
,
1307 "component of '%s' is too long"),
1313 dgettext(TEXT_DOMAIN
,
1314 "(%d) not defined"),
1318 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1325 case ZFS_PROP_SHARESMB
:
1326 case ZFS_PROP_SHARENFS
:
1328 * For the mountpoint and sharenfs or sharesmb
1329 * properties, check if it can be set in a
1330 * global/non-global zone based on
1331 * the zoned property value:
1333 * global zone non-global zone
1334 * --------------------------------------------------
1335 * zoned=on mountpoint (no) mountpoint (yes)
1336 * sharenfs (no) sharenfs (no)
1337 * sharesmb (no) sharesmb (no)
1339 * zoned=off mountpoint (yes) N/A
1344 if (getzoneid() == GLOBAL_ZONEID
) {
1345 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1346 "'%s' cannot be set on "
1347 "dataset in a non-global zone"),
1349 (void) zfs_error(hdl
, EZFS_ZONED
,
1352 } else if (prop
== ZFS_PROP_SHARENFS
||
1353 prop
== ZFS_PROP_SHARESMB
) {
1354 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1355 "'%s' cannot be set in "
1356 "a non-global zone"), propname
);
1357 (void) zfs_error(hdl
, EZFS_ZONED
,
1361 } else if (getzoneid() != GLOBAL_ZONEID
) {
1363 * If zoned property is 'off', this must be in
1364 * a global zone. If not, something is wrong.
1366 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1367 "'%s' cannot be set while dataset "
1368 "'zoned' property is set"), propname
);
1369 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1374 * At this point, it is legitimate to set the
1375 * property. Now we want to make sure that the
1376 * property value is valid if it is sharenfs.
1378 if ((prop
== ZFS_PROP_SHARENFS
||
1379 prop
== ZFS_PROP_SHARESMB
) &&
1380 strcmp(strval
, "on") != 0 &&
1381 strcmp(strval
, "off") != 0) {
1382 zfs_share_proto_t proto
;
1384 if (prop
== ZFS_PROP_SHARESMB
)
1390 * Must be an valid sharing protocol
1391 * option string so init the libshare
1392 * in order to enable the parser and
1393 * then parse the options. We use the
1394 * control API since we don't care about
1395 * the current configuration and don't
1396 * want the overhead of loading it
1397 * until we actually do something.
1400 if (zfs_init_libshare(hdl
,
1401 SA_INIT_CONTROL_API
) != SA_OK
) {
1403 * An error occurred so we can't do
1406 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1407 "'%s' cannot be set: problem "
1408 "in share initialization"),
1410 (void) zfs_error(hdl
, EZFS_BADPROP
,
1415 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1417 * There was an error in parsing so
1418 * deal with it by issuing an error
1419 * message and leaving after
1420 * uninitializing the the libshare
1423 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1424 "'%s' cannot be set to invalid "
1425 "options"), propname
);
1426 (void) zfs_error(hdl
, EZFS_BADPROP
,
1428 zfs_uninit_libshare(hdl
);
1431 zfs_uninit_libshare(hdl
);
1436 case ZFS_PROP_KEYLOCATION
:
1437 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1438 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1439 "invalid keylocation"));
1440 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1446 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1448 if (crypt
== ZIO_CRYPT_OFF
&&
1449 strcmp(strval
, "none") != 0) {
1450 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1451 "keylocation must be 'none' "
1452 "for unencrypted datasets"));
1453 (void) zfs_error(hdl
, EZFS_BADPROP
,
1456 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1457 strcmp(strval
, "none") == 0) {
1458 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1459 "keylocation must not be 'none' "
1460 "for encrypted datasets"));
1461 (void) zfs_error(hdl
, EZFS_BADPROP
,
1468 case ZFS_PROP_PBKDF2_ITERS
:
1469 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1470 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1471 "minimum pbkdf2 iterations is %u"),
1472 MIN_PBKDF2_ITERATIONS
);
1473 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1478 case ZFS_PROP_UTF8ONLY
:
1479 chosen_utf
= (int)intval
;
1482 case ZFS_PROP_NORMALIZE
:
1483 chosen_normal
= (int)intval
;
1491 * For changes to existing volumes, we have some additional
1492 * checks to enforce.
1494 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1495 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1496 ZFS_PROP_VOLBLOCKSIZE
);
1500 case ZFS_PROP_VOLSIZE
:
1501 if (intval
% blocksize
!= 0) {
1502 zfs_nicebytes(blocksize
, buf
,
1504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1505 "'%s' must be a multiple of "
1506 "volume block size (%s)"),
1508 (void) zfs_error(hdl
, EZFS_BADPROP
,
1514 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1515 "'%s' cannot be zero"),
1517 (void) zfs_error(hdl
, EZFS_BADPROP
,
1528 /* check encryption properties */
1530 int64_t crypt
= zfs_prop_get_int(zhp
,
1531 ZFS_PROP_ENCRYPTION
);
1534 case ZFS_PROP_COPIES
:
1535 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1537 "encrypted datasets cannot have "
1539 (void) zfs_error(hdl
, EZFS_BADPROP
,
1551 * If normalization was chosen, but no UTF8 choice was made,
1552 * enforce rejection of non-UTF8 names.
1554 * If normalization was chosen, but rejecting non-UTF8 names
1555 * was explicitly not chosen, it is an error.
1557 if (chosen_normal
> 0 && chosen_utf
< 0) {
1558 if (nvlist_add_uint64(ret
,
1559 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1560 (void) no_memory(hdl
);
1563 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1565 "'%s' must be set 'on' if normalization chosen"),
1566 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1567 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1578 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1580 uint64_t old_volsize
;
1581 uint64_t new_volsize
;
1582 uint64_t old_reservation
;
1583 uint64_t new_reservation
;
1584 zfs_prop_t resv_prop
;
1588 * If this is an existing volume, and someone is setting the volsize,
1589 * make sure that it matches the reservation, or add it if necessary.
1591 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1592 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1594 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1596 props
= fnvlist_alloc();
1597 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1598 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1600 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1601 old_reservation
) || nvlist_exists(nvl
,
1602 zfs_prop_to_name(resv_prop
))) {
1603 fnvlist_free(props
);
1606 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1607 &new_volsize
) != 0) {
1608 fnvlist_free(props
);
1611 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1612 fnvlist_free(props
);
1614 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1615 new_reservation
) != 0) {
1616 (void) no_memory(zhp
->zfs_hdl
);
1623 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1624 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1625 * Return codes must match zfs_add_synthetic_resv().
1628 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1635 if (!ZFS_IS_VOLUME(zhp
)) {
1639 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1643 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1647 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1648 /* No value being set, so it can't be "auto" */
1651 if (resvsize
!= UINT64_MAX
) {
1652 /* Being set to a value other than "auto" */
1656 props
= fnvlist_alloc();
1658 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1659 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1661 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1663 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1666 resvsize
= zvol_volsize_to_reservation(volsize
, props
);
1667 fnvlist_free(props
);
1669 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1670 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1671 (void) no_memory(zhp
->zfs_hdl
);
1678 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1685 * For quotas and reservations, ENOSPC indicates
1686 * something different; setting a quota or reservation
1687 * doesn't use any disk space.
1690 case ZFS_PROP_QUOTA
:
1691 case ZFS_PROP_REFQUOTA
:
1692 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1693 "size is less than current used or "
1695 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1698 case ZFS_PROP_RESERVATION
:
1699 case ZFS_PROP_REFRESERVATION
:
1700 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1701 "size is greater than available space"));
1702 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1706 (void) zfs_standard_error(hdl
, err
, errbuf
);
1712 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1716 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1720 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1721 "property value too long"));
1722 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1726 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1727 "pool and or dataset must be upgraded to set this "
1728 "property or value"));
1729 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1733 if (prop
== ZFS_PROP_COMPRESSION
||
1734 prop
== ZFS_PROP_DNODESIZE
||
1735 prop
== ZFS_PROP_RECORDSIZE
) {
1736 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1737 "property setting is not allowed on "
1738 "bootable datasets"));
1739 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1740 } else if (prop
== ZFS_PROP_CHECKSUM
||
1741 prop
== ZFS_PROP_DEDUP
) {
1742 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1743 "property setting is not allowed on "
1745 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1747 (void) zfs_standard_error(hdl
, err
, errbuf
);
1752 if (prop
== ZPROP_INVAL
) {
1753 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1755 (void) zfs_standard_error(hdl
, err
, errbuf
);
1760 if (prop
== ZFS_PROP_KEYLOCATION
) {
1761 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1762 "keylocation may only be set on encryption roots"));
1763 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1765 (void) zfs_standard_error(hdl
, err
, errbuf
);
1771 * This platform can't address a volume this big.
1774 if (prop
== ZFS_PROP_VOLSIZE
) {
1775 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1781 (void) zfs_standard_error(hdl
, err
, errbuf
);
1786 zfs_is_namespace_prop(zfs_prop_t prop
)
1790 case ZFS_PROP_ATIME
:
1791 case ZFS_PROP_RELATIME
:
1792 case ZFS_PROP_DEVICES
:
1794 case ZFS_PROP_SETUID
:
1795 case ZFS_PROP_READONLY
:
1796 case ZFS_PROP_XATTR
:
1797 case ZFS_PROP_NBMAND
:
1806 * Given a property name and value, set the property for the given dataset.
1809 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1813 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1814 nvlist_t
*nvl
= NULL
;
1816 (void) snprintf(errbuf
, sizeof (errbuf
),
1817 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1820 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1821 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1822 (void) no_memory(hdl
);
1826 ret
= zfs_prop_set_list(zhp
, nvl
);
1836 * Given an nvlist of property names and values, set the properties for the
1840 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1842 zfs_cmd_t zc
= {"\0"};
1844 prop_changelist_t
**cls
= NULL
;
1847 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1851 zfs_prop_t prop
= 0;
1854 (void) snprintf(errbuf
, sizeof (errbuf
),
1855 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1858 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1859 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1860 B_FALSE
, errbuf
)) == NULL
)
1864 * We have to check for any extra properties which need to be added
1865 * before computing the length of the nvlist.
1867 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1869 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1870 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1871 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1876 if (added_resv
!= 1 &&
1877 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1882 * Check how many properties we're setting and allocate an array to
1883 * store changelist pointers for postfix().
1885 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1887 elem
= nvlist_next_nvpair(nvl
, elem
))
1889 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1893 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1895 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1897 prop
= zfs_name_to_prop(nvpair_name(elem
));
1899 assert(cl_idx
< nvl_len
);
1901 * We don't want to unmount & remount the dataset when changing
1902 * its canmount property to 'on' or 'noauto'. We only use
1903 * the changelist logic to unmount when setting canmount=off.
1905 if (prop
!= ZFS_PROP_CANMOUNT
||
1906 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1907 zfs_is_mounted(zhp
, NULL
))) {
1908 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1909 if (cls
[cl_idx
] == NULL
)
1913 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1914 changelist_haszonedchild(cls
[cl_idx
])) {
1915 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1916 "child dataset with inherited mountpoint is used "
1917 "in a non-global zone"));
1918 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1922 if (cls
[cl_idx
] != NULL
&&
1923 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1928 assert(cl_idx
== nvl_len
);
1931 * Execute the corresponding ioctl() to set this list of properties.
1933 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1935 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1936 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1939 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1942 /* Get the list of unset properties back and report them. */
1943 nvlist_t
*errorprops
= NULL
;
1944 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1946 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1948 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1949 prop
= zfs_name_to_prop(nvpair_name(elem
));
1950 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1952 nvlist_free(errorprops
);
1954 if (added_resv
&& errno
== ENOSPC
) {
1955 /* clean up the volsize property we tried to set */
1956 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1960 zcmd_free_nvlists(&zc
);
1962 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1964 if (nvlist_add_uint64(nvl
,
1965 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1968 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1970 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1973 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1974 if (cls
[cl_idx
] != NULL
) {
1975 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1983 * Refresh the statistics so the new property
1984 * value is reflected.
1986 (void) get_stats(zhp
);
1989 * Remount the filesystem to propagate the change
1990 * if one of the options handled by the generic
1991 * Linux namespace layer has been modified.
1993 if (zfs_is_namespace_prop(prop
) &&
1994 zfs_is_mounted(zhp
, NULL
))
1995 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2001 zcmd_free_nvlists(&zc
);
2003 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
2004 if (cls
[cl_idx
] != NULL
)
2005 changelist_free(cls
[cl_idx
]);
2013 * Given a property, inherit the value from the parent dataset, or if received
2014 * is TRUE, revert to the received value, if any.
2017 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
2019 zfs_cmd_t zc
= {"\0"};
2021 prop_changelist_t
*cl
;
2022 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2026 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2027 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
2029 zc
.zc_cookie
= received
;
2030 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
2032 * For user properties, the amount of work we have to do is very
2033 * small, so just do it here.
2035 if (!zfs_prop_user(propname
)) {
2036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2037 "invalid property"));
2038 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2041 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2042 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2044 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
2045 return (zfs_standard_error(hdl
, errno
, errbuf
));
2051 * Verify that this property is inheritable.
2053 if (zfs_prop_readonly(prop
))
2054 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
2056 if (!zfs_prop_inheritable(prop
) && !received
)
2057 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
2060 * Check to see if the value applies to this type
2062 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2063 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
2066 * Normalize the name, to get rid of shorthand abbreviations.
2068 propname
= zfs_prop_to_name(prop
);
2069 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2070 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2072 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
2073 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
2074 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2075 "dataset is used in a non-global zone"));
2076 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
2080 * Determine datasets which will be affected by this change, if any.
2082 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2085 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2086 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2087 "child dataset with inherited mountpoint is used "
2088 "in a non-global zone"));
2089 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2093 if ((ret
= changelist_prefix(cl
)) != 0)
2096 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2097 return (zfs_standard_error(hdl
, errno
, errbuf
));
2100 if ((ret
= changelist_postfix(cl
)) != 0)
2104 * Refresh the statistics so the new property is reflected.
2106 (void) get_stats(zhp
);
2109 * Remount the filesystem to propagate the change
2110 * if one of the options handled by the generic
2111 * Linux namespace layer has been modified.
2113 if (zfs_is_namespace_prop(prop
) &&
2114 zfs_is_mounted(zhp
, NULL
))
2115 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2119 changelist_free(cl
);
2124 * True DSL properties are stored in an nvlist. The following two functions
2125 * extract them appropriately.
2128 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2134 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2135 zfs_prop_to_name(prop
), &nv
) == 0) {
2136 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2137 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2139 verify(!zhp
->zfs_props_table
||
2140 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2141 value
= zfs_prop_default_numeric(prop
);
2149 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2155 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2156 zfs_prop_to_name(prop
), &nv
) == 0) {
2157 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2158 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2160 verify(!zhp
->zfs_props_table
||
2161 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2162 value
= zfs_prop_default_string(prop
);
2170 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2172 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2176 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2178 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2179 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2183 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2185 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2190 * Internal function for getting a numeric property. Both zfs_prop_get() and
2191 * zfs_prop_get_int() are built using this interface.
2193 * Certain properties can be overridden using 'mount -o'. In this case, scan
2194 * the contents of the /proc/self/mounts entry, searching for the
2195 * appropriate options. If they differ from the on-disk values, report the
2196 * current values and mark the source "temporary".
2199 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2200 char **source
, uint64_t *val
)
2202 zfs_cmd_t zc
= {"\0"};
2203 nvlist_t
*zplprops
= NULL
;
2205 char *mntopt_on
= NULL
;
2206 char *mntopt_off
= NULL
;
2207 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2212 * If the property is being fetched for a snapshot, check whether
2213 * the property is valid for the snapshot's head dataset type.
2215 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2216 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2217 *val
= zfs_prop_default_numeric(prop
);
2222 case ZFS_PROP_ATIME
:
2223 mntopt_on
= MNTOPT_ATIME
;
2224 mntopt_off
= MNTOPT_NOATIME
;
2227 case ZFS_PROP_RELATIME
:
2228 mntopt_on
= MNTOPT_RELATIME
;
2229 mntopt_off
= MNTOPT_NORELATIME
;
2232 case ZFS_PROP_DEVICES
:
2233 mntopt_on
= MNTOPT_DEVICES
;
2234 mntopt_off
= MNTOPT_NODEVICES
;
2238 mntopt_on
= MNTOPT_EXEC
;
2239 mntopt_off
= MNTOPT_NOEXEC
;
2242 case ZFS_PROP_READONLY
:
2243 mntopt_on
= MNTOPT_RO
;
2244 mntopt_off
= MNTOPT_RW
;
2247 case ZFS_PROP_SETUID
:
2248 mntopt_on
= MNTOPT_SETUID
;
2249 mntopt_off
= MNTOPT_NOSETUID
;
2252 case ZFS_PROP_XATTR
:
2253 mntopt_on
= MNTOPT_XATTR
;
2254 mntopt_off
= MNTOPT_NOXATTR
;
2257 case ZFS_PROP_NBMAND
:
2258 mntopt_on
= MNTOPT_NBMAND
;
2259 mntopt_off
= MNTOPT_NONBMAND
;
2267 * Because looking up the mount options is potentially expensive
2268 * (iterating over all of /proc/self/mounts), we defer its
2269 * calculation until we're looking up a property which requires
2272 if (!zhp
->zfs_mntcheck
&&
2273 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2274 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2275 struct mnttab entry
;
2277 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2278 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2280 if (zhp
->zfs_mntopts
== NULL
)
2284 zhp
->zfs_mntcheck
= B_TRUE
;
2287 if (zhp
->zfs_mntopts
== NULL
)
2288 mnt
.mnt_mntopts
= "";
2290 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2293 case ZFS_PROP_ATIME
:
2294 case ZFS_PROP_RELATIME
:
2295 case ZFS_PROP_DEVICES
:
2297 case ZFS_PROP_READONLY
:
2298 case ZFS_PROP_SETUID
:
2299 case ZFS_PROP_XATTR
:
2300 case ZFS_PROP_NBMAND
:
2301 *val
= getprop_uint64(zhp
, prop
, source
);
2306 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2309 *src
= ZPROP_SRC_TEMPORARY
;
2310 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2313 *src
= ZPROP_SRC_TEMPORARY
;
2317 case ZFS_PROP_CANMOUNT
:
2318 case ZFS_PROP_VOLSIZE
:
2319 case ZFS_PROP_QUOTA
:
2320 case ZFS_PROP_REFQUOTA
:
2321 case ZFS_PROP_RESERVATION
:
2322 case ZFS_PROP_REFRESERVATION
:
2323 case ZFS_PROP_FILESYSTEM_LIMIT
:
2324 case ZFS_PROP_SNAPSHOT_LIMIT
:
2325 case ZFS_PROP_FILESYSTEM_COUNT
:
2326 case ZFS_PROP_SNAPSHOT_COUNT
:
2327 *val
= getprop_uint64(zhp
, prop
, source
);
2329 if (*source
== NULL
) {
2330 /* not default, must be local */
2331 *source
= zhp
->zfs_name
;
2335 case ZFS_PROP_MOUNTED
:
2336 *val
= (zhp
->zfs_mntopts
!= NULL
);
2339 case ZFS_PROP_NUMCLONES
:
2340 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2343 case ZFS_PROP_VERSION
:
2344 case ZFS_PROP_NORMALIZE
:
2345 case ZFS_PROP_UTF8ONLY
:
2347 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2349 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2350 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2351 zcmd_free_nvlists(&zc
);
2352 if (prop
== ZFS_PROP_VERSION
&&
2353 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2354 *val
= zfs_prop_default_numeric(prop
);
2357 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2358 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2360 zcmd_free_nvlists(&zc
);
2363 nvlist_free(zplprops
);
2364 zcmd_free_nvlists(&zc
);
2367 case ZFS_PROP_INCONSISTENT
:
2368 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2372 switch (zfs_prop_get_type(prop
)) {
2373 case PROP_TYPE_NUMBER
:
2374 case PROP_TYPE_INDEX
:
2375 *val
= getprop_uint64(zhp
, prop
, source
);
2377 * If we tried to use a default value for a
2378 * readonly property, it means that it was not
2379 * present. Note this only applies to "truly"
2380 * readonly properties, not set-once properties
2381 * like volblocksize.
2383 if (zfs_prop_readonly(prop
) &&
2384 !zfs_prop_setonce(prop
) &&
2385 *source
!= NULL
&& (*source
)[0] == '\0') {
2391 case PROP_TYPE_STRING
:
2393 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2394 "cannot get non-numeric property"));
2395 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2396 dgettext(TEXT_DOMAIN
, "internal error")));
2404 * Calculate the source type, given the raw source string.
2407 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2408 char *statbuf
, size_t statlen
)
2410 if (statbuf
== NULL
||
2411 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2415 if (source
== NULL
) {
2416 *srctype
= ZPROP_SRC_NONE
;
2417 } else if (source
[0] == '\0') {
2418 *srctype
= ZPROP_SRC_DEFAULT
;
2419 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2420 *srctype
= ZPROP_SRC_RECEIVED
;
2422 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2423 *srctype
= ZPROP_SRC_LOCAL
;
2425 (void) strlcpy(statbuf
, source
, statlen
);
2426 *srctype
= ZPROP_SRC_INHERITED
;
2433 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2434 size_t proplen
, boolean_t literal
)
2439 if (zhp
->zfs_recvd_props
== NULL
)
2440 if (get_recvd_props_ioctl(zhp
) != 0)
2443 prop
= zfs_name_to_prop(propname
);
2445 if (prop
!= ZPROP_INVAL
) {
2447 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2449 zfs_set_recvd_props_mode(zhp
, &cookie
);
2450 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2451 NULL
, NULL
, 0, literal
);
2452 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2456 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2457 propname
, &propval
) != 0)
2459 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2461 (void) strlcpy(propbuf
, recvdval
, proplen
);
2464 return (err
== 0 ? 0 : -1);
2468 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2473 value
= zfs_get_clones_nvl(zhp
);
2478 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2479 pair
= nvlist_next_nvpair(value
, pair
)) {
2480 if (propbuf
[0] != '\0')
2481 (void) strlcat(propbuf
, ",", proplen
);
2482 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2488 struct get_clones_arg
{
2492 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2496 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2498 struct get_clones_arg
*gca
= arg
;
2500 if (gca
->numclones
== 0) {
2505 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2506 NULL
, NULL
, 0, B_TRUE
) != 0)
2508 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2509 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2514 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2520 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2522 nvlist_t
*nv
, *value
;
2524 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2525 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2526 struct get_clones_arg gca
;
2529 * if this is a snapshot, then the kernel wasn't able
2530 * to get the clones. Do it by slowly iterating.
2532 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2534 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2536 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2541 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2543 gca
.origin
= zhp
->zfs_name
;
2545 if (gca
.numclones
!= 0) {
2547 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2550 /* get the pool name */
2551 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2552 (void) strsep(&cp
, "/@");
2553 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2554 ZFS_TYPE_FILESYSTEM
);
2561 (void) get_clones_cb(root
, &gca
);
2564 if (gca
.numclones
!= 0 ||
2565 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2566 nvlist_add_nvlist(zhp
->zfs_props
,
2567 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2574 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2575 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2578 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2584 * Accepts a property and value and checks that the value
2585 * matches the one found by the channel program. If they are
2586 * not equal, print both of them.
2589 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2592 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2595 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2596 const char *prop_name
= zfs_prop_to_name(prop
);
2597 const char *program
=
2599 "ds = args['dataset']\n"
2600 "prop = args['property']\n"
2601 "value, setpoint = zfs.get_prop(ds, prop)\n"
2602 "return {value=value, setpoint=setpoint}\n";
2605 nvlist_t
*argnvl
= fnvlist_alloc();
2607 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2608 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2610 error
= lzc_channel_program_nosync(poolname
, program
,
2611 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2614 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2615 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2617 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2619 (void) fprintf(stderr
, "%s: zcp check error: "
2620 "%u\n", prop_name
, error
);
2623 if (ans
!= intval
) {
2624 (void) fprintf(stderr
, "%s: zfs found %llu, "
2625 "but zcp found %llu\n", prop_name
,
2626 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2630 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2632 (void) fprintf(stderr
, "%s: zcp check error: "
2633 "%u\n", prop_name
, error
);
2636 if (strcmp(strval
, str_ans
) != 0) {
2637 (void) fprintf(stderr
,
2638 "%s: zfs found '%s', but zcp found '%s'\n",
2639 prop_name
, strval
, str_ans
);
2643 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2644 "error: %u\n", prop_name
, error
);
2646 nvlist_free(argnvl
);
2647 nvlist_free(outnvl
);
2651 * Retrieve a property from the given object. If 'literal' is specified, then
2652 * numbers are left as exact values. Otherwise, numbers are converted to a
2653 * human-readable form.
2655 * Returns 0 on success, or -1 on error.
2658 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2659 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2661 char *source
= NULL
;
2665 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2668 * Check to see if this property applies to our object
2670 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2673 if (received
&& zfs_prop_readonly(prop
))
2677 *src
= ZPROP_SRC_NONE
;
2680 case ZFS_PROP_CREATION
:
2682 * 'creation' is a time_t stored in the statistics. We convert
2683 * this into a string unless 'literal' is specified.
2686 val
= getprop_uint64(zhp
, prop
, &source
);
2687 time_t time
= (time_t)val
;
2691 localtime_r(&time
, &t
) == NULL
||
2692 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2694 (void) snprintf(propbuf
, proplen
, "%llu",
2697 zcp_check(zhp
, prop
, val
, NULL
);
2700 case ZFS_PROP_MOUNTPOINT
:
2702 * Getting the precise mountpoint can be tricky.
2704 * - for 'none' or 'legacy', return those values.
2705 * - for inherited mountpoints, we want to take everything
2706 * after our ancestor and append it to the inherited value.
2708 * If the pool has an alternate root, we want to prepend that
2709 * root to any values we return.
2712 str
= getprop_string(zhp
, prop
, &source
);
2714 if (str
[0] == '/') {
2715 char buf
[MAXPATHLEN
];
2717 const char *relpath
;
2720 * If we inherit the mountpoint, even from a dataset
2721 * with a received value, the source will be the path of
2722 * the dataset we inherit from. If source is
2723 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2726 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2729 relpath
= zhp
->zfs_name
+ strlen(source
);
2730 if (relpath
[0] == '/')
2734 if ((zpool_get_prop(zhp
->zpool_hdl
,
2735 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2736 B_FALSE
)) || (strcmp(root
, "-") == 0))
2739 * Special case an alternate root of '/'. This will
2740 * avoid having multiple leading slashes in the
2743 if (strcmp(root
, "/") == 0)
2747 * If the mountpoint is '/' then skip over this
2748 * if we are obtaining either an alternate root or
2749 * an inherited mountpoint.
2751 if (str
[1] == '\0' && (root
[0] != '\0' ||
2752 relpath
[0] != '\0'))
2755 if (relpath
[0] == '\0')
2756 (void) snprintf(propbuf
, proplen
, "%s%s",
2759 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2760 root
, str
, relpath
[0] == '@' ? "" : "/",
2763 /* 'legacy' or 'none' */
2764 (void) strlcpy(propbuf
, str
, proplen
);
2766 zcp_check(zhp
, prop
, 0, propbuf
);
2769 case ZFS_PROP_ORIGIN
:
2770 str
= getprop_string(zhp
, prop
, &source
);
2773 (void) strlcpy(propbuf
, str
, proplen
);
2774 zcp_check(zhp
, prop
, 0, str
);
2777 case ZFS_PROP_CLONES
:
2778 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2782 case ZFS_PROP_QUOTA
:
2783 case ZFS_PROP_REFQUOTA
:
2784 case ZFS_PROP_RESERVATION
:
2785 case ZFS_PROP_REFRESERVATION
:
2787 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2790 * If quota or reservation is 0, we translate this into 'none'
2791 * (unless literal is set), and indicate that it's the default
2792 * value. Otherwise, we print the number nicely and indicate
2793 * that its set locally.
2797 (void) strlcpy(propbuf
, "0", proplen
);
2799 (void) strlcpy(propbuf
, "none", proplen
);
2802 (void) snprintf(propbuf
, proplen
, "%llu",
2805 zfs_nicebytes(val
, propbuf
, proplen
);
2807 zcp_check(zhp
, prop
, val
, NULL
);
2810 case ZFS_PROP_FILESYSTEM_LIMIT
:
2811 case ZFS_PROP_SNAPSHOT_LIMIT
:
2812 case ZFS_PROP_FILESYSTEM_COUNT
:
2813 case ZFS_PROP_SNAPSHOT_COUNT
:
2815 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2819 * If limit is UINT64_MAX, we translate this into 'none' (unless
2820 * literal is set), and indicate that it's the default value.
2821 * Otherwise, we print the number nicely and indicate that it's
2825 (void) snprintf(propbuf
, proplen
, "%llu",
2827 } else if (val
== UINT64_MAX
) {
2828 (void) strlcpy(propbuf
, "none", proplen
);
2830 zfs_nicenum(val
, propbuf
, proplen
);
2833 zcp_check(zhp
, prop
, val
, NULL
);
2836 case ZFS_PROP_REFRATIO
:
2837 case ZFS_PROP_COMPRESSRATIO
:
2838 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2841 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2842 (u_longlong_t
)(val
/ 100),
2843 (u_longlong_t
)(val
% 100));
2845 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2846 (u_longlong_t
)(val
/ 100),
2847 (u_longlong_t
)(val
% 100));
2848 zcp_check(zhp
, prop
, val
, NULL
);
2852 switch (zhp
->zfs_type
) {
2853 case ZFS_TYPE_FILESYSTEM
:
2856 case ZFS_TYPE_VOLUME
:
2859 case ZFS_TYPE_SNAPSHOT
:
2862 case ZFS_TYPE_BOOKMARK
:
2868 (void) snprintf(propbuf
, proplen
, "%s", str
);
2869 zcp_check(zhp
, prop
, 0, propbuf
);
2872 case ZFS_PROP_MOUNTED
:
2874 * The 'mounted' property is a pseudo-property that described
2875 * whether the filesystem is currently mounted. Even though
2876 * it's a boolean value, the typical values of "on" and "off"
2877 * don't make sense, so we translate to "yes" and "no".
2879 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2880 src
, &source
, &val
) != 0)
2883 (void) strlcpy(propbuf
, "yes", proplen
);
2885 (void) strlcpy(propbuf
, "no", proplen
);
2890 * The 'name' property is a pseudo-property derived from the
2891 * dataset name. It is presented as a real property to simplify
2894 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2895 zcp_check(zhp
, prop
, 0, propbuf
);
2898 case ZFS_PROP_MLSLABEL
:
2900 #ifdef HAVE_MLSLABEL
2901 m_label_t
*new_sl
= NULL
;
2902 char *ascii
= NULL
; /* human readable label */
2904 (void) strlcpy(propbuf
,
2905 getprop_string(zhp
, prop
, &source
), proplen
);
2907 if (literal
|| (strcasecmp(propbuf
,
2908 ZFS_MLSLABEL_DEFAULT
) == 0))
2912 * Try to translate the internal hex string to
2913 * human-readable output. If there are any
2914 * problems just use the hex string.
2917 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2918 L_NO_CORRECTION
, NULL
) == -1) {
2919 m_label_free(new_sl
);
2923 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2927 m_label_free(new_sl
);
2930 m_label_free(new_sl
);
2932 (void) strlcpy(propbuf
, ascii
, proplen
);
2935 (void) strlcpy(propbuf
,
2936 getprop_string(zhp
, prop
, &source
), proplen
);
2937 #endif /* HAVE_MLSLABEL */
2942 case ZFS_PROP_CREATETXG
:
2944 * GUIDs are stored as numbers, but they are identifiers.
2945 * We don't want them to be pretty printed, because pretty
2946 * printing mangles the ID into a truncated and useless value.
2948 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2950 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2951 zcp_check(zhp
, prop
, val
, NULL
);
2954 case ZFS_PROP_REFERENCED
:
2955 case ZFS_PROP_AVAILABLE
:
2957 case ZFS_PROP_USEDSNAP
:
2958 case ZFS_PROP_USEDDS
:
2959 case ZFS_PROP_USEDREFRESERV
:
2960 case ZFS_PROP_USEDCHILD
:
2961 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2964 (void) snprintf(propbuf
, proplen
, "%llu",
2967 zfs_nicebytes(val
, propbuf
, proplen
);
2969 zcp_check(zhp
, prop
, val
, NULL
);
2973 switch (zfs_prop_get_type(prop
)) {
2974 case PROP_TYPE_NUMBER
:
2975 if (get_numeric_property(zhp
, prop
, src
,
2976 &source
, &val
) != 0) {
2981 (void) snprintf(propbuf
, proplen
, "%llu",
2984 zfs_nicenum(val
, propbuf
, proplen
);
2986 zcp_check(zhp
, prop
, val
, NULL
);
2989 case PROP_TYPE_STRING
:
2990 str
= getprop_string(zhp
, prop
, &source
);
2994 (void) strlcpy(propbuf
, str
, proplen
);
2995 zcp_check(zhp
, prop
, 0, str
);
2998 case PROP_TYPE_INDEX
:
2999 if (get_numeric_property(zhp
, prop
, src
,
3000 &source
, &val
) != 0)
3002 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
3005 (void) strlcpy(propbuf
, strval
, proplen
);
3006 zcp_check(zhp
, prop
, 0, strval
);
3014 get_source(zhp
, src
, source
, statbuf
, statlen
);
3020 * Utility function to get the given numeric property. Does no validation that
3021 * the given property is the appropriate type; should only be used with
3022 * hard-coded property types.
3025 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
3030 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3036 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3040 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3041 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3045 * Similar to zfs_prop_get(), but returns the value as an integer.
3048 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3049 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3054 * Check to see if this property applies to our object
3056 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3057 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3058 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3059 zfs_prop_to_name(prop
)));
3063 *src
= ZPROP_SRC_NONE
;
3065 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3068 get_source(zhp
, src
, source
, statbuf
, statlen
);
3075 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3076 char **domainp
, idmap_rid_t
*ridp
)
3078 idmap_get_handle_t
*get_hdl
= NULL
;
3082 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3086 err
= idmap_get_sidbyuid(get_hdl
, id
,
3087 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3089 err
= idmap_get_sidbygid(get_hdl
, id
,
3090 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3092 if (err
== IDMAP_SUCCESS
&&
3093 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3094 status
== IDMAP_SUCCESS
)
3100 idmap_get_destroy(get_hdl
);
3103 #endif /* HAVE_IDMAP */
3106 * convert the propname into parameters needed by kernel
3107 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3108 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3109 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3110 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3111 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3112 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3115 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3116 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3118 zfs_userquota_prop_t type
;
3122 boolean_t isproject
;
3128 /* Figure out the property type ({user|group|project}{quota|space}) */
3129 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3130 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3131 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3134 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3138 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3139 type
== ZFS_PROP_USEROBJQUOTA
||
3140 type
== ZFS_PROP_USEROBJUSED
);
3141 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3142 type
== ZFS_PROP_GROUPOBJQUOTA
||
3143 type
== ZFS_PROP_GROUPOBJUSED
);
3144 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3145 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3146 type
== ZFS_PROP_PROJECTOBJUSED
);
3148 cp
= strchr(propname
, '@') + 1;
3150 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3151 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3154 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3155 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3158 } else if (!isproject
&& strchr(cp
, '@')) {
3161 * It's a SID name (eg "user@domain") that needs to be
3162 * turned into S-1-domainID-RID.
3164 directory_error_t e
;
3165 char *numericsid
= NULL
;
3168 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3171 e
= directory_sid_from_user_name(NULL
,
3174 e
= directory_sid_from_group_name(NULL
,
3178 directory_error_free(e
);
3181 if (numericsid
== NULL
)
3184 (void) strlcpy(domain
, cp
, domainlen
);
3185 cp
= strrchr(domain
, '-');
3190 *ridp
= strtoull(cp
, &end
, 10);
3193 if (errno
!= 0 || *end
!= '\0')
3197 #endif /* HAVE_IDMAP */
3199 /* It's a user/group/project ID (eg "12345"). */
3202 id
= strtoul(cp
, &end
, 10);
3205 if (id
> MAXUID
&& !isproject
) {
3207 /* It's an ephemeral ID. */
3211 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3212 &mapdomain
, &rid
) != 0)
3214 (void) strlcpy(domain
, mapdomain
, domainlen
);
3218 #endif /* HAVE_IDMAP */
3228 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3229 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3232 zfs_cmd_t zc
= {"\0"};
3234 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3236 err
= userquota_propname_decode(propname
,
3237 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3238 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3239 zc
.zc_objset_type
= *typep
;
3243 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3247 *propvalue
= zc
.zc_cookie
;
3252 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3253 uint64_t *propvalue
)
3255 zfs_userquota_prop_t type
;
3257 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3262 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3263 char *propbuf
, int proplen
, boolean_t literal
)
3267 zfs_userquota_prop_t type
;
3269 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3276 (void) snprintf(propbuf
, proplen
, "%llu",
3277 (u_longlong_t
)propvalue
);
3278 } else if (propvalue
== 0 &&
3279 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3280 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3281 type
== ZFS_PROP_PROJECTQUOTA
||
3282 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3283 (void) strlcpy(propbuf
, "none", proplen
);
3284 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3285 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3286 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3287 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3289 zfs_nicenum(propvalue
, propbuf
, proplen
);
3295 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3296 uint64_t *propvalue
)
3299 zfs_cmd_t zc
= {"\0"};
3300 const char *snapname
;
3302 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3304 snapname
= strchr(propname
, '@') + 1;
3305 if (strchr(snapname
, '@')) {
3306 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3308 /* snapname is the short name, append it to zhp's fsname */
3311 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3312 sizeof (zc
.zc_value
));
3313 cp
= strchr(zc
.zc_value
, '@');
3316 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
3317 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3320 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3324 *propvalue
= zc
.zc_cookie
;
3329 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3330 char *propbuf
, int proplen
, boolean_t literal
)
3335 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3341 (void) snprintf(propbuf
, proplen
, "%llu",
3342 (u_longlong_t
)propvalue
);
3344 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3351 * Returns the name of the given zfs handle.
3354 zfs_get_name(const zfs_handle_t
*zhp
)
3356 return (zhp
->zfs_name
);
3360 * Returns the name of the parent pool for the given zfs handle.
3363 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3365 return (zhp
->zpool_hdl
->zpool_name
);
3369 * Returns the type of the given zfs handle.
3372 zfs_get_type(const zfs_handle_t
*zhp
)
3374 return (zhp
->zfs_type
);
3378 * Is one dataset name a child dataset of another?
3380 * Needs to handle these cases:
3381 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3382 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3383 * Descendant? No. No. No. Yes.
3386 is_descendant(const char *ds1
, const char *ds2
)
3388 size_t d1len
= strlen(ds1
);
3390 /* ds2 can't be a descendant if it's smaller */
3391 if (strlen(ds2
) < d1len
)
3394 /* otherwise, compare strings and verify that there's a '/' char */
3395 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3399 * Given a complete name, return just the portion that refers to the parent.
3400 * Will return -1 if there is no parent (path is just the name of the
3404 parent_name(const char *path
, char *buf
, size_t buflen
)
3408 (void) strlcpy(buf
, path
, buflen
);
3410 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3418 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3420 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3424 * If accept_ancestor is false, then check to make sure that the given path has
3425 * a parent, and that it exists. If accept_ancestor is true, then find the
3426 * closest existing ancestor for the given path. In prefixlen return the
3427 * length of already existing prefix of the given path. We also fetch the
3428 * 'zoned' property, which is used to validate property settings when creating
3432 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3433 boolean_t accept_ancestor
, int *prefixlen
)
3435 zfs_cmd_t zc
= {"\0"};
3436 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3442 (void) snprintf(errbuf
, sizeof (errbuf
),
3443 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3445 /* get parent, and check to see if this is just a pool */
3446 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3447 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3448 "missing dataset name"));
3449 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3452 /* check to see if the pool exists */
3453 if ((slash
= strchr(parent
, '/')) == NULL
)
3454 slash
= parent
+ strlen(parent
);
3455 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3456 zc
.zc_name
[slash
- parent
] = '\0';
3457 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3460 "no such pool '%s'"), zc
.zc_name
);
3461 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3464 /* check to see if the parent dataset exists */
3465 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3466 if (errno
== ENOENT
&& accept_ancestor
) {
3468 * Go deeper to find an ancestor, give up on top level.
3470 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3471 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3472 "no such pool '%s'"), zc
.zc_name
);
3473 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3475 } else if (errno
== ENOENT
) {
3476 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3477 "parent does not exist"));
3478 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3480 return (zfs_standard_error(hdl
, errno
, errbuf
));
3483 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3487 /* we are in a non-global zone, but parent is in the global zone */
3488 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3489 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3494 /* make sure parent is a filesystem */
3495 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3496 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3497 "parent is not a filesystem"));
3498 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3504 if (prefixlen
!= NULL
)
3505 *prefixlen
= strlen(parent
);
3510 * Finds whether the dataset of the given type(s) exists.
3513 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3517 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3521 * Try to get stats for the dataset, which will tell us if it exists.
3523 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3524 int ds_type
= zhp
->zfs_type
;
3527 if (types
& ds_type
)
3534 * Given a path to 'target', create all the ancestors between
3535 * the prefixlen portion of the path, and the target itself.
3536 * Fail if the initial prefixlen-ancestor does not already exist.
3539 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3545 /* make sure prefix exists */
3546 cp
= target
+ prefixlen
;
3548 assert(strchr(cp
, '/') == NULL
);
3549 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3552 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3560 * Attempt to create, mount, and share any ancestor filesystems,
3561 * up to the prefixlen-long one.
3563 for (cp
= target
+ prefixlen
+ 1;
3564 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3568 h
= make_dataset_handle(hdl
, target
);
3570 /* it already exists, nothing to do here */
3575 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3577 opname
= dgettext(TEXT_DOMAIN
, "create");
3581 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3583 opname
= dgettext(TEXT_DOMAIN
, "open");
3587 if (zfs_mount(h
, NULL
, 0) != 0) {
3588 opname
= dgettext(TEXT_DOMAIN
, "mount");
3592 if (zfs_share(h
) != 0) {
3593 opname
= dgettext(TEXT_DOMAIN
, "share");
3603 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3604 "failed to %s ancestor '%s'"), opname
, target
);
3609 * Creates non-existing ancestors of the given path.
3612 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3619 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3620 "cannot create '%s'"), path
);
3623 * Check that we are not passing the nesting limit
3624 * before we start creating any ancestors.
3626 if (dataset_nestcheck(path
) != 0) {
3627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3628 "maximum name nesting depth exceeded"));
3629 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3632 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3635 if ((path_copy
= strdup(path
)) != NULL
) {
3636 rc
= create_parents(hdl
, path_copy
, prefix
);
3639 if (path_copy
== NULL
|| rc
!= 0)
3646 * Create a new filesystem or volume.
3649 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3654 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3656 enum lzc_dataset_type ost
;
3657 zpool_handle_t
*zpool_handle
;
3658 uint8_t *wkeydata
= NULL
;
3661 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3663 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3664 "cannot create '%s'"), path
);
3666 /* validate the path, taking care to note the extended error message */
3667 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3668 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3670 if (dataset_nestcheck(path
) != 0) {
3671 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3672 "maximum name nesting depth exceeded"));
3673 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3676 /* validate parents exist */
3677 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3681 * The failure modes when creating a dataset of a different type over
3682 * one that already exists is a little strange. In particular, if you
3683 * try to create a dataset on top of an existing dataset, the ioctl()
3684 * will return ENOENT, not EEXIST. To prevent this from happening, we
3685 * first try to see if the dataset exists.
3687 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3688 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3689 "dataset already exists"));
3690 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3693 if (type
== ZFS_TYPE_VOLUME
)
3694 ost
= LZC_DATSET_TYPE_ZVOL
;
3696 ost
= LZC_DATSET_TYPE_ZFS
;
3698 /* open zpool handle for prop validation */
3699 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3700 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3702 /* truncate pool_path at first slash */
3703 char *p
= strchr(pool_path
, '/');
3707 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3710 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3711 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3712 zpool_close(zpool_handle
);
3715 zpool_close(zpool_handle
);
3717 if (type
== ZFS_TYPE_VOLUME
) {
3719 * If we are creating a volume, the size and block size must
3720 * satisfy a few restraints. First, the blocksize must be a
3721 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3722 * volsize must be a multiple of the block size, and cannot be
3725 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3726 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3728 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3729 "missing volume size"));
3730 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3733 if ((ret
= nvlist_lookup_uint64(props
,
3734 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3735 &blocksize
)) != 0) {
3736 if (ret
== ENOENT
) {
3737 blocksize
= zfs_prop_default_numeric(
3738 ZFS_PROP_VOLBLOCKSIZE
);
3741 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3742 "missing volume block size"));
3743 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3749 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3750 "volume size cannot be zero"));
3751 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3754 if (size
% blocksize
!= 0) {
3756 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3757 "volume size must be a multiple of volume block "
3759 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3763 (void) parent_name(path
, parent
, sizeof (parent
));
3764 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3765 &wkeydata
, &wkeylen
) != 0) {
3767 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3770 /* create the dataset */
3771 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3773 if (wkeydata
!= NULL
)
3776 /* check for failure */
3780 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3781 "no such parent '%s'"), parent
);
3782 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3785 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3786 "parent '%s' is not a filesystem"), parent
);
3787 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3790 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3791 "pool must be upgraded to set this "
3792 "property or value"));
3793 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3796 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3797 "encryption root's key is not loaded "
3799 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3802 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3803 "invalid property value(s) specified"));
3804 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3808 * This platform can't address a volume this big.
3810 if (type
== ZFS_TYPE_VOLUME
)
3811 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3816 return (zfs_standard_error(hdl
, errno
, errbuf
));
3824 * Destroys the given dataset. The caller must make sure that the filesystem
3825 * isn't mounted, and that there are no active dependents. If the file system
3826 * does not exist this function does nothing.
3829 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3831 zfs_cmd_t zc
= {"\0"};
3833 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3834 nvlist_t
*nv
= fnvlist_alloc();
3835 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3836 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3839 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3840 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3846 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3848 if (ZFS_IS_VOLUME(zhp
)) {
3849 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3851 zc
.zc_objset_type
= DMU_OST_ZFS
;
3854 zc
.zc_defer_destroy
= defer
;
3855 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3857 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3858 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3862 remove_mountpoint(zhp
);
3867 struct destroydata
{
3869 const char *snapname
;
3873 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3875 struct destroydata
*dd
= arg
;
3876 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3879 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3880 dd
->snapname
) >= sizeof (name
))
3883 if (lzc_exists(name
))
3884 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3886 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3892 * Destroys all snapshots with the given name in zhp & descendants.
3895 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3898 struct destroydata dd
= { 0 };
3900 dd
.snapname
= snapname
;
3901 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3902 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3904 if (nvlist_empty(dd
.nvl
)) {
3905 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3906 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3907 zhp
->zfs_name
, snapname
);
3909 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3911 nvlist_free(dd
.nvl
);
3916 * Destroys all the snapshots named in the nvlist.
3919 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3922 nvlist_t
*errlist
= NULL
;
3925 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3928 nvlist_free(errlist
);
3932 if (nvlist_empty(errlist
)) {
3934 (void) snprintf(errbuf
, sizeof (errbuf
),
3935 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3937 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3939 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3940 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3942 (void) snprintf(errbuf
, sizeof (errbuf
),
3943 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3946 switch (fnvpair_value_int32(pair
)) {
3949 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3950 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3953 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3958 nvlist_free(errlist
);
3963 * Clones the given dataset. The target must be of the same type as the source.
3966 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3968 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3971 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3974 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3976 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3977 "cannot create '%s'"), target
);
3979 /* validate the target/clone name */
3980 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3981 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3983 /* validate parents exist */
3984 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3987 (void) parent_name(target
, parent
, sizeof (parent
));
3994 if (ZFS_IS_VOLUME(zhp
)) {
3995 type
= ZFS_TYPE_VOLUME
;
3997 type
= ZFS_TYPE_FILESYSTEM
;
3999 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
4000 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
4002 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
4008 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
4010 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
4013 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
4021 * The parent doesn't exist. We should have caught this
4022 * above, but there may a race condition that has since
4023 * destroyed the parent.
4025 * At this point, we don't know whether it's the source
4026 * that doesn't exist anymore, or whether the target
4027 * dataset doesn't exist.
4029 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4030 "no such parent '%s'"), parent
);
4031 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4034 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4035 "source and target pools differ"));
4036 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4040 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4049 * Promotes the given clone fs to be the clone parent.
4052 zfs_promote(zfs_handle_t
*zhp
)
4054 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4055 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4059 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4060 "cannot promote '%s'"), zhp
->zfs_name
);
4062 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4063 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4064 "snapshots can not be promoted"));
4065 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4068 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4069 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4070 "not a cloned filesystem"));
4071 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4074 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4075 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4077 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4082 /* There is a conflicting snapshot name. */
4083 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4084 "conflicting snapshot '%s' from parent '%s'"),
4085 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4086 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4089 return (zfs_standard_error(hdl
, ret
, errbuf
));
4095 typedef struct snapdata
{
4097 const char *sd_snapname
;
4101 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4103 snapdata_t
*sd
= arg
;
4104 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4107 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4108 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4109 sd
->sd_snapname
) >= sizeof (name
))
4112 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4114 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4122 zfs_remap_indirects(libzfs_handle_t
*hdl
, const char *fs
)
4127 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4128 "cannot remap dataset '%s'"), fs
);
4130 err
= lzc_remap(fs
);
4135 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4136 "pool must be upgraded"));
4137 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4140 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4143 (void) zfs_standard_error(hdl
, err
, errbuf
);
4152 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4156 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4162 zpool_handle_t
*zpool_hdl
;
4163 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4165 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4166 "cannot create snapshots "));
4169 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4170 const char *snapname
= nvpair_name(elem
);
4172 /* validate the target name */
4173 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4175 (void) snprintf(errbuf
, sizeof (errbuf
),
4176 dgettext(TEXT_DOMAIN
,
4177 "cannot create snapshot '%s'"), snapname
);
4178 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4183 * get pool handle for prop validation. assumes all snaps are in the
4184 * same pool, as does lzc_snapshot (below).
4186 elem
= nvlist_next_nvpair(snaps
, NULL
);
4187 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4188 pool
[strcspn(pool
, "/@")] = '\0';
4189 zpool_hdl
= zpool_open(hdl
, pool
);
4190 if (zpool_hdl
== NULL
)
4193 if (props
!= NULL
&&
4194 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4195 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4196 zpool_close(zpool_hdl
);
4199 zpool_close(zpool_hdl
);
4201 ret
= lzc_snapshot(snaps
, props
, &errors
);
4204 boolean_t printed
= B_FALSE
;
4205 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4207 elem
= nvlist_next_nvpair(errors
, elem
)) {
4208 (void) snprintf(errbuf
, sizeof (errbuf
),
4209 dgettext(TEXT_DOMAIN
,
4210 "cannot create snapshot '%s'"), nvpair_name(elem
));
4211 (void) zfs_standard_error(hdl
,
4212 fnvpair_value_int32(elem
), errbuf
);
4218 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4219 "multiple snapshots of same "
4221 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4225 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4231 nvlist_free(errors
);
4236 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4240 snapdata_t sd
= { 0 };
4241 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4246 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4247 "cannot snapshot %s"), path
);
4249 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4250 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4252 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4253 cp
= strchr(fsname
, '@');
4255 sd
.sd_snapname
= cp
+ 1;
4257 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4258 ZFS_TYPE_VOLUME
)) == NULL
) {
4262 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4264 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4266 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4269 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4270 nvlist_free(sd
.sd_nvl
);
4276 * Destroy any more recent snapshots. We invoke this callback on any dependents
4277 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4278 * is a dependent and we should just destroy it without checking the transaction
4281 typedef struct rollback_data
{
4282 const char *cb_target
; /* the snapshot */
4283 uint64_t cb_create
; /* creation time reference */
4289 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4291 rollback_data_t
*cbp
= data
;
4292 prop_changelist_t
*clp
;
4294 /* We must destroy this clone; first unmount it */
4295 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4296 cbp
->cb_force
? MS_FORCE
: 0);
4297 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4298 cbp
->cb_error
= B_TRUE
;
4302 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4303 cbp
->cb_error
= B_TRUE
;
4305 changelist_remove(clp
, zhp
->zfs_name
);
4306 (void) changelist_postfix(clp
);
4307 changelist_free(clp
);
4314 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4316 rollback_data_t
*cbp
= data
;
4318 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4319 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4320 rollback_destroy_dependent
, cbp
);
4322 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4330 * Given a dataset, rollback to a specific snapshot, discarding any
4331 * data changes since then and making it the active dataset.
4333 * Any snapshots and bookmarks more recent than the target are
4334 * destroyed, along with their dependents (i.e. clones).
4337 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4339 rollback_data_t cb
= { 0 };
4341 boolean_t restore_resv
= 0;
4342 uint64_t old_volsize
= 0, new_volsize
;
4343 zfs_prop_t resv_prop
= { 0 };
4345 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4346 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4349 * Destroy all recent snapshots and their dependents.
4351 cb
.cb_force
= force
;
4352 cb
.cb_target
= snap
->zfs_name
;
4353 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4354 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
4355 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4361 * Now that we have verified that the snapshot is the latest,
4362 * rollback to the given snapshot.
4365 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4366 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4368 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4370 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4374 * Pass both the filesystem and the wanted snapshot names,
4375 * we would get an error back if the snapshot is destroyed or
4376 * a new snapshot is created before this request is processed.
4378 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4382 (void) snprintf(errbuf
, sizeof (errbuf
),
4383 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4387 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4388 "there is a snapshot or bookmark more recent "
4389 "than '%s'"), snap
->zfs_name
);
4390 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4393 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4394 "'%s' is not found among snapshots of '%s'"),
4395 snap
->zfs_name
, zhp
->zfs_name
);
4396 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4399 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4402 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4408 * For volumes, if the pre-rollback volsize matched the pre-
4409 * rollback reservation and the volsize has changed then set
4410 * the reservation property to the post-rollback volsize.
4411 * Make a new handle since the rollback closed the dataset.
4413 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4414 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4416 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4417 if (old_volsize
!= new_volsize
)
4418 err
= zfs_prop_set_int(zhp
, resv_prop
,
4427 * Renames the given dataset.
4430 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4431 boolean_t force_unmount
)
4434 zfs_cmd_t zc
= {"\0"};
4436 prop_changelist_t
*cl
= NULL
;
4437 zfs_handle_t
*zhrp
= NULL
;
4438 char *parentname
= NULL
;
4439 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4440 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4443 /* if we have the same exact name, just return success */
4444 if (strcmp(zhp
->zfs_name
, target
) == 0)
4447 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4448 "cannot rename to '%s'"), target
);
4450 /* make sure source name is valid */
4451 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4452 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4455 * Make sure the target name is valid
4457 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4458 if ((strchr(target
, '@') == NULL
) ||
4461 * Snapshot target name is abbreviated,
4462 * reconstruct full dataset name
4464 (void) strlcpy(parent
, zhp
->zfs_name
,
4466 delim
= strchr(parent
, '@');
4467 if (strchr(target
, '@') == NULL
)
4471 (void) strlcat(parent
, target
, sizeof (parent
));
4475 * Make sure we're renaming within the same dataset.
4477 delim
= strchr(target
, '@');
4478 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4479 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4480 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4481 "snapshots must be part of same "
4483 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4488 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4489 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4492 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4493 "recursive rename must be a snapshot"));
4494 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4497 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4498 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4500 /* validate parents */
4501 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4504 /* make sure we're in the same pool */
4505 verify((delim
= strchr(target
, '/')) != NULL
);
4506 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4507 zhp
->zfs_name
[delim
- target
] != '/') {
4508 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4509 "datasets must be within same pool"));
4510 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4513 /* new name cannot be a child of the current dataset name */
4514 if (is_descendant(zhp
->zfs_name
, target
)) {
4515 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4516 "New dataset name cannot be a descendant of "
4517 "current dataset name"));
4518 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4522 (void) snprintf(errbuf
, sizeof (errbuf
),
4523 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4525 if (getzoneid() == GLOBAL_ZONEID
&&
4526 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4527 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4528 "dataset is used in a non-global zone"));
4529 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4533 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4534 if (parentname
== NULL
) {
4538 delim
= strchr(parentname
, '@');
4540 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4545 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4546 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4547 force_unmount
? MS_FORCE
: 0)) == NULL
)
4550 if (changelist_haszonedchild(cl
)) {
4551 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4552 "child dataset with inherited mountpoint is used "
4553 "in a non-global zone"));
4554 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4559 if ((ret
= changelist_prefix(cl
)) != 0)
4563 if (ZFS_IS_VOLUME(zhp
))
4564 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4566 zc
.zc_objset_type
= DMU_OST_ZFS
;
4568 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4569 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4571 zc
.zc_cookie
= recursive
;
4573 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4575 * if it was recursive, the one that actually failed will
4578 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4579 "cannot rename '%s'"), zc
.zc_name
);
4581 if (recursive
&& errno
== EEXIST
) {
4582 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4583 "a child dataset already has a snapshot "
4584 "with the new name"));
4585 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4586 } else if (errno
== EACCES
) {
4587 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) ==
4589 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4590 "cannot rename an unencrypted dataset to "
4591 "be a decendent of an encrypted one"));
4593 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4594 "cannot move encryption child outside of "
4595 "its encryption root"));
4597 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4599 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4603 * On failure, we still want to remount any filesystems that
4604 * were previously mounted, so we don't alter the system state.
4607 (void) changelist_postfix(cl
);
4610 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4611 ret
= changelist_postfix(cl
);
4616 if (parentname
!= NULL
) {
4623 changelist_free(cl
);
4629 zfs_get_all_props(zfs_handle_t
*zhp
)
4631 return (zhp
->zfs_props
);
4635 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4637 if (zhp
->zfs_recvd_props
== NULL
)
4638 if (get_recvd_props_ioctl(zhp
) != 0)
4640 return (zhp
->zfs_recvd_props
);
4644 zfs_get_user_props(zfs_handle_t
*zhp
)
4646 return (zhp
->zfs_user_props
);
4650 * This function is used by 'zfs list' to determine the exact set of columns to
4651 * display, and their maximum widths. This does two main things:
4653 * - If this is a list of all properties, then expand the list to include
4654 * all native properties, and set a flag so that for each dataset we look
4655 * for new unique user properties and add them to the list.
4657 * - For non fixed-width properties, keep track of the maximum width seen
4658 * so that we can size the column appropriately. If the user has
4659 * requested received property values, we also need to compute the width
4660 * of the RECEIVED column.
4663 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4666 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4667 zprop_list_t
*entry
;
4668 zprop_list_t
**last
, **start
;
4669 nvlist_t
*userprops
, *propval
;
4672 char buf
[ZFS_MAXPROPLEN
];
4674 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4677 userprops
= zfs_get_user_props(zhp
);
4680 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4682 * Go through and add any user properties as necessary. We
4683 * start by incrementing our list pointer to the first
4684 * non-native property.
4687 while (*start
!= NULL
) {
4688 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4690 start
= &(*start
)->pl_next
;
4694 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4696 * See if we've already found this property in our list.
4698 for (last
= start
; *last
!= NULL
;
4699 last
= &(*last
)->pl_next
) {
4700 if (strcmp((*last
)->pl_user_prop
,
4701 nvpair_name(elem
)) == 0)
4705 if (*last
== NULL
) {
4706 if ((entry
= zfs_alloc(hdl
,
4707 sizeof (zprop_list_t
))) == NULL
||
4708 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4709 nvpair_name(elem
)))) == NULL
) {
4714 entry
->pl_prop
= ZPROP_INVAL
;
4715 entry
->pl_width
= strlen(nvpair_name(elem
));
4716 entry
->pl_all
= B_TRUE
;
4723 * Now go through and check the width of any non-fixed columns
4725 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4726 if (entry
->pl_fixed
&& !literal
)
4729 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4730 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4731 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4732 if (strlen(buf
) > entry
->pl_width
)
4733 entry
->pl_width
= strlen(buf
);
4735 if (received
&& zfs_prop_get_recvd(zhp
,
4736 zfs_prop_to_name(entry
->pl_prop
),
4737 buf
, sizeof (buf
), literal
) == 0)
4738 if (strlen(buf
) > entry
->pl_recvd_width
)
4739 entry
->pl_recvd_width
= strlen(buf
);
4741 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4743 verify(nvlist_lookup_string(propval
,
4744 ZPROP_VALUE
, &strval
) == 0);
4745 if (strlen(strval
) > entry
->pl_width
)
4746 entry
->pl_width
= strlen(strval
);
4748 if (received
&& zfs_prop_get_recvd(zhp
,
4749 entry
->pl_user_prop
,
4750 buf
, sizeof (buf
), literal
) == 0)
4751 if (strlen(buf
) > entry
->pl_recvd_width
)
4752 entry
->pl_recvd_width
= strlen(buf
);
4760 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4766 * Keep a reference to the props-table against which we prune the
4769 zhp
->zfs_props_table
= props
;
4771 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4774 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4775 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4778 * User properties will result in ZPROP_INVAL, and since we
4779 * only know how to prune standard ZFS properties, we always
4780 * leave these in the list. This can also happen if we
4781 * encounter an unknown DSL property (when running older
4782 * software, for example).
4784 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4785 (void) nvlist_remove(zhp
->zfs_props
,
4786 nvpair_name(curr
), nvpair_type(curr
));
4792 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4793 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4795 zfs_cmd_t zc
= {"\0"};
4796 nvlist_t
*nvlist
= NULL
;
4799 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4800 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4801 zc
.zc_cookie
= (uint64_t)cmd
;
4803 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4804 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4805 (void) no_memory(hdl
);
4811 case ZFS_SMB_ACL_ADD
:
4812 case ZFS_SMB_ACL_REMOVE
:
4813 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4815 case ZFS_SMB_ACL_RENAME
:
4816 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4818 (void) no_memory(hdl
);
4821 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4823 (void) no_memory(hdl
);
4826 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4827 nvlist_free(nvlist
);
4831 case ZFS_SMB_ACL_PURGE
:
4836 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4837 nvlist_free(nvlist
);
4842 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4843 char *path
, char *resource
)
4845 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4850 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4851 char *path
, char *resource
)
4853 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4858 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4860 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4865 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4866 char *oldname
, char *newname
)
4868 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4873 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4874 zfs_userspace_cb_t func
, void *arg
)
4876 zfs_cmd_t zc
= {"\0"};
4877 zfs_useracct_t buf
[100];
4878 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4881 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4883 zc
.zc_objset_type
= type
;
4884 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4887 zfs_useracct_t
*zua
= buf
;
4889 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4890 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4893 if ((errno
== ENOTSUP
&&
4894 (type
== ZFS_PROP_USEROBJUSED
||
4895 type
== ZFS_PROP_GROUPOBJUSED
||
4896 type
== ZFS_PROP_USEROBJQUOTA
||
4897 type
== ZFS_PROP_GROUPOBJQUOTA
||
4898 type
== ZFS_PROP_PROJECTOBJUSED
||
4899 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4900 type
== ZFS_PROP_PROJECTUSED
||
4901 type
== ZFS_PROP_PROJECTQUOTA
)))
4904 (void) snprintf(errbuf
, sizeof (errbuf
),
4905 dgettext(TEXT_DOMAIN
,
4906 "cannot get used/quota for %s"), zc
.zc_name
);
4907 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4909 if (zc
.zc_nvlist_dst_size
== 0)
4912 while (zc
.zc_nvlist_dst_size
> 0) {
4913 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4914 zua
->zu_space
)) != 0)
4917 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4926 const char *snapname
;
4928 boolean_t recursive
;
4933 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4935 struct holdarg
*ha
= arg
;
4936 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4939 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4940 ha
->snapname
) >= sizeof (name
))
4943 if (lzc_exists(name
))
4944 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4947 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4953 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4954 boolean_t recursive
, int cleanup_fd
)
4959 ha
.nvl
= fnvlist_alloc();
4960 ha
.snapname
= snapname
;
4962 ha
.recursive
= recursive
;
4963 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4965 if (nvlist_empty(ha
.nvl
)) {
4968 fnvlist_free(ha
.nvl
);
4970 (void) snprintf(errbuf
, sizeof (errbuf
),
4971 dgettext(TEXT_DOMAIN
,
4972 "cannot hold snapshot '%s@%s'"),
4973 zhp
->zfs_name
, snapname
);
4974 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4978 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4979 fnvlist_free(ha
.nvl
);
4985 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4989 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4994 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4997 /* There may be errors even in the success case. */
4998 fnvlist_free(errors
);
5002 if (nvlist_empty(errors
)) {
5003 /* no hold-specific errors */
5004 (void) snprintf(errbuf
, sizeof (errbuf
),
5005 dgettext(TEXT_DOMAIN
, "cannot hold"));
5008 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5009 "pool must be upgraded"));
5010 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5013 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5016 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5020 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5022 elem
= nvlist_next_nvpair(errors
, elem
)) {
5023 (void) snprintf(errbuf
, sizeof (errbuf
),
5024 dgettext(TEXT_DOMAIN
,
5025 "cannot hold snapshot '%s'"), nvpair_name(elem
));
5026 switch (fnvpair_value_int32(elem
)) {
5029 * Temporary tags wind up having the ds object id
5030 * prepended. So even if we passed the length check
5031 * above, it's still possible for the tag to wind
5032 * up being slightly too long.
5034 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5037 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5040 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5043 (void) zfs_standard_error(hdl
,
5044 fnvpair_value_int32(elem
), errbuf
);
5048 fnvlist_free(errors
);
5053 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5055 struct holdarg
*ha
= arg
;
5056 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5058 nvlist_t
*existing_holds
;
5060 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5061 ha
->snapname
) >= sizeof (name
)) {
5066 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5068 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5071 nvlist_t
*torelease
= fnvlist_alloc();
5072 fnvlist_add_boolean(torelease
, ha
->tag
);
5073 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5074 fnvlist_free(torelease
);
5078 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5084 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5085 boolean_t recursive
)
5089 nvlist_t
*errors
= NULL
;
5091 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5094 ha
.nvl
= fnvlist_alloc();
5095 ha
.snapname
= snapname
;
5097 ha
.recursive
= recursive
;
5099 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5101 if (nvlist_empty(ha
.nvl
)) {
5102 fnvlist_free(ha
.nvl
);
5104 (void) snprintf(errbuf
, sizeof (errbuf
),
5105 dgettext(TEXT_DOMAIN
,
5106 "cannot release hold from snapshot '%s@%s'"),
5107 zhp
->zfs_name
, snapname
);
5109 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5111 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5116 ret
= lzc_release(ha
.nvl
, &errors
);
5117 fnvlist_free(ha
.nvl
);
5120 /* There may be errors even in the success case. */
5121 fnvlist_free(errors
);
5125 if (nvlist_empty(errors
)) {
5126 /* no hold-specific errors */
5127 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5131 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5132 "pool must be upgraded"));
5133 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5136 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5140 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5142 elem
= nvlist_next_nvpair(errors
, elem
)) {
5143 (void) snprintf(errbuf
, sizeof (errbuf
),
5144 dgettext(TEXT_DOMAIN
,
5145 "cannot release hold from snapshot '%s'"),
5147 switch (fnvpair_value_int32(elem
)) {
5149 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5152 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5155 (void) zfs_standard_error_fmt(hdl
,
5156 fnvpair_value_int32(elem
), errbuf
);
5160 fnvlist_free(errors
);
5165 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5167 zfs_cmd_t zc
= {"\0"};
5168 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5174 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5175 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5179 nvbuf
= malloc(nvsz
);
5180 if (nvbuf
== NULL
) {
5181 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5185 zc
.zc_nvlist_dst_size
= nvsz
;
5186 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5188 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5190 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5191 (void) snprintf(errbuf
, sizeof (errbuf
),
5192 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5197 nvsz
= zc
.zc_nvlist_dst_size
;
5201 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5202 "pool must be upgraded"));
5203 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5206 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5209 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5212 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5217 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5219 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
5220 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5222 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
5232 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5234 zfs_cmd_t zc
= {"\0"};
5235 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5241 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5242 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5244 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5247 nvbuf
= malloc(nvsz
);
5249 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5252 zc
.zc_nvlist_src_size
= nvsz
;
5253 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5254 zc
.zc_perm_action
= un
;
5256 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5258 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5259 (void) snprintf(errbuf
, sizeof (errbuf
),
5260 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5264 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5265 "pool must be upgraded"));
5266 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5269 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5272 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5275 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5286 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5291 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5294 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5296 (void) snprintf(errbuf
, sizeof (errbuf
),
5297 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5301 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5302 "pool must be upgraded"));
5303 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5306 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5309 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5312 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5321 * Convert the zvol's volume size to an appropriate reservation.
5322 * Note: If this routine is updated, it is necessary to update the ZFS test
5323 * suite's shell version in reservation.kshlib.
5326 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
5329 uint64_t nblocks
, volblocksize
;
5333 if (nvlist_lookup_string(props
,
5334 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5335 ncopies
= atoi(strval
);
5338 if (nvlist_lookup_uint64(props
,
5339 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5340 &volblocksize
) != 0)
5341 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5342 nblocks
= volsize
/volblocksize
;
5343 /* start with metadnode L0-L6 */
5345 /* calculate number of indirects */
5346 while (nblocks
> 1) {
5347 nblocks
+= DNODES_PER_LEVEL
- 1;
5348 nblocks
/= DNODES_PER_LEVEL
;
5351 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5354 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5355 * compressed, but in practice they compress down to about
5358 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;