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) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017 RackTop Systems.
46 #include <sys/mntent.h>
47 #include <sys/mount.h>
55 #include <directory.h>
56 #endif /* HAVE_IDMAP */
58 #include <sys/dnode.h>
61 #include <sys/dsl_crypt.h>
64 #include "zfs_namecheck.h"
66 #include "libzfs_impl.h"
68 #include "zfs_deleg.h"
70 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
71 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
74 * Given a single type (not a mask of types), return the type in a human
78 zfs_type_to_name(zfs_type_t type
)
81 case ZFS_TYPE_FILESYSTEM
:
82 return (dgettext(TEXT_DOMAIN
, "filesystem"));
83 case ZFS_TYPE_SNAPSHOT
:
84 return (dgettext(TEXT_DOMAIN
, "snapshot"));
86 return (dgettext(TEXT_DOMAIN
, "volume"));
88 return (dgettext(TEXT_DOMAIN
, "pool"));
89 case ZFS_TYPE_BOOKMARK
:
90 return (dgettext(TEXT_DOMAIN
, "bookmark"));
92 assert(!"unhandled zfs_type_t");
99 * Validate a ZFS path. This is used even before trying to open the dataset, to
100 * provide a more meaningful error message. We call zfs_error_aux() to
101 * explain exactly why the name was not valid.
104 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
110 if (entity_namecheck(path
, &why
, &what
) != 0) {
113 case NAME_ERR_TOOLONG
:
114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
115 "name is too long"));
118 case NAME_ERR_LEADING_SLASH
:
119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
120 "leading slash in name"));
123 case NAME_ERR_EMPTY_COMPONENT
:
124 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
125 "empty component in name"));
128 case NAME_ERR_TRAILING_SLASH
:
129 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
130 "trailing slash in name"));
133 case NAME_ERR_INVALCHAR
:
135 dgettext(TEXT_DOMAIN
, "invalid character "
136 "'%c' in name"), what
);
139 case NAME_ERR_MULTIPLE_DELIMITERS
:
140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
141 "multiple '@' and/or '#' delimiters in "
145 case NAME_ERR_NOLETTER
:
146 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
147 "pool doesn't begin with a letter"));
150 case NAME_ERR_RESERVED
:
151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
152 "name is reserved"));
155 case NAME_ERR_DISKLIKE
:
156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
157 "reserved disk name"));
161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
162 "(%d) not defined"), why
);
170 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
172 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
173 "snapshot delimiter '@' is not expected here"));
177 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
179 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
180 "missing '@' delimiter in snapshot name"));
184 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
187 "bookmark delimiter '#' is not expected here"));
191 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
193 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
194 "missing '#' delimiter in bookmark name"));
198 if (modifying
&& strchr(path
, '%') != NULL
) {
200 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
201 "invalid character %c in name"), '%');
209 zfs_name_valid(const char *name
, zfs_type_t type
)
211 if (type
== ZFS_TYPE_POOL
)
212 return (zpool_name_valid(NULL
, B_FALSE
, name
));
213 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
217 * This function takes the raw DSL properties, and filters out the user-defined
218 * properties into a separate nvlist.
221 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
223 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
228 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
229 (void) no_memory(hdl
);
234 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
235 if (!zfs_prop_user(nvpair_name(elem
)))
238 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
239 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
241 (void) no_memory(hdl
);
249 static zpool_handle_t
*
250 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
252 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
255 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
256 if (hdl
->libzfs_pool_handles
!= NULL
)
257 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
258 hdl
->libzfs_pool_handles
= zph
;
263 static zpool_handle_t
*
264 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
266 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
267 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
269 while ((zph
!= NULL
) &&
270 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
271 zph
= zph
->zpool_next
;
276 * Returns a handle to the pool that contains the provided dataset.
277 * If a handle to that pool already exists then that handle is returned.
278 * Otherwise, a new handle is created and added to the list of handles.
280 static zpool_handle_t
*
281 zpool_handle(zfs_handle_t
*zhp
)
287 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
288 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
289 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
291 zph
= zpool_find_handle(zhp
, pool_name
, len
);
293 zph
= zpool_add_handle(zhp
, pool_name
);
300 zpool_free_handles(libzfs_handle_t
*hdl
)
302 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
304 while (zph
!= NULL
) {
305 next
= zph
->zpool_next
;
309 hdl
->libzfs_pool_handles
= NULL
;
313 * Utility function to gather stats (objset and zpl) for the given object.
316 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
318 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
320 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
322 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
323 if (errno
== ENOMEM
) {
324 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
335 * Utility function to get the received properties of the given object.
338 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
340 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
341 nvlist_t
*recvdprops
;
342 zfs_cmd_t zc
= {"\0"};
345 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
348 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
350 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
351 if (errno
== ENOMEM
) {
352 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
356 zcmd_free_nvlists(&zc
);
361 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
362 zcmd_free_nvlists(&zc
);
366 nvlist_free(zhp
->zfs_recvd_props
);
367 zhp
->zfs_recvd_props
= recvdprops
;
373 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
375 nvlist_t
*allprops
, *userprops
;
377 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
379 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
384 * XXX Why do we store the user props separately, in addition to
385 * storing them in zfs_props?
387 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
388 nvlist_free(allprops
);
392 nvlist_free(zhp
->zfs_props
);
393 nvlist_free(zhp
->zfs_user_props
);
395 zhp
->zfs_props
= allprops
;
396 zhp
->zfs_user_props
= userprops
;
402 get_stats(zfs_handle_t
*zhp
)
405 zfs_cmd_t zc
= {"\0"};
407 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
409 if (get_stats_ioctl(zhp
, &zc
) != 0)
411 else if (put_stats_zhdl(zhp
, &zc
) != 0)
413 zcmd_free_nvlists(&zc
);
418 * Refresh the properties currently stored in the handle.
421 zfs_refresh_properties(zfs_handle_t
*zhp
)
423 (void) get_stats(zhp
);
427 * Makes a handle from the given dataset name. Used by zfs_open() and
428 * zfs_iter_* to create child handles on the fly.
431 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
433 if (put_stats_zhdl(zhp
, zc
) != 0)
437 * We've managed to open the dataset and gather statistics. Determine
438 * the high-level type.
440 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
441 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
442 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
443 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
444 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
449 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
450 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
451 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
452 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
453 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
454 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
456 abort(); /* we should never see any other types */
458 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
465 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
467 zfs_cmd_t zc
= {"\0"};
469 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
475 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
476 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
480 if (get_stats_ioctl(zhp
, &zc
) == -1) {
481 zcmd_free_nvlists(&zc
);
485 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
489 zcmd_free_nvlists(&zc
);
494 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
496 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
502 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
503 if (make_dataset_handle_common(zhp
, zc
) == -1) {
511 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
513 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
518 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
519 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
520 zhp
->zfs_head_type
= pzhp
->zfs_type
;
521 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
522 zhp
->zpool_hdl
= zpool_handle(zhp
);
528 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
530 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
535 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
536 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
537 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
538 sizeof (zhp
->zfs_name
));
539 zhp
->zfs_type
= zhp_orig
->zfs_type
;
540 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
541 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
542 if (zhp_orig
->zfs_props
!= NULL
) {
543 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
544 (void) no_memory(zhp
->zfs_hdl
);
549 if (zhp_orig
->zfs_user_props
!= NULL
) {
550 if (nvlist_dup(zhp_orig
->zfs_user_props
,
551 &zhp
->zfs_user_props
, 0) != 0) {
552 (void) no_memory(zhp
->zfs_hdl
);
557 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
558 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
559 &zhp
->zfs_recvd_props
, 0)) {
560 (void) no_memory(zhp
->zfs_hdl
);
565 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
566 if (zhp_orig
->zfs_mntopts
!= NULL
) {
567 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
568 zhp_orig
->zfs_mntopts
);
570 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
575 zfs_bookmark_exists(const char *path
)
579 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
586 (void) strlcpy(fsname
, path
, sizeof (fsname
));
587 pound
= strchr(fsname
, '#');
592 bmark_name
= pound
+ 1;
593 props
= fnvlist_alloc();
594 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
601 rv
= nvlist_exists(bmarks
, bmark_name
);
607 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
608 nvlist_t
*bmark_props
)
610 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
615 /* Fill in the name. */
616 zhp
->zfs_hdl
= parent
->zfs_hdl
;
617 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
619 /* Set the property lists. */
620 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
626 zhp
->zfs_head_type
= parent
->zfs_head_type
;
627 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
629 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
630 nvlist_free(zhp
->zfs_props
);
638 struct zfs_open_bookmarks_cb_data
{
644 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
646 struct zfs_open_bookmarks_cb_data
*dp
= data
;
649 * Is it the one we are looking for?
651 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
653 * We found it. Save it and let the caller know we are done.
660 * Not found. Close the handle and ask for another one.
667 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
668 * argument is a mask of acceptable types. The function will print an
669 * appropriate error message and return NULL if it can't be opened.
672 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
678 (void) snprintf(errbuf
, sizeof (errbuf
),
679 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
682 * Validate the name before we even try to open it.
684 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
685 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
690 * Bookmarks needs to be handled separately.
692 bookp
= strchr(path
, '#');
695 * Try to get stats for the dataset, which will tell us if it
699 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
700 (void) zfs_standard_error(hdl
, errno
, errbuf
);
704 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
706 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
709 * We need to cut out '#' and everything after '#'
710 * to get the parent dataset name only.
712 assert(bookp
- path
< sizeof (dsname
));
713 (void) strncpy(dsname
, path
, bookp
- path
);
714 dsname
[bookp
- path
] = '\0';
717 * Create handle for the parent dataset.
720 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
721 (void) zfs_standard_error(hdl
, errno
, errbuf
);
726 * Iterate bookmarks to find the right one.
729 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
730 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
731 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
735 if (cb_data
.zhp
== NULL
) {
736 (void) zfs_standard_error(hdl
, errno
, errbuf
);
748 if (!(types
& zhp
->zfs_type
)) {
749 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
758 * Release a ZFS handle. Nothing to do but free the associated memory.
761 zfs_close(zfs_handle_t
*zhp
)
763 if (zhp
->zfs_mntopts
)
764 free(zhp
->zfs_mntopts
);
765 nvlist_free(zhp
->zfs_props
);
766 nvlist_free(zhp
->zfs_user_props
);
767 nvlist_free(zhp
->zfs_recvd_props
);
771 typedef struct mnttab_node
{
772 struct mnttab mtn_mt
;
777 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
779 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
780 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
783 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
785 return (AVL_ISIGN(rv
));
789 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
791 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
792 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
793 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
797 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
801 /* Reopen MNTTAB to prevent reading stale data from open file */
802 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
805 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
809 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
812 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
813 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
814 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
815 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
816 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
818 /* Exclude duplicate mounts */
819 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
820 free(mtn
->mtn_mt
.mnt_special
);
821 free(mtn
->mtn_mt
.mnt_mountp
);
822 free(mtn
->mtn_mt
.mnt_fstype
);
823 free(mtn
->mtn_mt
.mnt_mntopts
);
828 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
835 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
840 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
842 free(mtn
->mtn_mt
.mnt_special
);
843 free(mtn
->mtn_mt
.mnt_mountp
);
844 free(mtn
->mtn_mt
.mnt_fstype
);
845 free(mtn
->mtn_mt
.mnt_mntopts
);
848 avl_destroy(&hdl
->libzfs_mnttab_cache
);
852 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
854 hdl
->libzfs_mnttab_enable
= enable
;
858 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
859 struct mnttab
*entry
)
865 if (!hdl
->libzfs_mnttab_enable
) {
866 struct mnttab srch
= { 0 };
868 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
869 libzfs_mnttab_fini(hdl
);
871 /* Reopen MNTTAB to prevent reading stale data from open file */
872 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
875 srch
.mnt_special
= (char *)fsname
;
876 srch
.mnt_fstype
= MNTTYPE_ZFS
;
877 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
883 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
884 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
887 find
.mtn_mt
.mnt_special
= (char *)fsname
;
888 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
890 *entry
= mtn
->mtn_mt
;
897 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
898 const char *mountp
, const char *mntopts
)
902 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
904 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
905 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
906 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
907 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
908 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
909 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
913 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
918 find
.mtn_mt
.mnt_special
= (char *)fsname
;
919 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
921 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
922 free(ret
->mtn_mt
.mnt_special
);
923 free(ret
->mtn_mt
.mnt_mountp
);
924 free(ret
->mtn_mt
.mnt_fstype
);
925 free(ret
->mtn_mt
.mnt_mntopts
);
931 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
933 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
935 if (zpool_handle
== NULL
)
938 *spa_version
= zpool_get_prop_int(zpool_handle
,
939 ZPOOL_PROP_VERSION
, NULL
);
944 * The choice of reservation property depends on the SPA version.
947 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
951 if (zfs_spa_version(zhp
, &spa_version
) < 0)
954 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
955 *resv_prop
= ZFS_PROP_REFRESERVATION
;
957 *resv_prop
= ZFS_PROP_RESERVATION
;
963 * Given an nvlist of properties to set, validates that they are correct, and
964 * parses any numeric properties (index, boolean, etc) if they are specified as
968 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
969 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
970 boolean_t key_params_ok
, const char *errbuf
)
977 int chosen_normal
= -1;
980 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
981 (void) no_memory(hdl
);
986 * Make sure this property is valid and applies to this type.
990 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
991 const char *propname
= nvpair_name(elem
);
993 prop
= zfs_name_to_prop(propname
);
994 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
996 * This is a user property: make sure it's a
997 * string, and that it's less than ZAP_MAXNAMELEN.
999 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1000 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1001 "'%s' must be a string"), propname
);
1002 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1006 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1007 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1008 "property name '%s' is too long"),
1010 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1014 (void) nvpair_value_string(elem
, &strval
);
1015 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1016 (void) no_memory(hdl
);
1023 * Currently, only user properties can be modified on
1026 if (type
== ZFS_TYPE_SNAPSHOT
) {
1027 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1028 "this property can not be modified for snapshots"));
1029 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1033 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1034 zfs_userquota_prop_t uqtype
;
1035 char newpropname
[128];
1040 if (userquota_propname_decode(propname
, zoned
,
1041 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1043 dgettext(TEXT_DOMAIN
,
1044 "'%s' has an invalid user/group name"),
1046 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1050 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1051 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1052 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1053 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
) {
1055 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1057 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1062 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1063 (void) nvpair_value_string(elem
, &strval
);
1064 if (strcmp(strval
, "none") == 0) {
1066 } else if (zfs_nicestrtonum(hdl
,
1067 strval
, &intval
) != 0) {
1068 (void) zfs_error(hdl
,
1069 EZFS_BADPROP
, errbuf
);
1072 } else if (nvpair_type(elem
) ==
1074 (void) nvpair_value_uint64(elem
, &intval
);
1076 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1077 "use 'none' to disable "
1078 "userquota/groupquota"));
1082 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1083 "'%s' must be a number"), propname
);
1084 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1089 * Encode the prop name as
1090 * userquota@<hex-rid>-domain, to make it easy
1091 * for the kernel to decode.
1093 (void) snprintf(newpropname
, sizeof (newpropname
),
1094 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
1095 (longlong_t
)rid
, domain
);
1099 if (nvlist_add_uint64_array(ret
, newpropname
,
1101 (void) no_memory(hdl
);
1105 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1106 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1107 "'%s' is readonly"),
1109 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1113 if (prop
== ZPROP_INVAL
) {
1114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1115 "invalid property '%s'"), propname
);
1116 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1120 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1122 dgettext(TEXT_DOMAIN
, "'%s' does not "
1123 "apply to datasets of this type"), propname
);
1124 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1128 if (zfs_prop_readonly(prop
) &&
1129 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1130 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1132 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1134 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1138 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1139 &strval
, &intval
, errbuf
) != 0)
1143 * Perform some additional checks for specific properties.
1146 case ZFS_PROP_VERSION
:
1152 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1153 if (intval
< version
) {
1154 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1155 "Can not downgrade; already at version %u"),
1157 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1163 case ZFS_PROP_VOLBLOCKSIZE
:
1164 case ZFS_PROP_RECORDSIZE
:
1166 int maxbs
= SPA_MAXBLOCKSIZE
;
1169 if (zpool_hdl
!= NULL
) {
1170 maxbs
= zpool_get_prop_int(zpool_hdl
,
1171 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1174 * The value must be a power of two between
1175 * SPA_MINBLOCKSIZE and maxbs.
1177 if (intval
< SPA_MINBLOCKSIZE
||
1178 intval
> maxbs
|| !ISP2(intval
)) {
1179 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1180 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1181 "'%s' must be power of 2 from 512B "
1182 "to %s"), propname
, buf
);
1183 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1188 case ZFS_PROP_MLSLABEL
:
1190 #ifdef HAVE_MLSLABEL
1192 * Verify the mlslabel string and convert to
1193 * internal hex label string.
1197 char *hex
= NULL
; /* internal label string */
1199 /* Default value is already OK. */
1200 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1203 /* Verify the label can be converted to binary form */
1204 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1205 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1206 L_NO_CORRECTION
, NULL
) == -1)) {
1210 /* Now translate to hex internal label string */
1211 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1217 m_label_free(new_sl
);
1219 /* If string is already in internal form, we're done. */
1220 if (strcmp(strval
, hex
) == 0) {
1225 /* Replace the label string with the internal form. */
1226 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1228 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1235 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1236 "invalid mlslabel '%s'"), strval
);
1237 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1238 m_label_free(new_sl
); /* OK if null */
1241 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1242 "mlslabels are unsupported"));
1243 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1245 #endif /* HAVE_MLSLABEL */
1248 case ZFS_PROP_MOUNTPOINT
:
1250 namecheck_err_t why
;
1252 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1253 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1256 if (mountpoint_namecheck(strval
, &why
)) {
1258 case NAME_ERR_LEADING_SLASH
:
1260 dgettext(TEXT_DOMAIN
,
1261 "'%s' must be an absolute path, "
1262 "'none', or 'legacy'"), propname
);
1264 case NAME_ERR_TOOLONG
:
1266 dgettext(TEXT_DOMAIN
,
1267 "component of '%s' is too long"),
1273 dgettext(TEXT_DOMAIN
,
1274 "(%d) not defined"),
1278 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1285 case ZFS_PROP_SHARESMB
:
1286 case ZFS_PROP_SHARENFS
:
1288 * For the mountpoint and sharenfs or sharesmb
1289 * properties, check if it can be set in a
1290 * global/non-global zone based on
1291 * the zoned property value:
1293 * global zone non-global zone
1294 * --------------------------------------------------
1295 * zoned=on mountpoint (no) mountpoint (yes)
1296 * sharenfs (no) sharenfs (no)
1297 * sharesmb (no) sharesmb (no)
1299 * zoned=off mountpoint (yes) N/A
1304 if (getzoneid() == GLOBAL_ZONEID
) {
1305 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1306 "'%s' cannot be set on "
1307 "dataset in a non-global zone"),
1309 (void) zfs_error(hdl
, EZFS_ZONED
,
1312 } else if (prop
== ZFS_PROP_SHARENFS
||
1313 prop
== ZFS_PROP_SHARESMB
) {
1314 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1315 "'%s' cannot be set in "
1316 "a non-global zone"), propname
);
1317 (void) zfs_error(hdl
, EZFS_ZONED
,
1321 } else if (getzoneid() != GLOBAL_ZONEID
) {
1323 * If zoned property is 'off', this must be in
1324 * a global zone. If not, something is wrong.
1326 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1327 "'%s' cannot be set while dataset "
1328 "'zoned' property is set"), propname
);
1329 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1334 * At this point, it is legitimate to set the
1335 * property. Now we want to make sure that the
1336 * property value is valid if it is sharenfs.
1338 if ((prop
== ZFS_PROP_SHARENFS
||
1339 prop
== ZFS_PROP_SHARESMB
) &&
1340 strcmp(strval
, "on") != 0 &&
1341 strcmp(strval
, "off") != 0) {
1342 zfs_share_proto_t proto
;
1344 if (prop
== ZFS_PROP_SHARESMB
)
1350 * Must be an valid sharing protocol
1351 * option string so init the libshare
1352 * in order to enable the parser and
1353 * then parse the options. We use the
1354 * control API since we don't care about
1355 * the current configuration and don't
1356 * want the overhead of loading it
1357 * until we actually do something.
1360 if (zfs_init_libshare(hdl
,
1361 SA_INIT_CONTROL_API
) != SA_OK
) {
1363 * An error occurred so we can't do
1366 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1367 "'%s' cannot be set: problem "
1368 "in share initialization"),
1370 (void) zfs_error(hdl
, EZFS_BADPROP
,
1375 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1377 * There was an error in parsing so
1378 * deal with it by issuing an error
1379 * message and leaving after
1380 * uninitializing the the libshare
1383 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1384 "'%s' cannot be set to invalid "
1385 "options"), propname
);
1386 (void) zfs_error(hdl
, EZFS_BADPROP
,
1388 zfs_uninit_libshare(hdl
);
1391 zfs_uninit_libshare(hdl
);
1396 case ZFS_PROP_KEYLOCATION
:
1397 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1398 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1399 "invalid keylocation"));
1400 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1406 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1408 if (crypt
== ZIO_CRYPT_OFF
&&
1409 strcmp(strval
, "none") != 0) {
1410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1411 "keylocation must not be 'none' "
1412 "for encrypted datasets"));
1413 (void) zfs_error(hdl
, EZFS_BADPROP
,
1416 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1417 strcmp(strval
, "none") == 0) {
1418 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1419 "keylocation must be 'none' "
1420 "for unencrypted datasets"));
1421 (void) zfs_error(hdl
, EZFS_BADPROP
,
1428 case ZFS_PROP_PBKDF2_ITERS
:
1429 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1430 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1431 "minimum pbkdf2 iterations is %u"),
1432 MIN_PBKDF2_ITERATIONS
);
1433 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1438 case ZFS_PROP_UTF8ONLY
:
1439 chosen_utf
= (int)intval
;
1442 case ZFS_PROP_NORMALIZE
:
1443 chosen_normal
= (int)intval
;
1451 * For changes to existing volumes, we have some additional
1452 * checks to enforce.
1454 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1455 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1456 ZFS_PROP_VOLBLOCKSIZE
);
1460 case ZFS_PROP_VOLSIZE
:
1461 if (intval
% blocksize
!= 0) {
1462 zfs_nicebytes(blocksize
, buf
,
1464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1465 "'%s' must be a multiple of "
1466 "volume block size (%s)"),
1468 (void) zfs_error(hdl
, EZFS_BADPROP
,
1474 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1475 "'%s' cannot be zero"),
1477 (void) zfs_error(hdl
, EZFS_BADPROP
,
1488 /* check encryption properties */
1490 int64_t crypt
= zfs_prop_get_int(zhp
,
1491 ZFS_PROP_ENCRYPTION
);
1494 case ZFS_PROP_COPIES
:
1495 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1496 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1497 "encrypted datasets cannot have "
1499 (void) zfs_error(hdl
, EZFS_BADPROP
,
1511 * If normalization was chosen, but no UTF8 choice was made,
1512 * enforce rejection of non-UTF8 names.
1514 * If normalization was chosen, but rejecting non-UTF8 names
1515 * was explicitly not chosen, it is an error.
1517 if (chosen_normal
> 0 && chosen_utf
< 0) {
1518 if (nvlist_add_uint64(ret
,
1519 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1520 (void) no_memory(hdl
);
1523 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1525 "'%s' must be set 'on' if normalization chosen"),
1526 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1527 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1538 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1540 uint64_t old_volsize
;
1541 uint64_t new_volsize
;
1542 uint64_t old_reservation
;
1543 uint64_t new_reservation
;
1544 zfs_prop_t resv_prop
;
1548 * If this is an existing volume, and someone is setting the volsize,
1549 * make sure that it matches the reservation, or add it if necessary.
1551 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1552 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1554 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1556 props
= fnvlist_alloc();
1557 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1558 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1560 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1561 old_reservation
) || nvlist_exists(nvl
,
1562 zfs_prop_to_name(resv_prop
))) {
1563 fnvlist_free(props
);
1566 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1567 &new_volsize
) != 0) {
1568 fnvlist_free(props
);
1571 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1572 fnvlist_free(props
);
1574 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1575 new_reservation
) != 0) {
1576 (void) no_memory(zhp
->zfs_hdl
);
1583 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1590 * For quotas and reservations, ENOSPC indicates
1591 * something different; setting a quota or reservation
1592 * doesn't use any disk space.
1595 case ZFS_PROP_QUOTA
:
1596 case ZFS_PROP_REFQUOTA
:
1597 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1598 "size is less than current used or "
1600 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1603 case ZFS_PROP_RESERVATION
:
1604 case ZFS_PROP_REFRESERVATION
:
1605 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1606 "size is greater than available space"));
1607 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1611 (void) zfs_standard_error(hdl
, err
, errbuf
);
1617 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1621 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1625 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1626 "property value too long"));
1627 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1632 "pool and or dataset must be upgraded to set this "
1633 "property or value"));
1634 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1638 if (prop
== ZFS_PROP_COMPRESSION
||
1639 prop
== ZFS_PROP_DNODESIZE
||
1640 prop
== ZFS_PROP_RECORDSIZE
) {
1641 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1642 "property setting is not allowed on "
1643 "bootable datasets"));
1644 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1645 } else if (prop
== ZFS_PROP_CHECKSUM
||
1646 prop
== ZFS_PROP_DEDUP
) {
1647 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1648 "property setting is not allowed on "
1650 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1652 (void) zfs_standard_error(hdl
, err
, errbuf
);
1657 if (prop
== ZPROP_INVAL
) {
1658 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1660 (void) zfs_standard_error(hdl
, err
, errbuf
);
1665 if (prop
== ZFS_PROP_KEYLOCATION
) {
1666 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1667 "keylocation may only be set on encryption roots"));
1668 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1670 (void) zfs_standard_error(hdl
, err
, errbuf
);
1676 * This platform can't address a volume this big.
1679 if (prop
== ZFS_PROP_VOLSIZE
) {
1680 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1686 (void) zfs_standard_error(hdl
, err
, errbuf
);
1691 zfs_is_namespace_prop(zfs_prop_t prop
)
1695 case ZFS_PROP_ATIME
:
1696 case ZFS_PROP_RELATIME
:
1697 case ZFS_PROP_DEVICES
:
1699 case ZFS_PROP_SETUID
:
1700 case ZFS_PROP_READONLY
:
1701 case ZFS_PROP_XATTR
:
1702 case ZFS_PROP_NBMAND
:
1711 * Given a property name and value, set the property for the given dataset.
1714 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1718 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1719 nvlist_t
*nvl
= NULL
;
1721 (void) snprintf(errbuf
, sizeof (errbuf
),
1722 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1725 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1726 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1727 (void) no_memory(hdl
);
1731 ret
= zfs_prop_set_list(zhp
, nvl
);
1741 * Given an nvlist of property names and values, set the properties for the
1745 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1747 zfs_cmd_t zc
= {"\0"};
1749 prop_changelist_t
**cls
= NULL
;
1752 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1756 zfs_prop_t prop
= 0;
1759 (void) snprintf(errbuf
, sizeof (errbuf
),
1760 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1763 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1764 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1765 B_FALSE
, errbuf
)) == NULL
)
1769 * We have to check for any extra properties which need to be added
1770 * before computing the length of the nvlist.
1772 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1774 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1775 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1776 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1781 * Check how many properties we're setting and allocate an array to
1782 * store changelist pointers for postfix().
1784 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1786 elem
= nvlist_next_nvpair(nvl
, elem
))
1788 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1792 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1794 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1796 prop
= zfs_name_to_prop(nvpair_name(elem
));
1798 assert(cl_idx
< nvl_len
);
1800 * We don't want to unmount & remount the dataset when changing
1801 * its canmount property to 'on' or 'noauto'. We only use
1802 * the changelist logic to unmount when setting canmount=off.
1804 if (prop
!= ZFS_PROP_CANMOUNT
||
1805 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1806 zfs_is_mounted(zhp
, NULL
))) {
1807 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1808 if (cls
[cl_idx
] == NULL
)
1812 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1813 changelist_haszonedchild(cls
[cl_idx
])) {
1814 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1815 "child dataset with inherited mountpoint is used "
1816 "in a non-global zone"));
1817 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1821 if (cls
[cl_idx
] != NULL
&&
1822 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1827 assert(cl_idx
== nvl_len
);
1830 * Execute the corresponding ioctl() to set this list of properties.
1832 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1834 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1835 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1838 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1841 /* Get the list of unset properties back and report them. */
1842 nvlist_t
*errorprops
= NULL
;
1843 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1845 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1847 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1848 prop
= zfs_name_to_prop(nvpair_name(elem
));
1849 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1851 nvlist_free(errorprops
);
1853 if (added_resv
&& errno
== ENOSPC
) {
1854 /* clean up the volsize property we tried to set */
1855 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1859 zcmd_free_nvlists(&zc
);
1861 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1863 if (nvlist_add_uint64(nvl
,
1864 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1867 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1869 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1872 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1873 if (cls
[cl_idx
] != NULL
) {
1874 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1882 * Refresh the statistics so the new property
1883 * value is reflected.
1885 (void) get_stats(zhp
);
1888 * Remount the filesystem to propagate the change
1889 * if one of the options handled by the generic
1890 * Linux namespace layer has been modified.
1892 if (zfs_is_namespace_prop(prop
) &&
1893 zfs_is_mounted(zhp
, NULL
))
1894 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1900 zcmd_free_nvlists(&zc
);
1902 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1903 if (cls
[cl_idx
] != NULL
)
1904 changelist_free(cls
[cl_idx
]);
1912 * Given a property, inherit the value from the parent dataset, or if received
1913 * is TRUE, revert to the received value, if any.
1916 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1918 zfs_cmd_t zc
= {"\0"};
1920 prop_changelist_t
*cl
;
1921 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1925 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1926 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1928 zc
.zc_cookie
= received
;
1929 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1931 * For user properties, the amount of work we have to do is very
1932 * small, so just do it here.
1934 if (!zfs_prop_user(propname
)) {
1935 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1936 "invalid property"));
1937 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1940 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1941 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1943 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1944 return (zfs_standard_error(hdl
, errno
, errbuf
));
1950 * Verify that this property is inheritable.
1952 if (zfs_prop_readonly(prop
))
1953 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1955 if (!zfs_prop_inheritable(prop
) && !received
)
1956 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1959 * Check to see if the value applies to this type
1961 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1962 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1965 * Normalize the name, to get rid of shorthand abbreviations.
1967 propname
= zfs_prop_to_name(prop
);
1968 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1969 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1971 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1972 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1974 "dataset is used in a non-global zone"));
1975 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1979 * Determine datasets which will be affected by this change, if any.
1981 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1984 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1985 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1986 "child dataset with inherited mountpoint is used "
1987 "in a non-global zone"));
1988 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1992 if ((ret
= changelist_prefix(cl
)) != 0)
1995 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1996 return (zfs_standard_error(hdl
, errno
, errbuf
));
1999 if ((ret
= changelist_postfix(cl
)) != 0)
2003 * Refresh the statistics so the new property is reflected.
2005 (void) get_stats(zhp
);
2008 * Remount the filesystem to propagate the change
2009 * if one of the options handled by the generic
2010 * Linux namespace layer has been modified.
2012 if (zfs_is_namespace_prop(prop
) &&
2013 zfs_is_mounted(zhp
, NULL
))
2014 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2018 changelist_free(cl
);
2023 * True DSL properties are stored in an nvlist. The following two functions
2024 * extract them appropriately.
2027 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2033 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2034 zfs_prop_to_name(prop
), &nv
) == 0) {
2035 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2036 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2038 verify(!zhp
->zfs_props_table
||
2039 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2040 value
= zfs_prop_default_numeric(prop
);
2048 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2054 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2055 zfs_prop_to_name(prop
), &nv
) == 0) {
2056 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2057 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2059 verify(!zhp
->zfs_props_table
||
2060 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2061 value
= zfs_prop_default_string(prop
);
2069 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2071 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2075 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2077 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2078 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2082 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2084 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2089 * Internal function for getting a numeric property. Both zfs_prop_get() and
2090 * zfs_prop_get_int() are built using this interface.
2092 * Certain properties can be overridden using 'mount -o'. In this case, scan
2093 * the contents of the /proc/self/mounts entry, searching for the
2094 * appropriate options. If they differ from the on-disk values, report the
2095 * current values and mark the source "temporary".
2098 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2099 char **source
, uint64_t *val
)
2101 zfs_cmd_t zc
= {"\0"};
2102 nvlist_t
*zplprops
= NULL
;
2104 char *mntopt_on
= NULL
;
2105 char *mntopt_off
= NULL
;
2106 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2111 * If the property is being fetched for a snapshot, check whether
2112 * the property is valid for the snapshot's head dataset type.
2114 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2115 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2116 *val
= zfs_prop_default_numeric(prop
);
2121 case ZFS_PROP_ATIME
:
2122 mntopt_on
= MNTOPT_ATIME
;
2123 mntopt_off
= MNTOPT_NOATIME
;
2126 case ZFS_PROP_RELATIME
:
2127 mntopt_on
= MNTOPT_RELATIME
;
2128 mntopt_off
= MNTOPT_NORELATIME
;
2131 case ZFS_PROP_DEVICES
:
2132 mntopt_on
= MNTOPT_DEVICES
;
2133 mntopt_off
= MNTOPT_NODEVICES
;
2137 mntopt_on
= MNTOPT_EXEC
;
2138 mntopt_off
= MNTOPT_NOEXEC
;
2141 case ZFS_PROP_READONLY
:
2142 mntopt_on
= MNTOPT_RO
;
2143 mntopt_off
= MNTOPT_RW
;
2146 case ZFS_PROP_SETUID
:
2147 mntopt_on
= MNTOPT_SETUID
;
2148 mntopt_off
= MNTOPT_NOSETUID
;
2151 case ZFS_PROP_XATTR
:
2152 mntopt_on
= MNTOPT_XATTR
;
2153 mntopt_off
= MNTOPT_NOXATTR
;
2156 case ZFS_PROP_NBMAND
:
2157 mntopt_on
= MNTOPT_NBMAND
;
2158 mntopt_off
= MNTOPT_NONBMAND
;
2166 * Because looking up the mount options is potentially expensive
2167 * (iterating over all of /proc/self/mounts), we defer its
2168 * calculation until we're looking up a property which requires
2171 if (!zhp
->zfs_mntcheck
&&
2172 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2173 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2174 struct mnttab entry
;
2176 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2177 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2179 if (zhp
->zfs_mntopts
== NULL
)
2183 zhp
->zfs_mntcheck
= B_TRUE
;
2186 if (zhp
->zfs_mntopts
== NULL
)
2187 mnt
.mnt_mntopts
= "";
2189 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2192 case ZFS_PROP_ATIME
:
2193 case ZFS_PROP_RELATIME
:
2194 case ZFS_PROP_DEVICES
:
2196 case ZFS_PROP_READONLY
:
2197 case ZFS_PROP_SETUID
:
2198 case ZFS_PROP_XATTR
:
2199 case ZFS_PROP_NBMAND
:
2200 *val
= getprop_uint64(zhp
, prop
, source
);
2205 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2208 *src
= ZPROP_SRC_TEMPORARY
;
2209 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2212 *src
= ZPROP_SRC_TEMPORARY
;
2216 case ZFS_PROP_CANMOUNT
:
2217 case ZFS_PROP_VOLSIZE
:
2218 case ZFS_PROP_QUOTA
:
2219 case ZFS_PROP_REFQUOTA
:
2220 case ZFS_PROP_RESERVATION
:
2221 case ZFS_PROP_REFRESERVATION
:
2222 case ZFS_PROP_FILESYSTEM_LIMIT
:
2223 case ZFS_PROP_SNAPSHOT_LIMIT
:
2224 case ZFS_PROP_FILESYSTEM_COUNT
:
2225 case ZFS_PROP_SNAPSHOT_COUNT
:
2226 *val
= getprop_uint64(zhp
, prop
, source
);
2228 if (*source
== NULL
) {
2229 /* not default, must be local */
2230 *source
= zhp
->zfs_name
;
2234 case ZFS_PROP_MOUNTED
:
2235 *val
= (zhp
->zfs_mntopts
!= NULL
);
2238 case ZFS_PROP_NUMCLONES
:
2239 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2242 case ZFS_PROP_VERSION
:
2243 case ZFS_PROP_NORMALIZE
:
2244 case ZFS_PROP_UTF8ONLY
:
2246 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2248 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2249 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2250 zcmd_free_nvlists(&zc
);
2251 if (prop
== ZFS_PROP_VERSION
&&
2252 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2253 *val
= zfs_prop_default_numeric(prop
);
2256 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2257 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2259 zcmd_free_nvlists(&zc
);
2262 nvlist_free(zplprops
);
2263 zcmd_free_nvlists(&zc
);
2266 case ZFS_PROP_INCONSISTENT
:
2267 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2271 switch (zfs_prop_get_type(prop
)) {
2272 case PROP_TYPE_NUMBER
:
2273 case PROP_TYPE_INDEX
:
2274 *val
= getprop_uint64(zhp
, prop
, source
);
2276 * If we tried to use a default value for a
2277 * readonly property, it means that it was not
2278 * present. Note this only applies to "truly"
2279 * readonly properties, not set-once properties
2280 * like volblocksize.
2282 if (zfs_prop_readonly(prop
) &&
2283 !zfs_prop_setonce(prop
) &&
2284 *source
!= NULL
&& (*source
)[0] == '\0') {
2290 case PROP_TYPE_STRING
:
2292 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2293 "cannot get non-numeric property"));
2294 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2295 dgettext(TEXT_DOMAIN
, "internal error")));
2303 * Calculate the source type, given the raw source string.
2306 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2307 char *statbuf
, size_t statlen
)
2309 if (statbuf
== NULL
||
2310 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2314 if (source
== NULL
) {
2315 *srctype
= ZPROP_SRC_NONE
;
2316 } else if (source
[0] == '\0') {
2317 *srctype
= ZPROP_SRC_DEFAULT
;
2318 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2319 *srctype
= ZPROP_SRC_RECEIVED
;
2321 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2322 *srctype
= ZPROP_SRC_LOCAL
;
2324 (void) strlcpy(statbuf
, source
, statlen
);
2325 *srctype
= ZPROP_SRC_INHERITED
;
2332 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2333 size_t proplen
, boolean_t literal
)
2338 if (zhp
->zfs_recvd_props
== NULL
)
2339 if (get_recvd_props_ioctl(zhp
) != 0)
2342 prop
= zfs_name_to_prop(propname
);
2344 if (prop
!= ZPROP_INVAL
) {
2346 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2348 zfs_set_recvd_props_mode(zhp
, &cookie
);
2349 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2350 NULL
, NULL
, 0, literal
);
2351 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2355 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2356 propname
, &propval
) != 0)
2358 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2360 (void) strlcpy(propbuf
, recvdval
, proplen
);
2363 return (err
== 0 ? 0 : -1);
2367 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2372 value
= zfs_get_clones_nvl(zhp
);
2377 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2378 pair
= nvlist_next_nvpair(value
, pair
)) {
2379 if (propbuf
[0] != '\0')
2380 (void) strlcat(propbuf
, ",", proplen
);
2381 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2387 struct get_clones_arg
{
2391 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2395 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2397 struct get_clones_arg
*gca
= arg
;
2399 if (gca
->numclones
== 0) {
2404 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2405 NULL
, NULL
, 0, B_TRUE
) != 0)
2407 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2408 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2413 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2419 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2421 nvlist_t
*nv
, *value
;
2423 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2424 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2425 struct get_clones_arg gca
;
2428 * if this is a snapshot, then the kernel wasn't able
2429 * to get the clones. Do it by slowly iterating.
2431 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2433 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2435 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2440 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2442 gca
.origin
= zhp
->zfs_name
;
2444 if (gca
.numclones
!= 0) {
2446 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2449 /* get the pool name */
2450 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2451 (void) strsep(&cp
, "/@");
2452 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2453 ZFS_TYPE_FILESYSTEM
);
2460 (void) get_clones_cb(root
, &gca
);
2463 if (gca
.numclones
!= 0 ||
2464 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2465 nvlist_add_nvlist(zhp
->zfs_props
,
2466 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2473 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2474 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2477 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2483 * Retrieve a property from the given object. If 'literal' is specified, then
2484 * numbers are left as exact values. Otherwise, numbers are converted to a
2485 * human-readable form.
2487 * Returns 0 on success, or -1 on error.
2490 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2491 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2493 char *source
= NULL
;
2497 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2500 * Check to see if this property applies to our object
2502 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2505 if (received
&& zfs_prop_readonly(prop
))
2509 *src
= ZPROP_SRC_NONE
;
2512 case ZFS_PROP_CREATION
:
2514 * 'creation' is a time_t stored in the statistics. We convert
2515 * this into a string unless 'literal' is specified.
2518 val
= getprop_uint64(zhp
, prop
, &source
);
2519 time_t time
= (time_t)val
;
2523 localtime_r(&time
, &t
) == NULL
||
2524 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2526 (void) snprintf(propbuf
, proplen
, "%llu",
2531 case ZFS_PROP_MOUNTPOINT
:
2533 * Getting the precise mountpoint can be tricky.
2535 * - for 'none' or 'legacy', return those values.
2536 * - for inherited mountpoints, we want to take everything
2537 * after our ancestor and append it to the inherited value.
2539 * If the pool has an alternate root, we want to prepend that
2540 * root to any values we return.
2543 str
= getprop_string(zhp
, prop
, &source
);
2545 if (str
[0] == '/') {
2546 char buf
[MAXPATHLEN
];
2548 const char *relpath
;
2551 * If we inherit the mountpoint, even from a dataset
2552 * with a received value, the source will be the path of
2553 * the dataset we inherit from. If source is
2554 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2557 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2560 relpath
= zhp
->zfs_name
+ strlen(source
);
2561 if (relpath
[0] == '/')
2565 if ((zpool_get_prop(zhp
->zpool_hdl
,
2566 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2567 B_FALSE
)) || (strcmp(root
, "-") == 0))
2570 * Special case an alternate root of '/'. This will
2571 * avoid having multiple leading slashes in the
2574 if (strcmp(root
, "/") == 0)
2578 * If the mountpoint is '/' then skip over this
2579 * if we are obtaining either an alternate root or
2580 * an inherited mountpoint.
2582 if (str
[1] == '\0' && (root
[0] != '\0' ||
2583 relpath
[0] != '\0'))
2586 if (relpath
[0] == '\0')
2587 (void) snprintf(propbuf
, proplen
, "%s%s",
2590 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2591 root
, str
, relpath
[0] == '@' ? "" : "/",
2594 /* 'legacy' or 'none' */
2595 (void) strlcpy(propbuf
, str
, proplen
);
2600 case ZFS_PROP_ORIGIN
:
2601 str
= getprop_string(zhp
, prop
, &source
);
2604 (void) strlcpy(propbuf
, str
, proplen
);
2607 case ZFS_PROP_CLONES
:
2608 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2612 case ZFS_PROP_QUOTA
:
2613 case ZFS_PROP_REFQUOTA
:
2614 case ZFS_PROP_RESERVATION
:
2615 case ZFS_PROP_REFRESERVATION
:
2617 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2621 * If quota or reservation is 0, we translate this into 'none'
2622 * (unless literal is set), and indicate that it's the default
2623 * value. Otherwise, we print the number nicely and indicate
2624 * that its set locally.
2628 (void) strlcpy(propbuf
, "0", proplen
);
2630 (void) strlcpy(propbuf
, "none", proplen
);
2633 (void) snprintf(propbuf
, proplen
, "%llu",
2636 zfs_nicebytes(val
, propbuf
, proplen
);
2640 case ZFS_PROP_FILESYSTEM_LIMIT
:
2641 case ZFS_PROP_SNAPSHOT_LIMIT
:
2642 case ZFS_PROP_FILESYSTEM_COUNT
:
2643 case ZFS_PROP_SNAPSHOT_COUNT
:
2645 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2649 * If limit is UINT64_MAX, we translate this into 'none' (unless
2650 * literal is set), and indicate that it's the default value.
2651 * Otherwise, we print the number nicely and indicate that it's
2655 (void) snprintf(propbuf
, proplen
, "%llu",
2657 } else if (val
== UINT64_MAX
) {
2658 (void) strlcpy(propbuf
, "none", proplen
);
2660 zfs_nicenum(val
, propbuf
, proplen
);
2664 case ZFS_PROP_REFRATIO
:
2665 case ZFS_PROP_COMPRESSRATIO
:
2666 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2669 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2670 (u_longlong_t
)(val
/ 100),
2671 (u_longlong_t
)(val
% 100));
2673 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2674 (u_longlong_t
)(val
/ 100),
2675 (u_longlong_t
)(val
% 100));
2679 switch (zhp
->zfs_type
) {
2680 case ZFS_TYPE_FILESYSTEM
:
2683 case ZFS_TYPE_VOLUME
:
2686 case ZFS_TYPE_SNAPSHOT
:
2689 case ZFS_TYPE_BOOKMARK
:
2695 (void) snprintf(propbuf
, proplen
, "%s", str
);
2698 case ZFS_PROP_MOUNTED
:
2700 * The 'mounted' property is a pseudo-property that described
2701 * whether the filesystem is currently mounted. Even though
2702 * it's a boolean value, the typical values of "on" and "off"
2703 * don't make sense, so we translate to "yes" and "no".
2705 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2706 src
, &source
, &val
) != 0)
2709 (void) strlcpy(propbuf
, "yes", proplen
);
2711 (void) strlcpy(propbuf
, "no", proplen
);
2716 * The 'name' property is a pseudo-property derived from the
2717 * dataset name. It is presented as a real property to simplify
2720 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2723 case ZFS_PROP_MLSLABEL
:
2725 #ifdef HAVE_MLSLABEL
2726 m_label_t
*new_sl
= NULL
;
2727 char *ascii
= NULL
; /* human readable label */
2729 (void) strlcpy(propbuf
,
2730 getprop_string(zhp
, prop
, &source
), proplen
);
2732 if (literal
|| (strcasecmp(propbuf
,
2733 ZFS_MLSLABEL_DEFAULT
) == 0))
2737 * Try to translate the internal hex string to
2738 * human-readable output. If there are any
2739 * problems just use the hex string.
2742 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2743 L_NO_CORRECTION
, NULL
) == -1) {
2744 m_label_free(new_sl
);
2748 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2752 m_label_free(new_sl
);
2755 m_label_free(new_sl
);
2757 (void) strlcpy(propbuf
, ascii
, proplen
);
2760 (void) strlcpy(propbuf
,
2761 getprop_string(zhp
, prop
, &source
), proplen
);
2762 #endif /* HAVE_MLSLABEL */
2767 case ZFS_PROP_CREATETXG
:
2769 * GUIDs are stored as numbers, but they are identifiers.
2770 * We don't want them to be pretty printed, because pretty
2771 * printing mangles the ID into a truncated and useless value.
2773 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2775 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2778 case ZFS_PROP_REFERENCED
:
2779 case ZFS_PROP_AVAILABLE
:
2781 case ZFS_PROP_USEDSNAP
:
2782 case ZFS_PROP_USEDDS
:
2783 case ZFS_PROP_USEDREFRESERV
:
2784 case ZFS_PROP_USEDCHILD
:
2785 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2788 (void) snprintf(propbuf
, proplen
, "%llu",
2791 zfs_nicebytes(val
, propbuf
, proplen
);
2795 switch (zfs_prop_get_type(prop
)) {
2796 case PROP_TYPE_NUMBER
:
2797 if (get_numeric_property(zhp
, prop
, src
,
2798 &source
, &val
) != 0)
2801 (void) snprintf(propbuf
, proplen
, "%llu",
2804 zfs_nicenum(val
, propbuf
, proplen
);
2807 case PROP_TYPE_STRING
:
2808 str
= getprop_string(zhp
, prop
, &source
);
2811 (void) strlcpy(propbuf
, str
, proplen
);
2814 case PROP_TYPE_INDEX
:
2815 if (get_numeric_property(zhp
, prop
, src
,
2816 &source
, &val
) != 0)
2818 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2820 (void) strlcpy(propbuf
, strval
, proplen
);
2828 get_source(zhp
, src
, source
, statbuf
, statlen
);
2834 * Utility function to get the given numeric property. Does no validation that
2835 * the given property is the appropriate type; should only be used with
2836 * hard-coded property types.
2839 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2844 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2850 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2854 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2855 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2859 * Similar to zfs_prop_get(), but returns the value as an integer.
2862 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2863 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2868 * Check to see if this property applies to our object
2870 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
2871 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2872 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2873 zfs_prop_to_name(prop
)));
2877 *src
= ZPROP_SRC_NONE
;
2879 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2882 get_source(zhp
, src
, source
, statbuf
, statlen
);
2889 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2890 char **domainp
, idmap_rid_t
*ridp
)
2892 idmap_get_handle_t
*get_hdl
= NULL
;
2896 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2900 err
= idmap_get_sidbyuid(get_hdl
, id
,
2901 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2903 err
= idmap_get_sidbygid(get_hdl
, id
,
2904 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2906 if (err
== IDMAP_SUCCESS
&&
2907 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2908 status
== IDMAP_SUCCESS
)
2914 idmap_get_destroy(get_hdl
);
2917 #endif /* HAVE_IDMAP */
2920 * convert the propname into parameters needed by kernel
2921 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2922 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2923 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2924 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2927 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2928 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2930 zfs_userquota_prop_t type
;
2939 /* Figure out the property type ({user|group}{quota|space}) */
2940 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2941 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2942 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2945 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2949 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
2950 type
== ZFS_PROP_USEROBJQUOTA
||
2951 type
== ZFS_PROP_USEROBJUSED
);
2952 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
2953 type
== ZFS_PROP_GROUPOBJQUOTA
||
2954 type
== ZFS_PROP_GROUPOBJUSED
);
2956 cp
= strchr(propname
, '@') + 1;
2958 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2959 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2962 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2963 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2966 } else if (strchr(cp
, '@')) {
2969 * It's a SID name (eg "user@domain") that needs to be
2970 * turned into S-1-domainID-RID.
2972 directory_error_t e
;
2973 char *numericsid
= NULL
;
2976 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2979 e
= directory_sid_from_user_name(NULL
,
2982 e
= directory_sid_from_group_name(NULL
,
2986 directory_error_free(e
);
2989 if (numericsid
== NULL
)
2992 (void) strlcpy(domain
, cp
, domainlen
);
2993 cp
= strrchr(domain
, '-');
2998 *ridp
= strtoull(cp
, &end
, 10);
3001 if (errno
!= 0 || *end
!= '\0')
3005 #endif /* HAVE_IDMAP */
3007 /* It's a user/group ID (eg "12345"). */
3010 id
= strtoul(cp
, &end
, 10);
3015 /* It's an ephemeral ID. */
3019 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3020 &mapdomain
, &rid
) != 0)
3022 (void) strlcpy(domain
, mapdomain
, domainlen
);
3026 #endif /* HAVE_IDMAP */
3036 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3037 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3040 zfs_cmd_t zc
= {"\0"};
3042 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3044 err
= userquota_propname_decode(propname
,
3045 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3046 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3047 zc
.zc_objset_type
= *typep
;
3051 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3055 *propvalue
= zc
.zc_cookie
;
3060 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3061 uint64_t *propvalue
)
3063 zfs_userquota_prop_t type
;
3065 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3070 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3071 char *propbuf
, int proplen
, boolean_t literal
)
3075 zfs_userquota_prop_t type
;
3077 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3084 (void) snprintf(propbuf
, proplen
, "%llu",
3085 (u_longlong_t
)propvalue
);
3086 } else if (propvalue
== 0 &&
3087 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3088 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
)) {
3089 (void) strlcpy(propbuf
, "none", proplen
);
3090 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3091 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
) {
3092 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3094 zfs_nicenum(propvalue
, propbuf
, proplen
);
3100 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3101 uint64_t *propvalue
)
3104 zfs_cmd_t zc
= {"\0"};
3105 const char *snapname
;
3107 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3109 snapname
= strchr(propname
, '@') + 1;
3110 if (strchr(snapname
, '@')) {
3111 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3113 /* snapname is the short name, append it to zhp's fsname */
3116 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3117 sizeof (zc
.zc_value
));
3118 cp
= strchr(zc
.zc_value
, '@');
3121 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
3122 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3125 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3129 *propvalue
= zc
.zc_cookie
;
3134 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3135 char *propbuf
, int proplen
, boolean_t literal
)
3140 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3146 (void) snprintf(propbuf
, proplen
, "%llu",
3147 (u_longlong_t
)propvalue
);
3149 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3156 * Returns the name of the given zfs handle.
3159 zfs_get_name(const zfs_handle_t
*zhp
)
3161 return (zhp
->zfs_name
);
3165 * Returns the name of the parent pool for the given zfs handle.
3168 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3170 return (zhp
->zpool_hdl
->zpool_name
);
3174 * Returns the type of the given zfs handle.
3177 zfs_get_type(const zfs_handle_t
*zhp
)
3179 return (zhp
->zfs_type
);
3183 * Is one dataset name a child dataset of another?
3185 * Needs to handle these cases:
3186 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3187 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3188 * Descendant? No. No. No. Yes.
3191 is_descendant(const char *ds1
, const char *ds2
)
3193 size_t d1len
= strlen(ds1
);
3195 /* ds2 can't be a descendant if it's smaller */
3196 if (strlen(ds2
) < d1len
)
3199 /* otherwise, compare strings and verify that there's a '/' char */
3200 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3204 * Given a complete name, return just the portion that refers to the parent.
3205 * Will return -1 if there is no parent (path is just the name of the
3209 parent_name(const char *path
, char *buf
, size_t buflen
)
3213 (void) strlcpy(buf
, path
, buflen
);
3215 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3223 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3225 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3229 * If accept_ancestor is false, then check to make sure that the given path has
3230 * a parent, and that it exists. If accept_ancestor is true, then find the
3231 * closest existing ancestor for the given path. In prefixlen return the
3232 * length of already existing prefix of the given path. We also fetch the
3233 * 'zoned' property, which is used to validate property settings when creating
3237 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3238 boolean_t accept_ancestor
, int *prefixlen
)
3240 zfs_cmd_t zc
= {"\0"};
3241 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3247 (void) snprintf(errbuf
, sizeof (errbuf
),
3248 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3250 /* get parent, and check to see if this is just a pool */
3251 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3252 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3253 "missing dataset name"));
3254 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3257 /* check to see if the pool exists */
3258 if ((slash
= strchr(parent
, '/')) == NULL
)
3259 slash
= parent
+ strlen(parent
);
3260 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3261 zc
.zc_name
[slash
- parent
] = '\0';
3262 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3264 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3265 "no such pool '%s'"), zc
.zc_name
);
3266 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3269 /* check to see if the parent dataset exists */
3270 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3271 if (errno
== ENOENT
&& accept_ancestor
) {
3273 * Go deeper to find an ancestor, give up on top level.
3275 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3276 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3277 "no such pool '%s'"), zc
.zc_name
);
3278 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3280 } else if (errno
== ENOENT
) {
3281 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3282 "parent does not exist"));
3283 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3285 return (zfs_standard_error(hdl
, errno
, errbuf
));
3288 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3292 /* we are in a non-global zone, but parent is in the global zone */
3293 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3294 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3299 /* make sure parent is a filesystem */
3300 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3301 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3302 "parent is not a filesystem"));
3303 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3309 if (prefixlen
!= NULL
)
3310 *prefixlen
= strlen(parent
);
3315 * Finds whether the dataset of the given type(s) exists.
3318 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3322 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3326 * Try to get stats for the dataset, which will tell us if it exists.
3328 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3329 int ds_type
= zhp
->zfs_type
;
3332 if (types
& ds_type
)
3339 * Given a path to 'target', create all the ancestors between
3340 * the prefixlen portion of the path, and the target itself.
3341 * Fail if the initial prefixlen-ancestor does not already exist.
3344 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3350 /* make sure prefix exists */
3351 cp
= target
+ prefixlen
;
3353 assert(strchr(cp
, '/') == NULL
);
3354 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3357 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3365 * Attempt to create, mount, and share any ancestor filesystems,
3366 * up to the prefixlen-long one.
3368 for (cp
= target
+ prefixlen
+ 1;
3369 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3373 h
= make_dataset_handle(hdl
, target
);
3375 /* it already exists, nothing to do here */
3380 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3382 opname
= dgettext(TEXT_DOMAIN
, "create");
3386 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3388 opname
= dgettext(TEXT_DOMAIN
, "open");
3392 if (zfs_mount(h
, NULL
, 0) != 0) {
3393 opname
= dgettext(TEXT_DOMAIN
, "mount");
3397 if (zfs_share(h
) != 0) {
3398 opname
= dgettext(TEXT_DOMAIN
, "share");
3408 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3409 "failed to %s ancestor '%s'"), opname
, target
);
3414 * Creates non-existing ancestors of the given path.
3417 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3423 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3426 if ((path_copy
= strdup(path
)) != NULL
) {
3427 rc
= create_parents(hdl
, path_copy
, prefix
);
3430 if (path_copy
== NULL
|| rc
!= 0)
3437 * Create a new filesystem or volume.
3440 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3445 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3447 enum lzc_dataset_type ost
;
3448 zpool_handle_t
*zpool_handle
;
3449 uint8_t *wkeydata
= NULL
;
3452 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3454 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3455 "cannot create '%s'"), path
);
3457 /* validate the path, taking care to note the extended error message */
3458 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3459 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3461 /* validate parents exist */
3462 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3466 * The failure modes when creating a dataset of a different type over
3467 * one that already exists is a little strange. In particular, if you
3468 * try to create a dataset on top of an existing dataset, the ioctl()
3469 * will return ENOENT, not EEXIST. To prevent this from happening, we
3470 * first try to see if the dataset exists.
3472 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3474 "dataset already exists"));
3475 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3478 if (type
== ZFS_TYPE_VOLUME
)
3479 ost
= LZC_DATSET_TYPE_ZVOL
;
3481 ost
= LZC_DATSET_TYPE_ZFS
;
3483 /* open zpool handle for prop validation */
3484 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3485 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3487 /* truncate pool_path at first slash */
3488 char *p
= strchr(pool_path
, '/');
3492 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3495 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3496 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3497 zpool_close(zpool_handle
);
3500 zpool_close(zpool_handle
);
3502 if (type
== ZFS_TYPE_VOLUME
) {
3504 * If we are creating a volume, the size and block size must
3505 * satisfy a few restraints. First, the blocksize must be a
3506 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3507 * volsize must be a multiple of the block size, and cannot be
3510 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3511 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3513 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3514 "missing volume size"));
3515 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3518 if ((ret
= nvlist_lookup_uint64(props
,
3519 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3520 &blocksize
)) != 0) {
3521 if (ret
== ENOENT
) {
3522 blocksize
= zfs_prop_default_numeric(
3523 ZFS_PROP_VOLBLOCKSIZE
);
3526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3527 "missing volume block size"));
3528 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3535 "volume size cannot be zero"));
3536 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3539 if (size
% blocksize
!= 0) {
3541 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3542 "volume size must be a multiple of volume block "
3544 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3548 (void) parent_name(path
, parent
, sizeof (parent
));
3549 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, &wkeydata
,
3552 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3555 /* create the dataset */
3556 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3558 if (wkeydata
!= NULL
)
3561 /* check for failure */
3565 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3566 "no such parent '%s'"), parent
);
3567 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3570 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3571 "parent '%s' is not a filesystem"), parent
);
3572 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3575 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3576 "pool must be upgraded to set this "
3577 "property or value"));
3578 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3582 "encryption root's key is not loaded "
3584 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3587 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3588 "invalid property value(s) specified"));
3589 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3593 * This platform can't address a volume this big.
3595 if (type
== ZFS_TYPE_VOLUME
)
3596 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3601 return (zfs_standard_error(hdl
, errno
, errbuf
));
3609 * Destroys the given dataset. The caller must make sure that the filesystem
3610 * isn't mounted, and that there are no active dependents. If the file system
3611 * does not exist this function does nothing.
3614 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3616 zfs_cmd_t zc
= {"\0"};
3618 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3619 nvlist_t
*nv
= fnvlist_alloc();
3620 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3621 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3624 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3625 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3631 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3633 if (ZFS_IS_VOLUME(zhp
)) {
3634 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3636 zc
.zc_objset_type
= DMU_OST_ZFS
;
3639 zc
.zc_defer_destroy
= defer
;
3640 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3642 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3643 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3647 remove_mountpoint(zhp
);
3652 struct destroydata
{
3654 const char *snapname
;
3658 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3660 struct destroydata
*dd
= arg
;
3661 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3664 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3665 dd
->snapname
) >= sizeof (name
))
3668 if (lzc_exists(name
))
3669 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3671 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3677 * Destroys all snapshots with the given name in zhp & descendants.
3680 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3683 struct destroydata dd
= { 0 };
3685 dd
.snapname
= snapname
;
3686 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3687 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3689 if (nvlist_empty(dd
.nvl
)) {
3690 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3691 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3692 zhp
->zfs_name
, snapname
);
3694 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3696 nvlist_free(dd
.nvl
);
3701 * Destroys all the snapshots named in the nvlist.
3704 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3707 nvlist_t
*errlist
= NULL
;
3710 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3713 nvlist_free(errlist
);
3717 if (nvlist_empty(errlist
)) {
3719 (void) snprintf(errbuf
, sizeof (errbuf
),
3720 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3722 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3724 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3725 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3727 (void) snprintf(errbuf
, sizeof (errbuf
),
3728 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3731 switch (fnvpair_value_int32(pair
)) {
3734 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3735 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3738 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3743 nvlist_free(errlist
);
3748 * Clones the given dataset. The target must be of the same type as the source.
3751 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3753 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3756 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3759 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3761 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3762 "cannot create '%s'"), target
);
3764 /* validate the target/clone name */
3765 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3766 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3768 /* validate parents exist */
3769 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3772 (void) parent_name(target
, parent
, sizeof (parent
));
3778 if (ZFS_IS_VOLUME(zhp
)) {
3779 type
= ZFS_TYPE_VOLUME
;
3781 type
= ZFS_TYPE_FILESYSTEM
;
3783 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3784 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
3788 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
3790 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3793 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3801 * The parent doesn't exist. We should have caught this
3802 * above, but there may a race condition that has since
3803 * destroyed the parent.
3805 * At this point, we don't know whether it's the source
3806 * that doesn't exist anymore, or whether the target
3807 * dataset doesn't exist.
3809 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3810 "no such parent '%s'"), parent
);
3811 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3814 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3815 "source and target pools differ"));
3816 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3820 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3829 * Promotes the given clone fs to be the clone parent.
3832 zfs_promote(zfs_handle_t
*zhp
)
3834 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3835 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3839 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3840 "cannot promote '%s'"), zhp
->zfs_name
);
3842 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3843 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3844 "snapshots can not be promoted"));
3845 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3848 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
3849 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3850 "not a cloned filesystem"));
3851 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3854 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
3855 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3857 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
3862 /* There is a conflicting snapshot name. */
3863 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3864 "conflicting snapshot '%s' from parent '%s'"),
3865 snapname
, zhp
->zfs_dmustats
.dds_origin
);
3866 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3869 return (zfs_standard_error(hdl
, ret
, errbuf
));
3875 typedef struct snapdata
{
3877 const char *sd_snapname
;
3881 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3883 snapdata_t
*sd
= arg
;
3884 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3887 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3888 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
3889 sd
->sd_snapname
) >= sizeof (name
))
3892 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3894 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3902 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3906 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3912 zpool_handle_t
*zpool_hdl
;
3913 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
3915 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3916 "cannot create snapshots "));
3919 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3920 const char *snapname
= nvpair_name(elem
);
3922 /* validate the target name */
3923 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3925 (void) snprintf(errbuf
, sizeof (errbuf
),
3926 dgettext(TEXT_DOMAIN
,
3927 "cannot create snapshot '%s'"), snapname
);
3928 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3933 * get pool handle for prop validation. assumes all snaps are in the
3934 * same pool, as does lzc_snapshot (below).
3936 elem
= nvlist_next_nvpair(snaps
, NULL
);
3937 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
3938 pool
[strcspn(pool
, "/@")] = '\0';
3939 zpool_hdl
= zpool_open(hdl
, pool
);
3940 if (zpool_hdl
== NULL
)
3943 if (props
!= NULL
&&
3944 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3945 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
3946 zpool_close(zpool_hdl
);
3949 zpool_close(zpool_hdl
);
3951 ret
= lzc_snapshot(snaps
, props
, &errors
);
3954 boolean_t printed
= B_FALSE
;
3955 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3957 elem
= nvlist_next_nvpair(errors
, elem
)) {
3958 (void) snprintf(errbuf
, sizeof (errbuf
),
3959 dgettext(TEXT_DOMAIN
,
3960 "cannot create snapshot '%s'"), nvpair_name(elem
));
3961 (void) zfs_standard_error(hdl
,
3962 fnvpair_value_int32(elem
), errbuf
);
3968 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3969 "multiple snapshots of same "
3971 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3975 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3981 nvlist_free(errors
);
3986 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3990 snapdata_t sd
= { 0 };
3991 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
3996 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3997 "cannot snapshot %s"), path
);
3999 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4000 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4002 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4003 cp
= strchr(fsname
, '@');
4005 sd
.sd_snapname
= cp
+ 1;
4007 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4008 ZFS_TYPE_VOLUME
)) == NULL
) {
4012 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4014 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4016 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4019 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4020 nvlist_free(sd
.sd_nvl
);
4026 * Destroy any more recent snapshots. We invoke this callback on any dependents
4027 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4028 * is a dependent and we should just destroy it without checking the transaction
4031 typedef struct rollback_data
{
4032 const char *cb_target
; /* the snapshot */
4033 uint64_t cb_create
; /* creation time reference */
4039 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4041 rollback_data_t
*cbp
= data
;
4042 prop_changelist_t
*clp
;
4044 /* We must destroy this clone; first unmount it */
4045 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4046 cbp
->cb_force
? MS_FORCE
: 0);
4047 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4048 cbp
->cb_error
= B_TRUE
;
4052 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4053 cbp
->cb_error
= B_TRUE
;
4055 changelist_remove(clp
, zhp
->zfs_name
);
4056 (void) changelist_postfix(clp
);
4057 changelist_free(clp
);
4064 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4066 rollback_data_t
*cbp
= data
;
4068 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4069 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4070 rollback_destroy_dependent
, cbp
);
4072 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4080 * Given a dataset, rollback to a specific snapshot, discarding any
4081 * data changes since then and making it the active dataset.
4083 * Any snapshots and bookmarks more recent than the target are
4084 * destroyed, along with their dependents (i.e. clones).
4087 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4089 rollback_data_t cb
= { 0 };
4091 boolean_t restore_resv
= 0;
4092 uint64_t old_volsize
= 0, new_volsize
;
4093 zfs_prop_t resv_prop
= { 0 };
4095 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4096 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4099 * Destroy all recent snapshots and their dependents.
4101 cb
.cb_force
= force
;
4102 cb
.cb_target
= snap
->zfs_name
;
4103 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4104 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
4105 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4111 * Now that we have verified that the snapshot is the latest,
4112 * rollback to the given snapshot.
4115 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4116 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4118 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4120 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4124 * Pass both the filesystem and the wanted snapshot names,
4125 * we would get an error back if the snapshot is destroyed or
4126 * a new snapshot is created before this request is processed.
4128 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4130 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4131 "'%s' is not the latest snapshot"), snap
->zfs_name
);
4132 (void) zfs_error_fmt(zhp
->zfs_hdl
, EZFS_BUSY
,
4133 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4136 } else if (err
!= 0) {
4137 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
4138 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4144 * For volumes, if the pre-rollback volsize matched the pre-
4145 * rollback reservation and the volsize has changed then set
4146 * the reservation property to the post-rollback volsize.
4147 * Make a new handle since the rollback closed the dataset.
4149 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4150 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4152 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4153 if (old_volsize
!= new_volsize
)
4154 err
= zfs_prop_set_int(zhp
, resv_prop
,
4163 * Renames the given dataset.
4166 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4167 boolean_t force_unmount
)
4170 zfs_cmd_t zc
= {"\0"};
4172 prop_changelist_t
*cl
= NULL
;
4173 zfs_handle_t
*zhrp
= NULL
;
4174 char *parentname
= NULL
;
4175 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4176 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4179 /* if we have the same exact name, just return success */
4180 if (strcmp(zhp
->zfs_name
, target
) == 0)
4183 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4184 "cannot rename to '%s'"), target
);
4186 /* make sure source name is valid */
4187 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4188 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4191 * Make sure the target name is valid
4193 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4194 if ((strchr(target
, '@') == NULL
) ||
4197 * Snapshot target name is abbreviated,
4198 * reconstruct full dataset name
4200 (void) strlcpy(parent
, zhp
->zfs_name
,
4202 delim
= strchr(parent
, '@');
4203 if (strchr(target
, '@') == NULL
)
4207 (void) strlcat(parent
, target
, sizeof (parent
));
4211 * Make sure we're renaming within the same dataset.
4213 delim
= strchr(target
, '@');
4214 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4215 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4216 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4217 "snapshots must be part of same "
4219 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4223 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4224 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4227 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4228 "recursive rename must be a snapshot"));
4229 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4232 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4233 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4235 /* validate parents */
4236 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4239 /* make sure we're in the same pool */
4240 verify((delim
= strchr(target
, '/')) != NULL
);
4241 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4242 zhp
->zfs_name
[delim
- target
] != '/') {
4243 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4244 "datasets must be within same pool"));
4245 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4248 /* new name cannot be a child of the current dataset name */
4249 if (is_descendant(zhp
->zfs_name
, target
)) {
4250 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4251 "New dataset name cannot be a descendant of "
4252 "current dataset name"));
4253 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4257 (void) snprintf(errbuf
, sizeof (errbuf
),
4258 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4260 if (getzoneid() == GLOBAL_ZONEID
&&
4261 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4262 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4263 "dataset is used in a non-global zone"));
4264 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4268 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4269 if (parentname
== NULL
) {
4273 delim
= strchr(parentname
, '@');
4275 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4280 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4281 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4282 force_unmount
? MS_FORCE
: 0)) == NULL
)
4285 if (changelist_haszonedchild(cl
)) {
4286 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4287 "child dataset with inherited mountpoint is used "
4288 "in a non-global zone"));
4289 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4294 if ((ret
= changelist_prefix(cl
)) != 0)
4298 if (ZFS_IS_VOLUME(zhp
))
4299 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4301 zc
.zc_objset_type
= DMU_OST_ZFS
;
4303 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4304 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4306 zc
.zc_cookie
= recursive
;
4308 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4310 * if it was recursive, the one that actually failed will
4313 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4314 "cannot rename '%s'"), zc
.zc_name
);
4316 if (recursive
&& errno
== EEXIST
) {
4317 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4318 "a child dataset already has a snapshot "
4319 "with the new name"));
4320 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4321 } else if (errno
== EACCES
) {
4322 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) ==
4324 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4325 "cannot rename an unencrypted dataset to "
4326 "be a decendent of an encrypted one"));
4328 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4329 "cannot move encryption child outside of "
4330 "its encryption root"));
4332 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4334 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4338 * On failure, we still want to remount any filesystems that
4339 * were previously mounted, so we don't alter the system state.
4342 (void) changelist_postfix(cl
);
4345 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4346 ret
= changelist_postfix(cl
);
4351 if (parentname
!= NULL
) {
4358 changelist_free(cl
);
4364 zfs_get_all_props(zfs_handle_t
*zhp
)
4366 return (zhp
->zfs_props
);
4370 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4372 if (zhp
->zfs_recvd_props
== NULL
)
4373 if (get_recvd_props_ioctl(zhp
) != 0)
4375 return (zhp
->zfs_recvd_props
);
4379 zfs_get_user_props(zfs_handle_t
*zhp
)
4381 return (zhp
->zfs_user_props
);
4385 * This function is used by 'zfs list' to determine the exact set of columns to
4386 * display, and their maximum widths. This does two main things:
4388 * - If this is a list of all properties, then expand the list to include
4389 * all native properties, and set a flag so that for each dataset we look
4390 * for new unique user properties and add them to the list.
4392 * - For non fixed-width properties, keep track of the maximum width seen
4393 * so that we can size the column appropriately. If the user has
4394 * requested received property values, we also need to compute the width
4395 * of the RECEIVED column.
4398 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4401 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4402 zprop_list_t
*entry
;
4403 zprop_list_t
**last
, **start
;
4404 nvlist_t
*userprops
, *propval
;
4407 char buf
[ZFS_MAXPROPLEN
];
4409 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4412 userprops
= zfs_get_user_props(zhp
);
4415 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4417 * Go through and add any user properties as necessary. We
4418 * start by incrementing our list pointer to the first
4419 * non-native property.
4422 while (*start
!= NULL
) {
4423 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4425 start
= &(*start
)->pl_next
;
4429 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4431 * See if we've already found this property in our list.
4433 for (last
= start
; *last
!= NULL
;
4434 last
= &(*last
)->pl_next
) {
4435 if (strcmp((*last
)->pl_user_prop
,
4436 nvpair_name(elem
)) == 0)
4440 if (*last
== NULL
) {
4441 if ((entry
= zfs_alloc(hdl
,
4442 sizeof (zprop_list_t
))) == NULL
||
4443 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4444 nvpair_name(elem
)))) == NULL
) {
4449 entry
->pl_prop
= ZPROP_INVAL
;
4450 entry
->pl_width
= strlen(nvpair_name(elem
));
4451 entry
->pl_all
= B_TRUE
;
4458 * Now go through and check the width of any non-fixed columns
4460 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4461 if (entry
->pl_fixed
&& !literal
)
4464 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4465 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4466 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4467 if (strlen(buf
) > entry
->pl_width
)
4468 entry
->pl_width
= strlen(buf
);
4470 if (received
&& zfs_prop_get_recvd(zhp
,
4471 zfs_prop_to_name(entry
->pl_prop
),
4472 buf
, sizeof (buf
), literal
) == 0)
4473 if (strlen(buf
) > entry
->pl_recvd_width
)
4474 entry
->pl_recvd_width
= strlen(buf
);
4476 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4478 verify(nvlist_lookup_string(propval
,
4479 ZPROP_VALUE
, &strval
) == 0);
4480 if (strlen(strval
) > entry
->pl_width
)
4481 entry
->pl_width
= strlen(strval
);
4483 if (received
&& zfs_prop_get_recvd(zhp
,
4484 entry
->pl_user_prop
,
4485 buf
, sizeof (buf
), literal
) == 0)
4486 if (strlen(buf
) > entry
->pl_recvd_width
)
4487 entry
->pl_recvd_width
= strlen(buf
);
4495 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4501 * Keep a reference to the props-table against which we prune the
4504 zhp
->zfs_props_table
= props
;
4506 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4509 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4510 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4513 * User properties will result in ZPROP_INVAL, and since we
4514 * only know how to prune standard ZFS properties, we always
4515 * leave these in the list. This can also happen if we
4516 * encounter an unknown DSL property (when running older
4517 * software, for example).
4519 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4520 (void) nvlist_remove(zhp
->zfs_props
,
4521 nvpair_name(curr
), nvpair_type(curr
));
4527 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4528 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4530 zfs_cmd_t zc
= {"\0"};
4531 nvlist_t
*nvlist
= NULL
;
4534 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4535 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4536 zc
.zc_cookie
= (uint64_t)cmd
;
4538 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4539 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4540 (void) no_memory(hdl
);
4546 case ZFS_SMB_ACL_ADD
:
4547 case ZFS_SMB_ACL_REMOVE
:
4548 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4550 case ZFS_SMB_ACL_RENAME
:
4551 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4553 (void) no_memory(hdl
);
4556 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4558 (void) no_memory(hdl
);
4561 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4562 nvlist_free(nvlist
);
4566 case ZFS_SMB_ACL_PURGE
:
4571 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4572 nvlist_free(nvlist
);
4577 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4578 char *path
, char *resource
)
4580 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4585 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4586 char *path
, char *resource
)
4588 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4593 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4595 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4600 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4601 char *oldname
, char *newname
)
4603 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4608 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4609 zfs_userspace_cb_t func
, void *arg
)
4611 zfs_cmd_t zc
= {"\0"};
4612 zfs_useracct_t buf
[100];
4613 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4616 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4618 zc
.zc_objset_type
= type
;
4619 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4622 zfs_useracct_t
*zua
= buf
;
4624 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4625 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4628 if ((errno
== ENOTSUP
&&
4629 (type
== ZFS_PROP_USEROBJUSED
||
4630 type
== ZFS_PROP_GROUPOBJUSED
||
4631 type
== ZFS_PROP_USEROBJQUOTA
||
4632 type
== ZFS_PROP_GROUPOBJQUOTA
)))
4635 (void) snprintf(errbuf
, sizeof (errbuf
),
4636 dgettext(TEXT_DOMAIN
,
4637 "cannot get used/quota for %s"), zc
.zc_name
);
4638 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4640 if (zc
.zc_nvlist_dst_size
== 0)
4643 while (zc
.zc_nvlist_dst_size
> 0) {
4644 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4645 zua
->zu_space
)) != 0)
4648 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4657 const char *snapname
;
4659 boolean_t recursive
;
4664 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4666 struct holdarg
*ha
= arg
;
4667 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4670 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4671 ha
->snapname
) >= sizeof (name
))
4674 if (lzc_exists(name
))
4675 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4678 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4684 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4685 boolean_t recursive
, int cleanup_fd
)
4690 ha
.nvl
= fnvlist_alloc();
4691 ha
.snapname
= snapname
;
4693 ha
.recursive
= recursive
;
4694 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4696 if (nvlist_empty(ha
.nvl
)) {
4699 fnvlist_free(ha
.nvl
);
4701 (void) snprintf(errbuf
, sizeof (errbuf
),
4702 dgettext(TEXT_DOMAIN
,
4703 "cannot hold snapshot '%s@%s'"),
4704 zhp
->zfs_name
, snapname
);
4705 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4709 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4710 fnvlist_free(ha
.nvl
);
4716 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4720 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4725 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4728 /* There may be errors even in the success case. */
4729 fnvlist_free(errors
);
4733 if (nvlist_empty(errors
)) {
4734 /* no hold-specific errors */
4735 (void) snprintf(errbuf
, sizeof (errbuf
),
4736 dgettext(TEXT_DOMAIN
, "cannot hold"));
4739 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4740 "pool must be upgraded"));
4741 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4744 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4747 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4751 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4753 elem
= nvlist_next_nvpair(errors
, elem
)) {
4754 (void) snprintf(errbuf
, sizeof (errbuf
),
4755 dgettext(TEXT_DOMAIN
,
4756 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4757 switch (fnvpair_value_int32(elem
)) {
4760 * Temporary tags wind up having the ds object id
4761 * prepended. So even if we passed the length check
4762 * above, it's still possible for the tag to wind
4763 * up being slightly too long.
4765 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4768 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4771 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4774 (void) zfs_standard_error(hdl
,
4775 fnvpair_value_int32(elem
), errbuf
);
4779 fnvlist_free(errors
);
4784 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4786 struct holdarg
*ha
= arg
;
4787 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4789 nvlist_t
*existing_holds
;
4791 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4792 ha
->snapname
) >= sizeof (name
)) {
4797 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4799 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4802 nvlist_t
*torelease
= fnvlist_alloc();
4803 fnvlist_add_boolean(torelease
, ha
->tag
);
4804 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4805 fnvlist_free(torelease
);
4809 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4815 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4816 boolean_t recursive
)
4820 nvlist_t
*errors
= NULL
;
4822 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4825 ha
.nvl
= fnvlist_alloc();
4826 ha
.snapname
= snapname
;
4828 ha
.recursive
= recursive
;
4830 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4832 if (nvlist_empty(ha
.nvl
)) {
4833 fnvlist_free(ha
.nvl
);
4835 (void) snprintf(errbuf
, sizeof (errbuf
),
4836 dgettext(TEXT_DOMAIN
,
4837 "cannot release hold from snapshot '%s@%s'"),
4838 zhp
->zfs_name
, snapname
);
4840 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4842 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4847 ret
= lzc_release(ha
.nvl
, &errors
);
4848 fnvlist_free(ha
.nvl
);
4851 /* There may be errors even in the success case. */
4852 fnvlist_free(errors
);
4856 if (nvlist_empty(errors
)) {
4857 /* no hold-specific errors */
4858 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4862 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4863 "pool must be upgraded"));
4864 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4867 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4871 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4873 elem
= nvlist_next_nvpair(errors
, elem
)) {
4874 (void) snprintf(errbuf
, sizeof (errbuf
),
4875 dgettext(TEXT_DOMAIN
,
4876 "cannot release hold from snapshot '%s'"),
4878 switch (fnvpair_value_int32(elem
)) {
4880 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4883 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4886 (void) zfs_standard_error_fmt(hdl
,
4887 fnvpair_value_int32(elem
), errbuf
);
4891 fnvlist_free(errors
);
4896 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4898 zfs_cmd_t zc
= {"\0"};
4899 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4905 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4906 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4910 nvbuf
= malloc(nvsz
);
4911 if (nvbuf
== NULL
) {
4912 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4916 zc
.zc_nvlist_dst_size
= nvsz
;
4917 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4919 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4921 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4922 (void) snprintf(errbuf
, sizeof (errbuf
),
4923 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4928 nvsz
= zc
.zc_nvlist_dst_size
;
4932 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4933 "pool must be upgraded"));
4934 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4937 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4940 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4943 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4948 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4950 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4951 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4953 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4963 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4965 zfs_cmd_t zc
= {"\0"};
4966 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4972 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4973 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4975 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4978 nvbuf
= malloc(nvsz
);
4980 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4983 zc
.zc_nvlist_src_size
= nvsz
;
4984 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4985 zc
.zc_perm_action
= un
;
4987 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4989 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4990 (void) snprintf(errbuf
, sizeof (errbuf
),
4991 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4995 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4996 "pool must be upgraded"));
4997 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5000 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5003 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5006 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5017 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5022 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5025 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5027 (void) snprintf(errbuf
, sizeof (errbuf
),
5028 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5032 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5033 "pool must be upgraded"));
5034 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5037 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5040 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5043 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5052 * Convert the zvol's volume size to an appropriate reservation.
5053 * Note: If this routine is updated, it is necessary to update the ZFS test
5054 * suite's shell version in reservation.kshlib.
5057 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
5060 uint64_t nblocks
, volblocksize
;
5064 if (nvlist_lookup_string(props
,
5065 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5066 ncopies
= atoi(strval
);
5069 if (nvlist_lookup_uint64(props
,
5070 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5071 &volblocksize
) != 0)
5072 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5073 nblocks
= volsize
/volblocksize
;
5074 /* start with metadnode L0-L6 */
5076 /* calculate number of indirects */
5077 while (nblocks
> 1) {
5078 nblocks
+= DNODES_PER_LEVEL
- 1;
5079 nblocks
/= DNODES_PER_LEVEL
;
5082 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5085 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5086 * compressed, but in practice they compress down to about
5089 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;