4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2018, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2017 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017 RackTop Systems.
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
56 #include <directory.h>
58 #include <sys/dnode.h>
63 #include "zfs_namecheck.h"
65 #include "libzfs_impl.h"
66 #include "zfs_deleg.h"
68 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
69 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
72 * Given a single type (not a mask of types), return the type in a human
76 zfs_type_to_name(zfs_type_t type
)
79 case ZFS_TYPE_FILESYSTEM
:
80 return (dgettext(TEXT_DOMAIN
, "filesystem"));
81 case ZFS_TYPE_SNAPSHOT
:
82 return (dgettext(TEXT_DOMAIN
, "snapshot"));
84 return (dgettext(TEXT_DOMAIN
, "volume"));
86 return (dgettext(TEXT_DOMAIN
, "pool"));
87 case ZFS_TYPE_BOOKMARK
:
88 return (dgettext(TEXT_DOMAIN
, "bookmark"));
90 assert(!"unhandled zfs_type_t");
97 * Validate a ZFS path. This is used even before trying to open the dataset, to
98 * provide a more meaningful error message. We call zfs_error_aux() to
99 * explain exactly why the name was not valid.
102 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
108 if (entity_namecheck(path
, &why
, &what
) != 0) {
111 case NAME_ERR_TOOLONG
:
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "name is too long"));
116 case NAME_ERR_LEADING_SLASH
:
117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
118 "leading slash in name"));
121 case NAME_ERR_EMPTY_COMPONENT
:
122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
123 "empty component in name"));
126 case NAME_ERR_TRAILING_SLASH
:
127 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
128 "trailing slash in name"));
131 case NAME_ERR_INVALCHAR
:
133 dgettext(TEXT_DOMAIN
, "invalid character "
134 "'%c' in name"), what
);
137 case NAME_ERR_MULTIPLE_DELIMITERS
:
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "multiple '@' and/or '#' delimiters in "
143 case NAME_ERR_NOLETTER
:
144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
145 "pool doesn't begin with a letter"));
148 case NAME_ERR_RESERVED
:
149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
150 "name is reserved"));
153 case NAME_ERR_DISKLIKE
:
154 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
155 "reserved disk name"));
159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
160 "(%d) not defined"), why
);
168 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
170 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
171 "snapshot delimiter '@' is not expected here"));
175 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
177 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
178 "missing '@' delimiter in snapshot name"));
182 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
184 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
185 "bookmark delimiter '#' is not expected here"));
189 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
191 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
192 "missing '#' delimiter in bookmark name"));
196 if (modifying
&& strchr(path
, '%') != NULL
) {
198 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
199 "invalid character %c in name"), '%');
207 zfs_name_valid(const char *name
, zfs_type_t type
)
209 if (type
== ZFS_TYPE_POOL
)
210 return (zpool_name_valid(NULL
, B_FALSE
, name
));
211 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
215 * This function takes the raw DSL properties, and filters out the user-defined
216 * properties into a separate nvlist.
219 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
221 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
226 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
227 (void) no_memory(hdl
);
232 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
233 if (!zfs_prop_user(nvpair_name(elem
)))
236 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
237 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
239 (void) no_memory(hdl
);
247 static zpool_handle_t
*
248 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
250 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
253 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
254 if (hdl
->libzfs_pool_handles
!= NULL
)
255 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
256 hdl
->libzfs_pool_handles
= zph
;
261 static zpool_handle_t
*
262 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
264 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
265 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
267 while ((zph
!= NULL
) &&
268 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
269 zph
= zph
->zpool_next
;
274 * Returns a handle to the pool that contains the provided dataset.
275 * If a handle to that pool already exists then that handle is returned.
276 * Otherwise, a new handle is created and added to the list of handles.
278 static zpool_handle_t
*
279 zpool_handle(zfs_handle_t
*zhp
)
285 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
286 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
287 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
289 zph
= zpool_find_handle(zhp
, pool_name
, len
);
291 zph
= zpool_add_handle(zhp
, pool_name
);
298 zpool_free_handles(libzfs_handle_t
*hdl
)
300 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
302 while (zph
!= NULL
) {
303 next
= zph
->zpool_next
;
307 hdl
->libzfs_pool_handles
= NULL
;
311 * Utility function to gather stats (objset and zpl) for the given object.
314 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
316 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
318 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
320 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
321 if (errno
== ENOMEM
) {
322 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
333 * Utility function to get the received properties of the given object.
336 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
338 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
339 nvlist_t
*recvdprops
;
340 zfs_cmd_t zc
= { 0 };
343 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
346 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
348 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
349 if (errno
== ENOMEM
) {
350 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
354 zcmd_free_nvlists(&zc
);
359 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
360 zcmd_free_nvlists(&zc
);
364 nvlist_free(zhp
->zfs_recvd_props
);
365 zhp
->zfs_recvd_props
= recvdprops
;
371 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
373 nvlist_t
*allprops
, *userprops
;
375 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
377 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
382 * XXX Why do we store the user props separately, in addition to
383 * storing them in zfs_props?
385 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
386 nvlist_free(allprops
);
390 nvlist_free(zhp
->zfs_props
);
391 nvlist_free(zhp
->zfs_user_props
);
393 zhp
->zfs_props
= allprops
;
394 zhp
->zfs_user_props
= userprops
;
400 get_stats(zfs_handle_t
*zhp
)
403 zfs_cmd_t zc
= { 0 };
405 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
407 if (get_stats_ioctl(zhp
, &zc
) != 0)
409 else if (put_stats_zhdl(zhp
, &zc
) != 0)
411 zcmd_free_nvlists(&zc
);
416 * Refresh the properties currently stored in the handle.
419 zfs_refresh_properties(zfs_handle_t
*zhp
)
421 (void) get_stats(zhp
);
425 * Makes a handle from the given dataset name. Used by zfs_open() and
426 * zfs_iter_* to create child handles on the fly.
429 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
431 if (put_stats_zhdl(zhp
, zc
) != 0)
435 * We've managed to open the dataset and gather statistics. Determine
436 * the high-level type.
438 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
439 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
440 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
441 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
445 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
446 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
447 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
448 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
449 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
450 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
452 abort(); /* we should never see any other types */
454 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
461 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
463 zfs_cmd_t zc
= { 0 };
465 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
471 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
472 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
476 if (get_stats_ioctl(zhp
, &zc
) == -1) {
477 zcmd_free_nvlists(&zc
);
481 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
485 zcmd_free_nvlists(&zc
);
490 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
492 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
498 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
499 if (make_dataset_handle_common(zhp
, zc
) == -1) {
507 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
509 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
514 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
515 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
516 zhp
->zfs_head_type
= pzhp
->zfs_type
;
517 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
518 zhp
->zpool_hdl
= zpool_handle(zhp
);
523 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
525 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
530 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
531 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
532 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
533 sizeof (zhp
->zfs_name
));
534 zhp
->zfs_type
= zhp_orig
->zfs_type
;
535 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
536 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
537 if (zhp_orig
->zfs_props
!= NULL
) {
538 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
539 (void) no_memory(zhp
->zfs_hdl
);
544 if (zhp_orig
->zfs_user_props
!= NULL
) {
545 if (nvlist_dup(zhp_orig
->zfs_user_props
,
546 &zhp
->zfs_user_props
, 0) != 0) {
547 (void) no_memory(zhp
->zfs_hdl
);
552 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
553 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
554 &zhp
->zfs_recvd_props
, 0)) {
555 (void) no_memory(zhp
->zfs_hdl
);
560 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
561 if (zhp_orig
->zfs_mntopts
!= NULL
) {
562 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
563 zhp_orig
->zfs_mntopts
);
565 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
570 zfs_bookmark_exists(const char *path
)
574 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
581 (void) strlcpy(fsname
, path
, sizeof (fsname
));
582 pound
= strchr(fsname
, '#');
587 bmark_name
= pound
+ 1;
588 props
= fnvlist_alloc();
589 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
596 rv
= nvlist_exists(bmarks
, bmark_name
);
602 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
603 nvlist_t
*bmark_props
)
605 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
610 /* Fill in the name. */
611 zhp
->zfs_hdl
= parent
->zfs_hdl
;
612 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
614 /* Set the property lists. */
615 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
621 zhp
->zfs_head_type
= parent
->zfs_head_type
;
622 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
624 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
625 nvlist_free(zhp
->zfs_props
);
633 struct zfs_open_bookmarks_cb_data
{
639 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
641 struct zfs_open_bookmarks_cb_data
*dp
= data
;
644 * Is it the one we are looking for?
646 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
648 * We found it. Save it and let the caller know we are done.
655 * Not found. Close the handle and ask for another one.
662 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
663 * argument is a mask of acceptable types. The function will print an
664 * appropriate error message and return NULL if it can't be opened.
667 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
673 (void) snprintf(errbuf
, sizeof (errbuf
),
674 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
677 * Validate the name before we even try to open it.
679 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
680 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
685 * Bookmarks needs to be handled separately.
687 bookp
= strchr(path
, '#');
690 * Try to get stats for the dataset, which will tell us if it
694 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
695 (void) zfs_standard_error(hdl
, errno
, errbuf
);
699 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
701 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
704 * We need to cut out '#' and everything after '#'
705 * to get the parent dataset name only.
707 assert(bookp
- path
< sizeof (dsname
));
708 (void) strncpy(dsname
, path
, bookp
- path
);
709 dsname
[bookp
- path
] = '\0';
712 * Create handle for the parent dataset.
715 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
716 (void) zfs_standard_error(hdl
, errno
, errbuf
);
721 * Iterate bookmarks to find the right one.
724 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
725 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
726 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
730 if (cb_data
.zhp
== NULL
) {
731 (void) zfs_standard_error(hdl
, errno
, errbuf
);
743 if (!(types
& zhp
->zfs_type
)) {
744 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
753 * Release a ZFS handle. Nothing to do but free the associated memory.
756 zfs_close(zfs_handle_t
*zhp
)
758 free(zhp
->zfs_mntopts
);
759 nvlist_free(zhp
->zfs_props
);
760 nvlist_free(zhp
->zfs_user_props
);
761 nvlist_free(zhp
->zfs_recvd_props
);
765 typedef struct mnttab_node
{
766 struct mnttab mtn_mt
;
771 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
773 const mnttab_node_t
*mtn1
= arg1
;
774 const mnttab_node_t
*mtn2
= arg2
;
777 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
781 return (rv
> 0 ? 1 : -1);
785 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
787 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
788 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
789 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
793 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
797 rewind(hdl
->libzfs_mnttab
);
798 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
801 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
803 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
804 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
805 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
806 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
807 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
808 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
813 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
818 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))
820 free(mtn
->mtn_mt
.mnt_special
);
821 free(mtn
->mtn_mt
.mnt_mountp
);
822 free(mtn
->mtn_mt
.mnt_fstype
);
823 free(mtn
->mtn_mt
.mnt_mntopts
);
826 avl_destroy(&hdl
->libzfs_mnttab_cache
);
830 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
832 hdl
->libzfs_mnttab_enable
= enable
;
836 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
837 struct mnttab
*entry
)
842 if (!hdl
->libzfs_mnttab_enable
) {
843 struct mnttab srch
= { 0 };
845 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
846 libzfs_mnttab_fini(hdl
);
847 rewind(hdl
->libzfs_mnttab
);
848 srch
.mnt_special
= (char *)fsname
;
849 srch
.mnt_fstype
= MNTTYPE_ZFS
;
850 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
856 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
857 libzfs_mnttab_update(hdl
);
859 find
.mtn_mt
.mnt_special
= (char *)fsname
;
860 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
862 *entry
= mtn
->mtn_mt
;
869 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
870 const char *mountp
, const char *mntopts
)
874 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
876 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
877 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
878 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
879 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
880 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
881 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
885 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
890 find
.mtn_mt
.mnt_special
= (char *)fsname
;
891 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))
893 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
894 free(ret
->mtn_mt
.mnt_special
);
895 free(ret
->mtn_mt
.mnt_mountp
);
896 free(ret
->mtn_mt
.mnt_fstype
);
897 free(ret
->mtn_mt
.mnt_mntopts
);
903 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
905 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
907 if (zpool_handle
== NULL
)
910 *spa_version
= zpool_get_prop_int(zpool_handle
,
911 ZPOOL_PROP_VERSION
, NULL
);
916 * The choice of reservation property depends on the SPA version.
919 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
923 if (zfs_spa_version(zhp
, &spa_version
) < 0)
926 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
927 *resv_prop
= ZFS_PROP_REFRESERVATION
;
929 *resv_prop
= ZFS_PROP_RESERVATION
;
935 * Given an nvlist of properties to set, validates that they are correct, and
936 * parses any numeric properties (index, boolean, etc) if they are specified as
940 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
941 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
949 int chosen_normal
= -1;
952 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
953 (void) no_memory(hdl
);
958 * Make sure this property is valid and applies to this type.
962 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
963 const char *propname
= nvpair_name(elem
);
965 prop
= zfs_name_to_prop(propname
);
966 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
968 * This is a user property: make sure it's a
969 * string, and that it's less than ZAP_MAXNAMELEN.
971 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
972 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
973 "'%s' must be a string"), propname
);
974 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
978 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
979 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
980 "property name '%s' is too long"),
982 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
986 (void) nvpair_value_string(elem
, &strval
);
987 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
988 (void) no_memory(hdl
);
995 * Currently, only user properties can be modified on
998 if (type
== ZFS_TYPE_SNAPSHOT
) {
999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1000 "this property can not be modified for snapshots"));
1001 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1005 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1006 zfs_userquota_prop_t uqtype
;
1007 char newpropname
[128];
1012 if (userquota_propname_decode(propname
, zoned
,
1013 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1015 dgettext(TEXT_DOMAIN
,
1016 "'%s' has an invalid user/group name"),
1018 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1022 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1023 uqtype
!= ZFS_PROP_GROUPQUOTA
) {
1025 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1027 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1032 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1033 (void) nvpair_value_string(elem
, &strval
);
1034 if (strcmp(strval
, "none") == 0) {
1036 } else if (zfs_nicestrtonum(hdl
,
1037 strval
, &intval
) != 0) {
1038 (void) zfs_error(hdl
,
1039 EZFS_BADPROP
, errbuf
);
1042 } else if (nvpair_type(elem
) ==
1044 (void) nvpair_value_uint64(elem
, &intval
);
1046 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1047 "use 'none' to disable "
1048 "userquota/groupquota"));
1052 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1053 "'%s' must be a number"), propname
);
1054 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1059 * Encode the prop name as
1060 * userquota@<hex-rid>-domain, to make it easy
1061 * for the kernel to decode.
1063 (void) snprintf(newpropname
, sizeof (newpropname
),
1064 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
1065 (longlong_t
)rid
, domain
);
1069 if (nvlist_add_uint64_array(ret
, newpropname
,
1071 (void) no_memory(hdl
);
1075 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1076 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1077 "'%s' is readonly"),
1079 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1083 if (prop
== ZPROP_INVAL
) {
1084 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1085 "invalid property '%s'"), propname
);
1086 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1090 if (!zfs_prop_valid_for_type(prop
, type
)) {
1092 dgettext(TEXT_DOMAIN
, "'%s' does not "
1093 "apply to datasets of this type"), propname
);
1094 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1098 if (zfs_prop_readonly(prop
) &&
1099 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
1101 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1103 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1107 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1108 &strval
, &intval
, errbuf
) != 0)
1112 * Perform some additional checks for specific properties.
1115 case ZFS_PROP_VERSION
:
1121 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1122 if (intval
< version
) {
1123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1124 "Can not downgrade; already at version %u"),
1126 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1132 case ZFS_PROP_VOLBLOCKSIZE
:
1133 case ZFS_PROP_RECORDSIZE
:
1135 int maxbs
= SPA_MAXBLOCKSIZE
;
1136 if (zpool_hdl
!= NULL
) {
1137 maxbs
= zpool_get_prop_int(zpool_hdl
,
1138 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1141 * Volumes are limited to a volblocksize of 128KB,
1142 * because they typically service workloads with
1143 * small random writes, which incur a large performance
1144 * penalty with large blocks.
1146 if (prop
== ZFS_PROP_VOLBLOCKSIZE
)
1147 maxbs
= SPA_OLD_MAXBLOCKSIZE
;
1149 * The value must be a power of two between
1150 * SPA_MINBLOCKSIZE and maxbs.
1152 if (intval
< SPA_MINBLOCKSIZE
||
1153 intval
> maxbs
|| !ISP2(intval
)) {
1154 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1155 "'%s' must be power of 2 from 512B "
1156 "to %uKB"), propname
, maxbs
>> 10);
1157 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1162 case ZFS_PROP_MOUNTPOINT
:
1164 namecheck_err_t why
;
1166 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1167 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1170 if (mountpoint_namecheck(strval
, &why
)) {
1172 case NAME_ERR_LEADING_SLASH
:
1174 dgettext(TEXT_DOMAIN
,
1175 "'%s' must be an absolute path, "
1176 "'none', or 'legacy'"), propname
);
1178 case NAME_ERR_TOOLONG
:
1180 dgettext(TEXT_DOMAIN
,
1181 "component of '%s' is too long"),
1187 dgettext(TEXT_DOMAIN
,
1188 "(%d) not defined"),
1192 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1199 case ZFS_PROP_SHARESMB
:
1200 case ZFS_PROP_SHARENFS
:
1202 * For the mountpoint and sharenfs or sharesmb
1203 * properties, check if it can be set in a
1204 * global/non-global zone based on
1205 * the zoned property value:
1207 * global zone non-global zone
1208 * --------------------------------------------------
1209 * zoned=on mountpoint (no) mountpoint (yes)
1210 * sharenfs (no) sharenfs (no)
1211 * sharesmb (no) sharesmb (no)
1213 * zoned=off mountpoint (yes) N/A
1218 if (getzoneid() == GLOBAL_ZONEID
) {
1219 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1220 "'%s' cannot be set on "
1221 "dataset in a non-global zone"),
1223 (void) zfs_error(hdl
, EZFS_ZONED
,
1226 } else if (prop
== ZFS_PROP_SHARENFS
||
1227 prop
== ZFS_PROP_SHARESMB
) {
1228 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1229 "'%s' cannot be set in "
1230 "a non-global zone"), propname
);
1231 (void) zfs_error(hdl
, EZFS_ZONED
,
1235 } else if (getzoneid() != GLOBAL_ZONEID
) {
1237 * If zoned property is 'off', this must be in
1238 * a global zone. If not, something is wrong.
1240 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1241 "'%s' cannot be set while dataset "
1242 "'zoned' property is set"), propname
);
1243 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1248 * At this point, it is legitimate to set the
1249 * property. Now we want to make sure that the
1250 * property value is valid if it is sharenfs.
1252 if ((prop
== ZFS_PROP_SHARENFS
||
1253 prop
== ZFS_PROP_SHARESMB
) &&
1254 strcmp(strval
, "on") != 0 &&
1255 strcmp(strval
, "off") != 0) {
1256 zfs_share_proto_t proto
;
1258 if (prop
== ZFS_PROP_SHARESMB
)
1264 * Must be an valid sharing protocol
1265 * option string so init the libshare
1266 * in order to enable the parser and
1267 * then parse the options. We use the
1268 * control API since we don't care about
1269 * the current configuration and don't
1270 * want the overhead of loading it
1271 * until we actually do something.
1274 if (zfs_init_libshare(hdl
,
1275 SA_INIT_CONTROL_API
) != SA_OK
) {
1277 * An error occurred so we can't do
1280 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1281 "'%s' cannot be set: problem "
1282 "in share initialization"),
1284 (void) zfs_error(hdl
, EZFS_BADPROP
,
1289 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1291 * There was an error in parsing so
1292 * deal with it by issuing an error
1293 * message and leaving after
1294 * uninitializing the the libshare
1297 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1298 "'%s' cannot be set to invalid "
1299 "options"), propname
);
1300 (void) zfs_error(hdl
, EZFS_BADPROP
,
1302 zfs_uninit_libshare(hdl
);
1305 zfs_uninit_libshare(hdl
);
1310 case ZFS_PROP_UTF8ONLY
:
1311 chosen_utf
= (int)intval
;
1314 case ZFS_PROP_NORMALIZE
:
1315 chosen_normal
= (int)intval
;
1323 * For changes to existing volumes, we have some additional
1324 * checks to enforce.
1326 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1327 uint64_t volsize
= zfs_prop_get_int(zhp
,
1329 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1330 ZFS_PROP_VOLBLOCKSIZE
);
1334 case ZFS_PROP_RESERVATION
:
1335 if (intval
> volsize
) {
1336 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1337 "'%s' is greater than current "
1338 "volume size"), propname
);
1339 (void) zfs_error(hdl
, EZFS_BADPROP
,
1345 case ZFS_PROP_REFRESERVATION
:
1346 if (intval
> volsize
&& intval
!= UINT64_MAX
) {
1347 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1348 "'%s' is greater than current "
1349 "volume size"), propname
);
1350 (void) zfs_error(hdl
, EZFS_BADPROP
,
1356 case ZFS_PROP_VOLSIZE
:
1357 if (intval
% blocksize
!= 0) {
1358 zfs_nicenum(blocksize
, buf
,
1360 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1361 "'%s' must be a multiple of "
1362 "volume block size (%s)"),
1364 (void) zfs_error(hdl
, EZFS_BADPROP
,
1370 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1371 "'%s' cannot be zero"),
1373 (void) zfs_error(hdl
, EZFS_BADPROP
,
1386 * If normalization was chosen, but no UTF8 choice was made,
1387 * enforce rejection of non-UTF8 names.
1389 * If normalization was chosen, but rejecting non-UTF8 names
1390 * was explicitly not chosen, it is an error.
1392 if (chosen_normal
> 0 && chosen_utf
< 0) {
1393 if (nvlist_add_uint64(ret
,
1394 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1395 (void) no_memory(hdl
);
1398 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1399 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1400 "'%s' must be set 'on' if normalization chosen"),
1401 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1402 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1413 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1415 uint64_t old_volsize
;
1416 uint64_t new_volsize
;
1417 uint64_t old_reservation
;
1418 uint64_t new_reservation
;
1419 zfs_prop_t resv_prop
;
1423 * If this is an existing volume, and someone is setting the volsize,
1424 * make sure that it matches the reservation, or add it if necessary.
1426 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1427 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1429 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1431 props
= fnvlist_alloc();
1432 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1433 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1435 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1436 old_reservation
) || nvlist_exists(nvl
,
1437 zfs_prop_to_name(resv_prop
))) {
1438 fnvlist_free(props
);
1441 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1442 &new_volsize
) != 0) {
1443 fnvlist_free(props
);
1446 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1447 fnvlist_free(props
);
1449 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1450 new_reservation
) != 0) {
1451 (void) no_memory(zhp
->zfs_hdl
);
1458 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1459 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1460 * Return codes must match zfs_add_synthetic_resv().
1463 zfs_fix_auto_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1470 if (!ZFS_IS_VOLUME(zhp
)) {
1474 if (zfs_which_resv_prop(zhp
, &prop
) != 0) {
1478 if (prop
!= ZFS_PROP_REFRESERVATION
) {
1482 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(prop
), &resvsize
) != 0) {
1483 /* No value being set, so it can't be "auto" */
1486 if (resvsize
!= UINT64_MAX
) {
1487 /* Being set to a value other than "auto" */
1491 props
= fnvlist_alloc();
1493 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1494 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1496 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1498 volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1501 resvsize
= zvol_volsize_to_reservation(volsize
, props
);
1502 fnvlist_free(props
);
1504 (void) nvlist_remove_all(nvl
, zfs_prop_to_name(prop
));
1505 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(prop
), resvsize
) != 0) {
1506 (void) no_memory(zhp
->zfs_hdl
);
1513 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1520 * For quotas and reservations, ENOSPC indicates
1521 * something different; setting a quota or reservation
1522 * doesn't use any disk space.
1525 case ZFS_PROP_QUOTA
:
1526 case ZFS_PROP_REFQUOTA
:
1527 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1528 "size is less than current used or "
1530 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1533 case ZFS_PROP_RESERVATION
:
1534 case ZFS_PROP_REFRESERVATION
:
1535 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1536 "size is greater than available space"));
1537 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1541 (void) zfs_standard_error(hdl
, err
, errbuf
);
1547 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1551 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1555 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1556 "property value too long"));
1557 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1562 "pool and or dataset must be upgraded to set this "
1563 "property or value"));
1564 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1568 if (prop
== ZFS_PROP_COMPRESSION
||
1569 prop
== ZFS_PROP_RECORDSIZE
) {
1570 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1571 "property setting is not allowed on "
1572 "bootable datasets"));
1573 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1574 } else if (prop
== ZFS_PROP_CHECKSUM
||
1575 prop
== ZFS_PROP_DEDUP
) {
1576 (void) zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1577 "property setting is not allowed on "
1579 (void) zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
1581 (void) zfs_standard_error(hdl
, err
, errbuf
);
1586 if (prop
== ZPROP_INVAL
) {
1587 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1589 (void) zfs_standard_error(hdl
, err
, errbuf
);
1595 * This platform can't address a volume this big.
1598 if (prop
== ZFS_PROP_VOLSIZE
) {
1599 (void) zfs_error(hdl
, EZFS_VOLTOOBIG
, errbuf
);
1605 (void) zfs_standard_error(hdl
, err
, errbuf
);
1610 * Given a property name and value, set the property for the given dataset.
1613 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1617 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1618 nvlist_t
*nvl
= NULL
;
1620 (void) snprintf(errbuf
, sizeof (errbuf
),
1621 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1624 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1625 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1626 (void) no_memory(hdl
);
1630 ret
= zfs_prop_set_list(zhp
, nvl
);
1640 * Given an nvlist of property names and values, set the properties for the
1644 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1646 zfs_cmd_t zc
= { 0 };
1648 prop_changelist_t
**cls
= NULL
;
1651 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1656 (void) snprintf(errbuf
, sizeof (errbuf
),
1657 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1660 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1661 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1666 * We have to check for any extra properties which need to be added
1667 * before computing the length of the nvlist.
1669 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1671 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1672 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1673 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1678 if (added_resv
!= 1 &&
1679 (added_resv
= zfs_fix_auto_resv(zhp
, nvl
)) == -1) {
1684 * Check how many properties we're setting and allocate an array to
1685 * store changelist pointers for postfix().
1688 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1690 elem
= nvlist_next_nvpair(nvl
, elem
))
1692 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1696 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1698 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1700 zfs_prop_t prop
= zfs_name_to_prop(nvpair_name(elem
));
1702 assert(cl_idx
< nvl_len
);
1704 * We don't want to unmount & remount the dataset when changing
1705 * its canmount property to 'on' or 'noauto'. We only use
1706 * the changelist logic to unmount when setting canmount=off.
1708 if (prop
!= ZFS_PROP_CANMOUNT
||
1709 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1710 zfs_is_mounted(zhp
, NULL
))) {
1711 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1712 if (cls
[cl_idx
] == NULL
)
1716 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1717 changelist_haszonedchild(cls
[cl_idx
])) {
1718 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1719 "child dataset with inherited mountpoint is used "
1720 "in a non-global zone"));
1721 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1725 if (cls
[cl_idx
] != NULL
&&
1726 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1731 assert(cl_idx
== nvl_len
);
1734 * Execute the corresponding ioctl() to set this list of properties.
1736 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1738 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1739 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1742 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1745 /* Get the list of unset properties back and report them. */
1746 nvlist_t
*errorprops
= NULL
;
1747 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1749 for (nvpair_t
*elem
= nvlist_next_nvpair(nvl
, NULL
);
1751 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1752 zfs_prop_t prop
= zfs_name_to_prop(nvpair_name(elem
));
1753 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1755 nvlist_free(errorprops
);
1757 if (added_resv
&& errno
== ENOSPC
) {
1758 /* clean up the volsize property we tried to set */
1759 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1763 zcmd_free_nvlists(&zc
);
1765 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1767 if (nvlist_add_uint64(nvl
,
1768 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1771 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1773 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1776 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1777 if (cls
[cl_idx
] != NULL
) {
1778 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1785 * Refresh the statistics so the new property value
1789 (void) get_stats(zhp
);
1794 zcmd_free_nvlists(&zc
);
1796 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1797 if (cls
[cl_idx
] != NULL
)
1798 changelist_free(cls
[cl_idx
]);
1806 * Given a property, inherit the value from the parent dataset, or if received
1807 * is TRUE, revert to the received value, if any.
1810 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1812 zfs_cmd_t zc
= { 0 };
1814 prop_changelist_t
*cl
;
1815 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1819 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1820 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1822 zc
.zc_cookie
= received
;
1823 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1825 * For user properties, the amount of work we have to do is very
1826 * small, so just do it here.
1828 if (!zfs_prop_user(propname
)) {
1829 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1830 "invalid property"));
1831 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1834 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1835 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1837 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1838 return (zfs_standard_error(hdl
, errno
, errbuf
));
1844 * Verify that this property is inheritable.
1846 if (zfs_prop_readonly(prop
))
1847 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1849 if (!zfs_prop_inheritable(prop
) && !received
)
1850 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1853 * Check to see if the value applies to this type
1855 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
1856 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1859 * Normalize the name, to get rid of shorthand abbreviations.
1861 propname
= zfs_prop_to_name(prop
);
1862 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1863 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1865 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1866 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1867 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1868 "dataset is used in a non-global zone"));
1869 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1873 * Determine datasets which will be affected by this change, if any.
1875 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1878 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1879 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1880 "child dataset with inherited mountpoint is used "
1881 "in a non-global zone"));
1882 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1886 if ((ret
= changelist_prefix(cl
)) != 0)
1889 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1890 return (zfs_standard_error(hdl
, errno
, errbuf
));
1893 if ((ret
= changelist_postfix(cl
)) != 0)
1897 * Refresh the statistics so the new property is reflected.
1899 (void) get_stats(zhp
);
1903 changelist_free(cl
);
1908 * True DSL properties are stored in an nvlist. The following two functions
1909 * extract them appropriately.
1912 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1918 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1919 zfs_prop_to_name(prop
), &nv
) == 0) {
1920 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1921 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1923 verify(!zhp
->zfs_props_table
||
1924 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1925 value
= zfs_prop_default_numeric(prop
);
1933 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1939 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1940 zfs_prop_to_name(prop
), &nv
) == 0) {
1941 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
1942 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1944 verify(!zhp
->zfs_props_table
||
1945 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1946 value
= zfs_prop_default_string(prop
);
1954 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1956 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1960 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1962 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1963 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
1967 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1969 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
1974 * Internal function for getting a numeric property. Both zfs_prop_get() and
1975 * zfs_prop_get_int() are built using this interface.
1977 * Certain properties can be overridden using 'mount -o'. In this case, scan
1978 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1979 * If they differ from the on-disk values, report the current values and mark
1980 * the source "temporary".
1983 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
1984 char **source
, uint64_t *val
)
1986 zfs_cmd_t zc
= { 0 };
1987 nvlist_t
*zplprops
= NULL
;
1989 char *mntopt_on
= NULL
;
1990 char *mntopt_off
= NULL
;
1991 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
1996 case ZFS_PROP_ATIME
:
1997 mntopt_on
= MNTOPT_ATIME
;
1998 mntopt_off
= MNTOPT_NOATIME
;
2001 case ZFS_PROP_DEVICES
:
2002 mntopt_on
= MNTOPT_DEVICES
;
2003 mntopt_off
= MNTOPT_NODEVICES
;
2007 mntopt_on
= MNTOPT_EXEC
;
2008 mntopt_off
= MNTOPT_NOEXEC
;
2011 case ZFS_PROP_READONLY
:
2012 mntopt_on
= MNTOPT_RO
;
2013 mntopt_off
= MNTOPT_RW
;
2016 case ZFS_PROP_SETUID
:
2017 mntopt_on
= MNTOPT_SETUID
;
2018 mntopt_off
= MNTOPT_NOSETUID
;
2021 case ZFS_PROP_XATTR
:
2022 mntopt_on
= MNTOPT_XATTR
;
2023 mntopt_off
= MNTOPT_NOXATTR
;
2026 case ZFS_PROP_NBMAND
:
2027 mntopt_on
= MNTOPT_NBMAND
;
2028 mntopt_off
= MNTOPT_NONBMAND
;
2036 * Because looking up the mount options is potentially expensive
2037 * (iterating over all of /etc/mnttab), we defer its calculation until
2038 * we're looking up a property which requires its presence.
2040 if (!zhp
->zfs_mntcheck
&&
2041 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2042 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2043 struct mnttab entry
;
2045 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2046 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2048 if (zhp
->zfs_mntopts
== NULL
)
2052 zhp
->zfs_mntcheck
= B_TRUE
;
2055 if (zhp
->zfs_mntopts
== NULL
)
2056 mnt
.mnt_mntopts
= "";
2058 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2061 case ZFS_PROP_ATIME
:
2062 case ZFS_PROP_DEVICES
:
2064 case ZFS_PROP_READONLY
:
2065 case ZFS_PROP_SETUID
:
2066 case ZFS_PROP_XATTR
:
2067 case ZFS_PROP_NBMAND
:
2068 *val
= getprop_uint64(zhp
, prop
, source
);
2073 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2076 *src
= ZPROP_SRC_TEMPORARY
;
2077 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2080 *src
= ZPROP_SRC_TEMPORARY
;
2084 case ZFS_PROP_CANMOUNT
:
2085 case ZFS_PROP_VOLSIZE
:
2086 case ZFS_PROP_QUOTA
:
2087 case ZFS_PROP_REFQUOTA
:
2088 case ZFS_PROP_RESERVATION
:
2089 case ZFS_PROP_REFRESERVATION
:
2090 case ZFS_PROP_FILESYSTEM_LIMIT
:
2091 case ZFS_PROP_SNAPSHOT_LIMIT
:
2092 case ZFS_PROP_FILESYSTEM_COUNT
:
2093 case ZFS_PROP_SNAPSHOT_COUNT
:
2094 *val
= getprop_uint64(zhp
, prop
, source
);
2096 if (*source
== NULL
) {
2097 /* not default, must be local */
2098 *source
= zhp
->zfs_name
;
2102 case ZFS_PROP_MOUNTED
:
2103 *val
= (zhp
->zfs_mntopts
!= NULL
);
2106 case ZFS_PROP_NUMCLONES
:
2107 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2110 case ZFS_PROP_VERSION
:
2111 case ZFS_PROP_NORMALIZE
:
2112 case ZFS_PROP_UTF8ONLY
:
2114 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
) ||
2115 zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2117 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2118 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2119 zcmd_free_nvlists(&zc
);
2122 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2123 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2125 zcmd_free_nvlists(&zc
);
2128 nvlist_free(zplprops
);
2129 zcmd_free_nvlists(&zc
);
2132 case ZFS_PROP_INCONSISTENT
:
2133 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2137 switch (zfs_prop_get_type(prop
)) {
2138 case PROP_TYPE_NUMBER
:
2139 case PROP_TYPE_INDEX
:
2140 *val
= getprop_uint64(zhp
, prop
, source
);
2142 * If we tried to use a default value for a
2143 * readonly property, it means that it was not
2144 * present. Note this only applies to "truly"
2145 * readonly properties, not set-once properties
2146 * like volblocksize.
2148 if (zfs_prop_readonly(prop
) &&
2149 !zfs_prop_setonce(prop
) &&
2150 *source
!= NULL
&& (*source
)[0] == '\0') {
2156 case PROP_TYPE_STRING
:
2158 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2159 "cannot get non-numeric property"));
2160 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2161 dgettext(TEXT_DOMAIN
, "internal error")));
2169 * Calculate the source type, given the raw source string.
2172 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2173 char *statbuf
, size_t statlen
)
2175 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2178 if (source
== NULL
) {
2179 *srctype
= ZPROP_SRC_NONE
;
2180 } else if (source
[0] == '\0') {
2181 *srctype
= ZPROP_SRC_DEFAULT
;
2182 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2183 *srctype
= ZPROP_SRC_RECEIVED
;
2185 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2186 *srctype
= ZPROP_SRC_LOCAL
;
2188 (void) strlcpy(statbuf
, source
, statlen
);
2189 *srctype
= ZPROP_SRC_INHERITED
;
2196 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2197 size_t proplen
, boolean_t literal
)
2202 if (zhp
->zfs_recvd_props
== NULL
)
2203 if (get_recvd_props_ioctl(zhp
) != 0)
2206 prop
= zfs_name_to_prop(propname
);
2208 if (prop
!= ZPROP_INVAL
) {
2210 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2212 zfs_set_recvd_props_mode(zhp
, &cookie
);
2213 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2214 NULL
, NULL
, 0, literal
);
2215 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2219 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2220 propname
, &propval
) != 0)
2222 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2224 (void) strlcpy(propbuf
, recvdval
, proplen
);
2227 return (err
== 0 ? 0 : -1);
2231 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2236 value
= zfs_get_clones_nvl(zhp
);
2241 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2242 pair
= nvlist_next_nvpair(value
, pair
)) {
2243 if (propbuf
[0] != '\0')
2244 (void) strlcat(propbuf
, ",", proplen
);
2245 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2251 struct get_clones_arg
{
2255 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2259 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2261 struct get_clones_arg
*gca
= arg
;
2263 if (gca
->numclones
== 0) {
2268 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2269 NULL
, NULL
, 0, B_TRUE
) != 0)
2271 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2272 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2277 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2283 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2285 nvlist_t
*nv
, *value
;
2287 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2288 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2289 struct get_clones_arg gca
;
2292 * if this is a snapshot, then the kernel wasn't able
2293 * to get the clones. Do it by slowly iterating.
2295 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2297 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2299 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2304 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2306 gca
.origin
= zhp
->zfs_name
;
2308 if (gca
.numclones
!= 0) {
2310 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2313 /* get the pool name */
2314 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2315 (void) strsep(&cp
, "/@");
2316 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2317 ZFS_TYPE_FILESYSTEM
);
2319 (void) get_clones_cb(root
, &gca
);
2322 if (gca
.numclones
!= 0 ||
2323 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2324 nvlist_add_nvlist(zhp
->zfs_props
,
2325 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2332 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2333 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2336 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2342 * Accepts a property and value and checks that the value
2343 * matches the one found by the channel program. If they are
2344 * not equal, print both of them.
2347 zcp_check(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t intval
,
2350 if (!zhp
->zfs_hdl
->libzfs_prop_debug
)
2353 char *poolname
= zhp
->zpool_hdl
->zpool_name
;
2354 const char *program
=
2356 "ds = args['dataset']\n"
2357 "prop = args['property']\n"
2358 "value, setpoint = zfs.get_prop(ds, prop)\n"
2359 "return {value=value, setpoint=setpoint}\n";
2362 nvlist_t
*argnvl
= fnvlist_alloc();
2364 fnvlist_add_string(argnvl
, "dataset", zhp
->zfs_name
);
2365 fnvlist_add_string(argnvl
, "property", zfs_prop_to_name(prop
));
2367 error
= lzc_channel_program_nosync(poolname
, program
,
2368 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl
, &outnvl
);
2371 retnvl
= fnvlist_lookup_nvlist(outnvl
, "return");
2372 if (zfs_prop_get_type(prop
) == PROP_TYPE_NUMBER
) {
2374 error
= nvlist_lookup_int64(retnvl
, "value", &ans
);
2376 (void) fprintf(stderr
, "zcp check error: %u\n",
2380 if (ans
!= intval
) {
2381 (void) fprintf(stderr
,
2382 "%s: zfs found %lld, but zcp found %lld\n",
2383 zfs_prop_to_name(prop
),
2384 (longlong_t
)intval
, (longlong_t
)ans
);
2388 error
= nvlist_lookup_string(retnvl
, "value", &str_ans
);
2390 (void) fprintf(stderr
, "zcp check error: %u\n",
2394 if (strcmp(strval
, str_ans
) != 0) {
2395 (void) fprintf(stderr
,
2396 "%s: zfs found %s, but zcp found %s\n",
2397 zfs_prop_to_name(prop
),
2402 (void) fprintf(stderr
,
2403 "zcp check failed, channel program error: %u\n", error
);
2405 nvlist_free(argnvl
);
2406 nvlist_free(outnvl
);
2410 * Retrieve a property from the given object. If 'literal' is specified, then
2411 * numbers are left as exact values. Otherwise, numbers are converted to a
2412 * human-readable form.
2414 * Returns 0 on success, or -1 on error.
2417 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2418 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2420 char *source
= NULL
;
2424 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2427 * Check to see if this property applies to our object
2429 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
))
2432 if (received
&& zfs_prop_readonly(prop
))
2436 *src
= ZPROP_SRC_NONE
;
2439 case ZFS_PROP_CREATION
:
2441 * 'creation' is a time_t stored in the statistics. We convert
2442 * this into a string unless 'literal' is specified.
2445 val
= getprop_uint64(zhp
, prop
, &source
);
2446 time_t time
= (time_t)val
;
2450 localtime_r(&time
, &t
) == NULL
||
2451 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2453 (void) snprintf(propbuf
, proplen
, "%llu", val
);
2455 zcp_check(zhp
, prop
, val
, NULL
);
2458 case ZFS_PROP_MOUNTPOINT
:
2460 * Getting the precise mountpoint can be tricky.
2462 * - for 'none' or 'legacy', return those values.
2463 * - for inherited mountpoints, we want to take everything
2464 * after our ancestor and append it to the inherited value.
2466 * If the pool has an alternate root, we want to prepend that
2467 * root to any values we return.
2470 str
= getprop_string(zhp
, prop
, &source
);
2472 if (str
[0] == '/') {
2473 char buf
[MAXPATHLEN
];
2475 const char *relpath
;
2478 * If we inherit the mountpoint, even from a dataset
2479 * with a received value, the source will be the path of
2480 * the dataset we inherit from. If source is
2481 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2484 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2487 relpath
= zhp
->zfs_name
+ strlen(source
);
2488 if (relpath
[0] == '/')
2492 if ((zpool_get_prop(zhp
->zpool_hdl
,
2493 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2494 B_FALSE
)) || (strcmp(root
, "-") == 0))
2497 * Special case an alternate root of '/'. This will
2498 * avoid having multiple leading slashes in the
2501 if (strcmp(root
, "/") == 0)
2505 * If the mountpoint is '/' then skip over this
2506 * if we are obtaining either an alternate root or
2507 * an inherited mountpoint.
2509 if (str
[1] == '\0' && (root
[0] != '\0' ||
2510 relpath
[0] != '\0'))
2513 if (relpath
[0] == '\0')
2514 (void) snprintf(propbuf
, proplen
, "%s%s",
2517 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2518 root
, str
, relpath
[0] == '@' ? "" : "/",
2521 /* 'legacy' or 'none' */
2522 (void) strlcpy(propbuf
, str
, proplen
);
2524 zcp_check(zhp
, prop
, 0, propbuf
);
2527 case ZFS_PROP_ORIGIN
:
2528 str
= getprop_string(zhp
, prop
, &source
);
2531 (void) strlcpy(propbuf
, str
, proplen
);
2532 zcp_check(zhp
, prop
, 0, str
);
2535 case ZFS_PROP_CLONES
:
2536 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2540 case ZFS_PROP_QUOTA
:
2541 case ZFS_PROP_REFQUOTA
:
2542 case ZFS_PROP_RESERVATION
:
2543 case ZFS_PROP_REFRESERVATION
:
2545 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2548 * If quota or reservation is 0, we translate this into 'none'
2549 * (unless literal is set), and indicate that it's the default
2550 * value. Otherwise, we print the number nicely and indicate
2551 * that its set locally.
2555 (void) strlcpy(propbuf
, "0", proplen
);
2557 (void) strlcpy(propbuf
, "none", proplen
);
2560 (void) snprintf(propbuf
, proplen
, "%llu",
2563 zfs_nicenum(val
, propbuf
, proplen
);
2565 zcp_check(zhp
, prop
, val
, NULL
);
2568 case ZFS_PROP_FILESYSTEM_LIMIT
:
2569 case ZFS_PROP_SNAPSHOT_LIMIT
:
2570 case ZFS_PROP_FILESYSTEM_COUNT
:
2571 case ZFS_PROP_SNAPSHOT_COUNT
:
2573 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2577 * If limit is UINT64_MAX, we translate this into 'none' (unless
2578 * literal is set), and indicate that it's the default value.
2579 * Otherwise, we print the number nicely and indicate that it's
2583 (void) snprintf(propbuf
, proplen
, "%llu",
2585 } else if (val
== UINT64_MAX
) {
2586 (void) strlcpy(propbuf
, "none", proplen
);
2588 zfs_nicenum(val
, propbuf
, proplen
);
2591 zcp_check(zhp
, prop
, val
, NULL
);
2594 case ZFS_PROP_REFRATIO
:
2595 case ZFS_PROP_COMPRESSRATIO
:
2596 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2598 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2599 (u_longlong_t
)(val
/ 100),
2600 (u_longlong_t
)(val
% 100));
2601 zcp_check(zhp
, prop
, val
, NULL
);
2605 switch (zhp
->zfs_type
) {
2606 case ZFS_TYPE_FILESYSTEM
:
2609 case ZFS_TYPE_VOLUME
:
2612 case ZFS_TYPE_SNAPSHOT
:
2615 case ZFS_TYPE_BOOKMARK
:
2621 (void) snprintf(propbuf
, proplen
, "%s", str
);
2622 zcp_check(zhp
, prop
, 0, propbuf
);
2625 case ZFS_PROP_MOUNTED
:
2627 * The 'mounted' property is a pseudo-property that described
2628 * whether the filesystem is currently mounted. Even though
2629 * it's a boolean value, the typical values of "on" and "off"
2630 * don't make sense, so we translate to "yes" and "no".
2632 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2633 src
, &source
, &val
) != 0)
2636 (void) strlcpy(propbuf
, "yes", proplen
);
2638 (void) strlcpy(propbuf
, "no", proplen
);
2643 * The 'name' property is a pseudo-property derived from the
2644 * dataset name. It is presented as a real property to simplify
2647 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2648 zcp_check(zhp
, prop
, 0, propbuf
);
2653 * GUIDs are stored as numbers, but they are identifiers.
2654 * We don't want them to be pretty printed, because pretty
2655 * printing mangles the ID into a truncated and useless value.
2657 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2659 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2660 zcp_check(zhp
, prop
, val
, NULL
);
2664 switch (zfs_prop_get_type(prop
)) {
2665 case PROP_TYPE_NUMBER
:
2666 if (get_numeric_property(zhp
, prop
, src
,
2667 &source
, &val
) != 0) {
2672 (void) snprintf(propbuf
, proplen
, "%llu",
2675 zfs_nicenum(val
, propbuf
, proplen
);
2677 zcp_check(zhp
, prop
, val
, NULL
);
2680 case PROP_TYPE_STRING
:
2681 str
= getprop_string(zhp
, prop
, &source
);
2685 (void) strlcpy(propbuf
, str
, proplen
);
2686 zcp_check(zhp
, prop
, 0, str
);
2689 case PROP_TYPE_INDEX
:
2690 if (get_numeric_property(zhp
, prop
, src
,
2691 &source
, &val
) != 0)
2693 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2696 (void) strlcpy(propbuf
, strval
, proplen
);
2697 zcp_check(zhp
, prop
, 0, strval
);
2705 get_source(zhp
, src
, source
, statbuf
, statlen
);
2711 * Utility function to get the given numeric property. Does no validation that
2712 * the given property is the appropriate type; should only be used with
2713 * hard-coded property types.
2716 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2721 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2727 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2731 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2732 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2736 * Similar to zfs_prop_get(), but returns the value as an integer.
2739 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2740 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2745 * Check to see if this property applies to our object
2747 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
)) {
2748 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2749 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2750 zfs_prop_to_name(prop
)));
2754 *src
= ZPROP_SRC_NONE
;
2756 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2759 get_source(zhp
, src
, source
, statbuf
, statlen
);
2765 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2766 char **domainp
, idmap_rid_t
*ridp
)
2768 idmap_get_handle_t
*get_hdl
= NULL
;
2772 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2776 err
= idmap_get_sidbyuid(get_hdl
, id
,
2777 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2779 err
= idmap_get_sidbygid(get_hdl
, id
,
2780 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2782 if (err
== IDMAP_SUCCESS
&&
2783 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2784 status
== IDMAP_SUCCESS
)
2790 idmap_get_destroy(get_hdl
);
2795 * convert the propname into parameters needed by kernel
2796 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2797 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2800 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2801 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2803 zfs_userquota_prop_t type
;
2805 char *numericsid
= NULL
;
2810 /* Figure out the property type ({user|group}{quota|space}) */
2811 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2812 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2813 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2816 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2820 isuser
= (type
== ZFS_PROP_USERQUOTA
||
2821 type
== ZFS_PROP_USERUSED
);
2823 cp
= strchr(propname
, '@') + 1;
2825 if (strchr(cp
, '@')) {
2827 * It's a SID name (eg "user@domain") that needs to be
2828 * turned into S-1-domainID-RID.
2831 idmap_stat stat
, map_stat
;
2834 idmap_get_handle_t
*gh
= NULL
;
2836 stat
= idmap_get_create(&gh
);
2837 if (stat
!= IDMAP_SUCCESS
) {
2838 idmap_get_destroy(gh
);
2841 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2844 stat
= idmap_getuidbywinname(cp
, NULL
, flag
, &pid
);
2847 stat
= idmap_get_sidbyuid(gh
, pid
, flag
, &numericsid
,
2850 stat
= idmap_getgidbywinname(cp
, NULL
, flag
, &pid
);
2853 stat
= idmap_get_sidbygid(gh
, pid
, flag
, &numericsid
,
2857 idmap_get_destroy(gh
);
2860 stat
= idmap_get_mappings(gh
);
2861 idmap_get_destroy(gh
);
2866 if (numericsid
== NULL
)
2870 /* will be further decoded below */
2873 if (strncmp(cp
, "S-1-", 4) == 0) {
2874 /* It's a numeric SID (eg "S-1-234-567-89") */
2875 (void) strlcpy(domain
, cp
, domainlen
);
2878 cp
= strrchr(domain
, '-');
2881 *ridp
= strtoull(cp
, &end
, 10);
2889 if (errno
!= 0 || *end
!= '\0')
2891 } else if (!isdigit(*cp
)) {
2893 * It's a user/group name (eg "user") that needs to be
2894 * turned into a uid/gid
2896 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2912 /* It's a user/group ID (eg "12345"). */
2913 uid_t id
= strtoul(cp
, &end
, 10);
2920 /* It's an ephemeral ID. */
2921 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2922 &mapdomain
, &rid
) != 0)
2924 (void) strlcpy(domain
, mapdomain
, domainlen
);
2931 ASSERT3P(numericsid
, ==, NULL
);
2936 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2937 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2940 zfs_cmd_t zc
= { 0 };
2942 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2944 err
= userquota_propname_decode(propname
,
2945 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2946 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2947 zc
.zc_objset_type
= *typep
;
2951 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2955 *propvalue
= zc
.zc_cookie
;
2960 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2961 uint64_t *propvalue
)
2963 zfs_userquota_prop_t type
;
2965 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2970 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2971 char *propbuf
, int proplen
, boolean_t literal
)
2975 zfs_userquota_prop_t type
;
2977 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2984 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
2985 } else if (propvalue
== 0 &&
2986 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
)) {
2987 (void) strlcpy(propbuf
, "none", proplen
);
2989 zfs_nicenum(propvalue
, propbuf
, proplen
);
2995 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2996 uint64_t *propvalue
)
2999 zfs_cmd_t zc
= { 0 };
3000 const char *snapname
;
3002 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3004 snapname
= strchr(propname
, '@') + 1;
3005 if (strchr(snapname
, '@')) {
3006 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3008 /* snapname is the short name, append it to zhp's fsname */
3011 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3012 sizeof (zc
.zc_value
));
3013 cp
= strchr(zc
.zc_value
, '@');
3016 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
3017 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3020 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3024 *propvalue
= zc
.zc_cookie
;
3029 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3030 char *propbuf
, int proplen
, boolean_t literal
)
3035 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3041 (void) snprintf(propbuf
, proplen
, "%llu", propvalue
);
3043 zfs_nicenum(propvalue
, propbuf
, proplen
);
3049 * Returns the name of the given zfs handle.
3052 zfs_get_name(const zfs_handle_t
*zhp
)
3054 return (zhp
->zfs_name
);
3058 * Returns the name of the parent pool for the given zfs handle.
3061 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3063 return (zhp
->zpool_hdl
->zpool_name
);
3067 * Returns the type of the given zfs handle.
3070 zfs_get_type(const zfs_handle_t
*zhp
)
3072 return (zhp
->zfs_type
);
3076 * Is one dataset name a child dataset of another?
3078 * Needs to handle these cases:
3079 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3080 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3081 * Descendant? No. No. No. Yes.
3084 is_descendant(const char *ds1
, const char *ds2
)
3086 size_t d1len
= strlen(ds1
);
3088 /* ds2 can't be a descendant if it's smaller */
3089 if (strlen(ds2
) < d1len
)
3092 /* otherwise, compare strings and verify that there's a '/' char */
3093 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3097 * Given a complete name, return just the portion that refers to the parent.
3098 * Will return -1 if there is no parent (path is just the name of the
3102 parent_name(const char *path
, char *buf
, size_t buflen
)
3106 (void) strlcpy(buf
, path
, buflen
);
3108 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3116 * If accept_ancestor is false, then check to make sure that the given path has
3117 * a parent, and that it exists. If accept_ancestor is true, then find the
3118 * closest existing ancestor for the given path. In prefixlen return the
3119 * length of already existing prefix of the given path. We also fetch the
3120 * 'zoned' property, which is used to validate property settings when creating
3124 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3125 boolean_t accept_ancestor
, int *prefixlen
)
3127 zfs_cmd_t zc
= { 0 };
3128 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3134 (void) snprintf(errbuf
, sizeof (errbuf
),
3135 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3137 /* get parent, and check to see if this is just a pool */
3138 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3140 "missing dataset name"));
3141 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3144 /* check to see if the pool exists */
3145 if ((slash
= strchr(parent
, '/')) == NULL
)
3146 slash
= parent
+ strlen(parent
);
3147 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3148 zc
.zc_name
[slash
- parent
] = '\0';
3149 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3152 "no such pool '%s'"), zc
.zc_name
);
3153 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3156 /* check to see if the parent dataset exists */
3157 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3158 if (errno
== ENOENT
&& accept_ancestor
) {
3160 * Go deeper to find an ancestor, give up on top level.
3162 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3164 "no such pool '%s'"), zc
.zc_name
);
3165 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3167 } else if (errno
== ENOENT
) {
3168 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3169 "parent does not exist"));
3170 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3172 return (zfs_standard_error(hdl
, errno
, errbuf
));
3175 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3179 /* we are in a non-global zone, but parent is in the global zone */
3180 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3181 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3186 /* make sure parent is a filesystem */
3187 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3188 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3189 "parent is not a filesystem"));
3190 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3196 if (prefixlen
!= NULL
)
3197 *prefixlen
= strlen(parent
);
3202 * Finds whether the dataset of the given type(s) exists.
3205 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3209 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3213 * Try to get stats for the dataset, which will tell us if it exists.
3215 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3216 int ds_type
= zhp
->zfs_type
;
3219 if (types
& ds_type
)
3226 * Given a path to 'target', create all the ancestors between
3227 * the prefixlen portion of the path, and the target itself.
3228 * Fail if the initial prefixlen-ancestor does not already exist.
3231 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3237 /* make sure prefix exists */
3238 cp
= target
+ prefixlen
;
3240 assert(strchr(cp
, '/') == NULL
);
3241 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3244 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3252 * Attempt to create, mount, and share any ancestor filesystems,
3253 * up to the prefixlen-long one.
3255 for (cp
= target
+ prefixlen
+ 1;
3256 (cp
= strchr(cp
, '/')) != NULL
; *cp
= '/', cp
++) {
3260 h
= make_dataset_handle(hdl
, target
);
3262 /* it already exists, nothing to do here */
3267 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3269 opname
= dgettext(TEXT_DOMAIN
, "create");
3273 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3275 opname
= dgettext(TEXT_DOMAIN
, "open");
3279 if (zfs_mount(h
, NULL
, 0) != 0) {
3280 opname
= dgettext(TEXT_DOMAIN
, "mount");
3284 if (zfs_share(h
) != 0) {
3285 opname
= dgettext(TEXT_DOMAIN
, "share");
3295 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3296 "failed to %s ancestor '%s'"), opname
, target
);
3301 * Creates non-existing ancestors of the given path.
3304 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3310 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3313 if ((path_copy
= strdup(path
)) != NULL
) {
3314 rc
= create_parents(hdl
, path_copy
, prefix
);
3317 if (path_copy
== NULL
|| rc
!= 0)
3324 * Create a new filesystem or volume.
3327 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3332 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3335 enum lzc_dataset_type ost
;
3336 zpool_handle_t
*zpool_handle
;
3338 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3339 "cannot create '%s'"), path
);
3341 /* validate the path, taking care to note the extended error message */
3342 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3343 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3345 /* validate parents exist */
3346 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3350 * The failure modes when creating a dataset of a different type over
3351 * one that already exists is a little strange. In particular, if you
3352 * try to create a dataset on top of an existing dataset, the ioctl()
3353 * will return ENOENT, not EEXIST. To prevent this from happening, we
3354 * first try to see if the dataset exists.
3356 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3357 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3358 "dataset already exists"));
3359 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3362 if (type
== ZFS_TYPE_VOLUME
)
3363 ost
= LZC_DATSET_TYPE_ZVOL
;
3365 ost
= LZC_DATSET_TYPE_ZFS
;
3367 /* open zpool handle for prop validation */
3368 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3369 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3371 /* truncate pool_path at first slash */
3372 char *p
= strchr(pool_path
, '/');
3376 if ((zpool_handle
= zpool_open(hdl
, pool_path
)) == NULL
)
3379 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3380 zoned
, NULL
, zpool_handle
, errbuf
)) == 0) {
3381 zpool_close(zpool_handle
);
3384 zpool_close(zpool_handle
);
3386 if (type
== ZFS_TYPE_VOLUME
) {
3388 * If we are creating a volume, the size and block size must
3389 * satisfy a few restraints. First, the blocksize must be a
3390 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3391 * volsize must be a multiple of the block size, and cannot be
3394 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3395 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3397 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3398 "missing volume size"));
3399 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3402 if ((ret
= nvlist_lookup_uint64(props
,
3403 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3404 &blocksize
)) != 0) {
3405 if (ret
== ENOENT
) {
3406 blocksize
= zfs_prop_default_numeric(
3407 ZFS_PROP_VOLBLOCKSIZE
);
3410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3411 "missing volume block size"));
3412 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3418 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3419 "volume size cannot be zero"));
3420 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3423 if (size
% blocksize
!= 0) {
3425 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3426 "volume size must be a multiple of volume block "
3428 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3432 /* create the dataset */
3433 ret
= lzc_create(path
, ost
, props
);
3436 /* check for failure */
3438 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3439 (void) parent_name(path
, parent
, sizeof (parent
));
3443 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3444 "no such parent '%s'"), parent
);
3445 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3448 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3449 "parent '%s' is not a filesystem"), parent
);
3450 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3453 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3454 "pool must be upgraded to set this "
3455 "property or value"));
3456 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3458 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3459 "invalid property value(s) specified"));
3460 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3464 * This platform can't address a volume this big.
3466 if (type
== ZFS_TYPE_VOLUME
)
3467 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3472 return (zfs_standard_error(hdl
, errno
, errbuf
));
3480 * Destroys the given dataset. The caller must make sure that the filesystem
3481 * isn't mounted, and that there are no active dependents. If the file system
3482 * does not exist this function does nothing.
3485 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3487 zfs_cmd_t zc
= { 0 };
3489 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3490 nvlist_t
*nv
= fnvlist_alloc();
3491 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3492 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3495 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3496 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3502 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3504 if (ZFS_IS_VOLUME(zhp
)) {
3505 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3507 zc
.zc_objset_type
= DMU_OST_ZFS
;
3510 zc
.zc_defer_destroy
= defer
;
3511 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3513 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3514 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3518 remove_mountpoint(zhp
);
3523 struct destroydata
{
3525 const char *snapname
;
3529 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3531 struct destroydata
*dd
= arg
;
3532 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3535 (void) snprintf(name
, sizeof (name
),
3536 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3538 if (lzc_exists(name
))
3539 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3541 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3547 * Destroys all snapshots with the given name in zhp & descendants.
3550 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3553 struct destroydata dd
= { 0 };
3555 dd
.snapname
= snapname
;
3556 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3557 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3559 if (nvlist_empty(dd
.nvl
)) {
3560 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3561 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3562 zhp
->zfs_name
, snapname
);
3564 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3566 nvlist_free(dd
.nvl
);
3571 * Destroys all the snapshots named in the nvlist.
3574 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3577 nvlist_t
*errlist
= NULL
;
3579 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3582 nvlist_free(errlist
);
3586 if (nvlist_empty(errlist
)) {
3588 (void) snprintf(errbuf
, sizeof (errbuf
),
3589 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3591 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3593 for (nvpair_t
*pair
= nvlist_next_nvpair(errlist
, NULL
);
3594 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3596 (void) snprintf(errbuf
, sizeof (errbuf
),
3597 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3600 switch (fnvpair_value_int32(pair
)) {
3603 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3604 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3607 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3612 nvlist_free(errlist
);
3617 * Clones the given dataset. The target must be of the same type as the source.
3620 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3622 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3625 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3628 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3630 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3631 "cannot create '%s'"), target
);
3633 /* validate the target/clone name */
3634 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3635 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3637 /* validate parents exist */
3638 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3641 (void) parent_name(target
, parent
, sizeof (parent
));
3648 if (ZFS_IS_VOLUME(zhp
)) {
3649 type
= ZFS_TYPE_VOLUME
;
3651 type
= ZFS_TYPE_FILESYSTEM
;
3653 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3654 zhp
, zhp
->zpool_hdl
, errbuf
)) == NULL
)
3656 if (zfs_fix_auto_resv(zhp
, props
) == -1) {
3662 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3670 * The parent doesn't exist. We should have caught this
3671 * above, but there may a race condition that has since
3672 * destroyed the parent.
3674 * At this point, we don't know whether it's the source
3675 * that doesn't exist anymore, or whether the target
3676 * dataset doesn't exist.
3678 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3679 "no such parent '%s'"), parent
);
3680 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3683 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3684 "source and target pools differ"));
3685 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3689 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3698 * Promotes the given clone fs to be the clone parent.
3701 zfs_promote(zfs_handle_t
*zhp
)
3703 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3704 char snapname
[ZFS_MAX_DATASET_NAME_LEN
];
3708 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3709 "cannot promote '%s'"), zhp
->zfs_name
);
3711 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3712 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3713 "snapshots can not be promoted"));
3714 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3717 if (zhp
->zfs_dmustats
.dds_origin
[0] == '\0') {
3718 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3719 "not a cloned filesystem"));
3720 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3723 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
3724 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3726 ret
= lzc_promote(zhp
->zfs_name
, snapname
, sizeof (snapname
));
3731 /* There is a conflicting snapshot name. */
3732 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3733 "conflicting snapshot '%s' from parent '%s'"),
3734 snapname
, zhp
->zfs_dmustats
.dds_origin
);
3735 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3738 return (zfs_standard_error(hdl
, ret
, errbuf
));
3744 typedef struct snapdata
{
3746 const char *sd_snapname
;
3750 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3752 snapdata_t
*sd
= arg
;
3753 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3756 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3757 (void) snprintf(name
, sizeof (name
),
3758 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3760 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3762 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3770 zfs_remap_indirects(libzfs_handle_t
*hdl
, const char *fs
)
3775 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3776 "cannot remap filesystem '%s' "), fs
);
3778 err
= lzc_remap(fs
);
3781 (void) zfs_standard_error(hdl
, err
, errbuf
);
3788 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3792 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3799 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3800 "cannot create snapshots "));
3803 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3804 const char *snapname
= nvpair_name(elem
);
3806 /* validate the target name */
3807 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3809 (void) snprintf(errbuf
, sizeof (errbuf
),
3810 dgettext(TEXT_DOMAIN
,
3811 "cannot create snapshot '%s'"), snapname
);
3812 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3817 * get pool handle for prop validation. assumes all snaps are in the
3818 * same pool, as does lzc_snapshot (below).
3820 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
3821 elem
= nvlist_next_nvpair(snaps
, NULL
);
3822 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
3823 pool
[strcspn(pool
, "/@")] = '\0';
3824 zpool_handle_t
*zpool_hdl
= zpool_open(hdl
, pool
);
3826 if (props
!= NULL
&&
3827 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3828 props
, B_FALSE
, NULL
, zpool_hdl
, errbuf
)) == NULL
) {
3829 zpool_close(zpool_hdl
);
3832 zpool_close(zpool_hdl
);
3834 ret
= lzc_snapshot(snaps
, props
, &errors
);
3837 boolean_t printed
= B_FALSE
;
3838 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3840 elem
= nvlist_next_nvpair(errors
, elem
)) {
3841 (void) snprintf(errbuf
, sizeof (errbuf
),
3842 dgettext(TEXT_DOMAIN
,
3843 "cannot create snapshot '%s'"), nvpair_name(elem
));
3844 (void) zfs_standard_error(hdl
,
3845 fnvpair_value_int32(elem
), errbuf
);
3851 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3852 "multiple snapshots of same "
3854 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3858 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3864 nvlist_free(errors
);
3869 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3873 snapdata_t sd
= { 0 };
3874 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
3879 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3880 "cannot snapshot %s"), path
);
3882 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3883 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3885 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3886 cp
= strchr(fsname
, '@');
3888 sd
.sd_snapname
= cp
+ 1;
3890 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3891 ZFS_TYPE_VOLUME
)) == NULL
) {
3895 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3897 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3899 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3902 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3903 nvlist_free(sd
.sd_nvl
);
3909 * Destroy any more recent snapshots. We invoke this callback on any dependents
3910 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3911 * is a dependent and we should just destroy it without checking the transaction
3914 typedef struct rollback_data
{
3915 const char *cb_target
; /* the snapshot */
3916 uint64_t cb_create
; /* creation time reference */
3922 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3924 rollback_data_t
*cbp
= data
;
3925 prop_changelist_t
*clp
;
3927 /* We must destroy this clone; first unmount it */
3928 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3929 cbp
->cb_force
? MS_FORCE
: 0);
3930 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3931 cbp
->cb_error
= B_TRUE
;
3935 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3936 cbp
->cb_error
= B_TRUE
;
3938 changelist_remove(clp
, zhp
->zfs_name
);
3939 (void) changelist_postfix(clp
);
3940 changelist_free(clp
);
3947 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3949 rollback_data_t
*cbp
= data
;
3951 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3952 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3953 rollback_destroy_dependent
, cbp
);
3955 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3963 * Given a dataset, rollback to a specific snapshot, discarding any
3964 * data changes since then and making it the active dataset.
3966 * Any snapshots and bookmarks more recent than the target are
3967 * destroyed, along with their dependents (i.e. clones).
3970 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3972 rollback_data_t cb
= { 0 };
3974 boolean_t restore_resv
= 0;
3975 uint64_t old_volsize
= 0, new_volsize
;
3976 zfs_prop_t resv_prop
;
3978 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3979 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3982 * Destroy all recent snapshots and their dependents.
3984 cb
.cb_force
= force
;
3985 cb
.cb_target
= snap
->zfs_name
;
3986 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3987 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3988 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3994 * Now that we have verified that the snapshot is the latest,
3995 * rollback to the given snapshot.
3998 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3999 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
4001 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4003 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
4007 * Pass both the filesystem and the wanted snapshot names,
4008 * we would get an error back if the snapshot is destroyed or
4009 * a new snapshot is created before this request is processed.
4011 err
= lzc_rollback_to(zhp
->zfs_name
, snap
->zfs_name
);
4015 (void) snprintf(errbuf
, sizeof (errbuf
),
4016 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
4020 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4021 "there is a snapshot or bookmark more recent "
4022 "than '%s'"), snap
->zfs_name
);
4023 (void) zfs_error(zhp
->zfs_hdl
, EZFS_EXISTS
, errbuf
);
4026 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
4027 "'%s' is not found among snapshots of '%s'"),
4028 snap
->zfs_name
, zhp
->zfs_name
);
4029 (void) zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
);
4032 (void) zfs_error(zhp
->zfs_hdl
, EZFS_BADTYPE
, errbuf
);
4035 (void) zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
4041 * For volumes, if the pre-rollback volsize matched the pre-
4042 * rollback reservation and the volsize has changed then set
4043 * the reservation property to the post-rollback volsize.
4044 * Make a new handle since the rollback closed the dataset.
4046 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4047 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4049 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4050 if (old_volsize
!= new_volsize
)
4051 err
= zfs_prop_set_int(zhp
, resv_prop
,
4060 * Renames the given dataset.
4063 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4064 boolean_t force_unmount
)
4067 zfs_cmd_t zc
= { 0 };
4069 prop_changelist_t
*cl
= NULL
;
4070 zfs_handle_t
*zhrp
= NULL
;
4071 char *parentname
= NULL
;
4072 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4073 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4076 /* if we have the same exact name, just return success */
4077 if (strcmp(zhp
->zfs_name
, target
) == 0)
4080 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4081 "cannot rename to '%s'"), target
);
4083 /* make sure source name is valid */
4084 if (!zfs_validate_name(hdl
, zhp
->zfs_name
, zhp
->zfs_type
, B_TRUE
))
4085 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4088 * Make sure the target name is valid
4090 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4091 if ((strchr(target
, '@') == NULL
) ||
4094 * Snapshot target name is abbreviated,
4095 * reconstruct full dataset name
4097 (void) strlcpy(parent
, zhp
->zfs_name
,
4099 delim
= strchr(parent
, '@');
4100 if (strchr(target
, '@') == NULL
)
4104 (void) strlcat(parent
, target
, sizeof (parent
));
4108 * Make sure we're renaming within the same dataset.
4110 delim
= strchr(target
, '@');
4111 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4112 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4114 "snapshots must be part of same "
4116 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4120 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4121 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4124 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4125 "recursive rename must be a snapshot"));
4126 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4129 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4130 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4132 /* validate parents */
4133 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4136 /* make sure we're in the same pool */
4137 verify((delim
= strchr(target
, '/')) != NULL
);
4138 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4139 zhp
->zfs_name
[delim
- target
] != '/') {
4140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4141 "datasets must be within same pool"));
4142 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4145 /* new name cannot be a child of the current dataset name */
4146 if (is_descendant(zhp
->zfs_name
, target
)) {
4147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4148 "New dataset name cannot be a descendant of "
4149 "current dataset name"));
4150 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4154 (void) snprintf(errbuf
, sizeof (errbuf
),
4155 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4157 if (getzoneid() == GLOBAL_ZONEID
&&
4158 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4160 "dataset is used in a non-global zone"));
4161 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4165 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4166 if (parentname
== NULL
) {
4170 delim
= strchr(parentname
, '@');
4172 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4177 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4178 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4179 force_unmount
? MS_FORCE
: 0)) == NULL
)
4182 if (changelist_haszonedchild(cl
)) {
4183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4184 "child dataset with inherited mountpoint is used "
4185 "in a non-global zone"));
4186 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4191 if ((ret
= changelist_prefix(cl
)) != 0)
4195 if (ZFS_IS_VOLUME(zhp
))
4196 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4198 zc
.zc_objset_type
= DMU_OST_ZFS
;
4200 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4201 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4203 zc
.zc_cookie
= recursive
;
4205 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4207 * if it was recursive, the one that actually failed will
4210 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4211 "cannot rename '%s'"), zc
.zc_name
);
4213 if (recursive
&& errno
== EEXIST
) {
4214 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4215 "a child dataset already has a snapshot "
4216 "with the new name"));
4217 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4219 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4223 * On failure, we still want to remount any filesystems that
4224 * were previously mounted, so we don't alter the system state.
4227 (void) changelist_postfix(cl
);
4230 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4231 ret
= changelist_postfix(cl
);
4236 if (parentname
!= NULL
) {
4243 changelist_free(cl
);
4249 zfs_get_user_props(zfs_handle_t
*zhp
)
4251 return (zhp
->zfs_user_props
);
4255 zfs_get_recvd_props(zfs_handle_t
*zhp
)
4257 if (zhp
->zfs_recvd_props
== NULL
)
4258 if (get_recvd_props_ioctl(zhp
) != 0)
4260 return (zhp
->zfs_recvd_props
);
4264 * This function is used by 'zfs list' to determine the exact set of columns to
4265 * display, and their maximum widths. This does two main things:
4267 * - If this is a list of all properties, then expand the list to include
4268 * all native properties, and set a flag so that for each dataset we look
4269 * for new unique user properties and add them to the list.
4271 * - For non fixed-width properties, keep track of the maximum width seen
4272 * so that we can size the column appropriately. If the user has
4273 * requested received property values, we also need to compute the width
4274 * of the RECEIVED column.
4277 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4280 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4281 zprop_list_t
*entry
;
4282 zprop_list_t
**last
, **start
;
4283 nvlist_t
*userprops
, *propval
;
4286 char buf
[ZFS_MAXPROPLEN
];
4288 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4291 userprops
= zfs_get_user_props(zhp
);
4294 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4296 * Go through and add any user properties as necessary. We
4297 * start by incrementing our list pointer to the first
4298 * non-native property.
4301 while (*start
!= NULL
) {
4302 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4304 start
= &(*start
)->pl_next
;
4308 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4310 * See if we've already found this property in our list.
4312 for (last
= start
; *last
!= NULL
;
4313 last
= &(*last
)->pl_next
) {
4314 if (strcmp((*last
)->pl_user_prop
,
4315 nvpair_name(elem
)) == 0)
4319 if (*last
== NULL
) {
4320 if ((entry
= zfs_alloc(hdl
,
4321 sizeof (zprop_list_t
))) == NULL
||
4322 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4323 nvpair_name(elem
)))) == NULL
) {
4328 entry
->pl_prop
= ZPROP_INVAL
;
4329 entry
->pl_width
= strlen(nvpair_name(elem
));
4330 entry
->pl_all
= B_TRUE
;
4337 * Now go through and check the width of any non-fixed columns
4339 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4340 if (entry
->pl_fixed
&& !literal
)
4343 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4344 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4345 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4346 if (strlen(buf
) > entry
->pl_width
)
4347 entry
->pl_width
= strlen(buf
);
4349 if (received
&& zfs_prop_get_recvd(zhp
,
4350 zfs_prop_to_name(entry
->pl_prop
),
4351 buf
, sizeof (buf
), literal
) == 0)
4352 if (strlen(buf
) > entry
->pl_recvd_width
)
4353 entry
->pl_recvd_width
= strlen(buf
);
4355 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4357 verify(nvlist_lookup_string(propval
,
4358 ZPROP_VALUE
, &strval
) == 0);
4359 if (strlen(strval
) > entry
->pl_width
)
4360 entry
->pl_width
= strlen(strval
);
4362 if (received
&& zfs_prop_get_recvd(zhp
,
4363 entry
->pl_user_prop
,
4364 buf
, sizeof (buf
), literal
) == 0)
4365 if (strlen(buf
) > entry
->pl_recvd_width
)
4366 entry
->pl_recvd_width
= strlen(buf
);
4374 zfs_deleg_share_nfs(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4375 char *resource
, void *export
, void *sharetab
,
4376 int sharemax
, zfs_share_op_t operation
)
4378 zfs_cmd_t zc
= { 0 };
4381 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4382 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4384 (void) strlcpy(zc
.zc_string
, resource
, sizeof (zc
.zc_string
));
4385 zc
.zc_share
.z_sharedata
= (uint64_t)(uintptr_t)sharetab
;
4386 zc
.zc_share
.z_exportdata
= (uint64_t)(uintptr_t)export
;
4387 zc
.zc_share
.z_sharetype
= operation
;
4388 zc
.zc_share
.z_sharemax
= sharemax
;
4389 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SHARE
, &zc
);
4394 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4399 * Keep a reference to the props-table against which we prune the
4402 zhp
->zfs_props_table
= props
;
4404 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4407 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4408 nvpair_t
*next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4411 * User properties will result in ZPROP_INVAL, and since we
4412 * only know how to prune standard ZFS properties, we always
4413 * leave these in the list. This can also happen if we
4414 * encounter an unknown DSL property (when running older
4415 * software, for example).
4417 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4418 (void) nvlist_remove(zhp
->zfs_props
,
4419 nvpair_name(curr
), nvpair_type(curr
));
4425 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4426 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4428 zfs_cmd_t zc
= { 0 };
4429 nvlist_t
*nvlist
= NULL
;
4432 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4433 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4434 zc
.zc_cookie
= (uint64_t)cmd
;
4436 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4437 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4438 (void) no_memory(hdl
);
4444 case ZFS_SMB_ACL_ADD
:
4445 case ZFS_SMB_ACL_REMOVE
:
4446 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4448 case ZFS_SMB_ACL_RENAME
:
4449 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4451 (void) no_memory(hdl
);
4454 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4456 (void) no_memory(hdl
);
4459 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4460 nvlist_free(nvlist
);
4464 case ZFS_SMB_ACL_PURGE
:
4469 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4470 nvlist_free(nvlist
);
4475 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4476 char *path
, char *resource
)
4478 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4483 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4484 char *path
, char *resource
)
4486 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4491 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4493 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4498 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4499 char *oldname
, char *newname
)
4501 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4506 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4507 zfs_userspace_cb_t func
, void *arg
)
4509 zfs_cmd_t zc
= { 0 };
4510 zfs_useracct_t buf
[100];
4511 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4514 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4516 zc
.zc_objset_type
= type
;
4517 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4520 zfs_useracct_t
*zua
= buf
;
4522 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4523 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4526 (void) snprintf(errbuf
, sizeof (errbuf
),
4527 dgettext(TEXT_DOMAIN
,
4528 "cannot get used/quota for %s"), zc
.zc_name
);
4529 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4531 if (zc
.zc_nvlist_dst_size
== 0)
4534 while (zc
.zc_nvlist_dst_size
> 0) {
4535 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4536 zua
->zu_space
)) != 0)
4539 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4548 const char *snapname
;
4550 boolean_t recursive
;
4555 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4557 struct holdarg
*ha
= arg
;
4558 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4561 (void) snprintf(name
, sizeof (name
),
4562 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4564 if (lzc_exists(name
))
4565 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4568 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4574 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4575 boolean_t recursive
, int cleanup_fd
)
4580 ha
.nvl
= fnvlist_alloc();
4581 ha
.snapname
= snapname
;
4583 ha
.recursive
= recursive
;
4584 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4586 if (nvlist_empty(ha
.nvl
)) {
4589 fnvlist_free(ha
.nvl
);
4591 (void) snprintf(errbuf
, sizeof (errbuf
),
4592 dgettext(TEXT_DOMAIN
,
4593 "cannot hold snapshot '%s@%s'"),
4594 zhp
->zfs_name
, snapname
);
4595 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4599 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4600 fnvlist_free(ha
.nvl
);
4606 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4610 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4615 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4618 /* There may be errors even in the success case. */
4619 fnvlist_free(errors
);
4623 if (nvlist_empty(errors
)) {
4624 /* no hold-specific errors */
4625 (void) snprintf(errbuf
, sizeof (errbuf
),
4626 dgettext(TEXT_DOMAIN
, "cannot hold"));
4629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4630 "pool must be upgraded"));
4631 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4634 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4637 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4641 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4643 elem
= nvlist_next_nvpair(errors
, elem
)) {
4644 (void) snprintf(errbuf
, sizeof (errbuf
),
4645 dgettext(TEXT_DOMAIN
,
4646 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4647 switch (fnvpair_value_int32(elem
)) {
4650 * Temporary tags wind up having the ds object id
4651 * prepended. So even if we passed the length check
4652 * above, it's still possible for the tag to wind
4653 * up being slightly too long.
4655 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4658 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4661 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4664 (void) zfs_standard_error(hdl
,
4665 fnvpair_value_int32(elem
), errbuf
);
4669 fnvlist_free(errors
);
4674 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4676 struct holdarg
*ha
= arg
;
4677 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4679 nvlist_t
*existing_holds
;
4681 (void) snprintf(name
, sizeof (name
),
4682 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4684 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4686 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4689 nvlist_t
*torelease
= fnvlist_alloc();
4690 fnvlist_add_boolean(torelease
, ha
->tag
);
4691 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4692 fnvlist_free(torelease
);
4696 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4702 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4703 boolean_t recursive
)
4707 nvlist_t
*errors
= NULL
;
4709 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4712 ha
.nvl
= fnvlist_alloc();
4713 ha
.snapname
= snapname
;
4715 ha
.recursive
= recursive
;
4717 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4719 if (nvlist_empty(ha
.nvl
)) {
4720 fnvlist_free(ha
.nvl
);
4722 (void) snprintf(errbuf
, sizeof (errbuf
),
4723 dgettext(TEXT_DOMAIN
,
4724 "cannot release hold from snapshot '%s@%s'"),
4725 zhp
->zfs_name
, snapname
);
4727 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4729 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4734 ret
= lzc_release(ha
.nvl
, &errors
);
4735 fnvlist_free(ha
.nvl
);
4738 /* There may be errors even in the success case. */
4739 fnvlist_free(errors
);
4743 if (nvlist_empty(errors
)) {
4744 /* no hold-specific errors */
4745 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4749 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4750 "pool must be upgraded"));
4751 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4754 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4758 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4760 elem
= nvlist_next_nvpair(errors
, elem
)) {
4761 (void) snprintf(errbuf
, sizeof (errbuf
),
4762 dgettext(TEXT_DOMAIN
,
4763 "cannot release hold from snapshot '%s'"),
4765 switch (fnvpair_value_int32(elem
)) {
4767 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4770 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4773 (void) zfs_standard_error_fmt(hdl
,
4774 fnvpair_value_int32(elem
), errbuf
);
4778 fnvlist_free(errors
);
4783 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4785 zfs_cmd_t zc
= { 0 };
4786 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4792 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4793 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4797 nvbuf
= malloc(nvsz
);
4798 if (nvbuf
== NULL
) {
4799 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4803 zc
.zc_nvlist_dst_size
= nvsz
;
4804 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4806 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4808 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4809 (void) snprintf(errbuf
, sizeof (errbuf
),
4810 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4815 nvsz
= zc
.zc_nvlist_dst_size
;
4819 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4820 "pool must be upgraded"));
4821 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4824 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4827 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4830 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4835 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4837 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4838 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4840 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4850 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4852 zfs_cmd_t zc
= { 0 };
4853 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4859 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4860 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4862 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4865 nvbuf
= malloc(nvsz
);
4867 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4870 zc
.zc_nvlist_src_size
= nvsz
;
4871 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4872 zc
.zc_perm_action
= un
;
4874 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4876 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4877 (void) snprintf(errbuf
, sizeof (errbuf
),
4878 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4882 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4883 "pool must be upgraded"));
4884 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4887 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4890 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4893 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4904 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4909 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4912 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4914 (void) snprintf(errbuf
, sizeof (errbuf
),
4915 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4919 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4920 "pool must be upgraded"));
4921 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4924 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4927 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4930 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4939 * Convert the zvol's volume size to an appropriate reservation.
4940 * Note: If this routine is updated, it is necessary to update the ZFS test
4941 * suite's shell version in reservation.kshlib.
4944 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4947 uint64_t nblocks
, volblocksize
;
4951 if (nvlist_lookup_string(props
,
4952 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4953 ncopies
= atoi(strval
);
4956 if (nvlist_lookup_uint64(props
,
4957 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4958 &volblocksize
) != 0)
4959 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4960 nblocks
= volsize
/volblocksize
;
4961 /* start with metadnode L0-L6 */
4963 /* calculate number of indirects */
4964 while (nblocks
> 1) {
4965 nblocks
+= DNODES_PER_LEVEL
- 1;
4966 nblocks
/= DNODES_PER_LEVEL
;
4969 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4972 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4973 * compressed, but in practice they compress down to about
4976 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;