4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2015 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 2013 Nexenta Systems, Inc. All rights reserved.
44 #include <sys/mntent.h>
45 #include <sys/mount.h>
54 #include <directory.h>
55 #endif /* HAVE_IDMAP */
57 #include <sys/dnode.h>
62 #include "zfs_namecheck.h"
64 #include "libzfs_impl.h"
65 #include "zfs_deleg.h"
67 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
68 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
71 * Given a single type (not a mask of types), return the type in a human
75 zfs_type_to_name(zfs_type_t type
)
78 case ZFS_TYPE_FILESYSTEM
:
79 return (dgettext(TEXT_DOMAIN
, "filesystem"));
80 case ZFS_TYPE_SNAPSHOT
:
81 return (dgettext(TEXT_DOMAIN
, "snapshot"));
83 return (dgettext(TEXT_DOMAIN
, "volume"));
92 * Validate a ZFS path. This is used even before trying to open the dataset, to
93 * provide a more meaningful error message. We call zfs_error_aux() to
94 * explain exactly why the name was not valid.
97 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
103 (void) zfs_prop_get_table();
104 if (dataset_namecheck(path
, &why
, &what
) != 0) {
107 case NAME_ERR_TOOLONG
:
108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
109 "name is too long"));
112 case NAME_ERR_LEADING_SLASH
:
113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
114 "leading slash in name"));
117 case NAME_ERR_EMPTY_COMPONENT
:
118 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
119 "empty component in name"));
122 case NAME_ERR_TRAILING_SLASH
:
123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
124 "trailing slash in name"));
127 case NAME_ERR_INVALCHAR
:
129 dgettext(TEXT_DOMAIN
, "invalid character "
130 "'%c' in name"), what
);
133 case NAME_ERR_MULTIPLE_AT
:
134 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
135 "multiple '@' delimiters in name"));
138 case NAME_ERR_NOLETTER
:
139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
140 "pool doesn't begin with a letter"));
143 case NAME_ERR_RESERVED
:
144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
145 "name is reserved"));
148 case NAME_ERR_DISKLIKE
:
149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
150 "reserved disk name"));
160 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
162 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
163 "snapshot delimiter '@' in filesystem name"));
167 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
169 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
170 "missing '@' delimiter in snapshot name"));
174 if (modifying
&& strchr(path
, '%') != NULL
) {
176 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
177 "invalid character %c in name"), '%');
185 zfs_name_valid(const char *name
, zfs_type_t type
)
187 if (type
== ZFS_TYPE_POOL
)
188 return (zpool_name_valid(NULL
, B_FALSE
, name
));
189 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
193 * This function takes the raw DSL properties, and filters out the user-defined
194 * properties into a separate nvlist.
197 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
199 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
204 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
205 (void) no_memory(hdl
);
210 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
211 if (!zfs_prop_user(nvpair_name(elem
)))
214 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
215 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
217 (void) no_memory(hdl
);
225 static zpool_handle_t
*
226 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
228 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
231 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
232 if (hdl
->libzfs_pool_handles
!= NULL
)
233 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
234 hdl
->libzfs_pool_handles
= zph
;
239 static zpool_handle_t
*
240 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
242 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
243 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
245 while ((zph
!= NULL
) &&
246 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
247 zph
= zph
->zpool_next
;
252 * Returns a handle to the pool that contains the provided dataset.
253 * If a handle to that pool already exists then that handle is returned.
254 * Otherwise, a new handle is created and added to the list of handles.
256 static zpool_handle_t
*
257 zpool_handle(zfs_handle_t
*zhp
)
263 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
264 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
265 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
267 zph
= zpool_find_handle(zhp
, pool_name
, len
);
269 zph
= zpool_add_handle(zhp
, pool_name
);
276 zpool_free_handles(libzfs_handle_t
*hdl
)
278 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
280 while (zph
!= NULL
) {
281 next
= zph
->zpool_next
;
285 hdl
->libzfs_pool_handles
= NULL
;
289 * Utility function to gather stats (objset and zpl) for the given object.
292 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
294 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
296 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
298 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
299 if (errno
== ENOMEM
) {
300 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
311 * Utility function to get the received properties of the given object.
314 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
316 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
317 nvlist_t
*recvdprops
;
318 zfs_cmd_t zc
= {"\0"};
321 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
324 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
326 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
327 if (errno
== ENOMEM
) {
328 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
332 zcmd_free_nvlists(&zc
);
337 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
338 zcmd_free_nvlists(&zc
);
342 nvlist_free(zhp
->zfs_recvd_props
);
343 zhp
->zfs_recvd_props
= recvdprops
;
349 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
351 nvlist_t
*allprops
, *userprops
;
353 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
355 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
360 * XXX Why do we store the user props separately, in addition to
361 * storing them in zfs_props?
363 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
364 nvlist_free(allprops
);
368 nvlist_free(zhp
->zfs_props
);
369 nvlist_free(zhp
->zfs_user_props
);
371 zhp
->zfs_props
= allprops
;
372 zhp
->zfs_user_props
= userprops
;
378 get_stats(zfs_handle_t
*zhp
)
381 zfs_cmd_t zc
= {"\0"};
383 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
385 if (get_stats_ioctl(zhp
, &zc
) != 0)
387 else if (put_stats_zhdl(zhp
, &zc
) != 0)
389 zcmd_free_nvlists(&zc
);
394 * Refresh the properties currently stored in the handle.
397 zfs_refresh_properties(zfs_handle_t
*zhp
)
399 (void) get_stats(zhp
);
403 * Makes a handle from the given dataset name. Used by zfs_open() and
404 * zfs_iter_* to create child handles on the fly.
407 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
409 if (put_stats_zhdl(zhp
, zc
) != 0)
413 * We've managed to open the dataset and gather statistics. Determine
414 * the high-level type.
416 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
417 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
418 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
419 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
420 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
425 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
426 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
427 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
428 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
429 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
430 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
432 abort(); /* we should never see any other types */
434 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
441 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
443 zfs_cmd_t zc
= {"\0"};
445 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
451 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
452 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
456 if (get_stats_ioctl(zhp
, &zc
) == -1) {
457 zcmd_free_nvlists(&zc
);
461 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
465 zcmd_free_nvlists(&zc
);
470 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
472 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
478 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
479 if (make_dataset_handle_common(zhp
, zc
) == -1) {
487 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
489 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
494 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
495 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
496 zhp
->zfs_head_type
= pzhp
->zfs_type
;
497 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
498 zhp
->zpool_hdl
= zpool_handle(zhp
);
504 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
506 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
511 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
512 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
513 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
514 sizeof (zhp
->zfs_name
));
515 zhp
->zfs_type
= zhp_orig
->zfs_type
;
516 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
517 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
518 if (zhp_orig
->zfs_props
!= NULL
) {
519 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
520 (void) no_memory(zhp
->zfs_hdl
);
525 if (zhp_orig
->zfs_user_props
!= NULL
) {
526 if (nvlist_dup(zhp_orig
->zfs_user_props
,
527 &zhp
->zfs_user_props
, 0) != 0) {
528 (void) no_memory(zhp
->zfs_hdl
);
533 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
534 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
535 &zhp
->zfs_recvd_props
, 0)) {
536 (void) no_memory(zhp
->zfs_hdl
);
541 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
542 if (zhp_orig
->zfs_mntopts
!= NULL
) {
543 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
544 zhp_orig
->zfs_mntopts
);
546 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
551 zfs_bookmark_exists(const char *path
)
555 char fsname
[ZFS_MAXNAMELEN
];
562 (void) strlcpy(fsname
, path
, sizeof (fsname
));
563 pound
= strchr(fsname
, '#');
568 bmark_name
= pound
+ 1;
569 props
= fnvlist_alloc();
570 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
577 rv
= nvlist_exists(bmarks
, bmark_name
);
583 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
584 nvlist_t
*bmark_props
)
586 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
591 /* Fill in the name. */
592 zhp
->zfs_hdl
= parent
->zfs_hdl
;
593 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
595 /* Set the property lists. */
596 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
602 zhp
->zfs_head_type
= parent
->zfs_head_type
;
603 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
605 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
606 nvlist_free(zhp
->zfs_props
);
615 * Opens the given snapshot, filesystem, or volume. The 'types'
616 * argument is a mask of acceptable types. The function will print an
617 * appropriate error message and return NULL if it can't be opened.
620 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
625 (void) snprintf(errbuf
, sizeof (errbuf
),
626 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
629 * Validate the name before we even try to open it.
631 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
633 "invalid dataset name"));
634 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
639 * Try to get stats for the dataset, which will tell us if it exists.
642 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
643 (void) zfs_standard_error(hdl
, errno
, errbuf
);
647 if (!(types
& zhp
->zfs_type
)) {
648 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
657 * Release a ZFS handle. Nothing to do but free the associated memory.
660 zfs_close(zfs_handle_t
*zhp
)
662 if (zhp
->zfs_mntopts
)
663 free(zhp
->zfs_mntopts
);
664 nvlist_free(zhp
->zfs_props
);
665 nvlist_free(zhp
->zfs_user_props
);
666 nvlist_free(zhp
->zfs_recvd_props
);
670 typedef struct mnttab_node
{
671 struct mnttab mtn_mt
;
676 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
678 const mnttab_node_t
*mtn1
= arg1
;
679 const mnttab_node_t
*mtn2
= arg2
;
682 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
686 return (rv
> 0 ? 1 : -1);
690 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
692 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
693 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
694 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
698 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
702 /* Reopen MNTTAB to prevent reading stale data from open file */
703 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
706 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
710 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
713 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
714 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
715 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
716 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
717 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
719 /* Exclude duplicate mounts */
720 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
721 free(mtn
->mtn_mt
.mnt_special
);
722 free(mtn
->mtn_mt
.mnt_mountp
);
723 free(mtn
->mtn_mt
.mnt_fstype
);
724 free(mtn
->mtn_mt
.mnt_mntopts
);
729 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
736 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
741 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
742 free(mtn
->mtn_mt
.mnt_special
);
743 free(mtn
->mtn_mt
.mnt_mountp
);
744 free(mtn
->mtn_mt
.mnt_fstype
);
745 free(mtn
->mtn_mt
.mnt_mntopts
);
748 avl_destroy(&hdl
->libzfs_mnttab_cache
);
752 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
754 hdl
->libzfs_mnttab_enable
= enable
;
758 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
759 struct mnttab
*entry
)
765 if (!hdl
->libzfs_mnttab_enable
) {
766 struct mnttab srch
= { 0 };
768 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
769 libzfs_mnttab_fini(hdl
);
771 /* Reopen MNTTAB to prevent reading stale data from open file */
772 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
775 srch
.mnt_special
= (char *)fsname
;
776 srch
.mnt_fstype
= MNTTYPE_ZFS
;
777 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
783 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
784 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
787 find
.mtn_mt
.mnt_special
= (char *)fsname
;
788 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
790 *entry
= mtn
->mtn_mt
;
797 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
798 const char *mountp
, const char *mntopts
)
802 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
804 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
805 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
806 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
807 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
808 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
809 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
813 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
818 find
.mtn_mt
.mnt_special
= (char *)fsname
;
819 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
820 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
821 free(ret
->mtn_mt
.mnt_special
);
822 free(ret
->mtn_mt
.mnt_mountp
);
823 free(ret
->mtn_mt
.mnt_fstype
);
824 free(ret
->mtn_mt
.mnt_mntopts
);
830 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
832 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
834 if (zpool_handle
== NULL
)
837 *spa_version
= zpool_get_prop_int(zpool_handle
,
838 ZPOOL_PROP_VERSION
, NULL
);
843 * The choice of reservation property depends on the SPA version.
846 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
850 if (zfs_spa_version(zhp
, &spa_version
) < 0)
853 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
854 *resv_prop
= ZFS_PROP_REFRESERVATION
;
856 *resv_prop
= ZFS_PROP_RESERVATION
;
862 * Given an nvlist of properties to set, validates that they are correct, and
863 * parses any numeric properties (index, boolean, etc) if they are specified as
867 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
868 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
876 int chosen_normal
= -1;
879 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
880 (void) no_memory(hdl
);
885 * Make sure this property is valid and applies to this type.
889 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
890 const char *propname
= nvpair_name(elem
);
892 prop
= zfs_name_to_prop(propname
);
893 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
895 * This is a user property: make sure it's a
896 * string, and that it's less than ZAP_MAXNAMELEN.
898 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
899 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
900 "'%s' must be a string"), propname
);
901 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
905 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
906 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
907 "property name '%s' is too long"),
909 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
913 (void) nvpair_value_string(elem
, &strval
);
914 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
915 (void) no_memory(hdl
);
922 * Currently, only user properties can be modified on
925 if (type
== ZFS_TYPE_SNAPSHOT
) {
926 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
927 "this property can not be modified for snapshots"));
928 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
932 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
933 zfs_userquota_prop_t uqtype
;
934 char newpropname
[128];
939 if (userquota_propname_decode(propname
, zoned
,
940 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
942 dgettext(TEXT_DOMAIN
,
943 "'%s' has an invalid user/group name"),
945 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
949 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
950 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
952 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
954 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
959 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
960 (void) nvpair_value_string(elem
, &strval
);
961 if (strcmp(strval
, "none") == 0) {
963 } else if (zfs_nicestrtonum(hdl
,
964 strval
, &intval
) != 0) {
965 (void) zfs_error(hdl
,
966 EZFS_BADPROP
, errbuf
);
969 } else if (nvpair_type(elem
) ==
971 (void) nvpair_value_uint64(elem
, &intval
);
973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
974 "use 'none' to disable "
975 "userquota/groupquota"));
979 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
980 "'%s' must be a number"), propname
);
981 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
986 * Encode the prop name as
987 * userquota@<hex-rid>-domain, to make it easy
988 * for the kernel to decode.
990 (void) snprintf(newpropname
, sizeof (newpropname
),
991 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
992 (longlong_t
)rid
, domain
);
996 if (nvlist_add_uint64_array(ret
, newpropname
,
998 (void) no_memory(hdl
);
1002 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1003 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1004 "'%s' is readonly"),
1006 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1010 if (prop
== ZPROP_INVAL
) {
1011 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1012 "invalid property '%s'"), propname
);
1013 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1017 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1019 dgettext(TEXT_DOMAIN
, "'%s' does not "
1020 "apply to datasets of this type"), propname
);
1021 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1025 if (zfs_prop_readonly(prop
) &&
1026 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
1028 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1030 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1034 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1035 &strval
, &intval
, errbuf
) != 0)
1039 * Perform some additional checks for specific properties.
1042 case ZFS_PROP_VERSION
:
1048 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1049 if (intval
< version
) {
1050 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1051 "Can not downgrade; already at version %u"),
1053 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1059 case ZFS_PROP_VOLBLOCKSIZE
:
1060 case ZFS_PROP_RECORDSIZE
:
1062 int maxbs
= SPA_MAXBLOCKSIZE
;
1065 if (zpool_hdl
!= NULL
) {
1066 maxbs
= zpool_get_prop_int(zpool_hdl
,
1067 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1070 * The value must be a power of two between
1071 * SPA_MINBLOCKSIZE and maxbs.
1073 if (intval
< SPA_MINBLOCKSIZE
||
1074 intval
> maxbs
|| !ISP2(intval
)) {
1075 zfs_nicenum(maxbs
, buf
, sizeof (buf
));
1076 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1077 "'%s' must be power of 2 from 512B "
1078 "to %s"), propname
, buf
);
1079 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1084 case ZFS_PROP_MLSLABEL
:
1086 #ifdef HAVE_MLSLABEL
1088 * Verify the mlslabel string and convert to
1089 * internal hex label string.
1093 char *hex
= NULL
; /* internal label string */
1095 /* Default value is already OK. */
1096 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1099 /* Verify the label can be converted to binary form */
1100 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1101 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1102 L_NO_CORRECTION
, NULL
) == -1)) {
1106 /* Now translate to hex internal label string */
1107 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1113 m_label_free(new_sl
);
1115 /* If string is already in internal form, we're done. */
1116 if (strcmp(strval
, hex
) == 0) {
1121 /* Replace the label string with the internal form. */
1122 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1124 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1131 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1132 "invalid mlslabel '%s'"), strval
);
1133 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1134 m_label_free(new_sl
); /* OK if null */
1137 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1138 "mlslabels are unsupported"));
1139 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1141 #endif /* HAVE_MLSLABEL */
1144 case ZFS_PROP_MOUNTPOINT
:
1146 namecheck_err_t why
;
1148 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1149 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1152 if (mountpoint_namecheck(strval
, &why
)) {
1154 case NAME_ERR_LEADING_SLASH
:
1156 dgettext(TEXT_DOMAIN
,
1157 "'%s' must be an absolute path, "
1158 "'none', or 'legacy'"), propname
);
1160 case NAME_ERR_TOOLONG
:
1162 dgettext(TEXT_DOMAIN
,
1163 "component of '%s' is too long"),
1169 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1176 case ZFS_PROP_SHARESMB
:
1177 case ZFS_PROP_SHARENFS
:
1179 * For the mountpoint and sharenfs or sharesmb
1180 * properties, check if it can be set in a
1181 * global/non-global zone based on
1182 * the zoned property value:
1184 * global zone non-global zone
1185 * --------------------------------------------------
1186 * zoned=on mountpoint (no) mountpoint (yes)
1187 * sharenfs (no) sharenfs (no)
1188 * sharesmb (no) sharesmb (no)
1190 * zoned=off mountpoint (yes) N/A
1195 if (getzoneid() == GLOBAL_ZONEID
) {
1196 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1197 "'%s' cannot be set on "
1198 "dataset in a non-global zone"),
1200 (void) zfs_error(hdl
, EZFS_ZONED
,
1203 } else if (prop
== ZFS_PROP_SHARENFS
||
1204 prop
== ZFS_PROP_SHARESMB
) {
1205 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1206 "'%s' cannot be set in "
1207 "a non-global zone"), propname
);
1208 (void) zfs_error(hdl
, EZFS_ZONED
,
1212 } else if (getzoneid() != GLOBAL_ZONEID
) {
1214 * If zoned property is 'off', this must be in
1215 * a global zone. If not, something is wrong.
1217 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1218 "'%s' cannot be set while dataset "
1219 "'zoned' property is set"), propname
);
1220 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1225 * At this point, it is legitimate to set the
1226 * property. Now we want to make sure that the
1227 * property value is valid if it is sharenfs.
1229 if ((prop
== ZFS_PROP_SHARENFS
||
1230 prop
== ZFS_PROP_SHARESMB
) &&
1231 strcmp(strval
, "on") != 0 &&
1232 strcmp(strval
, "off") != 0) {
1233 zfs_share_proto_t proto
;
1235 if (prop
== ZFS_PROP_SHARESMB
)
1241 * Must be an valid sharing protocol
1242 * option string so init the libshare
1243 * in order to enable the parser and
1244 * then parse the options. We use the
1245 * control API since we don't care about
1246 * the current configuration and don't
1247 * want the overhead of loading it
1248 * until we actually do something.
1251 if (zfs_init_libshare(hdl
,
1252 SA_INIT_CONTROL_API
) != SA_OK
) {
1254 * An error occurred so we can't do
1257 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1258 "'%s' cannot be set: problem "
1259 "in share initialization"),
1261 (void) zfs_error(hdl
, EZFS_BADPROP
,
1266 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1268 * There was an error in parsing so
1269 * deal with it by issuing an error
1270 * message and leaving after
1271 * uninitializing the the libshare
1274 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1275 "'%s' cannot be set to invalid "
1276 "options"), propname
);
1277 (void) zfs_error(hdl
, EZFS_BADPROP
,
1279 zfs_uninit_libshare(hdl
);
1282 zfs_uninit_libshare(hdl
);
1286 case ZFS_PROP_UTF8ONLY
:
1287 chosen_utf
= (int)intval
;
1289 case ZFS_PROP_NORMALIZE
:
1290 chosen_normal
= (int)intval
;
1297 * For changes to existing volumes, we have some additional
1298 * checks to enforce.
1300 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1301 uint64_t volsize
= zfs_prop_get_int(zhp
,
1303 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1304 ZFS_PROP_VOLBLOCKSIZE
);
1308 case ZFS_PROP_RESERVATION
:
1309 case ZFS_PROP_REFRESERVATION
:
1310 if (intval
> volsize
) {
1311 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1312 "'%s' is greater than current "
1313 "volume size"), propname
);
1314 (void) zfs_error(hdl
, EZFS_BADPROP
,
1320 case ZFS_PROP_VOLSIZE
:
1321 if (intval
% blocksize
!= 0) {
1322 zfs_nicenum(blocksize
, buf
,
1324 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1325 "'%s' must be a multiple of "
1326 "volume block size (%s)"),
1328 (void) zfs_error(hdl
, EZFS_BADPROP
,
1334 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1335 "'%s' cannot be zero"),
1337 (void) zfs_error(hdl
, EZFS_BADPROP
,
1349 * If normalization was chosen, but no UTF8 choice was made,
1350 * enforce rejection of non-UTF8 names.
1352 * If normalization was chosen, but rejecting non-UTF8 names
1353 * was explicitly not chosen, it is an error.
1355 if (chosen_normal
> 0 && chosen_utf
< 0) {
1356 if (nvlist_add_uint64(ret
,
1357 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1358 (void) no_memory(hdl
);
1361 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1362 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1363 "'%s' must be set 'on' if normalization chosen"),
1364 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1365 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1376 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1378 uint64_t old_volsize
;
1379 uint64_t new_volsize
;
1380 uint64_t old_reservation
;
1381 uint64_t new_reservation
;
1382 zfs_prop_t resv_prop
;
1386 * If this is an existing volume, and someone is setting the volsize,
1387 * make sure that it matches the reservation, or add it if necessary.
1389 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1390 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1392 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1394 props
= fnvlist_alloc();
1395 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1396 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1398 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1399 old_reservation
) || nvlist_exists(nvl
,
1400 zfs_prop_to_name(resv_prop
))) {
1401 fnvlist_free(props
);
1404 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1405 &new_volsize
) != 0) {
1406 fnvlist_free(props
);
1409 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1410 fnvlist_free(props
);
1412 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1413 new_reservation
) != 0) {
1414 (void) no_memory(zhp
->zfs_hdl
);
1421 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1428 * For quotas and reservations, ENOSPC indicates
1429 * something different; setting a quota or reservation
1430 * doesn't use any disk space.
1433 case ZFS_PROP_QUOTA
:
1434 case ZFS_PROP_REFQUOTA
:
1435 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1436 "size is less than current used or "
1438 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1441 case ZFS_PROP_RESERVATION
:
1442 case ZFS_PROP_REFRESERVATION
:
1443 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1444 "size is greater than available space"));
1445 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1449 (void) zfs_standard_error(hdl
, err
, errbuf
);
1455 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1459 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1463 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1464 "property value too long"));
1465 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1469 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1470 "pool and or dataset must be upgraded to set this "
1471 "property or value"));
1472 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1476 if (prop
== ZFS_PROP_COMPRESSION
||
1477 prop
== ZFS_PROP_RECORDSIZE
) {
1478 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1479 "property setting is not allowed on "
1480 "bootable datasets"));
1481 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1483 (void) zfs_standard_error(hdl
, err
, errbuf
);
1488 if (prop
== ZPROP_INVAL
) {
1489 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1491 (void) zfs_standard_error(hdl
, err
, errbuf
);
1497 * This platform can't address a volume this big.
1500 if (prop
== ZFS_PROP_VOLSIZE
) {
1501 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1507 (void) zfs_standard_error(hdl
, err
, errbuf
);
1512 zfs_is_namespace_prop(zfs_prop_t prop
)
1516 case ZFS_PROP_ATIME
:
1517 case ZFS_PROP_RELATIME
:
1518 case ZFS_PROP_DEVICES
:
1520 case ZFS_PROP_SETUID
:
1521 case ZFS_PROP_READONLY
:
1522 case ZFS_PROP_XATTR
:
1523 case ZFS_PROP_NBMAND
:
1532 * Given a property name and value, set the property for the given dataset.
1535 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1539 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1540 nvlist_t
*nvl
= NULL
;
1542 (void) snprintf(errbuf
, sizeof (errbuf
),
1543 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1546 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1547 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1548 (void) no_memory(hdl
);
1552 ret
= zfs_prop_set_list(zhp
, nvl
);
1562 * Given an nvlist of property names and values, set the properties for the
1566 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1568 zfs_cmd_t zc
= {"\0"};
1570 prop_changelist_t
**cls
= NULL
;
1573 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1577 zfs_prop_t prop
= 0;
1580 (void) snprintf(errbuf
, sizeof (errbuf
),
1581 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1584 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1585 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1590 * We have to check for any extra properties which need to be added
1591 * before computing the length of the nvlist.
1593 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1595 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1596 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1597 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1602 * Check how many properties we're setting and allocate an array to
1603 * store changelist pointers for postfix().
1606 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1608 elem
= nvlist_next_nvpair(nvl
, elem
))
1610 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1614 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1616 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1618 prop
= zfs_name_to_prop(nvpair_name(elem
));
1620 assert(cl_idx
< nvl_len
);
1622 * We don't want to unmount & remount the dataset when changing
1623 * its canmount property to 'on' or 'noauto'. We only use
1624 * the changelist logic to unmount when setting canmount=off.
1626 if (!(prop
== ZFS_PROP_CANMOUNT
&&
1627 fnvpair_value_uint64(elem
) != ZFS_CANMOUNT_OFF
)) {
1628 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1629 if (cls
[cl_idx
] == NULL
)
1633 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1634 changelist_haszonedchild(cls
[cl_idx
])) {
1635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1636 "child dataset with inherited mountpoint is used "
1637 "in a non-global zone"));
1638 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1642 if (cls
[cl_idx
] != NULL
&&
1643 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1648 assert(cl_idx
== nvl_len
);
1651 * Execute the corresponding ioctl() to set this list of properties.
1653 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1655 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1656 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1659 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1662 /* Get the list of unset properties back and report them. */
1663 nvlist_t
*errorprops
= NULL
;
1664 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1666 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1668 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1669 prop
= zfs_name_to_prop(nvpair_name(elem
));
1670 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1672 nvlist_free(errorprops
);
1674 if (added_resv
&& errno
== ENOSPC
) {
1675 /* clean up the volsize property we tried to set */
1676 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1680 zcmd_free_nvlists(&zc
);
1682 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1684 if (nvlist_add_uint64(nvl
,
1685 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1688 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1690 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1693 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1694 if (cls
[cl_idx
] != NULL
) {
1695 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1703 * Refresh the statistics so the new property
1704 * value is reflected.
1706 (void) get_stats(zhp
);
1709 * Remount the filesystem to propagate the change
1710 * if one of the options handled by the generic
1711 * Linux namespace layer has been modified.
1713 if (zfs_is_namespace_prop(prop
) &&
1714 zfs_is_mounted(zhp
, NULL
))
1715 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1721 zcmd_free_nvlists(&zc
);
1723 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1724 if (cls
[cl_idx
] != NULL
)
1725 changelist_free(cls
[cl_idx
]);
1733 * Given a property, inherit the value from the parent dataset, or if received
1734 * is TRUE, revert to the received value, if any.
1737 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1739 zfs_cmd_t zc
= {"\0"};
1741 prop_changelist_t
*cl
;
1742 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1746 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1747 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1749 zc
.zc_cookie
= received
;
1750 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1752 * For user properties, the amount of work we have to do is very
1753 * small, so just do it here.
1755 if (!zfs_prop_user(propname
)) {
1756 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1757 "invalid property"));
1758 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1761 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1762 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1764 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1765 return (zfs_standard_error(hdl
, errno
, errbuf
));
1771 * Verify that this property is inheritable.
1773 if (zfs_prop_readonly(prop
))
1774 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1776 if (!zfs_prop_inheritable(prop
) && !received
)
1777 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1780 * Check to see if the value applies to this type
1782 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1783 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1786 * Normalize the name, to get rid of shorthand abbreviations.
1788 propname
= zfs_prop_to_name(prop
);
1789 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1790 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1792 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1793 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1794 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1795 "dataset is used in a non-global zone"));
1796 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1800 * Determine datasets which will be affected by this change, if any.
1802 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1805 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1806 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1807 "child dataset with inherited mountpoint is used "
1808 "in a non-global zone"));
1809 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1813 if ((ret
= changelist_prefix(cl
)) != 0)
1816 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1817 return (zfs_standard_error(hdl
, errno
, errbuf
));
1820 if ((ret
= changelist_postfix(cl
)) != 0)
1824 * Refresh the statistics so the new property is reflected.
1826 (void) get_stats(zhp
);
1829 * Remount the filesystem to propagate the change
1830 * if one of the options handled by the generic
1831 * Linux namespace layer has been modified.
1833 if (zfs_is_namespace_prop(prop
) &&
1834 zfs_is_mounted(zhp
, NULL
))
1835 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1839 changelist_free(cl
);
1844 * True DSL properties are stored in an nvlist. The following two functions
1845 * extract them appropriately.
1848 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1854 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1855 zfs_prop_to_name(prop
), &nv
) == 0) {
1856 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1857 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1859 verify(!zhp
->zfs_props_table
||
1860 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1861 value
= zfs_prop_default_numeric(prop
);
1869 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1875 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1876 zfs_prop_to_name(prop
), &nv
) == 0) {
1877 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1878 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1880 verify(!zhp
->zfs_props_table
||
1881 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1882 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1891 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1893 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1897 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1899 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1900 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1904 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1906 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1911 * Internal function for getting a numeric property. Both zfs_prop_get() and
1912 * zfs_prop_get_int() are built using this interface.
1914 * Certain properties can be overridden using 'mount -o'. In this case, scan
1915 * the contents of the /etc/mtab entry, searching for the appropriate options.
1916 * If they differ from the on-disk values, report the current values and mark
1917 * the source "temporary".
1920 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1921 char **source
, uint64_t *val
)
1923 zfs_cmd_t zc
= {"\0"};
1924 nvlist_t
*zplprops
= NULL
;
1926 char *mntopt_on
= NULL
;
1927 char *mntopt_off
= NULL
;
1928 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1933 * If the property is being fetched for a snapshot, check whether
1934 * the property is valid for the snapshot's head dataset type.
1936 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
1937 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
1938 *val
= zfs_prop_default_numeric(prop
);
1943 case ZFS_PROP_ATIME
:
1944 mntopt_on
= MNTOPT_ATIME
;
1945 mntopt_off
= MNTOPT_NOATIME
;
1948 case ZFS_PROP_RELATIME
:
1949 mntopt_on
= MNTOPT_RELATIME
;
1950 mntopt_off
= MNTOPT_NORELATIME
;
1953 case ZFS_PROP_DEVICES
:
1954 mntopt_on
= MNTOPT_DEVICES
;
1955 mntopt_off
= MNTOPT_NODEVICES
;
1959 mntopt_on
= MNTOPT_EXEC
;
1960 mntopt_off
= MNTOPT_NOEXEC
;
1963 case ZFS_PROP_READONLY
:
1964 mntopt_on
= MNTOPT_RO
;
1965 mntopt_off
= MNTOPT_RW
;
1968 case ZFS_PROP_SETUID
:
1969 mntopt_on
= MNTOPT_SETUID
;
1970 mntopt_off
= MNTOPT_NOSETUID
;
1973 case ZFS_PROP_XATTR
:
1974 mntopt_on
= MNTOPT_XATTR
;
1975 mntopt_off
= MNTOPT_NOXATTR
;
1978 case ZFS_PROP_NBMAND
:
1979 mntopt_on
= MNTOPT_NBMAND
;
1980 mntopt_off
= MNTOPT_NONBMAND
;
1987 * Because looking up the mount options is potentially expensive
1988 * (iterating over all of /etc/mtab), we defer its calculation until
1989 * we're looking up a property which requires its presence.
1991 if (!zhp
->zfs_mntcheck
&&
1992 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1993 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1994 struct mnttab entry
;
1996 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1997 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1999 if (zhp
->zfs_mntopts
== NULL
)
2003 zhp
->zfs_mntcheck
= B_TRUE
;
2006 if (zhp
->zfs_mntopts
== NULL
)
2007 mnt
.mnt_mntopts
= "";
2009 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2012 case ZFS_PROP_ATIME
:
2013 case ZFS_PROP_RELATIME
:
2014 case ZFS_PROP_DEVICES
:
2016 case ZFS_PROP_READONLY
:
2017 case ZFS_PROP_SETUID
:
2018 case ZFS_PROP_XATTR
:
2019 case ZFS_PROP_NBMAND
:
2020 *val
= getprop_uint64(zhp
, prop
, source
);
2025 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2028 *src
= ZPROP_SRC_TEMPORARY
;
2029 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2032 *src
= ZPROP_SRC_TEMPORARY
;
2036 case ZFS_PROP_CANMOUNT
:
2037 case ZFS_PROP_VOLSIZE
:
2038 case ZFS_PROP_QUOTA
:
2039 case ZFS_PROP_REFQUOTA
:
2040 case ZFS_PROP_RESERVATION
:
2041 case ZFS_PROP_REFRESERVATION
:
2042 case ZFS_PROP_FILESYSTEM_LIMIT
:
2043 case ZFS_PROP_SNAPSHOT_LIMIT
:
2044 case ZFS_PROP_FILESYSTEM_COUNT
:
2045 case ZFS_PROP_SNAPSHOT_COUNT
:
2046 *val
= getprop_uint64(zhp
, prop
, source
);
2048 if (*source
== NULL
) {
2049 /* not default, must be local */
2050 *source
= zhp
->zfs_name
;
2054 case ZFS_PROP_MOUNTED
:
2055 *val
= (zhp
->zfs_mntopts
!= NULL
);
2058 case ZFS_PROP_NUMCLONES
:
2059 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2062 case ZFS_PROP_VERSION
:
2063 case ZFS_PROP_NORMALIZE
:
2064 case ZFS_PROP_UTF8ONLY
:
2066 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2068 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2069 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2070 zcmd_free_nvlists(&zc
);
2071 if (prop
== ZFS_PROP_VERSION
&&
2072 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2073 *val
= zfs_prop_default_numeric(prop
);
2076 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2077 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2079 zcmd_free_nvlists(&zc
);
2082 nvlist_free(zplprops
);
2083 zcmd_free_nvlists(&zc
);
2086 case ZFS_PROP_INCONSISTENT
:
2087 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2091 switch (zfs_prop_get_type(prop
)) {
2092 case PROP_TYPE_NUMBER
:
2093 case PROP_TYPE_INDEX
:
2094 *val
= getprop_uint64(zhp
, prop
, source
);
2096 * If we tried to use a default value for a
2097 * readonly property, it means that it was not
2100 if (zfs_prop_readonly(prop
) &&
2101 *source
!= NULL
&& (*source
)[0] == '\0') {
2106 case PROP_TYPE_STRING
:
2108 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2109 "cannot get non-numeric property"));
2110 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2111 dgettext(TEXT_DOMAIN
, "internal error")));
2119 * Calculate the source type, given the raw source string.
2122 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2123 char *statbuf
, size_t statlen
)
2125 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2128 if (source
== NULL
) {
2129 *srctype
= ZPROP_SRC_NONE
;
2130 } else if (source
[0] == '\0') {
2131 *srctype
= ZPROP_SRC_DEFAULT
;
2132 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2133 *srctype
= ZPROP_SRC_RECEIVED
;
2135 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2136 *srctype
= ZPROP_SRC_LOCAL
;
2138 (void) strlcpy(statbuf
, source
, statlen
);
2139 *srctype
= ZPROP_SRC_INHERITED
;
2146 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2147 size_t proplen
, boolean_t literal
)
2152 if (zhp
->zfs_recvd_props
== NULL
)
2153 if (get_recvd_props_ioctl(zhp
) != 0)
2156 prop
= zfs_name_to_prop(propname
);
2158 if (prop
!= ZPROP_INVAL
) {
2160 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2162 zfs_set_recvd_props_mode(zhp
, &cookie
);
2163 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2164 NULL
, NULL
, 0, literal
);
2165 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2169 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2170 propname
, &propval
) != 0)
2172 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2174 (void) strlcpy(propbuf
, recvdval
, proplen
);
2177 return (err
== 0 ? 0 : -1);
2181 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2186 value
= zfs_get_clones_nvl(zhp
);
2191 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2192 pair
= nvlist_next_nvpair(value
, pair
)) {
2193 if (propbuf
[0] != '\0')
2194 (void) strlcat(propbuf
, ",", proplen
);
2195 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2201 struct get_clones_arg
{
2205 char buf
[ZFS_MAXNAMELEN
];
2209 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2211 struct get_clones_arg
*gca
= arg
;
2213 if (gca
->numclones
== 0) {
2218 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2219 NULL
, NULL
, 0, B_TRUE
) != 0)
2221 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2222 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2227 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2233 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2235 nvlist_t
*nv
, *value
;
2237 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2238 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2239 struct get_clones_arg gca
;
2242 * if this is a snapshot, then the kernel wasn't able
2243 * to get the clones. Do it by slowly iterating.
2245 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2247 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2249 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2254 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2256 gca
.origin
= zhp
->zfs_name
;
2258 if (gca
.numclones
!= 0) {
2260 char pool
[ZFS_MAXNAMELEN
];
2263 /* get the pool name */
2264 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2265 (void) strsep(&cp
, "/@");
2266 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2267 ZFS_TYPE_FILESYSTEM
);
2269 (void) get_clones_cb(root
, &gca
);
2272 if (gca
.numclones
!= 0 ||
2273 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2274 nvlist_add_nvlist(zhp
->zfs_props
,
2275 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2282 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2283 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2286 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2292 * Retrieve a property from the given object. If 'literal' is specified, then
2293 * numbers are left as exact values. Otherwise, numbers are converted to a
2294 * human-readable form.
2296 * Returns 0 on success, or -1 on error.
2299 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2300 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2302 char *source
= NULL
;
2306 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2309 * Check to see if this property applies to our object
2311 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2314 if (received
&& zfs_prop_readonly(prop
))
2318 *src
= ZPROP_SRC_NONE
;
2321 case ZFS_PROP_CREATION
:
2323 * 'creation' is a time_t stored in the statistics. We convert
2324 * this into a string unless 'literal' is specified.
2327 val
= getprop_uint64(zhp
, prop
, &source
);
2328 time_t time
= (time_t)val
;
2332 localtime_r(&time
, &t
) == NULL
||
2333 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2335 (void) snprintf(propbuf
, proplen
, "%llu",
2336 (u_longlong_t
) val
);
2340 case ZFS_PROP_MOUNTPOINT
:
2342 * Getting the precise mountpoint can be tricky.
2344 * - for 'none' or 'legacy', return those values.
2345 * - for inherited mountpoints, we want to take everything
2346 * after our ancestor and append it to the inherited value.
2348 * If the pool has an alternate root, we want to prepend that
2349 * root to any values we return.
2352 str
= getprop_string(zhp
, prop
, &source
);
2354 if (str
[0] == '/') {
2355 char buf
[MAXPATHLEN
];
2357 const char *relpath
;
2360 * If we inherit the mountpoint, even from a dataset
2361 * with a received value, the source will be the path of
2362 * the dataset we inherit from. If source is
2363 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2366 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2369 relpath
= zhp
->zfs_name
+ strlen(source
);
2370 if (relpath
[0] == '/')
2374 if ((zpool_get_prop(zhp
->zpool_hdl
,
2375 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
2376 (strcmp(root
, "-") == 0))
2379 * Special case an alternate root of '/'. This will
2380 * avoid having multiple leading slashes in the
2383 if (strcmp(root
, "/") == 0)
2387 * If the mountpoint is '/' then skip over this
2388 * if we are obtaining either an alternate root or
2389 * an inherited mountpoint.
2391 if (str
[1] == '\0' && (root
[0] != '\0' ||
2392 relpath
[0] != '\0'))
2395 if (relpath
[0] == '\0')
2396 (void) snprintf(propbuf
, proplen
, "%s%s",
2399 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2400 root
, str
, relpath
[0] == '@' ? "" : "/",
2403 /* 'legacy' or 'none' */
2404 (void) strlcpy(propbuf
, str
, proplen
);
2409 case ZFS_PROP_ORIGIN
:
2410 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2413 * If there is no parent at all, return failure to indicate that
2414 * it doesn't apply to this dataset.
2416 if (propbuf
[0] == '\0')
2420 case ZFS_PROP_CLONES
:
2421 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2425 case ZFS_PROP_QUOTA
:
2426 case ZFS_PROP_REFQUOTA
:
2427 case ZFS_PROP_RESERVATION
:
2428 case ZFS_PROP_REFRESERVATION
:
2430 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2434 * If quota or reservation is 0, we translate this into 'none'
2435 * (unless literal is set), and indicate that it's the default
2436 * value. Otherwise, we print the number nicely and indicate
2437 * that its set locally.
2441 (void) strlcpy(propbuf
, "0", proplen
);
2443 (void) strlcpy(propbuf
, "none", proplen
);
2446 (void) snprintf(propbuf
, proplen
, "%llu",
2449 zfs_nicenum(val
, propbuf
, proplen
);
2453 case ZFS_PROP_FILESYSTEM_LIMIT
:
2454 case ZFS_PROP_SNAPSHOT_LIMIT
:
2455 case ZFS_PROP_FILESYSTEM_COUNT
:
2456 case ZFS_PROP_SNAPSHOT_COUNT
:
2458 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2462 * If limit is UINT64_MAX, we translate this into 'none' (unless
2463 * literal is set), and indicate that it's the default value.
2464 * Otherwise, we print the number nicely and indicate that it's
2468 (void) snprintf(propbuf
, proplen
, "%llu",
2470 } else if (val
== UINT64_MAX
) {
2471 (void) strlcpy(propbuf
, "none", proplen
);
2473 zfs_nicenum(val
, propbuf
, proplen
);
2477 case ZFS_PROP_REFRATIO
:
2478 case ZFS_PROP_COMPRESSRATIO
:
2479 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2481 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2482 (u_longlong_t
)(val
/ 100),
2483 (u_longlong_t
)(val
% 100));
2487 switch (zhp
->zfs_type
) {
2488 case ZFS_TYPE_FILESYSTEM
:
2491 case ZFS_TYPE_VOLUME
:
2494 case ZFS_TYPE_SNAPSHOT
:
2497 case ZFS_TYPE_BOOKMARK
:
2503 (void) snprintf(propbuf
, proplen
, "%s", str
);
2506 case ZFS_PROP_MOUNTED
:
2508 * The 'mounted' property is a pseudo-property that described
2509 * whether the filesystem is currently mounted. Even though
2510 * it's a boolean value, the typical values of "on" and "off"
2511 * don't make sense, so we translate to "yes" and "no".
2513 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2514 src
, &source
, &val
) != 0)
2517 (void) strlcpy(propbuf
, "yes", proplen
);
2519 (void) strlcpy(propbuf
, "no", proplen
);
2524 * The 'name' property is a pseudo-property derived from the
2525 * dataset name. It is presented as a real property to simplify
2528 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2531 case ZFS_PROP_MLSLABEL
:
2533 #ifdef HAVE_MLSLABEL
2534 m_label_t
*new_sl
= NULL
;
2535 char *ascii
= NULL
; /* human readable label */
2537 (void) strlcpy(propbuf
,
2538 getprop_string(zhp
, prop
, &source
), proplen
);
2540 if (literal
|| (strcasecmp(propbuf
,
2541 ZFS_MLSLABEL_DEFAULT
) == 0))
2545 * Try to translate the internal hex string to
2546 * human-readable output. If there are any
2547 * problems just use the hex string.
2550 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2551 L_NO_CORRECTION
, NULL
) == -1) {
2552 m_label_free(new_sl
);
2556 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2560 m_label_free(new_sl
);
2563 m_label_free(new_sl
);
2565 (void) strlcpy(propbuf
, ascii
, proplen
);
2568 (void) strlcpy(propbuf
,
2569 getprop_string(zhp
, prop
, &source
), proplen
);
2570 #endif /* HAVE_MLSLABEL */
2576 * GUIDs are stored as numbers, but they are identifiers.
2577 * We don't want them to be pretty printed, because pretty
2578 * printing mangles the ID into a truncated and useless value.
2580 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2582 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2586 switch (zfs_prop_get_type(prop
)) {
2587 case PROP_TYPE_NUMBER
:
2588 if (get_numeric_property(zhp
, prop
, src
,
2589 &source
, &val
) != 0)
2592 (void) snprintf(propbuf
, proplen
, "%llu",
2595 zfs_nicenum(val
, propbuf
, proplen
);
2598 case PROP_TYPE_STRING
:
2599 (void) strlcpy(propbuf
,
2600 getprop_string(zhp
, prop
, &source
), proplen
);
2603 case PROP_TYPE_INDEX
:
2604 if (get_numeric_property(zhp
, prop
, src
,
2605 &source
, &val
) != 0)
2607 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2609 (void) strlcpy(propbuf
, strval
, proplen
);
2617 get_source(zhp
, src
, source
, statbuf
, statlen
);
2623 * Utility function to get the given numeric property. Does no validation that
2624 * the given property is the appropriate type; should only be used with
2625 * hard-coded property types.
2628 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2633 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2639 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2643 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2644 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2648 * Similar to zfs_prop_get(), but returns the value as an integer.
2651 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2652 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2657 * Check to see if this property applies to our object
2659 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
2660 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2661 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2662 zfs_prop_to_name(prop
)));
2666 *src
= ZPROP_SRC_NONE
;
2668 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2671 get_source(zhp
, src
, source
, statbuf
, statlen
);
2678 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2679 char **domainp
, idmap_rid_t
*ridp
)
2681 idmap_get_handle_t
*get_hdl
= NULL
;
2685 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2689 err
= idmap_get_sidbyuid(get_hdl
, id
,
2690 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2692 err
= idmap_get_sidbygid(get_hdl
, id
,
2693 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2695 if (err
== IDMAP_SUCCESS
&&
2696 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2697 status
== IDMAP_SUCCESS
)
2703 idmap_get_destroy(get_hdl
);
2706 #endif /* HAVE_IDMAP */
2709 * convert the propname into parameters needed by kernel
2710 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2711 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2712 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2713 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2716 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2717 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2719 zfs_userquota_prop_t type
;
2728 /* Figure out the property type ({user|group}{quota|space}) */
2729 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2730 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2731 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2734 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2738 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
);
2739 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
);
2741 cp
= strchr(propname
, '@') + 1;
2743 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2744 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2747 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2748 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2751 } else if (strchr(cp
, '@')) {
2754 * It's a SID name (eg "user@domain") that needs to be
2755 * turned into S-1-domainID-RID.
2757 directory_error_t e
;
2758 char *numericsid
= NULL
;
2761 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2764 e
= directory_sid_from_user_name(NULL
,
2767 e
= directory_sid_from_group_name(NULL
,
2771 directory_error_free(e
);
2774 if (numericsid
== NULL
)
2777 (void) strlcpy(domain
, cp
, domainlen
);
2778 cp
= strrchr(domain
, '-');
2783 *ridp
= strtoull(cp
, &end
, 10);
2786 if (errno
!= 0 || *end
!= '\0')
2790 #endif /* HAVE_IDMAP */
2792 /* It's a user/group ID (eg "12345"). */
2795 id
= strtoul(cp
, &end
, 10);
2800 /* It's an ephemeral ID. */
2804 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2805 &mapdomain
, &rid
) != 0)
2807 (void) strlcpy(domain
, mapdomain
, domainlen
);
2811 #endif /* HAVE_IDMAP */
2821 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2822 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2825 zfs_cmd_t zc
= {"\0"};
2827 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2829 err
= userquota_propname_decode(propname
,
2830 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2831 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2832 zc
.zc_objset_type
= *typep
;
2836 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2840 *propvalue
= zc
.zc_cookie
;
2845 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2846 uint64_t *propvalue
)
2848 zfs_userquota_prop_t type
;
2850 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2855 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2856 char *propbuf
, int proplen
, boolean_t literal
)
2860 zfs_userquota_prop_t type
;
2862 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2869 (void) snprintf(propbuf
, proplen
, "%llu",
2870 (u_longlong_t
)propvalue
);
2871 } else if (propvalue
== 0 &&
2872 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2873 (void) strlcpy(propbuf
, "none", proplen
);
2875 zfs_nicenum(propvalue
, propbuf
, proplen
);
2881 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2882 uint64_t *propvalue
)
2885 zfs_cmd_t zc
= {"\0"};
2886 const char *snapname
;
2888 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2890 snapname
= strchr(propname
, '@') + 1;
2891 if (strchr(snapname
, '@')) {
2892 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2894 /* snapname is the short name, append it to zhp's fsname */
2897 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2898 sizeof (zc
.zc_value
));
2899 cp
= strchr(zc
.zc_value
, '@');
2902 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2903 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2906 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2910 *propvalue
= zc
.zc_cookie
;
2915 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2916 char *propbuf
, int proplen
, boolean_t literal
)
2921 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2927 (void) snprintf(propbuf
, proplen
, "%llu",
2928 (u_longlong_t
)propvalue
);
2930 zfs_nicenum(propvalue
, propbuf
, proplen
);
2937 * Returns the name of the given zfs handle.
2940 zfs_get_name(const zfs_handle_t
*zhp
)
2942 return (zhp
->zfs_name
);
2946 * Returns the type of the given zfs handle.
2949 zfs_get_type(const zfs_handle_t
*zhp
)
2951 return (zhp
->zfs_type
);
2955 * Is one dataset name a child dataset of another?
2957 * Needs to handle these cases:
2958 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2959 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2960 * Descendant? No. No. No. Yes.
2963 is_descendant(const char *ds1
, const char *ds2
)
2965 size_t d1len
= strlen(ds1
);
2967 /* ds2 can't be a descendant if it's smaller */
2968 if (strlen(ds2
) < d1len
)
2971 /* otherwise, compare strings and verify that there's a '/' char */
2972 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2976 * Given a complete name, return just the portion that refers to the parent.
2977 * Will return -1 if there is no parent (path is just the name of the
2981 parent_name(const char *path
, char *buf
, size_t buflen
)
2985 (void) strlcpy(buf
, path
, buflen
);
2987 if ((slashp
= strrchr(buf
, '/')) == NULL
)
2995 * If accept_ancestor is false, then check to make sure that the given path has
2996 * a parent, and that it exists. If accept_ancestor is true, then find the
2997 * closest existing ancestor for the given path. In prefixlen return the
2998 * length of already existing prefix of the given path. We also fetch the
2999 * 'zoned' property, which is used to validate property settings when creating
3003 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3004 boolean_t accept_ancestor
, int *prefixlen
)
3006 zfs_cmd_t zc
= {"\0"};
3007 char parent
[ZFS_MAXNAMELEN
];
3013 (void) snprintf(errbuf
, sizeof (errbuf
),
3014 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3016 /* get parent, and check to see if this is just a pool */
3017 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3018 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3019 "missing dataset name"));
3020 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3023 /* check to see if the pool exists */
3024 if ((slash
= strchr(parent
, '/')) == NULL
)
3025 slash
= parent
+ strlen(parent
);
3026 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3027 zc
.zc_name
[slash
- parent
] = '\0';
3028 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3030 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3031 "no such pool '%s'"), zc
.zc_name
);
3032 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3035 /* check to see if the parent dataset exists */
3036 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3037 if (errno
== ENOENT
&& accept_ancestor
) {
3039 * Go deeper to find an ancestor, give up on top level.
3041 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3043 "no such pool '%s'"), zc
.zc_name
);
3044 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3046 } else if (errno
== ENOENT
) {
3047 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3048 "parent does not exist"));
3049 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3051 return (zfs_standard_error(hdl
, errno
, errbuf
));
3054 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3058 /* we are in a non-global zone, but parent is in the global zone */
3059 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3060 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3065 /* make sure parent is a filesystem */
3066 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3067 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3068 "parent is not a filesystem"));
3069 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3075 if (prefixlen
!= NULL
)
3076 *prefixlen
= strlen(parent
);
3081 * Finds whether the dataset of the given type(s) exists.
3084 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3088 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3092 * Try to get stats for the dataset, which will tell us if it exists.
3094 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3095 int ds_type
= zhp
->zfs_type
;
3098 if (types
& ds_type
)
3105 * Given a path to 'target', create all the ancestors between
3106 * the prefixlen portion of the path, and the target itself.
3107 * Fail if the initial prefixlen-ancestor does not already exist.
3110 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3116 /* make sure prefix exists */
3117 cp
= target
+ prefixlen
;
3119 assert(strchr(cp
, '/') == NULL
);
3120 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3123 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3131 * Attempt to create, mount, and share any ancestor filesystems,
3132 * up to the prefixlen-long one.
3134 for (cp
= target
+ prefixlen
+ 1;
3135 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
3139 h
= make_dataset_handle(hdl
, target
);
3141 /* it already exists, nothing to do here */
3146 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3148 opname
= dgettext(TEXT_DOMAIN
, "create");
3152 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3154 opname
= dgettext(TEXT_DOMAIN
, "open");
3158 if (zfs_mount(h
, NULL
, 0) != 0) {
3159 opname
= dgettext(TEXT_DOMAIN
, "mount");
3163 if (zfs_share(h
) != 0) {
3164 opname
= dgettext(TEXT_DOMAIN
, "share");
3174 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3175 "failed to %s ancestor '%s'"), opname
, target
);
3180 * Creates non-existing ancestors of the given path.
3183 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3189 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3192 if ((path_copy
= strdup(path
)) != NULL
) {
3193 rc
= create_parents(hdl
, path_copy
, prefix
);
3196 if (path_copy
== NULL
|| rc
!= 0)
3203 * Create a new filesystem or volume.
3206 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3211 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3214 dmu_objset_type_t ost
;
3216 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3217 "cannot create '%s'"), path
);
3219 /* validate the path, taking care to note the extended error message */
3220 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3221 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3223 /* validate parents exist */
3224 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3228 * The failure modes when creating a dataset of a different type over
3229 * one that already exists is a little strange. In particular, if you
3230 * try to create a dataset on top of an existing dataset, the ioctl()
3231 * will return ENOENT, not EEXIST. To prevent this from happening, we
3232 * first try to see if the dataset exists.
3234 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3235 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3236 "dataset already exists"));
3237 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3240 if (type
== ZFS_TYPE_VOLUME
)
3245 /* open zpool handle for prop validation */
3246 char pool_path
[MAXNAMELEN
];
3247 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3249 /* truncate pool_path at first slash */
3250 char *p
= strchr(pool_path
, '/');
3254 zpool_handle_t
*zpool_handle
= zpool_open(hdl
, pool_path
);
3256 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3257 zoned
, NULL
, zpool_handle
, errbuf
)) == 0) {
3258 zpool_close(zpool_handle
);
3261 zpool_close(zpool_handle
);
3263 if (type
== ZFS_TYPE_VOLUME
) {
3265 * If we are creating a volume, the size and block size must
3266 * satisfy a few restraints. First, the blocksize must be a
3267 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3268 * volsize must be a multiple of the block size, and cannot be
3271 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3272 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3274 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3275 "missing volume size"));
3276 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3279 if ((ret
= nvlist_lookup_uint64(props
,
3280 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3281 &blocksize
)) != 0) {
3282 if (ret
== ENOENT
) {
3283 blocksize
= zfs_prop_default_numeric(
3284 ZFS_PROP_VOLBLOCKSIZE
);
3287 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3288 "missing volume block size"));
3289 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3295 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3296 "volume size cannot be zero"));
3297 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3300 if (size
% blocksize
!= 0) {
3302 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3303 "volume size must be a multiple of volume block "
3305 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3309 /* create the dataset */
3310 ret
= lzc_create(path
, ost
, props
);
3313 /* check for failure */
3315 char parent
[ZFS_MAXNAMELEN
];
3316 (void) parent_name(path
, parent
, sizeof (parent
));
3320 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3321 "no such parent '%s'"), parent
);
3322 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3325 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3326 "parent '%s' is not a filesystem"), parent
);
3327 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3330 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3331 "pool must be upgraded to set this "
3332 "property or value"));
3333 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3337 * This platform can't address a volume this big.
3339 if (type
== ZFS_TYPE_VOLUME
)
3340 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3345 return (zfs_standard_error(hdl
, errno
, errbuf
));
3353 * Destroys the given dataset. The caller must make sure that the filesystem
3354 * isn't mounted, and that there are no active dependents. If the file system
3355 * does not exist this function does nothing.
3358 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3360 zfs_cmd_t zc
= {"\0"};
3362 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3363 nvlist_t
*nv
= fnvlist_alloc();
3364 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3365 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3368 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3369 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3375 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3377 if (ZFS_IS_VOLUME(zhp
)) {
3378 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3380 zc
.zc_objset_type
= DMU_OST_ZFS
;
3383 zc
.zc_defer_destroy
= defer
;
3384 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3386 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3387 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3391 remove_mountpoint(zhp
);
3396 struct destroydata
{
3398 const char *snapname
;
3402 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3404 struct destroydata
*dd
= arg
;
3405 char name
[ZFS_MAXNAMELEN
];
3408 (void) snprintf(name
, sizeof (name
),
3409 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3411 if (lzc_exists(name
))
3412 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3414 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3420 * Destroys all snapshots with the given name in zhp & descendants.
3423 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3426 struct destroydata dd
= { 0 };
3428 dd
.snapname
= snapname
;
3429 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3430 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3432 if (nvlist_empty(dd
.nvl
)) {
3433 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3434 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3435 zhp
->zfs_name
, snapname
);
3437 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3439 nvlist_free(dd
.nvl
);
3444 * Destroys all the snapshots named in the nvlist.
3447 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3453 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3458 if (nvlist_empty(errlist
)) {
3460 (void) snprintf(errbuf
, sizeof (errbuf
),
3461 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3463 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3465 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3466 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3468 (void) snprintf(errbuf
, sizeof (errbuf
),
3469 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3472 switch (fnvpair_value_int32(pair
)) {
3475 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3476 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3479 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3488 * Clones the given dataset. The target must be of the same type as the source.
3491 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3493 char parent
[ZFS_MAXNAMELEN
];
3496 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3499 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3501 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3502 "cannot create '%s'"), target
);
3504 /* validate the target/clone name */
3505 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3506 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3508 /* validate parents exist */
3509 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3512 (void) parent_name(target
, parent
, sizeof (parent
));
3518 if (ZFS_IS_VOLUME(zhp
)) {
3519 type
= ZFS_TYPE_VOLUME
;
3521 type
= ZFS_TYPE_FILESYSTEM
;
3523 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3524 zhp
, zhp
->zpool_hdl
, errbuf
)) == NULL
)
3528 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3536 * The parent doesn't exist. We should have caught this
3537 * above, but there may a race condition that has since
3538 * destroyed the parent.
3540 * At this point, we don't know whether it's the source
3541 * that doesn't exist anymore, or whether the target
3542 * dataset doesn't exist.
3544 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3545 "no such parent '%s'"), parent
);
3546 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3549 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3550 "source and target pools differ"));
3551 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3555 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3564 * Promotes the given clone fs to be the clone parent.
3567 zfs_promote(zfs_handle_t
*zhp
)
3569 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3570 zfs_cmd_t zc
= {"\0"};
3571 char parent
[MAXPATHLEN
];
3575 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3576 "cannot promote '%s'"), zhp
->zfs_name
);
3578 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3579 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3580 "snapshots can not be promoted"));
3581 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3584 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3585 if (parent
[0] == '\0') {
3586 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3587 "not a cloned filesystem"));
3588 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3591 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3592 sizeof (zc
.zc_value
));
3593 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3594 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3597 int save_errno
= errno
;
3599 switch (save_errno
) {
3601 /* There is a conflicting snapshot name. */
3602 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3603 "conflicting snapshot '%s' from parent '%s'"),
3604 zc
.zc_string
, parent
);
3605 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3608 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3614 typedef struct snapdata
{
3616 const char *sd_snapname
;
3620 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3622 snapdata_t
*sd
= arg
;
3623 char name
[ZFS_MAXNAMELEN
];
3626 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3627 (void) snprintf(name
, sizeof (name
),
3628 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3630 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3632 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3640 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3644 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3651 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3652 "cannot create snapshots "));
3655 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3656 const char *snapname
= nvpair_name(elem
);
3658 /* validate the target name */
3659 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3661 (void) snprintf(errbuf
, sizeof (errbuf
),
3662 dgettext(TEXT_DOMAIN
,
3663 "cannot create snapshot '%s'"), snapname
);
3664 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3669 * get pool handle for prop validation. assumes all snaps are in the
3670 * same pool, as does lzc_snapshot (below).
3672 char pool
[MAXNAMELEN
];
3673 elem
= nvlist_next_nvpair(snaps
, NULL
);
3674 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
3675 pool
[strcspn(pool
, "/@")] = '\0';
3676 zpool_handle_t
*zpool_hdl
= zpool_open(hdl
, pool
);
3678 if (props
!= NULL
&&
3679 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3680 props
, B_FALSE
, NULL
, zpool_hdl
, errbuf
)) == NULL
) {
3681 zpool_close(zpool_hdl
);
3684 zpool_close(zpool_hdl
);
3686 ret
= lzc_snapshot(snaps
, props
, &errors
);
3689 boolean_t printed
= B_FALSE
;
3690 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3692 elem
= nvlist_next_nvpair(errors
, elem
)) {
3693 (void) snprintf(errbuf
, sizeof (errbuf
),
3694 dgettext(TEXT_DOMAIN
,
3695 "cannot create snapshot '%s'"), nvpair_name(elem
));
3696 (void) zfs_standard_error(hdl
,
3697 fnvpair_value_int32(elem
), errbuf
);
3703 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3704 "multiple snapshots of same "
3706 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3710 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3716 nvlist_free(errors
);
3721 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3725 snapdata_t sd
= { 0 };
3726 char fsname
[ZFS_MAXNAMELEN
];
3731 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3732 "cannot snapshot %s"), path
);
3734 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3735 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3737 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3738 cp
= strchr(fsname
, '@');
3740 sd
.sd_snapname
= cp
+ 1;
3742 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3743 ZFS_TYPE_VOLUME
)) == NULL
) {
3747 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3749 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3751 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3754 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3755 nvlist_free(sd
.sd_nvl
);
3761 * Destroy any more recent snapshots. We invoke this callback on any dependents
3762 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3763 * is a dependent and we should just destroy it without checking the transaction
3766 typedef struct rollback_data
{
3767 const char *cb_target
; /* the snapshot */
3768 uint64_t cb_create
; /* creation time reference */
3774 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3776 rollback_data_t
*cbp
= data
;
3777 prop_changelist_t
*clp
;
3779 /* We must destroy this clone; first unmount it */
3780 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3781 cbp
->cb_force
? MS_FORCE
: 0);
3782 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3783 cbp
->cb_error
= B_TRUE
;
3787 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3788 cbp
->cb_error
= B_TRUE
;
3790 changelist_remove(clp
, zhp
->zfs_name
);
3791 (void) changelist_postfix(clp
);
3792 changelist_free(clp
);
3799 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3801 rollback_data_t
*cbp
= data
;
3803 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3804 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3805 rollback_destroy_dependent
, cbp
);
3807 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3815 * Given a dataset, rollback to a specific snapshot, discarding any
3816 * data changes since then and making it the active dataset.
3818 * Any snapshots and bookmarks more recent than the target are
3819 * destroyed, along with their dependents (i.e. clones).
3822 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3824 rollback_data_t cb
= { 0 };
3826 boolean_t restore_resv
= 0;
3827 uint64_t old_volsize
= 0, new_volsize
;
3828 zfs_prop_t resv_prop
= { 0 };
3830 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3831 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3834 * Destroy all recent snapshots and their dependents.
3836 cb
.cb_force
= force
;
3837 cb
.cb_target
= snap
->zfs_name
;
3838 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3839 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3840 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3846 * Now that we have verified that the snapshot is the latest,
3847 * rollback to the given snapshot.
3850 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3851 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3853 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3855 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3859 * We rely on zfs_iter_children() to verify that there are no
3860 * newer snapshots for the given dataset. Therefore, we can
3861 * simply pass the name on to the ioctl() call. There is still
3862 * an unlikely race condition where the user has taken a
3863 * snapshot since we verified that this was the most recent.
3865 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3867 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3868 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3874 * For volumes, if the pre-rollback volsize matched the pre-
3875 * rollback reservation and the volsize has changed then set
3876 * the reservation property to the post-rollback volsize.
3877 * Make a new handle since the rollback closed the dataset.
3879 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3880 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3882 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3883 if (old_volsize
!= new_volsize
)
3884 err
= zfs_prop_set_int(zhp
, resv_prop
,
3893 * Renames the given dataset.
3896 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
3897 boolean_t force_unmount
)
3900 zfs_cmd_t zc
= {"\0"};
3902 prop_changelist_t
*cl
= NULL
;
3903 zfs_handle_t
*zhrp
= NULL
;
3904 char *parentname
= NULL
;
3905 char parent
[ZFS_MAXNAMELEN
];
3906 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3909 /* if we have the same exact name, just return success */
3910 if (strcmp(zhp
->zfs_name
, target
) == 0)
3913 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3914 "cannot rename to '%s'"), target
);
3917 * Make sure the target name is valid
3919 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3920 if ((strchr(target
, '@') == NULL
) ||
3923 * Snapshot target name is abbreviated,
3924 * reconstruct full dataset name
3926 (void) strlcpy(parent
, zhp
->zfs_name
,
3928 delim
= strchr(parent
, '@');
3929 if (strchr(target
, '@') == NULL
)
3933 (void) strlcat(parent
, target
, sizeof (parent
));
3937 * Make sure we're renaming within the same dataset.
3939 delim
= strchr(target
, '@');
3940 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3941 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3942 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3943 "snapshots must be part of same "
3945 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3949 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3950 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3953 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3954 "recursive rename must be a snapshot"));
3955 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3958 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3959 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3961 /* validate parents */
3962 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3965 /* make sure we're in the same pool */
3966 verify((delim
= strchr(target
, '/')) != NULL
);
3967 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3968 zhp
->zfs_name
[delim
- target
] != '/') {
3969 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3970 "datasets must be within same pool"));
3971 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3974 /* new name cannot be a child of the current dataset name */
3975 if (is_descendant(zhp
->zfs_name
, target
)) {
3976 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3977 "New dataset name cannot be a descendant of "
3978 "current dataset name"));
3979 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3983 (void) snprintf(errbuf
, sizeof (errbuf
),
3984 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3986 if (getzoneid() == GLOBAL_ZONEID
&&
3987 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3988 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3989 "dataset is used in a non-global zone"));
3990 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3994 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3995 if (parentname
== NULL
) {
3999 delim
= strchr(parentname
, '@');
4001 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4006 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4007 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4008 force_unmount
? MS_FORCE
: 0)) == NULL
)
4011 if (changelist_haszonedchild(cl
)) {
4012 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4013 "child dataset with inherited mountpoint is used "
4014 "in a non-global zone"));
4015 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4020 if ((ret
= changelist_prefix(cl
)) != 0)
4024 if (ZFS_IS_VOLUME(zhp
))
4025 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4027 zc
.zc_objset_type
= DMU_OST_ZFS
;
4029 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4030 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4032 zc
.zc_cookie
= recursive
;
4034 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4036 * if it was recursive, the one that actually failed will
4039 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4040 "cannot rename '%s'"), zc
.zc_name
);
4042 if (recursive
&& errno
== EEXIST
) {
4043 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4044 "a child dataset already has a snapshot "
4045 "with the new name"));
4046 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4048 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4052 * On failure, we still want to remount any filesystems that
4053 * were previously mounted, so we don't alter the system state.
4056 (void) changelist_postfix(cl
);
4059 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4060 ret
= changelist_postfix(cl
);
4065 if (parentname
!= NULL
) {
4072 changelist_free(cl
);
4078 zfs_get_user_props(zfs_handle_t
*zhp
)
4080 return (zhp
->zfs_user_props
);
4084 * This function is used by 'zfs list' to determine the exact set of columns to
4085 * display, and their maximum widths. This does two main things:
4087 * - If this is a list of all properties, then expand the list to include
4088 * all native properties, and set a flag so that for each dataset we look
4089 * for new unique user properties and add them to the list.
4091 * - For non fixed-width properties, keep track of the maximum width seen
4092 * so that we can size the column appropriately. If the user has
4093 * requested received property values, we also need to compute the width
4094 * of the RECEIVED column.
4097 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4100 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4101 zprop_list_t
*entry
;
4102 zprop_list_t
**last
, **start
;
4103 nvlist_t
*userprops
, *propval
;
4106 char buf
[ZFS_MAXPROPLEN
];
4108 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4111 userprops
= zfs_get_user_props(zhp
);
4114 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4116 * Go through and add any user properties as necessary. We
4117 * start by incrementing our list pointer to the first
4118 * non-native property.
4121 while (*start
!= NULL
) {
4122 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4124 start
= &(*start
)->pl_next
;
4128 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4130 * See if we've already found this property in our list.
4132 for (last
= start
; *last
!= NULL
;
4133 last
= &(*last
)->pl_next
) {
4134 if (strcmp((*last
)->pl_user_prop
,
4135 nvpair_name(elem
)) == 0)
4139 if (*last
== NULL
) {
4140 if ((entry
= zfs_alloc(hdl
,
4141 sizeof (zprop_list_t
))) == NULL
||
4142 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4143 nvpair_name(elem
)))) == NULL
) {
4148 entry
->pl_prop
= ZPROP_INVAL
;
4149 entry
->pl_width
= strlen(nvpair_name(elem
));
4150 entry
->pl_all
= B_TRUE
;
4157 * Now go through and check the width of any non-fixed columns
4159 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4160 if (entry
->pl_fixed
&& !literal
)
4163 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4164 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4165 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4166 if (strlen(buf
) > entry
->pl_width
)
4167 entry
->pl_width
= strlen(buf
);
4169 if (received
&& zfs_prop_get_recvd(zhp
,
4170 zfs_prop_to_name(entry
->pl_prop
),
4171 buf
, sizeof (buf
), literal
) == 0)
4172 if (strlen(buf
) > entry
->pl_recvd_width
)
4173 entry
->pl_recvd_width
= strlen(buf
);
4175 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4177 verify(nvlist_lookup_string(propval
,
4178 ZPROP_VALUE
, &strval
) == 0);
4179 if (strlen(strval
) > entry
->pl_width
)
4180 entry
->pl_width
= strlen(strval
);
4182 if (received
&& zfs_prop_get_recvd(zhp
,
4183 entry
->pl_user_prop
,
4184 buf
, sizeof (buf
), literal
) == 0)
4185 if (strlen(buf
) > entry
->pl_recvd_width
)
4186 entry
->pl_recvd_width
= strlen(buf
);
4194 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4200 * Keep a reference to the props-table against which we prune the
4203 zhp
->zfs_props_table
= props
;
4205 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4208 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4209 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4212 * User properties will result in ZPROP_INVAL, and since we
4213 * only know how to prune standard ZFS properties, we always
4214 * leave these in the list. This can also happen if we
4215 * encounter an unknown DSL property (when running older
4216 * software, for example).
4218 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4219 (void) nvlist_remove(zhp
->zfs_props
,
4220 nvpair_name(curr
), nvpair_type(curr
));
4226 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4227 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4229 zfs_cmd_t zc
= {"\0"};
4230 nvlist_t
*nvlist
= NULL
;
4233 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4234 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4235 zc
.zc_cookie
= (uint64_t)cmd
;
4237 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4238 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4239 (void) no_memory(hdl
);
4245 case ZFS_SMB_ACL_ADD
:
4246 case ZFS_SMB_ACL_REMOVE
:
4247 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4249 case ZFS_SMB_ACL_RENAME
:
4250 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4252 (void) no_memory(hdl
);
4255 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4257 (void) no_memory(hdl
);
4260 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4261 nvlist_free(nvlist
);
4265 case ZFS_SMB_ACL_PURGE
:
4270 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4271 nvlist_free(nvlist
);
4276 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4277 char *path
, char *resource
)
4279 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4284 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4285 char *path
, char *resource
)
4287 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4292 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4294 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4299 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4300 char *oldname
, char *newname
)
4302 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4307 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4308 zfs_userspace_cb_t func
, void *arg
)
4310 zfs_cmd_t zc
= {"\0"};
4311 zfs_useracct_t buf
[100];
4312 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4315 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4317 zc
.zc_objset_type
= type
;
4318 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4321 zfs_useracct_t
*zua
= buf
;
4323 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4324 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4325 char errbuf
[ZFS_MAXNAMELEN
+ 32];
4327 (void) snprintf(errbuf
, sizeof (errbuf
),
4328 dgettext(TEXT_DOMAIN
,
4329 "cannot get used/quota for %s"), zc
.zc_name
);
4330 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4332 if (zc
.zc_nvlist_dst_size
== 0)
4335 while (zc
.zc_nvlist_dst_size
> 0) {
4336 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4337 zua
->zu_space
)) != 0)
4340 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4349 const char *snapname
;
4351 boolean_t recursive
;
4356 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4358 struct holdarg
*ha
= arg
;
4359 char name
[ZFS_MAXNAMELEN
];
4362 (void) snprintf(name
, sizeof (name
),
4363 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4365 if (lzc_exists(name
))
4366 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4369 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4375 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4376 boolean_t recursive
, int cleanup_fd
)
4381 ha
.nvl
= fnvlist_alloc();
4382 ha
.snapname
= snapname
;
4384 ha
.recursive
= recursive
;
4385 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4387 if (nvlist_empty(ha
.nvl
)) {
4390 fnvlist_free(ha
.nvl
);
4392 (void) snprintf(errbuf
, sizeof (errbuf
),
4393 dgettext(TEXT_DOMAIN
,
4394 "cannot hold snapshot '%s@%s'"),
4395 zhp
->zfs_name
, snapname
);
4396 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4400 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4401 fnvlist_free(ha
.nvl
);
4407 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4411 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4416 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4419 /* There may be errors even in the success case. */
4420 fnvlist_free(errors
);
4424 if (nvlist_empty(errors
)) {
4425 /* no hold-specific errors */
4426 (void) snprintf(errbuf
, sizeof (errbuf
),
4427 dgettext(TEXT_DOMAIN
, "cannot hold"));
4430 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4431 "pool must be upgraded"));
4432 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4435 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4438 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4442 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4444 elem
= nvlist_next_nvpair(errors
, elem
)) {
4445 (void) snprintf(errbuf
, sizeof (errbuf
),
4446 dgettext(TEXT_DOMAIN
,
4447 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4448 switch (fnvpair_value_int32(elem
)) {
4451 * Temporary tags wind up having the ds object id
4452 * prepended. So even if we passed the length check
4453 * above, it's still possible for the tag to wind
4454 * up being slightly too long.
4456 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4459 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4462 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4465 (void) zfs_standard_error(hdl
,
4466 fnvpair_value_int32(elem
), errbuf
);
4470 fnvlist_free(errors
);
4475 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4477 struct holdarg
*ha
= arg
;
4478 char name
[ZFS_MAXNAMELEN
];
4480 nvlist_t
*existing_holds
;
4482 (void) snprintf(name
, sizeof (name
),
4483 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4485 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4487 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4490 nvlist_t
*torelease
= fnvlist_alloc();
4491 fnvlist_add_boolean(torelease
, ha
->tag
);
4492 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4493 fnvlist_free(torelease
);
4497 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4503 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4504 boolean_t recursive
)
4508 nvlist_t
*errors
= NULL
;
4510 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4513 ha
.nvl
= fnvlist_alloc();
4514 ha
.snapname
= snapname
;
4516 ha
.recursive
= recursive
;
4518 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4520 if (nvlist_empty(ha
.nvl
)) {
4521 fnvlist_free(ha
.nvl
);
4523 (void) snprintf(errbuf
, sizeof (errbuf
),
4524 dgettext(TEXT_DOMAIN
,
4525 "cannot release hold from snapshot '%s@%s'"),
4526 zhp
->zfs_name
, snapname
);
4528 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4530 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4535 ret
= lzc_release(ha
.nvl
, &errors
);
4536 fnvlist_free(ha
.nvl
);
4539 /* There may be errors even in the success case. */
4540 fnvlist_free(errors
);
4544 if (nvlist_empty(errors
)) {
4545 /* no hold-specific errors */
4546 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4550 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4551 "pool must be upgraded"));
4552 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4555 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4559 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4561 elem
= nvlist_next_nvpair(errors
, elem
)) {
4562 (void) snprintf(errbuf
, sizeof (errbuf
),
4563 dgettext(TEXT_DOMAIN
,
4564 "cannot release hold from snapshot '%s'"),
4566 switch (fnvpair_value_int32(elem
)) {
4568 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4571 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4574 (void) zfs_standard_error_fmt(hdl
,
4575 fnvpair_value_int32(elem
), errbuf
);
4579 fnvlist_free(errors
);
4584 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4586 zfs_cmd_t zc
= {"\0"};
4587 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4593 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4594 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4598 nvbuf
= malloc(nvsz
);
4599 if (nvbuf
== NULL
) {
4600 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4604 zc
.zc_nvlist_dst_size
= nvsz
;
4605 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4607 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4609 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4610 (void) snprintf(errbuf
, sizeof (errbuf
),
4611 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4616 nvsz
= zc
.zc_nvlist_dst_size
;
4620 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4621 "pool must be upgraded"));
4622 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4625 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4628 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4631 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4636 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4638 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4639 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4641 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4651 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4653 zfs_cmd_t zc
= {"\0"};
4654 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4660 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4661 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4663 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4666 nvbuf
= malloc(nvsz
);
4668 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4671 zc
.zc_nvlist_src_size
= nvsz
;
4672 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4673 zc
.zc_perm_action
= un
;
4675 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4677 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4678 (void) snprintf(errbuf
, sizeof (errbuf
),
4679 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4683 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4684 "pool must be upgraded"));
4685 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4688 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4691 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4694 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4705 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4710 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4713 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4715 (void) snprintf(errbuf
, sizeof (errbuf
),
4716 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4720 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4721 "pool must be upgraded"));
4722 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4725 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4728 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4731 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4740 * Convert the zvol's volume size to an appropriate reservation.
4741 * Note: If this routine is updated, it is necessary to update the ZFS test
4742 * suite's shell version in reservation.kshlib.
4745 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4748 uint64_t nblocks
, volblocksize
;
4752 if (nvlist_lookup_string(props
,
4753 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4754 ncopies
= atoi(strval
);
4757 if (nvlist_lookup_uint64(props
,
4758 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4759 &volblocksize
) != 0)
4760 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4761 nblocks
= volsize
/volblocksize
;
4762 /* start with metadnode L0-L6 */
4764 /* calculate number of indirects */
4765 while (nblocks
> 1) {
4766 nblocks
+= DNODES_PER_LEVEL
- 1;
4767 nblocks
/= DNODES_PER_LEVEL
;
4770 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4773 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4774 * compressed, but in practice they compress down to about
4777 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;