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, 2018 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017-2018 RackTop Systems.
33 * Copyright (c) 2019 Datto Inc.
34 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
56 #include <directory.h>
57 #endif /* HAVE_IDMAP */
59 #include <sys/dnode.h>
62 #include <sys/dsl_crypt.h>
66 #include "zfs_namecheck.h"
68 #include "libzfs_impl.h"
70 #include "zfs_deleg.h"
72 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
73 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
76 * Given a single type (not a mask of types), return the type in a human
80 zfs_type_to_name(zfs_type_t type
)
83 case ZFS_TYPE_FILESYSTEM
:
84 return (dgettext(TEXT_DOMAIN
, "filesystem"));
85 case ZFS_TYPE_SNAPSHOT
:
86 return (dgettext(TEXT_DOMAIN
, "snapshot"));
88 return (dgettext(TEXT_DOMAIN
, "volume"));
90 return (dgettext(TEXT_DOMAIN
, "pool"));
91 case ZFS_TYPE_BOOKMARK
:
92 return (dgettext(TEXT_DOMAIN
, "bookmark"));
94 assert(!"unhandled zfs_type_t");
101 * Validate a ZFS path. This is used even before trying to open the dataset, to
102 * provide a more meaningful error message. We call zfs_error_aux() to
103 * explain exactly why the name was not valid.
106 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
112 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
115 "snapshot delimiter '@' is not expected here"));
119 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
121 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
122 "missing '@' delimiter in snapshot name, "
123 "did you mean to use -r?"));
127 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
129 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
130 "bookmark delimiter '#' is not expected here"));
134 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
136 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
137 "missing '#' delimiter in bookmark name, "
138 "did you mean to use -r?"));
142 if (modifying
&& strchr(path
, '%') != NULL
) {
144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
145 "invalid character %c in name"), '%');
149 if (entity_namecheck(path
, &why
, &what
) != 0) {
152 case NAME_ERR_TOOLONG
:
153 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
154 "name is too long"));
157 case NAME_ERR_LEADING_SLASH
:
158 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
159 "leading slash in name"));
162 case NAME_ERR_EMPTY_COMPONENT
:
163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
164 "empty component or misplaced '@'"
165 " or '#' delimiter in name"));
168 case NAME_ERR_TRAILING_SLASH
:
169 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
170 "trailing slash in name"));
173 case NAME_ERR_INVALCHAR
:
175 dgettext(TEXT_DOMAIN
, "invalid character "
176 "'%c' in name"), what
);
179 case NAME_ERR_MULTIPLE_DELIMITERS
:
180 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
181 "multiple '@' and/or '#' delimiters in "
185 case NAME_ERR_NOLETTER
:
186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
187 "pool doesn't begin with a letter"));
190 case NAME_ERR_RESERVED
:
191 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
192 "name is reserved"));
195 case NAME_ERR_DISKLIKE
:
196 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
197 "reserved disk name"));
200 case NAME_ERR_SELF_REF
:
201 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
202 "self reference, '.' is found in name"));
205 case NAME_ERR_PARENT_REF
:
206 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
207 "parent reference, '..' is found in name"));
211 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
212 "(%d) not defined"), why
);
224 zfs_name_valid(const char *name
, zfs_type_t type
)
226 if (type
== ZFS_TYPE_POOL
)
227 return (zpool_name_valid(NULL
, B_FALSE
, name
));
228 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
232 * This function takes the raw DSL properties, and filters out the user-defined
233 * properties into a separate nvlist.
236 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
238 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
243 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
244 (void) no_memory(hdl
);
249 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
250 if (!zfs_prop_user(nvpair_name(elem
)))
253 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
254 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
256 (void) no_memory(hdl
);
264 static zpool_handle_t
*
265 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
267 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
270 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
271 if (hdl
->libzfs_pool_handles
!= NULL
)
272 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
273 hdl
->libzfs_pool_handles
= zph
;
278 static zpool_handle_t
*
279 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
281 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
282 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
284 while ((zph
!= NULL
) &&
285 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
286 zph
= zph
->zpool_next
;
291 * Returns a handle to the pool that contains the provided dataset.
292 * If a handle to that pool already exists then that handle is returned.
293 * Otherwise, a new handle is created and added to the list of handles.
295 static zpool_handle_t
*
296 zpool_handle(zfs_handle_t
*zhp
)
302 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
303 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
304 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
306 zph
= zpool_find_handle(zhp
, pool_name
, len
);
308 zph
= zpool_add_handle(zhp
, pool_name
);
315 zpool_free_handles(libzfs_handle_t
*hdl
)
317 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
319 while (zph
!= NULL
) {
320 next
= zph
->zpool_next
;
324 hdl
->libzfs_pool_handles
= NULL
;
328 * Utility function to gather stats (objset and zpl) for the given object.
331 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
333 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
335 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
337 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
338 if (errno
== ENOMEM
) {
339 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
350 * Utility function to get the received properties of the given object.
353 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
355 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
356 nvlist_t
*recvdprops
;
357 zfs_cmd_t zc
= {"\0"};
360 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
363 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
365 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
366 if (errno
== ENOMEM
) {
367 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
371 zcmd_free_nvlists(&zc
);
376 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
377 zcmd_free_nvlists(&zc
);
381 nvlist_free(zhp
->zfs_recvd_props
);
382 zhp
->zfs_recvd_props
= recvdprops
;
388 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
390 nvlist_t
*allprops
, *userprops
;
392 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
394 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
399 * XXX Why do we store the user props separately, in addition to
400 * storing them in zfs_props?
402 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
403 nvlist_free(allprops
);
407 nvlist_free(zhp
->zfs_props
);
408 nvlist_free(zhp
->zfs_user_props
);
410 zhp
->zfs_props
= allprops
;
411 zhp
->zfs_user_props
= userprops
;
417 get_stats(zfs_handle_t
*zhp
)
420 zfs_cmd_t zc
= {"\0"};
422 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
424 if (get_stats_ioctl(zhp
, &zc
) != 0)
426 else if (put_stats_zhdl(zhp
, &zc
) != 0)
428 zcmd_free_nvlists(&zc
);
433 * Refresh the properties currently stored in the handle.
436 zfs_refresh_properties(zfs_handle_t
*zhp
)
438 (void) get_stats(zhp
);
442 * Makes a handle from the given dataset name. Used by zfs_open() and
443 * zfs_iter_* to create child handles on the fly.
446 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
448 if (put_stats_zhdl(zhp
, zc
) != 0)
452 * We've managed to open the dataset and gather statistics. Determine
453 * the high-level type.
455 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
456 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
457 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
458 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
459 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
464 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
465 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
466 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
467 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
468 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
469 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
471 abort(); /* we should never see any other types */
473 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
480 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
482 zfs_cmd_t zc
= {"\0"};
484 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
490 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
491 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
495 if (get_stats_ioctl(zhp
, &zc
) == -1) {
496 zcmd_free_nvlists(&zc
);
500 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
504 zcmd_free_nvlists(&zc
);
509 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
511 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
517 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
518 if (make_dataset_handle_common(zhp
, zc
) == -1) {
526 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
528 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
533 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
534 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
535 zhp
->zfs_head_type
= pzhp
->zfs_type
;
536 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
537 zhp
->zpool_hdl
= zpool_handle(zhp
);
543 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
545 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
550 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
551 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
552 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
553 sizeof (zhp
->zfs_name
));
554 zhp
->zfs_type
= zhp_orig
->zfs_type
;
555 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
556 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
557 if (zhp_orig
->zfs_props
!= NULL
) {
558 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
559 (void) no_memory(zhp
->zfs_hdl
);
564 if (zhp_orig
->zfs_user_props
!= NULL
) {
565 if (nvlist_dup(zhp_orig
->zfs_user_props
,
566 &zhp
->zfs_user_props
, 0) != 0) {
567 (void) no_memory(zhp
->zfs_hdl
);
572 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
573 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
574 &zhp
->zfs_recvd_props
, 0)) {
575 (void) no_memory(zhp
->zfs_hdl
);
580 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
581 if (zhp_orig
->zfs_mntopts
!= NULL
) {
582 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
583 zhp_orig
->zfs_mntopts
);
585 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
590 zfs_bookmark_exists(const char *path
)
594 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
600 (void) strlcpy(fsname
, path
, sizeof (fsname
));
601 pound
= strchr(fsname
, '#');
606 bmark_name
= pound
+ 1;
607 props
= fnvlist_alloc();
608 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
615 rv
= nvlist_exists(bmarks
, bmark_name
);
621 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
622 nvlist_t
*bmark_props
)
624 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
629 /* Fill in the name. */
630 zhp
->zfs_hdl
= parent
->zfs_hdl
;
631 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
633 /* Set the property lists. */
634 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
640 zhp
->zfs_head_type
= parent
->zfs_head_type
;
641 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
643 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
644 nvlist_free(zhp
->zfs_props
);
652 struct zfs_open_bookmarks_cb_data
{
658 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
660 struct zfs_open_bookmarks_cb_data
*dp
= data
;
663 * Is it the one we are looking for?
665 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
667 * We found it. Save it and let the caller know we are done.
674 * Not found. Close the handle and ask for another one.
681 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
682 * argument is a mask of acceptable types. The function will print an
683 * appropriate error message and return NULL if it can't be opened.
686 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
692 (void) snprintf(errbuf
, sizeof (errbuf
),
693 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
696 * Validate the name before we even try to open it.
698 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
699 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
704 * Bookmarks needs to be handled separately.
706 bookp
= strchr(path
, '#');
709 * Try to get stats for the dataset, which will tell us if it
713 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
714 (void) zfs_standard_error(hdl
, errno
, errbuf
);
718 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
720 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
723 * We need to cut out '#' and everything after '#'
724 * to get the parent dataset name only.
726 assert(bookp
- path
< sizeof (dsname
));
727 (void) strncpy(dsname
, path
, bookp
- path
);
728 dsname
[bookp
- path
] = '\0';
731 * Create handle for the parent dataset.
734 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
735 (void) zfs_standard_error(hdl
, errno
, errbuf
);
740 * Iterate bookmarks to find the right one.
743 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
744 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
745 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
749 if (cb_data
.zhp
== NULL
) {
750 (void) zfs_standard_error(hdl
, errno
, errbuf
);
762 if (!(types
& zhp
->zfs_type
)) {
763 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
772 * Release a ZFS handle. Nothing to do but free the associated memory.
775 zfs_close(zfs_handle_t
*zhp
)
777 if (zhp
->zfs_mntopts
)
778 free(zhp
->zfs_mntopts
);
779 nvlist_free(zhp
->zfs_props
);
780 nvlist_free(zhp
->zfs_user_props
);
781 nvlist_free(zhp
->zfs_recvd_props
);
785 typedef struct mnttab_node
{
786 struct mnttab mtn_mt
;
791 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
793 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
794 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
797 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
799 return (AVL_ISIGN(rv
));
803 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
805 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
806 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
807 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
808 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
812 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
816 /* Reopen MNTTAB to prevent reading stale data from open file */
817 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
820 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
824 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
827 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
828 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
829 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
830 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
831 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
833 /* Exclude duplicate mounts */
834 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
835 free(mtn
->mtn_mt
.mnt_special
);
836 free(mtn
->mtn_mt
.mnt_mountp
);
837 free(mtn
->mtn_mt
.mnt_fstype
);
838 free(mtn
->mtn_mt
.mnt_mntopts
);
843 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
850 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
855 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
857 free(mtn
->mtn_mt
.mnt_special
);
858 free(mtn
->mtn_mt
.mnt_mountp
);
859 free(mtn
->mtn_mt
.mnt_fstype
);
860 free(mtn
->mtn_mt
.mnt_mntopts
);
863 avl_destroy(&hdl
->libzfs_mnttab_cache
);
864 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
868 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
870 hdl
->libzfs_mnttab_enable
= enable
;
874 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
875 struct mnttab
*entry
)
881 if (!hdl
->libzfs_mnttab_enable
) {
882 struct mnttab srch
= { 0 };
884 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
885 libzfs_mnttab_fini(hdl
);
887 /* Reopen MNTTAB to prevent reading stale data from open file */
888 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
891 srch
.mnt_special
= (char *)fsname
;
892 srch
.mnt_fstype
= MNTTYPE_ZFS
;
893 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
899 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
900 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
903 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
904 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
909 find
.mtn_mt
.mnt_special
= (char *)fsname
;
910 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
912 *entry
= mtn
->mtn_mt
;
915 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
920 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
921 const char *mountp
, const char *mntopts
)
925 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
926 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
927 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
928 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
929 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
930 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
931 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
933 * Another thread may have already added this entry
934 * via libzfs_mnttab_update. If so we should skip it.
936 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
)
939 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
941 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
945 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
950 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
951 find
.mtn_mt
.mnt_special
= (char *)fsname
;
952 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
954 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
955 free(ret
->mtn_mt
.mnt_special
);
956 free(ret
->mtn_mt
.mnt_mountp
);
957 free(ret
->mtn_mt
.mnt_fstype
);
958 free(ret
->mtn_mt
.mnt_mntopts
);
961 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
965 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
967 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
969 if (zpool_handle
== NULL
)
972 *spa_version
= zpool_get_prop_int(zpool_handle
,
973 ZPOOL_PROP_VERSION
, NULL
);
978 * The choice of reservation property depends on the SPA version.
981 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
985 if (zfs_spa_version(zhp
, &spa_version
) < 0)
988 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
989 *resv_prop
= ZFS_PROP_REFRESERVATION
;
991 *resv_prop
= ZFS_PROP_RESERVATION
;
997 * Given an nvlist of properties to set, validates that they are correct, and
998 * parses any numeric properties (index, boolean, etc) if they are specified as
1002 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1003 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1004 boolean_t key_params_ok
, const char *errbuf
)
1011 int chosen_normal
= -1;
1012 int chosen_utf
= -1;
1014 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1015 (void) no_memory(hdl
);
1020 * Make sure this property is valid and applies to this type.
1024 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1025 const char *propname
= nvpair_name(elem
);
1027 prop
= zfs_name_to_prop(propname
);
1028 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
1030 * This is a user property: make sure it's a
1031 * string, and that it's less than ZAP_MAXNAMELEN.
1033 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1034 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1035 "'%s' must be a string"), propname
);
1036 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1040 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1042 "property name '%s' is too long"),
1044 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1048 (void) nvpair_value_string(elem
, &strval
);
1049 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1050 (void) no_memory(hdl
);
1057 * Currently, only user properties can be modified on
1060 if (type
== ZFS_TYPE_SNAPSHOT
) {
1061 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1062 "this property can not be modified for snapshots"));
1063 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1067 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1068 zfs_userquota_prop_t uqtype
;
1069 char *newpropname
= NULL
;
1075 if (userquota_propname_decode(propname
, zoned
,
1076 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1078 dgettext(TEXT_DOMAIN
,
1079 "'%s' has an invalid user/group name"),
1081 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1085 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1086 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1087 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1088 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1089 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1090 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1092 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1094 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1099 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1100 (void) nvpair_value_string(elem
, &strval
);
1101 if (strcmp(strval
, "none") == 0) {
1103 } else if (zfs_nicestrtonum(hdl
,
1104 strval
, &intval
) != 0) {
1105 (void) zfs_error(hdl
,
1106 EZFS_BADPROP
, errbuf
);
1109 } else if (nvpair_type(elem
) ==
1111 (void) nvpair_value_uint64(elem
, &intval
);
1113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1114 "use 'none' to disable "
1115 "{user|group|project}quota"));
1119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1120 "'%s' must be a number"), propname
);
1121 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1126 * Encode the prop name as
1127 * userquota@<hex-rid>-domain, to make it easy
1128 * for the kernel to decode.
1130 rc
= asprintf(&newpropname
, "%s%llx-%s",
1131 zfs_userquota_prop_prefixes
[uqtype
],
1132 (longlong_t
)rid
, domain
);
1133 if (rc
== -1 || newpropname
== NULL
) {
1134 (void) no_memory(hdl
);
1141 if (nvlist_add_uint64_array(ret
, newpropname
,
1144 (void) no_memory(hdl
);
1149 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1150 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1151 "'%s' is readonly"),
1153 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1157 if (prop
== ZPROP_INVAL
) {
1158 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1159 "invalid property '%s'"), propname
);
1160 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1164 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1166 dgettext(TEXT_DOMAIN
, "'%s' does not "
1167 "apply to datasets of this type"), propname
);
1168 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1172 if (zfs_prop_readonly(prop
) &&
1173 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1174 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1176 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1178 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1182 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1183 &strval
, &intval
, errbuf
) != 0)
1187 * Perform some additional checks for specific properties.
1190 case ZFS_PROP_VERSION
:
1196 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1197 if (intval
< version
) {
1198 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1199 "Can not downgrade; already at version %u"),
1201 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1207 case ZFS_PROP_VOLBLOCKSIZE
:
1208 case ZFS_PROP_RECORDSIZE
:
1210 int maxbs
= SPA_MAXBLOCKSIZE
;
1213 if (zpool_hdl
!= NULL
) {
1214 maxbs
= zpool_get_prop_int(zpool_hdl
,
1215 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1218 * The value must be a power of two between
1219 * SPA_MINBLOCKSIZE and maxbs.
1221 if (intval
< SPA_MINBLOCKSIZE
||
1222 intval
> maxbs
|| !ISP2(intval
)) {
1223 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1224 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1225 "'%s' must be power of 2 from 512B "
1226 "to %s"), propname
, buf
);
1227 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1233 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1234 if (zpool_hdl
!= NULL
) {
1235 char state
[64] = "";
1238 * Issue a warning but do not fail so that
1239 * tests for setable properties succeed.
1241 if (zpool_prop_get_feature(zpool_hdl
,
1242 "feature@allocation_classes", state
,
1243 sizeof (state
)) != 0 ||
1244 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1245 (void) fprintf(stderr
, gettext(
1246 "%s: property requires a special "
1247 "device in the pool\n"), propname
);
1251 (intval
< SPA_MINBLOCKSIZE
||
1252 intval
> SPA_OLD_MAXBLOCKSIZE
|| !ISP2(intval
))) {
1253 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1254 "invalid '%s=%d' property: must be zero or "
1255 "a power of 2 from 512B to 128K"), propname
,
1257 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1262 case ZFS_PROP_MLSLABEL
:
1264 #ifdef HAVE_MLSLABEL
1266 * Verify the mlslabel string and convert to
1267 * internal hex label string.
1271 char *hex
= NULL
; /* internal label string */
1273 /* Default value is already OK. */
1274 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1277 /* Verify the label can be converted to binary form */
1278 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1279 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1280 L_NO_CORRECTION
, NULL
) == -1)) {
1284 /* Now translate to hex internal label string */
1285 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1291 m_label_free(new_sl
);
1293 /* If string is already in internal form, we're done. */
1294 if (strcmp(strval
, hex
) == 0) {
1299 /* Replace the label string with the internal form. */
1300 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1302 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1309 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1310 "invalid mlslabel '%s'"), strval
);
1311 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1312 m_label_free(new_sl
); /* OK if null */
1315 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1316 "mlslabels are unsupported"));
1317 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1319 #endif /* HAVE_MLSLABEL */
1322 case ZFS_PROP_MOUNTPOINT
:
1324 namecheck_err_t why
;
1326 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1327 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1330 if (mountpoint_namecheck(strval
, &why
)) {
1332 case NAME_ERR_LEADING_SLASH
:
1334 dgettext(TEXT_DOMAIN
,
1335 "'%s' must be an absolute path, "
1336 "'none', or 'legacy'"), propname
);
1338 case NAME_ERR_TOOLONG
:
1340 dgettext(TEXT_DOMAIN
,
1341 "component of '%s' is too long"),
1347 dgettext(TEXT_DOMAIN
,
1348 "(%d) not defined"),
1352 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1359 case ZFS_PROP_SHARESMB
:
1360 case ZFS_PROP_SHARENFS
:
1362 * For the mountpoint and sharenfs or sharesmb
1363 * properties, check if it can be set in a
1364 * global/non-global zone based on
1365 * the zoned property value:
1367 * global zone non-global zone
1368 * --------------------------------------------------
1369 * zoned=on mountpoint (no) mountpoint (yes)
1370 * sharenfs (no) sharenfs (no)
1371 * sharesmb (no) sharesmb (no)
1373 * zoned=off mountpoint (yes) N/A
1378 if (getzoneid() == GLOBAL_ZONEID
) {
1379 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1380 "'%s' cannot be set on "
1381 "dataset in a non-global zone"),
1383 (void) zfs_error(hdl
, EZFS_ZONED
,
1386 } else if (prop
== ZFS_PROP_SHARENFS
||
1387 prop
== ZFS_PROP_SHARESMB
) {
1388 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1389 "'%s' cannot be set in "
1390 "a non-global zone"), propname
);
1391 (void) zfs_error(hdl
, EZFS_ZONED
,
1395 } else if (getzoneid() != GLOBAL_ZONEID
) {
1397 * If zoned property is 'off', this must be in
1398 * a global zone. If not, something is wrong.
1400 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1401 "'%s' cannot be set while dataset "
1402 "'zoned' property is set"), propname
);
1403 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1408 * At this point, it is legitimate to set the
1409 * property. Now we want to make sure that the
1410 * property value is valid if it is sharenfs.
1412 if ((prop
== ZFS_PROP_SHARENFS
||
1413 prop
== ZFS_PROP_SHARESMB
) &&
1414 strcmp(strval
, "on") != 0 &&
1415 strcmp(strval
, "off") != 0) {
1416 zfs_share_proto_t proto
;
1418 if (prop
== ZFS_PROP_SHARESMB
)
1424 * Must be an valid sharing protocol
1425 * option string so init the libshare
1426 * in order to enable the parser and
1427 * then parse the options. We use the
1428 * control API since we don't care about
1429 * the current configuration and don't
1430 * want the overhead of loading it
1431 * until we actually do something.
1434 if (zfs_init_libshare(hdl
,
1435 SA_INIT_CONTROL_API
) != SA_OK
) {
1437 * An error occurred so we can't do
1440 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1441 "'%s' cannot be set: problem "
1442 "in share initialization"),
1444 (void) zfs_error(hdl
, EZFS_BADPROP
,
1449 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1451 * There was an error in parsing so
1452 * deal with it by issuing an error
1453 * message and leaving after
1454 * uninitializing the the libshare
1457 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1458 "'%s' cannot be set to invalid "
1459 "options"), propname
);
1460 (void) zfs_error(hdl
, EZFS_BADPROP
,
1462 zfs_uninit_libshare(hdl
);
1465 zfs_uninit_libshare(hdl
);
1470 case ZFS_PROP_KEYLOCATION
:
1471 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1473 "invalid keylocation"));
1474 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1480 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1482 if (crypt
== ZIO_CRYPT_OFF
&&
1483 strcmp(strval
, "none") != 0) {
1484 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1485 "keylocation must be 'none' "
1486 "for unencrypted datasets"));
1487 (void) zfs_error(hdl
, EZFS_BADPROP
,
1490 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1491 strcmp(strval
, "none") == 0) {
1492 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1493 "keylocation must not be 'none' "
1494 "for encrypted datasets"));
1495 (void) zfs_error(hdl
, EZFS_BADPROP
,
1502 case ZFS_PROP_PBKDF2_ITERS
:
1503 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1505 "minimum pbkdf2 iterations is %u"),
1506 MIN_PBKDF2_ITERATIONS
);
1507 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1512 case ZFS_PROP_UTF8ONLY
:
1513 chosen_utf
= (int)intval
;
1516 case ZFS_PROP_NORMALIZE
:
1517 chosen_normal
= (int)intval
;
1525 * For changes to existing volumes, we have some additional
1526 * checks to enforce.
1528 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1529 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1530 ZFS_PROP_VOLBLOCKSIZE
);
1534 case ZFS_PROP_VOLSIZE
:
1535 if (intval
% blocksize
!= 0) {
1536 zfs_nicebytes(blocksize
, buf
,
1538 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1539 "'%s' must be a multiple of "
1540 "volume block size (%s)"),
1542 (void) zfs_error(hdl
, EZFS_BADPROP
,
1548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1549 "'%s' cannot be zero"),
1551 (void) zfs_error(hdl
, EZFS_BADPROP
,
1562 /* check encryption properties */
1564 int64_t crypt
= zfs_prop_get_int(zhp
,
1565 ZFS_PROP_ENCRYPTION
);
1568 case ZFS_PROP_COPIES
:
1569 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1570 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1571 "encrypted datasets cannot have "
1573 (void) zfs_error(hdl
, EZFS_BADPROP
,
1585 * If normalization was chosen, but no UTF8 choice was made,
1586 * enforce rejection of non-UTF8 names.
1588 * If normalization was chosen, but rejecting non-UTF8 names
1589 * was explicitly not chosen, it is an error.
1591 if (chosen_normal
> 0 && chosen_utf
< 0) {
1592 if (nvlist_add_uint64(ret
,
1593 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1594 (void) no_memory(hdl
);
1597 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1598 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1599 "'%s' must be set 'on' if normalization chosen"),
1600 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1601 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1612 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1614 uint64_t old_volsize
;
1615 uint64_t new_volsize
;
1616 uint64_t old_reservation
;
1617 uint64_t new_reservation
;
1618 zfs_prop_t resv_prop
;
1622 * If this is an existing volume, and someone is setting the volsize,
1623 * make sure that it matches the reservation, or add it if necessary.
1625 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1626 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1628 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1630 props
= fnvlist_alloc();
1631 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1632 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1634 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1635 old_reservation
) || nvlist_exists(nvl
,
1636 zfs_prop_to_name(resv_prop
))) {
1637 fnvlist_free(props
);
1640 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1641 &new_volsize
) != 0) {
1642 fnvlist_free(props
);
1645 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1646 fnvlist_free(props
);
1648 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1649 new_reservation
) != 0) {
1650 (void) no_memory(zhp
->zfs_hdl
);
1657 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1658 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1659 * Return codes must match zfs_add_synthetic_resv().
1662 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1669 if (!ZFS_IS_VOLUME(zhp
)) {
1673 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1677 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1681 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1682 /* No value being set, so it can't be "auto" */
1685 if (resvsize
!= UINT64_MAX
) {
1686 /* Being set to a value other than "auto" */
1690 props
= fnvlist_alloc();
1692 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1693 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1695 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1697 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1700 resvsize
= zvol_volsize_to_reservation(volsize
, props
);
1701 fnvlist_free(props
);
1703 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1704 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1705 (void) no_memory(zhp
->zfs_hdl
);
1712 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1719 * For quotas and reservations, ENOSPC indicates
1720 * something different; setting a quota or reservation
1721 * doesn't use any disk space.
1724 case ZFS_PROP_QUOTA
:
1725 case ZFS_PROP_REFQUOTA
:
1726 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1727 "size is less than current used or "
1729 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1732 case ZFS_PROP_RESERVATION
:
1733 case ZFS_PROP_REFRESERVATION
:
1734 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1735 "size is greater than available space"));
1736 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1740 (void) zfs_standard_error(hdl
, err
, errbuf
);
1746 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1750 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1754 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1755 "property value too long"));
1756 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1760 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1761 "pool and or dataset must be upgraded to set this "
1762 "property or value"));
1763 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1767 if (prop
== ZFS_PROP_COMPRESSION
||
1768 prop
== ZFS_PROP_DNODESIZE
||
1769 prop
== ZFS_PROP_RECORDSIZE
) {
1770 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1771 "property setting is not allowed on "
1772 "bootable datasets"));
1773 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1774 } else if (prop
== ZFS_PROP_CHECKSUM
||
1775 prop
== ZFS_PROP_DEDUP
) {
1776 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1777 "property setting is not allowed on "
1779 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1781 (void) zfs_standard_error(hdl
, err
, errbuf
);
1786 if (prop
== ZPROP_INVAL
) {
1787 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1789 (void) zfs_standard_error(hdl
, err
, errbuf
);
1794 if (prop
== ZFS_PROP_KEYLOCATION
) {
1795 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1796 "keylocation may only be set on encryption roots"));
1797 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1799 (void) zfs_standard_error(hdl
, err
, errbuf
);
1805 * This platform can't address a volume this big.
1808 if (prop
== ZFS_PROP_VOLSIZE
) {
1809 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1815 (void) zfs_standard_error(hdl
, err
, errbuf
);
1820 zfs_is_namespace_prop(zfs_prop_t prop
)
1824 case ZFS_PROP_ATIME
:
1825 case ZFS_PROP_RELATIME
:
1826 case ZFS_PROP_DEVICES
:
1828 case ZFS_PROP_SETUID
:
1829 case ZFS_PROP_READONLY
:
1830 case ZFS_PROP_XATTR
:
1831 case ZFS_PROP_NBMAND
:
1840 * Given a property name and value, set the property for the given dataset.
1843 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1847 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1848 nvlist_t
*nvl
= NULL
;
1850 (void) snprintf(errbuf
, sizeof (errbuf
),
1851 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1854 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1855 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1856 (void) no_memory(hdl
);
1860 ret
= zfs_prop_set_list(zhp
, nvl
);
1870 * Given an nvlist of property names and values, set the properties for the
1874 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1876 zfs_cmd_t zc
= {"\0"};
1878 prop_changelist_t
**cls
= NULL
;
1881 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1885 zfs_prop_t prop
= 0;
1888 (void) snprintf(errbuf
, sizeof (errbuf
),
1889 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1892 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1893 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1894 B_FALSE
, errbuf
)) == NULL
)
1898 * We have to check for any extra properties which need to be added
1899 * before computing the length of the nvlist.
1901 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1903 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1904 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1905 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1910 if (added_resv
!= 1 &&
1911 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1916 * Check how many properties we're setting and allocate an array to
1917 * store changelist pointers for postfix().
1919 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1921 elem
= nvlist_next_nvpair(nvl
, elem
))
1923 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1927 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1929 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1931 prop
= zfs_name_to_prop(nvpair_name(elem
));
1933 assert(cl_idx
< nvl_len
);
1935 * We don't want to unmount & remount the dataset when changing
1936 * its canmount property to 'on' or 'noauto'. We only use
1937 * the changelist logic to unmount when setting canmount=off.
1939 if (prop
!= ZFS_PROP_CANMOUNT
||
1940 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1941 zfs_is_mounted(zhp
, NULL
))) {
1942 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1943 if (cls
[cl_idx
] == NULL
)
1947 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1948 changelist_haszonedchild(cls
[cl_idx
])) {
1949 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1950 "child dataset with inherited mountpoint is used "
1951 "in a non-global zone"));
1952 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1956 if (cls
[cl_idx
] != NULL
&&
1957 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1962 assert(cl_idx
== nvl_len
);
1965 * Execute the corresponding ioctl() to set this list of properties.
1967 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1969 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1970 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1973 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1976 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1977 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1981 /* Get the list of unset properties back and report them. */
1982 nvlist_t
*errorprops
= NULL
;
1983 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1985 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1987 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1988 prop
= zfs_name_to_prop(nvpair_name(elem
));
1989 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1991 nvlist_free(errorprops
);
1993 if (added_resv
&& errno
== ENOSPC
) {
1994 /* clean up the volsize property we tried to set */
1995 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1999 zcmd_free_nvlists(&zc
);
2001 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
2003 if (nvlist_add_uint64(nvl
,
2004 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
2007 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
2009 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
2012 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
2013 if (cls
[cl_idx
] != NULL
) {
2014 int clp_err
= changelist_postfix(cls
[cl_idx
]);
2022 * Refresh the statistics so the new property
2023 * value is reflected.
2025 (void) get_stats(zhp
);
2028 * Remount the filesystem to propagate the change
2029 * if one of the options handled by the generic
2030 * Linux namespace layer has been modified.
2032 if (zfs_is_namespace_prop(prop
) &&
2033 zfs_is_mounted(zhp
, NULL
))
2034 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2040 zcmd_free_nvlists(&zc
);
2042 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
2043 if (cls
[cl_idx
] != NULL
)
2044 changelist_free(cls
[cl_idx
]);
2052 * Given a property, inherit the value from the parent dataset, or if received
2053 * is TRUE, revert to the received value, if any.
2056 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
2058 zfs_cmd_t zc
= {"\0"};
2060 prop_changelist_t
*cl
;
2061 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2065 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2066 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
2068 zc
.zc_cookie
= received
;
2069 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
2071 * For user properties, the amount of work we have to do is very
2072 * small, so just do it here.
2074 if (!zfs_prop_user(propname
)) {
2075 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2076 "invalid property"));
2077 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2080 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2081 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2083 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
2084 return (zfs_standard_error(hdl
, errno
, errbuf
));
2090 * Verify that this property is inheritable.
2092 if (zfs_prop_readonly(prop
))
2093 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
2095 if (!zfs_prop_inheritable(prop
) && !received
)
2096 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
2099 * Check to see if the value applies to this type
2101 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2102 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
2105 * Normalize the name, to get rid of shorthand abbreviations.
2107 propname
= zfs_prop_to_name(prop
);
2108 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2109 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2111 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
2112 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
2113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2114 "dataset is used in a non-global zone"));
2115 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
2119 * Determine datasets which will be affected by this change, if any.
2121 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2124 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2126 "child dataset with inherited mountpoint is used "
2127 "in a non-global zone"));
2128 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2132 if ((ret
= changelist_prefix(cl
)) != 0)
2135 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2136 return (zfs_standard_error(hdl
, errno
, errbuf
));
2139 if ((ret
= changelist_postfix(cl
)) != 0)
2143 * Refresh the statistics so the new property is reflected.
2145 (void) get_stats(zhp
);
2148 * Remount the filesystem to propagate the change
2149 * if one of the options handled by the generic
2150 * Linux namespace layer has been modified.
2152 if (zfs_is_namespace_prop(prop
) &&
2153 zfs_is_mounted(zhp
, NULL
))
2154 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2158 changelist_free(cl
);
2163 * True DSL properties are stored in an nvlist. The following two functions
2164 * extract them appropriately.
2167 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2173 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2174 zfs_prop_to_name(prop
), &nv
) == 0) {
2175 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2176 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2178 verify(!zhp
->zfs_props_table
||
2179 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2180 value
= zfs_prop_default_numeric(prop
);
2188 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2194 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2195 zfs_prop_to_name(prop
), &nv
) == 0) {
2196 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2197 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2199 verify(!zhp
->zfs_props_table
||
2200 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2201 value
= zfs_prop_default_string(prop
);
2209 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2211 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2215 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2217 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2218 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2222 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2224 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2229 * Internal function for getting a numeric property. Both zfs_prop_get() and
2230 * zfs_prop_get_int() are built using this interface.
2232 * Certain properties can be overridden using 'mount -o'. In this case, scan
2233 * the contents of the /proc/self/mounts entry, searching for the
2234 * appropriate options. If they differ from the on-disk values, report the
2235 * current values and mark the source "temporary".
2238 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2239 char **source
, uint64_t *val
)
2241 zfs_cmd_t zc
= {"\0"};
2242 nvlist_t
*zplprops
= NULL
;
2244 char *mntopt_on
= NULL
;
2245 char *mntopt_off
= NULL
;
2246 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2251 * If the property is being fetched for a snapshot, check whether
2252 * the property is valid for the snapshot's head dataset type.
2254 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2255 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2256 *val
= zfs_prop_default_numeric(prop
);
2261 case ZFS_PROP_ATIME
:
2262 mntopt_on
= MNTOPT_ATIME
;
2263 mntopt_off
= MNTOPT_NOATIME
;
2266 case ZFS_PROP_RELATIME
:
2267 mntopt_on
= MNTOPT_RELATIME
;
2268 mntopt_off
= MNTOPT_NORELATIME
;
2271 case ZFS_PROP_DEVICES
:
2272 mntopt_on
= MNTOPT_DEVICES
;
2273 mntopt_off
= MNTOPT_NODEVICES
;
2277 mntopt_on
= MNTOPT_EXEC
;
2278 mntopt_off
= MNTOPT_NOEXEC
;
2281 case ZFS_PROP_READONLY
:
2282 mntopt_on
= MNTOPT_RO
;
2283 mntopt_off
= MNTOPT_RW
;
2286 case ZFS_PROP_SETUID
:
2287 mntopt_on
= MNTOPT_SETUID
;
2288 mntopt_off
= MNTOPT_NOSETUID
;
2291 case ZFS_PROP_XATTR
:
2292 mntopt_on
= MNTOPT_XATTR
;
2293 mntopt_off
= MNTOPT_NOXATTR
;
2296 case ZFS_PROP_NBMAND
:
2297 mntopt_on
= MNTOPT_NBMAND
;
2298 mntopt_off
= MNTOPT_NONBMAND
;
2306 * Because looking up the mount options is potentially expensive
2307 * (iterating over all of /proc/self/mounts), we defer its
2308 * calculation until we're looking up a property which requires
2311 if (!zhp
->zfs_mntcheck
&&
2312 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2313 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2314 struct mnttab entry
;
2316 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2317 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2319 if (zhp
->zfs_mntopts
== NULL
)
2323 zhp
->zfs_mntcheck
= B_TRUE
;
2326 if (zhp
->zfs_mntopts
== NULL
)
2327 mnt
.mnt_mntopts
= "";
2329 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2332 case ZFS_PROP_ATIME
:
2333 case ZFS_PROP_RELATIME
:
2334 case ZFS_PROP_DEVICES
:
2336 case ZFS_PROP_READONLY
:
2337 case ZFS_PROP_SETUID
:
2338 case ZFS_PROP_XATTR
:
2339 case ZFS_PROP_NBMAND
:
2340 *val
= getprop_uint64(zhp
, prop
, source
);
2345 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2348 *src
= ZPROP_SRC_TEMPORARY
;
2349 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2352 *src
= ZPROP_SRC_TEMPORARY
;
2356 case ZFS_PROP_CANMOUNT
:
2357 case ZFS_PROP_VOLSIZE
:
2358 case ZFS_PROP_QUOTA
:
2359 case ZFS_PROP_REFQUOTA
:
2360 case ZFS_PROP_RESERVATION
:
2361 case ZFS_PROP_REFRESERVATION
:
2362 case ZFS_PROP_FILESYSTEM_LIMIT
:
2363 case ZFS_PROP_SNAPSHOT_LIMIT
:
2364 case ZFS_PROP_FILESYSTEM_COUNT
:
2365 case ZFS_PROP_SNAPSHOT_COUNT
:
2366 *val
= getprop_uint64(zhp
, prop
, source
);
2368 if (*source
== NULL
) {
2369 /* not default, must be local */
2370 *source
= zhp
->zfs_name
;
2374 case ZFS_PROP_MOUNTED
:
2375 *val
= (zhp
->zfs_mntopts
!= NULL
);
2378 case ZFS_PROP_NUMCLONES
:
2379 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2382 case ZFS_PROP_VERSION
:
2383 case ZFS_PROP_NORMALIZE
:
2384 case ZFS_PROP_UTF8ONLY
:
2386 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2388 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2389 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2390 zcmd_free_nvlists(&zc
);
2391 if (prop
== ZFS_PROP_VERSION
&&
2392 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2393 *val
= zfs_prop_default_numeric(prop
);
2396 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2397 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2399 zcmd_free_nvlists(&zc
);
2402 nvlist_free(zplprops
);
2403 zcmd_free_nvlists(&zc
);
2406 case ZFS_PROP_INCONSISTENT
:
2407 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2410 case ZFS_PROP_REDACTED
:
2411 *val
= zhp
->zfs_dmustats
.dds_redacted
;
2415 switch (zfs_prop_get_type(prop
)) {
2416 case PROP_TYPE_NUMBER
:
2417 case PROP_TYPE_INDEX
:
2418 *val
= getprop_uint64(zhp
, prop
, source
);
2420 * If we tried to use a default value for a
2421 * readonly property, it means that it was not
2422 * present. Note this only applies to "truly"
2423 * readonly properties, not set-once properties
2424 * like volblocksize.
2426 if (zfs_prop_readonly(prop
) &&
2427 !zfs_prop_setonce(prop
) &&
2428 *source
!= NULL
&& (*source
)[0] == '\0') {
2434 case PROP_TYPE_STRING
:
2436 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2437 "cannot get non-numeric property"));
2438 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2439 dgettext(TEXT_DOMAIN
, "internal error")));
2447 * Calculate the source type, given the raw source string.
2450 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2451 char *statbuf
, size_t statlen
)
2453 if (statbuf
== NULL
||
2454 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2458 if (source
== NULL
) {
2459 *srctype
= ZPROP_SRC_NONE
;
2460 } else if (source
[0] == '\0') {
2461 *srctype
= ZPROP_SRC_DEFAULT
;
2462 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2463 *srctype
= ZPROP_SRC_RECEIVED
;
2465 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2466 *srctype
= ZPROP_SRC_LOCAL
;
2468 (void) strlcpy(statbuf
, source
, statlen
);
2469 *srctype
= ZPROP_SRC_INHERITED
;
2476 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2477 size_t proplen
, boolean_t literal
)
2482 if (zhp
->zfs_recvd_props
== NULL
)
2483 if (get_recvd_props_ioctl(zhp
) != 0)
2486 prop
= zfs_name_to_prop(propname
);
2488 if (prop
!= ZPROP_INVAL
) {
2490 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2492 zfs_set_recvd_props_mode(zhp
, &cookie
);
2493 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2494 NULL
, NULL
, 0, literal
);
2495 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2499 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2500 propname
, &propval
) != 0)
2502 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2504 (void) strlcpy(propbuf
, recvdval
, proplen
);
2507 return (err
== 0 ? 0 : -1);
2511 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2516 value
= zfs_get_clones_nvl(zhp
);
2521 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2522 pair
= nvlist_next_nvpair(value
, pair
)) {
2523 if (propbuf
[0] != '\0')
2524 (void) strlcat(propbuf
, ",", proplen
);
2525 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2531 struct get_clones_arg
{
2535 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2539 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2541 struct get_clones_arg
*gca
= arg
;
2543 if (gca
->numclones
== 0) {
2548 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2549 NULL
, NULL
, 0, B_TRUE
) != 0)
2551 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2552 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2557 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2563 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2565 nvlist_t
*nv
, *value
;
2567 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2568 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2569 struct get_clones_arg gca
;
2572 * if this is a snapshot, then the kernel wasn't able
2573 * to get the clones. Do it by slowly iterating.
2575 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2577 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2579 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2584 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2586 gca
.origin
= zhp
->zfs_name
;
2588 if (gca
.numclones
!= 0) {
2590 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2593 /* get the pool name */
2594 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2595 (void) strsep(&cp
, "/@");
2596 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2597 ZFS_TYPE_FILESYSTEM
);
2604 (void) get_clones_cb(root
, &gca
);
2607 if (gca
.numclones
!= 0 ||
2608 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2609 nvlist_add_nvlist(zhp
->zfs_props
,
2610 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2617 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2618 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2621 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2627 get_rsnaps_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2633 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2634 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
), &value
) != 0)
2636 if (nvlist_lookup_uint64_array(value
, ZPROP_VALUE
, &snaps
,
2640 /* There's no redaction snapshots; pass a special value back */
2641 (void) snprintf(propbuf
, proplen
, "none");
2645 for (int i
= 0; i
< nsnaps
; i
++) {
2647 if (propbuf
[0] != '\0')
2648 (void) strlcat(propbuf
, ",", proplen
);
2649 (void) snprintf(buf
, sizeof (buf
), "%llu",
2650 (u_longlong_t
)snaps
[i
]);
2651 (void) strlcat(propbuf
, buf
, proplen
);
2658 * Accepts a property and value and checks that the value
2659 * matches the one found by the channel program. If they are
2660 * not equal, print both of them.
2663 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2666 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2669 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2670 const char *prop_name
= zfs_prop_to_name(prop
);
2671 const char *program
=
2673 "ds = args['dataset']\n"
2674 "prop = args['property']\n"
2675 "value, setpoint = zfs.get_prop(ds, prop)\n"
2676 "return {value=value, setpoint=setpoint}\n";
2679 nvlist_t
*argnvl
= fnvlist_alloc();
2681 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2682 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2684 error
= lzc_channel_program_nosync(poolname
, program
,
2685 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2688 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2689 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2691 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2693 (void) fprintf(stderr
, "%s: zcp check error: "
2694 "%u\n", prop_name
, error
);
2697 if (ans
!= intval
) {
2698 (void) fprintf(stderr
, "%s: zfs found %llu, "
2699 "but zcp found %llu\n", prop_name
,
2700 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2704 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2706 (void) fprintf(stderr
, "%s: zcp check error: "
2707 "%u\n", prop_name
, error
);
2710 if (strcmp(strval
, str_ans
) != 0) {
2711 (void) fprintf(stderr
,
2712 "%s: zfs found '%s', but zcp found '%s'\n",
2713 prop_name
, strval
, str_ans
);
2717 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2718 "error: %u\n", prop_name
, error
);
2720 nvlist_free(argnvl
);
2721 nvlist_free(outnvl
);
2725 * Retrieve a property from the given object. If 'literal' is specified, then
2726 * numbers are left as exact values. Otherwise, numbers are converted to a
2727 * human-readable form.
2729 * Returns 0 on success, or -1 on error.
2732 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2733 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2735 char *source
= NULL
;
2739 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2742 * Check to see if this property applies to our object
2744 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2747 if (received
&& zfs_prop_readonly(prop
))
2751 *src
= ZPROP_SRC_NONE
;
2754 case ZFS_PROP_CREATION
:
2756 * 'creation' is a time_t stored in the statistics. We convert
2757 * this into a string unless 'literal' is specified.
2760 val
= getprop_uint64(zhp
, prop
, &source
);
2761 time_t time
= (time_t)val
;
2765 localtime_r(&time
, &t
) == NULL
||
2766 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2768 (void) snprintf(propbuf
, proplen
, "%llu",
2771 zcp_check(zhp
, prop
, val
, NULL
);
2774 case ZFS_PROP_MOUNTPOINT
:
2776 * Getting the precise mountpoint can be tricky.
2778 * - for 'none' or 'legacy', return those values.
2779 * - for inherited mountpoints, we want to take everything
2780 * after our ancestor and append it to the inherited value.
2782 * If the pool has an alternate root, we want to prepend that
2783 * root to any values we return.
2786 str
= getprop_string(zhp
, prop
, &source
);
2788 if (str
[0] == '/') {
2789 char buf
[MAXPATHLEN
];
2791 const char *relpath
;
2794 * If we inherit the mountpoint, even from a dataset
2795 * with a received value, the source will be the path of
2796 * the dataset we inherit from. If source is
2797 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2800 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2803 relpath
= zhp
->zfs_name
+ strlen(source
);
2804 if (relpath
[0] == '/')
2808 if ((zpool_get_prop(zhp
->zpool_hdl
,
2809 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2810 B_FALSE
)) || (strcmp(root
, "-") == 0))
2813 * Special case an alternate root of '/'. This will
2814 * avoid having multiple leading slashes in the
2817 if (strcmp(root
, "/") == 0)
2821 * If the mountpoint is '/' then skip over this
2822 * if we are obtaining either an alternate root or
2823 * an inherited mountpoint.
2825 if (str
[1] == '\0' && (root
[0] != '\0' ||
2826 relpath
[0] != '\0'))
2829 if (relpath
[0] == '\0')
2830 (void) snprintf(propbuf
, proplen
, "%s%s",
2833 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2834 root
, str
, relpath
[0] == '@' ? "" : "/",
2837 /* 'legacy' or 'none' */
2838 (void) strlcpy(propbuf
, str
, proplen
);
2840 zcp_check(zhp
, prop
, 0, propbuf
);
2843 case ZFS_PROP_ORIGIN
:
2844 str
= getprop_string(zhp
, prop
, &source
);
2847 (void) strlcpy(propbuf
, str
, proplen
);
2848 zcp_check(zhp
, prop
, 0, str
);
2851 case ZFS_PROP_REDACT_SNAPS
:
2852 if (get_rsnaps_string(zhp
, propbuf
, proplen
) != 0)
2856 case ZFS_PROP_CLONES
:
2857 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2861 case ZFS_PROP_QUOTA
:
2862 case ZFS_PROP_REFQUOTA
:
2863 case ZFS_PROP_RESERVATION
:
2864 case ZFS_PROP_REFRESERVATION
:
2866 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2869 * If quota or reservation is 0, we translate this into 'none'
2870 * (unless literal is set), and indicate that it's the default
2871 * value. Otherwise, we print the number nicely and indicate
2872 * that its set locally.
2876 (void) strlcpy(propbuf
, "0", proplen
);
2878 (void) strlcpy(propbuf
, "none", proplen
);
2881 (void) snprintf(propbuf
, proplen
, "%llu",
2884 zfs_nicebytes(val
, propbuf
, proplen
);
2886 zcp_check(zhp
, prop
, val
, NULL
);
2889 case ZFS_PROP_FILESYSTEM_LIMIT
:
2890 case ZFS_PROP_SNAPSHOT_LIMIT
:
2891 case ZFS_PROP_FILESYSTEM_COUNT
:
2892 case ZFS_PROP_SNAPSHOT_COUNT
:
2894 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2898 * If limit is UINT64_MAX, we translate this into 'none' (unless
2899 * literal is set), and indicate that it's the default value.
2900 * Otherwise, we print the number nicely and indicate that it's
2904 (void) snprintf(propbuf
, proplen
, "%llu",
2906 } else if (val
== UINT64_MAX
) {
2907 (void) strlcpy(propbuf
, "none", proplen
);
2909 zfs_nicenum(val
, propbuf
, proplen
);
2912 zcp_check(zhp
, prop
, val
, NULL
);
2915 case ZFS_PROP_REFRATIO
:
2916 case ZFS_PROP_COMPRESSRATIO
:
2917 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2920 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2921 (u_longlong_t
)(val
/ 100),
2922 (u_longlong_t
)(val
% 100));
2924 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2925 (u_longlong_t
)(val
/ 100),
2926 (u_longlong_t
)(val
% 100));
2927 zcp_check(zhp
, prop
, val
, NULL
);
2931 switch (zhp
->zfs_type
) {
2932 case ZFS_TYPE_FILESYSTEM
:
2935 case ZFS_TYPE_VOLUME
:
2938 case ZFS_TYPE_SNAPSHOT
:
2941 case ZFS_TYPE_BOOKMARK
:
2947 (void) snprintf(propbuf
, proplen
, "%s", str
);
2948 zcp_check(zhp
, prop
, 0, propbuf
);
2951 case ZFS_PROP_MOUNTED
:
2953 * The 'mounted' property is a pseudo-property that described
2954 * whether the filesystem is currently mounted. Even though
2955 * it's a boolean value, the typical values of "on" and "off"
2956 * don't make sense, so we translate to "yes" and "no".
2958 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2959 src
, &source
, &val
) != 0)
2962 (void) strlcpy(propbuf
, "yes", proplen
);
2964 (void) strlcpy(propbuf
, "no", proplen
);
2969 * The 'name' property is a pseudo-property derived from the
2970 * dataset name. It is presented as a real property to simplify
2973 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2974 zcp_check(zhp
, prop
, 0, propbuf
);
2977 case ZFS_PROP_MLSLABEL
:
2979 #ifdef HAVE_MLSLABEL
2980 m_label_t
*new_sl
= NULL
;
2981 char *ascii
= NULL
; /* human readable label */
2983 (void) strlcpy(propbuf
,
2984 getprop_string(zhp
, prop
, &source
), proplen
);
2986 if (literal
|| (strcasecmp(propbuf
,
2987 ZFS_MLSLABEL_DEFAULT
) == 0))
2991 * Try to translate the internal hex string to
2992 * human-readable output. If there are any
2993 * problems just use the hex string.
2996 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2997 L_NO_CORRECTION
, NULL
) == -1) {
2998 m_label_free(new_sl
);
3002 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
3006 m_label_free(new_sl
);
3009 m_label_free(new_sl
);
3011 (void) strlcpy(propbuf
, ascii
, proplen
);
3014 (void) strlcpy(propbuf
,
3015 getprop_string(zhp
, prop
, &source
), proplen
);
3016 #endif /* HAVE_MLSLABEL */
3021 case ZFS_PROP_CREATETXG
:
3022 case ZFS_PROP_OBJSETID
:
3024 * These properties are stored as numbers, but they are
3026 * We don't want them to be pretty printed, because pretty
3027 * printing mangles the ID into a truncated and useless value.
3029 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
3031 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
3032 zcp_check(zhp
, prop
, val
, NULL
);
3035 case ZFS_PROP_REFERENCED
:
3036 case ZFS_PROP_AVAILABLE
:
3038 case ZFS_PROP_USEDSNAP
:
3039 case ZFS_PROP_USEDDS
:
3040 case ZFS_PROP_USEDREFRESERV
:
3041 case ZFS_PROP_USEDCHILD
:
3042 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
3045 (void) snprintf(propbuf
, proplen
, "%llu",
3048 zfs_nicebytes(val
, propbuf
, proplen
);
3050 zcp_check(zhp
, prop
, val
, NULL
);
3054 switch (zfs_prop_get_type(prop
)) {
3055 case PROP_TYPE_NUMBER
:
3056 if (get_numeric_property(zhp
, prop
, src
,
3057 &source
, &val
) != 0) {
3062 (void) snprintf(propbuf
, proplen
, "%llu",
3065 zfs_nicenum(val
, propbuf
, proplen
);
3067 zcp_check(zhp
, prop
, val
, NULL
);
3070 case PROP_TYPE_STRING
:
3071 str
= getprop_string(zhp
, prop
, &source
);
3075 (void) strlcpy(propbuf
, str
, proplen
);
3076 zcp_check(zhp
, prop
, 0, str
);
3079 case PROP_TYPE_INDEX
:
3080 if (get_numeric_property(zhp
, prop
, src
,
3081 &source
, &val
) != 0)
3083 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
3086 (void) strlcpy(propbuf
, strval
, proplen
);
3087 zcp_check(zhp
, prop
, 0, strval
);
3095 get_source(zhp
, src
, source
, statbuf
, statlen
);
3101 * Utility function to get the given numeric property. Does no validation that
3102 * the given property is the appropriate type; should only be used with
3103 * hard-coded property types.
3106 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
3111 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3117 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3121 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3122 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3126 * Similar to zfs_prop_get(), but returns the value as an integer.
3129 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3130 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3135 * Check to see if this property applies to our object
3137 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3138 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3139 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3140 zfs_prop_to_name(prop
)));
3144 *src
= ZPROP_SRC_NONE
;
3146 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3149 get_source(zhp
, src
, source
, statbuf
, statlen
);
3156 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3157 char **domainp
, idmap_rid_t
*ridp
)
3159 idmap_get_handle_t
*get_hdl
= NULL
;
3163 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3167 err
= idmap_get_sidbyuid(get_hdl
, id
,
3168 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3170 err
= idmap_get_sidbygid(get_hdl
, id
,
3171 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3173 if (err
== IDMAP_SUCCESS
&&
3174 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3175 status
== IDMAP_SUCCESS
)
3181 idmap_get_destroy(get_hdl
);
3184 #endif /* HAVE_IDMAP */
3187 * convert the propname into parameters needed by kernel
3188 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3189 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3190 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3191 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3192 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3193 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3196 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3197 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3199 zfs_userquota_prop_t type
;
3203 boolean_t isproject
;
3209 /* Figure out the property type ({user|group|project}{quota|space}) */
3210 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3211 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3212 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3215 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3219 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3220 type
== ZFS_PROP_USEROBJQUOTA
||
3221 type
== ZFS_PROP_USEROBJUSED
);
3222 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3223 type
== ZFS_PROP_GROUPOBJQUOTA
||
3224 type
== ZFS_PROP_GROUPOBJUSED
);
3225 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3226 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3227 type
== ZFS_PROP_PROJECTOBJUSED
);
3229 cp
= strchr(propname
, '@') + 1;
3231 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3232 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3235 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3236 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3239 } else if (!isproject
&& strchr(cp
, '@')) {
3242 * It's a SID name (eg "user@domain") that needs to be
3243 * turned into S-1-domainID-RID.
3245 directory_error_t e
;
3246 char *numericsid
= NULL
;
3249 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3252 e
= directory_sid_from_user_name(NULL
,
3255 e
= directory_sid_from_group_name(NULL
,
3259 directory_error_free(e
);
3262 if (numericsid
== NULL
)
3265 (void) strlcpy(domain
, cp
, domainlen
);
3266 cp
= strrchr(domain
, '-');
3271 *ridp
= strtoull(cp
, &end
, 10);
3274 if (errno
!= 0 || *end
!= '\0')
3278 #endif /* HAVE_IDMAP */
3280 /* It's a user/group/project ID (eg "12345"). */
3283 id
= strtoul(cp
, &end
, 10);
3286 if (id
> MAXUID
&& !isproject
) {
3288 /* It's an ephemeral ID. */
3292 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3293 &mapdomain
, &rid
) != 0)
3295 (void) strlcpy(domain
, mapdomain
, domainlen
);
3299 #endif /* HAVE_IDMAP */
3309 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3310 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3313 zfs_cmd_t zc
= {"\0"};
3315 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3317 err
= userquota_propname_decode(propname
,
3318 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3319 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3320 zc
.zc_objset_type
= *typep
;
3324 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3328 *propvalue
= zc
.zc_cookie
;
3333 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3334 uint64_t *propvalue
)
3336 zfs_userquota_prop_t type
;
3338 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3343 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3344 char *propbuf
, int proplen
, boolean_t literal
)
3348 zfs_userquota_prop_t type
;
3350 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3357 (void) snprintf(propbuf
, proplen
, "%llu",
3358 (u_longlong_t
)propvalue
);
3359 } else if (propvalue
== 0 &&
3360 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3361 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3362 type
== ZFS_PROP_PROJECTQUOTA
||
3363 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3364 (void) strlcpy(propbuf
, "none", proplen
);
3365 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3366 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3367 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3368 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3370 zfs_nicenum(propvalue
, propbuf
, proplen
);
3376 * propname must start with "written@" or "written#".
3379 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3380 uint64_t *propvalue
)
3383 zfs_cmd_t zc
= {"\0"};
3384 const char *snapname
;
3386 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3388 assert(zfs_prop_written(propname
));
3389 snapname
= propname
+ strlen("written@");
3390 if (strchr(snapname
, '@') != NULL
|| strchr(snapname
, '#') != NULL
) {
3391 /* full snapshot or bookmark name specified */
3392 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3394 /* snapname is the short name, append it to zhp's fsname */
3397 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3398 sizeof (zc
.zc_value
));
3399 cp
= strchr(zc
.zc_value
, '@');
3402 (void) strlcat(zc
.zc_value
, snapname
- 1, sizeof (zc
.zc_value
));
3405 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3409 *propvalue
= zc
.zc_cookie
;
3414 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3415 char *propbuf
, int proplen
, boolean_t literal
)
3420 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3426 (void) snprintf(propbuf
, proplen
, "%llu",
3427 (u_longlong_t
)propvalue
);
3429 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3436 * Returns the name of the given zfs handle.
3439 zfs_get_name(const zfs_handle_t
*zhp
)
3441 return (zhp
->zfs_name
);
3445 * Returns the name of the parent pool for the given zfs handle.
3448 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3450 return (zhp
->zpool_hdl
->zpool_name
);
3454 * Returns the type of the given zfs handle.
3457 zfs_get_type(const zfs_handle_t
*zhp
)
3459 return (zhp
->zfs_type
);
3463 * Is one dataset name a child dataset of another?
3465 * Needs to handle these cases:
3466 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3467 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3468 * Descendant? No. No. No. Yes.
3471 is_descendant(const char *ds1
, const char *ds2
)
3473 size_t d1len
= strlen(ds1
);
3475 /* ds2 can't be a descendant if it's smaller */
3476 if (strlen(ds2
) < d1len
)
3479 /* otherwise, compare strings and verify that there's a '/' char */
3480 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3484 * Given a complete name, return just the portion that refers to the parent.
3485 * Will return -1 if there is no parent (path is just the name of the
3489 parent_name(const char *path
, char *buf
, size_t buflen
)
3493 (void) strlcpy(buf
, path
, buflen
);
3495 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3503 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3505 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3509 * If accept_ancestor is false, then check to make sure that the given path has
3510 * a parent, and that it exists. If accept_ancestor is true, then find the
3511 * closest existing ancestor for the given path. In prefixlen return the
3512 * length of already existing prefix of the given path. We also fetch the
3513 * 'zoned' property, which is used to validate property settings when creating
3517 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3518 boolean_t accept_ancestor
, int *prefixlen
)
3520 zfs_cmd_t zc
= {"\0"};
3521 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3527 (void) snprintf(errbuf
, sizeof (errbuf
),
3528 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3530 /* get parent, and check to see if this is just a pool */
3531 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3532 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3533 "missing dataset name"));
3534 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3537 /* check to see if the pool exists */
3538 if ((slash
= strchr(parent
, '/')) == NULL
)
3539 slash
= parent
+ strlen(parent
);
3540 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3541 zc
.zc_name
[slash
- parent
] = '\0';
3542 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3544 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3545 "no such pool '%s'"), zc
.zc_name
);
3546 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3549 /* check to see if the parent dataset exists */
3550 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3551 if (errno
== ENOENT
&& accept_ancestor
) {
3553 * Go deeper to find an ancestor, give up on top level.
3555 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3557 "no such pool '%s'"), zc
.zc_name
);
3558 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3560 } else if (errno
== ENOENT
) {
3561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3562 "parent does not exist"));
3563 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3565 return (zfs_standard_error(hdl
, errno
, errbuf
));
3568 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3572 /* we are in a non-global zone, but parent is in the global zone */
3573 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3574 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3579 /* make sure parent is a filesystem */
3580 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3582 "parent is not a filesystem"));
3583 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3589 if (prefixlen
!= NULL
)
3590 *prefixlen
= strlen(parent
);
3595 * Finds whether the dataset of the given type(s) exists.
3598 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3602 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3606 * Try to get stats for the dataset, which will tell us if it exists.
3608 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3609 int ds_type
= zhp
->zfs_type
;
3612 if (types
& ds_type
)
3619 * Given a path to 'target', create all the ancestors between
3620 * the prefixlen portion of the path, and the target itself.
3621 * Fail if the initial prefixlen-ancestor does not already exist.
3624 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3630 /* make sure prefix exists */
3631 cp
= target
+ prefixlen
;
3633 assert(strchr(cp
, '/') == NULL
);
3634 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3637 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3645 * Attempt to create, mount, and share any ancestor filesystems,
3646 * up to the prefixlen-long one.
3648 for (cp
= target
+ prefixlen
+ 1;
3649 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3653 h
= make_dataset_handle(hdl
, target
);
3655 /* it already exists, nothing to do here */
3660 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3662 opname
= dgettext(TEXT_DOMAIN
, "create");
3666 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3668 opname
= dgettext(TEXT_DOMAIN
, "open");
3672 if (zfs_mount(h
, NULL
, 0) != 0) {
3673 opname
= dgettext(TEXT_DOMAIN
, "mount");
3677 if (zfs_share(h
) != 0) {
3678 opname
= dgettext(TEXT_DOMAIN
, "share");
3688 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3689 "failed to %s ancestor '%s'"), opname
, target
);
3694 * Creates non-existing ancestors of the given path.
3697 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3704 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3705 "cannot create '%s'"), path
);
3708 * Check that we are not passing the nesting limit
3709 * before we start creating any ancestors.
3711 if (dataset_nestcheck(path
) != 0) {
3712 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3713 "maximum name nesting depth exceeded"));
3714 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3717 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3720 if ((path_copy
= strdup(path
)) != NULL
) {
3721 rc
= create_parents(hdl
, path_copy
, prefix
);
3724 if (path_copy
== NULL
|| rc
!= 0)
3731 * Create a new filesystem or volume.
3734 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3739 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3741 enum lzc_dataset_type ost
;
3742 zpool_handle_t
*zpool_handle
;
3743 uint8_t *wkeydata
= NULL
;
3746 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3748 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3749 "cannot create '%s'"), path
);
3751 /* validate the path, taking care to note the extended error message */
3752 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3753 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3755 if (dataset_nestcheck(path
) != 0) {
3756 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3757 "maximum name nesting depth exceeded"));
3758 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3761 /* validate parents exist */
3762 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3766 * The failure modes when creating a dataset of a different type over
3767 * one that already exists is a little strange. In particular, if you
3768 * try to create a dataset on top of an existing dataset, the ioctl()
3769 * will return ENOENT, not EEXIST. To prevent this from happening, we
3770 * first try to see if the dataset exists.
3772 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3773 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3774 "dataset already exists"));
3775 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3778 if (type
== ZFS_TYPE_VOLUME
)
3779 ost
= LZC_DATSET_TYPE_ZVOL
;
3781 ost
= LZC_DATSET_TYPE_ZFS
;
3783 /* open zpool handle for prop validation */
3784 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3785 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3787 /* truncate pool_path at first slash */
3788 char *p
= strchr(pool_path
, '/');
3792 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3795 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3796 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3797 zpool_close(zpool_handle
);
3800 zpool_close(zpool_handle
);
3802 if (type
== ZFS_TYPE_VOLUME
) {
3804 * If we are creating a volume, the size and block size must
3805 * satisfy a few restraints. First, the blocksize must be a
3806 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3807 * volsize must be a multiple of the block size, and cannot be
3810 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3811 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3813 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3814 "missing volume size"));
3815 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3818 if ((ret
= nvlist_lookup_uint64(props
,
3819 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3820 &blocksize
)) != 0) {
3821 if (ret
== ENOENT
) {
3822 blocksize
= zfs_prop_default_numeric(
3823 ZFS_PROP_VOLBLOCKSIZE
);
3826 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3827 "missing volume block size"));
3828 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3834 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3835 "volume size cannot be zero"));
3836 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3839 if (size
% blocksize
!= 0) {
3841 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3842 "volume size must be a multiple of volume block "
3844 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3848 (void) parent_name(path
, parent
, sizeof (parent
));
3849 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3850 &wkeydata
, &wkeylen
) != 0) {
3852 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3855 /* create the dataset */
3856 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3858 if (wkeydata
!= NULL
)
3861 /* check for failure */
3865 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3866 "no such parent '%s'"), parent
);
3867 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3870 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3871 "pool must be upgraded to set this "
3872 "property or value"));
3873 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3876 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3877 "encryption root's key is not loaded "
3879 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3882 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3883 "invalid property value(s) specified"));
3884 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3888 * This platform can't address a volume this big.
3890 if (type
== ZFS_TYPE_VOLUME
)
3891 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3896 return (zfs_standard_error(hdl
, errno
, errbuf
));
3904 * Destroys the given dataset. The caller must make sure that the filesystem
3905 * isn't mounted, and that there are no active dependents. If the file system
3906 * does not exist this function does nothing.
3909 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3913 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3916 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3917 nvlist_t
*nv
= fnvlist_alloc();
3918 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3919 error
= lzc_destroy_bookmarks(nv
, NULL
);
3922 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3923 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3929 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3930 nvlist_t
*nv
= fnvlist_alloc();
3931 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3932 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3935 error
= lzc_destroy(zhp
->zfs_name
);
3938 if (error
!= 0 && error
!= ENOENT
) {
3939 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3940 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3944 remove_mountpoint(zhp
);
3949 struct destroydata
{
3951 const char *snapname
;
3955 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3957 struct destroydata
*dd
= arg
;
3958 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3961 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3962 dd
->snapname
) >= sizeof (name
))
3965 if (lzc_exists(name
))
3966 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3968 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3974 * Destroys all snapshots with the given name in zhp & descendants.
3977 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3980 struct destroydata dd
= { 0 };
3982 dd
.snapname
= snapname
;
3983 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3984 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3986 if (nvlist_empty(dd
.nvl
)) {
3987 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3988 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3989 zhp
->zfs_name
, snapname
);
3991 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3993 nvlist_free(dd
.nvl
);
3998 * Destroys all the snapshots named in the nvlist.
4001 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
4004 nvlist_t
*errlist
= NULL
;
4007 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
4010 nvlist_free(errlist
);
4014 if (nvlist_empty(errlist
)) {
4016 (void) snprintf(errbuf
, sizeof (errbuf
),
4017 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
4019 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
4021 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
4022 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
4024 (void) snprintf(errbuf
, sizeof (errbuf
),
4025 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
4028 switch (fnvpair_value_int32(pair
)) {
4031 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
4032 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4035 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
4040 nvlist_free(errlist
);
4045 * Clones the given dataset. The target must be of the same type as the source.
4048 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
4050 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4053 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4056 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
4058 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4059 "cannot create '%s'"), target
);
4061 /* validate the target/clone name */
4062 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
4063 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4065 /* validate parents exist */
4066 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
4069 (void) parent_name(target
, parent
, sizeof (parent
));
4076 if (ZFS_IS_VOLUME(zhp
)) {
4077 type
= ZFS_TYPE_VOLUME
;
4079 type
= ZFS_TYPE_FILESYSTEM
;
4081 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
4082 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
4084 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
4090 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
4092 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
4095 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
4103 * The parent doesn't exist. We should have caught this
4104 * above, but there may a race condition that has since
4105 * destroyed the parent.
4107 * At this point, we don't know whether it's the source
4108 * that doesn't exist anymore, or whether the target
4109 * dataset doesn't exist.
4111 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4112 "no such parent '%s'"), parent
);
4113 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4116 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4117 "source and target pools differ"));
4118 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4122 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4131 * Promotes the given clone fs to be the clone parent.
4134 zfs_promote(zfs_handle_t
*zhp
)
4136 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4137 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4141 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4142 "cannot promote '%s'"), zhp
->zfs_name
);
4144 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4145 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4146 "snapshots can not be promoted"));
4147 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4150 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4152 "not a cloned filesystem"));
4153 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4156 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4157 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4159 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4164 /* There is a conflicting snapshot name. */
4165 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4166 "conflicting snapshot '%s' from parent '%s'"),
4167 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4168 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4171 return (zfs_standard_error(hdl
, ret
, errbuf
));
4177 typedef struct snapdata
{
4179 const char *sd_snapname
;
4183 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4185 snapdata_t
*sd
= arg
;
4186 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4189 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4190 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4191 sd
->sd_snapname
) >= sizeof (name
))
4194 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4196 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4204 zfs_remap_indirects(libzfs_handle_t
*hdl
, const char *fs
)
4209 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4210 "cannot remap dataset '%s'"), fs
);
4212 err
= lzc_remap(fs
);
4217 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4218 "pool must be upgraded"));
4219 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4222 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4225 (void) zfs_standard_error(hdl
, err
, errbuf
);
4234 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4238 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4244 zpool_handle_t
*zpool_hdl
;
4245 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4247 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4248 "cannot create snapshots "));
4251 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4252 const char *snapname
= nvpair_name(elem
);
4254 /* validate the target name */
4255 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4257 (void) snprintf(errbuf
, sizeof (errbuf
),
4258 dgettext(TEXT_DOMAIN
,
4259 "cannot create snapshot '%s'"), snapname
);
4260 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4265 * get pool handle for prop validation. assumes all snaps are in the
4266 * same pool, as does lzc_snapshot (below).
4268 elem
= nvlist_next_nvpair(snaps
, NULL
);
4269 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4270 pool
[strcspn(pool
, "/@")] = '\0';
4271 zpool_hdl
= zpool_open(hdl
, pool
);
4272 if (zpool_hdl
== NULL
)
4275 if (props
!= NULL
&&
4276 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4277 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4278 zpool_close(zpool_hdl
);
4281 zpool_close(zpool_hdl
);
4283 ret
= lzc_snapshot(snaps
, props
, &errors
);
4286 boolean_t printed
= B_FALSE
;
4287 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4289 elem
= nvlist_next_nvpair(errors
, elem
)) {
4290 (void) snprintf(errbuf
, sizeof (errbuf
),
4291 dgettext(TEXT_DOMAIN
,
4292 "cannot create snapshot '%s'"), nvpair_name(elem
));
4293 (void) zfs_standard_error(hdl
,
4294 fnvpair_value_int32(elem
), errbuf
);
4300 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4301 "multiple snapshots of same "
4303 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4307 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4313 nvlist_free(errors
);
4318 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4322 snapdata_t sd
= { 0 };
4323 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4328 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4329 "cannot snapshot %s"), path
);
4331 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4332 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4334 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4335 cp
= strchr(fsname
, '@');
4337 sd
.sd_snapname
= cp
+ 1;
4339 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4340 ZFS_TYPE_VOLUME
)) == NULL
) {
4344 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4346 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4348 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4351 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4352 nvlist_free(sd
.sd_nvl
);
4358 * Destroy any more recent snapshots. We invoke this callback on any dependents
4359 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4360 * is a dependent and we should just destroy it without checking the transaction
4363 typedef struct rollback_data
{
4364 const char *cb_target
; /* the snapshot */
4365 uint64_t cb_create
; /* creation time reference */
4371 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4373 rollback_data_t
*cbp
= data
;
4374 prop_changelist_t
*clp
;
4376 /* We must destroy this clone; first unmount it */
4377 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4378 cbp
->cb_force
? MS_FORCE
: 0);
4379 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4380 cbp
->cb_error
= B_TRUE
;
4384 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4385 cbp
->cb_error
= B_TRUE
;
4387 changelist_remove(clp
, zhp
->zfs_name
);
4388 (void) changelist_postfix(clp
);
4389 changelist_free(clp
);
4396 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4398 rollback_data_t
*cbp
= data
;
4400 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4401 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4402 rollback_destroy_dependent
, cbp
);
4404 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4412 * Given a dataset, rollback to a specific snapshot, discarding any
4413 * data changes since then and making it the active dataset.
4415 * Any snapshots and bookmarks more recent than the target are
4416 * destroyed, along with their dependents (i.e. clones).
4419 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4421 rollback_data_t cb
= { 0 };
4423 boolean_t restore_resv
= 0;
4424 uint64_t old_volsize
= 0, new_volsize
;
4425 zfs_prop_t resv_prop
= { 0 };
4426 uint64_t min_txg
= 0;
4428 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4429 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4432 * Destroy all recent snapshots and their dependents.
4434 cb
.cb_force
= force
;
4435 cb
.cb_target
= snap
->zfs_name
;
4436 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4438 if (cb
.cb_create
> 0)
4439 min_txg
= cb
.cb_create
;
4441 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
,
4444 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4450 * Now that we have verified that the snapshot is the latest,
4451 * rollback to the given snapshot.
4454 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4455 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4457 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4459 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4463 * Pass both the filesystem and the wanted snapshot names,
4464 * we would get an error back if the snapshot is destroyed or
4465 * a new snapshot is created before this request is processed.
4467 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4471 (void) snprintf(errbuf
, sizeof (errbuf
),
4472 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4476 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4477 "there is a snapshot or bookmark more recent "
4478 "than '%s'"), snap
->zfs_name
);
4479 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4482 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4483 "'%s' is not found among snapshots of '%s'"),
4484 snap
->zfs_name
, zhp
->zfs_name
);
4485 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4488 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4491 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4497 * For volumes, if the pre-rollback volsize matched the pre-
4498 * rollback reservation and the volsize has changed then set
4499 * the reservation property to the post-rollback volsize.
4500 * Make a new handle since the rollback closed the dataset.
4502 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4503 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4505 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4506 if (old_volsize
!= new_volsize
)
4507 err
= zfs_prop_set_int(zhp
, resv_prop
,
4516 * Renames the given dataset.
4519 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4520 boolean_t force_unmount
)
4523 zfs_cmd_t zc
= {"\0"};
4525 prop_changelist_t
*cl
= NULL
;
4526 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4527 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4530 /* if we have the same exact name, just return success */
4531 if (strcmp(zhp
->zfs_name
, target
) == 0)
4534 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4535 "cannot rename to '%s'"), target
);
4537 /* make sure source name is valid */
4538 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4539 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4542 * Make sure the target name is valid
4544 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4545 if ((strchr(target
, '@') == NULL
) ||
4548 * Snapshot target name is abbreviated,
4549 * reconstruct full dataset name
4551 (void) strlcpy(parent
, zhp
->zfs_name
,
4553 delim
= strchr(parent
, '@');
4554 if (strchr(target
, '@') == NULL
)
4558 (void) strlcat(parent
, target
, sizeof (parent
));
4562 * Make sure we're renaming within the same dataset.
4564 delim
= strchr(target
, '@');
4565 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4566 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4567 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4568 "snapshots must be part of same "
4570 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4575 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4576 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4579 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4580 "recursive rename must be a snapshot"));
4581 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4584 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4585 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4587 /* validate parents */
4588 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4591 /* make sure we're in the same pool */
4592 verify((delim
= strchr(target
, '/')) != NULL
);
4593 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4594 zhp
->zfs_name
[delim
- target
] != '/') {
4595 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4596 "datasets must be within same pool"));
4597 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4600 /* new name cannot be a child of the current dataset name */
4601 if (is_descendant(zhp
->zfs_name
, target
)) {
4602 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4603 "New dataset name cannot be a descendant of "
4604 "current dataset name"));
4605 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4609 (void) snprintf(errbuf
, sizeof (errbuf
),
4610 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4612 if (getzoneid() == GLOBAL_ZONEID
&&
4613 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4614 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4615 "dataset is used in a non-global zone"));
4616 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4621 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4622 if (parentname
== NULL
) {
4626 delim
= strchr(parentname
, '@');
4628 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4635 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4636 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4637 CL_GATHER_ITER_MOUNTED
,
4638 force_unmount
? MS_FORCE
: 0)) == NULL
)
4641 if (changelist_haszonedchild(cl
)) {
4642 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4643 "child dataset with inherited mountpoint is used "
4644 "in a non-global zone"));
4645 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4650 if ((ret
= changelist_prefix(cl
)) != 0)
4654 if (ZFS_IS_VOLUME(zhp
))
4655 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4657 zc
.zc_objset_type
= DMU_OST_ZFS
;
4659 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4660 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4662 zc
.zc_cookie
= recursive
;
4664 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4666 * if it was recursive, the one that actually failed will
4669 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4670 "cannot rename '%s'"), zc
.zc_name
);
4672 if (recursive
&& errno
== EEXIST
) {
4673 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4674 "a child dataset already has a snapshot "
4675 "with the new name"));
4676 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4677 } else if (errno
== EACCES
) {
4678 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4679 "cannot move encrypted child outside of "
4680 "its encryption root"));
4681 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4683 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4687 * On failure, we still want to remount any filesystems that
4688 * were previously mounted, so we don't alter the system state.
4691 (void) changelist_postfix(cl
);
4694 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4695 ret
= changelist_postfix(cl
);
4701 changelist_free(cl
);
4707 zfs_get_all_props(zfs_handle_t
*zhp
)
4709 return (zhp
->zfs_props
);
4713 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4715 if (zhp
->zfs_recvd_props
== NULL
)
4716 if (get_recvd_props_ioctl(zhp
) != 0)
4718 return (zhp
->zfs_recvd_props
);
4722 zfs_get_user_props(zfs_handle_t
*zhp
)
4724 return (zhp
->zfs_user_props
);
4728 * This function is used by 'zfs list' to determine the exact set of columns to
4729 * display, and their maximum widths. This does two main things:
4731 * - If this is a list of all properties, then expand the list to include
4732 * all native properties, and set a flag so that for each dataset we look
4733 * for new unique user properties and add them to the list.
4735 * - For non fixed-width properties, keep track of the maximum width seen
4736 * so that we can size the column appropriately. If the user has
4737 * requested received property values, we also need to compute the width
4738 * of the RECEIVED column.
4741 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4744 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4745 zprop_list_t
*entry
;
4746 zprop_list_t
**last
, **start
;
4747 nvlist_t
*userprops
, *propval
;
4750 char buf
[ZFS_MAXPROPLEN
];
4752 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4755 userprops
= zfs_get_user_props(zhp
);
4758 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4760 * Go through and add any user properties as necessary. We
4761 * start by incrementing our list pointer to the first
4762 * non-native property.
4765 while (*start
!= NULL
) {
4766 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4768 start
= &(*start
)->pl_next
;
4772 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4774 * See if we've already found this property in our list.
4776 for (last
= start
; *last
!= NULL
;
4777 last
= &(*last
)->pl_next
) {
4778 if (strcmp((*last
)->pl_user_prop
,
4779 nvpair_name(elem
)) == 0)
4783 if (*last
== NULL
) {
4784 if ((entry
= zfs_alloc(hdl
,
4785 sizeof (zprop_list_t
))) == NULL
||
4786 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4787 nvpair_name(elem
)))) == NULL
) {
4792 entry
->pl_prop
= ZPROP_INVAL
;
4793 entry
->pl_width
= strlen(nvpair_name(elem
));
4794 entry
->pl_all
= B_TRUE
;
4801 * Now go through and check the width of any non-fixed columns
4803 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4804 if (entry
->pl_fixed
&& !literal
)
4807 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4808 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4809 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4810 if (strlen(buf
) > entry
->pl_width
)
4811 entry
->pl_width
= strlen(buf
);
4813 if (received
&& zfs_prop_get_recvd(zhp
,
4814 zfs_prop_to_name(entry
->pl_prop
),
4815 buf
, sizeof (buf
), literal
) == 0)
4816 if (strlen(buf
) > entry
->pl_recvd_width
)
4817 entry
->pl_recvd_width
= strlen(buf
);
4819 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4821 verify(nvlist_lookup_string(propval
,
4822 ZPROP_VALUE
, &strval
) == 0);
4823 if (strlen(strval
) > entry
->pl_width
)
4824 entry
->pl_width
= strlen(strval
);
4826 if (received
&& zfs_prop_get_recvd(zhp
,
4827 entry
->pl_user_prop
,
4828 buf
, sizeof (buf
), literal
) == 0)
4829 if (strlen(buf
) > entry
->pl_recvd_width
)
4830 entry
->pl_recvd_width
= strlen(buf
);
4838 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4844 * Keep a reference to the props-table against which we prune the
4847 zhp
->zfs_props_table
= props
;
4849 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4852 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4853 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4856 * User properties will result in ZPROP_INVAL, and since we
4857 * only know how to prune standard ZFS properties, we always
4858 * leave these in the list. This can also happen if we
4859 * encounter an unknown DSL property (when running older
4860 * software, for example).
4862 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4863 (void) nvlist_remove(zhp
->zfs_props
,
4864 nvpair_name(curr
), nvpair_type(curr
));
4870 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4871 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4873 zfs_cmd_t zc
= {"\0"};
4874 nvlist_t
*nvlist
= NULL
;
4877 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4878 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4879 zc
.zc_cookie
= (uint64_t)cmd
;
4881 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4882 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4883 (void) no_memory(hdl
);
4889 case ZFS_SMB_ACL_ADD
:
4890 case ZFS_SMB_ACL_REMOVE
:
4891 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4893 case ZFS_SMB_ACL_RENAME
:
4894 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4896 (void) no_memory(hdl
);
4899 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4901 (void) no_memory(hdl
);
4904 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4905 nvlist_free(nvlist
);
4909 case ZFS_SMB_ACL_PURGE
:
4914 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4915 nvlist_free(nvlist
);
4920 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4921 char *path
, char *resource
)
4923 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4928 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4929 char *path
, char *resource
)
4931 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4936 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4938 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4943 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4944 char *oldname
, char *newname
)
4946 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4951 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4952 zfs_userspace_cb_t func
, void *arg
)
4954 zfs_cmd_t zc
= {"\0"};
4955 zfs_useracct_t buf
[100];
4956 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4959 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4961 zc
.zc_objset_type
= type
;
4962 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4965 zfs_useracct_t
*zua
= buf
;
4967 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4968 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4971 if ((errno
== ENOTSUP
&&
4972 (type
== ZFS_PROP_USEROBJUSED
||
4973 type
== ZFS_PROP_GROUPOBJUSED
||
4974 type
== ZFS_PROP_USEROBJQUOTA
||
4975 type
== ZFS_PROP_GROUPOBJQUOTA
||
4976 type
== ZFS_PROP_PROJECTOBJUSED
||
4977 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4978 type
== ZFS_PROP_PROJECTUSED
||
4979 type
== ZFS_PROP_PROJECTQUOTA
)))
4982 (void) snprintf(errbuf
, sizeof (errbuf
),
4983 dgettext(TEXT_DOMAIN
,
4984 "cannot get used/quota for %s"), zc
.zc_name
);
4985 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4987 if (zc
.zc_nvlist_dst_size
== 0)
4990 while (zc
.zc_nvlist_dst_size
> 0) {
4991 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4992 zua
->zu_space
)) != 0)
4995 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
5004 const char *snapname
;
5006 boolean_t recursive
;
5011 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
5013 struct holdarg
*ha
= arg
;
5014 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5017 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5018 ha
->snapname
) >= sizeof (name
))
5021 if (lzc_exists(name
))
5022 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
5025 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
5031 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5032 boolean_t recursive
, int cleanup_fd
)
5037 ha
.nvl
= fnvlist_alloc();
5038 ha
.snapname
= snapname
;
5040 ha
.recursive
= recursive
;
5041 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
5043 if (nvlist_empty(ha
.nvl
)) {
5046 fnvlist_free(ha
.nvl
);
5048 (void) snprintf(errbuf
, sizeof (errbuf
),
5049 dgettext(TEXT_DOMAIN
,
5050 "cannot hold snapshot '%s@%s'"),
5051 zhp
->zfs_name
, snapname
);
5052 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
5056 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
5057 fnvlist_free(ha
.nvl
);
5063 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
5067 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5072 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
5075 /* There may be errors even in the success case. */
5076 fnvlist_free(errors
);
5080 if (nvlist_empty(errors
)) {
5081 /* no hold-specific errors */
5082 (void) snprintf(errbuf
, sizeof (errbuf
),
5083 dgettext(TEXT_DOMAIN
, "cannot hold"));
5086 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5087 "pool must be upgraded"));
5088 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5091 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5094 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5098 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5100 elem
= nvlist_next_nvpair(errors
, elem
)) {
5101 (void) snprintf(errbuf
, sizeof (errbuf
),
5102 dgettext(TEXT_DOMAIN
,
5103 "cannot hold snapshot '%s'"), nvpair_name(elem
));
5104 switch (fnvpair_value_int32(elem
)) {
5107 * Temporary tags wind up having the ds object id
5108 * prepended. So even if we passed the length check
5109 * above, it's still possible for the tag to wind
5110 * up being slightly too long.
5112 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5115 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5118 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5121 (void) zfs_standard_error(hdl
,
5122 fnvpair_value_int32(elem
), errbuf
);
5126 fnvlist_free(errors
);
5131 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5133 struct holdarg
*ha
= arg
;
5134 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5136 nvlist_t
*existing_holds
;
5138 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5139 ha
->snapname
) >= sizeof (name
)) {
5144 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5146 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5149 nvlist_t
*torelease
= fnvlist_alloc();
5150 fnvlist_add_boolean(torelease
, ha
->tag
);
5151 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5152 fnvlist_free(torelease
);
5156 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5162 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5163 boolean_t recursive
)
5167 nvlist_t
*errors
= NULL
;
5169 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5172 ha
.nvl
= fnvlist_alloc();
5173 ha
.snapname
= snapname
;
5175 ha
.recursive
= recursive
;
5177 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5179 if (nvlist_empty(ha
.nvl
)) {
5180 fnvlist_free(ha
.nvl
);
5182 (void) snprintf(errbuf
, sizeof (errbuf
),
5183 dgettext(TEXT_DOMAIN
,
5184 "cannot release hold from snapshot '%s@%s'"),
5185 zhp
->zfs_name
, snapname
);
5187 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5189 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5194 ret
= lzc_release(ha
.nvl
, &errors
);
5195 fnvlist_free(ha
.nvl
);
5198 /* There may be errors even in the success case. */
5199 fnvlist_free(errors
);
5203 if (nvlist_empty(errors
)) {
5204 /* no hold-specific errors */
5205 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5209 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5210 "pool must be upgraded"));
5211 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5214 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5218 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5220 elem
= nvlist_next_nvpair(errors
, elem
)) {
5221 (void) snprintf(errbuf
, sizeof (errbuf
),
5222 dgettext(TEXT_DOMAIN
,
5223 "cannot release hold from snapshot '%s'"),
5225 switch (fnvpair_value_int32(elem
)) {
5227 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5230 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5233 (void) zfs_standard_error_fmt(hdl
,
5234 fnvpair_value_int32(elem
), errbuf
);
5238 fnvlist_free(errors
);
5243 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5245 zfs_cmd_t zc
= {"\0"};
5246 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5252 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5253 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5257 nvbuf
= malloc(nvsz
);
5258 if (nvbuf
== NULL
) {
5259 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5263 zc
.zc_nvlist_dst_size
= nvsz
;
5264 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5266 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5268 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5269 (void) snprintf(errbuf
, sizeof (errbuf
),
5270 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5275 nvsz
= zc
.zc_nvlist_dst_size
;
5279 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5280 "pool must be upgraded"));
5281 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5284 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5287 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5290 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5295 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5297 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
5298 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5300 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
5310 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5312 zfs_cmd_t zc
= {"\0"};
5313 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5319 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5320 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5322 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5325 nvbuf
= malloc(nvsz
);
5327 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5330 zc
.zc_nvlist_src_size
= nvsz
;
5331 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5332 zc
.zc_perm_action
= un
;
5334 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5336 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5337 (void) snprintf(errbuf
, sizeof (errbuf
),
5338 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5342 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5343 "pool must be upgraded"));
5344 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5347 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5350 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5353 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5364 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5369 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5372 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5374 (void) snprintf(errbuf
, sizeof (errbuf
),
5375 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5379 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5380 "pool must be upgraded"));
5381 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5384 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5387 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5390 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5399 * Convert the zvol's volume size to an appropriate reservation.
5400 * Note: If this routine is updated, it is necessary to update the ZFS test
5401 * suite's shell version in reservation.kshlib.
5404 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
5407 uint64_t nblocks
, volblocksize
;
5411 if (nvlist_lookup_string(props
,
5412 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5413 ncopies
= atoi(strval
);
5416 if (nvlist_lookup_uint64(props
,
5417 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5418 &volblocksize
) != 0)
5419 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5420 nblocks
= volsize
/volblocksize
;
5421 /* start with metadnode L0-L6 */
5423 /* calculate number of indirects */
5424 while (nblocks
> 1) {
5425 nblocks
+= DNODES_PER_LEVEL
- 1;
5426 nblocks
/= DNODES_PER_LEVEL
;
5429 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5432 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5433 * compressed, but in practice they compress down to about
5436 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;