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 https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2019 Joyent, Inc.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017-2018 RackTop Systems.
33 * Copyright (c) 2019 Datto Inc.
34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
35 * Copyright (c) 2021 Matt Fiddaman
48 #include <sys/mntent.h>
49 #include <sys/mount.h>
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
;
238 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
239 (void) no_memory(hdl
);
244 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
245 if (!zfs_prop_user(nvpair_name(elem
)))
248 nvlist_t
*propval
= fnvpair_value_nvlist(elem
);
249 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
251 (void) no_memory(hdl
);
259 static zpool_handle_t
*
260 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
262 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
265 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
266 if (hdl
->libzfs_pool_handles
!= NULL
)
267 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
268 hdl
->libzfs_pool_handles
= zph
;
273 static zpool_handle_t
*
274 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
276 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
277 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
279 while ((zph
!= NULL
) &&
280 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
281 zph
= zph
->zpool_next
;
286 * Returns a handle to the pool that contains the provided dataset.
287 * If a handle to that pool already exists then that handle is returned.
288 * Otherwise, a new handle is created and added to the list of handles.
290 static zpool_handle_t
*
291 zpool_handle(zfs_handle_t
*zhp
)
297 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
298 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
299 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
301 zph
= zpool_find_handle(zhp
, pool_name
, len
);
303 zph
= zpool_add_handle(zhp
, pool_name
);
310 zpool_free_handles(libzfs_handle_t
*hdl
)
312 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
314 while (zph
!= NULL
) {
315 next
= zph
->zpool_next
;
319 hdl
->libzfs_pool_handles
= NULL
;
323 * Utility function to gather stats (objset and zpl) for the given object.
326 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
328 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
330 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
332 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
334 zcmd_expand_dst_nvlist(hdl
, zc
);
342 * Utility function to get the received properties of the given object.
345 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
347 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
348 nvlist_t
*recvdprops
;
349 zfs_cmd_t zc
= {"\0"};
352 zcmd_alloc_dst_nvlist(hdl
, &zc
, 0);
354 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
356 while (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
358 zcmd_expand_dst_nvlist(hdl
, &zc
);
360 zcmd_free_nvlists(&zc
);
365 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
366 zcmd_free_nvlists(&zc
);
370 nvlist_free(zhp
->zfs_recvd_props
);
371 zhp
->zfs_recvd_props
= recvdprops
;
377 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
379 nvlist_t
*allprops
, *userprops
;
381 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
383 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
388 * XXX Why do we store the user props separately, in addition to
389 * storing them in zfs_props?
391 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
392 nvlist_free(allprops
);
396 nvlist_free(zhp
->zfs_props
);
397 nvlist_free(zhp
->zfs_user_props
);
399 zhp
->zfs_props
= allprops
;
400 zhp
->zfs_user_props
= userprops
;
406 get_stats(zfs_handle_t
*zhp
)
409 zfs_cmd_t zc
= {"\0"};
411 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0);
413 if (get_stats_ioctl(zhp
, &zc
) != 0)
415 else if (put_stats_zhdl(zhp
, &zc
) != 0)
417 zcmd_free_nvlists(&zc
);
422 * Refresh the properties currently stored in the handle.
425 zfs_refresh_properties(zfs_handle_t
*zhp
)
427 (void) get_stats(zhp
);
431 * Makes a handle from the given dataset name. Used by zfs_open() and
432 * zfs_iter_* to create child handles on the fly.
435 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
437 if (put_stats_zhdl(zhp
, zc
) != 0)
441 * We've managed to open the dataset and gather statistics. Determine
442 * the high-level type.
444 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
) {
445 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
446 } else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
) {
447 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
448 } else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
) {
451 } else if (zhp
->zfs_dmustats
.dds_inconsistent
) {
458 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
459 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
460 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
461 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
462 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
463 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
465 abort(); /* we should never see any other types */
467 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
474 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
476 zfs_cmd_t zc
= {"\0"};
478 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
484 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
485 zcmd_alloc_dst_nvlist(hdl
, &zc
, 0);
487 if (get_stats_ioctl(zhp
, &zc
) == -1) {
488 zcmd_free_nvlists(&zc
);
492 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
496 zcmd_free_nvlists(&zc
);
501 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
503 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
509 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
510 if (make_dataset_handle_common(zhp
, zc
) == -1) {
518 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
520 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
525 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
526 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
527 zhp
->zfs_head_type
= pzhp
->zfs_type
;
528 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
529 zhp
->zpool_hdl
= zpool_handle(zhp
);
530 zhp
->zfs_dmustats
= zc
->zc_objset_stats
;
536 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
538 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
543 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
544 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
545 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
546 sizeof (zhp
->zfs_name
));
547 zhp
->zfs_type
= zhp_orig
->zfs_type
;
548 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
549 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
550 if (zhp_orig
->zfs_props
!= NULL
) {
551 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
552 (void) no_memory(zhp
->zfs_hdl
);
557 if (zhp_orig
->zfs_user_props
!= NULL
) {
558 if (nvlist_dup(zhp_orig
->zfs_user_props
,
559 &zhp
->zfs_user_props
, 0) != 0) {
560 (void) no_memory(zhp
->zfs_hdl
);
565 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
566 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
567 &zhp
->zfs_recvd_props
, 0)) {
568 (void) no_memory(zhp
->zfs_hdl
);
573 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
574 if (zhp_orig
->zfs_mntopts
!= NULL
) {
575 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
576 zhp_orig
->zfs_mntopts
);
578 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
583 zfs_bookmark_exists(const char *path
)
587 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
593 (void) strlcpy(fsname
, path
, sizeof (fsname
));
594 pound
= strchr(fsname
, '#');
599 bmark_name
= pound
+ 1;
600 props
= fnvlist_alloc();
601 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
608 rv
= nvlist_exists(bmarks
, bmark_name
);
614 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
615 nvlist_t
*bmark_props
)
617 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
622 /* Fill in the name. */
623 zhp
->zfs_hdl
= parent
->zfs_hdl
;
624 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
626 /* Set the property lists. */
627 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
633 zhp
->zfs_head_type
= parent
->zfs_head_type
;
634 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
636 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
637 nvlist_free(zhp
->zfs_props
);
645 struct zfs_open_bookmarks_cb_data
{
651 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
653 struct zfs_open_bookmarks_cb_data
*dp
= data
;
656 * Is it the one we are looking for?
658 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
660 * We found it. Save it and let the caller know we are done.
667 * Not found. Close the handle and ask for another one.
674 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
675 * argument is a mask of acceptable types. The function will print an
676 * appropriate error message and return NULL if it can't be opened.
679 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
682 char errbuf
[ERRBUFLEN
];
685 (void) snprintf(errbuf
, sizeof (errbuf
),
686 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
689 * Validate the name before we even try to open it.
691 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
692 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
697 * Bookmarks needs to be handled separately.
699 bookp
= strchr(path
, '#');
702 * Try to get stats for the dataset, which will tell us if it
706 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
707 (void) zfs_standard_error(hdl
, errno
, errbuf
);
711 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
713 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
716 * We need to cut out '#' and everything after '#'
717 * to get the parent dataset name only.
719 assert(bookp
- path
< sizeof (dsname
));
720 (void) strncpy(dsname
, path
, bookp
- path
);
721 dsname
[bookp
- path
] = '\0';
724 * Create handle for the parent dataset.
727 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
728 (void) zfs_standard_error(hdl
, errno
, errbuf
);
733 * Iterate bookmarks to find the right one.
736 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
737 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
738 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
742 if (cb_data
.zhp
== NULL
) {
743 (void) zfs_standard_error(hdl
, errno
, errbuf
);
755 if (!(types
& zhp
->zfs_type
)) {
756 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
765 * Release a ZFS handle. Nothing to do but free the associated memory.
768 zfs_close(zfs_handle_t
*zhp
)
770 if (zhp
->zfs_mntopts
)
771 free(zhp
->zfs_mntopts
);
772 nvlist_free(zhp
->zfs_props
);
773 nvlist_free(zhp
->zfs_user_props
);
774 nvlist_free(zhp
->zfs_recvd_props
);
778 typedef struct mnttab_node
{
779 struct mnttab mtn_mt
;
784 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
786 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
787 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
790 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
792 return (TREE_ISIGN(rv
));
796 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
798 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
799 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
800 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
801 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
805 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
810 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
813 while (getmntent(mnttab
, &entry
) == 0) {
817 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
820 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
821 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
822 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
823 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
824 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
826 /* Exclude duplicate mounts */
827 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
828 free(mtn
->mtn_mt
.mnt_special
);
829 free(mtn
->mtn_mt
.mnt_mountp
);
830 free(mtn
->mtn_mt
.mnt_fstype
);
831 free(mtn
->mtn_mt
.mnt_mntopts
);
836 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
839 (void) fclose(mnttab
);
844 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
849 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
851 free(mtn
->mtn_mt
.mnt_special
);
852 free(mtn
->mtn_mt
.mnt_mountp
);
853 free(mtn
->mtn_mt
.mnt_fstype
);
854 free(mtn
->mtn_mt
.mnt_mntopts
);
857 avl_destroy(&hdl
->libzfs_mnttab_cache
);
858 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
862 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
864 hdl
->libzfs_mnttab_enable
= enable
;
868 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
869 struct mnttab
*entry
)
876 if (!hdl
->libzfs_mnttab_enable
) {
877 struct mnttab srch
= { 0 };
879 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
880 libzfs_mnttab_fini(hdl
);
882 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
885 srch
.mnt_special
= (char *)fsname
;
886 srch
.mnt_fstype
= (char *)MNTTYPE_ZFS
;
887 ret
= getmntany(mnttab
, entry
, &srch
) ? ENOENT
: 0;
888 (void) fclose(mnttab
);
892 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
893 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
896 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
897 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
902 find
.mtn_mt
.mnt_special
= (char *)fsname
;
903 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
905 *entry
= mtn
->mtn_mt
;
908 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
913 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
914 const char *mountp
, const char *mntopts
)
918 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
919 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
920 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
921 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
922 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
923 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
924 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
926 * Another thread may have already added this entry
927 * via libzfs_mnttab_update. If so we should skip it.
929 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
) {
930 free(mtn
->mtn_mt
.mnt_special
);
931 free(mtn
->mtn_mt
.mnt_mountp
);
932 free(mtn
->mtn_mt
.mnt_fstype
);
933 free(mtn
->mtn_mt
.mnt_mntopts
);
936 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
939 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
943 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
948 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
949 find
.mtn_mt
.mnt_special
= (char *)fsname
;
950 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
952 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
953 free(ret
->mtn_mt
.mnt_special
);
954 free(ret
->mtn_mt
.mnt_mountp
);
955 free(ret
->mtn_mt
.mnt_fstype
);
956 free(ret
->mtn_mt
.mnt_mntopts
);
959 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
963 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
965 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
967 if (zpool_handle
== NULL
)
970 *spa_version
= zpool_get_prop_int(zpool_handle
,
971 ZPOOL_PROP_VERSION
, NULL
);
976 * The choice of reservation property depends on the SPA version.
979 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
983 if (zfs_spa_version(zhp
, &spa_version
) < 0)
986 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
987 *resv_prop
= ZFS_PROP_REFRESERVATION
;
989 *resv_prop
= ZFS_PROP_RESERVATION
;
995 * Given an nvlist of properties to set, validates that they are correct, and
996 * parses any numeric properties (index, boolean, etc) if they are specified as
1000 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1001 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1002 boolean_t key_params_ok
, const char *errbuf
)
1009 int chosen_normal
= -1;
1010 int chosen_utf
= -1;
1012 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1013 (void) no_memory(hdl
);
1018 * Make sure this property is valid and applies to this type.
1022 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1023 const char *propname
= nvpair_name(elem
);
1025 prop
= zfs_name_to_prop(propname
);
1026 if (prop
== ZPROP_USERPROP
&& zfs_prop_user(propname
)) {
1028 * This is a user property: make sure it's a
1029 * string, and that it's less than ZAP_MAXNAMELEN.
1031 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1032 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1033 "'%s' must be a string"), propname
);
1034 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1038 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1039 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1040 "property name '%s' is too long"),
1042 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1046 (void) nvpair_value_string(elem
, &strval
);
1047 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1048 (void) no_memory(hdl
);
1055 * Currently, only user properties can be modified on
1058 if (type
== ZFS_TYPE_SNAPSHOT
) {
1059 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1060 "this property can not be modified for snapshots"));
1061 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1065 if (prop
== ZPROP_USERPROP
&& zfs_prop_userquota(propname
)) {
1066 zfs_userquota_prop_t uqtype
;
1067 char *newpropname
= NULL
;
1073 if (userquota_propname_decode(propname
, zoned
,
1074 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1076 dgettext(TEXT_DOMAIN
,
1077 "'%s' has an invalid user/group name"),
1079 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1083 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1084 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1085 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1086 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1087 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1088 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1090 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1092 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1097 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1098 (void) nvpair_value_string(elem
, &strval
);
1099 if (strcmp(strval
, "none") == 0) {
1101 } else if (zfs_nicestrtonum(hdl
,
1102 strval
, &intval
) != 0) {
1103 (void) zfs_error(hdl
,
1104 EZFS_BADPROP
, errbuf
);
1107 } else if (nvpair_type(elem
) ==
1109 (void) nvpair_value_uint64(elem
, &intval
);
1111 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1112 "use 'none' to disable "
1113 "{user|group|project}quota"));
1117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1118 "'%s' must be a number"), propname
);
1119 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1124 * Encode the prop name as
1125 * userquota@<hex-rid>-domain, to make it easy
1126 * for the kernel to decode.
1128 rc
= asprintf(&newpropname
, "%s%llx-%s",
1129 zfs_userquota_prop_prefixes
[uqtype
],
1130 (longlong_t
)rid
, domain
);
1131 if (rc
== -1 || newpropname
== NULL
) {
1132 (void) no_memory(hdl
);
1139 if (nvlist_add_uint64_array(ret
, newpropname
,
1142 (void) no_memory(hdl
);
1147 } else if (prop
== ZPROP_USERPROP
&&
1148 zfs_prop_written(propname
)) {
1149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1150 "'%s' is readonly"),
1152 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1156 if (prop
== ZPROP_INVAL
) {
1157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1158 "invalid property '%s'"), propname
);
1159 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1163 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1165 dgettext(TEXT_DOMAIN
, "'%s' does not "
1166 "apply to datasets of this type"), propname
);
1167 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1171 if (zfs_prop_readonly(prop
) &&
1172 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1173 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1175 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1177 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1181 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1182 &strval
, &intval
, errbuf
) != 0)
1186 * Perform some additional checks for specific properties.
1189 case ZFS_PROP_VERSION
:
1195 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1196 if (intval
< version
) {
1197 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1198 "Can not downgrade; already at version %u"),
1200 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1206 case ZFS_PROP_VOLBLOCKSIZE
:
1207 case ZFS_PROP_RECORDSIZE
:
1209 int maxbs
= SPA_MAXBLOCKSIZE
;
1212 if (zpool_hdl
!= NULL
) {
1213 maxbs
= zpool_get_prop_int(zpool_hdl
,
1214 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1217 * The value must be a power of two between
1218 * SPA_MINBLOCKSIZE and maxbs.
1220 if (intval
< SPA_MINBLOCKSIZE
||
1221 intval
> maxbs
|| !ISP2(intval
)) {
1222 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1223 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1224 "'%s' must be power of 2 from 512B "
1225 "to %s"), propname
, buf
);
1226 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1232 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1234 int maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1237 if (zpool_hdl
!= NULL
) {
1238 char state
[64] = "";
1240 maxbs
= zpool_get_prop_int(zpool_hdl
,
1241 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1244 * Issue a warning but do not fail so that
1245 * tests for settable properties succeed.
1247 if (zpool_prop_get_feature(zpool_hdl
,
1248 "feature@allocation_classes", state
,
1249 sizeof (state
)) != 0 ||
1250 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1251 (void) fprintf(stderr
, gettext(
1252 "%s: property requires a special "
1253 "device in the pool\n"), propname
);
1257 (intval
< SPA_MINBLOCKSIZE
||
1258 intval
> maxbs
|| !ISP2(intval
))) {
1259 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1260 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1261 "invalid '%s=%llu' property: must be zero "
1262 "or a power of 2 from 512B to %s"),
1263 propname
, (unsigned long long)intval
, buf
);
1264 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1270 case ZFS_PROP_MLSLABEL
:
1272 #ifdef HAVE_MLSLABEL
1274 * Verify the mlslabel string and convert to
1275 * internal hex label string.
1279 char *hex
= NULL
; /* internal label string */
1281 /* Default value is already OK. */
1282 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1285 /* Verify the label can be converted to binary form */
1286 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1287 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1288 L_NO_CORRECTION
, NULL
) == -1)) {
1292 /* Now translate to hex internal label string */
1293 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1299 m_label_free(new_sl
);
1301 /* If string is already in internal form, we're done. */
1302 if (strcmp(strval
, hex
) == 0) {
1307 /* Replace the label string with the internal form. */
1308 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1310 fnvlist_add_string(ret
, zfs_prop_to_name(prop
), hex
);
1316 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1317 "invalid mlslabel '%s'"), strval
);
1318 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1319 m_label_free(new_sl
); /* OK if null */
1322 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1323 "mlslabels are unsupported"));
1324 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1326 #endif /* HAVE_MLSLABEL */
1329 case ZFS_PROP_MOUNTPOINT
:
1331 namecheck_err_t why
;
1333 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1334 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1337 if (mountpoint_namecheck(strval
, &why
)) {
1339 case NAME_ERR_LEADING_SLASH
:
1341 dgettext(TEXT_DOMAIN
,
1342 "'%s' must be an absolute path, "
1343 "'none', or 'legacy'"), propname
);
1345 case NAME_ERR_TOOLONG
:
1347 dgettext(TEXT_DOMAIN
,
1348 "component of '%s' is too long"),
1354 dgettext(TEXT_DOMAIN
,
1355 "(%d) not defined"),
1359 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1365 case ZFS_PROP_SHARESMB
:
1366 case ZFS_PROP_SHARENFS
:
1368 * For the mountpoint and sharenfs or sharesmb
1369 * properties, check if it can be set in a
1370 * global/non-global zone based on
1371 * the zoned property value:
1373 * global zone non-global zone
1374 * --------------------------------------------------
1375 * zoned=on mountpoint (no) mountpoint (yes)
1376 * sharenfs (no) sharenfs (no)
1377 * sharesmb (no) sharesmb (no)
1379 * zoned=off mountpoint (yes) N/A
1384 if (getzoneid() == GLOBAL_ZONEID
) {
1385 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1386 "'%s' cannot be set on "
1387 "dataset in a non-global zone"),
1389 (void) zfs_error(hdl
, EZFS_ZONED
,
1392 } else if (prop
== ZFS_PROP_SHARENFS
||
1393 prop
== ZFS_PROP_SHARESMB
) {
1394 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1395 "'%s' cannot be set in "
1396 "a non-global zone"), propname
);
1397 (void) zfs_error(hdl
, EZFS_ZONED
,
1401 } else if (getzoneid() != GLOBAL_ZONEID
) {
1403 * If zoned property is 'off', this must be in
1404 * a global zone. If not, something is wrong.
1406 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1407 "'%s' cannot be set while dataset "
1408 "'zoned' property is set"), propname
);
1409 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1414 * At this point, it is legitimate to set the
1415 * property. Now we want to make sure that the
1416 * property value is valid if it is sharenfs.
1418 if ((prop
== ZFS_PROP_SHARENFS
||
1419 prop
== ZFS_PROP_SHARESMB
) &&
1420 strcmp(strval
, "on") != 0 &&
1421 strcmp(strval
, "off") != 0) {
1422 enum sa_protocol proto
;
1424 if (prop
== ZFS_PROP_SHARESMB
)
1425 proto
= SA_PROTOCOL_SMB
;
1427 proto
= SA_PROTOCOL_NFS
;
1429 if (sa_validate_shareopts(strval
, proto
) !=
1431 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1432 "'%s' cannot be set to invalid "
1433 "options"), propname
);
1434 (void) zfs_error(hdl
, EZFS_BADPROP
,
1442 case ZFS_PROP_KEYLOCATION
:
1443 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1444 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1445 "invalid keylocation"));
1446 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1452 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1454 if (crypt
== ZIO_CRYPT_OFF
&&
1455 strcmp(strval
, "none") != 0) {
1456 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1457 "keylocation must be 'none' "
1458 "for unencrypted datasets"));
1459 (void) zfs_error(hdl
, EZFS_BADPROP
,
1462 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1463 strcmp(strval
, "none") == 0) {
1464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1465 "keylocation must not be 'none' "
1466 "for encrypted datasets"));
1467 (void) zfs_error(hdl
, EZFS_BADPROP
,
1474 case ZFS_PROP_PBKDF2_ITERS
:
1475 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1476 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1477 "minimum pbkdf2 iterations is %u"),
1478 MIN_PBKDF2_ITERATIONS
);
1479 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1484 case ZFS_PROP_UTF8ONLY
:
1485 chosen_utf
= (int)intval
;
1488 case ZFS_PROP_NORMALIZE
:
1489 chosen_normal
= (int)intval
;
1497 * For changes to existing volumes, we have some additional
1498 * checks to enforce.
1500 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1501 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1502 ZFS_PROP_VOLBLOCKSIZE
);
1506 case ZFS_PROP_VOLSIZE
:
1507 if (intval
% blocksize
!= 0) {
1508 zfs_nicebytes(blocksize
, buf
,
1510 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1511 "'%s' must be a multiple of "
1512 "volume block size (%s)"),
1514 (void) zfs_error(hdl
, EZFS_BADPROP
,
1520 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1521 "'%s' cannot be zero"),
1523 (void) zfs_error(hdl
, EZFS_BADPROP
,
1534 /* check encryption properties */
1536 int64_t crypt
= zfs_prop_get_int(zhp
,
1537 ZFS_PROP_ENCRYPTION
);
1540 case ZFS_PROP_COPIES
:
1541 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1542 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1543 "encrypted datasets cannot have "
1545 (void) zfs_error(hdl
, EZFS_BADPROP
,
1557 * If normalization was chosen, but no UTF8 choice was made,
1558 * enforce rejection of non-UTF8 names.
1560 * If normalization was chosen, but rejecting non-UTF8 names
1561 * was explicitly not chosen, it is an error.
1563 * If utf8only was turned off, but the parent has normalization,
1564 * turn off normalization.
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
);
1578 } else if (chosen_normal
< 0 && chosen_utf
== 0) {
1579 if (nvlist_add_uint64(ret
,
1580 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), 0) != 0) {
1581 (void) no_memory(hdl
);
1593 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1595 uint64_t old_volsize
;
1596 uint64_t new_volsize
;
1597 uint64_t old_reservation
;
1598 uint64_t new_reservation
;
1599 zfs_prop_t resv_prop
;
1601 zpool_handle_t
*zph
= zpool_handle(zhp
);
1604 * If this is an existing volume, and someone is setting the volsize,
1605 * make sure that it matches the reservation, or add it if necessary.
1607 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1608 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1610 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1612 props
= fnvlist_alloc();
1613 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1614 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1616 if ((zvol_volsize_to_reservation(zph
, old_volsize
, props
) !=
1617 old_reservation
) || nvlist_exists(nvl
,
1618 zfs_prop_to_name(resv_prop
))) {
1619 fnvlist_free(props
);
1622 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1623 &new_volsize
) != 0) {
1624 fnvlist_free(props
);
1627 new_reservation
= zvol_volsize_to_reservation(zph
, new_volsize
, props
);
1628 fnvlist_free(props
);
1630 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1631 new_reservation
) != 0) {
1632 (void) no_memory(zhp
->zfs_hdl
);
1639 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1640 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1641 * Return codes must match zfs_add_synthetic_resv().
1644 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1651 if (!ZFS_IS_VOLUME(zhp
)) {
1655 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1659 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1663 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1664 /* No value being set, so it can't be "auto" */
1667 if (resvsize
!= UINT64_MAX
) {
1668 /* Being set to a value other than "auto" */
1672 props
= fnvlist_alloc();
1674 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1675 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1677 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1679 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1682 resvsize
= zvol_volsize_to_reservation(zpool_handle(zhp
), volsize
,
1684 fnvlist_free(props
);
1686 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1687 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1688 (void) no_memory(zhp
->zfs_hdl
);
1695 zfs_is_namespace_prop(zfs_prop_t prop
)
1699 case ZFS_PROP_ATIME
:
1700 case ZFS_PROP_RELATIME
:
1701 case ZFS_PROP_DEVICES
:
1703 case ZFS_PROP_SETUID
:
1704 case ZFS_PROP_READONLY
:
1705 case ZFS_PROP_XATTR
:
1706 case ZFS_PROP_NBMAND
:
1715 * Given a property name and value, set the property for the given dataset.
1718 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1721 char errbuf
[ERRBUFLEN
];
1722 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1723 nvlist_t
*nvl
= NULL
;
1725 (void) snprintf(errbuf
, sizeof (errbuf
),
1726 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1729 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1730 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1731 (void) no_memory(hdl
);
1735 ret
= zfs_prop_set_list(zhp
, nvl
);
1745 * Given an nvlist of property names and values, set the properties for the
1749 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1751 zfs_cmd_t zc
= {"\0"};
1753 prop_changelist_t
**cls
= NULL
;
1755 char errbuf
[ERRBUFLEN
];
1756 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1760 zfs_prop_t prop
= 0;
1763 (void) snprintf(errbuf
, sizeof (errbuf
),
1764 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1767 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1768 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1769 B_FALSE
, errbuf
)) == NULL
)
1773 * We have to check for any extra properties which need to be added
1774 * before computing the length of the nvlist.
1776 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1778 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1779 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1780 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1785 if (added_resv
!= 1 &&
1786 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1791 * Check how many properties we're setting and allocate an array to
1792 * store changelist pointers for postfix().
1794 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1796 elem
= nvlist_next_nvpair(nvl
, elem
))
1798 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1802 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1804 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1806 prop
= zfs_name_to_prop(nvpair_name(elem
));
1808 assert(cl_idx
< nvl_len
);
1810 * We don't want to unmount & remount the dataset when changing
1811 * its canmount property to 'on' or 'noauto'. We only use
1812 * the changelist logic to unmount when setting canmount=off.
1814 if (prop
!= ZFS_PROP_CANMOUNT
||
1815 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1816 zfs_is_mounted(zhp
, NULL
))) {
1817 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1818 if (cls
[cl_idx
] == NULL
)
1822 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1823 changelist_haszonedchild(cls
[cl_idx
])) {
1824 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1825 "child dataset with inherited mountpoint is used "
1826 "in a non-global zone"));
1827 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1831 if (cls
[cl_idx
] != NULL
&&
1832 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1837 assert(cl_idx
== nvl_len
);
1840 * Execute the corresponding ioctl() to set this list of properties.
1842 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1844 zcmd_write_src_nvlist(hdl
, &zc
, nvl
);
1845 zcmd_alloc_dst_nvlist(hdl
, &zc
, 0);
1847 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1850 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1851 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1855 /* Get the list of unset properties back and report them. */
1856 nvlist_t
*errorprops
= NULL
;
1857 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1859 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1861 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1862 prop
= zfs_name_to_prop(nvpair_name(elem
));
1863 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1865 nvlist_free(errorprops
);
1867 if (added_resv
&& errno
== ENOSPC
) {
1868 /* clean up the volsize property we tried to set */
1869 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1873 zcmd_free_nvlists(&zc
);
1875 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1877 if (nvlist_add_uint64(nvl
,
1878 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1881 zcmd_write_src_nvlist(hdl
, &zc
, nvl
);
1882 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1885 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1886 if (cls
[cl_idx
] != NULL
) {
1887 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1895 * Refresh the statistics so the new property
1896 * value is reflected.
1898 (void) get_stats(zhp
);
1901 * Remount the filesystem to propagate the change
1902 * if one of the options handled by the generic
1903 * Linux namespace layer has been modified.
1905 if (zfs_is_namespace_prop(prop
) &&
1906 zfs_is_mounted(zhp
, NULL
))
1907 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1913 zcmd_free_nvlists(&zc
);
1915 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1916 if (cls
[cl_idx
] != NULL
)
1917 changelist_free(cls
[cl_idx
]);
1925 * Given a property, inherit the value from the parent dataset, or if received
1926 * is TRUE, revert to the received value, if any.
1929 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1931 zfs_cmd_t zc
= {"\0"};
1933 prop_changelist_t
*cl
;
1934 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1935 char errbuf
[ERRBUFLEN
];
1938 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1939 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1941 zc
.zc_cookie
= received
;
1942 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_USERPROP
) {
1944 * For user properties, the amount of work we have to do is very
1945 * small, so just do it here.
1947 if (!zfs_prop_user(propname
)) {
1948 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1949 "invalid property"));
1950 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1953 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1954 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1956 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1957 return (zfs_standard_error(hdl
, errno
, errbuf
));
1959 (void) get_stats(zhp
);
1964 * Verify that this property is inheritable.
1966 if (zfs_prop_readonly(prop
))
1967 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1969 if (!zfs_prop_inheritable(prop
) && !received
)
1970 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1973 * Check to see if the value applies to this type
1975 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1976 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1979 * Normalize the name, to get rid of shorthand abbreviations.
1981 propname
= zfs_prop_to_name(prop
);
1982 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1983 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1985 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1986 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1987 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1988 "dataset is used in a non-global zone"));
1989 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1993 * Determine datasets which will be affected by this change, if any.
1995 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1998 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2000 "child dataset with inherited mountpoint is used "
2001 "in a non-global zone"));
2002 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2006 if ((ret
= changelist_prefix(cl
)) != 0)
2009 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2010 return (zfs_standard_error(hdl
, errno
, errbuf
));
2013 if ((ret
= changelist_postfix(cl
)) != 0)
2017 * Refresh the statistics so the new property is reflected.
2019 (void) get_stats(zhp
);
2022 * Remount the filesystem to propagate the change
2023 * if one of the options handled by the generic
2024 * Linux namespace layer has been modified.
2026 if (zfs_is_namespace_prop(prop
) &&
2027 zfs_is_mounted(zhp
, NULL
))
2028 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2032 changelist_free(cl
);
2037 * True DSL properties are stored in an nvlist. The following two functions
2038 * extract them appropriately.
2041 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2047 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2048 zfs_prop_to_name(prop
), &nv
) == 0) {
2049 value
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
2050 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2052 verify(!zhp
->zfs_props_table
||
2053 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2054 value
= zfs_prop_default_numeric(prop
);
2055 *source
= (char *)"";
2062 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2068 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2069 zfs_prop_to_name(prop
), &nv
) == 0) {
2070 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2071 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2073 verify(!zhp
->zfs_props_table
||
2074 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2075 value
= zfs_prop_default_string(prop
);
2076 *source
= (char *)"";
2083 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2085 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2089 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2091 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2092 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2096 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2098 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2103 * Internal function for getting a numeric property. Both zfs_prop_get() and
2104 * zfs_prop_get_int() are built using this interface.
2106 * Certain properties can be overridden using 'mount -o'. In this case, scan
2107 * the contents of the /proc/self/mounts entry, searching for the
2108 * appropriate options. If they differ from the on-disk values, report the
2109 * current values and mark the source "temporary".
2112 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2113 char **source
, uint64_t *val
)
2115 zfs_cmd_t zc
= {"\0"};
2116 nvlist_t
*zplprops
= NULL
;
2118 const char *mntopt_on
= NULL
;
2119 const char *mntopt_off
= NULL
;
2120 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2125 * If the property is being fetched for a snapshot, check whether
2126 * the property is valid for the snapshot's head dataset type.
2128 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2129 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2130 *val
= zfs_prop_default_numeric(prop
);
2135 case ZFS_PROP_ATIME
:
2136 mntopt_on
= MNTOPT_ATIME
;
2137 mntopt_off
= MNTOPT_NOATIME
;
2140 case ZFS_PROP_RELATIME
:
2141 mntopt_on
= MNTOPT_RELATIME
;
2142 mntopt_off
= MNTOPT_NORELATIME
;
2145 case ZFS_PROP_DEVICES
:
2146 mntopt_on
= MNTOPT_DEVICES
;
2147 mntopt_off
= MNTOPT_NODEVICES
;
2151 mntopt_on
= MNTOPT_EXEC
;
2152 mntopt_off
= MNTOPT_NOEXEC
;
2155 case ZFS_PROP_READONLY
:
2156 mntopt_on
= MNTOPT_RO
;
2157 mntopt_off
= MNTOPT_RW
;
2160 case ZFS_PROP_SETUID
:
2161 mntopt_on
= MNTOPT_SETUID
;
2162 mntopt_off
= MNTOPT_NOSETUID
;
2165 case ZFS_PROP_XATTR
:
2166 mntopt_on
= MNTOPT_XATTR
;
2167 mntopt_off
= MNTOPT_NOXATTR
;
2170 case ZFS_PROP_NBMAND
:
2171 mntopt_on
= MNTOPT_NBMAND
;
2172 mntopt_off
= MNTOPT_NONBMAND
;
2180 * Because looking up the mount options is potentially expensive
2181 * (iterating over all of /proc/self/mounts), we defer its
2182 * calculation until we're looking up a property which requires
2185 if (!zhp
->zfs_mntcheck
&&
2186 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2187 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2188 struct mnttab entry
;
2190 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0)
2191 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2194 zhp
->zfs_mntcheck
= B_TRUE
;
2197 if (zhp
->zfs_mntopts
== NULL
)
2198 mnt
.mnt_mntopts
= (char *)"";
2200 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2203 case ZFS_PROP_ATIME
:
2204 case ZFS_PROP_RELATIME
:
2205 case ZFS_PROP_DEVICES
:
2207 case ZFS_PROP_READONLY
:
2208 case ZFS_PROP_SETUID
:
2210 case ZFS_PROP_XATTR
:
2212 case ZFS_PROP_NBMAND
:
2213 *val
= getprop_uint64(zhp
, prop
, source
);
2218 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2221 *src
= ZPROP_SRC_TEMPORARY
;
2222 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2225 *src
= ZPROP_SRC_TEMPORARY
;
2229 case ZFS_PROP_CANMOUNT
:
2230 case ZFS_PROP_VOLSIZE
:
2231 case ZFS_PROP_QUOTA
:
2232 case ZFS_PROP_REFQUOTA
:
2233 case ZFS_PROP_RESERVATION
:
2234 case ZFS_PROP_REFRESERVATION
:
2235 case ZFS_PROP_FILESYSTEM_LIMIT
:
2236 case ZFS_PROP_SNAPSHOT_LIMIT
:
2237 case ZFS_PROP_FILESYSTEM_COUNT
:
2238 case ZFS_PROP_SNAPSHOT_COUNT
:
2239 *val
= getprop_uint64(zhp
, prop
, source
);
2241 if (*source
== NULL
) {
2242 /* not default, must be local */
2243 *source
= zhp
->zfs_name
;
2247 case ZFS_PROP_MOUNTED
:
2248 *val
= (zhp
->zfs_mntopts
!= NULL
);
2251 case ZFS_PROP_NUMCLONES
:
2252 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2255 case ZFS_PROP_VERSION
:
2256 case ZFS_PROP_NORMALIZE
:
2257 case ZFS_PROP_UTF8ONLY
:
2259 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0);
2261 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2262 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2263 zcmd_free_nvlists(&zc
);
2264 if (prop
== ZFS_PROP_VERSION
&&
2265 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2266 *val
= zfs_prop_default_numeric(prop
);
2269 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2270 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2272 zcmd_free_nvlists(&zc
);
2275 nvlist_free(zplprops
);
2276 zcmd_free_nvlists(&zc
);
2279 case ZFS_PROP_INCONSISTENT
:
2280 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2283 case ZFS_PROP_REDACTED
:
2284 *val
= zhp
->zfs_dmustats
.dds_redacted
;
2287 case ZFS_PROP_CREATETXG
:
2289 * We can directly read createtxg property from zfs
2290 * handle for Filesystem, Snapshot and ZVOL types.
2292 if ((zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) ||
2293 (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) ||
2294 (zhp
->zfs_type
== ZFS_TYPE_VOLUME
)) {
2295 *val
= zhp
->zfs_dmustats
.dds_creation_txg
;
2301 switch (zfs_prop_get_type(prop
)) {
2302 case PROP_TYPE_NUMBER
:
2303 case PROP_TYPE_INDEX
:
2304 *val
= getprop_uint64(zhp
, prop
, source
);
2306 * If we tried to use a default value for a
2307 * readonly property, it means that it was not
2308 * present. Note this only applies to "truly"
2309 * readonly properties, not set-once properties
2310 * like volblocksize.
2312 if (zfs_prop_readonly(prop
) &&
2313 !zfs_prop_setonce(prop
) &&
2314 *source
!= NULL
&& (*source
)[0] == '\0') {
2320 case PROP_TYPE_STRING
:
2322 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2323 "cannot get non-numeric property"));
2324 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2325 dgettext(TEXT_DOMAIN
, "internal error")));
2333 * Calculate the source type, given the raw source string.
2336 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2337 char *statbuf
, size_t statlen
)
2339 if (statbuf
== NULL
||
2340 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2344 if (source
== NULL
) {
2345 *srctype
= ZPROP_SRC_NONE
;
2346 } else if (source
[0] == '\0') {
2347 *srctype
= ZPROP_SRC_DEFAULT
;
2348 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2349 *srctype
= ZPROP_SRC_RECEIVED
;
2351 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2352 *srctype
= ZPROP_SRC_LOCAL
;
2354 (void) strlcpy(statbuf
, source
, statlen
);
2355 *srctype
= ZPROP_SRC_INHERITED
;
2362 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2363 size_t proplen
, boolean_t literal
)
2368 if (zhp
->zfs_recvd_props
== NULL
)
2369 if (get_recvd_props_ioctl(zhp
) != 0)
2372 prop
= zfs_name_to_prop(propname
);
2374 if (prop
!= ZPROP_USERPROP
) {
2376 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2378 zfs_set_recvd_props_mode(zhp
, &cookie
);
2379 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2380 NULL
, NULL
, 0, literal
);
2381 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2385 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2386 propname
, &propval
) != 0)
2388 recvdval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
2389 (void) strlcpy(propbuf
, recvdval
, proplen
);
2392 return (err
== 0 ? 0 : -1);
2396 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2401 value
= zfs_get_clones_nvl(zhp
);
2402 if (value
== NULL
|| nvlist_empty(value
))
2406 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2407 pair
= nvlist_next_nvpair(value
, pair
)) {
2408 if (propbuf
[0] != '\0')
2409 (void) strlcat(propbuf
, ",", proplen
);
2410 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2416 struct get_clones_arg
{
2420 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2424 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2426 struct get_clones_arg
*gca
= arg
;
2428 if (gca
->numclones
== 0) {
2433 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2434 NULL
, NULL
, 0, B_TRUE
) != 0)
2436 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2437 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2442 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2448 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2450 nvlist_t
*nv
, *value
;
2452 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2453 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2454 struct get_clones_arg gca
;
2457 * if this is a snapshot, then the kernel wasn't able
2458 * to get the clones. Do it by slowly iterating.
2460 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2462 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2464 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2469 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2471 gca
.origin
= zhp
->zfs_name
;
2473 if (gca
.numclones
!= 0) {
2475 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2478 /* get the pool name */
2479 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2480 (void) strsep(&cp
, "/@");
2481 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2482 ZFS_TYPE_FILESYSTEM
);
2489 (void) get_clones_cb(root
, &gca
);
2492 if (gca
.numclones
!= 0 ||
2493 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2494 nvlist_add_nvlist(zhp
->zfs_props
,
2495 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2502 nv
= fnvlist_lookup_nvlist(zhp
->zfs_props
,
2503 zfs_prop_to_name(ZFS_PROP_CLONES
));
2506 return (fnvlist_lookup_nvlist(nv
, ZPROP_VALUE
));
2510 get_rsnaps_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2516 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2517 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
), &value
) != 0)
2519 if (nvlist_lookup_uint64_array(value
, ZPROP_VALUE
, &snaps
,
2523 /* There's no redaction snapshots; pass a special value back */
2524 (void) snprintf(propbuf
, proplen
, "none");
2528 for (int i
= 0; i
< nsnaps
; i
++) {
2530 if (propbuf
[0] != '\0')
2531 (void) strlcat(propbuf
, ",", proplen
);
2532 (void) snprintf(buf
, sizeof (buf
), "%llu",
2533 (u_longlong_t
)snaps
[i
]);
2534 (void) strlcat(propbuf
, buf
, proplen
);
2541 * Accepts a property and value and checks that the value
2542 * matches the one found by the channel program. If they are
2543 * not equal, print both of them.
2546 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2549 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2552 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2553 const char *prop_name
= zfs_prop_to_name(prop
);
2554 const char *program
=
2556 "ds = args['dataset']\n"
2557 "prop = args['property']\n"
2558 "value, setpoint = zfs.get_prop(ds, prop)\n"
2559 "return {value=value, setpoint=setpoint}\n";
2562 nvlist_t
*argnvl
= fnvlist_alloc();
2564 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2565 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2567 error
= lzc_channel_program_nosync(poolname
, program
,
2568 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2571 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2572 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2574 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2576 (void) fprintf(stderr
, "%s: zcp check error: "
2577 "%u\n", prop_name
, error
);
2580 if (ans
!= intval
) {
2581 (void) fprintf(stderr
, "%s: zfs found %llu, "
2582 "but zcp found %llu\n", prop_name
,
2583 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2587 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2589 (void) fprintf(stderr
, "%s: zcp check error: "
2590 "%u\n", prop_name
, error
);
2593 if (strcmp(strval
, str_ans
) != 0) {
2594 (void) fprintf(stderr
,
2595 "%s: zfs found '%s', but zcp found '%s'\n",
2596 prop_name
, strval
, str_ans
);
2600 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2601 "error: %u\n", prop_name
, error
);
2603 nvlist_free(argnvl
);
2604 nvlist_free(outnvl
);
2608 * Retrieve a property from the given object. If 'literal' is specified, then
2609 * numbers are left as exact values. Otherwise, numbers are converted to a
2610 * human-readable form.
2612 * Returns 0 on success, or -1 on error.
2615 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2616 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2618 char *source
= NULL
;
2622 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2625 * Check to see if this property applies to our object
2627 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2630 if (received
&& zfs_prop_readonly(prop
))
2634 *src
= ZPROP_SRC_NONE
;
2637 case ZFS_PROP_CREATION
:
2639 * 'creation' is a time_t stored in the statistics. We convert
2640 * this into a string unless 'literal' is specified.
2643 val
= getprop_uint64(zhp
, prop
, &source
);
2644 time_t time
= (time_t)val
;
2648 localtime_r(&time
, &t
) == NULL
||
2649 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2651 (void) snprintf(propbuf
, proplen
, "%llu",
2654 zcp_check(zhp
, prop
, val
, NULL
);
2657 case ZFS_PROP_MOUNTPOINT
:
2659 * Getting the precise mountpoint can be tricky.
2661 * - for 'none' or 'legacy', return those values.
2662 * - for inherited mountpoints, we want to take everything
2663 * after our ancestor and append it to the inherited value.
2665 * If the pool has an alternate root, we want to prepend that
2666 * root to any values we return.
2669 str
= getprop_string(zhp
, prop
, &source
);
2671 if (str
[0] == '/') {
2672 char buf
[MAXPATHLEN
];
2674 const char *relpath
;
2677 * If we inherit the mountpoint, even from a dataset
2678 * with a received value, the source will be the path of
2679 * the dataset we inherit from. If source is
2680 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2683 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2686 relpath
= zhp
->zfs_name
+ strlen(source
);
2687 if (relpath
[0] == '/')
2691 if ((zpool_get_prop(zhp
->zpool_hdl
,
2692 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2693 B_FALSE
)) || (strcmp(root
, "-") == 0))
2696 * Special case an alternate root of '/'. This will
2697 * avoid having multiple leading slashes in the
2700 if (strcmp(root
, "/") == 0)
2704 * If the mountpoint is '/' then skip over this
2705 * if we are obtaining either an alternate root or
2706 * an inherited mountpoint.
2708 if (str
[1] == '\0' && (root
[0] != '\0' ||
2709 relpath
[0] != '\0'))
2712 if (relpath
[0] == '\0')
2713 (void) snprintf(propbuf
, proplen
, "%s%s",
2716 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2717 root
, str
, relpath
[0] == '@' ? "" : "/",
2720 /* 'legacy' or 'none' */
2721 (void) strlcpy(propbuf
, str
, proplen
);
2723 zcp_check(zhp
, prop
, 0, propbuf
);
2726 case ZFS_PROP_ORIGIN
:
2727 str
= getprop_string(zhp
, prop
, &source
);
2730 (void) strlcpy(propbuf
, str
, proplen
);
2731 zcp_check(zhp
, prop
, 0, str
);
2734 case ZFS_PROP_REDACT_SNAPS
:
2735 if (get_rsnaps_string(zhp
, propbuf
, proplen
) != 0)
2739 case ZFS_PROP_CLONES
:
2740 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2744 case ZFS_PROP_QUOTA
:
2745 case ZFS_PROP_REFQUOTA
:
2746 case ZFS_PROP_RESERVATION
:
2747 case ZFS_PROP_REFRESERVATION
:
2749 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2752 * If quota or reservation is 0, we translate this into 'none'
2753 * (unless literal is set), and indicate that it's the default
2754 * value. Otherwise, we print the number nicely and indicate
2755 * that its set locally.
2759 (void) strlcpy(propbuf
, "0", proplen
);
2761 (void) strlcpy(propbuf
, "none", proplen
);
2764 (void) snprintf(propbuf
, proplen
, "%llu",
2767 zfs_nicebytes(val
, propbuf
, proplen
);
2769 zcp_check(zhp
, prop
, val
, NULL
);
2772 case ZFS_PROP_FILESYSTEM_LIMIT
:
2773 case ZFS_PROP_SNAPSHOT_LIMIT
:
2774 case ZFS_PROP_FILESYSTEM_COUNT
:
2775 case ZFS_PROP_SNAPSHOT_COUNT
:
2777 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2781 * If limit is UINT64_MAX, we translate this into 'none', and
2782 * indicate that it's the default value. Otherwise, we print
2783 * the number nicely and indicate that it's set locally.
2785 if (val
== UINT64_MAX
) {
2786 (void) strlcpy(propbuf
, "none", proplen
);
2787 } else if (literal
) {
2788 (void) snprintf(propbuf
, proplen
, "%llu",
2791 zfs_nicenum(val
, propbuf
, proplen
);
2794 zcp_check(zhp
, prop
, val
, NULL
);
2797 case ZFS_PROP_REFRATIO
:
2798 case ZFS_PROP_COMPRESSRATIO
:
2799 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2802 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2803 (u_longlong_t
)(val
/ 100),
2804 (u_longlong_t
)(val
% 100));
2806 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2807 (u_longlong_t
)(val
/ 100),
2808 (u_longlong_t
)(val
% 100));
2809 zcp_check(zhp
, prop
, val
, NULL
);
2813 switch (zhp
->zfs_type
) {
2814 case ZFS_TYPE_FILESYSTEM
:
2817 case ZFS_TYPE_VOLUME
:
2820 case ZFS_TYPE_SNAPSHOT
:
2823 case ZFS_TYPE_BOOKMARK
:
2829 (void) snprintf(propbuf
, proplen
, "%s", str
);
2830 zcp_check(zhp
, prop
, 0, propbuf
);
2833 case ZFS_PROP_MOUNTED
:
2835 * The 'mounted' property is a pseudo-property that described
2836 * whether the filesystem is currently mounted. Even though
2837 * it's a boolean value, the typical values of "on" and "off"
2838 * don't make sense, so we translate to "yes" and "no".
2840 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2841 src
, &source
, &val
) != 0)
2844 (void) strlcpy(propbuf
, "yes", proplen
);
2846 (void) strlcpy(propbuf
, "no", proplen
);
2851 * The 'name' property is a pseudo-property derived from the
2852 * dataset name. It is presented as a real property to simplify
2855 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2856 zcp_check(zhp
, prop
, 0, propbuf
);
2859 case ZFS_PROP_MLSLABEL
:
2861 #ifdef HAVE_MLSLABEL
2862 m_label_t
*new_sl
= NULL
;
2863 char *ascii
= NULL
; /* human readable label */
2865 (void) strlcpy(propbuf
,
2866 getprop_string(zhp
, prop
, &source
), proplen
);
2868 if (literal
|| (strcasecmp(propbuf
,
2869 ZFS_MLSLABEL_DEFAULT
) == 0))
2873 * Try to translate the internal hex string to
2874 * human-readable output. If there are any
2875 * problems just use the hex string.
2878 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2879 L_NO_CORRECTION
, NULL
) == -1) {
2880 m_label_free(new_sl
);
2884 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2888 m_label_free(new_sl
);
2891 m_label_free(new_sl
);
2893 (void) strlcpy(propbuf
, ascii
, proplen
);
2896 (void) strlcpy(propbuf
,
2897 getprop_string(zhp
, prop
, &source
), proplen
);
2898 #endif /* HAVE_MLSLABEL */
2903 case ZFS_PROP_KEY_GUID
:
2904 case ZFS_PROP_IVSET_GUID
:
2905 case ZFS_PROP_CREATETXG
:
2906 case ZFS_PROP_OBJSETID
:
2907 case ZFS_PROP_PBKDF2_ITERS
:
2909 * These properties are stored as numbers, but they are
2910 * identifiers or counters.
2911 * We don't want them to be pretty printed, because pretty
2912 * printing truncates their values making them useless.
2914 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2916 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2917 zcp_check(zhp
, prop
, val
, NULL
);
2920 case ZFS_PROP_REFERENCED
:
2921 case ZFS_PROP_AVAILABLE
:
2923 case ZFS_PROP_USEDSNAP
:
2924 case ZFS_PROP_USEDDS
:
2925 case ZFS_PROP_USEDREFRESERV
:
2926 case ZFS_PROP_USEDCHILD
:
2927 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2930 (void) snprintf(propbuf
, proplen
, "%llu",
2933 zfs_nicebytes(val
, propbuf
, proplen
);
2935 zcp_check(zhp
, prop
, val
, NULL
);
2938 case ZFS_PROP_SNAPSHOTS_CHANGED
:
2940 if ((get_numeric_property(zhp
, prop
, src
, &source
,
2941 &val
) != 0) || val
== 0) {
2945 time_t time
= (time_t)val
;
2949 localtime_r(&time
, &t
) == NULL
||
2950 strftime(propbuf
, proplen
, "%a %b %e %k:%M:%S %Y",
2952 (void) snprintf(propbuf
, proplen
, "%llu",
2955 zcp_check(zhp
, prop
, val
, NULL
);
2959 switch (zfs_prop_get_type(prop
)) {
2960 case PROP_TYPE_NUMBER
:
2961 if (get_numeric_property(zhp
, prop
, src
,
2962 &source
, &val
) != 0) {
2967 (void) snprintf(propbuf
, proplen
, "%llu",
2970 zfs_nicenum(val
, propbuf
, proplen
);
2972 zcp_check(zhp
, prop
, val
, NULL
);
2975 case PROP_TYPE_STRING
:
2976 str
= getprop_string(zhp
, prop
, &source
);
2980 (void) strlcpy(propbuf
, str
, proplen
);
2981 zcp_check(zhp
, prop
, 0, str
);
2984 case PROP_TYPE_INDEX
:
2985 if (get_numeric_property(zhp
, prop
, src
,
2986 &source
, &val
) != 0)
2988 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2991 (void) strlcpy(propbuf
, strval
, proplen
);
2992 zcp_check(zhp
, prop
, 0, strval
);
3000 get_source(zhp
, src
, source
, statbuf
, statlen
);
3006 * Utility function to get the given numeric property. Does no validation that
3007 * the given property is the appropriate type; should only be used with
3008 * hard-coded property types.
3011 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
3016 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3022 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3026 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3027 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3031 * Similar to zfs_prop_get(), but returns the value as an integer.
3034 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3035 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3040 * Check to see if this property applies to our object
3042 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3043 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3044 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3045 zfs_prop_to_name(prop
)));
3049 *src
= ZPROP_SRC_NONE
;
3051 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3054 get_source(zhp
, src
, source
, statbuf
, statlen
);
3061 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3062 char **domainp
, idmap_rid_t
*ridp
)
3064 idmap_get_handle_t
*get_hdl
= NULL
;
3068 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3072 err
= idmap_get_sidbyuid(get_hdl
, id
,
3073 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3075 err
= idmap_get_sidbygid(get_hdl
, id
,
3076 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3078 if (err
== IDMAP_SUCCESS
&&
3079 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3080 status
== IDMAP_SUCCESS
)
3086 idmap_get_destroy(get_hdl
);
3089 #endif /* HAVE_IDMAP */
3092 * convert the propname into parameters needed by kernel
3093 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3094 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3095 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3096 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3097 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3098 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3101 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3102 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3104 zfs_userquota_prop_t type
;
3108 boolean_t isproject
;
3114 /* Figure out the property type ({user|group|project}{quota|space}) */
3115 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3116 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3117 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3120 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3124 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3125 type
== ZFS_PROP_USEROBJQUOTA
||
3126 type
== ZFS_PROP_USEROBJUSED
);
3127 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3128 type
== ZFS_PROP_GROUPOBJQUOTA
||
3129 type
== ZFS_PROP_GROUPOBJUSED
);
3130 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3131 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3132 type
== ZFS_PROP_PROJECTOBJUSED
);
3134 cp
= strchr(propname
, '@') + 1;
3136 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3137 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3140 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3141 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3144 } else if (!isproject
&& strchr(cp
, '@')) {
3147 * It's a SID name (eg "user@domain") that needs to be
3148 * turned into S-1-domainID-RID.
3150 directory_error_t e
;
3151 char *numericsid
= NULL
;
3154 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3157 e
= directory_sid_from_user_name(NULL
,
3160 e
= directory_sid_from_group_name(NULL
,
3164 directory_error_free(e
);
3167 if (numericsid
== NULL
)
3170 (void) strlcpy(domain
, cp
, domainlen
);
3171 cp
= strrchr(domain
, '-');
3176 *ridp
= strtoull(cp
, &end
, 10);
3179 if (errno
!= 0 || *end
!= '\0')
3184 #endif /* HAVE_IDMAP */
3186 /* It's a user/group/project ID (eg "12345"). */
3189 id
= strtoul(cp
, &end
, 10);
3192 if (id
> MAXUID
&& !isproject
) {
3194 /* It's an ephemeral ID. */
3198 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3199 &mapdomain
, &rid
) != 0)
3201 (void) strlcpy(domain
, mapdomain
, domainlen
);
3205 #endif /* HAVE_IDMAP */
3215 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3216 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3219 zfs_cmd_t zc
= {"\0"};
3221 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3223 err
= userquota_propname_decode(propname
,
3224 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3225 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3226 zc
.zc_objset_type
= *typep
;
3230 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3234 *propvalue
= zc
.zc_cookie
;
3239 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3240 uint64_t *propvalue
)
3242 zfs_userquota_prop_t type
;
3244 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3249 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3250 char *propbuf
, int proplen
, boolean_t literal
)
3254 zfs_userquota_prop_t type
;
3256 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3263 (void) snprintf(propbuf
, proplen
, "%llu",
3264 (u_longlong_t
)propvalue
);
3265 } else if (propvalue
== 0 &&
3266 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3267 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3268 type
== ZFS_PROP_PROJECTQUOTA
||
3269 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3270 (void) strlcpy(propbuf
, "none", proplen
);
3271 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3272 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3273 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3274 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3276 zfs_nicenum(propvalue
, propbuf
, proplen
);
3282 * propname must start with "written@" or "written#".
3285 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3286 uint64_t *propvalue
)
3289 zfs_cmd_t zc
= {"\0"};
3290 const char *snapname
;
3292 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3294 assert(zfs_prop_written(propname
));
3295 snapname
= propname
+ strlen("written@");
3296 if (strchr(snapname
, '@') != NULL
|| strchr(snapname
, '#') != NULL
) {
3297 /* full snapshot or bookmark name specified */
3298 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3300 /* snapname is the short name, append it to zhp's fsname */
3303 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3304 sizeof (zc
.zc_value
));
3305 cp
= strchr(zc
.zc_value
, '@');
3308 (void) strlcat(zc
.zc_value
, snapname
- 1, sizeof (zc
.zc_value
));
3311 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3315 *propvalue
= zc
.zc_cookie
;
3320 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3321 char *propbuf
, int proplen
, boolean_t literal
)
3326 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3332 (void) snprintf(propbuf
, proplen
, "%llu",
3333 (u_longlong_t
)propvalue
);
3335 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3342 * Returns the name of the given zfs handle.
3345 zfs_get_name(const zfs_handle_t
*zhp
)
3347 return (zhp
->zfs_name
);
3351 * Returns the name of the parent pool for the given zfs handle.
3354 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3356 return (zhp
->zpool_hdl
->zpool_name
);
3360 * Returns the type of the given zfs handle.
3363 zfs_get_type(const zfs_handle_t
*zhp
)
3365 return (zhp
->zfs_type
);
3369 * Returns the type of the given zfs handle,
3370 * or, if a snapshot, the type of the snapshotted dataset.
3373 zfs_get_underlying_type(const zfs_handle_t
*zhp
)
3375 return (zhp
->zfs_head_type
);
3379 * Is one dataset name a child dataset of another?
3381 * Needs to handle these cases:
3382 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3383 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3384 * Descendant? No. No. No. Yes.
3387 is_descendant(const char *ds1
, const char *ds2
)
3389 size_t d1len
= strlen(ds1
);
3391 /* ds2 can't be a descendant if it's smaller */
3392 if (strlen(ds2
) < d1len
)
3395 /* otherwise, compare strings and verify that there's a '/' char */
3396 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3400 * Given a complete name, return just the portion that refers to the parent.
3401 * Will return -1 if there is no parent (path is just the name of the
3405 parent_name(const char *path
, char *buf
, size_t buflen
)
3409 (void) strlcpy(buf
, path
, buflen
);
3411 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3419 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3421 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3425 * If accept_ancestor is false, then check to make sure that the given path has
3426 * a parent, and that it exists. If accept_ancestor is true, then find the
3427 * closest existing ancestor for the given path. In prefixlen return the
3428 * length of already existing prefix of the given path. We also fetch the
3429 * 'zoned' property, which is used to validate property settings when creating
3433 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3434 boolean_t accept_ancestor
, int *prefixlen
)
3436 zfs_cmd_t zc
= {"\0"};
3437 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3440 char errbuf
[ERRBUFLEN
];
3443 (void) snprintf(errbuf
, sizeof (errbuf
),
3444 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3446 /* get parent, and check to see if this is just a pool */
3447 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3448 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3449 "missing dataset name"));
3450 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3453 /* check to see if the pool exists */
3454 if ((slash
= strchr(parent
, '/')) == NULL
)
3455 slash
= parent
+ strlen(parent
);
3456 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3457 zc
.zc_name
[slash
- parent
] = '\0';
3458 if (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3460 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3461 "no such pool '%s'"), zc
.zc_name
);
3462 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3465 /* check to see if the parent dataset exists */
3466 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3467 if (errno
== ENOENT
&& accept_ancestor
) {
3469 * Go deeper to find an ancestor, give up on top level.
3471 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3473 "no such pool '%s'"), zc
.zc_name
);
3474 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3476 } else if (errno
== ENOENT
) {
3477 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3478 "parent does not exist"));
3479 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3481 return (zfs_standard_error(hdl
, errno
, errbuf
));
3484 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3488 /* we are in a non-global zone, but parent is in the global zone */
3489 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3490 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3495 /* make sure parent is a filesystem */
3496 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3498 "parent is not a filesystem"));
3499 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3505 if (prefixlen
!= NULL
)
3506 *prefixlen
= strlen(parent
);
3511 * Finds whether the dataset of the given type(s) exists.
3514 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3518 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3522 * Try to get stats for the dataset, which will tell us if it exists.
3524 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3525 int ds_type
= zhp
->zfs_type
;
3528 if (types
& ds_type
)
3535 * Given a path to 'target', create all the ancestors between
3536 * the prefixlen portion of the path, and the target itself.
3537 * Fail if the initial prefixlen-ancestor does not already exist.
3540 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3546 /* make sure prefix exists */
3547 cp
= target
+ prefixlen
;
3549 assert(strchr(cp
, '/') == NULL
);
3550 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3553 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3561 * Attempt to create, mount, and share any ancestor filesystems,
3562 * up to the prefixlen-long one.
3564 for (cp
= target
+ prefixlen
+ 1;
3565 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3569 h
= make_dataset_handle(hdl
, target
);
3571 /* it already exists, nothing to do here */
3576 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3578 opname
= dgettext(TEXT_DOMAIN
, "create");
3582 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3584 opname
= dgettext(TEXT_DOMAIN
, "open");
3588 if (zfs_mount(h
, NULL
, 0) != 0) {
3589 opname
= dgettext(TEXT_DOMAIN
, "mount");
3593 if (zfs_share(h
, NULL
) != 0) {
3594 opname
= dgettext(TEXT_DOMAIN
, "share");
3600 zfs_commit_shares(NULL
);
3605 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3606 "failed to %s ancestor '%s'"), opname
, target
);
3611 * Creates non-existing ancestors of the given path.
3614 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3618 char errbuf
[ERRBUFLEN
];
3621 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3622 "cannot create '%s'"), path
);
3625 * Check that we are not passing the nesting limit
3626 * before we start creating any ancestors.
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 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3637 if ((path_copy
= strdup(path
)) != NULL
) {
3638 rc
= create_parents(hdl
, path_copy
, prefix
);
3641 if (path_copy
== NULL
|| rc
!= 0)
3648 * Create a new filesystem or volume.
3651 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3656 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3658 enum lzc_dataset_type ost
;
3659 zpool_handle_t
*zpool_handle
;
3660 uint8_t *wkeydata
= NULL
;
3662 char errbuf
[ERRBUFLEN
];
3663 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3665 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3666 "cannot create '%s'"), path
);
3668 /* validate the path, taking care to note the extended error message */
3669 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3670 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3672 if (dataset_nestcheck(path
) != 0) {
3673 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3674 "maximum name nesting depth exceeded"));
3675 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3678 /* validate parents exist */
3679 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3683 * The failure modes when creating a dataset of a different type over
3684 * one that already exists is a little strange. In particular, if you
3685 * try to create a dataset on top of an existing dataset, the ioctl()
3686 * will return ENOENT, not EEXIST. To prevent this from happening, we
3687 * first try to see if the dataset exists.
3689 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3690 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3691 "dataset already exists"));
3692 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3695 if (type
== ZFS_TYPE_VOLUME
)
3696 ost
= LZC_DATSET_TYPE_ZVOL
;
3698 ost
= LZC_DATSET_TYPE_ZFS
;
3700 /* open zpool handle for prop validation */
3701 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3702 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3704 /* truncate pool_path at first slash */
3705 char *p
= strchr(pool_path
, '/');
3709 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3712 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3713 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3714 zpool_close(zpool_handle
);
3717 zpool_close(zpool_handle
);
3719 if (type
== ZFS_TYPE_VOLUME
) {
3721 * If we are creating a volume, the size and block size must
3722 * satisfy a few restraints. First, the blocksize must be a
3723 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3724 * volsize must be a multiple of the block size, and cannot be
3727 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3728 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3731 "missing volume size"));
3732 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3735 if ((ret
= nvlist_lookup_uint64(props
,
3736 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3737 &blocksize
)) != 0) {
3738 if (ret
== ENOENT
) {
3739 blocksize
= zfs_prop_default_numeric(
3740 ZFS_PROP_VOLBLOCKSIZE
);
3743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3744 "missing volume block size"));
3745 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3751 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3752 "volume size cannot be zero"));
3753 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3756 if (size
% blocksize
!= 0) {
3758 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3759 "volume size must be a multiple of volume block "
3761 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3765 (void) parent_name(path
, parent
, sizeof (parent
));
3766 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3767 &wkeydata
, &wkeylen
) != 0) {
3769 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3772 /* create the dataset */
3773 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3775 if (wkeydata
!= NULL
)
3778 /* check for failure */
3782 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3783 "no such parent '%s'"), parent
);
3784 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3787 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3788 "pool must be upgraded to set this "
3789 "property or value"));
3790 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3793 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3794 "encryption root's key is not loaded "
3796 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3799 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3800 "invalid property value(s) specified"));
3801 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3805 * This platform can't address a volume this big.
3807 if (type
== ZFS_TYPE_VOLUME
)
3808 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3813 return (zfs_standard_error(hdl
, errno
, errbuf
));
3821 * Destroys the given dataset. The caller must make sure that the filesystem
3822 * isn't mounted, and that there are no active dependents. If the file system
3823 * does not exist this function does nothing.
3826 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3830 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3833 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3834 nvlist_t
*nv
= fnvlist_alloc();
3835 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3836 error
= lzc_destroy_bookmarks(nv
, NULL
);
3839 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3840 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3846 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3847 nvlist_t
*nv
= fnvlist_alloc();
3848 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3849 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3852 error
= lzc_destroy(zhp
->zfs_name
);
3855 if (error
!= 0 && error
!= ENOENT
) {
3856 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3857 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3861 remove_mountpoint(zhp
);
3866 struct destroydata
{
3868 const char *snapname
;
3872 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3874 struct destroydata
*dd
= arg
;
3875 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3878 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3879 dd
->snapname
) >= sizeof (name
))
3882 if (lzc_exists(name
))
3883 fnvlist_add_boolean(dd
->nvl
, name
);
3885 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3891 * Destroys all snapshots with the given name in zhp & descendants.
3894 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3897 struct destroydata dd
= { 0 };
3899 dd
.snapname
= snapname
;
3900 dd
.nvl
= fnvlist_alloc();
3901 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3903 if (nvlist_empty(dd
.nvl
)) {
3904 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3905 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3906 zhp
->zfs_name
, snapname
);
3908 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3910 fnvlist_free(dd
.nvl
);
3915 * Destroys all the snapshots named in the nvlist.
3918 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3920 nvlist_t
*errlist
= NULL
;
3923 int ret
= zfs_destroy_snaps_nvl_os(hdl
, snaps
);
3927 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3930 nvlist_free(errlist
);
3934 if (nvlist_empty(errlist
)) {
3935 char errbuf
[ERRBUFLEN
];
3936 (void) snprintf(errbuf
, sizeof (errbuf
),
3937 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3939 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3941 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3942 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3943 char errbuf
[ERRBUFLEN
];
3944 (void) snprintf(errbuf
, sizeof (errbuf
),
3945 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3948 switch (fnvpair_value_int32(pair
)) {
3951 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3952 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3955 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3960 nvlist_free(errlist
);
3965 * Clones the given dataset. The target must be of the same type as the source.
3968 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3970 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3972 char errbuf
[ERRBUFLEN
];
3973 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3976 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3978 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3979 "cannot create '%s'"), target
);
3981 /* validate the target/clone name */
3982 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3983 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3985 /* validate parents exist */
3986 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3989 (void) parent_name(target
, parent
, sizeof (parent
));
3994 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
3996 if (ZFS_IS_VOLUME(zhp
))
3997 type
= ZFS_TYPE_VOLUME
;
3998 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3999 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
4001 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
4007 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
4009 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
4012 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
4020 * The parent doesn't exist. We should have caught this
4021 * above, but there may a race condition that has since
4022 * destroyed the parent.
4024 * At this point, we don't know whether it's the source
4025 * that doesn't exist anymore, or whether the target
4026 * dataset doesn't exist.
4028 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4029 "no such parent '%s'"), parent
);
4030 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4033 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4034 "source and target pools differ"));
4035 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4039 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4048 * Promotes the given clone fs to be the clone parent.
4051 zfs_promote(zfs_handle_t
*zhp
)
4053 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4054 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4056 char errbuf
[ERRBUFLEN
];
4058 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4059 "cannot promote '%s'"), zhp
->zfs_name
);
4061 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4062 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4063 "snapshots can not be promoted"));
4064 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4067 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4068 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4069 "not a cloned filesystem"));
4070 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4073 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4074 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4076 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4082 * Promoting encrypted dataset outside its
4085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4086 "cannot promote dataset outside its "
4087 "encryption root"));
4088 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4091 /* There is a conflicting snapshot name. */
4092 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4093 "conflicting snapshot '%s' from parent '%s'"),
4094 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4095 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4098 return (zfs_standard_error(hdl
, ret
, errbuf
));
4104 typedef struct snapdata
{
4106 const char *sd_snapname
;
4110 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4112 snapdata_t
*sd
= arg
;
4113 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4116 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4117 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4118 sd
->sd_snapname
) >= sizeof (name
))
4121 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4123 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4131 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4135 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4138 char errbuf
[ERRBUFLEN
];
4141 zpool_handle_t
*zpool_hdl
;
4142 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4144 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4145 "cannot create snapshots "));
4148 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4149 const char *snapname
= nvpair_name(elem
);
4151 /* validate the target name */
4152 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4154 (void) snprintf(errbuf
, sizeof (errbuf
),
4155 dgettext(TEXT_DOMAIN
,
4156 "cannot create snapshot '%s'"), snapname
);
4157 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4162 * get pool handle for prop validation. assumes all snaps are in the
4163 * same pool, as does lzc_snapshot (below).
4165 elem
= nvlist_next_nvpair(snaps
, NULL
);
4166 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4167 pool
[strcspn(pool
, "/@")] = '\0';
4168 zpool_hdl
= zpool_open(hdl
, pool
);
4169 if (zpool_hdl
== NULL
)
4172 if (props
!= NULL
&&
4173 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4174 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4175 zpool_close(zpool_hdl
);
4178 zpool_close(zpool_hdl
);
4180 ret
= lzc_snapshot(snaps
, props
, &errors
);
4183 boolean_t printed
= B_FALSE
;
4184 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4186 elem
= nvlist_next_nvpair(errors
, elem
)) {
4187 (void) snprintf(errbuf
, sizeof (errbuf
),
4188 dgettext(TEXT_DOMAIN
,
4189 "cannot create snapshot '%s'"), nvpair_name(elem
));
4190 (void) zfs_standard_error(hdl
,
4191 fnvpair_value_int32(elem
), errbuf
);
4197 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4198 "multiple snapshots of same "
4200 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4204 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4210 nvlist_free(errors
);
4215 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4219 snapdata_t sd
= { 0 };
4220 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4223 char errbuf
[ERRBUFLEN
];
4225 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4226 "cannot snapshot %s"), path
);
4228 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4229 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4231 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4232 cp
= strchr(fsname
, '@');
4234 sd
.sd_snapname
= cp
+ 1;
4236 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4237 ZFS_TYPE_VOLUME
)) == NULL
) {
4241 sd
.sd_nvl
= fnvlist_alloc();
4243 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4245 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4248 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4249 fnvlist_free(sd
.sd_nvl
);
4255 * Destroy any more recent snapshots. We invoke this callback on any dependents
4256 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4257 * is a dependent and we should just destroy it without checking the transaction
4260 typedef struct rollback_data
{
4261 const char *cb_target
; /* the snapshot */
4262 uint64_t cb_create
; /* creation time reference */
4268 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4270 rollback_data_t
*cbp
= data
;
4271 prop_changelist_t
*clp
;
4273 /* We must destroy this clone; first unmount it */
4274 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4275 cbp
->cb_force
? MS_FORCE
: 0);
4276 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4277 cbp
->cb_error
= B_TRUE
;
4281 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4282 cbp
->cb_error
= B_TRUE
;
4284 changelist_remove(clp
, zhp
->zfs_name
);
4285 (void) changelist_postfix(clp
);
4286 changelist_free(clp
);
4293 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4295 rollback_data_t
*cbp
= data
;
4297 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4298 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4299 rollback_destroy_dependent
, cbp
);
4301 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4309 * Given a dataset, rollback to a specific snapshot, discarding any
4310 * data changes since then and making it the active dataset.
4312 * Any snapshots and bookmarks more recent than the target are
4313 * destroyed, along with their dependents (i.e. clones).
4316 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4318 rollback_data_t cb
= { 0 };
4320 boolean_t restore_resv
= 0;
4321 uint64_t old_volsize
= 0, new_volsize
;
4322 zfs_prop_t resv_prop
= { 0 };
4323 uint64_t min_txg
= 0;
4325 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4326 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4329 * Destroy all recent snapshots and their dependents.
4331 cb
.cb_force
= force
;
4332 cb
.cb_target
= snap
->zfs_name
;
4333 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4335 if (cb
.cb_create
> 0)
4336 min_txg
= cb
.cb_create
;
4338 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
,
4341 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4347 * Now that we have verified that the snapshot is the latest,
4348 * rollback to the given snapshot.
4351 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4352 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4354 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4356 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4360 * Pass both the filesystem and the wanted snapshot names,
4361 * we would get an error back if the snapshot is destroyed or
4362 * a new snapshot is created before this request is processed.
4364 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4366 char errbuf
[ERRBUFLEN
];
4368 (void) snprintf(errbuf
, sizeof (errbuf
),
4369 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4373 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4374 "there is a snapshot or bookmark more recent "
4375 "than '%s'"), snap
->zfs_name
);
4376 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4379 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4380 "'%s' is not found among snapshots of '%s'"),
4381 snap
->zfs_name
, zhp
->zfs_name
);
4382 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4385 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4388 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4394 * For volumes, if the pre-rollback volsize matched the pre-
4395 * rollback reservation and the volsize has changed then set
4396 * the reservation property to the post-rollback volsize.
4397 * Make a new handle since the rollback closed the dataset.
4399 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4400 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4402 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4403 if (old_volsize
!= new_volsize
)
4404 err
= zfs_prop_set_int(zhp
, resv_prop
,
4413 * Renames the given dataset.
4416 zfs_rename(zfs_handle_t
*zhp
, const char *target
, renameflags_t flags
)
4419 zfs_cmd_t zc
= {"\0"};
4421 prop_changelist_t
*cl
= NULL
;
4422 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4423 char property
[ZFS_MAXPROPLEN
];
4424 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4425 char errbuf
[ERRBUFLEN
];
4427 /* if we have the same exact name, just return success */
4428 if (strcmp(zhp
->zfs_name
, target
) == 0)
4431 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4432 "cannot rename to '%s'"), target
);
4434 /* make sure source name is valid */
4435 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4436 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4439 * Make sure the target name is valid
4441 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4442 if ((strchr(target
, '@') == NULL
) ||
4445 * Snapshot target name is abbreviated,
4446 * reconstruct full dataset name
4448 (void) strlcpy(parent
, zhp
->zfs_name
,
4450 delim
= strchr(parent
, '@');
4451 if (strchr(target
, '@') == NULL
)
4455 (void) strlcat(parent
, target
, sizeof (parent
));
4459 * Make sure we're renaming within the same dataset.
4461 delim
= strchr(target
, '@');
4462 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4463 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4465 "snapshots must be part of same "
4467 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4472 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4473 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4475 if (flags
.recursive
) {
4476 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4477 "recursive rename must be a snapshot"));
4478 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4481 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4482 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4484 /* validate parents */
4485 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4488 /* make sure we're in the same pool */
4489 verify((delim
= strchr(target
, '/')) != NULL
);
4490 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4491 zhp
->zfs_name
[delim
- target
] != '/') {
4492 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4493 "datasets must be within same pool"));
4494 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4497 /* new name cannot be a child of the current dataset name */
4498 if (is_descendant(zhp
->zfs_name
, target
)) {
4499 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4500 "New dataset name cannot be a descendant of "
4501 "current dataset name"));
4502 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4506 (void) snprintf(errbuf
, sizeof (errbuf
),
4507 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4509 if (getzoneid() == GLOBAL_ZONEID
&&
4510 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4511 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4512 "dataset is used in a non-global zone"));
4513 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4517 * Avoid unmounting file systems with mountpoint property set to
4518 * 'legacy' or 'none' even if -u option is not given.
4520 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
4521 !flags
.recursive
&& !flags
.nounmount
&&
4522 zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, property
,
4523 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) == 0 &&
4524 (strcmp(property
, "legacy") == 0 ||
4525 strcmp(property
, "none") == 0)) {
4526 flags
.nounmount
= B_TRUE
;
4528 if (flags
.recursive
) {
4529 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4530 delim
= strchr(parentname
, '@');
4532 zfs_handle_t
*zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
,
4540 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4541 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4542 flags
.nounmount
? CL_GATHER_DONT_UNMOUNT
:
4543 CL_GATHER_ITER_MOUNTED
,
4544 flags
.forceunmount
? MS_FORCE
: 0)) == NULL
)
4547 if (changelist_haszonedchild(cl
)) {
4548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4549 "child dataset with inherited mountpoint is used "
4550 "in a non-global zone"));
4551 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4556 if ((ret
= changelist_prefix(cl
)) != 0)
4560 if (ZFS_IS_VOLUME(zhp
))
4561 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4563 zc
.zc_objset_type
= DMU_OST_ZFS
;
4565 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4566 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4568 zc
.zc_cookie
= !!flags
.recursive
;
4569 zc
.zc_cookie
|= (!!flags
.nounmount
) << 1;
4571 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4573 * if it was recursive, the one that actually failed will
4576 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4577 "cannot rename '%s'"), zc
.zc_name
);
4579 if (flags
.recursive
&& errno
== EEXIST
) {
4580 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4581 "a child dataset already has a snapshot "
4582 "with the new name"));
4583 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4584 } else if (errno
== EACCES
) {
4585 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4586 "cannot move encrypted child outside of "
4587 "its encryption root"));
4588 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4590 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4594 * On failure, we still want to remount any filesystems that
4595 * were previously mounted, so we don't alter the system state.
4598 (void) changelist_postfix(cl
);
4601 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4602 ret
= changelist_postfix(cl
);
4608 changelist_free(cl
);
4614 zfs_get_all_props(zfs_handle_t
*zhp
)
4616 return (zhp
->zfs_props
);
4620 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4622 if (zhp
->zfs_recvd_props
== NULL
)
4623 if (get_recvd_props_ioctl(zhp
) != 0)
4625 return (zhp
->zfs_recvd_props
);
4629 zfs_get_user_props(zfs_handle_t
*zhp
)
4631 return (zhp
->zfs_user_props
);
4635 * This function is used by 'zfs list' to determine the exact set of columns to
4636 * display, and their maximum widths. This does two main things:
4638 * - If this is a list of all properties, then expand the list to include
4639 * all native properties, and set a flag so that for each dataset we look
4640 * for new unique user properties and add them to the list.
4642 * - For non fixed-width properties, keep track of the maximum width seen
4643 * so that we can size the column appropriately. If the user has
4644 * requested received property values, we also need to compute the width
4645 * of the RECEIVED column.
4648 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4651 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4652 zprop_list_t
*entry
;
4653 zprop_list_t
**last
, **start
;
4654 nvlist_t
*userprops
, *propval
;
4657 char buf
[ZFS_MAXPROPLEN
];
4659 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4662 userprops
= zfs_get_user_props(zhp
);
4665 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4667 * Go through and add any user properties as necessary. We
4668 * start by incrementing our list pointer to the first
4669 * non-native property.
4672 while (*start
!= NULL
) {
4673 if ((*start
)->pl_prop
== ZPROP_USERPROP
)
4675 start
= &(*start
)->pl_next
;
4679 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4681 * See if we've already found this property in our list.
4683 for (last
= start
; *last
!= NULL
;
4684 last
= &(*last
)->pl_next
) {
4685 if (strcmp((*last
)->pl_user_prop
,
4686 nvpair_name(elem
)) == 0)
4690 if (*last
== NULL
) {
4691 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
4692 entry
->pl_user_prop
=
4693 zfs_strdup(hdl
, nvpair_name(elem
));
4694 entry
->pl_prop
= ZPROP_USERPROP
;
4695 entry
->pl_width
= strlen(nvpair_name(elem
));
4696 entry
->pl_all
= B_TRUE
;
4703 * Now go through and check the width of any non-fixed columns
4705 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4706 if (entry
->pl_fixed
&& !literal
)
4709 if (entry
->pl_prop
!= ZPROP_USERPROP
) {
4710 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4711 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4712 if (strlen(buf
) > entry
->pl_width
)
4713 entry
->pl_width
= strlen(buf
);
4715 if (received
&& zfs_prop_get_recvd(zhp
,
4716 zfs_prop_to_name(entry
->pl_prop
),
4717 buf
, sizeof (buf
), literal
) == 0)
4718 if (strlen(buf
) > entry
->pl_recvd_width
)
4719 entry
->pl_recvd_width
= strlen(buf
);
4721 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4723 strval
= fnvlist_lookup_string(propval
,
4725 if (strlen(strval
) > entry
->pl_width
)
4726 entry
->pl_width
= strlen(strval
);
4728 if (received
&& zfs_prop_get_recvd(zhp
,
4729 entry
->pl_user_prop
,
4730 buf
, sizeof (buf
), literal
) == 0)
4731 if (strlen(buf
) > entry
->pl_recvd_width
)
4732 entry
->pl_recvd_width
= strlen(buf
);
4740 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4746 * Keep a reference to the props-table against which we prune the
4749 zhp
->zfs_props_table
= props
;
4751 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4754 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4755 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4758 * User properties will result in ZPROP_USERPROP (an alias
4759 * for ZPROP_INVAL), and since we
4760 * only know how to prune standard ZFS properties, we always
4761 * leave these in the list. This can also happen if we
4762 * encounter an unknown DSL property (when running older
4763 * software, for example).
4765 if (zfs_prop
!= ZPROP_USERPROP
&& props
[zfs_prop
] == B_FALSE
)
4766 (void) nvlist_remove(zhp
->zfs_props
,
4767 nvpair_name(curr
), nvpair_type(curr
));
4773 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4774 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4776 zfs_cmd_t zc
= {"\0"};
4777 nvlist_t
*nvlist
= NULL
;
4780 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4781 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4782 zc
.zc_cookie
= (uint64_t)cmd
;
4784 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4785 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4786 (void) no_memory(hdl
);
4792 case ZFS_SMB_ACL_ADD
:
4793 case ZFS_SMB_ACL_REMOVE
:
4794 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4796 case ZFS_SMB_ACL_RENAME
:
4797 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4799 (void) no_memory(hdl
);
4802 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4804 (void) no_memory(hdl
);
4807 zcmd_write_src_nvlist(hdl
, &zc
, nvlist
);
4809 case ZFS_SMB_ACL_PURGE
:
4814 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4815 nvlist_free(nvlist
);
4820 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4821 char *path
, char *resource
)
4823 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4828 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4829 char *path
, char *resource
)
4831 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4836 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4838 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4843 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4844 char *oldname
, char *newname
)
4846 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4851 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4852 zfs_userspace_cb_t func
, void *arg
)
4854 zfs_cmd_t zc
= {"\0"};
4855 zfs_useracct_t buf
[100];
4856 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4859 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4861 zc
.zc_objset_type
= type
;
4862 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4865 zfs_useracct_t
*zua
= buf
;
4867 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4868 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4869 if ((errno
== ENOTSUP
&&
4870 (type
== ZFS_PROP_USEROBJUSED
||
4871 type
== ZFS_PROP_GROUPOBJUSED
||
4872 type
== ZFS_PROP_USEROBJQUOTA
||
4873 type
== ZFS_PROP_GROUPOBJQUOTA
||
4874 type
== ZFS_PROP_PROJECTOBJUSED
||
4875 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4876 type
== ZFS_PROP_PROJECTUSED
||
4877 type
== ZFS_PROP_PROJECTQUOTA
)))
4880 return (zfs_standard_error_fmt(hdl
, errno
,
4881 dgettext(TEXT_DOMAIN
,
4882 "cannot get used/quota for %s"), zc
.zc_name
));
4884 if (zc
.zc_nvlist_dst_size
== 0)
4887 while (zc
.zc_nvlist_dst_size
> 0) {
4888 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4889 zua
->zu_space
)) != 0)
4892 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4901 const char *snapname
;
4903 boolean_t recursive
;
4908 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4910 struct holdarg
*ha
= arg
;
4911 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4914 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4915 ha
->snapname
) >= sizeof (name
))
4918 if (lzc_exists(name
))
4919 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4922 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4928 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4929 boolean_t recursive
, int cleanup_fd
)
4934 ha
.nvl
= fnvlist_alloc();
4935 ha
.snapname
= snapname
;
4937 ha
.recursive
= recursive
;
4938 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4940 if (nvlist_empty(ha
.nvl
)) {
4941 char errbuf
[ERRBUFLEN
];
4943 fnvlist_free(ha
.nvl
);
4945 (void) snprintf(errbuf
, sizeof (errbuf
),
4946 dgettext(TEXT_DOMAIN
,
4947 "cannot hold snapshot '%s@%s'"),
4948 zhp
->zfs_name
, snapname
);
4949 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4953 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4954 fnvlist_free(ha
.nvl
);
4960 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4964 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4965 char errbuf
[ERRBUFLEN
];
4969 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4972 /* There may be errors even in the success case. */
4973 fnvlist_free(errors
);
4977 if (nvlist_empty(errors
)) {
4978 /* no hold-specific errors */
4979 (void) snprintf(errbuf
, sizeof (errbuf
),
4980 dgettext(TEXT_DOMAIN
, "cannot hold"));
4983 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4984 "pool must be upgraded"));
4985 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4988 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4991 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4995 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4997 elem
= nvlist_next_nvpair(errors
, elem
)) {
4998 (void) snprintf(errbuf
, sizeof (errbuf
),
4999 dgettext(TEXT_DOMAIN
,
5000 "cannot hold snapshot '%s'"), nvpair_name(elem
));
5001 switch (fnvpair_value_int32(elem
)) {
5004 * Temporary tags wind up having the ds object id
5005 * prepended. So even if we passed the length check
5006 * above, it's still possible for the tag to wind
5007 * up being slightly too long.
5009 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5012 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5015 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5018 (void) zfs_standard_error(hdl
,
5019 fnvpair_value_int32(elem
), errbuf
);
5023 fnvlist_free(errors
);
5028 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5030 struct holdarg
*ha
= arg
;
5031 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5033 nvlist_t
*existing_holds
;
5035 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5036 ha
->snapname
) >= sizeof (name
)) {
5041 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5043 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5046 nvlist_t
*torelease
= fnvlist_alloc();
5047 fnvlist_add_boolean(torelease
, ha
->tag
);
5048 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5049 fnvlist_free(torelease
);
5053 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5059 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5060 boolean_t recursive
)
5064 nvlist_t
*errors
= NULL
;
5066 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5067 char errbuf
[ERRBUFLEN
];
5069 ha
.nvl
= fnvlist_alloc();
5070 ha
.snapname
= snapname
;
5072 ha
.recursive
= recursive
;
5074 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5076 if (nvlist_empty(ha
.nvl
)) {
5077 fnvlist_free(ha
.nvl
);
5079 (void) snprintf(errbuf
, sizeof (errbuf
),
5080 dgettext(TEXT_DOMAIN
,
5081 "cannot release hold from snapshot '%s@%s'"),
5082 zhp
->zfs_name
, snapname
);
5084 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5086 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5091 ret
= lzc_release(ha
.nvl
, &errors
);
5092 fnvlist_free(ha
.nvl
);
5095 /* There may be errors even in the success case. */
5096 fnvlist_free(errors
);
5100 if (nvlist_empty(errors
)) {
5101 /* no hold-specific errors */
5102 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5106 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5107 "pool must be upgraded"));
5108 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5111 (void) zfs_standard_error(hdl
, errno
, errbuf
);
5115 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5117 elem
= nvlist_next_nvpair(errors
, elem
)) {
5118 (void) snprintf(errbuf
, sizeof (errbuf
),
5119 dgettext(TEXT_DOMAIN
,
5120 "cannot release hold from snapshot '%s'"),
5122 switch (fnvpair_value_int32(elem
)) {
5124 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5127 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5130 (void) zfs_standard_error(hdl
,
5131 fnvpair_value_int32(elem
), errbuf
);
5135 fnvlist_free(errors
);
5140 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5142 zfs_cmd_t zc
= {"\0"};
5143 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5147 char errbuf
[ERRBUFLEN
];
5149 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5150 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5154 nvbuf
= malloc(nvsz
);
5155 if (nvbuf
== NULL
) {
5156 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5160 zc
.zc_nvlist_dst_size
= nvsz
;
5161 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5163 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5165 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5166 (void) snprintf(errbuf
, sizeof (errbuf
),
5167 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5172 nvsz
= zc
.zc_nvlist_dst_size
;
5176 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5177 "pool must be upgraded"));
5178 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5181 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5184 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5187 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5192 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5194 err
= zfs_standard_error_fmt(hdl
, rc
, dgettext(
5195 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5206 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5208 zfs_cmd_t zc
= {"\0"};
5209 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5211 char errbuf
[ERRBUFLEN
];
5215 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5216 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5218 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5221 nvbuf
= malloc(nvsz
);
5223 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5226 zc
.zc_nvlist_src_size
= nvsz
;
5227 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5228 zc
.zc_perm_action
= un
;
5230 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5232 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5233 (void) snprintf(errbuf
, sizeof (errbuf
),
5234 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5238 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5239 "pool must be upgraded"));
5240 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5243 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5246 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5249 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5260 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5263 char errbuf
[ERRBUFLEN
];
5265 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5268 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5270 (void) snprintf(errbuf
, sizeof (errbuf
),
5271 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5275 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5276 "pool must be upgraded"));
5277 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5280 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5283 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5286 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5295 * The theory of raidz space accounting
5297 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5298 * will "reference" 128KB, even though it allocates more than that, to store the
5299 * parity information (and perhaps skip sectors). This concept of the
5300 * "referenced" (and other DMU space accounting) being lower than the allocated
5301 * space by a constant factor is called "raidz deflation."
5303 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5304 * block size. However, zvols typically have a much smaller block size (default
5305 * 8KB). These smaller blocks may require proportionally much more parity
5306 * information (and perhaps skip sectors). In this case, the change to the
5307 * "referenced" property may be much more than the logical block size.
5309 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5312 * +-------+-------+-------+-------+-------+
5313 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5314 * +-------+-------+-------+-------+-------+
5315 * | P0 | D0 | D8 | D16 | D24 |
5316 * | P1 | D1 | D9 | D17 | D25 |
5317 * | P2 | D2 | D10 | D18 | D26 |
5318 * | P3 | D3 | D11 | D19 | D27 |
5319 * | P4 | D4 | D12 | D20 | D28 |
5320 * | P5 | D5 | D13 | D21 | D29 |
5321 * | P6 | D6 | D14 | D22 | D30 |
5322 * | P7 | D7 | D15 | D23 | D31 |
5323 * +-------+-------+-------+-------+-------+
5325 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5326 * sectors. The dataset's referenced will increase by 128k and the pool's
5327 * allocated and free properties will be adjusted by 160k.
5329 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5330 * blank cells represent unallocated space.
5332 * +-------+-------+-------+-------+-------+
5333 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5334 * +-------+-------+-------+-------+-------+
5336 * +-------+-------+-------+-------+-------+
5338 * Above, notice that the 4k block required one sector for parity and another
5339 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5340 * and free properties will be adjusted by 8k. The dataset will not be charged
5341 * 8k. Rather, it will be charged a value that is scaled according to the
5342 * overhead of the 128k block on the same vdev. This 8k allocation will be
5343 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5344 * calculated in the 128k block example above.
5346 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5347 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5348 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5349 * multiple of of 4 sectors. When a block does not fill the required number of
5350 * sectors, skip blocks (sectors) are used.
5352 * An 8k block being written to a raidz vdev may be written as follows:
5354 * +-------+-------+-------+-------+-------+
5355 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5356 * +-------+-------+-------+-------+-------+
5357 * | P0 | D0 | D1 | S0 | |
5358 * +-------+-------+-------+-------+-------+
5360 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5361 * added. For this 8k block, the pool's allocated and free properties are
5362 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5363 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5364 * the 128k block example above.
5366 * The situation is slightly different for dRAID since the minimum allocation
5367 * size is the full group width. The same 8K block above would be written as
5368 * follows in a dRAID group:
5370 * +-------+-------+-------+-------+-------+
5371 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5372 * +-------+-------+-------+-------+-------+
5373 * | P0 | D0 | D1 | S0 | S1 |
5374 * +-------+-------+-------+-------+-------+
5376 * Compression may lead to a variety of block sizes being written for the same
5377 * volume or file. There is no clear way to reserve just the amount of space
5378 * that will be required, so the worst case (no compression) is assumed.
5379 * Note that metadata blocks will typically be compressed, so the reservation
5380 * size returned by zvol_volsize_to_reservation() will generally be slightly
5381 * larger than the maximum that the volume can reference.
5385 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5386 * amount of space (in bytes) that will be allocated for the specified block
5387 * size. Note that the "referenced" space accounted will be less than this, but
5388 * not necessarily equal to "blksize", due to RAIDZ deflation.
5391 vdev_raidz_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5394 uint64_t asize
, ndata
;
5396 ASSERT3U(ndisks
, >, nparity
);
5397 ndata
= ndisks
- nparity
;
5398 asize
= ((blksize
- 1) >> ashift
) + 1;
5399 asize
+= nparity
* ((asize
+ ndata
- 1) / ndata
);
5400 asize
= roundup(asize
, nparity
+ 1) << ashift
;
5406 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5407 * amount of space (in bytes) that will be allocated for the specified block
5411 vdev_draid_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5414 ASSERT3U(ndisks
, >, nparity
);
5415 uint64_t ndata
= ndisks
- nparity
;
5416 uint64_t rows
= ((blksize
- 1) / (ndata
<< ashift
)) + 1;
5417 uint64_t asize
= (rows
* ndisks
) << ashift
;
5423 * Determine how much space will be allocated if it lands on the most space-
5424 * inefficient top-level vdev. Returns the size in bytes required to store one
5425 * copy of the volume data. See theory comment above.
5428 volsize_from_vdevs(zpool_handle_t
*zhp
, uint64_t nblocks
, uint64_t blksize
)
5430 nvlist_t
*config
, *tree
, **vdevs
;
5434 config
= zpool_get_config(zhp
, NULL
);
5435 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
5436 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
5437 &vdevs
, &nvdevs
) != 0) {
5438 return (nblocks
* blksize
);
5441 for (int v
= 0; v
< nvdevs
; v
++) {
5443 uint64_t nparity
, ashift
, asize
, tsize
;
5446 if (nvlist_lookup_string(vdevs
[v
], ZPOOL_CONFIG_TYPE
,
5450 if (strcmp(type
, VDEV_TYPE_RAIDZ
) != 0 &&
5451 strcmp(type
, VDEV_TYPE_DRAID
) != 0)
5454 if (nvlist_lookup_uint64(vdevs
[v
],
5455 ZPOOL_CONFIG_NPARITY
, &nparity
) != 0)
5458 if (nvlist_lookup_uint64(vdevs
[v
],
5459 ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
5462 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
5466 if (nvlist_lookup_nvlist_array(vdevs
[v
],
5467 ZPOOL_CONFIG_CHILDREN
, &disks
, &ndisks
) != 0)
5470 /* allocation size for the "typical" 128k block */
5471 tsize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5472 SPA_OLD_MAXBLOCKSIZE
);
5474 /* allocation size for the blksize block */
5475 asize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5480 if (nvlist_lookup_uint64(vdevs
[v
],
5481 ZPOOL_CONFIG_DRAID_NDATA
, &ndata
) != 0)
5484 /* allocation size for the "typical" 128k block */
5485 tsize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5486 ashift
, SPA_OLD_MAXBLOCKSIZE
);
5488 /* allocation size for the blksize block */
5489 asize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5494 * Scale this size down as a ratio of 128k / tsize.
5495 * See theory statement above.
5497 volsize
= nblocks
* asize
* SPA_OLD_MAXBLOCKSIZE
/ tsize
;
5498 if (volsize
> ret
) {
5504 ret
= nblocks
* blksize
;
5511 * Convert the zvol's volume size to an appropriate reservation. See theory
5514 * Note: If this routine is updated, it is necessary to update the ZFS test
5515 * suite's shell version in reservation.shlib.
5518 zvol_volsize_to_reservation(zpool_handle_t
*zph
, uint64_t volsize
,
5522 uint64_t nblocks
, volblocksize
;
5526 if (nvlist_lookup_string(props
,
5527 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5528 ncopies
= atoi(strval
);
5531 if (nvlist_lookup_uint64(props
,
5532 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5533 &volblocksize
) != 0)
5534 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5536 nblocks
= volsize
/ volblocksize
;
5538 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5539 * this reason, only the data blocks are scaled based on vdev config.
5541 volsize
= volsize_from_vdevs(zph
, nblocks
, volblocksize
);
5543 /* start with metadnode L0-L6 */
5545 /* calculate number of indirects */
5546 while (nblocks
> 1) {
5547 nblocks
+= DNODES_PER_LEVEL
- 1;
5548 nblocks
/= DNODES_PER_LEVEL
;
5551 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5554 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5555 * compressed, but in practice they compress down to about
5558 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;
5564 * Wait for the given activity and return the status of the wait (whether or not
5565 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5566 * reported via the 'missing' parameter, rather than by printing an error
5567 * message. This is convenient when this function is called in a loop over a
5568 * long period of time (as it is, for example, by zfs's wait cmd). In that
5569 * scenario, a fs being exported or destroyed should be considered a normal
5570 * event, so we don't want to print an error when we find that the fs doesn't
5574 zfs_wait_status(zfs_handle_t
*zhp
, zfs_wait_activity_t activity
,
5575 boolean_t
*missing
, boolean_t
*waited
)
5577 int error
= lzc_wait_fs(zhp
->zfs_name
, activity
, waited
);
5578 *missing
= (error
== ENOENT
);
5583 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
5584 dgettext(TEXT_DOMAIN
, "error waiting in fs '%s'"),