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 2019 Joyent, Inc.
25 * Copyright (c) 2011, 2020 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-2018 RackTop Systems.
33 * Copyright (c) 2019 Datto Inc.
34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
35 * Copyright (c) 2021 Matt Fiddaman
48 #include <sys/mntent.h>
49 #include <sys/mount.h>
56 #include <directory.h>
57 #endif /* HAVE_IDMAP */
59 #include <sys/dnode.h>
62 #include <sys/dsl_crypt.h>
66 #include "zfs_namecheck.h"
68 #include "libzfs_impl.h"
69 #include "zfs_deleg.h"
71 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
72 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
75 * Given a single type (not a mask of types), return the type in a human
79 zfs_type_to_name(zfs_type_t type
)
82 case ZFS_TYPE_FILESYSTEM
:
83 return (dgettext(TEXT_DOMAIN
, "filesystem"));
84 case ZFS_TYPE_SNAPSHOT
:
85 return (dgettext(TEXT_DOMAIN
, "snapshot"));
87 return (dgettext(TEXT_DOMAIN
, "volume"));
89 return (dgettext(TEXT_DOMAIN
, "pool"));
90 case ZFS_TYPE_BOOKMARK
:
91 return (dgettext(TEXT_DOMAIN
, "bookmark"));
93 assert(!"unhandled zfs_type_t");
100 * Validate a ZFS path. This is used even before trying to open the dataset, to
101 * provide a more meaningful error message. We call zfs_error_aux() to
102 * explain exactly why the name was not valid.
105 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
111 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
114 "snapshot delimiter '@' is not expected here"));
118 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
121 "missing '@' delimiter in snapshot name"));
125 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
127 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
128 "bookmark delimiter '#' is not expected here"));
132 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
134 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
135 "missing '#' delimiter in bookmark name"));
139 if (modifying
&& strchr(path
, '%') != NULL
) {
141 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
142 "invalid character %c in name"), '%');
146 if (entity_namecheck(path
, &why
, &what
) != 0) {
149 case NAME_ERR_TOOLONG
:
150 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
151 "name is too long"));
154 case NAME_ERR_LEADING_SLASH
:
155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
156 "leading slash in name"));
159 case NAME_ERR_EMPTY_COMPONENT
:
160 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
161 "empty component or misplaced '@'"
162 " or '#' delimiter in name"));
165 case NAME_ERR_TRAILING_SLASH
:
166 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
167 "trailing slash in name"));
170 case NAME_ERR_INVALCHAR
:
172 dgettext(TEXT_DOMAIN
, "invalid character "
173 "'%c' in name"), what
);
176 case NAME_ERR_MULTIPLE_DELIMITERS
:
177 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
178 "multiple '@' and/or '#' delimiters in "
182 case NAME_ERR_NOLETTER
:
183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
184 "pool doesn't begin with a letter"));
187 case NAME_ERR_RESERVED
:
188 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
189 "name is reserved"));
192 case NAME_ERR_DISKLIKE
:
193 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
194 "reserved disk name"));
197 case NAME_ERR_SELF_REF
:
198 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
199 "self reference, '.' is found in name"));
202 case NAME_ERR_PARENT_REF
:
203 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
204 "parent reference, '..' is found in name"));
208 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
209 "(%d) not defined"), why
);
221 zfs_name_valid(const char *name
, zfs_type_t type
)
223 if (type
== ZFS_TYPE_POOL
)
224 return (zpool_name_valid(NULL
, B_FALSE
, name
));
225 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
229 * This function takes the raw DSL properties, and filters out the user-defined
230 * properties into a separate nvlist.
233 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
235 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
240 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
241 (void) no_memory(hdl
);
246 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
247 if (!zfs_prop_user(nvpair_name(elem
)))
250 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
251 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
253 (void) no_memory(hdl
);
261 static zpool_handle_t
*
262 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
264 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
267 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
268 if (hdl
->libzfs_pool_handles
!= NULL
)
269 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
270 hdl
->libzfs_pool_handles
= zph
;
275 static zpool_handle_t
*
276 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
278 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
279 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
281 while ((zph
!= NULL
) &&
282 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
283 zph
= zph
->zpool_next
;
288 * Returns a handle to the pool that contains the provided dataset.
289 * If a handle to that pool already exists then that handle is returned.
290 * Otherwise, a new handle is created and added to the list of handles.
292 static zpool_handle_t
*
293 zpool_handle(zfs_handle_t
*zhp
)
299 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
300 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
301 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
303 zph
= zpool_find_handle(zhp
, pool_name
, len
);
305 zph
= zpool_add_handle(zhp
, pool_name
);
312 zpool_free_handles(libzfs_handle_t
*hdl
)
314 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
316 while (zph
!= NULL
) {
317 next
= zph
->zpool_next
;
321 hdl
->libzfs_pool_handles
= NULL
;
325 * Utility function to gather stats (objset and zpl) for the given object.
328 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
330 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
332 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
334 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
335 if (errno
== ENOMEM
) {
336 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
347 * Utility function to get the received properties of the given object.
350 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
352 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
353 nvlist_t
*recvdprops
;
354 zfs_cmd_t zc
= {"\0"};
357 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
360 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
362 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
363 if (errno
== ENOMEM
) {
364 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
368 zcmd_free_nvlists(&zc
);
373 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
374 zcmd_free_nvlists(&zc
);
378 nvlist_free(zhp
->zfs_recvd_props
);
379 zhp
->zfs_recvd_props
= recvdprops
;
385 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
387 nvlist_t
*allprops
, *userprops
;
389 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
391 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
396 * XXX Why do we store the user props separately, in addition to
397 * storing them in zfs_props?
399 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
400 nvlist_free(allprops
);
404 nvlist_free(zhp
->zfs_props
);
405 nvlist_free(zhp
->zfs_user_props
);
407 zhp
->zfs_props
= allprops
;
408 zhp
->zfs_user_props
= userprops
;
414 get_stats(zfs_handle_t
*zhp
)
417 zfs_cmd_t zc
= {"\0"};
419 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
421 if (get_stats_ioctl(zhp
, &zc
) != 0)
423 else if (put_stats_zhdl(zhp
, &zc
) != 0)
425 zcmd_free_nvlists(&zc
);
430 * Refresh the properties currently stored in the handle.
433 zfs_refresh_properties(zfs_handle_t
*zhp
)
435 (void) get_stats(zhp
);
439 * Makes a handle from the given dataset name. Used by zfs_open() and
440 * zfs_iter_* to create child handles on the fly.
443 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
445 if (put_stats_zhdl(zhp
, zc
) != 0)
449 * We've managed to open the dataset and gather statistics. Determine
450 * the high-level type.
452 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
453 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
454 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
455 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
456 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
461 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
462 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
463 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
464 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
465 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
466 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
468 abort(); /* we should never see any other types */
470 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
477 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
479 zfs_cmd_t zc
= {"\0"};
481 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
487 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
488 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
492 if (get_stats_ioctl(zhp
, &zc
) == -1) {
493 zcmd_free_nvlists(&zc
);
497 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
501 zcmd_free_nvlists(&zc
);
506 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
508 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
514 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
515 if (make_dataset_handle_common(zhp
, zc
) == -1) {
523 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
525 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
530 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
531 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
532 zhp
->zfs_head_type
= pzhp
->zfs_type
;
533 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
534 zhp
->zpool_hdl
= zpool_handle(zhp
);
540 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
542 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
547 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
548 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
549 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
550 sizeof (zhp
->zfs_name
));
551 zhp
->zfs_type
= zhp_orig
->zfs_type
;
552 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
553 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
554 if (zhp_orig
->zfs_props
!= NULL
) {
555 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
556 (void) no_memory(zhp
->zfs_hdl
);
561 if (zhp_orig
->zfs_user_props
!= NULL
) {
562 if (nvlist_dup(zhp_orig
->zfs_user_props
,
563 &zhp
->zfs_user_props
, 0) != 0) {
564 (void) no_memory(zhp
->zfs_hdl
);
569 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
570 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
571 &zhp
->zfs_recvd_props
, 0)) {
572 (void) no_memory(zhp
->zfs_hdl
);
577 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
578 if (zhp_orig
->zfs_mntopts
!= NULL
) {
579 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
580 zhp_orig
->zfs_mntopts
);
582 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
587 zfs_bookmark_exists(const char *path
)
591 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
597 (void) strlcpy(fsname
, path
, sizeof (fsname
));
598 pound
= strchr(fsname
, '#');
603 bmark_name
= pound
+ 1;
604 props
= fnvlist_alloc();
605 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
612 rv
= nvlist_exists(bmarks
, bmark_name
);
618 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
619 nvlist_t
*bmark_props
)
621 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
626 /* Fill in the name. */
627 zhp
->zfs_hdl
= parent
->zfs_hdl
;
628 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
630 /* Set the property lists. */
631 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
637 zhp
->zfs_head_type
= parent
->zfs_head_type
;
638 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
640 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
641 nvlist_free(zhp
->zfs_props
);
649 struct zfs_open_bookmarks_cb_data
{
655 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
657 struct zfs_open_bookmarks_cb_data
*dp
= data
;
660 * Is it the one we are looking for?
662 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
664 * We found it. Save it and let the caller know we are done.
671 * Not found. Close the handle and ask for another one.
678 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
679 * argument is a mask of acceptable types. The function will print an
680 * appropriate error message and return NULL if it can't be opened.
683 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
689 (void) snprintf(errbuf
, sizeof (errbuf
),
690 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
693 * Validate the name before we even try to open it.
695 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
696 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
701 * Bookmarks needs to be handled separately.
703 bookp
= strchr(path
, '#');
706 * Try to get stats for the dataset, which will tell us if it
710 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
711 (void) zfs_standard_error(hdl
, errno
, errbuf
);
715 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
717 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
720 * We need to cut out '#' and everything after '#'
721 * to get the parent dataset name only.
723 assert(bookp
- path
< sizeof (dsname
));
724 (void) strncpy(dsname
, path
, bookp
- path
);
725 dsname
[bookp
- path
] = '\0';
728 * Create handle for the parent dataset.
731 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
732 (void) zfs_standard_error(hdl
, errno
, errbuf
);
737 * Iterate bookmarks to find the right one.
740 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
741 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
742 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
746 if (cb_data
.zhp
== NULL
) {
747 (void) zfs_standard_error(hdl
, errno
, errbuf
);
759 if (!(types
& zhp
->zfs_type
)) {
760 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
769 * Release a ZFS handle. Nothing to do but free the associated memory.
772 zfs_close(zfs_handle_t
*zhp
)
774 if (zhp
->zfs_mntopts
)
775 free(zhp
->zfs_mntopts
);
776 nvlist_free(zhp
->zfs_props
);
777 nvlist_free(zhp
->zfs_user_props
);
778 nvlist_free(zhp
->zfs_recvd_props
);
782 typedef struct mnttab_node
{
783 struct mnttab mtn_mt
;
788 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
790 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
791 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
794 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
796 return (TREE_ISIGN(rv
));
800 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
802 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
803 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
804 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
805 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
809 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
814 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
817 while (getmntent(mnttab
, &entry
) == 0) {
821 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
824 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
825 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
826 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
827 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
828 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
830 /* Exclude duplicate mounts */
831 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
832 free(mtn
->mtn_mt
.mnt_special
);
833 free(mtn
->mtn_mt
.mnt_mountp
);
834 free(mtn
->mtn_mt
.mnt_fstype
);
835 free(mtn
->mtn_mt
.mnt_mntopts
);
840 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
843 (void) fclose(mnttab
);
848 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
853 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
855 free(mtn
->mtn_mt
.mnt_special
);
856 free(mtn
->mtn_mt
.mnt_mountp
);
857 free(mtn
->mtn_mt
.mnt_fstype
);
858 free(mtn
->mtn_mt
.mnt_mntopts
);
861 avl_destroy(&hdl
->libzfs_mnttab_cache
);
862 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
866 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
868 hdl
->libzfs_mnttab_enable
= enable
;
872 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
873 struct mnttab
*entry
)
880 if (!hdl
->libzfs_mnttab_enable
) {
881 struct mnttab srch
= { 0 };
883 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
884 libzfs_mnttab_fini(hdl
);
886 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
889 srch
.mnt_special
= (char *)fsname
;
890 srch
.mnt_fstype
= MNTTYPE_ZFS
;
891 ret
= getmntany(mnttab
, entry
, &srch
) ? ENOENT
: 0;
892 (void) fclose(mnttab
);
896 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
897 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
900 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
901 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
906 find
.mtn_mt
.mnt_special
= (char *)fsname
;
907 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
909 *entry
= mtn
->mtn_mt
;
912 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
917 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
918 const char *mountp
, const char *mntopts
)
922 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
923 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
924 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
925 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
926 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
927 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
928 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
930 * Another thread may have already added this entry
931 * via libzfs_mnttab_update. If so we should skip it.
933 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
) {
934 free(mtn
->mtn_mt
.mnt_special
);
935 free(mtn
->mtn_mt
.mnt_mountp
);
936 free(mtn
->mtn_mt
.mnt_fstype
);
937 free(mtn
->mtn_mt
.mnt_mntopts
);
940 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
943 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
947 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
952 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
953 find
.mtn_mt
.mnt_special
= (char *)fsname
;
954 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
956 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
957 free(ret
->mtn_mt
.mnt_special
);
958 free(ret
->mtn_mt
.mnt_mountp
);
959 free(ret
->mtn_mt
.mnt_fstype
);
960 free(ret
->mtn_mt
.mnt_mntopts
);
963 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
967 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
969 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
971 if (zpool_handle
== NULL
)
974 *spa_version
= zpool_get_prop_int(zpool_handle
,
975 ZPOOL_PROP_VERSION
, NULL
);
980 * The choice of reservation property depends on the SPA version.
983 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
987 if (zfs_spa_version(zhp
, &spa_version
) < 0)
990 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
991 *resv_prop
= ZFS_PROP_REFRESERVATION
;
993 *resv_prop
= ZFS_PROP_RESERVATION
;
999 * Given an nvlist of properties to set, validates that they are correct, and
1000 * parses any numeric properties (index, boolean, etc) if they are specified as
1004 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1005 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1006 boolean_t key_params_ok
, const char *errbuf
)
1013 int chosen_normal
= -1;
1014 int chosen_utf
= -1;
1016 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1017 (void) no_memory(hdl
);
1022 * Make sure this property is valid and applies to this type.
1026 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1027 const char *propname
= nvpair_name(elem
);
1029 prop
= zfs_name_to_prop(propname
);
1030 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
1032 * This is a user property: make sure it's a
1033 * string, and that it's less than ZAP_MAXNAMELEN.
1035 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1037 "'%s' must be a string"), propname
);
1038 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1042 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1043 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1044 "property name '%s' is too long"),
1046 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1050 (void) nvpair_value_string(elem
, &strval
);
1051 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1052 (void) no_memory(hdl
);
1059 * Currently, only user properties can be modified on
1062 if (type
== ZFS_TYPE_SNAPSHOT
) {
1063 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1064 "this property can not be modified for snapshots"));
1065 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1069 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1070 zfs_userquota_prop_t uqtype
;
1071 char *newpropname
= NULL
;
1077 if (userquota_propname_decode(propname
, zoned
,
1078 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1080 dgettext(TEXT_DOMAIN
,
1081 "'%s' has an invalid user/group name"),
1083 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1087 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1088 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1089 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1090 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1091 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1092 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1094 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1096 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1101 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1102 (void) nvpair_value_string(elem
, &strval
);
1103 if (strcmp(strval
, "none") == 0) {
1105 } else if (zfs_nicestrtonum(hdl
,
1106 strval
, &intval
) != 0) {
1107 (void) zfs_error(hdl
,
1108 EZFS_BADPROP
, errbuf
);
1111 } else if (nvpair_type(elem
) ==
1113 (void) nvpair_value_uint64(elem
, &intval
);
1115 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1116 "use 'none' to disable "
1117 "{user|group|project}quota"));
1121 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1122 "'%s' must be a number"), propname
);
1123 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1128 * Encode the prop name as
1129 * userquota@<hex-rid>-domain, to make it easy
1130 * for the kernel to decode.
1132 rc
= asprintf(&newpropname
, "%s%llx-%s",
1133 zfs_userquota_prop_prefixes
[uqtype
],
1134 (longlong_t
)rid
, domain
);
1135 if (rc
== -1 || newpropname
== NULL
) {
1136 (void) no_memory(hdl
);
1143 if (nvlist_add_uint64_array(ret
, newpropname
,
1146 (void) no_memory(hdl
);
1151 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1152 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1153 "'%s' is readonly"),
1155 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1159 if (prop
== ZPROP_INVAL
) {
1160 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1161 "invalid property '%s'"), propname
);
1162 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1166 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1168 dgettext(TEXT_DOMAIN
, "'%s' does not "
1169 "apply to datasets of this type"), propname
);
1170 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1174 if (zfs_prop_readonly(prop
) &&
1175 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1176 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1178 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1180 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1184 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1185 &strval
, &intval
, errbuf
) != 0)
1189 * Perform some additional checks for specific properties.
1192 case ZFS_PROP_VERSION
:
1198 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1199 if (intval
< version
) {
1200 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1201 "Can not downgrade; already at version %u"),
1203 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1209 case ZFS_PROP_VOLBLOCKSIZE
:
1210 case ZFS_PROP_RECORDSIZE
:
1212 int maxbs
= SPA_MAXBLOCKSIZE
;
1215 if (zpool_hdl
!= NULL
) {
1216 maxbs
= zpool_get_prop_int(zpool_hdl
,
1217 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1220 * The value must be a power of two between
1221 * SPA_MINBLOCKSIZE and maxbs.
1223 if (intval
< SPA_MINBLOCKSIZE
||
1224 intval
> maxbs
|| !ISP2(intval
)) {
1225 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1226 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1227 "'%s' must be power of 2 from 512B "
1228 "to %s"), propname
, buf
);
1229 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1235 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1237 int maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1240 if (zpool_hdl
!= NULL
) {
1241 char state
[64] = "";
1243 maxbs
= zpool_get_prop_int(zpool_hdl
,
1244 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1247 * Issue a warning but do not fail so that
1248 * tests for settable properties succeed.
1250 if (zpool_prop_get_feature(zpool_hdl
,
1251 "feature@allocation_classes", state
,
1252 sizeof (state
)) != 0 ||
1253 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1254 (void) fprintf(stderr
, gettext(
1255 "%s: property requires a special "
1256 "device in the pool\n"), propname
);
1260 (intval
< SPA_MINBLOCKSIZE
||
1261 intval
> maxbs
|| !ISP2(intval
))) {
1262 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1263 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1264 "invalid '%s=%llu' property: must be zero "
1265 "or a power of 2 from 512B to %s"),
1266 propname
, (unsigned long long)intval
, buf
);
1267 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1273 case ZFS_PROP_MLSLABEL
:
1275 #ifdef HAVE_MLSLABEL
1277 * Verify the mlslabel string and convert to
1278 * internal hex label string.
1282 char *hex
= NULL
; /* internal label string */
1284 /* Default value is already OK. */
1285 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1288 /* Verify the label can be converted to binary form */
1289 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1290 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1291 L_NO_CORRECTION
, NULL
) == -1)) {
1295 /* Now translate to hex internal label string */
1296 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1302 m_label_free(new_sl
);
1304 /* If string is already in internal form, we're done. */
1305 if (strcmp(strval
, hex
) == 0) {
1310 /* Replace the label string with the internal form. */
1311 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1313 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1320 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1321 "invalid mlslabel '%s'"), strval
);
1322 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1323 m_label_free(new_sl
); /* OK if null */
1326 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1327 "mlslabels are unsupported"));
1328 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1330 #endif /* HAVE_MLSLABEL */
1333 case ZFS_PROP_MOUNTPOINT
:
1335 namecheck_err_t why
;
1337 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1338 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1341 if (mountpoint_namecheck(strval
, &why
)) {
1343 case NAME_ERR_LEADING_SLASH
:
1345 dgettext(TEXT_DOMAIN
,
1346 "'%s' must be an absolute path, "
1347 "'none', or 'legacy'"), propname
);
1349 case NAME_ERR_TOOLONG
:
1351 dgettext(TEXT_DOMAIN
,
1352 "component of '%s' is too long"),
1358 dgettext(TEXT_DOMAIN
,
1359 "(%d) not defined"),
1363 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1369 case ZFS_PROP_SHARESMB
:
1370 case ZFS_PROP_SHARENFS
:
1372 * For the mountpoint and sharenfs or sharesmb
1373 * properties, check if it can be set in a
1374 * global/non-global zone based on
1375 * the zoned property value:
1377 * global zone non-global zone
1378 * --------------------------------------------------
1379 * zoned=on mountpoint (no) mountpoint (yes)
1380 * sharenfs (no) sharenfs (no)
1381 * sharesmb (no) sharesmb (no)
1383 * zoned=off mountpoint (yes) N/A
1388 if (getzoneid() == GLOBAL_ZONEID
) {
1389 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1390 "'%s' cannot be set on "
1391 "dataset in a non-global zone"),
1393 (void) zfs_error(hdl
, EZFS_ZONED
,
1396 } else if (prop
== ZFS_PROP_SHARENFS
||
1397 prop
== ZFS_PROP_SHARESMB
) {
1398 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1399 "'%s' cannot be set in "
1400 "a non-global zone"), propname
);
1401 (void) zfs_error(hdl
, EZFS_ZONED
,
1405 } else if (getzoneid() != GLOBAL_ZONEID
) {
1407 * If zoned property is 'off', this must be in
1408 * a global zone. If not, something is wrong.
1410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1411 "'%s' cannot be set while dataset "
1412 "'zoned' property is set"), propname
);
1413 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1418 * At this point, it is legitimate to set the
1419 * property. Now we want to make sure that the
1420 * property value is valid if it is sharenfs.
1422 if ((prop
== ZFS_PROP_SHARENFS
||
1423 prop
== ZFS_PROP_SHARESMB
) &&
1424 strcmp(strval
, "on") != 0 &&
1425 strcmp(strval
, "off") != 0) {
1426 zfs_share_proto_t proto
;
1428 if (prop
== ZFS_PROP_SHARESMB
)
1433 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1434 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1435 "'%s' cannot be set to invalid "
1436 "options"), propname
);
1437 (void) zfs_error(hdl
, EZFS_BADPROP
,
1445 case ZFS_PROP_KEYLOCATION
:
1446 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1447 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1448 "invalid keylocation"));
1449 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1455 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1457 if (crypt
== ZIO_CRYPT_OFF
&&
1458 strcmp(strval
, "none") != 0) {
1459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1460 "keylocation must be 'none' "
1461 "for unencrypted datasets"));
1462 (void) zfs_error(hdl
, EZFS_BADPROP
,
1465 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1466 strcmp(strval
, "none") == 0) {
1467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1468 "keylocation must not be 'none' "
1469 "for encrypted datasets"));
1470 (void) zfs_error(hdl
, EZFS_BADPROP
,
1477 case ZFS_PROP_PBKDF2_ITERS
:
1478 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1480 "minimum pbkdf2 iterations is %u"),
1481 MIN_PBKDF2_ITERATIONS
);
1482 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1487 case ZFS_PROP_UTF8ONLY
:
1488 chosen_utf
= (int)intval
;
1491 case ZFS_PROP_NORMALIZE
:
1492 chosen_normal
= (int)intval
;
1500 * For changes to existing volumes, we have some additional
1501 * checks to enforce.
1503 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1504 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1505 ZFS_PROP_VOLBLOCKSIZE
);
1509 case ZFS_PROP_VOLSIZE
:
1510 if (intval
% blocksize
!= 0) {
1511 zfs_nicebytes(blocksize
, buf
,
1513 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1514 "'%s' must be a multiple of "
1515 "volume block size (%s)"),
1517 (void) zfs_error(hdl
, EZFS_BADPROP
,
1523 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1524 "'%s' cannot be zero"),
1526 (void) zfs_error(hdl
, EZFS_BADPROP
,
1537 /* check encryption properties */
1539 int64_t crypt
= zfs_prop_get_int(zhp
,
1540 ZFS_PROP_ENCRYPTION
);
1543 case ZFS_PROP_COPIES
:
1544 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1545 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1546 "encrypted datasets cannot have "
1548 (void) zfs_error(hdl
, EZFS_BADPROP
,
1560 * If normalization was chosen, but no UTF8 choice was made,
1561 * enforce rejection of non-UTF8 names.
1563 * If normalization was chosen, but rejecting non-UTF8 names
1564 * was explicitly not chosen, it is an error.
1566 * If utf8only was turned off, but the parent has normalization,
1567 * turn off normalization.
1569 if (chosen_normal
> 0 && chosen_utf
< 0) {
1570 if (nvlist_add_uint64(ret
,
1571 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1572 (void) no_memory(hdl
);
1575 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1576 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1577 "'%s' must be set 'on' if normalization chosen"),
1578 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1579 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1581 } else if (chosen_normal
< 0 && chosen_utf
== 0) {
1582 if (nvlist_add_uint64(ret
,
1583 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), 0) != 0) {
1584 (void) no_memory(hdl
);
1596 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1598 uint64_t old_volsize
;
1599 uint64_t new_volsize
;
1600 uint64_t old_reservation
;
1601 uint64_t new_reservation
;
1602 zfs_prop_t resv_prop
;
1604 zpool_handle_t
*zph
= zpool_handle(zhp
);
1607 * If this is an existing volume, and someone is setting the volsize,
1608 * make sure that it matches the reservation, or add it if necessary.
1610 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1611 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1613 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1615 props
= fnvlist_alloc();
1616 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1617 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1619 if ((zvol_volsize_to_reservation(zph
, old_volsize
, props
) !=
1620 old_reservation
) || nvlist_exists(nvl
,
1621 zfs_prop_to_name(resv_prop
))) {
1622 fnvlist_free(props
);
1625 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1626 &new_volsize
) != 0) {
1627 fnvlist_free(props
);
1630 new_reservation
= zvol_volsize_to_reservation(zph
, new_volsize
, props
);
1631 fnvlist_free(props
);
1633 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1634 new_reservation
) != 0) {
1635 (void) no_memory(zhp
->zfs_hdl
);
1642 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1643 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1644 * Return codes must match zfs_add_synthetic_resv().
1647 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1654 if (!ZFS_IS_VOLUME(zhp
)) {
1658 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1662 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1666 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1667 /* No value being set, so it can't be "auto" */
1670 if (resvsize
!= UINT64_MAX
) {
1671 /* Being set to a value other than "auto" */
1675 props
= fnvlist_alloc();
1677 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1678 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1680 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1682 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1685 resvsize
= zvol_volsize_to_reservation(zpool_handle(zhp
), volsize
,
1687 fnvlist_free(props
);
1689 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1690 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1691 (void) no_memory(zhp
->zfs_hdl
);
1698 zfs_is_namespace_prop(zfs_prop_t prop
)
1702 case ZFS_PROP_ATIME
:
1703 case ZFS_PROP_RELATIME
:
1704 case ZFS_PROP_DEVICES
:
1706 case ZFS_PROP_SETUID
:
1707 case ZFS_PROP_READONLY
:
1708 case ZFS_PROP_XATTR
:
1709 case ZFS_PROP_NBMAND
:
1718 * Given a property name and value, set the property for the given dataset.
1721 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1725 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1726 nvlist_t
*nvl
= NULL
;
1728 (void) snprintf(errbuf
, sizeof (errbuf
),
1729 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1732 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1733 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1734 (void) no_memory(hdl
);
1738 ret
= zfs_prop_set_list(zhp
, nvl
);
1748 * Given an nvlist of property names and values, set the properties for the
1752 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1754 zfs_cmd_t zc
= {"\0"};
1756 prop_changelist_t
**cls
= NULL
;
1759 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1763 zfs_prop_t prop
= 0;
1766 (void) snprintf(errbuf
, sizeof (errbuf
),
1767 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1770 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1771 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1772 B_FALSE
, errbuf
)) == NULL
)
1776 * We have to check for any extra properties which need to be added
1777 * before computing the length of the nvlist.
1779 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1781 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1782 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1783 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1788 if (added_resv
!= 1 &&
1789 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1794 * Check how many properties we're setting and allocate an array to
1795 * store changelist pointers for postfix().
1797 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1799 elem
= nvlist_next_nvpair(nvl
, elem
))
1801 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1805 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1807 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1809 prop
= zfs_name_to_prop(nvpair_name(elem
));
1811 assert(cl_idx
< nvl_len
);
1813 * We don't want to unmount & remount the dataset when changing
1814 * its canmount property to 'on' or 'noauto'. We only use
1815 * the changelist logic to unmount when setting canmount=off.
1817 if (prop
!= ZFS_PROP_CANMOUNT
||
1818 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1819 zfs_is_mounted(zhp
, NULL
))) {
1820 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1821 if (cls
[cl_idx
] == NULL
)
1825 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1826 changelist_haszonedchild(cls
[cl_idx
])) {
1827 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1828 "child dataset with inherited mountpoint is used "
1829 "in a non-global zone"));
1830 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1834 if (cls
[cl_idx
] != NULL
&&
1835 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1840 assert(cl_idx
== nvl_len
);
1843 * Execute the corresponding ioctl() to set this list of properties.
1845 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1847 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1848 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1851 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1854 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1855 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1859 /* Get the list of unset properties back and report them. */
1860 nvlist_t
*errorprops
= NULL
;
1861 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1863 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1865 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1866 prop
= zfs_name_to_prop(nvpair_name(elem
));
1867 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1869 nvlist_free(errorprops
);
1871 if (added_resv
&& errno
== ENOSPC
) {
1872 /* clean up the volsize property we tried to set */
1873 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1877 zcmd_free_nvlists(&zc
);
1879 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1881 if (nvlist_add_uint64(nvl
,
1882 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1885 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1887 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1890 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1891 if (cls
[cl_idx
] != NULL
) {
1892 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1900 * Refresh the statistics so the new property
1901 * value is reflected.
1903 (void) get_stats(zhp
);
1906 * Remount the filesystem to propagate the change
1907 * if one of the options handled by the generic
1908 * Linux namespace layer has been modified.
1910 if (zfs_is_namespace_prop(prop
) &&
1911 zfs_is_mounted(zhp
, NULL
))
1912 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1918 zcmd_free_nvlists(&zc
);
1920 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1921 if (cls
[cl_idx
] != NULL
)
1922 changelist_free(cls
[cl_idx
]);
1930 * Given a property, inherit the value from the parent dataset, or if received
1931 * is TRUE, revert to the received value, if any.
1934 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1936 zfs_cmd_t zc
= {"\0"};
1938 prop_changelist_t
*cl
;
1939 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1943 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1944 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1946 zc
.zc_cookie
= received
;
1947 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1949 * For user properties, the amount of work we have to do is very
1950 * small, so just do it here.
1952 if (!zfs_prop_user(propname
)) {
1953 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1954 "invalid property"));
1955 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1958 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1959 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1961 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1962 return (zfs_standard_error(hdl
, errno
, errbuf
));
1964 (void) get_stats(zhp
);
1969 * Verify that this property is inheritable.
1971 if (zfs_prop_readonly(prop
))
1972 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1974 if (!zfs_prop_inheritable(prop
) && !received
)
1975 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1978 * Check to see if the value applies to this type
1980 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1981 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1984 * Normalize the name, to get rid of shorthand abbreviations.
1986 propname
= zfs_prop_to_name(prop
);
1987 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1988 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1990 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1991 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1992 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1993 "dataset is used in a non-global zone"));
1994 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1998 * Determine datasets which will be affected by this change, if any.
2000 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2003 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2004 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2005 "child dataset with inherited mountpoint is used "
2006 "in a non-global zone"));
2007 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2011 if ((ret
= changelist_prefix(cl
)) != 0)
2014 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2015 return (zfs_standard_error(hdl
, errno
, errbuf
));
2018 if ((ret
= changelist_postfix(cl
)) != 0)
2022 * Refresh the statistics so the new property is reflected.
2024 (void) get_stats(zhp
);
2027 * Remount the filesystem to propagate the change
2028 * if one of the options handled by the generic
2029 * Linux namespace layer has been modified.
2031 if (zfs_is_namespace_prop(prop
) &&
2032 zfs_is_mounted(zhp
, NULL
))
2033 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2037 changelist_free(cl
);
2042 * True DSL properties are stored in an nvlist. The following two functions
2043 * extract them appropriately.
2046 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2052 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2053 zfs_prop_to_name(prop
), &nv
) == 0) {
2054 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2055 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2057 verify(!zhp
->zfs_props_table
||
2058 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2059 value
= zfs_prop_default_numeric(prop
);
2067 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2073 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2074 zfs_prop_to_name(prop
), &nv
) == 0) {
2075 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2076 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2078 verify(!zhp
->zfs_props_table
||
2079 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2080 value
= zfs_prop_default_string(prop
);
2088 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2090 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2094 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2096 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2097 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2101 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2103 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2108 * Internal function for getting a numeric property. Both zfs_prop_get() and
2109 * zfs_prop_get_int() are built using this interface.
2111 * Certain properties can be overridden using 'mount -o'. In this case, scan
2112 * the contents of the /proc/self/mounts entry, searching for the
2113 * appropriate options. If they differ from the on-disk values, report the
2114 * current values and mark the source "temporary".
2117 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2118 char **source
, uint64_t *val
)
2120 zfs_cmd_t zc
= {"\0"};
2121 nvlist_t
*zplprops
= NULL
;
2123 char *mntopt_on
= NULL
;
2124 char *mntopt_off
= NULL
;
2125 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2130 * If the property is being fetched for a snapshot, check whether
2131 * the property is valid for the snapshot's head dataset type.
2133 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2134 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2135 *val
= zfs_prop_default_numeric(prop
);
2140 case ZFS_PROP_ATIME
:
2141 mntopt_on
= MNTOPT_ATIME
;
2142 mntopt_off
= MNTOPT_NOATIME
;
2145 case ZFS_PROP_RELATIME
:
2146 mntopt_on
= MNTOPT_RELATIME
;
2147 mntopt_off
= MNTOPT_NORELATIME
;
2150 case ZFS_PROP_DEVICES
:
2151 mntopt_on
= MNTOPT_DEVICES
;
2152 mntopt_off
= MNTOPT_NODEVICES
;
2156 mntopt_on
= MNTOPT_EXEC
;
2157 mntopt_off
= MNTOPT_NOEXEC
;
2160 case ZFS_PROP_READONLY
:
2161 mntopt_on
= MNTOPT_RO
;
2162 mntopt_off
= MNTOPT_RW
;
2165 case ZFS_PROP_SETUID
:
2166 mntopt_on
= MNTOPT_SETUID
;
2167 mntopt_off
= MNTOPT_NOSETUID
;
2170 case ZFS_PROP_XATTR
:
2171 mntopt_on
= MNTOPT_XATTR
;
2172 mntopt_off
= MNTOPT_NOXATTR
;
2175 case ZFS_PROP_NBMAND
:
2176 mntopt_on
= MNTOPT_NBMAND
;
2177 mntopt_off
= MNTOPT_NONBMAND
;
2185 * Because looking up the mount options is potentially expensive
2186 * (iterating over all of /proc/self/mounts), we defer its
2187 * calculation until we're looking up a property which requires
2190 if (!zhp
->zfs_mntcheck
&&
2191 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2192 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2193 struct mnttab entry
;
2195 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2196 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2198 if (zhp
->zfs_mntopts
== NULL
)
2202 zhp
->zfs_mntcheck
= B_TRUE
;
2205 if (zhp
->zfs_mntopts
== NULL
)
2206 mnt
.mnt_mntopts
= "";
2208 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2211 case ZFS_PROP_ATIME
:
2212 case ZFS_PROP_RELATIME
:
2213 case ZFS_PROP_DEVICES
:
2215 case ZFS_PROP_READONLY
:
2216 case ZFS_PROP_SETUID
:
2218 case ZFS_PROP_XATTR
:
2220 case ZFS_PROP_NBMAND
:
2221 *val
= getprop_uint64(zhp
, prop
, source
);
2226 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2229 *src
= ZPROP_SRC_TEMPORARY
;
2230 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2233 *src
= ZPROP_SRC_TEMPORARY
;
2237 case ZFS_PROP_CANMOUNT
:
2238 case ZFS_PROP_VOLSIZE
:
2239 case ZFS_PROP_QUOTA
:
2240 case ZFS_PROP_REFQUOTA
:
2241 case ZFS_PROP_RESERVATION
:
2242 case ZFS_PROP_REFRESERVATION
:
2243 case ZFS_PROP_FILESYSTEM_LIMIT
:
2244 case ZFS_PROP_SNAPSHOT_LIMIT
:
2245 case ZFS_PROP_FILESYSTEM_COUNT
:
2246 case ZFS_PROP_SNAPSHOT_COUNT
:
2247 *val
= getprop_uint64(zhp
, prop
, source
);
2249 if (*source
== NULL
) {
2250 /* not default, must be local */
2251 *source
= zhp
->zfs_name
;
2255 case ZFS_PROP_MOUNTED
:
2256 *val
= (zhp
->zfs_mntopts
!= NULL
);
2259 case ZFS_PROP_NUMCLONES
:
2260 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2263 case ZFS_PROP_VERSION
:
2264 case ZFS_PROP_NORMALIZE
:
2265 case ZFS_PROP_UTF8ONLY
:
2267 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2269 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2270 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2271 zcmd_free_nvlists(&zc
);
2272 if (prop
== ZFS_PROP_VERSION
&&
2273 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2274 *val
= zfs_prop_default_numeric(prop
);
2277 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2278 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2280 zcmd_free_nvlists(&zc
);
2283 nvlist_free(zplprops
);
2284 zcmd_free_nvlists(&zc
);
2287 case ZFS_PROP_INCONSISTENT
:
2288 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2291 case ZFS_PROP_REDACTED
:
2292 *val
= zhp
->zfs_dmustats
.dds_redacted
;
2296 switch (zfs_prop_get_type(prop
)) {
2297 case PROP_TYPE_NUMBER
:
2298 case PROP_TYPE_INDEX
:
2299 *val
= getprop_uint64(zhp
, prop
, source
);
2301 * If we tried to use a default value for a
2302 * readonly property, it means that it was not
2303 * present. Note this only applies to "truly"
2304 * readonly properties, not set-once properties
2305 * like volblocksize.
2307 if (zfs_prop_readonly(prop
) &&
2308 !zfs_prop_setonce(prop
) &&
2309 *source
!= NULL
&& (*source
)[0] == '\0') {
2315 case PROP_TYPE_STRING
:
2317 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2318 "cannot get non-numeric property"));
2319 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2320 dgettext(TEXT_DOMAIN
, "internal error")));
2328 * Calculate the source type, given the raw source string.
2331 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2332 char *statbuf
, size_t statlen
)
2334 if (statbuf
== NULL
||
2335 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2339 if (source
== NULL
) {
2340 *srctype
= ZPROP_SRC_NONE
;
2341 } else if (source
[0] == '\0') {
2342 *srctype
= ZPROP_SRC_DEFAULT
;
2343 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2344 *srctype
= ZPROP_SRC_RECEIVED
;
2346 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2347 *srctype
= ZPROP_SRC_LOCAL
;
2349 (void) strlcpy(statbuf
, source
, statlen
);
2350 *srctype
= ZPROP_SRC_INHERITED
;
2357 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2358 size_t proplen
, boolean_t literal
)
2363 if (zhp
->zfs_recvd_props
== NULL
)
2364 if (get_recvd_props_ioctl(zhp
) != 0)
2367 prop
= zfs_name_to_prop(propname
);
2369 if (prop
!= ZPROP_INVAL
) {
2371 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2373 zfs_set_recvd_props_mode(zhp
, &cookie
);
2374 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2375 NULL
, NULL
, 0, literal
);
2376 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2380 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2381 propname
, &propval
) != 0)
2383 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2385 (void) strlcpy(propbuf
, recvdval
, proplen
);
2388 return (err
== 0 ? 0 : -1);
2392 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2397 value
= zfs_get_clones_nvl(zhp
);
2398 if (value
== NULL
|| nvlist_empty(value
))
2402 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2403 pair
= nvlist_next_nvpair(value
, pair
)) {
2404 if (propbuf
[0] != '\0')
2405 (void) strlcat(propbuf
, ",", proplen
);
2406 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2412 struct get_clones_arg
{
2416 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2420 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2422 struct get_clones_arg
*gca
= arg
;
2424 if (gca
->numclones
== 0) {
2429 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2430 NULL
, NULL
, 0, B_TRUE
) != 0)
2432 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2433 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2438 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2444 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2446 nvlist_t
*nv
, *value
;
2448 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2449 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2450 struct get_clones_arg gca
;
2453 * if this is a snapshot, then the kernel wasn't able
2454 * to get the clones. Do it by slowly iterating.
2456 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2458 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2460 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2465 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2467 gca
.origin
= zhp
->zfs_name
;
2469 if (gca
.numclones
!= 0) {
2471 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2474 /* get the pool name */
2475 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2476 (void) strsep(&cp
, "/@");
2477 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2478 ZFS_TYPE_FILESYSTEM
);
2485 (void) get_clones_cb(root
, &gca
);
2488 if (gca
.numclones
!= 0 ||
2489 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2490 nvlist_add_nvlist(zhp
->zfs_props
,
2491 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2498 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2499 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2502 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2508 get_rsnaps_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2514 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2515 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
), &value
) != 0)
2517 if (nvlist_lookup_uint64_array(value
, ZPROP_VALUE
, &snaps
,
2521 /* There's no redaction snapshots; pass a special value back */
2522 (void) snprintf(propbuf
, proplen
, "none");
2526 for (int i
= 0; i
< nsnaps
; i
++) {
2528 if (propbuf
[0] != '\0')
2529 (void) strlcat(propbuf
, ",", proplen
);
2530 (void) snprintf(buf
, sizeof (buf
), "%llu",
2531 (u_longlong_t
)snaps
[i
]);
2532 (void) strlcat(propbuf
, buf
, proplen
);
2539 * Accepts a property and value and checks that the value
2540 * matches the one found by the channel program. If they are
2541 * not equal, print both of them.
2544 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2547 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2550 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2551 const char *prop_name
= zfs_prop_to_name(prop
);
2552 const char *program
=
2554 "ds = args['dataset']\n"
2555 "prop = args['property']\n"
2556 "value, setpoint = zfs.get_prop(ds, prop)\n"
2557 "return {value=value, setpoint=setpoint}\n";
2560 nvlist_t
*argnvl
= fnvlist_alloc();
2562 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2563 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2565 error
= lzc_channel_program_nosync(poolname
, program
,
2566 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2569 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2570 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2572 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2574 (void) fprintf(stderr
, "%s: zcp check error: "
2575 "%u\n", prop_name
, error
);
2578 if (ans
!= intval
) {
2579 (void) fprintf(stderr
, "%s: zfs found %llu, "
2580 "but zcp found %llu\n", prop_name
,
2581 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2585 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2587 (void) fprintf(stderr
, "%s: zcp check error: "
2588 "%u\n", prop_name
, error
);
2591 if (strcmp(strval
, str_ans
) != 0) {
2592 (void) fprintf(stderr
,
2593 "%s: zfs found '%s', but zcp found '%s'\n",
2594 prop_name
, strval
, str_ans
);
2598 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2599 "error: %u\n", prop_name
, error
);
2601 nvlist_free(argnvl
);
2602 nvlist_free(outnvl
);
2606 * Retrieve a property from the given object. If 'literal' is specified, then
2607 * numbers are left as exact values. Otherwise, numbers are converted to a
2608 * human-readable form.
2610 * Returns 0 on success, or -1 on error.
2613 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2614 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2616 char *source
= NULL
;
2620 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2623 * Check to see if this property applies to our object
2625 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2628 if (received
&& zfs_prop_readonly(prop
))
2632 *src
= ZPROP_SRC_NONE
;
2635 case ZFS_PROP_CREATION
:
2637 * 'creation' is a time_t stored in the statistics. We convert
2638 * this into a string unless 'literal' is specified.
2641 val
= getprop_uint64(zhp
, prop
, &source
);
2642 time_t time
= (time_t)val
;
2646 localtime_r(&time
, &t
) == NULL
||
2647 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2649 (void) snprintf(propbuf
, proplen
, "%llu",
2652 zcp_check(zhp
, prop
, val
, NULL
);
2655 case ZFS_PROP_MOUNTPOINT
:
2657 * Getting the precise mountpoint can be tricky.
2659 * - for 'none' or 'legacy', return those values.
2660 * - for inherited mountpoints, we want to take everything
2661 * after our ancestor and append it to the inherited value.
2663 * If the pool has an alternate root, we want to prepend that
2664 * root to any values we return.
2667 str
= getprop_string(zhp
, prop
, &source
);
2669 if (str
[0] == '/') {
2670 char buf
[MAXPATHLEN
];
2672 const char *relpath
;
2675 * If we inherit the mountpoint, even from a dataset
2676 * with a received value, the source will be the path of
2677 * the dataset we inherit from. If source is
2678 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2681 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2684 relpath
= zhp
->zfs_name
+ strlen(source
);
2685 if (relpath
[0] == '/')
2689 if ((zpool_get_prop(zhp
->zpool_hdl
,
2690 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2691 B_FALSE
)) || (strcmp(root
, "-") == 0))
2694 * Special case an alternate root of '/'. This will
2695 * avoid having multiple leading slashes in the
2698 if (strcmp(root
, "/") == 0)
2702 * If the mountpoint is '/' then skip over this
2703 * if we are obtaining either an alternate root or
2704 * an inherited mountpoint.
2706 if (str
[1] == '\0' && (root
[0] != '\0' ||
2707 relpath
[0] != '\0'))
2710 if (relpath
[0] == '\0')
2711 (void) snprintf(propbuf
, proplen
, "%s%s",
2714 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2715 root
, str
, relpath
[0] == '@' ? "" : "/",
2718 /* 'legacy' or 'none' */
2719 (void) strlcpy(propbuf
, str
, proplen
);
2721 zcp_check(zhp
, prop
, 0, propbuf
);
2724 case ZFS_PROP_ORIGIN
:
2725 str
= getprop_string(zhp
, prop
, &source
);
2728 (void) strlcpy(propbuf
, str
, proplen
);
2729 zcp_check(zhp
, prop
, 0, str
);
2732 case ZFS_PROP_REDACT_SNAPS
:
2733 if (get_rsnaps_string(zhp
, propbuf
, proplen
) != 0)
2737 case ZFS_PROP_CLONES
:
2738 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2742 case ZFS_PROP_QUOTA
:
2743 case ZFS_PROP_REFQUOTA
:
2744 case ZFS_PROP_RESERVATION
:
2745 case ZFS_PROP_REFRESERVATION
:
2747 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2750 * If quota or reservation is 0, we translate this into 'none'
2751 * (unless literal is set), and indicate that it's the default
2752 * value. Otherwise, we print the number nicely and indicate
2753 * that its set locally.
2757 (void) strlcpy(propbuf
, "0", proplen
);
2759 (void) strlcpy(propbuf
, "none", proplen
);
2762 (void) snprintf(propbuf
, proplen
, "%llu",
2765 zfs_nicebytes(val
, propbuf
, proplen
);
2767 zcp_check(zhp
, prop
, val
, NULL
);
2770 case ZFS_PROP_FILESYSTEM_LIMIT
:
2771 case ZFS_PROP_SNAPSHOT_LIMIT
:
2772 case ZFS_PROP_FILESYSTEM_COUNT
:
2773 case ZFS_PROP_SNAPSHOT_COUNT
:
2775 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2779 * If limit is UINT64_MAX, we translate this into 'none', and
2780 * indicate that it's the default value. Otherwise, we print
2781 * the number nicely and indicate that it's set locally.
2783 if (val
== UINT64_MAX
) {
2784 (void) strlcpy(propbuf
, "none", proplen
);
2785 } else if (literal
) {
2786 (void) snprintf(propbuf
, proplen
, "%llu",
2789 zfs_nicenum(val
, propbuf
, proplen
);
2792 zcp_check(zhp
, prop
, val
, NULL
);
2795 case ZFS_PROP_REFRATIO
:
2796 case ZFS_PROP_COMPRESSRATIO
:
2797 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2800 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2801 (u_longlong_t
)(val
/ 100),
2802 (u_longlong_t
)(val
% 100));
2804 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2805 (u_longlong_t
)(val
/ 100),
2806 (u_longlong_t
)(val
% 100));
2807 zcp_check(zhp
, prop
, val
, NULL
);
2811 switch (zhp
->zfs_type
) {
2812 case ZFS_TYPE_FILESYSTEM
:
2815 case ZFS_TYPE_VOLUME
:
2818 case ZFS_TYPE_SNAPSHOT
:
2821 case ZFS_TYPE_BOOKMARK
:
2827 (void) snprintf(propbuf
, proplen
, "%s", str
);
2828 zcp_check(zhp
, prop
, 0, propbuf
);
2831 case ZFS_PROP_MOUNTED
:
2833 * The 'mounted' property is a pseudo-property that described
2834 * whether the filesystem is currently mounted. Even though
2835 * it's a boolean value, the typical values of "on" and "off"
2836 * don't make sense, so we translate to "yes" and "no".
2838 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2839 src
, &source
, &val
) != 0)
2842 (void) strlcpy(propbuf
, "yes", proplen
);
2844 (void) strlcpy(propbuf
, "no", proplen
);
2849 * The 'name' property is a pseudo-property derived from the
2850 * dataset name. It is presented as a real property to simplify
2853 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2854 zcp_check(zhp
, prop
, 0, propbuf
);
2857 case ZFS_PROP_MLSLABEL
:
2859 #ifdef HAVE_MLSLABEL
2860 m_label_t
*new_sl
= NULL
;
2861 char *ascii
= NULL
; /* human readable label */
2863 (void) strlcpy(propbuf
,
2864 getprop_string(zhp
, prop
, &source
), proplen
);
2866 if (literal
|| (strcasecmp(propbuf
,
2867 ZFS_MLSLABEL_DEFAULT
) == 0))
2871 * Try to translate the internal hex string to
2872 * human-readable output. If there are any
2873 * problems just use the hex string.
2876 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2877 L_NO_CORRECTION
, NULL
) == -1) {
2878 m_label_free(new_sl
);
2882 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2886 m_label_free(new_sl
);
2889 m_label_free(new_sl
);
2891 (void) strlcpy(propbuf
, ascii
, proplen
);
2894 (void) strlcpy(propbuf
,
2895 getprop_string(zhp
, prop
, &source
), proplen
);
2896 #endif /* HAVE_MLSLABEL */
2901 case ZFS_PROP_CREATETXG
:
2902 case ZFS_PROP_OBJSETID
:
2903 case ZFS_PROP_PBKDF2_ITERS
:
2905 * These properties are stored as numbers, but they are
2906 * identifiers or counters.
2907 * We don't want them to be pretty printed, because pretty
2908 * printing truncates their values making them useless.
2910 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2912 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2913 zcp_check(zhp
, prop
, val
, NULL
);
2916 case ZFS_PROP_REFERENCED
:
2917 case ZFS_PROP_AVAILABLE
:
2919 case ZFS_PROP_USEDSNAP
:
2920 case ZFS_PROP_USEDDS
:
2921 case ZFS_PROP_USEDREFRESERV
:
2922 case ZFS_PROP_USEDCHILD
:
2923 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2926 (void) snprintf(propbuf
, proplen
, "%llu",
2929 zfs_nicebytes(val
, propbuf
, proplen
);
2931 zcp_check(zhp
, prop
, val
, NULL
);
2935 switch (zfs_prop_get_type(prop
)) {
2936 case PROP_TYPE_NUMBER
:
2937 if (get_numeric_property(zhp
, prop
, src
,
2938 &source
, &val
) != 0) {
2943 (void) snprintf(propbuf
, proplen
, "%llu",
2946 zfs_nicenum(val
, propbuf
, proplen
);
2948 zcp_check(zhp
, prop
, val
, NULL
);
2951 case PROP_TYPE_STRING
:
2952 str
= getprop_string(zhp
, prop
, &source
);
2956 (void) strlcpy(propbuf
, str
, proplen
);
2957 zcp_check(zhp
, prop
, 0, str
);
2960 case PROP_TYPE_INDEX
:
2961 if (get_numeric_property(zhp
, prop
, src
,
2962 &source
, &val
) != 0)
2964 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2967 (void) strlcpy(propbuf
, strval
, proplen
);
2968 zcp_check(zhp
, prop
, 0, strval
);
2976 get_source(zhp
, src
, source
, statbuf
, statlen
);
2982 * Utility function to get the given numeric property. Does no validation that
2983 * the given property is the appropriate type; should only be used with
2984 * hard-coded property types.
2987 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2992 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2998 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3002 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3003 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3007 * Similar to zfs_prop_get(), but returns the value as an integer.
3010 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3011 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3016 * Check to see if this property applies to our object
3018 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3019 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3020 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3021 zfs_prop_to_name(prop
)));
3025 *src
= ZPROP_SRC_NONE
;
3027 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3030 get_source(zhp
, src
, source
, statbuf
, statlen
);
3037 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3038 char **domainp
, idmap_rid_t
*ridp
)
3040 idmap_get_handle_t
*get_hdl
= NULL
;
3044 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3048 err
= idmap_get_sidbyuid(get_hdl
, id
,
3049 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3051 err
= idmap_get_sidbygid(get_hdl
, id
,
3052 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3054 if (err
== IDMAP_SUCCESS
&&
3055 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3056 status
== IDMAP_SUCCESS
)
3062 idmap_get_destroy(get_hdl
);
3065 #endif /* HAVE_IDMAP */
3068 * convert the propname into parameters needed by kernel
3069 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3070 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3071 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3072 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3073 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3074 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3077 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3078 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3080 zfs_userquota_prop_t type
;
3084 boolean_t isproject
;
3090 /* Figure out the property type ({user|group|project}{quota|space}) */
3091 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3092 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3093 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3096 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3100 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3101 type
== ZFS_PROP_USEROBJQUOTA
||
3102 type
== ZFS_PROP_USEROBJUSED
);
3103 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3104 type
== ZFS_PROP_GROUPOBJQUOTA
||
3105 type
== ZFS_PROP_GROUPOBJUSED
);
3106 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3107 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3108 type
== ZFS_PROP_PROJECTOBJUSED
);
3110 cp
= strchr(propname
, '@') + 1;
3112 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3113 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3116 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3117 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3120 } else if (!isproject
&& strchr(cp
, '@')) {
3123 * It's a SID name (eg "user@domain") that needs to be
3124 * turned into S-1-domainID-RID.
3126 directory_error_t e
;
3127 char *numericsid
= NULL
;
3130 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3133 e
= directory_sid_from_user_name(NULL
,
3136 e
= directory_sid_from_group_name(NULL
,
3140 directory_error_free(e
);
3143 if (numericsid
== NULL
)
3146 (void) strlcpy(domain
, cp
, domainlen
);
3147 cp
= strrchr(domain
, '-');
3152 *ridp
= strtoull(cp
, &end
, 10);
3155 if (errno
!= 0 || *end
!= '\0')
3160 #endif /* HAVE_IDMAP */
3162 /* It's a user/group/project ID (eg "12345"). */
3165 id
= strtoul(cp
, &end
, 10);
3168 if (id
> MAXUID
&& !isproject
) {
3170 /* It's an ephemeral ID. */
3174 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3175 &mapdomain
, &rid
) != 0)
3177 (void) strlcpy(domain
, mapdomain
, domainlen
);
3181 #endif /* HAVE_IDMAP */
3191 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3192 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3195 zfs_cmd_t zc
= {"\0"};
3197 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3199 err
= userquota_propname_decode(propname
,
3200 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3201 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3202 zc
.zc_objset_type
= *typep
;
3206 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3210 *propvalue
= zc
.zc_cookie
;
3215 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3216 uint64_t *propvalue
)
3218 zfs_userquota_prop_t type
;
3220 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3225 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3226 char *propbuf
, int proplen
, boolean_t literal
)
3230 zfs_userquota_prop_t type
;
3232 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3239 (void) snprintf(propbuf
, proplen
, "%llu",
3240 (u_longlong_t
)propvalue
);
3241 } else if (propvalue
== 0 &&
3242 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3243 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3244 type
== ZFS_PROP_PROJECTQUOTA
||
3245 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3246 (void) strlcpy(propbuf
, "none", proplen
);
3247 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3248 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3249 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3250 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3252 zfs_nicenum(propvalue
, propbuf
, proplen
);
3258 * propname must start with "written@" or "written#".
3261 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3262 uint64_t *propvalue
)
3265 zfs_cmd_t zc
= {"\0"};
3266 const char *snapname
;
3268 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3270 assert(zfs_prop_written(propname
));
3271 snapname
= propname
+ strlen("written@");
3272 if (strchr(snapname
, '@') != NULL
|| strchr(snapname
, '#') != NULL
) {
3273 /* full snapshot or bookmark name specified */
3274 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3276 /* snapname is the short name, append it to zhp's fsname */
3279 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3280 sizeof (zc
.zc_value
));
3281 cp
= strchr(zc
.zc_value
, '@');
3284 (void) strlcat(zc
.zc_value
, snapname
- 1, sizeof (zc
.zc_value
));
3287 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3291 *propvalue
= zc
.zc_cookie
;
3296 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3297 char *propbuf
, int proplen
, boolean_t literal
)
3302 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3308 (void) snprintf(propbuf
, proplen
, "%llu",
3309 (u_longlong_t
)propvalue
);
3311 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3318 * Returns the name of the given zfs handle.
3321 zfs_get_name(const zfs_handle_t
*zhp
)
3323 return (zhp
->zfs_name
);
3327 * Returns the name of the parent pool for the given zfs handle.
3330 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3332 return (zhp
->zpool_hdl
->zpool_name
);
3336 * Returns the type of the given zfs handle.
3339 zfs_get_type(const zfs_handle_t
*zhp
)
3341 return (zhp
->zfs_type
);
3345 * Returns the type of the given zfs handle,
3346 * or, if a snapshot, the type of the snapshotted dataset.
3349 zfs_get_underlying_type(const zfs_handle_t
*zhp
)
3351 return (zhp
->zfs_head_type
);
3355 * Is one dataset name a child dataset of another?
3357 * Needs to handle these cases:
3358 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3359 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3360 * Descendant? No. No. No. Yes.
3363 is_descendant(const char *ds1
, const char *ds2
)
3365 size_t d1len
= strlen(ds1
);
3367 /* ds2 can't be a descendant if it's smaller */
3368 if (strlen(ds2
) < d1len
)
3371 /* otherwise, compare strings and verify that there's a '/' char */
3372 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3376 * Given a complete name, return just the portion that refers to the parent.
3377 * Will return -1 if there is no parent (path is just the name of the
3381 parent_name(const char *path
, char *buf
, size_t buflen
)
3385 (void) strlcpy(buf
, path
, buflen
);
3387 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3395 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3397 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3401 * If accept_ancestor is false, then check to make sure that the given path has
3402 * a parent, and that it exists. If accept_ancestor is true, then find the
3403 * closest existing ancestor for the given path. In prefixlen return the
3404 * length of already existing prefix of the given path. We also fetch the
3405 * 'zoned' property, which is used to validate property settings when creating
3409 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3410 boolean_t accept_ancestor
, int *prefixlen
)
3412 zfs_cmd_t zc
= {"\0"};
3413 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3419 (void) snprintf(errbuf
, sizeof (errbuf
),
3420 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3422 /* get parent, and check to see if this is just a pool */
3423 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3424 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3425 "missing dataset name"));
3426 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3429 /* check to see if the pool exists */
3430 if ((slash
= strchr(parent
, '/')) == NULL
)
3431 slash
= parent
+ strlen(parent
);
3432 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3433 zc
.zc_name
[slash
- parent
] = '\0';
3434 if (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3436 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3437 "no such pool '%s'"), zc
.zc_name
);
3438 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3441 /* check to see if the parent dataset exists */
3442 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3443 if (errno
== ENOENT
&& accept_ancestor
) {
3445 * Go deeper to find an ancestor, give up on top level.
3447 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3448 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3449 "no such pool '%s'"), zc
.zc_name
);
3450 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3452 } else if (errno
== ENOENT
) {
3453 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3454 "parent does not exist"));
3455 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3457 return (zfs_standard_error(hdl
, errno
, errbuf
));
3460 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3464 /* we are in a non-global zone, but parent is in the global zone */
3465 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3466 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3471 /* make sure parent is a filesystem */
3472 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3474 "parent is not a filesystem"));
3475 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3481 if (prefixlen
!= NULL
)
3482 *prefixlen
= strlen(parent
);
3487 * Finds whether the dataset of the given type(s) exists.
3490 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3494 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3498 * Try to get stats for the dataset, which will tell us if it exists.
3500 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3501 int ds_type
= zhp
->zfs_type
;
3504 if (types
& ds_type
)
3511 * Given a path to 'target', create all the ancestors between
3512 * the prefixlen portion of the path, and the target itself.
3513 * Fail if the initial prefixlen-ancestor does not already exist.
3516 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3522 /* make sure prefix exists */
3523 cp
= target
+ prefixlen
;
3525 assert(strchr(cp
, '/') == NULL
);
3526 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3529 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3537 * Attempt to create, mount, and share any ancestor filesystems,
3538 * up to the prefixlen-long one.
3540 for (cp
= target
+ prefixlen
+ 1;
3541 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3545 h
= make_dataset_handle(hdl
, target
);
3547 /* it already exists, nothing to do here */
3552 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3554 opname
= dgettext(TEXT_DOMAIN
, "create");
3558 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3560 opname
= dgettext(TEXT_DOMAIN
, "open");
3564 if (zfs_mount(h
, NULL
, 0) != 0) {
3565 opname
= dgettext(TEXT_DOMAIN
, "mount");
3569 if (zfs_share(h
) != 0) {
3570 opname
= dgettext(TEXT_DOMAIN
, "share");
3576 zfs_commit_all_shares();
3581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3582 "failed to %s ancestor '%s'"), opname
, target
);
3587 * Creates non-existing ancestors of the given path.
3590 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3597 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3598 "cannot create '%s'"), path
);
3601 * Check that we are not passing the nesting limit
3602 * before we start creating any ancestors.
3604 if (dataset_nestcheck(path
) != 0) {
3605 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3606 "maximum name nesting depth exceeded"));
3607 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3610 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3613 if ((path_copy
= strdup(path
)) != NULL
) {
3614 rc
= create_parents(hdl
, path_copy
, prefix
);
3617 if (path_copy
== NULL
|| rc
!= 0)
3624 * Create a new filesystem or volume.
3627 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3632 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3634 enum lzc_dataset_type ost
;
3635 zpool_handle_t
*zpool_handle
;
3636 uint8_t *wkeydata
= NULL
;
3639 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3641 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3642 "cannot create '%s'"), path
);
3644 /* validate the path, taking care to note the extended error message */
3645 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3646 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3648 if (dataset_nestcheck(path
) != 0) {
3649 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3650 "maximum name nesting depth exceeded"));
3651 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3654 /* validate parents exist */
3655 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3659 * The failure modes when creating a dataset of a different type over
3660 * one that already exists is a little strange. In particular, if you
3661 * try to create a dataset on top of an existing dataset, the ioctl()
3662 * will return ENOENT, not EEXIST. To prevent this from happening, we
3663 * first try to see if the dataset exists.
3665 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3666 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3667 "dataset already exists"));
3668 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3671 if (type
== ZFS_TYPE_VOLUME
)
3672 ost
= LZC_DATSET_TYPE_ZVOL
;
3674 ost
= LZC_DATSET_TYPE_ZFS
;
3676 /* open zpool handle for prop validation */
3677 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3678 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3680 /* truncate pool_path at first slash */
3681 char *p
= strchr(pool_path
, '/');
3685 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3688 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3689 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3690 zpool_close(zpool_handle
);
3693 zpool_close(zpool_handle
);
3695 if (type
== ZFS_TYPE_VOLUME
) {
3697 * If we are creating a volume, the size and block size must
3698 * satisfy a few restraints. First, the blocksize must be a
3699 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3700 * volsize must be a multiple of the block size, and cannot be
3703 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3704 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3706 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3707 "missing volume size"));
3708 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3711 if ((ret
= nvlist_lookup_uint64(props
,
3712 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3713 &blocksize
)) != 0) {
3714 if (ret
== ENOENT
) {
3715 blocksize
= zfs_prop_default_numeric(
3716 ZFS_PROP_VOLBLOCKSIZE
);
3719 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3720 "missing volume block size"));
3721 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3727 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3728 "volume size cannot be zero"));
3729 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3732 if (size
% blocksize
!= 0) {
3734 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3735 "volume size must be a multiple of volume block "
3737 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3741 (void) parent_name(path
, parent
, sizeof (parent
));
3742 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3743 &wkeydata
, &wkeylen
) != 0) {
3745 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3748 /* create the dataset */
3749 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3751 if (wkeydata
!= NULL
)
3754 /* check for failure */
3758 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3759 "no such parent '%s'"), parent
);
3760 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3763 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3764 "pool must be upgraded to set this "
3765 "property or value"));
3766 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3769 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3770 "encryption root's key is not loaded "
3772 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3775 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3776 "invalid property value(s) specified"));
3777 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3781 * This platform can't address a volume this big.
3783 if (type
== ZFS_TYPE_VOLUME
)
3784 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3789 return (zfs_standard_error(hdl
, errno
, errbuf
));
3797 * Destroys the given dataset. The caller must make sure that the filesystem
3798 * isn't mounted, and that there are no active dependents. If the file system
3799 * does not exist this function does nothing.
3802 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3806 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3809 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3810 nvlist_t
*nv
= fnvlist_alloc();
3811 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3812 error
= lzc_destroy_bookmarks(nv
, NULL
);
3815 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3816 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3822 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3823 nvlist_t
*nv
= fnvlist_alloc();
3824 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3825 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3828 error
= lzc_destroy(zhp
->zfs_name
);
3831 if (error
!= 0 && error
!= ENOENT
) {
3832 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3833 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3837 remove_mountpoint(zhp
);
3842 struct destroydata
{
3844 const char *snapname
;
3848 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3850 struct destroydata
*dd
= arg
;
3851 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3854 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3855 dd
->snapname
) >= sizeof (name
))
3858 if (lzc_exists(name
))
3859 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3861 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3867 * Destroys all snapshots with the given name in zhp & descendants.
3870 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3873 struct destroydata dd
= { 0 };
3875 dd
.snapname
= snapname
;
3876 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3877 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3879 if (nvlist_empty(dd
.nvl
)) {
3880 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3881 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3882 zhp
->zfs_name
, snapname
);
3884 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3886 nvlist_free(dd
.nvl
);
3891 * Destroys all the snapshots named in the nvlist.
3894 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3896 nvlist_t
*errlist
= NULL
;
3899 int ret
= zfs_destroy_snaps_nvl_os(hdl
, snaps
);
3903 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3906 nvlist_free(errlist
);
3910 if (nvlist_empty(errlist
)) {
3912 (void) snprintf(errbuf
, sizeof (errbuf
),
3913 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3915 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3917 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3918 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3920 (void) snprintf(errbuf
, sizeof (errbuf
),
3921 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3924 switch (fnvpair_value_int32(pair
)) {
3927 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3928 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3931 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3936 nvlist_free(errlist
);
3941 * Clones the given dataset. The target must be of the same type as the source.
3944 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3946 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3949 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3952 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3954 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3955 "cannot create '%s'"), target
);
3957 /* validate the target/clone name */
3958 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3959 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3961 /* validate parents exist */
3962 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3965 (void) parent_name(target
, parent
, sizeof (parent
));
3972 if (ZFS_IS_VOLUME(zhp
)) {
3973 type
= ZFS_TYPE_VOLUME
;
3975 type
= ZFS_TYPE_FILESYSTEM
;
3977 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3978 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
3980 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
3986 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
3988 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3991 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3999 * The parent doesn't exist. We should have caught this
4000 * above, but there may a race condition that has since
4001 * destroyed the parent.
4003 * At this point, we don't know whether it's the source
4004 * that doesn't exist anymore, or whether the target
4005 * dataset doesn't exist.
4007 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4008 "no such parent '%s'"), parent
);
4009 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4012 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4013 "source and target pools differ"));
4014 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4018 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4027 * Promotes the given clone fs to be the clone parent.
4030 zfs_promote(zfs_handle_t
*zhp
)
4032 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4033 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4037 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4038 "cannot promote '%s'"), zhp
->zfs_name
);
4040 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4042 "snapshots can not be promoted"));
4043 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4046 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4047 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4048 "not a cloned filesystem"));
4049 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4052 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4053 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4055 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4061 * Promoting encrypted dataset outside its
4064 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4065 "cannot promote dataset outside its "
4066 "encryption root"));
4067 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4070 /* There is a conflicting snapshot name. */
4071 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4072 "conflicting snapshot '%s' from parent '%s'"),
4073 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4074 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4077 return (zfs_standard_error(hdl
, ret
, errbuf
));
4083 typedef struct snapdata
{
4085 const char *sd_snapname
;
4089 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4091 snapdata_t
*sd
= arg
;
4092 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4095 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4096 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4097 sd
->sd_snapname
) >= sizeof (name
))
4100 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4102 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4110 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4114 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4120 zpool_handle_t
*zpool_hdl
;
4121 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4123 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4124 "cannot create snapshots "));
4127 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4128 const char *snapname
= nvpair_name(elem
);
4130 /* validate the target name */
4131 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4133 (void) snprintf(errbuf
, sizeof (errbuf
),
4134 dgettext(TEXT_DOMAIN
,
4135 "cannot create snapshot '%s'"), snapname
);
4136 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4141 * get pool handle for prop validation. assumes all snaps are in the
4142 * same pool, as does lzc_snapshot (below).
4144 elem
= nvlist_next_nvpair(snaps
, NULL
);
4145 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4146 pool
[strcspn(pool
, "/@")] = '\0';
4147 zpool_hdl
= zpool_open(hdl
, pool
);
4148 if (zpool_hdl
== NULL
)
4151 if (props
!= NULL
&&
4152 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4153 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4154 zpool_close(zpool_hdl
);
4157 zpool_close(zpool_hdl
);
4159 ret
= lzc_snapshot(snaps
, props
, &errors
);
4162 boolean_t printed
= B_FALSE
;
4163 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4165 elem
= nvlist_next_nvpair(errors
, elem
)) {
4166 (void) snprintf(errbuf
, sizeof (errbuf
),
4167 dgettext(TEXT_DOMAIN
,
4168 "cannot create snapshot '%s'"), nvpair_name(elem
));
4169 (void) zfs_standard_error(hdl
,
4170 fnvpair_value_int32(elem
), errbuf
);
4176 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4177 "multiple snapshots of same "
4179 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4183 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4189 nvlist_free(errors
);
4194 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4198 snapdata_t sd
= { 0 };
4199 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4204 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4205 "cannot snapshot %s"), path
);
4207 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4208 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4210 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4211 cp
= strchr(fsname
, '@');
4213 sd
.sd_snapname
= cp
+ 1;
4215 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4216 ZFS_TYPE_VOLUME
)) == NULL
) {
4220 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4222 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4224 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4227 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4228 nvlist_free(sd
.sd_nvl
);
4234 * Destroy any more recent snapshots. We invoke this callback on any dependents
4235 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4236 * is a dependent and we should just destroy it without checking the transaction
4239 typedef struct rollback_data
{
4240 const char *cb_target
; /* the snapshot */
4241 uint64_t cb_create
; /* creation time reference */
4247 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4249 rollback_data_t
*cbp
= data
;
4250 prop_changelist_t
*clp
;
4252 /* We must destroy this clone; first unmount it */
4253 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4254 cbp
->cb_force
? MS_FORCE
: 0);
4255 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4256 cbp
->cb_error
= B_TRUE
;
4260 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4261 cbp
->cb_error
= B_TRUE
;
4263 changelist_remove(clp
, zhp
->zfs_name
);
4264 (void) changelist_postfix(clp
);
4265 changelist_free(clp
);
4272 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4274 rollback_data_t
*cbp
= data
;
4276 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4277 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4278 rollback_destroy_dependent
, cbp
);
4280 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4288 * Given a dataset, rollback to a specific snapshot, discarding any
4289 * data changes since then and making it the active dataset.
4291 * Any snapshots and bookmarks more recent than the target are
4292 * destroyed, along with their dependents (i.e. clones).
4295 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4297 rollback_data_t cb
= { 0 };
4299 boolean_t restore_resv
= 0;
4300 uint64_t old_volsize
= 0, new_volsize
;
4301 zfs_prop_t resv_prop
= { 0 };
4302 uint64_t min_txg
= 0;
4304 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4305 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4308 * Destroy all recent snapshots and their dependents.
4310 cb
.cb_force
= force
;
4311 cb
.cb_target
= snap
->zfs_name
;
4312 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4314 if (cb
.cb_create
> 0)
4315 min_txg
= cb
.cb_create
;
4317 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
,
4320 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4326 * Now that we have verified that the snapshot is the latest,
4327 * rollback to the given snapshot.
4330 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4331 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4333 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4335 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4339 * Pass both the filesystem and the wanted snapshot names,
4340 * we would get an error back if the snapshot is destroyed or
4341 * a new snapshot is created before this request is processed.
4343 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4347 (void) snprintf(errbuf
, sizeof (errbuf
),
4348 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4352 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4353 "there is a snapshot or bookmark more recent "
4354 "than '%s'"), snap
->zfs_name
);
4355 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4358 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4359 "'%s' is not found among snapshots of '%s'"),
4360 snap
->zfs_name
, zhp
->zfs_name
);
4361 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4364 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4367 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4373 * For volumes, if the pre-rollback volsize matched the pre-
4374 * rollback reservation and the volsize has changed then set
4375 * the reservation property to the post-rollback volsize.
4376 * Make a new handle since the rollback closed the dataset.
4378 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4379 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4381 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4382 if (old_volsize
!= new_volsize
)
4383 err
= zfs_prop_set_int(zhp
, resv_prop
,
4392 * Renames the given dataset.
4395 zfs_rename(zfs_handle_t
*zhp
, const char *target
, renameflags_t flags
)
4398 zfs_cmd_t zc
= {"\0"};
4400 prop_changelist_t
*cl
= NULL
;
4401 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4402 char property
[ZFS_MAXPROPLEN
];
4403 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4406 /* if we have the same exact name, just return success */
4407 if (strcmp(zhp
->zfs_name
, target
) == 0)
4410 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4411 "cannot rename to '%s'"), target
);
4413 /* make sure source name is valid */
4414 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4415 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4418 * Make sure the target name is valid
4420 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4421 if ((strchr(target
, '@') == NULL
) ||
4424 * Snapshot target name is abbreviated,
4425 * reconstruct full dataset name
4427 (void) strlcpy(parent
, zhp
->zfs_name
,
4429 delim
= strchr(parent
, '@');
4430 if (strchr(target
, '@') == NULL
)
4434 (void) strlcat(parent
, target
, sizeof (parent
));
4438 * Make sure we're renaming within the same dataset.
4440 delim
= strchr(target
, '@');
4441 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4442 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4443 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4444 "snapshots must be part of same "
4446 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4451 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4452 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4454 if (flags
.recursive
) {
4455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4456 "recursive rename must be a snapshot"));
4457 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4460 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4461 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4463 /* validate parents */
4464 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4467 /* make sure we're in the same pool */
4468 verify((delim
= strchr(target
, '/')) != NULL
);
4469 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4470 zhp
->zfs_name
[delim
- target
] != '/') {
4471 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4472 "datasets must be within same pool"));
4473 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4476 /* new name cannot be a child of the current dataset name */
4477 if (is_descendant(zhp
->zfs_name
, target
)) {
4478 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4479 "New dataset name cannot be a descendant of "
4480 "current dataset name"));
4481 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4485 (void) snprintf(errbuf
, sizeof (errbuf
),
4486 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4488 if (getzoneid() == GLOBAL_ZONEID
&&
4489 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4490 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4491 "dataset is used in a non-global zone"));
4492 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4496 * Avoid unmounting file systems with mountpoint property set to
4497 * 'legacy' or 'none' even if -u option is not given.
4499 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
4500 !flags
.recursive
&& !flags
.nounmount
&&
4501 zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, property
,
4502 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) == 0 &&
4503 (strcmp(property
, "legacy") == 0 ||
4504 strcmp(property
, "none") == 0)) {
4505 flags
.nounmount
= B_TRUE
;
4507 if (flags
.recursive
) {
4508 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4509 if (parentname
== NULL
) {
4513 delim
= strchr(parentname
, '@');
4515 zfs_handle_t
*zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
,
4523 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4524 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4525 flags
.nounmount
? CL_GATHER_DONT_UNMOUNT
:
4526 CL_GATHER_ITER_MOUNTED
,
4527 flags
.forceunmount
? MS_FORCE
: 0)) == NULL
)
4530 if (changelist_haszonedchild(cl
)) {
4531 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4532 "child dataset with inherited mountpoint is used "
4533 "in a non-global zone"));
4534 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4539 if ((ret
= changelist_prefix(cl
)) != 0)
4543 if (ZFS_IS_VOLUME(zhp
))
4544 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4546 zc
.zc_objset_type
= DMU_OST_ZFS
;
4548 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4549 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4551 zc
.zc_cookie
= !!flags
.recursive
;
4552 zc
.zc_cookie
|= (!!flags
.nounmount
) << 1;
4554 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4556 * if it was recursive, the one that actually failed will
4559 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4560 "cannot rename '%s'"), zc
.zc_name
);
4562 if (flags
.recursive
&& errno
== EEXIST
) {
4563 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4564 "a child dataset already has a snapshot "
4565 "with the new name"));
4566 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4567 } else if (errno
== EACCES
) {
4568 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4569 "cannot move encrypted child outside of "
4570 "its encryption root"));
4571 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4573 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4577 * On failure, we still want to remount any filesystems that
4578 * were previously mounted, so we don't alter the system state.
4581 (void) changelist_postfix(cl
);
4584 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4585 ret
= changelist_postfix(cl
);
4591 changelist_free(cl
);
4597 zfs_get_all_props(zfs_handle_t
*zhp
)
4599 return (zhp
->zfs_props
);
4603 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4605 if (zhp
->zfs_recvd_props
== NULL
)
4606 if (get_recvd_props_ioctl(zhp
) != 0)
4608 return (zhp
->zfs_recvd_props
);
4612 zfs_get_user_props(zfs_handle_t
*zhp
)
4614 return (zhp
->zfs_user_props
);
4618 * This function is used by 'zfs list' to determine the exact set of columns to
4619 * display, and their maximum widths. This does two main things:
4621 * - If this is a list of all properties, then expand the list to include
4622 * all native properties, and set a flag so that for each dataset we look
4623 * for new unique user properties and add them to the list.
4625 * - For non fixed-width properties, keep track of the maximum width seen
4626 * so that we can size the column appropriately. If the user has
4627 * requested received property values, we also need to compute the width
4628 * of the RECEIVED column.
4631 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4634 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4635 zprop_list_t
*entry
;
4636 zprop_list_t
**last
, **start
;
4637 nvlist_t
*userprops
, *propval
;
4640 char buf
[ZFS_MAXPROPLEN
];
4642 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4645 userprops
= zfs_get_user_props(zhp
);
4648 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4650 * Go through and add any user properties as necessary. We
4651 * start by incrementing our list pointer to the first
4652 * non-native property.
4655 while (*start
!= NULL
) {
4656 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4658 start
= &(*start
)->pl_next
;
4662 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4664 * See if we've already found this property in our list.
4666 for (last
= start
; *last
!= NULL
;
4667 last
= &(*last
)->pl_next
) {
4668 if (strcmp((*last
)->pl_user_prop
,
4669 nvpair_name(elem
)) == 0)
4673 if (*last
== NULL
) {
4674 if ((entry
= zfs_alloc(hdl
,
4675 sizeof (zprop_list_t
))) == NULL
||
4676 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4677 nvpair_name(elem
)))) == NULL
) {
4682 entry
->pl_prop
= ZPROP_INVAL
;
4683 entry
->pl_width
= strlen(nvpair_name(elem
));
4684 entry
->pl_all
= B_TRUE
;
4691 * Now go through and check the width of any non-fixed columns
4693 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4694 if (entry
->pl_fixed
&& !literal
)
4697 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4698 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4699 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4700 if (strlen(buf
) > entry
->pl_width
)
4701 entry
->pl_width
= strlen(buf
);
4703 if (received
&& zfs_prop_get_recvd(zhp
,
4704 zfs_prop_to_name(entry
->pl_prop
),
4705 buf
, sizeof (buf
), literal
) == 0)
4706 if (strlen(buf
) > entry
->pl_recvd_width
)
4707 entry
->pl_recvd_width
= strlen(buf
);
4709 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4711 verify(nvlist_lookup_string(propval
,
4712 ZPROP_VALUE
, &strval
) == 0);
4713 if (strlen(strval
) > entry
->pl_width
)
4714 entry
->pl_width
= strlen(strval
);
4716 if (received
&& zfs_prop_get_recvd(zhp
,
4717 entry
->pl_user_prop
,
4718 buf
, sizeof (buf
), literal
) == 0)
4719 if (strlen(buf
) > entry
->pl_recvd_width
)
4720 entry
->pl_recvd_width
= strlen(buf
);
4728 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4734 * Keep a reference to the props-table against which we prune the
4737 zhp
->zfs_props_table
= props
;
4739 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4742 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4743 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4746 * User properties will result in ZPROP_INVAL, and since we
4747 * only know how to prune standard ZFS properties, we always
4748 * leave these in the list. This can also happen if we
4749 * encounter an unknown DSL property (when running older
4750 * software, for example).
4752 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4753 (void) nvlist_remove(zhp
->zfs_props
,
4754 nvpair_name(curr
), nvpair_type(curr
));
4760 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4761 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4763 zfs_cmd_t zc
= {"\0"};
4764 nvlist_t
*nvlist
= NULL
;
4767 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4768 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4769 zc
.zc_cookie
= (uint64_t)cmd
;
4771 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4772 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4773 (void) no_memory(hdl
);
4779 case ZFS_SMB_ACL_ADD
:
4780 case ZFS_SMB_ACL_REMOVE
:
4781 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4783 case ZFS_SMB_ACL_RENAME
:
4784 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4786 (void) no_memory(hdl
);
4789 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4791 (void) no_memory(hdl
);
4794 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4795 nvlist_free(nvlist
);
4799 case ZFS_SMB_ACL_PURGE
:
4804 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4805 nvlist_free(nvlist
);
4810 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4811 char *path
, char *resource
)
4813 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4818 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4819 char *path
, char *resource
)
4821 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4826 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4828 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4833 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4834 char *oldname
, char *newname
)
4836 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4841 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4842 zfs_userspace_cb_t func
, void *arg
)
4844 zfs_cmd_t zc
= {"\0"};
4845 zfs_useracct_t buf
[100];
4846 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4849 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4851 zc
.zc_objset_type
= type
;
4852 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4855 zfs_useracct_t
*zua
= buf
;
4857 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4858 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4859 if ((errno
== ENOTSUP
&&
4860 (type
== ZFS_PROP_USEROBJUSED
||
4861 type
== ZFS_PROP_GROUPOBJUSED
||
4862 type
== ZFS_PROP_USEROBJQUOTA
||
4863 type
== ZFS_PROP_GROUPOBJQUOTA
||
4864 type
== ZFS_PROP_PROJECTOBJUSED
||
4865 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4866 type
== ZFS_PROP_PROJECTUSED
||
4867 type
== ZFS_PROP_PROJECTQUOTA
)))
4870 return (zfs_standard_error_fmt(hdl
, errno
,
4871 dgettext(TEXT_DOMAIN
,
4872 "cannot get used/quota for %s"), zc
.zc_name
));
4874 if (zc
.zc_nvlist_dst_size
== 0)
4877 while (zc
.zc_nvlist_dst_size
> 0) {
4878 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4879 zua
->zu_space
)) != 0)
4882 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4891 const char *snapname
;
4893 boolean_t recursive
;
4898 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4900 struct holdarg
*ha
= arg
;
4901 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4904 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4905 ha
->snapname
) >= sizeof (name
))
4908 if (lzc_exists(name
))
4909 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4912 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4918 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4919 boolean_t recursive
, int cleanup_fd
)
4924 ha
.nvl
= fnvlist_alloc();
4925 ha
.snapname
= snapname
;
4927 ha
.recursive
= recursive
;
4928 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4930 if (nvlist_empty(ha
.nvl
)) {
4933 fnvlist_free(ha
.nvl
);
4935 (void) snprintf(errbuf
, sizeof (errbuf
),
4936 dgettext(TEXT_DOMAIN
,
4937 "cannot hold snapshot '%s@%s'"),
4938 zhp
->zfs_name
, snapname
);
4939 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4943 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4944 fnvlist_free(ha
.nvl
);
4950 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4954 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4959 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4962 /* There may be errors even in the success case. */
4963 fnvlist_free(errors
);
4967 if (nvlist_empty(errors
)) {
4968 /* no hold-specific errors */
4969 (void) snprintf(errbuf
, sizeof (errbuf
),
4970 dgettext(TEXT_DOMAIN
, "cannot hold"));
4973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4974 "pool must be upgraded"));
4975 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4978 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4981 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4985 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4987 elem
= nvlist_next_nvpair(errors
, elem
)) {
4988 (void) snprintf(errbuf
, sizeof (errbuf
),
4989 dgettext(TEXT_DOMAIN
,
4990 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4991 switch (fnvpair_value_int32(elem
)) {
4994 * Temporary tags wind up having the ds object id
4995 * prepended. So even if we passed the length check
4996 * above, it's still possible for the tag to wind
4997 * up being slightly too long.
4999 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5002 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5005 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5008 (void) zfs_standard_error(hdl
,
5009 fnvpair_value_int32(elem
), errbuf
);
5013 fnvlist_free(errors
);
5018 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5020 struct holdarg
*ha
= arg
;
5021 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5023 nvlist_t
*existing_holds
;
5025 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5026 ha
->snapname
) >= sizeof (name
)) {
5031 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5033 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5036 nvlist_t
*torelease
= fnvlist_alloc();
5037 fnvlist_add_boolean(torelease
, ha
->tag
);
5038 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5039 fnvlist_free(torelease
);
5043 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5049 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5050 boolean_t recursive
)
5054 nvlist_t
*errors
= NULL
;
5056 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5059 ha
.nvl
= fnvlist_alloc();
5060 ha
.snapname
= snapname
;
5062 ha
.recursive
= recursive
;
5064 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5066 if (nvlist_empty(ha
.nvl
)) {
5067 fnvlist_free(ha
.nvl
);
5069 (void) snprintf(errbuf
, sizeof (errbuf
),
5070 dgettext(TEXT_DOMAIN
,
5071 "cannot release hold from snapshot '%s@%s'"),
5072 zhp
->zfs_name
, snapname
);
5074 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5076 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5081 ret
= lzc_release(ha
.nvl
, &errors
);
5082 fnvlist_free(ha
.nvl
);
5085 /* There may be errors even in the success case. */
5086 fnvlist_free(errors
);
5090 if (nvlist_empty(errors
)) {
5091 /* no hold-specific errors */
5092 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5096 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5097 "pool must be upgraded"));
5098 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5101 (void) zfs_standard_error(hdl
, errno
, errbuf
);
5105 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5107 elem
= nvlist_next_nvpair(errors
, elem
)) {
5108 (void) snprintf(errbuf
, sizeof (errbuf
),
5109 dgettext(TEXT_DOMAIN
,
5110 "cannot release hold from snapshot '%s'"),
5112 switch (fnvpair_value_int32(elem
)) {
5114 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5117 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5120 (void) zfs_standard_error(hdl
,
5121 fnvpair_value_int32(elem
), errbuf
);
5125 fnvlist_free(errors
);
5130 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5132 zfs_cmd_t zc
= {"\0"};
5133 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5139 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5140 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5144 nvbuf
= malloc(nvsz
);
5145 if (nvbuf
== NULL
) {
5146 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5150 zc
.zc_nvlist_dst_size
= nvsz
;
5151 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5153 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5155 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5156 (void) snprintf(errbuf
, sizeof (errbuf
),
5157 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5162 nvsz
= zc
.zc_nvlist_dst_size
;
5166 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5167 "pool must be upgraded"));
5168 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5171 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5174 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5177 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5182 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5184 err
= zfs_standard_error_fmt(hdl
, rc
, dgettext(
5185 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5196 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5198 zfs_cmd_t zc
= {"\0"};
5199 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5205 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5206 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5208 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5211 nvbuf
= malloc(nvsz
);
5213 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5216 zc
.zc_nvlist_src_size
= nvsz
;
5217 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5218 zc
.zc_perm_action
= un
;
5220 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5222 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5223 (void) snprintf(errbuf
, sizeof (errbuf
),
5224 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5228 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5229 "pool must be upgraded"));
5230 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5233 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5236 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5239 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5250 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5255 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5258 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5260 (void) snprintf(errbuf
, sizeof (errbuf
),
5261 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5265 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5266 "pool must be upgraded"));
5267 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5270 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5273 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5276 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5285 * The theory of raidz space accounting
5287 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5288 * will "reference" 128KB, even though it allocates more than that, to store the
5289 * parity information (and perhaps skip sectors). This concept of the
5290 * "referenced" (and other DMU space accounting) being lower than the allocated
5291 * space by a constant factor is called "raidz deflation."
5293 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5294 * block size. However, zvols typically have a much smaller block size (default
5295 * 8KB). These smaller blocks may require proportionally much more parity
5296 * information (and perhaps skip sectors). In this case, the change to the
5297 * "referenced" property may be much more than the logical block size.
5299 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5302 * +-------+-------+-------+-------+-------+
5303 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5304 * +-------+-------+-------+-------+-------+
5305 * | P0 | D0 | D8 | D16 | D24 |
5306 * | P1 | D1 | D9 | D17 | D25 |
5307 * | P2 | D2 | D10 | D18 | D26 |
5308 * | P3 | D3 | D11 | D19 | D27 |
5309 * | P4 | D4 | D12 | D20 | D28 |
5310 * | P5 | D5 | D13 | D21 | D29 |
5311 * | P6 | D6 | D14 | D22 | D30 |
5312 * | P7 | D7 | D15 | D23 | D31 |
5313 * +-------+-------+-------+-------+-------+
5315 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5316 * sectors. The dataset's referenced will increase by 128k and the pool's
5317 * allocated and free properties will be adjusted by 160k.
5319 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5320 * blank cells represent unallocated space.
5322 * +-------+-------+-------+-------+-------+
5323 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5324 * +-------+-------+-------+-------+-------+
5326 * +-------+-------+-------+-------+-------+
5328 * Above, notice that the 4k block required one sector for parity and another
5329 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5330 * and free properties will be adjusted by 8k. The dataset will not be charged
5331 * 8k. Rather, it will be charged a value that is scaled according to the
5332 * overhead of the 128k block on the same vdev. This 8k allocation will be
5333 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5334 * calculated in the 128k block example above.
5336 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5337 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5338 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5339 * multiple of of 4 sectors. When a block does not fill the required number of
5340 * sectors, skip blocks (sectors) are used.
5342 * An 8k block being written to a raidz vdev may be written as follows:
5344 * +-------+-------+-------+-------+-------+
5345 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5346 * +-------+-------+-------+-------+-------+
5347 * | P0 | D0 | D1 | S0 | |
5348 * +-------+-------+-------+-------+-------+
5350 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5351 * added. For this 8k block, the pool's allocated and free properties are
5352 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5353 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5354 * the 128k block example above.
5356 * The situation is slightly different for dRAID since the minimum allocation
5357 * size is the full group width. The same 8K block above would be written as
5358 * follows in a dRAID group:
5360 * +-------+-------+-------+-------+-------+
5361 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5362 * +-------+-------+-------+-------+-------+
5363 * | P0 | D0 | D1 | S0 | S1 |
5364 * +-------+-------+-------+-------+-------+
5366 * Compression may lead to a variety of block sizes being written for the same
5367 * volume or file. There is no clear way to reserve just the amount of space
5368 * that will be required, so the worst case (no compression) is assumed.
5369 * Note that metadata blocks will typically be compressed, so the reservation
5370 * size returned by zvol_volsize_to_reservation() will generally be slightly
5371 * larger than the maximum that the volume can reference.
5375 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5376 * amount of space (in bytes) that will be allocated for the specified block
5377 * size. Note that the "referenced" space accounted will be less than this, but
5378 * not necessarily equal to "blksize", due to RAIDZ deflation.
5381 vdev_raidz_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5384 uint64_t asize
, ndata
;
5386 ASSERT3U(ndisks
, >, nparity
);
5387 ndata
= ndisks
- nparity
;
5388 asize
= ((blksize
- 1) >> ashift
) + 1;
5389 asize
+= nparity
* ((asize
+ ndata
- 1) / ndata
);
5390 asize
= roundup(asize
, nparity
+ 1) << ashift
;
5396 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5397 * amount of space (in bytes) that will be allocated for the specified block
5401 vdev_draid_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5404 ASSERT3U(ndisks
, >, nparity
);
5405 uint64_t ndata
= ndisks
- nparity
;
5406 uint64_t rows
= ((blksize
- 1) / (ndata
<< ashift
)) + 1;
5407 uint64_t asize
= (rows
* ndisks
) << ashift
;
5413 * Determine how much space will be allocated if it lands on the most space-
5414 * inefficient top-level vdev. Returns the size in bytes required to store one
5415 * copy of the volume data. See theory comment above.
5418 volsize_from_vdevs(zpool_handle_t
*zhp
, uint64_t nblocks
, uint64_t blksize
)
5420 nvlist_t
*config
, *tree
, **vdevs
;
5424 config
= zpool_get_config(zhp
, NULL
);
5425 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
5426 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
5427 &vdevs
, &nvdevs
) != 0) {
5428 return (nblocks
* blksize
);
5431 for (int v
= 0; v
< nvdevs
; v
++) {
5433 uint64_t nparity
, ashift
, asize
, tsize
;
5436 if (nvlist_lookup_string(vdevs
[v
], ZPOOL_CONFIG_TYPE
,
5440 if (strcmp(type
, VDEV_TYPE_RAIDZ
) != 0 &&
5441 strcmp(type
, VDEV_TYPE_DRAID
) != 0)
5444 if (nvlist_lookup_uint64(vdevs
[v
],
5445 ZPOOL_CONFIG_NPARITY
, &nparity
) != 0)
5448 if (nvlist_lookup_uint64(vdevs
[v
],
5449 ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
5452 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
5456 if (nvlist_lookup_nvlist_array(vdevs
[v
],
5457 ZPOOL_CONFIG_CHILDREN
, &disks
, &ndisks
) != 0)
5460 /* allocation size for the "typical" 128k block */
5461 tsize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5462 SPA_OLD_MAXBLOCKSIZE
);
5464 /* allocation size for the blksize block */
5465 asize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5470 if (nvlist_lookup_uint64(vdevs
[v
],
5471 ZPOOL_CONFIG_DRAID_NDATA
, &ndata
) != 0)
5474 /* allocation size for the "typical" 128k block */
5475 tsize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5476 ashift
, SPA_OLD_MAXBLOCKSIZE
);
5478 /* allocation size for the blksize block */
5479 asize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5484 * Scale this size down as a ratio of 128k / tsize.
5485 * See theory statement above.
5487 volsize
= nblocks
* asize
* SPA_OLD_MAXBLOCKSIZE
/ tsize
;
5488 if (volsize
> ret
) {
5494 ret
= nblocks
* blksize
;
5501 * Convert the zvol's volume size to an appropriate reservation. See theory
5504 * Note: If this routine is updated, it is necessary to update the ZFS test
5505 * suite's shell version in reservation.shlib.
5508 zvol_volsize_to_reservation(zpool_handle_t
*zph
, uint64_t volsize
,
5512 uint64_t nblocks
, volblocksize
;
5516 if (nvlist_lookup_string(props
,
5517 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5518 ncopies
= atoi(strval
);
5521 if (nvlist_lookup_uint64(props
,
5522 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5523 &volblocksize
) != 0)
5524 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5526 nblocks
= volsize
/ volblocksize
;
5528 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5529 * this reason, only the data blocks are scaled based on vdev config.
5531 volsize
= volsize_from_vdevs(zph
, nblocks
, volblocksize
);
5533 /* start with metadnode L0-L6 */
5535 /* calculate number of indirects */
5536 while (nblocks
> 1) {
5537 nblocks
+= DNODES_PER_LEVEL
- 1;
5538 nblocks
/= DNODES_PER_LEVEL
;
5541 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5544 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5545 * compressed, but in practice they compress down to about
5548 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;
5554 * Wait for the given activity and return the status of the wait (whether or not
5555 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5556 * reported via the 'missing' parameter, rather than by printing an error
5557 * message. This is convenient when this function is called in a loop over a
5558 * long period of time (as it is, for example, by zfs's wait cmd). In that
5559 * scenario, a fs being exported or destroyed should be considered a normal
5560 * event, so we don't want to print an error when we find that the fs doesn't
5564 zfs_wait_status(zfs_handle_t
*zhp
, zfs_wait_activity_t activity
,
5565 boolean_t
*missing
, boolean_t
*waited
)
5567 int error
= lzc_wait_fs(zhp
->zfs_name
, activity
, waited
);
5568 *missing
= (error
== ENOENT
);
5573 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
5574 dgettext(TEXT_DOMAIN
, "error waiting in fs '%s'"),