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) 2010 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
39 #include <sys/mntent.h>
40 #include <sys/mount.h>
49 #include <directory.h>
50 #endif /* HAVE_IDMAP */
52 #include <sys/dnode.h>
57 #include "zfs_namecheck.h"
59 #include "libzfs_impl.h"
60 #include "zfs_deleg.h"
62 static int zvol_create_link_common(libzfs_handle_t
*, const char *, int);
63 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
64 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
67 * Given a single type (not a mask of types), return the type in a human
71 zfs_type_to_name(zfs_type_t type
)
74 case ZFS_TYPE_FILESYSTEM
:
75 return (dgettext(TEXT_DOMAIN
, "filesystem"));
76 case ZFS_TYPE_SNAPSHOT
:
77 return (dgettext(TEXT_DOMAIN
, "snapshot"));
79 return (dgettext(TEXT_DOMAIN
, "volume"));
88 * Validate a ZFS path. This is used even before trying to open the dataset, to
89 * provide a more meaningful error message. We call zfs_error_aux() to
90 * explain exactly why the name was not valid.
93 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
99 (void) zfs_prop_get_table();
100 if (dataset_namecheck(path
, &why
, &what
) != 0) {
103 case NAME_ERR_TOOLONG
:
104 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
105 "name is too long"));
108 case NAME_ERR_LEADING_SLASH
:
109 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
110 "leading slash in name"));
113 case NAME_ERR_EMPTY_COMPONENT
:
114 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
115 "empty component in name"));
118 case NAME_ERR_TRAILING_SLASH
:
119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
120 "trailing slash in name"));
123 case NAME_ERR_INVALCHAR
:
125 dgettext(TEXT_DOMAIN
, "invalid character "
126 "'%c' in name"), what
);
129 case NAME_ERR_MULTIPLE_AT
:
130 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
131 "multiple '@' delimiters in name"));
134 case NAME_ERR_NOLETTER
:
135 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
136 "pool doesn't begin with a letter"));
139 case NAME_ERR_RESERVED
:
140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
141 "name is reserved"));
144 case NAME_ERR_DISKLIKE
:
145 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
146 "reserved disk name"));
156 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
158 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
159 "snapshot delimiter '@' in filesystem name"));
163 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
165 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
166 "missing '@' delimiter in snapshot name"));
170 if (modifying
&& strchr(path
, '%') != NULL
) {
172 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
173 "invalid character %c in name"), '%');
181 zfs_name_valid(const char *name
, zfs_type_t type
)
183 if (type
== ZFS_TYPE_POOL
)
184 return (zpool_name_valid(NULL
, B_FALSE
, name
));
185 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
189 * This function takes the raw DSL properties, and filters out the user-defined
190 * properties into a separate nvlist.
193 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
195 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
200 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
201 (void) no_memory(hdl
);
206 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
207 if (!zfs_prop_user(nvpair_name(elem
)))
210 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
211 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
213 (void) no_memory(hdl
);
221 static zpool_handle_t
*
222 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
224 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
227 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
228 if (hdl
->libzfs_pool_handles
!= NULL
)
229 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
230 hdl
->libzfs_pool_handles
= zph
;
235 static zpool_handle_t
*
236 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
238 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
239 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
241 while ((zph
!= NULL
) &&
242 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
243 zph
= zph
->zpool_next
;
248 * Returns a handle to the pool that contains the provided dataset.
249 * If a handle to that pool already exists then that handle is returned.
250 * Otherwise, a new handle is created and added to the list of handles.
252 static zpool_handle_t
*
253 zpool_handle(zfs_handle_t
*zhp
)
259 len
= strcspn(zhp
->zfs_name
, "/@") + 1;
260 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
261 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
263 zph
= zpool_find_handle(zhp
, pool_name
, len
);
265 zph
= zpool_add_handle(zhp
, pool_name
);
272 zpool_free_handles(libzfs_handle_t
*hdl
)
274 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
276 while (zph
!= NULL
) {
277 next
= zph
->zpool_next
;
281 hdl
->libzfs_pool_handles
= NULL
;
285 * Utility function to gather stats (objset and zpl) for the given object.
288 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
290 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
292 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
294 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
295 if (errno
== ENOMEM
) {
296 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
307 * Utility function to get the received properties of the given object.
310 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
312 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
313 nvlist_t
*recvdprops
;
314 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
317 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
320 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
322 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
323 if (errno
== ENOMEM
) {
324 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
328 zcmd_free_nvlists(&zc
);
333 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
334 zcmd_free_nvlists(&zc
);
338 nvlist_free(zhp
->zfs_recvd_props
);
339 zhp
->zfs_recvd_props
= recvdprops
;
345 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
347 nvlist_t
*allprops
, *userprops
;
349 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
351 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
356 * XXX Why do we store the user props separately, in addition to
357 * storing them in zfs_props?
359 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
360 nvlist_free(allprops
);
364 nvlist_free(zhp
->zfs_props
);
365 nvlist_free(zhp
->zfs_user_props
);
367 zhp
->zfs_props
= allprops
;
368 zhp
->zfs_user_props
= userprops
;
374 get_stats(zfs_handle_t
*zhp
)
377 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
379 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
381 if (get_stats_ioctl(zhp
, &zc
) != 0)
383 else if (put_stats_zhdl(zhp
, &zc
) != 0)
385 zcmd_free_nvlists(&zc
);
390 * Refresh the properties currently stored in the handle.
393 zfs_refresh_properties(zfs_handle_t
*zhp
)
395 (void) get_stats(zhp
);
399 * Makes a handle from the given dataset name. Used by zfs_open() and
400 * zfs_iter_* to create child handles on the fly.
403 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
405 if (put_stats_zhdl(zhp
, zc
) != 0)
409 * We've managed to open the dataset and gather statistics. Determine
410 * the high-level type.
412 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
413 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
414 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
415 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
419 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
420 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
421 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
422 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
423 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
424 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
426 abort(); /* we should never see any other types */
428 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
435 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
437 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
439 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
445 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
446 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
450 if (get_stats_ioctl(zhp
, &zc
) == -1) {
451 zcmd_free_nvlists(&zc
);
455 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
459 zcmd_free_nvlists(&zc
);
463 static zfs_handle_t
*
464 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
466 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
472 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
473 if (make_dataset_handle_common(zhp
, zc
) == -1) {
481 * Opens the given snapshot, filesystem, or volume. The 'types'
482 * argument is a mask of acceptable types. The function will print an
483 * appropriate error message and return NULL if it can't be opened.
486 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
491 (void) snprintf(errbuf
, sizeof (errbuf
),
492 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
495 * Validate the name before we even try to open it.
497 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
498 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
499 "invalid dataset name"));
500 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
505 * Try to get stats for the dataset, which will tell us if it exists.
508 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
509 (void) zfs_standard_error(hdl
, errno
, errbuf
);
513 if (!(types
& zhp
->zfs_type
)) {
514 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
523 * Release a ZFS handle. Nothing to do but free the associated memory.
526 zfs_close(zfs_handle_t
*zhp
)
528 if (zhp
->zfs_mntopts
)
529 free(zhp
->zfs_mntopts
);
530 nvlist_free(zhp
->zfs_props
);
531 nvlist_free(zhp
->zfs_user_props
);
532 nvlist_free(zhp
->zfs_recvd_props
);
536 typedef struct mnttab_node
{
537 struct mnttab mtn_mt
;
542 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
544 const mnttab_node_t
*mtn1
= arg1
;
545 const mnttab_node_t
*mtn2
= arg2
;
548 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
552 return (rv
> 0 ? 1 : -1);
556 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
558 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
559 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
560 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
564 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
568 rewind(hdl
->libzfs_mnttab
);
569 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
572 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
574 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
575 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
576 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
577 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
578 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
579 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
584 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
589 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
590 free(mtn
->mtn_mt
.mnt_special
);
591 free(mtn
->mtn_mt
.mnt_mountp
);
592 free(mtn
->mtn_mt
.mnt_fstype
);
593 free(mtn
->mtn_mt
.mnt_mntopts
);
596 avl_destroy(&hdl
->libzfs_mnttab_cache
);
600 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
602 hdl
->libzfs_mnttab_enable
= enable
;
606 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
607 struct mnttab
*entry
)
612 if (!hdl
->libzfs_mnttab_enable
) {
613 struct mnttab srch
= { 0 };
615 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
616 libzfs_mnttab_fini(hdl
);
617 rewind(hdl
->libzfs_mnttab
);
618 srch
.mnt_special
= (char *)fsname
;
619 srch
.mnt_fstype
= MNTTYPE_ZFS
;
620 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
626 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
627 libzfs_mnttab_update(hdl
);
629 find
.mtn_mt
.mnt_special
= (char *)fsname
;
630 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
632 *entry
= mtn
->mtn_mt
;
639 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
640 const char *mountp
, const char *mntopts
)
644 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
646 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
647 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
648 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
649 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
650 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
651 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
655 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
660 find
.mtn_mt
.mnt_special
= (char *)fsname
;
661 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
662 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
663 free(ret
->mtn_mt
.mnt_special
);
664 free(ret
->mtn_mt
.mnt_mountp
);
665 free(ret
->mtn_mt
.mnt_fstype
);
666 free(ret
->mtn_mt
.mnt_mntopts
);
672 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
674 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
676 if (zpool_handle
== NULL
)
679 *spa_version
= zpool_get_prop_int(zpool_handle
,
680 ZPOOL_PROP_VERSION
, NULL
);
685 * The choice of reservation property depends on the SPA version.
688 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
692 if (zfs_spa_version(zhp
, &spa_version
) < 0)
695 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
696 *resv_prop
= ZFS_PROP_REFRESERVATION
;
698 *resv_prop
= ZFS_PROP_RESERVATION
;
704 * Given an nvlist of properties to set, validates that they are correct, and
705 * parses any numeric properties (index, boolean, etc) if they are specified as
709 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
710 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
717 int chosen_normal
= -1;
720 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
721 (void) no_memory(hdl
);
726 * Make sure this property is valid and applies to this type.
730 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
731 const char *propname
= nvpair_name(elem
);
733 prop
= zfs_name_to_prop(propname
);
734 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
736 * This is a user property: make sure it's a
737 * string, and that it's less than ZAP_MAXNAMELEN.
739 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
740 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
741 "'%s' must be a string"), propname
);
742 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
746 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
747 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
748 "property name '%s' is too long"),
750 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
754 (void) nvpair_value_string(elem
, &strval
);
755 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
756 (void) no_memory(hdl
);
763 * Currently, only user properties can be modified on
766 if (type
== ZFS_TYPE_SNAPSHOT
) {
767 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
768 "this property can not be modified for snapshots"));
769 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
773 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
774 zfs_userquota_prop_t uqtype
;
775 char newpropname
[128];
780 if (userquota_propname_decode(propname
, zoned
,
781 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
783 dgettext(TEXT_DOMAIN
,
784 "'%s' has an invalid user/group name"),
786 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
790 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
791 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
793 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
795 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
800 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
801 (void) nvpair_value_string(elem
, &strval
);
802 if (strcmp(strval
, "none") == 0) {
804 } else if (zfs_nicestrtonum(hdl
,
805 strval
, &intval
) != 0) {
806 (void) zfs_error(hdl
,
807 EZFS_BADPROP
, errbuf
);
810 } else if (nvpair_type(elem
) ==
812 (void) nvpair_value_uint64(elem
, &intval
);
814 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
815 "use 'none' to disable "
816 "userquota/groupquota"));
820 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
821 "'%s' must be a number"), propname
);
822 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
827 * Encode the prop name as
828 * userquota@<hex-rid>-domain, to make it easy
829 * for the kernel to decode.
831 (void) snprintf(newpropname
, sizeof (newpropname
),
832 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
833 (longlong_t
)rid
, domain
);
837 if (nvlist_add_uint64_array(ret
, newpropname
,
839 (void) no_memory(hdl
);
845 if (prop
== ZPROP_INVAL
) {
846 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
847 "invalid property '%s'"), propname
);
848 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
852 if (!zfs_prop_valid_for_type(prop
, type
)) {
854 dgettext(TEXT_DOMAIN
, "'%s' does not "
855 "apply to datasets of this type"), propname
);
856 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
860 if (zfs_prop_readonly(prop
) &&
861 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
863 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
865 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
869 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
870 &strval
, &intval
, errbuf
) != 0)
874 * Perform some additional checks for specific properties.
877 case ZFS_PROP_VERSION
:
883 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
884 if (intval
< version
) {
885 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
886 "Can not downgrade; already at version %u"),
888 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
894 case ZFS_PROP_RECORDSIZE
:
895 case ZFS_PROP_VOLBLOCKSIZE
:
896 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
897 if (intval
< SPA_MINBLOCKSIZE
||
898 intval
> SPA_MAXBLOCKSIZE
|| !ISP2(intval
)) {
899 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
900 "'%s' must be power of 2 from %u "
902 (uint_t
)SPA_MINBLOCKSIZE
,
903 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
904 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
909 case ZFS_PROP_MLSLABEL
:
913 * Verify the mlslabel string and convert to
914 * internal hex label string.
918 char *hex
= NULL
; /* internal label string */
920 /* Default value is already OK. */
921 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
924 /* Verify the label can be converted to binary form */
925 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
926 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
927 L_NO_CORRECTION
, NULL
) == -1)) {
931 /* Now translate to hex internal label string */
932 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
938 m_label_free(new_sl
);
940 /* If string is already in internal form, we're done. */
941 if (strcmp(strval
, hex
) == 0) {
946 /* Replace the label string with the internal form. */
947 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
949 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
956 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
957 "invalid mlslabel '%s'"), strval
);
958 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
959 m_label_free(new_sl
); /* OK if null */
962 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
963 "mlslabels are unsupported"));
964 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
966 #endif /* HAVE_MLSLABEL */
969 case ZFS_PROP_MOUNTPOINT
:
973 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
974 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
977 if (mountpoint_namecheck(strval
, &why
)) {
979 case NAME_ERR_LEADING_SLASH
:
981 dgettext(TEXT_DOMAIN
,
982 "'%s' must be an absolute path, "
983 "'none', or 'legacy'"), propname
);
985 case NAME_ERR_TOOLONG
:
987 dgettext(TEXT_DOMAIN
,
988 "component of '%s' is too long"),
994 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1001 case ZFS_PROP_SHARESMB
:
1002 case ZFS_PROP_SHARENFS
:
1004 * For the mountpoint and sharenfs or sharesmb
1005 * properties, check if it can be set in a
1006 * global/non-global zone based on
1007 * the zoned property value:
1009 * global zone non-global zone
1010 * --------------------------------------------------
1011 * zoned=on mountpoint (no) mountpoint (yes)
1012 * sharenfs (no) sharenfs (no)
1013 * sharesmb (no) sharesmb (no)
1015 * zoned=off mountpoint (yes) N/A
1020 if (getzoneid() == GLOBAL_ZONEID
) {
1021 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1022 "'%s' cannot be set on "
1023 "dataset in a non-global zone"),
1025 (void) zfs_error(hdl
, EZFS_ZONED
,
1028 } else if (prop
== ZFS_PROP_SHARENFS
||
1029 prop
== ZFS_PROP_SHARESMB
) {
1030 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1031 "'%s' cannot be set in "
1032 "a non-global zone"), propname
);
1033 (void) zfs_error(hdl
, EZFS_ZONED
,
1037 } else if (getzoneid() != GLOBAL_ZONEID
) {
1039 * If zoned property is 'off', this must be in
1040 * a global zone. If not, something is wrong.
1042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1043 "'%s' cannot be set while dataset "
1044 "'zoned' property is set"), propname
);
1045 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1050 * At this point, it is legitimate to set the
1051 * property. Now we want to make sure that the
1052 * property value is valid if it is sharenfs.
1054 if ((prop
== ZFS_PROP_SHARENFS
||
1055 prop
== ZFS_PROP_SHARESMB
) &&
1056 strcmp(strval
, "on") != 0 &&
1057 strcmp(strval
, "off") != 0) {
1058 zfs_share_proto_t proto
;
1060 if (prop
== ZFS_PROP_SHARESMB
)
1066 * Must be an valid sharing protocol
1067 * option string so init the libshare
1068 * in order to enable the parser and
1069 * then parse the options. We use the
1070 * control API since we don't care about
1071 * the current configuration and don't
1072 * want the overhead of loading it
1073 * until we actually do something.
1076 if (zfs_init_libshare(hdl
,
1077 SA_INIT_CONTROL_API
) != SA_OK
) {
1079 * An error occurred so we can't do
1082 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1083 "'%s' cannot be set: problem "
1084 "in share initialization"),
1086 (void) zfs_error(hdl
, EZFS_BADPROP
,
1091 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1093 * There was an error in parsing so
1094 * deal with it by issuing an error
1095 * message and leaving after
1096 * uninitializing the the libshare
1099 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1100 "'%s' cannot be set to invalid "
1101 "options"), propname
);
1102 (void) zfs_error(hdl
, EZFS_BADPROP
,
1104 zfs_uninit_libshare(hdl
);
1107 zfs_uninit_libshare(hdl
);
1111 case ZFS_PROP_UTF8ONLY
:
1112 chosen_utf
= (int)intval
;
1114 case ZFS_PROP_NORMALIZE
:
1115 chosen_normal
= (int)intval
;
1122 * For changes to existing volumes, we have some additional
1123 * checks to enforce.
1125 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1126 uint64_t volsize
= zfs_prop_get_int(zhp
,
1128 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1129 ZFS_PROP_VOLBLOCKSIZE
);
1133 case ZFS_PROP_RESERVATION
:
1134 case ZFS_PROP_REFRESERVATION
:
1135 if (intval
> volsize
) {
1136 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1137 "'%s' is greater than current "
1138 "volume size"), propname
);
1139 (void) zfs_error(hdl
, EZFS_BADPROP
,
1145 case ZFS_PROP_VOLSIZE
:
1146 if (intval
% blocksize
!= 0) {
1147 zfs_nicenum(blocksize
, buf
,
1149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1150 "'%s' must be a multiple of "
1151 "volume block size (%s)"),
1153 (void) zfs_error(hdl
, EZFS_BADPROP
,
1159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1160 "'%s' cannot be zero"),
1162 (void) zfs_error(hdl
, EZFS_BADPROP
,
1174 * If normalization was chosen, but no UTF8 choice was made,
1175 * enforce rejection of non-UTF8 names.
1177 * If normalization was chosen, but rejecting non-UTF8 names
1178 * was explicitly not chosen, it is an error.
1180 if (chosen_normal
> 0 && chosen_utf
< 0) {
1181 if (nvlist_add_uint64(ret
,
1182 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1183 (void) no_memory(hdl
);
1186 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1187 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1188 "'%s' must be set 'on' if normalization chosen"),
1189 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1190 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1201 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1203 uint64_t old_volsize
;
1204 uint64_t new_volsize
;
1205 uint64_t old_reservation
;
1206 uint64_t new_reservation
;
1207 zfs_prop_t resv_prop
;
1210 * If this is an existing volume, and someone is setting the volsize,
1211 * make sure that it matches the reservation, or add it if necessary.
1213 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1214 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1216 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1217 if ((zvol_volsize_to_reservation(old_volsize
, zhp
->zfs_props
) !=
1218 old_reservation
) || nvlist_lookup_uint64(nvl
,
1219 zfs_prop_to_name(resv_prop
), &new_reservation
) != ENOENT
) {
1222 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1225 new_reservation
= zvol_volsize_to_reservation(new_volsize
,
1227 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1228 new_reservation
) != 0) {
1229 (void) no_memory(zhp
->zfs_hdl
);
1236 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1243 * For quotas and reservations, ENOSPC indicates
1244 * something different; setting a quota or reservation
1245 * doesn't use any disk space.
1248 case ZFS_PROP_QUOTA
:
1249 case ZFS_PROP_REFQUOTA
:
1250 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1251 "size is less than current used or "
1253 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1256 case ZFS_PROP_RESERVATION
:
1257 case ZFS_PROP_REFRESERVATION
:
1258 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1259 "size is greater than available space"));
1260 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1264 (void) zfs_standard_error(hdl
, err
, errbuf
);
1270 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1274 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1278 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1279 "pool and or dataset must be upgraded to set this "
1280 "property or value"));
1281 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1285 if (prop
== ZFS_PROP_COMPRESSION
) {
1286 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1287 "property setting is not allowed on "
1288 "bootable datasets"));
1289 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1291 (void) zfs_standard_error(hdl
, err
, errbuf
);
1296 if (prop
== ZPROP_INVAL
) {
1297 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1299 (void) zfs_standard_error(hdl
, err
, errbuf
);
1305 * This platform can't address a volume this big.
1308 if (prop
== ZFS_PROP_VOLSIZE
) {
1309 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1315 (void) zfs_standard_error(hdl
, err
, errbuf
);
1320 zfs_is_namespace_prop(zfs_prop_t prop
)
1324 case ZFS_PROP_ATIME
:
1325 case ZFS_PROP_DEVICES
:
1327 case ZFS_PROP_SETUID
:
1328 case ZFS_PROP_READONLY
:
1329 case ZFS_PROP_XATTR
:
1330 case ZFS_PROP_NBMAND
:
1339 * Given a property name and value, set the property for the given dataset.
1342 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1344 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1346 prop_changelist_t
*cl
= NULL
;
1348 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1349 nvlist_t
*nvl
= NULL
, *realprops
;
1351 boolean_t do_prefix
;
1355 (void) snprintf(errbuf
, sizeof (errbuf
),
1356 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1359 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1360 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1361 (void) no_memory(hdl
);
1365 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1366 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1372 prop
= zfs_name_to_prop(propname
);
1374 if (prop
== ZFS_PROP_VOLSIZE
) {
1375 if ((added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1)
1379 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1382 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1383 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1384 "child dataset with inherited mountpoint is used "
1385 "in a non-global zone"));
1386 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1391 * If the dataset's canmount property is being set to noauto,
1392 * then we want to prevent unmounting & remounting it.
1394 do_prefix
= !((prop
== ZFS_PROP_CANMOUNT
) &&
1395 (zprop_string_to_index(prop
, propval
, &idx
,
1396 ZFS_TYPE_DATASET
) == 0) && (idx
== ZFS_CANMOUNT_NOAUTO
));
1398 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1402 * Execute the corresponding ioctl() to set this property.
1404 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1406 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1409 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1412 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1413 if (added_resv
&& errno
== ENOSPC
) {
1414 /* clean up the volsize property we tried to set */
1415 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1418 zcmd_free_nvlists(&zc
);
1419 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1421 if (nvlist_add_uint64(nvl
,
1422 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1425 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1427 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1431 ret
= changelist_postfix(cl
);
1435 * Refresh the statistics so the new property
1436 * value is reflected.
1438 (void) get_stats(zhp
);
1441 * Remount the filesystem to propagate the change
1442 * if one of the options handled by the generic
1443 * Linux namespace layer has been modified.
1445 if (zfs_is_namespace_prop(prop
) &&
1446 zfs_is_mounted(zhp
, NULL
))
1447 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1453 zcmd_free_nvlists(&zc
);
1455 changelist_free(cl
);
1460 * Given a property, inherit the value from the parent dataset, or if received
1461 * is TRUE, revert to the received value, if any.
1464 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1466 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1468 prop_changelist_t
*cl
;
1469 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1473 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1474 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1476 zc
.zc_cookie
= received
;
1477 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1479 * For user properties, the amount of work we have to do is very
1480 * small, so just do it here.
1482 if (!zfs_prop_user(propname
)) {
1483 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1484 "invalid property"));
1485 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1488 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1489 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1491 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1492 return (zfs_standard_error(hdl
, errno
, errbuf
));
1498 * Verify that this property is inheritable.
1500 if (zfs_prop_readonly(prop
))
1501 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1503 if (!zfs_prop_inheritable(prop
) && !received
)
1504 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1507 * Check to see if the value applies to this type
1509 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1510 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1513 * Normalize the name, to get rid of shorthand abbreviations.
1515 propname
= zfs_prop_to_name(prop
);
1516 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1517 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1519 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1520 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1521 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1522 "dataset is used in a non-global zone"));
1523 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1527 * Determine datasets which will be affected by this change, if any.
1529 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1532 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1533 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1534 "child dataset with inherited mountpoint is used "
1535 "in a non-global zone"));
1536 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1540 if ((ret
= changelist_prefix(cl
)) != 0)
1543 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1544 return (zfs_standard_error(hdl
, errno
, errbuf
));
1547 if ((ret
= changelist_postfix(cl
)) != 0)
1551 * Refresh the statistics so the new property is reflected.
1553 (void) get_stats(zhp
);
1557 changelist_free(cl
);
1562 * True DSL properties are stored in an nvlist. The following two functions
1563 * extract them appropriately.
1566 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1572 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1573 zfs_prop_to_name(prop
), &nv
) == 0) {
1574 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1575 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1577 verify(!zhp
->zfs_props_table
||
1578 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1579 value
= zfs_prop_default_numeric(prop
);
1587 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1593 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1594 zfs_prop_to_name(prop
), &nv
) == 0) {
1595 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
1596 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1598 verify(!zhp
->zfs_props_table
||
1599 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1600 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
1609 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1611 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1615 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1617 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1618 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1622 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1624 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1629 * Internal function for getting a numeric property. Both zfs_prop_get() and
1630 * zfs_prop_get_int() are built using this interface.
1632 * Certain properties can be overridden using 'mount -o'. In this case, scan
1633 * the contents of the /etc/mtab entry, searching for the appropriate options.
1634 * If they differ from the on-disk values, report the current values and mark
1635 * the source "temporary".
1638 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1639 char **source
, uint64_t *val
)
1641 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1642 nvlist_t
*zplprops
= NULL
;
1644 char *mntopt_on
= NULL
;
1645 char *mntopt_off
= NULL
;
1646 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1651 case ZFS_PROP_ATIME
:
1652 mntopt_on
= MNTOPT_ATIME
;
1653 mntopt_off
= MNTOPT_NOATIME
;
1656 case ZFS_PROP_DEVICES
:
1657 mntopt_on
= MNTOPT_DEVICES
;
1658 mntopt_off
= MNTOPT_NODEVICES
;
1662 mntopt_on
= MNTOPT_EXEC
;
1663 mntopt_off
= MNTOPT_NOEXEC
;
1666 case ZFS_PROP_READONLY
:
1667 mntopt_on
= MNTOPT_RO
;
1668 mntopt_off
= MNTOPT_RW
;
1671 case ZFS_PROP_SETUID
:
1672 mntopt_on
= MNTOPT_SETUID
;
1673 mntopt_off
= MNTOPT_NOSETUID
;
1676 case ZFS_PROP_XATTR
:
1677 mntopt_on
= MNTOPT_XATTR
;
1678 mntopt_off
= MNTOPT_NOXATTR
;
1681 case ZFS_PROP_NBMAND
:
1682 mntopt_on
= MNTOPT_NBMAND
;
1683 mntopt_off
= MNTOPT_NONBMAND
;
1690 * Because looking up the mount options is potentially expensive
1691 * (iterating over all of /etc/mtab), we defer its calculation until
1692 * we're looking up a property which requires its presence.
1694 if (!zhp
->zfs_mntcheck
&&
1695 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
1696 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1697 struct mnttab entry
;
1699 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
1700 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
1702 if (zhp
->zfs_mntopts
== NULL
)
1706 zhp
->zfs_mntcheck
= B_TRUE
;
1709 if (zhp
->zfs_mntopts
== NULL
)
1710 mnt
.mnt_mntopts
= "";
1712 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
1715 case ZFS_PROP_ATIME
:
1716 case ZFS_PROP_DEVICES
:
1718 case ZFS_PROP_READONLY
:
1719 case ZFS_PROP_SETUID
:
1720 case ZFS_PROP_XATTR
:
1721 case ZFS_PROP_NBMAND
:
1722 *val
= getprop_uint64(zhp
, prop
, source
);
1727 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
1730 *src
= ZPROP_SRC_TEMPORARY
;
1731 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
1734 *src
= ZPROP_SRC_TEMPORARY
;
1738 case ZFS_PROP_CANMOUNT
:
1739 case ZFS_PROP_VOLSIZE
:
1740 case ZFS_PROP_QUOTA
:
1741 case ZFS_PROP_REFQUOTA
:
1742 case ZFS_PROP_RESERVATION
:
1743 case ZFS_PROP_REFRESERVATION
:
1744 *val
= getprop_uint64(zhp
, prop
, source
);
1746 if (*source
== NULL
) {
1747 /* not default, must be local */
1748 *source
= zhp
->zfs_name
;
1752 case ZFS_PROP_MOUNTED
:
1753 *val
= (zhp
->zfs_mntopts
!= NULL
);
1756 case ZFS_PROP_NUMCLONES
:
1757 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
1760 case ZFS_PROP_VERSION
:
1761 case ZFS_PROP_NORMALIZE
:
1762 case ZFS_PROP_UTF8ONLY
:
1764 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
1765 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1767 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1768 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
1769 zcmd_free_nvlists(&zc
);
1772 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
1773 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
1775 zcmd_free_nvlists(&zc
);
1779 nvlist_free(zplprops
);
1780 zcmd_free_nvlists(&zc
);
1784 switch (zfs_prop_get_type(prop
)) {
1785 case PROP_TYPE_NUMBER
:
1786 case PROP_TYPE_INDEX
:
1787 *val
= getprop_uint64(zhp
, prop
, source
);
1789 * If we tried to use a default value for a
1790 * readonly property, it means that it was not
1793 if (zfs_prop_readonly(prop
) &&
1794 *source
!= NULL
&& (*source
)[0] == '\0') {
1799 case PROP_TYPE_STRING
:
1801 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1802 "cannot get non-numeric property"));
1803 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
1804 dgettext(TEXT_DOMAIN
, "internal error")));
1812 * Calculate the source type, given the raw source string.
1815 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
1816 char *statbuf
, size_t statlen
)
1818 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
1821 if (source
== NULL
) {
1822 *srctype
= ZPROP_SRC_NONE
;
1823 } else if (source
[0] == '\0') {
1824 *srctype
= ZPROP_SRC_DEFAULT
;
1825 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
1826 *srctype
= ZPROP_SRC_RECEIVED
;
1828 if (strcmp(source
, zhp
->zfs_name
) == 0) {
1829 *srctype
= ZPROP_SRC_LOCAL
;
1831 (void) strlcpy(statbuf
, source
, statlen
);
1832 *srctype
= ZPROP_SRC_INHERITED
;
1839 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
1840 size_t proplen
, boolean_t literal
)
1845 if (zhp
->zfs_recvd_props
== NULL
)
1846 if (get_recvd_props_ioctl(zhp
) != 0)
1849 prop
= zfs_name_to_prop(propname
);
1851 if (prop
!= ZPROP_INVAL
) {
1853 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
1855 zfs_set_recvd_props_mode(zhp
, &cookie
);
1856 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
1857 NULL
, NULL
, 0, literal
);
1858 zfs_unset_recvd_props_mode(zhp
, &cookie
);
1859 } else if (zfs_prop_userquota(propname
)) {
1864 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
1865 propname
, &propval
) != 0)
1867 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
1869 (void) strlcpy(propbuf
, recvdval
, proplen
);
1872 return (err
== 0 ? 0 : -1);
1876 * Retrieve a property from the given object. If 'literal' is specified, then
1877 * numbers are left as exact values. Otherwise, numbers are converted to a
1878 * human-readable form.
1880 * Returns 0 on success, or -1 on error.
1883 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
1884 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
1886 char *source
= NULL
;
1890 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1893 * Check to see if this property applies to our object
1895 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1898 if (received
&& zfs_prop_readonly(prop
))
1902 *src
= ZPROP_SRC_NONE
;
1905 case ZFS_PROP_CREATION
:
1907 * 'creation' is a time_t stored in the statistics. We convert
1908 * this into a string unless 'literal' is specified.
1911 val
= getprop_uint64(zhp
, prop
, &source
);
1912 time_t time
= (time_t)val
;
1916 localtime_r(&time
, &t
) == NULL
||
1917 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
1919 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
) val
);
1923 case ZFS_PROP_MOUNTPOINT
:
1925 * Getting the precise mountpoint can be tricky.
1927 * - for 'none' or 'legacy', return those values.
1928 * - for inherited mountpoints, we want to take everything
1929 * after our ancestor and append it to the inherited value.
1931 * If the pool has an alternate root, we want to prepend that
1932 * root to any values we return.
1935 str
= getprop_string(zhp
, prop
, &source
);
1937 if (str
[0] == '/') {
1938 char buf
[MAXPATHLEN
];
1940 const char *relpath
;
1943 * If we inherit the mountpoint, even from a dataset
1944 * with a received value, the source will be the path of
1945 * the dataset we inherit from. If source is
1946 * ZPROP_SOURCE_VAL_RECVD, the received value is not
1949 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
1952 relpath
= zhp
->zfs_name
+ strlen(source
);
1953 if (relpath
[0] == '/')
1957 if ((zpool_get_prop(zhp
->zpool_hdl
,
1958 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
1959 (strcmp(root
, "-") == 0))
1962 * Special case an alternate root of '/'. This will
1963 * avoid having multiple leading slashes in the
1966 if (strcmp(root
, "/") == 0)
1970 * If the mountpoint is '/' then skip over this
1971 * if we are obtaining either an alternate root or
1972 * an inherited mountpoint.
1974 if (str
[1] == '\0' && (root
[0] != '\0' ||
1975 relpath
[0] != '\0'))
1978 if (relpath
[0] == '\0')
1979 (void) snprintf(propbuf
, proplen
, "%s%s",
1982 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
1983 root
, str
, relpath
[0] == '@' ? "" : "/",
1986 /* 'legacy' or 'none' */
1987 (void) strlcpy(propbuf
, str
, proplen
);
1992 case ZFS_PROP_ORIGIN
:
1993 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
1996 * If there is no parent at all, return failure to indicate that
1997 * it doesn't apply to this dataset.
1999 if (propbuf
[0] == '\0')
2003 case ZFS_PROP_QUOTA
:
2004 case ZFS_PROP_REFQUOTA
:
2005 case ZFS_PROP_RESERVATION
:
2006 case ZFS_PROP_REFRESERVATION
:
2008 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2012 * If quota or reservation is 0, we translate this into 'none'
2013 * (unless literal is set), and indicate that it's the default
2014 * value. Otherwise, we print the number nicely and indicate
2015 * that its set locally.
2019 (void) strlcpy(propbuf
, "0", proplen
);
2021 (void) strlcpy(propbuf
, "none", proplen
);
2024 (void) snprintf(propbuf
, proplen
, "%llu",
2027 zfs_nicenum(val
, propbuf
, proplen
);
2031 case ZFS_PROP_REFRATIO
:
2032 case ZFS_PROP_COMPRESSRATIO
:
2033 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2035 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2036 (u_longlong_t
)(val
/ 100),
2037 (u_longlong_t
)(val
% 100));
2041 switch (zhp
->zfs_type
) {
2042 case ZFS_TYPE_FILESYSTEM
:
2045 case ZFS_TYPE_VOLUME
:
2048 case ZFS_TYPE_SNAPSHOT
:
2054 (void) snprintf(propbuf
, proplen
, "%s", str
);
2057 case ZFS_PROP_MOUNTED
:
2059 * The 'mounted' property is a pseudo-property that described
2060 * whether the filesystem is currently mounted. Even though
2061 * it's a boolean value, the typical values of "on" and "off"
2062 * don't make sense, so we translate to "yes" and "no".
2064 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2065 src
, &source
, &val
) != 0)
2068 (void) strlcpy(propbuf
, "yes", proplen
);
2070 (void) strlcpy(propbuf
, "no", proplen
);
2075 * The 'name' property is a pseudo-property derived from the
2076 * dataset name. It is presented as a real property to simplify
2079 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2082 case ZFS_PROP_MLSLABEL
:
2084 #ifdef HAVE_MLSLABEL
2085 m_label_t
*new_sl
= NULL
;
2086 char *ascii
= NULL
; /* human readable label */
2088 (void) strlcpy(propbuf
,
2089 getprop_string(zhp
, prop
, &source
), proplen
);
2091 if (literal
|| (strcasecmp(propbuf
,
2092 ZFS_MLSLABEL_DEFAULT
) == 0))
2096 * Try to translate the internal hex string to
2097 * human-readable output. If there are any
2098 * problems just use the hex string.
2101 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2102 L_NO_CORRECTION
, NULL
) == -1) {
2103 m_label_free(new_sl
);
2107 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2111 m_label_free(new_sl
);
2114 m_label_free(new_sl
);
2116 (void) strlcpy(propbuf
, ascii
, proplen
);
2119 (void) strlcpy(propbuf
,
2120 getprop_string(zhp
, prop
, &source
), proplen
);
2121 #endif /* HAVE_MLSLABEL */
2126 switch (zfs_prop_get_type(prop
)) {
2127 case PROP_TYPE_NUMBER
:
2128 if (get_numeric_property(zhp
, prop
, src
,
2129 &source
, &val
) != 0)
2132 (void) snprintf(propbuf
, proplen
, "%llu",
2135 zfs_nicenum(val
, propbuf
, proplen
);
2138 case PROP_TYPE_STRING
:
2139 (void) strlcpy(propbuf
,
2140 getprop_string(zhp
, prop
, &source
), proplen
);
2143 case PROP_TYPE_INDEX
:
2144 if (get_numeric_property(zhp
, prop
, src
,
2145 &source
, &val
) != 0)
2147 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2149 (void) strlcpy(propbuf
, strval
, proplen
);
2157 get_source(zhp
, src
, source
, statbuf
, statlen
);
2163 * Utility function to get the given numeric property. Does no validation that
2164 * the given property is the appropriate type; should only be used with
2165 * hard-coded property types.
2168 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2173 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2179 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2183 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2184 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2188 * Similar to zfs_prop_get(), but returns the value as an integer.
2191 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2192 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2197 * Check to see if this property applies to our object
2199 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2200 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2201 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2202 zfs_prop_to_name(prop
)));
2206 *src
= ZPROP_SRC_NONE
;
2208 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2211 get_source(zhp
, src
, source
, statbuf
, statlen
);
2218 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2219 char **domainp
, idmap_rid_t
*ridp
)
2221 idmap_get_handle_t
*get_hdl
= NULL
;
2225 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2229 err
= idmap_get_sidbyuid(get_hdl
, id
,
2230 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2232 err
= idmap_get_sidbygid(get_hdl
, id
,
2233 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2235 if (err
== IDMAP_SUCCESS
&&
2236 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2237 status
== IDMAP_SUCCESS
)
2243 idmap_get_destroy(get_hdl
);
2246 #endif /* HAVE_IDMAP */
2249 * convert the propname into parameters needed by kernel
2250 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2251 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2254 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2255 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2257 zfs_userquota_prop_t type
;
2259 char *numericsid
= NULL
;
2264 /* Figure out the property type ({user|group}{quota|space}) */
2265 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2266 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2267 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2270 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2274 isuser
= (type
== ZFS_PROP_USERQUOTA
||
2275 type
== ZFS_PROP_USERUSED
);
2277 cp
= strchr(propname
, '@') + 1;
2279 if (strchr(cp
, '@')) {
2282 * It's a SID name (eg "user@domain") that needs to be
2283 * turned into S-1-domainID-RID.
2285 directory_error_t e
;
2286 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2289 e
= directory_sid_from_user_name(NULL
,
2292 e
= directory_sid_from_group_name(NULL
,
2296 directory_error_free(e
);
2299 if (numericsid
== NULL
)
2302 /* will be further decoded below */
2305 #endif /* HAVE_IDMAP */
2308 if (strncmp(cp
, "S-1-", 4) == 0) {
2309 /* It's a numeric SID (eg "S-1-234-567-89") */
2310 (void) strlcpy(domain
, cp
, domainlen
);
2311 cp
= strrchr(domain
, '-');
2316 *ridp
= strtoull(cp
, &end
, 10);
2321 if (errno
!= 0 || *end
!= '\0')
2323 } else if (!isdigit(*cp
)) {
2325 * It's a user/group name (eg "user") that needs to be
2326 * turned into a uid/gid
2328 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2345 /* It's a user/group ID (eg "12345"). */
2346 uid_t id
= strtoul(cp
, &end
, 10);
2353 /* It's an ephemeral ID. */
2354 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2355 &mapdomain
, &rid
) != 0)
2357 (void) strlcpy(domain
, mapdomain
, domainlen
);
2364 #endif /* HAVE_IDMAP */
2367 ASSERT3P(numericsid
, ==, NULL
);
2372 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2373 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2376 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2378 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2380 err
= userquota_propname_decode(propname
,
2381 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2382 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2383 zc
.zc_objset_type
= *typep
;
2387 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2391 *propvalue
= zc
.zc_cookie
;
2396 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2397 uint64_t *propvalue
)
2399 zfs_userquota_prop_t type
;
2401 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2406 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2407 char *propbuf
, int proplen
, boolean_t literal
)
2411 zfs_userquota_prop_t type
;
2413 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2420 (void) snprintf(propbuf
, proplen
, "%llu",
2421 (u_longlong_t
)propvalue
);
2422 } else if (propvalue
== 0 &&
2423 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2424 (void) strlcpy(propbuf
, "none", proplen
);
2426 zfs_nicenum(propvalue
, propbuf
, proplen
);
2432 * Returns the name of the given zfs handle.
2435 zfs_get_name(const zfs_handle_t
*zhp
)
2437 return (zhp
->zfs_name
);
2441 * Returns the type of the given zfs handle.
2444 zfs_get_type(const zfs_handle_t
*zhp
)
2446 return (zhp
->zfs_type
);
2450 zfs_do_list_ioctl(zfs_handle_t
*zhp
, int arg
, zfs_cmd_t
*zc
)
2453 uint64_t orig_cookie
;
2455 orig_cookie
= zc
->zc_cookie
;
2457 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
2458 rc
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, arg
, zc
);
2463 /* expand nvlist memory and try again */
2464 if (zcmd_expand_dst_nvlist(zhp
->zfs_hdl
, zc
) != 0) {
2465 zcmd_free_nvlists(zc
);
2468 zc
->zc_cookie
= orig_cookie
;
2471 * An errno value of ESRCH indicates normal completion.
2472 * If ENOENT is returned, then the underlying dataset
2473 * has been removed since we obtained the handle.
2480 rc
= zfs_standard_error(zhp
->zfs_hdl
, errno
,
2481 dgettext(TEXT_DOMAIN
,
2482 "cannot iterate filesystems"));
2490 * Iterate over all child filesystems
2493 zfs_iter_filesystems(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2495 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2499 if (zhp
->zfs_type
!= ZFS_TYPE_FILESYSTEM
)
2502 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2505 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_DATASET_LIST_NEXT
,
2508 * Silently ignore errors, as the only plausible explanation is
2509 * that the pool has since been removed.
2511 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2516 if ((ret
= func(nzhp
, data
)) != 0) {
2517 zcmd_free_nvlists(&zc
);
2521 zcmd_free_nvlists(&zc
);
2522 return ((ret
< 0) ? ret
: 0);
2526 * Iterate over all snapshots
2529 zfs_iter_snapshots(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2531 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2535 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
2538 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2540 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2543 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2548 if ((ret
= func(nzhp
, data
)) != 0) {
2549 zcmd_free_nvlists(&zc
);
2553 zcmd_free_nvlists(&zc
);
2554 return ((ret
< 0) ? ret
: 0);
2558 * Iterate over all children, snapshots and filesystems
2561 zfs_iter_children(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2565 if ((ret
= zfs_iter_filesystems(zhp
, func
, data
)) != 0)
2568 return (zfs_iter_snapshots(zhp
, func
, data
));
2572 * Is one dataset name a child dataset of another?
2574 * Needs to handle these cases:
2575 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2576 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2577 * Descendant? No. No. No. Yes.
2580 is_descendant(const char *ds1
, const char *ds2
)
2582 size_t d1len
= strlen(ds1
);
2584 /* ds2 can't be a descendant if it's smaller */
2585 if (strlen(ds2
) < d1len
)
2588 /* otherwise, compare strings and verify that there's a '/' char */
2589 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2593 * Given a complete name, return just the portion that refers to the parent.
2594 * Can return NULL if this is a pool.
2597 parent_name(const char *path
, char *buf
, size_t buflen
)
2601 if ((loc
= strrchr(path
, '/')) == NULL
)
2604 (void) strncpy(buf
, path
, MIN(buflen
, loc
- path
));
2605 buf
[loc
- path
] = '\0';
2611 * If accept_ancestor is false, then check to make sure that the given path has
2612 * a parent, and that it exists. If accept_ancestor is true, then find the
2613 * closest existing ancestor for the given path. In prefixlen return the
2614 * length of already existing prefix of the given path. We also fetch the
2615 * 'zoned' property, which is used to validate property settings when creating
2619 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2620 boolean_t accept_ancestor
, int *prefixlen
)
2622 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2623 char parent
[ZFS_MAXNAMELEN
];
2629 (void) snprintf(errbuf
, sizeof (errbuf
),
2630 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2632 /* get parent, and check to see if this is just a pool */
2633 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2634 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2635 "missing dataset name"));
2636 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2639 /* check to see if the pool exists */
2640 if ((slash
= strchr(parent
, '/')) == NULL
)
2641 slash
= parent
+ strlen(parent
);
2642 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2643 zc
.zc_name
[slash
- parent
] = '\0';
2644 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2646 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2647 "no such pool '%s'"), zc
.zc_name
);
2648 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2651 /* check to see if the parent dataset exists */
2652 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2653 if (errno
== ENOENT
&& accept_ancestor
) {
2655 * Go deeper to find an ancestor, give up on top level.
2657 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2658 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2659 "no such pool '%s'"), zc
.zc_name
);
2660 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2662 } else if (errno
== ENOENT
) {
2663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2664 "parent does not exist"));
2665 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2667 return (zfs_standard_error(hdl
, errno
, errbuf
));
2670 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2674 /* we are in a non-global zone, but parent is in the global zone */
2675 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2676 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2681 /* make sure parent is a filesystem */
2682 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2683 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2684 "parent is not a filesystem"));
2685 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2691 if (prefixlen
!= NULL
)
2692 *prefixlen
= strlen(parent
);
2697 * Finds whether the dataset of the given type(s) exists.
2700 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2704 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2708 * Try to get stats for the dataset, which will tell us if it exists.
2710 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2711 int ds_type
= zhp
->zfs_type
;
2714 if (types
& ds_type
)
2721 * Given a path to 'target', create all the ancestors between
2722 * the prefixlen portion of the path, and the target itself.
2723 * Fail if the initial prefixlen-ancestor does not already exist.
2726 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2732 /* make sure prefix exists */
2733 cp
= target
+ prefixlen
;
2735 assert(strchr(cp
, '/') == NULL
);
2736 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2739 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2747 * Attempt to create, mount, and share any ancestor filesystems,
2748 * up to the prefixlen-long one.
2750 for (cp
= target
+ prefixlen
+ 1;
2751 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
2756 h
= make_dataset_handle(hdl
, target
);
2758 /* it already exists, nothing to do here */
2763 logstr
= hdl
->libzfs_log_str
;
2764 hdl
->libzfs_log_str
= NULL
;
2765 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2767 hdl
->libzfs_log_str
= logstr
;
2768 opname
= dgettext(TEXT_DOMAIN
, "create");
2772 hdl
->libzfs_log_str
= logstr
;
2773 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2775 opname
= dgettext(TEXT_DOMAIN
, "open");
2779 if (zfs_mount(h
, NULL
, 0) != 0) {
2780 opname
= dgettext(TEXT_DOMAIN
, "mount");
2784 if (zfs_share(h
) != 0) {
2785 opname
= dgettext(TEXT_DOMAIN
, "share");
2795 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2796 "failed to %s ancestor '%s'"), opname
, target
);
2801 * Creates non-existing ancestors of the given path.
2804 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2810 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
2813 if ((path_copy
= strdup(path
)) != NULL
) {
2814 rc
= create_parents(hdl
, path_copy
, prefix
);
2817 if (path_copy
== NULL
|| rc
!= 0)
2824 * Create a new filesystem or volume.
2827 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2830 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2833 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2837 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2838 "cannot create '%s'"), path
);
2840 /* validate the path, taking care to note the extended error message */
2841 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
2842 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2844 /* validate parents exist */
2845 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
2849 * The failure modes when creating a dataset of a different type over
2850 * one that already exists is a little strange. In particular, if you
2851 * try to create a dataset on top of an existing dataset, the ioctl()
2852 * will return ENOENT, not EEXIST. To prevent this from happening, we
2853 * first try to see if the dataset exists.
2855 (void) strlcpy(zc
.zc_name
, path
, sizeof (zc
.zc_name
));
2856 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2857 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2858 "dataset already exists"));
2859 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2862 if (type
== ZFS_TYPE_VOLUME
)
2863 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2865 zc
.zc_objset_type
= DMU_OST_ZFS
;
2867 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
2868 zoned
, NULL
, errbuf
)) == 0)
2871 if (type
== ZFS_TYPE_VOLUME
) {
2873 * If we are creating a volume, the size and block size must
2874 * satisfy a few restraints. First, the blocksize must be a
2875 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2876 * volsize must be a multiple of the block size, and cannot be
2879 if (props
== NULL
|| nvlist_lookup_uint64(props
,
2880 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
2882 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2883 "missing volume size"));
2884 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2887 if ((ret
= nvlist_lookup_uint64(props
,
2888 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2889 &blocksize
)) != 0) {
2890 if (ret
== ENOENT
) {
2891 blocksize
= zfs_prop_default_numeric(
2892 ZFS_PROP_VOLBLOCKSIZE
);
2895 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2896 "missing volume block size"));
2897 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2903 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2904 "volume size cannot be zero"));
2905 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2908 if (size
% blocksize
!= 0) {
2910 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2911 "volume size must be a multiple of volume block "
2913 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2917 if (props
&& zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0)
2921 /* create the dataset */
2922 ret
= zfs_ioctl(hdl
, ZFS_IOC_CREATE
, &zc
);
2924 if (ret
== 0 && type
== ZFS_TYPE_VOLUME
) {
2925 ret
= zvol_create_link(hdl
, path
);
2927 (void) zfs_standard_error(hdl
, errno
,
2928 dgettext(TEXT_DOMAIN
,
2929 "Volume successfully created, but device links "
2930 "were not created"));
2931 zcmd_free_nvlists(&zc
);
2936 zcmd_free_nvlists(&zc
);
2938 /* check for failure */
2940 char parent
[ZFS_MAXNAMELEN
];
2941 (void) parent_name(path
, parent
, sizeof (parent
));
2945 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2946 "no such parent '%s'"), parent
);
2947 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2950 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2951 "parent '%s' is not a filesystem"), parent
);
2952 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
2955 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2956 "volume block size must be power of 2 from "
2958 (uint_t
)SPA_MINBLOCKSIZE
,
2959 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
2961 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2964 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2965 "pool must be upgraded to set this "
2966 "property or value"));
2967 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
2971 * This platform can't address a volume this big.
2973 if (type
== ZFS_TYPE_VOLUME
)
2974 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
2979 return (zfs_standard_error(hdl
, errno
, errbuf
));
2987 * Destroys the given dataset. The caller must make sure that the filesystem
2988 * isn't mounted, and that there are no active dependents.
2991 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
2993 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2995 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2997 if (ZFS_IS_VOLUME(zhp
)) {
2998 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3001 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3003 zc
.zc_objset_type
= DMU_OST_ZFS
;
3006 zc
.zc_defer_destroy
= defer
;
3007 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0) {
3008 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3009 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3013 remove_mountpoint(zhp
);
3018 struct destroydata
{
3025 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3027 struct destroydata
*dd
= arg
;
3029 char name
[ZFS_MAXNAMELEN
];
3030 boolean_t closezhp
= dd
->closezhp
;
3033 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3034 (void) strlcat(name
, "@", sizeof (name
));
3035 (void) strlcat(name
, dd
->snapname
, sizeof (name
));
3037 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
3039 dd
->gotone
= B_TRUE
;
3043 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3044 (void) zvol_remove_link(zhp
->zfs_hdl
, name
);
3046 * NB: this is simply a best-effort. We don't want to
3047 * return an error, because then we wouldn't visit all
3052 dd
->closezhp
= B_TRUE
;
3053 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, arg
);
3060 * Destroys all snapshots with the given name in zhp & descendants.
3063 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3065 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3067 struct destroydata dd
= { 0 };
3069 dd
.snapname
= snapname
;
3070 (void) zfs_check_snap_cb(zhp
, &dd
);
3073 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3074 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3075 zhp
->zfs_name
, snapname
));
3078 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3079 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3080 zc
.zc_defer_destroy
= defer
;
3082 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY_SNAPS
, &zc
);
3086 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3087 "cannot destroy '%s@%s'"), zc
.zc_name
, snapname
);
3091 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3092 "snapshot is cloned"));
3093 return (zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
));
3096 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3105 * Clones the given dataset. The target must be of the same type as the source.
3108 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3110 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3111 char parent
[ZFS_MAXNAMELEN
];
3114 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3118 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3120 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3121 "cannot create '%s'"), target
);
3123 /* validate the target name */
3124 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3125 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3127 /* validate parents exist */
3128 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3131 (void) parent_name(target
, parent
, sizeof (parent
));
3134 if (ZFS_IS_VOLUME(zhp
)) {
3135 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3136 type
= ZFS_TYPE_VOLUME
;
3138 zc
.zc_objset_type
= DMU_OST_ZFS
;
3139 type
= ZFS_TYPE_FILESYSTEM
;
3143 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3144 zhp
, errbuf
)) == NULL
)
3147 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3155 (void) strlcpy(zc
.zc_name
, target
, sizeof (zc
.zc_name
));
3156 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
, sizeof (zc
.zc_value
));
3157 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_CREATE
, &zc
);
3159 zcmd_free_nvlists(&zc
);
3166 * The parent doesn't exist. We should have caught this
3167 * above, but there may a race condition that has since
3168 * destroyed the parent.
3170 * At this point, we don't know whether it's the source
3171 * that doesn't exist anymore, or whether the target
3172 * dataset doesn't exist.
3174 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3175 "no such parent '%s'"), parent
);
3176 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3179 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3180 "source and target pools differ"));
3181 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3185 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3188 } else if (ZFS_IS_VOLUME(zhp
)) {
3189 ret
= zvol_create_link(zhp
->zfs_hdl
, target
);
3195 typedef struct promote_data
{
3196 char cb_mountpoint
[MAXPATHLEN
];
3197 const char *cb_target
;
3198 const char *cb_errbuf
;
3199 uint64_t cb_pivot_txg
;
3203 promote_snap_cb(zfs_handle_t
*zhp
, void *data
)
3205 promote_data_t
*pd
= data
;
3207 char snapname
[MAXPATHLEN
];
3210 /* We don't care about snapshots after the pivot point */
3211 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > pd
->cb_pivot_txg
) {
3216 /* Remove the device link if it's a zvol. */
3217 if (ZFS_IS_VOLUME(zhp
))
3218 (void) zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3220 /* Check for conflicting names */
3221 (void) strlcpy(snapname
, pd
->cb_target
, sizeof (snapname
));
3222 (void) strlcat(snapname
, strchr(zhp
->zfs_name
, '@'), sizeof (snapname
));
3223 szhp
= make_dataset_handle(zhp
->zfs_hdl
, snapname
);
3226 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3227 "snapshot name '%s' from origin \n"
3228 "conflicts with '%s' from target"),
3229 zhp
->zfs_name
, snapname
);
3230 rv
= zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, pd
->cb_errbuf
);
3237 promote_snap_done_cb(zfs_handle_t
*zhp
, void *data
)
3239 promote_data_t
*pd
= data
;
3241 /* We don't care about snapshots after the pivot point */
3242 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) <= pd
->cb_pivot_txg
) {
3243 /* Create the device link if it's a zvol. */
3244 if (ZFS_IS_VOLUME(zhp
))
3245 (void) zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3253 * Promotes the given clone fs to be the clone parent.
3256 zfs_promote(zfs_handle_t
*zhp
)
3258 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3259 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3260 char parent
[MAXPATHLEN
];
3267 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3268 "cannot promote '%s'"), zhp
->zfs_name
);
3270 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3271 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3272 "snapshots can not be promoted"));
3273 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3276 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3277 if (parent
[0] == '\0') {
3278 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3279 "not a cloned filesystem"));
3280 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3282 cp
= strchr(parent
, '@');
3285 /* Walk the snapshots we will be moving */
3286 pzhp
= zfs_open(hdl
, zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
3289 pd
.cb_pivot_txg
= zfs_prop_get_int(pzhp
, ZFS_PROP_CREATETXG
);
3291 pd
.cb_target
= zhp
->zfs_name
;
3292 pd
.cb_errbuf
= errbuf
;
3293 pzhp
= zfs_open(hdl
, parent
, ZFS_TYPE_DATASET
);
3296 (void) zfs_prop_get(pzhp
, ZFS_PROP_MOUNTPOINT
, pd
.cb_mountpoint
,
3297 sizeof (pd
.cb_mountpoint
), NULL
, NULL
, 0, FALSE
);
3298 ret
= zfs_iter_snapshots(pzhp
, promote_snap_cb
, &pd
);
3304 /* issue the ioctl */
3305 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3306 sizeof (zc
.zc_value
));
3307 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3308 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3311 int save_errno
= errno
;
3313 (void) zfs_iter_snapshots(pzhp
, promote_snap_done_cb
, &pd
);
3316 switch (save_errno
) {
3319 * There is a conflicting snapshot name. We
3320 * should have caught this above, but they could
3321 * have renamed something in the mean time.
3323 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3324 "conflicting snapshot '%s' from parent '%s'"),
3325 zc
.zc_string
, parent
);
3326 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3329 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3332 (void) zfs_iter_snapshots(zhp
, promote_snap_done_cb
, &pd
);
3340 const char *cd_snapname
;
3345 zfs_create_link_cb(zfs_handle_t
*zhp
, void *arg
)
3347 struct createdata
*cd
= arg
;
3350 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3351 char name
[MAXPATHLEN
];
3353 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3354 (void) strlcat(name
, "@", sizeof (name
));
3355 (void) strlcat(name
, cd
->cd_snapname
, sizeof (name
));
3356 (void) zvol_create_link_common(zhp
->zfs_hdl
, name
,
3359 * NB: this is simply a best-effort. We don't want to
3360 * return an error, because then we wouldn't visit all
3365 ret
= zfs_iter_filesystems(zhp
, zfs_create_link_cb
, cd
);
3373 * Takes a snapshot of the given dataset.
3376 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3380 char parent
[ZFS_MAXNAMELEN
];
3382 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3386 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3387 "cannot snapshot '%s'"), path
);
3389 /* validate the target name */
3390 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3391 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3394 if ((props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3395 props
, B_FALSE
, NULL
, errbuf
)) == NULL
)
3398 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3406 /* make sure the parent exists and is of the appropriate type */
3407 delim
= strchr(path
, '@');
3408 (void) strncpy(parent
, path
, delim
- path
);
3409 parent
[delim
- path
] = '\0';
3411 if ((zhp
= zfs_open(hdl
, parent
, ZFS_TYPE_FILESYSTEM
|
3412 ZFS_TYPE_VOLUME
)) == NULL
) {
3413 zcmd_free_nvlists(&zc
);
3417 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3418 (void) strlcpy(zc
.zc_value
, delim
+1, sizeof (zc
.zc_value
));
3419 if (ZFS_IS_VOLUME(zhp
))
3420 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3422 zc
.zc_objset_type
= DMU_OST_ZFS
;
3423 zc
.zc_cookie
= recursive
;
3424 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SNAPSHOT
, &zc
);
3426 zcmd_free_nvlists(&zc
);
3429 * if it was recursive, the one that actually failed will be in
3433 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3434 "cannot create snapshot '%s@%s'"), zc
.zc_name
, zc
.zc_value
);
3436 if (ret
== 0 && recursive
) {
3437 struct createdata cd
;
3439 cd
.cd_snapname
= delim
+ 1;
3440 cd
.cd_ifexists
= B_FALSE
;
3441 (void) zfs_iter_filesystems(zhp
, zfs_create_link_cb
, &cd
);
3443 if (ret
== 0 && zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3444 ret
= zvol_create_link(zhp
->zfs_hdl
, path
);
3446 (void) zfs_standard_error(hdl
, errno
,
3447 dgettext(TEXT_DOMAIN
,
3448 "Volume successfully snapshotted, but device links "
3449 "were not created"));
3456 (void) zfs_standard_error(hdl
, errno
, errbuf
);
3464 * Destroy any more recent snapshots. We invoke this callback on any dependents
3465 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3466 * is a dependent and we should just destroy it without checking the transaction
3469 typedef struct rollback_data
{
3470 const char *cb_target
; /* the snapshot */
3471 uint64_t cb_create
; /* creation time reference */
3473 boolean_t cb_dependent
;
3478 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3480 rollback_data_t
*cbp
= data
;
3482 if (!cbp
->cb_dependent
) {
3483 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3484 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3485 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3489 cbp
->cb_dependent
= B_TRUE
;
3490 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3491 rollback_destroy
, cbp
);
3492 cbp
->cb_dependent
= B_FALSE
;
3494 logstr
= zhp
->zfs_hdl
->libzfs_log_str
;
3495 zhp
->zfs_hdl
->libzfs_log_str
= NULL
;
3496 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3497 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
3500 /* We must destroy this clone; first unmount it */
3501 prop_changelist_t
*clp
;
3503 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3504 cbp
->cb_force
? MS_FORCE
: 0);
3505 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3506 cbp
->cb_error
= B_TRUE
;
3510 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3511 cbp
->cb_error
= B_TRUE
;
3513 changelist_remove(clp
, zhp
->zfs_name
);
3514 (void) changelist_postfix(clp
);
3515 changelist_free(clp
);
3523 * Given a dataset, rollback to a specific snapshot, discarding any
3524 * data changes since then and making it the active dataset.
3526 * Any snapshots more recent than the target are destroyed, along with
3530 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3532 rollback_data_t cb
= { 0 };
3534 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3535 boolean_t restore_resv
= 0;
3536 uint64_t old_volsize
= 0, new_volsize
;
3537 zfs_prop_t resv_prop
= { 0 };
3539 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3540 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3543 * Destroy all recent snapshots and its dependends.
3545 cb
.cb_force
= force
;
3546 cb
.cb_target
= snap
->zfs_name
;
3547 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3548 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3554 * Now that we have verified that the snapshot is the latest,
3555 * rollback to the given snapshot.
3558 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3559 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3561 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3563 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3565 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3568 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3570 if (ZFS_IS_VOLUME(zhp
))
3571 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3573 zc
.zc_objset_type
= DMU_OST_ZFS
;
3576 * We rely on zfs_iter_children() to verify that there are no
3577 * newer snapshots for the given dataset. Therefore, we can
3578 * simply pass the name on to the ioctl() call. There is still
3579 * an unlikely race condition where the user has taken a
3580 * snapshot since we verified that this was the most recent.
3583 if ((err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_ROLLBACK
, &zc
)) != 0) {
3584 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3585 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3591 * For volumes, if the pre-rollback volsize matched the pre-
3592 * rollback reservation and the volsize has changed then set
3593 * the reservation property to the post-rollback volsize.
3594 * Make a new handle since the rollback closed the dataset.
3596 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3597 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3598 if ((err
= zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3603 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3604 if (old_volsize
!= new_volsize
)
3605 err
= zfs_prop_set_int(zhp
, resv_prop
,
3614 * Iterate over all dependents for a given dataset. This includes both
3615 * hierarchical dependents (children) and data dependents (snapshots and
3616 * clones). The bulk of the processing occurs in get_dependents() in
3620 zfs_iter_dependents(zfs_handle_t
*zhp
, boolean_t allowrecursion
,
3621 zfs_iter_f func
, void *data
)
3626 zfs_handle_t
*child
;
3629 if (get_dependents(zhp
->zfs_hdl
, allowrecursion
, zhp
->zfs_name
,
3630 &dependents
, &count
) != 0)
3633 for (i
= 0; i
< count
; i
++) {
3634 if ((child
= make_dataset_handle(zhp
->zfs_hdl
,
3635 dependents
[i
])) == NULL
)
3638 if ((ret
= func(child
, data
)) != 0)
3642 for (i
= 0; i
< count
; i
++)
3643 free(dependents
[i
]);
3650 * Renames the given dataset.
3653 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
)
3656 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3658 prop_changelist_t
*cl
= NULL
;
3659 zfs_handle_t
*zhrp
= NULL
;
3660 char *parentname
= NULL
;
3661 char parent
[ZFS_MAXNAMELEN
];
3662 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3665 /* if we have the same exact name, just return success */
3666 if (strcmp(zhp
->zfs_name
, target
) == 0)
3669 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3670 "cannot rename to '%s'"), target
);
3673 * Make sure the target name is valid
3675 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3676 if ((strchr(target
, '@') == NULL
) ||
3679 * Snapshot target name is abbreviated,
3680 * reconstruct full dataset name
3682 (void) strlcpy(parent
, zhp
->zfs_name
,
3684 delim
= strchr(parent
, '@');
3685 if (strchr(target
, '@') == NULL
)
3689 (void) strlcat(parent
, target
, sizeof (parent
));
3693 * Make sure we're renaming within the same dataset.
3695 delim
= strchr(target
, '@');
3696 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3697 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3698 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3699 "snapshots must be part of same "
3701 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3705 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3706 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3709 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3710 "recursive rename must be a snapshot"));
3711 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3714 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3715 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3717 /* validate parents */
3718 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3721 /* make sure we're in the same pool */
3722 verify((delim
= strchr(target
, '/')) != NULL
);
3723 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3724 zhp
->zfs_name
[delim
- target
] != '/') {
3725 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3726 "datasets must be within same pool"));
3727 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3730 /* new name cannot be a child of the current dataset name */
3731 if (is_descendant(zhp
->zfs_name
, target
)) {
3732 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3733 "New dataset name cannot be a descendant of "
3734 "current dataset name"));
3735 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3739 (void) snprintf(errbuf
, sizeof (errbuf
),
3740 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3742 if (getzoneid() == GLOBAL_ZONEID
&&
3743 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3745 "dataset is used in a non-global zone"));
3746 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3750 struct destroydata dd
;
3752 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3753 if (parentname
== NULL
) {
3757 delim
= strchr(parentname
, '@');
3759 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3765 dd
.snapname
= delim
+ 1;
3766 dd
.gotone
= B_FALSE
;
3767 dd
.closezhp
= B_TRUE
;
3769 /* We remove any zvol links prior to renaming them */
3770 ret
= zfs_iter_filesystems(zhrp
, zfs_check_snap_cb
, &dd
);
3775 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0)) == NULL
)
3778 if (changelist_haszonedchild(cl
)) {
3779 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3780 "child dataset with inherited mountpoint is used "
3781 "in a non-global zone"));
3782 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3787 if ((ret
= changelist_prefix(cl
)) != 0)
3791 if (ZFS_IS_VOLUME(zhp
))
3792 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3794 zc
.zc_objset_type
= DMU_OST_ZFS
;
3796 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3797 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3799 zc
.zc_cookie
= recursive
;
3801 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3803 * if it was recursive, the one that actually failed will
3806 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3807 "cannot rename '%s'"), zc
.zc_name
);
3809 if (recursive
&& errno
== EEXIST
) {
3810 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3811 "a child dataset already has a snapshot "
3812 "with the new name"));
3813 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3815 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3819 * On failure, we still want to remount any filesystems that
3820 * were previously mounted, so we don't alter the system state.
3823 struct createdata cd
;
3825 /* only create links for datasets that had existed */
3826 cd
.cd_snapname
= delim
+ 1;
3827 cd
.cd_ifexists
= B_TRUE
;
3828 (void) zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3831 (void) changelist_postfix(cl
);
3835 struct createdata cd
;
3837 /* only create links for datasets that had existed */
3838 cd
.cd_snapname
= strchr(target
, '@') + 1;
3839 cd
.cd_ifexists
= B_TRUE
;
3840 ret
= zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3843 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3844 ret
= changelist_postfix(cl
);
3856 changelist_free(cl
);
3862 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3863 * and wait briefly for udev to create the /dev link.
3866 zvol_create_link(libzfs_handle_t
*hdl
, const char *dataset
)
3868 return (zvol_create_link_common(hdl
, dataset
, B_FALSE
));
3872 zvol_create_link_common(libzfs_handle_t
*hdl
, const char *dataset
, int ifexists
)
3874 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3875 char path
[MAXPATHLEN
];
3878 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3881 * Issue the appropriate ioctl.
3883 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CREATE_MINOR
, &zc
) != 0) {
3887 * Silently ignore the case where the link already
3888 * exists. This allows 'zfs volinit' to be run multiple
3889 * times without errors.
3895 * Dataset does not exist in the kernel. If we
3896 * don't care (see zfs_rename), then ignore the
3906 return (zfs_standard_error_fmt(hdl
, errno
,
3907 dgettext(TEXT_DOMAIN
, "cannot create device links "
3908 "for '%s'"), dataset
));
3913 * Wait up to 10 seconds for udev to create the device.
3915 (void) snprintf(path
, sizeof (path
), "%s/%s", ZVOL_DIR
, dataset
);
3916 error
= zpool_label_disk_wait(path
, 10000);
3918 (void) printf(gettext("%s may not be immediately "
3919 "available\n"), path
);
3925 * Remove a minor node for the given zvol and the associated /dev links.
3928 zvol_remove_link(libzfs_handle_t
*hdl
, const char *dataset
)
3930 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3932 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3934 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_REMOVE_MINOR
, &zc
) != 0) {
3938 * Silently ignore the case where the link no longer
3939 * exists, so that 'zfs volfini' can be run multiple
3940 * times without errors.
3945 return (zfs_standard_error_fmt(hdl
, errno
,
3946 dgettext(TEXT_DOMAIN
, "cannot remove device "
3947 "links for '%s'"), dataset
));
3955 zfs_get_user_props(zfs_handle_t
*zhp
)
3957 return (zhp
->zfs_user_props
);
3961 * This function is used by 'zfs list' to determine the exact set of columns to
3962 * display, and their maximum widths. This does two main things:
3964 * - If this is a list of all properties, then expand the list to include
3965 * all native properties, and set a flag so that for each dataset we look
3966 * for new unique user properties and add them to the list.
3968 * - For non fixed-width properties, keep track of the maximum width seen
3969 * so that we can size the column appropriately. If the user has
3970 * requested received property values, we also need to compute the width
3971 * of the RECEIVED column.
3974 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
)
3976 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3977 zprop_list_t
*entry
;
3978 zprop_list_t
**last
, **start
;
3979 nvlist_t
*userprops
, *propval
;
3982 char buf
[ZFS_MAXPROPLEN
];
3984 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
3987 userprops
= zfs_get_user_props(zhp
);
3990 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
3992 * Go through and add any user properties as necessary. We
3993 * start by incrementing our list pointer to the first
3994 * non-native property.
3997 while (*start
!= NULL
) {
3998 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4000 start
= &(*start
)->pl_next
;
4004 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4006 * See if we've already found this property in our list.
4008 for (last
= start
; *last
!= NULL
;
4009 last
= &(*last
)->pl_next
) {
4010 if (strcmp((*last
)->pl_user_prop
,
4011 nvpair_name(elem
)) == 0)
4015 if (*last
== NULL
) {
4016 if ((entry
= zfs_alloc(hdl
,
4017 sizeof (zprop_list_t
))) == NULL
||
4018 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4019 nvpair_name(elem
)))) == NULL
) {
4024 entry
->pl_prop
= ZPROP_INVAL
;
4025 entry
->pl_width
= strlen(nvpair_name(elem
));
4026 entry
->pl_all
= B_TRUE
;
4033 * Now go through and check the width of any non-fixed columns
4035 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4036 if (entry
->pl_fixed
)
4039 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4040 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4041 buf
, sizeof (buf
), NULL
, NULL
, 0, B_FALSE
) == 0) {
4042 if (strlen(buf
) > entry
->pl_width
)
4043 entry
->pl_width
= strlen(buf
);
4045 if (received
&& zfs_prop_get_recvd(zhp
,
4046 zfs_prop_to_name(entry
->pl_prop
),
4047 buf
, sizeof (buf
), B_FALSE
) == 0)
4048 if (strlen(buf
) > entry
->pl_recvd_width
)
4049 entry
->pl_recvd_width
= strlen(buf
);
4051 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4053 verify(nvlist_lookup_string(propval
,
4054 ZPROP_VALUE
, &strval
) == 0);
4055 if (strlen(strval
) > entry
->pl_width
)
4056 entry
->pl_width
= strlen(strval
);
4058 if (received
&& zfs_prop_get_recvd(zhp
,
4059 entry
->pl_user_prop
,
4060 buf
, sizeof (buf
), B_FALSE
) == 0)
4061 if (strlen(buf
) > entry
->pl_recvd_width
)
4062 entry
->pl_recvd_width
= strlen(buf
);
4070 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4075 * Keep a reference to the props-table against which we prune the
4078 zhp
->zfs_props_table
= props
;
4080 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4083 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4084 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4087 * User properties will result in ZPROP_INVAL, and since we
4088 * only know how to prune standard ZFS properties, we always
4089 * leave these in the list. This can also happen if we
4090 * encounter an unknown DSL property (when running older
4091 * software, for example).
4093 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4094 (void) nvlist_remove(zhp
->zfs_props
,
4095 nvpair_name(curr
), nvpair_type(curr
));
4101 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4102 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4104 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4105 nvlist_t
*nvlist
= NULL
;
4108 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4109 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4110 zc
.zc_cookie
= (uint64_t)cmd
;
4112 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4113 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4114 (void) no_memory(hdl
);
4120 case ZFS_SMB_ACL_ADD
:
4121 case ZFS_SMB_ACL_REMOVE
:
4122 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4124 case ZFS_SMB_ACL_RENAME
:
4125 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4127 (void) no_memory(hdl
);
4130 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4132 (void) no_memory(hdl
);
4135 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4136 nvlist_free(nvlist
);
4140 case ZFS_SMB_ACL_PURGE
:
4145 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4147 nvlist_free(nvlist
);
4152 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4153 char *path
, char *resource
)
4155 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4160 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4161 char *path
, char *resource
)
4163 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4168 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4170 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4175 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4176 char *oldname
, char *newname
)
4178 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4183 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4184 zfs_userspace_cb_t func
, void *arg
)
4186 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4188 zfs_useracct_t buf
[100];
4190 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4192 zc
.zc_objset_type
= type
;
4193 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4197 zfs_useracct_t
*zua
= buf
;
4199 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4200 error
= ioctl(zhp
->zfs_hdl
->libzfs_fd
,
4201 ZFS_IOC_USERSPACE_MANY
, &zc
);
4202 if (error
|| zc
.zc_nvlist_dst_size
== 0)
4205 while (zc
.zc_nvlist_dst_size
> 0) {
4206 error
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4211 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4219 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4220 boolean_t recursive
, boolean_t temphold
, boolean_t enoent_ok
,
4221 int cleanup_fd
, uint64_t dsobj
, uint64_t createtxg
)
4223 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4224 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4226 ASSERT(!recursive
|| dsobj
== 0);
4228 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4229 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4230 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4231 >= sizeof (zc
.zc_string
))
4232 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4233 zc
.zc_cookie
= recursive
;
4234 zc
.zc_temphold
= temphold
;
4235 zc
.zc_cleanup_fd
= cleanup_fd
;
4236 zc
.zc_sendobj
= dsobj
;
4237 zc
.zc_createtxg
= createtxg
;
4239 if (zfs_ioctl(hdl
, ZFS_IOC_HOLD
, &zc
) != 0) {
4240 char errbuf
[ZFS_MAXNAMELEN
+32];
4243 * if it was recursive, the one that actually failed will be in
4246 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4247 "cannot hold '%s@%s'"), zc
.zc_name
, snapname
);
4251 * Temporary tags wind up having the ds object id
4252 * prepended. So even if we passed the length check
4253 * above, it's still possible for the tag to wind
4254 * up being slightly too long.
4256 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
));
4258 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4259 "pool must be upgraded"));
4260 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4262 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4264 return (zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
));
4270 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4278 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4279 boolean_t recursive
)
4281 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4282 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4284 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4285 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4286 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4287 >= sizeof (zc
.zc_string
))
4288 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4289 zc
.zc_cookie
= recursive
;
4291 if (zfs_ioctl(hdl
, ZFS_IOC_RELEASE
, &zc
) != 0) {
4292 char errbuf
[ZFS_MAXNAMELEN
+32];
4295 * if it was recursive, the one that actually failed will be in
4298 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4299 "cannot release '%s' from '%s@%s'"), tag
, zc
.zc_name
,
4303 return (zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
));
4305 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4306 "pool must be upgraded"));
4307 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4309 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4311 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4319 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4321 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4322 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4326 char errbuf
[ZFS_MAXNAMELEN
+32];
4328 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4329 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4333 nvbuf
= malloc(nvsz
);
4334 if (nvbuf
== NULL
) {
4335 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4339 zc
.zc_nvlist_dst_size
= nvsz
;
4340 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4342 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4344 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4345 (void) snprintf(errbuf
, sizeof (errbuf
),
4346 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4351 nvsz
= zc
.zc_nvlist_dst_size
;
4355 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4356 "pool must be upgraded"));
4357 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4360 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4363 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4366 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4371 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4373 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4374 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4376 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4386 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4388 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4389 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4391 char errbuf
[ZFS_MAXNAMELEN
+32];
4395 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4396 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4398 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4401 nvbuf
= malloc(nvsz
);
4403 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4406 zc
.zc_nvlist_src_size
= nvsz
;
4407 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4408 zc
.zc_perm_action
= un
;
4410 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4412 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4413 (void) snprintf(errbuf
, sizeof (errbuf
),
4414 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4418 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4419 "pool must be upgraded"));
4420 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4423 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4426 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4429 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4440 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4442 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4443 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4447 char errbuf
[ZFS_MAXNAMELEN
+32];
4449 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
4453 nvbuf
= malloc(nvsz
);
4454 if (nvbuf
== NULL
) {
4455 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4459 zc
.zc_nvlist_dst_size
= nvsz
;
4460 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4462 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4464 if (zfs_ioctl(hdl
, ZFS_IOC_GET_HOLDS
, &zc
) != 0) {
4465 (void) snprintf(errbuf
, sizeof (errbuf
),
4466 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4471 nvsz
= zc
.zc_nvlist_dst_size
;
4475 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4476 "pool must be upgraded"));
4477 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4480 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4483 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4486 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4491 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4493 (void) snprintf(errbuf
, sizeof (errbuf
),
4494 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4496 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4506 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4509 uint64_t nblocks
, volblocksize
;
4513 if (nvlist_lookup_string(props
,
4514 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4515 ncopies
= atoi(strval
);
4518 if (nvlist_lookup_uint64(props
,
4519 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4520 &volblocksize
) != 0)
4521 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4522 nblocks
= volsize
/volblocksize
;
4523 /* start with metadnode L0-L6 */
4525 /* calculate number of indirects */
4526 while (nblocks
> 1) {
4527 nblocks
+= DNODES_PER_LEVEL
- 1;
4528 nblocks
/= DNODES_PER_LEVEL
;
4531 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4534 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4535 * compressed, but in practice they compress down to about
4538 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;