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>
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
55 #include <directory.h>
56 #endif /* HAVE_IDMAP */
58 #include <sys/dnode.h>
61 #include <sys/dsl_crypt.h>
65 #include "zfs_namecheck.h"
67 #include "libzfs_impl.h"
68 #include "zfs_deleg.h"
70 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
71 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
74 * Given a single type (not a mask of types), return the type in a human
78 zfs_type_to_name(zfs_type_t type
)
81 case ZFS_TYPE_FILESYSTEM
:
82 return (dgettext(TEXT_DOMAIN
, "filesystem"));
83 case ZFS_TYPE_SNAPSHOT
:
84 return (dgettext(TEXT_DOMAIN
, "snapshot"));
86 return (dgettext(TEXT_DOMAIN
, "volume"));
88 return (dgettext(TEXT_DOMAIN
, "pool"));
89 case ZFS_TYPE_BOOKMARK
:
90 return (dgettext(TEXT_DOMAIN
, "bookmark"));
92 assert(!"unhandled zfs_type_t");
99 * Validate a ZFS path. This is used even before trying to open the dataset, to
100 * provide a more meaningful error message. We call zfs_error_aux() to
101 * explain exactly why the name was not valid.
104 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
110 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "snapshot delimiter '@' is not expected here"));
117 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
120 "missing '@' delimiter in snapshot name"));
124 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
126 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
127 "bookmark delimiter '#' is not expected here"));
131 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
134 "missing '#' delimiter in bookmark name"));
138 if (modifying
&& strchr(path
, '%') != NULL
) {
140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
141 "invalid character %c in name"), '%');
145 if (entity_namecheck(path
, &why
, &what
) != 0) {
148 case NAME_ERR_TOOLONG
:
149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
150 "name is too long"));
153 case NAME_ERR_LEADING_SLASH
:
154 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
155 "leading slash in name"));
158 case NAME_ERR_EMPTY_COMPONENT
:
159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
160 "empty component or misplaced '@'"
161 " or '#' delimiter in name"));
164 case NAME_ERR_TRAILING_SLASH
:
165 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
166 "trailing slash in name"));
169 case NAME_ERR_INVALCHAR
:
171 dgettext(TEXT_DOMAIN
, "invalid character "
172 "'%c' in name"), what
);
175 case NAME_ERR_MULTIPLE_DELIMITERS
:
176 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
177 "multiple '@' and/or '#' delimiters in "
181 case NAME_ERR_NOLETTER
:
182 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
183 "pool doesn't begin with a letter"));
186 case NAME_ERR_RESERVED
:
187 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
188 "name is reserved"));
191 case NAME_ERR_DISKLIKE
:
192 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
193 "reserved disk name"));
196 case NAME_ERR_SELF_REF
:
197 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
198 "self reference, '.' is found in name"));
201 case NAME_ERR_PARENT_REF
:
202 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
203 "parent reference, '..' is found in name"));
207 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
208 "(%d) not defined"), why
);
220 zfs_name_valid(const char *name
, zfs_type_t type
)
222 if (type
== ZFS_TYPE_POOL
)
223 return (zpool_name_valid(NULL
, B_FALSE
, name
));
224 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
228 * This function takes the raw DSL properties, and filters out the user-defined
229 * properties into a separate nvlist.
232 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
234 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
239 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
240 (void) no_memory(hdl
);
245 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
246 if (!zfs_prop_user(nvpair_name(elem
)))
249 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
250 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
252 (void) no_memory(hdl
);
260 static zpool_handle_t
*
261 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
263 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
266 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
267 if (hdl
->libzfs_pool_handles
!= NULL
)
268 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
269 hdl
->libzfs_pool_handles
= zph
;
274 static zpool_handle_t
*
275 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
277 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
278 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
280 while ((zph
!= NULL
) &&
281 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
282 zph
= zph
->zpool_next
;
287 * Returns a handle to the pool that contains the provided dataset.
288 * If a handle to that pool already exists then that handle is returned.
289 * Otherwise, a new handle is created and added to the list of handles.
291 static zpool_handle_t
*
292 zpool_handle(zfs_handle_t
*zhp
)
298 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
299 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
300 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
302 zph
= zpool_find_handle(zhp
, pool_name
, len
);
304 zph
= zpool_add_handle(zhp
, pool_name
);
311 zpool_free_handles(libzfs_handle_t
*hdl
)
313 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
315 while (zph
!= NULL
) {
316 next
= zph
->zpool_next
;
320 hdl
->libzfs_pool_handles
= NULL
;
324 * Utility function to gather stats (objset and zpl) for the given object.
327 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
329 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
331 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
333 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
334 if (errno
== ENOMEM
) {
335 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
346 * Utility function to get the received properties of the given object.
349 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
351 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
352 nvlist_t
*recvdprops
;
353 zfs_cmd_t zc
= {"\0"};
356 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
359 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
361 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
362 if (errno
== ENOMEM
) {
363 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
367 zcmd_free_nvlists(&zc
);
372 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
373 zcmd_free_nvlists(&zc
);
377 nvlist_free(zhp
->zfs_recvd_props
);
378 zhp
->zfs_recvd_props
= recvdprops
;
384 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
386 nvlist_t
*allprops
, *userprops
;
388 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
390 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
395 * XXX Why do we store the user props separately, in addition to
396 * storing them in zfs_props?
398 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
399 nvlist_free(allprops
);
403 nvlist_free(zhp
->zfs_props
);
404 nvlist_free(zhp
->zfs_user_props
);
406 zhp
->zfs_props
= allprops
;
407 zhp
->zfs_user_props
= userprops
;
413 get_stats(zfs_handle_t
*zhp
)
416 zfs_cmd_t zc
= {"\0"};
418 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
420 if (get_stats_ioctl(zhp
, &zc
) != 0)
422 else if (put_stats_zhdl(zhp
, &zc
) != 0)
424 zcmd_free_nvlists(&zc
);
429 * Refresh the properties currently stored in the handle.
432 zfs_refresh_properties(zfs_handle_t
*zhp
)
434 (void) get_stats(zhp
);
438 * Makes a handle from the given dataset name. Used by zfs_open() and
439 * zfs_iter_* to create child handles on the fly.
442 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
444 if (put_stats_zhdl(zhp
, zc
) != 0)
448 * We've managed to open the dataset and gather statistics. Determine
449 * the high-level type.
451 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
452 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
453 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
454 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
455 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
460 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
461 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
462 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
463 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
464 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
465 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
467 abort(); /* we should never see any other types */
469 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
476 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
478 zfs_cmd_t zc
= {"\0"};
480 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
486 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
487 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
491 if (get_stats_ioctl(zhp
, &zc
) == -1) {
492 zcmd_free_nvlists(&zc
);
496 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
500 zcmd_free_nvlists(&zc
);
505 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
507 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
513 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
514 if (make_dataset_handle_common(zhp
, zc
) == -1) {
522 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
524 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
529 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
530 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
531 zhp
->zfs_head_type
= pzhp
->zfs_type
;
532 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
533 zhp
->zpool_hdl
= zpool_handle(zhp
);
539 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
541 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
546 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
547 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
548 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
549 sizeof (zhp
->zfs_name
));
550 zhp
->zfs_type
= zhp_orig
->zfs_type
;
551 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
552 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
553 if (zhp_orig
->zfs_props
!= NULL
) {
554 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
555 (void) no_memory(zhp
->zfs_hdl
);
560 if (zhp_orig
->zfs_user_props
!= NULL
) {
561 if (nvlist_dup(zhp_orig
->zfs_user_props
,
562 &zhp
->zfs_user_props
, 0) != 0) {
563 (void) no_memory(zhp
->zfs_hdl
);
568 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
569 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
570 &zhp
->zfs_recvd_props
, 0)) {
571 (void) no_memory(zhp
->zfs_hdl
);
576 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
577 if (zhp_orig
->zfs_mntopts
!= NULL
) {
578 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
579 zhp_orig
->zfs_mntopts
);
581 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
586 zfs_bookmark_exists(const char *path
)
590 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
596 (void) strlcpy(fsname
, path
, sizeof (fsname
));
597 pound
= strchr(fsname
, '#');
602 bmark_name
= pound
+ 1;
603 props
= fnvlist_alloc();
604 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
611 rv
= nvlist_exists(bmarks
, bmark_name
);
617 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
618 nvlist_t
*bmark_props
)
620 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
625 /* Fill in the name. */
626 zhp
->zfs_hdl
= parent
->zfs_hdl
;
627 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
629 /* Set the property lists. */
630 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
636 zhp
->zfs_head_type
= parent
->zfs_head_type
;
637 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
639 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
640 nvlist_free(zhp
->zfs_props
);
648 struct zfs_open_bookmarks_cb_data
{
654 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
656 struct zfs_open_bookmarks_cb_data
*dp
= data
;
659 * Is it the one we are looking for?
661 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
663 * We found it. Save it and let the caller know we are done.
670 * Not found. Close the handle and ask for another one.
677 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
678 * argument is a mask of acceptable types. The function will print an
679 * appropriate error message and return NULL if it can't be opened.
682 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
688 (void) snprintf(errbuf
, sizeof (errbuf
),
689 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
692 * Validate the name before we even try to open it.
694 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
695 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
700 * Bookmarks needs to be handled separately.
702 bookp
= strchr(path
, '#');
705 * Try to get stats for the dataset, which will tell us if it
709 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
710 (void) zfs_standard_error(hdl
, errno
, errbuf
);
714 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
716 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
719 * We need to cut out '#' and everything after '#'
720 * to get the parent dataset name only.
722 assert(bookp
- path
< sizeof (dsname
));
723 (void) strncpy(dsname
, path
, bookp
- path
);
724 dsname
[bookp
- path
] = '\0';
727 * Create handle for the parent dataset.
730 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
731 (void) zfs_standard_error(hdl
, errno
, errbuf
);
736 * Iterate bookmarks to find the right one.
739 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
740 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
741 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
745 if (cb_data
.zhp
== NULL
) {
746 (void) zfs_standard_error(hdl
, errno
, errbuf
);
758 if (!(types
& zhp
->zfs_type
)) {
759 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
768 * Release a ZFS handle. Nothing to do but free the associated memory.
771 zfs_close(zfs_handle_t
*zhp
)
773 if (zhp
->zfs_mntopts
)
774 free(zhp
->zfs_mntopts
);
775 nvlist_free(zhp
->zfs_props
);
776 nvlist_free(zhp
->zfs_user_props
);
777 nvlist_free(zhp
->zfs_recvd_props
);
781 typedef struct mnttab_node
{
782 struct mnttab mtn_mt
;
787 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
789 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
790 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
793 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
795 return (TREE_ISIGN(rv
));
799 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
801 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
802 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
803 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
804 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
808 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
812 /* Reopen MNTTAB to prevent reading stale data from open file */
813 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
816 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
820 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
823 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
824 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
825 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
826 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
827 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
829 /* Exclude duplicate mounts */
830 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
831 free(mtn
->mtn_mt
.mnt_special
);
832 free(mtn
->mtn_mt
.mnt_mountp
);
833 free(mtn
->mtn_mt
.mnt_fstype
);
834 free(mtn
->mtn_mt
.mnt_mntopts
);
839 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
846 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
851 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
853 free(mtn
->mtn_mt
.mnt_special
);
854 free(mtn
->mtn_mt
.mnt_mountp
);
855 free(mtn
->mtn_mt
.mnt_fstype
);
856 free(mtn
->mtn_mt
.mnt_mntopts
);
859 avl_destroy(&hdl
->libzfs_mnttab_cache
);
860 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
864 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
866 hdl
->libzfs_mnttab_enable
= enable
;
870 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
871 struct mnttab
*entry
)
877 if (!hdl
->libzfs_mnttab_enable
) {
878 struct mnttab srch
= { 0 };
880 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
881 libzfs_mnttab_fini(hdl
);
883 /* Reopen MNTTAB to prevent reading stale data from open file */
884 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
887 srch
.mnt_special
= (char *)fsname
;
888 srch
.mnt_fstype
= MNTTYPE_ZFS
;
889 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
895 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
896 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
899 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
900 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
905 find
.mtn_mt
.mnt_special
= (char *)fsname
;
906 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
908 *entry
= mtn
->mtn_mt
;
911 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
916 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
917 const char *mountp
, const char *mntopts
)
921 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
922 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
923 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
924 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
925 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
926 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
927 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
929 * Another thread may have already added this entry
930 * via libzfs_mnttab_update. If so we should skip it.
932 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
) {
933 free(mtn
->mtn_mt
.mnt_special
);
934 free(mtn
->mtn_mt
.mnt_mountp
);
935 free(mtn
->mtn_mt
.mnt_fstype
);
936 free(mtn
->mtn_mt
.mnt_mntopts
);
939 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
942 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
946 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
951 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
952 find
.mtn_mt
.mnt_special
= (char *)fsname
;
953 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
955 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
956 free(ret
->mtn_mt
.mnt_special
);
957 free(ret
->mtn_mt
.mnt_mountp
);
958 free(ret
->mtn_mt
.mnt_fstype
);
959 free(ret
->mtn_mt
.mnt_mntopts
);
962 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
966 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
968 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
970 if (zpool_handle
== NULL
)
973 *spa_version
= zpool_get_prop_int(zpool_handle
,
974 ZPOOL_PROP_VERSION
, NULL
);
979 * The choice of reservation property depends on the SPA version.
982 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
986 if (zfs_spa_version(zhp
, &spa_version
) < 0)
989 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
990 *resv_prop
= ZFS_PROP_REFRESERVATION
;
992 *resv_prop
= ZFS_PROP_RESERVATION
;
998 * Given an nvlist of properties to set, validates that they are correct, and
999 * parses any numeric properties (index, boolean, etc) if they are specified as
1003 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1004 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1005 boolean_t key_params_ok
, const char *errbuf
)
1012 int chosen_normal
= -1;
1013 int chosen_utf
= -1;
1015 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1016 (void) no_memory(hdl
);
1021 * Make sure this property is valid and applies to this type.
1025 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1026 const char *propname
= nvpair_name(elem
);
1028 prop
= zfs_name_to_prop(propname
);
1029 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
1031 * This is a user property: make sure it's a
1032 * string, and that it's less than ZAP_MAXNAMELEN.
1034 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1035 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1036 "'%s' must be a string"), propname
);
1037 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1041 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1043 "property name '%s' is too long"),
1045 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1049 (void) nvpair_value_string(elem
, &strval
);
1050 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1051 (void) no_memory(hdl
);
1058 * Currently, only user properties can be modified on
1061 if (type
== ZFS_TYPE_SNAPSHOT
) {
1062 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1063 "this property can not be modified for snapshots"));
1064 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1068 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1069 zfs_userquota_prop_t uqtype
;
1070 char *newpropname
= NULL
;
1076 if (userquota_propname_decode(propname
, zoned
,
1077 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1079 dgettext(TEXT_DOMAIN
,
1080 "'%s' has an invalid user/group name"),
1082 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1086 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1087 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1088 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1089 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1090 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1091 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1093 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1095 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1100 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1101 (void) nvpair_value_string(elem
, &strval
);
1102 if (strcmp(strval
, "none") == 0) {
1104 } else if (zfs_nicestrtonum(hdl
,
1105 strval
, &intval
) != 0) {
1106 (void) zfs_error(hdl
,
1107 EZFS_BADPROP
, errbuf
);
1110 } else if (nvpair_type(elem
) ==
1112 (void) nvpair_value_uint64(elem
, &intval
);
1114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1115 "use 'none' to disable "
1116 "{user|group|project}quota"));
1120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1121 "'%s' must be a number"), propname
);
1122 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1127 * Encode the prop name as
1128 * userquota@<hex-rid>-domain, to make it easy
1129 * for the kernel to decode.
1131 rc
= asprintf(&newpropname
, "%s%llx-%s",
1132 zfs_userquota_prop_prefixes
[uqtype
],
1133 (longlong_t
)rid
, domain
);
1134 if (rc
== -1 || newpropname
== NULL
) {
1135 (void) no_memory(hdl
);
1142 if (nvlist_add_uint64_array(ret
, newpropname
,
1145 (void) no_memory(hdl
);
1150 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1152 "'%s' is readonly"),
1154 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1158 if (prop
== ZPROP_INVAL
) {
1159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1160 "invalid property '%s'"), propname
);
1161 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1165 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1167 dgettext(TEXT_DOMAIN
, "'%s' does not "
1168 "apply to datasets of this type"), propname
);
1169 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1173 if (zfs_prop_readonly(prop
) &&
1174 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1175 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1177 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1179 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1183 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1184 &strval
, &intval
, errbuf
) != 0)
1188 * Perform some additional checks for specific properties.
1191 case ZFS_PROP_VERSION
:
1197 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1198 if (intval
< version
) {
1199 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1200 "Can not downgrade; already at version %u"),
1202 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1208 case ZFS_PROP_VOLBLOCKSIZE
:
1209 case ZFS_PROP_RECORDSIZE
:
1211 int maxbs
= SPA_MAXBLOCKSIZE
;
1214 if (zpool_hdl
!= NULL
) {
1215 maxbs
= zpool_get_prop_int(zpool_hdl
,
1216 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1219 * The value must be a power of two between
1220 * SPA_MINBLOCKSIZE and maxbs.
1222 if (intval
< SPA_MINBLOCKSIZE
||
1223 intval
> maxbs
|| !ISP2(intval
)) {
1224 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1225 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1226 "'%s' must be power of 2 from 512B "
1227 "to %s"), propname
, buf
);
1228 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1234 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1236 int maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1239 if (zpool_hdl
!= NULL
) {
1240 char state
[64] = "";
1242 maxbs
= zpool_get_prop_int(zpool_hdl
,
1243 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1246 * Issue a warning but do not fail so that
1247 * tests for settable properties succeed.
1249 if (zpool_prop_get_feature(zpool_hdl
,
1250 "feature@allocation_classes", state
,
1251 sizeof (state
)) != 0 ||
1252 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1253 (void) fprintf(stderr
, gettext(
1254 "%s: property requires a special "
1255 "device in the pool\n"), propname
);
1259 (intval
< SPA_MINBLOCKSIZE
||
1260 intval
> maxbs
|| !ISP2(intval
))) {
1261 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1262 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1263 "invalid '%s=%d' property: must be zero or "
1264 "a power of 2 from 512B to %s"), propname
,
1266 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1272 case ZFS_PROP_MLSLABEL
:
1274 #ifdef HAVE_MLSLABEL
1276 * Verify the mlslabel string and convert to
1277 * internal hex label string.
1281 char *hex
= NULL
; /* internal label string */
1283 /* Default value is already OK. */
1284 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1287 /* Verify the label can be converted to binary form */
1288 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1289 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1290 L_NO_CORRECTION
, NULL
) == -1)) {
1294 /* Now translate to hex internal label string */
1295 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1301 m_label_free(new_sl
);
1303 /* If string is already in internal form, we're done. */
1304 if (strcmp(strval
, hex
) == 0) {
1309 /* Replace the label string with the internal form. */
1310 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1312 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1319 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1320 "invalid mlslabel '%s'"), strval
);
1321 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1322 m_label_free(new_sl
); /* OK if null */
1325 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1326 "mlslabels are unsupported"));
1327 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1329 #endif /* HAVE_MLSLABEL */
1332 case ZFS_PROP_MOUNTPOINT
:
1334 namecheck_err_t why
;
1336 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1337 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1340 if (mountpoint_namecheck(strval
, &why
)) {
1342 case NAME_ERR_LEADING_SLASH
:
1344 dgettext(TEXT_DOMAIN
,
1345 "'%s' must be an absolute path, "
1346 "'none', or 'legacy'"), propname
);
1348 case NAME_ERR_TOOLONG
:
1350 dgettext(TEXT_DOMAIN
,
1351 "component of '%s' is too long"),
1357 dgettext(TEXT_DOMAIN
,
1358 "(%d) not defined"),
1362 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1369 case ZFS_PROP_SHARESMB
:
1370 case ZFS_PROP_SHARENFS
:
1372 * For the mountpoint and sharenfs or sharesmb
1373 * properties, check if it can be set in a
1374 * global/non-global zone based on
1375 * the zoned property value:
1377 * global zone non-global zone
1378 * --------------------------------------------------
1379 * zoned=on mountpoint (no) mountpoint (yes)
1380 * sharenfs (no) sharenfs (no)
1381 * sharesmb (no) sharesmb (no)
1383 * zoned=off mountpoint (yes) N/A
1388 if (getzoneid() == GLOBAL_ZONEID
) {
1389 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1390 "'%s' cannot be set on "
1391 "dataset in a non-global zone"),
1393 (void) zfs_error(hdl
, EZFS_ZONED
,
1396 } else if (prop
== ZFS_PROP_SHARENFS
||
1397 prop
== ZFS_PROP_SHARESMB
) {
1398 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1399 "'%s' cannot be set in "
1400 "a non-global zone"), propname
);
1401 (void) zfs_error(hdl
, EZFS_ZONED
,
1405 } else if (getzoneid() != GLOBAL_ZONEID
) {
1407 * If zoned property is 'off', this must be in
1408 * a global zone. If not, something is wrong.
1410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1411 "'%s' cannot be set while dataset "
1412 "'zoned' property is set"), propname
);
1413 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1418 * At this point, it is legitimate to set the
1419 * property. Now we want to make sure that the
1420 * property value is valid if it is sharenfs.
1422 if ((prop
== ZFS_PROP_SHARENFS
||
1423 prop
== ZFS_PROP_SHARESMB
) &&
1424 strcmp(strval
, "on") != 0 &&
1425 strcmp(strval
, "off") != 0) {
1426 zfs_share_proto_t proto
;
1428 if (prop
== ZFS_PROP_SHARESMB
)
1433 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1434 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1435 "'%s' cannot be set to invalid "
1436 "options"), propname
);
1437 (void) zfs_error(hdl
, EZFS_BADPROP
,
1445 case ZFS_PROP_KEYLOCATION
:
1446 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1447 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1448 "invalid keylocation"));
1449 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1455 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1457 if (crypt
== ZIO_CRYPT_OFF
&&
1458 strcmp(strval
, "none") != 0) {
1459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1460 "keylocation must be 'none' "
1461 "for unencrypted datasets"));
1462 (void) zfs_error(hdl
, EZFS_BADPROP
,
1465 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1466 strcmp(strval
, "none") == 0) {
1467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1468 "keylocation must not be 'none' "
1469 "for encrypted datasets"));
1470 (void) zfs_error(hdl
, EZFS_BADPROP
,
1477 case ZFS_PROP_PBKDF2_ITERS
:
1478 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1480 "minimum pbkdf2 iterations is %u"),
1481 MIN_PBKDF2_ITERATIONS
);
1482 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1487 case ZFS_PROP_UTF8ONLY
:
1488 chosen_utf
= (int)intval
;
1491 case ZFS_PROP_NORMALIZE
:
1492 chosen_normal
= (int)intval
;
1500 * For changes to existing volumes, we have some additional
1501 * checks to enforce.
1503 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1504 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1505 ZFS_PROP_VOLBLOCKSIZE
);
1509 case ZFS_PROP_VOLSIZE
:
1510 if (intval
% blocksize
!= 0) {
1511 zfs_nicebytes(blocksize
, buf
,
1513 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1514 "'%s' must be a multiple of "
1515 "volume block size (%s)"),
1517 (void) zfs_error(hdl
, EZFS_BADPROP
,
1523 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1524 "'%s' cannot be zero"),
1526 (void) zfs_error(hdl
, EZFS_BADPROP
,
1537 /* check encryption properties */
1539 int64_t crypt
= zfs_prop_get_int(zhp
,
1540 ZFS_PROP_ENCRYPTION
);
1543 case ZFS_PROP_COPIES
:
1544 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1545 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1546 "encrypted datasets cannot have "
1548 (void) zfs_error(hdl
, EZFS_BADPROP
,
1560 * If normalization was chosen, but no UTF8 choice was made,
1561 * enforce rejection of non-UTF8 names.
1563 * If normalization was chosen, but rejecting non-UTF8 names
1564 * was explicitly not chosen, it is an error.
1566 if (chosen_normal
> 0 && chosen_utf
< 0) {
1567 if (nvlist_add_uint64(ret
,
1568 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1569 (void) no_memory(hdl
);
1572 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1573 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1574 "'%s' must be set 'on' if normalization chosen"),
1575 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1576 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1587 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1589 uint64_t old_volsize
;
1590 uint64_t new_volsize
;
1591 uint64_t old_reservation
;
1592 uint64_t new_reservation
;
1593 zfs_prop_t resv_prop
;
1595 zpool_handle_t
*zph
= zpool_handle(zhp
);
1598 * If this is an existing volume, and someone is setting the volsize,
1599 * make sure that it matches the reservation, or add it if necessary.
1601 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1602 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1604 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1606 props
= fnvlist_alloc();
1607 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1608 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1610 if ((zvol_volsize_to_reservation(zph
, old_volsize
, props
) !=
1611 old_reservation
) || nvlist_exists(nvl
,
1612 zfs_prop_to_name(resv_prop
))) {
1613 fnvlist_free(props
);
1616 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1617 &new_volsize
) != 0) {
1618 fnvlist_free(props
);
1621 new_reservation
= zvol_volsize_to_reservation(zph
, new_volsize
, props
);
1622 fnvlist_free(props
);
1624 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1625 new_reservation
) != 0) {
1626 (void) no_memory(zhp
->zfs_hdl
);
1633 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1634 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1635 * Return codes must match zfs_add_synthetic_resv().
1638 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1645 if (!ZFS_IS_VOLUME(zhp
)) {
1649 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1653 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1657 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1658 /* No value being set, so it can't be "auto" */
1661 if (resvsize
!= UINT64_MAX
) {
1662 /* Being set to a value other than "auto" */
1666 props
= fnvlist_alloc();
1668 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1669 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1671 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1673 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1676 resvsize
= zvol_volsize_to_reservation(zpool_handle(zhp
), volsize
,
1678 fnvlist_free(props
);
1680 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1681 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1682 (void) no_memory(zhp
->zfs_hdl
);
1689 zfs_is_namespace_prop(zfs_prop_t prop
)
1693 case ZFS_PROP_ATIME
:
1694 case ZFS_PROP_RELATIME
:
1695 case ZFS_PROP_DEVICES
:
1697 case ZFS_PROP_SETUID
:
1698 case ZFS_PROP_READONLY
:
1699 case ZFS_PROP_XATTR
:
1700 case ZFS_PROP_NBMAND
:
1709 * Given a property name and value, set the property for the given dataset.
1712 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1716 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1717 nvlist_t
*nvl
= NULL
;
1719 (void) snprintf(errbuf
, sizeof (errbuf
),
1720 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1723 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1724 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1725 (void) no_memory(hdl
);
1729 ret
= zfs_prop_set_list(zhp
, nvl
);
1739 * Given an nvlist of property names and values, set the properties for the
1743 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1745 zfs_cmd_t zc
= {"\0"};
1747 prop_changelist_t
**cls
= NULL
;
1750 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1754 zfs_prop_t prop
= 0;
1757 (void) snprintf(errbuf
, sizeof (errbuf
),
1758 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1761 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1762 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1763 B_FALSE
, errbuf
)) == NULL
)
1767 * We have to check for any extra properties which need to be added
1768 * before computing the length of the nvlist.
1770 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1772 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1773 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1774 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1779 if (added_resv
!= 1 &&
1780 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1785 * Check how many properties we're setting and allocate an array to
1786 * store changelist pointers for postfix().
1788 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1790 elem
= nvlist_next_nvpair(nvl
, elem
))
1792 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1796 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1798 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1800 prop
= zfs_name_to_prop(nvpair_name(elem
));
1802 assert(cl_idx
< nvl_len
);
1804 * We don't want to unmount & remount the dataset when changing
1805 * its canmount property to 'on' or 'noauto'. We only use
1806 * the changelist logic to unmount when setting canmount=off.
1808 if (prop
!= ZFS_PROP_CANMOUNT
||
1809 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1810 zfs_is_mounted(zhp
, NULL
))) {
1811 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1812 if (cls
[cl_idx
] == NULL
)
1816 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1817 changelist_haszonedchild(cls
[cl_idx
])) {
1818 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1819 "child dataset with inherited mountpoint is used "
1820 "in a non-global zone"));
1821 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1825 if (cls
[cl_idx
] != NULL
&&
1826 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1831 assert(cl_idx
== nvl_len
);
1834 * Execute the corresponding ioctl() to set this list of properties.
1836 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1838 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1839 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1842 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1845 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1846 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1850 /* Get the list of unset properties back and report them. */
1851 nvlist_t
*errorprops
= NULL
;
1852 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1854 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1856 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1857 prop
= zfs_name_to_prop(nvpair_name(elem
));
1858 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1860 nvlist_free(errorprops
);
1862 if (added_resv
&& errno
== ENOSPC
) {
1863 /* clean up the volsize property we tried to set */
1864 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1868 zcmd_free_nvlists(&zc
);
1870 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1872 if (nvlist_add_uint64(nvl
,
1873 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1876 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1878 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1881 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1882 if (cls
[cl_idx
] != NULL
) {
1883 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1891 * Refresh the statistics so the new property
1892 * value is reflected.
1894 (void) get_stats(zhp
);
1897 * Remount the filesystem to propagate the change
1898 * if one of the options handled by the generic
1899 * Linux namespace layer has been modified.
1901 if (zfs_is_namespace_prop(prop
) &&
1902 zfs_is_mounted(zhp
, NULL
))
1903 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1909 zcmd_free_nvlists(&zc
);
1911 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1912 if (cls
[cl_idx
] != NULL
)
1913 changelist_free(cls
[cl_idx
]);
1921 * Given a property, inherit the value from the parent dataset, or if received
1922 * is TRUE, revert to the received value, if any.
1925 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1927 zfs_cmd_t zc
= {"\0"};
1929 prop_changelist_t
*cl
;
1930 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1934 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1935 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1937 zc
.zc_cookie
= received
;
1938 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1940 * For user properties, the amount of work we have to do is very
1941 * small, so just do it here.
1943 if (!zfs_prop_user(propname
)) {
1944 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1945 "invalid property"));
1946 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1949 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1950 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1952 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1953 return (zfs_standard_error(hdl
, errno
, errbuf
));
1959 * Verify that this property is inheritable.
1961 if (zfs_prop_readonly(prop
))
1962 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1964 if (!zfs_prop_inheritable(prop
) && !received
)
1965 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1968 * Check to see if the value applies to this type
1970 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1971 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1974 * Normalize the name, to get rid of shorthand abbreviations.
1976 propname
= zfs_prop_to_name(prop
);
1977 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1978 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1980 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1981 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1982 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1983 "dataset is used in a non-global zone"));
1984 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1988 * Determine datasets which will be affected by this change, if any.
1990 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1993 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1994 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1995 "child dataset with inherited mountpoint is used "
1996 "in a non-global zone"));
1997 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2001 if ((ret
= changelist_prefix(cl
)) != 0)
2004 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2005 return (zfs_standard_error(hdl
, errno
, errbuf
));
2008 if ((ret
= changelist_postfix(cl
)) != 0)
2012 * Refresh the statistics so the new property is reflected.
2014 (void) get_stats(zhp
);
2017 * Remount the filesystem to propagate the change
2018 * if one of the options handled by the generic
2019 * Linux namespace layer has been modified.
2021 if (zfs_is_namespace_prop(prop
) &&
2022 zfs_is_mounted(zhp
, NULL
))
2023 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2027 changelist_free(cl
);
2032 * True DSL properties are stored in an nvlist. The following two functions
2033 * extract them appropriately.
2036 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2042 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2043 zfs_prop_to_name(prop
), &nv
) == 0) {
2044 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2045 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2047 verify(!zhp
->zfs_props_table
||
2048 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2049 value
= zfs_prop_default_numeric(prop
);
2057 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2063 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2064 zfs_prop_to_name(prop
), &nv
) == 0) {
2065 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2066 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2068 verify(!zhp
->zfs_props_table
||
2069 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2070 value
= zfs_prop_default_string(prop
);
2078 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2080 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2084 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2086 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2087 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2091 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2093 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2098 * Internal function for getting a numeric property. Both zfs_prop_get() and
2099 * zfs_prop_get_int() are built using this interface.
2101 * Certain properties can be overridden using 'mount -o'. In this case, scan
2102 * the contents of the /proc/self/mounts entry, searching for the
2103 * appropriate options. If they differ from the on-disk values, report the
2104 * current values and mark the source "temporary".
2107 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2108 char **source
, uint64_t *val
)
2110 zfs_cmd_t zc
= {"\0"};
2111 nvlist_t
*zplprops
= NULL
;
2113 char *mntopt_on
= NULL
;
2114 char *mntopt_off
= NULL
;
2115 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2120 * If the property is being fetched for a snapshot, check whether
2121 * the property is valid for the snapshot's head dataset type.
2123 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2124 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2125 *val
= zfs_prop_default_numeric(prop
);
2130 case ZFS_PROP_ATIME
:
2131 mntopt_on
= MNTOPT_ATIME
;
2132 mntopt_off
= MNTOPT_NOATIME
;
2135 case ZFS_PROP_RELATIME
:
2136 mntopt_on
= MNTOPT_RELATIME
;
2137 mntopt_off
= MNTOPT_NORELATIME
;
2140 case ZFS_PROP_DEVICES
:
2141 mntopt_on
= MNTOPT_DEVICES
;
2142 mntopt_off
= MNTOPT_NODEVICES
;
2146 mntopt_on
= MNTOPT_EXEC
;
2147 mntopt_off
= MNTOPT_NOEXEC
;
2150 case ZFS_PROP_READONLY
:
2151 mntopt_on
= MNTOPT_RO
;
2152 mntopt_off
= MNTOPT_RW
;
2155 case ZFS_PROP_SETUID
:
2156 mntopt_on
= MNTOPT_SETUID
;
2157 mntopt_off
= MNTOPT_NOSETUID
;
2160 case ZFS_PROP_XATTR
:
2161 mntopt_on
= MNTOPT_XATTR
;
2162 mntopt_off
= MNTOPT_NOXATTR
;
2165 case ZFS_PROP_NBMAND
:
2166 mntopt_on
= MNTOPT_NBMAND
;
2167 mntopt_off
= MNTOPT_NONBMAND
;
2175 * Because looking up the mount options is potentially expensive
2176 * (iterating over all of /proc/self/mounts), we defer its
2177 * calculation until we're looking up a property which requires
2180 if (!zhp
->zfs_mntcheck
&&
2181 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2182 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2183 struct mnttab entry
;
2185 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2186 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2188 if (zhp
->zfs_mntopts
== NULL
)
2192 zhp
->zfs_mntcheck
= B_TRUE
;
2195 if (zhp
->zfs_mntopts
== NULL
)
2196 mnt
.mnt_mntopts
= "";
2198 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2201 case ZFS_PROP_ATIME
:
2202 case ZFS_PROP_RELATIME
:
2203 case ZFS_PROP_DEVICES
:
2205 case ZFS_PROP_READONLY
:
2206 case ZFS_PROP_SETUID
:
2208 case ZFS_PROP_XATTR
:
2210 case ZFS_PROP_NBMAND
:
2211 *val
= getprop_uint64(zhp
, prop
, source
);
2216 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2219 *src
= ZPROP_SRC_TEMPORARY
;
2220 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2223 *src
= ZPROP_SRC_TEMPORARY
;
2227 case ZFS_PROP_CANMOUNT
:
2228 case ZFS_PROP_VOLSIZE
:
2229 case ZFS_PROP_QUOTA
:
2230 case ZFS_PROP_REFQUOTA
:
2231 case ZFS_PROP_RESERVATION
:
2232 case ZFS_PROP_REFRESERVATION
:
2233 case ZFS_PROP_FILESYSTEM_LIMIT
:
2234 case ZFS_PROP_SNAPSHOT_LIMIT
:
2235 case ZFS_PROP_FILESYSTEM_COUNT
:
2236 case ZFS_PROP_SNAPSHOT_COUNT
:
2237 *val
= getprop_uint64(zhp
, prop
, source
);
2239 if (*source
== NULL
) {
2240 /* not default, must be local */
2241 *source
= zhp
->zfs_name
;
2245 case ZFS_PROP_MOUNTED
:
2246 *val
= (zhp
->zfs_mntopts
!= NULL
);
2249 case ZFS_PROP_NUMCLONES
:
2250 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2253 case ZFS_PROP_VERSION
:
2254 case ZFS_PROP_NORMALIZE
:
2255 case ZFS_PROP_UTF8ONLY
:
2257 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2259 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2260 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2261 zcmd_free_nvlists(&zc
);
2262 if (prop
== ZFS_PROP_VERSION
&&
2263 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2264 *val
= zfs_prop_default_numeric(prop
);
2267 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2268 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2270 zcmd_free_nvlists(&zc
);
2273 nvlist_free(zplprops
);
2274 zcmd_free_nvlists(&zc
);
2277 case ZFS_PROP_INCONSISTENT
:
2278 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2281 case ZFS_PROP_REDACTED
:
2282 *val
= zhp
->zfs_dmustats
.dds_redacted
;
2286 switch (zfs_prop_get_type(prop
)) {
2287 case PROP_TYPE_NUMBER
:
2288 case PROP_TYPE_INDEX
:
2289 *val
= getprop_uint64(zhp
, prop
, source
);
2291 * If we tried to use a default value for a
2292 * readonly property, it means that it was not
2293 * present. Note this only applies to "truly"
2294 * readonly properties, not set-once properties
2295 * like volblocksize.
2297 if (zfs_prop_readonly(prop
) &&
2298 !zfs_prop_setonce(prop
) &&
2299 *source
!= NULL
&& (*source
)[0] == '\0') {
2305 case PROP_TYPE_STRING
:
2307 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2308 "cannot get non-numeric property"));
2309 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2310 dgettext(TEXT_DOMAIN
, "internal error")));
2318 * Calculate the source type, given the raw source string.
2321 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2322 char *statbuf
, size_t statlen
)
2324 if (statbuf
== NULL
||
2325 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2329 if (source
== NULL
) {
2330 *srctype
= ZPROP_SRC_NONE
;
2331 } else if (source
[0] == '\0') {
2332 *srctype
= ZPROP_SRC_DEFAULT
;
2333 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2334 *srctype
= ZPROP_SRC_RECEIVED
;
2336 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2337 *srctype
= ZPROP_SRC_LOCAL
;
2339 (void) strlcpy(statbuf
, source
, statlen
);
2340 *srctype
= ZPROP_SRC_INHERITED
;
2347 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2348 size_t proplen
, boolean_t literal
)
2353 if (zhp
->zfs_recvd_props
== NULL
)
2354 if (get_recvd_props_ioctl(zhp
) != 0)
2357 prop
= zfs_name_to_prop(propname
);
2359 if (prop
!= ZPROP_INVAL
) {
2361 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2363 zfs_set_recvd_props_mode(zhp
, &cookie
);
2364 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2365 NULL
, NULL
, 0, literal
);
2366 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2370 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2371 propname
, &propval
) != 0)
2373 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2375 (void) strlcpy(propbuf
, recvdval
, proplen
);
2378 return (err
== 0 ? 0 : -1);
2382 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2387 value
= zfs_get_clones_nvl(zhp
);
2392 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2393 pair
= nvlist_next_nvpair(value
, pair
)) {
2394 if (propbuf
[0] != '\0')
2395 (void) strlcat(propbuf
, ",", proplen
);
2396 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2402 struct get_clones_arg
{
2406 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2410 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2412 struct get_clones_arg
*gca
= arg
;
2414 if (gca
->numclones
== 0) {
2419 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2420 NULL
, NULL
, 0, B_TRUE
) != 0)
2422 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2423 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2428 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2434 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2436 nvlist_t
*nv
, *value
;
2438 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2439 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2440 struct get_clones_arg gca
;
2443 * if this is a snapshot, then the kernel wasn't able
2444 * to get the clones. Do it by slowly iterating.
2446 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2448 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2450 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2455 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2457 gca
.origin
= zhp
->zfs_name
;
2459 if (gca
.numclones
!= 0) {
2461 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2464 /* get the pool name */
2465 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2466 (void) strsep(&cp
, "/@");
2467 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2468 ZFS_TYPE_FILESYSTEM
);
2475 (void) get_clones_cb(root
, &gca
);
2478 if (gca
.numclones
!= 0 ||
2479 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2480 nvlist_add_nvlist(zhp
->zfs_props
,
2481 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2488 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2489 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2492 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2498 get_rsnaps_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2504 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2505 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
), &value
) != 0)
2507 if (nvlist_lookup_uint64_array(value
, ZPROP_VALUE
, &snaps
,
2511 /* There's no redaction snapshots; pass a special value back */
2512 (void) snprintf(propbuf
, proplen
, "none");
2516 for (int i
= 0; i
< nsnaps
; i
++) {
2518 if (propbuf
[0] != '\0')
2519 (void) strlcat(propbuf
, ",", proplen
);
2520 (void) snprintf(buf
, sizeof (buf
), "%llu",
2521 (u_longlong_t
)snaps
[i
]);
2522 (void) strlcat(propbuf
, buf
, proplen
);
2529 * Accepts a property and value and checks that the value
2530 * matches the one found by the channel program. If they are
2531 * not equal, print both of them.
2534 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2537 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2540 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2541 const char *prop_name
= zfs_prop_to_name(prop
);
2542 const char *program
=
2544 "ds = args['dataset']\n"
2545 "prop = args['property']\n"
2546 "value, setpoint = zfs.get_prop(ds, prop)\n"
2547 "return {value=value, setpoint=setpoint}\n";
2550 nvlist_t
*argnvl
= fnvlist_alloc();
2552 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2553 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2555 error
= lzc_channel_program_nosync(poolname
, program
,
2556 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2559 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2560 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2562 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2564 (void) fprintf(stderr
, "%s: zcp check error: "
2565 "%u\n", prop_name
, error
);
2568 if (ans
!= intval
) {
2569 (void) fprintf(stderr
, "%s: zfs found %llu, "
2570 "but zcp found %llu\n", prop_name
,
2571 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2575 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2577 (void) fprintf(stderr
, "%s: zcp check error: "
2578 "%u\n", prop_name
, error
);
2581 if (strcmp(strval
, str_ans
) != 0) {
2582 (void) fprintf(stderr
,
2583 "%s: zfs found '%s', but zcp found '%s'\n",
2584 prop_name
, strval
, str_ans
);
2588 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2589 "error: %u\n", prop_name
, error
);
2591 nvlist_free(argnvl
);
2592 nvlist_free(outnvl
);
2596 * Retrieve a property from the given object. If 'literal' is specified, then
2597 * numbers are left as exact values. Otherwise, numbers are converted to a
2598 * human-readable form.
2600 * Returns 0 on success, or -1 on error.
2603 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2604 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2606 char *source
= NULL
;
2610 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2613 * Check to see if this property applies to our object
2615 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2618 if (received
&& zfs_prop_readonly(prop
))
2622 *src
= ZPROP_SRC_NONE
;
2625 case ZFS_PROP_CREATION
:
2627 * 'creation' is a time_t stored in the statistics. We convert
2628 * this into a string unless 'literal' is specified.
2631 val
= getprop_uint64(zhp
, prop
, &source
);
2632 time_t time
= (time_t)val
;
2636 localtime_r(&time
, &t
) == NULL
||
2637 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2639 (void) snprintf(propbuf
, proplen
, "%llu",
2642 zcp_check(zhp
, prop
, val
, NULL
);
2645 case ZFS_PROP_MOUNTPOINT
:
2647 * Getting the precise mountpoint can be tricky.
2649 * - for 'none' or 'legacy', return those values.
2650 * - for inherited mountpoints, we want to take everything
2651 * after our ancestor and append it to the inherited value.
2653 * If the pool has an alternate root, we want to prepend that
2654 * root to any values we return.
2657 str
= getprop_string(zhp
, prop
, &source
);
2659 if (str
[0] == '/') {
2660 char buf
[MAXPATHLEN
];
2662 const char *relpath
;
2665 * If we inherit the mountpoint, even from a dataset
2666 * with a received value, the source will be the path of
2667 * the dataset we inherit from. If source is
2668 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2671 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2674 relpath
= zhp
->zfs_name
+ strlen(source
);
2675 if (relpath
[0] == '/')
2679 if ((zpool_get_prop(zhp
->zpool_hdl
,
2680 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2681 B_FALSE
)) || (strcmp(root
, "-") == 0))
2684 * Special case an alternate root of '/'. This will
2685 * avoid having multiple leading slashes in the
2688 if (strcmp(root
, "/") == 0)
2692 * If the mountpoint is '/' then skip over this
2693 * if we are obtaining either an alternate root or
2694 * an inherited mountpoint.
2696 if (str
[1] == '\0' && (root
[0] != '\0' ||
2697 relpath
[0] != '\0'))
2700 if (relpath
[0] == '\0')
2701 (void) snprintf(propbuf
, proplen
, "%s%s",
2704 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2705 root
, str
, relpath
[0] == '@' ? "" : "/",
2708 /* 'legacy' or 'none' */
2709 (void) strlcpy(propbuf
, str
, proplen
);
2711 zcp_check(zhp
, prop
, 0, propbuf
);
2714 case ZFS_PROP_ORIGIN
:
2715 str
= getprop_string(zhp
, prop
, &source
);
2718 (void) strlcpy(propbuf
, str
, proplen
);
2719 zcp_check(zhp
, prop
, 0, str
);
2722 case ZFS_PROP_REDACT_SNAPS
:
2723 if (get_rsnaps_string(zhp
, propbuf
, proplen
) != 0)
2727 case ZFS_PROP_CLONES
:
2728 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2732 case ZFS_PROP_QUOTA
:
2733 case ZFS_PROP_REFQUOTA
:
2734 case ZFS_PROP_RESERVATION
:
2735 case ZFS_PROP_REFRESERVATION
:
2737 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2740 * If quota or reservation is 0, we translate this into 'none'
2741 * (unless literal is set), and indicate that it's the default
2742 * value. Otherwise, we print the number nicely and indicate
2743 * that its set locally.
2747 (void) strlcpy(propbuf
, "0", proplen
);
2749 (void) strlcpy(propbuf
, "none", proplen
);
2752 (void) snprintf(propbuf
, proplen
, "%llu",
2755 zfs_nicebytes(val
, propbuf
, proplen
);
2757 zcp_check(zhp
, prop
, val
, NULL
);
2760 case ZFS_PROP_FILESYSTEM_LIMIT
:
2761 case ZFS_PROP_SNAPSHOT_LIMIT
:
2762 case ZFS_PROP_FILESYSTEM_COUNT
:
2763 case ZFS_PROP_SNAPSHOT_COUNT
:
2765 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2769 * If limit is UINT64_MAX, we translate this into 'none' (unless
2770 * literal is set), and indicate that it's the default value.
2771 * Otherwise, we print the number nicely and indicate that it's
2775 (void) snprintf(propbuf
, proplen
, "%llu",
2777 } else if (val
== UINT64_MAX
) {
2778 (void) strlcpy(propbuf
, "none", proplen
);
2780 zfs_nicenum(val
, propbuf
, proplen
);
2783 zcp_check(zhp
, prop
, val
, NULL
);
2786 case ZFS_PROP_REFRATIO
:
2787 case ZFS_PROP_COMPRESSRATIO
:
2788 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2791 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2792 (u_longlong_t
)(val
/ 100),
2793 (u_longlong_t
)(val
% 100));
2795 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2796 (u_longlong_t
)(val
/ 100),
2797 (u_longlong_t
)(val
% 100));
2798 zcp_check(zhp
, prop
, val
, NULL
);
2802 switch (zhp
->zfs_type
) {
2803 case ZFS_TYPE_FILESYSTEM
:
2806 case ZFS_TYPE_VOLUME
:
2809 case ZFS_TYPE_SNAPSHOT
:
2812 case ZFS_TYPE_BOOKMARK
:
2818 (void) snprintf(propbuf
, proplen
, "%s", str
);
2819 zcp_check(zhp
, prop
, 0, propbuf
);
2822 case ZFS_PROP_MOUNTED
:
2824 * The 'mounted' property is a pseudo-property that described
2825 * whether the filesystem is currently mounted. Even though
2826 * it's a boolean value, the typical values of "on" and "off"
2827 * don't make sense, so we translate to "yes" and "no".
2829 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2830 src
, &source
, &val
) != 0)
2833 (void) strlcpy(propbuf
, "yes", proplen
);
2835 (void) strlcpy(propbuf
, "no", proplen
);
2840 * The 'name' property is a pseudo-property derived from the
2841 * dataset name. It is presented as a real property to simplify
2844 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2845 zcp_check(zhp
, prop
, 0, propbuf
);
2848 case ZFS_PROP_MLSLABEL
:
2850 #ifdef HAVE_MLSLABEL
2851 m_label_t
*new_sl
= NULL
;
2852 char *ascii
= NULL
; /* human readable label */
2854 (void) strlcpy(propbuf
,
2855 getprop_string(zhp
, prop
, &source
), proplen
);
2857 if (literal
|| (strcasecmp(propbuf
,
2858 ZFS_MLSLABEL_DEFAULT
) == 0))
2862 * Try to translate the internal hex string to
2863 * human-readable output. If there are any
2864 * problems just use the hex string.
2867 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2868 L_NO_CORRECTION
, NULL
) == -1) {
2869 m_label_free(new_sl
);
2873 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2877 m_label_free(new_sl
);
2880 m_label_free(new_sl
);
2882 (void) strlcpy(propbuf
, ascii
, proplen
);
2885 (void) strlcpy(propbuf
,
2886 getprop_string(zhp
, prop
, &source
), proplen
);
2887 #endif /* HAVE_MLSLABEL */
2892 case ZFS_PROP_CREATETXG
:
2893 case ZFS_PROP_OBJSETID
:
2894 case ZFS_PROP_PBKDF2_ITERS
:
2896 * These properties are stored as numbers, but they are
2897 * identifiers or counters.
2898 * We don't want them to be pretty printed, because pretty
2899 * printing truncates their values making them useless.
2901 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2903 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2904 zcp_check(zhp
, prop
, val
, NULL
);
2907 case ZFS_PROP_REFERENCED
:
2908 case ZFS_PROP_AVAILABLE
:
2910 case ZFS_PROP_USEDSNAP
:
2911 case ZFS_PROP_USEDDS
:
2912 case ZFS_PROP_USEDREFRESERV
:
2913 case ZFS_PROP_USEDCHILD
:
2914 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2917 (void) snprintf(propbuf
, proplen
, "%llu",
2920 zfs_nicebytes(val
, propbuf
, proplen
);
2922 zcp_check(zhp
, prop
, val
, NULL
);
2926 switch (zfs_prop_get_type(prop
)) {
2927 case PROP_TYPE_NUMBER
:
2928 if (get_numeric_property(zhp
, prop
, src
,
2929 &source
, &val
) != 0) {
2934 (void) snprintf(propbuf
, proplen
, "%llu",
2937 zfs_nicenum(val
, propbuf
, proplen
);
2939 zcp_check(zhp
, prop
, val
, NULL
);
2942 case PROP_TYPE_STRING
:
2943 str
= getprop_string(zhp
, prop
, &source
);
2947 (void) strlcpy(propbuf
, str
, proplen
);
2948 zcp_check(zhp
, prop
, 0, str
);
2951 case PROP_TYPE_INDEX
:
2952 if (get_numeric_property(zhp
, prop
, src
,
2953 &source
, &val
) != 0)
2955 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2958 (void) strlcpy(propbuf
, strval
, proplen
);
2959 zcp_check(zhp
, prop
, 0, strval
);
2967 get_source(zhp
, src
, source
, statbuf
, statlen
);
2973 * Utility function to get the given numeric property. Does no validation that
2974 * the given property is the appropriate type; should only be used with
2975 * hard-coded property types.
2978 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2983 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2989 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2993 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2994 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2998 * Similar to zfs_prop_get(), but returns the value as an integer.
3001 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3002 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3007 * Check to see if this property applies to our object
3009 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3010 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3011 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3012 zfs_prop_to_name(prop
)));
3016 *src
= ZPROP_SRC_NONE
;
3018 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3021 get_source(zhp
, src
, source
, statbuf
, statlen
);
3028 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3029 char **domainp
, idmap_rid_t
*ridp
)
3031 idmap_get_handle_t
*get_hdl
= NULL
;
3035 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3039 err
= idmap_get_sidbyuid(get_hdl
, id
,
3040 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3042 err
= idmap_get_sidbygid(get_hdl
, id
,
3043 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3045 if (err
== IDMAP_SUCCESS
&&
3046 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3047 status
== IDMAP_SUCCESS
)
3053 idmap_get_destroy(get_hdl
);
3056 #endif /* HAVE_IDMAP */
3059 * convert the propname into parameters needed by kernel
3060 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3061 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3062 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3063 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3064 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3065 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3068 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3069 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3071 zfs_userquota_prop_t type
;
3075 boolean_t isproject
;
3081 /* Figure out the property type ({user|group|project}{quota|space}) */
3082 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3083 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3084 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3087 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3091 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3092 type
== ZFS_PROP_USEROBJQUOTA
||
3093 type
== ZFS_PROP_USEROBJUSED
);
3094 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3095 type
== ZFS_PROP_GROUPOBJQUOTA
||
3096 type
== ZFS_PROP_GROUPOBJUSED
);
3097 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3098 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3099 type
== ZFS_PROP_PROJECTOBJUSED
);
3101 cp
= strchr(propname
, '@') + 1;
3103 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3104 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3107 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3108 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3111 } else if (!isproject
&& strchr(cp
, '@')) {
3114 * It's a SID name (eg "user@domain") that needs to be
3115 * turned into S-1-domainID-RID.
3117 directory_error_t e
;
3118 char *numericsid
= NULL
;
3121 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3124 e
= directory_sid_from_user_name(NULL
,
3127 e
= directory_sid_from_group_name(NULL
,
3131 directory_error_free(e
);
3134 if (numericsid
== NULL
)
3137 (void) strlcpy(domain
, cp
, domainlen
);
3138 cp
= strrchr(domain
, '-');
3143 *ridp
= strtoull(cp
, &end
, 10);
3146 if (errno
!= 0 || *end
!= '\0')
3150 #endif /* HAVE_IDMAP */
3152 /* It's a user/group/project ID (eg "12345"). */
3155 id
= strtoul(cp
, &end
, 10);
3158 if (id
> MAXUID
&& !isproject
) {
3160 /* It's an ephemeral ID. */
3164 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3165 &mapdomain
, &rid
) != 0)
3167 (void) strlcpy(domain
, mapdomain
, domainlen
);
3171 #endif /* HAVE_IDMAP */
3181 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3182 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3185 zfs_cmd_t zc
= {"\0"};
3187 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3189 err
= userquota_propname_decode(propname
,
3190 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3191 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3192 zc
.zc_objset_type
= *typep
;
3196 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3200 *propvalue
= zc
.zc_cookie
;
3205 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3206 uint64_t *propvalue
)
3208 zfs_userquota_prop_t type
;
3210 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3215 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3216 char *propbuf
, int proplen
, boolean_t literal
)
3220 zfs_userquota_prop_t type
;
3222 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3229 (void) snprintf(propbuf
, proplen
, "%llu",
3230 (u_longlong_t
)propvalue
);
3231 } else if (propvalue
== 0 &&
3232 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3233 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3234 type
== ZFS_PROP_PROJECTQUOTA
||
3235 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3236 (void) strlcpy(propbuf
, "none", proplen
);
3237 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3238 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3239 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3240 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3242 zfs_nicenum(propvalue
, propbuf
, proplen
);
3248 * propname must start with "written@" or "written#".
3251 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3252 uint64_t *propvalue
)
3255 zfs_cmd_t zc
= {"\0"};
3256 const char *snapname
;
3258 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3260 assert(zfs_prop_written(propname
));
3261 snapname
= propname
+ strlen("written@");
3262 if (strchr(snapname
, '@') != NULL
|| strchr(snapname
, '#') != NULL
) {
3263 /* full snapshot or bookmark name specified */
3264 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3266 /* snapname is the short name, append it to zhp's fsname */
3269 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3270 sizeof (zc
.zc_value
));
3271 cp
= strchr(zc
.zc_value
, '@');
3274 (void) strlcat(zc
.zc_value
, snapname
- 1, sizeof (zc
.zc_value
));
3277 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3281 *propvalue
= zc
.zc_cookie
;
3286 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3287 char *propbuf
, int proplen
, boolean_t literal
)
3292 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3298 (void) snprintf(propbuf
, proplen
, "%llu",
3299 (u_longlong_t
)propvalue
);
3301 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3308 * Returns the name of the given zfs handle.
3311 zfs_get_name(const zfs_handle_t
*zhp
)
3313 return (zhp
->zfs_name
);
3317 * Returns the name of the parent pool for the given zfs handle.
3320 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3322 return (zhp
->zpool_hdl
->zpool_name
);
3326 * Returns the type of the given zfs handle.
3329 zfs_get_type(const zfs_handle_t
*zhp
)
3331 return (zhp
->zfs_type
);
3335 * Is one dataset name a child dataset of another?
3337 * Needs to handle these cases:
3338 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3339 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3340 * Descendant? No. No. No. Yes.
3343 is_descendant(const char *ds1
, const char *ds2
)
3345 size_t d1len
= strlen(ds1
);
3347 /* ds2 can't be a descendant if it's smaller */
3348 if (strlen(ds2
) < d1len
)
3351 /* otherwise, compare strings and verify that there's a '/' char */
3352 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3356 * Given a complete name, return just the portion that refers to the parent.
3357 * Will return -1 if there is no parent (path is just the name of the
3361 parent_name(const char *path
, char *buf
, size_t buflen
)
3365 (void) strlcpy(buf
, path
, buflen
);
3367 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3375 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3377 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3381 * If accept_ancestor is false, then check to make sure that the given path has
3382 * a parent, and that it exists. If accept_ancestor is true, then find the
3383 * closest existing ancestor for the given path. In prefixlen return the
3384 * length of already existing prefix of the given path. We also fetch the
3385 * 'zoned' property, which is used to validate property settings when creating
3389 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3390 boolean_t accept_ancestor
, int *prefixlen
)
3392 zfs_cmd_t zc
= {"\0"};
3393 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3399 (void) snprintf(errbuf
, sizeof (errbuf
),
3400 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3402 /* get parent, and check to see if this is just a pool */
3403 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3404 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3405 "missing dataset name"));
3406 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3409 /* check to see if the pool exists */
3410 if ((slash
= strchr(parent
, '/')) == NULL
)
3411 slash
= parent
+ strlen(parent
);
3412 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3413 zc
.zc_name
[slash
- parent
] = '\0';
3414 if (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3416 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3417 "no such pool '%s'"), zc
.zc_name
);
3418 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3421 /* check to see if the parent dataset exists */
3422 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3423 if (errno
== ENOENT
&& accept_ancestor
) {
3425 * Go deeper to find an ancestor, give up on top level.
3427 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3428 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3429 "no such pool '%s'"), zc
.zc_name
);
3430 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3432 } else if (errno
== ENOENT
) {
3433 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3434 "parent does not exist"));
3435 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3437 return (zfs_standard_error(hdl
, errno
, errbuf
));
3440 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3444 /* we are in a non-global zone, but parent is in the global zone */
3445 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3446 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3451 /* make sure parent is a filesystem */
3452 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3453 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3454 "parent is not a filesystem"));
3455 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3461 if (prefixlen
!= NULL
)
3462 *prefixlen
= strlen(parent
);
3467 * Finds whether the dataset of the given type(s) exists.
3470 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3474 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3478 * Try to get stats for the dataset, which will tell us if it exists.
3480 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3481 int ds_type
= zhp
->zfs_type
;
3484 if (types
& ds_type
)
3491 * Given a path to 'target', create all the ancestors between
3492 * the prefixlen portion of the path, and the target itself.
3493 * Fail if the initial prefixlen-ancestor does not already exist.
3496 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3502 /* make sure prefix exists */
3503 cp
= target
+ prefixlen
;
3505 assert(strchr(cp
, '/') == NULL
);
3506 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3509 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3517 * Attempt to create, mount, and share any ancestor filesystems,
3518 * up to the prefixlen-long one.
3520 for (cp
= target
+ prefixlen
+ 1;
3521 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3525 h
= make_dataset_handle(hdl
, target
);
3527 /* it already exists, nothing to do here */
3532 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3534 opname
= dgettext(TEXT_DOMAIN
, "create");
3538 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3540 opname
= dgettext(TEXT_DOMAIN
, "open");
3544 if (zfs_mount(h
, NULL
, 0) != 0) {
3545 opname
= dgettext(TEXT_DOMAIN
, "mount");
3549 if (zfs_share(h
) != 0) {
3550 opname
= dgettext(TEXT_DOMAIN
, "share");
3556 zfs_commit_all_shares();
3561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3562 "failed to %s ancestor '%s'"), opname
, target
);
3567 * Creates non-existing ancestors of the given path.
3570 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3577 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3578 "cannot create '%s'"), path
);
3581 * Check that we are not passing the nesting limit
3582 * before we start creating any ancestors.
3584 if (dataset_nestcheck(path
) != 0) {
3585 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3586 "maximum name nesting depth exceeded"));
3587 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3590 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3593 if ((path_copy
= strdup(path
)) != NULL
) {
3594 rc
= create_parents(hdl
, path_copy
, prefix
);
3597 if (path_copy
== NULL
|| rc
!= 0)
3604 * Create a new filesystem or volume.
3607 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3612 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3614 enum lzc_dataset_type ost
;
3615 zpool_handle_t
*zpool_handle
;
3616 uint8_t *wkeydata
= NULL
;
3619 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3621 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3622 "cannot create '%s'"), path
);
3624 /* validate the path, taking care to note the extended error message */
3625 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3626 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3628 if (dataset_nestcheck(path
) != 0) {
3629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3630 "maximum name nesting depth exceeded"));
3631 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3634 /* validate parents exist */
3635 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3639 * The failure modes when creating a dataset of a different type over
3640 * one that already exists is a little strange. In particular, if you
3641 * try to create a dataset on top of an existing dataset, the ioctl()
3642 * will return ENOENT, not EEXIST. To prevent this from happening, we
3643 * first try to see if the dataset exists.
3645 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3646 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3647 "dataset already exists"));
3648 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3651 if (type
== ZFS_TYPE_VOLUME
)
3652 ost
= LZC_DATSET_TYPE_ZVOL
;
3654 ost
= LZC_DATSET_TYPE_ZFS
;
3656 /* open zpool handle for prop validation */
3657 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3658 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3660 /* truncate pool_path at first slash */
3661 char *p
= strchr(pool_path
, '/');
3665 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3668 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3669 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3670 zpool_close(zpool_handle
);
3673 zpool_close(zpool_handle
);
3675 if (type
== ZFS_TYPE_VOLUME
) {
3677 * If we are creating a volume, the size and block size must
3678 * satisfy a few restraints. First, the blocksize must be a
3679 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3680 * volsize must be a multiple of the block size, and cannot be
3683 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3684 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3686 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3687 "missing volume size"));
3688 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3691 if ((ret
= nvlist_lookup_uint64(props
,
3692 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3693 &blocksize
)) != 0) {
3694 if (ret
== ENOENT
) {
3695 blocksize
= zfs_prop_default_numeric(
3696 ZFS_PROP_VOLBLOCKSIZE
);
3699 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3700 "missing volume block size"));
3701 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3707 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3708 "volume size cannot be zero"));
3709 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3712 if (size
% blocksize
!= 0) {
3714 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3715 "volume size must be a multiple of volume block "
3717 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3721 (void) parent_name(path
, parent
, sizeof (parent
));
3722 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3723 &wkeydata
, &wkeylen
) != 0) {
3725 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3728 /* create the dataset */
3729 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3731 if (wkeydata
!= NULL
)
3734 /* check for failure */
3738 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3739 "no such parent '%s'"), parent
);
3740 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3744 "pool must be upgraded to set this "
3745 "property or value"));
3746 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3749 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3750 "encryption root's key is not loaded "
3752 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3755 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3756 "invalid property value(s) specified"));
3757 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3761 * This platform can't address a volume this big.
3763 if (type
== ZFS_TYPE_VOLUME
)
3764 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3769 return (zfs_standard_error(hdl
, errno
, errbuf
));
3777 * Destroys the given dataset. The caller must make sure that the filesystem
3778 * isn't mounted, and that there are no active dependents. If the file system
3779 * does not exist this function does nothing.
3782 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3786 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3789 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3790 nvlist_t
*nv
= fnvlist_alloc();
3791 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3792 error
= lzc_destroy_bookmarks(nv
, NULL
);
3795 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3796 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3802 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3803 nvlist_t
*nv
= fnvlist_alloc();
3804 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3805 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3808 error
= lzc_destroy(zhp
->zfs_name
);
3811 if (error
!= 0 && error
!= ENOENT
) {
3812 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3813 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3817 remove_mountpoint(zhp
);
3822 struct destroydata
{
3824 const char *snapname
;
3828 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3830 struct destroydata
*dd
= arg
;
3831 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3834 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3835 dd
->snapname
) >= sizeof (name
))
3838 if (lzc_exists(name
))
3839 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3841 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3847 * Destroys all snapshots with the given name in zhp & descendants.
3850 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3853 struct destroydata dd
= { 0 };
3855 dd
.snapname
= snapname
;
3856 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3857 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3859 if (nvlist_empty(dd
.nvl
)) {
3860 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3861 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3862 zhp
->zfs_name
, snapname
);
3864 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3866 nvlist_free(dd
.nvl
);
3871 * Destroys all the snapshots named in the nvlist.
3874 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3877 nvlist_t
*errlist
= NULL
;
3880 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3883 nvlist_free(errlist
);
3887 if (nvlist_empty(errlist
)) {
3889 (void) snprintf(errbuf
, sizeof (errbuf
),
3890 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3892 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3894 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3895 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3897 (void) snprintf(errbuf
, sizeof (errbuf
),
3898 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3901 switch (fnvpair_value_int32(pair
)) {
3904 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3905 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3908 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3913 nvlist_free(errlist
);
3918 * Clones the given dataset. The target must be of the same type as the source.
3921 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3923 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3926 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3929 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3931 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3932 "cannot create '%s'"), target
);
3934 /* validate the target/clone name */
3935 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3936 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3938 /* validate parents exist */
3939 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3942 (void) parent_name(target
, parent
, sizeof (parent
));
3949 if (ZFS_IS_VOLUME(zhp
)) {
3950 type
= ZFS_TYPE_VOLUME
;
3952 type
= ZFS_TYPE_FILESYSTEM
;
3954 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3955 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
3957 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
3963 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
3965 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3968 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3976 * The parent doesn't exist. We should have caught this
3977 * above, but there may a race condition that has since
3978 * destroyed the parent.
3980 * At this point, we don't know whether it's the source
3981 * that doesn't exist anymore, or whether the target
3982 * dataset doesn't exist.
3984 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3985 "no such parent '%s'"), parent
);
3986 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3989 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3990 "source and target pools differ"));
3991 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3995 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4004 * Promotes the given clone fs to be the clone parent.
4007 zfs_promote(zfs_handle_t
*zhp
)
4009 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4010 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4014 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4015 "cannot promote '%s'"), zhp
->zfs_name
);
4017 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4018 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4019 "snapshots can not be promoted"));
4020 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4023 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4024 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4025 "not a cloned filesystem"));
4026 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4029 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4030 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4032 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4038 * Promoting encrypted dataset outside its
4041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4042 "cannot promote dataset outside its "
4043 "encryption root"));
4044 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4047 /* There is a conflicting snapshot name. */
4048 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4049 "conflicting snapshot '%s' from parent '%s'"),
4050 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4051 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4054 return (zfs_standard_error(hdl
, ret
, errbuf
));
4060 typedef struct snapdata
{
4062 const char *sd_snapname
;
4066 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4068 snapdata_t
*sd
= arg
;
4069 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4072 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4073 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4074 sd
->sd_snapname
) >= sizeof (name
))
4077 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4079 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4087 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4091 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4097 zpool_handle_t
*zpool_hdl
;
4098 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4100 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4101 "cannot create snapshots "));
4104 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4105 const char *snapname
= nvpair_name(elem
);
4107 /* validate the target name */
4108 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4110 (void) snprintf(errbuf
, sizeof (errbuf
),
4111 dgettext(TEXT_DOMAIN
,
4112 "cannot create snapshot '%s'"), snapname
);
4113 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4118 * get pool handle for prop validation. assumes all snaps are in the
4119 * same pool, as does lzc_snapshot (below).
4121 elem
= nvlist_next_nvpair(snaps
, NULL
);
4122 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4123 pool
[strcspn(pool
, "/@")] = '\0';
4124 zpool_hdl
= zpool_open(hdl
, pool
);
4125 if (zpool_hdl
== NULL
)
4128 if (props
!= NULL
&&
4129 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4130 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4131 zpool_close(zpool_hdl
);
4134 zpool_close(zpool_hdl
);
4136 ret
= lzc_snapshot(snaps
, props
, &errors
);
4139 boolean_t printed
= B_FALSE
;
4140 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4142 elem
= nvlist_next_nvpair(errors
, elem
)) {
4143 (void) snprintf(errbuf
, sizeof (errbuf
),
4144 dgettext(TEXT_DOMAIN
,
4145 "cannot create snapshot '%s'"), nvpair_name(elem
));
4146 (void) zfs_standard_error(hdl
,
4147 fnvpair_value_int32(elem
), errbuf
);
4153 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4154 "multiple snapshots of same "
4156 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4160 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4166 nvlist_free(errors
);
4171 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4175 snapdata_t sd
= { 0 };
4176 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4181 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4182 "cannot snapshot %s"), path
);
4184 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4185 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4187 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4188 cp
= strchr(fsname
, '@');
4190 sd
.sd_snapname
= cp
+ 1;
4192 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4193 ZFS_TYPE_VOLUME
)) == NULL
) {
4197 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4199 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4201 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4204 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4205 nvlist_free(sd
.sd_nvl
);
4211 * Destroy any more recent snapshots. We invoke this callback on any dependents
4212 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4213 * is a dependent and we should just destroy it without checking the transaction
4216 typedef struct rollback_data
{
4217 const char *cb_target
; /* the snapshot */
4218 uint64_t cb_create
; /* creation time reference */
4224 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4226 rollback_data_t
*cbp
= data
;
4227 prop_changelist_t
*clp
;
4229 /* We must destroy this clone; first unmount it */
4230 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4231 cbp
->cb_force
? MS_FORCE
: 0);
4232 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4233 cbp
->cb_error
= B_TRUE
;
4237 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4238 cbp
->cb_error
= B_TRUE
;
4240 changelist_remove(clp
, zhp
->zfs_name
);
4241 (void) changelist_postfix(clp
);
4242 changelist_free(clp
);
4249 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4251 rollback_data_t
*cbp
= data
;
4253 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4254 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4255 rollback_destroy_dependent
, cbp
);
4257 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4265 * Given a dataset, rollback to a specific snapshot, discarding any
4266 * data changes since then and making it the active dataset.
4268 * Any snapshots and bookmarks more recent than the target are
4269 * destroyed, along with their dependents (i.e. clones).
4272 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4274 rollback_data_t cb
= { 0 };
4276 boolean_t restore_resv
= 0;
4277 uint64_t old_volsize
= 0, new_volsize
;
4278 zfs_prop_t resv_prop
= { 0 };
4279 uint64_t min_txg
= 0;
4281 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4282 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4285 * Destroy all recent snapshots and their dependents.
4287 cb
.cb_force
= force
;
4288 cb
.cb_target
= snap
->zfs_name
;
4289 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4291 if (cb
.cb_create
> 0)
4292 min_txg
= cb
.cb_create
;
4294 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
,
4297 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4303 * Now that we have verified that the snapshot is the latest,
4304 * rollback to the given snapshot.
4307 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4308 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4310 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4312 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4316 * Pass both the filesystem and the wanted snapshot names,
4317 * we would get an error back if the snapshot is destroyed or
4318 * a new snapshot is created before this request is processed.
4320 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4324 (void) snprintf(errbuf
, sizeof (errbuf
),
4325 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4329 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4330 "there is a snapshot or bookmark more recent "
4331 "than '%s'"), snap
->zfs_name
);
4332 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4335 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4336 "'%s' is not found among snapshots of '%s'"),
4337 snap
->zfs_name
, zhp
->zfs_name
);
4338 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4341 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4344 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4350 * For volumes, if the pre-rollback volsize matched the pre-
4351 * rollback reservation and the volsize has changed then set
4352 * the reservation property to the post-rollback volsize.
4353 * Make a new handle since the rollback closed the dataset.
4355 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4356 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4358 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4359 if (old_volsize
!= new_volsize
)
4360 err
= zfs_prop_set_int(zhp
, resv_prop
,
4369 * Renames the given dataset.
4372 zfs_rename(zfs_handle_t
*zhp
, const char *target
, renameflags_t flags
)
4375 zfs_cmd_t zc
= {"\0"};
4377 prop_changelist_t
*cl
= NULL
;
4378 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4379 char property
[ZFS_MAXPROPLEN
];
4380 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4383 /* if we have the same exact name, just return success */
4384 if (strcmp(zhp
->zfs_name
, target
) == 0)
4387 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4388 "cannot rename to '%s'"), target
);
4390 /* make sure source name is valid */
4391 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4392 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4395 * Make sure the target name is valid
4397 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4398 if ((strchr(target
, '@') == NULL
) ||
4401 * Snapshot target name is abbreviated,
4402 * reconstruct full dataset name
4404 (void) strlcpy(parent
, zhp
->zfs_name
,
4406 delim
= strchr(parent
, '@');
4407 if (strchr(target
, '@') == NULL
)
4411 (void) strlcat(parent
, target
, sizeof (parent
));
4415 * Make sure we're renaming within the same dataset.
4417 delim
= strchr(target
, '@');
4418 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4419 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4420 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4421 "snapshots must be part of same "
4423 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4428 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4429 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4431 if (flags
.recursive
) {
4432 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4433 "recursive rename must be a snapshot"));
4434 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4437 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4438 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4440 /* validate parents */
4441 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4444 /* make sure we're in the same pool */
4445 verify((delim
= strchr(target
, '/')) != NULL
);
4446 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4447 zhp
->zfs_name
[delim
- target
] != '/') {
4448 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4449 "datasets must be within same pool"));
4450 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4453 /* new name cannot be a child of the current dataset name */
4454 if (is_descendant(zhp
->zfs_name
, target
)) {
4455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4456 "New dataset name cannot be a descendant of "
4457 "current dataset name"));
4458 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4462 (void) snprintf(errbuf
, sizeof (errbuf
),
4463 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4465 if (getzoneid() == GLOBAL_ZONEID
&&
4466 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4468 "dataset is used in a non-global zone"));
4469 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4473 * Avoid unmounting file systems with mountpoint property set to
4474 * 'legacy' or 'none' even if -u option is not given.
4476 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
4477 !flags
.recursive
&& !flags
.nounmount
&&
4478 zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, property
,
4479 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) == 0 &&
4480 (strcmp(property
, "legacy") == 0 ||
4481 strcmp(property
, "none") == 0)) {
4482 flags
.nounmount
= B_TRUE
;
4484 if (flags
.recursive
) {
4485 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4486 if (parentname
== NULL
) {
4490 delim
= strchr(parentname
, '@');
4492 zfs_handle_t
*zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
,
4500 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4501 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4502 flags
.nounmount
? CL_GATHER_DONT_UNMOUNT
:
4503 CL_GATHER_ITER_MOUNTED
,
4504 flags
.forceunmount
? MS_FORCE
: 0)) == NULL
)
4507 if (changelist_haszonedchild(cl
)) {
4508 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4509 "child dataset with inherited mountpoint is used "
4510 "in a non-global zone"));
4511 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4516 if ((ret
= changelist_prefix(cl
)) != 0)
4520 if (ZFS_IS_VOLUME(zhp
))
4521 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4523 zc
.zc_objset_type
= DMU_OST_ZFS
;
4525 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4526 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4528 zc
.zc_cookie
= !!flags
.recursive
;
4529 zc
.zc_cookie
|= (!!flags
.nounmount
) << 1;
4531 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4533 * if it was recursive, the one that actually failed will
4536 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4537 "cannot rename '%s'"), zc
.zc_name
);
4539 if (flags
.recursive
&& errno
== EEXIST
) {
4540 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4541 "a child dataset already has a snapshot "
4542 "with the new name"));
4543 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4544 } else if (errno
== EACCES
) {
4545 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4546 "cannot move encrypted child outside of "
4547 "its encryption root"));
4548 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4550 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4554 * On failure, we still want to remount any filesystems that
4555 * were previously mounted, so we don't alter the system state.
4558 (void) changelist_postfix(cl
);
4561 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4562 ret
= changelist_postfix(cl
);
4568 changelist_free(cl
);
4574 zfs_get_all_props(zfs_handle_t
*zhp
)
4576 return (zhp
->zfs_props
);
4580 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4582 if (zhp
->zfs_recvd_props
== NULL
)
4583 if (get_recvd_props_ioctl(zhp
) != 0)
4585 return (zhp
->zfs_recvd_props
);
4589 zfs_get_user_props(zfs_handle_t
*zhp
)
4591 return (zhp
->zfs_user_props
);
4595 * This function is used by 'zfs list' to determine the exact set of columns to
4596 * display, and their maximum widths. This does two main things:
4598 * - If this is a list of all properties, then expand the list to include
4599 * all native properties, and set a flag so that for each dataset we look
4600 * for new unique user properties and add them to the list.
4602 * - For non fixed-width properties, keep track of the maximum width seen
4603 * so that we can size the column appropriately. If the user has
4604 * requested received property values, we also need to compute the width
4605 * of the RECEIVED column.
4608 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4611 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4612 zprop_list_t
*entry
;
4613 zprop_list_t
**last
, **start
;
4614 nvlist_t
*userprops
, *propval
;
4617 char buf
[ZFS_MAXPROPLEN
];
4619 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4622 userprops
= zfs_get_user_props(zhp
);
4625 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4627 * Go through and add any user properties as necessary. We
4628 * start by incrementing our list pointer to the first
4629 * non-native property.
4632 while (*start
!= NULL
) {
4633 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4635 start
= &(*start
)->pl_next
;
4639 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4641 * See if we've already found this property in our list.
4643 for (last
= start
; *last
!= NULL
;
4644 last
= &(*last
)->pl_next
) {
4645 if (strcmp((*last
)->pl_user_prop
,
4646 nvpair_name(elem
)) == 0)
4650 if (*last
== NULL
) {
4651 if ((entry
= zfs_alloc(hdl
,
4652 sizeof (zprop_list_t
))) == NULL
||
4653 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4654 nvpair_name(elem
)))) == NULL
) {
4659 entry
->pl_prop
= ZPROP_INVAL
;
4660 entry
->pl_width
= strlen(nvpair_name(elem
));
4661 entry
->pl_all
= B_TRUE
;
4668 * Now go through and check the width of any non-fixed columns
4670 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4671 if (entry
->pl_fixed
&& !literal
)
4674 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4675 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4676 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4677 if (strlen(buf
) > entry
->pl_width
)
4678 entry
->pl_width
= strlen(buf
);
4680 if (received
&& zfs_prop_get_recvd(zhp
,
4681 zfs_prop_to_name(entry
->pl_prop
),
4682 buf
, sizeof (buf
), literal
) == 0)
4683 if (strlen(buf
) > entry
->pl_recvd_width
)
4684 entry
->pl_recvd_width
= strlen(buf
);
4686 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4688 verify(nvlist_lookup_string(propval
,
4689 ZPROP_VALUE
, &strval
) == 0);
4690 if (strlen(strval
) > entry
->pl_width
)
4691 entry
->pl_width
= strlen(strval
);
4693 if (received
&& zfs_prop_get_recvd(zhp
,
4694 entry
->pl_user_prop
,
4695 buf
, sizeof (buf
), literal
) == 0)
4696 if (strlen(buf
) > entry
->pl_recvd_width
)
4697 entry
->pl_recvd_width
= strlen(buf
);
4705 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4711 * Keep a reference to the props-table against which we prune the
4714 zhp
->zfs_props_table
= props
;
4716 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4719 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4720 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4723 * User properties will result in ZPROP_INVAL, and since we
4724 * only know how to prune standard ZFS properties, we always
4725 * leave these in the list. This can also happen if we
4726 * encounter an unknown DSL property (when running older
4727 * software, for example).
4729 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4730 (void) nvlist_remove(zhp
->zfs_props
,
4731 nvpair_name(curr
), nvpair_type(curr
));
4737 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4738 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4740 zfs_cmd_t zc
= {"\0"};
4741 nvlist_t
*nvlist
= NULL
;
4744 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4745 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4746 zc
.zc_cookie
= (uint64_t)cmd
;
4748 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4749 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4750 (void) no_memory(hdl
);
4756 case ZFS_SMB_ACL_ADD
:
4757 case ZFS_SMB_ACL_REMOVE
:
4758 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4760 case ZFS_SMB_ACL_RENAME
:
4761 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4763 (void) no_memory(hdl
);
4766 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4768 (void) no_memory(hdl
);
4771 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4772 nvlist_free(nvlist
);
4776 case ZFS_SMB_ACL_PURGE
:
4781 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4782 nvlist_free(nvlist
);
4787 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4788 char *path
, char *resource
)
4790 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4795 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4796 char *path
, char *resource
)
4798 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4803 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4805 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4810 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4811 char *oldname
, char *newname
)
4813 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4818 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4819 zfs_userspace_cb_t func
, void *arg
)
4821 zfs_cmd_t zc
= {"\0"};
4822 zfs_useracct_t buf
[100];
4823 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4826 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4828 zc
.zc_objset_type
= type
;
4829 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4832 zfs_useracct_t
*zua
= buf
;
4834 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4835 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4838 if ((errno
== ENOTSUP
&&
4839 (type
== ZFS_PROP_USEROBJUSED
||
4840 type
== ZFS_PROP_GROUPOBJUSED
||
4841 type
== ZFS_PROP_USEROBJQUOTA
||
4842 type
== ZFS_PROP_GROUPOBJQUOTA
||
4843 type
== ZFS_PROP_PROJECTOBJUSED
||
4844 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4845 type
== ZFS_PROP_PROJECTUSED
||
4846 type
== ZFS_PROP_PROJECTQUOTA
)))
4849 (void) snprintf(errbuf
, sizeof (errbuf
),
4850 dgettext(TEXT_DOMAIN
,
4851 "cannot get used/quota for %s"), zc
.zc_name
);
4852 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4854 if (zc
.zc_nvlist_dst_size
== 0)
4857 while (zc
.zc_nvlist_dst_size
> 0) {
4858 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4859 zua
->zu_space
)) != 0)
4862 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4871 const char *snapname
;
4873 boolean_t recursive
;
4878 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4880 struct holdarg
*ha
= arg
;
4881 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4884 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4885 ha
->snapname
) >= sizeof (name
))
4888 if (lzc_exists(name
))
4889 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4892 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4898 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4899 boolean_t recursive
, int cleanup_fd
)
4904 ha
.nvl
= fnvlist_alloc();
4905 ha
.snapname
= snapname
;
4907 ha
.recursive
= recursive
;
4908 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4910 if (nvlist_empty(ha
.nvl
)) {
4913 fnvlist_free(ha
.nvl
);
4915 (void) snprintf(errbuf
, sizeof (errbuf
),
4916 dgettext(TEXT_DOMAIN
,
4917 "cannot hold snapshot '%s@%s'"),
4918 zhp
->zfs_name
, snapname
);
4919 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4923 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4924 fnvlist_free(ha
.nvl
);
4930 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4934 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4939 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4942 /* There may be errors even in the success case. */
4943 fnvlist_free(errors
);
4947 if (nvlist_empty(errors
)) {
4948 /* no hold-specific errors */
4949 (void) snprintf(errbuf
, sizeof (errbuf
),
4950 dgettext(TEXT_DOMAIN
, "cannot hold"));
4953 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4954 "pool must be upgraded"));
4955 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4958 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4961 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4965 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4967 elem
= nvlist_next_nvpair(errors
, elem
)) {
4968 (void) snprintf(errbuf
, sizeof (errbuf
),
4969 dgettext(TEXT_DOMAIN
,
4970 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4971 switch (fnvpair_value_int32(elem
)) {
4974 * Temporary tags wind up having the ds object id
4975 * prepended. So even if we passed the length check
4976 * above, it's still possible for the tag to wind
4977 * up being slightly too long.
4979 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4982 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4985 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4988 (void) zfs_standard_error(hdl
,
4989 fnvpair_value_int32(elem
), errbuf
);
4993 fnvlist_free(errors
);
4998 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5000 struct holdarg
*ha
= arg
;
5001 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5003 nvlist_t
*existing_holds
;
5005 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5006 ha
->snapname
) >= sizeof (name
)) {
5011 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5013 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5016 nvlist_t
*torelease
= fnvlist_alloc();
5017 fnvlist_add_boolean(torelease
, ha
->tag
);
5018 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5019 fnvlist_free(torelease
);
5023 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5029 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5030 boolean_t recursive
)
5034 nvlist_t
*errors
= NULL
;
5036 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5039 ha
.nvl
= fnvlist_alloc();
5040 ha
.snapname
= snapname
;
5042 ha
.recursive
= recursive
;
5044 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5046 if (nvlist_empty(ha
.nvl
)) {
5047 fnvlist_free(ha
.nvl
);
5049 (void) snprintf(errbuf
, sizeof (errbuf
),
5050 dgettext(TEXT_DOMAIN
,
5051 "cannot release hold from snapshot '%s@%s'"),
5052 zhp
->zfs_name
, snapname
);
5054 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5056 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5061 ret
= lzc_release(ha
.nvl
, &errors
);
5062 fnvlist_free(ha
.nvl
);
5065 /* There may be errors even in the success case. */
5066 fnvlist_free(errors
);
5070 if (nvlist_empty(errors
)) {
5071 /* no hold-specific errors */
5072 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5076 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5077 "pool must be upgraded"));
5078 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5081 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5085 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5087 elem
= nvlist_next_nvpair(errors
, elem
)) {
5088 (void) snprintf(errbuf
, sizeof (errbuf
),
5089 dgettext(TEXT_DOMAIN
,
5090 "cannot release hold from snapshot '%s'"),
5092 switch (fnvpair_value_int32(elem
)) {
5094 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5097 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5100 (void) zfs_standard_error_fmt(hdl
,
5101 fnvpair_value_int32(elem
), errbuf
);
5105 fnvlist_free(errors
);
5110 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5112 zfs_cmd_t zc
= {"\0"};
5113 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5119 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5120 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5124 nvbuf
= malloc(nvsz
);
5125 if (nvbuf
== NULL
) {
5126 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5130 zc
.zc_nvlist_dst_size
= nvsz
;
5131 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5133 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5135 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5136 (void) snprintf(errbuf
, sizeof (errbuf
),
5137 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5142 nvsz
= zc
.zc_nvlist_dst_size
;
5146 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5147 "pool must be upgraded"));
5148 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5151 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5154 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5157 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5162 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5164 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
5165 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5167 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
5177 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5179 zfs_cmd_t zc
= {"\0"};
5180 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5186 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5187 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5189 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5192 nvbuf
= malloc(nvsz
);
5194 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5197 zc
.zc_nvlist_src_size
= nvsz
;
5198 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5199 zc
.zc_perm_action
= un
;
5201 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5203 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5204 (void) snprintf(errbuf
, sizeof (errbuf
),
5205 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5209 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5210 "pool must be upgraded"));
5211 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5214 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5217 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5220 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5231 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5236 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5239 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5241 (void) snprintf(errbuf
, sizeof (errbuf
),
5242 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5246 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5247 "pool must be upgraded"));
5248 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5251 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5254 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5257 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5266 * The theory of raidz space accounting
5268 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5269 * will "reference" 128KB, even though it allocates more than that, to store the
5270 * parity information (and perhaps skip sectors). This concept of the
5271 * "referenced" (and other DMU space accounting) being lower than the allocated
5272 * space by a constant factor is called "raidz deflation."
5274 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5275 * block size. However, zvols typically have a much smaller block size (default
5276 * 8KB). These smaller blocks may require proportionally much more parity
5277 * information (and perhaps skip sectors). In this case, the change to the
5278 * "referenced" property may be much more than the logical block size.
5280 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5283 * +-------+-------+-------+-------+-------+
5284 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5285 * +-------+-------+-------+-------+-------+
5286 * | P0 | D0 | D8 | D16 | D24 |
5287 * | P1 | D1 | D9 | D17 | D25 |
5288 * | P2 | D2 | D10 | D18 | D26 |
5289 * | P3 | D3 | D11 | D19 | D27 |
5290 * | P4 | D4 | D12 | D20 | D28 |
5291 * | P5 | D5 | D13 | D21 | D29 |
5292 * | P6 | D6 | D14 | D22 | D30 |
5293 * | P7 | D7 | D15 | D23 | D31 |
5294 * +-------+-------+-------+-------+-------+
5296 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5297 * sectors. The dataset's referenced will increase by 128k and the pool's
5298 * allocated and free properties will be adjusted by 160k.
5300 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5301 * blank cells represent unallocated space.
5303 * +-------+-------+-------+-------+-------+
5304 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5305 * +-------+-------+-------+-------+-------+
5307 * +-------+-------+-------+-------+-------+
5309 * Above, notice that the 4k block required one sector for parity and another
5310 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5311 * and free properties will be adjusted by 8k. The dataset will not be charged
5312 * 8k. Rather, it will be charged a value that is scaled according to the
5313 * overhead of the 128k block on the same vdev. This 8k allocation will be
5314 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5315 * calculated in the 128k block example above.
5317 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5318 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5319 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5320 * multiple of of 4 sectors. When a block does not fill the required number of
5321 * sectors, skip blocks (sectors) are used.
5323 * An 8k block being written to a raidz vdev may be written as follows:
5325 * +-------+-------+-------+-------+-------+
5326 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5327 * +-------+-------+-------+-------+-------+
5328 * | P0 | D0 | D1 | S0 | |
5329 * +-------+-------+-------+-------+-------+
5331 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5332 * added. For this 8k block, the pool's allocated and free properties are
5333 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5334 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5335 * the 128k block example above.
5337 * The situtation is slightly different for dRAID since the minimum allocation
5338 * size is the full group width. The same 8K block above would be written as
5339 * follows in a dRAID group:
5341 * +-------+-------+-------+-------+-------+
5342 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5343 * +-------+-------+-------+-------+-------+
5344 * | P0 | D0 | D1 | S0 | S1 |
5345 * +-------+-------+-------+-------+-------+
5347 * Compression may lead to a variety of block sizes being written for the same
5348 * volume or file. There is no clear way to reserve just the amount of space
5349 * that will be required, so the worst case (no compression) is assumed.
5350 * Note that metadata blocks will typically be compressed, so the reservation
5351 * size returned by zvol_volsize_to_reservation() will generally be slightly
5352 * larger than the maximum that the volume can reference.
5356 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5357 * amount of space (in bytes) that will be allocated for the specified block
5358 * size. Note that the "referenced" space accounted will be less than this, but
5359 * not necessarily equal to "blksize", due to RAIDZ deflation.
5362 vdev_raidz_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5365 uint64_t asize
, ndata
;
5367 ASSERT3U(ndisks
, >, nparity
);
5368 ndata
= ndisks
- nparity
;
5369 asize
= ((blksize
- 1) >> ashift
) + 1;
5370 asize
+= nparity
* ((asize
+ ndata
- 1) / ndata
);
5371 asize
= roundup(asize
, nparity
+ 1) << ashift
;
5377 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5378 * amount of space (in bytes) that will be allocated for the specified block
5382 vdev_draid_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5385 ASSERT3U(ndisks
, >, nparity
);
5386 uint64_t ndata
= ndisks
- nparity
;
5387 uint64_t rows
= ((blksize
- 1) / (ndata
<< ashift
)) + 1;
5388 uint64_t asize
= (rows
* ndisks
) << ashift
;
5394 * Determine how much space will be allocated if it lands on the most space-
5395 * inefficient top-level vdev. Returns the size in bytes required to store one
5396 * copy of the volume data. See theory comment above.
5399 volsize_from_vdevs(zpool_handle_t
*zhp
, uint64_t nblocks
, uint64_t blksize
)
5401 nvlist_t
*config
, *tree
, **vdevs
;
5405 config
= zpool_get_config(zhp
, NULL
);
5406 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
5407 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
5408 &vdevs
, &nvdevs
) != 0) {
5409 return (nblocks
* blksize
);
5412 for (int v
= 0; v
< nvdevs
; v
++) {
5414 uint64_t nparity
, ashift
, asize
, tsize
;
5417 if (nvlist_lookup_string(vdevs
[v
], ZPOOL_CONFIG_TYPE
,
5421 if (strcmp(type
, VDEV_TYPE_RAIDZ
) != 0 &&
5422 strcmp(type
, VDEV_TYPE_DRAID
) != 0)
5425 if (nvlist_lookup_uint64(vdevs
[v
],
5426 ZPOOL_CONFIG_NPARITY
, &nparity
) != 0)
5429 if (nvlist_lookup_uint64(vdevs
[v
],
5430 ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
5433 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
5437 if (nvlist_lookup_nvlist_array(vdevs
[v
],
5438 ZPOOL_CONFIG_CHILDREN
, &disks
, &ndisks
) != 0)
5441 /* allocation size for the "typical" 128k block */
5442 tsize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5443 SPA_OLD_MAXBLOCKSIZE
);
5445 /* allocation size for the blksize block */
5446 asize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5451 if (nvlist_lookup_uint64(vdevs
[v
],
5452 ZPOOL_CONFIG_DRAID_NDATA
, &ndata
) != 0)
5455 /* allocation size for the "typical" 128k block */
5456 tsize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5457 ashift
, SPA_OLD_MAXBLOCKSIZE
);
5459 /* allocation size for the blksize block */
5460 asize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5465 * Scale this size down as a ratio of 128k / tsize.
5466 * See theory statement above.
5468 volsize
= nblocks
* asize
* SPA_OLD_MAXBLOCKSIZE
/ tsize
;
5469 if (volsize
> ret
) {
5475 ret
= nblocks
* blksize
;
5482 * Convert the zvol's volume size to an appropriate reservation. See theory
5485 * Note: If this routine is updated, it is necessary to update the ZFS test
5486 * suite's shell version in reservation.shlib.
5489 zvol_volsize_to_reservation(zpool_handle_t
*zph
, uint64_t volsize
,
5493 uint64_t nblocks
, volblocksize
;
5497 if (nvlist_lookup_string(props
,
5498 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5499 ncopies
= atoi(strval
);
5502 if (nvlist_lookup_uint64(props
,
5503 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5504 &volblocksize
) != 0)
5505 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5507 nblocks
= volsize
/ volblocksize
;
5509 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5510 * this reason, only the data blocks are scaled based on vdev config.
5512 volsize
= volsize_from_vdevs(zph
, nblocks
, volblocksize
);
5514 /* start with metadnode L0-L6 */
5516 /* calculate number of indirects */
5517 while (nblocks
> 1) {
5518 nblocks
+= DNODES_PER_LEVEL
- 1;
5519 nblocks
/= DNODES_PER_LEVEL
;
5522 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5525 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5526 * compressed, but in practice they compress down to about
5529 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;
5535 * Wait for the given activity and return the status of the wait (whether or not
5536 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5537 * reported via the 'missing' parameter, rather than by printing an error
5538 * message. This is convenient when this function is called in a loop over a
5539 * long period of time (as it is, for example, by zfs's wait cmd). In that
5540 * scenario, a fs being exported or destroyed should be considered a normal
5541 * event, so we don't want to print an error when we find that the fs doesn't
5545 zfs_wait_status(zfs_handle_t
*zhp
, zfs_wait_activity_t activity
,
5546 boolean_t
*missing
, boolean_t
*waited
)
5548 int error
= lzc_wait_fs(zhp
->zfs_name
, activity
, waited
);
5549 *missing
= (error
== ENOENT
);
5554 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
5555 dgettext(TEXT_DOMAIN
, "error waiting in fs '%s'"),