4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017 RackTop Systems.
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
56 #include <directory.h>
58 #include <sys/dnode.h>
63 #include "zfs_namecheck.h"
65 #include "libzfs_impl.h"
66 #include "zfs_deleg.h"
68 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
69 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
72 * Given a single type (not a mask of types), return the type in a human
76 zfs_type_to_name(zfs_type_t type
)
79 case ZFS_TYPE_FILESYSTEM
:
80 return (dgettext(TEXT_DOMAIN
, "filesystem"));
81 case ZFS_TYPE_SNAPSHOT
:
82 return (dgettext(TEXT_DOMAIN
, "snapshot"));
84 return (dgettext(TEXT_DOMAIN
, "volume"));
86 return (dgettext(TEXT_DOMAIN
, "pool"));
87 case ZFS_TYPE_BOOKMARK
:
88 return (dgettext(TEXT_DOMAIN
, "bookmark"));
90 assert(!"unhandled zfs_type_t");
97 * Validate a ZFS path. This is used even before trying to open the dataset, to
98 * provide a more meaningful error message. We call zfs_error_aux() to
99 * explain exactly why the name was not valid.
102 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
108 (void) zfs_prop_get_table();
109 if (entity_namecheck(path
, &why
, &what
) != 0) {
112 case NAME_ERR_TOOLONG
:
113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
114 "name is too long"));
117 case NAME_ERR_LEADING_SLASH
:
118 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
119 "leading slash in name"));
122 case NAME_ERR_EMPTY_COMPONENT
:
123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
124 "empty component in name"));
127 case NAME_ERR_TRAILING_SLASH
:
128 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
129 "trailing slash in name"));
132 case NAME_ERR_INVALCHAR
:
134 dgettext(TEXT_DOMAIN
, "invalid character "
135 "'%c' in name"), what
);
138 case NAME_ERR_MULTIPLE_DELIMITERS
:
139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
140 "multiple '@' and/or '#' delimiters in "
144 case NAME_ERR_NOLETTER
:
145 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
146 "pool doesn't begin with a letter"));
149 case NAME_ERR_RESERVED
:
150 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
151 "name is reserved"));
154 case NAME_ERR_DISKLIKE
:
155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
156 "reserved disk name"));
160 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
161 "(%d) not defined"), why
);
169 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
171 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
172 "snapshot delimiter '@' is not expected here"));
176 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
178 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
179 "missing '@' delimiter in snapshot name"));
183 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
185 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
186 "bookmark delimiter '#' is not expected here"));
190 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
192 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
193 "missing '#' delimiter in bookmark name"));
197 if (modifying
&& strchr(path
, '%') != NULL
) {
199 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
200 "invalid character %c in name"), '%');
208 zfs_name_valid(const char *name
, zfs_type_t type
)
210 if (type
== ZFS_TYPE_POOL
)
211 return (zpool_name_valid(NULL
, B_FALSE
, name
));
212 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
216 * This function takes the raw DSL properties, and filters out the user-defined
217 * properties into a separate nvlist.
220 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
222 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
227 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
228 (void) no_memory(hdl
);
233 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
234 if (!zfs_prop_user(nvpair_name(elem
)))
237 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
238 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
240 (void) no_memory(hdl
);
248 static zpool_handle_t
*
249 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
251 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
254 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
255 if (hdl
->libzfs_pool_handles
!= NULL
)
256 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
257 hdl
->libzfs_pool_handles
= zph
;
262 static zpool_handle_t
*
263 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
265 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
266 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
268 while ((zph
!= NULL
) &&
269 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
270 zph
= zph
->zpool_next
;
275 * Returns a handle to the pool that contains the provided dataset.
276 * If a handle to that pool already exists then that handle is returned.
277 * Otherwise, a new handle is created and added to the list of handles.
279 static zpool_handle_t
*
280 zpool_handle(zfs_handle_t
*zhp
)
286 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
287 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
288 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
290 zph
= zpool_find_handle(zhp
, pool_name
, len
);
292 zph
= zpool_add_handle(zhp
, pool_name
);
299 zpool_free_handles(libzfs_handle_t
*hdl
)
301 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
303 while (zph
!= NULL
) {
304 next
= zph
->zpool_next
;
308 hdl
->libzfs_pool_handles
= NULL
;
312 * Utility function to gather stats (objset and zpl) for the given object.
315 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
317 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
319 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
321 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
322 if (errno
== ENOMEM
) {
323 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
334 * Utility function to get the received properties of the given object.
337 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
339 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
340 nvlist_t
*recvdprops
;
341 zfs_cmd_t zc
= { 0 };
344 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
347 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
349 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
350 if (errno
== ENOMEM
) {
351 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
355 zcmd_free_nvlists(&zc
);
360 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
361 zcmd_free_nvlists(&zc
);
365 nvlist_free(zhp
->zfs_recvd_props
);
366 zhp
->zfs_recvd_props
= recvdprops
;
372 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
374 nvlist_t
*allprops
, *userprops
;
376 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
378 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
383 * XXX Why do we store the user props separately, in addition to
384 * storing them in zfs_props?
386 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
387 nvlist_free(allprops
);
391 nvlist_free(zhp
->zfs_props
);
392 nvlist_free(zhp
->zfs_user_props
);
394 zhp
->zfs_props
= allprops
;
395 zhp
->zfs_user_props
= userprops
;
401 get_stats(zfs_handle_t
*zhp
)
404 zfs_cmd_t zc
= { 0 };
406 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
408 if (get_stats_ioctl(zhp
, &zc
) != 0)
410 else if (put_stats_zhdl(zhp
, &zc
) != 0)
412 zcmd_free_nvlists(&zc
);
417 * Refresh the properties currently stored in the handle.
420 zfs_refresh_properties(zfs_handle_t
*zhp
)
422 (void) get_stats(zhp
);
426 * Makes a handle from the given dataset name. Used by zfs_open() and
427 * zfs_iter_* to create child handles on the fly.
430 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
432 if (put_stats_zhdl(zhp
, zc
) != 0)
436 * We've managed to open the dataset and gather statistics. Determine
437 * the high-level type.
439 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
440 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
441 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
442 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
446 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
447 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
448 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
449 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
450 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
451 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
453 abort(); /* we should never see any other types */
455 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
462 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
464 zfs_cmd_t zc
= { 0 };
466 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
472 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
473 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
477 if (get_stats_ioctl(zhp
, &zc
) == -1) {
478 zcmd_free_nvlists(&zc
);
482 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
486 zcmd_free_nvlists(&zc
);
491 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
493 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
499 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
500 if (make_dataset_handle_common(zhp
, zc
) == -1) {
508 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
510 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
515 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
516 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
517 zhp
->zfs_head_type
= pzhp
->zfs_type
;
518 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
519 zhp
->zpool_hdl
= zpool_handle(zhp
);
524 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
526 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
531 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
532 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
533 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
534 sizeof (zhp
->zfs_name
));
535 zhp
->zfs_type
= zhp_orig
->zfs_type
;
536 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
537 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
538 if (zhp_orig
->zfs_props
!= NULL
) {
539 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
540 (void) no_memory(zhp
->zfs_hdl
);
545 if (zhp_orig
->zfs_user_props
!= NULL
) {
546 if (nvlist_dup(zhp_orig
->zfs_user_props
,
547 &zhp
->zfs_user_props
, 0) != 0) {
548 (void) no_memory(zhp
->zfs_hdl
);
553 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
554 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
555 &zhp
->zfs_recvd_props
, 0)) {
556 (void) no_memory(zhp
->zfs_hdl
);
561 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
562 if (zhp_orig
->zfs_mntopts
!= NULL
) {
563 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
564 zhp_orig
->zfs_mntopts
);
566 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
571 zfs_bookmark_exists(const char *path
)
575 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
582 (void) strlcpy(fsname
, path
, sizeof (fsname
));
583 pound
= strchr(fsname
, '#');
588 bmark_name
= pound
+ 1;
589 props
= fnvlist_alloc();
590 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
597 rv
= nvlist_exists(bmarks
, bmark_name
);
603 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
604 nvlist_t
*bmark_props
)
606 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
611 /* Fill in the name. */
612 zhp
->zfs_hdl
= parent
->zfs_hdl
;
613 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
615 /* Set the property lists. */
616 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
622 zhp
->zfs_head_type
= parent
->zfs_head_type
;
623 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
625 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
626 nvlist_free(zhp
->zfs_props
);
634 struct zfs_open_bookmarks_cb_data
{
640 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
642 struct zfs_open_bookmarks_cb_data
*dp
= data
;
645 * Is it the one we are looking for?
647 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
649 * We found it. Save it and let the caller know we are done.
656 * Not found. Close the handle and ask for another one.
663 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
664 * argument is a mask of acceptable types. The function will print an
665 * appropriate error message and return NULL if it can't be opened.
668 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
674 (void) snprintf(errbuf
, sizeof (errbuf
),
675 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
678 * Validate the name before we even try to open it.
680 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
681 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
686 * Bookmarks needs to be handled separately.
688 bookp
= strchr(path
, '#');
691 * Try to get stats for the dataset, which will tell us if it
695 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
696 (void) zfs_standard_error(hdl
, errno
, errbuf
);
700 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
702 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
705 * We need to cut out '#' and everything after '#'
706 * to get the parent dataset name only.
708 assert(bookp
- path
< sizeof (dsname
));
709 (void) strncpy(dsname
, path
, bookp
- path
);
710 dsname
[bookp
- path
] = '\0';
713 * Create handle for the parent dataset.
716 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
717 (void) zfs_standard_error(hdl
, errno
, errbuf
);
722 * Iterate bookmarks to find the right one.
725 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
726 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
727 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
731 if (cb_data
.zhp
== NULL
) {
732 (void) zfs_standard_error(hdl
, errno
, errbuf
);
744 if (!(types
& zhp
->zfs_type
)) {
745 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
754 * Release a ZFS handle. Nothing to do but free the associated memory.
757 zfs_close(zfs_handle_t
*zhp
)
759 if (zhp
->zfs_mntopts
)
760 free(zhp
->zfs_mntopts
);
761 nvlist_free(zhp
->zfs_props
);
762 nvlist_free(zhp
->zfs_user_props
);
763 nvlist_free(zhp
->zfs_recvd_props
);
767 typedef struct mnttab_node
{
768 struct mnttab mtn_mt
;
773 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
775 const mnttab_node_t
*mtn1
= arg1
;
776 const mnttab_node_t
*mtn2
= arg2
;
779 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
783 return (rv
> 0 ? 1 : -1);
787 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
789 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
790 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
791 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
795 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
799 rewind(hdl
->libzfs_mnttab
);
800 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
803 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
805 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
806 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
807 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
808 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
809 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
810 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
815 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
820 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
822 free(mtn
->mtn_mt
.mnt_special
);
823 free(mtn
->mtn_mt
.mnt_mountp
);
824 free(mtn
->mtn_mt
.mnt_fstype
);
825 free(mtn
->mtn_mt
.mnt_mntopts
);
828 avl_destroy(&hdl
->libzfs_mnttab_cache
);
832 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
834 hdl
->libzfs_mnttab_enable
= enable
;
838 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
839 struct mnttab
*entry
)
844 if (!hdl
->libzfs_mnttab_enable
) {
845 struct mnttab srch
= { 0 };
847 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
848 libzfs_mnttab_fini(hdl
);
849 rewind(hdl
->libzfs_mnttab
);
850 srch
.mnt_special
= (char *)fsname
;
851 srch
.mnt_fstype
= MNTTYPE_ZFS
;
852 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
858 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
859 libzfs_mnttab_update(hdl
);
861 find
.mtn_mt
.mnt_special
= (char *)fsname
;
862 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
864 *entry
= mtn
->mtn_mt
;
871 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
872 const char *mountp
, const char *mntopts
)
876 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
878 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
879 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
880 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
881 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
882 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
883 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
887 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
892 find
.mtn_mt
.mnt_special
= (char *)fsname
;
893 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
895 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
896 free(ret
->mtn_mt
.mnt_special
);
897 free(ret
->mtn_mt
.mnt_mountp
);
898 free(ret
->mtn_mt
.mnt_fstype
);
899 free(ret
->mtn_mt
.mnt_mntopts
);
905 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
907 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
909 if (zpool_handle
== NULL
)
912 *spa_version
= zpool_get_prop_int(zpool_handle
,
913 ZPOOL_PROP_VERSION
, NULL
);
918 * The choice of reservation property depends on the SPA version.
921 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
925 if (zfs_spa_version(zhp
, &spa_version
) < 0)
928 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
929 *resv_prop
= ZFS_PROP_REFRESERVATION
;
931 *resv_prop
= ZFS_PROP_RESERVATION
;
937 * Given an nvlist of properties to set, validates that they are correct, and
938 * parses any numeric properties (index, boolean, etc) if they are specified as
942 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
943 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
951 int chosen_normal
= -1;
954 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
955 (void) no_memory(hdl
);
960 * Make sure this property is valid and applies to this type.
964 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
965 const char *propname
= nvpair_name(elem
);
967 prop
= zfs_name_to_prop(propname
);
968 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
970 * This is a user property: make sure it's a
971 * string, and that it's less than ZAP_MAXNAMELEN.
973 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
974 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
975 "'%s' must be a string"), propname
);
976 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
980 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
981 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
982 "property name '%s' is too long"),
984 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
988 (void) nvpair_value_string(elem
, &strval
);
989 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
990 (void) no_memory(hdl
);
997 * Currently, only user properties can be modified on
1000 if (type
== ZFS_TYPE_SNAPSHOT
) {
1001 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1002 "this property can not be modified for snapshots"));
1003 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1007 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1008 zfs_userquota_prop_t uqtype
;
1009 char newpropname
[128];
1014 if (userquota_propname_decode(propname
, zoned
,
1015 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1017 dgettext(TEXT_DOMAIN
,
1018 "'%s' has an invalid user/group name"),
1020 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1024 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1025 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
1027 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1029 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1034 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1035 (void) nvpair_value_string(elem
, &strval
);
1036 if (strcmp(strval
, "none") == 0) {
1038 } else if (zfs_nicestrtonum(hdl
,
1039 strval
, &intval
) != 0) {
1040 (void) zfs_error(hdl
,
1041 EZFS_BADPROP
, errbuf
);
1044 } else if (nvpair_type(elem
) ==
1046 (void) nvpair_value_uint64(elem
, &intval
);
1048 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1049 "use 'none' to disable "
1050 "userquota/groupquota"));
1054 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1055 "'%s' must be a number"), propname
);
1056 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1061 * Encode the prop name as
1062 * userquota@<hex-rid>-domain, to make it easy
1063 * for the kernel to decode.
1065 (void) snprintf(newpropname
, sizeof (newpropname
),
1066 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
1067 (longlong_t
)rid
, domain
);
1071 if (nvlist_add_uint64_array(ret
, newpropname
,
1073 (void) no_memory(hdl
);
1077 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1078 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1079 "'%s' is readonly"),
1081 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1085 if (prop
== ZPROP_INVAL
) {
1086 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1087 "invalid property '%s'"), propname
);
1088 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1092 if (!zfs_prop_valid_for_type(prop
, type
)) {
1094 dgettext(TEXT_DOMAIN
, "'%s' does not "
1095 "apply to datasets of this type"), propname
);
1096 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1100 if (zfs_prop_readonly(prop
) &&
1101 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
1103 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1105 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1109 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1110 &strval
, &intval
, errbuf
) != 0)
1114 * Perform some additional checks for specific properties.
1117 case ZFS_PROP_VERSION
:
1123 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1124 if (intval
< version
) {
1125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1126 "Can not downgrade; already at version %u"),
1128 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1134 case ZFS_PROP_VOLBLOCKSIZE
:
1135 case ZFS_PROP_RECORDSIZE
:
1137 int maxbs
= SPA_MAXBLOCKSIZE
;
1138 if (zpool_hdl
!= NULL
) {
1139 maxbs
= zpool_get_prop_int(zpool_hdl
,
1140 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1143 * Volumes are limited to a volblocksize of 128KB,
1144 * because they typically service workloads with
1145 * small random writes, which incur a large performance
1146 * penalty with large blocks.
1148 if (prop
== ZFS_PROP_VOLBLOCKSIZE
)
1149 maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1151 * The value must be a power of two between
1152 * SPA_MINBLOCKSIZE and maxbs.
1154 if (intval
< SPA_MINBLOCKSIZE
||
1155 intval
> maxbs
|| !ISP2(intval
)) {
1156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1157 "'%s' must be power of 2 from 512B "
1158 "to %uKB"), propname
, maxbs
>> 10);
1159 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1164 case ZFS_PROP_MLSLABEL
:
1167 * Verify the mlslabel string and convert to
1168 * internal hex label string.
1172 char *hex
= NULL
; /* internal label string */
1174 /* Default value is already OK. */
1175 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1178 /* Verify the label can be converted to binary form */
1179 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1180 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1181 L_NO_CORRECTION
, NULL
) == -1)) {
1185 /* Now translate to hex internal label string */
1186 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1192 m_label_free(new_sl
);
1194 /* If string is already in internal form, we're done. */
1195 if (strcmp(strval
, hex
) == 0) {
1200 /* Replace the label string with the internal form. */
1201 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1203 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1210 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1211 "invalid mlslabel '%s'"), strval
);
1212 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1213 m_label_free(new_sl
); /* OK if null */
1218 case ZFS_PROP_MOUNTPOINT
:
1220 namecheck_err_t why
;
1222 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1223 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1226 if (mountpoint_namecheck(strval
, &why
)) {
1228 case NAME_ERR_LEADING_SLASH
:
1230 dgettext(TEXT_DOMAIN
,
1231 "'%s' must be an absolute path, "
1232 "'none', or 'legacy'"), propname
);
1234 case NAME_ERR_TOOLONG
:
1236 dgettext(TEXT_DOMAIN
,
1237 "component of '%s' is too long"),
1243 dgettext(TEXT_DOMAIN
,
1244 "(%d) not defined"),
1248 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1255 case ZFS_PROP_SHARESMB
:
1256 case ZFS_PROP_SHARENFS
:
1258 * For the mountpoint and sharenfs or sharesmb
1259 * properties, check if it can be set in a
1260 * global/non-global zone based on
1261 * the zoned property value:
1263 * global zone non-global zone
1264 * --------------------------------------------------
1265 * zoned=on mountpoint (no) mountpoint (yes)
1266 * sharenfs (no) sharenfs (no)
1267 * sharesmb (no) sharesmb (no)
1269 * zoned=off mountpoint (yes) N/A
1274 if (getzoneid() == GLOBAL_ZONEID
) {
1275 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1276 "'%s' cannot be set on "
1277 "dataset in a non-global zone"),
1279 (void) zfs_error(hdl
, EZFS_ZONED
,
1282 } else if (prop
== ZFS_PROP_SHARENFS
||
1283 prop
== ZFS_PROP_SHARESMB
) {
1284 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1285 "'%s' cannot be set in "
1286 "a non-global zone"), propname
);
1287 (void) zfs_error(hdl
, EZFS_ZONED
,
1291 } else if (getzoneid() != GLOBAL_ZONEID
) {
1293 * If zoned property is 'off', this must be in
1294 * a global zone. If not, something is wrong.
1296 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1297 "'%s' cannot be set while dataset "
1298 "'zoned' property is set"), propname
);
1299 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1304 * At this point, it is legitimate to set the
1305 * property. Now we want to make sure that the
1306 * property value is valid if it is sharenfs.
1308 if ((prop
== ZFS_PROP_SHARENFS
||
1309 prop
== ZFS_PROP_SHARESMB
) &&
1310 strcmp(strval
, "on") != 0 &&
1311 strcmp(strval
, "off") != 0) {
1312 zfs_share_proto_t proto
;
1314 if (prop
== ZFS_PROP_SHARESMB
)
1320 * Must be an valid sharing protocol
1321 * option string so init the libshare
1322 * in order to enable the parser and
1323 * then parse the options. We use the
1324 * control API since we don't care about
1325 * the current configuration and don't
1326 * want the overhead of loading it
1327 * until we actually do something.
1330 if (zfs_init_libshare(hdl
,
1331 SA_INIT_CONTROL_API
) != SA_OK
) {
1333 * An error occurred so we can't do
1336 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1337 "'%s' cannot be set: problem "
1338 "in share initialization"),
1340 (void) zfs_error(hdl
, EZFS_BADPROP
,
1345 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1347 * There was an error in parsing so
1348 * deal with it by issuing an error
1349 * message and leaving after
1350 * uninitializing the the libshare
1353 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1354 "'%s' cannot be set to invalid "
1355 "options"), propname
);
1356 (void) zfs_error(hdl
, EZFS_BADPROP
,
1358 zfs_uninit_libshare(hdl
);
1361 zfs_uninit_libshare(hdl
);
1366 case ZFS_PROP_UTF8ONLY
:
1367 chosen_utf
= (int)intval
;
1370 case ZFS_PROP_NORMALIZE
:
1371 chosen_normal
= (int)intval
;
1379 * For changes to existing volumes, we have some additional
1380 * checks to enforce.
1382 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1383 uint64_t volsize
= zfs_prop_get_int(zhp
,
1385 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1386 ZFS_PROP_VOLBLOCKSIZE
);
1390 case ZFS_PROP_RESERVATION
:
1391 case ZFS_PROP_REFRESERVATION
:
1392 if (intval
> volsize
) {
1393 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1394 "'%s' is greater than current "
1395 "volume size"), propname
);
1396 (void) zfs_error(hdl
, EZFS_BADPROP
,
1402 case ZFS_PROP_VOLSIZE
:
1403 if (intval
% blocksize
!= 0) {
1404 zfs_nicenum(blocksize
, buf
,
1406 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1407 "'%s' must be a multiple of "
1408 "volume block size (%s)"),
1410 (void) zfs_error(hdl
, EZFS_BADPROP
,
1416 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1417 "'%s' cannot be zero"),
1419 (void) zfs_error(hdl
, EZFS_BADPROP
,
1432 * If normalization was chosen, but no UTF8 choice was made,
1433 * enforce rejection of non-UTF8 names.
1435 * If normalization was chosen, but rejecting non-UTF8 names
1436 * was explicitly not chosen, it is an error.
1438 if (chosen_normal
> 0 && chosen_utf
< 0) {
1439 if (nvlist_add_uint64(ret
,
1440 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1441 (void) no_memory(hdl
);
1444 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1445 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1446 "'%s' must be set 'on' if normalization chosen"),
1447 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1448 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1459 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1461 uint64_t old_volsize
;
1462 uint64_t new_volsize
;
1463 uint64_t old_reservation
;
1464 uint64_t new_reservation
;
1465 zfs_prop_t resv_prop
;
1469 * If this is an existing volume, and someone is setting the volsize,
1470 * make sure that it matches the reservation, or add it if necessary.
1472 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1473 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1475 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1477 props
= fnvlist_alloc();
1478 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1479 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1481 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1482 old_reservation
) || nvlist_exists(nvl
,
1483 zfs_prop_to_name(resv_prop
))) {
1484 fnvlist_free(props
);
1487 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1488 &new_volsize
) != 0) {
1489 fnvlist_free(props
);
1492 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1493 fnvlist_free(props
);
1495 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1496 new_reservation
) != 0) {
1497 (void) no_memory(zhp
->zfs_hdl
);
1504 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1511 * For quotas and reservations, ENOSPC indicates
1512 * something different; setting a quota or reservation
1513 * doesn't use any disk space.
1516 case ZFS_PROP_QUOTA
:
1517 case ZFS_PROP_REFQUOTA
:
1518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1519 "size is less than current used or "
1521 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1524 case ZFS_PROP_RESERVATION
:
1525 case ZFS_PROP_REFRESERVATION
:
1526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1527 "size is greater than available space"));
1528 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1532 (void) zfs_standard_error(hdl
, err
, errbuf
);
1538 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1542 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1546 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1547 "property value too long"));
1548 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1552 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1553 "pool and or dataset must be upgraded to set this "
1554 "property or value"));
1555 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1559 if (prop
== ZFS_PROP_COMPRESSION
||
1560 prop
== ZFS_PROP_RECORDSIZE
) {
1561 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1562 "property setting is not allowed on "
1563 "bootable datasets"));
1564 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1565 } else if (prop
== ZFS_PROP_CHECKSUM
||
1566 prop
== ZFS_PROP_DEDUP
) {
1567 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1568 "property setting is not allowed on "
1570 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1572 (void) zfs_standard_error(hdl
, err
, errbuf
);
1577 if (prop
== ZPROP_INVAL
) {
1578 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1580 (void) zfs_standard_error(hdl
, err
, errbuf
);
1586 * This platform can't address a volume this big.
1589 if (prop
== ZFS_PROP_VOLSIZE
) {
1590 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1596 (void) zfs_standard_error(hdl
, err
, errbuf
);
1601 * Given a property name and value, set the property for the given dataset.
1604 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1608 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1609 nvlist_t
*nvl
= NULL
;
1611 (void) snprintf(errbuf
, sizeof (errbuf
),
1612 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1615 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1616 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1617 (void) no_memory(hdl
);
1621 ret
= zfs_prop_set_list(zhp
, nvl
);
1631 * Given an nvlist of property names and values, set the properties for the
1635 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1637 zfs_cmd_t zc
= { 0 };
1639 prop_changelist_t
**cls
= NULL
;
1642 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1647 (void) snprintf(errbuf
, sizeof (errbuf
),
1648 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1651 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1652 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1657 * We have to check for any extra properties which need to be added
1658 * before computing the length of the nvlist.
1660 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1662 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1663 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1664 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1669 * Check how many properties we're setting and allocate an array to
1670 * store changelist pointers for postfix().
1673 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1675 elem
= nvlist_next_nvpair(nvl
, elem
))
1677 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1681 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1683 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1685 zfs_prop_t prop
= zfs_name_to_prop(nvpair_name(elem
));
1687 assert(cl_idx
< nvl_len
);
1689 * We don't want to unmount & remount the dataset when changing
1690 * its canmount property to 'on' or 'noauto'. We only use
1691 * the changelist logic to unmount when setting canmount=off.
1693 if (prop
!= ZFS_PROP_CANMOUNT
||
1694 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1695 zfs_is_mounted(zhp
, NULL
))) {
1696 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1697 if (cls
[cl_idx
] == NULL
)
1701 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1702 changelist_haszonedchild(cls
[cl_idx
])) {
1703 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1704 "child dataset with inherited mountpoint is used "
1705 "in a non-global zone"));
1706 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1710 if (cls
[cl_idx
] != NULL
&&
1711 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1716 assert(cl_idx
== nvl_len
);
1719 * Execute the corresponding ioctl() to set this list of properties.
1721 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1723 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1724 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1727 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1730 /* Get the list of unset properties back and report them. */
1731 nvlist_t
*errorprops
= NULL
;
1732 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1734 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1736 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1737 zfs_prop_t prop
= zfs_name_to_prop(nvpair_name(elem
));
1738 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1740 nvlist_free(errorprops
);
1742 if (added_resv
&& errno
== ENOSPC
) {
1743 /* clean up the volsize property we tried to set */
1744 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1748 zcmd_free_nvlists(&zc
);
1750 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1752 if (nvlist_add_uint64(nvl
,
1753 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1756 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1758 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1761 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1762 if (cls
[cl_idx
] != NULL
) {
1763 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1770 * Refresh the statistics so the new property value
1774 (void) get_stats(zhp
);
1779 zcmd_free_nvlists(&zc
);
1781 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1782 if (cls
[cl_idx
] != NULL
)
1783 changelist_free(cls
[cl_idx
]);
1791 * Given a property, inherit the value from the parent dataset, or if received
1792 * is TRUE, revert to the received value, if any.
1795 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1797 zfs_cmd_t zc
= { 0 };
1799 prop_changelist_t
*cl
;
1800 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1804 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1805 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1807 zc
.zc_cookie
= received
;
1808 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1810 * For user properties, the amount of work we have to do is very
1811 * small, so just do it here.
1813 if (!zfs_prop_user(propname
)) {
1814 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1815 "invalid property"));
1816 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1819 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1820 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1822 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1823 return (zfs_standard_error(hdl
, errno
, errbuf
));
1829 * Verify that this property is inheritable.
1831 if (zfs_prop_readonly(prop
))
1832 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1834 if (!zfs_prop_inheritable(prop
) && !received
)
1835 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1838 * Check to see if the value applies to this type
1840 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1841 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1844 * Normalize the name, to get rid of shorthand abbreviations.
1846 propname
= zfs_prop_to_name(prop
);
1847 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1848 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1850 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1851 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1852 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1853 "dataset is used in a non-global zone"));
1854 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1858 * Determine datasets which will be affected by this change, if any.
1860 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1863 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1864 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1865 "child dataset with inherited mountpoint is used "
1866 "in a non-global zone"));
1867 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1871 if ((ret
= changelist_prefix(cl
)) != 0)
1874 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1875 return (zfs_standard_error(hdl
, errno
, errbuf
));
1878 if ((ret
= changelist_postfix(cl
)) != 0)
1882 * Refresh the statistics so the new property is reflected.
1884 (void) get_stats(zhp
);
1888 changelist_free(cl
);
1893 * True DSL properties are stored in an nvlist. The following two functions
1894 * extract them appropriately.
1897 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1903 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1904 zfs_prop_to_name(prop
), &nv
) == 0) {
1905 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1906 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1908 verify(!zhp
->zfs_props_table
||
1909 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1910 value
= zfs_prop_default_numeric(prop
);
1918 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1924 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1925 zfs_prop_to_name(prop
), &nv
) == 0) {
1926 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
1927 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1929 verify(!zhp
->zfs_props_table
||
1930 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1931 value
= zfs_prop_default_string(prop
);
1939 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1941 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1945 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1947 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1948 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1952 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1954 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1959 * Internal function for getting a numeric property. Both zfs_prop_get() and
1960 * zfs_prop_get_int() are built using this interface.
1962 * Certain properties can be overridden using 'mount -o'. In this case, scan
1963 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1964 * If they differ from the on-disk values, report the current values and mark
1965 * the source "temporary".
1968 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1969 char **source
, uint64_t *val
)
1971 zfs_cmd_t zc
= { 0 };
1972 nvlist_t
*zplprops
= NULL
;
1974 char *mntopt_on
= NULL
;
1975 char *mntopt_off
= NULL
;
1976 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1981 case ZFS_PROP_ATIME
:
1982 mntopt_on
= MNTOPT_ATIME
;
1983 mntopt_off
= MNTOPT_NOATIME
;
1986 case ZFS_PROP_DEVICES
:
1987 mntopt_on
= MNTOPT_DEVICES
;
1988 mntopt_off
= MNTOPT_NODEVICES
;
1992 mntopt_on
= MNTOPT_EXEC
;
1993 mntopt_off
= MNTOPT_NOEXEC
;
1996 case ZFS_PROP_READONLY
:
1997 mntopt_on
= MNTOPT_RO
;
1998 mntopt_off
= MNTOPT_RW
;
2001 case ZFS_PROP_SETUID
:
2002 mntopt_on
= MNTOPT_SETUID
;
2003 mntopt_off
= MNTOPT_NOSETUID
;
2006 case ZFS_PROP_XATTR
:
2007 mntopt_on
= MNTOPT_XATTR
;
2008 mntopt_off
= MNTOPT_NOXATTR
;
2011 case ZFS_PROP_NBMAND
:
2012 mntopt_on
= MNTOPT_NBMAND
;
2013 mntopt_off
= MNTOPT_NONBMAND
;
2021 * Because looking up the mount options is potentially expensive
2022 * (iterating over all of /etc/mnttab), we defer its calculation until
2023 * we're looking up a property which requires its presence.
2025 if (!zhp
->zfs_mntcheck
&&
2026 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2027 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2028 struct mnttab entry
;
2030 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2031 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2033 if (zhp
->zfs_mntopts
== NULL
)
2037 zhp
->zfs_mntcheck
= B_TRUE
;
2040 if (zhp
->zfs_mntopts
== NULL
)
2041 mnt
.mnt_mntopts
= "";
2043 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2046 case ZFS_PROP_ATIME
:
2047 case ZFS_PROP_DEVICES
:
2049 case ZFS_PROP_READONLY
:
2050 case ZFS_PROP_SETUID
:
2051 case ZFS_PROP_XATTR
:
2052 case ZFS_PROP_NBMAND
:
2053 *val
= getprop_uint64(zhp
, prop
, source
);
2058 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2061 *src
= ZPROP_SRC_TEMPORARY
;
2062 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2065 *src
= ZPROP_SRC_TEMPORARY
;
2069 case ZFS_PROP_CANMOUNT
:
2070 case ZFS_PROP_VOLSIZE
:
2071 case ZFS_PROP_QUOTA
:
2072 case ZFS_PROP_REFQUOTA
:
2073 case ZFS_PROP_RESERVATION
:
2074 case ZFS_PROP_REFRESERVATION
:
2075 case ZFS_PROP_FILESYSTEM_LIMIT
:
2076 case ZFS_PROP_SNAPSHOT_LIMIT
:
2077 case ZFS_PROP_FILESYSTEM_COUNT
:
2078 case ZFS_PROP_SNAPSHOT_COUNT
:
2079 *val
= getprop_uint64(zhp
, prop
, source
);
2081 if (*source
== NULL
) {
2082 /* not default, must be local */
2083 *source
= zhp
->zfs_name
;
2087 case ZFS_PROP_MOUNTED
:
2088 *val
= (zhp
->zfs_mntopts
!= NULL
);
2091 case ZFS_PROP_NUMCLONES
:
2092 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2095 case ZFS_PROP_VERSION
:
2096 case ZFS_PROP_NORMALIZE
:
2097 case ZFS_PROP_UTF8ONLY
:
2099 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
2100 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2102 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2103 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2104 zcmd_free_nvlists(&zc
);
2107 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2108 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2110 zcmd_free_nvlists(&zc
);
2113 nvlist_free(zplprops
);
2114 zcmd_free_nvlists(&zc
);
2117 case ZFS_PROP_INCONSISTENT
:
2118 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2122 switch (zfs_prop_get_type(prop
)) {
2123 case PROP_TYPE_NUMBER
:
2124 case PROP_TYPE_INDEX
:
2125 *val
= getprop_uint64(zhp
, prop
, source
);
2127 * If we tried to use a default value for a
2128 * readonly property, it means that it was not
2129 * present. Note this only applies to "truly"
2130 * readonly properties, not set-once properties
2131 * like volblocksize.
2133 if (zfs_prop_readonly(prop
) &&
2134 !zfs_prop_setonce(prop
) &&
2135 *source
!= NULL
&& (*source
)[0] == '\0') {
2141 case PROP_TYPE_STRING
:
2143 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2144 "cannot get non-numeric property"));
2145 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2146 dgettext(TEXT_DOMAIN
, "internal error")));
2154 * Calculate the source type, given the raw source string.
2157 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2158 char *statbuf
, size_t statlen
)
2160 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2163 if (source
== NULL
) {
2164 *srctype
= ZPROP_SRC_NONE
;
2165 } else if (source
[0] == '\0') {
2166 *srctype
= ZPROP_SRC_DEFAULT
;
2167 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2168 *srctype
= ZPROP_SRC_RECEIVED
;
2170 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2171 *srctype
= ZPROP_SRC_LOCAL
;
2173 (void) strlcpy(statbuf
, source
, statlen
);
2174 *srctype
= ZPROP_SRC_INHERITED
;
2181 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2182 size_t proplen
, boolean_t literal
)
2187 if (zhp
->zfs_recvd_props
== NULL
)
2188 if (get_recvd_props_ioctl(zhp
) != 0)
2191 prop
= zfs_name_to_prop(propname
);
2193 if (prop
!= ZPROP_INVAL
) {
2195 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2197 zfs_set_recvd_props_mode(zhp
, &cookie
);
2198 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2199 NULL
, NULL
, 0, literal
);
2200 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2204 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2205 propname
, &propval
) != 0)
2207 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2209 (void) strlcpy(propbuf
, recvdval
, proplen
);
2212 return (err
== 0 ? 0 : -1);
2216 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2221 value
= zfs_get_clones_nvl(zhp
);
2226 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2227 pair
= nvlist_next_nvpair(value
, pair
)) {
2228 if (propbuf
[0] != '\0')
2229 (void) strlcat(propbuf
, ",", proplen
);
2230 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2236 struct get_clones_arg
{
2240 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2244 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2246 struct get_clones_arg
*gca
= arg
;
2248 if (gca
->numclones
== 0) {
2253 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2254 NULL
, NULL
, 0, B_TRUE
) != 0)
2256 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2257 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2262 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2268 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2270 nvlist_t
*nv
, *value
;
2272 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2273 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2274 struct get_clones_arg gca
;
2277 * if this is a snapshot, then the kernel wasn't able
2278 * to get the clones. Do it by slowly iterating.
2280 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2282 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2284 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2289 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2291 gca
.origin
= zhp
->zfs_name
;
2293 if (gca
.numclones
!= 0) {
2295 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2298 /* get the pool name */
2299 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2300 (void) strsep(&cp
, "/@");
2301 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2302 ZFS_TYPE_FILESYSTEM
);
2304 (void) get_clones_cb(root
, &gca
);
2307 if (gca
.numclones
!= 0 ||
2308 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2309 nvlist_add_nvlist(zhp
->zfs_props
,
2310 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2317 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2318 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2321 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2327 * Retrieve a property from the given object. If 'literal' is specified, then
2328 * numbers are left as exact values. Otherwise, numbers are converted to a
2329 * human-readable form.
2331 * Returns 0 on success, or -1 on error.
2334 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2335 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2337 char *source
= NULL
;
2341 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2344 * Check to see if this property applies to our object
2346 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
2349 if (received
&& zfs_prop_readonly(prop
))
2353 *src
= ZPROP_SRC_NONE
;
2356 case ZFS_PROP_CREATION
:
2358 * 'creation' is a time_t stored in the statistics. We convert
2359 * this into a string unless 'literal' is specified.
2362 val
= getprop_uint64(zhp
, prop
, &source
);
2363 time_t time
= (time_t)val
;
2367 localtime_r(&time
, &t
) == NULL
||
2368 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2370 (void) snprintf(propbuf
, proplen
, "%llu", val
);
2374 case ZFS_PROP_MOUNTPOINT
:
2376 * Getting the precise mountpoint can be tricky.
2378 * - for 'none' or 'legacy', return those values.
2379 * - for inherited mountpoints, we want to take everything
2380 * after our ancestor and append it to the inherited value.
2382 * If the pool has an alternate root, we want to prepend that
2383 * root to any values we return.
2386 str
= getprop_string(zhp
, prop
, &source
);
2388 if (str
[0] == '/') {
2389 char buf
[MAXPATHLEN
];
2391 const char *relpath
;
2394 * If we inherit the mountpoint, even from a dataset
2395 * with a received value, the source will be the path of
2396 * the dataset we inherit from. If source is
2397 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2400 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2403 relpath
= zhp
->zfs_name
+ strlen(source
);
2404 if (relpath
[0] == '/')
2408 if ((zpool_get_prop(zhp
->zpool_hdl
,
2409 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2410 B_FALSE
)) || (strcmp(root
, "-") == 0))
2413 * Special case an alternate root of '/'. This will
2414 * avoid having multiple leading slashes in the
2417 if (strcmp(root
, "/") == 0)
2421 * If the mountpoint is '/' then skip over this
2422 * if we are obtaining either an alternate root or
2423 * an inherited mountpoint.
2425 if (str
[1] == '\0' && (root
[0] != '\0' ||
2426 relpath
[0] != '\0'))
2429 if (relpath
[0] == '\0')
2430 (void) snprintf(propbuf
, proplen
, "%s%s",
2433 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2434 root
, str
, relpath
[0] == '@' ? "" : "/",
2437 /* 'legacy' or 'none' */
2438 (void) strlcpy(propbuf
, str
, proplen
);
2443 case ZFS_PROP_ORIGIN
:
2444 str
= getprop_string(zhp
, prop
, &source
);
2447 (void) strlcpy(propbuf
, str
, proplen
);
2450 case ZFS_PROP_CLONES
:
2451 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2455 case ZFS_PROP_QUOTA
:
2456 case ZFS_PROP_REFQUOTA
:
2457 case ZFS_PROP_RESERVATION
:
2458 case ZFS_PROP_REFRESERVATION
:
2460 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2464 * If quota or reservation is 0, we translate this into 'none'
2465 * (unless literal is set), and indicate that it's the default
2466 * value. Otherwise, we print the number nicely and indicate
2467 * that its set locally.
2471 (void) strlcpy(propbuf
, "0", proplen
);
2473 (void) strlcpy(propbuf
, "none", proplen
);
2476 (void) snprintf(propbuf
, proplen
, "%llu",
2479 zfs_nicenum(val
, propbuf
, proplen
);
2483 case ZFS_PROP_FILESYSTEM_LIMIT
:
2484 case ZFS_PROP_SNAPSHOT_LIMIT
:
2485 case ZFS_PROP_FILESYSTEM_COUNT
:
2486 case ZFS_PROP_SNAPSHOT_COUNT
:
2488 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2492 * If limit is UINT64_MAX, we translate this into 'none' (unless
2493 * literal is set), and indicate that it's the default value.
2494 * Otherwise, we print the number nicely and indicate that it's
2498 (void) snprintf(propbuf
, proplen
, "%llu",
2500 } else if (val
== UINT64_MAX
) {
2501 (void) strlcpy(propbuf
, "none", proplen
);
2503 zfs_nicenum(val
, propbuf
, proplen
);
2507 case ZFS_PROP_REFRATIO
:
2508 case ZFS_PROP_COMPRESSRATIO
:
2509 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2511 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2512 (u_longlong_t
)(val
/ 100),
2513 (u_longlong_t
)(val
% 100));
2517 switch (zhp
->zfs_type
) {
2518 case ZFS_TYPE_FILESYSTEM
:
2521 case ZFS_TYPE_VOLUME
:
2524 case ZFS_TYPE_SNAPSHOT
:
2527 case ZFS_TYPE_BOOKMARK
:
2533 (void) snprintf(propbuf
, proplen
, "%s", str
);
2536 case ZFS_PROP_MOUNTED
:
2538 * The 'mounted' property is a pseudo-property that described
2539 * whether the filesystem is currently mounted. Even though
2540 * it's a boolean value, the typical values of "on" and "off"
2541 * don't make sense, so we translate to "yes" and "no".
2543 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2544 src
, &source
, &val
) != 0)
2547 (void) strlcpy(propbuf
, "yes", proplen
);
2549 (void) strlcpy(propbuf
, "no", proplen
);
2554 * The 'name' property is a pseudo-property derived from the
2555 * dataset name. It is presented as a real property to simplify
2558 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2561 case ZFS_PROP_MLSLABEL
:
2563 m_label_t
*new_sl
= NULL
;
2564 char *ascii
= NULL
; /* human readable label */
2566 (void) strlcpy(propbuf
,
2567 getprop_string(zhp
, prop
, &source
), proplen
);
2569 if (literal
|| (strcasecmp(propbuf
,
2570 ZFS_MLSLABEL_DEFAULT
) == 0))
2574 * Try to translate the internal hex string to
2575 * human-readable output. If there are any
2576 * problems just use the hex string.
2579 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2580 L_NO_CORRECTION
, NULL
) == -1) {
2581 m_label_free(new_sl
);
2585 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2589 m_label_free(new_sl
);
2592 m_label_free(new_sl
);
2594 (void) strlcpy(propbuf
, ascii
, proplen
);
2601 * GUIDs are stored as numbers, but they are identifiers.
2602 * We don't want them to be pretty printed, because pretty
2603 * printing mangles the ID into a truncated and useless value.
2605 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2607 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2611 switch (zfs_prop_get_type(prop
)) {
2612 case PROP_TYPE_NUMBER
:
2613 if (get_numeric_property(zhp
, prop
, src
,
2614 &source
, &val
) != 0)
2617 (void) snprintf(propbuf
, proplen
, "%llu",
2620 zfs_nicenum(val
, propbuf
, proplen
);
2623 case PROP_TYPE_STRING
:
2624 str
= getprop_string(zhp
, prop
, &source
);
2627 (void) strlcpy(propbuf
, str
, proplen
);
2630 case PROP_TYPE_INDEX
:
2631 if (get_numeric_property(zhp
, prop
, src
,
2632 &source
, &val
) != 0)
2634 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2636 (void) strlcpy(propbuf
, strval
, proplen
);
2644 get_source(zhp
, src
, source
, statbuf
, statlen
);
2650 * Utility function to get the given numeric property. Does no validation that
2651 * the given property is the appropriate type; should only be used with
2652 * hard-coded property types.
2655 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2660 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2666 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2670 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2671 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2675 * Similar to zfs_prop_get(), but returns the value as an integer.
2678 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2679 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2684 * Check to see if this property applies to our object
2686 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2687 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2688 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2689 zfs_prop_to_name(prop
)));
2693 *src
= ZPROP_SRC_NONE
;
2695 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2698 get_source(zhp
, src
, source
, statbuf
, statlen
);
2704 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2705 char **domainp
, idmap_rid_t
*ridp
)
2707 idmap_get_handle_t
*get_hdl
= NULL
;
2711 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2715 err
= idmap_get_sidbyuid(get_hdl
, id
,
2716 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2718 err
= idmap_get_sidbygid(get_hdl
, id
,
2719 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2721 if (err
== IDMAP_SUCCESS
&&
2722 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2723 status
== IDMAP_SUCCESS
)
2729 idmap_get_destroy(get_hdl
);
2734 * convert the propname into parameters needed by kernel
2735 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2736 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2739 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2740 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2742 zfs_userquota_prop_t type
;
2744 char *numericsid
= NULL
;
2749 /* Figure out the property type ({user|group}{quota|space}) */
2750 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2751 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2752 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2755 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2759 isuser
= (type
== ZFS_PROP_USERQUOTA
||
2760 type
== ZFS_PROP_USERUSED
);
2762 cp
= strchr(propname
, '@') + 1;
2764 if (strchr(cp
, '@')) {
2766 * It's a SID name (eg "user@domain") that needs to be
2767 * turned into S-1-domainID-RID.
2770 idmap_stat stat
, map_stat
;
2773 idmap_get_handle_t
*gh
= NULL
;
2775 stat
= idmap_get_create(&gh
);
2776 if (stat
!= IDMAP_SUCCESS
) {
2777 idmap_get_destroy(gh
);
2780 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2783 stat
= idmap_getuidbywinname(cp
, NULL
, flag
, &pid
);
2786 stat
= idmap_get_sidbyuid(gh
, pid
, flag
, &numericsid
,
2789 stat
= idmap_getgidbywinname(cp
, NULL
, flag
, &pid
);
2792 stat
= idmap_get_sidbygid(gh
, pid
, flag
, &numericsid
,
2796 idmap_get_destroy(gh
);
2799 stat
= idmap_get_mappings(gh
);
2800 idmap_get_destroy(gh
);
2805 if (numericsid
== NULL
)
2809 /* will be further decoded below */
2812 if (strncmp(cp
, "S-1-", 4) == 0) {
2813 /* It's a numeric SID (eg "S-1-234-567-89") */
2814 (void) strlcpy(domain
, cp
, domainlen
);
2817 cp
= strrchr(domain
, '-');
2820 *ridp
= strtoull(cp
, &end
, 10);
2828 if (errno
!= 0 || *end
!= '\0')
2830 } else if (!isdigit(*cp
)) {
2832 * It's a user/group name (eg "user") that needs to be
2833 * turned into a uid/gid
2835 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2851 /* It's a user/group ID (eg "12345"). */
2852 uid_t id
= strtoul(cp
, &end
, 10);
2859 /* It's an ephemeral ID. */
2860 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2861 &mapdomain
, &rid
) != 0)
2863 (void) strlcpy(domain
, mapdomain
, domainlen
);
2870 ASSERT3P(numericsid
, ==, NULL
);
2875 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2876 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2879 zfs_cmd_t zc
= { 0 };
2881 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2883 err
= userquota_propname_decode(propname
,
2884 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2885 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2886 zc
.zc_objset_type
= *typep
;
2890 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2894 *propvalue
= zc
.zc_cookie
;
2899 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2900 uint64_t *propvalue
)
2902 zfs_userquota_prop_t type
;
2904 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2909 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2910 char *propbuf
, int proplen
, boolean_t literal
)
2914 zfs_userquota_prop_t type
;
2916 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2923 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
2924 } else if (propvalue
== 0 &&
2925 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2926 (void) strlcpy(propbuf
, "none", proplen
);
2928 zfs_nicenum(propvalue
, propbuf
, proplen
);
2934 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2935 uint64_t *propvalue
)
2938 zfs_cmd_t zc
= { 0 };
2939 const char *snapname
;
2941 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2943 snapname
= strchr(propname
, '@') + 1;
2944 if (strchr(snapname
, '@')) {
2945 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2947 /* snapname is the short name, append it to zhp's fsname */
2950 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2951 sizeof (zc
.zc_value
));
2952 cp
= strchr(zc
.zc_value
, '@');
2955 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2956 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2959 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2963 *propvalue
= zc
.zc_cookie
;
2968 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2969 char *propbuf
, int proplen
, boolean_t literal
)
2974 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2980 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
2982 zfs_nicenum(propvalue
, propbuf
, proplen
);
2988 * Returns the name of the given zfs handle.
2991 zfs_get_name(const zfs_handle_t
*zhp
)
2993 return (zhp
->zfs_name
);
2997 * Returns the name of the parent pool for the given zfs handle.
3000 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3002 return (zhp
->zpool_hdl
->zpool_name
);
3006 * Returns the type of the given zfs handle.
3009 zfs_get_type(const zfs_handle_t
*zhp
)
3011 return (zhp
->zfs_type
);
3015 * Is one dataset name a child dataset of another?
3017 * Needs to handle these cases:
3018 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3019 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3020 * Descendant? No. No. No. Yes.
3023 is_descendant(const char *ds1
, const char *ds2
)
3025 size_t d1len
= strlen(ds1
);
3027 /* ds2 can't be a descendant if it's smaller */
3028 if (strlen(ds2
) < d1len
)
3031 /* otherwise, compare strings and verify that there's a '/' char */
3032 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3036 * Given a complete name, return just the portion that refers to the parent.
3037 * Will return -1 if there is no parent (path is just the name of the
3041 parent_name(const char *path
, char *buf
, size_t buflen
)
3045 (void) strlcpy(buf
, path
, buflen
);
3047 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3055 * If accept_ancestor is false, then check to make sure that the given path has
3056 * a parent, and that it exists. If accept_ancestor is true, then find the
3057 * closest existing ancestor for the given path. In prefixlen return the
3058 * length of already existing prefix of the given path. We also fetch the
3059 * 'zoned' property, which is used to validate property settings when creating
3063 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3064 boolean_t accept_ancestor
, int *prefixlen
)
3066 zfs_cmd_t zc
= { 0 };
3067 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3073 (void) snprintf(errbuf
, sizeof (errbuf
),
3074 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3076 /* get parent, and check to see if this is just a pool */
3077 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3078 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3079 "missing dataset name"));
3080 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3083 /* check to see if the pool exists */
3084 if ((slash
= strchr(parent
, '/')) == NULL
)
3085 slash
= parent
+ strlen(parent
);
3086 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3087 zc
.zc_name
[slash
- parent
] = '\0';
3088 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3090 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3091 "no such pool '%s'"), zc
.zc_name
);
3092 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3095 /* check to see if the parent dataset exists */
3096 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3097 if (errno
== ENOENT
&& accept_ancestor
) {
3099 * Go deeper to find an ancestor, give up on top level.
3101 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3103 "no such pool '%s'"), zc
.zc_name
);
3104 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3106 } else if (errno
== ENOENT
) {
3107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3108 "parent does not exist"));
3109 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3111 return (zfs_standard_error(hdl
, errno
, errbuf
));
3114 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3118 /* we are in a non-global zone, but parent is in the global zone */
3119 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3120 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3125 /* make sure parent is a filesystem */
3126 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3127 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3128 "parent is not a filesystem"));
3129 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3135 if (prefixlen
!= NULL
)
3136 *prefixlen
= strlen(parent
);
3141 * Finds whether the dataset of the given type(s) exists.
3144 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3148 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3152 * Try to get stats for the dataset, which will tell us if it exists.
3154 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3155 int ds_type
= zhp
->zfs_type
;
3158 if (types
& ds_type
)
3165 * Given a path to 'target', create all the ancestors between
3166 * the prefixlen portion of the path, and the target itself.
3167 * Fail if the initial prefixlen-ancestor does not already exist.
3170 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3176 /* make sure prefix exists */
3177 cp
= target
+ prefixlen
;
3179 assert(strchr(cp
, '/') == NULL
);
3180 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3183 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3191 * Attempt to create, mount, and share any ancestor filesystems,
3192 * up to the prefixlen-long one.
3194 for (cp
= target
+ prefixlen
+ 1;
3195 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3199 h
= make_dataset_handle(hdl
, target
);
3201 /* it already exists, nothing to do here */
3206 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3208 opname
= dgettext(TEXT_DOMAIN
, "create");
3212 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3214 opname
= dgettext(TEXT_DOMAIN
, "open");
3218 if (zfs_mount(h
, NULL
, 0) != 0) {
3219 opname
= dgettext(TEXT_DOMAIN
, "mount");
3223 if (zfs_share(h
) != 0) {
3224 opname
= dgettext(TEXT_DOMAIN
, "share");
3234 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3235 "failed to %s ancestor '%s'"), opname
, target
);
3240 * Creates non-existing ancestors of the given path.
3243 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3249 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3252 if ((path_copy
= strdup(path
)) != NULL
) {
3253 rc
= create_parents(hdl
, path_copy
, prefix
);
3256 if (path_copy
== NULL
|| rc
!= 0)
3263 * Create a new filesystem or volume.
3266 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3271 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3274 enum lzc_dataset_type ost
;
3275 zpool_handle_t
*zpool_handle
;
3277 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3278 "cannot create '%s'"), path
);
3280 /* validate the path, taking care to note the extended error message */
3281 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3282 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3284 /* validate parents exist */
3285 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3289 * The failure modes when creating a dataset of a different type over
3290 * one that already exists is a little strange. In particular, if you
3291 * try to create a dataset on top of an existing dataset, the ioctl()
3292 * will return ENOENT, not EEXIST. To prevent this from happening, we
3293 * first try to see if the dataset exists.
3295 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3296 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3297 "dataset already exists"));
3298 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3301 if (type
== ZFS_TYPE_VOLUME
)
3302 ost
= LZC_DATSET_TYPE_ZVOL
;
3304 ost
= LZC_DATSET_TYPE_ZFS
;
3306 /* open zpool handle for prop validation */
3307 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3308 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3310 /* truncate pool_path at first slash */
3311 char *p
= strchr(pool_path
, '/');
3315 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3318 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3319 zoned
, NULL
, zpool_handle
, errbuf
)) == 0) {
3320 zpool_close(zpool_handle
);
3323 zpool_close(zpool_handle
);
3325 if (type
== ZFS_TYPE_VOLUME
) {
3327 * If we are creating a volume, the size and block size must
3328 * satisfy a few restraints. First, the blocksize must be a
3329 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3330 * volsize must be a multiple of the block size, and cannot be
3333 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3334 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3336 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3337 "missing volume size"));
3338 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3341 if ((ret
= nvlist_lookup_uint64(props
,
3342 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3343 &blocksize
)) != 0) {
3344 if (ret
== ENOENT
) {
3345 blocksize
= zfs_prop_default_numeric(
3346 ZFS_PROP_VOLBLOCKSIZE
);
3349 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3350 "missing volume block size"));
3351 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3357 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3358 "volume size cannot be zero"));
3359 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3362 if (size
% blocksize
!= 0) {
3364 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3365 "volume size must be a multiple of volume block "
3367 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3371 /* create the dataset */
3372 ret
= lzc_create(path
, ost
, props
);
3375 /* check for failure */
3377 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3378 (void) parent_name(path
, parent
, sizeof (parent
));
3382 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3383 "no such parent '%s'"), parent
);
3384 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3387 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3388 "parent '%s' is not a filesystem"), parent
);
3389 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3392 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3393 "pool must be upgraded to set this "
3394 "property or value"));
3395 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3399 * This platform can't address a volume this big.
3401 if (type
== ZFS_TYPE_VOLUME
)
3402 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3407 return (zfs_standard_error(hdl
, errno
, errbuf
));
3415 * Destroys the given dataset. The caller must make sure that the filesystem
3416 * isn't mounted, and that there are no active dependents. If the file system
3417 * does not exist this function does nothing.
3420 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3422 zfs_cmd_t zc
= { 0 };
3424 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3425 nvlist_t
*nv
= fnvlist_alloc();
3426 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3427 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3430 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3431 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3437 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3439 if (ZFS_IS_VOLUME(zhp
)) {
3440 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3442 zc
.zc_objset_type
= DMU_OST_ZFS
;
3445 zc
.zc_defer_destroy
= defer
;
3446 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3448 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3449 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3453 remove_mountpoint(zhp
);
3458 struct destroydata
{
3460 const char *snapname
;
3464 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3466 struct destroydata
*dd
= arg
;
3467 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3470 (void) snprintf(name
, sizeof (name
),
3471 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3473 if (lzc_exists(name
))
3474 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3476 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3482 * Destroys all snapshots with the given name in zhp & descendants.
3485 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3488 struct destroydata dd
= { 0 };
3490 dd
.snapname
= snapname
;
3491 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3492 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3494 if (nvlist_empty(dd
.nvl
)) {
3495 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3496 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3497 zhp
->zfs_name
, snapname
);
3499 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3501 nvlist_free(dd
.nvl
);
3506 * Destroys all the snapshots named in the nvlist.
3509 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3512 nvlist_t
*errlist
= NULL
;
3514 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3517 nvlist_free(errlist
);
3521 if (nvlist_empty(errlist
)) {
3523 (void) snprintf(errbuf
, sizeof (errbuf
),
3524 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3526 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3528 for (nvpair_t
*pair
= nvlist_next_nvpair(errlist
, NULL
);
3529 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3531 (void) snprintf(errbuf
, sizeof (errbuf
),
3532 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3535 switch (fnvpair_value_int32(pair
)) {
3538 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3539 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3542 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3547 nvlist_free(errlist
);
3552 * Clones the given dataset. The target must be of the same type as the source.
3555 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3557 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3560 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3563 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3565 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3566 "cannot create '%s'"), target
);
3568 /* validate the target/clone name */
3569 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3570 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3572 /* validate parents exist */
3573 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3576 (void) parent_name(target
, parent
, sizeof (parent
));
3582 if (ZFS_IS_VOLUME(zhp
)) {
3583 type
= ZFS_TYPE_VOLUME
;
3585 type
= ZFS_TYPE_FILESYSTEM
;
3587 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3588 zhp
, zhp
->zpool_hdl
, errbuf
)) == NULL
)
3592 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3600 * The parent doesn't exist. We should have caught this
3601 * above, but there may a race condition that has since
3602 * destroyed the parent.
3604 * At this point, we don't know whether it's the source
3605 * that doesn't exist anymore, or whether the target
3606 * dataset doesn't exist.
3608 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3609 "no such parent '%s'"), parent
);
3610 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3613 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3614 "source and target pools differ"));
3615 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3619 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3628 * Promotes the given clone fs to be the clone parent.
3631 zfs_promote(zfs_handle_t
*zhp
)
3633 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3634 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3638 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3639 "cannot promote '%s'"), zhp
->zfs_name
);
3641 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3642 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3643 "snapshots can not be promoted"));
3644 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3647 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
3648 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3649 "not a cloned filesystem"));
3650 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3653 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
3658 /* There is a conflicting snapshot name. */
3659 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3660 "conflicting snapshot '%s' from parent '%s'"),
3661 snapname
, zhp
->zfs_dmustats
.dds_origin
);
3662 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3665 return (zfs_standard_error(hdl
, ret
, errbuf
));
3671 typedef struct snapdata
{
3673 const char *sd_snapname
;
3677 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3679 snapdata_t
*sd
= arg
;
3680 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3683 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3684 (void) snprintf(name
, sizeof (name
),
3685 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3687 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3689 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3697 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3701 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3708 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3709 "cannot create snapshots "));
3712 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3713 const char *snapname
= nvpair_name(elem
);
3715 /* validate the target name */
3716 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3718 (void) snprintf(errbuf
, sizeof (errbuf
),
3719 dgettext(TEXT_DOMAIN
,
3720 "cannot create snapshot '%s'"), snapname
);
3721 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3726 * get pool handle for prop validation. assumes all snaps are in the
3727 * same pool, as does lzc_snapshot (below).
3729 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
3730 elem
= nvlist_next_nvpair(snaps
, NULL
);
3731 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
3732 pool
[strcspn(pool
, "/@")] = '\0';
3733 zpool_handle_t
*zpool_hdl
= zpool_open(hdl
, pool
);
3735 if (props
!= NULL
&&
3736 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3737 props
, B_FALSE
, NULL
, zpool_hdl
, errbuf
)) == NULL
) {
3738 zpool_close(zpool_hdl
);
3741 zpool_close(zpool_hdl
);
3743 ret
= lzc_snapshot(snaps
, props
, &errors
);
3746 boolean_t printed
= B_FALSE
;
3747 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3749 elem
= nvlist_next_nvpair(errors
, elem
)) {
3750 (void) snprintf(errbuf
, sizeof (errbuf
),
3751 dgettext(TEXT_DOMAIN
,
3752 "cannot create snapshot '%s'"), nvpair_name(elem
));
3753 (void) zfs_standard_error(hdl
,
3754 fnvpair_value_int32(elem
), errbuf
);
3760 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3761 "multiple snapshots of same "
3763 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3767 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3773 nvlist_free(errors
);
3778 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3782 snapdata_t sd
= { 0 };
3783 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
3788 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3789 "cannot snapshot %s"), path
);
3791 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3792 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3794 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3795 cp
= strchr(fsname
, '@');
3797 sd
.sd_snapname
= cp
+ 1;
3799 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3800 ZFS_TYPE_VOLUME
)) == NULL
) {
3804 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3806 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3808 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3811 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3812 nvlist_free(sd
.sd_nvl
);
3818 * Destroy any more recent snapshots. We invoke this callback on any dependents
3819 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3820 * is a dependent and we should just destroy it without checking the transaction
3823 typedef struct rollback_data
{
3824 const char *cb_target
; /* the snapshot */
3825 uint64_t cb_create
; /* creation time reference */
3831 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3833 rollback_data_t
*cbp
= data
;
3834 prop_changelist_t
*clp
;
3836 /* We must destroy this clone; first unmount it */
3837 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3838 cbp
->cb_force
? MS_FORCE
: 0);
3839 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3840 cbp
->cb_error
= B_TRUE
;
3844 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3845 cbp
->cb_error
= B_TRUE
;
3847 changelist_remove(clp
, zhp
->zfs_name
);
3848 (void) changelist_postfix(clp
);
3849 changelist_free(clp
);
3856 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3858 rollback_data_t
*cbp
= data
;
3860 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3861 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3862 rollback_destroy_dependent
, cbp
);
3864 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3872 * Given a dataset, rollback to a specific snapshot, discarding any
3873 * data changes since then and making it the active dataset.
3875 * Any snapshots and bookmarks more recent than the target are
3876 * destroyed, along with their dependents (i.e. clones).
3879 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3881 rollback_data_t cb
= { 0 };
3883 boolean_t restore_resv
= 0;
3884 uint64_t old_volsize
= 0, new_volsize
;
3885 zfs_prop_t resv_prop
;
3887 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3888 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3891 * Destroy all recent snapshots and their dependents.
3893 cb
.cb_force
= force
;
3894 cb
.cb_target
= snap
->zfs_name
;
3895 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3896 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3897 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3903 * Now that we have verified that the snapshot is the latest,
3904 * rollback to the given snapshot.
3907 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3908 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3910 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3912 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3916 * We rely on zfs_iter_children() to verify that there are no
3917 * newer snapshots for the given dataset. Therefore, we can
3918 * simply pass the name on to the ioctl() call. There is still
3919 * an unlikely race condition where the user has taken a
3920 * snapshot since we verified that this was the most recent.
3922 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3924 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3925 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3931 * For volumes, if the pre-rollback volsize matched the pre-
3932 * rollback reservation and the volsize has changed then set
3933 * the reservation property to the post-rollback volsize.
3934 * Make a new handle since the rollback closed the dataset.
3936 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3937 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3939 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3940 if (old_volsize
!= new_volsize
)
3941 err
= zfs_prop_set_int(zhp
, resv_prop
,
3950 * Renames the given dataset.
3953 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
3954 boolean_t force_unmount
)
3957 zfs_cmd_t zc
= { 0 };
3959 prop_changelist_t
*cl
= NULL
;
3960 zfs_handle_t
*zhrp
= NULL
;
3961 char *parentname
= NULL
;
3962 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3963 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3966 /* if we have the same exact name, just return success */
3967 if (strcmp(zhp
->zfs_name
, target
) == 0)
3970 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3971 "cannot rename to '%s'"), target
);
3974 * Make sure the target name is valid
3976 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3977 if ((strchr(target
, '@') == NULL
) ||
3980 * Snapshot target name is abbreviated,
3981 * reconstruct full dataset name
3983 (void) strlcpy(parent
, zhp
->zfs_name
,
3985 delim
= strchr(parent
, '@');
3986 if (strchr(target
, '@') == NULL
)
3990 (void) strlcat(parent
, target
, sizeof (parent
));
3994 * Make sure we're renaming within the same dataset.
3996 delim
= strchr(target
, '@');
3997 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3998 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4000 "snapshots must be part of same "
4002 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4006 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4007 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4010 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4011 "recursive rename must be a snapshot"));
4012 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4015 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4016 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4018 /* validate parents */
4019 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4022 /* make sure we're in the same pool */
4023 verify((delim
= strchr(target
, '/')) != NULL
);
4024 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4025 zhp
->zfs_name
[delim
- target
] != '/') {
4026 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4027 "datasets must be within same pool"));
4028 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4031 /* new name cannot be a child of the current dataset name */
4032 if (is_descendant(zhp
->zfs_name
, target
)) {
4033 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4034 "New dataset name cannot be a descendant of "
4035 "current dataset name"));
4036 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4040 (void) snprintf(errbuf
, sizeof (errbuf
),
4041 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4043 if (getzoneid() == GLOBAL_ZONEID
&&
4044 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4045 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4046 "dataset is used in a non-global zone"));
4047 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4051 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4052 if (parentname
== NULL
) {
4056 delim
= strchr(parentname
, '@');
4058 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4063 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4064 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4065 force_unmount
? MS_FORCE
: 0)) == NULL
)
4068 if (changelist_haszonedchild(cl
)) {
4069 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4070 "child dataset with inherited mountpoint is used "
4071 "in a non-global zone"));
4072 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4077 if ((ret
= changelist_prefix(cl
)) != 0)
4081 if (ZFS_IS_VOLUME(zhp
))
4082 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4084 zc
.zc_objset_type
= DMU_OST_ZFS
;
4086 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4087 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4089 zc
.zc_cookie
= recursive
;
4091 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4093 * if it was recursive, the one that actually failed will
4096 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4097 "cannot rename '%s'"), zc
.zc_name
);
4099 if (recursive
&& errno
== EEXIST
) {
4100 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4101 "a child dataset already has a snapshot "
4102 "with the new name"));
4103 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4105 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4109 * On failure, we still want to remount any filesystems that
4110 * were previously mounted, so we don't alter the system state.
4113 (void) changelist_postfix(cl
);
4116 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4117 ret
= changelist_postfix(cl
);
4122 if (parentname
!= NULL
) {
4129 changelist_free(cl
);
4135 zfs_get_user_props(zfs_handle_t
*zhp
)
4137 return (zhp
->zfs_user_props
);
4141 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4143 if (zhp
->zfs_recvd_props
== NULL
)
4144 if (get_recvd_props_ioctl(zhp
) != 0)
4146 return (zhp
->zfs_recvd_props
);
4150 * This function is used by 'zfs list' to determine the exact set of columns to
4151 * display, and their maximum widths. This does two main things:
4153 * - If this is a list of all properties, then expand the list to include
4154 * all native properties, and set a flag so that for each dataset we look
4155 * for new unique user properties and add them to the list.
4157 * - For non fixed-width properties, keep track of the maximum width seen
4158 * so that we can size the column appropriately. If the user has
4159 * requested received property values, we also need to compute the width
4160 * of the RECEIVED column.
4163 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4166 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4167 zprop_list_t
*entry
;
4168 zprop_list_t
**last
, **start
;
4169 nvlist_t
*userprops
, *propval
;
4172 char buf
[ZFS_MAXPROPLEN
];
4174 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4177 userprops
= zfs_get_user_props(zhp
);
4180 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4182 * Go through and add any user properties as necessary. We
4183 * start by incrementing our list pointer to the first
4184 * non-native property.
4187 while (*start
!= NULL
) {
4188 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4190 start
= &(*start
)->pl_next
;
4194 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4196 * See if we've already found this property in our list.
4198 for (last
= start
; *last
!= NULL
;
4199 last
= &(*last
)->pl_next
) {
4200 if (strcmp((*last
)->pl_user_prop
,
4201 nvpair_name(elem
)) == 0)
4205 if (*last
== NULL
) {
4206 if ((entry
= zfs_alloc(hdl
,
4207 sizeof (zprop_list_t
))) == NULL
||
4208 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4209 nvpair_name(elem
)))) == NULL
) {
4214 entry
->pl_prop
= ZPROP_INVAL
;
4215 entry
->pl_width
= strlen(nvpair_name(elem
));
4216 entry
->pl_all
= B_TRUE
;
4223 * Now go through and check the width of any non-fixed columns
4225 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4226 if (entry
->pl_fixed
&& !literal
)
4229 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4230 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4231 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4232 if (strlen(buf
) > entry
->pl_width
)
4233 entry
->pl_width
= strlen(buf
);
4235 if (received
&& zfs_prop_get_recvd(zhp
,
4236 zfs_prop_to_name(entry
->pl_prop
),
4237 buf
, sizeof (buf
), literal
) == 0)
4238 if (strlen(buf
) > entry
->pl_recvd_width
)
4239 entry
->pl_recvd_width
= strlen(buf
);
4241 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4243 verify(nvlist_lookup_string(propval
,
4244 ZPROP_VALUE
, &strval
) == 0);
4245 if (strlen(strval
) > entry
->pl_width
)
4246 entry
->pl_width
= strlen(strval
);
4248 if (received
&& zfs_prop_get_recvd(zhp
,
4249 entry
->pl_user_prop
,
4250 buf
, sizeof (buf
), literal
) == 0)
4251 if (strlen(buf
) > entry
->pl_recvd_width
)
4252 entry
->pl_recvd_width
= strlen(buf
);
4260 zfs_deleg_share_nfs(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4261 char *resource
, void *export
, void *sharetab
,
4262 int sharemax
, zfs_share_op_t operation
)
4264 zfs_cmd_t zc
= { 0 };
4267 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4268 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4270 (void) strlcpy(zc
.zc_string
, resource
, sizeof (zc
.zc_string
));
4271 zc
.zc_share
.z_sharedata
= (uint64_t)(uintptr_t)sharetab
;
4272 zc
.zc_share
.z_exportdata
= (uint64_t)(uintptr_t)export
;
4273 zc
.zc_share
.z_sharetype
= operation
;
4274 zc
.zc_share
.z_sharemax
= sharemax
;
4275 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SHARE
, &zc
);
4280 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4285 * Keep a reference to the props-table against which we prune the
4288 zhp
->zfs_props_table
= props
;
4290 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4293 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4294 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4297 * User properties will result in ZPROP_INVAL, and since we
4298 * only know how to prune standard ZFS properties, we always
4299 * leave these in the list. This can also happen if we
4300 * encounter an unknown DSL property (when running older
4301 * software, for example).
4303 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4304 (void) nvlist_remove(zhp
->zfs_props
,
4305 nvpair_name(curr
), nvpair_type(curr
));
4311 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4312 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4314 zfs_cmd_t zc
= { 0 };
4315 nvlist_t
*nvlist
= NULL
;
4318 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4319 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4320 zc
.zc_cookie
= (uint64_t)cmd
;
4322 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4323 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4324 (void) no_memory(hdl
);
4330 case ZFS_SMB_ACL_ADD
:
4331 case ZFS_SMB_ACL_REMOVE
:
4332 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4334 case ZFS_SMB_ACL_RENAME
:
4335 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4337 (void) no_memory(hdl
);
4340 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4342 (void) no_memory(hdl
);
4345 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4346 nvlist_free(nvlist
);
4350 case ZFS_SMB_ACL_PURGE
:
4355 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4356 nvlist_free(nvlist
);
4361 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4362 char *path
, char *resource
)
4364 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4369 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4370 char *path
, char *resource
)
4372 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4377 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4379 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4384 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4385 char *oldname
, char *newname
)
4387 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4392 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4393 zfs_userspace_cb_t func
, void *arg
)
4395 zfs_cmd_t zc
= { 0 };
4396 zfs_useracct_t buf
[100];
4397 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4400 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4402 zc
.zc_objset_type
= type
;
4403 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4406 zfs_useracct_t
*zua
= buf
;
4408 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4409 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4412 (void) snprintf(errbuf
, sizeof (errbuf
),
4413 dgettext(TEXT_DOMAIN
,
4414 "cannot get used/quota for %s"), zc
.zc_name
);
4415 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4417 if (zc
.zc_nvlist_dst_size
== 0)
4420 while (zc
.zc_nvlist_dst_size
> 0) {
4421 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4422 zua
->zu_space
)) != 0)
4425 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4434 const char *snapname
;
4436 boolean_t recursive
;
4441 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4443 struct holdarg
*ha
= arg
;
4444 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4447 (void) snprintf(name
, sizeof (name
),
4448 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4450 if (lzc_exists(name
))
4451 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4454 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4460 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4461 boolean_t recursive
, int cleanup_fd
)
4466 ha
.nvl
= fnvlist_alloc();
4467 ha
.snapname
= snapname
;
4469 ha
.recursive
= recursive
;
4470 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4472 if (nvlist_empty(ha
.nvl
)) {
4475 fnvlist_free(ha
.nvl
);
4477 (void) snprintf(errbuf
, sizeof (errbuf
),
4478 dgettext(TEXT_DOMAIN
,
4479 "cannot hold snapshot '%s@%s'"),
4480 zhp
->zfs_name
, snapname
);
4481 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4485 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4486 fnvlist_free(ha
.nvl
);
4492 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4496 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4501 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4504 /* There may be errors even in the success case. */
4505 fnvlist_free(errors
);
4509 if (nvlist_empty(errors
)) {
4510 /* no hold-specific errors */
4511 (void) snprintf(errbuf
, sizeof (errbuf
),
4512 dgettext(TEXT_DOMAIN
, "cannot hold"));
4515 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4516 "pool must be upgraded"));
4517 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4520 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4523 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4527 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4529 elem
= nvlist_next_nvpair(errors
, elem
)) {
4530 (void) snprintf(errbuf
, sizeof (errbuf
),
4531 dgettext(TEXT_DOMAIN
,
4532 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4533 switch (fnvpair_value_int32(elem
)) {
4536 * Temporary tags wind up having the ds object id
4537 * prepended. So even if we passed the length check
4538 * above, it's still possible for the tag to wind
4539 * up being slightly too long.
4541 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4544 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4547 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4550 (void) zfs_standard_error(hdl
,
4551 fnvpair_value_int32(elem
), errbuf
);
4555 fnvlist_free(errors
);
4560 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4562 struct holdarg
*ha
= arg
;
4563 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4565 nvlist_t
*existing_holds
;
4567 (void) snprintf(name
, sizeof (name
),
4568 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4570 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4572 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4575 nvlist_t
*torelease
= fnvlist_alloc();
4576 fnvlist_add_boolean(torelease
, ha
->tag
);
4577 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4578 fnvlist_free(torelease
);
4582 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4588 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4589 boolean_t recursive
)
4593 nvlist_t
*errors
= NULL
;
4595 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4598 ha
.nvl
= fnvlist_alloc();
4599 ha
.snapname
= snapname
;
4601 ha
.recursive
= recursive
;
4603 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4605 if (nvlist_empty(ha
.nvl
)) {
4606 fnvlist_free(ha
.nvl
);
4608 (void) snprintf(errbuf
, sizeof (errbuf
),
4609 dgettext(TEXT_DOMAIN
,
4610 "cannot release hold from snapshot '%s@%s'"),
4611 zhp
->zfs_name
, snapname
);
4613 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4615 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4620 ret
= lzc_release(ha
.nvl
, &errors
);
4621 fnvlist_free(ha
.nvl
);
4624 /* There may be errors even in the success case. */
4625 fnvlist_free(errors
);
4629 if (nvlist_empty(errors
)) {
4630 /* no hold-specific errors */
4631 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4636 "pool must be upgraded"));
4637 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4640 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4644 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4646 elem
= nvlist_next_nvpair(errors
, elem
)) {
4647 (void) snprintf(errbuf
, sizeof (errbuf
),
4648 dgettext(TEXT_DOMAIN
,
4649 "cannot release hold from snapshot '%s'"),
4651 switch (fnvpair_value_int32(elem
)) {
4653 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4656 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4659 (void) zfs_standard_error_fmt(hdl
,
4660 fnvpair_value_int32(elem
), errbuf
);
4664 fnvlist_free(errors
);
4669 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4671 zfs_cmd_t zc
= { 0 };
4672 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4678 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4679 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4683 nvbuf
= malloc(nvsz
);
4684 if (nvbuf
== NULL
) {
4685 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4689 zc
.zc_nvlist_dst_size
= nvsz
;
4690 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4692 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4694 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4695 (void) snprintf(errbuf
, sizeof (errbuf
),
4696 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4701 nvsz
= zc
.zc_nvlist_dst_size
;
4705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4706 "pool must be upgraded"));
4707 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4710 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4713 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4716 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4721 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4723 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4724 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4726 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4736 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4738 zfs_cmd_t zc
= { 0 };
4739 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4745 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4746 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4748 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4751 nvbuf
= malloc(nvsz
);
4753 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4756 zc
.zc_nvlist_src_size
= nvsz
;
4757 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4758 zc
.zc_perm_action
= un
;
4760 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4762 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4763 (void) snprintf(errbuf
, sizeof (errbuf
),
4764 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4768 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4769 "pool must be upgraded"));
4770 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4773 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4776 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4779 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4790 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4795 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4798 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4800 (void) snprintf(errbuf
, sizeof (errbuf
),
4801 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4805 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4806 "pool must be upgraded"));
4807 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4810 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4813 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4816 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4825 * Convert the zvol's volume size to an appropriate reservation.
4826 * Note: If this routine is updated, it is necessary to update the ZFS test
4827 * suite's shell version in reservation.kshlib.
4830 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4833 uint64_t nblocks
, volblocksize
;
4837 if (nvlist_lookup_string(props
,
4838 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4839 ncopies
= atoi(strval
);
4842 if (nvlist_lookup_uint64(props
,
4843 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4844 &volblocksize
) != 0)
4845 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4846 nblocks
= volsize
/volblocksize
;
4847 /* start with metadnode L0-L6 */
4849 /* calculate number of indirects */
4850 while (nblocks
> 1) {
4851 nblocks
+= DNODES_PER_LEVEL
- 1;
4852 nblocks
/= DNODES_PER_LEVEL
;
4855 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4858 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4859 * compressed, but in practice they compress down to about
4862 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;