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) 2013, 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) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2016 Nexenta Systems, Inc.
44 #include <sys/mntent.h>
45 #include <sys/mount.h>
53 #include <directory.h>
54 #endif /* HAVE_IDMAP */
56 #include <sys/dnode.h>
61 #include "zfs_namecheck.h"
63 #include "libzfs_impl.h"
64 #include "zfs_deleg.h"
66 static int userquota_propname_decode(const char *propname
, boolean_t zoned
,
67 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
);
70 * Given a single type (not a mask of types), return the type in a human
74 zfs_type_to_name(zfs_type_t type
)
77 case ZFS_TYPE_FILESYSTEM
:
78 return (dgettext(TEXT_DOMAIN
, "filesystem"));
79 case ZFS_TYPE_SNAPSHOT
:
80 return (dgettext(TEXT_DOMAIN
, "snapshot"));
82 return (dgettext(TEXT_DOMAIN
, "volume"));
91 * Validate a ZFS path. This is used even before trying to open the dataset, to
92 * provide a more meaningful error message. We call zfs_error_aux() to
93 * explain exactly why the name was not valid.
96 zfs_validate_name(libzfs_handle_t
*hdl
, const char *path
, int type
,
102 (void) zfs_prop_get_table();
103 if (entity_namecheck(path
, &why
, &what
) != 0) {
106 case NAME_ERR_TOOLONG
:
107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
108 "name is too long"));
111 case NAME_ERR_LEADING_SLASH
:
112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
113 "leading slash in name"));
116 case NAME_ERR_EMPTY_COMPONENT
:
117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
118 "empty component in name"));
121 case NAME_ERR_TRAILING_SLASH
:
122 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
123 "trailing slash in name"));
126 case NAME_ERR_INVALCHAR
:
128 dgettext(TEXT_DOMAIN
, "invalid character "
129 "'%c' in name"), what
);
132 case NAME_ERR_MULTIPLE_DELIMITERS
:
133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
134 "multiple '@' and/or '#' delimiters in "
138 case NAME_ERR_NOLETTER
:
139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
140 "pool doesn't begin with a letter"));
143 case NAME_ERR_RESERVED
:
144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
145 "name is reserved"));
148 case NAME_ERR_DISKLIKE
:
149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
150 "reserved disk name"));
160 if (!(type
& ZFS_TYPE_SNAPSHOT
) && strchr(path
, '@') != NULL
) {
162 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
163 "snapshot delimiter '@' is not expected here"));
167 if (type
== ZFS_TYPE_SNAPSHOT
&& strchr(path
, '@') == NULL
) {
169 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
170 "missing '@' delimiter in snapshot name"));
174 if (!(type
& ZFS_TYPE_BOOKMARK
) && strchr(path
, '#') != NULL
) {
176 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
177 "bookmark delimiter '#' is not expected here"));
181 if (type
== ZFS_TYPE_BOOKMARK
&& strchr(path
, '#') == NULL
) {
183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
184 "missing '#' delimiter in bookmark name"));
188 if (modifying
&& strchr(path
, '%') != NULL
) {
190 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
191 "invalid character %c in name"), '%');
199 zfs_name_valid(const char *name
, zfs_type_t type
)
201 if (type
== ZFS_TYPE_POOL
)
202 return (zpool_name_valid(NULL
, B_FALSE
, name
));
203 return (zfs_validate_name(NULL
, name
, type
, B_FALSE
));
207 * This function takes the raw DSL properties, and filters out the user-defined
208 * properties into a separate nvlist.
211 process_user_props(zfs_handle_t
*zhp
, nvlist_t
*props
)
213 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
218 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0) {
219 (void) no_memory(hdl
);
224 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
225 if (!zfs_prop_user(nvpair_name(elem
)))
228 verify(nvpair_value_nvlist(elem
, &propval
) == 0);
229 if (nvlist_add_nvlist(nvl
, nvpair_name(elem
), propval
) != 0) {
231 (void) no_memory(hdl
);
239 static zpool_handle_t
*
240 zpool_add_handle(zfs_handle_t
*zhp
, const char *pool_name
)
242 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
245 if ((zph
= zpool_open_canfail(hdl
, pool_name
)) != NULL
) {
246 if (hdl
->libzfs_pool_handles
!= NULL
)
247 zph
->zpool_next
= hdl
->libzfs_pool_handles
;
248 hdl
->libzfs_pool_handles
= zph
;
253 static zpool_handle_t
*
254 zpool_find_handle(zfs_handle_t
*zhp
, const char *pool_name
, int len
)
256 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
257 zpool_handle_t
*zph
= hdl
->libzfs_pool_handles
;
259 while ((zph
!= NULL
) &&
260 (strncmp(pool_name
, zpool_get_name(zph
), len
) != 0))
261 zph
= zph
->zpool_next
;
266 * Returns a handle to the pool that contains the provided dataset.
267 * If a handle to that pool already exists then that handle is returned.
268 * Otherwise, a new handle is created and added to the list of handles.
270 static zpool_handle_t
*
271 zpool_handle(zfs_handle_t
*zhp
)
277 len
= strcspn(zhp
->zfs_name
, "/@#") + 1;
278 pool_name
= zfs_alloc(zhp
->zfs_hdl
, len
);
279 (void) strlcpy(pool_name
, zhp
->zfs_name
, len
);
281 zph
= zpool_find_handle(zhp
, pool_name
, len
);
283 zph
= zpool_add_handle(zhp
, pool_name
);
290 zpool_free_handles(libzfs_handle_t
*hdl
)
292 zpool_handle_t
*next
, *zph
= hdl
->libzfs_pool_handles
;
294 while (zph
!= NULL
) {
295 next
= zph
->zpool_next
;
299 hdl
->libzfs_pool_handles
= NULL
;
303 * Utility function to gather stats (objset and zpl) for the given object.
306 get_stats_ioctl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
308 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
310 (void) strlcpy(zc
->zc_name
, zhp
->zfs_name
, sizeof (zc
->zc_name
));
312 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, zc
) != 0) {
313 if (errno
== ENOMEM
) {
314 if (zcmd_expand_dst_nvlist(hdl
, zc
) != 0) {
325 * Utility function to get the received properties of the given object.
328 get_recvd_props_ioctl(zfs_handle_t
*zhp
)
330 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
331 nvlist_t
*recvdprops
;
332 zfs_cmd_t zc
= {"\0"};
335 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
338 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
340 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_RECVD_PROPS
, &zc
) != 0) {
341 if (errno
== ENOMEM
) {
342 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
346 zcmd_free_nvlists(&zc
);
351 err
= zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &recvdprops
);
352 zcmd_free_nvlists(&zc
);
356 nvlist_free(zhp
->zfs_recvd_props
);
357 zhp
->zfs_recvd_props
= recvdprops
;
363 put_stats_zhdl(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
365 nvlist_t
*allprops
, *userprops
;
367 zhp
->zfs_dmustats
= zc
->zc_objset_stats
; /* structure assignment */
369 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, zc
, &allprops
) != 0) {
374 * XXX Why do we store the user props separately, in addition to
375 * storing them in zfs_props?
377 if ((userprops
= process_user_props(zhp
, allprops
)) == NULL
) {
378 nvlist_free(allprops
);
382 nvlist_free(zhp
->zfs_props
);
383 nvlist_free(zhp
->zfs_user_props
);
385 zhp
->zfs_props
= allprops
;
386 zhp
->zfs_user_props
= userprops
;
392 get_stats(zfs_handle_t
*zhp
)
395 zfs_cmd_t zc
= {"\0"};
397 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
399 if (get_stats_ioctl(zhp
, &zc
) != 0)
401 else if (put_stats_zhdl(zhp
, &zc
) != 0)
403 zcmd_free_nvlists(&zc
);
408 * Refresh the properties currently stored in the handle.
411 zfs_refresh_properties(zfs_handle_t
*zhp
)
413 (void) get_stats(zhp
);
417 * Makes a handle from the given dataset name. Used by zfs_open() and
418 * zfs_iter_* to create child handles on the fly.
421 make_dataset_handle_common(zfs_handle_t
*zhp
, zfs_cmd_t
*zc
)
423 if (put_stats_zhdl(zhp
, zc
) != 0)
427 * We've managed to open the dataset and gather statistics. Determine
428 * the high-level type.
430 if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
431 zhp
->zfs_head_type
= ZFS_TYPE_VOLUME
;
432 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
433 zhp
->zfs_head_type
= ZFS_TYPE_FILESYSTEM
;
434 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_OTHER
)
439 if (zhp
->zfs_dmustats
.dds_is_snapshot
)
440 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
441 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZVOL
)
442 zhp
->zfs_type
= ZFS_TYPE_VOLUME
;
443 else if (zhp
->zfs_dmustats
.dds_type
== DMU_OST_ZFS
)
444 zhp
->zfs_type
= ZFS_TYPE_FILESYSTEM
;
446 abort(); /* we should never see any other types */
448 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
)
455 make_dataset_handle(libzfs_handle_t
*hdl
, const char *path
)
457 zfs_cmd_t zc
= {"\0"};
459 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
465 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
466 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0) {
470 if (get_stats_ioctl(zhp
, &zc
) == -1) {
471 zcmd_free_nvlists(&zc
);
475 if (make_dataset_handle_common(zhp
, &zc
) == -1) {
479 zcmd_free_nvlists(&zc
);
484 make_dataset_handle_zc(libzfs_handle_t
*hdl
, zfs_cmd_t
*zc
)
486 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
492 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
493 if (make_dataset_handle_common(zhp
, zc
) == -1) {
501 make_dataset_simple_handle_zc(zfs_handle_t
*pzhp
, zfs_cmd_t
*zc
)
503 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
508 zhp
->zfs_hdl
= pzhp
->zfs_hdl
;
509 (void) strlcpy(zhp
->zfs_name
, zc
->zc_name
, sizeof (zhp
->zfs_name
));
510 zhp
->zfs_head_type
= pzhp
->zfs_type
;
511 zhp
->zfs_type
= ZFS_TYPE_SNAPSHOT
;
512 zhp
->zpool_hdl
= zpool_handle(zhp
);
518 zfs_handle_dup(zfs_handle_t
*zhp_orig
)
520 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
525 zhp
->zfs_hdl
= zhp_orig
->zfs_hdl
;
526 zhp
->zpool_hdl
= zhp_orig
->zpool_hdl
;
527 (void) strlcpy(zhp
->zfs_name
, zhp_orig
->zfs_name
,
528 sizeof (zhp
->zfs_name
));
529 zhp
->zfs_type
= zhp_orig
->zfs_type
;
530 zhp
->zfs_head_type
= zhp_orig
->zfs_head_type
;
531 zhp
->zfs_dmustats
= zhp_orig
->zfs_dmustats
;
532 if (zhp_orig
->zfs_props
!= NULL
) {
533 if (nvlist_dup(zhp_orig
->zfs_props
, &zhp
->zfs_props
, 0) != 0) {
534 (void) no_memory(zhp
->zfs_hdl
);
539 if (zhp_orig
->zfs_user_props
!= NULL
) {
540 if (nvlist_dup(zhp_orig
->zfs_user_props
,
541 &zhp
->zfs_user_props
, 0) != 0) {
542 (void) no_memory(zhp
->zfs_hdl
);
547 if (zhp_orig
->zfs_recvd_props
!= NULL
) {
548 if (nvlist_dup(zhp_orig
->zfs_recvd_props
,
549 &zhp
->zfs_recvd_props
, 0)) {
550 (void) no_memory(zhp
->zfs_hdl
);
555 zhp
->zfs_mntcheck
= zhp_orig
->zfs_mntcheck
;
556 if (zhp_orig
->zfs_mntopts
!= NULL
) {
557 zhp
->zfs_mntopts
= zfs_strdup(zhp_orig
->zfs_hdl
,
558 zhp_orig
->zfs_mntopts
);
560 zhp
->zfs_props_table
= zhp_orig
->zfs_props_table
;
565 zfs_bookmark_exists(const char *path
)
569 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
576 (void) strlcpy(fsname
, path
, sizeof (fsname
));
577 pound
= strchr(fsname
, '#');
582 bmark_name
= pound
+ 1;
583 props
= fnvlist_alloc();
584 err
= lzc_get_bookmarks(fsname
, props
, &bmarks
);
591 rv
= nvlist_exists(bmarks
, bmark_name
);
597 make_bookmark_handle(zfs_handle_t
*parent
, const char *path
,
598 nvlist_t
*bmark_props
)
600 zfs_handle_t
*zhp
= calloc(sizeof (zfs_handle_t
), 1);
605 /* Fill in the name. */
606 zhp
->zfs_hdl
= parent
->zfs_hdl
;
607 (void) strlcpy(zhp
->zfs_name
, path
, sizeof (zhp
->zfs_name
));
609 /* Set the property lists. */
610 if (nvlist_dup(bmark_props
, &zhp
->zfs_props
, 0) != 0) {
616 zhp
->zfs_head_type
= parent
->zfs_head_type
;
617 zhp
->zfs_type
= ZFS_TYPE_BOOKMARK
;
619 if ((zhp
->zpool_hdl
= zpool_handle(zhp
)) == NULL
) {
620 nvlist_free(zhp
->zfs_props
);
628 struct zfs_open_bookmarks_cb_data
{
634 zfs_open_bookmarks_cb(zfs_handle_t
*zhp
, void *data
)
636 struct zfs_open_bookmarks_cb_data
*dp
= data
;
639 * Is it the one we are looking for?
641 if (strcmp(dp
->path
, zfs_get_name(zhp
)) == 0) {
643 * We found it. Save it and let the caller know we are done.
650 * Not found. Close the handle and ask for another one.
657 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
658 * argument is a mask of acceptable types. The function will print an
659 * appropriate error message and return NULL if it can't be opened.
662 zfs_open(libzfs_handle_t
*hdl
, const char *path
, int types
)
668 (void) snprintf(errbuf
, sizeof (errbuf
),
669 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), path
);
672 * Validate the name before we even try to open it.
674 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
)) {
675 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
680 * Bookmarks needs to be handled separately.
682 bookp
= strchr(path
, '#');
685 * Try to get stats for the dataset, which will tell us if it
689 if ((zhp
= make_dataset_handle(hdl
, path
)) == NULL
) {
690 (void) zfs_standard_error(hdl
, errno
, errbuf
);
694 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
696 struct zfs_open_bookmarks_cb_data cb_data
= {path
, NULL
};
699 * We need to cut out '#' and everything after '#'
700 * to get the parent dataset name only.
702 assert(bookp
- path
< sizeof (dsname
));
703 (void) strncpy(dsname
, path
, bookp
- path
);
704 dsname
[bookp
- path
] = '\0';
707 * Create handle for the parent dataset.
710 if ((pzhp
= make_dataset_handle(hdl
, dsname
)) == NULL
) {
711 (void) zfs_standard_error(hdl
, errno
, errbuf
);
716 * Iterate bookmarks to find the right one.
719 if ((zfs_iter_bookmarks(pzhp
, zfs_open_bookmarks_cb
,
720 &cb_data
) == 0) && (cb_data
.zhp
== NULL
)) {
721 (void) zfs_error(hdl
, EZFS_NOENT
, errbuf
);
725 if (cb_data
.zhp
== NULL
) {
726 (void) zfs_standard_error(hdl
, errno
, errbuf
);
738 if (!(types
& zhp
->zfs_type
)) {
739 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
748 * Release a ZFS handle. Nothing to do but free the associated memory.
751 zfs_close(zfs_handle_t
*zhp
)
753 if (zhp
->zfs_mntopts
)
754 free(zhp
->zfs_mntopts
);
755 nvlist_free(zhp
->zfs_props
);
756 nvlist_free(zhp
->zfs_user_props
);
757 nvlist_free(zhp
->zfs_recvd_props
);
761 typedef struct mnttab_node
{
762 struct mnttab mtn_mt
;
767 libzfs_mnttab_cache_compare(const void *arg1
, const void *arg2
)
769 const mnttab_node_t
*mtn1
= (const mnttab_node_t
*)arg1
;
770 const mnttab_node_t
*mtn2
= (const mnttab_node_t
*)arg2
;
773 rv
= strcmp(mtn1
->mtn_mt
.mnt_special
, mtn2
->mtn_mt
.mnt_special
);
775 return (AVL_ISIGN(rv
));
779 libzfs_mnttab_init(libzfs_handle_t
*hdl
)
781 assert(avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0);
782 avl_create(&hdl
->libzfs_mnttab_cache
, libzfs_mnttab_cache_compare
,
783 sizeof (mnttab_node_t
), offsetof(mnttab_node_t
, mtn_node
));
787 libzfs_mnttab_update(libzfs_handle_t
*hdl
)
791 /* Reopen MNTTAB to prevent reading stale data from open file */
792 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
795 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
799 if (strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
802 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
803 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, entry
.mnt_special
);
804 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, entry
.mnt_mountp
);
805 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, entry
.mnt_fstype
);
806 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, entry
.mnt_mntopts
);
808 /* Exclude duplicate mounts */
809 if (avl_find(&hdl
->libzfs_mnttab_cache
, mtn
, &where
) != NULL
) {
810 free(mtn
->mtn_mt
.mnt_special
);
811 free(mtn
->mtn_mt
.mnt_mountp
);
812 free(mtn
->mtn_mt
.mnt_fstype
);
813 free(mtn
->mtn_mt
.mnt_mntopts
);
818 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
825 libzfs_mnttab_fini(libzfs_handle_t
*hdl
)
830 while ((mtn
= avl_destroy_nodes(&hdl
->libzfs_mnttab_cache
, &cookie
))) {
831 free(mtn
->mtn_mt
.mnt_special
);
832 free(mtn
->mtn_mt
.mnt_mountp
);
833 free(mtn
->mtn_mt
.mnt_fstype
);
834 free(mtn
->mtn_mt
.mnt_mntopts
);
837 avl_destroy(&hdl
->libzfs_mnttab_cache
);
841 libzfs_mnttab_cache(libzfs_handle_t
*hdl
, boolean_t enable
)
843 hdl
->libzfs_mnttab_enable
= enable
;
847 libzfs_mnttab_find(libzfs_handle_t
*hdl
, const char *fsname
,
848 struct mnttab
*entry
)
854 if (!hdl
->libzfs_mnttab_enable
) {
855 struct mnttab srch
= { 0 };
857 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
))
858 libzfs_mnttab_fini(hdl
);
860 /* Reopen MNTTAB to prevent reading stale data from open file */
861 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
864 srch
.mnt_special
= (char *)fsname
;
865 srch
.mnt_fstype
= MNTTYPE_ZFS
;
866 if (getmntany(hdl
->libzfs_mnttab
, entry
, &srch
) == 0)
872 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
873 if ((error
= libzfs_mnttab_update(hdl
)) != 0)
876 find
.mtn_mt
.mnt_special
= (char *)fsname
;
877 mtn
= avl_find(&hdl
->libzfs_mnttab_cache
, &find
, NULL
);
879 *entry
= mtn
->mtn_mt
;
886 libzfs_mnttab_add(libzfs_handle_t
*hdl
, const char *special
,
887 const char *mountp
, const char *mntopts
)
891 if (avl_numnodes(&hdl
->libzfs_mnttab_cache
) == 0)
893 mtn
= zfs_alloc(hdl
, sizeof (mnttab_node_t
));
894 mtn
->mtn_mt
.mnt_special
= zfs_strdup(hdl
, special
);
895 mtn
->mtn_mt
.mnt_mountp
= zfs_strdup(hdl
, mountp
);
896 mtn
->mtn_mt
.mnt_fstype
= zfs_strdup(hdl
, MNTTYPE_ZFS
);
897 mtn
->mtn_mt
.mnt_mntopts
= zfs_strdup(hdl
, mntopts
);
898 avl_add(&hdl
->libzfs_mnttab_cache
, mtn
);
902 libzfs_mnttab_remove(libzfs_handle_t
*hdl
, const char *fsname
)
907 find
.mtn_mt
.mnt_special
= (char *)fsname
;
908 if ((ret
= avl_find(&hdl
->libzfs_mnttab_cache
, (void *)&find
, NULL
))) {
909 avl_remove(&hdl
->libzfs_mnttab_cache
, ret
);
910 free(ret
->mtn_mt
.mnt_special
);
911 free(ret
->mtn_mt
.mnt_mountp
);
912 free(ret
->mtn_mt
.mnt_fstype
);
913 free(ret
->mtn_mt
.mnt_mntopts
);
919 zfs_spa_version(zfs_handle_t
*zhp
, int *spa_version
)
921 zpool_handle_t
*zpool_handle
= zhp
->zpool_hdl
;
923 if (zpool_handle
== NULL
)
926 *spa_version
= zpool_get_prop_int(zpool_handle
,
927 ZPOOL_PROP_VERSION
, NULL
);
932 * The choice of reservation property depends on the SPA version.
935 zfs_which_resv_prop(zfs_handle_t
*zhp
, zfs_prop_t
*resv_prop
)
939 if (zfs_spa_version(zhp
, &spa_version
) < 0)
942 if (spa_version
>= SPA_VERSION_REFRESERVATION
)
943 *resv_prop
= ZFS_PROP_REFRESERVATION
;
945 *resv_prop
= ZFS_PROP_RESERVATION
;
951 * Given an nvlist of properties to set, validates that they are correct, and
952 * parses any numeric properties (index, boolean, etc) if they are specified as
956 zfs_valid_proplist(libzfs_handle_t
*hdl
, zfs_type_t type
, nvlist_t
*nvl
,
957 uint64_t zoned
, zfs_handle_t
*zhp
, zpool_handle_t
*zpool_hdl
,
965 int chosen_normal
= -1;
968 if (nvlist_alloc(&ret
, NV_UNIQUE_NAME
, 0) != 0) {
969 (void) no_memory(hdl
);
974 * Make sure this property is valid and applies to this type.
978 while ((elem
= nvlist_next_nvpair(nvl
, elem
)) != NULL
) {
979 const char *propname
= nvpair_name(elem
);
981 prop
= zfs_name_to_prop(propname
);
982 if (prop
== ZPROP_INVAL
&& zfs_prop_user(propname
)) {
984 * This is a user property: make sure it's a
985 * string, and that it's less than ZAP_MAXNAMELEN.
987 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
988 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
989 "'%s' must be a string"), propname
);
990 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
994 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
995 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
996 "property name '%s' is too long"),
998 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1002 (void) nvpair_value_string(elem
, &strval
);
1003 if (nvlist_add_string(ret
, propname
, strval
) != 0) {
1004 (void) no_memory(hdl
);
1011 * Currently, only user properties can be modified on
1014 if (type
== ZFS_TYPE_SNAPSHOT
) {
1015 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1016 "this property can not be modified for snapshots"));
1017 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1021 if (prop
== ZPROP_INVAL
&& zfs_prop_userquota(propname
)) {
1022 zfs_userquota_prop_t uqtype
;
1023 char newpropname
[128];
1028 if (userquota_propname_decode(propname
, zoned
,
1029 &uqtype
, domain
, sizeof (domain
), &rid
) != 0) {
1031 dgettext(TEXT_DOMAIN
,
1032 "'%s' has an invalid user/group name"),
1034 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1038 if (uqtype
!= ZFS_PROP_USERQUOTA
&&
1039 uqtype
!= ZFS_PROP_GROUPQUOTA
&&
1040 uqtype
!= ZFS_PROP_USEROBJQUOTA
&&
1041 uqtype
!= ZFS_PROP_GROUPOBJQUOTA
) {
1043 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1045 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
1050 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
1051 (void) nvpair_value_string(elem
, &strval
);
1052 if (strcmp(strval
, "none") == 0) {
1054 } else if (zfs_nicestrtonum(hdl
,
1055 strval
, &intval
) != 0) {
1056 (void) zfs_error(hdl
,
1057 EZFS_BADPROP
, errbuf
);
1060 } else if (nvpair_type(elem
) ==
1062 (void) nvpair_value_uint64(elem
, &intval
);
1064 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1065 "use 'none' to disable "
1066 "userquota/groupquota"));
1070 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1071 "'%s' must be a number"), propname
);
1072 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1077 * Encode the prop name as
1078 * userquota@<hex-rid>-domain, to make it easy
1079 * for the kernel to decode.
1081 (void) snprintf(newpropname
, sizeof (newpropname
),
1082 "%s%llx-%s", zfs_userquota_prop_prefixes
[uqtype
],
1083 (longlong_t
)rid
, domain
);
1087 if (nvlist_add_uint64_array(ret
, newpropname
,
1089 (void) no_memory(hdl
);
1093 } else if (prop
== ZPROP_INVAL
&& zfs_prop_written(propname
)) {
1094 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1095 "'%s' is readonly"),
1097 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1101 if (prop
== ZPROP_INVAL
) {
1102 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1103 "invalid property '%s'"), propname
);
1104 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1108 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
)) {
1110 dgettext(TEXT_DOMAIN
, "'%s' does not "
1111 "apply to datasets of this type"), propname
);
1112 (void) zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
);
1116 if (zfs_prop_readonly(prop
) &&
1117 (!zfs_prop_setonce(prop
) || zhp
!= NULL
)) {
1119 dgettext(TEXT_DOMAIN
, "'%s' is readonly"),
1121 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
1125 if (zprop_parse_value(hdl
, elem
, prop
, type
, ret
,
1126 &strval
, &intval
, errbuf
) != 0)
1130 * Perform some additional checks for specific properties.
1133 case ZFS_PROP_VERSION
:
1139 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1140 if (intval
< version
) {
1141 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1142 "Can not downgrade; already at version %u"),
1144 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1150 case ZFS_PROP_VOLBLOCKSIZE
:
1151 case ZFS_PROP_RECORDSIZE
:
1153 int maxbs
= SPA_MAXBLOCKSIZE
;
1156 if (zpool_hdl
!= NULL
) {
1157 maxbs
= zpool_get_prop_int(zpool_hdl
,
1158 ZPOOL_PROP_MAXBLOCKSIZE
, NULL
);
1161 * The value must be a power of two between
1162 * SPA_MINBLOCKSIZE and maxbs.
1164 if (intval
< SPA_MINBLOCKSIZE
||
1165 intval
> maxbs
|| !ISP2(intval
)) {
1166 zfs_nicenum(maxbs
, buf
, sizeof (buf
));
1167 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1168 "'%s' must be power of 2 from 512B "
1169 "to %s"), propname
, buf
);
1170 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1175 case ZFS_PROP_MLSLABEL
:
1177 #ifdef HAVE_MLSLABEL
1179 * Verify the mlslabel string and convert to
1180 * internal hex label string.
1184 char *hex
= NULL
; /* internal label string */
1186 /* Default value is already OK. */
1187 if (strcasecmp(strval
, ZFS_MLSLABEL_DEFAULT
) == 0)
1190 /* Verify the label can be converted to binary form */
1191 if (((new_sl
= m_label_alloc(MAC_LABEL
)) == NULL
) ||
1192 (str_to_label(strval
, &new_sl
, MAC_LABEL
,
1193 L_NO_CORRECTION
, NULL
) == -1)) {
1197 /* Now translate to hex internal label string */
1198 if (label_to_str(new_sl
, &hex
, M_INTERNAL
,
1204 m_label_free(new_sl
);
1206 /* If string is already in internal form, we're done. */
1207 if (strcmp(strval
, hex
) == 0) {
1212 /* Replace the label string with the internal form. */
1213 (void) nvlist_remove(ret
, zfs_prop_to_name(prop
),
1215 verify(nvlist_add_string(ret
, zfs_prop_to_name(prop
),
1222 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1223 "invalid mlslabel '%s'"), strval
);
1224 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1225 m_label_free(new_sl
); /* OK if null */
1228 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1229 "mlslabels are unsupported"));
1230 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1232 #endif /* HAVE_MLSLABEL */
1235 case ZFS_PROP_MOUNTPOINT
:
1237 namecheck_err_t why
;
1239 if (strcmp(strval
, ZFS_MOUNTPOINT_NONE
) == 0 ||
1240 strcmp(strval
, ZFS_MOUNTPOINT_LEGACY
) == 0)
1243 if (mountpoint_namecheck(strval
, &why
)) {
1245 case NAME_ERR_LEADING_SLASH
:
1247 dgettext(TEXT_DOMAIN
,
1248 "'%s' must be an absolute path, "
1249 "'none', or 'legacy'"), propname
);
1251 case NAME_ERR_TOOLONG
:
1253 dgettext(TEXT_DOMAIN
,
1254 "component of '%s' is too long"),
1260 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1267 case ZFS_PROP_SHARESMB
:
1268 case ZFS_PROP_SHARENFS
:
1270 * For the mountpoint and sharenfs or sharesmb
1271 * properties, check if it can be set in a
1272 * global/non-global zone based on
1273 * the zoned property value:
1275 * global zone non-global zone
1276 * --------------------------------------------------
1277 * zoned=on mountpoint (no) mountpoint (yes)
1278 * sharenfs (no) sharenfs (no)
1279 * sharesmb (no) sharesmb (no)
1281 * zoned=off mountpoint (yes) N/A
1286 if (getzoneid() == GLOBAL_ZONEID
) {
1287 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1288 "'%s' cannot be set on "
1289 "dataset in a non-global zone"),
1291 (void) zfs_error(hdl
, EZFS_ZONED
,
1294 } else if (prop
== ZFS_PROP_SHARENFS
||
1295 prop
== ZFS_PROP_SHARESMB
) {
1296 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1297 "'%s' cannot be set in "
1298 "a non-global zone"), propname
);
1299 (void) zfs_error(hdl
, EZFS_ZONED
,
1303 } else if (getzoneid() != GLOBAL_ZONEID
) {
1305 * If zoned property is 'off', this must be in
1306 * a global zone. If not, something is wrong.
1308 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1309 "'%s' cannot be set while dataset "
1310 "'zoned' property is set"), propname
);
1311 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1316 * At this point, it is legitimate to set the
1317 * property. Now we want to make sure that the
1318 * property value is valid if it is sharenfs.
1320 if ((prop
== ZFS_PROP_SHARENFS
||
1321 prop
== ZFS_PROP_SHARESMB
) &&
1322 strcmp(strval
, "on") != 0 &&
1323 strcmp(strval
, "off") != 0) {
1324 zfs_share_proto_t proto
;
1326 if (prop
== ZFS_PROP_SHARESMB
)
1332 * Must be an valid sharing protocol
1333 * option string so init the libshare
1334 * in order to enable the parser and
1335 * then parse the options. We use the
1336 * control API since we don't care about
1337 * the current configuration and don't
1338 * want the overhead of loading it
1339 * until we actually do something.
1342 if (zfs_init_libshare(hdl
,
1343 SA_INIT_CONTROL_API
) != SA_OK
) {
1345 * An error occurred so we can't do
1348 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1349 "'%s' cannot be set: problem "
1350 "in share initialization"),
1352 (void) zfs_error(hdl
, EZFS_BADPROP
,
1357 if (zfs_parse_options(strval
, proto
) != SA_OK
) {
1359 * There was an error in parsing so
1360 * deal with it by issuing an error
1361 * message and leaving after
1362 * uninitializing the the libshare
1365 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1366 "'%s' cannot be set to invalid "
1367 "options"), propname
);
1368 (void) zfs_error(hdl
, EZFS_BADPROP
,
1370 zfs_uninit_libshare(hdl
);
1373 zfs_uninit_libshare(hdl
);
1377 case ZFS_PROP_UTF8ONLY
:
1378 chosen_utf
= (int)intval
;
1380 case ZFS_PROP_NORMALIZE
:
1381 chosen_normal
= (int)intval
;
1388 * For changes to existing volumes, we have some additional
1389 * checks to enforce.
1391 if (type
== ZFS_TYPE_VOLUME
&& zhp
!= NULL
) {
1392 uint64_t volsize
= zfs_prop_get_int(zhp
,
1394 uint64_t blocksize
= zfs_prop_get_int(zhp
,
1395 ZFS_PROP_VOLBLOCKSIZE
);
1399 case ZFS_PROP_RESERVATION
:
1400 case ZFS_PROP_REFRESERVATION
:
1401 if (intval
> volsize
) {
1402 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1403 "'%s' is greater than current "
1404 "volume size"), propname
);
1405 (void) zfs_error(hdl
, EZFS_BADPROP
,
1411 case ZFS_PROP_VOLSIZE
:
1412 if (intval
% blocksize
!= 0) {
1413 zfs_nicenum(blocksize
, buf
,
1415 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1416 "'%s' must be a multiple of "
1417 "volume block size (%s)"),
1419 (void) zfs_error(hdl
, EZFS_BADPROP
,
1425 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1426 "'%s' cannot be zero"),
1428 (void) zfs_error(hdl
, EZFS_BADPROP
,
1440 * If normalization was chosen, but no UTF8 choice was made,
1441 * enforce rejection of non-UTF8 names.
1443 * If normalization was chosen, but rejecting non-UTF8 names
1444 * was explicitly not chosen, it is an error.
1446 if (chosen_normal
> 0 && chosen_utf
< 0) {
1447 if (nvlist_add_uint64(ret
,
1448 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
), 1) != 0) {
1449 (void) no_memory(hdl
);
1452 } else if (chosen_normal
> 0 && chosen_utf
== 0) {
1453 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1454 "'%s' must be set 'on' if normalization chosen"),
1455 zfs_prop_to_name(ZFS_PROP_UTF8ONLY
));
1456 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1467 zfs_add_synthetic_resv(zfs_handle_t
*zhp
, nvlist_t
*nvl
)
1469 uint64_t old_volsize
;
1470 uint64_t new_volsize
;
1471 uint64_t old_reservation
;
1472 uint64_t new_reservation
;
1473 zfs_prop_t resv_prop
;
1477 * If this is an existing volume, and someone is setting the volsize,
1478 * make sure that it matches the reservation, or add it if necessary.
1480 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
1481 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
1483 old_reservation
= zfs_prop_get_int(zhp
, resv_prop
);
1485 props
= fnvlist_alloc();
1486 fnvlist_add_uint64(props
, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
1487 zfs_prop_get_int(zhp
, ZFS_PROP_VOLBLOCKSIZE
));
1489 if ((zvol_volsize_to_reservation(old_volsize
, props
) !=
1490 old_reservation
) || nvlist_exists(nvl
,
1491 zfs_prop_to_name(resv_prop
))) {
1492 fnvlist_free(props
);
1495 if (nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1496 &new_volsize
) != 0) {
1497 fnvlist_free(props
);
1500 new_reservation
= zvol_volsize_to_reservation(new_volsize
, props
);
1501 fnvlist_free(props
);
1503 if (nvlist_add_uint64(nvl
, zfs_prop_to_name(resv_prop
),
1504 new_reservation
) != 0) {
1505 (void) no_memory(zhp
->zfs_hdl
);
1512 zfs_setprop_error(libzfs_handle_t
*hdl
, zfs_prop_t prop
, int err
,
1519 * For quotas and reservations, ENOSPC indicates
1520 * something different; setting a quota or reservation
1521 * doesn't use any disk space.
1524 case ZFS_PROP_QUOTA
:
1525 case ZFS_PROP_REFQUOTA
:
1526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1527 "size is less than current used or "
1529 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1532 case ZFS_PROP_RESERVATION
:
1533 case ZFS_PROP_REFRESERVATION
:
1534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1535 "size is greater than available space"));
1536 (void) zfs_error(hdl
, EZFS_PROPSPACE
, errbuf
);
1540 (void) zfs_standard_error(hdl
, err
, errbuf
);
1546 (void) zfs_standard_error(hdl
, EBUSY
, errbuf
);
1550 (void) zfs_error(hdl
, EZFS_DSREADONLY
, errbuf
);
1554 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1555 "property value too long"));
1556 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1560 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1561 "pool and or dataset must be upgraded to set this "
1562 "property or value"));
1563 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1567 if (prop
== ZFS_PROP_COMPRESSION
||
1568 prop
== ZFS_PROP_DNODESIZE
||
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 zfs_is_namespace_prop(zfs_prop_t prop
)
1614 case ZFS_PROP_ATIME
:
1615 case ZFS_PROP_RELATIME
:
1616 case ZFS_PROP_DEVICES
:
1618 case ZFS_PROP_SETUID
:
1619 case ZFS_PROP_READONLY
:
1620 case ZFS_PROP_XATTR
:
1621 case ZFS_PROP_NBMAND
:
1630 * Given a property name and value, set the property for the given dataset.
1633 zfs_prop_set(zfs_handle_t
*zhp
, const char *propname
, const char *propval
)
1637 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1638 nvlist_t
*nvl
= NULL
;
1640 (void) snprintf(errbuf
, sizeof (errbuf
),
1641 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1644 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0 ||
1645 nvlist_add_string(nvl
, propname
, propval
) != 0) {
1646 (void) no_memory(hdl
);
1650 ret
= zfs_prop_set_list(zhp
, nvl
);
1660 * Given an nvlist of property names and values, set the properties for the
1664 zfs_prop_set_list(zfs_handle_t
*zhp
, nvlist_t
*props
)
1666 zfs_cmd_t zc
= {"\0"};
1668 prop_changelist_t
**cls
= NULL
;
1671 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1675 zfs_prop_t prop
= 0;
1678 (void) snprintf(errbuf
, sizeof (errbuf
),
1679 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
1682 if ((nvl
= zfs_valid_proplist(hdl
, zhp
->zfs_type
, props
,
1683 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
), zhp
, zhp
->zpool_hdl
,
1688 * We have to check for any extra properties which need to be added
1689 * before computing the length of the nvlist.
1691 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1693 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1694 if (zfs_name_to_prop(nvpair_name(elem
)) == ZFS_PROP_VOLSIZE
&&
1695 (added_resv
= zfs_add_synthetic_resv(zhp
, nvl
)) == -1) {
1700 * Check how many properties we're setting and allocate an array to
1701 * store changelist pointers for postfix().
1703 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1705 elem
= nvlist_next_nvpair(nvl
, elem
))
1707 if ((cls
= calloc(nvl_len
, sizeof (prop_changelist_t
*))) == NULL
)
1711 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1713 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1715 prop
= zfs_name_to_prop(nvpair_name(elem
));
1717 assert(cl_idx
< nvl_len
);
1719 * We don't want to unmount & remount the dataset when changing
1720 * its canmount property to 'on' or 'noauto'. We only use
1721 * the changelist logic to unmount when setting canmount=off.
1723 if (prop
!= ZFS_PROP_CANMOUNT
||
1724 (fnvpair_value_uint64(elem
) == ZFS_CANMOUNT_OFF
&&
1725 zfs_is_mounted(zhp
, NULL
))) {
1726 cls
[cl_idx
] = changelist_gather(zhp
, prop
, 0, 0);
1727 if (cls
[cl_idx
] == NULL
)
1731 if (prop
== ZFS_PROP_MOUNTPOINT
&&
1732 changelist_haszonedchild(cls
[cl_idx
])) {
1733 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1734 "child dataset with inherited mountpoint is used "
1735 "in a non-global zone"));
1736 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1740 if (cls
[cl_idx
] != NULL
&&
1741 (ret
= changelist_prefix(cls
[cl_idx
])) != 0)
1746 assert(cl_idx
== nvl_len
);
1749 * Execute the corresponding ioctl() to set this list of properties.
1751 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1753 if ((ret
= zcmd_write_src_nvlist(hdl
, &zc
, nvl
)) != 0 ||
1754 (ret
= zcmd_alloc_dst_nvlist(hdl
, &zc
, 0)) != 0)
1757 ret
= zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1760 /* Get the list of unset properties back and report them. */
1761 nvlist_t
*errorprops
= NULL
;
1762 if (zcmd_read_dst_nvlist(hdl
, &zc
, &errorprops
) != 0)
1764 for (elem
= nvlist_next_nvpair(nvl
, NULL
);
1766 elem
= nvlist_next_nvpair(nvl
, elem
)) {
1767 prop
= zfs_name_to_prop(nvpair_name(elem
));
1768 zfs_setprop_error(hdl
, prop
, errno
, errbuf
);
1770 nvlist_free(errorprops
);
1772 if (added_resv
&& errno
== ENOSPC
) {
1773 /* clean up the volsize property we tried to set */
1774 uint64_t old_volsize
= zfs_prop_get_int(zhp
,
1778 zcmd_free_nvlists(&zc
);
1780 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
1782 if (nvlist_add_uint64(nvl
,
1783 zfs_prop_to_name(ZFS_PROP_VOLSIZE
),
1786 if (zcmd_write_src_nvlist(hdl
, &zc
, nvl
) != 0)
1788 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
1791 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1792 if (cls
[cl_idx
] != NULL
) {
1793 int clp_err
= changelist_postfix(cls
[cl_idx
]);
1801 * Refresh the statistics so the new property
1802 * value is reflected.
1804 (void) get_stats(zhp
);
1807 * Remount the filesystem to propagate the change
1808 * if one of the options handled by the generic
1809 * Linux namespace layer has been modified.
1811 if (zfs_is_namespace_prop(prop
) &&
1812 zfs_is_mounted(zhp
, NULL
))
1813 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1819 zcmd_free_nvlists(&zc
);
1821 for (cl_idx
= 0; cl_idx
< nvl_len
; cl_idx
++) {
1822 if (cls
[cl_idx
] != NULL
)
1823 changelist_free(cls
[cl_idx
]);
1831 * Given a property, inherit the value from the parent dataset, or if received
1832 * is TRUE, revert to the received value, if any.
1835 zfs_prop_inherit(zfs_handle_t
*zhp
, const char *propname
, boolean_t received
)
1837 zfs_cmd_t zc
= {"\0"};
1839 prop_changelist_t
*cl
;
1840 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1844 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1845 "cannot inherit %s for '%s'"), propname
, zhp
->zfs_name
);
1847 zc
.zc_cookie
= received
;
1848 if ((prop
= zfs_name_to_prop(propname
)) == ZPROP_INVAL
) {
1850 * For user properties, the amount of work we have to do is very
1851 * small, so just do it here.
1853 if (!zfs_prop_user(propname
)) {
1854 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1855 "invalid property"));
1856 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1859 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1860 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1862 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
) != 0)
1863 return (zfs_standard_error(hdl
, errno
, errbuf
));
1869 * Verify that this property is inheritable.
1871 if (zfs_prop_readonly(prop
))
1872 return (zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
));
1874 if (!zfs_prop_inheritable(prop
) && !received
)
1875 return (zfs_error(hdl
, EZFS_PROPNONINHERIT
, errbuf
));
1878 * Check to see if the value applies to this type
1880 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
1881 return (zfs_error(hdl
, EZFS_PROPTYPE
, errbuf
));
1884 * Normalize the name, to get rid of shorthand abbreviations.
1886 propname
= zfs_prop_to_name(prop
);
1887 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1888 (void) strlcpy(zc
.zc_value
, propname
, sizeof (zc
.zc_value
));
1890 if (prop
== ZFS_PROP_MOUNTPOINT
&& getzoneid() == GLOBAL_ZONEID
&&
1891 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
1892 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1893 "dataset is used in a non-global zone"));
1894 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
1898 * Determine datasets which will be affected by this change, if any.
1900 if ((cl
= changelist_gather(zhp
, prop
, 0, 0)) == NULL
)
1903 if (prop
== ZFS_PROP_MOUNTPOINT
&& changelist_haszonedchild(cl
)) {
1904 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1905 "child dataset with inherited mountpoint is used "
1906 "in a non-global zone"));
1907 ret
= zfs_error(hdl
, EZFS_ZONED
, errbuf
);
1911 if ((ret
= changelist_prefix(cl
)) != 0)
1914 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_INHERIT_PROP
, &zc
)) != 0) {
1915 return (zfs_standard_error(hdl
, errno
, errbuf
));
1918 if ((ret
= changelist_postfix(cl
)) != 0)
1922 * Refresh the statistics so the new property is reflected.
1924 (void) get_stats(zhp
);
1927 * Remount the filesystem to propagate the change
1928 * if one of the options handled by the generic
1929 * Linux namespace layer has been modified.
1931 if (zfs_is_namespace_prop(prop
) &&
1932 zfs_is_mounted(zhp
, NULL
))
1933 ret
= zfs_mount(zhp
, MNTOPT_REMOUNT
, 0);
1937 changelist_free(cl
);
1942 * True DSL properties are stored in an nvlist. The following two functions
1943 * extract them appropriately.
1946 getprop_uint64(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1952 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1953 zfs_prop_to_name(prop
), &nv
) == 0) {
1954 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
1955 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1957 verify(!zhp
->zfs_props_table
||
1958 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1959 value
= zfs_prop_default_numeric(prop
);
1967 getprop_string(zfs_handle_t
*zhp
, zfs_prop_t prop
, char **source
)
1973 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
1974 zfs_prop_to_name(prop
), &nv
) == 0) {
1975 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
1976 (void) nvlist_lookup_string(nv
, ZPROP_SOURCE
, source
);
1978 verify(!zhp
->zfs_props_table
||
1979 zhp
->zfs_props_table
[prop
] == B_TRUE
);
1980 value
= zfs_prop_default_string(prop
);
1988 zfs_is_recvd_props_mode(zfs_handle_t
*zhp
)
1990 return (zhp
->zfs_props
== zhp
->zfs_recvd_props
);
1994 zfs_set_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
1996 *cookie
= (uint64_t)(uintptr_t)zhp
->zfs_props
;
1997 zhp
->zfs_props
= zhp
->zfs_recvd_props
;
2001 zfs_unset_recvd_props_mode(zfs_handle_t
*zhp
, uint64_t *cookie
)
2003 zhp
->zfs_props
= (nvlist_t
*)(uintptr_t)*cookie
;
2008 * Internal function for getting a numeric property. Both zfs_prop_get() and
2009 * zfs_prop_get_int() are built using this interface.
2011 * Certain properties can be overridden using 'mount -o'. In this case, scan
2012 * the contents of the /proc/self/mounts entry, searching for the
2013 * appropriate options. If they differ from the on-disk values, report the
2014 * current values and mark the source "temporary".
2017 get_numeric_property(zfs_handle_t
*zhp
, zfs_prop_t prop
, zprop_source_t
*src
,
2018 char **source
, uint64_t *val
)
2020 zfs_cmd_t zc
= {"\0"};
2021 nvlist_t
*zplprops
= NULL
;
2023 char *mntopt_on
= NULL
;
2024 char *mntopt_off
= NULL
;
2025 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2030 * If the property is being fetched for a snapshot, check whether
2031 * the property is valid for the snapshot's head dataset type.
2033 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
&&
2034 !zfs_prop_valid_for_type(prop
, zhp
->zfs_head_type
, B_TRUE
)) {
2035 *val
= zfs_prop_default_numeric(prop
);
2040 case ZFS_PROP_ATIME
:
2041 mntopt_on
= MNTOPT_ATIME
;
2042 mntopt_off
= MNTOPT_NOATIME
;
2045 case ZFS_PROP_RELATIME
:
2046 mntopt_on
= MNTOPT_RELATIME
;
2047 mntopt_off
= MNTOPT_NORELATIME
;
2050 case ZFS_PROP_DEVICES
:
2051 mntopt_on
= MNTOPT_DEVICES
;
2052 mntopt_off
= MNTOPT_NODEVICES
;
2056 mntopt_on
= MNTOPT_EXEC
;
2057 mntopt_off
= MNTOPT_NOEXEC
;
2060 case ZFS_PROP_READONLY
:
2061 mntopt_on
= MNTOPT_RO
;
2062 mntopt_off
= MNTOPT_RW
;
2065 case ZFS_PROP_SETUID
:
2066 mntopt_on
= MNTOPT_SETUID
;
2067 mntopt_off
= MNTOPT_NOSETUID
;
2070 case ZFS_PROP_XATTR
:
2071 mntopt_on
= MNTOPT_XATTR
;
2072 mntopt_off
= MNTOPT_NOXATTR
;
2075 case ZFS_PROP_NBMAND
:
2076 mntopt_on
= MNTOPT_NBMAND
;
2077 mntopt_off
= MNTOPT_NONBMAND
;
2084 * Because looking up the mount options is potentially expensive
2085 * (iterating over all of /proc/self/mounts), we defer its
2086 * calculation until we're looking up a property which requires
2089 if (!zhp
->zfs_mntcheck
&&
2090 (mntopt_on
!= NULL
|| prop
== ZFS_PROP_MOUNTED
)) {
2091 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2092 struct mnttab entry
;
2094 if (libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0) {
2095 zhp
->zfs_mntopts
= zfs_strdup(hdl
,
2097 if (zhp
->zfs_mntopts
== NULL
)
2101 zhp
->zfs_mntcheck
= B_TRUE
;
2104 if (zhp
->zfs_mntopts
== NULL
)
2105 mnt
.mnt_mntopts
= "";
2107 mnt
.mnt_mntopts
= zhp
->zfs_mntopts
;
2110 case ZFS_PROP_ATIME
:
2111 case ZFS_PROP_RELATIME
:
2112 case ZFS_PROP_DEVICES
:
2114 case ZFS_PROP_READONLY
:
2115 case ZFS_PROP_SETUID
:
2116 case ZFS_PROP_XATTR
:
2117 case ZFS_PROP_NBMAND
:
2118 *val
= getprop_uint64(zhp
, prop
, source
);
2123 if (hasmntopt(&mnt
, mntopt_on
) && !*val
) {
2126 *src
= ZPROP_SRC_TEMPORARY
;
2127 } else if (hasmntopt(&mnt
, mntopt_off
) && *val
) {
2130 *src
= ZPROP_SRC_TEMPORARY
;
2134 case ZFS_PROP_CANMOUNT
:
2135 case ZFS_PROP_VOLSIZE
:
2136 case ZFS_PROP_QUOTA
:
2137 case ZFS_PROP_REFQUOTA
:
2138 case ZFS_PROP_RESERVATION
:
2139 case ZFS_PROP_REFRESERVATION
:
2140 case ZFS_PROP_FILESYSTEM_LIMIT
:
2141 case ZFS_PROP_SNAPSHOT_LIMIT
:
2142 case ZFS_PROP_FILESYSTEM_COUNT
:
2143 case ZFS_PROP_SNAPSHOT_COUNT
:
2144 *val
= getprop_uint64(zhp
, prop
, source
);
2146 if (*source
== NULL
) {
2147 /* not default, must be local */
2148 *source
= zhp
->zfs_name
;
2152 case ZFS_PROP_MOUNTED
:
2153 *val
= (zhp
->zfs_mntopts
!= NULL
);
2156 case ZFS_PROP_NUMCLONES
:
2157 *val
= zhp
->zfs_dmustats
.dds_num_clones
;
2160 case ZFS_PROP_VERSION
:
2161 case ZFS_PROP_NORMALIZE
:
2162 case ZFS_PROP_UTF8ONLY
:
2164 if (zcmd_alloc_dst_nvlist(zhp
->zfs_hdl
, &zc
, 0) != 0)
2166 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2167 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_ZPLPROPS
, &zc
)) {
2168 zcmd_free_nvlists(&zc
);
2169 if (prop
== ZFS_PROP_VERSION
&&
2170 zhp
->zfs_type
== ZFS_TYPE_VOLUME
)
2171 *val
= zfs_prop_default_numeric(prop
);
2174 if (zcmd_read_dst_nvlist(zhp
->zfs_hdl
, &zc
, &zplprops
) != 0 ||
2175 nvlist_lookup_uint64(zplprops
, zfs_prop_to_name(prop
),
2177 zcmd_free_nvlists(&zc
);
2180 nvlist_free(zplprops
);
2181 zcmd_free_nvlists(&zc
);
2184 case ZFS_PROP_INCONSISTENT
:
2185 *val
= zhp
->zfs_dmustats
.dds_inconsistent
;
2189 switch (zfs_prop_get_type(prop
)) {
2190 case PROP_TYPE_NUMBER
:
2191 case PROP_TYPE_INDEX
:
2192 *val
= getprop_uint64(zhp
, prop
, source
);
2194 * If we tried to use a default value for a
2195 * readonly property, it means that it was not
2196 * present. Note this only applies to "truly"
2197 * readonly properties, not set-once properties
2198 * like volblocksize.
2200 if (zfs_prop_readonly(prop
) &&
2201 !zfs_prop_setonce(prop
) &&
2202 *source
!= NULL
&& (*source
)[0] == '\0') {
2208 case PROP_TYPE_STRING
:
2210 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2211 "cannot get non-numeric property"));
2212 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADPROP
,
2213 dgettext(TEXT_DOMAIN
, "internal error")));
2221 * Calculate the source type, given the raw source string.
2224 get_source(zfs_handle_t
*zhp
, zprop_source_t
*srctype
, char *source
,
2225 char *statbuf
, size_t statlen
)
2227 if (statbuf
== NULL
|| *srctype
== ZPROP_SRC_TEMPORARY
)
2230 if (source
== NULL
) {
2231 *srctype
= ZPROP_SRC_NONE
;
2232 } else if (source
[0] == '\0') {
2233 *srctype
= ZPROP_SRC_DEFAULT
;
2234 } else if (strstr(source
, ZPROP_SOURCE_VAL_RECVD
) != NULL
) {
2235 *srctype
= ZPROP_SRC_RECEIVED
;
2237 if (strcmp(source
, zhp
->zfs_name
) == 0) {
2238 *srctype
= ZPROP_SRC_LOCAL
;
2240 (void) strlcpy(statbuf
, source
, statlen
);
2241 *srctype
= ZPROP_SRC_INHERITED
;
2248 zfs_prop_get_recvd(zfs_handle_t
*zhp
, const char *propname
, char *propbuf
,
2249 size_t proplen
, boolean_t literal
)
2254 if (zhp
->zfs_recvd_props
== NULL
)
2255 if (get_recvd_props_ioctl(zhp
) != 0)
2258 prop
= zfs_name_to_prop(propname
);
2260 if (prop
!= ZPROP_INVAL
) {
2262 if (!nvlist_exists(zhp
->zfs_recvd_props
, propname
))
2264 zfs_set_recvd_props_mode(zhp
, &cookie
);
2265 err
= zfs_prop_get(zhp
, prop
, propbuf
, proplen
,
2266 NULL
, NULL
, 0, literal
);
2267 zfs_unset_recvd_props_mode(zhp
, &cookie
);
2271 if (nvlist_lookup_nvlist(zhp
->zfs_recvd_props
,
2272 propname
, &propval
) != 0)
2274 verify(nvlist_lookup_string(propval
, ZPROP_VALUE
,
2276 (void) strlcpy(propbuf
, recvdval
, proplen
);
2279 return (err
== 0 ? 0 : -1);
2283 get_clones_string(zfs_handle_t
*zhp
, char *propbuf
, size_t proplen
)
2288 value
= zfs_get_clones_nvl(zhp
);
2293 for (pair
= nvlist_next_nvpair(value
, NULL
); pair
!= NULL
;
2294 pair
= nvlist_next_nvpair(value
, pair
)) {
2295 if (propbuf
[0] != '\0')
2296 (void) strlcat(propbuf
, ",", proplen
);
2297 (void) strlcat(propbuf
, nvpair_name(pair
), proplen
);
2303 struct get_clones_arg
{
2307 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2311 get_clones_cb(zfs_handle_t
*zhp
, void *arg
)
2313 struct get_clones_arg
*gca
= arg
;
2315 if (gca
->numclones
== 0) {
2320 if (zfs_prop_get(zhp
, ZFS_PROP_ORIGIN
, gca
->buf
, sizeof (gca
->buf
),
2321 NULL
, NULL
, 0, B_TRUE
) != 0)
2323 if (strcmp(gca
->buf
, gca
->origin
) == 0) {
2324 fnvlist_add_boolean(gca
->value
, zfs_get_name(zhp
));
2329 (void) zfs_iter_children(zhp
, get_clones_cb
, gca
);
2335 zfs_get_clones_nvl(zfs_handle_t
*zhp
)
2337 nvlist_t
*nv
, *value
;
2339 if (nvlist_lookup_nvlist(zhp
->zfs_props
,
2340 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
) != 0) {
2341 struct get_clones_arg gca
;
2344 * if this is a snapshot, then the kernel wasn't able
2345 * to get the clones. Do it by slowly iterating.
2347 if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
)
2349 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0) != 0)
2351 if (nvlist_alloc(&value
, NV_UNIQUE_NAME
, 0) != 0) {
2356 gca
.numclones
= zfs_prop_get_int(zhp
, ZFS_PROP_NUMCLONES
);
2358 gca
.origin
= zhp
->zfs_name
;
2360 if (gca
.numclones
!= 0) {
2362 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
2365 /* get the pool name */
2366 (void) strlcpy(pool
, zhp
->zfs_name
, sizeof (pool
));
2367 (void) strsep(&cp
, "/@");
2368 root
= zfs_open(zhp
->zfs_hdl
, pool
,
2369 ZFS_TYPE_FILESYSTEM
);
2376 (void) get_clones_cb(root
, &gca
);
2379 if (gca
.numclones
!= 0 ||
2380 nvlist_add_nvlist(nv
, ZPROP_VALUE
, value
) != 0 ||
2381 nvlist_add_nvlist(zhp
->zfs_props
,
2382 zfs_prop_to_name(ZFS_PROP_CLONES
), nv
) != 0) {
2389 verify(0 == nvlist_lookup_nvlist(zhp
->zfs_props
,
2390 zfs_prop_to_name(ZFS_PROP_CLONES
), &nv
));
2393 verify(nvlist_lookup_nvlist(nv
, ZPROP_VALUE
, &value
) == 0);
2399 * Retrieve a property from the given object. If 'literal' is specified, then
2400 * numbers are left as exact values. Otherwise, numbers are converted to a
2401 * human-readable form.
2403 * Returns 0 on success, or -1 on error.
2406 zfs_prop_get(zfs_handle_t
*zhp
, zfs_prop_t prop
, char *propbuf
, size_t proplen
,
2407 zprop_source_t
*src
, char *statbuf
, size_t statlen
, boolean_t literal
)
2409 char *source
= NULL
;
2413 boolean_t received
= zfs_is_recvd_props_mode(zhp
);
2416 * Check to see if this property applies to our object
2418 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
))
2421 if (received
&& zfs_prop_readonly(prop
))
2425 *src
= ZPROP_SRC_NONE
;
2428 case ZFS_PROP_CREATION
:
2430 * 'creation' is a time_t stored in the statistics. We convert
2431 * this into a string unless 'literal' is specified.
2434 val
= getprop_uint64(zhp
, prop
, &source
);
2435 time_t time
= (time_t)val
;
2439 localtime_r(&time
, &t
) == NULL
||
2440 strftime(propbuf
, proplen
, "%a %b %e %k:%M %Y",
2442 (void) snprintf(propbuf
, proplen
, "%llu",
2447 case ZFS_PROP_MOUNTPOINT
:
2449 * Getting the precise mountpoint can be tricky.
2451 * - for 'none' or 'legacy', return those values.
2452 * - for inherited mountpoints, we want to take everything
2453 * after our ancestor and append it to the inherited value.
2455 * If the pool has an alternate root, we want to prepend that
2456 * root to any values we return.
2459 str
= getprop_string(zhp
, prop
, &source
);
2461 if (str
[0] == '/') {
2462 char buf
[MAXPATHLEN
];
2464 const char *relpath
;
2467 * If we inherit the mountpoint, even from a dataset
2468 * with a received value, the source will be the path of
2469 * the dataset we inherit from. If source is
2470 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2473 if (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) == 0) {
2476 relpath
= zhp
->zfs_name
+ strlen(source
);
2477 if (relpath
[0] == '/')
2481 if ((zpool_get_prop(zhp
->zpool_hdl
,
2482 ZPOOL_PROP_ALTROOT
, buf
, MAXPATHLEN
, NULL
,
2483 B_FALSE
)) || (strcmp(root
, "-") == 0))
2486 * Special case an alternate root of '/'. This will
2487 * avoid having multiple leading slashes in the
2490 if (strcmp(root
, "/") == 0)
2494 * If the mountpoint is '/' then skip over this
2495 * if we are obtaining either an alternate root or
2496 * an inherited mountpoint.
2498 if (str
[1] == '\0' && (root
[0] != '\0' ||
2499 relpath
[0] != '\0'))
2502 if (relpath
[0] == '\0')
2503 (void) snprintf(propbuf
, proplen
, "%s%s",
2506 (void) snprintf(propbuf
, proplen
, "%s%s%s%s",
2507 root
, str
, relpath
[0] == '@' ? "" : "/",
2510 /* 'legacy' or 'none' */
2511 (void) strlcpy(propbuf
, str
, proplen
);
2516 case ZFS_PROP_ORIGIN
:
2517 str
= getprop_string(zhp
, prop
, &source
);
2520 (void) strlcpy(propbuf
, str
, proplen
);
2523 case ZFS_PROP_CLONES
:
2524 if (get_clones_string(zhp
, propbuf
, proplen
) != 0)
2528 case ZFS_PROP_QUOTA
:
2529 case ZFS_PROP_REFQUOTA
:
2530 case ZFS_PROP_RESERVATION
:
2531 case ZFS_PROP_REFRESERVATION
:
2533 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2537 * If quota or reservation is 0, we translate this into 'none'
2538 * (unless literal is set), and indicate that it's the default
2539 * value. Otherwise, we print the number nicely and indicate
2540 * that its set locally.
2544 (void) strlcpy(propbuf
, "0", proplen
);
2546 (void) strlcpy(propbuf
, "none", proplen
);
2549 (void) snprintf(propbuf
, proplen
, "%llu",
2552 zfs_nicenum(val
, propbuf
, proplen
);
2556 case ZFS_PROP_FILESYSTEM_LIMIT
:
2557 case ZFS_PROP_SNAPSHOT_LIMIT
:
2558 case ZFS_PROP_FILESYSTEM_COUNT
:
2559 case ZFS_PROP_SNAPSHOT_COUNT
:
2561 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2565 * If limit is UINT64_MAX, we translate this into 'none' (unless
2566 * literal is set), and indicate that it's the default value.
2567 * Otherwise, we print the number nicely and indicate that it's
2571 (void) snprintf(propbuf
, proplen
, "%llu",
2573 } else if (val
== UINT64_MAX
) {
2574 (void) strlcpy(propbuf
, "none", proplen
);
2576 zfs_nicenum(val
, propbuf
, proplen
);
2580 case ZFS_PROP_REFRATIO
:
2581 case ZFS_PROP_COMPRESSRATIO
:
2582 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2584 (void) snprintf(propbuf
, proplen
, "%llu.%02llux",
2585 (u_longlong_t
)(val
/ 100),
2586 (u_longlong_t
)(val
% 100));
2590 switch (zhp
->zfs_type
) {
2591 case ZFS_TYPE_FILESYSTEM
:
2594 case ZFS_TYPE_VOLUME
:
2597 case ZFS_TYPE_SNAPSHOT
:
2600 case ZFS_TYPE_BOOKMARK
:
2606 (void) snprintf(propbuf
, proplen
, "%s", str
);
2609 case ZFS_PROP_MOUNTED
:
2611 * The 'mounted' property is a pseudo-property that described
2612 * whether the filesystem is currently mounted. Even though
2613 * it's a boolean value, the typical values of "on" and "off"
2614 * don't make sense, so we translate to "yes" and "no".
2616 if (get_numeric_property(zhp
, ZFS_PROP_MOUNTED
,
2617 src
, &source
, &val
) != 0)
2620 (void) strlcpy(propbuf
, "yes", proplen
);
2622 (void) strlcpy(propbuf
, "no", proplen
);
2627 * The 'name' property is a pseudo-property derived from the
2628 * dataset name. It is presented as a real property to simplify
2631 (void) strlcpy(propbuf
, zhp
->zfs_name
, proplen
);
2634 case ZFS_PROP_MLSLABEL
:
2636 #ifdef HAVE_MLSLABEL
2637 m_label_t
*new_sl
= NULL
;
2638 char *ascii
= NULL
; /* human readable label */
2640 (void) strlcpy(propbuf
,
2641 getprop_string(zhp
, prop
, &source
), proplen
);
2643 if (literal
|| (strcasecmp(propbuf
,
2644 ZFS_MLSLABEL_DEFAULT
) == 0))
2648 * Try to translate the internal hex string to
2649 * human-readable output. If there are any
2650 * problems just use the hex string.
2653 if (str_to_label(propbuf
, &new_sl
, MAC_LABEL
,
2654 L_NO_CORRECTION
, NULL
) == -1) {
2655 m_label_free(new_sl
);
2659 if (label_to_str(new_sl
, &ascii
, M_LABEL
,
2663 m_label_free(new_sl
);
2666 m_label_free(new_sl
);
2668 (void) strlcpy(propbuf
, ascii
, proplen
);
2671 (void) strlcpy(propbuf
,
2672 getprop_string(zhp
, prop
, &source
), proplen
);
2673 #endif /* HAVE_MLSLABEL */
2679 * GUIDs are stored as numbers, but they are identifiers.
2680 * We don't want them to be pretty printed, because pretty
2681 * printing mangles the ID into a truncated and useless value.
2683 if (get_numeric_property(zhp
, prop
, src
, &source
, &val
) != 0)
2685 (void) snprintf(propbuf
, proplen
, "%llu", (u_longlong_t
)val
);
2689 switch (zfs_prop_get_type(prop
)) {
2690 case PROP_TYPE_NUMBER
:
2691 if (get_numeric_property(zhp
, prop
, src
,
2692 &source
, &val
) != 0)
2695 (void) snprintf(propbuf
, proplen
, "%llu",
2698 zfs_nicenum(val
, propbuf
, proplen
);
2701 case PROP_TYPE_STRING
:
2702 str
= getprop_string(zhp
, prop
, &source
);
2705 (void) strlcpy(propbuf
, str
, proplen
);
2708 case PROP_TYPE_INDEX
:
2709 if (get_numeric_property(zhp
, prop
, src
,
2710 &source
, &val
) != 0)
2712 if (zfs_prop_index_to_string(prop
, val
, &strval
) != 0)
2714 (void) strlcpy(propbuf
, strval
, proplen
);
2722 get_source(zhp
, src
, source
, statbuf
, statlen
);
2728 * Utility function to get the given numeric property. Does no validation that
2729 * the given property is the appropriate type; should only be used with
2730 * hard-coded property types.
2733 zfs_prop_get_int(zfs_handle_t
*zhp
, zfs_prop_t prop
)
2738 (void) get_numeric_property(zhp
, prop
, NULL
, &source
, &val
);
2744 zfs_prop_set_int(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t val
)
2748 (void) snprintf(buf
, sizeof (buf
), "%llu", (longlong_t
)val
);
2749 return (zfs_prop_set(zhp
, zfs_prop_to_name(prop
), buf
));
2753 * Similar to zfs_prop_get(), but returns the value as an integer.
2756 zfs_prop_get_numeric(zfs_handle_t
*zhp
, zfs_prop_t prop
, uint64_t *value
,
2757 zprop_source_t
*src
, char *statbuf
, size_t statlen
)
2762 * Check to see if this property applies to our object
2764 if (!zfs_prop_valid_for_type(prop
, zhp
->zfs_type
, B_FALSE
)) {
2765 return (zfs_error_fmt(zhp
->zfs_hdl
, EZFS_PROPTYPE
,
2766 dgettext(TEXT_DOMAIN
, "cannot get property '%s'"),
2767 zfs_prop_to_name(prop
)));
2771 *src
= ZPROP_SRC_NONE
;
2773 if (get_numeric_property(zhp
, prop
, src
, &source
, value
) != 0)
2776 get_source(zhp
, src
, source
, statbuf
, statlen
);
2783 idmap_id_to_numeric_domain_rid(uid_t id
, boolean_t isuser
,
2784 char **domainp
, idmap_rid_t
*ridp
)
2786 idmap_get_handle_t
*get_hdl
= NULL
;
2790 if (idmap_get_create(&get_hdl
) != IDMAP_SUCCESS
)
2794 err
= idmap_get_sidbyuid(get_hdl
, id
,
2795 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2797 err
= idmap_get_sidbygid(get_hdl
, id
,
2798 IDMAP_REQ_FLG_USE_CACHE
, domainp
, ridp
, &status
);
2800 if (err
== IDMAP_SUCCESS
&&
2801 idmap_get_mappings(get_hdl
) == IDMAP_SUCCESS
&&
2802 status
== IDMAP_SUCCESS
)
2808 idmap_get_destroy(get_hdl
);
2811 #endif /* HAVE_IDMAP */
2814 * convert the propname into parameters needed by kernel
2815 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2816 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2817 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2818 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2821 userquota_propname_decode(const char *propname
, boolean_t zoned
,
2822 zfs_userquota_prop_t
*typep
, char *domain
, int domainlen
, uint64_t *ridp
)
2824 zfs_userquota_prop_t type
;
2833 /* Figure out the property type ({user|group}{quota|space}) */
2834 for (type
= 0; type
< ZFS_NUM_USERQUOTA_PROPS
; type
++) {
2835 if (strncmp(propname
, zfs_userquota_prop_prefixes
[type
],
2836 strlen(zfs_userquota_prop_prefixes
[type
])) == 0)
2839 if (type
== ZFS_NUM_USERQUOTA_PROPS
)
2843 isuser
= (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_USERUSED
||
2844 type
== ZFS_PROP_USEROBJQUOTA
||
2845 type
== ZFS_PROP_USEROBJUSED
);
2846 isgroup
= (type
== ZFS_PROP_GROUPQUOTA
|| type
== ZFS_PROP_GROUPUSED
||
2847 type
== ZFS_PROP_GROUPOBJQUOTA
||
2848 type
== ZFS_PROP_GROUPOBJUSED
);
2850 cp
= strchr(propname
, '@') + 1;
2852 if (isuser
&& (pw
= getpwnam(cp
)) != NULL
) {
2853 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2856 } else if (isgroup
&& (gr
= getgrnam(cp
)) != NULL
) {
2857 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2860 } else if (strchr(cp
, '@')) {
2863 * It's a SID name (eg "user@domain") that needs to be
2864 * turned into S-1-domainID-RID.
2866 directory_error_t e
;
2867 char *numericsid
= NULL
;
2870 if (zoned
&& getzoneid() == GLOBAL_ZONEID
)
2873 e
= directory_sid_from_user_name(NULL
,
2876 e
= directory_sid_from_group_name(NULL
,
2880 directory_error_free(e
);
2883 if (numericsid
== NULL
)
2886 (void) strlcpy(domain
, cp
, domainlen
);
2887 cp
= strrchr(domain
, '-');
2892 *ridp
= strtoull(cp
, &end
, 10);
2895 if (errno
!= 0 || *end
!= '\0')
2899 #endif /* HAVE_IDMAP */
2901 /* It's a user/group ID (eg "12345"). */
2904 id
= strtoul(cp
, &end
, 10);
2909 /* It's an ephemeral ID. */
2913 if (idmap_id_to_numeric_domain_rid(id
, isuser
,
2914 &mapdomain
, &rid
) != 0)
2916 (void) strlcpy(domain
, mapdomain
, domainlen
);
2920 #endif /* HAVE_IDMAP */
2930 zfs_prop_get_userquota_common(zfs_handle_t
*zhp
, const char *propname
,
2931 uint64_t *propvalue
, zfs_userquota_prop_t
*typep
)
2934 zfs_cmd_t zc
= {"\0"};
2936 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
2938 err
= userquota_propname_decode(propname
,
2939 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
),
2940 typep
, zc
.zc_value
, sizeof (zc
.zc_value
), &zc
.zc_guid
);
2941 zc
.zc_objset_type
= *typep
;
2945 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_USERSPACE_ONE
, &zc
);
2949 *propvalue
= zc
.zc_cookie
;
2954 zfs_prop_get_userquota_int(zfs_handle_t
*zhp
, const char *propname
,
2955 uint64_t *propvalue
)
2957 zfs_userquota_prop_t type
;
2959 return (zfs_prop_get_userquota_common(zhp
, propname
, propvalue
,
2964 zfs_prop_get_userquota(zfs_handle_t
*zhp
, const char *propname
,
2965 char *propbuf
, int proplen
, boolean_t literal
)
2969 zfs_userquota_prop_t type
;
2971 err
= zfs_prop_get_userquota_common(zhp
, propname
, &propvalue
,
2978 (void) snprintf(propbuf
, proplen
, "%llu",
2979 (u_longlong_t
)propvalue
);
2980 } else if (propvalue
== 0 &&
2981 (type
== ZFS_PROP_USERQUOTA
|| type
== ZFS_PROP_GROUPQUOTA
||
2982 type
== ZFS_PROP_USEROBJQUOTA
|| type
== ZFS_PROP_GROUPOBJQUOTA
)) {
2983 (void) strlcpy(propbuf
, "none", proplen
);
2985 zfs_nicenum(propvalue
, propbuf
, proplen
);
2991 zfs_prop_get_written_int(zfs_handle_t
*zhp
, const char *propname
,
2992 uint64_t *propvalue
)
2995 zfs_cmd_t zc
= {"\0"};
2996 const char *snapname
;
2998 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3000 snapname
= strchr(propname
, '@') + 1;
3001 if (strchr(snapname
, '@')) {
3002 (void) strlcpy(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3004 /* snapname is the short name, append it to zhp's fsname */
3007 (void) strlcpy(zc
.zc_value
, zhp
->zfs_name
,
3008 sizeof (zc
.zc_value
));
3009 cp
= strchr(zc
.zc_value
, '@');
3012 (void) strlcat(zc
.zc_value
, "@", sizeof (zc
.zc_value
));
3013 (void) strlcat(zc
.zc_value
, snapname
, sizeof (zc
.zc_value
));
3016 err
= ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SPACE_WRITTEN
, &zc
);
3020 *propvalue
= zc
.zc_cookie
;
3025 zfs_prop_get_written(zfs_handle_t
*zhp
, const char *propname
,
3026 char *propbuf
, int proplen
, boolean_t literal
)
3031 err
= zfs_prop_get_written_int(zhp
, propname
, &propvalue
);
3037 (void) snprintf(propbuf
, proplen
, "%llu",
3038 (u_longlong_t
)propvalue
);
3040 zfs_nicenum(propvalue
, propbuf
, proplen
);
3047 * Returns the name of the given zfs handle.
3050 zfs_get_name(const zfs_handle_t
*zhp
)
3052 return (zhp
->zfs_name
);
3056 * Returns the name of the parent pool for the given zfs handle.
3059 zfs_get_pool_name(const zfs_handle_t
*zhp
)
3061 return (zhp
->zpool_hdl
->zpool_name
);
3065 * Returns the type of the given zfs handle.
3068 zfs_get_type(const zfs_handle_t
*zhp
)
3070 return (zhp
->zfs_type
);
3074 * Is one dataset name a child dataset of another?
3076 * Needs to handle these cases:
3077 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3078 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3079 * Descendant? No. No. No. Yes.
3082 is_descendant(const char *ds1
, const char *ds2
)
3084 size_t d1len
= strlen(ds1
);
3086 /* ds2 can't be a descendant if it's smaller */
3087 if (strlen(ds2
) < d1len
)
3090 /* otherwise, compare strings and verify that there's a '/' char */
3091 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3095 * Given a complete name, return just the portion that refers to the parent.
3096 * Will return -1 if there is no parent (path is just the name of the
3100 parent_name(const char *path
, char *buf
, size_t buflen
)
3104 (void) strlcpy(buf
, path
, buflen
);
3106 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3114 * If accept_ancestor is false, then check to make sure that the given path has
3115 * a parent, and that it exists. If accept_ancestor is true, then find the
3116 * closest existing ancestor for the given path. In prefixlen return the
3117 * length of already existing prefix of the given path. We also fetch the
3118 * 'zoned' property, which is used to validate property settings when creating
3122 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3123 boolean_t accept_ancestor
, int *prefixlen
)
3125 zfs_cmd_t zc
= {"\0"};
3126 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3132 (void) snprintf(errbuf
, sizeof (errbuf
),
3133 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3135 /* get parent, and check to see if this is just a pool */
3136 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3137 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3138 "missing dataset name"));
3139 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3142 /* check to see if the pool exists */
3143 if ((slash
= strchr(parent
, '/')) == NULL
)
3144 slash
= parent
+ strlen(parent
);
3145 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3146 zc
.zc_name
[slash
- parent
] = '\0';
3147 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3149 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3150 "no such pool '%s'"), zc
.zc_name
);
3151 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3154 /* check to see if the parent dataset exists */
3155 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3156 if (errno
== ENOENT
&& accept_ancestor
) {
3158 * Go deeper to find an ancestor, give up on top level.
3160 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3162 "no such pool '%s'"), zc
.zc_name
);
3163 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3165 } else if (errno
== ENOENT
) {
3166 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3167 "parent does not exist"));
3168 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3170 return (zfs_standard_error(hdl
, errno
, errbuf
));
3173 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3177 /* we are in a non-global zone, but parent is in the global zone */
3178 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3179 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3184 /* make sure parent is a filesystem */
3185 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3187 "parent is not a filesystem"));
3188 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3194 if (prefixlen
!= NULL
)
3195 *prefixlen
= strlen(parent
);
3200 * Finds whether the dataset of the given type(s) exists.
3203 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3207 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3211 * Try to get stats for the dataset, which will tell us if it exists.
3213 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3214 int ds_type
= zhp
->zfs_type
;
3217 if (types
& ds_type
)
3224 * Given a path to 'target', create all the ancestors between
3225 * the prefixlen portion of the path, and the target itself.
3226 * Fail if the initial prefixlen-ancestor does not already exist.
3229 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3235 /* make sure prefix exists */
3236 cp
= target
+ prefixlen
;
3238 assert(strchr(cp
, '/') == NULL
);
3239 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3242 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3250 * Attempt to create, mount, and share any ancestor filesystems,
3251 * up to the prefixlen-long one.
3253 for (cp
= target
+ prefixlen
+ 1;
3254 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
3258 h
= make_dataset_handle(hdl
, target
);
3260 /* it already exists, nothing to do here */
3265 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3267 opname
= dgettext(TEXT_DOMAIN
, "create");
3271 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3273 opname
= dgettext(TEXT_DOMAIN
, "open");
3277 if (zfs_mount(h
, NULL
, 0) != 0) {
3278 opname
= dgettext(TEXT_DOMAIN
, "mount");
3282 if (zfs_share(h
) != 0) {
3283 opname
= dgettext(TEXT_DOMAIN
, "share");
3293 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3294 "failed to %s ancestor '%s'"), opname
, target
);
3299 * Creates non-existing ancestors of the given path.
3302 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3308 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3311 if ((path_copy
= strdup(path
)) != NULL
) {
3312 rc
= create_parents(hdl
, path_copy
, prefix
);
3315 if (path_copy
== NULL
|| rc
!= 0)
3322 * Create a new filesystem or volume.
3325 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3330 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3333 enum lzc_dataset_type ost
;
3335 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3336 "cannot create '%s'"), path
);
3338 /* validate the path, taking care to note the extended error message */
3339 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3340 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3342 /* validate parents exist */
3343 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3347 * The failure modes when creating a dataset of a different type over
3348 * one that already exists is a little strange. In particular, if you
3349 * try to create a dataset on top of an existing dataset, the ioctl()
3350 * will return ENOENT, not EEXIST. To prevent this from happening, we
3351 * first try to see if the dataset exists.
3353 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3354 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3355 "dataset already exists"));
3356 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3359 if (type
== ZFS_TYPE_VOLUME
)
3360 ost
= LZC_DATSET_TYPE_ZVOL
;
3362 ost
= LZC_DATSET_TYPE_ZFS
;
3364 /* open zpool handle for prop validation */
3365 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3366 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3368 /* truncate pool_path at first slash */
3369 char *p
= strchr(pool_path
, '/');
3373 zpool_handle_t
*zpool_handle
= zpool_open(hdl
, pool_path
);
3375 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3376 zoned
, NULL
, zpool_handle
, errbuf
)) == 0) {
3377 zpool_close(zpool_handle
);
3380 zpool_close(zpool_handle
);
3382 if (type
== ZFS_TYPE_VOLUME
) {
3384 * If we are creating a volume, the size and block size must
3385 * satisfy a few restraints. First, the blocksize must be a
3386 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3387 * volsize must be a multiple of the block size, and cannot be
3390 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3391 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3393 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3394 "missing volume size"));
3395 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3398 if ((ret
= nvlist_lookup_uint64(props
,
3399 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3400 &blocksize
)) != 0) {
3401 if (ret
== ENOENT
) {
3402 blocksize
= zfs_prop_default_numeric(
3403 ZFS_PROP_VOLBLOCKSIZE
);
3406 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3407 "missing volume block size"));
3408 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3414 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3415 "volume size cannot be zero"));
3416 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3419 if (size
% blocksize
!= 0) {
3421 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3422 "volume size must be a multiple of volume block "
3424 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3428 /* create the dataset */
3429 ret
= lzc_create(path
, ost
, props
);
3432 /* check for failure */
3434 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3435 (void) parent_name(path
, parent
, sizeof (parent
));
3439 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3440 "no such parent '%s'"), parent
);
3441 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3444 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3445 "parent '%s' is not a filesystem"), parent
);
3446 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3449 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3450 "pool must be upgraded to set this "
3451 "property or value"));
3452 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3456 * This platform can't address a volume this big.
3458 if (type
== ZFS_TYPE_VOLUME
)
3459 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3464 return (zfs_standard_error(hdl
, errno
, errbuf
));
3472 * Destroys the given dataset. The caller must make sure that the filesystem
3473 * isn't mounted, and that there are no active dependents. If the file system
3474 * does not exist this function does nothing.
3477 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3479 zfs_cmd_t zc
= {"\0"};
3481 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3482 nvlist_t
*nv
= fnvlist_alloc();
3483 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3484 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3487 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3488 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3494 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3496 if (ZFS_IS_VOLUME(zhp
)) {
3497 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3499 zc
.zc_objset_type
= DMU_OST_ZFS
;
3502 zc
.zc_defer_destroy
= defer
;
3503 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3505 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3506 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3510 remove_mountpoint(zhp
);
3515 struct destroydata
{
3517 const char *snapname
;
3521 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3523 struct destroydata
*dd
= arg
;
3524 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3527 (void) snprintf(name
, sizeof (name
),
3528 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3530 if (lzc_exists(name
))
3531 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3533 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3539 * Destroys all snapshots with the given name in zhp & descendants.
3542 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3545 struct destroydata dd
= { 0 };
3547 dd
.snapname
= snapname
;
3548 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3549 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3551 if (nvlist_empty(dd
.nvl
)) {
3552 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3553 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3554 zhp
->zfs_name
, snapname
);
3556 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3558 nvlist_free(dd
.nvl
);
3563 * Destroys all the snapshots named in the nvlist.
3566 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3569 nvlist_t
*errlist
= NULL
;
3572 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3575 nvlist_free(errlist
);
3579 if (nvlist_empty(errlist
)) {
3581 (void) snprintf(errbuf
, sizeof (errbuf
),
3582 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3584 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3586 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3587 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3589 (void) snprintf(errbuf
, sizeof (errbuf
),
3590 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3593 switch (fnvpair_value_int32(pair
)) {
3596 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3597 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3600 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3605 nvlist_free(errlist
);
3610 * Clones the given dataset. The target must be of the same type as the source.
3613 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3615 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3618 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3621 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3623 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3624 "cannot create '%s'"), target
);
3626 /* validate the target/clone name */
3627 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3628 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3630 /* validate parents exist */
3631 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3634 (void) parent_name(target
, parent
, sizeof (parent
));
3640 if (ZFS_IS_VOLUME(zhp
)) {
3641 type
= ZFS_TYPE_VOLUME
;
3643 type
= ZFS_TYPE_FILESYSTEM
;
3645 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3646 zhp
, zhp
->zpool_hdl
, errbuf
)) == NULL
)
3650 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3658 * The parent doesn't exist. We should have caught this
3659 * above, but there may a race condition that has since
3660 * destroyed the parent.
3662 * At this point, we don't know whether it's the source
3663 * that doesn't exist anymore, or whether the target
3664 * dataset doesn't exist.
3666 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3667 "no such parent '%s'"), parent
);
3668 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3671 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3672 "source and target pools differ"));
3673 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3677 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3686 * Promotes the given clone fs to be the clone parent.
3689 zfs_promote(zfs_handle_t
*zhp
)
3691 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3692 zfs_cmd_t zc
= {"\0"};
3693 char parent
[MAXPATHLEN
];
3697 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3698 "cannot promote '%s'"), zhp
->zfs_name
);
3700 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3701 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3702 "snapshots can not be promoted"));
3703 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3706 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3707 if (parent
[0] == '\0') {
3708 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3709 "not a cloned filesystem"));
3710 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3713 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3714 sizeof (zc
.zc_value
));
3715 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3716 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3719 int save_errno
= errno
;
3721 switch (save_errno
) {
3723 /* There is a conflicting snapshot name. */
3724 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3725 "conflicting snapshot '%s' from parent '%s'"),
3726 zc
.zc_string
, parent
);
3727 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3730 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3736 typedef struct snapdata
{
3738 const char *sd_snapname
;
3742 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3744 snapdata_t
*sd
= arg
;
3745 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3748 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3749 (void) snprintf(name
, sizeof (name
),
3750 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3752 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3754 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3762 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3766 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3772 zpool_handle_t
*zpool_hdl
;
3773 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
3775 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3776 "cannot create snapshots "));
3779 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3780 const char *snapname
= nvpair_name(elem
);
3782 /* validate the target name */
3783 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3785 (void) snprintf(errbuf
, sizeof (errbuf
),
3786 dgettext(TEXT_DOMAIN
,
3787 "cannot create snapshot '%s'"), snapname
);
3788 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3793 * get pool handle for prop validation. assumes all snaps are in the
3794 * same pool, as does lzc_snapshot (below).
3796 elem
= nvlist_next_nvpair(snaps
, NULL
);
3797 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
3798 pool
[strcspn(pool
, "/@")] = '\0';
3799 zpool_hdl
= zpool_open(hdl
, pool
);
3800 if (zpool_hdl
== NULL
)
3803 if (props
!= NULL
&&
3804 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3805 props
, B_FALSE
, NULL
, zpool_hdl
, errbuf
)) == NULL
) {
3806 zpool_close(zpool_hdl
);
3809 zpool_close(zpool_hdl
);
3811 ret
= lzc_snapshot(snaps
, props
, &errors
);
3814 boolean_t printed
= B_FALSE
;
3815 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3817 elem
= nvlist_next_nvpair(errors
, elem
)) {
3818 (void) snprintf(errbuf
, sizeof (errbuf
),
3819 dgettext(TEXT_DOMAIN
,
3820 "cannot create snapshot '%s'"), nvpair_name(elem
));
3821 (void) zfs_standard_error(hdl
,
3822 fnvpair_value_int32(elem
), errbuf
);
3828 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3829 "multiple snapshots of same "
3831 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3835 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3841 nvlist_free(errors
);
3846 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3850 snapdata_t sd
= { 0 };
3851 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
3856 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3857 "cannot snapshot %s"), path
);
3859 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3860 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3862 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3863 cp
= strchr(fsname
, '@');
3865 sd
.sd_snapname
= cp
+ 1;
3867 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3868 ZFS_TYPE_VOLUME
)) == NULL
) {
3872 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3874 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3876 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3879 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3880 nvlist_free(sd
.sd_nvl
);
3886 * Destroy any more recent snapshots. We invoke this callback on any dependents
3887 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3888 * is a dependent and we should just destroy it without checking the transaction
3891 typedef struct rollback_data
{
3892 const char *cb_target
; /* the snapshot */
3893 uint64_t cb_create
; /* creation time reference */
3899 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3901 rollback_data_t
*cbp
= data
;
3902 prop_changelist_t
*clp
;
3904 /* We must destroy this clone; first unmount it */
3905 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3906 cbp
->cb_force
? MS_FORCE
: 0);
3907 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3908 cbp
->cb_error
= B_TRUE
;
3912 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3913 cbp
->cb_error
= B_TRUE
;
3915 changelist_remove(clp
, zhp
->zfs_name
);
3916 (void) changelist_postfix(clp
);
3917 changelist_free(clp
);
3924 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3926 rollback_data_t
*cbp
= data
;
3928 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3929 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3930 rollback_destroy_dependent
, cbp
);
3932 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3940 * Given a dataset, rollback to a specific snapshot, discarding any
3941 * data changes since then and making it the active dataset.
3943 * Any snapshots and bookmarks more recent than the target are
3944 * destroyed, along with their dependents (i.e. clones).
3947 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3949 rollback_data_t cb
= { 0 };
3951 boolean_t restore_resv
= 0;
3952 uint64_t old_volsize
= 0, new_volsize
;
3953 zfs_prop_t resv_prop
= { 0 };
3955 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3956 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3959 * Destroy all recent snapshots and their dependents.
3961 cb
.cb_force
= force
;
3962 cb
.cb_target
= snap
->zfs_name
;
3963 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3964 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3965 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3971 * Now that we have verified that the snapshot is the latest,
3972 * rollback to the given snapshot.
3975 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3976 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3978 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3980 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3984 * We rely on zfs_iter_children() to verify that there are no
3985 * newer snapshots for the given dataset. Therefore, we can
3986 * simply pass the name on to the ioctl() call. There is still
3987 * an unlikely race condition where the user has taken a
3988 * snapshot since we verified that this was the most recent.
3990 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3992 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3993 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3999 * For volumes, if the pre-rollback volsize matched the pre-
4000 * rollback reservation and the volsize has changed then set
4001 * the reservation property to the post-rollback volsize.
4002 * Make a new handle since the rollback closed the dataset.
4004 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
4005 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
4007 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
4008 if (old_volsize
!= new_volsize
)
4009 err
= zfs_prop_set_int(zhp
, resv_prop
,
4018 * Renames the given dataset.
4021 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4022 boolean_t force_unmount
)
4025 zfs_cmd_t zc
= {"\0"};
4027 prop_changelist_t
*cl
= NULL
;
4028 zfs_handle_t
*zhrp
= NULL
;
4029 char *parentname
= NULL
;
4030 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4031 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4034 /* if we have the same exact name, just return success */
4035 if (strcmp(zhp
->zfs_name
, target
) == 0)
4038 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4039 "cannot rename to '%s'"), target
);
4042 * Make sure the target name is valid
4044 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4045 if ((strchr(target
, '@') == NULL
) ||
4048 * Snapshot target name is abbreviated,
4049 * reconstruct full dataset name
4051 (void) strlcpy(parent
, zhp
->zfs_name
,
4053 delim
= strchr(parent
, '@');
4054 if (strchr(target
, '@') == NULL
)
4058 (void) strlcat(parent
, target
, sizeof (parent
));
4062 * Make sure we're renaming within the same dataset.
4064 delim
= strchr(target
, '@');
4065 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4066 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4067 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4068 "snapshots must be part of same "
4070 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4074 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4075 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4078 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4079 "recursive rename must be a snapshot"));
4080 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4083 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4084 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4086 /* validate parents */
4087 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4090 /* make sure we're in the same pool */
4091 verify((delim
= strchr(target
, '/')) != NULL
);
4092 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4093 zhp
->zfs_name
[delim
- target
] != '/') {
4094 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4095 "datasets must be within same pool"));
4096 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4099 /* new name cannot be a child of the current dataset name */
4100 if (is_descendant(zhp
->zfs_name
, target
)) {
4101 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4102 "New dataset name cannot be a descendant of "
4103 "current dataset name"));
4104 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4108 (void) snprintf(errbuf
, sizeof (errbuf
),
4109 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4111 if (getzoneid() == GLOBAL_ZONEID
&&
4112 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4114 "dataset is used in a non-global zone"));
4115 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4119 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4120 if (parentname
== NULL
) {
4124 delim
= strchr(parentname
, '@');
4126 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4131 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4132 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4133 force_unmount
? MS_FORCE
: 0)) == NULL
)
4136 if (changelist_haszonedchild(cl
)) {
4137 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4138 "child dataset with inherited mountpoint is used "
4139 "in a non-global zone"));
4140 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4145 if ((ret
= changelist_prefix(cl
)) != 0)
4149 if (ZFS_IS_VOLUME(zhp
))
4150 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4152 zc
.zc_objset_type
= DMU_OST_ZFS
;
4154 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4155 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4157 zc
.zc_cookie
= recursive
;
4159 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4161 * if it was recursive, the one that actually failed will
4164 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4165 "cannot rename '%s'"), zc
.zc_name
);
4167 if (recursive
&& errno
== EEXIST
) {
4168 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4169 "a child dataset already has a snapshot "
4170 "with the new name"));
4171 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4173 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4177 * On failure, we still want to remount any filesystems that
4178 * were previously mounted, so we don't alter the system state.
4181 (void) changelist_postfix(cl
);
4184 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4185 ret
= changelist_postfix(cl
);
4190 if (parentname
!= NULL
) {
4197 changelist_free(cl
);
4203 zfs_get_user_props(zfs_handle_t
*zhp
)
4205 return (zhp
->zfs_user_props
);
4209 * This function is used by 'zfs list' to determine the exact set of columns to
4210 * display, and their maximum widths. This does two main things:
4212 * - If this is a list of all properties, then expand the list to include
4213 * all native properties, and set a flag so that for each dataset we look
4214 * for new unique user properties and add them to the list.
4216 * - For non fixed-width properties, keep track of the maximum width seen
4217 * so that we can size the column appropriately. If the user has
4218 * requested received property values, we also need to compute the width
4219 * of the RECEIVED column.
4222 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4225 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4226 zprop_list_t
*entry
;
4227 zprop_list_t
**last
, **start
;
4228 nvlist_t
*userprops
, *propval
;
4231 char buf
[ZFS_MAXPROPLEN
];
4233 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4236 userprops
= zfs_get_user_props(zhp
);
4239 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4241 * Go through and add any user properties as necessary. We
4242 * start by incrementing our list pointer to the first
4243 * non-native property.
4246 while (*start
!= NULL
) {
4247 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4249 start
= &(*start
)->pl_next
;
4253 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4255 * See if we've already found this property in our list.
4257 for (last
= start
; *last
!= NULL
;
4258 last
= &(*last
)->pl_next
) {
4259 if (strcmp((*last
)->pl_user_prop
,
4260 nvpair_name(elem
)) == 0)
4264 if (*last
== NULL
) {
4265 if ((entry
= zfs_alloc(hdl
,
4266 sizeof (zprop_list_t
))) == NULL
||
4267 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4268 nvpair_name(elem
)))) == NULL
) {
4273 entry
->pl_prop
= ZPROP_INVAL
;
4274 entry
->pl_width
= strlen(nvpair_name(elem
));
4275 entry
->pl_all
= B_TRUE
;
4282 * Now go through and check the width of any non-fixed columns
4284 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4285 if (entry
->pl_fixed
&& !literal
)
4288 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4289 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4290 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4291 if (strlen(buf
) > entry
->pl_width
)
4292 entry
->pl_width
= strlen(buf
);
4294 if (received
&& zfs_prop_get_recvd(zhp
,
4295 zfs_prop_to_name(entry
->pl_prop
),
4296 buf
, sizeof (buf
), literal
) == 0)
4297 if (strlen(buf
) > entry
->pl_recvd_width
)
4298 entry
->pl_recvd_width
= strlen(buf
);
4300 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4302 verify(nvlist_lookup_string(propval
,
4303 ZPROP_VALUE
, &strval
) == 0);
4304 if (strlen(strval
) > entry
->pl_width
)
4305 entry
->pl_width
= strlen(strval
);
4307 if (received
&& zfs_prop_get_recvd(zhp
,
4308 entry
->pl_user_prop
,
4309 buf
, sizeof (buf
), literal
) == 0)
4310 if (strlen(buf
) > entry
->pl_recvd_width
)
4311 entry
->pl_recvd_width
= strlen(buf
);
4319 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4325 * Keep a reference to the props-table against which we prune the
4328 zhp
->zfs_props_table
= props
;
4330 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4333 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4334 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4337 * User properties will result in ZPROP_INVAL, and since we
4338 * only know how to prune standard ZFS properties, we always
4339 * leave these in the list. This can also happen if we
4340 * encounter an unknown DSL property (when running older
4341 * software, for example).
4343 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4344 (void) nvlist_remove(zhp
->zfs_props
,
4345 nvpair_name(curr
), nvpair_type(curr
));
4351 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4352 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4354 zfs_cmd_t zc
= {"\0"};
4355 nvlist_t
*nvlist
= NULL
;
4358 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4359 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4360 zc
.zc_cookie
= (uint64_t)cmd
;
4362 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4363 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4364 (void) no_memory(hdl
);
4370 case ZFS_SMB_ACL_ADD
:
4371 case ZFS_SMB_ACL_REMOVE
:
4372 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4374 case ZFS_SMB_ACL_RENAME
:
4375 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4377 (void) no_memory(hdl
);
4380 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4382 (void) no_memory(hdl
);
4385 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4386 nvlist_free(nvlist
);
4390 case ZFS_SMB_ACL_PURGE
:
4395 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4396 nvlist_free(nvlist
);
4401 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4402 char *path
, char *resource
)
4404 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4409 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4410 char *path
, char *resource
)
4412 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4417 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4419 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4424 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4425 char *oldname
, char *newname
)
4427 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4432 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4433 zfs_userspace_cb_t func
, void *arg
)
4435 zfs_cmd_t zc
= {"\0"};
4436 zfs_useracct_t buf
[100];
4437 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4440 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4442 zc
.zc_objset_type
= type
;
4443 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4446 zfs_useracct_t
*zua
= buf
;
4448 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4449 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4452 if ((errno
== ENOTSUP
&&
4453 (type
== ZFS_PROP_USEROBJUSED
||
4454 type
== ZFS_PROP_GROUPOBJUSED
||
4455 type
== ZFS_PROP_USEROBJQUOTA
||
4456 type
== ZFS_PROP_GROUPOBJQUOTA
)))
4459 (void) snprintf(errbuf
, sizeof (errbuf
),
4460 dgettext(TEXT_DOMAIN
,
4461 "cannot get used/quota for %s"), zc
.zc_name
);
4462 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4464 if (zc
.zc_nvlist_dst_size
== 0)
4467 while (zc
.zc_nvlist_dst_size
> 0) {
4468 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4469 zua
->zu_space
)) != 0)
4472 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4481 const char *snapname
;
4483 boolean_t recursive
;
4488 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4490 struct holdarg
*ha
= arg
;
4491 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4494 (void) snprintf(name
, sizeof (name
),
4495 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4497 if (lzc_exists(name
))
4498 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4501 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4507 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4508 boolean_t recursive
, int cleanup_fd
)
4513 ha
.nvl
= fnvlist_alloc();
4514 ha
.snapname
= snapname
;
4516 ha
.recursive
= recursive
;
4517 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4519 if (nvlist_empty(ha
.nvl
)) {
4522 fnvlist_free(ha
.nvl
);
4524 (void) snprintf(errbuf
, sizeof (errbuf
),
4525 dgettext(TEXT_DOMAIN
,
4526 "cannot hold snapshot '%s@%s'"),
4527 zhp
->zfs_name
, snapname
);
4528 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4532 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4533 fnvlist_free(ha
.nvl
);
4539 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4543 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4548 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4551 /* There may be errors even in the success case. */
4552 fnvlist_free(errors
);
4556 if (nvlist_empty(errors
)) {
4557 /* no hold-specific errors */
4558 (void) snprintf(errbuf
, sizeof (errbuf
),
4559 dgettext(TEXT_DOMAIN
, "cannot hold"));
4562 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4563 "pool must be upgraded"));
4564 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4567 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4570 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4574 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4576 elem
= nvlist_next_nvpair(errors
, elem
)) {
4577 (void) snprintf(errbuf
, sizeof (errbuf
),
4578 dgettext(TEXT_DOMAIN
,
4579 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4580 switch (fnvpair_value_int32(elem
)) {
4583 * Temporary tags wind up having the ds object id
4584 * prepended. So even if we passed the length check
4585 * above, it's still possible for the tag to wind
4586 * up being slightly too long.
4588 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4591 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4594 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4597 (void) zfs_standard_error(hdl
,
4598 fnvpair_value_int32(elem
), errbuf
);
4602 fnvlist_free(errors
);
4607 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4609 struct holdarg
*ha
= arg
;
4610 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4612 nvlist_t
*existing_holds
;
4614 (void) snprintf(name
, sizeof (name
),
4615 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4617 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4619 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4622 nvlist_t
*torelease
= fnvlist_alloc();
4623 fnvlist_add_boolean(torelease
, ha
->tag
);
4624 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4625 fnvlist_free(torelease
);
4629 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4635 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4636 boolean_t recursive
)
4640 nvlist_t
*errors
= NULL
;
4642 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4645 ha
.nvl
= fnvlist_alloc();
4646 ha
.snapname
= snapname
;
4648 ha
.recursive
= recursive
;
4650 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4652 if (nvlist_empty(ha
.nvl
)) {
4653 fnvlist_free(ha
.nvl
);
4655 (void) snprintf(errbuf
, sizeof (errbuf
),
4656 dgettext(TEXT_DOMAIN
,
4657 "cannot release hold from snapshot '%s@%s'"),
4658 zhp
->zfs_name
, snapname
);
4660 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4662 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4667 ret
= lzc_release(ha
.nvl
, &errors
);
4668 fnvlist_free(ha
.nvl
);
4671 /* There may be errors even in the success case. */
4672 fnvlist_free(errors
);
4676 if (nvlist_empty(errors
)) {
4677 /* no hold-specific errors */
4678 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4682 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4683 "pool must be upgraded"));
4684 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4687 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4691 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4693 elem
= nvlist_next_nvpair(errors
, elem
)) {
4694 (void) snprintf(errbuf
, sizeof (errbuf
),
4695 dgettext(TEXT_DOMAIN
,
4696 "cannot release hold from snapshot '%s'"),
4698 switch (fnvpair_value_int32(elem
)) {
4700 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4703 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4706 (void) zfs_standard_error_fmt(hdl
,
4707 fnvpair_value_int32(elem
), errbuf
);
4711 fnvlist_free(errors
);
4716 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4718 zfs_cmd_t zc
= {"\0"};
4719 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4725 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4726 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4730 nvbuf
= malloc(nvsz
);
4731 if (nvbuf
== NULL
) {
4732 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4736 zc
.zc_nvlist_dst_size
= nvsz
;
4737 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4739 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4741 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4742 (void) snprintf(errbuf
, sizeof (errbuf
),
4743 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4748 nvsz
= zc
.zc_nvlist_dst_size
;
4752 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4753 "pool must be upgraded"));
4754 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4757 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4760 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4763 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4768 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4770 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4771 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4773 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4783 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, 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
);
4795 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4798 nvbuf
= malloc(nvsz
);
4800 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4803 zc
.zc_nvlist_src_size
= nvsz
;
4804 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4805 zc
.zc_perm_action
= un
;
4807 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4809 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4810 (void) snprintf(errbuf
, sizeof (errbuf
),
4811 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4815 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4816 "pool must be upgraded"));
4817 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4820 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4823 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4826 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4837 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4842 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4845 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4847 (void) snprintf(errbuf
, sizeof (errbuf
),
4848 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4852 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4853 "pool must be upgraded"));
4854 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4857 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4860 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4863 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4872 * Convert the zvol's volume size to an appropriate reservation.
4873 * Note: If this routine is updated, it is necessary to update the ZFS test
4874 * suite's shell version in reservation.kshlib.
4877 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4880 uint64_t nblocks
, volblocksize
;
4884 if (nvlist_lookup_string(props
,
4885 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4886 ncopies
= atoi(strval
);
4889 if (nvlist_lookup_uint64(props
,
4890 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4891 &volblocksize
) != 0)
4892 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4893 nblocks
= volsize
/volblocksize
;
4894 /* start with metadnode L0-L6 */
4896 /* calculate number of indirects */
4897 while (nblocks
> 1) {
4898 nblocks
+= DNODES_PER_LEVEL
- 1;
4899 nblocks
/= DNODES_PER_LEVEL
;
4902 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4905 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4906 * compressed, but in practice they compress down to about
4909 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;