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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
30 #include <libdevinfo.h>
40 #include <sys/mntent.h>
41 #include <sys/mnttab.h>
42 #include <sys/mount.h>
54 #include "zfs_namecheck.h"
56 #include "libzfs_impl.h"
57 #include "zfs_deleg.h"
59 static int zvol_create_link_common(libzfs_handle_t
*, const char *, int);
62 * Given a single type (not a mask of types), return the type in a human
66 zfs_type_to_name(zfs_type_t type
)
69 case ZFS_TYPE_FILESYSTEM
:
70 return (dgettext(TEXT_DOMAIN
, "filesystem"));
71 case ZFS_TYPE_SNAPSHOT
:
72 return (dgettext(TEXT_DOMAIN
, "snapshot"));
74 return (dgettext(TEXT_DOMAIN
, "volume"));
81 * Given a path and mask of ZFS types, return a string describing this dataset.
82 * This is used when we fail to open a dataset and we cannot get an exact type.
83 * We guess what the type would have been based on the path and the mask of
87 path_to_str(const char *path
, int types
)
90 * When given a single type, always report the exact type.
92 if (types
== ZFS_TYPE_SNAPSHOT
)
93 return (dgettext(TEXT_DOMAIN
, "snapshot"));
94 if (types
== ZFS_TYPE_FILESYSTEM
)
95 return (dgettext(TEXT_DOMAIN
, "filesystem"));
96 if (types
== ZFS_TYPE_VOLUME
)
97 return (dgettext(TEXT_DOMAIN
, "volume"));
100 * The user is requesting more than one type of dataset. If this is the
101 * case, consult the path itself. If we're looking for a snapshot, and
102 * a '@' is found, then report it as "snapshot". Otherwise, remove the
103 * snapshot attribute and try again.
105 if (types
& ZFS_TYPE_SNAPSHOT
) {
106 if (strchr(path
, '@') != NULL
)
107 return (dgettext(TEXT_DOMAIN
, "snapshot"));
108 return (path_to_str(path
, types
& ~ZFS_TYPE_SNAPSHOT
));
113 * The user has requested either filesystems or volumes.
114 * We have no way of knowing a priori what type this would be, so always
115 * report it as "filesystem" or "volume", our two primitive types.
117 if (types
& ZFS_TYPE_FILESYSTEM
)
118 return (dgettext(TEXT_DOMAIN
, "filesystem"));
120 assert(types
& ZFS_TYPE_VOLUME
);
121 return (dgettext(TEXT_DOMAIN
, "volume"));
125 * Validate a ZFS path. This is used even before trying to open the dataset, to
126 * provide a more meaningful error message. We place a more useful message in
127 * 'buf' detailing exactly why the name was not valid.
130 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
136 if (dataset_namecheck(path
, &why
, &what
) != 0) {
139 case NAME_ERR_TOOLONG
:
140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
141 "name is too long"));
144 case NAME_ERR_LEADING_SLASH
:
145 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
146 "leading slash in name"));
149 case NAME_ERR_EMPTY_COMPONENT
:
150 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
151 "empty component in name"));
154 case NAME_ERR_TRAILING_SLASH
:
155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
156 "trailing slash in name"));
159 case NAME_ERR_INVALCHAR
:
161 dgettext(TEXT_DOMAIN
, "invalid character "
162 "'%c' in name"), what
);
165 case NAME_ERR_MULTIPLE_AT
:
166 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
167 "multiple '@' delimiters in name"));
170 case NAME_ERR_NOLETTER
:
171 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
172 "pool doesn't begin with a letter"));
175 case NAME_ERR_RESERVED
:
176 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
177 "name is reserved"));
180 case NAME_ERR_DISKLIKE
:
181 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
182 "reserved disk name"));
190 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
192 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
193 "snapshot delimiter '@' in filesystem name"));
197 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
199 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
200 "missing '@' delimiter in snapshot name"));
204 if (modifying
&& strchr(path
, '%') != NULL
) {
206 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
207 "invalid character %c in name"), '%');
215 zfs_name_valid(const char *name
, zfs_type_t type
)
217 if (type
== ZFS_TYPE_POOL
)
218 return (zpool_name_valid(NULL
, B_FALSE
, name
));
219 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
223 * This function takes the raw DSL properties, and filters out the user-defined
224 * properties into a separate nvlist.
227 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
229 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
234 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
235 (void) no_memory(hdl
);
240 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
241 if (!zfs_prop_user(nvpair_name(elem
)))
244 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
245 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
247 (void) no_memory(hdl
);
255 static zpool_handle_t
*
256 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
258 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
261 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
262 if (hdl
->libzfs_pool_handles
!= NULL
)
263 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
264 hdl
->libzfs_pool_handles
= zph
;
269 static zpool_handle_t
*
270 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
272 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
273 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
275 while ((zph
!= NULL
) &&
276 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
277 zph
= zph
->zpool_next
;
282 * Returns a handle to the pool that contains the provided dataset.
283 * If a handle to that pool already exists then that handle is returned.
284 * Otherwise, a new handle is created and added to the list of handles.
286 static zpool_handle_t
*
287 zpool_handle(zfs_handle_t
*zhp
)
293 len
= strcspn(zhp
->zfs_name
, "/@") + 1;
294 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
295 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
297 zph
= zpool_find_handle(zhp
, pool_name
, len
);
299 zph
= zpool_add_handle(zhp
, pool_name
);
306 zpool_free_handles(libzfs_handle_t
*hdl
)
308 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
310 while (zph
!= NULL
) {
311 next
= zph
->zpool_next
;
315 hdl
->libzfs_pool_handles
= NULL
;
319 * Utility function to gather stats (objset and zpl) for the given object.
322 get_stats(zfs_handle_t
*zhp
)
324 zfs_cmd_t zc
= { 0 };
325 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
326 nvlist_t
*allprops
, *userprops
;
328 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
330 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
333 while (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
334 if (errno
== ENOMEM
) {
335 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
336 zcmd_free_nvlists(&zc
);
340 zcmd_free_nvlists(&zc
);
345 zhp
->zfs_dmustats
= zc
.zc_objset_stats
; /* structure assignment */
347 if (zcmd_read_dst_nvlist(hdl
, &zc
, &allprops
) != 0) {
348 zcmd_free_nvlists(&zc
);
352 zcmd_free_nvlists(&zc
);
354 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
355 nvlist_free(allprops
);
359 nvlist_free(zhp
->zfs_props
);
360 nvlist_free(zhp
->zfs_user_props
);
362 zhp
->zfs_props
= allprops
;
363 zhp
->zfs_user_props
= userprops
;
369 * Refresh the properties currently stored in the handle.
372 zfs_refresh_properties(zfs_handle_t
*zhp
)
374 (void) get_stats(zhp
);
378 * Makes a handle from the given dataset name. Used by zfs_open() and
379 * zfs_iter_* to create child handles on the fly.
382 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
384 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
393 * Preserve history log string.
394 * any changes performed here will be
395 * logged as an internal event.
397 logstr
= zhp
->zfs_hdl
->libzfs_log_str
;
398 zhp
->zfs_hdl
->libzfs_log_str
= NULL
;
400 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
402 if (get_stats(zhp
) != 0) {
403 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
408 if (zhp
->zfs_dmustats
.dds_inconsistent
) {
409 zfs_cmd_t zc
= { 0 };
412 * If it is dds_inconsistent, then we've caught it in
413 * the middle of a 'zfs receive' or 'zfs destroy', and
414 * it is inconsistent from the ZPL's point of view, so
415 * can't be mounted. However, it could also be that we
416 * have crashed in the middle of one of those
417 * operations, in which case we need to get rid of the
418 * inconsistent state. We do that by either rolling
419 * back to the previous snapshot (which will fail if
420 * there is none), or destroying the filesystem. Note
421 * that if we are still in the middle of an active
422 * 'receive' or 'destroy', then the rollback and destroy
423 * will fail with EBUSY and we will drive on as usual.
426 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
428 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
) {
429 (void) zvol_remove_link(hdl
, zhp
->zfs_name
);
430 zc
.zc_objset_type
= DMU_OST_ZVOL
;
432 zc
.zc_objset_type
= DMU_OST_ZFS
;
436 * If we can successfully destroy it, pretend that it
439 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
) == 0) {
440 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
445 /* If we can successfully roll it back, reget the stats */
446 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_ROLLBACK
, &zc
) == 0)
451 * We've managed to open the dataset and gather statistics. Determine
452 * the high-level type.
454 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
455 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
456 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
457 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
461 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
462 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
463 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
464 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
465 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
466 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
468 abort(); /* we should never see any other types */
470 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
471 zhp
->zpool_hdl
= zpool_handle(zhp
);
476 * Opens the given snapshot, filesystem, or volume. The 'types'
477 * argument is a mask of acceptable types. The function will print an
478 * appropriate error message and return NULL if it can't be opened.
481 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
486 (void) snprintf(errbuf
, sizeof (errbuf
),
487 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
490 * Validate the name before we even try to open it.
492 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_DATASET
, B_FALSE
)) {
493 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
494 "invalid dataset name"));
495 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
500 * Try to get stats for the dataset, which will tell us if it exists.
503 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
504 (void) zfs_standard_error(hdl
, errno
, errbuf
);
508 if (!(types
& zhp
->zfs_type
)) {
509 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
518 * Release a ZFS handle. Nothing to do but free the associated memory.
521 zfs_close(zfs_handle_t
*zhp
)
523 if (zhp
->zfs_mntopts
)
524 free(zhp
->zfs_mntopts
);
525 nvlist_free(zhp
->zfs_props
);
526 nvlist_free(zhp
->zfs_user_props
);
531 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
533 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
535 if (zpool_handle
== NULL
)
538 *spa_version
= zpool_get_prop_int(zpool_handle
,
539 ZPOOL_PROP_VERSION
, NULL
);
544 * The choice of reservation property depends on the SPA version.
547 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
551 if (zfs_spa_version(zhp
, &spa_version
) < 0)
554 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
555 *resv_prop
= ZFS_PROP_REFRESERVATION
;
557 *resv_prop
= ZFS_PROP_RESERVATION
;
563 * Given an nvlist of properties to set, validates that they are correct, and
564 * parses any numeric properties (index, boolean, etc) if they are specified as
568 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
569 uint64_t zoned
, zfs_handle_t
*zhp
, const char *errbuf
)
576 int chosen_normal
= -1;
579 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
580 (void) no_memory(hdl
);
585 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
586 const char *propname
= nvpair_name(elem
);
589 * Make sure this property is valid and applies to this type.
591 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
592 if (!zfs_prop_user(propname
)) {
593 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
594 "invalid property '%s'"), propname
);
595 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
600 * If this is a user property, make sure it's a
601 * string, and that it's less than ZAP_MAXNAMELEN.
603 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
604 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
605 "'%s' must be a string"), propname
);
606 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
610 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
611 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
612 "property name '%s' is too long"),
614 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
618 (void) nvpair_value_string(elem
, &strval
);
619 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
620 (void) no_memory(hdl
);
626 if (type
== ZFS_TYPE_SNAPSHOT
) {
627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
628 "this property can not be modified for snapshots"));
629 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
633 if (!zfs_prop_valid_for_type(prop
, type
)) {
635 dgettext(TEXT_DOMAIN
, "'%s' does not "
636 "apply to datasets of this type"), propname
);
637 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
641 if (zfs_prop_readonly(prop
) &&
642 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
644 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
646 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
650 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
651 &strval
, &intval
, errbuf
) != 0)
655 * Perform some additional checks for specific properties.
658 case ZFS_PROP_VERSION
:
664 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
665 if (intval
< version
) {
666 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
667 "Can not downgrade; already at version %u"),
669 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
675 case ZFS_PROP_RECORDSIZE
:
676 case ZFS_PROP_VOLBLOCKSIZE
:
677 /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */
678 if (intval
< SPA_MINBLOCKSIZE
||
679 intval
> SPA_MAXBLOCKSIZE
|| !ISP2(intval
)) {
680 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
681 "'%s' must be power of 2 from %u "
683 (uint_t
)SPA_MINBLOCKSIZE
,
684 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
685 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
690 case ZFS_PROP_SHAREISCSI
:
691 if (strcmp(strval
, "off") != 0 &&
692 strcmp(strval
, "on") != 0 &&
693 strcmp(strval
, "type=disk") != 0) {
694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
695 "'%s' must be 'on', 'off', or 'type=disk'"),
697 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
703 case ZFS_PROP_MOUNTPOINT
:
707 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
708 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
711 if (mountpoint_namecheck(strval
, &why
)) {
713 case NAME_ERR_LEADING_SLASH
:
715 dgettext(TEXT_DOMAIN
,
716 "'%s' must be an absolute path, "
717 "'none', or 'legacy'"), propname
);
719 case NAME_ERR_TOOLONG
:
721 dgettext(TEXT_DOMAIN
,
722 "component of '%s' is too long"),
726 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
733 case ZFS_PROP_SHARESMB
:
734 case ZFS_PROP_SHARENFS
:
736 * For the mountpoint and sharenfs or sharesmb
737 * properties, check if it can be set in a
738 * global/non-global zone based on
739 * the zoned property value:
741 * global zone non-global zone
742 * --------------------------------------------------
743 * zoned=on mountpoint (no) mountpoint (yes)
744 * sharenfs (no) sharenfs (no)
745 * sharesmb (no) sharesmb (no)
747 * zoned=off mountpoint (yes) N/A
752 if (getzoneid() == GLOBAL_ZONEID
) {
753 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
754 "'%s' cannot be set on "
755 "dataset in a non-global zone"),
757 (void) zfs_error(hdl
, EZFS_ZONED
,
760 } else if (prop
== ZFS_PROP_SHARENFS
||
761 prop
== ZFS_PROP_SHARESMB
) {
762 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
763 "'%s' cannot be set in "
764 "a non-global zone"), propname
);
765 (void) zfs_error(hdl
, EZFS_ZONED
,
769 } else if (getzoneid() != GLOBAL_ZONEID
) {
771 * If zoned property is 'off', this must be in
772 * a globle zone. If not, something is wrong.
774 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
775 "'%s' cannot be set while dataset "
776 "'zoned' property is set"), propname
);
777 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
782 * At this point, it is legitimate to set the
783 * property. Now we want to make sure that the
784 * property value is valid if it is sharenfs.
786 if ((prop
== ZFS_PROP_SHARENFS
||
787 prop
== ZFS_PROP_SHARESMB
) &&
788 strcmp(strval
, "on") != 0 &&
789 strcmp(strval
, "off") != 0) {
790 zfs_share_proto_t proto
;
792 if (prop
== ZFS_PROP_SHARESMB
)
798 * Must be an valid sharing protocol
799 * option string so init the libshare
800 * in order to enable the parser and
801 * then parse the options. We use the
802 * control API since we don't care about
803 * the current configuration and don't
804 * want the overhead of loading it
805 * until we actually do something.
808 if (zfs_init_libshare(hdl
,
809 SA_INIT_CONTROL_API
) != SA_OK
) {
811 * An error occurred so we can't do
814 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
815 "'%s' cannot be set: problem "
816 "in share initialization"),
818 (void) zfs_error(hdl
, EZFS_BADPROP
,
823 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
825 * There was an error in parsing so
826 * deal with it by issuing an error
827 * message and leaving after
828 * uninitializing the the libshare
831 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
832 "'%s' cannot be set to invalid "
833 "options"), propname
);
834 (void) zfs_error(hdl
, EZFS_BADPROP
,
836 zfs_uninit_libshare(hdl
);
839 zfs_uninit_libshare(hdl
);
843 case ZFS_PROP_UTF8ONLY
:
844 chosen_utf
= (int)intval
;
846 case ZFS_PROP_NORMALIZE
:
847 chosen_normal
= (int)intval
;
852 * For changes to existing volumes, we have some additional
855 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
856 uint64_t volsize
= zfs_prop_get_int(zhp
,
858 uint64_t blocksize
= zfs_prop_get_int(zhp
,
859 ZFS_PROP_VOLBLOCKSIZE
);
863 case ZFS_PROP_RESERVATION
:
864 case ZFS_PROP_REFRESERVATION
:
865 if (intval
> volsize
) {
866 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
867 "'%s' is greater than current "
868 "volume size"), propname
);
869 (void) zfs_error(hdl
, EZFS_BADPROP
,
875 case ZFS_PROP_VOLSIZE
:
876 if (intval
% blocksize
!= 0) {
877 zfs_nicenum(blocksize
, buf
,
879 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
880 "'%s' must be a multiple of "
881 "volume block size (%s)"),
883 (void) zfs_error(hdl
, EZFS_BADPROP
,
889 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
890 "'%s' cannot be zero"),
892 (void) zfs_error(hdl
, EZFS_BADPROP
,
902 * If normalization was chosen, but no UTF8 choice was made,
903 * enforce rejection of non-UTF8 names.
905 * If normalization was chosen, but rejecting non-UTF8 names
906 * was explicitly not chosen, it is an error.
908 if (chosen_normal
> 0 && chosen_utf
< 0) {
909 if (nvlist_add_uint64(ret
,
910 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
911 (void) no_memory(hdl
);
914 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
915 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
916 "'%s' must be set 'on' if normalization chosen"),
917 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
918 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
923 * If this is an existing volume, and someone is setting the volsize,
924 * make sure that it matches the reservation, or add it if necessary.
926 if (zhp
!= NULL
&& type
== ZFS_TYPE_VOLUME
&&
927 nvlist_lookup_uint64(ret
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
929 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
931 uint64_t old_reservation
;
932 uint64_t new_reservation
;
933 zfs_prop_t resv_prop
;
935 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
937 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
939 if (old_volsize
== old_reservation
&&
940 nvlist_lookup_uint64(ret
, zfs_prop_to_name(resv_prop
),
941 &new_reservation
) != 0) {
942 if (nvlist_add_uint64(ret
,
943 zfs_prop_to_name(resv_prop
), intval
) != 0) {
944 (void) no_memory(hdl
);
957 zfs_get_perm_who(const char *who
, zfs_deleg_who_type_t
*who_type
,
964 if (*who_type
== ZFS_DELEG_EVERYONE
|| *who_type
== ZFS_DELEG_CREATE
||
965 *who_type
== ZFS_DELEG_NAMED_SET
) {
969 if (who
== NULL
&& !(*who_type
== ZFS_DELEG_EVERYONE
))
970 return (EZFS_BADWHO
);
972 if (*who_type
== ZFS_DELEG_WHO_UNKNOWN
&&
973 strcmp(who
, "everyone") == 0) {
975 *who_type
= ZFS_DELEG_EVERYONE
;
982 if ((*who_type
== ZFS_DELEG_USER
) && pwd
) {
983 *ret_who
= pwd
->pw_uid
;
984 } else if ((*who_type
== ZFS_DELEG_GROUP
) && grp
) {
985 *ret_who
= grp
->gr_gid
;
987 *ret_who
= pwd
->pw_uid
;
988 *who_type
= ZFS_DELEG_USER
;
990 *ret_who
= grp
->gr_gid
;
991 *who_type
= ZFS_DELEG_GROUP
;
995 id
= strtol(who
, &end
, 10);
996 if (errno
!= 0 || *end
!= '\0') {
997 return (EZFS_BADWHO
);
1000 if (*who_type
== ZFS_DELEG_WHO_UNKNOWN
)
1001 *who_type
= ZFS_DELEG_USER
;
1009 zfs_perms_add_to_nvlist(nvlist_t
*who_nvp
, char *name
, nvlist_t
*perms_nvp
)
1011 if (perms_nvp
!= NULL
) {
1012 verify(nvlist_add_nvlist(who_nvp
,
1013 name
, perms_nvp
) == 0);
1015 verify(nvlist_add_boolean(who_nvp
, name
) == 0);
1020 helper(zfs_deleg_who_type_t who_type
, uint64_t whoid
, char *whostr
,
1021 zfs_deleg_inherit_t inherit
, nvlist_t
*who_nvp
, nvlist_t
*perms_nvp
,
1024 boolean_t do_perms
, do_sets
;
1025 char name
[ZFS_MAX_DELEG_NAME
];
1027 do_perms
= (nvlist_next_nvpair(perms_nvp
, NULL
) != NULL
);
1028 do_sets
= (nvlist_next_nvpair(sets_nvp
, NULL
) != NULL
);
1030 if (!do_perms
&& !do_sets
)
1031 do_perms
= do_sets
= B_TRUE
;
1034 zfs_deleg_whokey(name
, who_type
, inherit
,
1035 (who_type
== ZFS_DELEG_NAMED_SET
) ?
1036 whostr
: (void *)&whoid
);
1037 zfs_perms_add_to_nvlist(who_nvp
, name
, perms_nvp
);
1040 zfs_deleg_whokey(name
, toupper(who_type
), inherit
,
1041 (who_type
== ZFS_DELEG_NAMED_SET
) ?
1042 whostr
: (void *)&whoid
);
1043 zfs_perms_add_to_nvlist(who_nvp
, name
, sets_nvp
);
1048 zfs_perms_add_who_nvlist(nvlist_t
*who_nvp
, uint64_t whoid
, void *whostr
,
1049 nvlist_t
*perms_nvp
, nvlist_t
*sets_nvp
,
1050 zfs_deleg_who_type_t who_type
, zfs_deleg_inherit_t inherit
)
1052 if (who_type
== ZFS_DELEG_NAMED_SET
|| who_type
== ZFS_DELEG_CREATE
) {
1053 helper(who_type
, whoid
, whostr
, 0,
1054 who_nvp
, perms_nvp
, sets_nvp
);
1056 if (inherit
& ZFS_DELEG_PERM_LOCAL
) {
1057 helper(who_type
, whoid
, whostr
, ZFS_DELEG_LOCAL
,
1058 who_nvp
, perms_nvp
, sets_nvp
);
1060 if (inherit
& ZFS_DELEG_PERM_DESCENDENT
) {
1061 helper(who_type
, whoid
, whostr
, ZFS_DELEG_DESCENDENT
,
1062 who_nvp
, perms_nvp
, sets_nvp
);
1068 * Construct nvlist to pass down to kernel for setting/removing permissions.
1070 * The nvlist is constructed as a series of nvpairs with an optional embedded
1071 * nvlist of permissions to remove or set. The topmost nvpairs are the actual
1072 * base attribute named stored in the dsl.
1075 * whostr: is a comma separated list of users, groups, or a single set name.
1076 * whostr may be null for everyone or create perms.
1077 * who_type: is the type of entry in whostr. Typically this will be
1078 * ZFS_DELEG_WHO_UNKNOWN.
1079 * perms: common separated list of permissions. May be null if user
1080 * is requested to remove permissions by who.
1081 * inherit: Specifies the inheritance of the permissions. Will be either
1082 * ZFS_DELEG_PERM_LOCAL and/or ZFS_DELEG_PERM_DESCENDENT.
1083 * nvp The constructed nvlist to pass to zfs_perm_set().
1084 * The output nvp will look something like this.
1085 * ul$1234 -> {create ; destroy }
1086 * Ul$1234 -> { @myset }
1087 * s-$@myset - { snapshot; checksum; compression }
1090 zfs_build_perms(zfs_handle_t
*zhp
, char *whostr
, char *perms
,
1091 zfs_deleg_who_type_t who_type
, zfs_deleg_inherit_t inherit
, nvlist_t
**nvp
)
1094 nvlist_t
*perms_nvp
= NULL
;
1095 nvlist_t
*sets_nvp
= NULL
;
1097 char *who_tok
, *perm
;
1103 if ((error
= nvlist_alloc(&perms_nvp
,
1104 NV_UNIQUE_NAME
, 0)) != 0) {
1107 if ((error
= nvlist_alloc(&sets_nvp
,
1108 NV_UNIQUE_NAME
, 0)) != 0) {
1109 nvlist_free(perms_nvp
);
1114 if ((error
= nvlist_alloc(&who_nvp
, NV_UNIQUE_NAME
, 0)) != 0) {
1116 nvlist_free(perms_nvp
);
1118 nvlist_free(sets_nvp
);
1122 if (who_type
== ZFS_DELEG_NAMED_SET
) {
1123 namecheck_err_t why
;
1126 if ((error
= permset_namecheck(whostr
, &why
, &what
)) != 0) {
1127 nvlist_free(who_nvp
);
1129 nvlist_free(perms_nvp
);
1131 nvlist_free(sets_nvp
);
1134 case NAME_ERR_NO_AT
:
1135 zfs_error_aux(zhp
->zfs_hdl
,
1136 dgettext(TEXT_DOMAIN
,
1137 "set definition must begin with an '@' "
1140 return (zfs_error(zhp
->zfs_hdl
,
1141 EZFS_BADPERMSET
, whostr
));
1146 * Build up nvlist(s) of permissions. Two nvlists are maintained.
1147 * The first nvlist perms_nvp will have normal permissions and the
1148 * other sets_nvp will have only permssion set names in it.
1150 for (perm
= strtok(perms
, ","); perm
; perm
= strtok(NULL
, ",")) {
1151 const char *perm_canonical
= zfs_deleg_canonicalize_perm(perm
);
1153 if (perm_canonical
) {
1154 verify(nvlist_add_boolean(perms_nvp
,
1155 perm_canonical
) == 0);
1156 } else if (perm
[0] == '@') {
1157 verify(nvlist_add_boolean(sets_nvp
, perm
) == 0);
1159 nvlist_free(who_nvp
);
1160 nvlist_free(perms_nvp
);
1161 nvlist_free(sets_nvp
);
1162 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPERM
, perm
));
1166 if (whostr
&& who_type
!= ZFS_DELEG_CREATE
) {
1167 who_tok
= strtok(whostr
, ",");
1168 if (who_tok
== NULL
) {
1169 nvlist_free(who_nvp
);
1171 nvlist_free(perms_nvp
);
1173 nvlist_free(sets_nvp
);
1174 (void) snprintf(errbuf
, sizeof (errbuf
),
1175 dgettext(TEXT_DOMAIN
, "Who string is NULL"),
1177 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADWHO
, errbuf
));
1182 * Now create the nvlist(s)
1187 error
= zfs_get_perm_who(who_tok
, &who_type
,
1190 nvlist_free(who_nvp
);
1192 nvlist_free(perms_nvp
);
1194 nvlist_free(sets_nvp
);
1195 (void) snprintf(errbuf
, sizeof (errbuf
),
1196 dgettext(TEXT_DOMAIN
,
1197 "Unable to determine uid/gid for "
1199 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADWHO
, errbuf
));
1203 * add entries for both local and descendent when required
1205 zfs_perms_add_who_nvlist(who_nvp
, who_id
, who_tok
,
1206 perms_nvp
, sets_nvp
, who_type
, inherit
);
1208 } while (who_tok
= strtok(NULL
, ","));
1214 zfs_perm_set_common(zfs_handle_t
*zhp
, nvlist_t
*nvp
, boolean_t unset
)
1216 zfs_cmd_t zc
= { 0 };
1220 (void) snprintf(errbuf
, sizeof (errbuf
),
1221 dgettext(TEXT_DOMAIN
, "Cannot update 'allows' for '%s'"),
1224 if (zcmd_write_src_nvlist(zhp
->zfs_hdl
, &zc
, nvp
))
1227 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1228 zc
.zc_perm_action
= unset
;
1230 error
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SET_FSACL
, &zc
);
1231 if (error
&& errno
== ENOTSUP
) {
1232 (void) snprintf(errbuf
, sizeof (errbuf
),
1233 gettext("Pool must be upgraded to use 'allow/unallow'"));
1234 zcmd_free_nvlists(&zc
);
1235 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADVERSION
, errbuf
));
1237 return (zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
));
1239 zcmd_free_nvlists(&zc
);
1245 zfs_perm_set(zfs_handle_t
*zhp
, nvlist_t
*nvp
)
1247 return (zfs_perm_set_common(zhp
, nvp
, B_FALSE
));
1251 zfs_perm_remove(zfs_handle_t
*zhp
, nvlist_t
*perms
)
1253 return (zfs_perm_set_common(zhp
, perms
, B_TRUE
));
1257 perm_compare(const void *arg1
, const void *arg2
)
1259 const zfs_perm_node_t
*node1
= arg1
;
1260 const zfs_perm_node_t
*node2
= arg2
;
1263 ret
= strcmp(node1
->z_pname
, node2
->z_pname
);
1274 zfs_destroy_perm_tree(avl_tree_t
*tree
)
1276 zfs_perm_node_t
*permnode
;
1277 void *cookie
= NULL
;
1279 while ((permnode
= avl_destroy_nodes(tree
, &cookie
)) != NULL
)
1285 zfs_destroy_tree(avl_tree_t
*tree
)
1287 zfs_allow_node_t
*allownode
;
1288 void *cookie
= NULL
;
1290 while ((allownode
= avl_destroy_nodes(tree
, &cookie
)) != NULL
) {
1291 zfs_destroy_perm_tree(&allownode
->z_localdescend
);
1292 zfs_destroy_perm_tree(&allownode
->z_local
);
1293 zfs_destroy_perm_tree(&allownode
->z_descend
);
1300 zfs_free_allows(zfs_allow_t
*allow
)
1302 zfs_allow_t
*allownext
;
1303 zfs_allow_t
*freeallow
;
1307 zfs_destroy_tree(&allownext
->z_sets
);
1308 zfs_destroy_tree(&allownext
->z_crperms
);
1309 zfs_destroy_tree(&allownext
->z_user
);
1310 zfs_destroy_tree(&allownext
->z_group
);
1311 zfs_destroy_tree(&allownext
->z_everyone
);
1312 freeallow
= allownext
;
1313 allownext
= allownext
->z_next
;
1318 static zfs_allow_t
*
1319 zfs_alloc_perm_tree(zfs_handle_t
*zhp
, zfs_allow_t
*prev
, char *setpoint
)
1323 if ((ptree
= zfs_alloc(zhp
->zfs_hdl
,
1324 sizeof (zfs_allow_t
))) == NULL
) {
1328 (void) strlcpy(ptree
->z_setpoint
, setpoint
, sizeof (ptree
->z_setpoint
));
1329 avl_create(&ptree
->z_sets
,
1330 perm_compare
, sizeof (zfs_allow_node_t
),
1331 offsetof(zfs_allow_node_t
, z_node
));
1332 avl_create(&ptree
->z_crperms
,
1333 perm_compare
, sizeof (zfs_allow_node_t
),
1334 offsetof(zfs_allow_node_t
, z_node
));
1335 avl_create(&ptree
->z_user
,
1336 perm_compare
, sizeof (zfs_allow_node_t
),
1337 offsetof(zfs_allow_node_t
, z_node
));
1338 avl_create(&ptree
->z_group
,
1339 perm_compare
, sizeof (zfs_allow_node_t
),
1340 offsetof(zfs_allow_node_t
, z_node
));
1341 avl_create(&ptree
->z_everyone
,
1342 perm_compare
, sizeof (zfs_allow_node_t
),
1343 offsetof(zfs_allow_node_t
, z_node
));
1346 prev
->z_next
= ptree
;
1347 ptree
->z_next
= NULL
;
1352 * Add permissions to the appropriate AVL permission tree.
1353 * The appropriate tree may not be the requested tree.
1354 * For example if ld indicates a local permission, but
1355 * same permission also exists as a descendent permission
1356 * then the permission will be removed from the descendent
1357 * tree and add the the local+descendent tree.
1360 zfs_coalesce_perm(zfs_handle_t
*zhp
, zfs_allow_node_t
*allownode
,
1361 char *perm
, char ld
)
1363 zfs_perm_node_t pnode
, *permnode
, *permnode2
;
1364 zfs_perm_node_t
*newnode
;
1365 avl_index_t where
, where2
;
1366 avl_tree_t
*tree
, *altree
;
1368 (void) strlcpy(pnode
.z_pname
, perm
, sizeof (pnode
.z_pname
));
1370 if (ld
== ZFS_DELEG_NA
) {
1371 tree
= &allownode
->z_localdescend
;
1372 altree
= &allownode
->z_descend
;
1373 } else if (ld
== ZFS_DELEG_LOCAL
) {
1374 tree
= &allownode
->z_local
;
1375 altree
= &allownode
->z_descend
;
1377 tree
= &allownode
->z_descend
;
1378 altree
= &allownode
->z_local
;
1380 permnode
= avl_find(tree
, &pnode
, &where
);
1381 permnode2
= avl_find(altree
, &pnode
, &where2
);
1384 avl_remove(altree
, permnode2
);
1386 if (permnode
== NULL
) {
1387 tree
= &allownode
->z_localdescend
;
1392 * Now insert new permission in either requested location
1393 * local/descendent or into ld when perm will exist in both.
1395 if (permnode
== NULL
) {
1396 if ((newnode
= zfs_alloc(zhp
->zfs_hdl
,
1397 sizeof (zfs_perm_node_t
))) == NULL
) {
1401 avl_add(tree
, newnode
);
1407 * Uggh, this is going to be a bit complicated.
1408 * we have an nvlist coming out of the kernel that
1409 * will indicate where the permission is set and then
1410 * it will contain allow of the various "who's", and what
1411 * their permissions are. To further complicate this
1412 * we will then have to coalesce the local,descendent
1413 * and local+descendent permissions where appropriate.
1414 * The kernel only knows about a permission as being local
1415 * or descendent, but not both.
1417 * In order to make this easier for zfs_main to deal with
1418 * a series of AVL trees will be used to maintain
1419 * all of this, primarily for sorting purposes as well
1420 * as the ability to quickly locate a specific entry.
1422 * What we end up with are tree's for sets, create perms,
1423 * user, groups and everyone. With each of those trees
1424 * we have subtrees for local, descendent and local+descendent
1428 zfs_perm_get(zfs_handle_t
*zhp
, zfs_allow_t
**zfs_perms
)
1430 zfs_cmd_t zc
= { 0 };
1433 nvlist_t
*permnv
, *sourcenv
;
1434 nvpair_t
*who_pair
, *source_pair
;
1435 nvpair_t
*perm_pair
;
1437 zfs_allow_t
*zallowp
, *newallowp
;
1445 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1447 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
1450 while (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
1451 if (errno
== ENOMEM
) {
1452 if (zcmd_expand_dst_nvlist(zhp
->zfs_hdl
, &zc
) != 0) {
1453 zcmd_free_nvlists(&zc
);
1456 } else if (errno
== ENOTSUP
) {
1457 zcmd_free_nvlists(&zc
);
1458 (void) snprintf(errbuf
, sizeof (errbuf
),
1459 gettext("Pool must be upgraded to use 'allow'"));
1460 return (zfs_error(zhp
->zfs_hdl
,
1461 EZFS_BADVERSION
, errbuf
));
1463 zcmd_free_nvlists(&zc
);
1468 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &nvlist
) != 0) {
1469 zcmd_free_nvlists(&zc
);
1473 zcmd_free_nvlists(&zc
);
1475 source_pair
= nvlist_next_nvpair(nvlist
, NULL
);
1477 if (source_pair
== NULL
) {
1482 *zfs_perms
= zfs_alloc_perm_tree(zhp
, NULL
, nvpair_name(source_pair
));
1483 if (*zfs_perms
== NULL
) {
1487 zallowp
= *zfs_perms
;
1492 zfs_allow_node_t
*allownode
;
1493 zfs_allow_node_t findallownode
;
1494 zfs_allow_node_t
*newallownode
;
1496 (void) strlcpy(zallowp
->z_setpoint
,
1497 nvpair_name(source_pair
),
1498 sizeof (zallowp
->z_setpoint
));
1500 if ((error
= nvpair_value_nvlist(source_pair
, &sourcenv
)) != 0)
1504 * Make sure nvlist is composed correctly
1506 if (zfs_deleg_verify_nvlist(sourcenv
)) {
1510 who_pair
= nvlist_next_nvpair(sourcenv
, NULL
);
1511 if (who_pair
== NULL
) {
1516 error
= nvpair_value_nvlist(who_pair
, &permnv
);
1522 * First build up the key to use
1523 * for looking up in the various
1526 ld
= nvpair_name(who_pair
)[1];
1527 nvpname
= nvpair_name(who_pair
);
1528 switch (nvpair_name(who_pair
)[0]) {
1529 case ZFS_DELEG_USER
:
1530 case ZFS_DELEG_USER_SETS
:
1531 tree
= &zallowp
->z_user
;
1532 uid
= atol(&nvpname
[3]);
1533 pwd
= getpwuid(uid
);
1534 (void) snprintf(findallownode
.z_key
,
1535 sizeof (findallownode
.z_key
), "user %s",
1536 (pwd
) ? pwd
->pw_name
:
1537 &nvpair_name(who_pair
)[3]);
1539 case ZFS_DELEG_GROUP
:
1540 case ZFS_DELEG_GROUP_SETS
:
1541 tree
= &zallowp
->z_group
;
1542 gid
= atol(&nvpname
[3]);
1543 grp
= getgrgid(gid
);
1544 (void) snprintf(findallownode
.z_key
,
1545 sizeof (findallownode
.z_key
), "group %s",
1546 (grp
) ? grp
->gr_name
:
1547 &nvpair_name(who_pair
)[3]);
1549 case ZFS_DELEG_CREATE
:
1550 case ZFS_DELEG_CREATE_SETS
:
1551 tree
= &zallowp
->z_crperms
;
1552 (void) strlcpy(findallownode
.z_key
, "",
1553 sizeof (findallownode
.z_key
));
1555 case ZFS_DELEG_EVERYONE
:
1556 case ZFS_DELEG_EVERYONE_SETS
:
1557 (void) snprintf(findallownode
.z_key
,
1558 sizeof (findallownode
.z_key
), "everyone");
1559 tree
= &zallowp
->z_everyone
;
1561 case ZFS_DELEG_NAMED_SET
:
1562 case ZFS_DELEG_NAMED_SET_SETS
:
1563 (void) snprintf(findallownode
.z_key
,
1564 sizeof (findallownode
.z_key
), "%s",
1565 &nvpair_name(who_pair
)[3]);
1566 tree
= &zallowp
->z_sets
;
1573 allownode
= avl_find(tree
, &findallownode
, &where
);
1574 if (allownode
== NULL
) {
1575 if ((newallownode
= zfs_alloc(zhp
->zfs_hdl
,
1576 sizeof (zfs_allow_node_t
))) == NULL
) {
1579 avl_create(&newallownode
->z_localdescend
,
1581 sizeof (zfs_perm_node_t
),
1582 offsetof(zfs_perm_node_t
, z_node
));
1583 avl_create(&newallownode
->z_local
,
1585 sizeof (zfs_perm_node_t
),
1586 offsetof(zfs_perm_node_t
, z_node
));
1587 avl_create(&newallownode
->z_descend
,
1589 sizeof (zfs_perm_node_t
),
1590 offsetof(zfs_perm_node_t
, z_node
));
1591 (void) strlcpy(newallownode
->z_key
,
1592 findallownode
.z_key
,
1593 sizeof (findallownode
.z_key
));
1594 avl_insert(tree
, newallownode
, where
);
1595 allownode
= newallownode
;
1599 * Now iterate over the permissions and
1600 * place them in the appropriate local,
1601 * descendent or local+descendent tree.
1603 * The permissions are added to the tree
1604 * via zfs_coalesce_perm().
1606 perm_pair
= nvlist_next_nvpair(permnv
, NULL
);
1607 if (perm_pair
== NULL
)
1610 if (zfs_coalesce_perm(zhp
, allownode
,
1611 nvpair_name(perm_pair
), ld
) != 0)
1613 } while (perm_pair
= nvlist_next_nvpair(permnv
,
1615 } while (who_pair
= nvlist_next_nvpair(sourcenv
, who_pair
));
1617 source_pair
= nvlist_next_nvpair(nvlist
, source_pair
);
1618 if (source_pair
== NULL
)
1622 * allocate another node from the link list of
1623 * zfs_allow_t structures
1625 newallowp
= zfs_alloc_perm_tree(zhp
, zallowp
,
1626 nvpair_name(source_pair
));
1627 if (newallowp
== NULL
) {
1630 zallowp
= newallowp
;
1632 nvlist_free(nvlist
);
1635 zfs_free_allows(*zfs_perms
);
1636 nvlist_free(nvlist
);
1641 zfs_deleg_perm_note(zfs_deleg_note_t note
)
1644 * Don't put newlines on end of lines
1647 case ZFS_DELEG_NOTE_CREATE
:
1648 return (dgettext(TEXT_DOMAIN
,
1649 "Must also have the 'mount' ability"));
1650 case ZFS_DELEG_NOTE_DESTROY
:
1651 return (dgettext(TEXT_DOMAIN
,
1652 "Must also have the 'mount' ability"));
1653 case ZFS_DELEG_NOTE_SNAPSHOT
:
1654 return (dgettext(TEXT_DOMAIN
,
1655 "Must also have the 'mount' ability"));
1656 case ZFS_DELEG_NOTE_ROLLBACK
:
1657 return (dgettext(TEXT_DOMAIN
,
1658 "Must also have the 'mount' ability"));
1659 case ZFS_DELEG_NOTE_CLONE
:
1660 return (dgettext(TEXT_DOMAIN
, "Must also have the 'create' "
1661 "ability and 'mount'\n"
1662 "\t\t\t\tability in the origin file system"));
1663 case ZFS_DELEG_NOTE_PROMOTE
:
1664 return (dgettext(TEXT_DOMAIN
, "Must also have the 'mount'\n"
1665 "\t\t\t\tand 'promote' ability in the origin file system"));
1666 case ZFS_DELEG_NOTE_RENAME
:
1667 return (dgettext(TEXT_DOMAIN
, "Must also have the 'mount' "
1668 "and 'create' \n\t\t\t\tability in the new parent"));
1669 case ZFS_DELEG_NOTE_RECEIVE
:
1670 return (dgettext(TEXT_DOMAIN
, "Must also have the 'mount'"
1671 " and 'create' ability"));
1672 case ZFS_DELEG_NOTE_USERPROP
:
1673 return (dgettext(TEXT_DOMAIN
,
1674 "Allows changing any user property"));
1675 case ZFS_DELEG_NOTE_ALLOW
:
1676 return (dgettext(TEXT_DOMAIN
,
1677 "Must also have the permission that is being\n"
1678 "\t\t\t\tallowed"));
1679 case ZFS_DELEG_NOTE_MOUNT
:
1680 return (dgettext(TEXT_DOMAIN
,
1681 "Allows mount/umount of ZFS datasets"));
1682 case ZFS_DELEG_NOTE_SHARE
:
1683 return (dgettext(TEXT_DOMAIN
,
1684 "Allows sharing file systems over NFS or SMB\n"
1685 "\t\t\t\tprotocols"));
1686 case ZFS_DELEG_NOTE_NONE
:
1688 return (dgettext(TEXT_DOMAIN
, ""));
1693 ZFS_DELEG_SUBCOMMAND
,
1696 } zfs_deleg_perm_type_t
;
1699 * is the permission a subcommand or other?
1701 zfs_deleg_perm_type_t
1702 zfs_deleg_perm_type(const char *perm
)
1704 if (strcmp(perm
, "userprop") == 0)
1705 return (ZFS_DELEG_OTHER
);
1707 return (ZFS_DELEG_SUBCOMMAND
);
1711 zfs_deleg_perm_type_str(zfs_deleg_perm_type_t type
)
1714 case ZFS_DELEG_SUBCOMMAND
:
1715 return (dgettext(TEXT_DOMAIN
, "subcommand"));
1716 case ZFS_DELEG_PROP
:
1717 return (dgettext(TEXT_DOMAIN
, "property"));
1718 case ZFS_DELEG_OTHER
:
1719 return (dgettext(TEXT_DOMAIN
, "other"));
1726 zfs_deleg_prop_cb(int prop
, void *cb
)
1728 if (zfs_prop_delegatable(prop
))
1729 (void) fprintf(stderr
, "%-15s %-15s\n", zfs_prop_to_name(prop
),
1730 zfs_deleg_perm_type_str(ZFS_DELEG_PROP
));
1732 return (ZPROP_CONT
);
1736 zfs_deleg_permissions(void)
1740 (void) fprintf(stderr
, "\n%-15s %-15s\t%s\n\n", "NAME",
1744 * First print out the subcommands
1746 for (i
= 0; zfs_deleg_perm_tab
[i
].z_perm
!= NULL
; i
++) {
1747 (void) fprintf(stderr
, "%-15s %-15s\t%s\n",
1748 zfs_deleg_perm_tab
[i
].z_perm
,
1749 zfs_deleg_perm_type_str(
1750 zfs_deleg_perm_type(zfs_deleg_perm_tab
[i
].z_perm
)),
1751 zfs_deleg_perm_note(zfs_deleg_perm_tab
[i
].z_note
));
1754 (void) zprop_iter(zfs_deleg_prop_cb
, NULL
, B_FALSE
, B_TRUE
,
1755 ZFS_TYPE_DATASET
|ZFS_TYPE_VOLUME
);
1759 * Given a property name and value, set the property for the given dataset.
1762 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1764 zfs_cmd_t zc
= { 0 };
1766 prop_changelist_t
*cl
= NULL
;
1768 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1769 nvlist_t
*nvl
= NULL
, *realprops
;
1771 boolean_t do_prefix
;
1774 (void) snprintf(errbuf
, sizeof (errbuf
),
1775 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1778 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1779 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1780 (void) no_memory(hdl
);
1784 if ((realprops
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, nvl
,
1785 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, errbuf
)) == NULL
)
1791 prop
= zfs_name_to_prop(propname
);
1793 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1796 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1797 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1798 "child dataset with inherited mountpoint is used "
1799 "in a non-global zone"));
1800 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1805 * If the dataset's canmount property is being set to noauto,
1806 * then we want to prevent unmounting & remounting it.
1808 do_prefix
= !((prop
== ZFS_PROP_CANMOUNT
) &&
1809 (zprop_string_to_index(prop
, propval
, &idx
,
1810 ZFS_TYPE_DATASET
) == 0) && (idx
== ZFS_CANMOUNT_NOAUTO
));
1812 if (do_prefix
&& (ret
= changelist_prefix(cl
)) != 0)
1816 * Execute the corresponding ioctl() to set this property.
1818 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1820 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1823 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1829 * For quotas and reservations, ENOSPC indicates
1830 * something different; setting a quota or reservation
1831 * doesn't use any disk space.
1834 case ZFS_PROP_QUOTA
:
1835 case ZFS_PROP_REFQUOTA
:
1836 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1837 "size is less than current used or "
1839 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1842 case ZFS_PROP_RESERVATION
:
1843 case ZFS_PROP_REFRESERVATION
:
1844 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1845 "size is greater than available space"));
1846 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1850 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1856 if (prop
== ZFS_PROP_VOLBLOCKSIZE
)
1857 (void) zfs_error(hdl
, EZFS_VOLHASDATA
, errbuf
);
1859 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1863 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1867 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1868 "pool and or dataset must be upgraded to set this "
1869 "property or value"));
1870 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1874 if (prop
== ZFS_PROP_COMPRESSION
) {
1875 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1876 "property setting is not allowed on "
1877 "bootable datasets"));
1878 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1880 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1886 * This platform can't address a volume this big.
1889 if (prop
== ZFS_PROP_VOLSIZE
) {
1890 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1896 (void) zfs_standard_error(hdl
, errno
, errbuf
);
1900 ret
= changelist_postfix(cl
);
1903 * Refresh the statistics so the new property value
1907 (void) get_stats(zhp
);
1912 zcmd_free_nvlists(&zc
);
1914 changelist_free(cl
);
1919 * Given a property, inherit the value from the parent dataset.
1922 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
)
1924 zfs_cmd_t zc
= { 0 };
1926 prop_changelist_t
*cl
;
1927 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1931 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1932 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1934 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1936 * For user properties, the amount of work we have to do is very
1937 * small, so just do it here.
1939 if (!zfs_prop_user(propname
)) {
1940 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1941 "invalid property"));
1942 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1945 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1946 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1948 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1949 return (zfs_standard_error(hdl
, errno
, errbuf
));
1955 * Verify that this property is inheritable.
1957 if (zfs_prop_readonly(prop
))
1958 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1960 if (!zfs_prop_inheritable(prop
))
1961 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1964 * Check to see if the value applies to this type
1966 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1967 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1970 * Normalize the name, to get rid of shorthand abbrevations.
1972 propname
= zfs_prop_to_name(prop
);
1973 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1974 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1976 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1977 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1978 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1979 "dataset is used in a non-global zone"));
1980 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1984 * Determine datasets which will be affected by this change, if any.
1986 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1989 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1990 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1991 "child dataset with inherited mountpoint is used "
1992 "in a non-global zone"));
1993 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1997 if ((ret
= changelist_prefix(cl
)) != 0)
2000 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
2001 return (zfs_standard_error(hdl
, errno
, errbuf
));
2004 if ((ret
= changelist_postfix(cl
)) != 0)
2008 * Refresh the statistics so the new property is reflected.
2010 (void) get_stats(zhp
);
2014 changelist_free(cl
);
2019 * True DSL properties are stored in an nvlist. The following two functions
2020 * extract them appropriately.
2023 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2029 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2030 zfs_prop_to_name(prop
), &nv
) == 0) {
2031 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
2032 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2034 value
= zfs_prop_default_numeric(prop
);
2042 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
2048 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2049 zfs_prop_to_name(prop
), &nv
) == 0) {
2050 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
2051 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
2053 if ((value
= (char *)zfs_prop_default_string(prop
)) == NULL
)
2062 * Internal function for getting a numeric property. Both zfs_prop_get() and
2063 * zfs_prop_get_int() are built using this interface.
2065 * Certain properties can be overridden using 'mount -o'. In this case, scan
2066 * the contents of the /etc/mnttab entry, searching for the appropriate options.
2067 * If they differ from the on-disk values, report the current values and mark
2068 * the source "temporary".
2071 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2072 char **source
, uint64_t *val
)
2074 zfs_cmd_t zc
= { 0 };
2075 nvlist_t
*zplprops
= NULL
;
2077 char *mntopt_on
= NULL
;
2078 char *mntopt_off
= NULL
;
2083 case ZFS_PROP_ATIME
:
2084 mntopt_on
= MNTOPT_ATIME
;
2085 mntopt_off
= MNTOPT_NOATIME
;
2088 case ZFS_PROP_DEVICES
:
2089 mntopt_on
= MNTOPT_DEVICES
;
2090 mntopt_off
= MNTOPT_NODEVICES
;
2094 mntopt_on
= MNTOPT_EXEC
;
2095 mntopt_off
= MNTOPT_NOEXEC
;
2098 case ZFS_PROP_READONLY
:
2099 mntopt_on
= MNTOPT_RO
;
2100 mntopt_off
= MNTOPT_RW
;
2103 case ZFS_PROP_SETUID
:
2104 mntopt_on
= MNTOPT_SETUID
;
2105 mntopt_off
= MNTOPT_NOSETUID
;
2108 case ZFS_PROP_XATTR
:
2109 mntopt_on
= MNTOPT_XATTR
;
2110 mntopt_off
= MNTOPT_NOXATTR
;
2113 case ZFS_PROP_NBMAND
:
2114 mntopt_on
= MNTOPT_NBMAND
;
2115 mntopt_off
= MNTOPT_NONBMAND
;
2120 * Because looking up the mount options is potentially expensive
2121 * (iterating over all of /etc/mnttab), we defer its calculation until
2122 * we're looking up a property which requires its presence.
2124 if (!zhp
->zfs_mntcheck
&&
2125 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2126 struct mnttab entry
, search
= { 0 };
2127 FILE *mnttab
= zhp
->zfs_hdl
->libzfs_mnttab
;
2129 search
.mnt_special
= (char *)zhp
->zfs_name
;
2130 search
.mnt_fstype
= MNTTYPE_ZFS
;
2133 if (getmntany(mnttab
, &entry
, &search
) == 0) {
2134 zhp
->zfs_mntopts
= zfs_strdup(zhp
->zfs_hdl
,
2136 if (zhp
->zfs_mntopts
== NULL
)
2140 zhp
->zfs_mntcheck
= B_TRUE
;
2143 if (zhp
->zfs_mntopts
== NULL
)
2144 mnt
.mnt_mntopts
= "";
2146 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2149 case ZFS_PROP_ATIME
:
2150 case ZFS_PROP_DEVICES
:
2152 case ZFS_PROP_READONLY
:
2153 case ZFS_PROP_SETUID
:
2154 case ZFS_PROP_XATTR
:
2155 case ZFS_PROP_NBMAND
:
2156 *val
= getprop_uint64(zhp
, prop
, source
);
2158 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2161 *src
= ZPROP_SRC_TEMPORARY
;
2162 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2165 *src
= ZPROP_SRC_TEMPORARY
;
2169 case ZFS_PROP_CANMOUNT
:
2170 *val
= getprop_uint64(zhp
, prop
, source
);
2171 if (*val
!= ZFS_CANMOUNT_ON
)
2172 *source
= zhp
->zfs_name
;
2174 *source
= ""; /* default */
2177 case ZFS_PROP_QUOTA
:
2178 case ZFS_PROP_REFQUOTA
:
2179 case ZFS_PROP_RESERVATION
:
2180 case ZFS_PROP_REFRESERVATION
:
2181 *val
= getprop_uint64(zhp
, prop
, source
);
2183 *source
= ""; /* default */
2185 *source
= zhp
->zfs_name
;
2188 case ZFS_PROP_MOUNTED
:
2189 *val
= (zhp
->zfs_mntopts
!= NULL
);
2192 case ZFS_PROP_NUMCLONES
:
2193 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2196 case ZFS_PROP_VERSION
:
2197 case ZFS_PROP_NORMALIZE
:
2198 case ZFS_PROP_UTF8ONLY
:
2200 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
2201 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2203 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2204 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2205 zcmd_free_nvlists(&zc
);
2206 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2207 "unable to get %s property"),
2208 zfs_prop_to_name(prop
));
2209 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADVERSION
,
2210 dgettext(TEXT_DOMAIN
, "internal error")));
2212 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2213 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2215 zcmd_free_nvlists(&zc
);
2216 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2217 "unable to get %s property"),
2218 zfs_prop_to_name(prop
));
2219 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOMEM
,
2220 dgettext(TEXT_DOMAIN
, "internal error")));
2223 nvlist_free(zplprops
);
2224 zcmd_free_nvlists(&zc
);
2228 switch (zfs_prop_get_type(prop
)) {
2229 case PROP_TYPE_NUMBER
:
2230 case PROP_TYPE_INDEX
:
2231 *val
= getprop_uint64(zhp
, prop
, source
);
2233 * If we tried to use a defalut value for a
2234 * readonly property, it means that it was not
2235 * present; return an error.
2237 if (zfs_prop_readonly(prop
) &&
2238 *source
&& (*source
)[0] == '\0') {
2243 case PROP_TYPE_STRING
:
2245 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2246 "cannot get non-numeric property"));
2247 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2248 dgettext(TEXT_DOMAIN
, "internal error")));
2256 * Calculate the source type, given the raw source string.
2259 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2260 char *statbuf
, size_t statlen
)
2262 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2265 if (source
== NULL
) {
2266 *srctype
= ZPROP_SRC_NONE
;
2267 } else if (source
[0] == '\0') {
2268 *srctype
= ZPROP_SRC_DEFAULT
;
2270 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2271 *srctype
= ZPROP_SRC_LOCAL
;
2273 (void) strlcpy(statbuf
, source
, statlen
);
2274 *srctype
= ZPROP_SRC_INHERITED
;
2281 * Retrieve a property from the given object. If 'literal' is specified, then
2282 * numbers are left as exact values. Otherwise, numbers are converted to a
2283 * human-readable form.
2285 * Returns 0 on success, or -1 on error.
2288 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2289 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2291 char *source
= NULL
;
2297 * Check to see if this property applies to our object
2299 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
2303 *src
= ZPROP_SRC_NONE
;
2306 case ZFS_PROP_CREATION
:
2308 * 'creation' is a time_t stored in the statistics. We convert
2309 * this into a string unless 'literal' is specified.
2312 val
= getprop_uint64(zhp
, prop
, &source
);
2313 time_t time
= (time_t)val
;
2317 localtime_r(&time
, &t
) == NULL
||
2318 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2320 (void) snprintf(propbuf
, proplen
, "%llu", val
);
2324 case ZFS_PROP_MOUNTPOINT
:
2326 * Getting the precise mountpoint can be tricky.
2328 * - for 'none' or 'legacy', return those values.
2329 * - for inherited mountpoints, we want to take everything
2330 * after our ancestor and append it to the inherited value.
2332 * If the pool has an alternate root, we want to prepend that
2333 * root to any values we return.
2336 str
= getprop_string(zhp
, prop
, &source
);
2338 if (str
[0] == '/') {
2339 char buf
[MAXPATHLEN
];
2341 const char *relpath
= zhp
->zfs_name
+ strlen(source
);
2343 if (relpath
[0] == '/')
2346 if ((zpool_get_prop(zhp
->zpool_hdl
,
2347 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
)) ||
2348 (strcmp(root
, "-") == 0))
2351 * Special case an alternate root of '/'. This will
2352 * avoid having multiple leading slashes in the
2355 if (strcmp(root
, "/") == 0)
2359 * If the mountpoint is '/' then skip over this
2360 * if we are obtaining either an alternate root or
2361 * an inherited mountpoint.
2363 if (str
[1] == '\0' && (root
[0] != '\0' ||
2364 relpath
[0] != '\0'))
2367 if (relpath
[0] == '\0')
2368 (void) snprintf(propbuf
, proplen
, "%s%s",
2371 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2372 root
, str
, relpath
[0] == '@' ? "" : "/",
2375 /* 'legacy' or 'none' */
2376 (void) strlcpy(propbuf
, str
, proplen
);
2381 case ZFS_PROP_ORIGIN
:
2382 (void) strlcpy(propbuf
, getprop_string(zhp
, prop
, &source
),
2385 * If there is no parent at all, return failure to indicate that
2386 * it doesn't apply to this dataset.
2388 if (propbuf
[0] == '\0')
2392 case ZFS_PROP_QUOTA
:
2393 case ZFS_PROP_REFQUOTA
:
2394 case ZFS_PROP_RESERVATION
:
2395 case ZFS_PROP_REFRESERVATION
:
2397 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2401 * If quota or reservation is 0, we translate this into 'none'
2402 * (unless literal is set), and indicate that it's the default
2403 * value. Otherwise, we print the number nicely and indicate
2404 * that its set locally.
2408 (void) strlcpy(propbuf
, "0", proplen
);
2410 (void) strlcpy(propbuf
, "none", proplen
);
2413 (void) snprintf(propbuf
, proplen
, "%llu",
2416 zfs_nicenum(val
, propbuf
, proplen
);
2420 case ZFS_PROP_COMPRESSRATIO
:
2421 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2423 (void) snprintf(propbuf
, proplen
, "%lld.%02lldx", (longlong_t
)
2424 val
/ 100, (longlong_t
)val
% 100);
2428 switch (zhp
->zfs_type
) {
2429 case ZFS_TYPE_FILESYSTEM
:
2432 case ZFS_TYPE_VOLUME
:
2435 case ZFS_TYPE_SNAPSHOT
:
2441 (void) snprintf(propbuf
, proplen
, "%s", str
);
2444 case ZFS_PROP_MOUNTED
:
2446 * The 'mounted' property is a pseudo-property that described
2447 * whether the filesystem is currently mounted. Even though
2448 * it's a boolean value, the typical values of "on" and "off"
2449 * don't make sense, so we translate to "yes" and "no".
2451 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2452 src
, &source
, &val
) != 0)
2455 (void) strlcpy(propbuf
, "yes", proplen
);
2457 (void) strlcpy(propbuf
, "no", proplen
);
2462 * The 'name' property is a pseudo-property derived from the
2463 * dataset name. It is presented as a real property to simplify
2466 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2470 switch (zfs_prop_get_type(prop
)) {
2471 case PROP_TYPE_NUMBER
:
2472 if (get_numeric_property(zhp
, prop
, src
,
2473 &source
, &val
) != 0)
2476 (void) snprintf(propbuf
, proplen
, "%llu",
2479 zfs_nicenum(val
, propbuf
, proplen
);
2482 case PROP_TYPE_STRING
:
2483 (void) strlcpy(propbuf
,
2484 getprop_string(zhp
, prop
, &source
), proplen
);
2487 case PROP_TYPE_INDEX
:
2488 if (get_numeric_property(zhp
, prop
, src
,
2489 &source
, &val
) != 0)
2491 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2493 (void) strlcpy(propbuf
, strval
, proplen
);
2501 get_source(zhp
, src
, source
, statbuf
, statlen
);
2507 * Utility function to get the given numeric property. Does no validation that
2508 * the given property is the appropriate type; should only be used with
2509 * hard-coded property types.
2512 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2517 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2523 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2527 zfs_nicenum(val
, buf
, sizeof (buf
));
2528 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2532 * Similar to zfs_prop_get(), but returns the value as an integer.
2535 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2536 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2541 * Check to see if this property applies to our object
2543 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2544 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2545 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2546 zfs_prop_to_name(prop
)));
2550 *src
= ZPROP_SRC_NONE
;
2552 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2555 get_source(zhp
, src
, source
, statbuf
, statlen
);
2561 * Returns the name of the given zfs handle.
2564 zfs_get_name(const zfs_handle_t
*zhp
)
2566 return (zhp
->zfs_name
);
2570 * Returns the type of the given zfs handle.
2573 zfs_get_type(const zfs_handle_t
*zhp
)
2575 return (zhp
->zfs_type
);
2579 * Iterate over all child filesystems
2582 zfs_iter_filesystems(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2584 zfs_cmd_t zc
= { 0 };
2588 if (zhp
->zfs_type
!= ZFS_TYPE_FILESYSTEM
)
2591 for ((void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2592 ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_DATASET_LIST_NEXT
, &zc
) == 0;
2593 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
))) {
2595 * Ignore private dataset names.
2597 if (dataset_name_hidden(zc
.zc_name
))
2601 * Silently ignore errors, as the only plausible explanation is
2602 * that the pool has since been removed.
2604 if ((nzhp
= make_dataset_handle(zhp
->zfs_hdl
,
2605 zc
.zc_name
)) == NULL
)
2608 if ((ret
= func(nzhp
, data
)) != 0)
2613 * An errno value of ESRCH indicates normal completion. If ENOENT is
2614 * returned, then the underlying dataset has been removed since we
2615 * obtained the handle.
2617 if (errno
!= ESRCH
&& errno
!= ENOENT
)
2618 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
2619 dgettext(TEXT_DOMAIN
, "cannot iterate filesystems")));
2625 * Iterate over all snapshots
2628 zfs_iter_snapshots(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2630 zfs_cmd_t zc
= { 0 };
2634 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
2637 for ((void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2638 ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2640 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
))) {
2642 if ((nzhp
= make_dataset_handle(zhp
->zfs_hdl
,
2643 zc
.zc_name
)) == NULL
)
2646 if ((ret
= func(nzhp
, data
)) != 0)
2651 * An errno value of ESRCH indicates normal completion. If ENOENT is
2652 * returned, then the underlying dataset has been removed since we
2653 * obtained the handle. Silently ignore this case, and return success.
2655 if (errno
!= ESRCH
&& errno
!= ENOENT
)
2656 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
2657 dgettext(TEXT_DOMAIN
, "cannot iterate filesystems")));
2663 * Iterate over all children, snapshots and filesystems
2666 zfs_iter_children(zfs_handle_t
*zhp
, zfs_iter_f func
, void *data
)
2670 if ((ret
= zfs_iter_filesystems(zhp
, func
, data
)) != 0)
2673 return (zfs_iter_snapshots(zhp
, func
, data
));
2677 * Given a complete name, return just the portion that refers to the parent.
2678 * Can return NULL if this is a pool.
2681 parent_name(const char *path
, char *buf
, size_t buflen
)
2685 if ((loc
= strrchr(path
, '/')) == NULL
)
2688 (void) strncpy(buf
, path
, MIN(buflen
, loc
- path
));
2689 buf
[loc
- path
] = '\0';
2695 * If accept_ancestor is false, then check to make sure that the given path has
2696 * a parent, and that it exists. If accept_ancestor is true, then find the
2697 * closest existing ancestor for the given path. In prefixlen return the
2698 * length of already existing prefix of the given path. We also fetch the
2699 * 'zoned' property, which is used to validate property settings when creating
2703 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
2704 boolean_t accept_ancestor
, int *prefixlen
)
2706 zfs_cmd_t zc
= { 0 };
2707 char parent
[ZFS_MAXNAMELEN
];
2712 (void) snprintf(errbuf
, sizeof (errbuf
), "cannot create '%s'",
2715 /* get parent, and check to see if this is just a pool */
2716 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
2717 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2718 "missing dataset name"));
2719 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2722 /* check to see if the pool exists */
2723 if ((slash
= strchr(parent
, '/')) == NULL
)
2724 slash
= parent
+ strlen(parent
);
2725 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
2726 zc
.zc_name
[slash
- parent
] = '\0';
2727 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
2729 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2730 "no such pool '%s'"), zc
.zc_name
);
2731 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2734 /* check to see if the parent dataset exists */
2735 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
2736 if (errno
== ENOENT
&& accept_ancestor
) {
2738 * Go deeper to find an ancestor, give up on top level.
2740 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
2741 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2742 "no such pool '%s'"), zc
.zc_name
);
2743 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2745 } else if (errno
== ENOENT
) {
2746 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2747 "parent does not exist"));
2748 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2750 return (zfs_standard_error(hdl
, errno
, errbuf
));
2753 *zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
2754 /* we are in a non-global zone, but parent is in the global zone */
2755 if (getzoneid() != GLOBAL_ZONEID
&& !(*zoned
)) {
2756 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
2761 /* make sure parent is a filesystem */
2762 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
2763 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2764 "parent is not a filesystem"));
2765 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
2771 if (prefixlen
!= NULL
)
2772 *prefixlen
= strlen(parent
);
2777 * Finds whether the dataset of the given type(s) exists.
2780 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
2784 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
2788 * Try to get stats for the dataset, which will tell us if it exists.
2790 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
2791 int ds_type
= zhp
->zfs_type
;
2794 if (types
& ds_type
)
2801 * Given a path to 'target', create all the ancestors between
2802 * the prefixlen portion of the path, and the target itself.
2803 * Fail if the initial prefixlen-ancestor does not already exist.
2806 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
2812 /* make sure prefix exists */
2813 cp
= target
+ prefixlen
;
2815 assert(strchr(cp
, '/') == NULL
);
2816 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2819 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2827 * Attempt to create, mount, and share any ancestor filesystems,
2828 * up to the prefixlen-long one.
2830 for (cp
= target
+ prefixlen
+ 1;
2831 cp
= strchr(cp
, '/'); *cp
= '/', cp
++) {
2836 h
= make_dataset_handle(hdl
, target
);
2838 /* it already exists, nothing to do here */
2843 logstr
= hdl
->libzfs_log_str
;
2844 hdl
->libzfs_log_str
= NULL
;
2845 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
2847 hdl
->libzfs_log_str
= logstr
;
2848 opname
= dgettext(TEXT_DOMAIN
, "create");
2852 hdl
->libzfs_log_str
= logstr
;
2853 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
2855 opname
= dgettext(TEXT_DOMAIN
, "open");
2859 if (zfs_mount(h
, NULL
, 0) != 0) {
2860 opname
= dgettext(TEXT_DOMAIN
, "mount");
2864 if (zfs_share(h
) != 0) {
2865 opname
= dgettext(TEXT_DOMAIN
, "share");
2875 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2876 "failed to %s ancestor '%s'"), opname
, target
);
2881 * Creates non-existing ancestors of the given path.
2884 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
2891 if (check_parents(hdl
, path
, &zoned
, B_TRUE
, &prefix
) != 0)
2894 if ((path_copy
= strdup(path
)) != NULL
) {
2895 rc
= create_parents(hdl
, path_copy
, prefix
);
2898 if (path_copy
== NULL
|| rc
!= 0)
2905 * Create a new filesystem or volume.
2908 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
2911 zfs_cmd_t zc
= { 0 };
2914 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
2918 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2919 "cannot create '%s'"), path
);
2921 /* validate the path, taking care to note the extended error message */
2922 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
2923 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2925 /* validate parents exist */
2926 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
2930 * The failure modes when creating a dataset of a different type over
2931 * one that already exists is a little strange. In particular, if you
2932 * try to create a dataset on top of an existing dataset, the ioctl()
2933 * will return ENOENT, not EEXIST. To prevent this from happening, we
2934 * first try to see if the dataset exists.
2936 (void) strlcpy(zc
.zc_name
, path
, sizeof (zc
.zc_name
));
2937 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2938 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2939 "dataset already exists"));
2940 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2943 if (type
== ZFS_TYPE_VOLUME
)
2944 zc
.zc_objset_type
= DMU_OST_ZVOL
;
2946 zc
.zc_objset_type
= DMU_OST_ZFS
;
2948 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
2949 zoned
, NULL
, errbuf
)) == 0)
2952 if (type
== ZFS_TYPE_VOLUME
) {
2954 * If we are creating a volume, the size and block size must
2955 * satisfy a few restraints. First, the blocksize must be a
2956 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
2957 * volsize must be a multiple of the block size, and cannot be
2960 if (props
== NULL
|| nvlist_lookup_uint64(props
,
2961 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
2963 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2964 "missing volume size"));
2965 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2968 if ((ret
= nvlist_lookup_uint64(props
,
2969 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
2970 &blocksize
)) != 0) {
2971 if (ret
== ENOENT
) {
2972 blocksize
= zfs_prop_default_numeric(
2973 ZFS_PROP_VOLBLOCKSIZE
);
2976 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2977 "missing volume block size"));
2978 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2984 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2985 "volume size cannot be zero"));
2986 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2989 if (size
% blocksize
!= 0) {
2991 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2992 "volume size must be a multiple of volume block "
2994 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
2998 if (props
&& zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0)
3002 /* create the dataset */
3003 ret
= zfs_ioctl(hdl
, ZFS_IOC_CREATE
, &zc
);
3005 if (ret
== 0 && type
== ZFS_TYPE_VOLUME
) {
3006 ret
= zvol_create_link(hdl
, path
);
3008 (void) zfs_standard_error(hdl
, errno
,
3009 dgettext(TEXT_DOMAIN
,
3010 "Volume successfully created, but device links "
3011 "were not created"));
3012 zcmd_free_nvlists(&zc
);
3017 zcmd_free_nvlists(&zc
);
3019 /* check for failure */
3021 char parent
[ZFS_MAXNAMELEN
];
3022 (void) parent_name(path
, parent
, sizeof (parent
));
3026 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3027 "no such parent '%s'"), parent
);
3028 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3031 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3032 "parent '%s' is not a filesystem"), parent
);
3033 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3037 "volume block size must be power of 2 from "
3039 (uint_t
)SPA_MINBLOCKSIZE
,
3040 (uint_t
)SPA_MAXBLOCKSIZE
>> 10);
3042 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3045 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3046 "pool must be upgraded to set this "
3047 "property or value"));
3048 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3052 * This platform can't address a volume this big.
3054 if (type
== ZFS_TYPE_VOLUME
)
3055 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3060 return (zfs_standard_error(hdl
, errno
, errbuf
));
3068 * Destroys the given dataset. The caller must make sure that the filesystem
3069 * isn't mounted, and that there are no active dependents.
3072 zfs_destroy(zfs_handle_t
*zhp
)
3074 zfs_cmd_t zc
= { 0 };
3076 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3078 if (ZFS_IS_VOLUME(zhp
)) {
3080 * If user doesn't have permissions to unshare volume, then
3081 * abort the request. This would only happen for a
3082 * non-privileged user.
3084 if (zfs_unshare_iscsi(zhp
) != 0) {
3088 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3091 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3093 zc
.zc_objset_type
= DMU_OST_ZFS
;
3096 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0) {
3097 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3098 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3102 remove_mountpoint(zhp
);
3107 struct destroydata
{
3114 zfs_remove_link_cb(zfs_handle_t
*zhp
, void *arg
)
3116 struct destroydata
*dd
= arg
;
3118 char name
[ZFS_MAXNAMELEN
];
3119 boolean_t closezhp
= dd
->closezhp
;
3122 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3123 (void) strlcat(name
, "@", sizeof (name
));
3124 (void) strlcat(name
, dd
->snapname
, sizeof (name
));
3126 szhp
= make_dataset_handle(zhp
->zfs_hdl
, name
);
3128 dd
->gotone
= B_TRUE
;
3132 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3133 (void) zvol_remove_link(zhp
->zfs_hdl
, name
);
3135 * NB: this is simply a best-effort. We don't want to
3136 * return an error, because then we wouldn't visit all
3141 dd
->closezhp
= B_TRUE
;
3142 rv
= zfs_iter_filesystems(zhp
, zfs_remove_link_cb
, arg
);
3149 * Destroys all snapshots with the given name in zhp & descendants.
3152 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
)
3154 zfs_cmd_t zc
= { 0 };
3156 struct destroydata dd
= { 0 };
3158 dd
.snapname
= snapname
;
3159 (void) zfs_remove_link_cb(zhp
, &dd
);
3162 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3163 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3164 zhp
->zfs_name
, snapname
));
3167 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3168 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3170 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY_SNAPS
, &zc
);
3174 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3175 "cannot destroy '%s@%s'"), zc
.zc_name
, snapname
);
3179 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3180 "snapshot is cloned"));
3181 return (zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
));
3184 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3193 * Clones the given dataset. The target must be of the same type as the source.
3196 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3198 zfs_cmd_t zc
= { 0 };
3199 char parent
[ZFS_MAXNAMELEN
];
3202 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3206 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3208 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3209 "cannot create '%s'"), target
);
3211 /* validate the target name */
3212 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3213 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3215 /* validate parents exist */
3216 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3219 (void) parent_name(target
, parent
, sizeof (parent
));
3222 if (ZFS_IS_VOLUME(zhp
)) {
3223 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3224 type
= ZFS_TYPE_VOLUME
;
3226 zc
.zc_objset_type
= DMU_OST_ZFS
;
3227 type
= ZFS_TYPE_FILESYSTEM
;
3231 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3232 zhp
, errbuf
)) == NULL
)
3235 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3243 (void) strlcpy(zc
.zc_name
, target
, sizeof (zc
.zc_name
));
3244 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
, sizeof (zc
.zc_value
));
3245 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_CREATE
, &zc
);
3247 zcmd_free_nvlists(&zc
);
3254 * The parent doesn't exist. We should have caught this
3255 * above, but there may a race condition that has since
3256 * destroyed the parent.
3258 * At this point, we don't know whether it's the source
3259 * that doesn't exist anymore, or whether the target
3260 * dataset doesn't exist.
3262 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3263 "no such parent '%s'"), parent
);
3264 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3267 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3268 "source and target pools differ"));
3269 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3273 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3276 } else if (ZFS_IS_VOLUME(zhp
)) {
3277 ret
= zvol_create_link(zhp
->zfs_hdl
, target
);
3283 typedef struct promote_data
{
3284 char cb_mountpoint
[MAXPATHLEN
];
3285 const char *cb_target
;
3286 const char *cb_errbuf
;
3287 uint64_t cb_pivot_txg
;
3291 promote_snap_cb(zfs_handle_t
*zhp
, void *data
)
3293 promote_data_t
*pd
= data
;
3295 char snapname
[MAXPATHLEN
];
3298 /* We don't care about snapshots after the pivot point */
3299 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > pd
->cb_pivot_txg
) {
3304 /* Remove the device link if it's a zvol. */
3305 if (ZFS_IS_VOLUME(zhp
))
3306 (void) zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3308 /* Check for conflicting names */
3309 (void) strlcpy(snapname
, pd
->cb_target
, sizeof (snapname
));
3310 (void) strlcat(snapname
, strchr(zhp
->zfs_name
, '@'), sizeof (snapname
));
3311 szhp
= make_dataset_handle(zhp
->zfs_hdl
, snapname
);
3314 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3315 "snapshot name '%s' from origin \n"
3316 "conflicts with '%s' from target"),
3317 zhp
->zfs_name
, snapname
);
3318 rv
= zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, pd
->cb_errbuf
);
3325 promote_snap_done_cb(zfs_handle_t
*zhp
, void *data
)
3327 promote_data_t
*pd
= data
;
3329 /* We don't care about snapshots after the pivot point */
3330 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) <= pd
->cb_pivot_txg
) {
3331 /* Create the device link if it's a zvol. */
3332 if (ZFS_IS_VOLUME(zhp
))
3333 (void) zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
);
3341 * Promotes the given clone fs to be the clone parent.
3344 zfs_promote(zfs_handle_t
*zhp
)
3346 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3347 zfs_cmd_t zc
= { 0 };
3348 char parent
[MAXPATHLEN
];
3355 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3356 "cannot promote '%s'"), zhp
->zfs_name
);
3358 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3359 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3360 "snapshots can not be promoted"));
3361 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3364 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3365 if (parent
[0] == '\0') {
3366 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3367 "not a cloned filesystem"));
3368 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3370 cp
= strchr(parent
, '@');
3373 /* Walk the snapshots we will be moving */
3374 pzhp
= zfs_open(hdl
, zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
3377 pd
.cb_pivot_txg
= zfs_prop_get_int(pzhp
, ZFS_PROP_CREATETXG
);
3379 pd
.cb_target
= zhp
->zfs_name
;
3380 pd
.cb_errbuf
= errbuf
;
3381 pzhp
= zfs_open(hdl
, parent
, ZFS_TYPE_DATASET
);
3384 (void) zfs_prop_get(pzhp
, ZFS_PROP_MOUNTPOINT
, pd
.cb_mountpoint
,
3385 sizeof (pd
.cb_mountpoint
), NULL
, NULL
, 0, FALSE
);
3386 ret
= zfs_iter_snapshots(pzhp
, promote_snap_cb
, &pd
);
3392 /* issue the ioctl */
3393 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3394 sizeof (zc
.zc_value
));
3395 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3396 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3399 int save_errno
= errno
;
3401 (void) zfs_iter_snapshots(pzhp
, promote_snap_done_cb
, &pd
);
3404 switch (save_errno
) {
3407 * There is a conflicting snapshot name. We
3408 * should have caught this above, but they could
3409 * have renamed something in the mean time.
3411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3412 "conflicting snapshot name from parent '%s'"),
3414 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3417 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3420 (void) zfs_iter_snapshots(zhp
, promote_snap_done_cb
, &pd
);
3428 const char *cd_snapname
;
3433 zfs_create_link_cb(zfs_handle_t
*zhp
, void *arg
)
3435 struct createdata
*cd
= arg
;
3438 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3439 char name
[MAXPATHLEN
];
3441 (void) strlcpy(name
, zhp
->zfs_name
, sizeof (name
));
3442 (void) strlcat(name
, "@", sizeof (name
));
3443 (void) strlcat(name
, cd
->cd_snapname
, sizeof (name
));
3444 (void) zvol_create_link_common(zhp
->zfs_hdl
, name
,
3447 * NB: this is simply a best-effort. We don't want to
3448 * return an error, because then we wouldn't visit all
3453 ret
= zfs_iter_filesystems(zhp
, zfs_create_link_cb
, cd
);
3461 * Takes a snapshot of the given dataset.
3464 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3468 char parent
[ZFS_MAXNAMELEN
];
3470 zfs_cmd_t zc
= { 0 };
3474 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3475 "cannot snapshot '%s'"), path
);
3477 /* validate the target name */
3478 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3479 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3482 if ((props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3483 props
, B_FALSE
, NULL
, errbuf
)) == NULL
)
3486 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
3494 /* make sure the parent exists and is of the appropriate type */
3495 delim
= strchr(path
, '@');
3496 (void) strncpy(parent
, path
, delim
- path
);
3497 parent
[delim
- path
] = '\0';
3499 if ((zhp
= zfs_open(hdl
, parent
, ZFS_TYPE_FILESYSTEM
|
3500 ZFS_TYPE_VOLUME
)) == NULL
) {
3501 zcmd_free_nvlists(&zc
);
3505 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3506 (void) strlcpy(zc
.zc_value
, delim
+1, sizeof (zc
.zc_value
));
3507 if (ZFS_IS_VOLUME(zhp
))
3508 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3510 zc
.zc_objset_type
= DMU_OST_ZFS
;
3511 zc
.zc_cookie
= recursive
;
3512 ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SNAPSHOT
, &zc
);
3514 zcmd_free_nvlists(&zc
);
3517 * if it was recursive, the one that actually failed will be in
3521 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3522 "cannot create snapshot '%s@%s'"), zc
.zc_name
, zc
.zc_value
);
3524 if (ret
== 0 && recursive
) {
3525 struct createdata cd
;
3527 cd
.cd_snapname
= delim
+ 1;
3528 cd
.cd_ifexists
= B_FALSE
;
3529 (void) zfs_iter_filesystems(zhp
, zfs_create_link_cb
, &cd
);
3531 if (ret
== 0 && zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3532 ret
= zvol_create_link(zhp
->zfs_hdl
, path
);
3534 (void) zfs_standard_error(hdl
, errno
,
3535 dgettext(TEXT_DOMAIN
,
3536 "Volume successfully snapshotted, but device links "
3537 "were not created"));
3544 (void) zfs_standard_error(hdl
, errno
, errbuf
);
3552 * Destroy any more recent snapshots. We invoke this callback on any dependents
3553 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3554 * is a dependent and we should just destroy it without checking the transaction
3557 typedef struct rollback_data
{
3558 const char *cb_target
; /* the snapshot */
3559 uint64_t cb_create
; /* creation time reference */
3561 boolean_t cb_dependent
;
3566 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3568 rollback_data_t
*cbp
= data
;
3570 if (!cbp
->cb_dependent
) {
3571 if (strcmp(zhp
->zfs_name
, cbp
->cb_target
) != 0 &&
3572 zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
3573 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) >
3577 cbp
->cb_dependent
= B_TRUE
;
3578 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3579 rollback_destroy
, cbp
);
3580 cbp
->cb_dependent
= B_FALSE
;
3582 logstr
= zhp
->zfs_hdl
->libzfs_log_str
;
3583 zhp
->zfs_hdl
->libzfs_log_str
= NULL
;
3584 cbp
->cb_error
|= zfs_destroy(zhp
);
3585 zhp
->zfs_hdl
->libzfs_log_str
= logstr
;
3588 /* We must destroy this clone; first unmount it */
3589 prop_changelist_t
*clp
;
3591 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3592 cbp
->cb_force
? MS_FORCE
: 0);
3593 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3594 cbp
->cb_error
= B_TRUE
;
3598 if (zfs_destroy(zhp
) != 0)
3599 cbp
->cb_error
= B_TRUE
;
3601 changelist_remove(clp
, zhp
->zfs_name
);
3602 (void) changelist_postfix(clp
);
3603 changelist_free(clp
);
3611 * Given a dataset, rollback to a specific snapshot, discarding any
3612 * data changes since then and making it the active dataset.
3614 * Any snapshots more recent than the target are destroyed, along with
3618 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3620 rollback_data_t cb
= { 0 };
3622 zfs_cmd_t zc
= { 0 };
3623 boolean_t restore_resv
= 0;
3624 uint64_t old_volsize
, new_volsize
;
3625 zfs_prop_t resv_prop
;
3627 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3628 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3631 * Destroy all recent snapshots and its dependends.
3633 cb
.cb_force
= force
;
3634 cb
.cb_target
= snap
->zfs_name
;
3635 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3636 (void) zfs_iter_children(zhp
, rollback_destroy
, &cb
);
3642 * Now that we have verified that the snapshot is the latest,
3643 * rollback to the given snapshot.
3646 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3647 if (zvol_remove_link(zhp
->zfs_hdl
, zhp
->zfs_name
) != 0)
3649 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3651 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3653 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3656 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3658 if (ZFS_IS_VOLUME(zhp
))
3659 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3661 zc
.zc_objset_type
= DMU_OST_ZFS
;
3664 * We rely on zfs_iter_children() to verify that there are no
3665 * newer snapshots for the given dataset. Therefore, we can
3666 * simply pass the name on to the ioctl() call. There is still
3667 * an unlikely race condition where the user has taken a
3668 * snapshot since we verified that this was the most recent.
3671 if ((err
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_ROLLBACK
, &zc
)) != 0) {
3672 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3673 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3679 * For volumes, if the pre-rollback volsize matched the pre-
3680 * rollback reservation and the volsize has changed then set
3681 * the reservation property to the post-rollback volsize.
3682 * Make a new handle since the rollback closed the dataset.
3684 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3685 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3686 if (err
= zvol_create_link(zhp
->zfs_hdl
, zhp
->zfs_name
)) {
3691 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3692 if (old_volsize
!= new_volsize
)
3693 err
= zfs_prop_set_int(zhp
, resv_prop
,
3702 * Iterate over all dependents for a given dataset. This includes both
3703 * hierarchical dependents (children) and data dependents (snapshots and
3704 * clones). The bulk of the processing occurs in get_dependents() in
3708 zfs_iter_dependents(zfs_handle_t
*zhp
, boolean_t allowrecursion
,
3709 zfs_iter_f func
, void *data
)
3714 zfs_handle_t
*child
;
3717 if (get_dependents(zhp
->zfs_hdl
, allowrecursion
, zhp
->zfs_name
,
3718 &dependents
, &count
) != 0)
3721 for (i
= 0; i
< count
; i
++) {
3722 if ((child
= make_dataset_handle(zhp
->zfs_hdl
,
3723 dependents
[i
])) == NULL
)
3726 if ((ret
= func(child
, data
)) != 0)
3730 for (i
= 0; i
< count
; i
++)
3731 free(dependents
[i
]);
3738 * Renames the given dataset.
3741 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
)
3744 zfs_cmd_t zc
= { 0 };
3746 prop_changelist_t
*cl
= NULL
;
3747 zfs_handle_t
*zhrp
= NULL
;
3748 char *parentname
= NULL
;
3749 char parent
[ZFS_MAXNAMELEN
];
3750 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3753 /* if we have the same exact name, just return success */
3754 if (strcmp(zhp
->zfs_name
, target
) == 0)
3757 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3758 "cannot rename to '%s'"), target
);
3761 * Make sure the target name is valid
3763 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3764 if ((strchr(target
, '@') == NULL
) ||
3767 * Snapshot target name is abbreviated,
3768 * reconstruct full dataset name
3770 (void) strlcpy(parent
, zhp
->zfs_name
,
3772 delim
= strchr(parent
, '@');
3773 if (strchr(target
, '@') == NULL
)
3777 (void) strlcat(parent
, target
, sizeof (parent
));
3781 * Make sure we're renaming within the same dataset.
3783 delim
= strchr(target
, '@');
3784 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
3785 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
3786 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3787 "snapshots must be part of same "
3789 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
3793 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3794 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3797 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3798 "recursive rename must be a snapshot"));
3799 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3802 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
3803 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3806 /* validate parents */
3807 if (check_parents(hdl
, target
, &unused
, B_FALSE
, NULL
) != 0)
3810 (void) parent_name(target
, parent
, sizeof (parent
));
3812 /* make sure we're in the same pool */
3813 verify((delim
= strchr(target
, '/')) != NULL
);
3814 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
3815 zhp
->zfs_name
[delim
- target
] != '/') {
3816 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3817 "datasets must be within same pool"));
3818 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
3821 /* new name cannot be a child of the current dataset name */
3822 if (strncmp(parent
, zhp
->zfs_name
,
3823 strlen(zhp
->zfs_name
)) == 0) {
3824 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3825 "New dataset name cannot be a descendent of "
3826 "current dataset name"));
3827 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3831 (void) snprintf(errbuf
, sizeof (errbuf
),
3832 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
3834 if (getzoneid() == GLOBAL_ZONEID
&&
3835 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
3836 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3837 "dataset is used in a non-global zone"));
3838 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
3842 struct destroydata dd
;
3844 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
3845 if (parentname
== NULL
) {
3849 delim
= strchr(parentname
, '@');
3851 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
3857 dd
.snapname
= delim
+ 1;
3858 dd
.gotone
= B_FALSE
;
3859 dd
.closezhp
= B_TRUE
;
3861 /* We remove any zvol links prior to renaming them */
3862 ret
= zfs_iter_filesystems(zhrp
, zfs_remove_link_cb
, &dd
);
3867 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0)) == NULL
)
3870 if (changelist_haszonedchild(cl
)) {
3871 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3872 "child dataset with inherited mountpoint is used "
3873 "in a non-global zone"));
3874 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
3878 if ((ret
= changelist_prefix(cl
)) != 0)
3882 if (ZFS_IS_VOLUME(zhp
))
3883 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3885 zc
.zc_objset_type
= DMU_OST_ZFS
;
3887 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3888 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
3890 zc
.zc_cookie
= recursive
;
3892 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
3894 * if it was recursive, the one that actually failed will
3897 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3898 "cannot rename '%s'"), zc
.zc_name
);
3900 if (recursive
&& errno
== EEXIST
) {
3901 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3902 "a child dataset already has a snapshot "
3903 "with the new name"));
3904 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3906 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
3910 * On failure, we still want to remount any filesystems that
3911 * were previously mounted, so we don't alter the system state.
3914 struct createdata cd
;
3916 /* only create links for datasets that had existed */
3917 cd
.cd_snapname
= delim
+ 1;
3918 cd
.cd_ifexists
= B_TRUE
;
3919 (void) zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3922 (void) changelist_postfix(cl
);
3926 struct createdata cd
;
3928 /* only create links for datasets that had existed */
3929 cd
.cd_snapname
= strchr(target
, '@') + 1;
3930 cd
.cd_ifexists
= B_TRUE
;
3931 ret
= zfs_iter_filesystems(zhrp
, zfs_create_link_cb
,
3934 changelist_rename(cl
, zfs_get_name(zhp
), target
);
3935 ret
= changelist_postfix(cl
);
3947 changelist_free(cl
);
3953 * Given a zvol dataset, issue the ioctl to create the appropriate minor node,
3954 * poke devfsadm to create the /dev link, and then wait for the link to appear.
3957 zvol_create_link(libzfs_handle_t
*hdl
, const char *dataset
)
3959 return (zvol_create_link_common(hdl
, dataset
, B_FALSE
));
3963 zvol_create_link_common(libzfs_handle_t
*hdl
, const char *dataset
, int ifexists
)
3965 zfs_cmd_t zc
= { 0 };
3966 di_devlink_handle_t dhdl
;
3967 priv_set_t
*priv_effective
;
3970 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
3973 * Issue the appropriate ioctl.
3975 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CREATE_MINOR
, &zc
) != 0) {
3979 * Silently ignore the case where the link already
3980 * exists. This allows 'zfs volinit' to be run multiple
3981 * times without errors.
3987 * Dataset does not exist in the kernel. If we
3988 * don't care (see zfs_rename), then ignore the
3998 return (zfs_standard_error_fmt(hdl
, errno
,
3999 dgettext(TEXT_DOMAIN
, "cannot create device links "
4000 "for '%s'"), dataset
));
4005 * If privileged call devfsadm and wait for the links to
4007 * Otherwise, print out an informational message.
4010 priv_effective
= priv_allocset();
4011 (void) getppriv(PRIV_EFFECTIVE
, priv_effective
);
4012 privileged
= (priv_isfullset(priv_effective
) == B_TRUE
);
4013 priv_freeset(priv_effective
);
4016 if ((dhdl
= di_devlink_init(ZFS_DRIVER
,
4017 DI_MAKE_LINK
)) == NULL
) {
4018 zfs_error_aux(hdl
, strerror(errno
));
4019 (void) zfs_error_fmt(hdl
, errno
,
4020 dgettext(TEXT_DOMAIN
, "cannot create device links "
4021 "for '%s'"), dataset
);
4022 (void) ioctl(hdl
->libzfs_fd
, ZFS_IOC_REMOVE_MINOR
, &zc
);
4025 (void) di_devlink_fini(&dhdl
);
4028 char pathname
[MAXPATHLEN
];
4029 struct stat64 statbuf
;
4035 * This is the poor mans way of waiting for the link
4036 * to show up. If after 10 seconds we still don't
4037 * have it, then print out a message.
4039 (void) snprintf(pathname
, sizeof (pathname
), "/dev/zvol/dsk/%s",
4042 for (i
= 0; i
!= MAX_WAIT
; i
++) {
4043 if (stat64(pathname
, &statbuf
) == 0)
4048 (void) printf(gettext("%s may not be immediately "
4049 "available\n"), pathname
);
4056 * Remove a minor node for the given zvol and the associated /dev links.
4059 zvol_remove_link(libzfs_handle_t
*hdl
, const char *dataset
)
4061 zfs_cmd_t zc
= { 0 };
4063 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4065 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_REMOVE_MINOR
, &zc
) != 0) {
4069 * Silently ignore the case where the link no longer
4070 * exists, so that 'zfs volfini' can be run multiple
4071 * times without errors.
4076 return (zfs_standard_error_fmt(hdl
, errno
,
4077 dgettext(TEXT_DOMAIN
, "cannot remove device "
4078 "links for '%s'"), dataset
));
4086 zfs_get_user_props(zfs_handle_t
*zhp
)
4088 return (zhp
->zfs_user_props
);
4092 * This function is used by 'zfs list' to determine the exact set of columns to
4093 * display, and their maximum widths. This does two main things:
4095 * - If this is a list of all properties, then expand the list to include
4096 * all native properties, and set a flag so that for each dataset we look
4097 * for new unique user properties and add them to the list.
4099 * - For non fixed-width properties, keep track of the maximum width seen
4100 * so that we can size the column appropriately.
4103 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
)
4105 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4106 zprop_list_t
*entry
;
4107 zprop_list_t
**last
, **start
;
4108 nvlist_t
*userprops
, *propval
;
4111 char buf
[ZFS_MAXPROPLEN
];
4113 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4116 userprops
= zfs_get_user_props(zhp
);
4119 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4121 * Go through and add any user properties as necessary. We
4122 * start by incrementing our list pointer to the first
4123 * non-native property.
4126 while (*start
!= NULL
) {
4127 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4129 start
= &(*start
)->pl_next
;
4133 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4135 * See if we've already found this property in our list.
4137 for (last
= start
; *last
!= NULL
;
4138 last
= &(*last
)->pl_next
) {
4139 if (strcmp((*last
)->pl_user_prop
,
4140 nvpair_name(elem
)) == 0)
4144 if (*last
== NULL
) {
4145 if ((entry
= zfs_alloc(hdl
,
4146 sizeof (zprop_list_t
))) == NULL
||
4147 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4148 nvpair_name(elem
)))) == NULL
) {
4153 entry
->pl_prop
= ZPROP_INVAL
;
4154 entry
->pl_width
= strlen(nvpair_name(elem
));
4155 entry
->pl_all
= B_TRUE
;
4162 * Now go through and check the width of any non-fixed columns
4164 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4165 if (entry
->pl_fixed
)
4168 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4169 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4170 buf
, sizeof (buf
), NULL
, NULL
, 0, B_FALSE
) == 0) {
4171 if (strlen(buf
) > entry
->pl_width
)
4172 entry
->pl_width
= strlen(buf
);
4174 } else if (nvlist_lookup_nvlist(userprops
,
4175 entry
->pl_user_prop
, &propval
) == 0) {
4176 verify(nvlist_lookup_string(propval
,
4177 ZPROP_VALUE
, &strval
) == 0);
4178 if (strlen(strval
) > entry
->pl_width
)
4179 entry
->pl_width
= strlen(strval
);
4187 zfs_iscsi_perm_check(libzfs_handle_t
*hdl
, char *dataset
, ucred_t
*cred
)
4189 zfs_cmd_t zc
= { 0 };
4193 const gid_t
*groups
;
4197 if (nvlist_alloc(&nvp
, NV_UNIQUE_NAME
, 0) != 0)
4198 return (no_memory(hdl
));
4200 uid
= ucred_geteuid(cred
);
4201 gid
= ucred_getegid(cred
);
4202 group_cnt
= ucred_getgroups(cred
, &groups
);
4204 if (uid
== (uid_t
)-1 || gid
== (uid_t
)-1 || group_cnt
== (uid_t
)-1)
4207 if (nvlist_add_uint32(nvp
, ZFS_DELEG_PERM_UID
, uid
) != 0) {
4212 if (nvlist_add_uint32(nvp
, ZFS_DELEG_PERM_GID
, gid
) != 0) {
4217 if (nvlist_add_uint32_array(nvp
,
4218 ZFS_DELEG_PERM_GROUPS
, (uint32_t *)groups
, group_cnt
) != 0) {
4222 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4224 if (zcmd_write_src_nvlist(hdl
, &zc
, nvp
))
4227 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_ISCSI_PERM_CHECK
, &zc
);
4233 zfs_deleg_share_nfs(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4234 void *export
, void *sharetab
, int sharemax
, zfs_share_op_t operation
)
4236 zfs_cmd_t zc
= { 0 };
4239 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4240 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4241 zc
.zc_share
.z_sharedata
= (uint64_t)(uintptr_t)sharetab
;
4242 zc
.zc_share
.z_exportdata
= (uint64_t)(uintptr_t)export
;
4243 zc
.zc_share
.z_sharetype
= operation
;
4244 zc
.zc_share
.z_sharemax
= sharemax
;
4246 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SHARE
, &zc
);