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
;
239 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
240 (void) no_memory(hdl
);
245 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
246 if (!zfs_prop_user(nvpair_name(elem
)))
249 nvlist_t
*propval
= fnvpair_value_nvlist(elem
);
250 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
252 (void) no_memory(hdl
);
260 static zpool_handle_t
*
261 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
263 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
266 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
267 if (hdl
->libzfs_pool_handles
!= NULL
)
268 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
269 hdl
->libzfs_pool_handles
= zph
;
274 static zpool_handle_t
*
275 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
277 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
278 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
280 while ((zph
!= NULL
) &&
281 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
282 zph
= zph
->zpool_next
;
287 * Returns a handle to the pool that contains the provided dataset.
288 * If a handle to that pool already exists then that handle is returned.
289 * Otherwise, a new handle is created and added to the list of handles.
291 static zpool_handle_t
*
292 zpool_handle(zfs_handle_t
*zhp
)
298 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
299 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
300 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
302 zph
= zpool_find_handle(zhp
, pool_name
, len
);
304 zph
= zpool_add_handle(zhp
, pool_name
);
311 zpool_free_handles(libzfs_handle_t
*hdl
)
313 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
315 while (zph
!= NULL
) {
316 next
= zph
->zpool_next
;
320 hdl
->libzfs_pool_handles
= NULL
;
324 * Utility function to gather stats (objset and zpl) for the given object.
327 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
329 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
331 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
333 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
334 if (errno
== ENOMEM
) {
335 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
346 * Utility function to get the received properties of the given object.
349 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
351 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
352 nvlist_t
*recvdprops
;
353 zfs_cmd_t zc
= {"\0"};
356 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
359 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
361 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
362 if (errno
== ENOMEM
) {
363 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
367 zcmd_free_nvlists(&zc
);
372 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
373 zcmd_free_nvlists(&zc
);
377 nvlist_free(zhp
->zfs_recvd_props
);
378 zhp
->zfs_recvd_props
= recvdprops
;
384 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
386 nvlist_t
*allprops
, *userprops
;
388 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
390 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
395 * XXX Why do we store the user props separately, in addition to
396 * storing them in zfs_props?
398 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
399 nvlist_free(allprops
);
403 nvlist_free(zhp
->zfs_props
);
404 nvlist_free(zhp
->zfs_user_props
);
406 zhp
->zfs_props
= allprops
;
407 zhp
->zfs_user_props
= userprops
;
413 get_stats(zfs_handle_t
*zhp
)
416 zfs_cmd_t zc
= {"\0"};
418 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
420 if (get_stats_ioctl(zhp
, &zc
) != 0)
422 else if (put_stats_zhdl(zhp
, &zc
) != 0)
424 zcmd_free_nvlists(&zc
);
429 * Refresh the properties currently stored in the handle.
432 zfs_refresh_properties(zfs_handle_t
*zhp
)
434 (void) get_stats(zhp
);
438 * Makes a handle from the given dataset name. Used by zfs_open() and
439 * zfs_iter_* to create child handles on the fly.
442 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
444 if (put_stats_zhdl(zhp
, zc
) != 0)
448 * We've managed to open the dataset and gather statistics. Determine
449 * the high-level type.
451 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
) {
452 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
453 } else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
) {
454 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
455 } else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
) {
458 } else if (zhp
->zfs_dmustats
.dds_inconsistent
) {
465 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
466 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
467 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
468 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
469 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
470 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
472 abort(); /* we should never see any other types */
474 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
481 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
483 zfs_cmd_t zc
= {"\0"};
485 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
491 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
492 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
496 if (get_stats_ioctl(zhp
, &zc
) == -1) {
497 zcmd_free_nvlists(&zc
);
501 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
505 zcmd_free_nvlists(&zc
);
510 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
512 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
518 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
519 if (make_dataset_handle_common(zhp
, zc
) == -1) {
527 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
529 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
534 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
535 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
536 zhp
->zfs_head_type
= pzhp
->zfs_type
;
537 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
538 zhp
->zpool_hdl
= zpool_handle(zhp
);
544 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
546 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
551 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
552 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
553 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
554 sizeof (zhp
->zfs_name
));
555 zhp
->zfs_type
= zhp_orig
->zfs_type
;
556 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
557 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
558 if (zhp_orig
->zfs_props
!= NULL
) {
559 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
560 (void) no_memory(zhp
->zfs_hdl
);
565 if (zhp_orig
->zfs_user_props
!= NULL
) {
566 if (nvlist_dup(zhp_orig
->zfs_user_props
,
567 &zhp
->zfs_user_props
, 0) != 0) {
568 (void) no_memory(zhp
->zfs_hdl
);
573 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
574 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
575 &zhp
->zfs_recvd_props
, 0)) {
576 (void) no_memory(zhp
->zfs_hdl
);
581 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
582 if (zhp_orig
->zfs_mntopts
!= NULL
) {
583 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
584 zhp_orig
->zfs_mntopts
);
586 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
591 zfs_bookmark_exists(const char *path
)
595 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
601 (void) strlcpy(fsname
, path
, sizeof (fsname
));
602 pound
= strchr(fsname
, '#');
607 bmark_name
= pound
+ 1;
608 props
= fnvlist_alloc();
609 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
616 rv
= nvlist_exists(bmarks
, bmark_name
);
622 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
623 nvlist_t
*bmark_props
)
625 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
630 /* Fill in the name. */
631 zhp
->zfs_hdl
= parent
->zfs_hdl
;
632 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
634 /* Set the property lists. */
635 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
641 zhp
->zfs_head_type
= parent
->zfs_head_type
;
642 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
644 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
645 nvlist_free(zhp
->zfs_props
);
653 struct zfs_open_bookmarks_cb_data
{
659 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
661 struct zfs_open_bookmarks_cb_data
*dp
= data
;
664 * Is it the one we are looking for?
666 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
668 * We found it. Save it and let the caller know we are done.
675 * Not found. Close the handle and ask for another one.
682 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
683 * argument is a mask of acceptable types. The function will print an
684 * appropriate error message and return NULL if it can't be opened.
687 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
693 (void) snprintf(errbuf
, sizeof (errbuf
),
694 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
697 * Validate the name before we even try to open it.
699 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
700 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
705 * Bookmarks needs to be handled separately.
707 bookp
= strchr(path
, '#');
710 * Try to get stats for the dataset, which will tell us if it
714 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
715 (void) zfs_standard_error(hdl
, errno
, errbuf
);
719 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
721 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
724 * We need to cut out '#' and everything after '#'
725 * to get the parent dataset name only.
727 assert(bookp
- path
< sizeof (dsname
));
728 (void) strncpy(dsname
, path
, bookp
- path
);
729 dsname
[bookp
- path
] = '\0';
732 * Create handle for the parent dataset.
735 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
736 (void) zfs_standard_error(hdl
, errno
, errbuf
);
741 * Iterate bookmarks to find the right one.
744 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
745 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
746 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
750 if (cb_data
.zhp
== NULL
) {
751 (void) zfs_standard_error(hdl
, errno
, errbuf
);
763 if (!(types
& zhp
->zfs_type
)) {
764 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
773 * Release a ZFS handle. Nothing to do but free the associated memory.
776 zfs_close(zfs_handle_t
*zhp
)
778 if (zhp
->zfs_mntopts
)
779 free(zhp
->zfs_mntopts
);
780 nvlist_free(zhp
->zfs_props
);
781 nvlist_free(zhp
->zfs_user_props
);
782 nvlist_free(zhp
->zfs_recvd_props
);
786 typedef struct mnttab_node
{
787 struct mnttab mtn_mt
;
792 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
794 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
795 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
798 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
800 return (TREE_ISIGN(rv
));
804 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
806 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
807 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
808 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
809 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
813 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
818 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
821 while (getmntent(mnttab
, &entry
) == 0) {
825 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
828 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
829 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
830 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
831 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
832 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
834 /* Exclude duplicate mounts */
835 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
836 free(mtn
->mtn_mt
.mnt_special
);
837 free(mtn
->mtn_mt
.mnt_mountp
);
838 free(mtn
->mtn_mt
.mnt_fstype
);
839 free(mtn
->mtn_mt
.mnt_mntopts
);
844 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
847 (void) fclose(mnttab
);
852 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
857 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
859 free(mtn
->mtn_mt
.mnt_special
);
860 free(mtn
->mtn_mt
.mnt_mountp
);
861 free(mtn
->mtn_mt
.mnt_fstype
);
862 free(mtn
->mtn_mt
.mnt_mntopts
);
865 avl_destroy(&hdl
->libzfs_mnttab_cache
);
866 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
870 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
872 hdl
->libzfs_mnttab_enable
= enable
;
876 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
877 struct mnttab
*entry
)
884 if (!hdl
->libzfs_mnttab_enable
) {
885 struct mnttab srch
= { 0 };
887 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
888 libzfs_mnttab_fini(hdl
);
890 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
893 srch
.mnt_special
= (char *)fsname
;
894 srch
.mnt_fstype
= MNTTYPE_ZFS
;
895 ret
= getmntany(mnttab
, entry
, &srch
) ? ENOENT
: 0;
896 (void) fclose(mnttab
);
900 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
901 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
904 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
905 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
910 find
.mtn_mt
.mnt_special
= (char *)fsname
;
911 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
913 *entry
= mtn
->mtn_mt
;
916 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
921 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
922 const char *mountp
, const char *mntopts
)
926 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
927 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
928 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
929 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
930 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
931 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
932 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
934 * Another thread may have already added this entry
935 * via libzfs_mnttab_update. If so we should skip it.
937 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
) {
938 free(mtn
->mtn_mt
.mnt_special
);
939 free(mtn
->mtn_mt
.mnt_mountp
);
940 free(mtn
->mtn_mt
.mnt_fstype
);
941 free(mtn
->mtn_mt
.mnt_mntopts
);
944 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
947 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
951 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
956 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
957 find
.mtn_mt
.mnt_special
= (char *)fsname
;
958 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
960 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
961 free(ret
->mtn_mt
.mnt_special
);
962 free(ret
->mtn_mt
.mnt_mountp
);
963 free(ret
->mtn_mt
.mnt_fstype
);
964 free(ret
->mtn_mt
.mnt_mntopts
);
967 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
971 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
973 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
975 if (zpool_handle
== NULL
)
978 *spa_version
= zpool_get_prop_int(zpool_handle
,
979 ZPOOL_PROP_VERSION
, NULL
);
984 * The choice of reservation property depends on the SPA version.
987 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
991 if (zfs_spa_version(zhp
, &spa_version
) < 0)
994 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
995 *resv_prop
= ZFS_PROP_REFRESERVATION
;
997 *resv_prop
= ZFS_PROP_RESERVATION
;
1003 * Given an nvlist of properties to set, validates that they are correct, and
1004 * parses any numeric properties (index, boolean, etc) if they are specified as
1008 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1009 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1010 boolean_t key_params_ok
, const char *errbuf
)
1017 int chosen_normal
= -1;
1018 int chosen_utf
= -1;
1020 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1021 (void) no_memory(hdl
);
1026 * Make sure this property is valid and applies to this type.
1030 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1031 const char *propname
= nvpair_name(elem
);
1033 prop
= zfs_name_to_prop(propname
);
1034 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
1036 * This is a user property: make sure it's a
1037 * string, and that it's less than ZAP_MAXNAMELEN.
1039 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1040 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1041 "'%s' must be a string"), propname
);
1042 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1046 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1047 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1048 "property name '%s' is too long"),
1050 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1054 (void) nvpair_value_string(elem
, &strval
);
1055 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1056 (void) no_memory(hdl
);
1063 * Currently, only user properties can be modified on
1066 if (type
== ZFS_TYPE_SNAPSHOT
) {
1067 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1068 "this property can not be modified for snapshots"));
1069 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1073 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1074 zfs_userquota_prop_t uqtype
;
1075 char *newpropname
= NULL
;
1081 if (userquota_propname_decode(propname
, zoned
,
1082 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1084 dgettext(TEXT_DOMAIN
,
1085 "'%s' has an invalid user/group name"),
1087 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1091 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1092 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1093 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1094 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1095 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1096 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1098 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1100 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1105 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1106 (void) nvpair_value_string(elem
, &strval
);
1107 if (strcmp(strval
, "none") == 0) {
1109 } else if (zfs_nicestrtonum(hdl
,
1110 strval
, &intval
) != 0) {
1111 (void) zfs_error(hdl
,
1112 EZFS_BADPROP
, errbuf
);
1115 } else if (nvpair_type(elem
) ==
1117 (void) nvpair_value_uint64(elem
, &intval
);
1119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1120 "use 'none' to disable "
1121 "{user|group|project}quota"));
1125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1126 "'%s' must be a number"), propname
);
1127 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1132 * Encode the prop name as
1133 * userquota@<hex-rid>-domain, to make it easy
1134 * for the kernel to decode.
1136 rc
= asprintf(&newpropname
, "%s%llx-%s",
1137 zfs_userquota_prop_prefixes
[uqtype
],
1138 (longlong_t
)rid
, domain
);
1139 if (rc
== -1 || newpropname
== NULL
) {
1140 (void) no_memory(hdl
);
1147 if (nvlist_add_uint64_array(ret
, newpropname
,
1150 (void) no_memory(hdl
);
1155 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1157 "'%s' is readonly"),
1159 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1163 if (prop
== ZPROP_INVAL
) {
1164 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1165 "invalid property '%s'"), propname
);
1166 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1170 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1172 dgettext(TEXT_DOMAIN
, "'%s' does not "
1173 "apply to datasets of this type"), propname
);
1174 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1178 if (zfs_prop_readonly(prop
) &&
1179 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1180 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1182 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1184 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1188 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1189 &strval
, &intval
, errbuf
) != 0)
1193 * Perform some additional checks for specific properties.
1196 case ZFS_PROP_VERSION
:
1202 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1203 if (intval
< version
) {
1204 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1205 "Can not downgrade; already at version %u"),
1207 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1213 case ZFS_PROP_VOLBLOCKSIZE
:
1214 case ZFS_PROP_RECORDSIZE
:
1216 int maxbs
= SPA_MAXBLOCKSIZE
;
1219 if (zpool_hdl
!= NULL
) {
1220 maxbs
= zpool_get_prop_int(zpool_hdl
,
1221 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1224 * The value must be a power of two between
1225 * SPA_MINBLOCKSIZE and maxbs.
1227 if (intval
< SPA_MINBLOCKSIZE
||
1228 intval
> maxbs
|| !ISP2(intval
)) {
1229 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1230 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1231 "'%s' must be power of 2 from 512B "
1232 "to %s"), propname
, buf
);
1233 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1239 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1241 int maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1244 if (zpool_hdl
!= NULL
) {
1245 char state
[64] = "";
1247 maxbs
= zpool_get_prop_int(zpool_hdl
,
1248 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1251 * Issue a warning but do not fail so that
1252 * tests for settable properties succeed.
1254 if (zpool_prop_get_feature(zpool_hdl
,
1255 "feature@allocation_classes", state
,
1256 sizeof (state
)) != 0 ||
1257 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1258 (void) fprintf(stderr
, gettext(
1259 "%s: property requires a special "
1260 "device in the pool\n"), propname
);
1264 (intval
< SPA_MINBLOCKSIZE
||
1265 intval
> maxbs
|| !ISP2(intval
))) {
1266 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1267 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1268 "invalid '%s=%llu' property: must be zero "
1269 "or a power of 2 from 512B to %s"),
1270 propname
, (unsigned long long)intval
, buf
);
1271 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1277 case ZFS_PROP_MLSLABEL
:
1279 #ifdef HAVE_MLSLABEL
1281 * Verify the mlslabel string and convert to
1282 * internal hex label string.
1286 char *hex
= NULL
; /* internal label string */
1288 /* Default value is already OK. */
1289 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1292 /* Verify the label can be converted to binary form */
1293 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1294 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1295 L_NO_CORRECTION
, NULL
) == -1)) {
1299 /* Now translate to hex internal label string */
1300 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1306 m_label_free(new_sl
);
1308 /* If string is already in internal form, we're done. */
1309 if (strcmp(strval
, hex
) == 0) {
1314 /* Replace the label string with the internal form. */
1315 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1317 fnvlist_add_string(ret
, zfs_prop_to_name(prop
), hex
);
1323 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1324 "invalid mlslabel '%s'"), strval
);
1325 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1326 m_label_free(new_sl
); /* OK if null */
1329 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1330 "mlslabels are unsupported"));
1331 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1333 #endif /* HAVE_MLSLABEL */
1336 case ZFS_PROP_MOUNTPOINT
:
1338 namecheck_err_t why
;
1340 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1341 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1344 if (mountpoint_namecheck(strval
, &why
)) {
1346 case NAME_ERR_LEADING_SLASH
:
1348 dgettext(TEXT_DOMAIN
,
1349 "'%s' must be an absolute path, "
1350 "'none', or 'legacy'"), propname
);
1352 case NAME_ERR_TOOLONG
:
1354 dgettext(TEXT_DOMAIN
,
1355 "component of '%s' is too long"),
1361 dgettext(TEXT_DOMAIN
,
1362 "(%d) not defined"),
1366 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1372 case ZFS_PROP_SHARESMB
:
1373 case ZFS_PROP_SHARENFS
:
1375 * For the mountpoint and sharenfs or sharesmb
1376 * properties, check if it can be set in a
1377 * global/non-global zone based on
1378 * the zoned property value:
1380 * global zone non-global zone
1381 * --------------------------------------------------
1382 * zoned=on mountpoint (no) mountpoint (yes)
1383 * sharenfs (no) sharenfs (no)
1384 * sharesmb (no) sharesmb (no)
1386 * zoned=off mountpoint (yes) N/A
1391 if (getzoneid() == GLOBAL_ZONEID
) {
1392 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1393 "'%s' cannot be set on "
1394 "dataset in a non-global zone"),
1396 (void) zfs_error(hdl
, EZFS_ZONED
,
1399 } else if (prop
== ZFS_PROP_SHARENFS
||
1400 prop
== ZFS_PROP_SHARESMB
) {
1401 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1402 "'%s' cannot be set in "
1403 "a non-global zone"), propname
);
1404 (void) zfs_error(hdl
, EZFS_ZONED
,
1408 } else if (getzoneid() != GLOBAL_ZONEID
) {
1410 * If zoned property is 'off', this must be in
1411 * a global zone. If not, something is wrong.
1413 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1414 "'%s' cannot be set while dataset "
1415 "'zoned' property is set"), propname
);
1416 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1421 * At this point, it is legitimate to set the
1422 * property. Now we want to make sure that the
1423 * property value is valid if it is sharenfs.
1425 if ((prop
== ZFS_PROP_SHARENFS
||
1426 prop
== ZFS_PROP_SHARESMB
) &&
1427 strcmp(strval
, "on") != 0 &&
1428 strcmp(strval
, "off") != 0) {
1429 zfs_share_proto_t proto
;
1431 if (prop
== ZFS_PROP_SHARESMB
)
1436 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1437 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1438 "'%s' cannot be set to invalid "
1439 "options"), propname
);
1440 (void) zfs_error(hdl
, EZFS_BADPROP
,
1448 case ZFS_PROP_KEYLOCATION
:
1449 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1450 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1451 "invalid keylocation"));
1452 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1458 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1460 if (crypt
== ZIO_CRYPT_OFF
&&
1461 strcmp(strval
, "none") != 0) {
1462 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1463 "keylocation must be 'none' "
1464 "for unencrypted datasets"));
1465 (void) zfs_error(hdl
, EZFS_BADPROP
,
1468 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1469 strcmp(strval
, "none") == 0) {
1470 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1471 "keylocation must not be 'none' "
1472 "for encrypted datasets"));
1473 (void) zfs_error(hdl
, EZFS_BADPROP
,
1480 case ZFS_PROP_PBKDF2_ITERS
:
1481 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1483 "minimum pbkdf2 iterations is %u"),
1484 MIN_PBKDF2_ITERATIONS
);
1485 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1490 case ZFS_PROP_UTF8ONLY
:
1491 chosen_utf
= (int)intval
;
1494 case ZFS_PROP_NORMALIZE
:
1495 chosen_normal
= (int)intval
;
1503 * For changes to existing volumes, we have some additional
1504 * checks to enforce.
1506 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1507 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1508 ZFS_PROP_VOLBLOCKSIZE
);
1512 case ZFS_PROP_VOLSIZE
:
1513 if (intval
% blocksize
!= 0) {
1514 zfs_nicebytes(blocksize
, buf
,
1516 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1517 "'%s' must be a multiple of "
1518 "volume block size (%s)"),
1520 (void) zfs_error(hdl
, EZFS_BADPROP
,
1526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1527 "'%s' cannot be zero"),
1529 (void) zfs_error(hdl
, EZFS_BADPROP
,
1540 /* check encryption properties */
1542 int64_t crypt
= zfs_prop_get_int(zhp
,
1543 ZFS_PROP_ENCRYPTION
);
1546 case ZFS_PROP_COPIES
:
1547 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1549 "encrypted datasets cannot have "
1551 (void) zfs_error(hdl
, EZFS_BADPROP
,
1563 * If normalization was chosen, but no UTF8 choice was made,
1564 * enforce rejection of non-UTF8 names.
1566 * If normalization was chosen, but rejecting non-UTF8 names
1567 * was explicitly not chosen, it is an error.
1569 * If utf8only was turned off, but the parent has normalization,
1570 * turn off normalization.
1572 if (chosen_normal
> 0 && chosen_utf
< 0) {
1573 if (nvlist_add_uint64(ret
,
1574 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1575 (void) no_memory(hdl
);
1578 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1579 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1580 "'%s' must be set 'on' if normalization chosen"),
1581 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1582 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1584 } else if (chosen_normal
< 0 && chosen_utf
== 0) {
1585 if (nvlist_add_uint64(ret
,
1586 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), 0) != 0) {
1587 (void) no_memory(hdl
);
1599 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1601 uint64_t old_volsize
;
1602 uint64_t new_volsize
;
1603 uint64_t old_reservation
;
1604 uint64_t new_reservation
;
1605 zfs_prop_t resv_prop
;
1607 zpool_handle_t
*zph
= zpool_handle(zhp
);
1610 * If this is an existing volume, and someone is setting the volsize,
1611 * make sure that it matches the reservation, or add it if necessary.
1613 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1614 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1616 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1618 props
= fnvlist_alloc();
1619 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1620 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1622 if ((zvol_volsize_to_reservation(zph
, old_volsize
, props
) !=
1623 old_reservation
) || nvlist_exists(nvl
,
1624 zfs_prop_to_name(resv_prop
))) {
1625 fnvlist_free(props
);
1628 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1629 &new_volsize
) != 0) {
1630 fnvlist_free(props
);
1633 new_reservation
= zvol_volsize_to_reservation(zph
, new_volsize
, props
);
1634 fnvlist_free(props
);
1636 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1637 new_reservation
) != 0) {
1638 (void) no_memory(zhp
->zfs_hdl
);
1645 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1646 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1647 * Return codes must match zfs_add_synthetic_resv().
1650 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1657 if (!ZFS_IS_VOLUME(zhp
)) {
1661 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1665 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1669 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1670 /* No value being set, so it can't be "auto" */
1673 if (resvsize
!= UINT64_MAX
) {
1674 /* Being set to a value other than "auto" */
1678 props
= fnvlist_alloc();
1680 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1681 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1683 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1685 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1688 resvsize
= zvol_volsize_to_reservation(zpool_handle(zhp
), volsize
,
1690 fnvlist_free(props
);
1692 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1693 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1694 (void) no_memory(zhp
->zfs_hdl
);
1701 zfs_is_namespace_prop(zfs_prop_t prop
)
1705 case ZFS_PROP_ATIME
:
1706 case ZFS_PROP_RELATIME
:
1707 case ZFS_PROP_DEVICES
:
1709 case ZFS_PROP_SETUID
:
1710 case ZFS_PROP_READONLY
:
1711 case ZFS_PROP_XATTR
:
1712 case ZFS_PROP_NBMAND
:
1721 * Given a property name and value, set the property for the given dataset.
1724 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1728 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1729 nvlist_t
*nvl
= NULL
;
1731 (void) snprintf(errbuf
, sizeof (errbuf
),
1732 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1735 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1736 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1737 (void) no_memory(hdl
);
1741 ret
= zfs_prop_set_list(zhp
, nvl
);
1751 * Given an nvlist of property names and values, set the properties for the
1755 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1757 zfs_cmd_t zc
= {"\0"};
1759 prop_changelist_t
**cls
= NULL
;
1762 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1766 zfs_prop_t prop
= 0;
1769 (void) snprintf(errbuf
, sizeof (errbuf
),
1770 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1773 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1774 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1775 B_FALSE
, errbuf
)) == NULL
)
1779 * We have to check for any extra properties which need to be added
1780 * before computing the length of the nvlist.
1782 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1784 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1785 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1786 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1791 if (added_resv
!= 1 &&
1792 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1797 * Check how many properties we're setting and allocate an array to
1798 * store changelist pointers for postfix().
1800 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1802 elem
= nvlist_next_nvpair(nvl
, elem
))
1804 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1808 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1810 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1812 prop
= zfs_name_to_prop(nvpair_name(elem
));
1814 assert(cl_idx
< nvl_len
);
1816 * We don't want to unmount & remount the dataset when changing
1817 * its canmount property to 'on' or 'noauto'. We only use
1818 * the changelist logic to unmount when setting canmount=off.
1820 if (prop
!= ZFS_PROP_CANMOUNT
||
1821 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1822 zfs_is_mounted(zhp
, NULL
))) {
1823 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1824 if (cls
[cl_idx
] == NULL
)
1828 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1829 changelist_haszonedchild(cls
[cl_idx
])) {
1830 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1831 "child dataset with inherited mountpoint is used "
1832 "in a non-global zone"));
1833 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1837 if (cls
[cl_idx
] != NULL
&&
1838 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1843 assert(cl_idx
== nvl_len
);
1846 * Execute the corresponding ioctl() to set this list of properties.
1848 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1850 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1851 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1854 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1857 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1858 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1862 /* Get the list of unset properties back and report them. */
1863 nvlist_t
*errorprops
= NULL
;
1864 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1866 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1868 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1869 prop
= zfs_name_to_prop(nvpair_name(elem
));
1870 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1872 nvlist_free(errorprops
);
1874 if (added_resv
&& errno
== ENOSPC
) {
1875 /* clean up the volsize property we tried to set */
1876 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1880 zcmd_free_nvlists(&zc
);
1882 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1884 if (nvlist_add_uint64(nvl
,
1885 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1888 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1890 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1893 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1894 if (cls
[cl_idx
] != NULL
) {
1895 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1903 * Refresh the statistics so the new property
1904 * value is reflected.
1906 (void) get_stats(zhp
);
1909 * Remount the filesystem to propagate the change
1910 * if one of the options handled by the generic
1911 * Linux namespace layer has been modified.
1913 if (zfs_is_namespace_prop(prop
) &&
1914 zfs_is_mounted(zhp
, NULL
))
1915 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1921 zcmd_free_nvlists(&zc
);
1923 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1924 if (cls
[cl_idx
] != NULL
)
1925 changelist_free(cls
[cl_idx
]);
1933 * Given a property, inherit the value from the parent dataset, or if received
1934 * is TRUE, revert to the received value, if any.
1937 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1939 zfs_cmd_t zc
= {"\0"};
1941 prop_changelist_t
*cl
;
1942 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1946 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1947 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1949 zc
.zc_cookie
= received
;
1950 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1952 * For user properties, the amount of work we have to do is very
1953 * small, so just do it here.
1955 if (!zfs_prop_user(propname
)) {
1956 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1957 "invalid property"));
1958 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1961 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1962 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1964 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1965 return (zfs_standard_error(hdl
, errno
, errbuf
));
1967 (void) get_stats(zhp
);
1972 * Verify that this property is inheritable.
1974 if (zfs_prop_readonly(prop
))
1975 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1977 if (!zfs_prop_inheritable(prop
) && !received
)
1978 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1981 * Check to see if the value applies to this type
1983 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1984 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1987 * Normalize the name, to get rid of shorthand abbreviations.
1989 propname
= zfs_prop_to_name(prop
);
1990 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1991 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1993 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1994 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1995 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1996 "dataset is used in a non-global zone"));
1997 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
2001 * Determine datasets which will be affected by this change, if any.
2003 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2006 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2007 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2008 "child dataset with inherited mountpoint is used "
2009 "in a non-global zone"));
2010 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2014 if ((ret
= changelist_prefix(cl
)) != 0)
2017 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2018 return (zfs_standard_error(hdl
, errno
, errbuf
));
2021 if ((ret
= changelist_postfix(cl
)) != 0)
2025 * Refresh the statistics so the new property is reflected.
2027 (void) get_stats(zhp
);
2030 * Remount the filesystem to propagate the change
2031 * if one of the options handled by the generic
2032 * Linux namespace layer has been modified.
2034 if (zfs_is_namespace_prop(prop
) &&
2035 zfs_is_mounted(zhp
, NULL
))
2036 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2040 changelist_free(cl
);
2045 * True DSL properties are stored in an nvlist. The following two functions
2046 * extract them appropriately.
2049 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2055 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2056 zfs_prop_to_name(prop
), &nv
) == 0) {
2057 value
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
2058 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2060 verify(!zhp
->zfs_props_table
||
2061 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2062 value
= zfs_prop_default_numeric(prop
);
2070 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2076 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2077 zfs_prop_to_name(prop
), &nv
) == 0) {
2078 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2079 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2081 verify(!zhp
->zfs_props_table
||
2082 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2083 value
= zfs_prop_default_string(prop
);
2091 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2093 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2097 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2099 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2100 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2104 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2106 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2111 * Internal function for getting a numeric property. Both zfs_prop_get() and
2112 * zfs_prop_get_int() are built using this interface.
2114 * Certain properties can be overridden using 'mount -o'. In this case, scan
2115 * the contents of the /proc/self/mounts entry, searching for the
2116 * appropriate options. If they differ from the on-disk values, report the
2117 * current values and mark the source "temporary".
2120 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2121 char **source
, uint64_t *val
)
2123 zfs_cmd_t zc
= {"\0"};
2124 nvlist_t
*zplprops
= NULL
;
2126 char *mntopt_on
= NULL
;
2127 char *mntopt_off
= NULL
;
2128 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2133 * If the property is being fetched for a snapshot, check whether
2134 * the property is valid for the snapshot's head dataset type.
2136 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2137 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2138 *val
= zfs_prop_default_numeric(prop
);
2143 case ZFS_PROP_ATIME
:
2144 mntopt_on
= MNTOPT_ATIME
;
2145 mntopt_off
= MNTOPT_NOATIME
;
2148 case ZFS_PROP_RELATIME
:
2149 mntopt_on
= MNTOPT_RELATIME
;
2150 mntopt_off
= MNTOPT_NORELATIME
;
2153 case ZFS_PROP_DEVICES
:
2154 mntopt_on
= MNTOPT_DEVICES
;
2155 mntopt_off
= MNTOPT_NODEVICES
;
2159 mntopt_on
= MNTOPT_EXEC
;
2160 mntopt_off
= MNTOPT_NOEXEC
;
2163 case ZFS_PROP_READONLY
:
2164 mntopt_on
= MNTOPT_RO
;
2165 mntopt_off
= MNTOPT_RW
;
2168 case ZFS_PROP_SETUID
:
2169 mntopt_on
= MNTOPT_SETUID
;
2170 mntopt_off
= MNTOPT_NOSETUID
;
2173 case ZFS_PROP_XATTR
:
2174 mntopt_on
= MNTOPT_XATTR
;
2175 mntopt_off
= MNTOPT_NOXATTR
;
2178 case ZFS_PROP_NBMAND
:
2179 mntopt_on
= MNTOPT_NBMAND
;
2180 mntopt_off
= MNTOPT_NONBMAND
;
2188 * Because looking up the mount options is potentially expensive
2189 * (iterating over all of /proc/self/mounts), we defer its
2190 * calculation until we're looking up a property which requires
2193 if (!zhp
->zfs_mntcheck
&&
2194 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2195 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2196 struct mnttab entry
;
2198 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2199 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2201 if (zhp
->zfs_mntopts
== NULL
)
2205 zhp
->zfs_mntcheck
= B_TRUE
;
2208 if (zhp
->zfs_mntopts
== NULL
)
2209 mnt
.mnt_mntopts
= "";
2211 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2214 case ZFS_PROP_ATIME
:
2215 case ZFS_PROP_RELATIME
:
2216 case ZFS_PROP_DEVICES
:
2218 case ZFS_PROP_READONLY
:
2219 case ZFS_PROP_SETUID
:
2221 case ZFS_PROP_XATTR
:
2223 case ZFS_PROP_NBMAND
:
2224 *val
= getprop_uint64(zhp
, prop
, source
);
2229 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2232 *src
= ZPROP_SRC_TEMPORARY
;
2233 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2236 *src
= ZPROP_SRC_TEMPORARY
;
2240 case ZFS_PROP_CANMOUNT
:
2241 case ZFS_PROP_VOLSIZE
:
2242 case ZFS_PROP_QUOTA
:
2243 case ZFS_PROP_REFQUOTA
:
2244 case ZFS_PROP_RESERVATION
:
2245 case ZFS_PROP_REFRESERVATION
:
2246 case ZFS_PROP_FILESYSTEM_LIMIT
:
2247 case ZFS_PROP_SNAPSHOT_LIMIT
:
2248 case ZFS_PROP_FILESYSTEM_COUNT
:
2249 case ZFS_PROP_SNAPSHOT_COUNT
:
2250 *val
= getprop_uint64(zhp
, prop
, source
);
2252 if (*source
== NULL
) {
2253 /* not default, must be local */
2254 *source
= zhp
->zfs_name
;
2258 case ZFS_PROP_MOUNTED
:
2259 *val
= (zhp
->zfs_mntopts
!= NULL
);
2262 case ZFS_PROP_NUMCLONES
:
2263 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2266 case ZFS_PROP_VERSION
:
2267 case ZFS_PROP_NORMALIZE
:
2268 case ZFS_PROP_UTF8ONLY
:
2270 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2272 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2273 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2274 zcmd_free_nvlists(&zc
);
2275 if (prop
== ZFS_PROP_VERSION
&&
2276 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2277 *val
= zfs_prop_default_numeric(prop
);
2280 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2281 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2283 zcmd_free_nvlists(&zc
);
2286 nvlist_free(zplprops
);
2287 zcmd_free_nvlists(&zc
);
2290 case ZFS_PROP_INCONSISTENT
:
2291 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2294 case ZFS_PROP_REDACTED
:
2295 *val
= zhp
->zfs_dmustats
.dds_redacted
;
2299 switch (zfs_prop_get_type(prop
)) {
2300 case PROP_TYPE_NUMBER
:
2301 case PROP_TYPE_INDEX
:
2302 *val
= getprop_uint64(zhp
, prop
, source
);
2304 * If we tried to use a default value for a
2305 * readonly property, it means that it was not
2306 * present. Note this only applies to "truly"
2307 * readonly properties, not set-once properties
2308 * like volblocksize.
2310 if (zfs_prop_readonly(prop
) &&
2311 !zfs_prop_setonce(prop
) &&
2312 *source
!= NULL
&& (*source
)[0] == '\0') {
2318 case PROP_TYPE_STRING
:
2320 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2321 "cannot get non-numeric property"));
2322 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2323 dgettext(TEXT_DOMAIN
, "internal error")));
2331 * Calculate the source type, given the raw source string.
2334 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2335 char *statbuf
, size_t statlen
)
2337 if (statbuf
== NULL
||
2338 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2342 if (source
== NULL
) {
2343 *srctype
= ZPROP_SRC_NONE
;
2344 } else if (source
[0] == '\0') {
2345 *srctype
= ZPROP_SRC_DEFAULT
;
2346 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2347 *srctype
= ZPROP_SRC_RECEIVED
;
2349 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2350 *srctype
= ZPROP_SRC_LOCAL
;
2352 (void) strlcpy(statbuf
, source
, statlen
);
2353 *srctype
= ZPROP_SRC_INHERITED
;
2360 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2361 size_t proplen
, boolean_t literal
)
2366 if (zhp
->zfs_recvd_props
== NULL
)
2367 if (get_recvd_props_ioctl(zhp
) != 0)
2370 prop
= zfs_name_to_prop(propname
);
2372 if (prop
!= ZPROP_INVAL
) {
2374 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2376 zfs_set_recvd_props_mode(zhp
, &cookie
);
2377 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2378 NULL
, NULL
, 0, literal
);
2379 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2383 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2384 propname
, &propval
) != 0)
2386 recvdval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
2387 (void) strlcpy(propbuf
, recvdval
, proplen
);
2390 return (err
== 0 ? 0 : -1);
2394 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2399 value
= zfs_get_clones_nvl(zhp
);
2400 if (value
== NULL
|| nvlist_empty(value
))
2404 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2405 pair
= nvlist_next_nvpair(value
, pair
)) {
2406 if (propbuf
[0] != '\0')
2407 (void) strlcat(propbuf
, ",", proplen
);
2408 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2414 struct get_clones_arg
{
2418 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2422 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2424 struct get_clones_arg
*gca
= arg
;
2426 if (gca
->numclones
== 0) {
2431 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2432 NULL
, NULL
, 0, B_TRUE
) != 0)
2434 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2435 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2440 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2446 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2448 nvlist_t
*nv
, *value
;
2450 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2451 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2452 struct get_clones_arg gca
;
2455 * if this is a snapshot, then the kernel wasn't able
2456 * to get the clones. Do it by slowly iterating.
2458 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2460 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2462 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2467 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2469 gca
.origin
= zhp
->zfs_name
;
2471 if (gca
.numclones
!= 0) {
2473 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2476 /* get the pool name */
2477 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2478 (void) strsep(&cp
, "/@");
2479 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2480 ZFS_TYPE_FILESYSTEM
);
2487 (void) get_clones_cb(root
, &gca
);
2490 if (gca
.numclones
!= 0 ||
2491 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2492 nvlist_add_nvlist(zhp
->zfs_props
,
2493 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2500 nv
= fnvlist_lookup_nvlist(zhp
->zfs_props
,
2501 zfs_prop_to_name(ZFS_PROP_CLONES
));
2504 return (fnvlist_lookup_nvlist(nv
, ZPROP_VALUE
));
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_KEY_GUID
:
2902 case ZFS_PROP_IVSET_GUID
:
2903 case ZFS_PROP_CREATETXG
:
2904 case ZFS_PROP_OBJSETID
:
2905 case ZFS_PROP_PBKDF2_ITERS
:
2907 * These properties are stored as numbers, but they are
2908 * identifiers or counters.
2909 * We don't want them to be pretty printed, because pretty
2910 * printing truncates their values making them useless.
2912 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2914 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2915 zcp_check(zhp
, prop
, val
, NULL
);
2918 case ZFS_PROP_REFERENCED
:
2919 case ZFS_PROP_AVAILABLE
:
2921 case ZFS_PROP_USEDSNAP
:
2922 case ZFS_PROP_USEDDS
:
2923 case ZFS_PROP_USEDREFRESERV
:
2924 case ZFS_PROP_USEDCHILD
:
2925 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2928 (void) snprintf(propbuf
, proplen
, "%llu",
2931 zfs_nicebytes(val
, propbuf
, proplen
);
2933 zcp_check(zhp
, prop
, val
, NULL
);
2937 switch (zfs_prop_get_type(prop
)) {
2938 case PROP_TYPE_NUMBER
:
2939 if (get_numeric_property(zhp
, prop
, src
,
2940 &source
, &val
) != 0) {
2945 (void) snprintf(propbuf
, proplen
, "%llu",
2948 zfs_nicenum(val
, propbuf
, proplen
);
2950 zcp_check(zhp
, prop
, val
, NULL
);
2953 case PROP_TYPE_STRING
:
2954 str
= getprop_string(zhp
, prop
, &source
);
2958 (void) strlcpy(propbuf
, str
, proplen
);
2959 zcp_check(zhp
, prop
, 0, str
);
2962 case PROP_TYPE_INDEX
:
2963 if (get_numeric_property(zhp
, prop
, src
,
2964 &source
, &val
) != 0)
2966 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2969 (void) strlcpy(propbuf
, strval
, proplen
);
2970 zcp_check(zhp
, prop
, 0, strval
);
2978 get_source(zhp
, src
, source
, statbuf
, statlen
);
2984 * Utility function to get the given numeric property. Does no validation that
2985 * the given property is the appropriate type; should only be used with
2986 * hard-coded property types.
2989 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2994 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3000 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3004 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3005 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3009 * Similar to zfs_prop_get(), but returns the value as an integer.
3012 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3013 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3018 * Check to see if this property applies to our object
3020 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3021 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3022 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3023 zfs_prop_to_name(prop
)));
3027 *src
= ZPROP_SRC_NONE
;
3029 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3032 get_source(zhp
, src
, source
, statbuf
, statlen
);
3039 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3040 char **domainp
, idmap_rid_t
*ridp
)
3042 idmap_get_handle_t
*get_hdl
= NULL
;
3046 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3050 err
= idmap_get_sidbyuid(get_hdl
, id
,
3051 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3053 err
= idmap_get_sidbygid(get_hdl
, id
,
3054 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3056 if (err
== IDMAP_SUCCESS
&&
3057 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3058 status
== IDMAP_SUCCESS
)
3064 idmap_get_destroy(get_hdl
);
3067 #endif /* HAVE_IDMAP */
3070 * convert the propname into parameters needed by kernel
3071 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3072 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3073 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3074 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3075 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3076 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3079 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3080 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3082 zfs_userquota_prop_t type
;
3086 boolean_t isproject
;
3092 /* Figure out the property type ({user|group|project}{quota|space}) */
3093 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3094 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3095 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3098 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3102 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3103 type
== ZFS_PROP_USEROBJQUOTA
||
3104 type
== ZFS_PROP_USEROBJUSED
);
3105 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3106 type
== ZFS_PROP_GROUPOBJQUOTA
||
3107 type
== ZFS_PROP_GROUPOBJUSED
);
3108 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3109 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3110 type
== ZFS_PROP_PROJECTOBJUSED
);
3112 cp
= strchr(propname
, '@') + 1;
3114 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3115 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3118 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3119 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3122 } else if (!isproject
&& strchr(cp
, '@')) {
3125 * It's a SID name (eg "user@domain") that needs to be
3126 * turned into S-1-domainID-RID.
3128 directory_error_t e
;
3129 char *numericsid
= NULL
;
3132 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3135 e
= directory_sid_from_user_name(NULL
,
3138 e
= directory_sid_from_group_name(NULL
,
3142 directory_error_free(e
);
3145 if (numericsid
== NULL
)
3148 (void) strlcpy(domain
, cp
, domainlen
);
3149 cp
= strrchr(domain
, '-');
3154 *ridp
= strtoull(cp
, &end
, 10);
3157 if (errno
!= 0 || *end
!= '\0')
3162 #endif /* HAVE_IDMAP */
3164 /* It's a user/group/project ID (eg "12345"). */
3167 id
= strtoul(cp
, &end
, 10);
3170 if (id
> MAXUID
&& !isproject
) {
3172 /* It's an ephemeral ID. */
3176 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3177 &mapdomain
, &rid
) != 0)
3179 (void) strlcpy(domain
, mapdomain
, domainlen
);
3183 #endif /* HAVE_IDMAP */
3193 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3194 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3197 zfs_cmd_t zc
= {"\0"};
3199 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3201 err
= userquota_propname_decode(propname
,
3202 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3203 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3204 zc
.zc_objset_type
= *typep
;
3208 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3212 *propvalue
= zc
.zc_cookie
;
3217 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3218 uint64_t *propvalue
)
3220 zfs_userquota_prop_t type
;
3222 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3227 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3228 char *propbuf
, int proplen
, boolean_t literal
)
3232 zfs_userquota_prop_t type
;
3234 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3241 (void) snprintf(propbuf
, proplen
, "%llu",
3242 (u_longlong_t
)propvalue
);
3243 } else if (propvalue
== 0 &&
3244 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3245 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3246 type
== ZFS_PROP_PROJECTQUOTA
||
3247 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3248 (void) strlcpy(propbuf
, "none", proplen
);
3249 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3250 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3251 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3252 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3254 zfs_nicenum(propvalue
, propbuf
, proplen
);
3260 * propname must start with "written@" or "written#".
3263 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3264 uint64_t *propvalue
)
3267 zfs_cmd_t zc
= {"\0"};
3268 const char *snapname
;
3270 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3272 assert(zfs_prop_written(propname
));
3273 snapname
= propname
+ strlen("written@");
3274 if (strchr(snapname
, '@') != NULL
|| strchr(snapname
, '#') != NULL
) {
3275 /* full snapshot or bookmark name specified */
3276 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3278 /* snapname is the short name, append it to zhp's fsname */
3281 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3282 sizeof (zc
.zc_value
));
3283 cp
= strchr(zc
.zc_value
, '@');
3286 (void) strlcat(zc
.zc_value
, snapname
- 1, sizeof (zc
.zc_value
));
3289 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3293 *propvalue
= zc
.zc_cookie
;
3298 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3299 char *propbuf
, int proplen
, boolean_t literal
)
3304 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3310 (void) snprintf(propbuf
, proplen
, "%llu",
3311 (u_longlong_t
)propvalue
);
3313 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3320 * Returns the name of the given zfs handle.
3323 zfs_get_name(const zfs_handle_t
*zhp
)
3325 return (zhp
->zfs_name
);
3329 * Returns the name of the parent pool for the given zfs handle.
3332 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3334 return (zhp
->zpool_hdl
->zpool_name
);
3338 * Returns the type of the given zfs handle.
3341 zfs_get_type(const zfs_handle_t
*zhp
)
3343 return (zhp
->zfs_type
);
3347 * Returns the type of the given zfs handle,
3348 * or, if a snapshot, the type of the snapshotted dataset.
3351 zfs_get_underlying_type(const zfs_handle_t
*zhp
)
3353 return (zhp
->zfs_head_type
);
3357 * Is one dataset name a child dataset of another?
3359 * Needs to handle these cases:
3360 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3361 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3362 * Descendant? No. No. No. Yes.
3365 is_descendant(const char *ds1
, const char *ds2
)
3367 size_t d1len
= strlen(ds1
);
3369 /* ds2 can't be a descendant if it's smaller */
3370 if (strlen(ds2
) < d1len
)
3373 /* otherwise, compare strings and verify that there's a '/' char */
3374 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3378 * Given a complete name, return just the portion that refers to the parent.
3379 * Will return -1 if there is no parent (path is just the name of the
3383 parent_name(const char *path
, char *buf
, size_t buflen
)
3387 (void) strlcpy(buf
, path
, buflen
);
3389 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3397 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3399 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3403 * If accept_ancestor is false, then check to make sure that the given path has
3404 * a parent, and that it exists. If accept_ancestor is true, then find the
3405 * closest existing ancestor for the given path. In prefixlen return the
3406 * length of already existing prefix of the given path. We also fetch the
3407 * 'zoned' property, which is used to validate property settings when creating
3411 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3412 boolean_t accept_ancestor
, int *prefixlen
)
3414 zfs_cmd_t zc
= {"\0"};
3415 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3421 (void) snprintf(errbuf
, sizeof (errbuf
),
3422 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3424 /* get parent, and check to see if this is just a pool */
3425 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3426 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3427 "missing dataset name"));
3428 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3431 /* check to see if the pool exists */
3432 if ((slash
= strchr(parent
, '/')) == NULL
)
3433 slash
= parent
+ strlen(parent
);
3434 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3435 zc
.zc_name
[slash
- parent
] = '\0';
3436 if (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3438 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3439 "no such pool '%s'"), zc
.zc_name
);
3440 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3443 /* check to see if the parent dataset exists */
3444 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3445 if (errno
== ENOENT
&& accept_ancestor
) {
3447 * Go deeper to find an ancestor, give up on top level.
3449 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3450 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3451 "no such pool '%s'"), zc
.zc_name
);
3452 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3454 } else if (errno
== ENOENT
) {
3455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3456 "parent does not exist"));
3457 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3459 return (zfs_standard_error(hdl
, errno
, errbuf
));
3462 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3466 /* we are in a non-global zone, but parent is in the global zone */
3467 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3468 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3473 /* make sure parent is a filesystem */
3474 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3475 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3476 "parent is not a filesystem"));
3477 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3483 if (prefixlen
!= NULL
)
3484 *prefixlen
= strlen(parent
);
3489 * Finds whether the dataset of the given type(s) exists.
3492 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3496 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3500 * Try to get stats for the dataset, which will tell us if it exists.
3502 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3503 int ds_type
= zhp
->zfs_type
;
3506 if (types
& ds_type
)
3513 * Given a path to 'target', create all the ancestors between
3514 * the prefixlen portion of the path, and the target itself.
3515 * Fail if the initial prefixlen-ancestor does not already exist.
3518 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3524 /* make sure prefix exists */
3525 cp
= target
+ prefixlen
;
3527 assert(strchr(cp
, '/') == NULL
);
3528 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3531 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3539 * Attempt to create, mount, and share any ancestor filesystems,
3540 * up to the prefixlen-long one.
3542 for (cp
= target
+ prefixlen
+ 1;
3543 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3547 h
= make_dataset_handle(hdl
, target
);
3549 /* it already exists, nothing to do here */
3554 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3556 opname
= dgettext(TEXT_DOMAIN
, "create");
3560 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3562 opname
= dgettext(TEXT_DOMAIN
, "open");
3566 if (zfs_mount(h
, NULL
, 0) != 0) {
3567 opname
= dgettext(TEXT_DOMAIN
, "mount");
3571 if (zfs_share(h
) != 0) {
3572 opname
= dgettext(TEXT_DOMAIN
, "share");
3578 zfs_commit_all_shares();
3583 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3584 "failed to %s ancestor '%s'"), opname
, target
);
3589 * Creates non-existing ancestors of the given path.
3592 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3599 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3600 "cannot create '%s'"), path
);
3603 * Check that we are not passing the nesting limit
3604 * before we start creating any ancestors.
3606 if (dataset_nestcheck(path
) != 0) {
3607 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3608 "maximum name nesting depth exceeded"));
3609 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3612 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3615 if ((path_copy
= strdup(path
)) != NULL
) {
3616 rc
= create_parents(hdl
, path_copy
, prefix
);
3619 if (path_copy
== NULL
|| rc
!= 0)
3626 * Create a new filesystem or volume.
3629 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3634 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3636 enum lzc_dataset_type ost
;
3637 zpool_handle_t
*zpool_handle
;
3638 uint8_t *wkeydata
= NULL
;
3641 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3643 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3644 "cannot create '%s'"), path
);
3646 /* validate the path, taking care to note the extended error message */
3647 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3648 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3650 if (dataset_nestcheck(path
) != 0) {
3651 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3652 "maximum name nesting depth exceeded"));
3653 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3656 /* validate parents exist */
3657 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3661 * The failure modes when creating a dataset of a different type over
3662 * one that already exists is a little strange. In particular, if you
3663 * try to create a dataset on top of an existing dataset, the ioctl()
3664 * will return ENOENT, not EEXIST. To prevent this from happening, we
3665 * first try to see if the dataset exists.
3667 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3668 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3669 "dataset already exists"));
3670 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3673 if (type
== ZFS_TYPE_VOLUME
)
3674 ost
= LZC_DATSET_TYPE_ZVOL
;
3676 ost
= LZC_DATSET_TYPE_ZFS
;
3678 /* open zpool handle for prop validation */
3679 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3680 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3682 /* truncate pool_path at first slash */
3683 char *p
= strchr(pool_path
, '/');
3687 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3690 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3691 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3692 zpool_close(zpool_handle
);
3695 zpool_close(zpool_handle
);
3697 if (type
== ZFS_TYPE_VOLUME
) {
3699 * If we are creating a volume, the size and block size must
3700 * satisfy a few restraints. First, the blocksize must be a
3701 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3702 * volsize must be a multiple of the block size, and cannot be
3705 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3706 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3708 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3709 "missing volume size"));
3710 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3713 if ((ret
= nvlist_lookup_uint64(props
,
3714 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3715 &blocksize
)) != 0) {
3716 if (ret
== ENOENT
) {
3717 blocksize
= zfs_prop_default_numeric(
3718 ZFS_PROP_VOLBLOCKSIZE
);
3721 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3722 "missing volume block size"));
3723 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3729 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3730 "volume size cannot be zero"));
3731 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3734 if (size
% blocksize
!= 0) {
3736 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3737 "volume size must be a multiple of volume block "
3739 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3743 (void) parent_name(path
, parent
, sizeof (parent
));
3744 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3745 &wkeydata
, &wkeylen
) != 0) {
3747 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3750 /* create the dataset */
3751 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3753 if (wkeydata
!= NULL
)
3756 /* check for failure */
3760 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3761 "no such parent '%s'"), parent
);
3762 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3765 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3766 "pool must be upgraded to set this "
3767 "property or value"));
3768 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3771 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3772 "encryption root's key is not loaded "
3774 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3778 "invalid property value(s) specified"));
3779 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3783 * This platform can't address a volume this big.
3785 if (type
== ZFS_TYPE_VOLUME
)
3786 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3791 return (zfs_standard_error(hdl
, errno
, errbuf
));
3799 * Destroys the given dataset. The caller must make sure that the filesystem
3800 * isn't mounted, and that there are no active dependents. If the file system
3801 * does not exist this function does nothing.
3804 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3808 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3811 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3812 nvlist_t
*nv
= fnvlist_alloc();
3813 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3814 error
= lzc_destroy_bookmarks(nv
, NULL
);
3817 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3818 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3824 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3825 nvlist_t
*nv
= fnvlist_alloc();
3826 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3827 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3830 error
= lzc_destroy(zhp
->zfs_name
);
3833 if (error
!= 0 && error
!= ENOENT
) {
3834 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3835 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3839 remove_mountpoint(zhp
);
3844 struct destroydata
{
3846 const char *snapname
;
3850 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3852 struct destroydata
*dd
= arg
;
3853 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3856 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3857 dd
->snapname
) >= sizeof (name
))
3860 if (lzc_exists(name
))
3861 fnvlist_add_boolean(dd
->nvl
, name
);
3863 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3869 * Destroys all snapshots with the given name in zhp & descendants.
3872 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3875 struct destroydata dd
= { 0 };
3877 dd
.snapname
= snapname
;
3878 dd
.nvl
= fnvlist_alloc();
3879 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3881 if (nvlist_empty(dd
.nvl
)) {
3882 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3883 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3884 zhp
->zfs_name
, snapname
);
3886 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3888 fnvlist_free(dd
.nvl
);
3893 * Destroys all the snapshots named in the nvlist.
3896 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3898 nvlist_t
*errlist
= NULL
;
3901 int ret
= zfs_destroy_snaps_nvl_os(hdl
, snaps
);
3905 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3908 nvlist_free(errlist
);
3912 if (nvlist_empty(errlist
)) {
3914 (void) snprintf(errbuf
, sizeof (errbuf
),
3915 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3917 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3919 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3920 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3922 (void) snprintf(errbuf
, sizeof (errbuf
),
3923 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3926 switch (fnvpair_value_int32(pair
)) {
3929 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3930 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3933 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3938 nvlist_free(errlist
);
3943 * Clones the given dataset. The target must be of the same type as the source.
3946 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3948 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3951 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3954 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3956 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3957 "cannot create '%s'"), target
);
3959 /* validate the target/clone name */
3960 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3961 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3963 /* validate parents exist */
3964 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3967 (void) parent_name(target
, parent
, sizeof (parent
));
3972 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
3974 if (ZFS_IS_VOLUME(zhp
))
3975 type
= ZFS_TYPE_VOLUME
;
3976 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3977 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
3979 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
3985 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
3987 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3990 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3998 * The parent doesn't exist. We should have caught this
3999 * above, but there may a race condition that has since
4000 * destroyed the parent.
4002 * At this point, we don't know whether it's the source
4003 * that doesn't exist anymore, or whether the target
4004 * dataset doesn't exist.
4006 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4007 "no such parent '%s'"), parent
);
4008 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4011 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4012 "source and target pools differ"));
4013 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4017 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4026 * Promotes the given clone fs to be the clone parent.
4029 zfs_promote(zfs_handle_t
*zhp
)
4031 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4032 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4036 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4037 "cannot promote '%s'"), zhp
->zfs_name
);
4039 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4040 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4041 "snapshots can not be promoted"));
4042 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4045 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4046 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4047 "not a cloned filesystem"));
4048 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4051 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4052 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4054 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4060 * Promoting encrypted dataset outside its
4063 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4064 "cannot promote dataset outside its "
4065 "encryption root"));
4066 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4069 /* There is a conflicting snapshot name. */
4070 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4071 "conflicting snapshot '%s' from parent '%s'"),
4072 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4073 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4076 return (zfs_standard_error(hdl
, ret
, errbuf
));
4082 typedef struct snapdata
{
4084 const char *sd_snapname
;
4088 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4090 snapdata_t
*sd
= arg
;
4091 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4094 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4095 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4096 sd
->sd_snapname
) >= sizeof (name
))
4099 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4101 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4109 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4113 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4119 zpool_handle_t
*zpool_hdl
;
4120 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4122 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4123 "cannot create snapshots "));
4126 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4127 const char *snapname
= nvpair_name(elem
);
4129 /* validate the target name */
4130 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4132 (void) snprintf(errbuf
, sizeof (errbuf
),
4133 dgettext(TEXT_DOMAIN
,
4134 "cannot create snapshot '%s'"), snapname
);
4135 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4140 * get pool handle for prop validation. assumes all snaps are in the
4141 * same pool, as does lzc_snapshot (below).
4143 elem
= nvlist_next_nvpair(snaps
, NULL
);
4144 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4145 pool
[strcspn(pool
, "/@")] = '\0';
4146 zpool_hdl
= zpool_open(hdl
, pool
);
4147 if (zpool_hdl
== NULL
)
4150 if (props
!= NULL
&&
4151 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4152 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4153 zpool_close(zpool_hdl
);
4156 zpool_close(zpool_hdl
);
4158 ret
= lzc_snapshot(snaps
, props
, &errors
);
4161 boolean_t printed
= B_FALSE
;
4162 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4164 elem
= nvlist_next_nvpair(errors
, elem
)) {
4165 (void) snprintf(errbuf
, sizeof (errbuf
),
4166 dgettext(TEXT_DOMAIN
,
4167 "cannot create snapshot '%s'"), nvpair_name(elem
));
4168 (void) zfs_standard_error(hdl
,
4169 fnvpair_value_int32(elem
), errbuf
);
4175 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4176 "multiple snapshots of same "
4178 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4182 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4188 nvlist_free(errors
);
4193 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4197 snapdata_t sd
= { 0 };
4198 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4203 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4204 "cannot snapshot %s"), path
);
4206 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4207 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4209 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4210 cp
= strchr(fsname
, '@');
4212 sd
.sd_snapname
= cp
+ 1;
4214 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4215 ZFS_TYPE_VOLUME
)) == NULL
) {
4219 sd
.sd_nvl
= fnvlist_alloc();
4221 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4223 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4226 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4227 fnvlist_free(sd
.sd_nvl
);
4233 * Destroy any more recent snapshots. We invoke this callback on any dependents
4234 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4235 * is a dependent and we should just destroy it without checking the transaction
4238 typedef struct rollback_data
{
4239 const char *cb_target
; /* the snapshot */
4240 uint64_t cb_create
; /* creation time reference */
4246 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4248 rollback_data_t
*cbp
= data
;
4249 prop_changelist_t
*clp
;
4251 /* We must destroy this clone; first unmount it */
4252 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4253 cbp
->cb_force
? MS_FORCE
: 0);
4254 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4255 cbp
->cb_error
= B_TRUE
;
4259 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4260 cbp
->cb_error
= B_TRUE
;
4262 changelist_remove(clp
, zhp
->zfs_name
);
4263 (void) changelist_postfix(clp
);
4264 changelist_free(clp
);
4271 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4273 rollback_data_t
*cbp
= data
;
4275 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4276 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4277 rollback_destroy_dependent
, cbp
);
4279 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4287 * Given a dataset, rollback to a specific snapshot, discarding any
4288 * data changes since then and making it the active dataset.
4290 * Any snapshots and bookmarks more recent than the target are
4291 * destroyed, along with their dependents (i.e. clones).
4294 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4296 rollback_data_t cb
= { 0 };
4298 boolean_t restore_resv
= 0;
4299 uint64_t old_volsize
= 0, new_volsize
;
4300 zfs_prop_t resv_prop
= { 0 };
4301 uint64_t min_txg
= 0;
4303 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4304 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4307 * Destroy all recent snapshots and their dependents.
4309 cb
.cb_force
= force
;
4310 cb
.cb_target
= snap
->zfs_name
;
4311 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4313 if (cb
.cb_create
> 0)
4314 min_txg
= cb
.cb_create
;
4316 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
,
4319 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4325 * Now that we have verified that the snapshot is the latest,
4326 * rollback to the given snapshot.
4329 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4330 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4332 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4334 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4338 * Pass both the filesystem and the wanted snapshot names,
4339 * we would get an error back if the snapshot is destroyed or
4340 * a new snapshot is created before this request is processed.
4342 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4346 (void) snprintf(errbuf
, sizeof (errbuf
),
4347 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4351 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4352 "there is a snapshot or bookmark more recent "
4353 "than '%s'"), snap
->zfs_name
);
4354 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4357 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4358 "'%s' is not found among snapshots of '%s'"),
4359 snap
->zfs_name
, zhp
->zfs_name
);
4360 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4363 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4366 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4372 * For volumes, if the pre-rollback volsize matched the pre-
4373 * rollback reservation and the volsize has changed then set
4374 * the reservation property to the post-rollback volsize.
4375 * Make a new handle since the rollback closed the dataset.
4377 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4378 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4380 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4381 if (old_volsize
!= new_volsize
)
4382 err
= zfs_prop_set_int(zhp
, resv_prop
,
4391 * Renames the given dataset.
4394 zfs_rename(zfs_handle_t
*zhp
, const char *target
, renameflags_t flags
)
4397 zfs_cmd_t zc
= {"\0"};
4399 prop_changelist_t
*cl
= NULL
;
4400 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4401 char property
[ZFS_MAXPROPLEN
];
4402 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4405 /* if we have the same exact name, just return success */
4406 if (strcmp(zhp
->zfs_name
, target
) == 0)
4409 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4410 "cannot rename to '%s'"), target
);
4412 /* make sure source name is valid */
4413 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4414 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4417 * Make sure the target name is valid
4419 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4420 if ((strchr(target
, '@') == NULL
) ||
4423 * Snapshot target name is abbreviated,
4424 * reconstruct full dataset name
4426 (void) strlcpy(parent
, zhp
->zfs_name
,
4428 delim
= strchr(parent
, '@');
4429 if (strchr(target
, '@') == NULL
)
4433 (void) strlcat(parent
, target
, sizeof (parent
));
4437 * Make sure we're renaming within the same dataset.
4439 delim
= strchr(target
, '@');
4440 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4441 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4442 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4443 "snapshots must be part of same "
4445 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4450 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4451 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4453 if (flags
.recursive
) {
4454 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4455 "recursive rename must be a snapshot"));
4456 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4459 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4460 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4462 /* validate parents */
4463 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4466 /* make sure we're in the same pool */
4467 verify((delim
= strchr(target
, '/')) != NULL
);
4468 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4469 zhp
->zfs_name
[delim
- target
] != '/') {
4470 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4471 "datasets must be within same pool"));
4472 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4475 /* new name cannot be a child of the current dataset name */
4476 if (is_descendant(zhp
->zfs_name
, target
)) {
4477 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4478 "New dataset name cannot be a descendant of "
4479 "current dataset name"));
4480 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4484 (void) snprintf(errbuf
, sizeof (errbuf
),
4485 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4487 if (getzoneid() == GLOBAL_ZONEID
&&
4488 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4489 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4490 "dataset is used in a non-global zone"));
4491 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4495 * Avoid unmounting file systems with mountpoint property set to
4496 * 'legacy' or 'none' even if -u option is not given.
4498 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
4499 !flags
.recursive
&& !flags
.nounmount
&&
4500 zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, property
,
4501 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) == 0 &&
4502 (strcmp(property
, "legacy") == 0 ||
4503 strcmp(property
, "none") == 0)) {
4504 flags
.nounmount
= B_TRUE
;
4506 if (flags
.recursive
) {
4507 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4508 if (parentname
== NULL
) {
4512 delim
= strchr(parentname
, '@');
4514 zfs_handle_t
*zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
,
4522 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4523 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4524 flags
.nounmount
? CL_GATHER_DONT_UNMOUNT
:
4525 CL_GATHER_ITER_MOUNTED
,
4526 flags
.forceunmount
? MS_FORCE
: 0)) == NULL
)
4529 if (changelist_haszonedchild(cl
)) {
4530 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4531 "child dataset with inherited mountpoint is used "
4532 "in a non-global zone"));
4533 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4538 if ((ret
= changelist_prefix(cl
)) != 0)
4542 if (ZFS_IS_VOLUME(zhp
))
4543 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4545 zc
.zc_objset_type
= DMU_OST_ZFS
;
4547 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4548 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4550 zc
.zc_cookie
= !!flags
.recursive
;
4551 zc
.zc_cookie
|= (!!flags
.nounmount
) << 1;
4553 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4555 * if it was recursive, the one that actually failed will
4558 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4559 "cannot rename '%s'"), zc
.zc_name
);
4561 if (flags
.recursive
&& errno
== EEXIST
) {
4562 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4563 "a child dataset already has a snapshot "
4564 "with the new name"));
4565 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4566 } else if (errno
== EACCES
) {
4567 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4568 "cannot move encrypted child outside of "
4569 "its encryption root"));
4570 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4572 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4576 * On failure, we still want to remount any filesystems that
4577 * were previously mounted, so we don't alter the system state.
4580 (void) changelist_postfix(cl
);
4583 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4584 ret
= changelist_postfix(cl
);
4590 changelist_free(cl
);
4596 zfs_get_all_props(zfs_handle_t
*zhp
)
4598 return (zhp
->zfs_props
);
4602 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4604 if (zhp
->zfs_recvd_props
== NULL
)
4605 if (get_recvd_props_ioctl(zhp
) != 0)
4607 return (zhp
->zfs_recvd_props
);
4611 zfs_get_user_props(zfs_handle_t
*zhp
)
4613 return (zhp
->zfs_user_props
);
4617 * This function is used by 'zfs list' to determine the exact set of columns to
4618 * display, and their maximum widths. This does two main things:
4620 * - If this is a list of all properties, then expand the list to include
4621 * all native properties, and set a flag so that for each dataset we look
4622 * for new unique user properties and add them to the list.
4624 * - For non fixed-width properties, keep track of the maximum width seen
4625 * so that we can size the column appropriately. If the user has
4626 * requested received property values, we also need to compute the width
4627 * of the RECEIVED column.
4630 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4633 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4634 zprop_list_t
*entry
;
4635 zprop_list_t
**last
, **start
;
4636 nvlist_t
*userprops
, *propval
;
4639 char buf
[ZFS_MAXPROPLEN
];
4641 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4644 userprops
= zfs_get_user_props(zhp
);
4647 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4649 * Go through and add any user properties as necessary. We
4650 * start by incrementing our list pointer to the first
4651 * non-native property.
4654 while (*start
!= NULL
) {
4655 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4657 start
= &(*start
)->pl_next
;
4661 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4663 * See if we've already found this property in our list.
4665 for (last
= start
; *last
!= NULL
;
4666 last
= &(*last
)->pl_next
) {
4667 if (strcmp((*last
)->pl_user_prop
,
4668 nvpair_name(elem
)) == 0)
4672 if (*last
== NULL
) {
4673 if ((entry
= zfs_alloc(hdl
,
4674 sizeof (zprop_list_t
))) == NULL
||
4675 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4676 nvpair_name(elem
)))) == NULL
) {
4681 entry
->pl_prop
= ZPROP_INVAL
;
4682 entry
->pl_width
= strlen(nvpair_name(elem
));
4683 entry
->pl_all
= B_TRUE
;
4690 * Now go through and check the width of any non-fixed columns
4692 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4693 if (entry
->pl_fixed
&& !literal
)
4696 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4697 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4698 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4699 if (strlen(buf
) > entry
->pl_width
)
4700 entry
->pl_width
= strlen(buf
);
4702 if (received
&& zfs_prop_get_recvd(zhp
,
4703 zfs_prop_to_name(entry
->pl_prop
),
4704 buf
, sizeof (buf
), literal
) == 0)
4705 if (strlen(buf
) > entry
->pl_recvd_width
)
4706 entry
->pl_recvd_width
= strlen(buf
);
4708 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4710 strval
= fnvlist_lookup_string(propval
,
4712 if (strlen(strval
) > entry
->pl_width
)
4713 entry
->pl_width
= strlen(strval
);
4715 if (received
&& zfs_prop_get_recvd(zhp
,
4716 entry
->pl_user_prop
,
4717 buf
, sizeof (buf
), literal
) == 0)
4718 if (strlen(buf
) > entry
->pl_recvd_width
)
4719 entry
->pl_recvd_width
= strlen(buf
);
4727 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4733 * Keep a reference to the props-table against which we prune the
4736 zhp
->zfs_props_table
= props
;
4738 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4741 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4742 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4745 * User properties will result in ZPROP_INVAL, and since we
4746 * only know how to prune standard ZFS properties, we always
4747 * leave these in the list. This can also happen if we
4748 * encounter an unknown DSL property (when running older
4749 * software, for example).
4751 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4752 (void) nvlist_remove(zhp
->zfs_props
,
4753 nvpair_name(curr
), nvpair_type(curr
));
4759 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4760 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4762 zfs_cmd_t zc
= {"\0"};
4763 nvlist_t
*nvlist
= NULL
;
4766 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4767 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4768 zc
.zc_cookie
= (uint64_t)cmd
;
4770 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4771 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4772 (void) no_memory(hdl
);
4778 case ZFS_SMB_ACL_ADD
:
4779 case ZFS_SMB_ACL_REMOVE
:
4780 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4782 case ZFS_SMB_ACL_RENAME
:
4783 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4785 (void) no_memory(hdl
);
4788 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4790 (void) no_memory(hdl
);
4793 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4794 nvlist_free(nvlist
);
4798 case ZFS_SMB_ACL_PURGE
:
4803 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4804 nvlist_free(nvlist
);
4809 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4810 char *path
, char *resource
)
4812 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4817 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4818 char *path
, char *resource
)
4820 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4825 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4827 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4832 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4833 char *oldname
, char *newname
)
4835 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4840 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4841 zfs_userspace_cb_t func
, void *arg
)
4843 zfs_cmd_t zc
= {"\0"};
4844 zfs_useracct_t buf
[100];
4845 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4848 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4850 zc
.zc_objset_type
= type
;
4851 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4854 zfs_useracct_t
*zua
= buf
;
4856 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4857 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4858 if ((errno
== ENOTSUP
&&
4859 (type
== ZFS_PROP_USEROBJUSED
||
4860 type
== ZFS_PROP_GROUPOBJUSED
||
4861 type
== ZFS_PROP_USEROBJQUOTA
||
4862 type
== ZFS_PROP_GROUPOBJQUOTA
||
4863 type
== ZFS_PROP_PROJECTOBJUSED
||
4864 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4865 type
== ZFS_PROP_PROJECTUSED
||
4866 type
== ZFS_PROP_PROJECTQUOTA
)))
4869 return (zfs_standard_error_fmt(hdl
, errno
,
4870 dgettext(TEXT_DOMAIN
,
4871 "cannot get used/quota for %s"), zc
.zc_name
));
4873 if (zc
.zc_nvlist_dst_size
== 0)
4876 while (zc
.zc_nvlist_dst_size
> 0) {
4877 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4878 zua
->zu_space
)) != 0)
4881 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4890 const char *snapname
;
4892 boolean_t recursive
;
4897 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4899 struct holdarg
*ha
= arg
;
4900 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4903 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4904 ha
->snapname
) >= sizeof (name
))
4907 if (lzc_exists(name
))
4908 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4911 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4917 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4918 boolean_t recursive
, int cleanup_fd
)
4923 ha
.nvl
= fnvlist_alloc();
4924 ha
.snapname
= snapname
;
4926 ha
.recursive
= recursive
;
4927 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4929 if (nvlist_empty(ha
.nvl
)) {
4932 fnvlist_free(ha
.nvl
);
4934 (void) snprintf(errbuf
, sizeof (errbuf
),
4935 dgettext(TEXT_DOMAIN
,
4936 "cannot hold snapshot '%s@%s'"),
4937 zhp
->zfs_name
, snapname
);
4938 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4942 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4943 fnvlist_free(ha
.nvl
);
4949 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4953 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4958 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4961 /* There may be errors even in the success case. */
4962 fnvlist_free(errors
);
4966 if (nvlist_empty(errors
)) {
4967 /* no hold-specific errors */
4968 (void) snprintf(errbuf
, sizeof (errbuf
),
4969 dgettext(TEXT_DOMAIN
, "cannot hold"));
4972 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4973 "pool must be upgraded"));
4974 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4977 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4980 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4984 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4986 elem
= nvlist_next_nvpair(errors
, elem
)) {
4987 (void) snprintf(errbuf
, sizeof (errbuf
),
4988 dgettext(TEXT_DOMAIN
,
4989 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4990 switch (fnvpair_value_int32(elem
)) {
4993 * Temporary tags wind up having the ds object id
4994 * prepended. So even if we passed the length check
4995 * above, it's still possible for the tag to wind
4996 * up being slightly too long.
4998 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5001 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5004 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5007 (void) zfs_standard_error(hdl
,
5008 fnvpair_value_int32(elem
), errbuf
);
5012 fnvlist_free(errors
);
5017 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5019 struct holdarg
*ha
= arg
;
5020 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5022 nvlist_t
*existing_holds
;
5024 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5025 ha
->snapname
) >= sizeof (name
)) {
5030 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5032 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5035 nvlist_t
*torelease
= fnvlist_alloc();
5036 fnvlist_add_boolean(torelease
, ha
->tag
);
5037 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5038 fnvlist_free(torelease
);
5042 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5048 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5049 boolean_t recursive
)
5053 nvlist_t
*errors
= NULL
;
5055 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5058 ha
.nvl
= fnvlist_alloc();
5059 ha
.snapname
= snapname
;
5061 ha
.recursive
= recursive
;
5063 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5065 if (nvlist_empty(ha
.nvl
)) {
5066 fnvlist_free(ha
.nvl
);
5068 (void) snprintf(errbuf
, sizeof (errbuf
),
5069 dgettext(TEXT_DOMAIN
,
5070 "cannot release hold from snapshot '%s@%s'"),
5071 zhp
->zfs_name
, snapname
);
5073 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5075 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5080 ret
= lzc_release(ha
.nvl
, &errors
);
5081 fnvlist_free(ha
.nvl
);
5084 /* There may be errors even in the success case. */
5085 fnvlist_free(errors
);
5089 if (nvlist_empty(errors
)) {
5090 /* no hold-specific errors */
5091 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5095 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5096 "pool must be upgraded"));
5097 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5100 (void) zfs_standard_error(hdl
, errno
, errbuf
);
5104 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5106 elem
= nvlist_next_nvpair(errors
, elem
)) {
5107 (void) snprintf(errbuf
, sizeof (errbuf
),
5108 dgettext(TEXT_DOMAIN
,
5109 "cannot release hold from snapshot '%s'"),
5111 switch (fnvpair_value_int32(elem
)) {
5113 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5116 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5119 (void) zfs_standard_error(hdl
,
5120 fnvpair_value_int32(elem
), errbuf
);
5124 fnvlist_free(errors
);
5129 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5131 zfs_cmd_t zc
= {"\0"};
5132 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5138 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5139 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5143 nvbuf
= malloc(nvsz
);
5144 if (nvbuf
== NULL
) {
5145 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5149 zc
.zc_nvlist_dst_size
= nvsz
;
5150 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5152 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5154 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5155 (void) snprintf(errbuf
, sizeof (errbuf
),
5156 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5161 nvsz
= zc
.zc_nvlist_dst_size
;
5165 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5166 "pool must be upgraded"));
5167 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5170 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5173 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5176 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5181 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5183 err
= zfs_standard_error_fmt(hdl
, rc
, dgettext(
5184 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5195 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5197 zfs_cmd_t zc
= {"\0"};
5198 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5204 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5205 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5207 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5210 nvbuf
= malloc(nvsz
);
5212 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5215 zc
.zc_nvlist_src_size
= nvsz
;
5216 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5217 zc
.zc_perm_action
= un
;
5219 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5221 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5222 (void) snprintf(errbuf
, sizeof (errbuf
),
5223 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5227 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5228 "pool must be upgraded"));
5229 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5232 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5235 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5238 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5249 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5254 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5257 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5259 (void) snprintf(errbuf
, sizeof (errbuf
),
5260 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5264 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5265 "pool must be upgraded"));
5266 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5269 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5272 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5275 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5284 * The theory of raidz space accounting
5286 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5287 * will "reference" 128KB, even though it allocates more than that, to store the
5288 * parity information (and perhaps skip sectors). This concept of the
5289 * "referenced" (and other DMU space accounting) being lower than the allocated
5290 * space by a constant factor is called "raidz deflation."
5292 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5293 * block size. However, zvols typically have a much smaller block size (default
5294 * 8KB). These smaller blocks may require proportionally much more parity
5295 * information (and perhaps skip sectors). In this case, the change to the
5296 * "referenced" property may be much more than the logical block size.
5298 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5301 * +-------+-------+-------+-------+-------+
5302 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5303 * +-------+-------+-------+-------+-------+
5304 * | P0 | D0 | D8 | D16 | D24 |
5305 * | P1 | D1 | D9 | D17 | D25 |
5306 * | P2 | D2 | D10 | D18 | D26 |
5307 * | P3 | D3 | D11 | D19 | D27 |
5308 * | P4 | D4 | D12 | D20 | D28 |
5309 * | P5 | D5 | D13 | D21 | D29 |
5310 * | P6 | D6 | D14 | D22 | D30 |
5311 * | P7 | D7 | D15 | D23 | D31 |
5312 * +-------+-------+-------+-------+-------+
5314 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5315 * sectors. The dataset's referenced will increase by 128k and the pool's
5316 * allocated and free properties will be adjusted by 160k.
5318 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5319 * blank cells represent unallocated space.
5321 * +-------+-------+-------+-------+-------+
5322 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5323 * +-------+-------+-------+-------+-------+
5325 * +-------+-------+-------+-------+-------+
5327 * Above, notice that the 4k block required one sector for parity and another
5328 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5329 * and free properties will be adjusted by 8k. The dataset will not be charged
5330 * 8k. Rather, it will be charged a value that is scaled according to the
5331 * overhead of the 128k block on the same vdev. This 8k allocation will be
5332 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5333 * calculated in the 128k block example above.
5335 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5336 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5337 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5338 * multiple of of 4 sectors. When a block does not fill the required number of
5339 * sectors, skip blocks (sectors) are used.
5341 * An 8k block being written to a raidz vdev may be written as follows:
5343 * +-------+-------+-------+-------+-------+
5344 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5345 * +-------+-------+-------+-------+-------+
5346 * | P0 | D0 | D1 | S0 | |
5347 * +-------+-------+-------+-------+-------+
5349 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5350 * added. For this 8k block, the pool's allocated and free properties are
5351 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5352 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5353 * the 128k block example above.
5355 * The situation is slightly different for dRAID since the minimum allocation
5356 * size is the full group width. The same 8K block above would be written as
5357 * follows in a dRAID group:
5359 * +-------+-------+-------+-------+-------+
5360 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5361 * +-------+-------+-------+-------+-------+
5362 * | P0 | D0 | D1 | S0 | S1 |
5363 * +-------+-------+-------+-------+-------+
5365 * Compression may lead to a variety of block sizes being written for the same
5366 * volume or file. There is no clear way to reserve just the amount of space
5367 * that will be required, so the worst case (no compression) is assumed.
5368 * Note that metadata blocks will typically be compressed, so the reservation
5369 * size returned by zvol_volsize_to_reservation() will generally be slightly
5370 * larger than the maximum that the volume can reference.
5374 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5375 * amount of space (in bytes) that will be allocated for the specified block
5376 * size. Note that the "referenced" space accounted will be less than this, but
5377 * not necessarily equal to "blksize", due to RAIDZ deflation.
5380 vdev_raidz_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5383 uint64_t asize
, ndata
;
5385 ASSERT3U(ndisks
, >, nparity
);
5386 ndata
= ndisks
- nparity
;
5387 asize
= ((blksize
- 1) >> ashift
) + 1;
5388 asize
+= nparity
* ((asize
+ ndata
- 1) / ndata
);
5389 asize
= roundup(asize
, nparity
+ 1) << ashift
;
5395 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5396 * amount of space (in bytes) that will be allocated for the specified block
5400 vdev_draid_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5403 ASSERT3U(ndisks
, >, nparity
);
5404 uint64_t ndata
= ndisks
- nparity
;
5405 uint64_t rows
= ((blksize
- 1) / (ndata
<< ashift
)) + 1;
5406 uint64_t asize
= (rows
* ndisks
) << ashift
;
5412 * Determine how much space will be allocated if it lands on the most space-
5413 * inefficient top-level vdev. Returns the size in bytes required to store one
5414 * copy of the volume data. See theory comment above.
5417 volsize_from_vdevs(zpool_handle_t
*zhp
, uint64_t nblocks
, uint64_t blksize
)
5419 nvlist_t
*config
, *tree
, **vdevs
;
5423 config
= zpool_get_config(zhp
, NULL
);
5424 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
5425 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
5426 &vdevs
, &nvdevs
) != 0) {
5427 return (nblocks
* blksize
);
5430 for (int v
= 0; v
< nvdevs
; v
++) {
5432 uint64_t nparity
, ashift
, asize
, tsize
;
5435 if (nvlist_lookup_string(vdevs
[v
], ZPOOL_CONFIG_TYPE
,
5439 if (strcmp(type
, VDEV_TYPE_RAIDZ
) != 0 &&
5440 strcmp(type
, VDEV_TYPE_DRAID
) != 0)
5443 if (nvlist_lookup_uint64(vdevs
[v
],
5444 ZPOOL_CONFIG_NPARITY
, &nparity
) != 0)
5447 if (nvlist_lookup_uint64(vdevs
[v
],
5448 ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
5451 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
5455 if (nvlist_lookup_nvlist_array(vdevs
[v
],
5456 ZPOOL_CONFIG_CHILDREN
, &disks
, &ndisks
) != 0)
5459 /* allocation size for the "typical" 128k block */
5460 tsize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5461 SPA_OLD_MAXBLOCKSIZE
);
5463 /* allocation size for the blksize block */
5464 asize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5469 if (nvlist_lookup_uint64(vdevs
[v
],
5470 ZPOOL_CONFIG_DRAID_NDATA
, &ndata
) != 0)
5473 /* allocation size for the "typical" 128k block */
5474 tsize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5475 ashift
, SPA_OLD_MAXBLOCKSIZE
);
5477 /* allocation size for the blksize block */
5478 asize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5483 * Scale this size down as a ratio of 128k / tsize.
5484 * See theory statement above.
5486 volsize
= nblocks
* asize
* SPA_OLD_MAXBLOCKSIZE
/ tsize
;
5487 if (volsize
> ret
) {
5493 ret
= nblocks
* blksize
;
5500 * Convert the zvol's volume size to an appropriate reservation. See theory
5503 * Note: If this routine is updated, it is necessary to update the ZFS test
5504 * suite's shell version in reservation.shlib.
5507 zvol_volsize_to_reservation(zpool_handle_t
*zph
, uint64_t volsize
,
5511 uint64_t nblocks
, volblocksize
;
5515 if (nvlist_lookup_string(props
,
5516 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5517 ncopies
= atoi(strval
);
5520 if (nvlist_lookup_uint64(props
,
5521 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5522 &volblocksize
) != 0)
5523 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5525 nblocks
= volsize
/ volblocksize
;
5527 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5528 * this reason, only the data blocks are scaled based on vdev config.
5530 volsize
= volsize_from_vdevs(zph
, nblocks
, volblocksize
);
5532 /* start with metadnode L0-L6 */
5534 /* calculate number of indirects */
5535 while (nblocks
> 1) {
5536 nblocks
+= DNODES_PER_LEVEL
- 1;
5537 nblocks
/= DNODES_PER_LEVEL
;
5540 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5543 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5544 * compressed, but in practice they compress down to about
5547 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;
5553 * Wait for the given activity and return the status of the wait (whether or not
5554 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5555 * reported via the 'missing' parameter, rather than by printing an error
5556 * message. This is convenient when this function is called in a loop over a
5557 * long period of time (as it is, for example, by zfs's wait cmd). In that
5558 * scenario, a fs being exported or destroyed should be considered a normal
5559 * event, so we don't want to print an error when we find that the fs doesn't
5563 zfs_wait_status(zfs_handle_t
*zhp
, zfs_wait_activity_t activity
,
5564 boolean_t
*missing
, boolean_t
*waited
)
5566 int error
= lzc_wait_fs(zhp
->zfs_name
, activity
, waited
);
5567 *missing
= (error
== ENOENT
);
5572 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
5573 dgettext(TEXT_DOMAIN
, "error waiting in fs '%s'"),