4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2018, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017-2018 RackTop Systems.
33 * Copyright (c) 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
];
601 (void) strlcpy(fsname
, path
, sizeof (fsname
));
602 pound
= strchr(fsname
, '#');
607 bmark_name
= pound
+ 1;
608 props
= fnvlist_alloc();
609 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
616 rv
= nvlist_exists(bmarks
, bmark_name
);
622 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
623 nvlist_t
*bmark_props
)
625 zfs_handle_t
*zhp
= calloc(1, sizeof (zfs_handle_t
));
630 /* Fill in the name. */
631 zhp
->zfs_hdl
= parent
->zfs_hdl
;
632 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
634 /* Set the property lists. */
635 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
641 zhp
->zfs_head_type
= parent
->zfs_head_type
;
642 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
644 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
645 nvlist_free(zhp
->zfs_props
);
653 struct zfs_open_bookmarks_cb_data
{
659 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
661 struct zfs_open_bookmarks_cb_data
*dp
= data
;
664 * Is it the one we are looking for?
666 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
668 * We found it. Save it and let the caller know we are done.
675 * Not found. Close the handle and ask for another one.
682 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
683 * argument is a mask of acceptable types. The function will print an
684 * appropriate error message and return NULL if it can't be opened.
687 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
693 (void) snprintf(errbuf
, sizeof (errbuf
),
694 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
697 * Validate the name before we even try to open it.
699 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
700 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
705 * Bookmarks needs to be handled separately.
707 bookp
= strchr(path
, '#');
710 * Try to get stats for the dataset, which will tell us if it
714 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
715 (void) zfs_standard_error(hdl
, errno
, errbuf
);
719 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
721 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
724 * We need to cut out '#' and everything after '#'
725 * to get the parent dataset name only.
727 assert(bookp
- path
< sizeof (dsname
));
728 (void) strncpy(dsname
, path
, bookp
- path
);
729 dsname
[bookp
- path
] = '\0';
732 * Create handle for the parent dataset.
735 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
736 (void) zfs_standard_error(hdl
, errno
, errbuf
);
741 * Iterate bookmarks to find the right one.
744 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
745 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
746 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
750 if (cb_data
.zhp
== NULL
) {
751 (void) zfs_standard_error(hdl
, errno
, errbuf
);
763 if (!(types
& zhp
->zfs_type
)) {
764 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
773 * Release a ZFS handle. Nothing to do but free the associated memory.
776 zfs_close(zfs_handle_t
*zhp
)
778 if (zhp
->zfs_mntopts
)
779 free(zhp
->zfs_mntopts
);
780 nvlist_free(zhp
->zfs_props
);
781 nvlist_free(zhp
->zfs_user_props
);
782 nvlist_free(zhp
->zfs_recvd_props
);
786 typedef struct mnttab_node
{
787 struct mnttab mtn_mt
;
792 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
794 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
795 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
798 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
800 return (AVL_ISIGN(rv
));
804 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
806 pthread_mutex_init(&hdl
->libzfs_mnttab_cache_lock
, NULL
);
807 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
808 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
809 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
813 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
817 /* Reopen MNTTAB to prevent reading stale data from open file */
818 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
821 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
825 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
828 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
829 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
830 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
831 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
832 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
834 /* Exclude duplicate mounts */
835 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
836 free(mtn
->mtn_mt
.mnt_special
);
837 free(mtn
->mtn_mt
.mnt_mountp
);
838 free(mtn
->mtn_mt
.mnt_fstype
);
839 free(mtn
->mtn_mt
.mnt_mntopts
);
844 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
851 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
856 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
858 free(mtn
->mtn_mt
.mnt_special
);
859 free(mtn
->mtn_mt
.mnt_mountp
);
860 free(mtn
->mtn_mt
.mnt_fstype
);
861 free(mtn
->mtn_mt
.mnt_mntopts
);
864 avl_destroy(&hdl
->libzfs_mnttab_cache
);
865 (void) pthread_mutex_destroy(&hdl
->libzfs_mnttab_cache_lock
);
869 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
871 hdl
->libzfs_mnttab_enable
= enable
;
875 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
876 struct mnttab
*entry
)
882 if (!hdl
->libzfs_mnttab_enable
) {
883 struct mnttab srch
= { 0 };
885 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
886 libzfs_mnttab_fini(hdl
);
888 /* Reopen MNTTAB to prevent reading stale data from open file */
889 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
892 srch
.mnt_special
= (char *)fsname
;
893 srch
.mnt_fstype
= MNTTYPE_ZFS
;
894 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
900 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
901 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0) {
904 if ((error
= libzfs_mnttab_update(hdl
)) != 0) {
905 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
910 find
.mtn_mt
.mnt_special
= (char *)fsname
;
911 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
913 *entry
= mtn
->mtn_mt
;
916 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
921 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
922 const char *mountp
, const char *mntopts
)
926 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
927 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) != 0) {
928 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
929 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
930 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
931 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
932 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
934 * Another thread may have already added this entry
935 * via libzfs_mnttab_update. If so we should skip it.
937 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, NULL
) != NULL
)
940 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
942 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
946 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
951 pthread_mutex_lock(&hdl
->libzfs_mnttab_cache_lock
);
952 find
.mtn_mt
.mnt_special
= (char *)fsname
;
953 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
955 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
956 free(ret
->mtn_mt
.mnt_special
);
957 free(ret
->mtn_mt
.mnt_mountp
);
958 free(ret
->mtn_mt
.mnt_fstype
);
959 free(ret
->mtn_mt
.mnt_mntopts
);
962 pthread_mutex_unlock(&hdl
->libzfs_mnttab_cache_lock
);
966 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
968 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
970 if (zpool_handle
== NULL
)
973 *spa_version
= zpool_get_prop_int(zpool_handle
,
974 ZPOOL_PROP_VERSION
, NULL
);
979 * The choice of reservation property depends on the SPA version.
982 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
986 if (zfs_spa_version(zhp
, &spa_version
) < 0)
989 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
990 *resv_prop
= ZFS_PROP_REFRESERVATION
;
992 *resv_prop
= ZFS_PROP_RESERVATION
;
998 * Given an nvlist of properties to set, validates that they are correct, and
999 * parses any numeric properties (index, boolean, etc) if they are specified as
1003 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
1004 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
1005 boolean_t key_params_ok
, const char *errbuf
)
1012 int chosen_normal
= -1;
1013 int chosen_utf
= -1;
1015 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
1016 (void) no_memory(hdl
);
1021 * Make sure this property is valid and applies to this type.
1025 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
1026 const char *propname
= nvpair_name(elem
);
1028 prop
= zfs_name_to_prop(propname
);
1029 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
1031 * This is a user property: make sure it's a
1032 * string, and that it's less than ZAP_MAXNAMELEN.
1034 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
1035 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1036 "'%s' must be a string"), propname
);
1037 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1041 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
1042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1043 "property name '%s' is too long"),
1045 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1049 (void) nvpair_value_string(elem
, &strval
);
1050 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1051 (void) no_memory(hdl
);
1058 * Currently, only user properties can be modified on
1061 if (type
== ZFS_TYPE_SNAPSHOT
) {
1062 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1063 "this property can not be modified for snapshots"));
1064 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1068 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1069 zfs_userquota_prop_t uqtype
;
1070 char *newpropname
= NULL
;
1076 if (userquota_propname_decode(propname
, zoned
,
1077 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1079 dgettext(TEXT_DOMAIN
,
1080 "'%s' has an invalid user/group name"),
1082 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1086 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1087 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1088 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1089 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
&&
1090 uqtype
!= ZFS_PROP_PROJECTQUOTA
&&
1091 uqtype
!= ZFS_PROP_PROJECTOBJQUOTA
) {
1093 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1095 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1100 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1101 (void) nvpair_value_string(elem
, &strval
);
1102 if (strcmp(strval
, "none") == 0) {
1104 } else if (zfs_nicestrtonum(hdl
,
1105 strval
, &intval
) != 0) {
1106 (void) zfs_error(hdl
,
1107 EZFS_BADPROP
, errbuf
);
1110 } else if (nvpair_type(elem
) ==
1112 (void) nvpair_value_uint64(elem
, &intval
);
1114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1115 "use 'none' to disable "
1116 "{user|group|project}quota"));
1120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1121 "'%s' must be a number"), propname
);
1122 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1127 * Encode the prop name as
1128 * userquota@<hex-rid>-domain, to make it easy
1129 * for the kernel to decode.
1131 rc
= asprintf(&newpropname
, "%s%llx-%s",
1132 zfs_userquota_prop_prefixes
[uqtype
],
1133 (longlong_t
)rid
, domain
);
1134 if (rc
== -1 || newpropname
== NULL
) {
1135 (void) no_memory(hdl
);
1142 if (nvlist_add_uint64_array(ret
, newpropname
,
1145 (void) no_memory(hdl
);
1150 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1152 "'%s' is readonly"),
1154 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1158 if (prop
== ZPROP_INVAL
) {
1159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1160 "invalid property '%s'"), propname
);
1161 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1165 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1167 dgettext(TEXT_DOMAIN
, "'%s' does not "
1168 "apply to datasets of this type"), propname
);
1169 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1173 if (zfs_prop_readonly(prop
) &&
1174 !(zfs_prop_setonce(prop
) && zhp
== NULL
) &&
1175 !(zfs_prop_encryption_key_param(prop
) && key_params_ok
)) {
1177 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1179 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1183 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1184 &strval
, &intval
, errbuf
) != 0)
1188 * Perform some additional checks for specific properties.
1191 case ZFS_PROP_VERSION
:
1197 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1198 if (intval
< version
) {
1199 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1200 "Can not downgrade; already at version %u"),
1202 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1208 case ZFS_PROP_VOLBLOCKSIZE
:
1209 case ZFS_PROP_RECORDSIZE
:
1211 int maxbs
= SPA_MAXBLOCKSIZE
;
1214 if (zpool_hdl
!= NULL
) {
1215 maxbs
= zpool_get_prop_int(zpool_hdl
,
1216 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1219 * The value must be a power of two between
1220 * SPA_MINBLOCKSIZE and maxbs.
1222 if (intval
< SPA_MINBLOCKSIZE
||
1223 intval
> maxbs
|| !ISP2(intval
)) {
1224 zfs_nicebytes(maxbs
, buf
, sizeof (buf
));
1225 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1226 "'%s' must be power of 2 from 512B "
1227 "to %s"), propname
, buf
);
1228 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1234 case ZFS_PROP_SPECIAL_SMALL_BLOCKS
:
1235 if (zpool_hdl
!= NULL
) {
1236 char state
[64] = "";
1239 * Issue a warning but do not fail so that
1240 * tests for setable properties succeed.
1242 if (zpool_prop_get_feature(zpool_hdl
,
1243 "feature@allocation_classes", state
,
1244 sizeof (state
)) != 0 ||
1245 strcmp(state
, ZFS_FEATURE_ACTIVE
) != 0) {
1246 (void) fprintf(stderr
, gettext(
1247 "%s: property requires a special "
1248 "device in the pool\n"), propname
);
1252 (intval
< SPA_MINBLOCKSIZE
||
1253 intval
> SPA_OLD_MAXBLOCKSIZE
|| !ISP2(intval
))) {
1254 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1255 "invalid '%s=%d' property: must be zero or "
1256 "a power of 2 from 512B to 128K"), propname
,
1258 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1263 case ZFS_PROP_MLSLABEL
:
1265 #ifdef HAVE_MLSLABEL
1267 * Verify the mlslabel string and convert to
1268 * internal hex label string.
1272 char *hex
= NULL
; /* internal label string */
1274 /* Default value is already OK. */
1275 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1278 /* Verify the label can be converted to binary form */
1279 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1280 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1281 L_NO_CORRECTION
, NULL
) == -1)) {
1285 /* Now translate to hex internal label string */
1286 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1292 m_label_free(new_sl
);
1294 /* If string is already in internal form, we're done. */
1295 if (strcmp(strval
, hex
) == 0) {
1300 /* Replace the label string with the internal form. */
1301 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1303 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1310 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1311 "invalid mlslabel '%s'"), strval
);
1312 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1313 m_label_free(new_sl
); /* OK if null */
1316 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1317 "mlslabels are unsupported"));
1318 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1320 #endif /* HAVE_MLSLABEL */
1323 case ZFS_PROP_MOUNTPOINT
:
1325 namecheck_err_t why
;
1327 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1328 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1331 if (mountpoint_namecheck(strval
, &why
)) {
1333 case NAME_ERR_LEADING_SLASH
:
1335 dgettext(TEXT_DOMAIN
,
1336 "'%s' must be an absolute path, "
1337 "'none', or 'legacy'"), propname
);
1339 case NAME_ERR_TOOLONG
:
1341 dgettext(TEXT_DOMAIN
,
1342 "component of '%s' is too long"),
1348 dgettext(TEXT_DOMAIN
,
1349 "(%d) not defined"),
1353 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1360 case ZFS_PROP_SHARESMB
:
1361 case ZFS_PROP_SHARENFS
:
1363 * For the mountpoint and sharenfs or sharesmb
1364 * properties, check if it can be set in a
1365 * global/non-global zone based on
1366 * the zoned property value:
1368 * global zone non-global zone
1369 * --------------------------------------------------
1370 * zoned=on mountpoint (no) mountpoint (yes)
1371 * sharenfs (no) sharenfs (no)
1372 * sharesmb (no) sharesmb (no)
1374 * zoned=off mountpoint (yes) N/A
1379 if (getzoneid() == GLOBAL_ZONEID
) {
1380 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1381 "'%s' cannot be set on "
1382 "dataset in a non-global zone"),
1384 (void) zfs_error(hdl
, EZFS_ZONED
,
1387 } else if (prop
== ZFS_PROP_SHARENFS
||
1388 prop
== ZFS_PROP_SHARESMB
) {
1389 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1390 "'%s' cannot be set in "
1391 "a non-global zone"), propname
);
1392 (void) zfs_error(hdl
, EZFS_ZONED
,
1396 } else if (getzoneid() != GLOBAL_ZONEID
) {
1398 * If zoned property is 'off', this must be in
1399 * a global zone. If not, something is wrong.
1401 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1402 "'%s' cannot be set while dataset "
1403 "'zoned' property is set"), propname
);
1404 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1409 * At this point, it is legitimate to set the
1410 * property. Now we want to make sure that the
1411 * property value is valid if it is sharenfs.
1413 if ((prop
== ZFS_PROP_SHARENFS
||
1414 prop
== ZFS_PROP_SHARESMB
) &&
1415 strcmp(strval
, "on") != 0 &&
1416 strcmp(strval
, "off") != 0) {
1417 zfs_share_proto_t proto
;
1419 if (prop
== ZFS_PROP_SHARESMB
)
1425 * Must be an valid sharing protocol
1426 * option string so init the libshare
1427 * in order to enable the parser and
1428 * then parse the options. We use the
1429 * control API since we don't care about
1430 * the current configuration and don't
1431 * want the overhead of loading it
1432 * until we actually do something.
1435 if (zfs_init_libshare(hdl
,
1436 SA_INIT_CONTROL_API
) != SA_OK
) {
1438 * An error occurred so we can't do
1441 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1442 "'%s' cannot be set: problem "
1443 "in share initialization"),
1445 (void) zfs_error(hdl
, EZFS_BADPROP
,
1450 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1452 * There was an error in parsing so
1453 * deal with it by issuing an error
1454 * message and leaving after
1455 * uninitializing the the libshare
1458 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1459 "'%s' cannot be set to invalid "
1460 "options"), propname
);
1461 (void) zfs_error(hdl
, EZFS_BADPROP
,
1463 zfs_uninit_libshare(hdl
);
1466 zfs_uninit_libshare(hdl
);
1471 case ZFS_PROP_KEYLOCATION
:
1472 if (!zfs_prop_valid_keylocation(strval
, B_FALSE
)) {
1473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1474 "invalid keylocation"));
1475 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1481 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
1483 if (crypt
== ZIO_CRYPT_OFF
&&
1484 strcmp(strval
, "none") != 0) {
1485 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1486 "keylocation must be 'none' "
1487 "for unencrypted datasets"));
1488 (void) zfs_error(hdl
, EZFS_BADPROP
,
1491 } else if (crypt
!= ZIO_CRYPT_OFF
&&
1492 strcmp(strval
, "none") == 0) {
1493 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1494 "keylocation must not be 'none' "
1495 "for encrypted datasets"));
1496 (void) zfs_error(hdl
, EZFS_BADPROP
,
1503 case ZFS_PROP_PBKDF2_ITERS
:
1504 if (intval
< MIN_PBKDF2_ITERATIONS
) {
1505 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1506 "minimum pbkdf2 iterations is %u"),
1507 MIN_PBKDF2_ITERATIONS
);
1508 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1513 case ZFS_PROP_UTF8ONLY
:
1514 chosen_utf
= (int)intval
;
1517 case ZFS_PROP_NORMALIZE
:
1518 chosen_normal
= (int)intval
;
1526 * For changes to existing volumes, we have some additional
1527 * checks to enforce.
1529 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1530 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1531 ZFS_PROP_VOLBLOCKSIZE
);
1535 case ZFS_PROP_VOLSIZE
:
1536 if (intval
% blocksize
!= 0) {
1537 zfs_nicebytes(blocksize
, buf
,
1539 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1540 "'%s' must be a multiple of "
1541 "volume block size (%s)"),
1543 (void) zfs_error(hdl
, EZFS_BADPROP
,
1549 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1550 "'%s' cannot be zero"),
1552 (void) zfs_error(hdl
, EZFS_BADPROP
,
1563 /* check encryption properties */
1565 int64_t crypt
= zfs_prop_get_int(zhp
,
1566 ZFS_PROP_ENCRYPTION
);
1569 case ZFS_PROP_COPIES
:
1570 if (crypt
!= ZIO_CRYPT_OFF
&& intval
> 2) {
1571 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1572 "encrypted datasets cannot have "
1574 (void) zfs_error(hdl
, EZFS_BADPROP
,
1586 * If normalization was chosen, but no UTF8 choice was made,
1587 * enforce rejection of non-UTF8 names.
1589 * If normalization was chosen, but rejecting non-UTF8 names
1590 * was explicitly not chosen, it is an error.
1592 if (chosen_normal
> 0 && chosen_utf
< 0) {
1593 if (nvlist_add_uint64(ret
,
1594 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1595 (void) no_memory(hdl
);
1598 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1599 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1600 "'%s' must be set 'on' if normalization chosen"),
1601 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1602 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1613 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1615 uint64_t old_volsize
;
1616 uint64_t new_volsize
;
1617 uint64_t old_reservation
;
1618 uint64_t new_reservation
;
1619 zfs_prop_t resv_prop
;
1623 * If this is an existing volume, and someone is setting the volsize,
1624 * make sure that it matches the reservation, or add it if necessary.
1626 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1627 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1629 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1631 props
= fnvlist_alloc();
1632 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1633 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1635 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1636 old_reservation
) || nvlist_exists(nvl
,
1637 zfs_prop_to_name(resv_prop
))) {
1638 fnvlist_free(props
);
1641 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1642 &new_volsize
) != 0) {
1643 fnvlist_free(props
);
1646 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1647 fnvlist_free(props
);
1649 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1650 new_reservation
) != 0) {
1651 (void) no_memory(zhp
->zfs_hdl
);
1658 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1659 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1660 * Return codes must match zfs_add_synthetic_resv().
1663 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1670 if (!ZFS_IS_VOLUME(zhp
)) {
1674 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1678 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1682 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1683 /* No value being set, so it can't be "auto" */
1686 if (resvsize
!= UINT64_MAX
) {
1687 /* Being set to a value other than "auto" */
1691 props
= fnvlist_alloc();
1693 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1694 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1696 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1698 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1701 resvsize
= zvol_volsize_to_reservation(volsize
, props
);
1702 fnvlist_free(props
);
1704 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1705 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1706 (void) no_memory(zhp
->zfs_hdl
);
1713 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1720 * For quotas and reservations, ENOSPC indicates
1721 * something different; setting a quota or reservation
1722 * doesn't use any disk space.
1725 case ZFS_PROP_QUOTA
:
1726 case ZFS_PROP_REFQUOTA
:
1727 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1728 "size is less than current used or "
1730 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1733 case ZFS_PROP_RESERVATION
:
1734 case ZFS_PROP_REFRESERVATION
:
1735 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1736 "size is greater than available space"));
1737 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1741 (void) zfs_standard_error(hdl
, err
, errbuf
);
1747 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1751 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1755 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1756 "property value too long"));
1757 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1761 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1762 "pool and or dataset must be upgraded to set this "
1763 "property or value"));
1764 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1768 if (prop
== ZFS_PROP_COMPRESSION
||
1769 prop
== ZFS_PROP_DNODESIZE
||
1770 prop
== ZFS_PROP_RECORDSIZE
) {
1771 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1772 "property setting is not allowed on "
1773 "bootable datasets"));
1774 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1775 } else if (prop
== ZFS_PROP_CHECKSUM
||
1776 prop
== ZFS_PROP_DEDUP
) {
1777 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1778 "property setting is not allowed on "
1780 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1782 (void) zfs_standard_error(hdl
, err
, errbuf
);
1787 if (prop
== ZPROP_INVAL
) {
1788 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1790 (void) zfs_standard_error(hdl
, err
, errbuf
);
1795 if (prop
== ZFS_PROP_KEYLOCATION
) {
1796 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1797 "keylocation may only be set on encryption roots"));
1798 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1800 (void) zfs_standard_error(hdl
, err
, errbuf
);
1806 * This platform can't address a volume this big.
1809 if (prop
== ZFS_PROP_VOLSIZE
) {
1810 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1816 (void) zfs_standard_error(hdl
, err
, errbuf
);
1821 zfs_is_namespace_prop(zfs_prop_t prop
)
1825 case ZFS_PROP_ATIME
:
1826 case ZFS_PROP_RELATIME
:
1827 case ZFS_PROP_DEVICES
:
1829 case ZFS_PROP_SETUID
:
1830 case ZFS_PROP_READONLY
:
1831 case ZFS_PROP_XATTR
:
1832 case ZFS_PROP_NBMAND
:
1841 * Given a property name and value, set the property for the given dataset.
1844 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1848 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1849 nvlist_t
*nvl
= NULL
;
1851 (void) snprintf(errbuf
, sizeof (errbuf
),
1852 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1855 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1856 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1857 (void) no_memory(hdl
);
1861 ret
= zfs_prop_set_list(zhp
, nvl
);
1871 * Given an nvlist of property names and values, set the properties for the
1875 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1877 zfs_cmd_t zc
= {"\0"};
1879 prop_changelist_t
**cls
= NULL
;
1882 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1886 zfs_prop_t prop
= 0;
1889 (void) snprintf(errbuf
, sizeof (errbuf
),
1890 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1893 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1894 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1895 B_FALSE
, errbuf
)) == NULL
)
1899 * We have to check for any extra properties which need to be added
1900 * before computing the length of the nvlist.
1902 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1904 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1905 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1906 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1911 if (added_resv
!= 1 &&
1912 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1917 * Check how many properties we're setting and allocate an array to
1918 * store changelist pointers for postfix().
1920 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1922 elem
= nvlist_next_nvpair(nvl
, elem
))
1924 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1928 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1930 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1932 prop
= zfs_name_to_prop(nvpair_name(elem
));
1934 assert(cl_idx
< nvl_len
);
1936 * We don't want to unmount & remount the dataset when changing
1937 * its canmount property to 'on' or 'noauto'. We only use
1938 * the changelist logic to unmount when setting canmount=off.
1940 if (prop
!= ZFS_PROP_CANMOUNT
||
1941 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1942 zfs_is_mounted(zhp
, NULL
))) {
1943 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1944 if (cls
[cl_idx
] == NULL
)
1948 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1949 changelist_haszonedchild(cls
[cl_idx
])) {
1950 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1951 "child dataset with inherited mountpoint is used "
1952 "in a non-global zone"));
1953 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1957 if (cls
[cl_idx
] != NULL
&&
1958 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1963 assert(cl_idx
== nvl_len
);
1966 * Execute the corresponding ioctl() to set this list of properties.
1968 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1970 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1971 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1974 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1977 if (zc
.zc_nvlist_dst_filled
== B_FALSE
) {
1978 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1982 /* Get the list of unset properties back and report them. */
1983 nvlist_t
*errorprops
= NULL
;
1984 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1986 for (nvpair_t
*elem
= nvlist_next_nvpair(errorprops
, NULL
);
1988 elem
= nvlist_next_nvpair(errorprops
, elem
)) {
1989 prop
= zfs_name_to_prop(nvpair_name(elem
));
1990 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1992 nvlist_free(errorprops
);
1994 if (added_resv
&& errno
== ENOSPC
) {
1995 /* clean up the volsize property we tried to set */
1996 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
2000 zcmd_free_nvlists(&zc
);
2002 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
2004 if (nvlist_add_uint64(nvl
,
2005 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
2008 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
2010 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
2013 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
2014 if (cls
[cl_idx
] != NULL
) {
2015 int clp_err
= changelist_postfix(cls
[cl_idx
]);
2023 * Refresh the statistics so the new property
2024 * value is reflected.
2026 (void) get_stats(zhp
);
2029 * Remount the filesystem to propagate the change
2030 * if one of the options handled by the generic
2031 * Linux namespace layer has been modified.
2033 if (zfs_is_namespace_prop(prop
) &&
2034 zfs_is_mounted(zhp
, NULL
))
2035 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2041 zcmd_free_nvlists(&zc
);
2043 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
2044 if (cls
[cl_idx
] != NULL
)
2045 changelist_free(cls
[cl_idx
]);
2053 * Given a property, inherit the value from the parent dataset, or if received
2054 * is TRUE, revert to the received value, if any.
2057 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
2059 zfs_cmd_t zc
= {"\0"};
2061 prop_changelist_t
*cl
;
2062 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2066 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2067 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
2069 zc
.zc_cookie
= received
;
2070 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
2072 * For user properties, the amount of work we have to do is very
2073 * small, so just do it here.
2075 if (!zfs_prop_user(propname
)) {
2076 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2077 "invalid property"));
2078 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2081 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2082 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2084 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
2085 return (zfs_standard_error(hdl
, errno
, errbuf
));
2091 * Verify that this property is inheritable.
2093 if (zfs_prop_readonly(prop
))
2094 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
2096 if (!zfs_prop_inheritable(prop
) && !received
)
2097 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
2100 * Check to see if the value applies to this type
2102 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2103 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
2106 * Normalize the name, to get rid of shorthand abbreviations.
2108 propname
= zfs_prop_to_name(prop
);
2109 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2110 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
2112 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
2113 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
2114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2115 "dataset is used in a non-global zone"));
2116 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
2120 * Determine datasets which will be affected by this change, if any.
2122 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
2125 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
2126 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2127 "child dataset with inherited mountpoint is used "
2128 "in a non-global zone"));
2129 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
2133 if ((ret
= changelist_prefix(cl
)) != 0)
2136 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2137 return (zfs_standard_error(hdl
, errno
, errbuf
));
2140 if ((ret
= changelist_postfix(cl
)) != 0)
2144 * Refresh the statistics so the new property is reflected.
2146 (void) get_stats(zhp
);
2149 * Remount the filesystem to propagate the change
2150 * if one of the options handled by the generic
2151 * Linux namespace layer has been modified.
2153 if (zfs_is_namespace_prop(prop
) &&
2154 zfs_is_mounted(zhp
, NULL
))
2155 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
2159 changelist_free(cl
);
2164 * True DSL properties are stored in an nvlist. The following two functions
2165 * extract them appropriately.
2168 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2174 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2175 zfs_prop_to_name(prop
), &nv
) == 0) {
2176 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2177 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2179 verify(!zhp
->zfs_props_table
||
2180 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2181 value
= zfs_prop_default_numeric(prop
);
2189 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2195 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2196 zfs_prop_to_name(prop
), &nv
) == 0) {
2197 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
2198 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2200 verify(!zhp
->zfs_props_table
||
2201 zhp
->zfs_props_table
[prop
] == B_TRUE
);
2202 value
= zfs_prop_default_string(prop
);
2210 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
2212 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
2216 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2218 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
2219 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2223 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2225 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2230 * Internal function for getting a numeric property. Both zfs_prop_get() and
2231 * zfs_prop_get_int() are built using this interface.
2233 * Certain properties can be overridden using 'mount -o'. In this case, scan
2234 * the contents of the /proc/self/mounts entry, searching for the
2235 * appropriate options. If they differ from the on-disk values, report the
2236 * current values and mark the source "temporary".
2239 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2240 char **source
, uint64_t *val
)
2242 zfs_cmd_t zc
= {"\0"};
2243 nvlist_t
*zplprops
= NULL
;
2245 char *mntopt_on
= NULL
;
2246 char *mntopt_off
= NULL
;
2247 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2252 * If the property is being fetched for a snapshot, check whether
2253 * the property is valid for the snapshot's head dataset type.
2255 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2256 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2257 *val
= zfs_prop_default_numeric(prop
);
2262 case ZFS_PROP_ATIME
:
2263 mntopt_on
= MNTOPT_ATIME
;
2264 mntopt_off
= MNTOPT_NOATIME
;
2267 case ZFS_PROP_RELATIME
:
2268 mntopt_on
= MNTOPT_RELATIME
;
2269 mntopt_off
= MNTOPT_NORELATIME
;
2272 case ZFS_PROP_DEVICES
:
2273 mntopt_on
= MNTOPT_DEVICES
;
2274 mntopt_off
= MNTOPT_NODEVICES
;
2278 mntopt_on
= MNTOPT_EXEC
;
2279 mntopt_off
= MNTOPT_NOEXEC
;
2282 case ZFS_PROP_READONLY
:
2283 mntopt_on
= MNTOPT_RO
;
2284 mntopt_off
= MNTOPT_RW
;
2287 case ZFS_PROP_SETUID
:
2288 mntopt_on
= MNTOPT_SETUID
;
2289 mntopt_off
= MNTOPT_NOSETUID
;
2292 case ZFS_PROP_XATTR
:
2293 mntopt_on
= MNTOPT_XATTR
;
2294 mntopt_off
= MNTOPT_NOXATTR
;
2297 case ZFS_PROP_NBMAND
:
2298 mntopt_on
= MNTOPT_NBMAND
;
2299 mntopt_off
= MNTOPT_NONBMAND
;
2307 * Because looking up the mount options is potentially expensive
2308 * (iterating over all of /proc/self/mounts), we defer its
2309 * calculation until we're looking up a property which requires
2312 if (!zhp
->zfs_mntcheck
&&
2313 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2314 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2315 struct mnttab entry
;
2317 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2318 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2320 if (zhp
->zfs_mntopts
== NULL
)
2324 zhp
->zfs_mntcheck
= B_TRUE
;
2327 if (zhp
->zfs_mntopts
== NULL
)
2328 mnt
.mnt_mntopts
= "";
2330 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2333 case ZFS_PROP_ATIME
:
2334 case ZFS_PROP_RELATIME
:
2335 case ZFS_PROP_DEVICES
:
2337 case ZFS_PROP_READONLY
:
2338 case ZFS_PROP_SETUID
:
2339 case ZFS_PROP_XATTR
:
2340 case ZFS_PROP_NBMAND
:
2341 *val
= getprop_uint64(zhp
, prop
, source
);
2346 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2349 *src
= ZPROP_SRC_TEMPORARY
;
2350 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2353 *src
= ZPROP_SRC_TEMPORARY
;
2357 case ZFS_PROP_CANMOUNT
:
2358 case ZFS_PROP_VOLSIZE
:
2359 case ZFS_PROP_QUOTA
:
2360 case ZFS_PROP_REFQUOTA
:
2361 case ZFS_PROP_RESERVATION
:
2362 case ZFS_PROP_REFRESERVATION
:
2363 case ZFS_PROP_FILESYSTEM_LIMIT
:
2364 case ZFS_PROP_SNAPSHOT_LIMIT
:
2365 case ZFS_PROP_FILESYSTEM_COUNT
:
2366 case ZFS_PROP_SNAPSHOT_COUNT
:
2367 *val
= getprop_uint64(zhp
, prop
, source
);
2369 if (*source
== NULL
) {
2370 /* not default, must be local */
2371 *source
= zhp
->zfs_name
;
2375 case ZFS_PROP_MOUNTED
:
2376 *val
= (zhp
->zfs_mntopts
!= NULL
);
2379 case ZFS_PROP_NUMCLONES
:
2380 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2383 case ZFS_PROP_VERSION
:
2384 case ZFS_PROP_NORMALIZE
:
2385 case ZFS_PROP_UTF8ONLY
:
2387 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2389 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2390 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2391 zcmd_free_nvlists(&zc
);
2392 if (prop
== ZFS_PROP_VERSION
&&
2393 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2394 *val
= zfs_prop_default_numeric(prop
);
2397 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2398 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2400 zcmd_free_nvlists(&zc
);
2403 nvlist_free(zplprops
);
2404 zcmd_free_nvlists(&zc
);
2407 case ZFS_PROP_INCONSISTENT
:
2408 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2412 switch (zfs_prop_get_type(prop
)) {
2413 case PROP_TYPE_NUMBER
:
2414 case PROP_TYPE_INDEX
:
2415 *val
= getprop_uint64(zhp
, prop
, source
);
2417 * If we tried to use a default value for a
2418 * readonly property, it means that it was not
2419 * present. Note this only applies to "truly"
2420 * readonly properties, not set-once properties
2421 * like volblocksize.
2423 if (zfs_prop_readonly(prop
) &&
2424 !zfs_prop_setonce(prop
) &&
2425 *source
!= NULL
&& (*source
)[0] == '\0') {
2431 case PROP_TYPE_STRING
:
2433 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2434 "cannot get non-numeric property"));
2435 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2436 dgettext(TEXT_DOMAIN
, "internal error")));
2444 * Calculate the source type, given the raw source string.
2447 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2448 char *statbuf
, size_t statlen
)
2450 if (statbuf
== NULL
||
2451 srctype
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
) {
2455 if (source
== NULL
) {
2456 *srctype
= ZPROP_SRC_NONE
;
2457 } else if (source
[0] == '\0') {
2458 *srctype
= ZPROP_SRC_DEFAULT
;
2459 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2460 *srctype
= ZPROP_SRC_RECEIVED
;
2462 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2463 *srctype
= ZPROP_SRC_LOCAL
;
2465 (void) strlcpy(statbuf
, source
, statlen
);
2466 *srctype
= ZPROP_SRC_INHERITED
;
2473 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2474 size_t proplen
, boolean_t literal
)
2479 if (zhp
->zfs_recvd_props
== NULL
)
2480 if (get_recvd_props_ioctl(zhp
) != 0)
2483 prop
= zfs_name_to_prop(propname
);
2485 if (prop
!= ZPROP_INVAL
) {
2487 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2489 zfs_set_recvd_props_mode(zhp
, &cookie
);
2490 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2491 NULL
, NULL
, 0, literal
);
2492 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2496 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2497 propname
, &propval
) != 0)
2499 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2501 (void) strlcpy(propbuf
, recvdval
, proplen
);
2504 return (err
== 0 ? 0 : -1);
2508 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2513 value
= zfs_get_clones_nvl(zhp
);
2518 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2519 pair
= nvlist_next_nvpair(value
, pair
)) {
2520 if (propbuf
[0] != '\0')
2521 (void) strlcat(propbuf
, ",", proplen
);
2522 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2528 struct get_clones_arg
{
2532 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2536 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2538 struct get_clones_arg
*gca
= arg
;
2540 if (gca
->numclones
== 0) {
2545 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2546 NULL
, NULL
, 0, B_TRUE
) != 0)
2548 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2549 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2554 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2560 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2562 nvlist_t
*nv
, *value
;
2564 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2565 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2566 struct get_clones_arg gca
;
2569 * if this is a snapshot, then the kernel wasn't able
2570 * to get the clones. Do it by slowly iterating.
2572 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2574 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2576 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2581 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2583 gca
.origin
= zhp
->zfs_name
;
2585 if (gca
.numclones
!= 0) {
2587 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2590 /* get the pool name */
2591 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2592 (void) strsep(&cp
, "/@");
2593 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2594 ZFS_TYPE_FILESYSTEM
);
2601 (void) get_clones_cb(root
, &gca
);
2604 if (gca
.numclones
!= 0 ||
2605 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2606 nvlist_add_nvlist(zhp
->zfs_props
,
2607 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2614 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2615 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2618 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2624 * Accepts a property and value and checks that the value
2625 * matches the one found by the channel program. If they are
2626 * not equal, print both of them.
2629 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2632 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2635 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2636 const char *prop_name
= zfs_prop_to_name(prop
);
2637 const char *program
=
2639 "ds = args['dataset']\n"
2640 "prop = args['property']\n"
2641 "value, setpoint = zfs.get_prop(ds, prop)\n"
2642 "return {value=value, setpoint=setpoint}\n";
2645 nvlist_t
*argnvl
= fnvlist_alloc();
2647 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2648 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2650 error
= lzc_channel_program_nosync(poolname
, program
,
2651 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2654 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2655 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2657 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2659 (void) fprintf(stderr
, "%s: zcp check error: "
2660 "%u\n", prop_name
, error
);
2663 if (ans
!= intval
) {
2664 (void) fprintf(stderr
, "%s: zfs found %llu, "
2665 "but zcp found %llu\n", prop_name
,
2666 (u_longlong_t
)intval
, (u_longlong_t
)ans
);
2670 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2672 (void) fprintf(stderr
, "%s: zcp check error: "
2673 "%u\n", prop_name
, error
);
2676 if (strcmp(strval
, str_ans
) != 0) {
2677 (void) fprintf(stderr
,
2678 "%s: zfs found '%s', but zcp found '%s'\n",
2679 prop_name
, strval
, str_ans
);
2683 (void) fprintf(stderr
, "%s: zcp check failed, channel program "
2684 "error: %u\n", prop_name
, error
);
2686 nvlist_free(argnvl
);
2687 nvlist_free(outnvl
);
2691 * Retrieve a property from the given object. If 'literal' is specified, then
2692 * numbers are left as exact values. Otherwise, numbers are converted to a
2693 * human-readable form.
2695 * Returns 0 on success, or -1 on error.
2698 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2699 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2701 char *source
= NULL
;
2705 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2708 * Check to see if this property applies to our object
2710 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2713 if (received
&& zfs_prop_readonly(prop
))
2717 *src
= ZPROP_SRC_NONE
;
2720 case ZFS_PROP_CREATION
:
2722 * 'creation' is a time_t stored in the statistics. We convert
2723 * this into a string unless 'literal' is specified.
2726 val
= getprop_uint64(zhp
, prop
, &source
);
2727 time_t time
= (time_t)val
;
2731 localtime_r(&time
, &t
) == NULL
||
2732 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2734 (void) snprintf(propbuf
, proplen
, "%llu",
2737 zcp_check(zhp
, prop
, val
, NULL
);
2740 case ZFS_PROP_MOUNTPOINT
:
2742 * Getting the precise mountpoint can be tricky.
2744 * - for 'none' or 'legacy', return those values.
2745 * - for inherited mountpoints, we want to take everything
2746 * after our ancestor and append it to the inherited value.
2748 * If the pool has an alternate root, we want to prepend that
2749 * root to any values we return.
2752 str
= getprop_string(zhp
, prop
, &source
);
2754 if (str
[0] == '/') {
2755 char buf
[MAXPATHLEN
];
2757 const char *relpath
;
2760 * If we inherit the mountpoint, even from a dataset
2761 * with a received value, the source will be the path of
2762 * the dataset we inherit from. If source is
2763 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2766 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2769 relpath
= zhp
->zfs_name
+ strlen(source
);
2770 if (relpath
[0] == '/')
2774 if ((zpool_get_prop(zhp
->zpool_hdl
,
2775 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2776 B_FALSE
)) || (strcmp(root
, "-") == 0))
2779 * Special case an alternate root of '/'. This will
2780 * avoid having multiple leading slashes in the
2783 if (strcmp(root
, "/") == 0)
2787 * If the mountpoint is '/' then skip over this
2788 * if we are obtaining either an alternate root or
2789 * an inherited mountpoint.
2791 if (str
[1] == '\0' && (root
[0] != '\0' ||
2792 relpath
[0] != '\0'))
2795 if (relpath
[0] == '\0')
2796 (void) snprintf(propbuf
, proplen
, "%s%s",
2799 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2800 root
, str
, relpath
[0] == '@' ? "" : "/",
2803 /* 'legacy' or 'none' */
2804 (void) strlcpy(propbuf
, str
, proplen
);
2806 zcp_check(zhp
, prop
, 0, propbuf
);
2809 case ZFS_PROP_ORIGIN
:
2810 str
= getprop_string(zhp
, prop
, &source
);
2813 (void) strlcpy(propbuf
, str
, proplen
);
2814 zcp_check(zhp
, prop
, 0, str
);
2817 case ZFS_PROP_CLONES
:
2818 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2822 case ZFS_PROP_QUOTA
:
2823 case ZFS_PROP_REFQUOTA
:
2824 case ZFS_PROP_RESERVATION
:
2825 case ZFS_PROP_REFRESERVATION
:
2827 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2830 * If quota or reservation is 0, we translate this into 'none'
2831 * (unless literal is set), and indicate that it's the default
2832 * value. Otherwise, we print the number nicely and indicate
2833 * that its set locally.
2837 (void) strlcpy(propbuf
, "0", proplen
);
2839 (void) strlcpy(propbuf
, "none", proplen
);
2842 (void) snprintf(propbuf
, proplen
, "%llu",
2845 zfs_nicebytes(val
, propbuf
, proplen
);
2847 zcp_check(zhp
, prop
, val
, NULL
);
2850 case ZFS_PROP_FILESYSTEM_LIMIT
:
2851 case ZFS_PROP_SNAPSHOT_LIMIT
:
2852 case ZFS_PROP_FILESYSTEM_COUNT
:
2853 case ZFS_PROP_SNAPSHOT_COUNT
:
2855 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2859 * If limit is UINT64_MAX, we translate this into 'none' (unless
2860 * literal is set), and indicate that it's the default value.
2861 * Otherwise, we print the number nicely and indicate that it's
2865 (void) snprintf(propbuf
, proplen
, "%llu",
2867 } else if (val
== UINT64_MAX
) {
2868 (void) strlcpy(propbuf
, "none", proplen
);
2870 zfs_nicenum(val
, propbuf
, proplen
);
2873 zcp_check(zhp
, prop
, val
, NULL
);
2876 case ZFS_PROP_REFRATIO
:
2877 case ZFS_PROP_COMPRESSRATIO
:
2878 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2881 (void) snprintf(propbuf
, proplen
, "%llu.%02llu",
2882 (u_longlong_t
)(val
/ 100),
2883 (u_longlong_t
)(val
% 100));
2885 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2886 (u_longlong_t
)(val
/ 100),
2887 (u_longlong_t
)(val
% 100));
2888 zcp_check(zhp
, prop
, val
, NULL
);
2892 switch (zhp
->zfs_type
) {
2893 case ZFS_TYPE_FILESYSTEM
:
2896 case ZFS_TYPE_VOLUME
:
2899 case ZFS_TYPE_SNAPSHOT
:
2902 case ZFS_TYPE_BOOKMARK
:
2908 (void) snprintf(propbuf
, proplen
, "%s", str
);
2909 zcp_check(zhp
, prop
, 0, propbuf
);
2912 case ZFS_PROP_MOUNTED
:
2914 * The 'mounted' property is a pseudo-property that described
2915 * whether the filesystem is currently mounted. Even though
2916 * it's a boolean value, the typical values of "on" and "off"
2917 * don't make sense, so we translate to "yes" and "no".
2919 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2920 src
, &source
, &val
) != 0)
2923 (void) strlcpy(propbuf
, "yes", proplen
);
2925 (void) strlcpy(propbuf
, "no", proplen
);
2930 * The 'name' property is a pseudo-property derived from the
2931 * dataset name. It is presented as a real property to simplify
2934 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2935 zcp_check(zhp
, prop
, 0, propbuf
);
2938 case ZFS_PROP_MLSLABEL
:
2940 #ifdef HAVE_MLSLABEL
2941 m_label_t
*new_sl
= NULL
;
2942 char *ascii
= NULL
; /* human readable label */
2944 (void) strlcpy(propbuf
,
2945 getprop_string(zhp
, prop
, &source
), proplen
);
2947 if (literal
|| (strcasecmp(propbuf
,
2948 ZFS_MLSLABEL_DEFAULT
) == 0))
2952 * Try to translate the internal hex string to
2953 * human-readable output. If there are any
2954 * problems just use the hex string.
2957 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2958 L_NO_CORRECTION
, NULL
) == -1) {
2959 m_label_free(new_sl
);
2963 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2967 m_label_free(new_sl
);
2970 m_label_free(new_sl
);
2972 (void) strlcpy(propbuf
, ascii
, proplen
);
2975 (void) strlcpy(propbuf
,
2976 getprop_string(zhp
, prop
, &source
), proplen
);
2977 #endif /* HAVE_MLSLABEL */
2982 case ZFS_PROP_CREATETXG
:
2983 case ZFS_PROP_OBJSETID
:
2985 * These properties are stored as numbers, but they are
2987 * We don't want them to be pretty printed, because pretty
2988 * printing mangles the ID into a truncated and useless value.
2990 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2992 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2993 zcp_check(zhp
, prop
, val
, NULL
);
2996 case ZFS_PROP_REFERENCED
:
2997 case ZFS_PROP_AVAILABLE
:
2999 case ZFS_PROP_USEDSNAP
:
3000 case ZFS_PROP_USEDDS
:
3001 case ZFS_PROP_USEDREFRESERV
:
3002 case ZFS_PROP_USEDCHILD
:
3003 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
3006 (void) snprintf(propbuf
, proplen
, "%llu",
3009 zfs_nicebytes(val
, propbuf
, proplen
);
3011 zcp_check(zhp
, prop
, val
, NULL
);
3015 switch (zfs_prop_get_type(prop
)) {
3016 case PROP_TYPE_NUMBER
:
3017 if (get_numeric_property(zhp
, prop
, src
,
3018 &source
, &val
) != 0) {
3023 (void) snprintf(propbuf
, proplen
, "%llu",
3026 zfs_nicenum(val
, propbuf
, proplen
);
3028 zcp_check(zhp
, prop
, val
, NULL
);
3031 case PROP_TYPE_STRING
:
3032 str
= getprop_string(zhp
, prop
, &source
);
3036 (void) strlcpy(propbuf
, str
, proplen
);
3037 zcp_check(zhp
, prop
, 0, str
);
3040 case PROP_TYPE_INDEX
:
3041 if (get_numeric_property(zhp
, prop
, src
,
3042 &source
, &val
) != 0)
3044 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
3047 (void) strlcpy(propbuf
, strval
, proplen
);
3048 zcp_check(zhp
, prop
, 0, strval
);
3056 get_source(zhp
, src
, source
, statbuf
, statlen
);
3062 * Utility function to get the given numeric property. Does no validation that
3063 * the given property is the appropriate type; should only be used with
3064 * hard-coded property types.
3067 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
3072 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
3078 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
3082 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
3083 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
3087 * Similar to zfs_prop_get(), but returns the value as an integer.
3090 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
3091 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
3096 * Check to see if this property applies to our object
3098 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
3099 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
3100 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
3101 zfs_prop_to_name(prop
)));
3105 *src
= ZPROP_SRC_NONE
;
3107 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
3110 get_source(zhp
, src
, source
, statbuf
, statlen
);
3117 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
3118 char **domainp
, idmap_rid_t
*ridp
)
3120 idmap_get_handle_t
*get_hdl
= NULL
;
3124 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
3128 err
= idmap_get_sidbyuid(get_hdl
, id
,
3129 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3131 err
= idmap_get_sidbygid(get_hdl
, id
,
3132 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
3134 if (err
== IDMAP_SUCCESS
&&
3135 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
3136 status
== IDMAP_SUCCESS
)
3142 idmap_get_destroy(get_hdl
);
3145 #endif /* HAVE_IDMAP */
3148 * convert the propname into parameters needed by kernel
3149 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3150 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3151 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3152 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3153 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3154 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3157 userquota_propname_decode(const char *propname
, boolean_t zoned
,
3158 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
3160 zfs_userquota_prop_t type
;
3164 boolean_t isproject
;
3170 /* Figure out the property type ({user|group|project}{quota|space}) */
3171 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
3172 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
3173 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
3176 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
3180 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
3181 type
== ZFS_PROP_USEROBJQUOTA
||
3182 type
== ZFS_PROP_USEROBJUSED
);
3183 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
3184 type
== ZFS_PROP_GROUPOBJQUOTA
||
3185 type
== ZFS_PROP_GROUPOBJUSED
);
3186 isproject
= (type
== ZFS_PROP_PROJECTQUOTA
||
3187 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTOBJQUOTA
||
3188 type
== ZFS_PROP_PROJECTOBJUSED
);
3190 cp
= strchr(propname
, '@') + 1;
3192 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
3193 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3196 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
3197 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3200 } else if (!isproject
&& strchr(cp
, '@')) {
3203 * It's a SID name (eg "user@domain") that needs to be
3204 * turned into S-1-domainID-RID.
3206 directory_error_t e
;
3207 char *numericsid
= NULL
;
3210 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
3213 e
= directory_sid_from_user_name(NULL
,
3216 e
= directory_sid_from_group_name(NULL
,
3220 directory_error_free(e
);
3223 if (numericsid
== NULL
)
3226 (void) strlcpy(domain
, cp
, domainlen
);
3227 cp
= strrchr(domain
, '-');
3232 *ridp
= strtoull(cp
, &end
, 10);
3235 if (errno
!= 0 || *end
!= '\0')
3239 #endif /* HAVE_IDMAP */
3241 /* It's a user/group/project ID (eg "12345"). */
3244 id
= strtoul(cp
, &end
, 10);
3247 if (id
> MAXUID
&& !isproject
) {
3249 /* It's an ephemeral ID. */
3253 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
3254 &mapdomain
, &rid
) != 0)
3256 (void) strlcpy(domain
, mapdomain
, domainlen
);
3260 #endif /* HAVE_IDMAP */
3270 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
3271 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
3274 zfs_cmd_t zc
= {"\0"};
3276 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3278 err
= userquota_propname_decode(propname
,
3279 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
3280 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
3281 zc
.zc_objset_type
= *typep
;
3285 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
3289 *propvalue
= zc
.zc_cookie
;
3294 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
3295 uint64_t *propvalue
)
3297 zfs_userquota_prop_t type
;
3299 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
3304 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
3305 char *propbuf
, int proplen
, boolean_t literal
)
3309 zfs_userquota_prop_t type
;
3311 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
3318 (void) snprintf(propbuf
, proplen
, "%llu",
3319 (u_longlong_t
)propvalue
);
3320 } else if (propvalue
== 0 &&
3321 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3322 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
||
3323 type
== ZFS_PROP_PROJECTQUOTA
||
3324 type
== ZFS_PROP_PROJECTOBJQUOTA
)) {
3325 (void) strlcpy(propbuf
, "none", proplen
);
3326 } else if (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
3327 type
== ZFS_PROP_USERUSED
|| type
== ZFS_PROP_GROUPUSED
||
3328 type
== ZFS_PROP_PROJECTUSED
|| type
== ZFS_PROP_PROJECTQUOTA
) {
3329 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3331 zfs_nicenum(propvalue
, propbuf
, proplen
);
3337 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
3338 uint64_t *propvalue
)
3341 zfs_cmd_t zc
= {"\0"};
3342 const char *snapname
;
3344 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3346 snapname
= strchr(propname
, '@') + 1;
3347 if (strchr(snapname
, '@')) {
3348 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3350 /* snapname is the short name, append it to zhp's fsname */
3353 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3354 sizeof (zc
.zc_value
));
3355 cp
= strchr(zc
.zc_value
, '@');
3358 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
3359 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3362 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3366 *propvalue
= zc
.zc_cookie
;
3371 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3372 char *propbuf
, int proplen
, boolean_t literal
)
3377 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3383 (void) snprintf(propbuf
, proplen
, "%llu",
3384 (u_longlong_t
)propvalue
);
3386 zfs_nicebytes(propvalue
, propbuf
, proplen
);
3393 * Returns the name of the given zfs handle.
3396 zfs_get_name(const zfs_handle_t
*zhp
)
3398 return (zhp
->zfs_name
);
3402 * Returns the name of the parent pool for the given zfs handle.
3405 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3407 return (zhp
->zpool_hdl
->zpool_name
);
3411 * Returns the type of the given zfs handle.
3414 zfs_get_type(const zfs_handle_t
*zhp
)
3416 return (zhp
->zfs_type
);
3420 * Is one dataset name a child dataset of another?
3422 * Needs to handle these cases:
3423 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3424 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3425 * Descendant? No. No. No. Yes.
3428 is_descendant(const char *ds1
, const char *ds2
)
3430 size_t d1len
= strlen(ds1
);
3432 /* ds2 can't be a descendant if it's smaller */
3433 if (strlen(ds2
) < d1len
)
3436 /* otherwise, compare strings and verify that there's a '/' char */
3437 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3441 * Given a complete name, return just the portion that refers to the parent.
3442 * Will return -1 if there is no parent (path is just the name of the
3446 parent_name(const char *path
, char *buf
, size_t buflen
)
3450 (void) strlcpy(buf
, path
, buflen
);
3452 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3460 zfs_parent_name(zfs_handle_t
*zhp
, char *buf
, size_t buflen
)
3462 return (parent_name(zfs_get_name(zhp
), buf
, buflen
));
3466 * If accept_ancestor is false, then check to make sure that the given path has
3467 * a parent, and that it exists. If accept_ancestor is true, then find the
3468 * closest existing ancestor for the given path. In prefixlen return the
3469 * length of already existing prefix of the given path. We also fetch the
3470 * 'zoned' property, which is used to validate property settings when creating
3474 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3475 boolean_t accept_ancestor
, int *prefixlen
)
3477 zfs_cmd_t zc
= {"\0"};
3478 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3484 (void) snprintf(errbuf
, sizeof (errbuf
),
3485 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3487 /* get parent, and check to see if this is just a pool */
3488 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3489 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3490 "missing dataset name"));
3491 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3494 /* check to see if the pool exists */
3495 if ((slash
= strchr(parent
, '/')) == NULL
)
3496 slash
= parent
+ strlen(parent
);
3497 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3498 zc
.zc_name
[slash
- parent
] = '\0';
3499 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3501 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3502 "no such pool '%s'"), zc
.zc_name
);
3503 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3506 /* check to see if the parent dataset exists */
3507 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3508 if (errno
== ENOENT
&& accept_ancestor
) {
3510 * Go deeper to find an ancestor, give up on top level.
3512 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3513 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3514 "no such pool '%s'"), zc
.zc_name
);
3515 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3517 } else if (errno
== ENOENT
) {
3518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3519 "parent does not exist"));
3520 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3522 return (zfs_standard_error(hdl
, errno
, errbuf
));
3525 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3529 /* we are in a non-global zone, but parent is in the global zone */
3530 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3531 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3536 /* make sure parent is a filesystem */
3537 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3538 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3539 "parent is not a filesystem"));
3540 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3546 if (prefixlen
!= NULL
)
3547 *prefixlen
= strlen(parent
);
3552 * Finds whether the dataset of the given type(s) exists.
3555 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3559 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3563 * Try to get stats for the dataset, which will tell us if it exists.
3565 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3566 int ds_type
= zhp
->zfs_type
;
3569 if (types
& ds_type
)
3576 * Given a path to 'target', create all the ancestors between
3577 * the prefixlen portion of the path, and the target itself.
3578 * Fail if the initial prefixlen-ancestor does not already exist.
3581 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3587 /* make sure prefix exists */
3588 cp
= target
+ prefixlen
;
3590 assert(strchr(cp
, '/') == NULL
);
3591 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3594 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3602 * Attempt to create, mount, and share any ancestor filesystems,
3603 * up to the prefixlen-long one.
3605 for (cp
= target
+ prefixlen
+ 1;
3606 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3610 h
= make_dataset_handle(hdl
, target
);
3612 /* it already exists, nothing to do here */
3617 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3619 opname
= dgettext(TEXT_DOMAIN
, "create");
3623 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3625 opname
= dgettext(TEXT_DOMAIN
, "open");
3629 if (zfs_mount(h
, NULL
, 0) != 0) {
3630 opname
= dgettext(TEXT_DOMAIN
, "mount");
3634 if (zfs_share(h
) != 0) {
3635 opname
= dgettext(TEXT_DOMAIN
, "share");
3645 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3646 "failed to %s ancestor '%s'"), opname
, target
);
3651 * Creates non-existing ancestors of the given path.
3654 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3661 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3662 "cannot create '%s'"), path
);
3665 * Check that we are not passing the nesting limit
3666 * before we start creating any ancestors.
3668 if (dataset_nestcheck(path
) != 0) {
3669 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3670 "maximum name nesting depth exceeded"));
3671 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3674 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3677 if ((path_copy
= strdup(path
)) != NULL
) {
3678 rc
= create_parents(hdl
, path_copy
, prefix
);
3681 if (path_copy
== NULL
|| rc
!= 0)
3688 * Create a new filesystem or volume.
3691 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3696 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3698 enum lzc_dataset_type ost
;
3699 zpool_handle_t
*zpool_handle
;
3700 uint8_t *wkeydata
= NULL
;
3703 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3705 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3706 "cannot create '%s'"), path
);
3708 /* validate the path, taking care to note the extended error message */
3709 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3710 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3712 if (dataset_nestcheck(path
) != 0) {
3713 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3714 "maximum name nesting depth exceeded"));
3715 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3718 /* validate parents exist */
3719 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3723 * The failure modes when creating a dataset of a different type over
3724 * one that already exists is a little strange. In particular, if you
3725 * try to create a dataset on top of an existing dataset, the ioctl()
3726 * will return ENOENT, not EEXIST. To prevent this from happening, we
3727 * first try to see if the dataset exists.
3729 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3731 "dataset already exists"));
3732 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3735 if (type
== ZFS_TYPE_VOLUME
)
3736 ost
= LZC_DATSET_TYPE_ZVOL
;
3738 ost
= LZC_DATSET_TYPE_ZFS
;
3740 /* open zpool handle for prop validation */
3741 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3742 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3744 /* truncate pool_path at first slash */
3745 char *p
= strchr(pool_path
, '/');
3749 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3752 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3753 zoned
, NULL
, zpool_handle
, B_TRUE
, errbuf
)) == 0) {
3754 zpool_close(zpool_handle
);
3757 zpool_close(zpool_handle
);
3759 if (type
== ZFS_TYPE_VOLUME
) {
3761 * If we are creating a volume, the size and block size must
3762 * satisfy a few restraints. First, the blocksize must be a
3763 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3764 * volsize must be a multiple of the block size, and cannot be
3767 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3768 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3770 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3771 "missing volume size"));
3772 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3775 if ((ret
= nvlist_lookup_uint64(props
,
3776 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3777 &blocksize
)) != 0) {
3778 if (ret
== ENOENT
) {
3779 blocksize
= zfs_prop_default_numeric(
3780 ZFS_PROP_VOLBLOCKSIZE
);
3783 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3784 "missing volume block size"));
3785 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3791 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3792 "volume size cannot be zero"));
3793 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3796 if (size
% blocksize
!= 0) {
3798 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3799 "volume size must be a multiple of volume block "
3801 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3805 (void) parent_name(path
, parent
, sizeof (parent
));
3806 if (zfs_crypto_create(hdl
, parent
, props
, NULL
, B_TRUE
,
3807 &wkeydata
, &wkeylen
) != 0) {
3809 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3812 /* create the dataset */
3813 ret
= lzc_create(path
, ost
, props
, wkeydata
, wkeylen
);
3815 if (wkeydata
!= NULL
)
3818 /* check for failure */
3822 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3823 "no such parent '%s'"), parent
);
3824 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3827 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3828 "pool must be upgraded to set this "
3829 "property or value"));
3830 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3833 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3834 "encryption root's key is not loaded "
3836 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
3839 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3840 "invalid property value(s) specified"));
3841 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3845 * This platform can't address a volume this big.
3847 if (type
== ZFS_TYPE_VOLUME
)
3848 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3853 return (zfs_standard_error(hdl
, errno
, errbuf
));
3861 * Destroys the given dataset. The caller must make sure that the filesystem
3862 * isn't mounted, and that there are no active dependents. If the file system
3863 * does not exist this function does nothing.
3866 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3870 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
&& defer
)
3873 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3874 nvlist_t
*nv
= fnvlist_alloc();
3875 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3876 error
= lzc_destroy_bookmarks(nv
, NULL
);
3879 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, error
,
3880 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3886 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3887 nvlist_t
*nv
= fnvlist_alloc();
3888 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3889 error
= lzc_destroy_snaps(nv
, defer
, NULL
);
3892 error
= lzc_destroy(zhp
->zfs_name
);
3895 if (error
!= 0 && error
!= ENOENT
) {
3896 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3897 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3901 remove_mountpoint(zhp
);
3906 struct destroydata
{
3908 const char *snapname
;
3912 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3914 struct destroydata
*dd
= arg
;
3915 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3918 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
3919 dd
->snapname
) >= sizeof (name
))
3922 if (lzc_exists(name
))
3923 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3925 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3931 * Destroys all snapshots with the given name in zhp & descendants.
3934 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3937 struct destroydata dd
= { 0 };
3939 dd
.snapname
= snapname
;
3940 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3941 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3943 if (nvlist_empty(dd
.nvl
)) {
3944 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3945 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3946 zhp
->zfs_name
, snapname
);
3948 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3950 nvlist_free(dd
.nvl
);
3955 * Destroys all the snapshots named in the nvlist.
3958 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3961 nvlist_t
*errlist
= NULL
;
3964 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3967 nvlist_free(errlist
);
3971 if (nvlist_empty(errlist
)) {
3973 (void) snprintf(errbuf
, sizeof (errbuf
),
3974 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3976 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3978 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3979 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3981 (void) snprintf(errbuf
, sizeof (errbuf
),
3982 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3985 switch (fnvpair_value_int32(pair
)) {
3988 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3989 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3992 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3997 nvlist_free(errlist
);
4002 * Clones the given dataset. The target must be of the same type as the source.
4005 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
4007 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4010 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4013 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
4015 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4016 "cannot create '%s'"), target
);
4018 /* validate the target/clone name */
4019 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
4020 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4022 /* validate parents exist */
4023 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
4026 (void) parent_name(target
, parent
, sizeof (parent
));
4033 if (ZFS_IS_VOLUME(zhp
)) {
4034 type
= ZFS_TYPE_VOLUME
;
4036 type
= ZFS_TYPE_FILESYSTEM
;
4038 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
4039 zhp
, zhp
->zpool_hdl
, B_TRUE
, errbuf
)) == NULL
)
4041 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
4047 if (zfs_crypto_clone_check(hdl
, zhp
, parent
, props
) != 0) {
4049 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
4052 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
4060 * The parent doesn't exist. We should have caught this
4061 * above, but there may a race condition that has since
4062 * destroyed the parent.
4064 * At this point, we don't know whether it's the source
4065 * that doesn't exist anymore, or whether the target
4066 * dataset doesn't exist.
4068 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4069 "no such parent '%s'"), parent
);
4070 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
4073 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4074 "source and target pools differ"));
4075 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
4079 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
4088 * Promotes the given clone fs to be the clone parent.
4091 zfs_promote(zfs_handle_t
*zhp
)
4093 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4094 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
4098 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4099 "cannot promote '%s'"), zhp
->zfs_name
);
4101 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4103 "snapshots can not be promoted"));
4104 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4107 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
4108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4109 "not a cloned filesystem"));
4110 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4113 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4114 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4116 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
4121 /* There is a conflicting snapshot name. */
4122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4123 "conflicting snapshot '%s' from parent '%s'"),
4124 snapname
, zhp
->zfs_dmustats
.dds_origin
);
4125 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
4128 return (zfs_standard_error(hdl
, ret
, errbuf
));
4134 typedef struct snapdata
{
4136 const char *sd_snapname
;
4140 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
4142 snapdata_t
*sd
= arg
;
4143 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4146 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
4147 if (snprintf(name
, sizeof (name
), "%s@%s", zfs_get_name(zhp
),
4148 sd
->sd_snapname
) >= sizeof (name
))
4151 fnvlist_add_boolean(sd
->sd_nvl
, name
);
4153 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
4161 zfs_remap_indirects(libzfs_handle_t
*hdl
, const char *fs
)
4166 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4167 "cannot remap dataset '%s'"), fs
);
4169 err
= lzc_remap(fs
);
4174 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4175 "pool must be upgraded"));
4176 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4179 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4182 (void) zfs_standard_error(hdl
, err
, errbuf
);
4191 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4195 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
4201 zpool_handle_t
*zpool_hdl
;
4202 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
4204 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4205 "cannot create snapshots "));
4208 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
4209 const char *snapname
= nvpair_name(elem
);
4211 /* validate the target name */
4212 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
4214 (void) snprintf(errbuf
, sizeof (errbuf
),
4215 dgettext(TEXT_DOMAIN
,
4216 "cannot create snapshot '%s'"), snapname
);
4217 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4222 * get pool handle for prop validation. assumes all snaps are in the
4223 * same pool, as does lzc_snapshot (below).
4225 elem
= nvlist_next_nvpair(snaps
, NULL
);
4226 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
4227 pool
[strcspn(pool
, "/@")] = '\0';
4228 zpool_hdl
= zpool_open(hdl
, pool
);
4229 if (zpool_hdl
== NULL
)
4232 if (props
!= NULL
&&
4233 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
4234 props
, B_FALSE
, NULL
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4235 zpool_close(zpool_hdl
);
4238 zpool_close(zpool_hdl
);
4240 ret
= lzc_snapshot(snaps
, props
, &errors
);
4243 boolean_t printed
= B_FALSE
;
4244 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4246 elem
= nvlist_next_nvpair(errors
, elem
)) {
4247 (void) snprintf(errbuf
, sizeof (errbuf
),
4248 dgettext(TEXT_DOMAIN
,
4249 "cannot create snapshot '%s'"), nvpair_name(elem
));
4250 (void) zfs_standard_error(hdl
,
4251 fnvpair_value_int32(elem
), errbuf
);
4257 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4258 "multiple snapshots of same "
4260 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4264 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4270 nvlist_free(errors
);
4275 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
4279 snapdata_t sd
= { 0 };
4280 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
4285 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4286 "cannot snapshot %s"), path
);
4288 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
4289 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4291 (void) strlcpy(fsname
, path
, sizeof (fsname
));
4292 cp
= strchr(fsname
, '@');
4294 sd
.sd_snapname
= cp
+ 1;
4296 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
4297 ZFS_TYPE_VOLUME
)) == NULL
) {
4301 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
4303 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
4305 fnvlist_add_boolean(sd
.sd_nvl
, path
);
4308 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
4309 nvlist_free(sd
.sd_nvl
);
4315 * Destroy any more recent snapshots. We invoke this callback on any dependents
4316 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4317 * is a dependent and we should just destroy it without checking the transaction
4320 typedef struct rollback_data
{
4321 const char *cb_target
; /* the snapshot */
4322 uint64_t cb_create
; /* creation time reference */
4328 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
4330 rollback_data_t
*cbp
= data
;
4331 prop_changelist_t
*clp
;
4333 /* We must destroy this clone; first unmount it */
4334 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4335 cbp
->cb_force
? MS_FORCE
: 0);
4336 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
4337 cbp
->cb_error
= B_TRUE
;
4341 if (zfs_destroy(zhp
, B_FALSE
) != 0)
4342 cbp
->cb_error
= B_TRUE
;
4344 changelist_remove(clp
, zhp
->zfs_name
);
4345 (void) changelist_postfix(clp
);
4346 changelist_free(clp
);
4353 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
4355 rollback_data_t
*cbp
= data
;
4357 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
4358 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
4359 rollback_destroy_dependent
, cbp
);
4361 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
4369 * Given a dataset, rollback to a specific snapshot, discarding any
4370 * data changes since then and making it the active dataset.
4372 * Any snapshots and bookmarks more recent than the target are
4373 * destroyed, along with their dependents (i.e. clones).
4376 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
4378 rollback_data_t cb
= { 0 };
4380 boolean_t restore_resv
= 0;
4381 uint64_t old_volsize
= 0, new_volsize
;
4382 zfs_prop_t resv_prop
= { 0 };
4383 uint64_t min_txg
= 0;
4385 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
4386 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
4389 * Destroy all recent snapshots and their dependents.
4391 cb
.cb_force
= force
;
4392 cb
.cb_target
= snap
->zfs_name
;
4393 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
4395 if (cb
.cb_create
> 0)
4396 min_txg
= cb
.cb_create
;
4398 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
,
4401 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
4407 * Now that we have verified that the snapshot is the latest,
4408 * rollback to the given snapshot.
4411 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
4412 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4414 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4416 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4420 * Pass both the filesystem and the wanted snapshot names,
4421 * we would get an error back if the snapshot is destroyed or
4422 * a new snapshot is created before this request is processed.
4424 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4428 (void) snprintf(errbuf
, sizeof (errbuf
),
4429 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4433 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4434 "there is a snapshot or bookmark more recent "
4435 "than '%s'"), snap
->zfs_name
);
4436 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4439 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4440 "'%s' is not found among snapshots of '%s'"),
4441 snap
->zfs_name
, zhp
->zfs_name
);
4442 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4445 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4448 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4454 * For volumes, if the pre-rollback volsize matched the pre-
4455 * rollback reservation and the volsize has changed then set
4456 * the reservation property to the post-rollback volsize.
4457 * Make a new handle since the rollback closed the dataset.
4459 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4460 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4462 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4463 if (old_volsize
!= new_volsize
)
4464 err
= zfs_prop_set_int(zhp
, resv_prop
,
4473 * Renames the given dataset.
4476 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4477 boolean_t force_unmount
)
4480 zfs_cmd_t zc
= {"\0"};
4482 prop_changelist_t
*cl
= NULL
;
4483 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4484 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4487 /* if we have the same exact name, just return success */
4488 if (strcmp(zhp
->zfs_name
, target
) == 0)
4491 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4492 "cannot rename to '%s'"), target
);
4494 /* make sure source name is valid */
4495 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4496 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4499 * Make sure the target name is valid
4501 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4502 if ((strchr(target
, '@') == NULL
) ||
4505 * Snapshot target name is abbreviated,
4506 * reconstruct full dataset name
4508 (void) strlcpy(parent
, zhp
->zfs_name
,
4510 delim
= strchr(parent
, '@');
4511 if (strchr(target
, '@') == NULL
)
4515 (void) strlcat(parent
, target
, sizeof (parent
));
4519 * Make sure we're renaming within the same dataset.
4521 delim
= strchr(target
, '@');
4522 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4523 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4525 "snapshots must be part of same "
4527 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4532 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4533 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4537 "recursive rename must be a snapshot"));
4538 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4541 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4542 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4544 /* validate parents */
4545 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4548 /* make sure we're in the same pool */
4549 verify((delim
= strchr(target
, '/')) != NULL
);
4550 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4551 zhp
->zfs_name
[delim
- target
] != '/') {
4552 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4553 "datasets must be within same pool"));
4554 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4557 /* new name cannot be a child of the current dataset name */
4558 if (is_descendant(zhp
->zfs_name
, target
)) {
4559 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4560 "New dataset name cannot be a descendant of "
4561 "current dataset name"));
4562 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4566 (void) snprintf(errbuf
, sizeof (errbuf
),
4567 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4569 if (getzoneid() == GLOBAL_ZONEID
&&
4570 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4571 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4572 "dataset is used in a non-global zone"));
4573 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4578 char *parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4579 if (parentname
== NULL
) {
4583 delim
= strchr(parentname
, '@');
4585 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4592 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4593 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
,
4594 CL_GATHER_ITER_MOUNTED
,
4595 force_unmount
? MS_FORCE
: 0)) == NULL
)
4598 if (changelist_haszonedchild(cl
)) {
4599 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4600 "child dataset with inherited mountpoint is used "
4601 "in a non-global zone"));
4602 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4607 if ((ret
= changelist_prefix(cl
)) != 0)
4611 if (ZFS_IS_VOLUME(zhp
))
4612 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4614 zc
.zc_objset_type
= DMU_OST_ZFS
;
4616 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4617 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4619 zc
.zc_cookie
= recursive
;
4621 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4623 * if it was recursive, the one that actually failed will
4626 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4627 "cannot rename '%s'"), zc
.zc_name
);
4629 if (recursive
&& errno
== EEXIST
) {
4630 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4631 "a child dataset already has a snapshot "
4632 "with the new name"));
4633 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4634 } else if (errno
== EACCES
) {
4635 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) ==
4637 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4638 "cannot rename an unencrypted dataset to "
4639 "be a decendent of an encrypted one"));
4641 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4642 "cannot move encryption child outside of "
4643 "its encryption root"));
4645 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4647 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4651 * On failure, we still want to remount any filesystems that
4652 * were previously mounted, so we don't alter the system state.
4655 (void) changelist_postfix(cl
);
4658 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4659 ret
= changelist_postfix(cl
);
4665 changelist_free(cl
);
4671 zfs_get_all_props(zfs_handle_t
*zhp
)
4673 return (zhp
->zfs_props
);
4677 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4679 if (zhp
->zfs_recvd_props
== NULL
)
4680 if (get_recvd_props_ioctl(zhp
) != 0)
4682 return (zhp
->zfs_recvd_props
);
4686 zfs_get_user_props(zfs_handle_t
*zhp
)
4688 return (zhp
->zfs_user_props
);
4692 * This function is used by 'zfs list' to determine the exact set of columns to
4693 * display, and their maximum widths. This does two main things:
4695 * - If this is a list of all properties, then expand the list to include
4696 * all native properties, and set a flag so that for each dataset we look
4697 * for new unique user properties and add them to the list.
4699 * - For non fixed-width properties, keep track of the maximum width seen
4700 * so that we can size the column appropriately. If the user has
4701 * requested received property values, we also need to compute the width
4702 * of the RECEIVED column.
4705 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4708 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4709 zprop_list_t
*entry
;
4710 zprop_list_t
**last
, **start
;
4711 nvlist_t
*userprops
, *propval
;
4714 char buf
[ZFS_MAXPROPLEN
];
4716 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4719 userprops
= zfs_get_user_props(zhp
);
4722 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4724 * Go through and add any user properties as necessary. We
4725 * start by incrementing our list pointer to the first
4726 * non-native property.
4729 while (*start
!= NULL
) {
4730 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4732 start
= &(*start
)->pl_next
;
4736 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4738 * See if we've already found this property in our list.
4740 for (last
= start
; *last
!= NULL
;
4741 last
= &(*last
)->pl_next
) {
4742 if (strcmp((*last
)->pl_user_prop
,
4743 nvpair_name(elem
)) == 0)
4747 if (*last
== NULL
) {
4748 if ((entry
= zfs_alloc(hdl
,
4749 sizeof (zprop_list_t
))) == NULL
||
4750 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4751 nvpair_name(elem
)))) == NULL
) {
4756 entry
->pl_prop
= ZPROP_INVAL
;
4757 entry
->pl_width
= strlen(nvpair_name(elem
));
4758 entry
->pl_all
= B_TRUE
;
4765 * Now go through and check the width of any non-fixed columns
4767 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4768 if (entry
->pl_fixed
&& !literal
)
4771 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4772 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4773 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4774 if (strlen(buf
) > entry
->pl_width
)
4775 entry
->pl_width
= strlen(buf
);
4777 if (received
&& zfs_prop_get_recvd(zhp
,
4778 zfs_prop_to_name(entry
->pl_prop
),
4779 buf
, sizeof (buf
), literal
) == 0)
4780 if (strlen(buf
) > entry
->pl_recvd_width
)
4781 entry
->pl_recvd_width
= strlen(buf
);
4783 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4785 verify(nvlist_lookup_string(propval
,
4786 ZPROP_VALUE
, &strval
) == 0);
4787 if (strlen(strval
) > entry
->pl_width
)
4788 entry
->pl_width
= strlen(strval
);
4790 if (received
&& zfs_prop_get_recvd(zhp
,
4791 entry
->pl_user_prop
,
4792 buf
, sizeof (buf
), literal
) == 0)
4793 if (strlen(buf
) > entry
->pl_recvd_width
)
4794 entry
->pl_recvd_width
= strlen(buf
);
4802 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4808 * Keep a reference to the props-table against which we prune the
4811 zhp
->zfs_props_table
= props
;
4813 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4816 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4817 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4820 * User properties will result in ZPROP_INVAL, and since we
4821 * only know how to prune standard ZFS properties, we always
4822 * leave these in the list. This can also happen if we
4823 * encounter an unknown DSL property (when running older
4824 * software, for example).
4826 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4827 (void) nvlist_remove(zhp
->zfs_props
,
4828 nvpair_name(curr
), nvpair_type(curr
));
4834 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4835 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4837 zfs_cmd_t zc
= {"\0"};
4838 nvlist_t
*nvlist
= NULL
;
4841 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4842 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4843 zc
.zc_cookie
= (uint64_t)cmd
;
4845 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4846 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4847 (void) no_memory(hdl
);
4853 case ZFS_SMB_ACL_ADD
:
4854 case ZFS_SMB_ACL_REMOVE
:
4855 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4857 case ZFS_SMB_ACL_RENAME
:
4858 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4860 (void) no_memory(hdl
);
4863 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4865 (void) no_memory(hdl
);
4868 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4869 nvlist_free(nvlist
);
4873 case ZFS_SMB_ACL_PURGE
:
4878 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4879 nvlist_free(nvlist
);
4884 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4885 char *path
, char *resource
)
4887 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4892 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4893 char *path
, char *resource
)
4895 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4900 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4902 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4907 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4908 char *oldname
, char *newname
)
4910 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4915 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4916 zfs_userspace_cb_t func
, void *arg
)
4918 zfs_cmd_t zc
= {"\0"};
4919 zfs_useracct_t buf
[100];
4920 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4923 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4925 zc
.zc_objset_type
= type
;
4926 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4929 zfs_useracct_t
*zua
= buf
;
4931 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4932 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4935 if ((errno
== ENOTSUP
&&
4936 (type
== ZFS_PROP_USEROBJUSED
||
4937 type
== ZFS_PROP_GROUPOBJUSED
||
4938 type
== ZFS_PROP_USEROBJQUOTA
||
4939 type
== ZFS_PROP_GROUPOBJQUOTA
||
4940 type
== ZFS_PROP_PROJECTOBJUSED
||
4941 type
== ZFS_PROP_PROJECTOBJQUOTA
||
4942 type
== ZFS_PROP_PROJECTUSED
||
4943 type
== ZFS_PROP_PROJECTQUOTA
)))
4946 (void) snprintf(errbuf
, sizeof (errbuf
),
4947 dgettext(TEXT_DOMAIN
,
4948 "cannot get used/quota for %s"), zc
.zc_name
);
4949 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4951 if (zc
.zc_nvlist_dst_size
== 0)
4954 while (zc
.zc_nvlist_dst_size
> 0) {
4955 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4956 zua
->zu_space
)) != 0)
4959 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4968 const char *snapname
;
4970 boolean_t recursive
;
4975 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4977 struct holdarg
*ha
= arg
;
4978 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4981 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
4982 ha
->snapname
) >= sizeof (name
))
4985 if (lzc_exists(name
))
4986 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4989 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4995 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4996 boolean_t recursive
, int cleanup_fd
)
5001 ha
.nvl
= fnvlist_alloc();
5002 ha
.snapname
= snapname
;
5004 ha
.recursive
= recursive
;
5005 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
5007 if (nvlist_empty(ha
.nvl
)) {
5010 fnvlist_free(ha
.nvl
);
5012 (void) snprintf(errbuf
, sizeof (errbuf
),
5013 dgettext(TEXT_DOMAIN
,
5014 "cannot hold snapshot '%s@%s'"),
5015 zhp
->zfs_name
, snapname
);
5016 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
5020 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
5021 fnvlist_free(ha
.nvl
);
5027 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
5031 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5036 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
5039 /* There may be errors even in the success case. */
5040 fnvlist_free(errors
);
5044 if (nvlist_empty(errors
)) {
5045 /* no hold-specific errors */
5046 (void) snprintf(errbuf
, sizeof (errbuf
),
5047 dgettext(TEXT_DOMAIN
, "cannot hold"));
5050 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5051 "pool must be upgraded"));
5052 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5055 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5058 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5062 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5064 elem
= nvlist_next_nvpair(errors
, elem
)) {
5065 (void) snprintf(errbuf
, sizeof (errbuf
),
5066 dgettext(TEXT_DOMAIN
,
5067 "cannot hold snapshot '%s'"), nvpair_name(elem
));
5068 switch (fnvpair_value_int32(elem
)) {
5071 * Temporary tags wind up having the ds object id
5072 * prepended. So even if we passed the length check
5073 * above, it's still possible for the tag to wind
5074 * up being slightly too long.
5076 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
5079 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5082 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
5085 (void) zfs_standard_error(hdl
,
5086 fnvpair_value_int32(elem
), errbuf
);
5090 fnvlist_free(errors
);
5095 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
5097 struct holdarg
*ha
= arg
;
5098 char name
[ZFS_MAX_DATASET_NAME_LEN
];
5100 nvlist_t
*existing_holds
;
5102 if (snprintf(name
, sizeof (name
), "%s@%s", zhp
->zfs_name
,
5103 ha
->snapname
) >= sizeof (name
)) {
5108 if (lzc_get_holds(name
, &existing_holds
) != 0) {
5110 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
5113 nvlist_t
*torelease
= fnvlist_alloc();
5114 fnvlist_add_boolean(torelease
, ha
->tag
);
5115 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
5116 fnvlist_free(torelease
);
5120 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
5126 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
5127 boolean_t recursive
)
5131 nvlist_t
*errors
= NULL
;
5133 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5136 ha
.nvl
= fnvlist_alloc();
5137 ha
.snapname
= snapname
;
5139 ha
.recursive
= recursive
;
5141 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
5143 if (nvlist_empty(ha
.nvl
)) {
5144 fnvlist_free(ha
.nvl
);
5146 (void) snprintf(errbuf
, sizeof (errbuf
),
5147 dgettext(TEXT_DOMAIN
,
5148 "cannot release hold from snapshot '%s@%s'"),
5149 zhp
->zfs_name
, snapname
);
5151 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5153 (void) zfs_standard_error(hdl
, ret
, errbuf
);
5158 ret
= lzc_release(ha
.nvl
, &errors
);
5159 fnvlist_free(ha
.nvl
);
5162 /* There may be errors even in the success case. */
5163 fnvlist_free(errors
);
5167 if (nvlist_empty(errors
)) {
5168 /* no hold-specific errors */
5169 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5173 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5174 "pool must be upgraded"));
5175 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5178 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5182 for (elem
= nvlist_next_nvpair(errors
, NULL
);
5184 elem
= nvlist_next_nvpair(errors
, elem
)) {
5185 (void) snprintf(errbuf
, sizeof (errbuf
),
5186 dgettext(TEXT_DOMAIN
,
5187 "cannot release hold from snapshot '%s'"),
5189 switch (fnvpair_value_int32(elem
)) {
5191 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
5194 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5197 (void) zfs_standard_error_fmt(hdl
,
5198 fnvpair_value_int32(elem
), errbuf
);
5202 fnvlist_free(errors
);
5207 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5209 zfs_cmd_t zc
= {"\0"};
5210 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5216 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5217 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5221 nvbuf
= malloc(nvsz
);
5222 if (nvbuf
== NULL
) {
5223 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
5227 zc
.zc_nvlist_dst_size
= nvsz
;
5228 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
5230 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5232 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
5233 (void) snprintf(errbuf
, sizeof (errbuf
),
5234 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5239 nvsz
= zc
.zc_nvlist_dst_size
;
5243 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5244 "pool must be upgraded"));
5245 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5248 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5251 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5254 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5259 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
5261 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
5262 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
5264 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
5274 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
5276 zfs_cmd_t zc
= {"\0"};
5277 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5283 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
5284 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
5286 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
5289 nvbuf
= malloc(nvsz
);
5291 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
5294 zc
.zc_nvlist_src_size
= nvsz
;
5295 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
5296 zc
.zc_perm_action
= un
;
5298 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
5300 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
5301 (void) snprintf(errbuf
, sizeof (errbuf
),
5302 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
5306 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5307 "pool must be upgraded"));
5308 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5311 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5314 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5317 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5328 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
5333 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
5336 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
5338 (void) snprintf(errbuf
, sizeof (errbuf
),
5339 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
5343 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5344 "pool must be upgraded"));
5345 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5348 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
5351 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
5354 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
5363 * Convert the zvol's volume size to an appropriate reservation.
5364 * Note: If this routine is updated, it is necessary to update the ZFS test
5365 * suite's shell version in reservation.kshlib.
5368 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
5371 uint64_t nblocks
, volblocksize
;
5375 if (nvlist_lookup_string(props
,
5376 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
5377 ncopies
= atoi(strval
);
5380 if (nvlist_lookup_uint64(props
,
5381 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
5382 &volblocksize
) != 0)
5383 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
5384 nblocks
= volsize
/volblocksize
;
5385 /* start with metadnode L0-L6 */
5387 /* calculate number of indirects */
5388 while (nblocks
> 1) {
5389 nblocks
+= DNODES_PER_LEVEL
- 1;
5390 nblocks
/= DNODES_PER_LEVEL
;
5393 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
5396 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5397 * compressed, but in practice they compress down to about
5400 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;