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
2252 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2253 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2256 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2257 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2259 zfs_userquota_prop_t type
;
2268 /* Figure out the property type ({user|group}{quota|space}) */
2269 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2270 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2271 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2274 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2278 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
);
2279 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
);
2281 cp
= strchr(propname
, '@') + 1;
2283 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2284 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2287 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2288 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2291 } else if (strchr(cp
, '@')) {
2294 * It's a SID name (eg "user@domain") that needs to be
2295 * turned into S-1-domainID-RID.
2297 directory_error_t e
;
2298 char *numericsid
= NULL
;
2301 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2304 e
= directory_sid_from_user_name(NULL
,
2307 e
= directory_sid_from_group_name(NULL
,
2311 directory_error_free(e
);
2314 if (numericsid
== NULL
)
2317 (void) strlcpy(domain
, cp
, domainlen
);
2318 cp
= strrchr(domain
, '-');
2323 *ridp
= strtoull(cp
, &end
, 10);
2326 if (errno
!= 0 || *end
!= '\0')
2330 #endif /* HAVE_IDMAP */
2333 /* It's a user/group ID (eg "12345"). */
2339 id
= strtoul(cp
, &end
, 10);
2343 /* It's an ephemeral ID. */
2344 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2345 &mapdomain
, &rid
) != 0)
2347 (void) strlcpy(domain
, mapdomain
, domainlen
);
2354 #endif /* HAVE_IDMAP */
2361 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2362 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2365 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2367 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2369 err
= userquota_propname_decode(propname
,
2370 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2371 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2372 zc
.zc_objset_type
= *typep
;
2376 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2380 *propvalue
= zc
.zc_cookie
;
2385 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2386 uint64_t *propvalue
)
2388 zfs_userquota_prop_t type
;
2390 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2395 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2396 char *propbuf
, int proplen
, boolean_t literal
)
2400 zfs_userquota_prop_t type
;
2402 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2409 (void) snprintf(propbuf
, proplen
, "%llu",
2410 (u_longlong_t
)propvalue
);
2411 } else if (propvalue
== 0 &&
2412 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2413 (void) strlcpy(propbuf
, "none", proplen
);
2415 zfs_nicenum(propvalue
, propbuf
, proplen
);
2421 * Returns the name of the given zfs handle.
2424 zfs_get_name(const zfs_handle_t
*zhp
)
2426 return (zhp
->zfs_name
);
2430 * Returns the type of the given zfs handle.
2433 zfs_get_type(const zfs_handle_t
*zhp
)
2435 return (zhp
->zfs_type
);
2439 zfs_do_list_ioctl(zfs_handle_t
*zhp
, int arg
, zfs_cmd_t
*zc
)
2442 uint64_t orig_cookie
;
2444 orig_cookie
= zc
->zc_cookie
;
2446 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
2447 rc
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, arg
, zc
);
2452 /* expand nvlist memory and try again */
2453 if (zcmd_expand_dst_nvlist(zhp
->zfs_hdl
, zc
) != 0) {
2454 zcmd_free_nvlists(zc
);
2457 zc
->zc_cookie
= orig_cookie
;
2460 * An errno value of ESRCH indicates normal completion.
2461 * If ENOENT is returned, then the underlying dataset
2462 * has been removed since we obtained the handle.
2469 rc
= zfs_standard_error(zhp
->zfs_hdl
, errno
,
2470 dgettext(TEXT_DOMAIN
,
2471 "cannot iterate filesystems"));
2479 * Iterate over all child filesystems
2482 zfs_iter_filesystems(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2484 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2488 if (zhp
->zfs_type
!= ZFS_TYPE_FILESYSTEM
)
2491 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2494 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_DATASET_LIST_NEXT
,
2497 * Silently ignore errors, as the only plausible explanation is
2498 * that the pool has since been removed.
2500 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2505 if ((ret
= func(nzhp
, data
)) != 0) {
2506 zcmd_free_nvlists(&zc
);
2510 zcmd_free_nvlists(&zc
);
2511 return ((ret
< 0) ? ret
: 0);
2515 * Iterate over all snapshots
2518 zfs_iter_snapshots(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2520 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2524 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
2527 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2529 while ((ret
= zfs_do_list_ioctl(zhp
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2532 if ((nzhp
= make_dataset_handle_zc(zhp
->zfs_hdl
,
2537 if ((ret
= func(nzhp
, data
)) != 0) {
2538 zcmd_free_nvlists(&zc
);
2542 zcmd_free_nvlists(&zc
);
2543 return ((ret
< 0) ? ret
: 0);
2547 * Iterate over all children, snapshots and filesystems
2550 zfs_iter_children(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2554 if ((ret
= zfs_iter_filesystems(zhp
, func
, data
)) != 0)
2557 return (zfs_iter_snapshots(zhp
, func
, data
));
2561 * Is one dataset name a child dataset of another?
2563 * Needs to handle these cases:
2564 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
2565 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
2566 * Descendant? No. No. No. Yes.
2569 is_descendant(const char *ds1
, const char *ds2
)
2571 size_t d1len
= strlen(ds1
);
2573 /* ds2 can't be a descendant if it's smaller */
2574 if (strlen(ds2
) < d1len
)
2577 /* otherwise, compare strings and verify that there's a '/' char */
2578 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
2582 * Given a complete name, return just the portion that refers to the parent.
2583 * Can return NULL if this is a pool.
2586 parent_name(const char *path
, char *buf
, size_t buflen
)
2590 if ((loc
= strrchr(path
, '/')) == NULL
)
2593 (void) strncpy(buf
, path
, MIN(buflen
, loc
- path
));
2594 buf
[loc
- path
] = '\0';
2600 * If accept_ancestor is false, then check to make sure that the given path has
2601 * a parent, and that it exists. If accept_ancestor is true, then find the
2602 * closest existing ancestor for the given path. In prefixlen return the
2603 * length of already existing prefix of the given path. We also fetch the
2604 * 'zoned' property, which is used to validate property settings when creating
2608 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2609 boolean_t accept_ancestor
, int *prefixlen
)
2611 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2612 char parent
[ZFS_MAXNAMELEN
];
2618 (void) snprintf(errbuf
, sizeof (errbuf
),
2619 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
2621 /* get parent, and check to see if this is just a pool */
2622 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2623 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2624 "missing dataset name"));
2625 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2628 /* check to see if the pool exists */
2629 if ((slash
= strchr(parent
, '/')) == NULL
)
2630 slash
= parent
+ strlen(parent
);
2631 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2632 zc
.zc_name
[slash
- parent
] = '\0';
2633 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2636 "no such pool '%s'"), zc
.zc_name
);
2637 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2640 /* check to see if the parent dataset exists */
2641 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2642 if (errno
== ENOENT
&& accept_ancestor
) {
2644 * Go deeper to find an ancestor, give up on top level.
2646 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2647 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2648 "no such pool '%s'"), zc
.zc_name
);
2649 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2651 } else if (errno
== ENOENT
) {
2652 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2653 "parent does not exist"));
2654 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2656 return (zfs_standard_error(hdl
, errno
, errbuf
));
2659 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2663 /* we are in a non-global zone, but parent is in the global zone */
2664 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
2665 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2670 /* make sure parent is a filesystem */
2671 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2672 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2673 "parent is not a filesystem"));
2674 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2680 if (prefixlen
!= NULL
)
2681 *prefixlen
= strlen(parent
);
2686 * Finds whether the dataset of the given type(s) exists.
2689 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2693 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2697 * Try to get stats for the dataset, which will tell us if it exists.
2699 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2700 int ds_type
= zhp
->zfs_type
;
2703 if (types
& ds_type
)
2710 * Given a path to 'target', create all the ancestors between
2711 * the prefixlen portion of the path, and the target itself.
2712 * Fail if the initial prefixlen-ancestor does not already exist.
2715 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2721 /* make sure prefix exists */
2722 cp
= target
+ prefixlen
;
2724 assert(strchr(cp
, '/') == NULL
);
2725 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2728 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2736 * Attempt to create, mount, and share any ancestor filesystems,
2737 * up to the prefixlen-long one.
2739 for (cp
= target
+ prefixlen
+ 1;
2740 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
2745 h
= make_dataset_handle(hdl
, target
);
2747 /* it already exists, nothing to do here */
2752 logstr
= hdl
->libzfs_log_str
;
2753 hdl
->libzfs_log_str
= NULL
;
2754 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2756 hdl
->libzfs_log_str
= logstr
;
2757 opname
= dgettext(TEXT_DOMAIN
, "create");
2761 hdl
->libzfs_log_str
= logstr
;
2762 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2764 opname
= dgettext(TEXT_DOMAIN
, "open");
2768 if (zfs_mount(h
, NULL
, 0) != 0) {
2769 opname
= dgettext(TEXT_DOMAIN
, "mount");
2773 if (zfs_share(h
) != 0) {
2774 opname
= dgettext(TEXT_DOMAIN
, "share");
2784 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2785 "failed to %s ancestor '%s'"), opname
, target
);
2790 * Creates non-existing ancestors of the given path.
2793 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2799 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
2802 if ((path_copy
= strdup(path
)) != NULL
) {
2803 rc
= create_parents(hdl
, path_copy
, prefix
);
2806 if (path_copy
== NULL
|| rc
!= 0)
2813 * Create a new filesystem or volume.
2816 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2819 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2822 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2826 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2827 "cannot create '%s'"), path
);
2829 /* validate the path, taking care to note the extended error message */
2830 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
2831 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2833 /* validate parents exist */
2834 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
2838 * The failure modes when creating a dataset of a different type over
2839 * one that already exists is a little strange. In particular, if you
2840 * try to create a dataset on top of an existing dataset, the ioctl()
2841 * will return ENOENT, not EEXIST. To prevent this from happening, we
2842 * first try to see if the dataset exists.
2844 (void) strlcpy(zc
.zc_name
, path
, sizeof (zc
.zc_name
));
2845 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2846 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2847 "dataset already exists"));
2848 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2851 if (type
== ZFS_TYPE_VOLUME
)
2852 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2854 zc
.zc_objset_type
= DMU_OST_ZFS
;
2856 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
2857 zoned
, NULL
, errbuf
)) == 0)
2860 if (type
== ZFS_TYPE_VOLUME
) {
2862 * If we are creating a volume, the size and block size must
2863 * satisfy a few restraints. First, the blocksize must be a
2864 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2865 * volsize must be a multiple of the block size, and cannot be
2868 if (props
== NULL
|| nvlist_lookup_uint64(props
,
2869 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
2871 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2872 "missing volume size"));
2873 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2876 if ((ret
= nvlist_lookup_uint64(props
,
2877 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2878 &blocksize
)) != 0) {
2879 if (ret
== ENOENT
) {
2880 blocksize
= zfs_prop_default_numeric(
2881 ZFS_PROP_VOLBLOCKSIZE
);
2884 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2885 "missing volume block size"));
2886 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2892 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2893 "volume size cannot be zero"));
2894 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2897 if (size
% blocksize
!= 0) {
2899 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2900 "volume size must be a multiple of volume block "
2902 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2906 if (props
&& zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0)
2910 /* create the dataset */
2911 ret
= zfs_ioctl(hdl
, ZFS_IOC_CREATE
, &zc
);
2913 if (ret
== 0 && type
== ZFS_TYPE_VOLUME
) {
2914 ret
= zvol_create_link(hdl
, path
);
2916 (void) zfs_standard_error(hdl
, errno
,
2917 dgettext(TEXT_DOMAIN
,
2918 "Volume successfully created, but device links "
2919 "were not created"));
2920 zcmd_free_nvlists(&zc
);
2925 zcmd_free_nvlists(&zc
);
2927 /* check for failure */
2929 char parent
[ZFS_MAXNAMELEN
];
2930 (void) parent_name(path
, parent
, sizeof (parent
));
2934 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2935 "no such parent '%s'"), parent
);
2936 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2939 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2940 "parent '%s' is not a filesystem"), parent
);
2941 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
2944 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2945 "volume block size must be power of 2 from "
2947 (uint_t
)SPA_MINBLOCKSIZE
,
2948 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
2950 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2953 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2954 "pool must be upgraded to set this "
2955 "property or value"));
2956 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
2960 * This platform can't address a volume this big.
2962 if (type
== ZFS_TYPE_VOLUME
)
2963 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
2968 return (zfs_standard_error(hdl
, errno
, errbuf
));
2976 * Destroys the given dataset. The caller must make sure that the filesystem
2977 * isn't mounted, and that there are no active dependents.
2980 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
2982 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2984 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2986 if (ZFS_IS_VOLUME(zhp
)) {
2987 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
2990 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2992 zc
.zc_objset_type
= DMU_OST_ZFS
;
2995 zc
.zc_defer_destroy
= defer
;
2996 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0) {
2997 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
2998 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3002 remove_mountpoint(zhp
);
3007 struct destroydata
{
3014 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3016 struct destroydata
*dd
= arg
;
3018 char name
[ZFS_MAXNAMELEN
];
3019 boolean_t closezhp
= dd
->closezhp
;
3022 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3023 (void) strlcat(name
, "@", sizeof (name
));
3024 (void) strlcat(name
, dd
->snapname
, sizeof (name
));
3026 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
3028 dd
->gotone
= B_TRUE
;
3032 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3033 (void) zvol_remove_link(zhp
->zfs_hdl
, name
);
3035 * NB: this is simply a best-effort. We don't want to
3036 * return an error, because then we wouldn't visit all
3041 dd
->closezhp
= B_TRUE
;
3042 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, arg
);
3049 * Destroys all snapshots with the given name in zhp & descendants.
3052 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3054 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3056 struct destroydata dd
= { 0 };
3058 dd
.snapname
= snapname
;
3059 (void) zfs_check_snap_cb(zhp
, &dd
);
3062 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3063 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3064 zhp
->zfs_name
, snapname
));
3067 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3068 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3069 zc
.zc_defer_destroy
= defer
;
3071 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY_SNAPS
, &zc
);
3075 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3076 "cannot destroy '%s@%s'"), zc
.zc_name
, snapname
);
3080 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3081 "snapshot is cloned"));
3082 return (zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
));
3085 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3094 * Clones the given dataset. The target must be of the same type as the source.
3097 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3099 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3100 char parent
[ZFS_MAXNAMELEN
];
3103 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3107 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3109 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3110 "cannot create '%s'"), target
);
3112 /* validate the target name */
3113 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3114 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3116 /* validate parents exist */
3117 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3120 (void) parent_name(target
, parent
, sizeof (parent
));
3123 if (ZFS_IS_VOLUME(zhp
)) {
3124 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3125 type
= ZFS_TYPE_VOLUME
;
3127 zc
.zc_objset_type
= DMU_OST_ZFS
;
3128 type
= ZFS_TYPE_FILESYSTEM
;
3132 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3133 zhp
, errbuf
)) == NULL
)
3136 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3144 (void) strlcpy(zc
.zc_name
, target
, sizeof (zc
.zc_name
));
3145 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
, sizeof (zc
.zc_value
));
3146 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_CREATE
, &zc
);
3148 zcmd_free_nvlists(&zc
);
3155 * The parent doesn't exist. We should have caught this
3156 * above, but there may a race condition that has since
3157 * destroyed the parent.
3159 * At this point, we don't know whether it's the source
3160 * that doesn't exist anymore, or whether the target
3161 * dataset doesn't exist.
3163 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3164 "no such parent '%s'"), parent
);
3165 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3168 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3169 "source and target pools differ"));
3170 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3174 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3177 } else if (ZFS_IS_VOLUME(zhp
)) {
3178 ret
= zvol_create_link(zhp
->zfs_hdl
, target
);
3184 typedef struct promote_data
{
3185 char cb_mountpoint
[MAXPATHLEN
];
3186 const char *cb_target
;
3187 const char *cb_errbuf
;
3188 uint64_t cb_pivot_txg
;
3192 promote_snap_cb(zfs_handle_t
*zhp
, void *data
)
3194 promote_data_t
*pd
= data
;
3196 char snapname
[MAXPATHLEN
];
3199 /* We don't care about snapshots after the pivot point */
3200 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > pd
->cb_pivot_txg
) {
3205 /* Remove the device link if it's a zvol. */
3206 if (ZFS_IS_VOLUME(zhp
))
3207 (void) zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3209 /* Check for conflicting names */
3210 (void) strlcpy(snapname
, pd
->cb_target
, sizeof (snapname
));
3211 (void) strlcat(snapname
, strchr(zhp
->zfs_name
, '@'), sizeof (snapname
));
3212 szhp
= make_dataset_handle(zhp
->zfs_hdl
, snapname
);
3215 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3216 "snapshot name '%s' from origin \n"
3217 "conflicts with '%s' from target"),
3218 zhp
->zfs_name
, snapname
);
3219 rv
= zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, pd
->cb_errbuf
);
3226 promote_snap_done_cb(zfs_handle_t
*zhp
, void *data
)
3228 promote_data_t
*pd
= data
;
3230 /* We don't care about snapshots after the pivot point */
3231 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) <= pd
->cb_pivot_txg
) {
3232 /* Create the device link if it's a zvol. */
3233 if (ZFS_IS_VOLUME(zhp
))
3234 (void) zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3242 * Promotes the given clone fs to be the clone parent.
3245 zfs_promote(zfs_handle_t
*zhp
)
3247 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3248 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3249 char parent
[MAXPATHLEN
];
3256 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3257 "cannot promote '%s'"), zhp
->zfs_name
);
3259 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3260 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3261 "snapshots can not be promoted"));
3262 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3265 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3266 if (parent
[0] == '\0') {
3267 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3268 "not a cloned filesystem"));
3269 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3271 cp
= strchr(parent
, '@');
3274 /* Walk the snapshots we will be moving */
3275 pzhp
= zfs_open(hdl
, zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
3278 pd
.cb_pivot_txg
= zfs_prop_get_int(pzhp
, ZFS_PROP_CREATETXG
);
3280 pd
.cb_target
= zhp
->zfs_name
;
3281 pd
.cb_errbuf
= errbuf
;
3282 pzhp
= zfs_open(hdl
, parent
, ZFS_TYPE_DATASET
);
3285 (void) zfs_prop_get(pzhp
, ZFS_PROP_MOUNTPOINT
, pd
.cb_mountpoint
,
3286 sizeof (pd
.cb_mountpoint
), NULL
, NULL
, 0, FALSE
);
3287 ret
= zfs_iter_snapshots(pzhp
, promote_snap_cb
, &pd
);
3293 /* issue the ioctl */
3294 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3295 sizeof (zc
.zc_value
));
3296 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3297 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3300 int save_errno
= errno
;
3302 (void) zfs_iter_snapshots(pzhp
, promote_snap_done_cb
, &pd
);
3305 switch (save_errno
) {
3308 * There is a conflicting snapshot name. We
3309 * should have caught this above, but they could
3310 * have renamed something in the mean time.
3312 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3313 "conflicting snapshot '%s' from parent '%s'"),
3314 zc
.zc_string
, parent
);
3315 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3318 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3321 (void) zfs_iter_snapshots(zhp
, promote_snap_done_cb
, &pd
);
3329 const char *cd_snapname
;
3334 zfs_create_link_cb(zfs_handle_t
*zhp
, void *arg
)
3336 struct createdata
*cd
= arg
;
3339 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3340 char name
[MAXPATHLEN
];
3342 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3343 (void) strlcat(name
, "@", sizeof (name
));
3344 (void) strlcat(name
, cd
->cd_snapname
, sizeof (name
));
3345 (void) zvol_create_link_common(zhp
->zfs_hdl
, name
,
3348 * NB: this is simply a best-effort. We don't want to
3349 * return an error, because then we wouldn't visit all
3354 ret
= zfs_iter_filesystems(zhp
, zfs_create_link_cb
, cd
);
3362 * Takes a snapshot of the given dataset.
3365 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3369 char parent
[ZFS_MAXNAMELEN
];
3371 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3375 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3376 "cannot snapshot '%s'"), path
);
3378 /* validate the target name */
3379 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3380 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3383 if ((props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3384 props
, B_FALSE
, NULL
, errbuf
)) == NULL
)
3387 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3395 /* make sure the parent exists and is of the appropriate type */
3396 delim
= strchr(path
, '@');
3397 (void) strncpy(parent
, path
, delim
- path
);
3398 parent
[delim
- path
] = '\0';
3400 if ((zhp
= zfs_open(hdl
, parent
, ZFS_TYPE_FILESYSTEM
|
3401 ZFS_TYPE_VOLUME
)) == NULL
) {
3402 zcmd_free_nvlists(&zc
);
3406 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3407 (void) strlcpy(zc
.zc_value
, delim
+1, sizeof (zc
.zc_value
));
3408 if (ZFS_IS_VOLUME(zhp
))
3409 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3411 zc
.zc_objset_type
= DMU_OST_ZFS
;
3412 zc
.zc_cookie
= recursive
;
3413 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SNAPSHOT
, &zc
);
3415 zcmd_free_nvlists(&zc
);
3418 * if it was recursive, the one that actually failed will be in
3422 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3423 "cannot create snapshot '%s@%s'"), zc
.zc_name
, zc
.zc_value
);
3425 if (ret
== 0 && recursive
) {
3426 struct createdata cd
;
3428 cd
.cd_snapname
= delim
+ 1;
3429 cd
.cd_ifexists
= B_FALSE
;
3430 (void) zfs_iter_filesystems(zhp
, zfs_create_link_cb
, &cd
);
3432 if (ret
== 0 && zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3433 ret
= zvol_create_link(zhp
->zfs_hdl
, path
);
3435 (void) zfs_standard_error(hdl
, errno
,
3436 dgettext(TEXT_DOMAIN
,
3437 "Volume successfully snapshotted, but device links "
3438 "were not created"));
3445 (void) zfs_standard_error(hdl
, errno
, errbuf
);
3453 * Destroy any more recent snapshots. We invoke this callback on any dependents
3454 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3455 * is a dependent and we should just destroy it without checking the transaction
3458 typedef struct rollback_data
{
3459 const char *cb_target
; /* the snapshot */
3460 uint64_t cb_create
; /* creation time reference */
3462 boolean_t cb_dependent
;
3467 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3469 rollback_data_t
*cbp
= data
;
3471 if (!cbp
->cb_dependent
) {
3472 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3473 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3474 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3478 cbp
->cb_dependent
= B_TRUE
;
3479 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3480 rollback_destroy
, cbp
);
3481 cbp
->cb_dependent
= B_FALSE
;
3483 logstr
= zhp
->zfs_hdl
->libzfs_log_str
;
3484 zhp
->zfs_hdl
->libzfs_log_str
= NULL
;
3485 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3486 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
3489 /* We must destroy this clone; first unmount it */
3490 prop_changelist_t
*clp
;
3492 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3493 cbp
->cb_force
? MS_FORCE
: 0);
3494 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3495 cbp
->cb_error
= B_TRUE
;
3499 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3500 cbp
->cb_error
= B_TRUE
;
3502 changelist_remove(clp
, zhp
->zfs_name
);
3503 (void) changelist_postfix(clp
);
3504 changelist_free(clp
);
3512 * Given a dataset, rollback to a specific snapshot, discarding any
3513 * data changes since then and making it the active dataset.
3515 * Any snapshots more recent than the target are destroyed, along with
3519 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3521 rollback_data_t cb
= { 0 };
3523 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3524 boolean_t restore_resv
= 0;
3525 uint64_t old_volsize
= 0, new_volsize
;
3526 zfs_prop_t resv_prop
= { 0 };
3528 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3529 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3532 * Destroy all recent snapshots and its dependends.
3534 cb
.cb_force
= force
;
3535 cb
.cb_target
= snap
->zfs_name
;
3536 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3537 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3543 * Now that we have verified that the snapshot is the latest,
3544 * rollback to the given snapshot.
3547 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3548 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3550 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3552 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3554 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3557 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3559 if (ZFS_IS_VOLUME(zhp
))
3560 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3562 zc
.zc_objset_type
= DMU_OST_ZFS
;
3565 * We rely on zfs_iter_children() to verify that there are no
3566 * newer snapshots for the given dataset. Therefore, we can
3567 * simply pass the name on to the ioctl() call. There is still
3568 * an unlikely race condition where the user has taken a
3569 * snapshot since we verified that this was the most recent.
3572 if ((err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_ROLLBACK
, &zc
)) != 0) {
3573 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3574 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3580 * For volumes, if the pre-rollback volsize matched the pre-
3581 * rollback reservation and the volsize has changed then set
3582 * the reservation property to the post-rollback volsize.
3583 * Make a new handle since the rollback closed the dataset.
3585 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3586 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3587 if ((err
= zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3592 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3593 if (old_volsize
!= new_volsize
)
3594 err
= zfs_prop_set_int(zhp
, resv_prop
,
3603 * Iterate over all dependents for a given dataset. This includes both
3604 * hierarchical dependents (children) and data dependents (snapshots and
3605 * clones). The bulk of the processing occurs in get_dependents() in
3609 zfs_iter_dependents(zfs_handle_t
*zhp
, boolean_t allowrecursion
,
3610 zfs_iter_f func
, void *data
)
3615 zfs_handle_t
*child
;
3618 if (get_dependents(zhp
->zfs_hdl
, allowrecursion
, zhp
->zfs_name
,
3619 &dependents
, &count
) != 0)
3622 for (i
= 0; i
< count
; i
++) {
3623 if ((child
= make_dataset_handle(zhp
->zfs_hdl
,
3624 dependents
[i
])) == NULL
)
3627 if ((ret
= func(child
, data
)) != 0)
3631 for (i
= 0; i
< count
; i
++)
3632 free(dependents
[i
]);
3639 * Renames the given dataset.
3642 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
)
3645 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3647 prop_changelist_t
*cl
= NULL
;
3648 zfs_handle_t
*zhrp
= NULL
;
3649 char *parentname
= NULL
;
3650 char parent
[ZFS_MAXNAMELEN
];
3651 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3654 /* if we have the same exact name, just return success */
3655 if (strcmp(zhp
->zfs_name
, target
) == 0)
3658 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3659 "cannot rename to '%s'"), target
);
3662 * Make sure the target name is valid
3664 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3665 if ((strchr(target
, '@') == NULL
) ||
3668 * Snapshot target name is abbreviated,
3669 * reconstruct full dataset name
3671 (void) strlcpy(parent
, zhp
->zfs_name
,
3673 delim
= strchr(parent
, '@');
3674 if (strchr(target
, '@') == NULL
)
3678 (void) strlcat(parent
, target
, sizeof (parent
));
3682 * Make sure we're renaming within the same dataset.
3684 delim
= strchr(target
, '@');
3685 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3686 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3688 "snapshots must be part of same "
3690 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3694 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3695 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3698 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3699 "recursive rename must be a snapshot"));
3700 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3703 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3704 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3706 /* validate parents */
3707 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
3710 /* make sure we're in the same pool */
3711 verify((delim
= strchr(target
, '/')) != NULL
);
3712 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3713 zhp
->zfs_name
[delim
- target
] != '/') {
3714 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3715 "datasets must be within same pool"));
3716 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3719 /* new name cannot be a child of the current dataset name */
3720 if (is_descendant(zhp
->zfs_name
, target
)) {
3721 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3722 "New dataset name cannot be a descendant of "
3723 "current dataset name"));
3724 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3728 (void) snprintf(errbuf
, sizeof (errbuf
),
3729 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3731 if (getzoneid() == GLOBAL_ZONEID
&&
3732 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3733 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3734 "dataset is used in a non-global zone"));
3735 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3739 struct destroydata dd
;
3741 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3742 if (parentname
== NULL
) {
3746 delim
= strchr(parentname
, '@');
3748 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3754 dd
.snapname
= delim
+ 1;
3755 dd
.gotone
= B_FALSE
;
3756 dd
.closezhp
= B_TRUE
;
3758 /* We remove any zvol links prior to renaming them */
3759 ret
= zfs_iter_filesystems(zhrp
, zfs_check_snap_cb
, &dd
);
3764 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0)) == NULL
)
3767 if (changelist_haszonedchild(cl
)) {
3768 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3769 "child dataset with inherited mountpoint is used "
3770 "in a non-global zone"));
3771 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3776 if ((ret
= changelist_prefix(cl
)) != 0)
3780 if (ZFS_IS_VOLUME(zhp
))
3781 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3783 zc
.zc_objset_type
= DMU_OST_ZFS
;
3785 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3786 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3788 zc
.zc_cookie
= recursive
;
3790 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3792 * if it was recursive, the one that actually failed will
3795 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3796 "cannot rename '%s'"), zc
.zc_name
);
3798 if (recursive
&& errno
== EEXIST
) {
3799 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3800 "a child dataset already has a snapshot "
3801 "with the new name"));
3802 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3804 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3808 * On failure, we still want to remount any filesystems that
3809 * were previously mounted, so we don't alter the system state.
3812 struct createdata cd
;
3814 /* only create links for datasets that had existed */
3815 cd
.cd_snapname
= delim
+ 1;
3816 cd
.cd_ifexists
= B_TRUE
;
3817 (void) zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3820 (void) changelist_postfix(cl
);
3824 struct createdata cd
;
3826 /* only create links for datasets that had existed */
3827 cd
.cd_snapname
= strchr(target
, '@') + 1;
3828 cd
.cd_ifexists
= B_TRUE
;
3829 ret
= zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3832 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3833 ret
= changelist_postfix(cl
);
3845 changelist_free(cl
);
3851 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3852 * and wait briefly for udev to create the /dev link.
3855 zvol_create_link(libzfs_handle_t
*hdl
, const char *dataset
)
3857 return (zvol_create_link_common(hdl
, dataset
, B_FALSE
));
3861 zvol_create_link_common(libzfs_handle_t
*hdl
, const char *dataset
, int ifexists
)
3863 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3864 char path
[MAXPATHLEN
];
3867 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3870 * Issue the appropriate ioctl.
3872 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CREATE_MINOR
, &zc
) != 0) {
3876 * Silently ignore the case where the link already
3877 * exists. This allows 'zfs volinit' to be run multiple
3878 * times without errors.
3884 * Dataset does not exist in the kernel. If we
3885 * don't care (see zfs_rename), then ignore the
3895 return (zfs_standard_error_fmt(hdl
, errno
,
3896 dgettext(TEXT_DOMAIN
, "cannot create device links "
3897 "for '%s'"), dataset
));
3902 * Wait up to 10 seconds for udev to create the device.
3904 (void) snprintf(path
, sizeof (path
), "%s/%s", ZVOL_DIR
, dataset
);
3905 error
= zpool_label_disk_wait(path
, 10000);
3907 (void) printf(gettext("%s may not be immediately "
3908 "available\n"), path
);
3914 * Remove a minor node for the given zvol and the associated /dev links.
3917 zvol_remove_link(libzfs_handle_t
*hdl
, const char *dataset
)
3919 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3921 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3923 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_REMOVE_MINOR
, &zc
) != 0) {
3927 * Silently ignore the case where the link no longer
3928 * exists, so that 'zfs volfini' can be run multiple
3929 * times without errors.
3934 return (zfs_standard_error_fmt(hdl
, errno
,
3935 dgettext(TEXT_DOMAIN
, "cannot remove device "
3936 "links for '%s'"), dataset
));
3944 zfs_get_user_props(zfs_handle_t
*zhp
)
3946 return (zhp
->zfs_user_props
);
3950 * This function is used by 'zfs list' to determine the exact set of columns to
3951 * display, and their maximum widths. This does two main things:
3953 * - If this is a list of all properties, then expand the list to include
3954 * all native properties, and set a flag so that for each dataset we look
3955 * for new unique user properties and add them to the list.
3957 * - For non fixed-width properties, keep track of the maximum width seen
3958 * so that we can size the column appropriately. If the user has
3959 * requested received property values, we also need to compute the width
3960 * of the RECEIVED column.
3963 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
)
3965 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3966 zprop_list_t
*entry
;
3967 zprop_list_t
**last
, **start
;
3968 nvlist_t
*userprops
, *propval
;
3971 char buf
[ZFS_MAXPROPLEN
];
3973 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
3976 userprops
= zfs_get_user_props(zhp
);
3979 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
3981 * Go through and add any user properties as necessary. We
3982 * start by incrementing our list pointer to the first
3983 * non-native property.
3986 while (*start
!= NULL
) {
3987 if ((*start
)->pl_prop
== ZPROP_INVAL
)
3989 start
= &(*start
)->pl_next
;
3993 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
3995 * See if we've already found this property in our list.
3997 for (last
= start
; *last
!= NULL
;
3998 last
= &(*last
)->pl_next
) {
3999 if (strcmp((*last
)->pl_user_prop
,
4000 nvpair_name(elem
)) == 0)
4004 if (*last
== NULL
) {
4005 if ((entry
= zfs_alloc(hdl
,
4006 sizeof (zprop_list_t
))) == NULL
||
4007 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4008 nvpair_name(elem
)))) == NULL
) {
4013 entry
->pl_prop
= ZPROP_INVAL
;
4014 entry
->pl_width
= strlen(nvpair_name(elem
));
4015 entry
->pl_all
= B_TRUE
;
4022 * Now go through and check the width of any non-fixed columns
4024 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4025 if (entry
->pl_fixed
)
4028 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4029 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4030 buf
, sizeof (buf
), NULL
, NULL
, 0, B_FALSE
) == 0) {
4031 if (strlen(buf
) > entry
->pl_width
)
4032 entry
->pl_width
= strlen(buf
);
4034 if (received
&& zfs_prop_get_recvd(zhp
,
4035 zfs_prop_to_name(entry
->pl_prop
),
4036 buf
, sizeof (buf
), B_FALSE
) == 0)
4037 if (strlen(buf
) > entry
->pl_recvd_width
)
4038 entry
->pl_recvd_width
= strlen(buf
);
4040 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4042 verify(nvlist_lookup_string(propval
,
4043 ZPROP_VALUE
, &strval
) == 0);
4044 if (strlen(strval
) > entry
->pl_width
)
4045 entry
->pl_width
= strlen(strval
);
4047 if (received
&& zfs_prop_get_recvd(zhp
,
4048 entry
->pl_user_prop
,
4049 buf
, sizeof (buf
), B_FALSE
) == 0)
4050 if (strlen(buf
) > entry
->pl_recvd_width
)
4051 entry
->pl_recvd_width
= strlen(buf
);
4059 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4064 * Keep a reference to the props-table against which we prune the
4067 zhp
->zfs_props_table
= props
;
4069 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4072 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4073 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4076 * User properties will result in ZPROP_INVAL, and since we
4077 * only know how to prune standard ZFS properties, we always
4078 * leave these in the list. This can also happen if we
4079 * encounter an unknown DSL property (when running older
4080 * software, for example).
4082 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4083 (void) nvlist_remove(zhp
->zfs_props
,
4084 nvpair_name(curr
), nvpair_type(curr
));
4090 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4091 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4093 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4094 nvlist_t
*nvlist
= NULL
;
4097 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4098 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4099 zc
.zc_cookie
= (uint64_t)cmd
;
4101 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4102 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4103 (void) no_memory(hdl
);
4109 case ZFS_SMB_ACL_ADD
:
4110 case ZFS_SMB_ACL_REMOVE
:
4111 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4113 case ZFS_SMB_ACL_RENAME
:
4114 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4116 (void) no_memory(hdl
);
4119 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4121 (void) no_memory(hdl
);
4124 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4125 nvlist_free(nvlist
);
4129 case ZFS_SMB_ACL_PURGE
:
4134 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4136 nvlist_free(nvlist
);
4141 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4142 char *path
, char *resource
)
4144 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4149 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4150 char *path
, char *resource
)
4152 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4157 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4159 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4164 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4165 char *oldname
, char *newname
)
4167 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4172 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4173 zfs_userspace_cb_t func
, void *arg
)
4175 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4177 zfs_useracct_t buf
[100];
4179 (void) strncpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4181 zc
.zc_objset_type
= type
;
4182 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4186 zfs_useracct_t
*zua
= buf
;
4188 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4189 error
= ioctl(zhp
->zfs_hdl
->libzfs_fd
,
4190 ZFS_IOC_USERSPACE_MANY
, &zc
);
4191 if (error
|| zc
.zc_nvlist_dst_size
== 0)
4194 while (zc
.zc_nvlist_dst_size
> 0) {
4195 error
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4200 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4208 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4209 boolean_t recursive
, boolean_t temphold
, boolean_t enoent_ok
,
4210 int cleanup_fd
, uint64_t dsobj
, uint64_t createtxg
)
4212 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4213 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4215 ASSERT(!recursive
|| dsobj
== 0);
4217 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4218 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4219 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4220 >= sizeof (zc
.zc_string
))
4221 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4222 zc
.zc_cookie
= recursive
;
4223 zc
.zc_temphold
= temphold
;
4224 zc
.zc_cleanup_fd
= cleanup_fd
;
4225 zc
.zc_sendobj
= dsobj
;
4226 zc
.zc_createtxg
= createtxg
;
4228 if (zfs_ioctl(hdl
, ZFS_IOC_HOLD
, &zc
) != 0) {
4229 char errbuf
[ZFS_MAXNAMELEN
+32];
4232 * if it was recursive, the one that actually failed will be in
4235 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4236 "cannot hold '%s@%s'"), zc
.zc_name
, snapname
);
4240 * Temporary tags wind up having the ds object id
4241 * prepended. So even if we passed the length check
4242 * above, it's still possible for the tag to wind
4243 * up being slightly too long.
4245 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
));
4247 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4248 "pool must be upgraded"));
4249 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4251 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4253 return (zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
));
4259 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4267 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4268 boolean_t recursive
)
4270 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4271 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4273 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4274 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
4275 if (strlcpy(zc
.zc_string
, tag
, sizeof (zc
.zc_string
))
4276 >= sizeof (zc
.zc_string
))
4277 return (zfs_error(hdl
, EZFS_TAGTOOLONG
, tag
));
4278 zc
.zc_cookie
= recursive
;
4280 if (zfs_ioctl(hdl
, ZFS_IOC_RELEASE
, &zc
) != 0) {
4281 char errbuf
[ZFS_MAXNAMELEN
+32];
4284 * if it was recursive, the one that actually failed will be in
4287 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4288 "cannot release '%s' from '%s@%s'"), tag
, zc
.zc_name
,
4292 return (zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
));
4294 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4295 "pool must be upgraded"));
4296 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4298 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4300 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4308 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4310 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4311 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4315 char errbuf
[ZFS_MAXNAMELEN
+32];
4317 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4318 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4322 nvbuf
= malloc(nvsz
);
4323 if (nvbuf
== NULL
) {
4324 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4328 zc
.zc_nvlist_dst_size
= nvsz
;
4329 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4331 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4333 if (zfs_ioctl(hdl
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4334 (void) snprintf(errbuf
, sizeof (errbuf
),
4335 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4340 nvsz
= zc
.zc_nvlist_dst_size
;
4344 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4345 "pool must be upgraded"));
4346 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4349 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4352 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4355 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4360 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4362 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4363 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4365 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4375 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4377 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4378 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4380 char errbuf
[ZFS_MAXNAMELEN
+32];
4384 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4385 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4387 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4390 nvbuf
= malloc(nvsz
);
4392 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4395 zc
.zc_nvlist_src_size
= nvsz
;
4396 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4397 zc
.zc_perm_action
= un
;
4399 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4401 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4402 (void) snprintf(errbuf
, sizeof (errbuf
),
4403 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4408 "pool must be upgraded"));
4409 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4412 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4415 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4418 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4429 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4431 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
4432 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4436 char errbuf
[ZFS_MAXNAMELEN
+32];
4438 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
4442 nvbuf
= malloc(nvsz
);
4443 if (nvbuf
== NULL
) {
4444 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4448 zc
.zc_nvlist_dst_size
= nvsz
;
4449 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4451 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, ZFS_MAXNAMELEN
);
4453 if (zfs_ioctl(hdl
, ZFS_IOC_GET_HOLDS
, &zc
) != 0) {
4454 (void) snprintf(errbuf
, sizeof (errbuf
),
4455 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4460 nvsz
= zc
.zc_nvlist_dst_size
;
4464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4465 "pool must be upgraded"));
4466 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4469 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4472 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4475 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4480 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4482 (void) snprintf(errbuf
, sizeof (errbuf
),
4483 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4485 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4495 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4498 uint64_t nblocks
, volblocksize
;
4502 if (nvlist_lookup_string(props
,
4503 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4504 ncopies
= atoi(strval
);
4507 if (nvlist_lookup_uint64(props
,
4508 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4509 &volblocksize
) != 0)
4510 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4511 nblocks
= volsize
/volblocksize
;
4512 /* start with metadnode L0-L6 */
4514 /* calculate number of indirects */
4515 while (nblocks
> 1) {
4516 nblocks
+= DNODES_PER_LEVEL
- 1;
4517 nblocks
/= DNODES_PER_LEVEL
;
4520 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4523 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4524 * compressed, but in practice they compress down to about
4527 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;