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) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27 * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
42 #include <sys/mntent.h>
43 #include <sys/mount.h>
52 #include <directory.h>
53 #endif /* HAVE_IDMAP */
55 #include <sys/dnode.h>
60 #include "zfs_namecheck.h"
62 #include "libzfs_impl.h"
63 #include "zfs_deleg.h"
65 static int zvol_create_link_common(libzfs_handle_t
*, const char *, int);
66 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
67 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
70 * Given a single type (not a mask of types), return the type in a human
74 zfs_type_to_name(zfs_type_t type
)
77 case ZFS_TYPE_FILESYSTEM
:
78 return (dgettext(TEXT_DOMAIN
, "filesystem"));
79 case ZFS_TYPE_SNAPSHOT
:
80 return (dgettext(TEXT_DOMAIN
, "snapshot"));
82 return (dgettext(TEXT_DOMAIN
, "volume"));
91 * Validate a ZFS path. This is used even before trying to open the dataset, to
92 * provide a more meaningful error message. We call zfs_error_aux() to
93 * explain exactly why the name was not valid.
96 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
102 (void) zfs_prop_get_table();
103 if (dataset_namecheck(path
, &why
, &what
) != 0) {
106 case NAME_ERR_TOOLONG
:
107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
108 "name is too long"));
111 case NAME_ERR_LEADING_SLASH
:
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "leading slash in name"));
116 case NAME_ERR_EMPTY_COMPONENT
:
117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
118 "empty component in name"));
121 case NAME_ERR_TRAILING_SLASH
:
122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
123 "trailing slash in name"));
126 case NAME_ERR_INVALCHAR
:
128 dgettext(TEXT_DOMAIN
, "invalid character "
129 "'%c' in name"), what
);
132 case NAME_ERR_MULTIPLE_AT
:
133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
134 "multiple '@' delimiters in name"));
137 case NAME_ERR_NOLETTER
:
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "pool doesn't begin with a letter"));
142 case NAME_ERR_RESERVED
:
143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
144 "name is reserved"));
147 case NAME_ERR_DISKLIKE
:
148 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
149 "reserved disk name"));
159 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
162 "snapshot delimiter '@' in filesystem name"));
166 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
168 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
169 "missing '@' delimiter in snapshot name"));
173 if (modifying
&& strchr(path
, '%') != NULL
) {
175 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
176 "invalid character %c in name"), '%');
184 zfs_name_valid(const char *name
, zfs_type_t type
)
186 if (type
== ZFS_TYPE_POOL
)
187 return (zpool_name_valid(NULL
, B_FALSE
, name
));
188 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
192 * This function takes the raw DSL properties, and filters out the user-defined
193 * properties into a separate nvlist.
196 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
198 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
203 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
204 (void) no_memory(hdl
);
209 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
210 if (!zfs_prop_user(nvpair_name(elem
)))
213 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
214 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
216 (void) no_memory(hdl
);
224 static zpool_handle_t
*
225 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
227 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
230 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
231 if (hdl
->libzfs_pool_handles
!= NULL
)
232 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
233 hdl
->libzfs_pool_handles
= zph
;
238 static zpool_handle_t
*
239 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
241 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
242 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
244 while ((zph
!= NULL
) &&
245 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
246 zph
= zph
->zpool_next
;
251 * Returns a handle to the pool that contains the provided dataset.
252 * If a handle to that pool already exists then that handle is returned.
253 * Otherwise, a new handle is created and added to the list of handles.
255 static zpool_handle_t
*
256 zpool_handle(zfs_handle_t
*zhp
)
262 len
= strcspn(zhp
->zfs_name
, "/@") + 1;
263 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
264 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
266 zph
= zpool_find_handle(zhp
, pool_name
, len
);
268 zph
= zpool_add_handle(zhp
, pool_name
);
275 zpool_free_handles(libzfs_handle_t
*hdl
)
277 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
279 while (zph
!= NULL
) {
280 next
= zph
->zpool_next
;
284 hdl
->libzfs_pool_handles
= NULL
;
288 * Utility function to gather stats (objset and zpl) for the given object.
291 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
293 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
295 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
297 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
298 if (errno
== ENOMEM
) {
299 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
310 * Utility function to get the received properties of the given object.
313 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
315 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
316 nvlist_t
*recvdprops
;
317 zfs_cmd_t zc
= {"\0"};
320 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
323 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
325 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
326 if (errno
== ENOMEM
) {
327 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
331 zcmd_free_nvlists(&zc
);
336 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
337 zcmd_free_nvlists(&zc
);
341 nvlist_free(zhp
->zfs_recvd_props
);
342 zhp
->zfs_recvd_props
= recvdprops
;
348 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
350 nvlist_t
*allprops
, *userprops
;
352 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
354 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
359 * XXX Why do we store the user props separately, in addition to
360 * storing them in zfs_props?
362 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
363 nvlist_free(allprops
);
367 nvlist_free(zhp
->zfs_props
);
368 nvlist_free(zhp
->zfs_user_props
);
370 zhp
->zfs_props
= allprops
;
371 zhp
->zfs_user_props
= userprops
;
377 get_stats(zfs_handle_t
*zhp
)
380 zfs_cmd_t zc
= {"\0"};
382 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
384 if (get_stats_ioctl(zhp
, &zc
) != 0)
386 else if (put_stats_zhdl(zhp
, &zc
) != 0)
388 zcmd_free_nvlists(&zc
);
393 * Refresh the properties currently stored in the handle.
396 zfs_refresh_properties(zfs_handle_t
*zhp
)
398 (void) get_stats(zhp
);
402 * Makes a handle from the given dataset name. Used by zfs_open() and
403 * zfs_iter_* to create child handles on the fly.
406 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
408 if (put_stats_zhdl(zhp
, zc
) != 0)
412 * We've managed to open the dataset and gather statistics. Determine
413 * the high-level type.
415 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
416 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
417 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
418 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
419 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
420 return (-1); /* zpios' and other testing datasets are
421 of this type, ignore if encountered */
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 * Opens the given snapshot, filesystem, or volume. The 'types'
552 * argument is a mask of acceptable types. The function will print an
553 * appropriate error message and return NULL if it can't be opened.
556 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
561 (void) snprintf(errbuf
, sizeof (errbuf
),
562 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
565 * Validate the name before we even try to open it.
567 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
568 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
569 "invalid dataset name"));
570 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
575 * Try to get stats for the dataset, which will tell us if it exists.
578 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
579 (void) zfs_standard_error(hdl
, errno
, errbuf
);
583 if (!(types
& zhp
->zfs_type
)) {
584 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
593 * Release a ZFS handle. Nothing to do but free the associated memory.
596 zfs_close(zfs_handle_t
*zhp
)
598 if (zhp
->zfs_mntopts
)
599 free(zhp
->zfs_mntopts
);
600 nvlist_free(zhp
->zfs_props
);
601 nvlist_free(zhp
->zfs_user_props
);
602 nvlist_free(zhp
->zfs_recvd_props
);
606 typedef struct mnttab_node
{
607 struct mnttab mtn_mt
;
612 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
614 const mnttab_node_t
*mtn1
= arg1
;
615 const mnttab_node_t
*mtn2
= arg2
;
618 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
622 return (rv
> 0 ? 1 : -1);
626 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
628 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
629 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
630 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
634 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
638 /* Reopen MNTTAB to prevent reading stale data from open file */
639 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
642 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
645 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
647 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
648 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
649 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
650 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
651 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
652 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
659 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
664 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
665 free(mtn
->mtn_mt
.mnt_special
);
666 free(mtn
->mtn_mt
.mnt_mountp
);
667 free(mtn
->mtn_mt
.mnt_fstype
);
668 free(mtn
->mtn_mt
.mnt_mntopts
);
671 avl_destroy(&hdl
->libzfs_mnttab_cache
);
675 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
677 hdl
->libzfs_mnttab_enable
= enable
;
681 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
682 struct mnttab
*entry
)
688 if (!hdl
->libzfs_mnttab_enable
) {
689 struct mnttab srch
= { 0 };
691 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
692 libzfs_mnttab_fini(hdl
);
694 /* Reopen MNTTAB to prevent reading stale data from open file */
695 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
698 srch
.mnt_special
= (char *)fsname
;
699 srch
.mnt_fstype
= MNTTYPE_ZFS
;
700 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
706 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
707 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
710 find
.mtn_mt
.mnt_special
= (char *)fsname
;
711 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
713 *entry
= mtn
->mtn_mt
;
720 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
721 const char *mountp
, const char *mntopts
)
725 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
727 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
728 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
729 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
730 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
731 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
732 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
736 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
741 find
.mtn_mt
.mnt_special
= (char *)fsname
;
742 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
743 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
744 free(ret
->mtn_mt
.mnt_special
);
745 free(ret
->mtn_mt
.mnt_mountp
);
746 free(ret
->mtn_mt
.mnt_fstype
);
747 free(ret
->mtn_mt
.mnt_mntopts
);
753 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
755 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
757 if (zpool_handle
== NULL
)
760 *spa_version
= zpool_get_prop_int(zpool_handle
,
761 ZPOOL_PROP_VERSION
, NULL
);
766 * The choice of reservation property depends on the SPA version.
769 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
773 if (zfs_spa_version(zhp
, &spa_version
) < 0)
776 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
777 *resv_prop
= ZFS_PROP_REFRESERVATION
;
779 *resv_prop
= ZFS_PROP_RESERVATION
;
785 * Given an nvlist of properties to set, validates that they are correct, and
786 * parses any numeric properties (index, boolean, etc) if they are specified as
790 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
791 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
798 int chosen_normal
= -1;
801 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
802 (void) no_memory(hdl
);
807 * Make sure this property is valid and applies to this type.
811 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
812 const char *propname
= nvpair_name(elem
);
814 prop
= zfs_name_to_prop(propname
);
815 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
817 * This is a user property: make sure it's a
818 * string, and that it's less than ZAP_MAXNAMELEN.
820 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
821 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
822 "'%s' must be a string"), propname
);
823 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
827 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
828 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
829 "property name '%s' is too long"),
831 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
835 (void) nvpair_value_string(elem
, &strval
);
836 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
837 (void) no_memory(hdl
);
844 * Currently, only user properties can be modified on
847 if (type
== ZFS_TYPE_SNAPSHOT
) {
848 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
849 "this property can not be modified for snapshots"));
850 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
854 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
855 zfs_userquota_prop_t uqtype
;
856 char newpropname
[128];
861 if (userquota_propname_decode(propname
, zoned
,
862 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
864 dgettext(TEXT_DOMAIN
,
865 "'%s' has an invalid user/group name"),
867 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
871 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
872 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
874 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
876 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
881 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
882 (void) nvpair_value_string(elem
, &strval
);
883 if (strcmp(strval
, "none") == 0) {
885 } else if (zfs_nicestrtonum(hdl
,
886 strval
, &intval
) != 0) {
887 (void) zfs_error(hdl
,
888 EZFS_BADPROP
, errbuf
);
891 } else if (nvpair_type(elem
) ==
893 (void) nvpair_value_uint64(elem
, &intval
);
895 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
896 "use 'none' to disable "
897 "userquota/groupquota"));
901 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
902 "'%s' must be a number"), propname
);
903 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
908 * Encode the prop name as
909 * userquota@<hex-rid>-domain, to make it easy
910 * for the kernel to decode.
912 (void) snprintf(newpropname
, sizeof (newpropname
),
913 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
914 (longlong_t
)rid
, domain
);
918 if (nvlist_add_uint64_array(ret
, newpropname
,
920 (void) no_memory(hdl
);
924 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
925 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
928 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
932 if (prop
== ZPROP_INVAL
) {
933 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
934 "invalid property '%s'"), propname
);
935 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
939 if (!zfs_prop_valid_for_type(prop
, type
)) {
941 dgettext(TEXT_DOMAIN
, "'%s' does not "
942 "apply to datasets of this type"), propname
);
943 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
947 if (zfs_prop_readonly(prop
) &&
948 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
950 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
952 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
956 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
957 &strval
, &intval
, errbuf
) != 0)
961 * Perform some additional checks for specific properties.
964 case ZFS_PROP_VERSION
:
970 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
971 if (intval
< version
) {
972 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
973 "Can not downgrade; already at version %u"),
975 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
981 case ZFS_PROP_RECORDSIZE
:
982 case ZFS_PROP_VOLBLOCKSIZE
:
983 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
984 if (intval
< SPA_MINBLOCKSIZE
||
985 intval
> SPA_MAXBLOCKSIZE
|| !ISP2(intval
)) {
986 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
987 "'%s' must be power of 2 from %u "
989 (uint_t
)SPA_MINBLOCKSIZE
,
990 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
991 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
996 case ZFS_PROP_MLSLABEL
:
1000 * Verify the mlslabel string and convert to
1001 * internal hex label string.
1005 char *hex
= NULL
; /* internal label string */
1007 /* Default value is already OK. */
1008 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1011 /* Verify the label can be converted to binary form */
1012 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1013 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1014 L_NO_CORRECTION
, NULL
) == -1)) {
1018 /* Now translate to hex internal label string */
1019 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1025 m_label_free(new_sl
);
1027 /* If string is already in internal form, we're done. */
1028 if (strcmp(strval
, hex
) == 0) {
1033 /* Replace the label string with the internal form. */
1034 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1036 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1043 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1044 "invalid mlslabel '%s'"), strval
);
1045 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1046 m_label_free(new_sl
); /* OK if null */
1049 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1050 "mlslabels are unsupported"));
1051 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1053 #endif /* HAVE_MLSLABEL */
1056 case ZFS_PROP_MOUNTPOINT
:
1058 namecheck_err_t why
;
1060 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1061 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1064 if (mountpoint_namecheck(strval
, &why
)) {
1066 case NAME_ERR_LEADING_SLASH
:
1068 dgettext(TEXT_DOMAIN
,
1069 "'%s' must be an absolute path, "
1070 "'none', or 'legacy'"), propname
);
1072 case NAME_ERR_TOOLONG
:
1074 dgettext(TEXT_DOMAIN
,
1075 "component of '%s' is too long"),
1081 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1088 case ZFS_PROP_SHARESMB
:
1089 case ZFS_PROP_SHARENFS
:
1091 * For the mountpoint and sharenfs or sharesmb
1092 * properties, check if it can be set in a
1093 * global/non-global zone based on
1094 * the zoned property value:
1096 * global zone non-global zone
1097 * --------------------------------------------------
1098 * zoned=on mountpoint (no) mountpoint (yes)
1099 * sharenfs (no) sharenfs (no)
1100 * sharesmb (no) sharesmb (no)
1102 * zoned=off mountpoint (yes) N/A
1107 if (getzoneid() == GLOBAL_ZONEID
) {
1108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1109 "'%s' cannot be set on "
1110 "dataset in a non-global zone"),
1112 (void) zfs_error(hdl
, EZFS_ZONED
,
1115 } else if (prop
== ZFS_PROP_SHARENFS
||
1116 prop
== ZFS_PROP_SHARESMB
) {
1117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1118 "'%s' cannot be set in "
1119 "a non-global zone"), propname
);
1120 (void) zfs_error(hdl
, EZFS_ZONED
,
1124 } else if (getzoneid() != GLOBAL_ZONEID
) {
1126 * If zoned property is 'off', this must be in
1127 * a global zone. If not, something is wrong.
1129 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1130 "'%s' cannot be set while dataset "
1131 "'zoned' property is set"), propname
);
1132 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1137 * At this point, it is legitimate to set the
1138 * property. Now we want to make sure that the
1139 * property value is valid if it is sharenfs.
1141 if ((prop
== ZFS_PROP_SHARENFS
||
1142 prop
== ZFS_PROP_SHARESMB
) &&
1143 strcmp(strval
, "on") != 0 &&
1144 strcmp(strval
, "off") != 0) {
1145 zfs_share_proto_t proto
;
1147 if (prop
== ZFS_PROP_SHARESMB
)
1153 * Must be an valid sharing protocol
1154 * option string so init the libshare
1155 * in order to enable the parser and
1156 * then parse the options. We use the
1157 * control API since we don't care about
1158 * the current configuration and don't
1159 * want the overhead of loading it
1160 * until we actually do something.
1163 if (zfs_init_libshare(hdl
,
1164 SA_INIT_CONTROL_API
) != SA_OK
) {
1166 * An error occurred so we can't do
1169 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1170 "'%s' cannot be set: problem "
1171 "in share initialization"),
1173 (void) zfs_error(hdl
, EZFS_BADPROP
,
1178 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1180 * There was an error in parsing so
1181 * deal with it by issuing an error
1182 * message and leaving after
1183 * uninitializing the the libshare
1186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1187 "'%s' cannot be set to invalid "
1188 "options"), propname
);
1189 (void) zfs_error(hdl
, EZFS_BADPROP
,
1191 zfs_uninit_libshare(hdl
);
1194 zfs_uninit_libshare(hdl
);
1198 case ZFS_PROP_UTF8ONLY
:
1199 chosen_utf
= (int)intval
;
1201 case ZFS_PROP_NORMALIZE
:
1202 chosen_normal
= (int)intval
;
1209 * For changes to existing volumes, we have some additional
1210 * checks to enforce.
1212 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1213 uint64_t volsize
= zfs_prop_get_int(zhp
,
1215 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1216 ZFS_PROP_VOLBLOCKSIZE
);
1220 case ZFS_PROP_RESERVATION
:
1221 case ZFS_PROP_REFRESERVATION
:
1222 if (intval
> volsize
) {
1223 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1224 "'%s' is greater than current "
1225 "volume size"), propname
);
1226 (void) zfs_error(hdl
, EZFS_BADPROP
,
1232 case ZFS_PROP_VOLSIZE
:
1233 if (intval
% blocksize
!= 0) {
1234 zfs_nicenum(blocksize
, buf
,
1236 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1237 "'%s' must be a multiple of "
1238 "volume block size (%s)"),
1240 (void) zfs_error(hdl
, EZFS_BADPROP
,
1246 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1247 "'%s' cannot be zero"),
1249 (void) zfs_error(hdl
, EZFS_BADPROP
,
1261 * If normalization was chosen, but no UTF8 choice was made,
1262 * enforce rejection of non-UTF8 names.
1264 * If normalization was chosen, but rejecting non-UTF8 names
1265 * was explicitly not chosen, it is an error.
1267 if (chosen_normal
> 0 && chosen_utf
< 0) {
1268 if (nvlist_add_uint64(ret
,
1269 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1270 (void) no_memory(hdl
);
1273 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1274 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1275 "'%s' must be set 'on' if normalization chosen"),
1276 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1277 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1288 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1290 uint64_t old_volsize
;
1291 uint64_t new_volsize
;
1292 uint64_t old_reservation
;
1293 uint64_t new_reservation
;
1294 zfs_prop_t resv_prop
;
1297 * If this is an existing volume, and someone is setting the volsize,
1298 * make sure that it matches the reservation, or add it if necessary.
1300 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1301 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1303 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1304 if ((zvol_volsize_to_reservation(old_volsize
, zhp
->zfs_props
) !=
1305 old_reservation
) || nvlist_lookup_uint64(nvl
,
1306 zfs_prop_to_name(resv_prop
), &new_reservation
) != ENOENT
) {
1309 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1312 new_reservation
= zvol_volsize_to_reservation(new_volsize
,
1314 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1315 new_reservation
) != 0) {
1316 (void) no_memory(zhp
->zfs_hdl
);
1323 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1330 * For quotas and reservations, ENOSPC indicates
1331 * something different; setting a quota or reservation
1332 * doesn't use any disk space.
1335 case ZFS_PROP_QUOTA
:
1336 case ZFS_PROP_REFQUOTA
:
1337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1338 "size is less than current used or "
1340 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1343 case ZFS_PROP_RESERVATION
:
1344 case ZFS_PROP_REFRESERVATION
:
1345 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1346 "size is greater than available space"));
1347 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1351 (void) zfs_standard_error(hdl
, err
, errbuf
);
1357 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1361 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1365 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1366 "pool and or dataset must be upgraded to set this "
1367 "property or value"));
1368 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1372 if (prop
== ZFS_PROP_COMPRESSION
) {
1373 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1374 "property setting is not allowed on "
1375 "bootable datasets"));
1376 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1378 (void) zfs_standard_error(hdl
, err
, errbuf
);
1383 if (prop
== ZPROP_INVAL
) {
1384 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1386 (void) zfs_standard_error(hdl
, err
, errbuf
);
1392 * This platform can't address a volume this big.
1395 if (prop
== ZFS_PROP_VOLSIZE
) {
1396 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1402 (void) zfs_standard_error(hdl
, err
, errbuf
);
1407 zfs_is_namespace_prop(zfs_prop_t prop
)
1411 case ZFS_PROP_ATIME
:
1412 case ZFS_PROP_DEVICES
:
1414 case ZFS_PROP_SETUID
:
1415 case ZFS_PROP_READONLY
:
1416 case ZFS_PROP_XATTR
:
1417 case ZFS_PROP_NBMAND
:
1426 * Given a property name and value, set the property for the given dataset.
1429 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1431 zfs_cmd_t zc
= {"\0"};
1433 prop_changelist_t
*cl
= NULL
;
1435 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1436 nvlist_t
*nvl
= NULL
, *realprops
;
1438 boolean_t do_prefix
= B_TRUE
;
1441 (void) snprintf(errbuf
, sizeof (errbuf
),
1442 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1445 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1446 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1447 (void) no_memory(hdl
);
1451 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1452 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1458 prop
= zfs_name_to_prop(propname
);
1460 if (prop
== ZFS_PROP_VOLSIZE
) {
1461 if ((added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1)
1465 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1468 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1469 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1470 "child dataset with inherited mountpoint is used "
1471 "in a non-global zone"));
1472 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1477 * We don't want to unmount & remount the dataset when changing
1478 * its canmount property to 'on' or 'noauto'. We only use
1479 * the changelist logic to unmount when setting canmount=off.
1481 if (prop
== ZFS_PROP_CANMOUNT
) {
1483 int err
= zprop_string_to_index(prop
, propval
, &idx
,
1485 if (err
== 0 && idx
!= ZFS_CANMOUNT_OFF
)
1486 do_prefix
= B_FALSE
;
1489 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1493 * Execute the corresponding ioctl() to set this property.
1495 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1497 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1500 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1503 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1504 if (added_resv
&& errno
== ENOSPC
) {
1505 /* clean up the volsize property we tried to set */
1506 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1509 zcmd_free_nvlists(&zc
);
1510 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1512 if (nvlist_add_uint64(nvl
,
1513 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1516 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1518 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1522 ret
= changelist_postfix(cl
);
1526 * Refresh the statistics so the new property
1527 * value is reflected.
1529 (void) get_stats(zhp
);
1532 * Remount the filesystem to propagate the change
1533 * if one of the options handled by the generic
1534 * Linux namespace layer has been modified.
1536 if (zfs_is_namespace_prop(prop
) &&
1537 zfs_is_mounted(zhp
, NULL
))
1538 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1544 zcmd_free_nvlists(&zc
);
1546 changelist_free(cl
);
1551 * Given a property, inherit the value from the parent dataset, or if received
1552 * is TRUE, revert to the received value, if any.
1555 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1557 zfs_cmd_t zc
= {"\0"};
1559 prop_changelist_t
*cl
;
1560 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1564 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1565 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1567 zc
.zc_cookie
= received
;
1568 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1570 * For user properties, the amount of work we have to do is very
1571 * small, so just do it here.
1573 if (!zfs_prop_user(propname
)) {
1574 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1575 "invalid property"));
1576 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1579 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1580 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1582 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1583 return (zfs_standard_error(hdl
, errno
, errbuf
));
1589 * Verify that this property is inheritable.
1591 if (zfs_prop_readonly(prop
))
1592 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1594 if (!zfs_prop_inheritable(prop
) && !received
)
1595 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1598 * Check to see if the value applies to this type
1600 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1601 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1604 * Normalize the name, to get rid of shorthand abbreviations.
1606 propname
= zfs_prop_to_name(prop
);
1607 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1608 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1610 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1611 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1612 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1613 "dataset is used in a non-global zone"));
1614 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1618 * Determine datasets which will be affected by this change, if any.
1620 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1623 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1624 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1625 "child dataset with inherited mountpoint is used "
1626 "in a non-global zone"));
1627 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1631 if ((ret
= changelist_prefix(cl
)) != 0)
1634 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1635 return (zfs_standard_error(hdl
, errno
, errbuf
));
1638 if ((ret
= changelist_postfix(cl
)) != 0)
1642 * Refresh the statistics so the new property is reflected.
1644 (void) get_stats(zhp
);
1648 changelist_free(cl
);
1653 * True DSL properties are stored in an nvlist. The following two functions
1654 * extract them appropriately.
1657 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1663 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1664 zfs_prop_to_name(prop
), &nv
) == 0) {
1665 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1666 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1668 verify(!zhp
->zfs_props_table
||
1669 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1670 value
= zfs_prop_default_numeric(prop
);
1678 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1684 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1685 zfs_prop_to_name(prop
), &nv
) == 0) {
1686 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1687 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1689 verify(!zhp
->zfs_props_table
||
1690 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1691 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1700 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1702 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1706 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1708 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1709 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1713 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1715 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1720 * Internal function for getting a numeric property. Both zfs_prop_get() and
1721 * zfs_prop_get_int() are built using this interface.
1723 * Certain properties can be overridden using 'mount -o'. In this case, scan
1724 * the contents of the /etc/mtab entry, searching for the appropriate options.
1725 * If they differ from the on-disk values, report the current values and mark
1726 * the source "temporary".
1729 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1730 char **source
, uint64_t *val
)
1732 zfs_cmd_t zc
= {"\0"};
1733 nvlist_t
*zplprops
= NULL
;
1735 char *mntopt_on
= NULL
;
1736 char *mntopt_off
= NULL
;
1737 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1742 case ZFS_PROP_ATIME
:
1743 mntopt_on
= MNTOPT_ATIME
;
1744 mntopt_off
= MNTOPT_NOATIME
;
1747 case ZFS_PROP_DEVICES
:
1748 mntopt_on
= MNTOPT_DEVICES
;
1749 mntopt_off
= MNTOPT_NODEVICES
;
1753 mntopt_on
= MNTOPT_EXEC
;
1754 mntopt_off
= MNTOPT_NOEXEC
;
1757 case ZFS_PROP_READONLY
:
1758 mntopt_on
= MNTOPT_RO
;
1759 mntopt_off
= MNTOPT_RW
;
1762 case ZFS_PROP_SETUID
:
1763 mntopt_on
= MNTOPT_SETUID
;
1764 mntopt_off
= MNTOPT_NOSETUID
;
1767 case ZFS_PROP_XATTR
:
1768 mntopt_on
= MNTOPT_XATTR
;
1769 mntopt_off
= MNTOPT_NOXATTR
;
1772 case ZFS_PROP_NBMAND
:
1773 mntopt_on
= MNTOPT_NBMAND
;
1774 mntopt_off
= MNTOPT_NONBMAND
;
1781 * Because looking up the mount options is potentially expensive
1782 * (iterating over all of /etc/mtab), we defer its calculation until
1783 * we're looking up a property which requires its presence.
1785 if (!zhp
->zfs_mntcheck
&&
1786 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1787 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1788 struct mnttab entry
;
1790 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1791 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1793 if (zhp
->zfs_mntopts
== NULL
)
1797 zhp
->zfs_mntcheck
= B_TRUE
;
1800 if (zhp
->zfs_mntopts
== NULL
)
1801 mnt
.mnt_mntopts
= "";
1803 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
1806 case ZFS_PROP_ATIME
:
1807 case ZFS_PROP_DEVICES
:
1809 case ZFS_PROP_READONLY
:
1810 case ZFS_PROP_SETUID
:
1811 case ZFS_PROP_XATTR
:
1812 case ZFS_PROP_NBMAND
:
1813 *val
= getprop_uint64(zhp
, prop
, source
);
1818 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
1821 *src
= ZPROP_SRC_TEMPORARY
;
1822 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
1825 *src
= ZPROP_SRC_TEMPORARY
;
1829 case ZFS_PROP_CANMOUNT
:
1830 case ZFS_PROP_VOLSIZE
:
1831 case ZFS_PROP_QUOTA
:
1832 case ZFS_PROP_REFQUOTA
:
1833 case ZFS_PROP_RESERVATION
:
1834 case ZFS_PROP_REFRESERVATION
:
1835 *val
= getprop_uint64(zhp
, prop
, source
);
1837 if (*source
== NULL
) {
1838 /* not default, must be local */
1839 *source
= zhp
->zfs_name
;
1843 case ZFS_PROP_MOUNTED
:
1844 *val
= (zhp
->zfs_mntopts
!= NULL
);
1847 case ZFS_PROP_NUMCLONES
:
1848 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
1851 case ZFS_PROP_VERSION
:
1852 case ZFS_PROP_NORMALIZE
:
1853 case ZFS_PROP_UTF8ONLY
:
1855 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
1856 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1858 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1859 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
1860 zcmd_free_nvlists(&zc
);
1863 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
1864 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
1866 zcmd_free_nvlists(&zc
);
1870 nvlist_free(zplprops
);
1871 zcmd_free_nvlists(&zc
);
1875 switch (zfs_prop_get_type(prop
)) {
1876 case PROP_TYPE_NUMBER
:
1877 case PROP_TYPE_INDEX
:
1878 *val
= getprop_uint64(zhp
, prop
, source
);
1880 * If we tried to use a default value for a
1881 * readonly property, it means that it was not
1884 if (zfs_prop_readonly(prop
) &&
1885 *source
!= NULL
&& (*source
)[0] == '\0') {
1890 case PROP_TYPE_STRING
:
1892 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1893 "cannot get non-numeric property"));
1894 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
1895 dgettext(TEXT_DOMAIN
, "internal error")));
1903 * Calculate the source type, given the raw source string.
1906 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
1907 char *statbuf
, size_t statlen
)
1909 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
1912 if (source
== NULL
) {
1913 *srctype
= ZPROP_SRC_NONE
;
1914 } else if (source
[0] == '\0') {
1915 *srctype
= ZPROP_SRC_DEFAULT
;
1916 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
1917 *srctype
= ZPROP_SRC_RECEIVED
;
1919 if (strcmp(source
, zhp
->zfs_name
) == 0) {
1920 *srctype
= ZPROP_SRC_LOCAL
;
1922 (void) strlcpy(statbuf
, source
, statlen
);
1923 *srctype
= ZPROP_SRC_INHERITED
;
1930 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
1931 size_t proplen
, boolean_t literal
)
1936 if (zhp
->zfs_recvd_props
== NULL
)
1937 if (get_recvd_props_ioctl(zhp
) != 0)
1940 prop
= zfs_name_to_prop(propname
);
1942 if (prop
!= ZPROP_INVAL
) {
1944 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
1946 zfs_set_recvd_props_mode(zhp
, &cookie
);
1947 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
1948 NULL
, NULL
, 0, literal
);
1949 zfs_unset_recvd_props_mode(zhp
, &cookie
);
1953 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
1954 propname
, &propval
) != 0)
1956 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
1958 (void) strlcpy(propbuf
, recvdval
, proplen
);
1961 return (err
== 0 ? 0 : -1);
1965 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
1970 value
= zfs_get_clones_nvl(zhp
);
1975 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
1976 pair
= nvlist_next_nvpair(value
, pair
)) {
1977 if (propbuf
[0] != '\0')
1978 (void) strlcat(propbuf
, ",", proplen
);
1979 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
1985 struct get_clones_arg
{
1989 char buf
[ZFS_MAXNAMELEN
];
1993 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
1995 struct get_clones_arg
*gca
= arg
;
1997 if (gca
->numclones
== 0) {
2002 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2003 NULL
, NULL
, 0, B_TRUE
) != 0)
2005 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2006 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2011 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2017 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2019 nvlist_t
*nv
, *value
;
2021 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2022 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2023 struct get_clones_arg gca
;
2026 * if this is a snapshot, then the kernel wasn't able
2027 * to get the clones. Do it by slowly iterating.
2029 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2031 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2033 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2038 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2040 gca
.origin
= zhp
->zfs_name
;
2042 if (gca
.numclones
!= 0) {
2044 char pool
[ZFS_MAXNAMELEN
];
2047 /* get the pool name */
2048 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2049 (void) strsep(&cp
, "/@");
2050 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2051 ZFS_TYPE_FILESYSTEM
);
2053 (void) get_clones_cb(root
, &gca
);
2056 if (gca
.numclones
!= 0 ||
2057 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2058 nvlist_add_nvlist(zhp
->zfs_props
,
2059 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2066 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2067 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2070 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2076 * Retrieve a property from the given object. If 'literal' is specified, then
2077 * numbers are left as exact values. Otherwise, numbers are converted to a
2078 * human-readable form.
2080 * Returns 0 on success, or -1 on error.
2083 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2084 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2086 char *source
= NULL
;
2090 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2093 * Check to see if this property applies to our object
2095 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
2098 if (received
&& zfs_prop_readonly(prop
))
2102 *src
= ZPROP_SRC_NONE
;
2105 case ZFS_PROP_CREATION
:
2107 * 'creation' is a time_t stored in the statistics. We convert
2108 * this into a string unless 'literal' is specified.
2111 val
= getprop_uint64(zhp
, prop
, &source
);
2112 time_t time
= (time_t)val
;
2116 localtime_r(&time
, &t
) == NULL
||
2117 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2119 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
) val
);
2123 case ZFS_PROP_MOUNTPOINT
:
2125 * Getting the precise mountpoint can be tricky.
2127 * - for 'none' or 'legacy', return those values.
2128 * - for inherited mountpoints, we want to take everything
2129 * after our ancestor and append it to the inherited value.
2131 * If the pool has an alternate root, we want to prepend that
2132 * root to any values we return.
2135 str
= getprop_string(zhp
, prop
, &source
);
2137 if (str
[0] == '/') {
2138 char buf
[MAXPATHLEN
];
2140 const char *relpath
;
2143 * If we inherit the mountpoint, even from a dataset
2144 * with a received value, the source will be the path of
2145 * the dataset we inherit from. If source is
2146 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2149 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2152 relpath
= zhp
->zfs_name
+ strlen(source
);
2153 if (relpath
[0] == '/')
2157 if ((zpool_get_prop(zhp
->zpool_hdl
,
2158 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
2159 (strcmp(root
, "-") == 0))
2162 * Special case an alternate root of '/'. This will
2163 * avoid having multiple leading slashes in the
2166 if (strcmp(root
, "/") == 0)
2170 * If the mountpoint is '/' then skip over this
2171 * if we are obtaining either an alternate root or
2172 * an inherited mountpoint.
2174 if (str
[1] == '\0' && (root
[0] != '\0' ||
2175 relpath
[0] != '\0'))
2178 if (relpath
[0] == '\0')
2179 (void) snprintf(propbuf
, proplen
, "%s%s",
2182 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2183 root
, str
, relpath
[0] == '@' ? "" : "/",
2186 /* 'legacy' or 'none' */
2187 (void) strlcpy(propbuf
, str
, proplen
);
2192 case ZFS_PROP_ORIGIN
:
2193 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2196 * If there is no parent at all, return failure to indicate that
2197 * it doesn't apply to this dataset.
2199 if (propbuf
[0] == '\0')
2203 case ZFS_PROP_CLONES
:
2204 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2208 case ZFS_PROP_QUOTA
:
2209 case ZFS_PROP_REFQUOTA
:
2210 case ZFS_PROP_RESERVATION
:
2211 case ZFS_PROP_REFRESERVATION
:
2213 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2217 * If quota or reservation is 0, we translate this into 'none'
2218 * (unless literal is set), and indicate that it's the default
2219 * value. Otherwise, we print the number nicely and indicate
2220 * that its set locally.
2224 (void) strlcpy(propbuf
, "0", proplen
);
2226 (void) strlcpy(propbuf
, "none", proplen
);
2229 (void) snprintf(propbuf
, proplen
, "%llu",
2232 zfs_nicenum(val
, propbuf
, proplen
);
2236 case ZFS_PROP_REFRATIO
:
2237 case ZFS_PROP_COMPRESSRATIO
:
2238 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2240 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2241 (u_longlong_t
)(val
/ 100),
2242 (u_longlong_t
)(val
% 100));
2246 switch (zhp
->zfs_type
) {
2247 case ZFS_TYPE_FILESYSTEM
:
2250 case ZFS_TYPE_VOLUME
:
2253 case ZFS_TYPE_SNAPSHOT
:
2259 (void) snprintf(propbuf
, proplen
, "%s", str
);
2262 case ZFS_PROP_MOUNTED
:
2264 * The 'mounted' property is a pseudo-property that described
2265 * whether the filesystem is currently mounted. Even though
2266 * it's a boolean value, the typical values of "on" and "off"
2267 * don't make sense, so we translate to "yes" and "no".
2269 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2270 src
, &source
, &val
) != 0)
2273 (void) strlcpy(propbuf
, "yes", proplen
);
2275 (void) strlcpy(propbuf
, "no", proplen
);
2280 * The 'name' property is a pseudo-property derived from the
2281 * dataset name. It is presented as a real property to simplify
2284 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2287 case ZFS_PROP_MLSLABEL
:
2289 #ifdef HAVE_MLSLABEL
2290 m_label_t
*new_sl
= NULL
;
2291 char *ascii
= NULL
; /* human readable label */
2293 (void) strlcpy(propbuf
,
2294 getprop_string(zhp
, prop
, &source
), proplen
);
2296 if (literal
|| (strcasecmp(propbuf
,
2297 ZFS_MLSLABEL_DEFAULT
) == 0))
2301 * Try to translate the internal hex string to
2302 * human-readable output. If there are any
2303 * problems just use the hex string.
2306 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2307 L_NO_CORRECTION
, NULL
) == -1) {
2308 m_label_free(new_sl
);
2312 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2316 m_label_free(new_sl
);
2319 m_label_free(new_sl
);
2321 (void) strlcpy(propbuf
, ascii
, proplen
);
2324 (void) strlcpy(propbuf
,
2325 getprop_string(zhp
, prop
, &source
), proplen
);
2326 #endif /* HAVE_MLSLABEL */
2332 * GUIDs are stored as numbers, but they are identifiers.
2333 * We don't want them to be pretty printed, because pretty
2334 * printing mangles the ID into a truncated and useless value.
2336 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2338 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2342 switch (zfs_prop_get_type(prop
)) {
2343 case PROP_TYPE_NUMBER
:
2344 if (get_numeric_property(zhp
, prop
, src
,
2345 &source
, &val
) != 0)
2348 (void) snprintf(propbuf
, proplen
, "%llu",
2351 zfs_nicenum(val
, propbuf
, proplen
);
2354 case PROP_TYPE_STRING
:
2355 (void) strlcpy(propbuf
,
2356 getprop_string(zhp
, prop
, &source
), proplen
);
2359 case PROP_TYPE_INDEX
:
2360 if (get_numeric_property(zhp
, prop
, src
,
2361 &source
, &val
) != 0)
2363 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2365 (void) strlcpy(propbuf
, strval
, proplen
);
2373 get_source(zhp
, src
, source
, statbuf
, statlen
);
2379 * Utility function to get the given numeric property. Does no validation that
2380 * the given property is the appropriate type; should only be used with
2381 * hard-coded property types.
2384 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2389 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2395 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2399 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2400 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2404 * Similar to zfs_prop_get(), but returns the value as an integer.
2407 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2408 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2413 * Check to see if this property applies to our object
2415 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2416 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2417 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2418 zfs_prop_to_name(prop
)));
2422 *src
= ZPROP_SRC_NONE
;
2424 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2427 get_source(zhp
, src
, source
, statbuf
, statlen
);
2434 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2435 char **domainp
, idmap_rid_t
*ridp
)
2437 idmap_get_handle_t
*get_hdl
= NULL
;
2441 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2445 err
= idmap_get_sidbyuid(get_hdl
, id
,
2446 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2448 err
= idmap_get_sidbygid(get_hdl
, id
,
2449 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2451 if (err
== IDMAP_SUCCESS
&&
2452 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2453 status
== IDMAP_SUCCESS
)
2459 idmap_get_destroy(get_hdl
);
2462 #endif /* HAVE_IDMAP */
2465 * convert the propname into parameters needed by kernel
2466 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2467 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2468 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2469 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2472 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2473 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2475 zfs_userquota_prop_t type
;
2484 /* Figure out the property type ({user|group}{quota|space}) */
2485 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2486 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2487 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2490 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2494 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
);
2495 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
);
2497 cp
= strchr(propname
, '@') + 1;
2499 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2500 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2503 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2504 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2507 } else if (strchr(cp
, '@')) {
2510 * It's a SID name (eg "user@domain") that needs to be
2511 * turned into S-1-domainID-RID.
2513 directory_error_t e
;
2514 char *numericsid
= NULL
;
2517 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2520 e
= directory_sid_from_user_name(NULL
,
2523 e
= directory_sid_from_group_name(NULL
,
2527 directory_error_free(e
);
2530 if (numericsid
== NULL
)
2533 (void) strlcpy(domain
, cp
, domainlen
);
2534 cp
= strrchr(domain
, '-');
2539 *ridp
= strtoull(cp
, &end
, 10);
2542 if (errno
!= 0 || *end
!= '\0')
2546 #endif /* HAVE_IDMAP */
2548 /* It's a user/group ID (eg "12345"). */
2551 id
= strtoul(cp
, &end
, 10);
2556 /* It's an ephemeral ID. */
2560 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2561 &mapdomain
, &rid
) != 0)
2563 (void) strlcpy(domain
, mapdomain
, domainlen
);
2567 #endif /* HAVE_IDMAP */
2577 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2578 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2581 zfs_cmd_t zc
= {"\0"};
2583 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2585 err
= userquota_propname_decode(propname
,
2586 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2587 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2588 zc
.zc_objset_type
= *typep
;
2592 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2596 *propvalue
= zc
.zc_cookie
;
2601 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2602 uint64_t *propvalue
)
2604 zfs_userquota_prop_t type
;
2606 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2611 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2612 char *propbuf
, int proplen
, boolean_t literal
)
2616 zfs_userquota_prop_t type
;
2618 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2625 (void) snprintf(propbuf
, proplen
, "%llu",
2626 (u_longlong_t
)propvalue
);
2627 } else if (propvalue
== 0 &&
2628 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2629 (void) strlcpy(propbuf
, "none", proplen
);
2631 zfs_nicenum(propvalue
, propbuf
, proplen
);
2637 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2638 uint64_t *propvalue
)
2641 zfs_cmd_t zc
= {"\0"};
2642 const char *snapname
;
2644 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2646 snapname
= strchr(propname
, '@') + 1;
2647 if (strchr(snapname
, '@')) {
2648 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2650 /* snapname is the short name, append it to zhp's fsname */
2653 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
2654 sizeof (zc
.zc_value
));
2655 cp
= strchr(zc
.zc_value
, '@');
2658 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
2659 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
2662 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
2666 *propvalue
= zc
.zc_cookie
;
2671 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
2672 char *propbuf
, int proplen
, boolean_t literal
)
2677 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
2683 (void) snprintf(propbuf
, proplen
, "%llu", (long long unsigned int)propvalue
);
2685 zfs_nicenum(propvalue
, propbuf
, proplen
);
2692 * Returns the name of the given zfs handle.
2695 zfs_get_name(const zfs_handle_t
*zhp
)
2697 return (zhp
->zfs_name
);
2701 * Returns the type of the given zfs handle.
2704 zfs_get_type(const zfs_handle_t
*zhp
)
2706 return (zhp
->zfs_type
);
2710 * Is one dataset name a child dataset of another?
2712 * Needs to handle these cases:
2713 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2714 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2715 * Descendant? No. No. No. Yes.
2718 is_descendant(const char *ds1
, const char *ds2
)
2720 size_t d1len
= strlen(ds1
);
2722 /* ds2 can't be a descendant if it's smaller */
2723 if (strlen(ds2
) < d1len
)
2726 /* otherwise, compare strings and verify that there's a '/' char */
2727 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2731 * Given a complete name, return just the portion that refers to the parent.
2732 * Will return -1 if there is no parent (path is just the name of the
2736 parent_name(const char *path
, char *buf
, size_t buflen
)
2740 (void) strlcpy(buf
, path
, buflen
);
2742 if ((slashp
= strrchr(buf
, '/')) == NULL
)
2750 * If accept_ancestor is false, then check to make sure that the given path has
2751 * a parent, and that it exists. If accept_ancestor is true, then find the
2752 * closest existing ancestor for the given path. In prefixlen return the
2753 * length of already existing prefix of the given path. We also fetch the
2754 * 'zoned' property, which is used to validate property settings when creating
2758 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2759 boolean_t accept_ancestor
, int *prefixlen
)
2761 zfs_cmd_t zc
= {"\0"};
2762 char parent
[ZFS_MAXNAMELEN
];
2768 (void) snprintf(errbuf
, sizeof (errbuf
),
2769 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2771 /* get parent, and check to see if this is just a pool */
2772 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2773 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2774 "missing dataset name"));
2775 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2778 /* check to see if the pool exists */
2779 if ((slash
= strchr(parent
, '/')) == NULL
)
2780 slash
= parent
+ strlen(parent
);
2781 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2782 zc
.zc_name
[slash
- parent
] = '\0';
2783 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2785 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2786 "no such pool '%s'"), zc
.zc_name
);
2787 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2790 /* check to see if the parent dataset exists */
2791 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2792 if (errno
== ENOENT
&& accept_ancestor
) {
2794 * Go deeper to find an ancestor, give up on top level.
2796 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2797 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2798 "no such pool '%s'"), zc
.zc_name
);
2799 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2801 } else if (errno
== ENOENT
) {
2802 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2803 "parent does not exist"));
2804 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2806 return (zfs_standard_error(hdl
, errno
, errbuf
));
2809 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2813 /* we are in a non-global zone, but parent is in the global zone */
2814 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2815 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2820 /* make sure parent is a filesystem */
2821 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2822 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2823 "parent is not a filesystem"));
2824 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2830 if (prefixlen
!= NULL
)
2831 *prefixlen
= strlen(parent
);
2836 * Finds whether the dataset of the given type(s) exists.
2839 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2843 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2847 * Try to get stats for the dataset, which will tell us if it exists.
2849 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2850 int ds_type
= zhp
->zfs_type
;
2853 if (types
& ds_type
)
2860 * Given a path to 'target', create all the ancestors between
2861 * the prefixlen portion of the path, and the target itself.
2862 * Fail if the initial prefixlen-ancestor does not already exist.
2865 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2871 /* make sure prefix exists */
2872 cp
= target
+ prefixlen
;
2874 assert(strchr(cp
, '/') == NULL
);
2875 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2878 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2886 * Attempt to create, mount, and share any ancestor filesystems,
2887 * up to the prefixlen-long one.
2889 for (cp
= target
+ prefixlen
+ 1;
2890 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
2894 h
= make_dataset_handle(hdl
, target
);
2896 /* it already exists, nothing to do here */
2901 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2903 opname
= dgettext(TEXT_DOMAIN
, "create");
2907 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2909 opname
= dgettext(TEXT_DOMAIN
, "open");
2913 if (zfs_mount(h
, NULL
, 0) != 0) {
2914 opname
= dgettext(TEXT_DOMAIN
, "mount");
2918 if (zfs_share(h
) != 0) {
2919 opname
= dgettext(TEXT_DOMAIN
, "share");
2929 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2930 "failed to %s ancestor '%s'"), opname
, target
);
2935 * Creates non-existing ancestors of the given path.
2938 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2944 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
2947 if ((path_copy
= strdup(path
)) != NULL
) {
2948 rc
= create_parents(hdl
, path_copy
, prefix
);
2951 if (path_copy
== NULL
|| rc
!= 0)
2958 * Create a new filesystem or volume.
2961 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2966 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2969 dmu_objset_type_t ost
;
2971 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2972 "cannot create '%s'"), path
);
2974 /* validate the path, taking care to note the extended error message */
2975 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
2976 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2978 /* validate parents exist */
2979 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
2983 * The failure modes when creating a dataset of a different type over
2984 * one that already exists is a little strange. In particular, if you
2985 * try to create a dataset on top of an existing dataset, the ioctl()
2986 * will return ENOENT, not EEXIST. To prevent this from happening, we
2987 * first try to see if the dataset exists.
2989 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
2990 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2991 "dataset already exists"));
2992 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2995 if (type
== ZFS_TYPE_VOLUME
)
3000 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3001 zoned
, NULL
, errbuf
)) == 0)
3004 if (type
== ZFS_TYPE_VOLUME
) {
3006 * If we are creating a volume, the size and block size must
3007 * satisfy a few restraints. First, the blocksize must be a
3008 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3009 * volsize must be a multiple of the block size, and cannot be
3012 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3013 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3015 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3016 "missing volume size"));
3017 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3020 if ((ret
= nvlist_lookup_uint64(props
,
3021 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3022 &blocksize
)) != 0) {
3023 if (ret
== ENOENT
) {
3024 blocksize
= zfs_prop_default_numeric(
3025 ZFS_PROP_VOLBLOCKSIZE
);
3028 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3029 "missing volume block size"));
3030 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3037 "volume size cannot be zero"));
3038 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3041 if (size
% blocksize
!= 0) {
3043 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3044 "volume size must be a multiple of volume block "
3046 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3050 /* create the dataset */
3051 ret
= lzc_create(path
, ost
, props
);
3054 if (ret
== 0 && type
== ZFS_TYPE_VOLUME
) {
3055 ret
= zvol_create_link(hdl
, path
);
3057 (void) zfs_standard_error(hdl
, errno
,
3058 dgettext(TEXT_DOMAIN
,
3059 "Volume successfully created, but device links "
3060 "were not created"));
3065 /* check for failure */
3067 char parent
[ZFS_MAXNAMELEN
];
3068 (void) parent_name(path
, parent
, sizeof (parent
));
3072 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3073 "no such parent '%s'"), parent
);
3074 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3077 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3078 "parent '%s' is not a filesystem"), parent
);
3079 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3082 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3083 "volume block size must be power of 2 from "
3085 (uint_t
)SPA_MINBLOCKSIZE
,
3086 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
3088 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3091 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3092 "pool must be upgraded to set this "
3093 "property or value"));
3094 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3098 * This platform can't address a volume this big.
3100 if (type
== ZFS_TYPE_VOLUME
)
3101 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3106 return (zfs_standard_error(hdl
, errno
, errbuf
));
3114 * Destroys the given dataset. The caller must make sure that the filesystem
3115 * isn't mounted, and that there are no active dependents. If the file system
3116 * does not exist this function does nothing.
3119 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3121 zfs_cmd_t zc
= {"\0"};
3123 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3125 if (ZFS_IS_VOLUME(zhp
)) {
3126 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3129 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3131 zc
.zc_objset_type
= DMU_OST_ZFS
;
3134 zc
.zc_defer_destroy
= defer
;
3135 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3137 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3138 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3142 remove_mountpoint(zhp
);
3147 struct destroydata
{
3149 const char *snapname
;
3153 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3155 struct destroydata
*dd
= arg
;
3157 char name
[ZFS_MAXNAMELEN
];
3160 (void) snprintf(name
, sizeof (name
),
3161 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3163 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
3165 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3169 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3170 (void) zvol_remove_link(zhp
->zfs_hdl
, name
);
3172 * NB: this is simply a best-effort. We don't want to
3173 * return an error, because then we wouldn't visit all
3178 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3184 * Destroys all snapshots with the given name in zhp & descendants.
3187 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3190 struct destroydata dd
= { 0 };
3192 dd
.snapname
= snapname
;
3193 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3194 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3196 if (nvlist_next_nvpair(dd
.nvl
, NULL
) == NULL
) {
3197 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3198 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3199 zhp
->zfs_name
, snapname
);
3201 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3203 nvlist_free(dd
.nvl
);
3208 * Destroys all the snapshots named in the nvlist.
3211 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3217 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3222 if (nvlist_next_nvpair(errlist
, NULL
) == NULL
) {
3224 (void) snprintf(errbuf
, sizeof (errbuf
),
3225 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3227 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3229 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3230 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3232 (void) snprintf(errbuf
, sizeof (errbuf
),
3233 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3236 switch (fnvpair_value_int32(pair
)) {
3239 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3240 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3243 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3252 * Clones the given dataset. The target must be of the same type as the source.
3255 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3257 char parent
[ZFS_MAXNAMELEN
];
3260 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3263 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3265 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3266 "cannot create '%s'"), target
);
3268 /* validate the target/clone name */
3269 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3270 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3272 /* validate parents exist */
3273 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3276 (void) parent_name(target
, parent
, sizeof (parent
));
3282 if (ZFS_IS_VOLUME(zhp
)) {
3283 type
= ZFS_TYPE_VOLUME
;
3285 type
= ZFS_TYPE_FILESYSTEM
;
3287 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3288 zhp
, errbuf
)) == NULL
)
3292 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3300 * The parent doesn't exist. We should have caught this
3301 * above, but there may a race condition that has since
3302 * destroyed the parent.
3304 * At this point, we don't know whether it's the source
3305 * that doesn't exist anymore, or whether the target
3306 * dataset doesn't exist.
3308 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3309 "no such parent '%s'"), parent
);
3310 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3313 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3314 "source and target pools differ"));
3315 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3319 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3322 } else if (ZFS_IS_VOLUME(zhp
)) {
3323 ret
= zvol_create_link(zhp
->zfs_hdl
, target
);
3329 typedef struct promote_data
{
3330 char cb_mountpoint
[MAXPATHLEN
];
3331 const char *cb_target
;
3332 const char *cb_errbuf
;
3333 uint64_t cb_pivot_txg
;
3337 promote_snap_cb(zfs_handle_t
*zhp
, void *data
)
3339 promote_data_t
*pd
= data
;
3341 char snapname
[MAXPATHLEN
];
3344 /* We don't care about snapshots after the pivot point */
3345 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > pd
->cb_pivot_txg
) {
3350 /* Remove the device link if it's a zvol. */
3351 if (ZFS_IS_VOLUME(zhp
))
3352 (void) zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3354 /* Check for conflicting names */
3355 (void) strlcpy(snapname
, pd
->cb_target
, sizeof (snapname
));
3356 (void) strlcat(snapname
, strchr(zhp
->zfs_name
, '@'), sizeof (snapname
));
3357 szhp
= make_dataset_handle(zhp
->zfs_hdl
, snapname
);
3360 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3361 "snapshot name '%s' from origin \n"
3362 "conflicts with '%s' from target"),
3363 zhp
->zfs_name
, snapname
);
3364 rv
= zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, pd
->cb_errbuf
);
3371 promote_snap_done_cb(zfs_handle_t
*zhp
, void *data
)
3373 promote_data_t
*pd
= data
;
3375 /* We don't care about snapshots after the pivot point */
3376 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) <= pd
->cb_pivot_txg
) {
3377 /* Create the device link if it's a zvol. */
3378 if (ZFS_IS_VOLUME(zhp
))
3379 (void) zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3387 * Promotes the given clone fs to be the clone parent.
3390 zfs_promote(zfs_handle_t
*zhp
)
3392 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3393 zfs_cmd_t zc
= {"\0"};
3394 char parent
[MAXPATHLEN
];
3401 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3402 "cannot promote '%s'"), zhp
->zfs_name
);
3404 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3405 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3406 "snapshots can not be promoted"));
3407 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3410 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3411 if (parent
[0] == '\0') {
3412 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3413 "not a cloned filesystem"));
3414 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3416 cp
= strchr(parent
, '@');
3419 /* Walk the snapshots we will be moving */
3420 pzhp
= zfs_open(hdl
, zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
3423 pd
.cb_pivot_txg
= zfs_prop_get_int(pzhp
, ZFS_PROP_CREATETXG
);
3425 pd
.cb_target
= zhp
->zfs_name
;
3426 pd
.cb_errbuf
= errbuf
;
3427 pzhp
= zfs_open(hdl
, parent
, ZFS_TYPE_DATASET
);
3430 (void) zfs_prop_get(pzhp
, ZFS_PROP_MOUNTPOINT
, pd
.cb_mountpoint
,
3431 sizeof (pd
.cb_mountpoint
), NULL
, NULL
, 0, FALSE
);
3432 ret
= zfs_iter_snapshots(pzhp
, B_FALSE
, promote_snap_cb
, &pd
);
3438 /* issue the ioctl */
3439 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3440 sizeof (zc
.zc_value
));
3441 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3442 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3445 int save_errno
= errno
;
3447 (void) zfs_iter_snapshots(pzhp
, B_FALSE
, promote_snap_done_cb
,
3451 switch (save_errno
) {
3454 * There is a conflicting snapshot name. We
3455 * should have caught this above, but they could
3456 * have renamed something in the mean time.
3458 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3459 "conflicting snapshot '%s' from parent '%s'"),
3460 zc
.zc_string
, parent
);
3461 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3464 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3467 (void) zfs_iter_snapshots(zhp
, B_FALSE
, promote_snap_done_cb
,
3476 const char *cd_snapname
;
3481 zfs_create_link_cb(zfs_handle_t
*zhp
, void *arg
)
3483 struct createdata
*cd
= arg
;
3486 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3487 char name
[MAXPATHLEN
];
3489 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3490 (void) strlcat(name
, "@", sizeof (name
));
3491 (void) strlcat(name
, cd
->cd_snapname
, sizeof (name
));
3492 (void) zvol_create_link_common(zhp
->zfs_hdl
, name
,
3495 * NB: this is simply a best-effort. We don't want to
3496 * return an error, because then we wouldn't visit all
3501 ret
= zfs_iter_filesystems(zhp
, zfs_create_link_cb
, cd
);
3508 typedef struct snapdata
{
3510 const char *sd_snapname
;
3514 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3516 snapdata_t
*sd
= arg
;
3517 char name
[ZFS_MAXNAMELEN
];
3520 (void) snprintf(name
, sizeof (name
),
3521 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3523 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3525 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3531 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3535 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3542 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3543 "cannot create snapshots "));
3546 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3547 const char *snapname
= nvpair_name(elem
);
3549 /* validate the target name */
3550 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3552 (void) snprintf(errbuf
, sizeof (errbuf
),
3553 dgettext(TEXT_DOMAIN
,
3554 "cannot create snapshot '%s'"), snapname
);
3555 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3559 if (props
!= NULL
&&
3560 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3561 props
, B_FALSE
, NULL
, errbuf
)) == NULL
) {
3565 ret
= lzc_snapshot(snaps
, props
, &errors
);
3568 boolean_t printed
= B_FALSE
;
3569 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3571 elem
= nvlist_next_nvpair(errors
, elem
)) {
3572 (void) snprintf(errbuf
, sizeof (errbuf
),
3573 dgettext(TEXT_DOMAIN
,
3574 "cannot create snapshot '%s'"), nvpair_name(elem
));
3575 (void) zfs_standard_error(hdl
,
3576 fnvpair_value_int32(elem
), errbuf
);
3582 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3583 "multiple snapshots of same "
3585 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3589 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3594 int linktries
= 0, linkok
= 0, linkfail
= 0;
3597 for (snap
= nvlist_next_nvpair(snaps
, NULL
); snap
!= NULL
;
3598 snap
= nvlist_next_nvpair(snaps
, snap
)) {
3599 char *cp
, *snapname
;
3601 snapname
= nvpair_name(snap
);
3602 cp
= strchr(snapname
, '@');
3605 if ((zhp
= zfs_open(hdl
, snapname
, ZFS_TYPE_FILESYSTEM
|
3606 ZFS_TYPE_VOLUME
)) != NULL
) {
3607 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3610 if (zvol_create_link(zhp
->zfs_hdl
, nvpair_name(snap
)))
3620 nvlist_free(errors
);
3625 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3629 snapdata_t sd
= { 0 };
3630 char fsname
[ZFS_MAXNAMELEN
];
3635 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3636 "cannot snapshot %s"), path
);
3638 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3639 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3641 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3642 cp
= strchr(fsname
, '@');
3644 sd
.sd_snapname
= cp
+ 1;
3646 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3647 ZFS_TYPE_VOLUME
)) == NULL
) {
3651 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3653 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3655 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3658 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3659 nvlist_free(sd
.sd_nvl
);
3665 * Destroy any more recent snapshots. We invoke this callback on any dependents
3666 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3667 * is a dependent and we should just destroy it without checking the transaction
3670 typedef struct rollback_data
{
3671 const char *cb_target
; /* the snapshot */
3672 uint64_t cb_create
; /* creation time reference */
3674 boolean_t cb_dependent
;
3679 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3681 rollback_data_t
*cbp
= data
;
3683 if (!cbp
->cb_dependent
) {
3684 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3685 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3686 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3689 cbp
->cb_dependent
= B_TRUE
;
3690 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3691 rollback_destroy
, cbp
);
3692 cbp
->cb_dependent
= B_FALSE
;
3694 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3697 /* We must destroy this clone; first unmount it */
3698 prop_changelist_t
*clp
;
3700 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3701 cbp
->cb_force
? MS_FORCE
: 0);
3702 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3703 cbp
->cb_error
= B_TRUE
;
3707 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3708 cbp
->cb_error
= B_TRUE
;
3710 changelist_remove(clp
, zhp
->zfs_name
);
3711 (void) changelist_postfix(clp
);
3712 changelist_free(clp
);
3720 * Given a dataset, rollback to a specific snapshot, discarding any
3721 * data changes since then and making it the active dataset.
3723 * Any snapshots more recent than the target are destroyed, along with
3727 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3729 rollback_data_t cb
= { 0 };
3731 zfs_cmd_t zc
= {"\0"};
3732 boolean_t restore_resv
= 0;
3733 uint64_t old_volsize
= 0, new_volsize
;
3734 zfs_prop_t resv_prop
= { 0 };
3736 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3737 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3740 * Destroy all recent snapshots and their dependents.
3742 cb
.cb_force
= force
;
3743 cb
.cb_target
= snap
->zfs_name
;
3744 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3745 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3751 * Now that we have verified that the snapshot is the latest,
3752 * rollback to the given snapshot.
3755 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3756 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3758 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3760 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3762 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3765 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3767 if (ZFS_IS_VOLUME(zhp
))
3768 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3770 zc
.zc_objset_type
= DMU_OST_ZFS
;
3773 * We rely on zfs_iter_children() to verify that there are no
3774 * newer snapshots for the given dataset. Therefore, we can
3775 * simply pass the name on to the ioctl() call. There is still
3776 * an unlikely race condition where the user has taken a
3777 * snapshot since we verified that this was the most recent.
3780 if ((err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_ROLLBACK
, &zc
)) != 0) {
3781 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3782 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3788 * For volumes, if the pre-rollback volsize matched the pre-
3789 * rollback reservation and the volsize has changed then set
3790 * the reservation property to the post-rollback volsize.
3791 * Make a new handle since the rollback closed the dataset.
3793 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3794 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3795 if ((err
= zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3800 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3801 if (old_volsize
!= new_volsize
)
3802 err
= zfs_prop_set_int(zhp
, resv_prop
,
3811 * Renames the given dataset.
3814 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
3815 boolean_t force_unmount
)
3818 zfs_cmd_t zc
= {"\0"};
3820 prop_changelist_t
*cl
= NULL
;
3821 zfs_handle_t
*zhrp
= NULL
;
3822 char *parentname
= NULL
;
3823 char parent
[ZFS_MAXNAMELEN
];
3824 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3827 /* if we have the same exact name, just return success */
3828 if (strcmp(zhp
->zfs_name
, target
) == 0)
3831 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3832 "cannot rename to '%s'"), target
);
3835 * Make sure the target name is valid
3837 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3838 if ((strchr(target
, '@') == NULL
) ||
3841 * Snapshot target name is abbreviated,
3842 * reconstruct full dataset name
3844 (void) strlcpy(parent
, zhp
->zfs_name
,
3846 delim
= strchr(parent
, '@');
3847 if (strchr(target
, '@') == NULL
)
3851 (void) strlcat(parent
, target
, sizeof (parent
));
3855 * Make sure we're renaming within the same dataset.
3857 delim
= strchr(target
, '@');
3858 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3859 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3860 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3861 "snapshots must be part of same "
3863 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3867 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3868 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3871 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3872 "recursive rename must be a snapshot"));
3873 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3876 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3877 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3879 /* validate parents */
3880 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3883 /* make sure we're in the same pool */
3884 verify((delim
= strchr(target
, '/')) != NULL
);
3885 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3886 zhp
->zfs_name
[delim
- target
] != '/') {
3887 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3888 "datasets must be within same pool"));
3889 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3892 /* new name cannot be a child of the current dataset name */
3893 if (is_descendant(zhp
->zfs_name
, target
)) {
3894 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3895 "New dataset name cannot be a descendant of "
3896 "current dataset name"));
3897 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3901 (void) snprintf(errbuf
, sizeof (errbuf
),
3902 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3904 if (getzoneid() == GLOBAL_ZONEID
&&
3905 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3906 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3907 "dataset is used in a non-global zone"));
3908 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3912 struct destroydata dd
;
3914 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3915 if (parentname
== NULL
) {
3919 delim
= strchr(parentname
, '@');
3921 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3927 dd
.snapname
= delim
+ 1;
3929 /* We remove any zvol links prior to renaming them */
3930 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3931 ret
= zfs_iter_filesystems(zhrp
, zfs_check_snap_cb
, &dd
);
3932 nvlist_free(dd
.nvl
);
3937 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3938 force_unmount
? MS_FORCE
: 0)) == NULL
)
3941 if (changelist_haszonedchild(cl
)) {
3942 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3943 "child dataset with inherited mountpoint is used "
3944 "in a non-global zone"));
3945 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3950 if ((ret
= changelist_prefix(cl
)) != 0)
3954 if (ZFS_IS_VOLUME(zhp
))
3955 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3957 zc
.zc_objset_type
= DMU_OST_ZFS
;
3959 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3960 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3962 zc
.zc_cookie
= recursive
;
3964 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3966 * if it was recursive, the one that actually failed will
3969 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3970 "cannot rename '%s'"), zc
.zc_name
);
3972 if (recursive
&& errno
== EEXIST
) {
3973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3974 "a child dataset already has a snapshot "
3975 "with the new name"));
3976 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3978 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3982 * On failure, we still want to remount any filesystems that
3983 * were previously mounted, so we don't alter the system state.
3986 struct createdata cd
;
3988 /* only create links for datasets that had existed */
3989 cd
.cd_snapname
= delim
+ 1;
3990 cd
.cd_ifexists
= B_TRUE
;
3991 (void) zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3994 (void) changelist_postfix(cl
);
3998 struct createdata cd
;
4000 /* only create links for datasets that had existed */
4001 cd
.cd_snapname
= strchr(target
, '@') + 1;
4002 cd
.cd_ifexists
= B_TRUE
;
4003 ret
= zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
4006 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4007 ret
= changelist_postfix(cl
);
4019 changelist_free(cl
);
4025 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
4026 * and wait briefly for udev to create the /dev link.
4029 zvol_create_link(libzfs_handle_t
*hdl
, const char *dataset
)
4031 return (zvol_create_link_common(hdl
, dataset
, B_FALSE
));
4035 zvol_create_link_common(libzfs_handle_t
*hdl
, const char *dataset
, int ifexists
)
4037 zfs_cmd_t zc
= {"\0"};
4038 char path
[MAXPATHLEN
];
4041 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4044 * Issue the appropriate ioctl.
4046 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CREATE_MINOR
, &zc
) != 0) {
4050 * Silently ignore the case where the link already
4051 * exists. This allows 'zfs volinit' to be run multiple
4052 * times without errors.
4058 * snapdev set to hidden :
4059 * device creation was not permitted (see zvol.c)
4060 * ignore error quietly
4066 * Dataset does not exist in the kernel. If we
4067 * don't care (see zfs_rename), then ignore the
4077 return (zfs_standard_error_fmt(hdl
, errno
,
4078 dgettext(TEXT_DOMAIN
, "cannot create device links "
4079 "for '%s'"), dataset
));
4084 * Wait for udev to create the device.
4086 (void) snprintf(path
, sizeof (path
), "%s/%s", ZVOL_DIR
, dataset
);
4087 error
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4089 (void) printf(gettext("%s may not be immediately "
4090 "available\n"), path
);
4096 * Remove a minor node for the given zvol and the associated /dev links.
4099 zvol_remove_link(libzfs_handle_t
*hdl
, const char *dataset
)
4101 zfs_cmd_t zc
= {"\0"};
4102 int timeout
= 3000; /* in milliseconds */
4106 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4109 * Due to concurrent updates by udev the device may be reported as
4110 * busy. In this case don't immediately fail. Instead briefly delay
4111 * and retry the ioctl() which is now likely to succeed. If unable
4112 * remove the link after timeout milliseconds return the failure.
4114 for (i
= 0; i
< timeout
; i
++) {
4115 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_REMOVE_MINOR
, &zc
);
4116 if (error
&& errno
== EBUSY
) {
4128 * Silently ignore the case where the link no longer
4129 * exists, so that 'zfs volfini' can be run multiple
4130 * times without errors.
4135 return (zfs_standard_error_fmt(hdl
, errno
,
4136 dgettext(TEXT_DOMAIN
, "cannot remove device "
4137 "links for '%s': %s"), dataset
, strerror(errno
)));
4145 zfs_get_user_props(zfs_handle_t
*zhp
)
4147 return (zhp
->zfs_user_props
);
4151 * This function is used by 'zfs list' to determine the exact set of columns to
4152 * display, and their maximum widths. This does two main things:
4154 * - If this is a list of all properties, then expand the list to include
4155 * all native properties, and set a flag so that for each dataset we look
4156 * for new unique user properties and add them to the list.
4158 * - For non fixed-width properties, keep track of the maximum width seen
4159 * so that we can size the column appropriately. If the user has
4160 * requested received property values, we also need to compute the width
4161 * of the RECEIVED column.
4164 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
)
4166 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4167 zprop_list_t
*entry
;
4168 zprop_list_t
**last
, **start
;
4169 nvlist_t
*userprops
, *propval
;
4172 char buf
[ZFS_MAXPROPLEN
];
4174 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4177 userprops
= zfs_get_user_props(zhp
);
4180 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4182 * Go through and add any user properties as necessary. We
4183 * start by incrementing our list pointer to the first
4184 * non-native property.
4187 while (*start
!= NULL
) {
4188 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4190 start
= &(*start
)->pl_next
;
4194 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4196 * See if we've already found this property in our list.
4198 for (last
= start
; *last
!= NULL
;
4199 last
= &(*last
)->pl_next
) {
4200 if (strcmp((*last
)->pl_user_prop
,
4201 nvpair_name(elem
)) == 0)
4205 if (*last
== NULL
) {
4206 if ((entry
= zfs_alloc(hdl
,
4207 sizeof (zprop_list_t
))) == NULL
||
4208 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4209 nvpair_name(elem
)))) == NULL
) {
4214 entry
->pl_prop
= ZPROP_INVAL
;
4215 entry
->pl_width
= strlen(nvpair_name(elem
));
4216 entry
->pl_all
= B_TRUE
;
4223 * Now go through and check the width of any non-fixed columns
4225 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4226 if (entry
->pl_fixed
)
4229 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4230 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4231 buf
, sizeof (buf
), NULL
, NULL
, 0, B_FALSE
) == 0) {
4232 if (strlen(buf
) > entry
->pl_width
)
4233 entry
->pl_width
= strlen(buf
);
4235 if (received
&& zfs_prop_get_recvd(zhp
,
4236 zfs_prop_to_name(entry
->pl_prop
),
4237 buf
, sizeof (buf
), B_FALSE
) == 0)
4238 if (strlen(buf
) > entry
->pl_recvd_width
)
4239 entry
->pl_recvd_width
= strlen(buf
);
4241 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4243 verify(nvlist_lookup_string(propval
,
4244 ZPROP_VALUE
, &strval
) == 0);
4245 if (strlen(strval
) > entry
->pl_width
)
4246 entry
->pl_width
= strlen(strval
);
4248 if (received
&& zfs_prop_get_recvd(zhp
,
4249 entry
->pl_user_prop
,
4250 buf
, sizeof (buf
), B_FALSE
) == 0)
4251 if (strlen(buf
) > entry
->pl_recvd_width
)
4252 entry
->pl_recvd_width
= strlen(buf
);
4260 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4265 * Keep a reference to the props-table against which we prune the
4268 zhp
->zfs_props_table
= props
;
4270 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4273 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4274 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4277 * User properties will result in ZPROP_INVAL, and since we
4278 * only know how to prune standard ZFS properties, we always
4279 * leave these in the list. This can also happen if we
4280 * encounter an unknown DSL property (when running older
4281 * software, for example).
4283 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4284 (void) nvlist_remove(zhp
->zfs_props
,
4285 nvpair_name(curr
), nvpair_type(curr
));
4291 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4292 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4294 zfs_cmd_t zc
= {"\0"};
4295 nvlist_t
*nvlist
= NULL
;
4298 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4299 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4300 zc
.zc_cookie
= (uint64_t)cmd
;
4302 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4303 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4304 (void) no_memory(hdl
);
4310 case ZFS_SMB_ACL_ADD
:
4311 case ZFS_SMB_ACL_REMOVE
:
4312 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4314 case ZFS_SMB_ACL_RENAME
:
4315 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4317 (void) no_memory(hdl
);
4320 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4322 (void) no_memory(hdl
);
4325 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4326 nvlist_free(nvlist
);
4330 case ZFS_SMB_ACL_PURGE
:
4335 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4337 nvlist_free(nvlist
);
4342 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4343 char *path
, char *resource
)
4345 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4350 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4351 char *path
, char *resource
)
4353 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4358 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4360 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4365 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4366 char *oldname
, char *newname
)
4368 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4373 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4374 zfs_userspace_cb_t func
, void *arg
)
4376 zfs_cmd_t zc
= {"\0"};
4377 zfs_useracct_t buf
[100];
4378 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4381 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4383 zc
.zc_objset_type
= type
;
4384 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4387 zfs_useracct_t
*zua
= buf
;
4389 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4390 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4391 char errbuf
[ZFS_MAXNAMELEN
+ 32];
4393 (void) snprintf(errbuf
, sizeof (errbuf
),
4394 dgettext(TEXT_DOMAIN
,
4395 "cannot get used/quota for %s"), zc
.zc_name
);
4396 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4398 if (zc
.zc_nvlist_dst_size
== 0)
4401 while (zc
.zc_nvlist_dst_size
> 0) {
4402 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4403 zua
->zu_space
)) != 0)
4406 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4415 const char *snapname
;
4417 boolean_t recursive
;
4421 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4423 struct holdarg
*ha
= arg
;
4425 char name
[ZFS_MAXNAMELEN
];
4428 (void) snprintf(name
, sizeof (name
),
4429 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4431 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
4433 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4438 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4444 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4445 boolean_t recursive
, boolean_t enoent_ok
, int cleanup_fd
)
4450 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4454 ha
.nvl
= fnvlist_alloc();
4455 ha
.snapname
= snapname
;
4457 ha
.recursive
= recursive
;
4458 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4460 if (nvlist_next_nvpair(ha
.nvl
, NULL
) == NULL
) {
4461 fnvlist_free(ha
.nvl
);
4464 (void) snprintf(errbuf
, sizeof (errbuf
),
4465 dgettext(TEXT_DOMAIN
,
4466 "cannot hold snapshot '%s@%s'"),
4467 zhp
->zfs_name
, snapname
);
4468 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4473 ret
= lzc_hold(ha
.nvl
, cleanup_fd
, &errors
);
4474 fnvlist_free(ha
.nvl
);
4479 if (nvlist_next_nvpair(errors
, NULL
) == NULL
) {
4480 /* no hold-specific errors */
4481 (void) snprintf(errbuf
, sizeof (errbuf
),
4482 dgettext(TEXT_DOMAIN
, "cannot hold"));
4485 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4486 "pool must be upgraded"));
4487 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4490 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4493 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4497 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4499 elem
= nvlist_next_nvpair(errors
, elem
)) {
4500 (void) snprintf(errbuf
, sizeof (errbuf
),
4501 dgettext(TEXT_DOMAIN
,
4502 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4503 switch (fnvpair_value_int32(elem
)) {
4506 * Temporary tags wind up having the ds object id
4507 * prepended. So even if we passed the length check
4508 * above, it's still possible for the tag to wind
4509 * up being slightly too long.
4511 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4514 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4517 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4524 (void) zfs_standard_error(hdl
,
4525 fnvpair_value_int32(elem
), errbuf
);
4529 fnvlist_free(errors
);
4535 const char *snapname
;
4537 boolean_t recursive
;
4541 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4543 struct holdarg
*ha
= arg
;
4545 char name
[ZFS_MAXNAMELEN
];
4548 (void) snprintf(name
, sizeof (name
),
4549 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4551 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
4553 nvlist_t
*holds
= fnvlist_alloc();
4554 fnvlist_add_boolean(holds
, ha
->tag
);
4555 fnvlist_add_nvlist(ha
->nvl
, name
, holds
);
4560 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4566 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4567 boolean_t recursive
)
4573 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4576 ha
.nvl
= fnvlist_alloc();
4577 ha
.snapname
= snapname
;
4579 ha
.recursive
= recursive
;
4580 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4582 if (nvlist_next_nvpair(ha
.nvl
, NULL
) == NULL
) {
4583 fnvlist_free(ha
.nvl
);
4585 (void) snprintf(errbuf
, sizeof (errbuf
),
4586 dgettext(TEXT_DOMAIN
,
4587 "cannot release hold from snapshot '%s@%s'"),
4588 zhp
->zfs_name
, snapname
);
4589 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4593 ret
= lzc_release(ha
.nvl
, &errors
);
4594 fnvlist_free(ha
.nvl
);
4599 if (nvlist_next_nvpair(errors
, NULL
) == NULL
) {
4600 /* no hold-specific errors */
4601 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4605 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4606 "pool must be upgraded"));
4607 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4610 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4614 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4616 elem
= nvlist_next_nvpair(errors
, elem
)) {
4617 (void) snprintf(errbuf
, sizeof (errbuf
),
4618 dgettext(TEXT_DOMAIN
,
4619 "cannot release hold from snapshot '%s'"),
4621 switch (fnvpair_value_int32(elem
)) {
4623 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4626 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4629 (void) zfs_standard_error_fmt(hdl
,
4630 fnvpair_value_int32(elem
), errbuf
);
4634 fnvlist_free(errors
);
4639 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4641 zfs_cmd_t zc
= {"\0"};
4642 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4648 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4649 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4653 nvbuf
= malloc(nvsz
);
4654 if (nvbuf
== NULL
) {
4655 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4659 zc
.zc_nvlist_dst_size
= nvsz
;
4660 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4662 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4664 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4665 (void) snprintf(errbuf
, sizeof (errbuf
),
4666 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4671 nvsz
= zc
.zc_nvlist_dst_size
;
4675 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4676 "pool must be upgraded"));
4677 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4680 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4683 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4686 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4691 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4693 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4694 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4696 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4706 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4708 zfs_cmd_t zc
= {"\0"};
4709 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4715 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4716 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4718 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4721 nvbuf
= malloc(nvsz
);
4723 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4726 zc
.zc_nvlist_src_size
= nvsz
;
4727 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4728 zc
.zc_perm_action
= un
;
4730 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4732 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4733 (void) snprintf(errbuf
, sizeof (errbuf
),
4734 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4738 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4739 "pool must be upgraded"));
4740 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4743 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4746 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4749 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4760 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4765 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4768 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4770 (void) snprintf(errbuf
, sizeof (errbuf
),
4771 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4775 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4776 "pool must be upgraded"));
4777 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4780 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4783 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4786 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4795 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4798 uint64_t nblocks
, volblocksize
;
4802 if (nvlist_lookup_string(props
,
4803 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4804 ncopies
= atoi(strval
);
4807 if (nvlist_lookup_uint64(props
,
4808 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4809 &volblocksize
) != 0)
4810 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4811 nblocks
= volsize
/volblocksize
;
4812 /* start with metadnode L0-L6 */
4814 /* calculate number of indirects */
4815 while (nblocks
> 1) {
4816 nblocks
+= DNODES_PER_LEVEL
- 1;
4817 nblocks
/= DNODES_PER_LEVEL
;
4820 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4823 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4824 * compressed, but in practice they compress down to about
4827 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;