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
);
531 if (zc
->zc_objset_stats
.dds_creation_txg
!= 0) {
532 /* structure assignment */
533 zhp
->zfs_dmustats
= zc
->zc_objset_stats
;
535 if (get_stats_ioctl(zhp
, zc
) == -1) {
536 zcmd_free_nvlists(zc
);
540 if (make_dataset_handle_common(zhp
, zc
) == -1) {
541 zcmd_free_nvlists(zc
);
547 if (zhp
->zfs_dmustats
.dds_is_snapshot
||
548 strchr(zc
->zc_name
, '@') != NULL
)
549 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
550 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
551 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
552 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
553 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
559 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
561 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
566 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
567 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
568 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
569 sizeof (zhp
->zfs_name
));
570 zhp
->zfs_type
= zhp_orig
->zfs_type
;
571 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
572 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
573 if (zhp_orig
->zfs_props
!= NULL
) {
574 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
575 (void) no_memory(zhp
->zfs_hdl
);
580 if (zhp_orig
->zfs_user_props
!= NULL
) {
581 if (nvlist_dup(zhp_orig
->zfs_user_props
,
582 &zhp
->zfs_user_props
, 0) != 0) {
583 (void) no_memory(zhp
->zfs_hdl
);
588 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
589 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
590 &zhp
->zfs_recvd_props
, 0)) {
591 (void) no_memory(zhp
->zfs_hdl
);
596 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
597 if (zhp_orig
->zfs_mntopts
!= NULL
) {
598 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
599 zhp_orig
->zfs_mntopts
);
601 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
606 zfs_bookmark_exists(const char *path
)
610 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
616 (void) strlcpy(fsname
, path
, sizeof (fsname
));
617 pound
= strchr(fsname
, '#');
622 bmark_name
= pound
+ 1;
623 props
= fnvlist_alloc();
624 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
631 rv
= nvlist_exists(bmarks
, bmark_name
);
637 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
638 nvlist_t
*bmark_props
)
640 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
645 /* Fill in the name. */
646 zhp
->zfs_hdl
= parent
->zfs_hdl
;
647 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
649 /* Set the property lists. */
650 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
656 zhp
->zfs_head_type
= parent
->zfs_head_type
;
657 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
659 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
660 nvlist_free(zhp
->zfs_props
);
668 struct zfs_open_bookmarks_cb_data
{
674 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
676 struct zfs_open_bookmarks_cb_data
*dp
= data
;
679 * Is it the one we are looking for?
681 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
683 * We found it. Save it and let the caller know we are done.
690 * Not found. Close the handle and ask for another one.
697 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
698 * argument is a mask of acceptable types. The function will print an
699 * appropriate error message and return NULL if it can't be opened.
702 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
705 char errbuf
[ERRBUFLEN
];
708 (void) snprintf(errbuf
, sizeof (errbuf
),
709 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
712 * Validate the name before we even try to open it.
714 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
715 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
721 * Bookmarks needs to be handled separately.
723 bookp
= strchr(path
, '#');
726 * Try to get stats for the dataset, which will tell us if it
730 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
731 (void) zfs_standard_error(hdl
, errno
, errbuf
);
735 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
737 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
740 * We need to cut out '#' and everything after '#'
741 * to get the parent dataset name only.
743 assert(bookp
- path
< sizeof (dsname
));
744 (void) strlcpy(dsname
, path
,
745 MIN(sizeof (dsname
), bookp
- path
+ 1));
748 * Create handle for the parent dataset.
751 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
752 (void) zfs_standard_error(hdl
, errno
, errbuf
);
757 * Iterate bookmarks to find the right one.
760 if ((zfs_iter_bookmarks(pzhp
, 0, zfs_open_bookmarks_cb
,
761 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
762 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
767 if (cb_data
.zhp
== NULL
) {
768 (void) zfs_standard_error(hdl
, errno
, errbuf
);
780 if (!(types
& zhp
->zfs_type
)) {
781 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
791 * Release a ZFS handle. Nothing to do but free the associated memory.
794 zfs_close(zfs_handle_t
*zhp
)
796 if (zhp
->zfs_mntopts
)
797 free(zhp
->zfs_mntopts
);
798 nvlist_free(zhp
->zfs_props
);
799 nvlist_free(zhp
->zfs_user_props
);
800 nvlist_free(zhp
->zfs_recvd_props
);
804 typedef struct mnttab_node
{
805 struct mnttab mtn_mt
;
810 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
812 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
813 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
816 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
818 return (TREE_ISIGN(rv
));
822 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
824 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
825 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
826 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
827 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
831 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
836 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
839 while (getmntent(mnttab
, &entry
) == 0) {
843 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
846 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
847 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
848 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
849 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
850 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
852 /* Exclude duplicate mounts */
853 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
854 free(mtn
->mtn_mt
.mnt_special
);
855 free(mtn
->mtn_mt
.mnt_mountp
);
856 free(mtn
->mtn_mt
.mnt_fstype
);
857 free(mtn
->mtn_mt
.mnt_mntopts
);
862 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
865 (void) fclose(mnttab
);
870 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
875 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
877 free(mtn
->mtn_mt
.mnt_special
);
878 free(mtn
->mtn_mt
.mnt_mountp
);
879 free(mtn
->mtn_mt
.mnt_fstype
);
880 free(mtn
->mtn_mt
.mnt_mntopts
);
883 avl_destroy(&hdl
->libzfs_mnttab_cache
);
884 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
888 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
890 hdl
->libzfs_mnttab_enable
= enable
;
894 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
895 struct mnttab
*entry
)
902 if (!hdl
->libzfs_mnttab_enable
) {
903 struct mnttab srch
= { 0 };
905 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
906 libzfs_mnttab_fini(hdl
);
908 if ((mnttab
= fopen(MNTTAB
, "re")) == NULL
)
911 srch
.mnt_special
= (char *)fsname
;
912 srch
.mnt_fstype
= (char *)MNTTYPE_ZFS
;
913 ret
= getmntany(mnttab
, entry
, &srch
) ? ENOENT
: 0;
914 (void) fclose(mnttab
);
918 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
919 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
922 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
923 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
928 find
.mtn_mt
.mnt_special
= (char *)fsname
;
929 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
931 *entry
= mtn
->mtn_mt
;
934 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
939 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
940 const char *mountp
, const char *mntopts
)
944 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
945 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
946 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
947 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
948 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
949 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
950 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
952 * Another thread may have already added this entry
953 * via libzfs_mnttab_update. If so we should skip it.
955 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
) {
956 free(mtn
->mtn_mt
.mnt_special
);
957 free(mtn
->mtn_mt
.mnt_mountp
);
958 free(mtn
->mtn_mt
.mnt_fstype
);
959 free(mtn
->mtn_mt
.mnt_mntopts
);
962 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
965 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
969 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
974 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
975 find
.mtn_mt
.mnt_special
= (char *)fsname
;
976 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
978 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
979 free(ret
->mtn_mt
.mnt_special
);
980 free(ret
->mtn_mt
.mnt_mountp
);
981 free(ret
->mtn_mt
.mnt_fstype
);
982 free(ret
->mtn_mt
.mnt_mntopts
);
985 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
989 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
991 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
993 if (zpool_handle
== NULL
)
996 *spa_version
= zpool_get_prop_int(zpool_handle
,
997 ZPOOL_PROP_VERSION
, NULL
);
1002 * The choice of reservation property depends on the SPA version.
1005 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
1009 if (zfs_spa_version(zhp
, &spa_version
) < 0)
1012 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
1013 *resv_prop
= ZFS_PROP_REFRESERVATION
;
1015 *resv_prop
= ZFS_PROP_RESERVATION
;
1021 * Given an nvlist of properties to set, validates that they are correct, and
1022 * parses any numeric properties (index, boolean, etc) if they are specified as
1026 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1027 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1028 boolean_t key_params_ok
, const char *errbuf
)
1035 int chosen_normal
= -1;
1036 int chosen_utf
= -1;
1038 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1039 (void) no_memory(hdl
);
1044 * Make sure this property is valid and applies to this type.
1048 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1049 const char *propname
= nvpair_name(elem
);
1051 prop
= zfs_name_to_prop(propname
);
1052 if (prop
== ZPROP_USERPROP
&& zfs_prop_user(propname
)) {
1054 * This is a user property: make sure it's a
1055 * string, and that it's less than ZAP_MAXNAMELEN.
1057 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1058 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1059 "'%s' must be a string"), propname
);
1060 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1064 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1065 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1066 "property name '%s' is too long"),
1068 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1072 (void) nvpair_value_string(elem
, &strval
);
1073 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1074 (void) no_memory(hdl
);
1081 * Currently, only user properties can be modified on
1084 if (type
== ZFS_TYPE_SNAPSHOT
) {
1085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1086 "this property can not be modified for snapshots"));
1087 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1091 if (prop
== ZPROP_USERPROP
&& zfs_prop_userquota(propname
)) {
1092 zfs_userquota_prop_t uqtype
;
1093 char *newpropname
= NULL
;
1099 if (userquota_propname_decode(propname
, zoned
,
1100 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1102 dgettext(TEXT_DOMAIN
,
1103 "'%s' has an invalid user/group name"),
1105 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1109 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1110 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1111 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1112 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1113 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1114 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1116 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1118 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1123 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1124 (void) nvpair_value_string(elem
, &strval
);
1125 if (strcmp(strval
, "none") == 0) {
1127 } else if (zfs_nicestrtonum(hdl
,
1128 strval
, &intval
) != 0) {
1129 (void) zfs_error(hdl
,
1130 EZFS_BADPROP
, errbuf
);
1133 } else if (nvpair_type(elem
) ==
1135 (void) nvpair_value_uint64(elem
, &intval
);
1137 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1138 "use 'none' to disable "
1139 "{user|group|project}quota"));
1143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1144 "'%s' must be a number"), propname
);
1145 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1150 * Encode the prop name as
1151 * userquota@<hex-rid>-domain, to make it easy
1152 * for the kernel to decode.
1154 rc
= asprintf(&newpropname
, "%s%llx-%s",
1155 zfs_userquota_prop_prefixes
[uqtype
],
1156 (longlong_t
)rid
, domain
);
1157 if (rc
== -1 || newpropname
== NULL
) {
1158 (void) no_memory(hdl
);
1165 if (nvlist_add_uint64_array(ret
, newpropname
,
1168 (void) no_memory(hdl
);
1173 } else if (prop
== ZPROP_USERPROP
&&
1174 zfs_prop_written(propname
)) {
1175 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1176 "'%s' is readonly"),
1178 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1182 if (prop
== ZPROP_INVAL
) {
1183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1184 "invalid property '%s'"), propname
);
1185 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1189 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1191 dgettext(TEXT_DOMAIN
, "'%s' does not "
1192 "apply to datasets of this type"), propname
);
1193 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1197 if (zfs_prop_readonly(prop
) &&
1198 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1199 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1201 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1203 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1207 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1208 &strval
, &intval
, errbuf
) != 0)
1212 * Perform some additional checks for specific properties.
1215 case ZFS_PROP_VERSION
:
1221 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1222 if (intval
< version
) {
1223 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1224 "Can not downgrade; already at version %u"),
1226 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1232 case ZFS_PROP_VOLBLOCKSIZE
:
1233 case ZFS_PROP_RECORDSIZE
:
1235 int maxbs
= SPA_MAXBLOCKSIZE
;
1238 if (zpool_hdl
!= NULL
) {
1239 maxbs
= zpool_get_prop_int(zpool_hdl
,
1240 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1243 * The value must be a power of two between
1244 * SPA_MINBLOCKSIZE and maxbs.
1246 if (intval
< SPA_MINBLOCKSIZE
||
1247 intval
> maxbs
|| !ISP2(intval
)) {
1248 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1249 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1250 "'%s' must be power of 2 from 512B "
1251 "to %s"), propname
, buf
);
1252 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1258 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1260 int maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1263 if (zpool_hdl
!= NULL
) {
1264 char state
[64] = "";
1266 maxbs
= zpool_get_prop_int(zpool_hdl
,
1267 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1270 * Issue a warning but do not fail so that
1271 * tests for settable properties succeed.
1273 if (zpool_prop_get_feature(zpool_hdl
,
1274 "feature@allocation_classes", state
,
1275 sizeof (state
)) != 0 ||
1276 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1277 (void) fprintf(stderr
, gettext(
1278 "%s: property requires a special "
1279 "device in the pool\n"), propname
);
1283 (intval
< SPA_MINBLOCKSIZE
||
1284 intval
> maxbs
|| !ISP2(intval
))) {
1285 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1286 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1287 "invalid '%s=%llu' property: must be zero "
1288 "or a power of 2 from 512B to %s"),
1289 propname
, (unsigned long long)intval
, buf
);
1290 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1296 case ZFS_PROP_MLSLABEL
:
1298 #ifdef HAVE_MLSLABEL
1300 * Verify the mlslabel string and convert to
1301 * internal hex label string.
1305 char *hex
= NULL
; /* internal label string */
1307 /* Default value is already OK. */
1308 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1311 /* Verify the label can be converted to binary form */
1312 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1313 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1314 L_NO_CORRECTION
, NULL
) == -1)) {
1318 /* Now translate to hex internal label string */
1319 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1325 m_label_free(new_sl
);
1327 /* If string is already in internal form, we're done. */
1328 if (strcmp(strval
, hex
) == 0) {
1333 /* Replace the label string with the internal form. */
1334 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1336 fnvlist_add_string(ret
, zfs_prop_to_name(prop
), hex
);
1342 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1343 "invalid mlslabel '%s'"), strval
);
1344 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1345 m_label_free(new_sl
); /* OK if null */
1348 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1349 "mlslabels are unsupported"));
1350 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1352 #endif /* HAVE_MLSLABEL */
1355 case ZFS_PROP_MOUNTPOINT
:
1357 namecheck_err_t why
;
1359 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1360 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1363 if (mountpoint_namecheck(strval
, &why
)) {
1365 case NAME_ERR_LEADING_SLASH
:
1367 dgettext(TEXT_DOMAIN
,
1368 "'%s' must be an absolute path, "
1369 "'none', or 'legacy'"), propname
);
1371 case NAME_ERR_TOOLONG
:
1373 dgettext(TEXT_DOMAIN
,
1374 "component of '%s' is too long"),
1380 dgettext(TEXT_DOMAIN
,
1381 "(%d) not defined"),
1385 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1391 case ZFS_PROP_SHARESMB
:
1392 case ZFS_PROP_SHARENFS
:
1394 * For the mountpoint and sharenfs or sharesmb
1395 * properties, check if it can be set in a
1396 * global/non-global zone based on
1397 * the zoned property value:
1399 * global zone non-global zone
1400 * --------------------------------------------------
1401 * zoned=on mountpoint (no) mountpoint (yes)
1402 * sharenfs (no) sharenfs (no)
1403 * sharesmb (no) sharesmb (no)
1405 * zoned=off mountpoint (yes) N/A
1410 if (getzoneid() == GLOBAL_ZONEID
) {
1411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1412 "'%s' cannot be set on "
1413 "dataset in a non-global zone"),
1415 (void) zfs_error(hdl
, EZFS_ZONED
,
1418 } else if (prop
== ZFS_PROP_SHARENFS
||
1419 prop
== ZFS_PROP_SHARESMB
) {
1420 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1421 "'%s' cannot be set in "
1422 "a non-global zone"), propname
);
1423 (void) zfs_error(hdl
, EZFS_ZONED
,
1427 } else if (getzoneid() != GLOBAL_ZONEID
) {
1429 * If zoned property is 'off', this must be in
1430 * a global zone. If not, something is wrong.
1432 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1433 "'%s' cannot be set while dataset "
1434 "'zoned' property is set"), propname
);
1435 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1440 * At this point, it is legitimate to set the
1441 * property. Now we want to make sure that the
1442 * property value is valid if it is sharenfs.
1444 if ((prop
== ZFS_PROP_SHARENFS
||
1445 prop
== ZFS_PROP_SHARESMB
) &&
1446 strcmp(strval
, "on") != 0 &&
1447 strcmp(strval
, "off") != 0) {
1448 enum sa_protocol proto
;
1450 if (prop
== ZFS_PROP_SHARESMB
)
1451 proto
= SA_PROTOCOL_SMB
;
1453 proto
= SA_PROTOCOL_NFS
;
1455 if (sa_validate_shareopts(strval
, proto
) !=
1457 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1458 "'%s' cannot be set to invalid "
1459 "options"), propname
);
1460 (void) zfs_error(hdl
, EZFS_BADPROP
,
1468 case ZFS_PROP_KEYLOCATION
:
1469 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1470 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1471 "invalid keylocation"));
1472 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1478 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1480 if (crypt
== ZIO_CRYPT_OFF
&&
1481 strcmp(strval
, "none") != 0) {
1482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1483 "keylocation must be 'none' "
1484 "for unencrypted datasets"));
1485 (void) zfs_error(hdl
, EZFS_BADPROP
,
1488 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1489 strcmp(strval
, "none") == 0) {
1490 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1491 "keylocation must not be 'none' "
1492 "for encrypted datasets"));
1493 (void) zfs_error(hdl
, EZFS_BADPROP
,
1500 case ZFS_PROP_PBKDF2_ITERS
:
1501 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1502 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1503 "minimum pbkdf2 iterations is %u"),
1504 MIN_PBKDF2_ITERATIONS
);
1505 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1510 case ZFS_PROP_UTF8ONLY
:
1511 chosen_utf
= (int)intval
;
1514 case ZFS_PROP_NORMALIZE
:
1515 chosen_normal
= (int)intval
;
1523 * For changes to existing volumes, we have some additional
1524 * checks to enforce.
1526 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1527 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1528 ZFS_PROP_VOLBLOCKSIZE
);
1532 case ZFS_PROP_VOLSIZE
:
1533 if (intval
% blocksize
!= 0) {
1534 zfs_nicebytes(blocksize
, buf
,
1536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1537 "'%s' must be a multiple of "
1538 "volume block size (%s)"),
1540 (void) zfs_error(hdl
, EZFS_BADPROP
,
1546 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1547 "'%s' cannot be zero"),
1549 (void) zfs_error(hdl
, EZFS_BADPROP
,
1560 /* check encryption properties */
1562 int64_t crypt
= zfs_prop_get_int(zhp
,
1563 ZFS_PROP_ENCRYPTION
);
1566 case ZFS_PROP_COPIES
:
1567 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1568 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1569 "encrypted datasets cannot have "
1571 (void) zfs_error(hdl
, EZFS_BADPROP
,
1583 * If normalization was chosen, but no UTF8 choice was made,
1584 * enforce rejection of non-UTF8 names.
1586 * If normalization was chosen, but rejecting non-UTF8 names
1587 * was explicitly not chosen, it is an error.
1589 * If utf8only was turned off, but the parent has normalization,
1590 * turn off normalization.
1592 if (chosen_normal
> 0 && chosen_utf
< 0) {
1593 if (nvlist_add_uint64(ret
,
1594 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1595 (void) no_memory(hdl
);
1598 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1599 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1600 "'%s' must be set 'on' if normalization chosen"),
1601 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1602 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1604 } else if (chosen_normal
< 0 && chosen_utf
== 0) {
1605 if (nvlist_add_uint64(ret
,
1606 zfs_prop_to_name(ZFS_PROP_NORMALIZE
), 0) != 0) {
1607 (void) no_memory(hdl
);
1619 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1621 uint64_t old_volsize
;
1622 uint64_t new_volsize
;
1623 uint64_t old_reservation
;
1624 uint64_t new_reservation
;
1625 zfs_prop_t resv_prop
;
1627 zpool_handle_t
*zph
= zpool_handle(zhp
);
1630 * If this is an existing volume, and someone is setting the volsize,
1631 * make sure that it matches the reservation, or add it if necessary.
1633 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1634 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1636 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1638 props
= fnvlist_alloc();
1639 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1640 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1642 if ((zvol_volsize_to_reservation(zph
, old_volsize
, props
) !=
1643 old_reservation
) || nvlist_exists(nvl
,
1644 zfs_prop_to_name(resv_prop
))) {
1645 fnvlist_free(props
);
1648 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1649 &new_volsize
) != 0) {
1650 fnvlist_free(props
);
1653 new_reservation
= zvol_volsize_to_reservation(zph
, new_volsize
, props
);
1654 fnvlist_free(props
);
1656 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1657 new_reservation
) != 0) {
1658 (void) no_memory(zhp
->zfs_hdl
);
1665 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1666 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1667 * Return codes must match zfs_add_synthetic_resv().
1670 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1677 if (!ZFS_IS_VOLUME(zhp
)) {
1681 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1685 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1689 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1690 /* No value being set, so it can't be "auto" */
1693 if (resvsize
!= UINT64_MAX
) {
1694 /* Being set to a value other than "auto" */
1698 props
= fnvlist_alloc();
1700 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1701 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1703 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1705 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1708 resvsize
= zvol_volsize_to_reservation(zpool_handle(zhp
), volsize
,
1710 fnvlist_free(props
);
1712 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1713 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1714 (void) no_memory(zhp
->zfs_hdl
);
1721 zfs_is_namespace_prop(zfs_prop_t prop
)
1725 case ZFS_PROP_ATIME
:
1726 case ZFS_PROP_RELATIME
:
1727 case ZFS_PROP_DEVICES
:
1729 case ZFS_PROP_SETUID
:
1730 case ZFS_PROP_READONLY
:
1731 case ZFS_PROP_XATTR
:
1732 case ZFS_PROP_NBMAND
:
1741 * Given a property name and value, set the property for the given dataset.
1744 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1747 char errbuf
[ERRBUFLEN
];
1748 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1749 nvlist_t
*nvl
= NULL
;
1751 (void) snprintf(errbuf
, sizeof (errbuf
),
1752 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1755 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1756 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1757 (void) no_memory(hdl
);
1761 ret
= zfs_prop_set_list(zhp
, nvl
);
1771 * Given an nvlist of property names and values, set the properties for the
1775 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1777 zfs_cmd_t zc
= {"\0"};
1779 prop_changelist_t
**cls
= NULL
;
1781 char errbuf
[ERRBUFLEN
];
1782 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1786 zfs_prop_t prop
= 0;
1789 (void) snprintf(errbuf
, sizeof (errbuf
),
1790 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1793 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1794 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1795 B_FALSE
, errbuf
)) == NULL
)
1799 * We have to check for any extra properties which need to be added
1800 * before computing the length of the nvlist.
1802 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1804 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1805 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1806 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1811 if (added_resv
!= 1 &&
1812 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1817 * Check how many properties we're setting and allocate an array to
1818 * store changelist pointers for postfix().
1820 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1822 elem
= nvlist_next_nvpair(nvl
, elem
))
1824 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1828 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1830 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1832 prop
= zfs_name_to_prop(nvpair_name(elem
));
1834 assert(cl_idx
< nvl_len
);
1836 * We don't want to unmount & remount the dataset when changing
1837 * its canmount property to 'on' or 'noauto'. We only use
1838 * the changelist logic to unmount when setting canmount=off.
1840 if (prop
!= ZFS_PROP_CANMOUNT
||
1841 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1842 zfs_is_mounted(zhp
, NULL
))) {
1843 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1844 if (cls
[cl_idx
] == NULL
)
1848 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1849 changelist_haszonedchild(cls
[cl_idx
])) {
1850 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1851 "child dataset with inherited mountpoint is used "
1852 "in a non-global zone"));
1853 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1857 if (cls
[cl_idx
] != NULL
&&
1858 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1863 assert(cl_idx
== nvl_len
);
1866 * Execute the corresponding ioctl() to set this list of properties.
1868 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1870 zcmd_write_src_nvlist(hdl
, &zc
, nvl
);
1871 zcmd_alloc_dst_nvlist(hdl
, &zc
, 0);
1873 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1876 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1877 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1881 /* Get the list of unset properties back and report them. */
1882 nvlist_t
*errorprops
= NULL
;
1883 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1885 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1887 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1888 prop
= zfs_name_to_prop(nvpair_name(elem
));
1889 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1891 nvlist_free(errorprops
);
1893 if (added_resv
&& errno
== ENOSPC
) {
1894 /* clean up the volsize property we tried to set */
1895 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1899 zcmd_free_nvlists(&zc
);
1901 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1903 if (nvlist_add_uint64(nvl
,
1904 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1907 zcmd_write_src_nvlist(hdl
, &zc
, nvl
);
1908 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1911 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1912 if (cls
[cl_idx
] != NULL
) {
1913 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1921 * Refresh the statistics so the new property
1922 * value is reflected.
1924 (void) get_stats(zhp
);
1927 * Remount the filesystem to propagate the change
1928 * if one of the options handled by the generic
1929 * Linux namespace layer has been modified.
1931 if (zfs_is_namespace_prop(prop
) &&
1932 zfs_is_mounted(zhp
, NULL
))
1933 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1939 zcmd_free_nvlists(&zc
);
1941 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1942 if (cls
[cl_idx
] != NULL
)
1943 changelist_free(cls
[cl_idx
]);
1951 * Given a property, inherit the value from the parent dataset, or if received
1952 * is TRUE, revert to the received value, if any.
1955 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1957 zfs_cmd_t zc
= {"\0"};
1959 prop_changelist_t
*cl
;
1960 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1961 char errbuf
[ERRBUFLEN
];
1964 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1965 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1967 zc
.zc_cookie
= received
;
1968 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_USERPROP
) {
1970 * For user properties, the amount of work we have to do is very
1971 * small, so just do it here.
1973 if (!zfs_prop_user(propname
)) {
1974 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1975 "invalid property"));
1976 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1979 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1980 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1982 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1983 return (zfs_standard_error(hdl
, errno
, errbuf
));
1985 (void) get_stats(zhp
);
1990 * Verify that this property is inheritable.
1992 if (zfs_prop_readonly(prop
))
1993 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1995 if (!zfs_prop_inheritable(prop
) && !received
)
1996 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1999 * Check to see if the value applies to this type
2001 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2002 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
2005 * Normalize the name, to get rid of shorthand abbreviations.
2007 propname
= zfs_prop_to_name(prop
);
2008 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2009 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2011 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
2012 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
2013 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2014 "dataset is used in a non-global zone"));
2015 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
2019 * Determine datasets which will be affected by this change, if any.
2021 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2024 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2025 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2026 "child dataset with inherited mountpoint is used "
2027 "in a non-global zone"));
2028 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2032 if ((ret
= changelist_prefix(cl
)) != 0)
2035 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0) {
2036 changelist_free(cl
);
2037 return (zfs_standard_error(hdl
, errno
, errbuf
));
2040 if ((ret
= changelist_postfix(cl
)) != 0)
2044 * Refresh the statistics so the new property is reflected.
2046 (void) get_stats(zhp
);
2049 * Remount the filesystem to propagate the change
2050 * if one of the options handled by the generic
2051 * Linux namespace layer has been modified.
2053 if (zfs_is_namespace_prop(prop
) &&
2054 zfs_is_mounted(zhp
, NULL
))
2055 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2059 changelist_free(cl
);
2064 * True DSL properties are stored in an nvlist. The following two functions
2065 * extract them appropriately.
2068 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2074 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2075 zfs_prop_to_name(prop
), &nv
) == 0) {
2076 value
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
2077 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2079 verify(!zhp
->zfs_props_table
||
2080 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2081 value
= zfs_prop_default_numeric(prop
);
2082 *source
= (char *)"";
2089 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2095 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2096 zfs_prop_to_name(prop
), &nv
) == 0) {
2097 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2098 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2100 verify(!zhp
->zfs_props_table
||
2101 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2102 value
= zfs_prop_default_string(prop
);
2103 *source
= (char *)"";
2110 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2112 return (zhp
->zfs_props
!= NULL
&&
2113 zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2117 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uintptr_t *cookie
)
2119 *cookie
= (uintptr_t)zhp
->zfs_props
;
2120 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2124 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uintptr_t *cookie
)
2126 zhp
->zfs_props
= (nvlist_t
*)*cookie
;
2131 * Internal function for getting a numeric property. Both zfs_prop_get() and
2132 * zfs_prop_get_int() are built using this interface.
2134 * Certain properties can be overridden using 'mount -o'. In this case, scan
2135 * the contents of the /proc/self/mounts entry, searching for the
2136 * appropriate options. If they differ from the on-disk values, report the
2137 * current values and mark the source "temporary".
2140 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2141 char **source
, uint64_t *val
)
2143 zfs_cmd_t zc
= {"\0"};
2144 nvlist_t
*zplprops
= NULL
;
2146 const char *mntopt_on
= NULL
;
2147 const char *mntopt_off
= NULL
;
2148 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2153 * If the property is being fetched for a snapshot, check whether
2154 * the property is valid for the snapshot's head dataset type.
2156 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2157 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2158 *val
= zfs_prop_default_numeric(prop
);
2163 case ZFS_PROP_ATIME
:
2164 mntopt_on
= MNTOPT_ATIME
;
2165 mntopt_off
= MNTOPT_NOATIME
;
2168 case ZFS_PROP_RELATIME
:
2169 mntopt_on
= MNTOPT_RELATIME
;
2170 mntopt_off
= MNTOPT_NORELATIME
;
2173 case ZFS_PROP_DEVICES
:
2174 mntopt_on
= MNTOPT_DEVICES
;
2175 mntopt_off
= MNTOPT_NODEVICES
;
2179 mntopt_on
= MNTOPT_EXEC
;
2180 mntopt_off
= MNTOPT_NOEXEC
;
2183 case ZFS_PROP_READONLY
:
2184 mntopt_on
= MNTOPT_RO
;
2185 mntopt_off
= MNTOPT_RW
;
2188 case ZFS_PROP_SETUID
:
2189 mntopt_on
= MNTOPT_SETUID
;
2190 mntopt_off
= MNTOPT_NOSETUID
;
2193 case ZFS_PROP_XATTR
:
2194 mntopt_on
= MNTOPT_XATTR
;
2195 mntopt_off
= MNTOPT_NOXATTR
;
2198 case ZFS_PROP_NBMAND
:
2199 mntopt_on
= MNTOPT_NBMAND
;
2200 mntopt_off
= MNTOPT_NONBMAND
;
2208 * Because looking up the mount options is potentially expensive
2209 * (iterating over all of /proc/self/mounts), we defer its
2210 * calculation until we're looking up a property which requires
2213 if (!zhp
->zfs_mntcheck
&&
2214 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2215 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2216 struct mnttab entry
;
2218 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0)
2219 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2222 zhp
->zfs_mntcheck
= B_TRUE
;
2225 if (zhp
->zfs_mntopts
== NULL
)
2226 mnt
.mnt_mntopts
= (char *)"";
2228 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2231 case ZFS_PROP_ATIME
:
2232 case ZFS_PROP_RELATIME
:
2233 case ZFS_PROP_DEVICES
:
2235 case ZFS_PROP_READONLY
:
2236 case ZFS_PROP_SETUID
:
2238 case ZFS_PROP_XATTR
:
2240 case ZFS_PROP_NBMAND
:
2241 *val
= getprop_uint64(zhp
, prop
, source
);
2246 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2249 *src
= ZPROP_SRC_TEMPORARY
;
2250 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2253 *src
= ZPROP_SRC_TEMPORARY
;
2257 case ZFS_PROP_CANMOUNT
:
2258 case ZFS_PROP_VOLSIZE
:
2259 case ZFS_PROP_QUOTA
:
2260 case ZFS_PROP_REFQUOTA
:
2261 case ZFS_PROP_RESERVATION
:
2262 case ZFS_PROP_REFRESERVATION
:
2263 case ZFS_PROP_FILESYSTEM_LIMIT
:
2264 case ZFS_PROP_SNAPSHOT_LIMIT
:
2265 case ZFS_PROP_FILESYSTEM_COUNT
:
2266 case ZFS_PROP_SNAPSHOT_COUNT
:
2267 *val
= getprop_uint64(zhp
, prop
, source
);
2269 if (*source
== NULL
) {
2270 /* not default, must be local */
2271 *source
= zhp
->zfs_name
;
2275 case ZFS_PROP_MOUNTED
:
2276 *val
= (zhp
->zfs_mntopts
!= NULL
);
2279 case ZFS_PROP_NUMCLONES
:
2280 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2283 case ZFS_PROP_VERSION
:
2284 case ZFS_PROP_NORMALIZE
:
2285 case ZFS_PROP_UTF8ONLY
:
2287 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0);
2289 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2290 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2291 zcmd_free_nvlists(&zc
);
2292 if (prop
== ZFS_PROP_VERSION
&&
2293 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2294 *val
= zfs_prop_default_numeric(prop
);
2297 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2298 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2300 zcmd_free_nvlists(&zc
);
2303 nvlist_free(zplprops
);
2304 zcmd_free_nvlists(&zc
);
2307 case ZFS_PROP_INCONSISTENT
:
2308 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2311 case ZFS_PROP_REDACTED
:
2312 *val
= zhp
->zfs_dmustats
.dds_redacted
;
2316 if (zhp
->zfs_dmustats
.dds_guid
!= 0)
2317 *val
= zhp
->zfs_dmustats
.dds_guid
;
2319 *val
= getprop_uint64(zhp
, prop
, source
);
2322 case ZFS_PROP_CREATETXG
:
2324 * We can directly read createtxg property from zfs
2325 * handle for Filesystem, Snapshot and ZVOL types.
2327 if (((zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) ||
2328 (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) ||
2329 (zhp
->zfs_type
== ZFS_TYPE_VOLUME
)) &&
2330 (zhp
->zfs_dmustats
.dds_creation_txg
!= 0)) {
2331 *val
= zhp
->zfs_dmustats
.dds_creation_txg
;
2334 *val
= getprop_uint64(zhp
, prop
, source
);
2338 switch (zfs_prop_get_type(prop
)) {
2339 case PROP_TYPE_NUMBER
:
2340 case PROP_TYPE_INDEX
:
2341 *val
= getprop_uint64(zhp
, prop
, source
);
2343 * If we tried to use a default value for a
2344 * readonly property, it means that it was not
2345 * present. Note this only applies to "truly"
2346 * readonly properties, not set-once properties
2347 * like volblocksize.
2349 if (zfs_prop_readonly(prop
) &&
2350 !zfs_prop_setonce(prop
) &&
2351 *source
!= NULL
&& (*source
)[0] == '\0') {
2357 case PROP_TYPE_STRING
:
2359 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2360 "cannot get non-numeric property"));
2361 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2362 dgettext(TEXT_DOMAIN
, "internal error")));
2370 * Calculate the source type, given the raw source string.
2373 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2374 char *statbuf
, size_t statlen
)
2376 if (statbuf
== NULL
||
2377 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2381 if (source
== NULL
) {
2382 *srctype
= ZPROP_SRC_NONE
;
2383 } else if (source
[0] == '\0') {
2384 *srctype
= ZPROP_SRC_DEFAULT
;
2385 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2386 *srctype
= ZPROP_SRC_RECEIVED
;
2388 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2389 *srctype
= ZPROP_SRC_LOCAL
;
2391 (void) strlcpy(statbuf
, source
, statlen
);
2392 *srctype
= ZPROP_SRC_INHERITED
;
2399 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2400 size_t proplen
, boolean_t literal
)
2405 if (zhp
->zfs_recvd_props
== NULL
)
2406 if (get_recvd_props_ioctl(zhp
) != 0)
2409 prop
= zfs_name_to_prop(propname
);
2411 if (prop
!= ZPROP_USERPROP
) {
2413 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2415 zfs_set_recvd_props_mode(zhp
, &cookie
);
2416 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2417 NULL
, NULL
, 0, literal
);
2418 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2422 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2423 propname
, &propval
) != 0)
2425 recvdval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
2426 (void) strlcpy(propbuf
, recvdval
, proplen
);
2429 return (err
== 0 ? 0 : -1);
2433 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2438 value
= zfs_get_clones_nvl(zhp
);
2439 if (value
== NULL
|| nvlist_empty(value
))
2443 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2444 pair
= nvlist_next_nvpair(value
, pair
)) {
2445 if (propbuf
[0] != '\0')
2446 (void) strlcat(propbuf
, ",", proplen
);
2447 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2453 struct get_clones_arg
{
2457 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2461 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2463 struct get_clones_arg
*gca
= arg
;
2465 if (gca
->numclones
== 0) {
2470 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2471 NULL
, NULL
, 0, B_TRUE
) != 0)
2473 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2474 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2479 (void) zfs_iter_children(zhp
, 0, get_clones_cb
, gca
);
2485 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2487 nvlist_t
*nv
, *value
;
2489 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2490 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2491 struct get_clones_arg gca
;
2494 * if this is a snapshot, then the kernel wasn't able
2495 * to get the clones. Do it by slowly iterating.
2497 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2499 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2501 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2506 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2508 gca
.origin
= zhp
->zfs_name
;
2510 if (gca
.numclones
!= 0) {
2512 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2515 /* get the pool name */
2516 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2517 (void) strsep(&cp
, "/@");
2518 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2519 ZFS_TYPE_FILESYSTEM
);
2526 (void) get_clones_cb(root
, &gca
);
2529 if (gca
.numclones
!= 0 ||
2530 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2531 nvlist_add_nvlist(zhp
->zfs_props
,
2532 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2539 nv
= fnvlist_lookup_nvlist(zhp
->zfs_props
,
2540 zfs_prop_to_name(ZFS_PROP_CLONES
));
2543 return (fnvlist_lookup_nvlist(nv
, ZPROP_VALUE
));
2547 get_rsnaps_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2553 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2554 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
), &value
) != 0)
2556 if (nvlist_lookup_uint64_array(value
, ZPROP_VALUE
, &snaps
,
2560 /* There's no redaction snapshots; pass a special value back */
2561 (void) snprintf(propbuf
, proplen
, "none");
2565 for (int i
= 0; i
< nsnaps
; i
++) {
2567 if (propbuf
[0] != '\0')
2568 (void) strlcat(propbuf
, ",", proplen
);
2569 (void) snprintf(buf
, sizeof (buf
), "%llu",
2570 (u_longlong_t
)snaps
[i
]);
2571 (void) strlcat(propbuf
, buf
, proplen
);
2578 * Accepts a property and value and checks that the value
2579 * matches the one found by the channel program. If they are
2580 * not equal, print both of them.
2583 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2586 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2589 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2590 const char *prop_name
= zfs_prop_to_name(prop
);
2591 const char *program
=
2593 "ds = args['dataset']\n"
2594 "prop = args['property']\n"
2595 "value, setpoint = zfs.get_prop(ds, prop)\n"
2596 "return {value=value, setpoint=setpoint}\n";
2599 nvlist_t
*argnvl
= fnvlist_alloc();
2601 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2602 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2604 error
= lzc_channel_program_nosync(poolname
, program
,
2605 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2608 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2609 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2611 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2613 (void) fprintf(stderr
, "%s: zcp check error: "
2614 "%u\n", prop_name
, error
);
2617 if (ans
!= intval
) {
2618 (void) fprintf(stderr
, "%s: zfs found %llu, "
2619 "but zcp found %llu\n", prop_name
,
2620 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2624 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2626 (void) fprintf(stderr
, "%s: zcp check error: "
2627 "%u\n", prop_name
, error
);
2630 if (strcmp(strval
, str_ans
) != 0) {
2631 (void) fprintf(stderr
,
2632 "%s: zfs found '%s', but zcp found '%s'\n",
2633 prop_name
, strval
, str_ans
);
2637 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2638 "error: %u\n", prop_name
, error
);
2640 nvlist_free(argnvl
);
2641 nvlist_free(outnvl
);
2645 * Retrieve a property from the given object. If 'literal' is specified, then
2646 * numbers are left as exact values. Otherwise, numbers are converted to a
2647 * human-readable form.
2649 * Returns 0 on success, or -1 on error.
2652 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2653 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2655 char *source
= NULL
;
2659 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2662 * Check to see if this property applies to our object
2664 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2667 if (received
&& zfs_prop_readonly(prop
))
2671 *src
= ZPROP_SRC_NONE
;
2674 case ZFS_PROP_CREATION
:
2676 * 'creation' is a time_t stored in the statistics. We convert
2677 * this into a string unless 'literal' is specified.
2680 val
= getprop_uint64(zhp
, prop
, &source
);
2681 time_t time
= (time_t)val
;
2685 localtime_r(&time
, &t
) == NULL
||
2686 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2688 (void) snprintf(propbuf
, proplen
, "%llu",
2691 zcp_check(zhp
, prop
, val
, NULL
);
2694 case ZFS_PROP_MOUNTPOINT
:
2696 * Getting the precise mountpoint can be tricky.
2698 * - for 'none' or 'legacy', return those values.
2699 * - for inherited mountpoints, we want to take everything
2700 * after our ancestor and append it to the inherited value.
2702 * If the pool has an alternate root, we want to prepend that
2703 * root to any values we return.
2706 str
= getprop_string(zhp
, prop
, &source
);
2708 if (str
[0] == '/') {
2709 char buf
[MAXPATHLEN
];
2711 const char *relpath
;
2714 * If we inherit the mountpoint, even from a dataset
2715 * with a received value, the source will be the path of
2716 * the dataset we inherit from. If source is
2717 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2720 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2723 relpath
= zhp
->zfs_name
+ strlen(source
);
2724 if (relpath
[0] == '/')
2728 if ((zpool_get_prop(zhp
->zpool_hdl
,
2729 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2730 B_FALSE
)) || (strcmp(root
, "-") == 0))
2733 * Special case an alternate root of '/'. This will
2734 * avoid having multiple leading slashes in the
2737 if (strcmp(root
, "/") == 0)
2741 * If the mountpoint is '/' then skip over this
2742 * if we are obtaining either an alternate root or
2743 * an inherited mountpoint.
2745 if (str
[1] == '\0' && (root
[0] != '\0' ||
2746 relpath
[0] != '\0'))
2749 if (relpath
[0] == '\0')
2750 (void) snprintf(propbuf
, proplen
, "%s%s",
2753 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2754 root
, str
, relpath
[0] == '@' ? "" : "/",
2757 /* 'legacy' or 'none' */
2758 (void) strlcpy(propbuf
, str
, proplen
);
2760 zcp_check(zhp
, prop
, 0, propbuf
);
2763 case ZFS_PROP_ORIGIN
:
2764 if (*zhp
->zfs_dmustats
.dds_origin
!= '\0') {
2765 str
= (char *)&zhp
->zfs_dmustats
.dds_origin
;
2767 str
= getprop_string(zhp
, prop
, &source
);
2769 if (str
== NULL
|| *str
== '\0')
2770 str
= zfs_prop_default_string(prop
);
2773 (void) strlcpy(propbuf
, str
, proplen
);
2774 zcp_check(zhp
, prop
, 0, str
);
2777 case ZFS_PROP_REDACT_SNAPS
:
2778 if (get_rsnaps_string(zhp
, propbuf
, proplen
) != 0)
2782 case ZFS_PROP_CLONES
:
2783 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2787 case ZFS_PROP_QUOTA
:
2788 case ZFS_PROP_REFQUOTA
:
2789 case ZFS_PROP_RESERVATION
:
2790 case ZFS_PROP_REFRESERVATION
:
2792 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2795 * If quota or reservation is 0, we translate this into 'none'
2796 * (unless literal is set), and indicate that it's the default
2797 * value. Otherwise, we print the number nicely and indicate
2798 * that its set locally.
2802 (void) strlcpy(propbuf
, "0", proplen
);
2804 (void) strlcpy(propbuf
, "none", proplen
);
2807 (void) snprintf(propbuf
, proplen
, "%llu",
2810 zfs_nicebytes(val
, propbuf
, proplen
);
2812 zcp_check(zhp
, prop
, val
, NULL
);
2815 case ZFS_PROP_FILESYSTEM_LIMIT
:
2816 case ZFS_PROP_SNAPSHOT_LIMIT
:
2817 case ZFS_PROP_FILESYSTEM_COUNT
:
2818 case ZFS_PROP_SNAPSHOT_COUNT
:
2820 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2824 * If limit is UINT64_MAX, we translate this into 'none', and
2825 * indicate that it's the default value. Otherwise, we print
2826 * the number nicely and indicate that it's set locally.
2828 if (val
== UINT64_MAX
) {
2829 (void) strlcpy(propbuf
, "none", proplen
);
2830 } else if (literal
) {
2831 (void) snprintf(propbuf
, proplen
, "%llu",
2834 zfs_nicenum(val
, propbuf
, proplen
);
2837 zcp_check(zhp
, prop
, val
, NULL
);
2840 case ZFS_PROP_REFRATIO
:
2841 case ZFS_PROP_COMPRESSRATIO
:
2842 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2845 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2846 (u_longlong_t
)(val
/ 100),
2847 (u_longlong_t
)(val
% 100));
2849 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2850 (u_longlong_t
)(val
/ 100),
2851 (u_longlong_t
)(val
% 100));
2852 zcp_check(zhp
, prop
, val
, NULL
);
2856 switch (zhp
->zfs_type
) {
2857 case ZFS_TYPE_FILESYSTEM
:
2860 case ZFS_TYPE_VOLUME
:
2863 case ZFS_TYPE_SNAPSHOT
:
2866 case ZFS_TYPE_BOOKMARK
:
2872 (void) snprintf(propbuf
, proplen
, "%s", str
);
2873 zcp_check(zhp
, prop
, 0, propbuf
);
2876 case ZFS_PROP_MOUNTED
:
2878 * The 'mounted' property is a pseudo-property that described
2879 * whether the filesystem is currently mounted. Even though
2880 * it's a boolean value, the typical values of "on" and "off"
2881 * don't make sense, so we translate to "yes" and "no".
2883 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2884 src
, &source
, &val
) != 0)
2887 (void) strlcpy(propbuf
, "yes", proplen
);
2889 (void) strlcpy(propbuf
, "no", proplen
);
2894 * The 'name' property is a pseudo-property derived from the
2895 * dataset name. It is presented as a real property to simplify
2898 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2899 zcp_check(zhp
, prop
, 0, propbuf
);
2902 case ZFS_PROP_MLSLABEL
:
2904 #ifdef HAVE_MLSLABEL
2905 m_label_t
*new_sl
= NULL
;
2906 char *ascii
= NULL
; /* human readable label */
2908 (void) strlcpy(propbuf
,
2909 getprop_string(zhp
, prop
, &source
), proplen
);
2911 if (literal
|| (strcasecmp(propbuf
,
2912 ZFS_MLSLABEL_DEFAULT
) == 0))
2916 * Try to translate the internal hex string to
2917 * human-readable output. If there are any
2918 * problems just use the hex string.
2921 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2922 L_NO_CORRECTION
, NULL
) == -1) {
2923 m_label_free(new_sl
);
2927 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2931 m_label_free(new_sl
);
2934 m_label_free(new_sl
);
2936 (void) strlcpy(propbuf
, ascii
, proplen
);
2939 (void) strlcpy(propbuf
,
2940 getprop_string(zhp
, prop
, &source
), proplen
);
2941 #endif /* HAVE_MLSLABEL */
2946 case ZFS_PROP_KEY_GUID
:
2947 case ZFS_PROP_IVSET_GUID
:
2948 case ZFS_PROP_CREATETXG
:
2949 case ZFS_PROP_OBJSETID
:
2950 case ZFS_PROP_PBKDF2_ITERS
:
2952 * These properties are stored as numbers, but they are
2953 * identifiers or counters.
2954 * We don't want them to be pretty printed, because pretty
2955 * printing truncates their values making them useless.
2957 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2959 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2960 zcp_check(zhp
, prop
, val
, NULL
);
2963 case ZFS_PROP_REFERENCED
:
2964 case ZFS_PROP_AVAILABLE
:
2966 case ZFS_PROP_USEDSNAP
:
2967 case ZFS_PROP_USEDDS
:
2968 case ZFS_PROP_USEDREFRESERV
:
2969 case ZFS_PROP_USEDCHILD
:
2970 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2973 (void) snprintf(propbuf
, proplen
, "%llu",
2976 zfs_nicebytes(val
, propbuf
, proplen
);
2978 zcp_check(zhp
, prop
, val
, NULL
);
2981 case ZFS_PROP_SNAPSHOTS_CHANGED
:
2983 if ((get_numeric_property(zhp
, prop
, src
, &source
,
2984 &val
) != 0) || val
== 0) {
2988 time_t time
= (time_t)val
;
2992 localtime_r(&time
, &t
) == NULL
||
2993 strftime(propbuf
, proplen
, "%a %b %e %k:%M:%S %Y",
2995 (void) snprintf(propbuf
, proplen
, "%llu",
2998 zcp_check(zhp
, prop
, val
, NULL
);
3002 switch (zfs_prop_get_type(prop
)) {
3003 case PROP_TYPE_NUMBER
:
3004 if (get_numeric_property(zhp
, prop
, src
,
3005 &source
, &val
) != 0) {
3010 (void) snprintf(propbuf
, proplen
, "%llu",
3013 zfs_nicenum(val
, propbuf
, proplen
);
3015 zcp_check(zhp
, prop
, val
, NULL
);
3018 case PROP_TYPE_STRING
:
3019 str
= getprop_string(zhp
, prop
, &source
);
3023 (void) strlcpy(propbuf
, str
, proplen
);
3024 zcp_check(zhp
, prop
, 0, str
);
3027 case PROP_TYPE_INDEX
:
3028 if (get_numeric_property(zhp
, prop
, src
,
3029 &source
, &val
) != 0)
3031 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
3034 (void) strlcpy(propbuf
, strval
, proplen
);
3035 zcp_check(zhp
, prop
, 0, strval
);
3043 get_source(zhp
, src
, source
, statbuf
, statlen
);
3049 * Utility function to get the given numeric property. Does no validation that
3050 * the given property is the appropriate type; should only be used with
3051 * hard-coded property types.
3054 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
3059 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3065 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3069 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3070 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3074 * Similar to zfs_prop_get(), but returns the value as an integer.
3077 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3078 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3083 * Check to see if this property applies to our object
3085 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3086 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3087 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3088 zfs_prop_to_name(prop
)));
3092 *src
= ZPROP_SRC_NONE
;
3094 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3097 get_source(zhp
, src
, source
, statbuf
, statlen
);
3104 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3105 char **domainp
, idmap_rid_t
*ridp
)
3107 idmap_get_handle_t
*get_hdl
= NULL
;
3111 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3115 err
= idmap_get_sidbyuid(get_hdl
, id
,
3116 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3118 err
= idmap_get_sidbygid(get_hdl
, id
,
3119 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3121 if (err
== IDMAP_SUCCESS
&&
3122 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3123 status
== IDMAP_SUCCESS
)
3129 idmap_get_destroy(get_hdl
);
3132 #endif /* HAVE_IDMAP */
3135 * convert the propname into parameters needed by kernel
3136 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3137 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3138 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3139 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3140 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3141 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3144 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3145 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3147 zfs_userquota_prop_t type
;
3151 boolean_t isproject
;
3157 /* Figure out the property type ({user|group|project}{quota|space}) */
3158 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3159 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3160 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3163 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3167 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3168 type
== ZFS_PROP_USEROBJQUOTA
||
3169 type
== ZFS_PROP_USEROBJUSED
);
3170 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3171 type
== ZFS_PROP_GROUPOBJQUOTA
||
3172 type
== ZFS_PROP_GROUPOBJUSED
);
3173 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3174 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3175 type
== ZFS_PROP_PROJECTOBJUSED
);
3177 cp
= strchr(propname
, '@') + 1;
3179 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3180 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3183 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3184 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3187 } else if (!isproject
&& strchr(cp
, '@')) {
3190 * It's a SID name (eg "user@domain") that needs to be
3191 * turned into S-1-domainID-RID.
3193 directory_error_t e
;
3194 char *numericsid
= NULL
;
3197 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3200 e
= directory_sid_from_user_name(NULL
,
3203 e
= directory_sid_from_group_name(NULL
,
3207 directory_error_free(e
);
3210 if (numericsid
== NULL
)
3213 (void) strlcpy(domain
, cp
, domainlen
);
3214 cp
= strrchr(domain
, '-');
3219 *ridp
= strtoull(cp
, &end
, 10);
3222 if (errno
!= 0 || *end
!= '\0')
3227 #endif /* HAVE_IDMAP */
3229 /* It's a user/group/project ID (eg "12345"). */
3232 id
= strtoul(cp
, &end
, 10);
3235 if (id
> MAXUID
&& !isproject
) {
3237 /* It's an ephemeral ID. */
3241 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3242 &mapdomain
, &rid
) != 0)
3244 (void) strlcpy(domain
, mapdomain
, domainlen
);
3248 #endif /* HAVE_IDMAP */
3258 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3259 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3262 zfs_cmd_t zc
= {"\0"};
3264 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3266 err
= userquota_propname_decode(propname
,
3267 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3268 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3269 zc
.zc_objset_type
= *typep
;
3273 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3277 *propvalue
= zc
.zc_cookie
;
3282 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3283 uint64_t *propvalue
)
3285 zfs_userquota_prop_t type
;
3287 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3292 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3293 char *propbuf
, int proplen
, boolean_t literal
)
3297 zfs_userquota_prop_t type
;
3299 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3306 (void) snprintf(propbuf
, proplen
, "%llu",
3307 (u_longlong_t
)propvalue
);
3308 } else if (propvalue
== 0 &&
3309 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3310 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3311 type
== ZFS_PROP_PROJECTQUOTA
||
3312 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3313 (void) strlcpy(propbuf
, "none", proplen
);
3314 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3315 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3316 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3317 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3319 zfs_nicenum(propvalue
, propbuf
, proplen
);
3325 * propname must start with "written@" or "written#".
3328 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3329 uint64_t *propvalue
)
3332 zfs_cmd_t zc
= {"\0"};
3333 const char *snapname
;
3335 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3337 assert(zfs_prop_written(propname
));
3338 snapname
= propname
+ strlen("written@");
3339 if (strchr(snapname
, '@') != NULL
|| strchr(snapname
, '#') != NULL
) {
3340 /* full snapshot or bookmark name specified */
3341 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3343 /* snapname is the short name, append it to zhp's fsname */
3346 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3347 sizeof (zc
.zc_value
));
3348 cp
= strchr(zc
.zc_value
, '@');
3351 (void) strlcat(zc
.zc_value
, snapname
- 1, sizeof (zc
.zc_value
));
3354 err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3358 *propvalue
= zc
.zc_cookie
;
3363 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3364 char *propbuf
, int proplen
, boolean_t literal
)
3369 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3375 (void) snprintf(propbuf
, proplen
, "%llu",
3376 (u_longlong_t
)propvalue
);
3378 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3385 * Returns the name of the given zfs handle.
3388 zfs_get_name(const zfs_handle_t
*zhp
)
3390 return (zhp
->zfs_name
);
3394 * Returns the name of the parent pool for the given zfs handle.
3397 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3399 return (zhp
->zpool_hdl
->zpool_name
);
3403 * Returns the type of the given zfs handle.
3406 zfs_get_type(const zfs_handle_t
*zhp
)
3408 return (zhp
->zfs_type
);
3412 * Returns the type of the given zfs handle,
3413 * or, if a snapshot, the type of the snapshotted dataset.
3416 zfs_get_underlying_type(const zfs_handle_t
*zhp
)
3418 return (zhp
->zfs_head_type
);
3422 * Is one dataset name a child dataset of another?
3424 * Needs to handle these cases:
3425 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3426 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3427 * Descendant? No. No. No. Yes.
3430 is_descendant(const char *ds1
, const char *ds2
)
3432 size_t d1len
= strlen(ds1
);
3434 /* ds2 can't be a descendant if it's smaller */
3435 if (strlen(ds2
) < d1len
)
3438 /* otherwise, compare strings and verify that there's a '/' char */
3439 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3443 * Given a complete name, return just the portion that refers to the parent.
3444 * Will return -1 if there is no parent (path is just the name of the
3448 parent_name(const char *path
, char *buf
, size_t buflen
)
3452 (void) strlcpy(buf
, path
, buflen
);
3454 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3462 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3464 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3468 * If accept_ancestor is false, then check to make sure that the given path has
3469 * a parent, and that it exists. If accept_ancestor is true, then find the
3470 * closest existing ancestor for the given path. In prefixlen return the
3471 * length of already existing prefix of the given path. We also fetch the
3472 * 'zoned' property, which is used to validate property settings when creating
3476 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3477 boolean_t accept_ancestor
, int *prefixlen
)
3479 zfs_cmd_t zc
= {"\0"};
3480 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3483 char errbuf
[ERRBUFLEN
];
3486 (void) snprintf(errbuf
, sizeof (errbuf
),
3487 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3489 /* get parent, and check to see if this is just a pool */
3490 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3491 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3492 "missing dataset name"));
3493 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3496 /* check to see if the pool exists */
3497 if ((slash
= strchr(parent
, '/')) == NULL
)
3498 slash
= parent
+ strlen(parent
);
3499 (void) strlcpy(zc
.zc_name
, parent
,
3500 MIN(sizeof (zc
.zc_name
), slash
- parent
+ 1));
3501 if (zfs_ioctl(hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3503 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3504 "no such pool '%s'"), zc
.zc_name
);
3505 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3508 /* check to see if the parent dataset exists */
3509 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3510 if (errno
== ENOENT
&& accept_ancestor
) {
3512 * Go deeper to find an ancestor, give up on top level.
3514 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3515 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3516 "no such pool '%s'"), zc
.zc_name
);
3517 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3519 } else if (errno
== ENOENT
) {
3520 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3521 "parent does not exist"));
3522 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3524 return (zfs_standard_error(hdl
, errno
, errbuf
));
3527 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3531 /* we are in a non-global zone, but parent is in the global zone */
3532 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3533 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3538 /* make sure parent is a filesystem */
3539 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3540 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3541 "parent is not a filesystem"));
3542 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3548 if (prefixlen
!= NULL
)
3549 *prefixlen
= strlen(parent
);
3554 * Finds whether the dataset of the given type(s) exists.
3557 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3561 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3565 * Try to get stats for the dataset, which will tell us if it exists.
3567 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3568 int ds_type
= zhp
->zfs_type
;
3571 if (types
& ds_type
)
3578 * Given a path to 'target', create all the ancestors between
3579 * the prefixlen portion of the path, and the target itself.
3580 * Fail if the initial prefixlen-ancestor does not already exist.
3583 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3589 /* make sure prefix exists */
3590 cp
= target
+ prefixlen
;
3592 assert(strchr(cp
, '/') == NULL
);
3593 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3596 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3604 * Attempt to create, mount, and share any ancestor filesystems,
3605 * up to the prefixlen-long one.
3607 for (cp
= target
+ prefixlen
+ 1;
3608 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3612 h
= make_dataset_handle(hdl
, target
);
3614 /* it already exists, nothing to do here */
3619 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3621 opname
= dgettext(TEXT_DOMAIN
, "create");
3625 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3627 opname
= dgettext(TEXT_DOMAIN
, "open");
3631 if (zfs_mount(h
, NULL
, 0) != 0) {
3632 opname
= dgettext(TEXT_DOMAIN
, "mount");
3636 if (zfs_share(h
, NULL
) != 0) {
3637 opname
= dgettext(TEXT_DOMAIN
, "share");
3643 zfs_commit_shares(NULL
);
3648 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3649 "failed to %s ancestor '%s'"), opname
, target
);
3654 * Creates non-existing ancestors of the given path.
3657 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3661 char errbuf
[ERRBUFLEN
];
3664 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3665 "cannot create '%s'"), path
);
3668 * Check that we are not passing the nesting limit
3669 * before we start creating any ancestors.
3671 if (dataset_nestcheck(path
) != 0) {
3672 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3673 "maximum name nesting depth exceeded"));
3674 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3677 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3680 if ((path_copy
= strdup(path
)) != NULL
) {
3681 rc
= create_parents(hdl
, path_copy
, prefix
);
3684 if (path_copy
== NULL
|| rc
!= 0)
3691 * Create a new filesystem or volume.
3694 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3699 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3701 enum lzc_dataset_type ost
;
3702 zpool_handle_t
*zpool_handle
;
3703 uint8_t *wkeydata
= NULL
;
3705 char errbuf
[ERRBUFLEN
];
3706 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3708 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3709 "cannot create '%s'"), path
);
3711 /* validate the path, taking care to note the extended error message */
3712 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3713 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3715 if (dataset_nestcheck(path
) != 0) {
3716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3717 "maximum name nesting depth exceeded"));
3718 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3721 /* validate parents exist */
3722 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3726 * The failure modes when creating a dataset of a different type over
3727 * one that already exists is a little strange. In particular, if you
3728 * try to create a dataset on top of an existing dataset, the ioctl()
3729 * will return ENOENT, not EEXIST. To prevent this from happening, we
3730 * first try to see if the dataset exists.
3732 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3733 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3734 "dataset already exists"));
3735 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3738 if (type
== ZFS_TYPE_VOLUME
)
3739 ost
= LZC_DATSET_TYPE_ZVOL
;
3741 ost
= LZC_DATSET_TYPE_ZFS
;
3743 /* open zpool handle for prop validation */
3744 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3745 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3747 /* truncate pool_path at first slash */
3748 char *p
= strchr(pool_path
, '/');
3752 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3755 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3756 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3757 zpool_close(zpool_handle
);
3760 zpool_close(zpool_handle
);
3762 if (type
== ZFS_TYPE_VOLUME
) {
3764 * If we are creating a volume, the size and block size must
3765 * satisfy a few restraints. First, the blocksize must be a
3766 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3767 * volsize must be a multiple of the block size, and cannot be
3770 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3771 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3773 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3774 "missing volume size"));
3775 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3778 if ((ret
= nvlist_lookup_uint64(props
,
3779 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3780 &blocksize
)) != 0) {
3781 if (ret
== ENOENT
) {
3782 blocksize
= zfs_prop_default_numeric(
3783 ZFS_PROP_VOLBLOCKSIZE
);
3786 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3787 "missing volume block size"));
3788 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3794 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3795 "volume size cannot be zero"));
3796 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3799 if (size
% blocksize
!= 0) {
3801 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3802 "volume size must be a multiple of volume block "
3804 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3808 (void) parent_name(path
, parent
, sizeof (parent
));
3809 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3810 &wkeydata
, &wkeylen
) != 0) {
3812 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3815 /* create the dataset */
3816 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3818 if (wkeydata
!= NULL
)
3821 /* check for failure */
3825 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3826 "no such parent '%s'"), parent
);
3827 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3830 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3831 "pool must be upgraded to set this "
3832 "property or value"));
3833 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3836 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3837 "encryption root's key is not loaded "
3839 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3842 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3843 "invalid property value(s) specified"));
3844 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3848 * This platform can't address a volume this big.
3850 if (type
== ZFS_TYPE_VOLUME
)
3851 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3856 return (zfs_standard_error(hdl
, errno
, errbuf
));
3864 * Destroys the given dataset. The caller must make sure that the filesystem
3865 * isn't mounted, and that there are no active dependents. If the file system
3866 * does not exist this function does nothing.
3869 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3873 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3876 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3877 nvlist_t
*nv
= fnvlist_alloc();
3878 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3879 error
= lzc_destroy_bookmarks(nv
, NULL
);
3882 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3883 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3889 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3890 nvlist_t
*nv
= fnvlist_alloc();
3891 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3892 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3895 error
= lzc_destroy(zhp
->zfs_name
);
3898 if (error
!= 0 && error
!= ENOENT
) {
3899 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3900 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3904 remove_mountpoint(zhp
);
3909 struct destroydata
{
3911 const char *snapname
;
3915 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3917 struct destroydata
*dd
= arg
;
3918 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3921 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3922 dd
->snapname
) >= sizeof (name
))
3925 if (lzc_exists(name
))
3926 fnvlist_add_boolean(dd
->nvl
, name
);
3928 rv
= zfs_iter_filesystems(zhp
, 0, zfs_check_snap_cb
, dd
);
3934 * Destroys all snapshots with the given name in zhp & descendants.
3937 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3940 struct destroydata dd
= { 0 };
3942 dd
.snapname
= snapname
;
3943 dd
.nvl
= fnvlist_alloc();
3944 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3946 if (nvlist_empty(dd
.nvl
)) {
3947 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3948 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3949 zhp
->zfs_name
, snapname
);
3951 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3953 fnvlist_free(dd
.nvl
);
3958 * Destroys all the snapshots named in the nvlist.
3961 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3963 nvlist_t
*errlist
= NULL
;
3966 int ret
= zfs_destroy_snaps_nvl_os(hdl
, snaps
);
3970 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3973 nvlist_free(errlist
);
3977 if (nvlist_empty(errlist
)) {
3978 char errbuf
[ERRBUFLEN
];
3979 (void) snprintf(errbuf
, sizeof (errbuf
),
3980 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3982 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3984 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3985 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3986 char errbuf
[ERRBUFLEN
];
3987 (void) snprintf(errbuf
, sizeof (errbuf
),
3988 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3991 switch (fnvpair_value_int32(pair
)) {
3994 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3995 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3998 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
4003 nvlist_free(errlist
);
4008 * Clones the given dataset. The target must be of the same type as the source.
4011 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
4013 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4015 char errbuf
[ERRBUFLEN
];
4016 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4019 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
4021 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4022 "cannot create '%s'"), target
);
4024 /* validate the target/clone name */
4025 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
4026 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4028 /* validate parents exist */
4029 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
4032 (void) parent_name(target
, parent
, sizeof (parent
));
4037 zfs_type_t type
= ZFS_TYPE_FILESYSTEM
;
4039 if (ZFS_IS_VOLUME(zhp
))
4040 type
= ZFS_TYPE_VOLUME
;
4041 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
4042 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
4044 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
4050 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
4052 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
4055 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
4063 * The parent doesn't exist. We should have caught this
4064 * above, but there may a race condition that has since
4065 * destroyed the parent.
4067 * At this point, we don't know whether it's the source
4068 * that doesn't exist anymore, or whether the target
4069 * dataset doesn't exist.
4071 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4072 "no such parent '%s'"), parent
);
4073 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4076 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4077 "source and target pools differ"));
4078 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4082 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4091 * Promotes the given clone fs to be the clone parent.
4094 zfs_promote(zfs_handle_t
*zhp
)
4096 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4097 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4099 char errbuf
[ERRBUFLEN
];
4101 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4102 "cannot promote '%s'"), zhp
->zfs_name
);
4104 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4105 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4106 "snapshots can not be promoted"));
4107 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4110 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4111 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4112 "not a cloned filesystem"));
4113 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4116 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4117 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4119 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4125 * Promoting encrypted dataset outside its
4128 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4129 "cannot promote dataset outside its "
4130 "encryption root"));
4131 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4134 /* There is a conflicting snapshot name. */
4135 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4136 "conflicting snapshot '%s' from parent '%s'"),
4137 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4138 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4141 return (zfs_standard_error(hdl
, ret
, errbuf
));
4147 typedef struct snapdata
{
4149 const char *sd_snapname
;
4153 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4155 snapdata_t
*sd
= arg
;
4156 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4159 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4160 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4161 sd
->sd_snapname
) >= sizeof (name
))
4164 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4166 rv
= zfs_iter_filesystems(zhp
, 0, zfs_snapshot_cb
, sd
);
4174 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4178 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4181 char errbuf
[ERRBUFLEN
];
4184 zpool_handle_t
*zpool_hdl
;
4185 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4187 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4188 "cannot create snapshots "));
4191 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4192 const char *snapname
= nvpair_name(elem
);
4194 /* validate the target name */
4195 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4197 (void) snprintf(errbuf
, sizeof (errbuf
),
4198 dgettext(TEXT_DOMAIN
,
4199 "cannot create snapshot '%s'"), snapname
);
4200 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4205 * get pool handle for prop validation. assumes all snaps are in the
4206 * same pool, as does lzc_snapshot (below).
4208 elem
= nvlist_next_nvpair(snaps
, NULL
);
4211 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4212 pool
[strcspn(pool
, "/@")] = '\0';
4213 zpool_hdl
= zpool_open(hdl
, pool
);
4214 if (zpool_hdl
== NULL
)
4217 if (props
!= NULL
&&
4218 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4219 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4220 zpool_close(zpool_hdl
);
4223 zpool_close(zpool_hdl
);
4225 ret
= lzc_snapshot(snaps
, props
, &errors
);
4228 boolean_t printed
= B_FALSE
;
4229 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4231 elem
= nvlist_next_nvpair(errors
, elem
)) {
4232 (void) snprintf(errbuf
, sizeof (errbuf
),
4233 dgettext(TEXT_DOMAIN
,
4234 "cannot create snapshot '%s'"), nvpair_name(elem
));
4235 (void) zfs_standard_error(hdl
,
4236 fnvpair_value_int32(elem
), errbuf
);
4242 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4243 "multiple snapshots of same "
4245 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4249 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4255 nvlist_free(errors
);
4260 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4264 snapdata_t sd
= { 0 };
4265 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4268 char errbuf
[ERRBUFLEN
];
4270 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4271 "cannot snapshot %s"), path
);
4273 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4274 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4276 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4277 cp
= strchr(fsname
, '@');
4279 sd
.sd_snapname
= cp
+ 1;
4281 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4282 ZFS_TYPE_VOLUME
)) == NULL
) {
4286 sd
.sd_nvl
= fnvlist_alloc();
4288 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4290 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4293 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4294 fnvlist_free(sd
.sd_nvl
);
4300 * Destroy any more recent snapshots. We invoke this callback on any dependents
4301 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4302 * is a dependent and we should just destroy it without checking the transaction
4305 typedef struct rollback_data
{
4306 const char *cb_target
; /* the snapshot */
4307 uint64_t cb_create
; /* creation time reference */
4313 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4315 rollback_data_t
*cbp
= data
;
4316 prop_changelist_t
*clp
;
4318 /* We must destroy this clone; first unmount it */
4319 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4320 cbp
->cb_force
? MS_FORCE
: 0);
4321 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4322 cbp
->cb_error
= B_TRUE
;
4326 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4327 cbp
->cb_error
= B_TRUE
;
4329 changelist_remove(clp
, zhp
->zfs_name
);
4330 (void) changelist_postfix(clp
);
4331 changelist_free(clp
);
4338 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4340 rollback_data_t
*cbp
= data
;
4342 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4343 cbp
->cb_error
|= zfs_iter_dependents(zhp
, 0, B_FALSE
,
4344 rollback_destroy_dependent
, cbp
);
4346 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4354 * Given a dataset, rollback to a specific snapshot, discarding any
4355 * data changes since then and making it the active dataset.
4357 * Any snapshots and bookmarks more recent than the target are
4358 * destroyed, along with their dependents (i.e. clones).
4361 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4363 rollback_data_t cb
= { 0 };
4365 boolean_t restore_resv
= 0;
4366 uint64_t old_volsize
= 0, new_volsize
;
4367 zfs_prop_t resv_prop
= { 0 };
4368 uint64_t min_txg
= 0;
4370 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4371 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4374 * Destroy all recent snapshots and their dependents.
4376 cb
.cb_force
= force
;
4377 cb
.cb_target
= snap
->zfs_name
;
4378 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4380 if (cb
.cb_create
> 0)
4381 min_txg
= cb
.cb_create
;
4383 (void) zfs_iter_snapshots(zhp
, 0, rollback_destroy
, &cb
,
4386 (void) zfs_iter_bookmarks(zhp
, 0, rollback_destroy
, &cb
);
4392 * Now that we have verified that the snapshot is the latest,
4393 * rollback to the given snapshot.
4396 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4397 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4399 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4401 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4405 * Pass both the filesystem and the wanted snapshot names,
4406 * we would get an error back if the snapshot is destroyed or
4407 * a new snapshot is created before this request is processed.
4409 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4411 char errbuf
[ERRBUFLEN
];
4413 (void) snprintf(errbuf
, sizeof (errbuf
),
4414 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4418 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4419 "there is a snapshot or bookmark more recent "
4420 "than '%s'"), snap
->zfs_name
);
4421 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4424 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4425 "'%s' is not found among snapshots of '%s'"),
4426 snap
->zfs_name
, zhp
->zfs_name
);
4427 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4430 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4433 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4439 * For volumes, if the pre-rollback volsize matched the pre-
4440 * rollback reservation and the volsize has changed then set
4441 * the reservation property to the post-rollback volsize.
4442 * Make a new handle since the rollback closed the dataset.
4444 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4445 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4447 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4448 if (old_volsize
!= new_volsize
)
4449 err
= zfs_prop_set_int(zhp
, resv_prop
,
4458 * Renames the given dataset.
4461 zfs_rename(zfs_handle_t
*zhp
, const char *target
, renameflags_t flags
)
4464 zfs_cmd_t zc
= {"\0"};
4466 prop_changelist_t
*cl
= NULL
;
4467 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4468 char property
[ZFS_MAXPROPLEN
];
4469 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4470 char errbuf
[ERRBUFLEN
];
4472 /* if we have the same exact name, just return success */
4473 if (strcmp(zhp
->zfs_name
, target
) == 0)
4476 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4477 "cannot rename to '%s'"), target
);
4479 /* make sure source name is valid */
4480 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4481 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4484 * Make sure the target name is valid
4486 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4487 if ((strchr(target
, '@') == NULL
) ||
4490 * Snapshot target name is abbreviated,
4491 * reconstruct full dataset name
4493 (void) strlcpy(parent
, zhp
->zfs_name
,
4495 delim
= strchr(parent
, '@');
4496 if (strchr(target
, '@') == NULL
)
4500 (void) strlcat(parent
, target
, sizeof (parent
));
4504 * Make sure we're renaming within the same dataset.
4506 delim
= strchr(target
, '@');
4507 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4508 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4509 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4510 "snapshots must be part of same "
4512 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4517 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4518 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4520 if (flags
.recursive
) {
4521 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4522 "recursive rename must be a snapshot"));
4523 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4526 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4527 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4529 /* validate parents */
4530 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4533 /* make sure we're in the same pool */
4534 verify((delim
= strchr(target
, '/')) != NULL
);
4535 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4536 zhp
->zfs_name
[delim
- target
] != '/') {
4537 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4538 "datasets must be within same pool"));
4539 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4542 /* new name cannot be a child of the current dataset name */
4543 if (is_descendant(zhp
->zfs_name
, target
)) {
4544 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4545 "New dataset name cannot be a descendant of "
4546 "current dataset name"));
4547 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4551 (void) snprintf(errbuf
, sizeof (errbuf
),
4552 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4554 if (getzoneid() == GLOBAL_ZONEID
&&
4555 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4557 "dataset is used in a non-global zone"));
4558 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4562 * Avoid unmounting file systems with mountpoint property set to
4563 * 'legacy' or 'none' even if -u option is not given.
4565 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
4566 !flags
.recursive
&& !flags
.nounmount
&&
4567 zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, property
,
4568 sizeof (property
), NULL
, NULL
, 0, B_FALSE
) == 0 &&
4569 (strcmp(property
, "legacy") == 0 ||
4570 strcmp(property
, "none") == 0)) {
4571 flags
.nounmount
= B_TRUE
;
4573 if (flags
.recursive
) {
4574 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4575 delim
= strchr(parentname
, '@');
4577 zfs_handle_t
*zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
,
4585 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4586 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4587 flags
.nounmount
? CL_GATHER_DONT_UNMOUNT
:
4588 CL_GATHER_ITER_MOUNTED
,
4589 flags
.forceunmount
? MS_FORCE
: 0)) == NULL
)
4592 if (changelist_haszonedchild(cl
)) {
4593 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4594 "child dataset with inherited mountpoint is used "
4595 "in a non-global zone"));
4596 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4601 if ((ret
= changelist_prefix(cl
)) != 0)
4605 if (ZFS_IS_VOLUME(zhp
))
4606 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4608 zc
.zc_objset_type
= DMU_OST_ZFS
;
4610 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4611 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4613 zc
.zc_cookie
= !!flags
.recursive
;
4614 zc
.zc_cookie
|= (!!flags
.nounmount
) << 1;
4616 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4618 * if it was recursive, the one that actually failed will
4621 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4622 "cannot rename '%s'"), zc
.zc_name
);
4624 if (flags
.recursive
&& errno
== EEXIST
) {
4625 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4626 "a child dataset already has a snapshot "
4627 "with the new name"));
4628 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4629 } else if (errno
== EACCES
) {
4630 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4631 "cannot move encrypted child outside of "
4632 "its encryption root"));
4633 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4635 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4639 * On failure, we still want to remount any filesystems that
4640 * were previously mounted, so we don't alter the system state.
4643 (void) changelist_postfix(cl
);
4646 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4647 ret
= changelist_postfix(cl
);
4653 changelist_free(cl
);
4659 zfs_get_all_props(zfs_handle_t
*zhp
)
4661 return (zhp
->zfs_props
);
4665 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4667 if (zhp
->zfs_recvd_props
== NULL
)
4668 if (get_recvd_props_ioctl(zhp
) != 0)
4670 return (zhp
->zfs_recvd_props
);
4674 zfs_get_user_props(zfs_handle_t
*zhp
)
4676 return (zhp
->zfs_user_props
);
4680 * This function is used by 'zfs list' to determine the exact set of columns to
4681 * display, and their maximum widths. This does two main things:
4683 * - If this is a list of all properties, then expand the list to include
4684 * all native properties, and set a flag so that for each dataset we look
4685 * for new unique user properties and add them to the list.
4687 * - For non fixed-width properties, keep track of the maximum width seen
4688 * so that we can size the column appropriately. If the user has
4689 * requested received property values, we also need to compute the width
4690 * of the RECEIVED column.
4693 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4696 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4697 zprop_list_t
*entry
;
4698 zprop_list_t
**last
, **start
;
4699 nvlist_t
*userprops
, *propval
;
4702 char buf
[ZFS_MAXPROPLEN
];
4704 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4707 userprops
= zfs_get_user_props(zhp
);
4710 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4712 * Go through and add any user properties as necessary. We
4713 * start by incrementing our list pointer to the first
4714 * non-native property.
4717 while (*start
!= NULL
) {
4718 if ((*start
)->pl_prop
== ZPROP_USERPROP
)
4720 start
= &(*start
)->pl_next
;
4724 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4726 * See if we've already found this property in our list.
4728 for (last
= start
; *last
!= NULL
;
4729 last
= &(*last
)->pl_next
) {
4730 if (strcmp((*last
)->pl_user_prop
,
4731 nvpair_name(elem
)) == 0)
4735 if (*last
== NULL
) {
4736 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
4737 entry
->pl_user_prop
=
4738 zfs_strdup(hdl
, nvpair_name(elem
));
4739 entry
->pl_prop
= ZPROP_USERPROP
;
4740 entry
->pl_width
= strlen(nvpair_name(elem
));
4741 entry
->pl_all
= B_TRUE
;
4748 * Now go through and check the width of any non-fixed columns
4750 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4751 if (entry
->pl_fixed
&& !literal
)
4754 if (entry
->pl_prop
!= ZPROP_USERPROP
) {
4755 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4756 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4757 if (strlen(buf
) > entry
->pl_width
)
4758 entry
->pl_width
= strlen(buf
);
4760 if (received
&& zfs_prop_get_recvd(zhp
,
4761 zfs_prop_to_name(entry
->pl_prop
),
4762 buf
, sizeof (buf
), literal
) == 0)
4763 if (strlen(buf
) > entry
->pl_recvd_width
)
4764 entry
->pl_recvd_width
= strlen(buf
);
4766 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4768 strval
= fnvlist_lookup_string(propval
,
4770 if (strlen(strval
) > entry
->pl_width
)
4771 entry
->pl_width
= strlen(strval
);
4773 if (received
&& zfs_prop_get_recvd(zhp
,
4774 entry
->pl_user_prop
,
4775 buf
, sizeof (buf
), literal
) == 0)
4776 if (strlen(buf
) > entry
->pl_recvd_width
)
4777 entry
->pl_recvd_width
= strlen(buf
);
4785 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4791 * Keep a reference to the props-table against which we prune the
4794 zhp
->zfs_props_table
= props
;
4796 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4799 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4800 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4803 * User properties will result in ZPROP_USERPROP (an alias
4804 * for ZPROP_INVAL), and since we
4805 * only know how to prune standard ZFS properties, we always
4806 * leave these in the list. This can also happen if we
4807 * encounter an unknown DSL property (when running older
4808 * software, for example).
4810 if (zfs_prop
!= ZPROP_USERPROP
&& props
[zfs_prop
] == B_FALSE
)
4811 (void) nvlist_remove(zhp
->zfs_props
,
4812 nvpair_name(curr
), nvpair_type(curr
));
4818 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4819 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4821 zfs_cmd_t zc
= {"\0"};
4822 nvlist_t
*nvlist
= NULL
;
4825 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4826 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4827 zc
.zc_cookie
= (uint64_t)cmd
;
4829 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4830 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4831 (void) no_memory(hdl
);
4837 case ZFS_SMB_ACL_ADD
:
4838 case ZFS_SMB_ACL_REMOVE
:
4839 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4841 case ZFS_SMB_ACL_RENAME
:
4842 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4844 (void) no_memory(hdl
);
4847 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4849 (void) no_memory(hdl
);
4852 zcmd_write_src_nvlist(hdl
, &zc
, nvlist
);
4854 case ZFS_SMB_ACL_PURGE
:
4859 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4860 nvlist_free(nvlist
);
4865 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4866 char *path
, char *resource
)
4868 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4873 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4874 char *path
, char *resource
)
4876 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4881 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4883 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4888 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4889 char *oldname
, char *newname
)
4891 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4896 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4897 zfs_userspace_cb_t func
, void *arg
)
4899 zfs_cmd_t zc
= {"\0"};
4900 zfs_useracct_t buf
[100];
4901 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4904 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4906 zc
.zc_objset_type
= type
;
4907 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4910 zfs_useracct_t
*zua
= buf
;
4912 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4913 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4914 if ((errno
== ENOTSUP
&&
4915 (type
== ZFS_PROP_USEROBJUSED
||
4916 type
== ZFS_PROP_GROUPOBJUSED
||
4917 type
== ZFS_PROP_USEROBJQUOTA
||
4918 type
== ZFS_PROP_GROUPOBJQUOTA
||
4919 type
== ZFS_PROP_PROJECTOBJUSED
||
4920 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4921 type
== ZFS_PROP_PROJECTUSED
||
4922 type
== ZFS_PROP_PROJECTQUOTA
)))
4925 return (zfs_standard_error_fmt(hdl
, errno
,
4926 dgettext(TEXT_DOMAIN
,
4927 "cannot get used/quota for %s"), zc
.zc_name
));
4929 if (zc
.zc_nvlist_dst_size
== 0)
4932 while (zc
.zc_nvlist_dst_size
> 0) {
4933 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4934 zua
->zu_space
)) != 0)
4937 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4946 const char *snapname
;
4948 boolean_t recursive
;
4953 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4955 struct holdarg
*ha
= arg
;
4956 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4959 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4960 ha
->snapname
) >= sizeof (name
))
4963 if (lzc_exists(name
))
4964 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4967 rv
= zfs_iter_filesystems(zhp
, 0, zfs_hold_one
, ha
);
4973 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4974 boolean_t recursive
, int cleanup_fd
)
4979 ha
.nvl
= fnvlist_alloc();
4980 ha
.snapname
= snapname
;
4982 ha
.recursive
= recursive
;
4983 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4985 if (nvlist_empty(ha
.nvl
)) {
4986 char errbuf
[ERRBUFLEN
];
4988 fnvlist_free(ha
.nvl
);
4990 (void) snprintf(errbuf
, sizeof (errbuf
),
4991 dgettext(TEXT_DOMAIN
,
4992 "cannot hold snapshot '%s@%s'"),
4993 zhp
->zfs_name
, snapname
);
4994 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4998 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4999 fnvlist_free(ha
.nvl
);
5005 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
5009 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5010 char errbuf
[ERRBUFLEN
];
5014 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
5017 /* There may be errors even in the success case. */
5018 fnvlist_free(errors
);
5022 if (nvlist_empty(errors
)) {
5023 /* no hold-specific errors */
5024 (void) snprintf(errbuf
, sizeof (errbuf
),
5025 dgettext(TEXT_DOMAIN
, "cannot hold"));
5028 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5029 "pool must be upgraded"));
5030 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5033 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5036 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5040 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5042 elem
= nvlist_next_nvpair(errors
, elem
)) {
5043 (void) snprintf(errbuf
, sizeof (errbuf
),
5044 dgettext(TEXT_DOMAIN
,
5045 "cannot hold snapshot '%s'"), nvpair_name(elem
));
5046 switch (fnvpair_value_int32(elem
)) {
5049 * Temporary tags wind up having the ds object id
5050 * prepended. So even if we passed the length check
5051 * above, it's still possible for the tag to wind
5052 * up being slightly too long.
5054 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5057 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5060 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5063 (void) zfs_standard_error(hdl
,
5064 fnvpair_value_int32(elem
), errbuf
);
5068 fnvlist_free(errors
);
5073 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5075 struct holdarg
*ha
= arg
;
5076 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5078 nvlist_t
*existing_holds
;
5080 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5081 ha
->snapname
) >= sizeof (name
)) {
5086 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5088 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5091 nvlist_t
*torelease
= fnvlist_alloc();
5092 fnvlist_add_boolean(torelease
, ha
->tag
);
5093 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5094 fnvlist_free(torelease
);
5098 rv
= zfs_iter_filesystems(zhp
, 0, zfs_release_one
, ha
);
5104 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5105 boolean_t recursive
)
5109 nvlist_t
*errors
= NULL
;
5111 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5112 char errbuf
[ERRBUFLEN
];
5114 ha
.nvl
= fnvlist_alloc();
5115 ha
.snapname
= snapname
;
5117 ha
.recursive
= recursive
;
5119 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5121 if (nvlist_empty(ha
.nvl
)) {
5122 fnvlist_free(ha
.nvl
);
5124 (void) snprintf(errbuf
, sizeof (errbuf
),
5125 dgettext(TEXT_DOMAIN
,
5126 "cannot release hold from snapshot '%s@%s'"),
5127 zhp
->zfs_name
, snapname
);
5129 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5131 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5136 ret
= lzc_release(ha
.nvl
, &errors
);
5137 fnvlist_free(ha
.nvl
);
5140 /* There may be errors even in the success case. */
5141 fnvlist_free(errors
);
5145 if (nvlist_empty(errors
)) {
5146 /* no hold-specific errors */
5147 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5152 "pool must be upgraded"));
5153 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5156 (void) zfs_standard_error(hdl
, errno
, errbuf
);
5160 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5162 elem
= nvlist_next_nvpair(errors
, elem
)) {
5163 (void) snprintf(errbuf
, sizeof (errbuf
),
5164 dgettext(TEXT_DOMAIN
,
5165 "cannot release hold from snapshot '%s'"),
5167 switch (fnvpair_value_int32(elem
)) {
5169 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5172 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5175 (void) zfs_standard_error(hdl
,
5176 fnvpair_value_int32(elem
), errbuf
);
5180 fnvlist_free(errors
);
5185 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5187 zfs_cmd_t zc
= {"\0"};
5188 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5192 char errbuf
[ERRBUFLEN
];
5194 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5195 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5199 nvbuf
= malloc(nvsz
);
5200 if (nvbuf
== NULL
) {
5201 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5205 zc
.zc_nvlist_dst_size
= nvsz
;
5206 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5208 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5210 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5211 (void) snprintf(errbuf
, sizeof (errbuf
),
5212 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5217 nvsz
= zc
.zc_nvlist_dst_size
;
5221 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5222 "pool must be upgraded"));
5223 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5226 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5229 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5232 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5237 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5239 err
= zfs_standard_error_fmt(hdl
, rc
, dgettext(
5240 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5251 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5253 zfs_cmd_t zc
= {"\0"};
5254 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5256 char errbuf
[ERRBUFLEN
];
5260 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5261 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5263 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5266 nvbuf
= malloc(nvsz
);
5268 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5271 zc
.zc_nvlist_src_size
= nvsz
;
5272 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5273 zc
.zc_perm_action
= un
;
5275 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5277 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5278 (void) snprintf(errbuf
, sizeof (errbuf
),
5279 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5283 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5284 "pool must be upgraded"));
5285 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5288 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5291 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5294 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5305 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5308 char errbuf
[ERRBUFLEN
];
5310 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5313 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5315 (void) snprintf(errbuf
, sizeof (errbuf
),
5316 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5320 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5321 "pool must be upgraded"));
5322 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5325 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5328 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5331 err
= zfs_standard_error(hdl
, errno
, errbuf
);
5340 * The theory of raidz space accounting
5342 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5343 * will "reference" 128KB, even though it allocates more than that, to store the
5344 * parity information (and perhaps skip sectors). This concept of the
5345 * "referenced" (and other DMU space accounting) being lower than the allocated
5346 * space by a constant factor is called "raidz deflation."
5348 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5349 * block size. However, zvols typically have a much smaller block size (default
5350 * 8KB). These smaller blocks may require proportionally much more parity
5351 * information (and perhaps skip sectors). In this case, the change to the
5352 * "referenced" property may be much more than the logical block size.
5354 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5357 * +-------+-------+-------+-------+-------+
5358 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5359 * +-------+-------+-------+-------+-------+
5360 * | P0 | D0 | D8 | D16 | D24 |
5361 * | P1 | D1 | D9 | D17 | D25 |
5362 * | P2 | D2 | D10 | D18 | D26 |
5363 * | P3 | D3 | D11 | D19 | D27 |
5364 * | P4 | D4 | D12 | D20 | D28 |
5365 * | P5 | D5 | D13 | D21 | D29 |
5366 * | P6 | D6 | D14 | D22 | D30 |
5367 * | P7 | D7 | D15 | D23 | D31 |
5368 * +-------+-------+-------+-------+-------+
5370 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5371 * sectors. The dataset's referenced will increase by 128k and the pool's
5372 * allocated and free properties will be adjusted by 160k.
5374 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5375 * blank cells represent unallocated space.
5377 * +-------+-------+-------+-------+-------+
5378 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5379 * +-------+-------+-------+-------+-------+
5381 * +-------+-------+-------+-------+-------+
5383 * Above, notice that the 4k block required one sector for parity and another
5384 * for data. vdev_raidz_asize() will return 8k and as such the pool's allocated
5385 * and free properties will be adjusted by 8k. The dataset will not be charged
5386 * 8k. Rather, it will be charged a value that is scaled according to the
5387 * overhead of the 128k block on the same vdev. This 8k allocation will be
5388 * charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as
5389 * calculated in the 128k block example above.
5391 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5392 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5393 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5394 * multiple of of 4 sectors. When a block does not fill the required number of
5395 * sectors, skip blocks (sectors) are used.
5397 * An 8k block being written to a raidz vdev may be written as follows:
5399 * +-------+-------+-------+-------+-------+
5400 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5401 * +-------+-------+-------+-------+-------+
5402 * | P0 | D0 | D1 | S0 | |
5403 * +-------+-------+-------+-------+-------+
5405 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5406 * added. For this 8k block, the pool's allocated and free properties are
5407 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5408 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5409 * the 128k block example above.
5411 * The situation is slightly different for dRAID since the minimum allocation
5412 * size is the full group width. The same 8K block above would be written as
5413 * follows in a dRAID group:
5415 * +-------+-------+-------+-------+-------+
5416 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5417 * +-------+-------+-------+-------+-------+
5418 * | P0 | D0 | D1 | S0 | S1 |
5419 * +-------+-------+-------+-------+-------+
5421 * Compression may lead to a variety of block sizes being written for the same
5422 * volume or file. There is no clear way to reserve just the amount of space
5423 * that will be required, so the worst case (no compression) is assumed.
5424 * Note that metadata blocks will typically be compressed, so the reservation
5425 * size returned by zvol_volsize_to_reservation() will generally be slightly
5426 * larger than the maximum that the volume can reference.
5430 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5431 * amount of space (in bytes) that will be allocated for the specified block
5432 * size. Note that the "referenced" space accounted will be less than this, but
5433 * not necessarily equal to "blksize", due to RAIDZ deflation.
5436 vdev_raidz_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5439 uint64_t asize
, ndata
;
5441 ASSERT3U(ndisks
, >, nparity
);
5442 ndata
= ndisks
- nparity
;
5443 asize
= ((blksize
- 1) >> ashift
) + 1;
5444 asize
+= nparity
* ((asize
+ ndata
- 1) / ndata
);
5445 asize
= roundup(asize
, nparity
+ 1) << ashift
;
5451 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5452 * amount of space (in bytes) that will be allocated for the specified block
5456 vdev_draid_asize(uint64_t ndisks
, uint64_t nparity
, uint64_t ashift
,
5459 ASSERT3U(ndisks
, >, nparity
);
5460 uint64_t ndata
= ndisks
- nparity
;
5461 uint64_t rows
= ((blksize
- 1) / (ndata
<< ashift
)) + 1;
5462 uint64_t asize
= (rows
* ndisks
) << ashift
;
5468 * Determine how much space will be allocated if it lands on the most space-
5469 * inefficient top-level vdev. Returns the size in bytes required to store one
5470 * copy of the volume data. See theory comment above.
5473 volsize_from_vdevs(zpool_handle_t
*zhp
, uint64_t nblocks
, uint64_t blksize
)
5475 nvlist_t
*config
, *tree
, **vdevs
;
5479 config
= zpool_get_config(zhp
, NULL
);
5480 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
) != 0 ||
5481 nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
,
5482 &vdevs
, &nvdevs
) != 0) {
5483 return (nblocks
* blksize
);
5486 for (int v
= 0; v
< nvdevs
; v
++) {
5488 uint64_t nparity
, ashift
, asize
, tsize
;
5491 if (nvlist_lookup_string(vdevs
[v
], ZPOOL_CONFIG_TYPE
,
5495 if (strcmp(type
, VDEV_TYPE_RAIDZ
) != 0 &&
5496 strcmp(type
, VDEV_TYPE_DRAID
) != 0)
5499 if (nvlist_lookup_uint64(vdevs
[v
],
5500 ZPOOL_CONFIG_NPARITY
, &nparity
) != 0)
5503 if (nvlist_lookup_uint64(vdevs
[v
],
5504 ZPOOL_CONFIG_ASHIFT
, &ashift
) != 0)
5507 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
5511 if (nvlist_lookup_nvlist_array(vdevs
[v
],
5512 ZPOOL_CONFIG_CHILDREN
, &disks
, &ndisks
) != 0)
5515 /* allocation size for the "typical" 128k block */
5516 tsize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5517 SPA_OLD_MAXBLOCKSIZE
);
5519 /* allocation size for the blksize block */
5520 asize
= vdev_raidz_asize(ndisks
, nparity
, ashift
,
5525 if (nvlist_lookup_uint64(vdevs
[v
],
5526 ZPOOL_CONFIG_DRAID_NDATA
, &ndata
) != 0)
5529 /* allocation size for the "typical" 128k block */
5530 tsize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5531 ashift
, SPA_OLD_MAXBLOCKSIZE
);
5533 /* allocation size for the blksize block */
5534 asize
= vdev_draid_asize(ndata
+ nparity
, nparity
,
5539 * Scale this size down as a ratio of 128k / tsize.
5540 * See theory statement above.
5542 volsize
= nblocks
* asize
* SPA_OLD_MAXBLOCKSIZE
/ tsize
;
5543 if (volsize
> ret
) {
5549 ret
= nblocks
* blksize
;
5556 * Convert the zvol's volume size to an appropriate reservation. See theory
5559 * Note: If this routine is updated, it is necessary to update the ZFS test
5560 * suite's shell version in reservation.shlib.
5563 zvol_volsize_to_reservation(zpool_handle_t
*zph
, uint64_t volsize
,
5567 uint64_t nblocks
, volblocksize
;
5571 if (nvlist_lookup_string(props
,
5572 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5573 ncopies
= atoi(strval
);
5576 if (nvlist_lookup_uint64(props
,
5577 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5578 &volblocksize
) != 0)
5579 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5581 nblocks
= volsize
/ volblocksize
;
5583 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5584 * this reason, only the data blocks are scaled based on vdev config.
5586 volsize
= volsize_from_vdevs(zph
, nblocks
, volblocksize
);
5588 /* start with metadnode L0-L6 */
5590 /* calculate number of indirects */
5591 while (nblocks
> 1) {
5592 nblocks
+= DNODES_PER_LEVEL
- 1;
5593 nblocks
/= DNODES_PER_LEVEL
;
5596 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5599 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5600 * compressed, but in practice they compress down to about
5603 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;
5609 * Wait for the given activity and return the status of the wait (whether or not
5610 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5611 * reported via the 'missing' parameter, rather than by printing an error
5612 * message. This is convenient when this function is called in a loop over a
5613 * long period of time (as it is, for example, by zfs's wait cmd). In that
5614 * scenario, a fs being exported or destroyed should be considered a normal
5615 * event, so we don't want to print an error when we find that the fs doesn't
5619 zfs_wait_status(zfs_handle_t
*zhp
, zfs_wait_activity_t activity
,
5620 boolean_t
*missing
, boolean_t
*waited
)
5622 int error
= lzc_wait_fs(zhp
->zfs_name
, activity
, waited
);
5623 *missing
= (error
== ENOENT
);
5628 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
5629 dgettext(TEXT_DOMAIN
, "error waiting in fs '%s'"),