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) 2018, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017-2018 RackTop Systems.
33 * Copyright (c) 2018 Datto Inc.
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>
65 #include "zfs_namecheck.h"
67 #include "libzfs_impl.h"
69 #include "zfs_deleg.h"
71 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
72 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
75 * Given a single type (not a mask of types), return the type in a human
79 zfs_type_to_name(zfs_type_t type
)
82 case ZFS_TYPE_FILESYSTEM
:
83 return (dgettext(TEXT_DOMAIN
, "filesystem"));
84 case ZFS_TYPE_SNAPSHOT
:
85 return (dgettext(TEXT_DOMAIN
, "snapshot"));
87 return (dgettext(TEXT_DOMAIN
, "volume"));
89 return (dgettext(TEXT_DOMAIN
, "pool"));
90 case ZFS_TYPE_BOOKMARK
:
91 return (dgettext(TEXT_DOMAIN
, "bookmark"));
93 assert(!"unhandled zfs_type_t");
100 * Validate a ZFS path. This is used even before trying to open the dataset, to
101 * provide a more meaningful error message. We call zfs_error_aux() to
102 * explain exactly why the name was not valid.
105 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
111 if (entity_namecheck(path
, &why
, &what
) != 0) {
114 case NAME_ERR_TOOLONG
:
115 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
116 "name is too long"));
119 case NAME_ERR_LEADING_SLASH
:
120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
121 "leading slash in name"));
124 case NAME_ERR_EMPTY_COMPONENT
:
125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
126 "empty component in name"));
129 case NAME_ERR_TRAILING_SLASH
:
130 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
131 "trailing slash in name"));
134 case NAME_ERR_INVALCHAR
:
136 dgettext(TEXT_DOMAIN
, "invalid character "
137 "'%c' in name"), what
);
140 case NAME_ERR_MULTIPLE_DELIMITERS
:
141 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
142 "multiple '@' and/or '#' delimiters in "
146 case NAME_ERR_NOLETTER
:
147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
148 "pool doesn't begin with a letter"));
151 case NAME_ERR_RESERVED
:
152 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
153 "name is reserved"));
156 case NAME_ERR_DISKLIKE
:
157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
158 "reserved disk name"));
162 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
163 "(%d) not defined"), why
);
171 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
173 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
174 "snapshot delimiter '@' is not expected here"));
178 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
180 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
181 "missing '@' delimiter in snapshot name, "
182 "did you mean to use -r?"));
186 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
188 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
189 "bookmark delimiter '#' is not expected here"));
193 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
195 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
196 "missing '#' delimiter in bookmark name, "
197 "did you mean to use -r?"));
201 if (modifying
&& strchr(path
, '%') != NULL
) {
203 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
204 "invalid character %c in name"), '%');
212 zfs_name_valid(const char *name
, zfs_type_t type
)
214 if (type
== ZFS_TYPE_POOL
)
215 return (zpool_name_valid(NULL
, B_FALSE
, name
));
216 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
220 * This function takes the raw DSL properties, and filters out the user-defined
221 * properties into a separate nvlist.
224 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
226 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
231 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
232 (void) no_memory(hdl
);
237 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
238 if (!zfs_prop_user(nvpair_name(elem
)))
241 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
242 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
244 (void) no_memory(hdl
);
252 static zpool_handle_t
*
253 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
255 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
258 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
259 if (hdl
->libzfs_pool_handles
!= NULL
)
260 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
261 hdl
->libzfs_pool_handles
= zph
;
266 static zpool_handle_t
*
267 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
269 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
270 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
272 while ((zph
!= NULL
) &&
273 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
274 zph
= zph
->zpool_next
;
279 * Returns a handle to the pool that contains the provided dataset.
280 * If a handle to that pool already exists then that handle is returned.
281 * Otherwise, a new handle is created and added to the list of handles.
283 static zpool_handle_t
*
284 zpool_handle(zfs_handle_t
*zhp
)
290 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
291 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
292 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
294 zph
= zpool_find_handle(zhp
, pool_name
, len
);
296 zph
= zpool_add_handle(zhp
, pool_name
);
303 zpool_free_handles(libzfs_handle_t
*hdl
)
305 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
307 while (zph
!= NULL
) {
308 next
= zph
->zpool_next
;
312 hdl
->libzfs_pool_handles
= NULL
;
316 * Utility function to gather stats (objset and zpl) for the given object.
319 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
321 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
323 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
325 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
326 if (errno
== ENOMEM
) {
327 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
338 * Utility function to get the received properties of the given object.
341 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
343 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
344 nvlist_t
*recvdprops
;
345 zfs_cmd_t zc
= {"\0"};
348 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
351 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
353 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
354 if (errno
== ENOMEM
) {
355 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
359 zcmd_free_nvlists(&zc
);
364 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
365 zcmd_free_nvlists(&zc
);
369 nvlist_free(zhp
->zfs_recvd_props
);
370 zhp
->zfs_recvd_props
= recvdprops
;
376 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
378 nvlist_t
*allprops
, *userprops
;
380 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
382 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
387 * XXX Why do we store the user props separately, in addition to
388 * storing them in zfs_props?
390 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
391 nvlist_free(allprops
);
395 nvlist_free(zhp
->zfs_props
);
396 nvlist_free(zhp
->zfs_user_props
);
398 zhp
->zfs_props
= allprops
;
399 zhp
->zfs_user_props
= userprops
;
405 get_stats(zfs_handle_t
*zhp
)
408 zfs_cmd_t zc
= {"\0"};
410 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
412 if (get_stats_ioctl(zhp
, &zc
) != 0)
414 else if (put_stats_zhdl(zhp
, &zc
) != 0)
416 zcmd_free_nvlists(&zc
);
421 * Refresh the properties currently stored in the handle.
424 zfs_refresh_properties(zfs_handle_t
*zhp
)
426 (void) get_stats(zhp
);
430 * Makes a handle from the given dataset name. Used by zfs_open() and
431 * zfs_iter_* to create child handles on the fly.
434 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
436 if (put_stats_zhdl(zhp
, zc
) != 0)
440 * We've managed to open the dataset and gather statistics. Determine
441 * the high-level type.
443 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
444 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
445 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
446 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
447 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
452 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
453 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
454 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
455 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
456 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
457 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
459 abort(); /* we should never see any other types */
461 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
468 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
470 zfs_cmd_t zc
= {"\0"};
472 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
478 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
479 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
483 if (get_stats_ioctl(zhp
, &zc
) == -1) {
484 zcmd_free_nvlists(&zc
);
488 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
492 zcmd_free_nvlists(&zc
);
497 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
499 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
505 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
506 if (make_dataset_handle_common(zhp
, zc
) == -1) {
514 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
516 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
521 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
522 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
523 zhp
->zfs_head_type
= pzhp
->zfs_type
;
524 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
525 zhp
->zpool_hdl
= zpool_handle(zhp
);
531 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
533 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
538 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
539 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
540 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
541 sizeof (zhp
->zfs_name
));
542 zhp
->zfs_type
= zhp_orig
->zfs_type
;
543 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
544 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
545 if (zhp_orig
->zfs_props
!= NULL
) {
546 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
547 (void) no_memory(zhp
->zfs_hdl
);
552 if (zhp_orig
->zfs_user_props
!= NULL
) {
553 if (nvlist_dup(zhp_orig
->zfs_user_props
,
554 &zhp
->zfs_user_props
, 0) != 0) {
555 (void) no_memory(zhp
->zfs_hdl
);
560 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
561 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
562 &zhp
->zfs_recvd_props
, 0)) {
563 (void) no_memory(zhp
->zfs_hdl
);
568 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
569 if (zhp_orig
->zfs_mntopts
!= NULL
) {
570 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
571 zhp_orig
->zfs_mntopts
);
573 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
578 zfs_bookmark_exists(const char *path
)
582 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
589 (void) strlcpy(fsname
, path
, sizeof (fsname
));
590 pound
= strchr(fsname
, '#');
595 bmark_name
= pound
+ 1;
596 props
= fnvlist_alloc();
597 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
604 rv
= nvlist_exists(bmarks
, bmark_name
);
610 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
611 nvlist_t
*bmark_props
)
613 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
618 /* Fill in the name. */
619 zhp
->zfs_hdl
= parent
->zfs_hdl
;
620 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
622 /* Set the property lists. */
623 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
629 zhp
->zfs_head_type
= parent
->zfs_head_type
;
630 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
632 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
633 nvlist_free(zhp
->zfs_props
);
641 struct zfs_open_bookmarks_cb_data
{
647 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
649 struct zfs_open_bookmarks_cb_data
*dp
= data
;
652 * Is it the one we are looking for?
654 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
656 * We found it. Save it and let the caller know we are done.
663 * Not found. Close the handle and ask for another one.
670 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
671 * argument is a mask of acceptable types. The function will print an
672 * appropriate error message and return NULL if it can't be opened.
675 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
681 (void) snprintf(errbuf
, sizeof (errbuf
),
682 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
685 * Validate the name before we even try to open it.
687 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
688 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
693 * Bookmarks needs to be handled separately.
695 bookp
= strchr(path
, '#');
698 * Try to get stats for the dataset, which will tell us if it
702 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
703 (void) zfs_standard_error(hdl
, errno
, errbuf
);
707 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
709 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
712 * We need to cut out '#' and everything after '#'
713 * to get the parent dataset name only.
715 assert(bookp
- path
< sizeof (dsname
));
716 (void) strncpy(dsname
, path
, bookp
- path
);
717 dsname
[bookp
- path
] = '\0';
720 * Create handle for the parent dataset.
723 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
724 (void) zfs_standard_error(hdl
, errno
, errbuf
);
729 * Iterate bookmarks to find the right one.
732 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
733 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
734 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
738 if (cb_data
.zhp
== NULL
) {
739 (void) zfs_standard_error(hdl
, errno
, errbuf
);
751 if (!(types
& zhp
->zfs_type
)) {
752 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
761 * Release a ZFS handle. Nothing to do but free the associated memory.
764 zfs_close(zfs_handle_t
*zhp
)
766 if (zhp
->zfs_mntopts
)
767 free(zhp
->zfs_mntopts
);
768 nvlist_free(zhp
->zfs_props
);
769 nvlist_free(zhp
->zfs_user_props
);
770 nvlist_free(zhp
->zfs_recvd_props
);
774 typedef struct mnttab_node
{
775 struct mnttab mtn_mt
;
780 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
782 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
783 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
786 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
788 return (AVL_ISIGN(rv
));
792 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
794 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
795 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
796 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
797 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
801 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
805 /* Reopen MNTTAB to prevent reading stale data from open file */
806 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
809 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
813 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
816 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
817 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
818 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
819 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
820 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
822 /* Exclude duplicate mounts */
823 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
824 free(mtn
->mtn_mt
.mnt_special
);
825 free(mtn
->mtn_mt
.mnt_mountp
);
826 free(mtn
->mtn_mt
.mnt_fstype
);
827 free(mtn
->mtn_mt
.mnt_mntopts
);
832 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
839 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
844 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
846 free(mtn
->mtn_mt
.mnt_special
);
847 free(mtn
->mtn_mt
.mnt_mountp
);
848 free(mtn
->mtn_mt
.mnt_fstype
);
849 free(mtn
->mtn_mt
.mnt_mntopts
);
852 avl_destroy(&hdl
->libzfs_mnttab_cache
);
853 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
857 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
859 hdl
->libzfs_mnttab_enable
= enable
;
863 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
864 struct mnttab
*entry
)
870 if (!hdl
->libzfs_mnttab_enable
) {
871 struct mnttab srch
= { 0 };
873 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
874 libzfs_mnttab_fini(hdl
);
876 /* Reopen MNTTAB to prevent reading stale data from open file */
877 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
880 srch
.mnt_special
= (char *)fsname
;
881 srch
.mnt_fstype
= MNTTYPE_ZFS
;
882 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
888 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
889 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
892 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
893 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
898 find
.mtn_mt
.mnt_special
= (char *)fsname
;
899 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
901 *entry
= mtn
->mtn_mt
;
904 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
909 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
910 const char *mountp
, const char *mntopts
)
914 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
915 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
916 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
917 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
918 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
919 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
920 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
922 * Another thread may have already added this entry
923 * via libzfs_mnttab_update. If so we should skip it.
925 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
)
928 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
930 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
934 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
939 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
940 find
.mtn_mt
.mnt_special
= (char *)fsname
;
941 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
943 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
944 free(ret
->mtn_mt
.mnt_special
);
945 free(ret
->mtn_mt
.mnt_mountp
);
946 free(ret
->mtn_mt
.mnt_fstype
);
947 free(ret
->mtn_mt
.mnt_mntopts
);
950 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
954 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
956 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
958 if (zpool_handle
== NULL
)
961 *spa_version
= zpool_get_prop_int(zpool_handle
,
962 ZPOOL_PROP_VERSION
, NULL
);
967 * The choice of reservation property depends on the SPA version.
970 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
974 if (zfs_spa_version(zhp
, &spa_version
) < 0)
977 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
978 *resv_prop
= ZFS_PROP_REFRESERVATION
;
980 *resv_prop
= ZFS_PROP_RESERVATION
;
986 * Given an nvlist of properties to set, validates that they are correct, and
987 * parses any numeric properties (index, boolean, etc) if they are specified as
991 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
992 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
993 boolean_t key_params_ok
, const char *errbuf
)
1000 int chosen_normal
= -1;
1001 int chosen_utf
= -1;
1003 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1004 (void) no_memory(hdl
);
1009 * Make sure this property is valid and applies to this type.
1013 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1014 const char *propname
= nvpair_name(elem
);
1016 prop
= zfs_name_to_prop(propname
);
1017 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
1019 * This is a user property: make sure it's a
1020 * string, and that it's less than ZAP_MAXNAMELEN.
1022 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1023 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1024 "'%s' must be a string"), propname
);
1025 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1029 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1030 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1031 "property name '%s' is too long"),
1033 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1037 (void) nvpair_value_string(elem
, &strval
);
1038 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1039 (void) no_memory(hdl
);
1046 * Currently, only user properties can be modified on
1049 if (type
== ZFS_TYPE_SNAPSHOT
) {
1050 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1051 "this property can not be modified for snapshots"));
1052 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1056 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1057 zfs_userquota_prop_t uqtype
;
1058 char *newpropname
= NULL
;
1064 if (userquota_propname_decode(propname
, zoned
,
1065 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1067 dgettext(TEXT_DOMAIN
,
1068 "'%s' has an invalid user/group name"),
1070 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1074 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1075 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1076 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1077 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1078 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1079 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1081 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1083 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1088 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1089 (void) nvpair_value_string(elem
, &strval
);
1090 if (strcmp(strval
, "none") == 0) {
1092 } else if (zfs_nicestrtonum(hdl
,
1093 strval
, &intval
) != 0) {
1094 (void) zfs_error(hdl
,
1095 EZFS_BADPROP
, errbuf
);
1098 } else if (nvpair_type(elem
) ==
1100 (void) nvpair_value_uint64(elem
, &intval
);
1102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1103 "use 'none' to disable "
1104 "{user|group|project}quota"));
1108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1109 "'%s' must be a number"), propname
);
1110 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1115 * Encode the prop name as
1116 * userquota@<hex-rid>-domain, to make it easy
1117 * for the kernel to decode.
1119 rc
= asprintf(&newpropname
, "%s%llx-%s",
1120 zfs_userquota_prop_prefixes
[uqtype
],
1121 (longlong_t
)rid
, domain
);
1122 if (rc
== -1 || newpropname
== NULL
) {
1123 (void) no_memory(hdl
);
1130 if (nvlist_add_uint64_array(ret
, newpropname
,
1133 (void) no_memory(hdl
);
1138 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1140 "'%s' is readonly"),
1142 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1146 if (prop
== ZPROP_INVAL
) {
1147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1148 "invalid property '%s'"), propname
);
1149 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1153 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1155 dgettext(TEXT_DOMAIN
, "'%s' does not "
1156 "apply to datasets of this type"), propname
);
1157 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1161 if (zfs_prop_readonly(prop
) &&
1162 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1163 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1165 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1167 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1171 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1172 &strval
, &intval
, errbuf
) != 0)
1176 * Perform some additional checks for specific properties.
1179 case ZFS_PROP_VERSION
:
1185 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1186 if (intval
< version
) {
1187 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1188 "Can not downgrade; already at version %u"),
1190 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1196 case ZFS_PROP_VOLBLOCKSIZE
:
1197 case ZFS_PROP_RECORDSIZE
:
1199 int maxbs
= SPA_MAXBLOCKSIZE
;
1202 if (zpool_hdl
!= NULL
) {
1203 maxbs
= zpool_get_prop_int(zpool_hdl
,
1204 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1207 * The value must be a power of two between
1208 * SPA_MINBLOCKSIZE and maxbs.
1210 if (intval
< SPA_MINBLOCKSIZE
||
1211 intval
> maxbs
|| !ISP2(intval
)) {
1212 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1213 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1214 "'%s' must be power of 2 from 512B "
1215 "to %s"), propname
, buf
);
1216 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1222 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1223 if (zpool_hdl
!= NULL
) {
1224 char state
[64] = "";
1227 * Issue a warning but do not fail so that
1228 * tests for setable properties succeed.
1230 if (zpool_prop_get_feature(zpool_hdl
,
1231 "feature@allocation_classes", state
,
1232 sizeof (state
)) != 0 ||
1233 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1234 (void) fprintf(stderr
, gettext(
1235 "%s: property requires a special "
1236 "device in the pool\n"), propname
);
1240 (intval
< SPA_MINBLOCKSIZE
||
1241 intval
> SPA_OLD_MAXBLOCKSIZE
|| !ISP2(intval
))) {
1242 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1243 "invalid '%s=%d' property: must be zero or "
1244 "a power of 2 from 512B to 128K"), propname
,
1246 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1251 case ZFS_PROP_MLSLABEL
:
1253 #ifdef HAVE_MLSLABEL
1255 * Verify the mlslabel string and convert to
1256 * internal hex label string.
1260 char *hex
= NULL
; /* internal label string */
1262 /* Default value is already OK. */
1263 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1266 /* Verify the label can be converted to binary form */
1267 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1268 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1269 L_NO_CORRECTION
, NULL
) == -1)) {
1273 /* Now translate to hex internal label string */
1274 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1280 m_label_free(new_sl
);
1282 /* If string is already in internal form, we're done. */
1283 if (strcmp(strval
, hex
) == 0) {
1288 /* Replace the label string with the internal form. */
1289 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1291 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1298 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1299 "invalid mlslabel '%s'"), strval
);
1300 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1301 m_label_free(new_sl
); /* OK if null */
1304 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1305 "mlslabels are unsupported"));
1306 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1308 #endif /* HAVE_MLSLABEL */
1311 case ZFS_PROP_MOUNTPOINT
:
1313 namecheck_err_t why
;
1315 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1316 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1319 if (mountpoint_namecheck(strval
, &why
)) {
1321 case NAME_ERR_LEADING_SLASH
:
1323 dgettext(TEXT_DOMAIN
,
1324 "'%s' must be an absolute path, "
1325 "'none', or 'legacy'"), propname
);
1327 case NAME_ERR_TOOLONG
:
1329 dgettext(TEXT_DOMAIN
,
1330 "component of '%s' is too long"),
1336 dgettext(TEXT_DOMAIN
,
1337 "(%d) not defined"),
1341 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1348 case ZFS_PROP_SHARESMB
:
1349 case ZFS_PROP_SHARENFS
:
1351 * For the mountpoint and sharenfs or sharesmb
1352 * properties, check if it can be set in a
1353 * global/non-global zone based on
1354 * the zoned property value:
1356 * global zone non-global zone
1357 * --------------------------------------------------
1358 * zoned=on mountpoint (no) mountpoint (yes)
1359 * sharenfs (no) sharenfs (no)
1360 * sharesmb (no) sharesmb (no)
1362 * zoned=off mountpoint (yes) N/A
1367 if (getzoneid() == GLOBAL_ZONEID
) {
1368 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1369 "'%s' cannot be set on "
1370 "dataset in a non-global zone"),
1372 (void) zfs_error(hdl
, EZFS_ZONED
,
1375 } else if (prop
== ZFS_PROP_SHARENFS
||
1376 prop
== ZFS_PROP_SHARESMB
) {
1377 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1378 "'%s' cannot be set in "
1379 "a non-global zone"), propname
);
1380 (void) zfs_error(hdl
, EZFS_ZONED
,
1384 } else if (getzoneid() != GLOBAL_ZONEID
) {
1386 * If zoned property is 'off', this must be in
1387 * a global zone. If not, something is wrong.
1389 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1390 "'%s' cannot be set while dataset "
1391 "'zoned' property is set"), propname
);
1392 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1397 * At this point, it is legitimate to set the
1398 * property. Now we want to make sure that the
1399 * property value is valid if it is sharenfs.
1401 if ((prop
== ZFS_PROP_SHARENFS
||
1402 prop
== ZFS_PROP_SHARESMB
) &&
1403 strcmp(strval
, "on") != 0 &&
1404 strcmp(strval
, "off") != 0) {
1405 zfs_share_proto_t proto
;
1407 if (prop
== ZFS_PROP_SHARESMB
)
1413 * Must be an valid sharing protocol
1414 * option string so init the libshare
1415 * in order to enable the parser and
1416 * then parse the options. We use the
1417 * control API since we don't care about
1418 * the current configuration and don't
1419 * want the overhead of loading it
1420 * until we actually do something.
1423 if (zfs_init_libshare(hdl
,
1424 SA_INIT_CONTROL_API
) != SA_OK
) {
1426 * An error occurred so we can't do
1429 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1430 "'%s' cannot be set: problem "
1431 "in share initialization"),
1433 (void) zfs_error(hdl
, EZFS_BADPROP
,
1438 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1440 * There was an error in parsing so
1441 * deal with it by issuing an error
1442 * message and leaving after
1443 * uninitializing the the libshare
1446 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1447 "'%s' cannot be set to invalid "
1448 "options"), propname
);
1449 (void) zfs_error(hdl
, EZFS_BADPROP
,
1451 zfs_uninit_libshare(hdl
);
1454 zfs_uninit_libshare(hdl
);
1459 case ZFS_PROP_KEYLOCATION
:
1460 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1461 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1462 "invalid keylocation"));
1463 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1469 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1471 if (crypt
== ZIO_CRYPT_OFF
&&
1472 strcmp(strval
, "none") != 0) {
1473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1474 "keylocation must be 'none' "
1475 "for unencrypted datasets"));
1476 (void) zfs_error(hdl
, EZFS_BADPROP
,
1479 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1480 strcmp(strval
, "none") == 0) {
1481 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1482 "keylocation must not be 'none' "
1483 "for encrypted datasets"));
1484 (void) zfs_error(hdl
, EZFS_BADPROP
,
1491 case ZFS_PROP_PBKDF2_ITERS
:
1492 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1493 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1494 "minimum pbkdf2 iterations is %u"),
1495 MIN_PBKDF2_ITERATIONS
);
1496 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1501 case ZFS_PROP_UTF8ONLY
:
1502 chosen_utf
= (int)intval
;
1505 case ZFS_PROP_NORMALIZE
:
1506 chosen_normal
= (int)intval
;
1514 * For changes to existing volumes, we have some additional
1515 * checks to enforce.
1517 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1518 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1519 ZFS_PROP_VOLBLOCKSIZE
);
1523 case ZFS_PROP_VOLSIZE
:
1524 if (intval
% blocksize
!= 0) {
1525 zfs_nicebytes(blocksize
, buf
,
1527 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1528 "'%s' must be a multiple of "
1529 "volume block size (%s)"),
1531 (void) zfs_error(hdl
, EZFS_BADPROP
,
1537 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1538 "'%s' cannot be zero"),
1540 (void) zfs_error(hdl
, EZFS_BADPROP
,
1551 /* check encryption properties */
1553 int64_t crypt
= zfs_prop_get_int(zhp
,
1554 ZFS_PROP_ENCRYPTION
);
1557 case ZFS_PROP_COPIES
:
1558 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1559 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1560 "encrypted datasets cannot have "
1562 (void) zfs_error(hdl
, EZFS_BADPROP
,
1574 * If normalization was chosen, but no UTF8 choice was made,
1575 * enforce rejection of non-UTF8 names.
1577 * If normalization was chosen, but rejecting non-UTF8 names
1578 * was explicitly not chosen, it is an error.
1580 if (chosen_normal
> 0 && chosen_utf
< 0) {
1581 if (nvlist_add_uint64(ret
,
1582 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1583 (void) no_memory(hdl
);
1586 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1587 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1588 "'%s' must be set 'on' if normalization chosen"),
1589 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1590 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1601 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1603 uint64_t old_volsize
;
1604 uint64_t new_volsize
;
1605 uint64_t old_reservation
;
1606 uint64_t new_reservation
;
1607 zfs_prop_t resv_prop
;
1611 * If this is an existing volume, and someone is setting the volsize,
1612 * make sure that it matches the reservation, or add it if necessary.
1614 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1615 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1617 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1619 props
= fnvlist_alloc();
1620 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1621 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1623 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1624 old_reservation
) || nvlist_exists(nvl
,
1625 zfs_prop_to_name(resv_prop
))) {
1626 fnvlist_free(props
);
1629 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1630 &new_volsize
) != 0) {
1631 fnvlist_free(props
);
1634 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1635 fnvlist_free(props
);
1637 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1638 new_reservation
) != 0) {
1639 (void) no_memory(zhp
->zfs_hdl
);
1646 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1647 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1648 * Return codes must match zfs_add_synthetic_resv().
1651 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1658 if (!ZFS_IS_VOLUME(zhp
)) {
1662 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1666 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1670 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1671 /* No value being set, so it can't be "auto" */
1674 if (resvsize
!= UINT64_MAX
) {
1675 /* Being set to a value other than "auto" */
1679 props
= fnvlist_alloc();
1681 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1682 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1684 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1686 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1689 resvsize
= zvol_volsize_to_reservation(volsize
, props
);
1690 fnvlist_free(props
);
1692 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1693 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1694 (void) no_memory(zhp
->zfs_hdl
);
1701 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1708 * For quotas and reservations, ENOSPC indicates
1709 * something different; setting a quota or reservation
1710 * doesn't use any disk space.
1713 case ZFS_PROP_QUOTA
:
1714 case ZFS_PROP_REFQUOTA
:
1715 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1716 "size is less than current used or "
1718 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1721 case ZFS_PROP_RESERVATION
:
1722 case ZFS_PROP_REFRESERVATION
:
1723 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1724 "size is greater than available space"));
1725 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1729 (void) zfs_standard_error(hdl
, err
, errbuf
);
1735 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1739 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1744 "property value too long"));
1745 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1749 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1750 "pool and or dataset must be upgraded to set this "
1751 "property or value"));
1752 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1756 if (prop
== ZFS_PROP_COMPRESSION
||
1757 prop
== ZFS_PROP_DNODESIZE
||
1758 prop
== ZFS_PROP_RECORDSIZE
) {
1759 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1760 "property setting is not allowed on "
1761 "bootable datasets"));
1762 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1763 } else if (prop
== ZFS_PROP_CHECKSUM
||
1764 prop
== ZFS_PROP_DEDUP
) {
1765 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1766 "property setting is not allowed on "
1768 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1770 (void) zfs_standard_error(hdl
, err
, errbuf
);
1775 if (prop
== ZPROP_INVAL
) {
1776 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1778 (void) zfs_standard_error(hdl
, err
, errbuf
);
1783 if (prop
== ZFS_PROP_KEYLOCATION
) {
1784 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1785 "keylocation may only be set on encryption roots"));
1786 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1788 (void) zfs_standard_error(hdl
, err
, errbuf
);
1794 * This platform can't address a volume this big.
1797 if (prop
== ZFS_PROP_VOLSIZE
) {
1798 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1804 (void) zfs_standard_error(hdl
, err
, errbuf
);
1809 zfs_is_namespace_prop(zfs_prop_t prop
)
1813 case ZFS_PROP_ATIME
:
1814 case ZFS_PROP_RELATIME
:
1815 case ZFS_PROP_DEVICES
:
1817 case ZFS_PROP_SETUID
:
1818 case ZFS_PROP_READONLY
:
1819 case ZFS_PROP_XATTR
:
1820 case ZFS_PROP_NBMAND
:
1829 * Given a property name and value, set the property for the given dataset.
1832 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1836 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1837 nvlist_t
*nvl
= NULL
;
1839 (void) snprintf(errbuf
, sizeof (errbuf
),
1840 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1843 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1844 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1845 (void) no_memory(hdl
);
1849 ret
= zfs_prop_set_list(zhp
, nvl
);
1859 * Given an nvlist of property names and values, set the properties for the
1863 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1865 zfs_cmd_t zc
= {"\0"};
1867 prop_changelist_t
**cls
= NULL
;
1870 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1874 zfs_prop_t prop
= 0;
1877 (void) snprintf(errbuf
, sizeof (errbuf
),
1878 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1881 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1882 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1883 B_FALSE
, errbuf
)) == NULL
)
1887 * We have to check for any extra properties which need to be added
1888 * before computing the length of the nvlist.
1890 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1892 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1893 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1894 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1899 if (added_resv
!= 1 &&
1900 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1905 * Check how many properties we're setting and allocate an array to
1906 * store changelist pointers for postfix().
1908 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1910 elem
= nvlist_next_nvpair(nvl
, elem
))
1912 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1916 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1918 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1920 prop
= zfs_name_to_prop(nvpair_name(elem
));
1922 assert(cl_idx
< nvl_len
);
1924 * We don't want to unmount & remount the dataset when changing
1925 * its canmount property to 'on' or 'noauto'. We only use
1926 * the changelist logic to unmount when setting canmount=off.
1928 if (prop
!= ZFS_PROP_CANMOUNT
||
1929 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1930 zfs_is_mounted(zhp
, NULL
))) {
1931 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1932 if (cls
[cl_idx
] == NULL
)
1936 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1937 changelist_haszonedchild(cls
[cl_idx
])) {
1938 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1939 "child dataset with inherited mountpoint is used "
1940 "in a non-global zone"));
1941 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1945 if (cls
[cl_idx
] != NULL
&&
1946 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1951 assert(cl_idx
== nvl_len
);
1954 * Execute the corresponding ioctl() to set this list of properties.
1956 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1958 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1959 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1962 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1965 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1966 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1970 /* Get the list of unset properties back and report them. */
1971 nvlist_t
*errorprops
= NULL
;
1972 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1974 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1976 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1977 prop
= zfs_name_to_prop(nvpair_name(elem
));
1978 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1980 nvlist_free(errorprops
);
1982 if (added_resv
&& errno
== ENOSPC
) {
1983 /* clean up the volsize property we tried to set */
1984 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1988 zcmd_free_nvlists(&zc
);
1990 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1992 if (nvlist_add_uint64(nvl
,
1993 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1996 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1998 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
2001 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
2002 if (cls
[cl_idx
] != NULL
) {
2003 int clp_err
= changelist_postfix(cls
[cl_idx
]);
2011 * Refresh the statistics so the new property
2012 * value is reflected.
2014 (void) get_stats(zhp
);
2017 * Remount the filesystem to propagate the change
2018 * if one of the options handled by the generic
2019 * Linux namespace layer has been modified.
2021 if (zfs_is_namespace_prop(prop
) &&
2022 zfs_is_mounted(zhp
, NULL
))
2023 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2029 zcmd_free_nvlists(&zc
);
2031 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
2032 if (cls
[cl_idx
] != NULL
)
2033 changelist_free(cls
[cl_idx
]);
2041 * Given a property, inherit the value from the parent dataset, or if received
2042 * is TRUE, revert to the received value, if any.
2045 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
2047 zfs_cmd_t zc
= {"\0"};
2049 prop_changelist_t
*cl
;
2050 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2054 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2055 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
2057 zc
.zc_cookie
= received
;
2058 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
2060 * For user properties, the amount of work we have to do is very
2061 * small, so just do it here.
2063 if (!zfs_prop_user(propname
)) {
2064 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2065 "invalid property"));
2066 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2069 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2070 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2072 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
2073 return (zfs_standard_error(hdl
, errno
, errbuf
));
2079 * Verify that this property is inheritable.
2081 if (zfs_prop_readonly(prop
))
2082 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
2084 if (!zfs_prop_inheritable(prop
) && !received
)
2085 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
2088 * Check to see if the value applies to this type
2090 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2091 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
2094 * Normalize the name, to get rid of shorthand abbreviations.
2096 propname
= zfs_prop_to_name(prop
);
2097 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2098 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2100 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
2101 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
2102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2103 "dataset is used in a non-global zone"));
2104 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
2108 * Determine datasets which will be affected by this change, if any.
2110 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2113 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2115 "child dataset with inherited mountpoint is used "
2116 "in a non-global zone"));
2117 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2121 if ((ret
= changelist_prefix(cl
)) != 0)
2124 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2125 return (zfs_standard_error(hdl
, errno
, errbuf
));
2128 if ((ret
= changelist_postfix(cl
)) != 0)
2132 * Refresh the statistics so the new property is reflected.
2134 (void) get_stats(zhp
);
2137 * Remount the filesystem to propagate the change
2138 * if one of the options handled by the generic
2139 * Linux namespace layer has been modified.
2141 if (zfs_is_namespace_prop(prop
) &&
2142 zfs_is_mounted(zhp
, NULL
))
2143 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2147 changelist_free(cl
);
2152 * True DSL properties are stored in an nvlist. The following two functions
2153 * extract them appropriately.
2156 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2162 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2163 zfs_prop_to_name(prop
), &nv
) == 0) {
2164 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2165 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2167 verify(!zhp
->zfs_props_table
||
2168 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2169 value
= zfs_prop_default_numeric(prop
);
2177 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2183 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2184 zfs_prop_to_name(prop
), &nv
) == 0) {
2185 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2186 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2188 verify(!zhp
->zfs_props_table
||
2189 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2190 value
= zfs_prop_default_string(prop
);
2198 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2200 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2204 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2206 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2207 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2211 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2213 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2218 * Internal function for getting a numeric property. Both zfs_prop_get() and
2219 * zfs_prop_get_int() are built using this interface.
2221 * Certain properties can be overridden using 'mount -o'. In this case, scan
2222 * the contents of the /proc/self/mounts entry, searching for the
2223 * appropriate options. If they differ from the on-disk values, report the
2224 * current values and mark the source "temporary".
2227 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2228 char **source
, uint64_t *val
)
2230 zfs_cmd_t zc
= {"\0"};
2231 nvlist_t
*zplprops
= NULL
;
2233 char *mntopt_on
= NULL
;
2234 char *mntopt_off
= NULL
;
2235 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2240 * If the property is being fetched for a snapshot, check whether
2241 * the property is valid for the snapshot's head dataset type.
2243 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2244 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2245 *val
= zfs_prop_default_numeric(prop
);
2250 case ZFS_PROP_ATIME
:
2251 mntopt_on
= MNTOPT_ATIME
;
2252 mntopt_off
= MNTOPT_NOATIME
;
2255 case ZFS_PROP_RELATIME
:
2256 mntopt_on
= MNTOPT_RELATIME
;
2257 mntopt_off
= MNTOPT_NORELATIME
;
2260 case ZFS_PROP_DEVICES
:
2261 mntopt_on
= MNTOPT_DEVICES
;
2262 mntopt_off
= MNTOPT_NODEVICES
;
2266 mntopt_on
= MNTOPT_EXEC
;
2267 mntopt_off
= MNTOPT_NOEXEC
;
2270 case ZFS_PROP_READONLY
:
2271 mntopt_on
= MNTOPT_RO
;
2272 mntopt_off
= MNTOPT_RW
;
2275 case ZFS_PROP_SETUID
:
2276 mntopt_on
= MNTOPT_SETUID
;
2277 mntopt_off
= MNTOPT_NOSETUID
;
2280 case ZFS_PROP_XATTR
:
2281 mntopt_on
= MNTOPT_XATTR
;
2282 mntopt_off
= MNTOPT_NOXATTR
;
2285 case ZFS_PROP_NBMAND
:
2286 mntopt_on
= MNTOPT_NBMAND
;
2287 mntopt_off
= MNTOPT_NONBMAND
;
2295 * Because looking up the mount options is potentially expensive
2296 * (iterating over all of /proc/self/mounts), we defer its
2297 * calculation until we're looking up a property which requires
2300 if (!zhp
->zfs_mntcheck
&&
2301 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2302 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2303 struct mnttab entry
;
2305 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2306 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2308 if (zhp
->zfs_mntopts
== NULL
)
2312 zhp
->zfs_mntcheck
= B_TRUE
;
2315 if (zhp
->zfs_mntopts
== NULL
)
2316 mnt
.mnt_mntopts
= "";
2318 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2321 case ZFS_PROP_ATIME
:
2322 case ZFS_PROP_RELATIME
:
2323 case ZFS_PROP_DEVICES
:
2325 case ZFS_PROP_READONLY
:
2326 case ZFS_PROP_SETUID
:
2327 case ZFS_PROP_XATTR
:
2328 case ZFS_PROP_NBMAND
:
2329 *val
= getprop_uint64(zhp
, prop
, source
);
2334 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2337 *src
= ZPROP_SRC_TEMPORARY
;
2338 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2341 *src
= ZPROP_SRC_TEMPORARY
;
2345 case ZFS_PROP_CANMOUNT
:
2346 case ZFS_PROP_VOLSIZE
:
2347 case ZFS_PROP_QUOTA
:
2348 case ZFS_PROP_REFQUOTA
:
2349 case ZFS_PROP_RESERVATION
:
2350 case ZFS_PROP_REFRESERVATION
:
2351 case ZFS_PROP_FILESYSTEM_LIMIT
:
2352 case ZFS_PROP_SNAPSHOT_LIMIT
:
2353 case ZFS_PROP_FILESYSTEM_COUNT
:
2354 case ZFS_PROP_SNAPSHOT_COUNT
:
2355 *val
= getprop_uint64(zhp
, prop
, source
);
2357 if (*source
== NULL
) {
2358 /* not default, must be local */
2359 *source
= zhp
->zfs_name
;
2363 case ZFS_PROP_MOUNTED
:
2364 *val
= (zhp
->zfs_mntopts
!= NULL
);
2367 case ZFS_PROP_NUMCLONES
:
2368 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2371 case ZFS_PROP_VERSION
:
2372 case ZFS_PROP_NORMALIZE
:
2373 case ZFS_PROP_UTF8ONLY
:
2375 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2377 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2378 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2379 zcmd_free_nvlists(&zc
);
2380 if (prop
== ZFS_PROP_VERSION
&&
2381 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2382 *val
= zfs_prop_default_numeric(prop
);
2385 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2386 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2388 zcmd_free_nvlists(&zc
);
2391 nvlist_free(zplprops
);
2392 zcmd_free_nvlists(&zc
);
2395 case ZFS_PROP_INCONSISTENT
:
2396 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2400 switch (zfs_prop_get_type(prop
)) {
2401 case PROP_TYPE_NUMBER
:
2402 case PROP_TYPE_INDEX
:
2403 *val
= getprop_uint64(zhp
, prop
, source
);
2405 * If we tried to use a default value for a
2406 * readonly property, it means that it was not
2407 * present. Note this only applies to "truly"
2408 * readonly properties, not set-once properties
2409 * like volblocksize.
2411 if (zfs_prop_readonly(prop
) &&
2412 !zfs_prop_setonce(prop
) &&
2413 *source
!= NULL
&& (*source
)[0] == '\0') {
2419 case PROP_TYPE_STRING
:
2421 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2422 "cannot get non-numeric property"));
2423 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2424 dgettext(TEXT_DOMAIN
, "internal error")));
2432 * Calculate the source type, given the raw source string.
2435 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2436 char *statbuf
, size_t statlen
)
2438 if (statbuf
== NULL
||
2439 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2443 if (source
== NULL
) {
2444 *srctype
= ZPROP_SRC_NONE
;
2445 } else if (source
[0] == '\0') {
2446 *srctype
= ZPROP_SRC_DEFAULT
;
2447 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2448 *srctype
= ZPROP_SRC_RECEIVED
;
2450 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2451 *srctype
= ZPROP_SRC_LOCAL
;
2453 (void) strlcpy(statbuf
, source
, statlen
);
2454 *srctype
= ZPROP_SRC_INHERITED
;
2461 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2462 size_t proplen
, boolean_t literal
)
2467 if (zhp
->zfs_recvd_props
== NULL
)
2468 if (get_recvd_props_ioctl(zhp
) != 0)
2471 prop
= zfs_name_to_prop(propname
);
2473 if (prop
!= ZPROP_INVAL
) {
2475 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2477 zfs_set_recvd_props_mode(zhp
, &cookie
);
2478 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2479 NULL
, NULL
, 0, literal
);
2480 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2484 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2485 propname
, &propval
) != 0)
2487 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2489 (void) strlcpy(propbuf
, recvdval
, proplen
);
2492 return (err
== 0 ? 0 : -1);
2496 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2501 value
= zfs_get_clones_nvl(zhp
);
2506 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2507 pair
= nvlist_next_nvpair(value
, pair
)) {
2508 if (propbuf
[0] != '\0')
2509 (void) strlcat(propbuf
, ",", proplen
);
2510 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2516 struct get_clones_arg
{
2520 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2524 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2526 struct get_clones_arg
*gca
= arg
;
2528 if (gca
->numclones
== 0) {
2533 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2534 NULL
, NULL
, 0, B_TRUE
) != 0)
2536 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2537 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2542 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2548 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2550 nvlist_t
*nv
, *value
;
2552 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2553 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2554 struct get_clones_arg gca
;
2557 * if this is a snapshot, then the kernel wasn't able
2558 * to get the clones. Do it by slowly iterating.
2560 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2562 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2564 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2569 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2571 gca
.origin
= zhp
->zfs_name
;
2573 if (gca
.numclones
!= 0) {
2575 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2578 /* get the pool name */
2579 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2580 (void) strsep(&cp
, "/@");
2581 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2582 ZFS_TYPE_FILESYSTEM
);
2589 (void) get_clones_cb(root
, &gca
);
2592 if (gca
.numclones
!= 0 ||
2593 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2594 nvlist_add_nvlist(zhp
->zfs_props
,
2595 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2602 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2603 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2606 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2612 * Accepts a property and value and checks that the value
2613 * matches the one found by the channel program. If they are
2614 * not equal, print both of them.
2617 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2620 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2623 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2624 const char *prop_name
= zfs_prop_to_name(prop
);
2625 const char *program
=
2627 "ds = args['dataset']\n"
2628 "prop = args['property']\n"
2629 "value, setpoint = zfs.get_prop(ds, prop)\n"
2630 "return {value=value, setpoint=setpoint}\n";
2633 nvlist_t
*argnvl
= fnvlist_alloc();
2635 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2636 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2638 error
= lzc_channel_program_nosync(poolname
, program
,
2639 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2642 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2643 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2645 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2647 (void) fprintf(stderr
, "%s: zcp check error: "
2648 "%u\n", prop_name
, error
);
2651 if (ans
!= intval
) {
2652 (void) fprintf(stderr
, "%s: zfs found %llu, "
2653 "but zcp found %llu\n", prop_name
,
2654 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2658 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2660 (void) fprintf(stderr
, "%s: zcp check error: "
2661 "%u\n", prop_name
, error
);
2664 if (strcmp(strval
, str_ans
) != 0) {
2665 (void) fprintf(stderr
,
2666 "%s: zfs found '%s', but zcp found '%s'\n",
2667 prop_name
, strval
, str_ans
);
2671 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2672 "error: %u\n", prop_name
, error
);
2674 nvlist_free(argnvl
);
2675 nvlist_free(outnvl
);
2679 * Retrieve a property from the given object. If 'literal' is specified, then
2680 * numbers are left as exact values. Otherwise, numbers are converted to a
2681 * human-readable form.
2683 * Returns 0 on success, or -1 on error.
2686 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2687 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2689 char *source
= NULL
;
2693 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2696 * Check to see if this property applies to our object
2698 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2701 if (received
&& zfs_prop_readonly(prop
))
2705 *src
= ZPROP_SRC_NONE
;
2708 case ZFS_PROP_CREATION
:
2710 * 'creation' is a time_t stored in the statistics. We convert
2711 * this into a string unless 'literal' is specified.
2714 val
= getprop_uint64(zhp
, prop
, &source
);
2715 time_t time
= (time_t)val
;
2719 localtime_r(&time
, &t
) == NULL
||
2720 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2722 (void) snprintf(propbuf
, proplen
, "%llu",
2725 zcp_check(zhp
, prop
, val
, NULL
);
2728 case ZFS_PROP_MOUNTPOINT
:
2730 * Getting the precise mountpoint can be tricky.
2732 * - for 'none' or 'legacy', return those values.
2733 * - for inherited mountpoints, we want to take everything
2734 * after our ancestor and append it to the inherited value.
2736 * If the pool has an alternate root, we want to prepend that
2737 * root to any values we return.
2740 str
= getprop_string(zhp
, prop
, &source
);
2742 if (str
[0] == '/') {
2743 char buf
[MAXPATHLEN
];
2745 const char *relpath
;
2748 * If we inherit the mountpoint, even from a dataset
2749 * with a received value, the source will be the path of
2750 * the dataset we inherit from. If source is
2751 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2754 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2757 relpath
= zhp
->zfs_name
+ strlen(source
);
2758 if (relpath
[0] == '/')
2762 if ((zpool_get_prop(zhp
->zpool_hdl
,
2763 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2764 B_FALSE
)) || (strcmp(root
, "-") == 0))
2767 * Special case an alternate root of '/'. This will
2768 * avoid having multiple leading slashes in the
2771 if (strcmp(root
, "/") == 0)
2775 * If the mountpoint is '/' then skip over this
2776 * if we are obtaining either an alternate root or
2777 * an inherited mountpoint.
2779 if (str
[1] == '\0' && (root
[0] != '\0' ||
2780 relpath
[0] != '\0'))
2783 if (relpath
[0] == '\0')
2784 (void) snprintf(propbuf
, proplen
, "%s%s",
2787 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2788 root
, str
, relpath
[0] == '@' ? "" : "/",
2791 /* 'legacy' or 'none' */
2792 (void) strlcpy(propbuf
, str
, proplen
);
2794 zcp_check(zhp
, prop
, 0, propbuf
);
2797 case ZFS_PROP_ORIGIN
:
2798 str
= getprop_string(zhp
, prop
, &source
);
2801 (void) strlcpy(propbuf
, str
, proplen
);
2802 zcp_check(zhp
, prop
, 0, str
);
2805 case ZFS_PROP_CLONES
:
2806 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2810 case ZFS_PROP_QUOTA
:
2811 case ZFS_PROP_REFQUOTA
:
2812 case ZFS_PROP_RESERVATION
:
2813 case ZFS_PROP_REFRESERVATION
:
2815 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2818 * If quota or reservation is 0, we translate this into 'none'
2819 * (unless literal is set), and indicate that it's the default
2820 * value. Otherwise, we print the number nicely and indicate
2821 * that its set locally.
2825 (void) strlcpy(propbuf
, "0", proplen
);
2827 (void) strlcpy(propbuf
, "none", proplen
);
2830 (void) snprintf(propbuf
, proplen
, "%llu",
2833 zfs_nicebytes(val
, propbuf
, proplen
);
2835 zcp_check(zhp
, prop
, val
, NULL
);
2838 case ZFS_PROP_FILESYSTEM_LIMIT
:
2839 case ZFS_PROP_SNAPSHOT_LIMIT
:
2840 case ZFS_PROP_FILESYSTEM_COUNT
:
2841 case ZFS_PROP_SNAPSHOT_COUNT
:
2843 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2847 * If limit is UINT64_MAX, we translate this into 'none' (unless
2848 * literal is set), and indicate that it's the default value.
2849 * Otherwise, we print the number nicely and indicate that it's
2853 (void) snprintf(propbuf
, proplen
, "%llu",
2855 } else if (val
== UINT64_MAX
) {
2856 (void) strlcpy(propbuf
, "none", proplen
);
2858 zfs_nicenum(val
, propbuf
, proplen
);
2861 zcp_check(zhp
, prop
, val
, NULL
);
2864 case ZFS_PROP_REFRATIO
:
2865 case ZFS_PROP_COMPRESSRATIO
:
2866 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2869 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2870 (u_longlong_t
)(val
/ 100),
2871 (u_longlong_t
)(val
% 100));
2873 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2874 (u_longlong_t
)(val
/ 100),
2875 (u_longlong_t
)(val
% 100));
2876 zcp_check(zhp
, prop
, val
, NULL
);
2880 switch (zhp
->zfs_type
) {
2881 case ZFS_TYPE_FILESYSTEM
:
2884 case ZFS_TYPE_VOLUME
:
2887 case ZFS_TYPE_SNAPSHOT
:
2890 case ZFS_TYPE_BOOKMARK
:
2896 (void) snprintf(propbuf
, proplen
, "%s", str
);
2897 zcp_check(zhp
, prop
, 0, propbuf
);
2900 case ZFS_PROP_MOUNTED
:
2902 * The 'mounted' property is a pseudo-property that described
2903 * whether the filesystem is currently mounted. Even though
2904 * it's a boolean value, the typical values of "on" and "off"
2905 * don't make sense, so we translate to "yes" and "no".
2907 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2908 src
, &source
, &val
) != 0)
2911 (void) strlcpy(propbuf
, "yes", proplen
);
2913 (void) strlcpy(propbuf
, "no", proplen
);
2918 * The 'name' property is a pseudo-property derived from the
2919 * dataset name. It is presented as a real property to simplify
2922 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2923 zcp_check(zhp
, prop
, 0, propbuf
);
2926 case ZFS_PROP_MLSLABEL
:
2928 #ifdef HAVE_MLSLABEL
2929 m_label_t
*new_sl
= NULL
;
2930 char *ascii
= NULL
; /* human readable label */
2932 (void) strlcpy(propbuf
,
2933 getprop_string(zhp
, prop
, &source
), proplen
);
2935 if (literal
|| (strcasecmp(propbuf
,
2936 ZFS_MLSLABEL_DEFAULT
) == 0))
2940 * Try to translate the internal hex string to
2941 * human-readable output. If there are any
2942 * problems just use the hex string.
2945 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2946 L_NO_CORRECTION
, NULL
) == -1) {
2947 m_label_free(new_sl
);
2951 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2955 m_label_free(new_sl
);
2958 m_label_free(new_sl
);
2960 (void) strlcpy(propbuf
, ascii
, proplen
);
2963 (void) strlcpy(propbuf
,
2964 getprop_string(zhp
, prop
, &source
), proplen
);
2965 #endif /* HAVE_MLSLABEL */
2970 case ZFS_PROP_CREATETXG
:
2972 * GUIDs are stored as numbers, but they are identifiers.
2973 * We don't want them to be pretty printed, because pretty
2974 * printing mangles the ID into a truncated and useless value.
2976 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2978 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2979 zcp_check(zhp
, prop
, val
, NULL
);
2982 case ZFS_PROP_REFERENCED
:
2983 case ZFS_PROP_AVAILABLE
:
2985 case ZFS_PROP_USEDSNAP
:
2986 case ZFS_PROP_USEDDS
:
2987 case ZFS_PROP_USEDREFRESERV
:
2988 case ZFS_PROP_USEDCHILD
:
2989 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2992 (void) snprintf(propbuf
, proplen
, "%llu",
2995 zfs_nicebytes(val
, propbuf
, proplen
);
2997 zcp_check(zhp
, prop
, val
, NULL
);
3001 switch (zfs_prop_get_type(prop
)) {
3002 case PROP_TYPE_NUMBER
:
3003 if (get_numeric_property(zhp
, prop
, src
,
3004 &source
, &val
) != 0) {
3009 (void) snprintf(propbuf
, proplen
, "%llu",
3012 zfs_nicenum(val
, propbuf
, proplen
);
3014 zcp_check(zhp
, prop
, val
, NULL
);
3017 case PROP_TYPE_STRING
:
3018 str
= getprop_string(zhp
, prop
, &source
);
3022 (void) strlcpy(propbuf
, str
, proplen
);
3023 zcp_check(zhp
, prop
, 0, str
);
3026 case PROP_TYPE_INDEX
:
3027 if (get_numeric_property(zhp
, prop
, src
,
3028 &source
, &val
) != 0)
3030 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
3033 (void) strlcpy(propbuf
, strval
, proplen
);
3034 zcp_check(zhp
, prop
, 0, strval
);
3042 get_source(zhp
, src
, source
, statbuf
, statlen
);
3048 * Utility function to get the given numeric property. Does no validation that
3049 * the given property is the appropriate type; should only be used with
3050 * hard-coded property types.
3053 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
3058 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3064 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3068 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3069 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3073 * Similar to zfs_prop_get(), but returns the value as an integer.
3076 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3077 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3082 * Check to see if this property applies to our object
3084 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3085 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3086 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3087 zfs_prop_to_name(prop
)));
3091 *src
= ZPROP_SRC_NONE
;
3093 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3096 get_source(zhp
, src
, source
, statbuf
, statlen
);
3103 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3104 char **domainp
, idmap_rid_t
*ridp
)
3106 idmap_get_handle_t
*get_hdl
= NULL
;
3110 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3114 err
= idmap_get_sidbyuid(get_hdl
, id
,
3115 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3117 err
= idmap_get_sidbygid(get_hdl
, id
,
3118 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3120 if (err
== IDMAP_SUCCESS
&&
3121 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3122 status
== IDMAP_SUCCESS
)
3128 idmap_get_destroy(get_hdl
);
3131 #endif /* HAVE_IDMAP */
3134 * convert the propname into parameters needed by kernel
3135 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3136 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3137 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3138 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3139 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3140 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3143 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3144 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3146 zfs_userquota_prop_t type
;
3150 boolean_t isproject
;
3156 /* Figure out the property type ({user|group|project}{quota|space}) */
3157 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3158 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3159 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3162 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3166 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3167 type
== ZFS_PROP_USEROBJQUOTA
||
3168 type
== ZFS_PROP_USEROBJUSED
);
3169 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3170 type
== ZFS_PROP_GROUPOBJQUOTA
||
3171 type
== ZFS_PROP_GROUPOBJUSED
);
3172 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3173 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3174 type
== ZFS_PROP_PROJECTOBJUSED
);
3176 cp
= strchr(propname
, '@') + 1;
3178 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3179 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3182 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3183 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3186 } else if (!isproject
&& strchr(cp
, '@')) {
3189 * It's a SID name (eg "user@domain") that needs to be
3190 * turned into S-1-domainID-RID.
3192 directory_error_t e
;
3193 char *numericsid
= NULL
;
3196 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3199 e
= directory_sid_from_user_name(NULL
,
3202 e
= directory_sid_from_group_name(NULL
,
3206 directory_error_free(e
);
3209 if (numericsid
== NULL
)
3212 (void) strlcpy(domain
, cp
, domainlen
);
3213 cp
= strrchr(domain
, '-');
3218 *ridp
= strtoull(cp
, &end
, 10);
3221 if (errno
!= 0 || *end
!= '\0')
3225 #endif /* HAVE_IDMAP */
3227 /* It's a user/group/project ID (eg "12345"). */
3230 id
= strtoul(cp
, &end
, 10);
3233 if (id
> MAXUID
&& !isproject
) {
3235 /* It's an ephemeral ID. */
3239 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3240 &mapdomain
, &rid
) != 0)
3242 (void) strlcpy(domain
, mapdomain
, domainlen
);
3246 #endif /* HAVE_IDMAP */
3256 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3257 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3260 zfs_cmd_t zc
= {"\0"};
3262 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3264 err
= userquota_propname_decode(propname
,
3265 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3266 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3267 zc
.zc_objset_type
= *typep
;
3271 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3275 *propvalue
= zc
.zc_cookie
;
3280 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3281 uint64_t *propvalue
)
3283 zfs_userquota_prop_t type
;
3285 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3290 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3291 char *propbuf
, int proplen
, boolean_t literal
)
3295 zfs_userquota_prop_t type
;
3297 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3304 (void) snprintf(propbuf
, proplen
, "%llu",
3305 (u_longlong_t
)propvalue
);
3306 } else if (propvalue
== 0 &&
3307 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3308 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3309 type
== ZFS_PROP_PROJECTQUOTA
||
3310 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3311 (void) strlcpy(propbuf
, "none", proplen
);
3312 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3313 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3314 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3315 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3317 zfs_nicenum(propvalue
, propbuf
, proplen
);
3323 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3324 uint64_t *propvalue
)
3327 zfs_cmd_t zc
= {"\0"};
3328 const char *snapname
;
3330 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3332 snapname
= strchr(propname
, '@') + 1;
3333 if (strchr(snapname
, '@')) {
3334 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3336 /* snapname is the short name, append it to zhp's fsname */
3339 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3340 sizeof (zc
.zc_value
));
3341 cp
= strchr(zc
.zc_value
, '@');
3344 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
3345 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3348 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3352 *propvalue
= zc
.zc_cookie
;
3357 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3358 char *propbuf
, int proplen
, boolean_t literal
)
3363 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3369 (void) snprintf(propbuf
, proplen
, "%llu",
3370 (u_longlong_t
)propvalue
);
3372 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3379 * Returns the name of the given zfs handle.
3382 zfs_get_name(const zfs_handle_t
*zhp
)
3384 return (zhp
->zfs_name
);
3388 * Returns the name of the parent pool for the given zfs handle.
3391 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3393 return (zhp
->zpool_hdl
->zpool_name
);
3397 * Returns the type of the given zfs handle.
3400 zfs_get_type(const zfs_handle_t
*zhp
)
3402 return (zhp
->zfs_type
);
3406 * Is one dataset name a child dataset of another?
3408 * Needs to handle these cases:
3409 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3410 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3411 * Descendant? No. No. No. Yes.
3414 is_descendant(const char *ds1
, const char *ds2
)
3416 size_t d1len
= strlen(ds1
);
3418 /* ds2 can't be a descendant if it's smaller */
3419 if (strlen(ds2
) < d1len
)
3422 /* otherwise, compare strings and verify that there's a '/' char */
3423 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3427 * Given a complete name, return just the portion that refers to the parent.
3428 * Will return -1 if there is no parent (path is just the name of the
3432 parent_name(const char *path
, char *buf
, size_t buflen
)
3436 (void) strlcpy(buf
, path
, buflen
);
3438 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3446 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3448 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3452 * If accept_ancestor is false, then check to make sure that the given path has
3453 * a parent, and that it exists. If accept_ancestor is true, then find the
3454 * closest existing ancestor for the given path. In prefixlen return the
3455 * length of already existing prefix of the given path. We also fetch the
3456 * 'zoned' property, which is used to validate property settings when creating
3460 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3461 boolean_t accept_ancestor
, int *prefixlen
)
3463 zfs_cmd_t zc
= {"\0"};
3464 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3470 (void) snprintf(errbuf
, sizeof (errbuf
),
3471 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3473 /* get parent, and check to see if this is just a pool */
3474 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3475 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3476 "missing dataset name"));
3477 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3480 /* check to see if the pool exists */
3481 if ((slash
= strchr(parent
, '/')) == NULL
)
3482 slash
= parent
+ strlen(parent
);
3483 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3484 zc
.zc_name
[slash
- parent
] = '\0';
3485 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3487 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3488 "no such pool '%s'"), zc
.zc_name
);
3489 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3492 /* check to see if the parent dataset exists */
3493 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3494 if (errno
== ENOENT
&& accept_ancestor
) {
3496 * Go deeper to find an ancestor, give up on top level.
3498 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3499 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3500 "no such pool '%s'"), zc
.zc_name
);
3501 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3503 } else if (errno
== ENOENT
) {
3504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3505 "parent does not exist"));
3506 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3508 return (zfs_standard_error(hdl
, errno
, errbuf
));
3511 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3515 /* we are in a non-global zone, but parent is in the global zone */
3516 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3517 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3522 /* make sure parent is a filesystem */
3523 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3525 "parent is not a filesystem"));
3526 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3532 if (prefixlen
!= NULL
)
3533 *prefixlen
= strlen(parent
);
3538 * Finds whether the dataset of the given type(s) exists.
3541 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3545 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3549 * Try to get stats for the dataset, which will tell us if it exists.
3551 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3552 int ds_type
= zhp
->zfs_type
;
3555 if (types
& ds_type
)
3562 * Given a path to 'target', create all the ancestors between
3563 * the prefixlen portion of the path, and the target itself.
3564 * Fail if the initial prefixlen-ancestor does not already exist.
3567 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3573 /* make sure prefix exists */
3574 cp
= target
+ prefixlen
;
3576 assert(strchr(cp
, '/') == NULL
);
3577 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3580 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3588 * Attempt to create, mount, and share any ancestor filesystems,
3589 * up to the prefixlen-long one.
3591 for (cp
= target
+ prefixlen
+ 1;
3592 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3596 h
= make_dataset_handle(hdl
, target
);
3598 /* it already exists, nothing to do here */
3603 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3605 opname
= dgettext(TEXT_DOMAIN
, "create");
3609 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3611 opname
= dgettext(TEXT_DOMAIN
, "open");
3615 if (zfs_mount(h
, NULL
, 0) != 0) {
3616 opname
= dgettext(TEXT_DOMAIN
, "mount");
3620 if (zfs_share(h
) != 0) {
3621 opname
= dgettext(TEXT_DOMAIN
, "share");
3631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3632 "failed to %s ancestor '%s'"), opname
, target
);
3637 * Creates non-existing ancestors of the given path.
3640 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3647 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3648 "cannot create '%s'"), path
);
3651 * Check that we are not passing the nesting limit
3652 * before we start creating any ancestors.
3654 if (dataset_nestcheck(path
) != 0) {
3655 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3656 "maximum name nesting depth exceeded"));
3657 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3660 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3663 if ((path_copy
= strdup(path
)) != NULL
) {
3664 rc
= create_parents(hdl
, path_copy
, prefix
);
3667 if (path_copy
== NULL
|| rc
!= 0)
3674 * Create a new filesystem or volume.
3677 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3682 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3684 enum lzc_dataset_type ost
;
3685 zpool_handle_t
*zpool_handle
;
3686 uint8_t *wkeydata
= NULL
;
3689 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3691 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3692 "cannot create '%s'"), path
);
3694 /* validate the path, taking care to note the extended error message */
3695 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3696 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3698 if (dataset_nestcheck(path
) != 0) {
3699 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3700 "maximum name nesting depth exceeded"));
3701 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3704 /* validate parents exist */
3705 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3709 * The failure modes when creating a dataset of a different type over
3710 * one that already exists is a little strange. In particular, if you
3711 * try to create a dataset on top of an existing dataset, the ioctl()
3712 * will return ENOENT, not EEXIST. To prevent this from happening, we
3713 * first try to see if the dataset exists.
3715 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3717 "dataset already exists"));
3718 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3721 if (type
== ZFS_TYPE_VOLUME
)
3722 ost
= LZC_DATSET_TYPE_ZVOL
;
3724 ost
= LZC_DATSET_TYPE_ZFS
;
3726 /* open zpool handle for prop validation */
3727 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3728 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3730 /* truncate pool_path at first slash */
3731 char *p
= strchr(pool_path
, '/');
3735 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3738 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3739 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3740 zpool_close(zpool_handle
);
3743 zpool_close(zpool_handle
);
3745 if (type
== ZFS_TYPE_VOLUME
) {
3747 * If we are creating a volume, the size and block size must
3748 * satisfy a few restraints. First, the blocksize must be a
3749 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3750 * volsize must be a multiple of the block size, and cannot be
3753 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3754 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3756 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3757 "missing volume size"));
3758 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3761 if ((ret
= nvlist_lookup_uint64(props
,
3762 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3763 &blocksize
)) != 0) {
3764 if (ret
== ENOENT
) {
3765 blocksize
= zfs_prop_default_numeric(
3766 ZFS_PROP_VOLBLOCKSIZE
);
3769 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3770 "missing volume block size"));
3771 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3778 "volume size cannot be zero"));
3779 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3782 if (size
% blocksize
!= 0) {
3784 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3785 "volume size must be a multiple of volume block "
3787 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3791 (void) parent_name(path
, parent
, sizeof (parent
));
3792 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3793 &wkeydata
, &wkeylen
) != 0) {
3795 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3798 /* create the dataset */
3799 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3801 if (wkeydata
!= NULL
)
3804 /* check for failure */
3808 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3809 "no such parent '%s'"), parent
);
3810 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3813 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3814 "parent '%s' is not a filesystem"), parent
);
3815 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3818 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3819 "pool must be upgraded to set this "
3820 "property or value"));
3821 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3824 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3825 "encryption root's key is not loaded "
3827 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3830 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3831 "invalid property value(s) specified"));
3832 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3836 * This platform can't address a volume this big.
3838 if (type
== ZFS_TYPE_VOLUME
)
3839 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3844 return (zfs_standard_error(hdl
, errno
, errbuf
));
3852 * Destroys the given dataset. The caller must make sure that the filesystem
3853 * isn't mounted, and that there are no active dependents. If the file system
3854 * does not exist this function does nothing.
3857 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3859 zfs_cmd_t zc
= {"\0"};
3861 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3862 nvlist_t
*nv
= fnvlist_alloc();
3863 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3864 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3867 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3868 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3874 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3876 if (ZFS_IS_VOLUME(zhp
)) {
3877 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3879 zc
.zc_objset_type
= DMU_OST_ZFS
;
3882 zc
.zc_defer_destroy
= defer
;
3883 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3885 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3886 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3890 remove_mountpoint(zhp
);
3895 struct destroydata
{
3897 const char *snapname
;
3901 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3903 struct destroydata
*dd
= arg
;
3904 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3907 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3908 dd
->snapname
) >= sizeof (name
))
3911 if (lzc_exists(name
))
3912 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3914 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3920 * Destroys all snapshots with the given name in zhp & descendants.
3923 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3926 struct destroydata dd
= { 0 };
3928 dd
.snapname
= snapname
;
3929 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3930 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3932 if (nvlist_empty(dd
.nvl
)) {
3933 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3934 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3935 zhp
->zfs_name
, snapname
);
3937 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3939 nvlist_free(dd
.nvl
);
3944 * Destroys all the snapshots named in the nvlist.
3947 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3950 nvlist_t
*errlist
= NULL
;
3953 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3956 nvlist_free(errlist
);
3960 if (nvlist_empty(errlist
)) {
3962 (void) snprintf(errbuf
, sizeof (errbuf
),
3963 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3965 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3967 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3968 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3970 (void) snprintf(errbuf
, sizeof (errbuf
),
3971 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3974 switch (fnvpair_value_int32(pair
)) {
3977 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3978 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3981 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3986 nvlist_free(errlist
);
3991 * Clones the given dataset. The target must be of the same type as the source.
3994 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3996 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3999 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4002 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
4004 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4005 "cannot create '%s'"), target
);
4007 /* validate the target/clone name */
4008 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
4009 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4011 /* validate parents exist */
4012 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
4015 (void) parent_name(target
, parent
, sizeof (parent
));
4022 if (ZFS_IS_VOLUME(zhp
)) {
4023 type
= ZFS_TYPE_VOLUME
;
4025 type
= ZFS_TYPE_FILESYSTEM
;
4027 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
4028 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
4030 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
4036 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
4038 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
4041 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
4049 * The parent doesn't exist. We should have caught this
4050 * above, but there may a race condition that has since
4051 * destroyed the parent.
4053 * At this point, we don't know whether it's the source
4054 * that doesn't exist anymore, or whether the target
4055 * dataset doesn't exist.
4057 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4058 "no such parent '%s'"), parent
);
4059 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4062 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4063 "source and target pools differ"));
4064 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4068 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4077 * Promotes the given clone fs to be the clone parent.
4080 zfs_promote(zfs_handle_t
*zhp
)
4082 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4083 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4087 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4088 "cannot promote '%s'"), zhp
->zfs_name
);
4090 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4091 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4092 "snapshots can not be promoted"));
4093 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4096 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4097 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4098 "not a cloned filesystem"));
4099 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4102 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4103 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4105 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4110 /* There is a conflicting snapshot name. */
4111 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4112 "conflicting snapshot '%s' from parent '%s'"),
4113 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4114 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4117 return (zfs_standard_error(hdl
, ret
, errbuf
));
4123 typedef struct snapdata
{
4125 const char *sd_snapname
;
4129 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4131 snapdata_t
*sd
= arg
;
4132 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4135 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4136 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4137 sd
->sd_snapname
) >= sizeof (name
))
4140 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4142 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4150 zfs_remap_indirects(libzfs_handle_t
*hdl
, const char *fs
)
4155 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4156 "cannot remap dataset '%s'"), fs
);
4158 err
= lzc_remap(fs
);
4163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4164 "pool must be upgraded"));
4165 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4168 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4171 (void) zfs_standard_error(hdl
, err
, errbuf
);
4180 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4184 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4190 zpool_handle_t
*zpool_hdl
;
4191 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4193 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4194 "cannot create snapshots "));
4197 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4198 const char *snapname
= nvpair_name(elem
);
4200 /* validate the target name */
4201 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4203 (void) snprintf(errbuf
, sizeof (errbuf
),
4204 dgettext(TEXT_DOMAIN
,
4205 "cannot create snapshot '%s'"), snapname
);
4206 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4211 * get pool handle for prop validation. assumes all snaps are in the
4212 * same pool, as does lzc_snapshot (below).
4214 elem
= nvlist_next_nvpair(snaps
, NULL
);
4215 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4216 pool
[strcspn(pool
, "/@")] = '\0';
4217 zpool_hdl
= zpool_open(hdl
, pool
);
4218 if (zpool_hdl
== NULL
)
4221 if (props
!= NULL
&&
4222 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4223 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4224 zpool_close(zpool_hdl
);
4227 zpool_close(zpool_hdl
);
4229 ret
= lzc_snapshot(snaps
, props
, &errors
);
4232 boolean_t printed
= B_FALSE
;
4233 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4235 elem
= nvlist_next_nvpair(errors
, elem
)) {
4236 (void) snprintf(errbuf
, sizeof (errbuf
),
4237 dgettext(TEXT_DOMAIN
,
4238 "cannot create snapshot '%s'"), nvpair_name(elem
));
4239 (void) zfs_standard_error(hdl
,
4240 fnvpair_value_int32(elem
), errbuf
);
4246 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4247 "multiple snapshots of same "
4249 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4253 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4259 nvlist_free(errors
);
4264 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4268 snapdata_t sd
= { 0 };
4269 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4274 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4275 "cannot snapshot %s"), path
);
4277 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4278 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4280 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4281 cp
= strchr(fsname
, '@');
4283 sd
.sd_snapname
= cp
+ 1;
4285 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4286 ZFS_TYPE_VOLUME
)) == NULL
) {
4290 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4292 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4294 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4297 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4298 nvlist_free(sd
.sd_nvl
);
4304 * Destroy any more recent snapshots. We invoke this callback on any dependents
4305 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4306 * is a dependent and we should just destroy it without checking the transaction
4309 typedef struct rollback_data
{
4310 const char *cb_target
; /* the snapshot */
4311 uint64_t cb_create
; /* creation time reference */
4317 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4319 rollback_data_t
*cbp
= data
;
4320 prop_changelist_t
*clp
;
4322 /* We must destroy this clone; first unmount it */
4323 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4324 cbp
->cb_force
? MS_FORCE
: 0);
4325 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4326 cbp
->cb_error
= B_TRUE
;
4330 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4331 cbp
->cb_error
= B_TRUE
;
4333 changelist_remove(clp
, zhp
->zfs_name
);
4334 (void) changelist_postfix(clp
);
4335 changelist_free(clp
);
4342 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4344 rollback_data_t
*cbp
= data
;
4346 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4347 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4348 rollback_destroy_dependent
, cbp
);
4350 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4358 * Given a dataset, rollback to a specific snapshot, discarding any
4359 * data changes since then and making it the active dataset.
4361 * Any snapshots and bookmarks more recent than the target are
4362 * destroyed, along with their dependents (i.e. clones).
4365 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4367 rollback_data_t cb
= { 0 };
4369 boolean_t restore_resv
= 0;
4370 uint64_t old_volsize
= 0, new_volsize
;
4371 zfs_prop_t resv_prop
= { 0 };
4373 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4374 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4377 * Destroy all recent snapshots and their dependents.
4379 cb
.cb_force
= force
;
4380 cb
.cb_target
= snap
->zfs_name
;
4381 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4382 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
4383 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4389 * Now that we have verified that the snapshot is the latest,
4390 * rollback to the given snapshot.
4393 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4394 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4396 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4398 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4402 * Pass both the filesystem and the wanted snapshot names,
4403 * we would get an error back if the snapshot is destroyed or
4404 * a new snapshot is created before this request is processed.
4406 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4410 (void) snprintf(errbuf
, sizeof (errbuf
),
4411 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4415 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4416 "there is a snapshot or bookmark more recent "
4417 "than '%s'"), snap
->zfs_name
);
4418 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4421 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4422 "'%s' is not found among snapshots of '%s'"),
4423 snap
->zfs_name
, zhp
->zfs_name
);
4424 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4427 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4430 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4436 * For volumes, if the pre-rollback volsize matched the pre-
4437 * rollback reservation and the volsize has changed then set
4438 * the reservation property to the post-rollback volsize.
4439 * Make a new handle since the rollback closed the dataset.
4441 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4442 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4444 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4445 if (old_volsize
!= new_volsize
)
4446 err
= zfs_prop_set_int(zhp
, resv_prop
,
4455 * Renames the given dataset.
4458 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4459 boolean_t force_unmount
)
4462 zfs_cmd_t zc
= {"\0"};
4464 prop_changelist_t
*cl
= NULL
;
4465 zfs_handle_t
*zhrp
= NULL
;
4466 char *parentname
= NULL
;
4467 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4468 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4471 /* if we have the same exact name, just return success */
4472 if (strcmp(zhp
->zfs_name
, target
) == 0)
4475 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4476 "cannot rename to '%s'"), target
);
4478 /* make sure source name is valid */
4479 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4480 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4483 * Make sure the target name is valid
4485 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4486 if ((strchr(target
, '@') == NULL
) ||
4489 * Snapshot target name is abbreviated,
4490 * reconstruct full dataset name
4492 (void) strlcpy(parent
, zhp
->zfs_name
,
4494 delim
= strchr(parent
, '@');
4495 if (strchr(target
, '@') == NULL
)
4499 (void) strlcat(parent
, target
, sizeof (parent
));
4503 * Make sure we're renaming within the same dataset.
4505 delim
= strchr(target
, '@');
4506 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4507 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4508 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4509 "snapshots must be part of same "
4511 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4516 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4517 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4520 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4521 "recursive rename must be a snapshot"));
4522 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4525 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4526 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4528 /* validate parents */
4529 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4532 /* make sure we're in the same pool */
4533 verify((delim
= strchr(target
, '/')) != NULL
);
4534 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4535 zhp
->zfs_name
[delim
- target
] != '/') {
4536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4537 "datasets must be within same pool"));
4538 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4541 /* new name cannot be a child of the current dataset name */
4542 if (is_descendant(zhp
->zfs_name
, target
)) {
4543 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4544 "New dataset name cannot be a descendant of "
4545 "current dataset name"));
4546 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4550 (void) snprintf(errbuf
, sizeof (errbuf
),
4551 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4553 if (getzoneid() == GLOBAL_ZONEID
&&
4554 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4555 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4556 "dataset is used in a non-global zone"));
4557 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4561 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4562 if (parentname
== NULL
) {
4566 delim
= strchr(parentname
, '@');
4568 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4573 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4574 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4575 CL_GATHER_ITER_MOUNTED
,
4576 force_unmount
? MS_FORCE
: 0)) == NULL
)
4579 if (changelist_haszonedchild(cl
)) {
4580 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4581 "child dataset with inherited mountpoint is used "
4582 "in a non-global zone"));
4583 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4588 if ((ret
= changelist_prefix(cl
)) != 0)
4592 if (ZFS_IS_VOLUME(zhp
))
4593 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4595 zc
.zc_objset_type
= DMU_OST_ZFS
;
4597 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4598 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4600 zc
.zc_cookie
= recursive
;
4602 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4604 * if it was recursive, the one that actually failed will
4607 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4608 "cannot rename '%s'"), zc
.zc_name
);
4610 if (recursive
&& errno
== EEXIST
) {
4611 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4612 "a child dataset already has a snapshot "
4613 "with the new name"));
4614 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4615 } else if (errno
== EACCES
) {
4616 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) ==
4618 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4619 "cannot rename an unencrypted dataset to "
4620 "be a decendent of an encrypted one"));
4622 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4623 "cannot move encryption child outside of "
4624 "its encryption root"));
4626 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4628 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4632 * On failure, we still want to remount any filesystems that
4633 * were previously mounted, so we don't alter the system state.
4636 (void) changelist_postfix(cl
);
4639 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4640 ret
= changelist_postfix(cl
);
4645 if (parentname
!= NULL
) {
4652 changelist_free(cl
);
4658 zfs_get_all_props(zfs_handle_t
*zhp
)
4660 return (zhp
->zfs_props
);
4664 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4666 if (zhp
->zfs_recvd_props
== NULL
)
4667 if (get_recvd_props_ioctl(zhp
) != 0)
4669 return (zhp
->zfs_recvd_props
);
4673 zfs_get_user_props(zfs_handle_t
*zhp
)
4675 return (zhp
->zfs_user_props
);
4679 * This function is used by 'zfs list' to determine the exact set of columns to
4680 * display, and their maximum widths. This does two main things:
4682 * - If this is a list of all properties, then expand the list to include
4683 * all native properties, and set a flag so that for each dataset we look
4684 * for new unique user properties and add them to the list.
4686 * - For non fixed-width properties, keep track of the maximum width seen
4687 * so that we can size the column appropriately. If the user has
4688 * requested received property values, we also need to compute the width
4689 * of the RECEIVED column.
4692 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4695 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4696 zprop_list_t
*entry
;
4697 zprop_list_t
**last
, **start
;
4698 nvlist_t
*userprops
, *propval
;
4701 char buf
[ZFS_MAXPROPLEN
];
4703 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4706 userprops
= zfs_get_user_props(zhp
);
4709 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4711 * Go through and add any user properties as necessary. We
4712 * start by incrementing our list pointer to the first
4713 * non-native property.
4716 while (*start
!= NULL
) {
4717 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4719 start
= &(*start
)->pl_next
;
4723 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4725 * See if we've already found this property in our list.
4727 for (last
= start
; *last
!= NULL
;
4728 last
= &(*last
)->pl_next
) {
4729 if (strcmp((*last
)->pl_user_prop
,
4730 nvpair_name(elem
)) == 0)
4734 if (*last
== NULL
) {
4735 if ((entry
= zfs_alloc(hdl
,
4736 sizeof (zprop_list_t
))) == NULL
||
4737 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4738 nvpair_name(elem
)))) == NULL
) {
4743 entry
->pl_prop
= ZPROP_INVAL
;
4744 entry
->pl_width
= strlen(nvpair_name(elem
));
4745 entry
->pl_all
= B_TRUE
;
4752 * Now go through and check the width of any non-fixed columns
4754 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4755 if (entry
->pl_fixed
&& !literal
)
4758 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4759 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4760 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4761 if (strlen(buf
) > entry
->pl_width
)
4762 entry
->pl_width
= strlen(buf
);
4764 if (received
&& zfs_prop_get_recvd(zhp
,
4765 zfs_prop_to_name(entry
->pl_prop
),
4766 buf
, sizeof (buf
), literal
) == 0)
4767 if (strlen(buf
) > entry
->pl_recvd_width
)
4768 entry
->pl_recvd_width
= strlen(buf
);
4770 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4772 verify(nvlist_lookup_string(propval
,
4773 ZPROP_VALUE
, &strval
) == 0);
4774 if (strlen(strval
) > entry
->pl_width
)
4775 entry
->pl_width
= strlen(strval
);
4777 if (received
&& zfs_prop_get_recvd(zhp
,
4778 entry
->pl_user_prop
,
4779 buf
, sizeof (buf
), literal
) == 0)
4780 if (strlen(buf
) > entry
->pl_recvd_width
)
4781 entry
->pl_recvd_width
= strlen(buf
);
4789 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4795 * Keep a reference to the props-table against which we prune the
4798 zhp
->zfs_props_table
= props
;
4800 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4803 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4804 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4807 * User properties will result in ZPROP_INVAL, and since we
4808 * only know how to prune standard ZFS properties, we always
4809 * leave these in the list. This can also happen if we
4810 * encounter an unknown DSL property (when running older
4811 * software, for example).
4813 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4814 (void) nvlist_remove(zhp
->zfs_props
,
4815 nvpair_name(curr
), nvpair_type(curr
));
4821 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4822 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4824 zfs_cmd_t zc
= {"\0"};
4825 nvlist_t
*nvlist
= NULL
;
4828 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4829 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4830 zc
.zc_cookie
= (uint64_t)cmd
;
4832 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4833 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4834 (void) no_memory(hdl
);
4840 case ZFS_SMB_ACL_ADD
:
4841 case ZFS_SMB_ACL_REMOVE
:
4842 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4844 case ZFS_SMB_ACL_RENAME
:
4845 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4847 (void) no_memory(hdl
);
4850 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4852 (void) no_memory(hdl
);
4855 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4856 nvlist_free(nvlist
);
4860 case ZFS_SMB_ACL_PURGE
:
4865 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4866 nvlist_free(nvlist
);
4871 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4872 char *path
, char *resource
)
4874 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4879 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4880 char *path
, char *resource
)
4882 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4887 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4889 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4894 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4895 char *oldname
, char *newname
)
4897 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4902 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4903 zfs_userspace_cb_t func
, void *arg
)
4905 zfs_cmd_t zc
= {"\0"};
4906 zfs_useracct_t buf
[100];
4907 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4910 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4912 zc
.zc_objset_type
= type
;
4913 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4916 zfs_useracct_t
*zua
= buf
;
4918 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4919 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4922 if ((errno
== ENOTSUP
&&
4923 (type
== ZFS_PROP_USEROBJUSED
||
4924 type
== ZFS_PROP_GROUPOBJUSED
||
4925 type
== ZFS_PROP_USEROBJQUOTA
||
4926 type
== ZFS_PROP_GROUPOBJQUOTA
||
4927 type
== ZFS_PROP_PROJECTOBJUSED
||
4928 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4929 type
== ZFS_PROP_PROJECTUSED
||
4930 type
== ZFS_PROP_PROJECTQUOTA
)))
4933 (void) snprintf(errbuf
, sizeof (errbuf
),
4934 dgettext(TEXT_DOMAIN
,
4935 "cannot get used/quota for %s"), zc
.zc_name
);
4936 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4938 if (zc
.zc_nvlist_dst_size
== 0)
4941 while (zc
.zc_nvlist_dst_size
> 0) {
4942 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4943 zua
->zu_space
)) != 0)
4946 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4955 const char *snapname
;
4957 boolean_t recursive
;
4962 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4964 struct holdarg
*ha
= arg
;
4965 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4968 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4969 ha
->snapname
) >= sizeof (name
))
4972 if (lzc_exists(name
))
4973 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4976 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4982 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4983 boolean_t recursive
, int cleanup_fd
)
4988 ha
.nvl
= fnvlist_alloc();
4989 ha
.snapname
= snapname
;
4991 ha
.recursive
= recursive
;
4992 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4994 if (nvlist_empty(ha
.nvl
)) {
4997 fnvlist_free(ha
.nvl
);
4999 (void) snprintf(errbuf
, sizeof (errbuf
),
5000 dgettext(TEXT_DOMAIN
,
5001 "cannot hold snapshot '%s@%s'"),
5002 zhp
->zfs_name
, snapname
);
5003 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
5007 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
5008 fnvlist_free(ha
.nvl
);
5014 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
5018 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5023 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
5026 /* There may be errors even in the success case. */
5027 fnvlist_free(errors
);
5031 if (nvlist_empty(errors
)) {
5032 /* no hold-specific errors */
5033 (void) snprintf(errbuf
, sizeof (errbuf
),
5034 dgettext(TEXT_DOMAIN
, "cannot hold"));
5037 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5038 "pool must be upgraded"));
5039 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5042 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5045 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5049 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5051 elem
= nvlist_next_nvpair(errors
, elem
)) {
5052 (void) snprintf(errbuf
, sizeof (errbuf
),
5053 dgettext(TEXT_DOMAIN
,
5054 "cannot hold snapshot '%s'"), nvpair_name(elem
));
5055 switch (fnvpair_value_int32(elem
)) {
5058 * Temporary tags wind up having the ds object id
5059 * prepended. So even if we passed the length check
5060 * above, it's still possible for the tag to wind
5061 * up being slightly too long.
5063 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5066 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5069 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5072 (void) zfs_standard_error(hdl
,
5073 fnvpair_value_int32(elem
), errbuf
);
5077 fnvlist_free(errors
);
5082 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5084 struct holdarg
*ha
= arg
;
5085 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5087 nvlist_t
*existing_holds
;
5089 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5090 ha
->snapname
) >= sizeof (name
)) {
5095 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5097 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5100 nvlist_t
*torelease
= fnvlist_alloc();
5101 fnvlist_add_boolean(torelease
, ha
->tag
);
5102 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5103 fnvlist_free(torelease
);
5107 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5113 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5114 boolean_t recursive
)
5118 nvlist_t
*errors
= NULL
;
5120 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5123 ha
.nvl
= fnvlist_alloc();
5124 ha
.snapname
= snapname
;
5126 ha
.recursive
= recursive
;
5128 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5130 if (nvlist_empty(ha
.nvl
)) {
5131 fnvlist_free(ha
.nvl
);
5133 (void) snprintf(errbuf
, sizeof (errbuf
),
5134 dgettext(TEXT_DOMAIN
,
5135 "cannot release hold from snapshot '%s@%s'"),
5136 zhp
->zfs_name
, snapname
);
5138 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5140 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5145 ret
= lzc_release(ha
.nvl
, &errors
);
5146 fnvlist_free(ha
.nvl
);
5149 /* There may be errors even in the success case. */
5150 fnvlist_free(errors
);
5154 if (nvlist_empty(errors
)) {
5155 /* no hold-specific errors */
5156 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5160 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5161 "pool must be upgraded"));
5162 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5165 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5169 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5171 elem
= nvlist_next_nvpair(errors
, elem
)) {
5172 (void) snprintf(errbuf
, sizeof (errbuf
),
5173 dgettext(TEXT_DOMAIN
,
5174 "cannot release hold from snapshot '%s'"),
5176 switch (fnvpair_value_int32(elem
)) {
5178 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5181 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5184 (void) zfs_standard_error_fmt(hdl
,
5185 fnvpair_value_int32(elem
), errbuf
);
5189 fnvlist_free(errors
);
5194 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5196 zfs_cmd_t zc
= {"\0"};
5197 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5203 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5204 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5208 nvbuf
= malloc(nvsz
);
5209 if (nvbuf
== NULL
) {
5210 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5214 zc
.zc_nvlist_dst_size
= nvsz
;
5215 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5217 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5219 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5220 (void) snprintf(errbuf
, sizeof (errbuf
),
5221 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5226 nvsz
= zc
.zc_nvlist_dst_size
;
5230 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5231 "pool must be upgraded"));
5232 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5235 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5238 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5241 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5246 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5248 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
5249 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5251 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
5261 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5263 zfs_cmd_t zc
= {"\0"};
5264 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5270 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5271 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5273 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5276 nvbuf
= malloc(nvsz
);
5278 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5281 zc
.zc_nvlist_src_size
= nvsz
;
5282 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5283 zc
.zc_perm_action
= un
;
5285 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5287 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5288 (void) snprintf(errbuf
, sizeof (errbuf
),
5289 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5293 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5294 "pool must be upgraded"));
5295 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5298 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5301 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5304 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5315 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5320 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5323 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5325 (void) snprintf(errbuf
, sizeof (errbuf
),
5326 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5330 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5331 "pool must be upgraded"));
5332 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5335 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5338 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5341 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5350 * Convert the zvol's volume size to an appropriate reservation.
5351 * Note: If this routine is updated, it is necessary to update the ZFS test
5352 * suite's shell version in reservation.kshlib.
5355 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
5358 uint64_t nblocks
, volblocksize
;
5362 if (nvlist_lookup_string(props
,
5363 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5364 ncopies
= atoi(strval
);
5367 if (nvlist_lookup_uint64(props
,
5368 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5369 &volblocksize
) != 0)
5370 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5371 nblocks
= volsize
/volblocksize
;
5372 /* start with metadnode L0-L6 */
5374 /* calculate number of indirects */
5375 while (nblocks
> 1) {
5376 nblocks
+= DNODES_PER_LEVEL
- 1;
5377 nblocks
/= DNODES_PER_LEVEL
;
5380 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5383 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5384 * compressed, but in practice they compress down to about
5387 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;