4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2019 Joyent, Inc.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017-2018 RackTop Systems.
33 * Copyright (c) 2019 Datto Inc.
34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
35 * Copyright (c) 2021 Matt Fiddaman
48 #include <sys/mntent.h>
49 #include <sys/mount.h>
56 #include <directory.h>
57 #endif /* HAVE_IDMAP */
59 #include <sys/dnode.h>
62 #include <sys/dsl_crypt.h>
66 #include "zfs_namecheck.h"
68 #include "libzfs_impl.h"
69 #include "zfs_deleg.h"
71 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
72 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
75 * Given a single type (not a mask of types), return the type in a human
79 zfs_type_to_name(zfs_type_t type
)
82 case ZFS_TYPE_FILESYSTEM
:
83 return (dgettext(TEXT_DOMAIN
, "filesystem"));
84 case ZFS_TYPE_SNAPSHOT
:
85 return (dgettext(TEXT_DOMAIN
, "snapshot"));
87 return (dgettext(TEXT_DOMAIN
, "volume"));
89 return (dgettext(TEXT_DOMAIN
, "pool"));
90 case ZFS_TYPE_BOOKMARK
:
91 return (dgettext(TEXT_DOMAIN
, "bookmark"));
93 assert(!"unhandled zfs_type_t");
100 * Validate a ZFS path. This is used even before trying to open the dataset, to
101 * provide a more meaningful error message. We call zfs_error_aux() to
102 * explain exactly why the name was not valid.
105 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
111 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
114 "snapshot delimiter '@' is not expected here"));
118 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
121 "missing '@' delimiter in snapshot name"));
125 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
127 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
128 "bookmark delimiter '#' is not expected here"));
132 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
134 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
135 "missing '#' delimiter in bookmark name"));
139 if (modifying
&& strchr(path
, '%') != NULL
) {
141 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
142 "invalid character %c in name"), '%');
146 if (entity_namecheck(path
, &why
, &what
) != 0) {
149 case NAME_ERR_TOOLONG
:
150 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
151 "name is too long"));
154 case NAME_ERR_LEADING_SLASH
:
155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
156 "leading slash in name"));
159 case NAME_ERR_EMPTY_COMPONENT
:
160 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
161 "empty component or misplaced '@'"
162 " or '#' delimiter in name"));
165 case NAME_ERR_TRAILING_SLASH
:
166 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
167 "trailing slash in name"));
170 case NAME_ERR_INVALCHAR
:
172 dgettext(TEXT_DOMAIN
, "invalid character "
173 "'%c' in name"), what
);
176 case NAME_ERR_MULTIPLE_DELIMITERS
:
177 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
178 "multiple '@' and/or '#' delimiters in "
182 case NAME_ERR_NOLETTER
:
183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
184 "pool doesn't begin with a letter"));
187 case NAME_ERR_RESERVED
:
188 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
189 "name is reserved"));
192 case NAME_ERR_DISKLIKE
:
193 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
194 "reserved disk name"));
197 case NAME_ERR_SELF_REF
:
198 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
199 "self reference, '.' is found in name"));
202 case NAME_ERR_PARENT_REF
:
203 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
204 "parent reference, '..' is found in name"));
208 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
209 "(%d) not defined"), why
);
221 zfs_name_valid(const char *name
, zfs_type_t type
)
223 if (type
== ZFS_TYPE_POOL
)
224 return (zpool_name_valid(NULL
, B_FALSE
, name
));
225 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
229 * This function takes the raw DSL properties, and filters out the user-defined
230 * properties into a separate nvlist.
233 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
235 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
240 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
241 (void) no_memory(hdl
);
246 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
247 if (!zfs_prop_user(nvpair_name(elem
)))
250 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
251 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
253 (void) no_memory(hdl
);
261 static zpool_handle_t
*
262 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
264 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
267 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
268 if (hdl
->libzfs_pool_handles
!= NULL
)
269 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
270 hdl
->libzfs_pool_handles
= zph
;
275 static zpool_handle_t
*
276 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
278 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
279 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
281 while ((zph
!= NULL
) &&
282 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
283 zph
= zph
->zpool_next
;
288 * Returns a handle to the pool that contains the provided dataset.
289 * If a handle to that pool already exists then that handle is returned.
290 * Otherwise, a new handle is created and added to the list of handles.
292 static zpool_handle_t
*
293 zpool_handle(zfs_handle_t
*zhp
)
299 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
300 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
301 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
303 zph
= zpool_find_handle(zhp
, pool_name
, len
);
305 zph
= zpool_add_handle(zhp
, pool_name
);
312 zpool_free_handles(libzfs_handle_t
*hdl
)
314 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
316 while (zph
!= NULL
) {
317 next
= zph
->zpool_next
;
321 hdl
->libzfs_pool_handles
= NULL
;
325 * Utility function to gather stats (objset and zpl) for the given object.
328 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
330 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
332 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
334 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
335 if (errno
== ENOMEM
) {
336 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
347 * Utility function to get the received properties of the given object.
350 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
352 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
353 nvlist_t
*recvdprops
;
354 zfs_cmd_t zc
= {"\0"};
357 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
360 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
362 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
363 if (errno
== ENOMEM
) {
364 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
368 zcmd_free_nvlists(&zc
);
373 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
374 zcmd_free_nvlists(&zc
);
378 nvlist_free(zhp
->zfs_recvd_props
);
379 zhp
->zfs_recvd_props
= recvdprops
;
385 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
387 nvlist_t
*allprops
, *userprops
;
389 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
391 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
396 * XXX Why do we store the user props separately, in addition to
397 * storing them in zfs_props?
399 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
400 nvlist_free(allprops
);
404 nvlist_free(zhp
->zfs_props
);
405 nvlist_free(zhp
->zfs_user_props
);
407 zhp
->zfs_props
= allprops
;
408 zhp
->zfs_user_props
= userprops
;
414 get_stats(zfs_handle_t
*zhp
)
417 zfs_cmd_t zc
= {"\0"};
419 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
421 if (get_stats_ioctl(zhp
, &zc
) != 0)
423 else if (put_stats_zhdl(zhp
, &zc
) != 0)
425 zcmd_free_nvlists(&zc
);
430 * Refresh the properties currently stored in the handle.
433 zfs_refresh_properties(zfs_handle_t
*zhp
)
435 (void) get_stats(zhp
);
439 * Makes a handle from the given dataset name. Used by zfs_open() and
440 * zfs_iter_* to create child handles on the fly.
443 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
445 if (put_stats_zhdl(zhp
, zc
) != 0)
449 * We've managed to open the dataset and gather statistics. Determine
450 * the high-level type.
452 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
) {
453 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
454 } else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
) {
455 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
456 } else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
) {
459 } else if (zhp
->zfs_dmustats
.dds_inconsistent
) {
466 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
467 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
468 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
469 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
470 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
471 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
473 abort(); /* we should never see any other types */
475 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
482 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
484 zfs_cmd_t zc
= {"\0"};
486 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
492 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
493 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
497 if (get_stats_ioctl(zhp
, &zc
) == -1) {
498 zcmd_free_nvlists(&zc
);
502 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
506 zcmd_free_nvlists(&zc
);
511 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
513 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
519 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
520 if (make_dataset_handle_common(zhp
, zc
) == -1) {
528 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
530 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
535 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
536 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
537 zhp
->zfs_head_type
= pzhp
->zfs_type
;
538 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
539 zhp
->zpool_hdl
= zpool_handle(zhp
);
545 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
547 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
552 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
553 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
554 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
555 sizeof (zhp
->zfs_name
));
556 zhp
->zfs_type
= zhp_orig
->zfs_type
;
557 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
558 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
559 if (zhp_orig
->zfs_props
!= NULL
) {
560 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
561 (void) no_memory(zhp
->zfs_hdl
);
566 if (zhp_orig
->zfs_user_props
!= NULL
) {
567 if (nvlist_dup(zhp_orig
->zfs_user_props
,
568 &zhp
->zfs_user_props
, 0) != 0) {
569 (void) no_memory(zhp
->zfs_hdl
);
574 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
575 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
576 &zhp
->zfs_recvd_props
, 0)) {
577 (void) no_memory(zhp
->zfs_hdl
);
582 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
583 if (zhp_orig
->zfs_mntopts
!= NULL
) {
584 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
585 zhp_orig
->zfs_mntopts
);
587 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
592 zfs_bookmark_exists(const char *path
)
596 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
602 (void) strlcpy(fsname
, path
, sizeof (fsname
));
603 pound
= strchr(fsname
, '#');
608 bmark_name
= pound
+ 1;
609 props
= fnvlist_alloc();
610 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
617 rv
= nvlist_exists(bmarks
, bmark_name
);
623 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
624 nvlist_t
*bmark_props
)
626 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
631 /* Fill in the name. */
632 zhp
->zfs_hdl
= parent
->zfs_hdl
;
633 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
635 /* Set the property lists. */
636 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
642 zhp
->zfs_head_type
= parent
->zfs_head_type
;
643 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
645 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
646 nvlist_free(zhp
->zfs_props
);
654 struct zfs_open_bookmarks_cb_data
{
660 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
662 struct zfs_open_bookmarks_cb_data
*dp
= data
;
665 * Is it the one we are looking for?
667 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
669 * We found it. Save it and let the caller know we are done.
676 * Not found. Close the handle and ask for another one.
683 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
684 * argument is a mask of acceptable types. The function will print an
685 * appropriate error message and return NULL if it can't be opened.
688 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
694 (void) snprintf(errbuf
, sizeof (errbuf
),
695 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
698 * Validate the name before we even try to open it.
700 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
701 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
706 * Bookmarks needs to be handled separately.
708 bookp
= strchr(path
, '#');
711 * Try to get stats for the dataset, which will tell us if it
715 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
716 (void) zfs_standard_error(hdl
, errno
, errbuf
);
720 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
722 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
725 * We need to cut out '#' and everything after '#'
726 * to get the parent dataset name only.
728 assert(bookp
- path
< sizeof (dsname
));
729 (void) strncpy(dsname
, path
, bookp
- path
);
730 dsname
[bookp
- path
] = '\0';
733 * Create handle for the parent dataset.
736 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
737 (void) zfs_standard_error(hdl
, errno
, errbuf
);
742 * Iterate bookmarks to find the right one.
745 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
746 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
747 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
751 if (cb_data
.zhp
== NULL
) {
752 (void) zfs_standard_error(hdl
, errno
, errbuf
);
764 if (!(types
& zhp
->zfs_type
)) {
765 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
774 * Release a ZFS handle. Nothing to do but free the associated memory.
777 zfs_close(zfs_handle_t
*zhp
)
779 if (zhp
->zfs_mntopts
)
780 free(zhp
->zfs_mntopts
);
781 nvlist_free(zhp
->zfs_props
);
782 nvlist_free(zhp
->zfs_user_props
);
783 nvlist_free(zhp
->zfs_recvd_props
);
787 typedef struct mnttab_node
{
788 struct mnttab mtn_mt
;
793 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
795 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
796 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
799 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
801 return (TREE_ISIGN(rv
));
805 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
807 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
808 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
809 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
810 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
814 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
819 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
822 while (getmntent(mnttab
, &entry
) == 0) {
826 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
829 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
830 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
831 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
832 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
833 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
835 /* Exclude duplicate mounts */
836 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
837 free(mtn
->mtn_mt
.mnt_special
);
838 free(mtn
->mtn_mt
.mnt_mountp
);
839 free(mtn
->mtn_mt
.mnt_fstype
);
840 free(mtn
->mtn_mt
.mnt_mntopts
);
845 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
848 (void) fclose(mnttab
);
853 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
858 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
860 free(mtn
->mtn_mt
.mnt_special
);
861 free(mtn
->mtn_mt
.mnt_mountp
);
862 free(mtn
->mtn_mt
.mnt_fstype
);
863 free(mtn
->mtn_mt
.mnt_mntopts
);
866 avl_destroy(&hdl
->libzfs_mnttab_cache
);
867 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
871 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
873 hdl
->libzfs_mnttab_enable
= enable
;
877 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
878 struct mnttab
*entry
)
885 if (!hdl
->libzfs_mnttab_enable
) {
886 struct mnttab srch
= { 0 };
888 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
889 libzfs_mnttab_fini(hdl
);
891 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
894 srch
.mnt_special
= (char *)fsname
;
895 srch
.mnt_fstype
= MNTTYPE_ZFS
;
896 ret
= getmntany(mnttab
, entry
, &srch
) ? ENOENT
: 0;
897 (void) fclose(mnttab
);
901 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
902 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
905 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
906 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
911 find
.mtn_mt
.mnt_special
= (char *)fsname
;
912 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
914 *entry
= mtn
->mtn_mt
;
917 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
922 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
923 const char *mountp
, const char *mntopts
)
927 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
928 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
929 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
930 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
931 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
932 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
933 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
935 * Another thread may have already added this entry
936 * via libzfs_mnttab_update. If so we should skip it.
938 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
) {
939 free(mtn
->mtn_mt
.mnt_special
);
940 free(mtn
->mtn_mt
.mnt_mountp
);
941 free(mtn
->mtn_mt
.mnt_fstype
);
942 free(mtn
->mtn_mt
.mnt_mntopts
);
945 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
948 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
952 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
957 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
958 find
.mtn_mt
.mnt_special
= (char *)fsname
;
959 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
961 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
962 free(ret
->mtn_mt
.mnt_special
);
963 free(ret
->mtn_mt
.mnt_mountp
);
964 free(ret
->mtn_mt
.mnt_fstype
);
965 free(ret
->mtn_mt
.mnt_mntopts
);
968 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
972 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
974 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
976 if (zpool_handle
== NULL
)
979 *spa_version
= zpool_get_prop_int(zpool_handle
,
980 ZPOOL_PROP_VERSION
, NULL
);
985 * The choice of reservation property depends on the SPA version.
988 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
992 if (zfs_spa_version(zhp
, &spa_version
) < 0)
995 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
996 *resv_prop
= ZFS_PROP_REFRESERVATION
;
998 *resv_prop
= ZFS_PROP_RESERVATION
;
1004 * Given an nvlist of properties to set, validates that they are correct, and
1005 * parses any numeric properties (index, boolean, etc) if they are specified as
1009 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1010 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1011 boolean_t key_params_ok
, const char *errbuf
)
1018 int chosen_normal
= -1;
1019 int chosen_utf
= -1;
1021 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1022 (void) no_memory(hdl
);
1027 * Make sure this property is valid and applies to this type.
1031 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1032 const char *propname
= nvpair_name(elem
);
1034 prop
= zfs_name_to_prop(propname
);
1035 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
1037 * This is a user property: make sure it's a
1038 * string, and that it's less than ZAP_MAXNAMELEN.
1040 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1042 "'%s' must be a string"), propname
);
1043 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1047 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1048 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1049 "property name '%s' is too long"),
1051 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1055 (void) nvpair_value_string(elem
, &strval
);
1056 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1057 (void) no_memory(hdl
);
1064 * Currently, only user properties can be modified on
1067 if (type
== ZFS_TYPE_SNAPSHOT
) {
1068 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1069 "this property can not be modified for snapshots"));
1070 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1074 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1075 zfs_userquota_prop_t uqtype
;
1076 char *newpropname
= NULL
;
1082 if (userquota_propname_decode(propname
, zoned
,
1083 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1085 dgettext(TEXT_DOMAIN
,
1086 "'%s' has an invalid user/group name"),
1088 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1092 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1093 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1094 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1095 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1096 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1097 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1099 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1101 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1106 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1107 (void) nvpair_value_string(elem
, &strval
);
1108 if (strcmp(strval
, "none") == 0) {
1110 } else if (zfs_nicestrtonum(hdl
,
1111 strval
, &intval
) != 0) {
1112 (void) zfs_error(hdl
,
1113 EZFS_BADPROP
, errbuf
);
1116 } else if (nvpair_type(elem
) ==
1118 (void) nvpair_value_uint64(elem
, &intval
);
1120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1121 "use 'none' to disable "
1122 "{user|group|project}quota"));
1126 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1127 "'%s' must be a number"), propname
);
1128 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1133 * Encode the prop name as
1134 * userquota@<hex-rid>-domain, to make it easy
1135 * for the kernel to decode.
1137 rc
= asprintf(&newpropname
, "%s%llx-%s",
1138 zfs_userquota_prop_prefixes
[uqtype
],
1139 (longlong_t
)rid
, domain
);
1140 if (rc
== -1 || newpropname
== NULL
) {
1141 (void) no_memory(hdl
);
1148 if (nvlist_add_uint64_array(ret
, newpropname
,
1151 (void) no_memory(hdl
);
1156 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1158 "'%s' is readonly"),
1160 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1164 if (prop
== ZPROP_INVAL
) {
1165 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1166 "invalid property '%s'"), propname
);
1167 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1171 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1173 dgettext(TEXT_DOMAIN
, "'%s' does not "
1174 "apply to datasets of this type"), propname
);
1175 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1179 if (zfs_prop_readonly(prop
) &&
1180 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1181 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1183 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1185 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1189 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1190 &strval
, &intval
, errbuf
) != 0)
1194 * Perform some additional checks for specific properties.
1197 case ZFS_PROP_VERSION
:
1203 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1204 if (intval
< version
) {
1205 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1206 "Can not downgrade; already at version %u"),
1208 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1214 case ZFS_PROP_VOLBLOCKSIZE
:
1215 case ZFS_PROP_RECORDSIZE
:
1217 int maxbs
= SPA_MAXBLOCKSIZE
;
1220 if (zpool_hdl
!= NULL
) {
1221 maxbs
= zpool_get_prop_int(zpool_hdl
,
1222 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1225 * The value must be a power of two between
1226 * SPA_MINBLOCKSIZE and maxbs.
1228 if (intval
< SPA_MINBLOCKSIZE
||
1229 intval
> maxbs
|| !ISP2(intval
)) {
1230 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1231 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1232 "'%s' must be power of 2 from 512B "
1233 "to %s"), propname
, buf
);
1234 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1240 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1242 int maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1245 if (zpool_hdl
!= NULL
) {
1246 char state
[64] = "";
1248 maxbs
= zpool_get_prop_int(zpool_hdl
,
1249 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1252 * Issue a warning but do not fail so that
1253 * tests for settable properties succeed.
1255 if (zpool_prop_get_feature(zpool_hdl
,
1256 "feature@allocation_classes", state
,
1257 sizeof (state
)) != 0 ||
1258 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1259 (void) fprintf(stderr
, gettext(
1260 "%s: property requires a special "
1261 "device in the pool\n"), propname
);
1265 (intval
< SPA_MINBLOCKSIZE
||
1266 intval
> maxbs
|| !ISP2(intval
))) {
1267 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1268 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1269 "invalid '%s=%llu' property: must be zero "
1270 "or a power of 2 from 512B to %s"),
1271 propname
, (unsigned long long)intval
, buf
);
1272 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1278 case ZFS_PROP_MLSLABEL
:
1280 #ifdef HAVE_MLSLABEL
1282 * Verify the mlslabel string and convert to
1283 * internal hex label string.
1287 char *hex
= NULL
; /* internal label string */
1289 /* Default value is already OK. */
1290 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1293 /* Verify the label can be converted to binary form */
1294 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1295 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1296 L_NO_CORRECTION
, NULL
) == -1)) {
1300 /* Now translate to hex internal label string */
1301 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1307 m_label_free(new_sl
);
1309 /* If string is already in internal form, we're done. */
1310 if (strcmp(strval
, hex
) == 0) {
1315 /* Replace the label string with the internal form. */
1316 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1318 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1325 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1326 "invalid mlslabel '%s'"), strval
);
1327 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1328 m_label_free(new_sl
); /* OK if null */
1331 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1332 "mlslabels are unsupported"));
1333 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1335 #endif /* HAVE_MLSLABEL */
1338 case ZFS_PROP_MOUNTPOINT
:
1340 namecheck_err_t why
;
1342 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1343 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1346 if (mountpoint_namecheck(strval
, &why
)) {
1348 case NAME_ERR_LEADING_SLASH
:
1350 dgettext(TEXT_DOMAIN
,
1351 "'%s' must be an absolute path, "
1352 "'none', or 'legacy'"), propname
);
1354 case NAME_ERR_TOOLONG
:
1356 dgettext(TEXT_DOMAIN
,
1357 "component of '%s' is too long"),
1363 dgettext(TEXT_DOMAIN
,
1364 "(%d) not defined"),
1368 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1374 case ZFS_PROP_SHARESMB
:
1375 case ZFS_PROP_SHARENFS
:
1377 * For the mountpoint and sharenfs or sharesmb
1378 * properties, check if it can be set in a
1379 * global/non-global zone based on
1380 * the zoned property value:
1382 * global zone non-global zone
1383 * --------------------------------------------------
1384 * zoned=on mountpoint (no) mountpoint (yes)
1385 * sharenfs (no) sharenfs (no)
1386 * sharesmb (no) sharesmb (no)
1388 * zoned=off mountpoint (yes) N/A
1393 if (getzoneid() == GLOBAL_ZONEID
) {
1394 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1395 "'%s' cannot be set on "
1396 "dataset in a non-global zone"),
1398 (void) zfs_error(hdl
, EZFS_ZONED
,
1401 } else if (prop
== ZFS_PROP_SHARENFS
||
1402 prop
== ZFS_PROP_SHARESMB
) {
1403 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1404 "'%s' cannot be set in "
1405 "a non-global zone"), propname
);
1406 (void) zfs_error(hdl
, EZFS_ZONED
,
1410 } else if (getzoneid() != GLOBAL_ZONEID
) {
1412 * If zoned property is 'off', this must be in
1413 * a global zone. If not, something is wrong.
1415 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1416 "'%s' cannot be set while dataset "
1417 "'zoned' property is set"), propname
);
1418 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1423 * At this point, it is legitimate to set the
1424 * property. Now we want to make sure that the
1425 * property value is valid if it is sharenfs.
1427 if ((prop
== ZFS_PROP_SHARENFS
||
1428 prop
== ZFS_PROP_SHARESMB
) &&
1429 strcmp(strval
, "on") != 0 &&
1430 strcmp(strval
, "off") != 0) {
1431 zfs_share_proto_t proto
;
1433 if (prop
== ZFS_PROP_SHARESMB
)
1438 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1439 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1440 "'%s' cannot be set to invalid "
1441 "options"), propname
);
1442 (void) zfs_error(hdl
, EZFS_BADPROP
,
1450 case ZFS_PROP_KEYLOCATION
:
1451 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1452 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1453 "invalid keylocation"));
1454 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1460 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1462 if (crypt
== ZIO_CRYPT_OFF
&&
1463 strcmp(strval
, "none") != 0) {
1464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1465 "keylocation must be 'none' "
1466 "for unencrypted datasets"));
1467 (void) zfs_error(hdl
, EZFS_BADPROP
,
1470 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1471 strcmp(strval
, "none") == 0) {
1472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1473 "keylocation must not be 'none' "
1474 "for encrypted datasets"));
1475 (void) zfs_error(hdl
, EZFS_BADPROP
,
1482 case ZFS_PROP_PBKDF2_ITERS
:
1483 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1484 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1485 "minimum pbkdf2 iterations is %u"),
1486 MIN_PBKDF2_ITERATIONS
);
1487 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1492 case ZFS_PROP_UTF8ONLY
:
1493 chosen_utf
= (int)intval
;
1496 case ZFS_PROP_NORMALIZE
:
1497 chosen_normal
= (int)intval
;
1505 * For changes to existing volumes, we have some additional
1506 * checks to enforce.
1508 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1509 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1510 ZFS_PROP_VOLBLOCKSIZE
);
1514 case ZFS_PROP_VOLSIZE
:
1515 if (intval
% blocksize
!= 0) {
1516 zfs_nicebytes(blocksize
, buf
,
1518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1519 "'%s' must be a multiple of "
1520 "volume block size (%s)"),
1522 (void) zfs_error(hdl
, EZFS_BADPROP
,
1528 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1529 "'%s' cannot be zero"),
1531 (void) zfs_error(hdl
, EZFS_BADPROP
,
1542 /* check encryption properties */
1544 int64_t crypt
= zfs_prop_get_int(zhp
,
1545 ZFS_PROP_ENCRYPTION
);
1548 case ZFS_PROP_COPIES
:
1549 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1550 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1551 "encrypted datasets cannot have "
1553 (void) zfs_error(hdl
, EZFS_BADPROP
,
1565 * If normalization was chosen, but no UTF8 choice was made,
1566 * enforce rejection of non-UTF8 names.
1568 * If normalization was chosen, but rejecting non-UTF8 names
1569 * was explicitly not chosen, it is an error.
1571 * If utf8only was turned off, but the parent has normalization,
1572 * turn off normalization.
1574 if (chosen_normal
> 0 && chosen_utf
< 0) {
1575 if (nvlist_add_uint64(ret
,
1576 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1577 (void) no_memory(hdl
);
1580 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1582 "'%s' must be set 'on' if normalization chosen"),
1583 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1584 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1586 } else if (chosen_normal
< 0 && chosen_utf
== 0) {
1587 if (nvlist_add_uint64(ret
,
1588 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), 0) != 0) {
1589 (void) no_memory(hdl
);
1601 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1603 uint64_t old_volsize
;
1604 uint64_t new_volsize
;
1605 uint64_t old_reservation
;
1606 uint64_t new_reservation
;
1607 zfs_prop_t resv_prop
;
1609 zpool_handle_t
*zph
= zpool_handle(zhp
);
1612 * If this is an existing volume, and someone is setting the volsize,
1613 * make sure that it matches the reservation, or add it if necessary.
1615 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1616 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1618 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1620 props
= fnvlist_alloc();
1621 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1622 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1624 if ((zvol_volsize_to_reservation(zph
, old_volsize
, props
) !=
1625 old_reservation
) || nvlist_exists(nvl
,
1626 zfs_prop_to_name(resv_prop
))) {
1627 fnvlist_free(props
);
1630 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1631 &new_volsize
) != 0) {
1632 fnvlist_free(props
);
1635 new_reservation
= zvol_volsize_to_reservation(zph
, new_volsize
, props
);
1636 fnvlist_free(props
);
1638 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1639 new_reservation
) != 0) {
1640 (void) no_memory(zhp
->zfs_hdl
);
1647 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1648 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1649 * Return codes must match zfs_add_synthetic_resv().
1652 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1659 if (!ZFS_IS_VOLUME(zhp
)) {
1663 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1667 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1671 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1672 /* No value being set, so it can't be "auto" */
1675 if (resvsize
!= UINT64_MAX
) {
1676 /* Being set to a value other than "auto" */
1680 props
= fnvlist_alloc();
1682 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1683 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1685 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1687 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1690 resvsize
= zvol_volsize_to_reservation(zpool_handle(zhp
), volsize
,
1692 fnvlist_free(props
);
1694 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1695 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1696 (void) no_memory(zhp
->zfs_hdl
);
1703 zfs_is_namespace_prop(zfs_prop_t prop
)
1707 case ZFS_PROP_ATIME
:
1708 case ZFS_PROP_RELATIME
:
1709 case ZFS_PROP_DEVICES
:
1711 case ZFS_PROP_SETUID
:
1712 case ZFS_PROP_READONLY
:
1713 case ZFS_PROP_XATTR
:
1714 case ZFS_PROP_NBMAND
:
1723 * Given a property name and value, set the property for the given dataset.
1726 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1730 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1731 nvlist_t
*nvl
= NULL
;
1733 (void) snprintf(errbuf
, sizeof (errbuf
),
1734 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1737 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1738 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1739 (void) no_memory(hdl
);
1743 ret
= zfs_prop_set_list(zhp
, nvl
);
1753 * Given an nvlist of property names and values, set the properties for the
1757 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1759 zfs_cmd_t zc
= {"\0"};
1761 prop_changelist_t
**cls
= NULL
;
1764 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1768 zfs_prop_t prop
= 0;
1771 (void) snprintf(errbuf
, sizeof (errbuf
),
1772 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1775 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1776 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1777 B_FALSE
, errbuf
)) == NULL
)
1781 * We have to check for any extra properties which need to be added
1782 * before computing the length of the nvlist.
1784 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1786 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1787 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1788 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1793 if (added_resv
!= 1 &&
1794 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1799 * Check how many properties we're setting and allocate an array to
1800 * store changelist pointers for postfix().
1802 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1804 elem
= nvlist_next_nvpair(nvl
, elem
))
1806 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1810 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1812 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1814 prop
= zfs_name_to_prop(nvpair_name(elem
));
1816 assert(cl_idx
< nvl_len
);
1818 * We don't want to unmount & remount the dataset when changing
1819 * its canmount property to 'on' or 'noauto'. We only use
1820 * the changelist logic to unmount when setting canmount=off.
1822 if (prop
!= ZFS_PROP_CANMOUNT
||
1823 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1824 zfs_is_mounted(zhp
, NULL
))) {
1825 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1826 if (cls
[cl_idx
] == NULL
)
1830 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1831 changelist_haszonedchild(cls
[cl_idx
])) {
1832 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1833 "child dataset with inherited mountpoint is used "
1834 "in a non-global zone"));
1835 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1839 if (cls
[cl_idx
] != NULL
&&
1840 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1845 assert(cl_idx
== nvl_len
);
1848 * Execute the corresponding ioctl() to set this list of properties.
1850 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1852 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1853 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1856 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1859 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1860 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1864 /* Get the list of unset properties back and report them. */
1865 nvlist_t
*errorprops
= NULL
;
1866 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1868 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1870 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1871 prop
= zfs_name_to_prop(nvpair_name(elem
));
1872 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1874 nvlist_free(errorprops
);
1876 if (added_resv
&& errno
== ENOSPC
) {
1877 /* clean up the volsize property we tried to set */
1878 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1882 zcmd_free_nvlists(&zc
);
1884 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1886 if (nvlist_add_uint64(nvl
,
1887 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1890 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1892 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1895 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1896 if (cls
[cl_idx
] != NULL
) {
1897 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1905 * Refresh the statistics so the new property
1906 * value is reflected.
1908 (void) get_stats(zhp
);
1911 * Remount the filesystem to propagate the change
1912 * if one of the options handled by the generic
1913 * Linux namespace layer has been modified.
1915 if (zfs_is_namespace_prop(prop
) &&
1916 zfs_is_mounted(zhp
, NULL
))
1917 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1923 zcmd_free_nvlists(&zc
);
1925 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1926 if (cls
[cl_idx
] != NULL
)
1927 changelist_free(cls
[cl_idx
]);
1935 * Given a property, inherit the value from the parent dataset, or if received
1936 * is TRUE, revert to the received value, if any.
1939 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1941 zfs_cmd_t zc
= {"\0"};
1943 prop_changelist_t
*cl
;
1944 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1948 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1949 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1951 zc
.zc_cookie
= received
;
1952 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1954 * For user properties, the amount of work we have to do is very
1955 * small, so just do it here.
1957 if (!zfs_prop_user(propname
)) {
1958 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1959 "invalid property"));
1960 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1963 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1964 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1966 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1967 return (zfs_standard_error(hdl
, errno
, errbuf
));
1969 (void) get_stats(zhp
);
1974 * Verify that this property is inheritable.
1976 if (zfs_prop_readonly(prop
))
1977 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1979 if (!zfs_prop_inheritable(prop
) && !received
)
1980 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1983 * Check to see if the value applies to this type
1985 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1986 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1989 * Normalize the name, to get rid of shorthand abbreviations.
1991 propname
= zfs_prop_to_name(prop
);
1992 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1993 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1995 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1996 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1997 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1998 "dataset is used in a non-global zone"));
1999 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
2003 * Determine datasets which will be affected by this change, if any.
2005 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2008 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2009 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2010 "child dataset with inherited mountpoint is used "
2011 "in a non-global zone"));
2012 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2016 if ((ret
= changelist_prefix(cl
)) != 0)
2019 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2020 return (zfs_standard_error(hdl
, errno
, errbuf
));
2023 if ((ret
= changelist_postfix(cl
)) != 0)
2027 * Refresh the statistics so the new property is reflected.
2029 (void) get_stats(zhp
);
2032 * Remount the filesystem to propagate the change
2033 * if one of the options handled by the generic
2034 * Linux namespace layer has been modified.
2036 if (zfs_is_namespace_prop(prop
) &&
2037 zfs_is_mounted(zhp
, NULL
))
2038 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2042 changelist_free(cl
);
2047 * True DSL properties are stored in an nvlist. The following two functions
2048 * extract them appropriately.
2051 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2057 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2058 zfs_prop_to_name(prop
), &nv
) == 0) {
2059 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2060 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2062 verify(!zhp
->zfs_props_table
||
2063 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2064 value
= zfs_prop_default_numeric(prop
);
2072 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2078 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2079 zfs_prop_to_name(prop
), &nv
) == 0) {
2080 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2081 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2083 verify(!zhp
->zfs_props_table
||
2084 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2085 value
= zfs_prop_default_string(prop
);
2093 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2095 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2099 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2101 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2102 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2106 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2108 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2113 * Internal function for getting a numeric property. Both zfs_prop_get() and
2114 * zfs_prop_get_int() are built using this interface.
2116 * Certain properties can be overridden using 'mount -o'. In this case, scan
2117 * the contents of the /proc/self/mounts entry, searching for the
2118 * appropriate options. If they differ from the on-disk values, report the
2119 * current values and mark the source "temporary".
2122 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2123 char **source
, uint64_t *val
)
2125 zfs_cmd_t zc
= {"\0"};
2126 nvlist_t
*zplprops
= NULL
;
2128 char *mntopt_on
= NULL
;
2129 char *mntopt_off
= NULL
;
2130 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2135 * If the property is being fetched for a snapshot, check whether
2136 * the property is valid for the snapshot's head dataset type.
2138 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2139 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2140 *val
= zfs_prop_default_numeric(prop
);
2145 case ZFS_PROP_ATIME
:
2146 mntopt_on
= MNTOPT_ATIME
;
2147 mntopt_off
= MNTOPT_NOATIME
;
2150 case ZFS_PROP_RELATIME
:
2151 mntopt_on
= MNTOPT_RELATIME
;
2152 mntopt_off
= MNTOPT_NORELATIME
;
2155 case ZFS_PROP_DEVICES
:
2156 mntopt_on
= MNTOPT_DEVICES
;
2157 mntopt_off
= MNTOPT_NODEVICES
;
2161 mntopt_on
= MNTOPT_EXEC
;
2162 mntopt_off
= MNTOPT_NOEXEC
;
2165 case ZFS_PROP_READONLY
:
2166 mntopt_on
= MNTOPT_RO
;
2167 mntopt_off
= MNTOPT_RW
;
2170 case ZFS_PROP_SETUID
:
2171 mntopt_on
= MNTOPT_SETUID
;
2172 mntopt_off
= MNTOPT_NOSETUID
;
2175 case ZFS_PROP_XATTR
:
2176 mntopt_on
= MNTOPT_XATTR
;
2177 mntopt_off
= MNTOPT_NOXATTR
;
2180 case ZFS_PROP_NBMAND
:
2181 mntopt_on
= MNTOPT_NBMAND
;
2182 mntopt_off
= MNTOPT_NONBMAND
;
2190 * Because looking up the mount options is potentially expensive
2191 * (iterating over all of /proc/self/mounts), we defer its
2192 * calculation until we're looking up a property which requires
2195 if (!zhp
->zfs_mntcheck
&&
2196 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2197 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2198 struct mnttab entry
;
2200 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2201 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2203 if (zhp
->zfs_mntopts
== NULL
)
2207 zhp
->zfs_mntcheck
= B_TRUE
;
2210 if (zhp
->zfs_mntopts
== NULL
)
2211 mnt
.mnt_mntopts
= "";
2213 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2216 case ZFS_PROP_ATIME
:
2217 case ZFS_PROP_RELATIME
:
2218 case ZFS_PROP_DEVICES
:
2220 case ZFS_PROP_READONLY
:
2221 case ZFS_PROP_SETUID
:
2223 case ZFS_PROP_XATTR
:
2225 case ZFS_PROP_NBMAND
:
2226 *val
= getprop_uint64(zhp
, prop
, source
);
2231 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2234 *src
= ZPROP_SRC_TEMPORARY
;
2235 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2238 *src
= ZPROP_SRC_TEMPORARY
;
2242 case ZFS_PROP_CANMOUNT
:
2243 case ZFS_PROP_VOLSIZE
:
2244 case ZFS_PROP_QUOTA
:
2245 case ZFS_PROP_REFQUOTA
:
2246 case ZFS_PROP_RESERVATION
:
2247 case ZFS_PROP_REFRESERVATION
:
2248 case ZFS_PROP_FILESYSTEM_LIMIT
:
2249 case ZFS_PROP_SNAPSHOT_LIMIT
:
2250 case ZFS_PROP_FILESYSTEM_COUNT
:
2251 case ZFS_PROP_SNAPSHOT_COUNT
:
2252 *val
= getprop_uint64(zhp
, prop
, source
);
2254 if (*source
== NULL
) {
2255 /* not default, must be local */
2256 *source
= zhp
->zfs_name
;
2260 case ZFS_PROP_MOUNTED
:
2261 *val
= (zhp
->zfs_mntopts
!= NULL
);
2264 case ZFS_PROP_NUMCLONES
:
2265 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2268 case ZFS_PROP_VERSION
:
2269 case ZFS_PROP_NORMALIZE
:
2270 case ZFS_PROP_UTF8ONLY
:
2272 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2274 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2275 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2276 zcmd_free_nvlists(&zc
);
2277 if (prop
== ZFS_PROP_VERSION
&&
2278 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2279 *val
= zfs_prop_default_numeric(prop
);
2282 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2283 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2285 zcmd_free_nvlists(&zc
);
2288 nvlist_free(zplprops
);
2289 zcmd_free_nvlists(&zc
);
2292 case ZFS_PROP_INCONSISTENT
:
2293 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2296 case ZFS_PROP_REDACTED
:
2297 *val
= zhp
->zfs_dmustats
.dds_redacted
;
2301 switch (zfs_prop_get_type(prop
)) {
2302 case PROP_TYPE_NUMBER
:
2303 case PROP_TYPE_INDEX
:
2304 *val
= getprop_uint64(zhp
, prop
, source
);
2306 * If we tried to use a default value for a
2307 * readonly property, it means that it was not
2308 * present. Note this only applies to "truly"
2309 * readonly properties, not set-once properties
2310 * like volblocksize.
2312 if (zfs_prop_readonly(prop
) &&
2313 !zfs_prop_setonce(prop
) &&
2314 *source
!= NULL
&& (*source
)[0] == '\0') {
2320 case PROP_TYPE_STRING
:
2322 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2323 "cannot get non-numeric property"));
2324 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2325 dgettext(TEXT_DOMAIN
, "internal error")));
2333 * Calculate the source type, given the raw source string.
2336 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2337 char *statbuf
, size_t statlen
)
2339 if (statbuf
== NULL
||
2340 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2344 if (source
== NULL
) {
2345 *srctype
= ZPROP_SRC_NONE
;
2346 } else if (source
[0] == '\0') {
2347 *srctype
= ZPROP_SRC_DEFAULT
;
2348 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2349 *srctype
= ZPROP_SRC_RECEIVED
;
2351 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2352 *srctype
= ZPROP_SRC_LOCAL
;
2354 (void) strlcpy(statbuf
, source
, statlen
);
2355 *srctype
= ZPROP_SRC_INHERITED
;
2362 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2363 size_t proplen
, boolean_t literal
)
2368 if (zhp
->zfs_recvd_props
== NULL
)
2369 if (get_recvd_props_ioctl(zhp
) != 0)
2372 prop
= zfs_name_to_prop(propname
);
2374 if (prop
!= ZPROP_INVAL
) {
2376 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2378 zfs_set_recvd_props_mode(zhp
, &cookie
);
2379 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2380 NULL
, NULL
, 0, literal
);
2381 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2385 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2386 propname
, &propval
) != 0)
2388 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2390 (void) strlcpy(propbuf
, recvdval
, proplen
);
2393 return (err
== 0 ? 0 : -1);
2397 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2402 value
= zfs_get_clones_nvl(zhp
);
2403 if (value
== NULL
|| nvlist_empty(value
))
2407 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2408 pair
= nvlist_next_nvpair(value
, pair
)) {
2409 if (propbuf
[0] != '\0')
2410 (void) strlcat(propbuf
, ",", proplen
);
2411 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2417 struct get_clones_arg
{
2421 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2425 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2427 struct get_clones_arg
*gca
= arg
;
2429 if (gca
->numclones
== 0) {
2434 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2435 NULL
, NULL
, 0, B_TRUE
) != 0)
2437 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2438 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2443 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2449 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2451 nvlist_t
*nv
, *value
;
2453 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2454 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2455 struct get_clones_arg gca
;
2458 * if this is a snapshot, then the kernel wasn't able
2459 * to get the clones. Do it by slowly iterating.
2461 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2463 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2465 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2470 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2472 gca
.origin
= zhp
->zfs_name
;
2474 if (gca
.numclones
!= 0) {
2476 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2479 /* get the pool name */
2480 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2481 (void) strsep(&cp
, "/@");
2482 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2483 ZFS_TYPE_FILESYSTEM
);
2490 (void) get_clones_cb(root
, &gca
);
2493 if (gca
.numclones
!= 0 ||
2494 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2495 nvlist_add_nvlist(zhp
->zfs_props
,
2496 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2503 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2504 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2507 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2513 get_rsnaps_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2519 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2520 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
), &value
) != 0)
2522 if (nvlist_lookup_uint64_array(value
, ZPROP_VALUE
, &snaps
,
2526 /* There's no redaction snapshots; pass a special value back */
2527 (void) snprintf(propbuf
, proplen
, "none");
2531 for (int i
= 0; i
< nsnaps
; i
++) {
2533 if (propbuf
[0] != '\0')
2534 (void) strlcat(propbuf
, ",", proplen
);
2535 (void) snprintf(buf
, sizeof (buf
), "%llu",
2536 (u_longlong_t
)snaps
[i
]);
2537 (void) strlcat(propbuf
, buf
, proplen
);
2544 * Accepts a property and value and checks that the value
2545 * matches the one found by the channel program. If they are
2546 * not equal, print both of them.
2549 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2552 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2555 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2556 const char *prop_name
= zfs_prop_to_name(prop
);
2557 const char *program
=
2559 "ds = args['dataset']\n"
2560 "prop = args['property']\n"
2561 "value, setpoint = zfs.get_prop(ds, prop)\n"
2562 "return {value=value, setpoint=setpoint}\n";
2565 nvlist_t
*argnvl
= fnvlist_alloc();
2567 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2568 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2570 error
= lzc_channel_program_nosync(poolname
, program
,
2571 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2574 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2575 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2577 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2579 (void) fprintf(stderr
, "%s: zcp check error: "
2580 "%u\n", prop_name
, error
);
2583 if (ans
!= intval
) {
2584 (void) fprintf(stderr
, "%s: zfs found %llu, "
2585 "but zcp found %llu\n", prop_name
,
2586 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2590 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2592 (void) fprintf(stderr
, "%s: zcp check error: "
2593 "%u\n", prop_name
, error
);
2596 if (strcmp(strval
, str_ans
) != 0) {
2597 (void) fprintf(stderr
,
2598 "%s: zfs found '%s', but zcp found '%s'\n",
2599 prop_name
, strval
, str_ans
);
2603 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2604 "error: %u\n", prop_name
, error
);
2606 nvlist_free(argnvl
);
2607 nvlist_free(outnvl
);
2611 * Retrieve a property from the given object. If 'literal' is specified, then
2612 * numbers are left as exact values. Otherwise, numbers are converted to a
2613 * human-readable form.
2615 * Returns 0 on success, or -1 on error.
2618 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2619 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2621 char *source
= NULL
;
2625 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2628 * Check to see if this property applies to our object
2630 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2633 if (received
&& zfs_prop_readonly(prop
))
2637 *src
= ZPROP_SRC_NONE
;
2640 case ZFS_PROP_CREATION
:
2642 * 'creation' is a time_t stored in the statistics. We convert
2643 * this into a string unless 'literal' is specified.
2646 val
= getprop_uint64(zhp
, prop
, &source
);
2647 time_t time
= (time_t)val
;
2651 localtime_r(&time
, &t
) == NULL
||
2652 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2654 (void) snprintf(propbuf
, proplen
, "%llu",
2657 zcp_check(zhp
, prop
, val
, NULL
);
2660 case ZFS_PROP_MOUNTPOINT
:
2662 * Getting the precise mountpoint can be tricky.
2664 * - for 'none' or 'legacy', return those values.
2665 * - for inherited mountpoints, we want to take everything
2666 * after our ancestor and append it to the inherited value.
2668 * If the pool has an alternate root, we want to prepend that
2669 * root to any values we return.
2672 str
= getprop_string(zhp
, prop
, &source
);
2674 if (str
[0] == '/') {
2675 char buf
[MAXPATHLEN
];
2677 const char *relpath
;
2680 * If we inherit the mountpoint, even from a dataset
2681 * with a received value, the source will be the path of
2682 * the dataset we inherit from. If source is
2683 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2686 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2689 relpath
= zhp
->zfs_name
+ strlen(source
);
2690 if (relpath
[0] == '/')
2694 if ((zpool_get_prop(zhp
->zpool_hdl
,
2695 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2696 B_FALSE
)) || (strcmp(root
, "-") == 0))
2699 * Special case an alternate root of '/'. This will
2700 * avoid having multiple leading slashes in the
2703 if (strcmp(root
, "/") == 0)
2707 * If the mountpoint is '/' then skip over this
2708 * if we are obtaining either an alternate root or
2709 * an inherited mountpoint.
2711 if (str
[1] == '\0' && (root
[0] != '\0' ||
2712 relpath
[0] != '\0'))
2715 if (relpath
[0] == '\0')
2716 (void) snprintf(propbuf
, proplen
, "%s%s",
2719 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2720 root
, str
, relpath
[0] == '@' ? "" : "/",
2723 /* 'legacy' or 'none' */
2724 (void) strlcpy(propbuf
, str
, proplen
);
2726 zcp_check(zhp
, prop
, 0, propbuf
);
2729 case ZFS_PROP_ORIGIN
:
2730 str
= getprop_string(zhp
, prop
, &source
);
2733 (void) strlcpy(propbuf
, str
, proplen
);
2734 zcp_check(zhp
, prop
, 0, str
);
2737 case ZFS_PROP_REDACT_SNAPS
:
2738 if (get_rsnaps_string(zhp
, propbuf
, proplen
) != 0)
2742 case ZFS_PROP_CLONES
:
2743 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2747 case ZFS_PROP_QUOTA
:
2748 case ZFS_PROP_REFQUOTA
:
2749 case ZFS_PROP_RESERVATION
:
2750 case ZFS_PROP_REFRESERVATION
:
2752 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2755 * If quota or reservation is 0, we translate this into 'none'
2756 * (unless literal is set), and indicate that it's the default
2757 * value. Otherwise, we print the number nicely and indicate
2758 * that its set locally.
2762 (void) strlcpy(propbuf
, "0", proplen
);
2764 (void) strlcpy(propbuf
, "none", proplen
);
2767 (void) snprintf(propbuf
, proplen
, "%llu",
2770 zfs_nicebytes(val
, propbuf
, proplen
);
2772 zcp_check(zhp
, prop
, val
, NULL
);
2775 case ZFS_PROP_FILESYSTEM_LIMIT
:
2776 case ZFS_PROP_SNAPSHOT_LIMIT
:
2777 case ZFS_PROP_FILESYSTEM_COUNT
:
2778 case ZFS_PROP_SNAPSHOT_COUNT
:
2780 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2784 * If limit is UINT64_MAX, we translate this into 'none', and
2785 * indicate that it's the default value. Otherwise, we print
2786 * the number nicely and indicate that it's set locally.
2788 if (val
== UINT64_MAX
) {
2789 (void) strlcpy(propbuf
, "none", proplen
);
2790 } else if (literal
) {
2791 (void) snprintf(propbuf
, proplen
, "%llu",
2794 zfs_nicenum(val
, propbuf
, proplen
);
2797 zcp_check(zhp
, prop
, val
, NULL
);
2800 case ZFS_PROP_REFRATIO
:
2801 case ZFS_PROP_COMPRESSRATIO
:
2802 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2805 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2806 (u_longlong_t
)(val
/ 100),
2807 (u_longlong_t
)(val
% 100));
2809 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2810 (u_longlong_t
)(val
/ 100),
2811 (u_longlong_t
)(val
% 100));
2812 zcp_check(zhp
, prop
, val
, NULL
);
2816 switch (zhp
->zfs_type
) {
2817 case ZFS_TYPE_FILESYSTEM
:
2820 case ZFS_TYPE_VOLUME
:
2823 case ZFS_TYPE_SNAPSHOT
:
2826 case ZFS_TYPE_BOOKMARK
:
2832 (void) snprintf(propbuf
, proplen
, "%s", str
);
2833 zcp_check(zhp
, prop
, 0, propbuf
);
2836 case ZFS_PROP_MOUNTED
:
2838 * The 'mounted' property is a pseudo-property that described
2839 * whether the filesystem is currently mounted. Even though
2840 * it's a boolean value, the typical values of "on" and "off"
2841 * don't make sense, so we translate to "yes" and "no".
2843 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2844 src
, &source
, &val
) != 0)
2847 (void) strlcpy(propbuf
, "yes", proplen
);
2849 (void) strlcpy(propbuf
, "no", proplen
);
2854 * The 'name' property is a pseudo-property derived from the
2855 * dataset name. It is presented as a real property to simplify
2858 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2859 zcp_check(zhp
, prop
, 0, propbuf
);
2862 case ZFS_PROP_MLSLABEL
:
2864 #ifdef HAVE_MLSLABEL
2865 m_label_t
*new_sl
= NULL
;
2866 char *ascii
= NULL
; /* human readable label */
2868 (void) strlcpy(propbuf
,
2869 getprop_string(zhp
, prop
, &source
), proplen
);
2871 if (literal
|| (strcasecmp(propbuf
,
2872 ZFS_MLSLABEL_DEFAULT
) == 0))
2876 * Try to translate the internal hex string to
2877 * human-readable output. If there are any
2878 * problems just use the hex string.
2881 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2882 L_NO_CORRECTION
, NULL
) == -1) {
2883 m_label_free(new_sl
);
2887 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2891 m_label_free(new_sl
);
2894 m_label_free(new_sl
);
2896 (void) strlcpy(propbuf
, ascii
, proplen
);
2899 (void) strlcpy(propbuf
,
2900 getprop_string(zhp
, prop
, &source
), proplen
);
2901 #endif /* HAVE_MLSLABEL */
2906 case ZFS_PROP_KEY_GUID
:
2907 case ZFS_PROP_IVSET_GUID
:
2908 case ZFS_PROP_CREATETXG
:
2909 case ZFS_PROP_OBJSETID
:
2910 case ZFS_PROP_PBKDF2_ITERS
:
2912 * These properties are stored as numbers, but they are
2913 * identifiers or counters.
2914 * We don't want them to be pretty printed, because pretty
2915 * printing truncates their values making them useless.
2917 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2919 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2920 zcp_check(zhp
, prop
, val
, NULL
);
2923 case ZFS_PROP_REFERENCED
:
2924 case ZFS_PROP_AVAILABLE
:
2926 case ZFS_PROP_USEDSNAP
:
2927 case ZFS_PROP_USEDDS
:
2928 case ZFS_PROP_USEDREFRESERV
:
2929 case ZFS_PROP_USEDCHILD
:
2930 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2933 (void) snprintf(propbuf
, proplen
, "%llu",
2936 zfs_nicebytes(val
, propbuf
, proplen
);
2938 zcp_check(zhp
, prop
, val
, NULL
);
2942 switch (zfs_prop_get_type(prop
)) {
2943 case PROP_TYPE_NUMBER
:
2944 if (get_numeric_property(zhp
, prop
, src
,
2945 &source
, &val
) != 0) {
2950 (void) snprintf(propbuf
, proplen
, "%llu",
2953 zfs_nicenum(val
, propbuf
, proplen
);
2955 zcp_check(zhp
, prop
, val
, NULL
);
2958 case PROP_TYPE_STRING
:
2959 str
= getprop_string(zhp
, prop
, &source
);
2963 (void) strlcpy(propbuf
, str
, proplen
);
2964 zcp_check(zhp
, prop
, 0, str
);
2967 case PROP_TYPE_INDEX
:
2968 if (get_numeric_property(zhp
, prop
, src
,
2969 &source
, &val
) != 0)
2971 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2974 (void) strlcpy(propbuf
, strval
, proplen
);
2975 zcp_check(zhp
, prop
, 0, strval
);
2983 get_source(zhp
, src
, source
, statbuf
, statlen
);
2989 * Utility function to get the given numeric property. Does no validation that
2990 * the given property is the appropriate type; should only be used with
2991 * hard-coded property types.
2994 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2999 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3005 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3009 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3010 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3014 * Similar to zfs_prop_get(), but returns the value as an integer.
3017 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3018 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3023 * Check to see if this property applies to our object
3025 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3026 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3027 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3028 zfs_prop_to_name(prop
)));
3032 *src
= ZPROP_SRC_NONE
;
3034 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3037 get_source(zhp
, src
, source
, statbuf
, statlen
);
3044 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3045 char **domainp
, idmap_rid_t
*ridp
)
3047 idmap_get_handle_t
*get_hdl
= NULL
;
3051 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3055 err
= idmap_get_sidbyuid(get_hdl
, id
,
3056 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3058 err
= idmap_get_sidbygid(get_hdl
, id
,
3059 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3061 if (err
== IDMAP_SUCCESS
&&
3062 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3063 status
== IDMAP_SUCCESS
)
3069 idmap_get_destroy(get_hdl
);
3072 #endif /* HAVE_IDMAP */
3075 * convert the propname into parameters needed by kernel
3076 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3077 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3078 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3079 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3080 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3081 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3084 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3085 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3087 zfs_userquota_prop_t type
;
3091 boolean_t isproject
;
3097 /* Figure out the property type ({user|group|project}{quota|space}) */
3098 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3099 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3100 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3103 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3107 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3108 type
== ZFS_PROP_USEROBJQUOTA
||
3109 type
== ZFS_PROP_USEROBJUSED
);
3110 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3111 type
== ZFS_PROP_GROUPOBJQUOTA
||
3112 type
== ZFS_PROP_GROUPOBJUSED
);
3113 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3114 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3115 type
== ZFS_PROP_PROJECTOBJUSED
);
3117 cp
= strchr(propname
, '@') + 1;
3119 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3120 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3123 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3124 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3127 } else if (!isproject
&& strchr(cp
, '@')) {
3130 * It's a SID name (eg "user@domain") that needs to be
3131 * turned into S-1-domainID-RID.
3133 directory_error_t e
;
3134 char *numericsid
= NULL
;
3137 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3140 e
= directory_sid_from_user_name(NULL
,
3143 e
= directory_sid_from_group_name(NULL
,
3147 directory_error_free(e
);
3150 if (numericsid
== NULL
)
3153 (void) strlcpy(domain
, cp
, domainlen
);
3154 cp
= strrchr(domain
, '-');
3159 *ridp
= strtoull(cp
, &end
, 10);
3162 if (errno
!= 0 || *end
!= '\0')
3167 #endif /* HAVE_IDMAP */
3169 /* It's a user/group/project ID (eg "12345"). */
3172 id
= strtoul(cp
, &end
, 10);
3175 if (id
> MAXUID
&& !isproject
) {
3177 /* It's an ephemeral ID. */
3181 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3182 &mapdomain
, &rid
) != 0)
3184 (void) strlcpy(domain
, mapdomain
, domainlen
);
3188 #endif /* HAVE_IDMAP */
3198 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3199 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3202 zfs_cmd_t zc
= {"\0"};
3204 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3206 err
= userquota_propname_decode(propname
,
3207 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3208 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3209 zc
.zc_objset_type
= *typep
;
3213 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3217 *propvalue
= zc
.zc_cookie
;
3222 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3223 uint64_t *propvalue
)
3225 zfs_userquota_prop_t type
;
3227 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3232 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3233 char *propbuf
, int proplen
, boolean_t literal
)
3237 zfs_userquota_prop_t type
;
3239 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3246 (void) snprintf(propbuf
, proplen
, "%llu",
3247 (u_longlong_t
)propvalue
);
3248 } else if (propvalue
== 0 &&
3249 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3250 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3251 type
== ZFS_PROP_PROJECTQUOTA
||
3252 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3253 (void) strlcpy(propbuf
, "none", proplen
);
3254 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3255 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3256 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3257 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3259 zfs_nicenum(propvalue
, propbuf
, proplen
);
3265 * propname must start with "written@" or "written#".
3268 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3269 uint64_t *propvalue
)
3272 zfs_cmd_t zc
= {"\0"};
3273 const char *snapname
;
3275 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3277 assert(zfs_prop_written(propname
));
3278 snapname
= propname
+ strlen("written@");
3279 if (strchr(snapname
, '@') != NULL
|| strchr(snapname
, '#') != NULL
) {
3280 /* full snapshot or bookmark name specified */
3281 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3283 /* snapname is the short name, append it to zhp's fsname */
3286 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3287 sizeof (zc
.zc_value
));
3288 cp
= strchr(zc
.zc_value
, '@');
3291 (void) strlcat(zc
.zc_value
, snapname
- 1, sizeof (zc
.zc_value
));
3294 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3298 *propvalue
= zc
.zc_cookie
;
3303 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3304 char *propbuf
, int proplen
, boolean_t literal
)
3309 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3315 (void) snprintf(propbuf
, proplen
, "%llu",
3316 (u_longlong_t
)propvalue
);
3318 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3325 * Returns the name of the given zfs handle.
3328 zfs_get_name(const zfs_handle_t
*zhp
)
3330 return (zhp
->zfs_name
);
3334 * Returns the name of the parent pool for the given zfs handle.
3337 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3339 return (zhp
->zpool_hdl
->zpool_name
);
3343 * Returns the type of the given zfs handle.
3346 zfs_get_type(const zfs_handle_t
*zhp
)
3348 return (zhp
->zfs_type
);
3352 * Returns the type of the given zfs handle,
3353 * or, if a snapshot, the type of the snapshotted dataset.
3356 zfs_get_underlying_type(const zfs_handle_t
*zhp
)
3358 return (zhp
->zfs_head_type
);
3362 * Is one dataset name a child dataset of another?
3364 * Needs to handle these cases:
3365 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3366 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3367 * Descendant? No. No. No. Yes.
3370 is_descendant(const char *ds1
, const char *ds2
)
3372 size_t d1len
= strlen(ds1
);
3374 /* ds2 can't be a descendant if it's smaller */
3375 if (strlen(ds2
) < d1len
)
3378 /* otherwise, compare strings and verify that there's a '/' char */
3379 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3383 * Given a complete name, return just the portion that refers to the parent.
3384 * Will return -1 if there is no parent (path is just the name of the
3388 parent_name(const char *path
, char *buf
, size_t buflen
)
3392 (void) strlcpy(buf
, path
, buflen
);
3394 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3402 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3404 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3408 * If accept_ancestor is false, then check to make sure that the given path has
3409 * a parent, and that it exists. If accept_ancestor is true, then find the
3410 * closest existing ancestor for the given path. In prefixlen return the
3411 * length of already existing prefix of the given path. We also fetch the
3412 * 'zoned' property, which is used to validate property settings when creating
3416 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3417 boolean_t accept_ancestor
, int *prefixlen
)
3419 zfs_cmd_t zc
= {"\0"};
3420 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3426 (void) snprintf(errbuf
, sizeof (errbuf
),
3427 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3429 /* get parent, and check to see if this is just a pool */
3430 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3431 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3432 "missing dataset name"));
3433 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3436 /* check to see if the pool exists */
3437 if ((slash
= strchr(parent
, '/')) == NULL
)
3438 slash
= parent
+ strlen(parent
);
3439 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3440 zc
.zc_name
[slash
- parent
] = '\0';
3441 if (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3443 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3444 "no such pool '%s'"), zc
.zc_name
);
3445 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3448 /* check to see if the parent dataset exists */
3449 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3450 if (errno
== ENOENT
&& accept_ancestor
) {
3452 * Go deeper to find an ancestor, give up on top level.
3454 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3456 "no such pool '%s'"), zc
.zc_name
);
3457 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3459 } else if (errno
== ENOENT
) {
3460 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3461 "parent does not exist"));
3462 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3464 return (zfs_standard_error(hdl
, errno
, errbuf
));
3467 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3471 /* we are in a non-global zone, but parent is in the global zone */
3472 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3473 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3478 /* make sure parent is a filesystem */
3479 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3480 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3481 "parent is not a filesystem"));
3482 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3488 if (prefixlen
!= NULL
)
3489 *prefixlen
= strlen(parent
);
3494 * Finds whether the dataset of the given type(s) exists.
3497 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3501 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3505 * Try to get stats for the dataset, which will tell us if it exists.
3507 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3508 int ds_type
= zhp
->zfs_type
;
3511 if (types
& ds_type
)
3518 * Given a path to 'target', create all the ancestors between
3519 * the prefixlen portion of the path, and the target itself.
3520 * Fail if the initial prefixlen-ancestor does not already exist.
3523 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3529 /* make sure prefix exists */
3530 cp
= target
+ prefixlen
;
3532 assert(strchr(cp
, '/') == NULL
);
3533 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3536 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3544 * Attempt to create, mount, and share any ancestor filesystems,
3545 * up to the prefixlen-long one.
3547 for (cp
= target
+ prefixlen
+ 1;
3548 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3552 h
= make_dataset_handle(hdl
, target
);
3554 /* it already exists, nothing to do here */
3559 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3561 opname
= dgettext(TEXT_DOMAIN
, "create");
3565 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3567 opname
= dgettext(TEXT_DOMAIN
, "open");
3571 if (zfs_mount(h
, NULL
, 0) != 0) {
3572 opname
= dgettext(TEXT_DOMAIN
, "mount");
3576 if (zfs_share(h
) != 0) {
3577 opname
= dgettext(TEXT_DOMAIN
, "share");
3583 zfs_commit_all_shares();
3588 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3589 "failed to %s ancestor '%s'"), opname
, target
);
3594 * Creates non-existing ancestors of the given path.
3597 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3604 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3605 "cannot create '%s'"), path
);
3608 * Check that we are not passing the nesting limit
3609 * before we start creating any ancestors.
3611 if (dataset_nestcheck(path
) != 0) {
3612 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3613 "maximum name nesting depth exceeded"));
3614 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3617 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3620 if ((path_copy
= strdup(path
)) != NULL
) {
3621 rc
= create_parents(hdl
, path_copy
, prefix
);
3624 if (path_copy
== NULL
|| rc
!= 0)
3631 * Create a new filesystem or volume.
3634 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3639 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3641 enum lzc_dataset_type ost
;
3642 zpool_handle_t
*zpool_handle
;
3643 uint8_t *wkeydata
= NULL
;
3646 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3648 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3649 "cannot create '%s'"), path
);
3651 /* validate the path, taking care to note the extended error message */
3652 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3653 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3655 if (dataset_nestcheck(path
) != 0) {
3656 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3657 "maximum name nesting depth exceeded"));
3658 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3661 /* validate parents exist */
3662 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3666 * The failure modes when creating a dataset of a different type over
3667 * one that already exists is a little strange. In particular, if you
3668 * try to create a dataset on top of an existing dataset, the ioctl()
3669 * will return ENOENT, not EEXIST. To prevent this from happening, we
3670 * first try to see if the dataset exists.
3672 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3673 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3674 "dataset already exists"));
3675 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3678 if (type
== ZFS_TYPE_VOLUME
)
3679 ost
= LZC_DATSET_TYPE_ZVOL
;
3681 ost
= LZC_DATSET_TYPE_ZFS
;
3683 /* open zpool handle for prop validation */
3684 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3685 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3687 /* truncate pool_path at first slash */
3688 char *p
= strchr(pool_path
, '/');
3692 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3695 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3696 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3697 zpool_close(zpool_handle
);
3700 zpool_close(zpool_handle
);
3702 if (type
== ZFS_TYPE_VOLUME
) {
3704 * If we are creating a volume, the size and block size must
3705 * satisfy a few restraints. First, the blocksize must be a
3706 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3707 * volsize must be a multiple of the block size, and cannot be
3710 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3711 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3713 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3714 "missing volume size"));
3715 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3718 if ((ret
= nvlist_lookup_uint64(props
,
3719 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3720 &blocksize
)) != 0) {
3721 if (ret
== ENOENT
) {
3722 blocksize
= zfs_prop_default_numeric(
3723 ZFS_PROP_VOLBLOCKSIZE
);
3726 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3727 "missing volume block size"));
3728 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3734 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3735 "volume size cannot be zero"));
3736 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3739 if (size
% blocksize
!= 0) {
3741 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3742 "volume size must be a multiple of volume block "
3744 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3748 (void) parent_name(path
, parent
, sizeof (parent
));
3749 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3750 &wkeydata
, &wkeylen
) != 0) {
3752 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3755 /* create the dataset */
3756 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3758 if (wkeydata
!= NULL
)
3761 /* check for failure */
3765 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3766 "no such parent '%s'"), parent
);
3767 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3770 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3771 "pool must be upgraded to set this "
3772 "property or value"));
3773 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3776 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3777 "encryption root's key is not loaded "
3779 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3782 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3783 "invalid property value(s) specified"));
3784 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3788 * This platform can't address a volume this big.
3790 if (type
== ZFS_TYPE_VOLUME
)
3791 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3796 return (zfs_standard_error(hdl
, errno
, errbuf
));
3804 * Destroys the given dataset. The caller must make sure that the filesystem
3805 * isn't mounted, and that there are no active dependents. If the file system
3806 * does not exist this function does nothing.
3809 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3813 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3816 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3817 nvlist_t
*nv
= fnvlist_alloc();
3818 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3819 error
= lzc_destroy_bookmarks(nv
, NULL
);
3822 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3823 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3829 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3830 nvlist_t
*nv
= fnvlist_alloc();
3831 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3832 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3835 error
= lzc_destroy(zhp
->zfs_name
);
3838 if (error
!= 0 && error
!= ENOENT
) {
3839 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3840 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3844 remove_mountpoint(zhp
);
3849 struct destroydata
{
3851 const char *snapname
;
3855 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3857 struct destroydata
*dd
= arg
;
3858 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3861 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3862 dd
->snapname
) >= sizeof (name
))
3865 if (lzc_exists(name
))
3866 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3868 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3874 * Destroys all snapshots with the given name in zhp & descendants.
3877 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3880 struct destroydata dd
= { 0 };
3882 dd
.snapname
= snapname
;
3883 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3884 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3886 if (nvlist_empty(dd
.nvl
)) {
3887 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3888 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3889 zhp
->zfs_name
, snapname
);
3891 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3893 nvlist_free(dd
.nvl
);
3898 * Destroys all the snapshots named in the nvlist.
3901 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3903 nvlist_t
*errlist
= NULL
;
3906 int ret
= zfs_destroy_snaps_nvl_os(hdl
, snaps
);
3910 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3913 nvlist_free(errlist
);
3917 if (nvlist_empty(errlist
)) {
3919 (void) snprintf(errbuf
, sizeof (errbuf
),
3920 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3922 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3924 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3925 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3927 (void) snprintf(errbuf
, sizeof (errbuf
),
3928 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3931 switch (fnvpair_value_int32(pair
)) {
3934 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3935 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3938 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3943 nvlist_free(errlist
);
3948 * Clones the given dataset. The target must be of the same type as the source.
3951 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3953 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3956 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3959 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3961 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3962 "cannot create '%s'"), target
);
3964 /* validate the target/clone name */
3965 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3966 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3968 /* validate parents exist */
3969 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3972 (void) parent_name(target
, parent
, sizeof (parent
));
3977 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
3979 if (ZFS_IS_VOLUME(zhp
))
3980 type
= ZFS_TYPE_VOLUME
;
3981 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3982 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
3984 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
3990 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
3992 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3995 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
4003 * The parent doesn't exist. We should have caught this
4004 * above, but there may a race condition that has since
4005 * destroyed the parent.
4007 * At this point, we don't know whether it's the source
4008 * that doesn't exist anymore, or whether the target
4009 * dataset doesn't exist.
4011 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4012 "no such parent '%s'"), parent
);
4013 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4016 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4017 "source and target pools differ"));
4018 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4022 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4031 * Promotes the given clone fs to be the clone parent.
4034 zfs_promote(zfs_handle_t
*zhp
)
4036 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4037 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4041 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4042 "cannot promote '%s'"), zhp
->zfs_name
);
4044 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4045 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4046 "snapshots can not be promoted"));
4047 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4050 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4051 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4052 "not a cloned filesystem"));
4053 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4056 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4057 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4059 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4065 * Promoting encrypted dataset outside its
4068 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4069 "cannot promote dataset outside its "
4070 "encryption root"));
4071 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4074 /* There is a conflicting snapshot name. */
4075 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4076 "conflicting snapshot '%s' from parent '%s'"),
4077 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4078 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4081 return (zfs_standard_error(hdl
, ret
, errbuf
));
4087 typedef struct snapdata
{
4089 const char *sd_snapname
;
4093 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4095 snapdata_t
*sd
= arg
;
4096 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4099 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4100 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4101 sd
->sd_snapname
) >= sizeof (name
))
4104 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4106 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4114 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4118 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4124 zpool_handle_t
*zpool_hdl
;
4125 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4127 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4128 "cannot create snapshots "));
4131 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4132 const char *snapname
= nvpair_name(elem
);
4134 /* validate the target name */
4135 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4137 (void) snprintf(errbuf
, sizeof (errbuf
),
4138 dgettext(TEXT_DOMAIN
,
4139 "cannot create snapshot '%s'"), snapname
);
4140 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4145 * get pool handle for prop validation. assumes all snaps are in the
4146 * same pool, as does lzc_snapshot (below).
4148 elem
= nvlist_next_nvpair(snaps
, NULL
);
4149 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4150 pool
[strcspn(pool
, "/@")] = '\0';
4151 zpool_hdl
= zpool_open(hdl
, pool
);
4152 if (zpool_hdl
== NULL
)
4155 if (props
!= NULL
&&
4156 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4157 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4158 zpool_close(zpool_hdl
);
4161 zpool_close(zpool_hdl
);
4163 ret
= lzc_snapshot(snaps
, props
, &errors
);
4166 boolean_t printed
= B_FALSE
;
4167 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4169 elem
= nvlist_next_nvpair(errors
, elem
)) {
4170 (void) snprintf(errbuf
, sizeof (errbuf
),
4171 dgettext(TEXT_DOMAIN
,
4172 "cannot create snapshot '%s'"), nvpair_name(elem
));
4173 (void) zfs_standard_error(hdl
,
4174 fnvpair_value_int32(elem
), errbuf
);
4180 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4181 "multiple snapshots of same "
4183 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4187 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4193 nvlist_free(errors
);
4198 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4202 snapdata_t sd
= { 0 };
4203 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4208 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4209 "cannot snapshot %s"), path
);
4211 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4212 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4214 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4215 cp
= strchr(fsname
, '@');
4217 sd
.sd_snapname
= cp
+ 1;
4219 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4220 ZFS_TYPE_VOLUME
)) == NULL
) {
4224 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4226 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4228 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4231 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4232 nvlist_free(sd
.sd_nvl
);
4238 * Destroy any more recent snapshots. We invoke this callback on any dependents
4239 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4240 * is a dependent and we should just destroy it without checking the transaction
4243 typedef struct rollback_data
{
4244 const char *cb_target
; /* the snapshot */
4245 uint64_t cb_create
; /* creation time reference */
4251 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4253 rollback_data_t
*cbp
= data
;
4254 prop_changelist_t
*clp
;
4256 /* We must destroy this clone; first unmount it */
4257 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4258 cbp
->cb_force
? MS_FORCE
: 0);
4259 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4260 cbp
->cb_error
= B_TRUE
;
4264 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4265 cbp
->cb_error
= B_TRUE
;
4267 changelist_remove(clp
, zhp
->zfs_name
);
4268 (void) changelist_postfix(clp
);
4269 changelist_free(clp
);
4276 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4278 rollback_data_t
*cbp
= data
;
4280 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4281 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4282 rollback_destroy_dependent
, cbp
);
4284 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4292 * Given a dataset, rollback to a specific snapshot, discarding any
4293 * data changes since then and making it the active dataset.
4295 * Any snapshots and bookmarks more recent than the target are
4296 * destroyed, along with their dependents (i.e. clones).
4299 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4301 rollback_data_t cb
= { 0 };
4303 boolean_t restore_resv
= 0;
4304 uint64_t old_volsize
= 0, new_volsize
;
4305 zfs_prop_t resv_prop
= { 0 };
4306 uint64_t min_txg
= 0;
4308 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4309 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4312 * Destroy all recent snapshots and their dependents.
4314 cb
.cb_force
= force
;
4315 cb
.cb_target
= snap
->zfs_name
;
4316 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4318 if (cb
.cb_create
> 0)
4319 min_txg
= cb
.cb_create
;
4321 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
,
4324 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4330 * Now that we have verified that the snapshot is the latest,
4331 * rollback to the given snapshot.
4334 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4335 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4337 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4339 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4343 * Pass both the filesystem and the wanted snapshot names,
4344 * we would get an error back if the snapshot is destroyed or
4345 * a new snapshot is created before this request is processed.
4347 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4351 (void) snprintf(errbuf
, sizeof (errbuf
),
4352 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4356 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4357 "there is a snapshot or bookmark more recent "
4358 "than '%s'"), snap
->zfs_name
);
4359 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4362 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4363 "'%s' is not found among snapshots of '%s'"),
4364 snap
->zfs_name
, zhp
->zfs_name
);
4365 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4368 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4371 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4377 * For volumes, if the pre-rollback volsize matched the pre-
4378 * rollback reservation and the volsize has changed then set
4379 * the reservation property to the post-rollback volsize.
4380 * Make a new handle since the rollback closed the dataset.
4382 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4383 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4385 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4386 if (old_volsize
!= new_volsize
)
4387 err
= zfs_prop_set_int(zhp
, resv_prop
,
4396 * Renames the given dataset.
4399 zfs_rename(zfs_handle_t
*zhp
, const char *target
, renameflags_t flags
)
4402 zfs_cmd_t zc
= {"\0"};
4404 prop_changelist_t
*cl
= NULL
;
4405 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4406 char property
[ZFS_MAXPROPLEN
];
4407 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4410 /* if we have the same exact name, just return success */
4411 if (strcmp(zhp
->zfs_name
, target
) == 0)
4414 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4415 "cannot rename to '%s'"), target
);
4417 /* make sure source name is valid */
4418 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4419 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4422 * Make sure the target name is valid
4424 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4425 if ((strchr(target
, '@') == NULL
) ||
4428 * Snapshot target name is abbreviated,
4429 * reconstruct full dataset name
4431 (void) strlcpy(parent
, zhp
->zfs_name
,
4433 delim
= strchr(parent
, '@');
4434 if (strchr(target
, '@') == NULL
)
4438 (void) strlcat(parent
, target
, sizeof (parent
));
4442 * Make sure we're renaming within the same dataset.
4444 delim
= strchr(target
, '@');
4445 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4446 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4447 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4448 "snapshots must be part of same "
4450 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4455 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4456 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4458 if (flags
.recursive
) {
4459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4460 "recursive rename must be a snapshot"));
4461 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4464 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4465 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4467 /* validate parents */
4468 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4471 /* make sure we're in the same pool */
4472 verify((delim
= strchr(target
, '/')) != NULL
);
4473 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4474 zhp
->zfs_name
[delim
- target
] != '/') {
4475 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4476 "datasets must be within same pool"));
4477 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4480 /* new name cannot be a child of the current dataset name */
4481 if (is_descendant(zhp
->zfs_name
, target
)) {
4482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4483 "New dataset name cannot be a descendant of "
4484 "current dataset name"));
4485 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4489 (void) snprintf(errbuf
, sizeof (errbuf
),
4490 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4492 if (getzoneid() == GLOBAL_ZONEID
&&
4493 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4494 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4495 "dataset is used in a non-global zone"));
4496 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4500 * Avoid unmounting file systems with mountpoint property set to
4501 * 'legacy' or 'none' even if -u option is not given.
4503 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
4504 !flags
.recursive
&& !flags
.nounmount
&&
4505 zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, property
,
4506 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) == 0 &&
4507 (strcmp(property
, "legacy") == 0 ||
4508 strcmp(property
, "none") == 0)) {
4509 flags
.nounmount
= B_TRUE
;
4511 if (flags
.recursive
) {
4512 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4513 if (parentname
== NULL
) {
4517 delim
= strchr(parentname
, '@');
4519 zfs_handle_t
*zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
,
4527 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4528 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4529 flags
.nounmount
? CL_GATHER_DONT_UNMOUNT
:
4530 CL_GATHER_ITER_MOUNTED
,
4531 flags
.forceunmount
? MS_FORCE
: 0)) == NULL
)
4534 if (changelist_haszonedchild(cl
)) {
4535 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4536 "child dataset with inherited mountpoint is used "
4537 "in a non-global zone"));
4538 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4543 if ((ret
= changelist_prefix(cl
)) != 0)
4547 if (ZFS_IS_VOLUME(zhp
))
4548 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4550 zc
.zc_objset_type
= DMU_OST_ZFS
;
4552 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4553 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4555 zc
.zc_cookie
= !!flags
.recursive
;
4556 zc
.zc_cookie
|= (!!flags
.nounmount
) << 1;
4558 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4560 * if it was recursive, the one that actually failed will
4563 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4564 "cannot rename '%s'"), zc
.zc_name
);
4566 if (flags
.recursive
&& errno
== EEXIST
) {
4567 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4568 "a child dataset already has a snapshot "
4569 "with the new name"));
4570 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4571 } else if (errno
== EACCES
) {
4572 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4573 "cannot move encrypted child outside of "
4574 "its encryption root"));
4575 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4577 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4581 * On failure, we still want to remount any filesystems that
4582 * were previously mounted, so we don't alter the system state.
4585 (void) changelist_postfix(cl
);
4588 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4589 ret
= changelist_postfix(cl
);
4595 changelist_free(cl
);
4601 zfs_get_all_props(zfs_handle_t
*zhp
)
4603 return (zhp
->zfs_props
);
4607 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4609 if (zhp
->zfs_recvd_props
== NULL
)
4610 if (get_recvd_props_ioctl(zhp
) != 0)
4612 return (zhp
->zfs_recvd_props
);
4616 zfs_get_user_props(zfs_handle_t
*zhp
)
4618 return (zhp
->zfs_user_props
);
4622 * This function is used by 'zfs list' to determine the exact set of columns to
4623 * display, and their maximum widths. This does two main things:
4625 * - If this is a list of all properties, then expand the list to include
4626 * all native properties, and set a flag so that for each dataset we look
4627 * for new unique user properties and add them to the list.
4629 * - For non fixed-width properties, keep track of the maximum width seen
4630 * so that we can size the column appropriately. If the user has
4631 * requested received property values, we also need to compute the width
4632 * of the RECEIVED column.
4635 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4638 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4639 zprop_list_t
*entry
;
4640 zprop_list_t
**last
, **start
;
4641 nvlist_t
*userprops
, *propval
;
4644 char buf
[ZFS_MAXPROPLEN
];
4646 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4649 userprops
= zfs_get_user_props(zhp
);
4652 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4654 * Go through and add any user properties as necessary. We
4655 * start by incrementing our list pointer to the first
4656 * non-native property.
4659 while (*start
!= NULL
) {
4660 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4662 start
= &(*start
)->pl_next
;
4666 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4668 * See if we've already found this property in our list.
4670 for (last
= start
; *last
!= NULL
;
4671 last
= &(*last
)->pl_next
) {
4672 if (strcmp((*last
)->pl_user_prop
,
4673 nvpair_name(elem
)) == 0)
4677 if (*last
== NULL
) {
4678 if ((entry
= zfs_alloc(hdl
,
4679 sizeof (zprop_list_t
))) == NULL
||
4680 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4681 nvpair_name(elem
)))) == NULL
) {
4686 entry
->pl_prop
= ZPROP_INVAL
;
4687 entry
->pl_width
= strlen(nvpair_name(elem
));
4688 entry
->pl_all
= B_TRUE
;
4695 * Now go through and check the width of any non-fixed columns
4697 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4698 if (entry
->pl_fixed
&& !literal
)
4701 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4702 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4703 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4704 if (strlen(buf
) > entry
->pl_width
)
4705 entry
->pl_width
= strlen(buf
);
4707 if (received
&& zfs_prop_get_recvd(zhp
,
4708 zfs_prop_to_name(entry
->pl_prop
),
4709 buf
, sizeof (buf
), literal
) == 0)
4710 if (strlen(buf
) > entry
->pl_recvd_width
)
4711 entry
->pl_recvd_width
= strlen(buf
);
4713 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4715 verify(nvlist_lookup_string(propval
,
4716 ZPROP_VALUE
, &strval
) == 0);
4717 if (strlen(strval
) > entry
->pl_width
)
4718 entry
->pl_width
= strlen(strval
);
4720 if (received
&& zfs_prop_get_recvd(zhp
,
4721 entry
->pl_user_prop
,
4722 buf
, sizeof (buf
), literal
) == 0)
4723 if (strlen(buf
) > entry
->pl_recvd_width
)
4724 entry
->pl_recvd_width
= strlen(buf
);
4732 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4738 * Keep a reference to the props-table against which we prune the
4741 zhp
->zfs_props_table
= props
;
4743 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4746 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4747 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4750 * User properties will result in ZPROP_INVAL, and since we
4751 * only know how to prune standard ZFS properties, we always
4752 * leave these in the list. This can also happen if we
4753 * encounter an unknown DSL property (when running older
4754 * software, for example).
4756 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4757 (void) nvlist_remove(zhp
->zfs_props
,
4758 nvpair_name(curr
), nvpair_type(curr
));
4764 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4765 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4767 zfs_cmd_t zc
= {"\0"};
4768 nvlist_t
*nvlist
= NULL
;
4771 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4772 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4773 zc
.zc_cookie
= (uint64_t)cmd
;
4775 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4776 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4777 (void) no_memory(hdl
);
4783 case ZFS_SMB_ACL_ADD
:
4784 case ZFS_SMB_ACL_REMOVE
:
4785 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4787 case ZFS_SMB_ACL_RENAME
:
4788 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4790 (void) no_memory(hdl
);
4793 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4795 (void) no_memory(hdl
);
4798 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4799 nvlist_free(nvlist
);
4803 case ZFS_SMB_ACL_PURGE
:
4808 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4809 nvlist_free(nvlist
);
4814 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4815 char *path
, char *resource
)
4817 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4822 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4823 char *path
, char *resource
)
4825 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4830 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4832 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4837 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4838 char *oldname
, char *newname
)
4840 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4845 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4846 zfs_userspace_cb_t func
, void *arg
)
4848 zfs_cmd_t zc
= {"\0"};
4849 zfs_useracct_t buf
[100];
4850 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4853 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4855 zc
.zc_objset_type
= type
;
4856 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4859 zfs_useracct_t
*zua
= buf
;
4861 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4862 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4863 if ((errno
== ENOTSUP
&&
4864 (type
== ZFS_PROP_USEROBJUSED
||
4865 type
== ZFS_PROP_GROUPOBJUSED
||
4866 type
== ZFS_PROP_USEROBJQUOTA
||
4867 type
== ZFS_PROP_GROUPOBJQUOTA
||
4868 type
== ZFS_PROP_PROJECTOBJUSED
||
4869 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4870 type
== ZFS_PROP_PROJECTUSED
||
4871 type
== ZFS_PROP_PROJECTQUOTA
)))
4874 return (zfs_standard_error_fmt(hdl
, errno
,
4875 dgettext(TEXT_DOMAIN
,
4876 "cannot get used/quota for %s"), zc
.zc_name
));
4878 if (zc
.zc_nvlist_dst_size
== 0)
4881 while (zc
.zc_nvlist_dst_size
> 0) {
4882 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4883 zua
->zu_space
)) != 0)
4886 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4895 const char *snapname
;
4897 boolean_t recursive
;
4902 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4904 struct holdarg
*ha
= arg
;
4905 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4908 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4909 ha
->snapname
) >= sizeof (name
))
4912 if (lzc_exists(name
))
4913 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4916 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4922 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4923 boolean_t recursive
, int cleanup_fd
)
4928 ha
.nvl
= fnvlist_alloc();
4929 ha
.snapname
= snapname
;
4931 ha
.recursive
= recursive
;
4932 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4934 if (nvlist_empty(ha
.nvl
)) {
4937 fnvlist_free(ha
.nvl
);
4939 (void) snprintf(errbuf
, sizeof (errbuf
),
4940 dgettext(TEXT_DOMAIN
,
4941 "cannot hold snapshot '%s@%s'"),
4942 zhp
->zfs_name
, snapname
);
4943 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4947 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4948 fnvlist_free(ha
.nvl
);
4954 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4958 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4963 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4966 /* There may be errors even in the success case. */
4967 fnvlist_free(errors
);
4971 if (nvlist_empty(errors
)) {
4972 /* no hold-specific errors */
4973 (void) snprintf(errbuf
, sizeof (errbuf
),
4974 dgettext(TEXT_DOMAIN
, "cannot hold"));
4977 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4978 "pool must be upgraded"));
4979 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4982 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4985 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4989 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4991 elem
= nvlist_next_nvpair(errors
, elem
)) {
4992 (void) snprintf(errbuf
, sizeof (errbuf
),
4993 dgettext(TEXT_DOMAIN
,
4994 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4995 switch (fnvpair_value_int32(elem
)) {
4998 * Temporary tags wind up having the ds object id
4999 * prepended. So even if we passed the length check
5000 * above, it's still possible for the tag to wind
5001 * up being slightly too long.
5003 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5006 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5009 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5012 (void) zfs_standard_error(hdl
,
5013 fnvpair_value_int32(elem
), errbuf
);
5017 fnvlist_free(errors
);
5022 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5024 struct holdarg
*ha
= arg
;
5025 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5027 nvlist_t
*existing_holds
;
5029 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5030 ha
->snapname
) >= sizeof (name
)) {
5035 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5037 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5040 nvlist_t
*torelease
= fnvlist_alloc();
5041 fnvlist_add_boolean(torelease
, ha
->tag
);
5042 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5043 fnvlist_free(torelease
);
5047 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5053 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5054 boolean_t recursive
)
5058 nvlist_t
*errors
= NULL
;
5060 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5063 ha
.nvl
= fnvlist_alloc();
5064 ha
.snapname
= snapname
;
5066 ha
.recursive
= recursive
;
5068 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5070 if (nvlist_empty(ha
.nvl
)) {
5071 fnvlist_free(ha
.nvl
);
5073 (void) snprintf(errbuf
, sizeof (errbuf
),
5074 dgettext(TEXT_DOMAIN
,
5075 "cannot release hold from snapshot '%s@%s'"),
5076 zhp
->zfs_name
, snapname
);
5078 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5080 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5085 ret
= lzc_release(ha
.nvl
, &errors
);
5086 fnvlist_free(ha
.nvl
);
5089 /* There may be errors even in the success case. */
5090 fnvlist_free(errors
);
5094 if (nvlist_empty(errors
)) {
5095 /* no hold-specific errors */
5096 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5100 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5101 "pool must be upgraded"));
5102 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5105 (void) zfs_standard_error(hdl
, errno
, errbuf
);
5109 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5111 elem
= nvlist_next_nvpair(errors
, elem
)) {
5112 (void) snprintf(errbuf
, sizeof (errbuf
),
5113 dgettext(TEXT_DOMAIN
,
5114 "cannot release hold from snapshot '%s'"),
5116 switch (fnvpair_value_int32(elem
)) {
5118 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5121 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5124 (void) zfs_standard_error(hdl
,
5125 fnvpair_value_int32(elem
), errbuf
);
5129 fnvlist_free(errors
);
5134 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5136 zfs_cmd_t zc
= {"\0"};
5137 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5143 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5144 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5148 nvbuf
= malloc(nvsz
);
5149 if (nvbuf
== NULL
) {
5150 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5154 zc
.zc_nvlist_dst_size
= nvsz
;
5155 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5157 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5159 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5160 (void) snprintf(errbuf
, sizeof (errbuf
),
5161 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5166 nvsz
= zc
.zc_nvlist_dst_size
;
5170 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5171 "pool must be upgraded"));
5172 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5175 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5178 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5181 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5186 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5188 err
= zfs_standard_error_fmt(hdl
, rc
, dgettext(
5189 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5200 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5202 zfs_cmd_t zc
= {"\0"};
5203 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5209 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5210 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5212 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5215 nvbuf
= malloc(nvsz
);
5217 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5220 zc
.zc_nvlist_src_size
= nvsz
;
5221 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5222 zc
.zc_perm_action
= un
;
5224 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5226 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5227 (void) snprintf(errbuf
, sizeof (errbuf
),
5228 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5232 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5233 "pool must be upgraded"));
5234 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5237 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5240 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5243 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5254 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5259 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5262 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5264 (void) snprintf(errbuf
, sizeof (errbuf
),
5265 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5269 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5270 "pool must be upgraded"));
5271 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5274 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5277 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5280 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5289 * The theory of raidz space accounting
5291 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5292 * will "reference" 128KB, even though it allocates more than that, to store the
5293 * parity information (and perhaps skip sectors). This concept of the
5294 * "referenced" (and other DMU space accounting) being lower than the allocated
5295 * space by a constant factor is called "raidz deflation."
5297 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5298 * block size. However, zvols typically have a much smaller block size (default
5299 * 8KB). These smaller blocks may require proportionally much more parity
5300 * information (and perhaps skip sectors). In this case, the change to the
5301 * "referenced" property may be much more than the logical block size.
5303 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5306 * +-------+-------+-------+-------+-------+
5307 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5308 * +-------+-------+-------+-------+-------+
5309 * | P0 | D0 | D8 | D16 | D24 |
5310 * | P1 | D1 | D9 | D17 | D25 |
5311 * | P2 | D2 | D10 | D18 | D26 |
5312 * | P3 | D3 | D11 | D19 | D27 |
5313 * | P4 | D4 | D12 | D20 | D28 |
5314 * | P5 | D5 | D13 | D21 | D29 |
5315 * | P6 | D6 | D14 | D22 | D30 |
5316 * | P7 | D7 | D15 | D23 | D31 |
5317 * +-------+-------+-------+-------+-------+
5319 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5320 * sectors. The dataset's referenced will increase by 128k and the pool's
5321 * allocated and free properties will be adjusted by 160k.
5323 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5324 * blank cells represent unallocated space.
5326 * +-------+-------+-------+-------+-------+
5327 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5328 * +-------+-------+-------+-------+-------+
5330 * +-------+-------+-------+-------+-------+
5332 * Above, notice that the 4k block required one sector for parity and another
5333 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5334 * and free properties will be adjusted by 8k. The dataset will not be charged
5335 * 8k. Rather, it will be charged a value that is scaled according to the
5336 * overhead of the 128k block on the same vdev. This 8k allocation will be
5337 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5338 * calculated in the 128k block example above.
5340 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5341 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5342 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5343 * multiple of of 4 sectors. When a block does not fill the required number of
5344 * sectors, skip blocks (sectors) are used.
5346 * An 8k block being written to a raidz vdev may be written as follows:
5348 * +-------+-------+-------+-------+-------+
5349 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5350 * +-------+-------+-------+-------+-------+
5351 * | P0 | D0 | D1 | S0 | |
5352 * +-------+-------+-------+-------+-------+
5354 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5355 * added. For this 8k block, the pool's allocated and free properties are
5356 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5357 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5358 * the 128k block example above.
5360 * The situation is slightly different for dRAID since the minimum allocation
5361 * size is the full group width. The same 8K block above would be written as
5362 * follows in a dRAID group:
5364 * +-------+-------+-------+-------+-------+
5365 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5366 * +-------+-------+-------+-------+-------+
5367 * | P0 | D0 | D1 | S0 | S1 |
5368 * +-------+-------+-------+-------+-------+
5370 * Compression may lead to a variety of block sizes being written for the same
5371 * volume or file. There is no clear way to reserve just the amount of space
5372 * that will be required, so the worst case (no compression) is assumed.
5373 * Note that metadata blocks will typically be compressed, so the reservation
5374 * size returned by zvol_volsize_to_reservation() will generally be slightly
5375 * larger than the maximum that the volume can reference.
5379 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5380 * amount of space (in bytes) that will be allocated for the specified block
5381 * size. Note that the "referenced" space accounted will be less than this, but
5382 * not necessarily equal to "blksize", due to RAIDZ deflation.
5385 vdev_raidz_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5388 uint64_t asize
, ndata
;
5390 ASSERT3U(ndisks
, >, nparity
);
5391 ndata
= ndisks
- nparity
;
5392 asize
= ((blksize
- 1) >> ashift
) + 1;
5393 asize
+= nparity
* ((asize
+ ndata
- 1) / ndata
);
5394 asize
= roundup(asize
, nparity
+ 1) << ashift
;
5400 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5401 * amount of space (in bytes) that will be allocated for the specified block
5405 vdev_draid_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5408 ASSERT3U(ndisks
, >, nparity
);
5409 uint64_t ndata
= ndisks
- nparity
;
5410 uint64_t rows
= ((blksize
- 1) / (ndata
<< ashift
)) + 1;
5411 uint64_t asize
= (rows
* ndisks
) << ashift
;
5417 * Determine how much space will be allocated if it lands on the most space-
5418 * inefficient top-level vdev. Returns the size in bytes required to store one
5419 * copy of the volume data. See theory comment above.
5422 volsize_from_vdevs(zpool_handle_t
*zhp
, uint64_t nblocks
, uint64_t blksize
)
5424 nvlist_t
*config
, *tree
, **vdevs
;
5428 config
= zpool_get_config(zhp
, NULL
);
5429 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
5430 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
5431 &vdevs
, &nvdevs
) != 0) {
5432 return (nblocks
* blksize
);
5435 for (int v
= 0; v
< nvdevs
; v
++) {
5437 uint64_t nparity
, ashift
, asize
, tsize
;
5440 if (nvlist_lookup_string(vdevs
[v
], ZPOOL_CONFIG_TYPE
,
5444 if (strcmp(type
, VDEV_TYPE_RAIDZ
) != 0 &&
5445 strcmp(type
, VDEV_TYPE_DRAID
) != 0)
5448 if (nvlist_lookup_uint64(vdevs
[v
],
5449 ZPOOL_CONFIG_NPARITY
, &nparity
) != 0)
5452 if (nvlist_lookup_uint64(vdevs
[v
],
5453 ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
5456 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
5460 if (nvlist_lookup_nvlist_array(vdevs
[v
],
5461 ZPOOL_CONFIG_CHILDREN
, &disks
, &ndisks
) != 0)
5464 /* allocation size for the "typical" 128k block */
5465 tsize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5466 SPA_OLD_MAXBLOCKSIZE
);
5468 /* allocation size for the blksize block */
5469 asize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5474 if (nvlist_lookup_uint64(vdevs
[v
],
5475 ZPOOL_CONFIG_DRAID_NDATA
, &ndata
) != 0)
5478 /* allocation size for the "typical" 128k block */
5479 tsize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5480 ashift
, SPA_OLD_MAXBLOCKSIZE
);
5482 /* allocation size for the blksize block */
5483 asize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5488 * Scale this size down as a ratio of 128k / tsize.
5489 * See theory statement above.
5491 volsize
= nblocks
* asize
* SPA_OLD_MAXBLOCKSIZE
/ tsize
;
5492 if (volsize
> ret
) {
5498 ret
= nblocks
* blksize
;
5505 * Convert the zvol's volume size to an appropriate reservation. See theory
5508 * Note: If this routine is updated, it is necessary to update the ZFS test
5509 * suite's shell version in reservation.shlib.
5512 zvol_volsize_to_reservation(zpool_handle_t
*zph
, uint64_t volsize
,
5516 uint64_t nblocks
, volblocksize
;
5520 if (nvlist_lookup_string(props
,
5521 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5522 ncopies
= atoi(strval
);
5525 if (nvlist_lookup_uint64(props
,
5526 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5527 &volblocksize
) != 0)
5528 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5530 nblocks
= volsize
/ volblocksize
;
5532 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5533 * this reason, only the data blocks are scaled based on vdev config.
5535 volsize
= volsize_from_vdevs(zph
, nblocks
, volblocksize
);
5537 /* start with metadnode L0-L6 */
5539 /* calculate number of indirects */
5540 while (nblocks
> 1) {
5541 nblocks
+= DNODES_PER_LEVEL
- 1;
5542 nblocks
/= DNODES_PER_LEVEL
;
5545 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5548 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5549 * compressed, but in practice they compress down to about
5552 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;
5558 * Wait for the given activity and return the status of the wait (whether or not
5559 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5560 * reported via the 'missing' parameter, rather than by printing an error
5561 * message. This is convenient when this function is called in a loop over a
5562 * long period of time (as it is, for example, by zfs's wait cmd). In that
5563 * scenario, a fs being exported or destroyed should be considered a normal
5564 * event, so we don't want to print an error when we find that the fs doesn't
5568 zfs_wait_status(zfs_handle_t
*zhp
, zfs_wait_activity_t activity
,
5569 boolean_t
*missing
, boolean_t
*waited
)
5571 int error
= lzc_wait_fs(zhp
->zfs_name
, activity
, waited
);
5572 *missing
= (error
== ENOENT
);
5577 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
5578 dgettext(TEXT_DOMAIN
, "error waiting in fs '%s'"),