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>
46 #include <sys/mntent.h>
47 #include <sys/mount.h>
55 #include <directory.h>
57 #include <sys/dnode.h>
62 #include "zfs_namecheck.h"
64 #include "libzfs_impl.h"
65 #include "zfs_deleg.h"
67 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
68 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
71 * Given a single type (not a mask of types), return the type in a human
75 zfs_type_to_name(zfs_type_t type
)
78 case ZFS_TYPE_FILESYSTEM
:
79 return (dgettext(TEXT_DOMAIN
, "filesystem"));
80 case ZFS_TYPE_SNAPSHOT
:
81 return (dgettext(TEXT_DOMAIN
, "snapshot"));
83 return (dgettext(TEXT_DOMAIN
, "volume"));
85 return (dgettext(TEXT_DOMAIN
, "pool"));
86 case ZFS_TYPE_BOOKMARK
:
87 return (dgettext(TEXT_DOMAIN
, "bookmark"));
89 assert(!"unhandled zfs_type_t");
96 * Validate a ZFS path. This is used even before trying to open the dataset, to
97 * provide a more meaningful error message. We call zfs_error_aux() to
98 * explain exactly why the name was not valid.
101 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
107 (void) zfs_prop_get_table();
108 if (entity_namecheck(path
, &why
, &what
) != 0) {
111 case NAME_ERR_TOOLONG
:
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "name is too long"));
116 case NAME_ERR_LEADING_SLASH
:
117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
118 "leading slash in name"));
121 case NAME_ERR_EMPTY_COMPONENT
:
122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
123 "empty component in name"));
126 case NAME_ERR_TRAILING_SLASH
:
127 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
128 "trailing slash in name"));
131 case NAME_ERR_INVALCHAR
:
133 dgettext(TEXT_DOMAIN
, "invalid character "
134 "'%c' in name"), what
);
137 case NAME_ERR_MULTIPLE_DELIMITERS
:
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "multiple '@' and/or '#' delimiters in "
143 case NAME_ERR_NOLETTER
:
144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
145 "pool doesn't begin with a letter"));
148 case NAME_ERR_RESERVED
:
149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
150 "name is reserved"));
153 case NAME_ERR_DISKLIKE
:
154 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
155 "reserved disk name"));
159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
160 "(%d) not defined"), why
);
168 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
170 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
171 "snapshot delimiter '@' is not expected here"));
175 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
177 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
178 "missing '@' delimiter in snapshot name"));
182 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
184 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
185 "bookmark delimiter '#' is not expected here"));
189 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
191 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
192 "missing '#' delimiter in bookmark name"));
196 if (modifying
&& strchr(path
, '%') != NULL
) {
198 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
199 "invalid character %c in name"), '%');
207 zfs_name_valid(const char *name
, zfs_type_t type
)
209 if (type
== ZFS_TYPE_POOL
)
210 return (zpool_name_valid(NULL
, B_FALSE
, name
));
211 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
215 * This function takes the raw DSL properties, and filters out the user-defined
216 * properties into a separate nvlist.
219 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
221 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
226 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
227 (void) no_memory(hdl
);
232 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
233 if (!zfs_prop_user(nvpair_name(elem
)))
236 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
237 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
239 (void) no_memory(hdl
);
247 static zpool_handle_t
*
248 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
250 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
253 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
254 if (hdl
->libzfs_pool_handles
!= NULL
)
255 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
256 hdl
->libzfs_pool_handles
= zph
;
261 static zpool_handle_t
*
262 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
264 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
265 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
267 while ((zph
!= NULL
) &&
268 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
269 zph
= zph
->zpool_next
;
274 * Returns a handle to the pool that contains the provided dataset.
275 * If a handle to that pool already exists then that handle is returned.
276 * Otherwise, a new handle is created and added to the list of handles.
278 static zpool_handle_t
*
279 zpool_handle(zfs_handle_t
*zhp
)
285 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
286 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
287 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
289 zph
= zpool_find_handle(zhp
, pool_name
, len
);
291 zph
= zpool_add_handle(zhp
, pool_name
);
298 zpool_free_handles(libzfs_handle_t
*hdl
)
300 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
302 while (zph
!= NULL
) {
303 next
= zph
->zpool_next
;
307 hdl
->libzfs_pool_handles
= NULL
;
311 * Utility function to gather stats (objset and zpl) for the given object.
314 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
316 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
318 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
320 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
321 if (errno
== ENOMEM
) {
322 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
333 * Utility function to get the received properties of the given object.
336 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
338 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
339 nvlist_t
*recvdprops
;
340 zfs_cmd_t zc
= { 0 };
343 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
346 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
348 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
349 if (errno
== ENOMEM
) {
350 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
354 zcmd_free_nvlists(&zc
);
359 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
360 zcmd_free_nvlists(&zc
);
364 nvlist_free(zhp
->zfs_recvd_props
);
365 zhp
->zfs_recvd_props
= recvdprops
;
371 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
373 nvlist_t
*allprops
, *userprops
;
375 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
377 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
382 * XXX Why do we store the user props separately, in addition to
383 * storing them in zfs_props?
385 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
386 nvlist_free(allprops
);
390 nvlist_free(zhp
->zfs_props
);
391 nvlist_free(zhp
->zfs_user_props
);
393 zhp
->zfs_props
= allprops
;
394 zhp
->zfs_user_props
= userprops
;
400 get_stats(zfs_handle_t
*zhp
)
403 zfs_cmd_t zc
= { 0 };
405 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
407 if (get_stats_ioctl(zhp
, &zc
) != 0)
409 else if (put_stats_zhdl(zhp
, &zc
) != 0)
411 zcmd_free_nvlists(&zc
);
416 * Refresh the properties currently stored in the handle.
419 zfs_refresh_properties(zfs_handle_t
*zhp
)
421 (void) get_stats(zhp
);
425 * Makes a handle from the given dataset name. Used by zfs_open() and
426 * zfs_iter_* to create child handles on the fly.
429 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
431 if (put_stats_zhdl(zhp
, zc
) != 0)
435 * We've managed to open the dataset and gather statistics. Determine
436 * the high-level type.
438 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
439 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
440 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
441 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
445 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
446 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
447 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
448 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
449 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
450 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
452 abort(); /* we should never see any other types */
454 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
461 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
463 zfs_cmd_t zc
= { 0 };
465 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
471 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
472 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
476 if (get_stats_ioctl(zhp
, &zc
) == -1) {
477 zcmd_free_nvlists(&zc
);
481 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
485 zcmd_free_nvlists(&zc
);
490 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
492 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
498 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
499 if (make_dataset_handle_common(zhp
, zc
) == -1) {
507 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
509 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
514 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
515 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
516 zhp
->zfs_head_type
= pzhp
->zfs_type
;
517 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
518 zhp
->zpool_hdl
= zpool_handle(zhp
);
523 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
525 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
530 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
531 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
532 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
533 sizeof (zhp
->zfs_name
));
534 zhp
->zfs_type
= zhp_orig
->zfs_type
;
535 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
536 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
537 if (zhp_orig
->zfs_props
!= NULL
) {
538 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
539 (void) no_memory(zhp
->zfs_hdl
);
544 if (zhp_orig
->zfs_user_props
!= NULL
) {
545 if (nvlist_dup(zhp_orig
->zfs_user_props
,
546 &zhp
->zfs_user_props
, 0) != 0) {
547 (void) no_memory(zhp
->zfs_hdl
);
552 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
553 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
554 &zhp
->zfs_recvd_props
, 0)) {
555 (void) no_memory(zhp
->zfs_hdl
);
560 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
561 if (zhp_orig
->zfs_mntopts
!= NULL
) {
562 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
563 zhp_orig
->zfs_mntopts
);
565 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
570 zfs_bookmark_exists(const char *path
)
574 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
581 (void) strlcpy(fsname
, path
, sizeof (fsname
));
582 pound
= strchr(fsname
, '#');
587 bmark_name
= pound
+ 1;
588 props
= fnvlist_alloc();
589 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
596 rv
= nvlist_exists(bmarks
, bmark_name
);
602 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
603 nvlist_t
*bmark_props
)
605 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
610 /* Fill in the name. */
611 zhp
->zfs_hdl
= parent
->zfs_hdl
;
612 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
614 /* Set the property lists. */
615 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
621 zhp
->zfs_head_type
= parent
->zfs_head_type
;
622 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
624 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
625 nvlist_free(zhp
->zfs_props
);
633 struct zfs_open_bookmarks_cb_data
{
639 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
641 struct zfs_open_bookmarks_cb_data
*dp
= data
;
644 * Is it the one we are looking for?
646 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
648 * We found it. Save it and let the caller know we are done.
655 * Not found. Close the handle and ask for another one.
662 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
663 * argument is a mask of acceptable types. The function will print an
664 * appropriate error message and return NULL if it can't be opened.
667 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
673 (void) snprintf(errbuf
, sizeof (errbuf
),
674 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
677 * Validate the name before we even try to open it.
679 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
680 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
685 * Bookmarks needs to be handled separately.
687 bookp
= strchr(path
, '#');
690 * Try to get stats for the dataset, which will tell us if it
694 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
695 (void) zfs_standard_error(hdl
, errno
, errbuf
);
699 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
701 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
704 * We need to cut out '#' and everything after '#'
705 * to get the parent dataset name only.
707 assert(bookp
- path
< sizeof (dsname
));
708 (void) strncpy(dsname
, path
, bookp
- path
);
709 dsname
[bookp
- path
] = '\0';
712 * Create handle for the parent dataset.
715 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
716 (void) zfs_standard_error(hdl
, errno
, errbuf
);
721 * Iterate bookmarks to find the right one.
724 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
725 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
726 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
730 if (cb_data
.zhp
== NULL
) {
731 (void) zfs_standard_error(hdl
, errno
, errbuf
);
743 if (!(types
& zhp
->zfs_type
)) {
744 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
753 * Release a ZFS handle. Nothing to do but free the associated memory.
756 zfs_close(zfs_handle_t
*zhp
)
758 if (zhp
->zfs_mntopts
)
759 free(zhp
->zfs_mntopts
);
760 nvlist_free(zhp
->zfs_props
);
761 nvlist_free(zhp
->zfs_user_props
);
762 nvlist_free(zhp
->zfs_recvd_props
);
766 typedef struct mnttab_node
{
767 struct mnttab mtn_mt
;
772 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
774 const mnttab_node_t
*mtn1
= arg1
;
775 const mnttab_node_t
*mtn2
= arg2
;
778 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
782 return (rv
> 0 ? 1 : -1);
786 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
788 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
789 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
790 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
794 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
798 rewind(hdl
->libzfs_mnttab
);
799 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
802 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
804 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
805 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
806 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
807 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
808 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
809 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
814 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
819 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
821 free(mtn
->mtn_mt
.mnt_special
);
822 free(mtn
->mtn_mt
.mnt_mountp
);
823 free(mtn
->mtn_mt
.mnt_fstype
);
824 free(mtn
->mtn_mt
.mnt_mntopts
);
827 avl_destroy(&hdl
->libzfs_mnttab_cache
);
831 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
833 hdl
->libzfs_mnttab_enable
= enable
;
837 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
838 struct mnttab
*entry
)
843 if (!hdl
->libzfs_mnttab_enable
) {
844 struct mnttab srch
= { 0 };
846 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
847 libzfs_mnttab_fini(hdl
);
848 rewind(hdl
->libzfs_mnttab
);
849 srch
.mnt_special
= (char *)fsname
;
850 srch
.mnt_fstype
= MNTTYPE_ZFS
;
851 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
857 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
858 libzfs_mnttab_update(hdl
);
860 find
.mtn_mt
.mnt_special
= (char *)fsname
;
861 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
863 *entry
= mtn
->mtn_mt
;
870 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
871 const char *mountp
, const char *mntopts
)
875 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
877 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
878 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
879 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
880 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
881 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
882 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
886 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
891 find
.mtn_mt
.mnt_special
= (char *)fsname
;
892 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
894 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
895 free(ret
->mtn_mt
.mnt_special
);
896 free(ret
->mtn_mt
.mnt_mountp
);
897 free(ret
->mtn_mt
.mnt_fstype
);
898 free(ret
->mtn_mt
.mnt_mntopts
);
904 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
906 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
908 if (zpool_handle
== NULL
)
911 *spa_version
= zpool_get_prop_int(zpool_handle
,
912 ZPOOL_PROP_VERSION
, NULL
);
917 * The choice of reservation property depends on the SPA version.
920 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
924 if (zfs_spa_version(zhp
, &spa_version
) < 0)
927 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
928 *resv_prop
= ZFS_PROP_REFRESERVATION
;
930 *resv_prop
= ZFS_PROP_RESERVATION
;
936 * Given an nvlist of properties to set, validates that they are correct, and
937 * parses any numeric properties (index, boolean, etc) if they are specified as
941 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
942 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
950 int chosen_normal
= -1;
953 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
954 (void) no_memory(hdl
);
959 * Make sure this property is valid and applies to this type.
963 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
964 const char *propname
= nvpair_name(elem
);
966 prop
= zfs_name_to_prop(propname
);
967 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
969 * This is a user property: make sure it's a
970 * string, and that it's less than ZAP_MAXNAMELEN.
972 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
974 "'%s' must be a string"), propname
);
975 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
979 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
980 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
981 "property name '%s' is too long"),
983 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
987 (void) nvpair_value_string(elem
, &strval
);
988 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
989 (void) no_memory(hdl
);
996 * Currently, only user properties can be modified on
999 if (type
== ZFS_TYPE_SNAPSHOT
) {
1000 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1001 "this property can not be modified for snapshots"));
1002 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1006 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1007 zfs_userquota_prop_t uqtype
;
1008 char newpropname
[128];
1013 if (userquota_propname_decode(propname
, zoned
,
1014 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1016 dgettext(TEXT_DOMAIN
,
1017 "'%s' has an invalid user/group name"),
1019 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1023 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1024 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
1026 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1028 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1033 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1034 (void) nvpair_value_string(elem
, &strval
);
1035 if (strcmp(strval
, "none") == 0) {
1037 } else if (zfs_nicestrtonum(hdl
,
1038 strval
, &intval
) != 0) {
1039 (void) zfs_error(hdl
,
1040 EZFS_BADPROP
, errbuf
);
1043 } else if (nvpair_type(elem
) ==
1045 (void) nvpair_value_uint64(elem
, &intval
);
1047 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1048 "use 'none' to disable "
1049 "userquota/groupquota"));
1053 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1054 "'%s' must be a number"), propname
);
1055 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1060 * Encode the prop name as
1061 * userquota@<hex-rid>-domain, to make it easy
1062 * for the kernel to decode.
1064 (void) snprintf(newpropname
, sizeof (newpropname
),
1065 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
1066 (longlong_t
)rid
, domain
);
1070 if (nvlist_add_uint64_array(ret
, newpropname
,
1072 (void) no_memory(hdl
);
1076 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1077 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1078 "'%s' is readonly"),
1080 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1084 if (prop
== ZPROP_INVAL
) {
1085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1086 "invalid property '%s'"), propname
);
1087 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1091 if (!zfs_prop_valid_for_type(prop
, type
)) {
1093 dgettext(TEXT_DOMAIN
, "'%s' does not "
1094 "apply to datasets of this type"), propname
);
1095 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1099 if (zfs_prop_readonly(prop
) &&
1100 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
1102 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1104 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1108 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1109 &strval
, &intval
, errbuf
) != 0)
1113 * Perform some additional checks for specific properties.
1116 case ZFS_PROP_VERSION
:
1122 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1123 if (intval
< version
) {
1124 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1125 "Can not downgrade; already at version %u"),
1127 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1133 case ZFS_PROP_VOLBLOCKSIZE
:
1134 case ZFS_PROP_RECORDSIZE
:
1136 int maxbs
= SPA_MAXBLOCKSIZE
;
1137 if (zpool_hdl
!= NULL
) {
1138 maxbs
= zpool_get_prop_int(zpool_hdl
,
1139 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1142 * Volumes are limited to a volblocksize of 128KB,
1143 * because they typically service workloads with
1144 * small random writes, which incur a large performance
1145 * penalty with large blocks.
1147 if (prop
== ZFS_PROP_VOLBLOCKSIZE
)
1148 maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1150 * The value must be a power of two between
1151 * SPA_MINBLOCKSIZE and maxbs.
1153 if (intval
< SPA_MINBLOCKSIZE
||
1154 intval
> maxbs
|| !ISP2(intval
)) {
1155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1156 "'%s' must be power of 2 from 512B "
1157 "to %uKB"), propname
, maxbs
>> 10);
1158 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1163 case ZFS_PROP_MLSLABEL
:
1166 * Verify the mlslabel string and convert to
1167 * internal hex label string.
1171 char *hex
= NULL
; /* internal label string */
1173 /* Default value is already OK. */
1174 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1177 /* Verify the label can be converted to binary form */
1178 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1179 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1180 L_NO_CORRECTION
, NULL
) == -1)) {
1184 /* Now translate to hex internal label string */
1185 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1191 m_label_free(new_sl
);
1193 /* If string is already in internal form, we're done. */
1194 if (strcmp(strval
, hex
) == 0) {
1199 /* Replace the label string with the internal form. */
1200 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1202 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1209 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1210 "invalid mlslabel '%s'"), strval
);
1211 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1212 m_label_free(new_sl
); /* OK if null */
1217 case ZFS_PROP_MOUNTPOINT
:
1219 namecheck_err_t why
;
1221 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1222 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1225 if (mountpoint_namecheck(strval
, &why
)) {
1227 case NAME_ERR_LEADING_SLASH
:
1229 dgettext(TEXT_DOMAIN
,
1230 "'%s' must be an absolute path, "
1231 "'none', or 'legacy'"), propname
);
1233 case NAME_ERR_TOOLONG
:
1235 dgettext(TEXT_DOMAIN
,
1236 "component of '%s' is too long"),
1242 dgettext(TEXT_DOMAIN
,
1243 "(%d) not defined"),
1247 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1254 case ZFS_PROP_SHARESMB
:
1255 case ZFS_PROP_SHARENFS
:
1257 * For the mountpoint and sharenfs or sharesmb
1258 * properties, check if it can be set in a
1259 * global/non-global zone based on
1260 * the zoned property value:
1262 * global zone non-global zone
1263 * --------------------------------------------------
1264 * zoned=on mountpoint (no) mountpoint (yes)
1265 * sharenfs (no) sharenfs (no)
1266 * sharesmb (no) sharesmb (no)
1268 * zoned=off mountpoint (yes) N/A
1273 if (getzoneid() == GLOBAL_ZONEID
) {
1274 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1275 "'%s' cannot be set on "
1276 "dataset in a non-global zone"),
1278 (void) zfs_error(hdl
, EZFS_ZONED
,
1281 } else if (prop
== ZFS_PROP_SHARENFS
||
1282 prop
== ZFS_PROP_SHARESMB
) {
1283 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1284 "'%s' cannot be set in "
1285 "a non-global zone"), propname
);
1286 (void) zfs_error(hdl
, EZFS_ZONED
,
1290 } else if (getzoneid() != GLOBAL_ZONEID
) {
1292 * If zoned property is 'off', this must be in
1293 * a global zone. If not, something is wrong.
1295 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1296 "'%s' cannot be set while dataset "
1297 "'zoned' property is set"), propname
);
1298 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1303 * At this point, it is legitimate to set the
1304 * property. Now we want to make sure that the
1305 * property value is valid if it is sharenfs.
1307 if ((prop
== ZFS_PROP_SHARENFS
||
1308 prop
== ZFS_PROP_SHARESMB
) &&
1309 strcmp(strval
, "on") != 0 &&
1310 strcmp(strval
, "off") != 0) {
1311 zfs_share_proto_t proto
;
1313 if (prop
== ZFS_PROP_SHARESMB
)
1319 * Must be an valid sharing protocol
1320 * option string so init the libshare
1321 * in order to enable the parser and
1322 * then parse the options. We use the
1323 * control API since we don't care about
1324 * the current configuration and don't
1325 * want the overhead of loading it
1326 * until we actually do something.
1329 if (zfs_init_libshare(hdl
,
1330 SA_INIT_CONTROL_API
) != SA_OK
) {
1332 * An error occurred so we can't do
1335 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1336 "'%s' cannot be set: problem "
1337 "in share initialization"),
1339 (void) zfs_error(hdl
, EZFS_BADPROP
,
1344 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1346 * There was an error in parsing so
1347 * deal with it by issuing an error
1348 * message and leaving after
1349 * uninitializing the the libshare
1352 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1353 "'%s' cannot be set to invalid "
1354 "options"), propname
);
1355 (void) zfs_error(hdl
, EZFS_BADPROP
,
1357 zfs_uninit_libshare(hdl
);
1360 zfs_uninit_libshare(hdl
);
1365 case ZFS_PROP_UTF8ONLY
:
1366 chosen_utf
= (int)intval
;
1369 case ZFS_PROP_NORMALIZE
:
1370 chosen_normal
= (int)intval
;
1378 * For changes to existing volumes, we have some additional
1379 * checks to enforce.
1381 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1382 uint64_t volsize
= zfs_prop_get_int(zhp
,
1384 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1385 ZFS_PROP_VOLBLOCKSIZE
);
1389 case ZFS_PROP_RESERVATION
:
1390 case ZFS_PROP_REFRESERVATION
:
1391 if (intval
> volsize
) {
1392 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1393 "'%s' is greater than current "
1394 "volume size"), propname
);
1395 (void) zfs_error(hdl
, EZFS_BADPROP
,
1401 case ZFS_PROP_VOLSIZE
:
1402 if (intval
% blocksize
!= 0) {
1403 zfs_nicenum(blocksize
, buf
,
1405 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1406 "'%s' must be a multiple of "
1407 "volume block size (%s)"),
1409 (void) zfs_error(hdl
, EZFS_BADPROP
,
1415 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1416 "'%s' cannot be zero"),
1418 (void) zfs_error(hdl
, EZFS_BADPROP
,
1431 * If normalization was chosen, but no UTF8 choice was made,
1432 * enforce rejection of non-UTF8 names.
1434 * If normalization was chosen, but rejecting non-UTF8 names
1435 * was explicitly not chosen, it is an error.
1437 if (chosen_normal
> 0 && chosen_utf
< 0) {
1438 if (nvlist_add_uint64(ret
,
1439 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1440 (void) no_memory(hdl
);
1443 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1444 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1445 "'%s' must be set 'on' if normalization chosen"),
1446 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1447 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1458 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1460 uint64_t old_volsize
;
1461 uint64_t new_volsize
;
1462 uint64_t old_reservation
;
1463 uint64_t new_reservation
;
1464 zfs_prop_t resv_prop
;
1468 * If this is an existing volume, and someone is setting the volsize,
1469 * make sure that it matches the reservation, or add it if necessary.
1471 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1472 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1474 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1476 props
= fnvlist_alloc();
1477 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1478 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1480 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1481 old_reservation
) || nvlist_exists(nvl
,
1482 zfs_prop_to_name(resv_prop
))) {
1483 fnvlist_free(props
);
1486 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1487 &new_volsize
) != 0) {
1488 fnvlist_free(props
);
1491 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1492 fnvlist_free(props
);
1494 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1495 new_reservation
) != 0) {
1496 (void) no_memory(zhp
->zfs_hdl
);
1503 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1510 * For quotas and reservations, ENOSPC indicates
1511 * something different; setting a quota or reservation
1512 * doesn't use any disk space.
1515 case ZFS_PROP_QUOTA
:
1516 case ZFS_PROP_REFQUOTA
:
1517 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1518 "size is less than current used or "
1520 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1523 case ZFS_PROP_RESERVATION
:
1524 case ZFS_PROP_REFRESERVATION
:
1525 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1526 "size is greater than available space"));
1527 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1531 (void) zfs_standard_error(hdl
, err
, errbuf
);
1537 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1541 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1545 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1546 "property value too long"));
1547 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1551 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1552 "pool and or dataset must be upgraded to set this "
1553 "property or value"));
1554 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1558 if (prop
== ZFS_PROP_COMPRESSION
||
1559 prop
== ZFS_PROP_RECORDSIZE
) {
1560 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1561 "property setting is not allowed on "
1562 "bootable datasets"));
1563 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1564 } else if (prop
== ZFS_PROP_CHECKSUM
||
1565 prop
== ZFS_PROP_DEDUP
) {
1566 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1567 "property setting is not allowed on "
1569 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1571 (void) zfs_standard_error(hdl
, err
, errbuf
);
1576 if (prop
== ZPROP_INVAL
) {
1577 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1579 (void) zfs_standard_error(hdl
, err
, errbuf
);
1585 * This platform can't address a volume this big.
1588 if (prop
== ZFS_PROP_VOLSIZE
) {
1589 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1595 (void) zfs_standard_error(hdl
, err
, errbuf
);
1600 * Given a property name and value, set the property for the given dataset.
1603 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1607 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1608 nvlist_t
*nvl
= NULL
;
1610 (void) snprintf(errbuf
, sizeof (errbuf
),
1611 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1614 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1615 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1616 (void) no_memory(hdl
);
1620 ret
= zfs_prop_set_list(zhp
, nvl
);
1630 * Given an nvlist of property names and values, set the properties for the
1634 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1636 zfs_cmd_t zc
= { 0 };
1638 prop_changelist_t
**cls
= NULL
;
1641 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1646 (void) snprintf(errbuf
, sizeof (errbuf
),
1647 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1650 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1651 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1656 * We have to check for any extra properties which need to be added
1657 * before computing the length of the nvlist.
1659 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1661 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1662 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1663 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1668 * Check how many properties we're setting and allocate an array to
1669 * store changelist pointers for postfix().
1672 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1674 elem
= nvlist_next_nvpair(nvl
, elem
))
1676 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1680 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1682 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1684 zfs_prop_t prop
= zfs_name_to_prop(nvpair_name(elem
));
1686 assert(cl_idx
< nvl_len
);
1688 * We don't want to unmount & remount the dataset when changing
1689 * its canmount property to 'on' or 'noauto'. We only use
1690 * the changelist logic to unmount when setting canmount=off.
1692 if (!(prop
== ZFS_PROP_CANMOUNT
&&
1693 fnvpair_value_uint64(elem
) != ZFS_CANMOUNT_OFF
)) {
1694 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1695 if (cls
[cl_idx
] == NULL
)
1699 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1700 changelist_haszonedchild(cls
[cl_idx
])) {
1701 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1702 "child dataset with inherited mountpoint is used "
1703 "in a non-global zone"));
1704 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1708 if (cls
[cl_idx
] != NULL
&&
1709 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1714 assert(cl_idx
== nvl_len
);
1717 * Execute the corresponding ioctl() to set this list of properties.
1719 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1721 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1722 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1725 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1728 /* Get the list of unset properties back and report them. */
1729 nvlist_t
*errorprops
= NULL
;
1730 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1732 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1734 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1735 zfs_prop_t prop
= zfs_name_to_prop(nvpair_name(elem
));
1736 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1738 nvlist_free(errorprops
);
1740 if (added_resv
&& errno
== ENOSPC
) {
1741 /* clean up the volsize property we tried to set */
1742 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1746 zcmd_free_nvlists(&zc
);
1748 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1750 if (nvlist_add_uint64(nvl
,
1751 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1754 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1756 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1759 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1760 if (cls
[cl_idx
] != NULL
) {
1761 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1768 * Refresh the statistics so the new property value
1772 (void) get_stats(zhp
);
1777 zcmd_free_nvlists(&zc
);
1779 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1780 if (cls
[cl_idx
] != NULL
)
1781 changelist_free(cls
[cl_idx
]);
1789 * Given a property, inherit the value from the parent dataset, or if received
1790 * is TRUE, revert to the received value, if any.
1793 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1795 zfs_cmd_t zc
= { 0 };
1797 prop_changelist_t
*cl
;
1798 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1802 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1803 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1805 zc
.zc_cookie
= received
;
1806 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1808 * For user properties, the amount of work we have to do is very
1809 * small, so just do it here.
1811 if (!zfs_prop_user(propname
)) {
1812 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1813 "invalid property"));
1814 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1817 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1818 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1820 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1821 return (zfs_standard_error(hdl
, errno
, errbuf
));
1827 * Verify that this property is inheritable.
1829 if (zfs_prop_readonly(prop
))
1830 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1832 if (!zfs_prop_inheritable(prop
) && !received
)
1833 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1836 * Check to see if the value applies to this type
1838 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1839 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1842 * Normalize the name, to get rid of shorthand abbreviations.
1844 propname
= zfs_prop_to_name(prop
);
1845 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1846 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1848 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1849 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1850 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1851 "dataset is used in a non-global zone"));
1852 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1856 * Determine datasets which will be affected by this change, if any.
1858 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1861 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1862 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1863 "child dataset with inherited mountpoint is used "
1864 "in a non-global zone"));
1865 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1869 if ((ret
= changelist_prefix(cl
)) != 0)
1872 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1873 return (zfs_standard_error(hdl
, errno
, errbuf
));
1876 if ((ret
= changelist_postfix(cl
)) != 0)
1880 * Refresh the statistics so the new property is reflected.
1882 (void) get_stats(zhp
);
1886 changelist_free(cl
);
1891 * True DSL properties are stored in an nvlist. The following two functions
1892 * extract them appropriately.
1895 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1901 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1902 zfs_prop_to_name(prop
), &nv
) == 0) {
1903 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1904 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1906 verify(!zhp
->zfs_props_table
||
1907 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1908 value
= zfs_prop_default_numeric(prop
);
1916 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1922 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1923 zfs_prop_to_name(prop
), &nv
) == 0) {
1924 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
1925 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1927 verify(!zhp
->zfs_props_table
||
1928 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1929 value
= zfs_prop_default_string(prop
);
1937 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1939 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1943 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1945 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1946 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1950 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1952 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1957 * Internal function for getting a numeric property. Both zfs_prop_get() and
1958 * zfs_prop_get_int() are built using this interface.
1960 * Certain properties can be overridden using 'mount -o'. In this case, scan
1961 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1962 * If they differ from the on-disk values, report the current values and mark
1963 * the source "temporary".
1966 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1967 char **source
, uint64_t *val
)
1969 zfs_cmd_t zc
= { 0 };
1970 nvlist_t
*zplprops
= NULL
;
1972 char *mntopt_on
= NULL
;
1973 char *mntopt_off
= NULL
;
1974 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1979 case ZFS_PROP_ATIME
:
1980 mntopt_on
= MNTOPT_ATIME
;
1981 mntopt_off
= MNTOPT_NOATIME
;
1984 case ZFS_PROP_DEVICES
:
1985 mntopt_on
= MNTOPT_DEVICES
;
1986 mntopt_off
= MNTOPT_NODEVICES
;
1990 mntopt_on
= MNTOPT_EXEC
;
1991 mntopt_off
= MNTOPT_NOEXEC
;
1994 case ZFS_PROP_READONLY
:
1995 mntopt_on
= MNTOPT_RO
;
1996 mntopt_off
= MNTOPT_RW
;
1999 case ZFS_PROP_SETUID
:
2000 mntopt_on
= MNTOPT_SETUID
;
2001 mntopt_off
= MNTOPT_NOSETUID
;
2004 case ZFS_PROP_XATTR
:
2005 mntopt_on
= MNTOPT_XATTR
;
2006 mntopt_off
= MNTOPT_NOXATTR
;
2009 case ZFS_PROP_NBMAND
:
2010 mntopt_on
= MNTOPT_NBMAND
;
2011 mntopt_off
= MNTOPT_NONBMAND
;
2019 * Because looking up the mount options is potentially expensive
2020 * (iterating over all of /etc/mnttab), we defer its calculation until
2021 * we're looking up a property which requires its presence.
2023 if (!zhp
->zfs_mntcheck
&&
2024 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2025 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2026 struct mnttab entry
;
2028 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2029 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2031 if (zhp
->zfs_mntopts
== NULL
)
2035 zhp
->zfs_mntcheck
= B_TRUE
;
2038 if (zhp
->zfs_mntopts
== NULL
)
2039 mnt
.mnt_mntopts
= "";
2041 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2044 case ZFS_PROP_ATIME
:
2045 case ZFS_PROP_DEVICES
:
2047 case ZFS_PROP_READONLY
:
2048 case ZFS_PROP_SETUID
:
2049 case ZFS_PROP_XATTR
:
2050 case ZFS_PROP_NBMAND
:
2051 *val
= getprop_uint64(zhp
, prop
, source
);
2056 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2059 *src
= ZPROP_SRC_TEMPORARY
;
2060 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2063 *src
= ZPROP_SRC_TEMPORARY
;
2067 case ZFS_PROP_CANMOUNT
:
2068 case ZFS_PROP_VOLSIZE
:
2069 case ZFS_PROP_QUOTA
:
2070 case ZFS_PROP_REFQUOTA
:
2071 case ZFS_PROP_RESERVATION
:
2072 case ZFS_PROP_REFRESERVATION
:
2073 case ZFS_PROP_FILESYSTEM_LIMIT
:
2074 case ZFS_PROP_SNAPSHOT_LIMIT
:
2075 case ZFS_PROP_FILESYSTEM_COUNT
:
2076 case ZFS_PROP_SNAPSHOT_COUNT
:
2077 *val
= getprop_uint64(zhp
, prop
, source
);
2079 if (*source
== NULL
) {
2080 /* not default, must be local */
2081 *source
= zhp
->zfs_name
;
2085 case ZFS_PROP_MOUNTED
:
2086 *val
= (zhp
->zfs_mntopts
!= NULL
);
2089 case ZFS_PROP_NUMCLONES
:
2090 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2093 case ZFS_PROP_VERSION
:
2094 case ZFS_PROP_NORMALIZE
:
2095 case ZFS_PROP_UTF8ONLY
:
2097 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
2098 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2100 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2101 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2102 zcmd_free_nvlists(&zc
);
2105 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2106 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2108 zcmd_free_nvlists(&zc
);
2111 nvlist_free(zplprops
);
2112 zcmd_free_nvlists(&zc
);
2115 case ZFS_PROP_INCONSISTENT
:
2116 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2120 switch (zfs_prop_get_type(prop
)) {
2121 case PROP_TYPE_NUMBER
:
2122 case PROP_TYPE_INDEX
:
2123 *val
= getprop_uint64(zhp
, prop
, source
);
2125 * If we tried to use a default value for a
2126 * readonly property, it means that it was not
2129 if (zfs_prop_readonly(prop
) &&
2130 *source
!= NULL
&& (*source
)[0] == '\0') {
2136 case PROP_TYPE_STRING
:
2138 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2139 "cannot get non-numeric property"));
2140 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2141 dgettext(TEXT_DOMAIN
, "internal error")));
2149 * Calculate the source type, given the raw source string.
2152 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2153 char *statbuf
, size_t statlen
)
2155 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2158 if (source
== NULL
) {
2159 *srctype
= ZPROP_SRC_NONE
;
2160 } else if (source
[0] == '\0') {
2161 *srctype
= ZPROP_SRC_DEFAULT
;
2162 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2163 *srctype
= ZPROP_SRC_RECEIVED
;
2165 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2166 *srctype
= ZPROP_SRC_LOCAL
;
2168 (void) strlcpy(statbuf
, source
, statlen
);
2169 *srctype
= ZPROP_SRC_INHERITED
;
2176 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2177 size_t proplen
, boolean_t literal
)
2182 if (zhp
->zfs_recvd_props
== NULL
)
2183 if (get_recvd_props_ioctl(zhp
) != 0)
2186 prop
= zfs_name_to_prop(propname
);
2188 if (prop
!= ZPROP_INVAL
) {
2190 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2192 zfs_set_recvd_props_mode(zhp
, &cookie
);
2193 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2194 NULL
, NULL
, 0, literal
);
2195 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2199 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2200 propname
, &propval
) != 0)
2202 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2204 (void) strlcpy(propbuf
, recvdval
, proplen
);
2207 return (err
== 0 ? 0 : -1);
2211 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2216 value
= zfs_get_clones_nvl(zhp
);
2221 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2222 pair
= nvlist_next_nvpair(value
, pair
)) {
2223 if (propbuf
[0] != '\0')
2224 (void) strlcat(propbuf
, ",", proplen
);
2225 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2231 struct get_clones_arg
{
2235 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2239 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2241 struct get_clones_arg
*gca
= arg
;
2243 if (gca
->numclones
== 0) {
2248 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2249 NULL
, NULL
, 0, B_TRUE
) != 0)
2251 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2252 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2257 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2263 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2265 nvlist_t
*nv
, *value
;
2267 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2268 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2269 struct get_clones_arg gca
;
2272 * if this is a snapshot, then the kernel wasn't able
2273 * to get the clones. Do it by slowly iterating.
2275 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2277 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2279 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2284 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2286 gca
.origin
= zhp
->zfs_name
;
2288 if (gca
.numclones
!= 0) {
2290 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2293 /* get the pool name */
2294 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2295 (void) strsep(&cp
, "/@");
2296 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2297 ZFS_TYPE_FILESYSTEM
);
2299 (void) get_clones_cb(root
, &gca
);
2302 if (gca
.numclones
!= 0 ||
2303 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2304 nvlist_add_nvlist(zhp
->zfs_props
,
2305 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2312 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2313 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2316 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2322 * Retrieve a property from the given object. If 'literal' is specified, then
2323 * numbers are left as exact values. Otherwise, numbers are converted to a
2324 * human-readable form.
2326 * Returns 0 on success, or -1 on error.
2329 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2330 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2332 char *source
= NULL
;
2336 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2339 * Check to see if this property applies to our object
2341 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
2344 if (received
&& zfs_prop_readonly(prop
))
2348 *src
= ZPROP_SRC_NONE
;
2351 case ZFS_PROP_CREATION
:
2353 * 'creation' is a time_t stored in the statistics. We convert
2354 * this into a string unless 'literal' is specified.
2357 val
= getprop_uint64(zhp
, prop
, &source
);
2358 time_t time
= (time_t)val
;
2362 localtime_r(&time
, &t
) == NULL
||
2363 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2365 (void) snprintf(propbuf
, proplen
, "%llu", val
);
2369 case ZFS_PROP_MOUNTPOINT
:
2371 * Getting the precise mountpoint can be tricky.
2373 * - for 'none' or 'legacy', return those values.
2374 * - for inherited mountpoints, we want to take everything
2375 * after our ancestor and append it to the inherited value.
2377 * If the pool has an alternate root, we want to prepend that
2378 * root to any values we return.
2381 str
= getprop_string(zhp
, prop
, &source
);
2383 if (str
[0] == '/') {
2384 char buf
[MAXPATHLEN
];
2386 const char *relpath
;
2389 * If we inherit the mountpoint, even from a dataset
2390 * with a received value, the source will be the path of
2391 * the dataset we inherit from. If source is
2392 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2395 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2398 relpath
= zhp
->zfs_name
+ strlen(source
);
2399 if (relpath
[0] == '/')
2403 if ((zpool_get_prop(zhp
->zpool_hdl
,
2404 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2405 B_FALSE
)) || (strcmp(root
, "-") == 0))
2408 * Special case an alternate root of '/'. This will
2409 * avoid having multiple leading slashes in the
2412 if (strcmp(root
, "/") == 0)
2416 * If the mountpoint is '/' then skip over this
2417 * if we are obtaining either an alternate root or
2418 * an inherited mountpoint.
2420 if (str
[1] == '\0' && (root
[0] != '\0' ||
2421 relpath
[0] != '\0'))
2424 if (relpath
[0] == '\0')
2425 (void) snprintf(propbuf
, proplen
, "%s%s",
2428 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2429 root
, str
, relpath
[0] == '@' ? "" : "/",
2432 /* 'legacy' or 'none' */
2433 (void) strlcpy(propbuf
, str
, proplen
);
2438 case ZFS_PROP_ORIGIN
:
2439 str
= getprop_string(zhp
, prop
, &source
);
2442 (void) strlcpy(propbuf
, str
, proplen
);
2445 case ZFS_PROP_CLONES
:
2446 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2450 case ZFS_PROP_QUOTA
:
2451 case ZFS_PROP_REFQUOTA
:
2452 case ZFS_PROP_RESERVATION
:
2453 case ZFS_PROP_REFRESERVATION
:
2455 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2459 * If quota or reservation is 0, we translate this into 'none'
2460 * (unless literal is set), and indicate that it's the default
2461 * value. Otherwise, we print the number nicely and indicate
2462 * that its set locally.
2466 (void) strlcpy(propbuf
, "0", proplen
);
2468 (void) strlcpy(propbuf
, "none", proplen
);
2471 (void) snprintf(propbuf
, proplen
, "%llu",
2474 zfs_nicenum(val
, propbuf
, proplen
);
2478 case ZFS_PROP_FILESYSTEM_LIMIT
:
2479 case ZFS_PROP_SNAPSHOT_LIMIT
:
2480 case ZFS_PROP_FILESYSTEM_COUNT
:
2481 case ZFS_PROP_SNAPSHOT_COUNT
:
2483 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2487 * If limit is UINT64_MAX, we translate this into 'none' (unless
2488 * literal is set), and indicate that it's the default value.
2489 * Otherwise, we print the number nicely and indicate that it's
2493 (void) snprintf(propbuf
, proplen
, "%llu",
2495 } else if (val
== UINT64_MAX
) {
2496 (void) strlcpy(propbuf
, "none", proplen
);
2498 zfs_nicenum(val
, propbuf
, proplen
);
2502 case ZFS_PROP_REFRATIO
:
2503 case ZFS_PROP_COMPRESSRATIO
:
2504 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2506 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2507 (u_longlong_t
)(val
/ 100),
2508 (u_longlong_t
)(val
% 100));
2512 switch (zhp
->zfs_type
) {
2513 case ZFS_TYPE_FILESYSTEM
:
2516 case ZFS_TYPE_VOLUME
:
2519 case ZFS_TYPE_SNAPSHOT
:
2522 case ZFS_TYPE_BOOKMARK
:
2528 (void) snprintf(propbuf
, proplen
, "%s", str
);
2531 case ZFS_PROP_MOUNTED
:
2533 * The 'mounted' property is a pseudo-property that described
2534 * whether the filesystem is currently mounted. Even though
2535 * it's a boolean value, the typical values of "on" and "off"
2536 * don't make sense, so we translate to "yes" and "no".
2538 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2539 src
, &source
, &val
) != 0)
2542 (void) strlcpy(propbuf
, "yes", proplen
);
2544 (void) strlcpy(propbuf
, "no", proplen
);
2549 * The 'name' property is a pseudo-property derived from the
2550 * dataset name. It is presented as a real property to simplify
2553 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2556 case ZFS_PROP_MLSLABEL
:
2558 m_label_t
*new_sl
= NULL
;
2559 char *ascii
= NULL
; /* human readable label */
2561 (void) strlcpy(propbuf
,
2562 getprop_string(zhp
, prop
, &source
), proplen
);
2564 if (literal
|| (strcasecmp(propbuf
,
2565 ZFS_MLSLABEL_DEFAULT
) == 0))
2569 * Try to translate the internal hex string to
2570 * human-readable output. If there are any
2571 * problems just use the hex string.
2574 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2575 L_NO_CORRECTION
, NULL
) == -1) {
2576 m_label_free(new_sl
);
2580 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2584 m_label_free(new_sl
);
2587 m_label_free(new_sl
);
2589 (void) strlcpy(propbuf
, ascii
, proplen
);
2596 * GUIDs are stored as numbers, but they are identifiers.
2597 * We don't want them to be pretty printed, because pretty
2598 * printing mangles the ID into a truncated and useless value.
2600 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2602 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2606 switch (zfs_prop_get_type(prop
)) {
2607 case PROP_TYPE_NUMBER
:
2608 if (get_numeric_property(zhp
, prop
, src
,
2609 &source
, &val
) != 0)
2612 (void) snprintf(propbuf
, proplen
, "%llu",
2615 zfs_nicenum(val
, propbuf
, proplen
);
2618 case PROP_TYPE_STRING
:
2619 str
= getprop_string(zhp
, prop
, &source
);
2622 (void) strlcpy(propbuf
, str
, proplen
);
2625 case PROP_TYPE_INDEX
:
2626 if (get_numeric_property(zhp
, prop
, src
,
2627 &source
, &val
) != 0)
2629 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2631 (void) strlcpy(propbuf
, strval
, proplen
);
2639 get_source(zhp
, src
, source
, statbuf
, statlen
);
2645 * Utility function to get the given numeric property. Does no validation that
2646 * the given property is the appropriate type; should only be used with
2647 * hard-coded property types.
2650 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2655 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2661 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2665 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2666 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2670 * Similar to zfs_prop_get(), but returns the value as an integer.
2673 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2674 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2679 * Check to see if this property applies to our object
2681 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2682 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2683 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2684 zfs_prop_to_name(prop
)));
2688 *src
= ZPROP_SRC_NONE
;
2690 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2693 get_source(zhp
, src
, source
, statbuf
, statlen
);
2699 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2700 char **domainp
, idmap_rid_t
*ridp
)
2702 idmap_get_handle_t
*get_hdl
= NULL
;
2706 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2710 err
= idmap_get_sidbyuid(get_hdl
, id
,
2711 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2713 err
= idmap_get_sidbygid(get_hdl
, id
,
2714 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2716 if (err
== IDMAP_SUCCESS
&&
2717 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2718 status
== IDMAP_SUCCESS
)
2724 idmap_get_destroy(get_hdl
);
2729 * convert the propname into parameters needed by kernel
2730 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2731 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2734 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2735 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2737 zfs_userquota_prop_t type
;
2739 char *numericsid
= NULL
;
2744 /* Figure out the property type ({user|group}{quota|space}) */
2745 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2746 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2747 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2750 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2754 isuser
= (type
== ZFS_PROP_USERQUOTA
||
2755 type
== ZFS_PROP_USERUSED
);
2757 cp
= strchr(propname
, '@') + 1;
2759 if (strchr(cp
, '@')) {
2761 * It's a SID name (eg "user@domain") that needs to be
2762 * turned into S-1-domainID-RID.
2765 idmap_stat stat
, map_stat
;
2768 idmap_get_handle_t
*gh
= NULL
;
2770 stat
= idmap_get_create(&gh
);
2771 if (stat
!= IDMAP_SUCCESS
) {
2772 idmap_get_destroy(gh
);
2775 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2778 stat
= idmap_getuidbywinname(cp
, NULL
, flag
, &pid
);
2781 stat
= idmap_get_sidbyuid(gh
, pid
, flag
, &numericsid
,
2784 stat
= idmap_getgidbywinname(cp
, NULL
, flag
, &pid
);
2787 stat
= idmap_get_sidbygid(gh
, pid
, flag
, &numericsid
,
2791 idmap_get_destroy(gh
);
2794 stat
= idmap_get_mappings(gh
);
2795 idmap_get_destroy(gh
);
2800 if (numericsid
== NULL
)
2804 /* will be further decoded below */
2807 if (strncmp(cp
, "S-1-", 4) == 0) {
2808 /* It's a numeric SID (eg "S-1-234-567-89") */
2809 (void) strlcpy(domain
, cp
, domainlen
);
2812 cp
= strrchr(domain
, '-');
2815 *ridp
= strtoull(cp
, &end
, 10);
2823 if (errno
!= 0 || *end
!= '\0')
2825 } else if (!isdigit(*cp
)) {
2827 * It's a user/group name (eg "user") that needs to be
2828 * turned into a uid/gid
2830 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2846 /* It's a user/group ID (eg "12345"). */
2847 uid_t id
= strtoul(cp
, &end
, 10);
2854 /* It's an ephemeral ID. */
2855 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2856 &mapdomain
, &rid
) != 0)
2858 (void) strlcpy(domain
, mapdomain
, domainlen
);
2865 ASSERT3P(numericsid
, ==, NULL
);
2870 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2871 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2874 zfs_cmd_t zc
= { 0 };
2876 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2878 err
= userquota_propname_decode(propname
,
2879 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2880 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2881 zc
.zc_objset_type
= *typep
;
2885 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2889 *propvalue
= zc
.zc_cookie
;
2894 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2895 uint64_t *propvalue
)
2897 zfs_userquota_prop_t type
;
2899 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2904 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2905 char *propbuf
, int proplen
, boolean_t literal
)
2909 zfs_userquota_prop_t type
;
2911 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2918 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
2919 } else if (propvalue
== 0 &&
2920 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2921 (void) strlcpy(propbuf
, "none", proplen
);
2923 zfs_nicenum(propvalue
, propbuf
, proplen
);
2929 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2930 uint64_t *propvalue
)
2933 zfs_cmd_t zc
= { 0 };
2934 const char *snapname
;
2936 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2938 snapname
= strchr(propname
, '@') + 1;
2939 if (strchr(snapname
, '@')) {
2940 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2942 /* snapname is the short name, append it to zhp's fsname */
2945 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2946 sizeof (zc
.zc_value
));
2947 cp
= strchr(zc
.zc_value
, '@');
2950 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2951 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2954 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2958 *propvalue
= zc
.zc_cookie
;
2963 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2964 char *propbuf
, int proplen
, boolean_t literal
)
2969 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2975 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
2977 zfs_nicenum(propvalue
, propbuf
, proplen
);
2983 * Returns the name of the given zfs handle.
2986 zfs_get_name(const zfs_handle_t
*zhp
)
2988 return (zhp
->zfs_name
);
2992 * Returns the name of the parent pool for the given zfs handle.
2995 zfs_get_pool_name(const zfs_handle_t
*zhp
)
2997 return (zhp
->zpool_hdl
->zpool_name
);
3001 * Returns the type of the given zfs handle.
3004 zfs_get_type(const zfs_handle_t
*zhp
)
3006 return (zhp
->zfs_type
);
3010 * Is one dataset name a child dataset of another?
3012 * Needs to handle these cases:
3013 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3014 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3015 * Descendant? No. No. No. Yes.
3018 is_descendant(const char *ds1
, const char *ds2
)
3020 size_t d1len
= strlen(ds1
);
3022 /* ds2 can't be a descendant if it's smaller */
3023 if (strlen(ds2
) < d1len
)
3026 /* otherwise, compare strings and verify that there's a '/' char */
3027 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3031 * Given a complete name, return just the portion that refers to the parent.
3032 * Will return -1 if there is no parent (path is just the name of the
3036 parent_name(const char *path
, char *buf
, size_t buflen
)
3040 (void) strlcpy(buf
, path
, buflen
);
3042 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3050 * If accept_ancestor is false, then check to make sure that the given path has
3051 * a parent, and that it exists. If accept_ancestor is true, then find the
3052 * closest existing ancestor for the given path. In prefixlen return the
3053 * length of already existing prefix of the given path. We also fetch the
3054 * 'zoned' property, which is used to validate property settings when creating
3058 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3059 boolean_t accept_ancestor
, int *prefixlen
)
3061 zfs_cmd_t zc
= { 0 };
3062 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3068 (void) snprintf(errbuf
, sizeof (errbuf
),
3069 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3071 /* get parent, and check to see if this is just a pool */
3072 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3073 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3074 "missing dataset name"));
3075 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3078 /* check to see if the pool exists */
3079 if ((slash
= strchr(parent
, '/')) == NULL
)
3080 slash
= parent
+ strlen(parent
);
3081 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3082 zc
.zc_name
[slash
- parent
] = '\0';
3083 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3086 "no such pool '%s'"), zc
.zc_name
);
3087 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3090 /* check to see if the parent dataset exists */
3091 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3092 if (errno
== ENOENT
&& accept_ancestor
) {
3094 * Go deeper to find an ancestor, give up on top level.
3096 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3097 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3098 "no such pool '%s'"), zc
.zc_name
);
3099 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3101 } else if (errno
== ENOENT
) {
3102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3103 "parent does not exist"));
3104 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3106 return (zfs_standard_error(hdl
, errno
, errbuf
));
3109 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3113 /* we are in a non-global zone, but parent is in the global zone */
3114 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3115 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3120 /* make sure parent is a filesystem */
3121 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3123 "parent is not a filesystem"));
3124 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3130 if (prefixlen
!= NULL
)
3131 *prefixlen
= strlen(parent
);
3136 * Finds whether the dataset of the given type(s) exists.
3139 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3143 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3147 * Try to get stats for the dataset, which will tell us if it exists.
3149 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3150 int ds_type
= zhp
->zfs_type
;
3153 if (types
& ds_type
)
3160 * Given a path to 'target', create all the ancestors between
3161 * the prefixlen portion of the path, and the target itself.
3162 * Fail if the initial prefixlen-ancestor does not already exist.
3165 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3171 /* make sure prefix exists */
3172 cp
= target
+ prefixlen
;
3174 assert(strchr(cp
, '/') == NULL
);
3175 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3178 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3186 * Attempt to create, mount, and share any ancestor filesystems,
3187 * up to the prefixlen-long one.
3189 for (cp
= target
+ prefixlen
+ 1;
3190 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3194 h
= make_dataset_handle(hdl
, target
);
3196 /* it already exists, nothing to do here */
3201 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3203 opname
= dgettext(TEXT_DOMAIN
, "create");
3207 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3209 opname
= dgettext(TEXT_DOMAIN
, "open");
3213 if (zfs_mount(h
, NULL
, 0) != 0) {
3214 opname
= dgettext(TEXT_DOMAIN
, "mount");
3218 if (zfs_share(h
) != 0) {
3219 opname
= dgettext(TEXT_DOMAIN
, "share");
3229 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3230 "failed to %s ancestor '%s'"), opname
, target
);
3235 * Creates non-existing ancestors of the given path.
3238 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3244 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3247 if ((path_copy
= strdup(path
)) != NULL
) {
3248 rc
= create_parents(hdl
, path_copy
, prefix
);
3251 if (path_copy
== NULL
|| rc
!= 0)
3258 * Create a new filesystem or volume.
3261 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3266 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3269 enum lzc_dataset_type ost
;
3271 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3272 "cannot create '%s'"), path
);
3274 /* validate the path, taking care to note the extended error message */
3275 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3276 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3278 /* validate parents exist */
3279 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3283 * The failure modes when creating a dataset of a different type over
3284 * one that already exists is a little strange. In particular, if you
3285 * try to create a dataset on top of an existing dataset, the ioctl()
3286 * will return ENOENT, not EEXIST. To prevent this from happening, we
3287 * first try to see if the dataset exists.
3289 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3290 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3291 "dataset already exists"));
3292 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3295 if (type
== ZFS_TYPE_VOLUME
)
3296 ost
= LZC_DATSET_TYPE_ZVOL
;
3298 ost
= LZC_DATSET_TYPE_ZFS
;
3300 /* open zpool handle for prop validation */
3301 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3302 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3304 /* truncate pool_path at first slash */
3305 char *p
= strchr(pool_path
, '/');
3309 zpool_handle_t
*zpool_handle
= zpool_open(hdl
, pool_path
);
3311 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3312 zoned
, NULL
, zpool_handle
, errbuf
)) == 0) {
3313 zpool_close(zpool_handle
);
3316 zpool_close(zpool_handle
);
3318 if (type
== ZFS_TYPE_VOLUME
) {
3320 * If we are creating a volume, the size and block size must
3321 * satisfy a few restraints. First, the blocksize must be a
3322 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3323 * volsize must be a multiple of the block size, and cannot be
3326 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3327 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3329 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3330 "missing volume size"));
3331 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3334 if ((ret
= nvlist_lookup_uint64(props
,
3335 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3336 &blocksize
)) != 0) {
3337 if (ret
== ENOENT
) {
3338 blocksize
= zfs_prop_default_numeric(
3339 ZFS_PROP_VOLBLOCKSIZE
);
3342 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3343 "missing volume block size"));
3344 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3350 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3351 "volume size cannot be zero"));
3352 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3355 if (size
% blocksize
!= 0) {
3357 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3358 "volume size must be a multiple of volume block "
3360 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3364 /* create the dataset */
3365 ret
= lzc_create(path
, ost
, props
);
3368 /* check for failure */
3370 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3371 (void) parent_name(path
, parent
, sizeof (parent
));
3375 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3376 "no such parent '%s'"), parent
);
3377 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3380 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3381 "parent '%s' is not a filesystem"), parent
);
3382 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3385 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3386 "pool must be upgraded to set this "
3387 "property or value"));
3388 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3392 * This platform can't address a volume this big.
3394 if (type
== ZFS_TYPE_VOLUME
)
3395 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3400 return (zfs_standard_error(hdl
, errno
, errbuf
));
3408 * Destroys the given dataset. The caller must make sure that the filesystem
3409 * isn't mounted, and that there are no active dependents. If the file system
3410 * does not exist this function does nothing.
3413 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3415 zfs_cmd_t zc
= { 0 };
3417 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3418 nvlist_t
*nv
= fnvlist_alloc();
3419 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3420 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3423 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3424 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3430 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3432 if (ZFS_IS_VOLUME(zhp
)) {
3433 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3435 zc
.zc_objset_type
= DMU_OST_ZFS
;
3438 zc
.zc_defer_destroy
= defer
;
3439 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3441 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3442 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3446 remove_mountpoint(zhp
);
3451 struct destroydata
{
3453 const char *snapname
;
3457 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3459 struct destroydata
*dd
= arg
;
3460 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3463 (void) snprintf(name
, sizeof (name
),
3464 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3466 if (lzc_exists(name
))
3467 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3469 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3475 * Destroys all snapshots with the given name in zhp & descendants.
3478 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3481 struct destroydata dd
= { 0 };
3483 dd
.snapname
= snapname
;
3484 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3485 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3487 if (nvlist_empty(dd
.nvl
)) {
3488 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3489 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3490 zhp
->zfs_name
, snapname
);
3492 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3494 nvlist_free(dd
.nvl
);
3499 * Destroys all the snapshots named in the nvlist.
3502 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3505 nvlist_t
*errlist
= NULL
;
3507 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3510 nvlist_free(errlist
);
3514 if (nvlist_empty(errlist
)) {
3516 (void) snprintf(errbuf
, sizeof (errbuf
),
3517 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3519 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3521 for (nvpair_t
*pair
= nvlist_next_nvpair(errlist
, NULL
);
3522 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3524 (void) snprintf(errbuf
, sizeof (errbuf
),
3525 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3528 switch (fnvpair_value_int32(pair
)) {
3531 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3532 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3535 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3540 nvlist_free(errlist
);
3545 * Clones the given dataset. The target must be of the same type as the source.
3548 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3550 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3553 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3556 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3558 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3559 "cannot create '%s'"), target
);
3561 /* validate the target/clone name */
3562 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3563 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3565 /* validate parents exist */
3566 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3569 (void) parent_name(target
, parent
, sizeof (parent
));
3575 if (ZFS_IS_VOLUME(zhp
)) {
3576 type
= ZFS_TYPE_VOLUME
;
3578 type
= ZFS_TYPE_FILESYSTEM
;
3580 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3581 zhp
, zhp
->zpool_hdl
, errbuf
)) == NULL
)
3585 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3593 * The parent doesn't exist. We should have caught this
3594 * above, but there may a race condition that has since
3595 * destroyed the parent.
3597 * At this point, we don't know whether it's the source
3598 * that doesn't exist anymore, or whether the target
3599 * dataset doesn't exist.
3601 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3602 "no such parent '%s'"), parent
);
3603 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3606 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3607 "source and target pools differ"));
3608 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3612 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3621 * Promotes the given clone fs to be the clone parent.
3624 zfs_promote(zfs_handle_t
*zhp
)
3626 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3627 zfs_cmd_t zc
= { 0 };
3628 char parent
[MAXPATHLEN
];
3632 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3633 "cannot promote '%s'"), zhp
->zfs_name
);
3635 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3636 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3637 "snapshots can not be promoted"));
3638 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3641 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3642 if (parent
[0] == '\0') {
3643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3644 "not a cloned filesystem"));
3645 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3648 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3649 sizeof (zc
.zc_value
));
3650 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3651 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3654 int save_errno
= errno
;
3656 switch (save_errno
) {
3658 /* There is a conflicting snapshot name. */
3659 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3660 "conflicting snapshot '%s' from parent '%s'"),
3661 zc
.zc_string
, parent
);
3662 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3665 return (zfs_standard_error(hdl
, save_errno
, 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
;