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 2013 Nexenta Systems, Inc. All rights reserved.
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 type of the given zfs handle.
3059 zfs_get_type(const zfs_handle_t
*zhp
)
3061 return (zhp
->zfs_type
);
3065 * Is one dataset name a child dataset of another?
3067 * Needs to handle these cases:
3068 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3069 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3070 * Descendant? No. No. No. Yes.
3073 is_descendant(const char *ds1
, const char *ds2
)
3075 size_t d1len
= strlen(ds1
);
3077 /* ds2 can't be a descendant if it's smaller */
3078 if (strlen(ds2
) < d1len
)
3081 /* otherwise, compare strings and verify that there's a '/' char */
3082 return (ds2
[d1len
] == '/' && (strncmp(ds1
, ds2
, d1len
) == 0));
3086 * Given a complete name, return just the portion that refers to the parent.
3087 * Will return -1 if there is no parent (path is just the name of the
3091 parent_name(const char *path
, char *buf
, size_t buflen
)
3095 (void) strlcpy(buf
, path
, buflen
);
3097 if ((slashp
= strrchr(buf
, '/')) == NULL
)
3105 * If accept_ancestor is false, then check to make sure that the given path has
3106 * a parent, and that it exists. If accept_ancestor is true, then find the
3107 * closest existing ancestor for the given path. In prefixlen return the
3108 * length of already existing prefix of the given path. We also fetch the
3109 * 'zoned' property, which is used to validate property settings when creating
3113 check_parents(libzfs_handle_t
*hdl
, const char *path
, uint64_t *zoned
,
3114 boolean_t accept_ancestor
, int *prefixlen
)
3116 zfs_cmd_t zc
= {"\0"};
3117 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3123 (void) snprintf(errbuf
, sizeof (errbuf
),
3124 dgettext(TEXT_DOMAIN
, "cannot create '%s'"), path
);
3126 /* get parent, and check to see if this is just a pool */
3127 if (parent_name(path
, parent
, sizeof (parent
)) != 0) {
3128 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3129 "missing dataset name"));
3130 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3133 /* check to see if the pool exists */
3134 if ((slash
= strchr(parent
, '/')) == NULL
)
3135 slash
= parent
+ strlen(parent
);
3136 (void) strncpy(zc
.zc_name
, parent
, slash
- parent
);
3137 zc
.zc_name
[slash
- parent
] = '\0';
3138 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0 &&
3140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3141 "no such pool '%s'"), zc
.zc_name
);
3142 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3145 /* check to see if the parent dataset exists */
3146 while ((zhp
= make_dataset_handle(hdl
, parent
)) == NULL
) {
3147 if (errno
== ENOENT
&& accept_ancestor
) {
3149 * Go deeper to find an ancestor, give up on top level.
3151 if (parent_name(parent
, parent
, sizeof (parent
)) != 0) {
3152 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3153 "no such pool '%s'"), zc
.zc_name
);
3154 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3156 } else if (errno
== ENOENT
) {
3157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3158 "parent does not exist"));
3159 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3161 return (zfs_standard_error(hdl
, errno
, errbuf
));
3164 is_zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
3168 /* we are in a non-global zone, but parent is in the global zone */
3169 if (getzoneid() != GLOBAL_ZONEID
&& !is_zoned
) {
3170 (void) zfs_standard_error(hdl
, EPERM
, errbuf
);
3175 /* make sure parent is a filesystem */
3176 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
3177 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3178 "parent is not a filesystem"));
3179 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
3185 if (prefixlen
!= NULL
)
3186 *prefixlen
= strlen(parent
);
3191 * Finds whether the dataset of the given type(s) exists.
3194 zfs_dataset_exists(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t types
)
3198 if (!zfs_validate_name(hdl
, path
, types
, B_FALSE
))
3202 * Try to get stats for the dataset, which will tell us if it exists.
3204 if ((zhp
= make_dataset_handle(hdl
, path
)) != NULL
) {
3205 int ds_type
= zhp
->zfs_type
;
3208 if (types
& ds_type
)
3215 * Given a path to 'target', create all the ancestors between
3216 * the prefixlen portion of the path, and the target itself.
3217 * Fail if the initial prefixlen-ancestor does not already exist.
3220 create_parents(libzfs_handle_t
*hdl
, char *target
, int prefixlen
)
3226 /* make sure prefix exists */
3227 cp
= target
+ prefixlen
;
3229 assert(strchr(cp
, '/') == NULL
);
3230 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3233 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3241 * Attempt to create, mount, and share any ancestor filesystems,
3242 * up to the prefixlen-long one.
3244 for (cp
= target
+ prefixlen
+ 1;
3245 (cp
= strchr(cp
, '/')); *cp
= '/', cp
++) {
3249 h
= make_dataset_handle(hdl
, target
);
3251 /* it already exists, nothing to do here */
3256 if (zfs_create(hdl
, target
, ZFS_TYPE_FILESYSTEM
,
3258 opname
= dgettext(TEXT_DOMAIN
, "create");
3262 h
= zfs_open(hdl
, target
, ZFS_TYPE_FILESYSTEM
);
3264 opname
= dgettext(TEXT_DOMAIN
, "open");
3268 if (zfs_mount(h
, NULL
, 0) != 0) {
3269 opname
= dgettext(TEXT_DOMAIN
, "mount");
3273 if (zfs_share(h
) != 0) {
3274 opname
= dgettext(TEXT_DOMAIN
, "share");
3284 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3285 "failed to %s ancestor '%s'"), opname
, target
);
3290 * Creates non-existing ancestors of the given path.
3293 zfs_create_ancestors(libzfs_handle_t
*hdl
, const char *path
)
3299 if (check_parents(hdl
, path
, NULL
, B_TRUE
, &prefix
) != 0)
3302 if ((path_copy
= strdup(path
)) != NULL
) {
3303 rc
= create_parents(hdl
, path_copy
, prefix
);
3306 if (path_copy
== NULL
|| rc
!= 0)
3313 * Create a new filesystem or volume.
3316 zfs_create(libzfs_handle_t
*hdl
, const char *path
, zfs_type_t type
,
3321 uint64_t blocksize
= zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE
);
3324 enum lzc_dataset_type ost
;
3326 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3327 "cannot create '%s'"), path
);
3329 /* validate the path, taking care to note the extended error message */
3330 if (!zfs_validate_name(hdl
, path
, type
, B_TRUE
))
3331 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3333 /* validate parents exist */
3334 if (check_parents(hdl
, path
, &zoned
, B_FALSE
, NULL
) != 0)
3338 * The failure modes when creating a dataset of a different type over
3339 * one that already exists is a little strange. In particular, if you
3340 * try to create a dataset on top of an existing dataset, the ioctl()
3341 * will return ENOENT, not EEXIST. To prevent this from happening, we
3342 * first try to see if the dataset exists.
3344 if (zfs_dataset_exists(hdl
, path
, ZFS_TYPE_DATASET
)) {
3345 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3346 "dataset already exists"));
3347 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3350 if (type
== ZFS_TYPE_VOLUME
)
3351 ost
= LZC_DATSET_TYPE_ZVOL
;
3353 ost
= LZC_DATSET_TYPE_ZFS
;
3355 /* open zpool handle for prop validation */
3356 char pool_path
[ZFS_MAX_DATASET_NAME_LEN
];
3357 (void) strlcpy(pool_path
, path
, sizeof (pool_path
));
3359 /* truncate pool_path at first slash */
3360 char *p
= strchr(pool_path
, '/');
3364 zpool_handle_t
*zpool_handle
= zpool_open(hdl
, pool_path
);
3366 if (props
&& (props
= zfs_valid_proplist(hdl
, type
, props
,
3367 zoned
, NULL
, zpool_handle
, errbuf
)) == 0) {
3368 zpool_close(zpool_handle
);
3371 zpool_close(zpool_handle
);
3373 if (type
== ZFS_TYPE_VOLUME
) {
3375 * If we are creating a volume, the size and block size must
3376 * satisfy a few restraints. First, the blocksize must be a
3377 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3378 * volsize must be a multiple of the block size, and cannot be
3381 if (props
== NULL
|| nvlist_lookup_uint64(props
,
3382 zfs_prop_to_name(ZFS_PROP_VOLSIZE
), &size
) != 0) {
3384 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3385 "missing volume size"));
3386 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3389 if ((ret
= nvlist_lookup_uint64(props
,
3390 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
3391 &blocksize
)) != 0) {
3392 if (ret
== ENOENT
) {
3393 blocksize
= zfs_prop_default_numeric(
3394 ZFS_PROP_VOLBLOCKSIZE
);
3397 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3398 "missing volume block size"));
3399 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3405 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3406 "volume size cannot be zero"));
3407 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3410 if (size
% blocksize
!= 0) {
3412 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3413 "volume size must be a multiple of volume block "
3415 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
3419 /* create the dataset */
3420 ret
= lzc_create(path
, ost
, props
);
3423 /* check for failure */
3425 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3426 (void) parent_name(path
, parent
, sizeof (parent
));
3430 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3431 "no such parent '%s'"), parent
);
3432 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3435 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3436 "parent '%s' is not a filesystem"), parent
);
3437 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3440 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3441 "pool must be upgraded to set this "
3442 "property or value"));
3443 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3447 * This platform can't address a volume this big.
3449 if (type
== ZFS_TYPE_VOLUME
)
3450 return (zfs_error(hdl
, EZFS_VOLTOOBIG
,
3455 return (zfs_standard_error(hdl
, errno
, errbuf
));
3463 * Destroys the given dataset. The caller must make sure that the filesystem
3464 * isn't mounted, and that there are no active dependents. If the file system
3465 * does not exist this function does nothing.
3468 zfs_destroy(zfs_handle_t
*zhp
, boolean_t defer
)
3470 zfs_cmd_t zc
= {"\0"};
3472 if (zhp
->zfs_type
== ZFS_TYPE_BOOKMARK
) {
3473 nvlist_t
*nv
= fnvlist_alloc();
3474 fnvlist_add_boolean(nv
, zhp
->zfs_name
);
3475 int error
= lzc_destroy_bookmarks(nv
, NULL
);
3478 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3479 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3485 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3487 if (ZFS_IS_VOLUME(zhp
)) {
3488 zc
.zc_objset_type
= DMU_OST_ZVOL
;
3490 zc
.zc_objset_type
= DMU_OST_ZFS
;
3493 zc
.zc_defer_destroy
= defer
;
3494 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_DESTROY
, &zc
) != 0 &&
3496 return (zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3497 dgettext(TEXT_DOMAIN
, "cannot destroy '%s'"),
3501 remove_mountpoint(zhp
);
3506 struct destroydata
{
3508 const char *snapname
;
3512 zfs_check_snap_cb(zfs_handle_t
*zhp
, void *arg
)
3514 struct destroydata
*dd
= arg
;
3515 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3518 (void) snprintf(name
, sizeof (name
),
3519 "%s@%s", zhp
->zfs_name
, dd
->snapname
);
3521 if (lzc_exists(name
))
3522 verify(nvlist_add_boolean(dd
->nvl
, name
) == 0);
3524 rv
= zfs_iter_filesystems(zhp
, zfs_check_snap_cb
, dd
);
3530 * Destroys all snapshots with the given name in zhp & descendants.
3533 zfs_destroy_snaps(zfs_handle_t
*zhp
, char *snapname
, boolean_t defer
)
3536 struct destroydata dd
= { 0 };
3538 dd
.snapname
= snapname
;
3539 verify(nvlist_alloc(&dd
.nvl
, NV_UNIQUE_NAME
, 0) == 0);
3540 (void) zfs_check_snap_cb(zfs_handle_dup(zhp
), &dd
);
3542 if (nvlist_empty(dd
.nvl
)) {
3543 ret
= zfs_standard_error_fmt(zhp
->zfs_hdl
, ENOENT
,
3544 dgettext(TEXT_DOMAIN
, "cannot destroy '%s@%s'"),
3545 zhp
->zfs_name
, snapname
);
3547 ret
= zfs_destroy_snaps_nvl(zhp
->zfs_hdl
, dd
.nvl
, defer
);
3549 nvlist_free(dd
.nvl
);
3554 * Destroys all the snapshots named in the nvlist.
3557 zfs_destroy_snaps_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, boolean_t defer
)
3560 nvlist_t
*errlist
= NULL
;
3563 ret
= lzc_destroy_snaps(snaps
, defer
, &errlist
);
3566 nvlist_free(errlist
);
3570 if (nvlist_empty(errlist
)) {
3572 (void) snprintf(errbuf
, sizeof (errbuf
),
3573 dgettext(TEXT_DOMAIN
, "cannot destroy snapshots"));
3575 ret
= zfs_standard_error(hdl
, ret
, errbuf
);
3577 for (pair
= nvlist_next_nvpair(errlist
, NULL
);
3578 pair
!= NULL
; pair
= nvlist_next_nvpair(errlist
, pair
)) {
3580 (void) snprintf(errbuf
, sizeof (errbuf
),
3581 dgettext(TEXT_DOMAIN
, "cannot destroy snapshot %s"),
3584 switch (fnvpair_value_int32(pair
)) {
3587 dgettext(TEXT_DOMAIN
, "snapshot is cloned"));
3588 ret
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3591 ret
= zfs_standard_error(hdl
, errno
, errbuf
);
3596 nvlist_free(errlist
);
3601 * Clones the given dataset. The target must be of the same type as the source.
3604 zfs_clone(zfs_handle_t
*zhp
, const char *target
, nvlist_t
*props
)
3606 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
3609 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3612 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
3614 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3615 "cannot create '%s'"), target
);
3617 /* validate the target/clone name */
3618 if (!zfs_validate_name(hdl
, target
, ZFS_TYPE_FILESYSTEM
, B_TRUE
))
3619 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3621 /* validate parents exist */
3622 if (check_parents(hdl
, target
, &zoned
, B_FALSE
, NULL
) != 0)
3625 (void) parent_name(target
, parent
, sizeof (parent
));
3631 if (ZFS_IS_VOLUME(zhp
)) {
3632 type
= ZFS_TYPE_VOLUME
;
3634 type
= ZFS_TYPE_FILESYSTEM
;
3636 if ((props
= zfs_valid_proplist(hdl
, type
, props
, zoned
,
3637 zhp
, zhp
->zpool_hdl
, errbuf
)) == NULL
)
3641 ret
= lzc_clone(target
, zhp
->zfs_name
, props
);
3649 * The parent doesn't exist. We should have caught this
3650 * above, but there may a race condition that has since
3651 * destroyed the parent.
3653 * At this point, we don't know whether it's the source
3654 * that doesn't exist anymore, or whether the target
3655 * dataset doesn't exist.
3657 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3658 "no such parent '%s'"), parent
);
3659 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
3662 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
3663 "source and target pools differ"));
3664 return (zfs_error(zhp
->zfs_hdl
, EZFS_CROSSTARGET
,
3668 return (zfs_standard_error(zhp
->zfs_hdl
, errno
,
3677 * Promotes the given clone fs to be the clone parent.
3680 zfs_promote(zfs_handle_t
*zhp
)
3682 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
3683 zfs_cmd_t zc
= {"\0"};
3684 char parent
[MAXPATHLEN
];
3688 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3689 "cannot promote '%s'"), zhp
->zfs_name
);
3691 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
3692 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3693 "snapshots can not be promoted"));
3694 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3697 (void) strlcpy(parent
, zhp
->zfs_dmustats
.dds_origin
, sizeof (parent
));
3698 if (parent
[0] == '\0') {
3699 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3700 "not a cloned filesystem"));
3701 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
3704 (void) strlcpy(zc
.zc_value
, zhp
->zfs_dmustats
.dds_origin
,
3705 sizeof (zc
.zc_value
));
3706 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
3707 ret
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3710 int save_errno
= errno
;
3712 switch (save_errno
) {
3714 /* There is a conflicting snapshot name. */
3715 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3716 "conflicting snapshot '%s' from parent '%s'"),
3717 zc
.zc_string
, parent
);
3718 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3721 return (zfs_standard_error(hdl
, save_errno
, errbuf
));
3727 typedef struct snapdata
{
3729 const char *sd_snapname
;
3733 zfs_snapshot_cb(zfs_handle_t
*zhp
, void *arg
)
3735 snapdata_t
*sd
= arg
;
3736 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3739 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) == 0) {
3740 (void) snprintf(name
, sizeof (name
),
3741 "%s@%s", zfs_get_name(zhp
), sd
->sd_snapname
);
3743 fnvlist_add_boolean(sd
->sd_nvl
, name
);
3745 rv
= zfs_iter_filesystems(zhp
, zfs_snapshot_cb
, sd
);
3753 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3757 zfs_snapshot_nvl(libzfs_handle_t
*hdl
, nvlist_t
*snaps
, nvlist_t
*props
)
3763 zpool_handle_t
*zpool_hdl
;
3764 char pool
[ZFS_MAX_DATASET_NAME_LEN
];
3766 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3767 "cannot create snapshots "));
3770 while ((elem
= nvlist_next_nvpair(snaps
, elem
)) != NULL
) {
3771 const char *snapname
= nvpair_name(elem
);
3773 /* validate the target name */
3774 if (!zfs_validate_name(hdl
, snapname
, ZFS_TYPE_SNAPSHOT
,
3776 (void) snprintf(errbuf
, sizeof (errbuf
),
3777 dgettext(TEXT_DOMAIN
,
3778 "cannot create snapshot '%s'"), snapname
);
3779 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3784 * get pool handle for prop validation. assumes all snaps are in the
3785 * same pool, as does lzc_snapshot (below).
3787 elem
= nvlist_next_nvpair(snaps
, NULL
);
3788 (void) strlcpy(pool
, nvpair_name(elem
), sizeof (pool
));
3789 pool
[strcspn(pool
, "/@")] = '\0';
3790 zpool_hdl
= zpool_open(hdl
, pool
);
3791 if (zpool_hdl
== NULL
)
3794 if (props
!= NULL
&&
3795 (props
= zfs_valid_proplist(hdl
, ZFS_TYPE_SNAPSHOT
,
3796 props
, B_FALSE
, NULL
, zpool_hdl
, errbuf
)) == NULL
) {
3797 zpool_close(zpool_hdl
);
3800 zpool_close(zpool_hdl
);
3802 ret
= lzc_snapshot(snaps
, props
, &errors
);
3805 boolean_t printed
= B_FALSE
;
3806 for (elem
= nvlist_next_nvpair(errors
, NULL
);
3808 elem
= nvlist_next_nvpair(errors
, elem
)) {
3809 (void) snprintf(errbuf
, sizeof (errbuf
),
3810 dgettext(TEXT_DOMAIN
,
3811 "cannot create snapshot '%s'"), nvpair_name(elem
));
3812 (void) zfs_standard_error(hdl
,
3813 fnvpair_value_int32(elem
), errbuf
);
3819 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3820 "multiple snapshots of same "
3822 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3826 (void) zfs_standard_error(hdl
, ret
, errbuf
);
3832 nvlist_free(errors
);
3837 zfs_snapshot(libzfs_handle_t
*hdl
, const char *path
, boolean_t recursive
,
3841 snapdata_t sd
= { 0 };
3842 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
3847 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3848 "cannot snapshot %s"), path
);
3850 if (!zfs_validate_name(hdl
, path
, ZFS_TYPE_SNAPSHOT
, B_TRUE
))
3851 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3853 (void) strlcpy(fsname
, path
, sizeof (fsname
));
3854 cp
= strchr(fsname
, '@');
3856 sd
.sd_snapname
= cp
+ 1;
3858 if ((zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
|
3859 ZFS_TYPE_VOLUME
)) == NULL
) {
3863 verify(nvlist_alloc(&sd
.sd_nvl
, NV_UNIQUE_NAME
, 0) == 0);
3865 (void) zfs_snapshot_cb(zfs_handle_dup(zhp
), &sd
);
3867 fnvlist_add_boolean(sd
.sd_nvl
, path
);
3870 ret
= zfs_snapshot_nvl(hdl
, sd
.sd_nvl
, props
);
3871 nvlist_free(sd
.sd_nvl
);
3877 * Destroy any more recent snapshots. We invoke this callback on any dependents
3878 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3879 * is a dependent and we should just destroy it without checking the transaction
3882 typedef struct rollback_data
{
3883 const char *cb_target
; /* the snapshot */
3884 uint64_t cb_create
; /* creation time reference */
3890 rollback_destroy_dependent(zfs_handle_t
*zhp
, void *data
)
3892 rollback_data_t
*cbp
= data
;
3893 prop_changelist_t
*clp
;
3895 /* We must destroy this clone; first unmount it */
3896 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3897 cbp
->cb_force
? MS_FORCE
: 0);
3898 if (clp
== NULL
|| changelist_prefix(clp
) != 0) {
3899 cbp
->cb_error
= B_TRUE
;
3903 if (zfs_destroy(zhp
, B_FALSE
) != 0)
3904 cbp
->cb_error
= B_TRUE
;
3906 changelist_remove(clp
, zhp
->zfs_name
);
3907 (void) changelist_postfix(clp
);
3908 changelist_free(clp
);
3915 rollback_destroy(zfs_handle_t
*zhp
, void *data
)
3917 rollback_data_t
*cbp
= data
;
3919 if (zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
) > cbp
->cb_create
) {
3920 cbp
->cb_error
|= zfs_iter_dependents(zhp
, B_FALSE
,
3921 rollback_destroy_dependent
, cbp
);
3923 cbp
->cb_error
|= zfs_destroy(zhp
, B_FALSE
);
3931 * Given a dataset, rollback to a specific snapshot, discarding any
3932 * data changes since then and making it the active dataset.
3934 * Any snapshots and bookmarks more recent than the target are
3935 * destroyed, along with their dependents (i.e. clones).
3938 zfs_rollback(zfs_handle_t
*zhp
, zfs_handle_t
*snap
, boolean_t force
)
3940 rollback_data_t cb
= { 0 };
3942 boolean_t restore_resv
= 0;
3943 uint64_t old_volsize
= 0, new_volsize
;
3944 zfs_prop_t resv_prop
= { 0 };
3946 assert(zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
3947 zhp
->zfs_type
== ZFS_TYPE_VOLUME
);
3950 * Destroy all recent snapshots and their dependents.
3952 cb
.cb_force
= force
;
3953 cb
.cb_target
= snap
->zfs_name
;
3954 cb
.cb_create
= zfs_prop_get_int(snap
, ZFS_PROP_CREATETXG
);
3955 (void) zfs_iter_snapshots(zhp
, B_FALSE
, rollback_destroy
, &cb
);
3956 (void) zfs_iter_bookmarks(zhp
, rollback_destroy
, &cb
);
3962 * Now that we have verified that the snapshot is the latest,
3963 * rollback to the given snapshot.
3966 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
3967 if (zfs_which_resv_prop(zhp
, &resv_prop
) < 0)
3969 old_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3971 (old_volsize
== zfs_prop_get_int(zhp
, resv_prop
));
3975 * We rely on zfs_iter_children() to verify that there are no
3976 * newer snapshots for the given dataset. Therefore, we can
3977 * simply pass the name on to the ioctl() call. There is still
3978 * an unlikely race condition where the user has taken a
3979 * snapshot since we verified that this was the most recent.
3981 err
= lzc_rollback(zhp
->zfs_name
, NULL
, 0);
3983 (void) zfs_standard_error_fmt(zhp
->zfs_hdl
, errno
,
3984 dgettext(TEXT_DOMAIN
, "cannot rollback '%s'"),
3990 * For volumes, if the pre-rollback volsize matched the pre-
3991 * rollback reservation and the volsize has changed then set
3992 * the reservation property to the post-rollback volsize.
3993 * Make a new handle since the rollback closed the dataset.
3995 if ((zhp
->zfs_type
== ZFS_TYPE_VOLUME
) &&
3996 (zhp
= make_dataset_handle(zhp
->zfs_hdl
, zhp
->zfs_name
))) {
3998 new_volsize
= zfs_prop_get_int(zhp
, ZFS_PROP_VOLSIZE
);
3999 if (old_volsize
!= new_volsize
)
4000 err
= zfs_prop_set_int(zhp
, resv_prop
,
4009 * Renames the given dataset.
4012 zfs_rename(zfs_handle_t
*zhp
, const char *target
, boolean_t recursive
,
4013 boolean_t force_unmount
)
4016 zfs_cmd_t zc
= {"\0"};
4018 prop_changelist_t
*cl
= NULL
;
4019 zfs_handle_t
*zhrp
= NULL
;
4020 char *parentname
= NULL
;
4021 char parent
[ZFS_MAX_DATASET_NAME_LEN
];
4022 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4025 /* if we have the same exact name, just return success */
4026 if (strcmp(zhp
->zfs_name
, target
) == 0)
4029 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4030 "cannot rename to '%s'"), target
);
4033 * Make sure the target name is valid
4035 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
4036 if ((strchr(target
, '@') == NULL
) ||
4039 * Snapshot target name is abbreviated,
4040 * reconstruct full dataset name
4042 (void) strlcpy(parent
, zhp
->zfs_name
,
4044 delim
= strchr(parent
, '@');
4045 if (strchr(target
, '@') == NULL
)
4049 (void) strlcat(parent
, target
, sizeof (parent
));
4053 * Make sure we're renaming within the same dataset.
4055 delim
= strchr(target
, '@');
4056 if (strncmp(zhp
->zfs_name
, target
, delim
- target
)
4057 != 0 || zhp
->zfs_name
[delim
- target
] != '@') {
4058 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4059 "snapshots must be part of same "
4061 return (zfs_error(hdl
, EZFS_CROSSTARGET
,
4065 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4066 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4069 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4070 "recursive rename must be a snapshot"));
4071 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
4074 if (!zfs_validate_name(hdl
, target
, zhp
->zfs_type
, B_TRUE
))
4075 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4077 /* validate parents */
4078 if (check_parents(hdl
, target
, NULL
, B_FALSE
, NULL
) != 0)
4081 /* make sure we're in the same pool */
4082 verify((delim
= strchr(target
, '/')) != NULL
);
4083 if (strncmp(zhp
->zfs_name
, target
, delim
- target
) != 0 ||
4084 zhp
->zfs_name
[delim
- target
] != '/') {
4085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4086 "datasets must be within same pool"));
4087 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
4090 /* new name cannot be a child of the current dataset name */
4091 if (is_descendant(zhp
->zfs_name
, target
)) {
4092 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4093 "New dataset name cannot be a descendant of "
4094 "current dataset name"));
4095 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
4099 (void) snprintf(errbuf
, sizeof (errbuf
),
4100 dgettext(TEXT_DOMAIN
, "cannot rename '%s'"), zhp
->zfs_name
);
4102 if (getzoneid() == GLOBAL_ZONEID
&&
4103 zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
)) {
4104 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4105 "dataset is used in a non-global zone"));
4106 return (zfs_error(hdl
, EZFS_ZONED
, errbuf
));
4110 parentname
= zfs_strdup(zhp
->zfs_hdl
, zhp
->zfs_name
);
4111 if (parentname
== NULL
) {
4115 delim
= strchr(parentname
, '@');
4117 zhrp
= zfs_open(zhp
->zfs_hdl
, parentname
, ZFS_TYPE_DATASET
);
4122 } else if (zhp
->zfs_type
!= ZFS_TYPE_SNAPSHOT
) {
4123 if ((cl
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4124 force_unmount
? MS_FORCE
: 0)) == NULL
)
4127 if (changelist_haszonedchild(cl
)) {
4128 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4129 "child dataset with inherited mountpoint is used "
4130 "in a non-global zone"));
4131 (void) zfs_error(hdl
, EZFS_ZONED
, errbuf
);
4136 if ((ret
= changelist_prefix(cl
)) != 0)
4140 if (ZFS_IS_VOLUME(zhp
))
4141 zc
.zc_objset_type
= DMU_OST_ZVOL
;
4143 zc
.zc_objset_type
= DMU_OST_ZFS
;
4145 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4146 (void) strlcpy(zc
.zc_value
, target
, sizeof (zc
.zc_value
));
4148 zc
.zc_cookie
= recursive
;
4150 if ((ret
= zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_RENAME
, &zc
)) != 0) {
4152 * if it was recursive, the one that actually failed will
4155 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4156 "cannot rename '%s'"), zc
.zc_name
);
4158 if (recursive
&& errno
== EEXIST
) {
4159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4160 "a child dataset already has a snapshot "
4161 "with the new name"));
4162 (void) zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4164 (void) zfs_standard_error(zhp
->zfs_hdl
, errno
, errbuf
);
4168 * On failure, we still want to remount any filesystems that
4169 * were previously mounted, so we don't alter the system state.
4172 (void) changelist_postfix(cl
);
4175 changelist_rename(cl
, zfs_get_name(zhp
), target
);
4176 ret
= changelist_postfix(cl
);
4181 if (parentname
!= NULL
) {
4188 changelist_free(cl
);
4194 zfs_get_user_props(zfs_handle_t
*zhp
)
4196 return (zhp
->zfs_user_props
);
4200 * This function is used by 'zfs list' to determine the exact set of columns to
4201 * display, and their maximum widths. This does two main things:
4203 * - If this is a list of all properties, then expand the list to include
4204 * all native properties, and set a flag so that for each dataset we look
4205 * for new unique user properties and add them to the list.
4207 * - For non fixed-width properties, keep track of the maximum width seen
4208 * so that we can size the column appropriately. If the user has
4209 * requested received property values, we also need to compute the width
4210 * of the RECEIVED column.
4213 zfs_expand_proplist(zfs_handle_t
*zhp
, zprop_list_t
**plp
, boolean_t received
,
4216 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4217 zprop_list_t
*entry
;
4218 zprop_list_t
**last
, **start
;
4219 nvlist_t
*userprops
, *propval
;
4222 char buf
[ZFS_MAXPROPLEN
];
4224 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_DATASET
) != 0)
4227 userprops
= zfs_get_user_props(zhp
);
4230 if (entry
->pl_all
&& nvlist_next_nvpair(userprops
, NULL
) != NULL
) {
4232 * Go through and add any user properties as necessary. We
4233 * start by incrementing our list pointer to the first
4234 * non-native property.
4237 while (*start
!= NULL
) {
4238 if ((*start
)->pl_prop
== ZPROP_INVAL
)
4240 start
= &(*start
)->pl_next
;
4244 while ((elem
= nvlist_next_nvpair(userprops
, elem
)) != NULL
) {
4246 * See if we've already found this property in our list.
4248 for (last
= start
; *last
!= NULL
;
4249 last
= &(*last
)->pl_next
) {
4250 if (strcmp((*last
)->pl_user_prop
,
4251 nvpair_name(elem
)) == 0)
4255 if (*last
== NULL
) {
4256 if ((entry
= zfs_alloc(hdl
,
4257 sizeof (zprop_list_t
))) == NULL
||
4258 ((entry
->pl_user_prop
= zfs_strdup(hdl
,
4259 nvpair_name(elem
)))) == NULL
) {
4264 entry
->pl_prop
= ZPROP_INVAL
;
4265 entry
->pl_width
= strlen(nvpair_name(elem
));
4266 entry
->pl_all
= B_TRUE
;
4273 * Now go through and check the width of any non-fixed columns
4275 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
4276 if (entry
->pl_fixed
&& !literal
)
4279 if (entry
->pl_prop
!= ZPROP_INVAL
) {
4280 if (zfs_prop_get(zhp
, entry
->pl_prop
,
4281 buf
, sizeof (buf
), NULL
, NULL
, 0, literal
) == 0) {
4282 if (strlen(buf
) > entry
->pl_width
)
4283 entry
->pl_width
= strlen(buf
);
4285 if (received
&& zfs_prop_get_recvd(zhp
,
4286 zfs_prop_to_name(entry
->pl_prop
),
4287 buf
, sizeof (buf
), literal
) == 0)
4288 if (strlen(buf
) > entry
->pl_recvd_width
)
4289 entry
->pl_recvd_width
= strlen(buf
);
4291 if (nvlist_lookup_nvlist(userprops
, entry
->pl_user_prop
,
4293 verify(nvlist_lookup_string(propval
,
4294 ZPROP_VALUE
, &strval
) == 0);
4295 if (strlen(strval
) > entry
->pl_width
)
4296 entry
->pl_width
= strlen(strval
);
4298 if (received
&& zfs_prop_get_recvd(zhp
,
4299 entry
->pl_user_prop
,
4300 buf
, sizeof (buf
), literal
) == 0)
4301 if (strlen(buf
) > entry
->pl_recvd_width
)
4302 entry
->pl_recvd_width
= strlen(buf
);
4310 zfs_prune_proplist(zfs_handle_t
*zhp
, uint8_t *props
)
4316 * Keep a reference to the props-table against which we prune the
4319 zhp
->zfs_props_table
= props
;
4321 curr
= nvlist_next_nvpair(zhp
->zfs_props
, NULL
);
4324 zfs_prop_t zfs_prop
= zfs_name_to_prop(nvpair_name(curr
));
4325 next
= nvlist_next_nvpair(zhp
->zfs_props
, curr
);
4328 * User properties will result in ZPROP_INVAL, and since we
4329 * only know how to prune standard ZFS properties, we always
4330 * leave these in the list. This can also happen if we
4331 * encounter an unknown DSL property (when running older
4332 * software, for example).
4334 if (zfs_prop
!= ZPROP_INVAL
&& props
[zfs_prop
] == B_FALSE
)
4335 (void) nvlist_remove(zhp
->zfs_props
,
4336 nvpair_name(curr
), nvpair_type(curr
));
4342 zfs_smb_acl_mgmt(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4343 zfs_smb_acl_op_t cmd
, char *resource1
, char *resource2
)
4345 zfs_cmd_t zc
= {"\0"};
4346 nvlist_t
*nvlist
= NULL
;
4349 (void) strlcpy(zc
.zc_name
, dataset
, sizeof (zc
.zc_name
));
4350 (void) strlcpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
4351 zc
.zc_cookie
= (uint64_t)cmd
;
4353 if (cmd
== ZFS_SMB_ACL_RENAME
) {
4354 if (nvlist_alloc(&nvlist
, NV_UNIQUE_NAME
, 0) != 0) {
4355 (void) no_memory(hdl
);
4361 case ZFS_SMB_ACL_ADD
:
4362 case ZFS_SMB_ACL_REMOVE
:
4363 (void) strlcpy(zc
.zc_string
, resource1
, sizeof (zc
.zc_string
));
4365 case ZFS_SMB_ACL_RENAME
:
4366 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_SRC
,
4368 (void) no_memory(hdl
);
4371 if (nvlist_add_string(nvlist
, ZFS_SMB_ACL_TARGET
,
4373 (void) no_memory(hdl
);
4376 if (zcmd_write_src_nvlist(hdl
, &zc
, nvlist
) != 0) {
4377 nvlist_free(nvlist
);
4381 case ZFS_SMB_ACL_PURGE
:
4386 error
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_SMB_ACL
, &zc
);
4387 nvlist_free(nvlist
);
4392 zfs_smb_acl_add(libzfs_handle_t
*hdl
, char *dataset
,
4393 char *path
, char *resource
)
4395 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_ADD
,
4400 zfs_smb_acl_remove(libzfs_handle_t
*hdl
, char *dataset
,
4401 char *path
, char *resource
)
4403 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_REMOVE
,
4408 zfs_smb_acl_purge(libzfs_handle_t
*hdl
, char *dataset
, char *path
)
4410 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_PURGE
,
4415 zfs_smb_acl_rename(libzfs_handle_t
*hdl
, char *dataset
, char *path
,
4416 char *oldname
, char *newname
)
4418 return (zfs_smb_acl_mgmt(hdl
, dataset
, path
, ZFS_SMB_ACL_RENAME
,
4423 zfs_userspace(zfs_handle_t
*zhp
, zfs_userquota_prop_t type
,
4424 zfs_userspace_cb_t func
, void *arg
)
4426 zfs_cmd_t zc
= {"\0"};
4427 zfs_useracct_t buf
[100];
4428 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4431 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4433 zc
.zc_objset_type
= type
;
4434 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4437 zfs_useracct_t
*zua
= buf
;
4439 zc
.zc_nvlist_dst_size
= sizeof (buf
);
4440 if (zfs_ioctl(hdl
, ZFS_IOC_USERSPACE_MANY
, &zc
) != 0) {
4443 if ((errno
== ENOTSUP
&&
4444 (type
== ZFS_PROP_USEROBJUSED
||
4445 type
== ZFS_PROP_GROUPOBJUSED
||
4446 type
== ZFS_PROP_USEROBJQUOTA
||
4447 type
== ZFS_PROP_GROUPOBJQUOTA
)))
4450 (void) snprintf(errbuf
, sizeof (errbuf
),
4451 dgettext(TEXT_DOMAIN
,
4452 "cannot get used/quota for %s"), zc
.zc_name
);
4453 return (zfs_standard_error_fmt(hdl
, errno
, errbuf
));
4455 if (zc
.zc_nvlist_dst_size
== 0)
4458 while (zc
.zc_nvlist_dst_size
> 0) {
4459 if ((ret
= func(arg
, zua
->zu_domain
, zua
->zu_rid
,
4460 zua
->zu_space
)) != 0)
4463 zc
.zc_nvlist_dst_size
-= sizeof (zfs_useracct_t
);
4472 const char *snapname
;
4474 boolean_t recursive
;
4479 zfs_hold_one(zfs_handle_t
*zhp
, void *arg
)
4481 struct holdarg
*ha
= arg
;
4482 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4485 (void) snprintf(name
, sizeof (name
),
4486 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4488 if (lzc_exists(name
))
4489 fnvlist_add_string(ha
->nvl
, name
, ha
->tag
);
4492 rv
= zfs_iter_filesystems(zhp
, zfs_hold_one
, ha
);
4498 zfs_hold(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4499 boolean_t recursive
, int cleanup_fd
)
4504 ha
.nvl
= fnvlist_alloc();
4505 ha
.snapname
= snapname
;
4507 ha
.recursive
= recursive
;
4508 (void) zfs_hold_one(zfs_handle_dup(zhp
), &ha
);
4510 if (nvlist_empty(ha
.nvl
)) {
4513 fnvlist_free(ha
.nvl
);
4515 (void) snprintf(errbuf
, sizeof (errbuf
),
4516 dgettext(TEXT_DOMAIN
,
4517 "cannot hold snapshot '%s@%s'"),
4518 zhp
->zfs_name
, snapname
);
4519 (void) zfs_standard_error(zhp
->zfs_hdl
, ret
, errbuf
);
4523 ret
= zfs_hold_nvl(zhp
, cleanup_fd
, ha
.nvl
);
4524 fnvlist_free(ha
.nvl
);
4530 zfs_hold_nvl(zfs_handle_t
*zhp
, int cleanup_fd
, nvlist_t
*holds
)
4534 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4539 ret
= lzc_hold(holds
, cleanup_fd
, &errors
);
4542 /* There may be errors even in the success case. */
4543 fnvlist_free(errors
);
4547 if (nvlist_empty(errors
)) {
4548 /* no hold-specific errors */
4549 (void) snprintf(errbuf
, sizeof (errbuf
),
4550 dgettext(TEXT_DOMAIN
, "cannot hold"));
4553 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4554 "pool must be upgraded"));
4555 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4558 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4561 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4565 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4567 elem
= nvlist_next_nvpair(errors
, elem
)) {
4568 (void) snprintf(errbuf
, sizeof (errbuf
),
4569 dgettext(TEXT_DOMAIN
,
4570 "cannot hold snapshot '%s'"), nvpair_name(elem
));
4571 switch (fnvpair_value_int32(elem
)) {
4574 * Temporary tags wind up having the ds object id
4575 * prepended. So even if we passed the length check
4576 * above, it's still possible for the tag to wind
4577 * up being slightly too long.
4579 (void) zfs_error(hdl
, EZFS_TAGTOOLONG
, errbuf
);
4582 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4585 (void) zfs_error(hdl
, EZFS_REFTAG_HOLD
, errbuf
);
4588 (void) zfs_standard_error(hdl
,
4589 fnvpair_value_int32(elem
), errbuf
);
4593 fnvlist_free(errors
);
4598 zfs_release_one(zfs_handle_t
*zhp
, void *arg
)
4600 struct holdarg
*ha
= arg
;
4601 char name
[ZFS_MAX_DATASET_NAME_LEN
];
4603 nvlist_t
*existing_holds
;
4605 (void) snprintf(name
, sizeof (name
),
4606 "%s@%s", zhp
->zfs_name
, ha
->snapname
);
4608 if (lzc_get_holds(name
, &existing_holds
) != 0) {
4610 } else if (!nvlist_exists(existing_holds
, ha
->tag
)) {
4613 nvlist_t
*torelease
= fnvlist_alloc();
4614 fnvlist_add_boolean(torelease
, ha
->tag
);
4615 fnvlist_add_nvlist(ha
->nvl
, name
, torelease
);
4616 fnvlist_free(torelease
);
4620 rv
= zfs_iter_filesystems(zhp
, zfs_release_one
, ha
);
4626 zfs_release(zfs_handle_t
*zhp
, const char *snapname
, const char *tag
,
4627 boolean_t recursive
)
4631 nvlist_t
*errors
= NULL
;
4633 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4636 ha
.nvl
= fnvlist_alloc();
4637 ha
.snapname
= snapname
;
4639 ha
.recursive
= recursive
;
4641 (void) zfs_release_one(zfs_handle_dup(zhp
), &ha
);
4643 if (nvlist_empty(ha
.nvl
)) {
4644 fnvlist_free(ha
.nvl
);
4646 (void) snprintf(errbuf
, sizeof (errbuf
),
4647 dgettext(TEXT_DOMAIN
,
4648 "cannot release hold from snapshot '%s@%s'"),
4649 zhp
->zfs_name
, snapname
);
4651 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4653 (void) zfs_standard_error(hdl
, ret
, errbuf
);
4658 ret
= lzc_release(ha
.nvl
, &errors
);
4659 fnvlist_free(ha
.nvl
);
4662 /* There may be errors even in the success case. */
4663 fnvlist_free(errors
);
4667 if (nvlist_empty(errors
)) {
4668 /* no hold-specific errors */
4669 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4673 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4674 "pool must be upgraded"));
4675 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4678 (void) zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4682 for (elem
= nvlist_next_nvpair(errors
, NULL
);
4684 elem
= nvlist_next_nvpair(errors
, elem
)) {
4685 (void) snprintf(errbuf
, sizeof (errbuf
),
4686 dgettext(TEXT_DOMAIN
,
4687 "cannot release hold from snapshot '%s'"),
4689 switch (fnvpair_value_int32(elem
)) {
4691 (void) zfs_error(hdl
, EZFS_REFTAG_RELE
, errbuf
);
4694 (void) zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4697 (void) zfs_standard_error_fmt(hdl
,
4698 fnvpair_value_int32(elem
), errbuf
);
4702 fnvlist_free(errors
);
4707 zfs_get_fsacl(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4709 zfs_cmd_t zc
= {"\0"};
4710 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4716 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4717 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4721 nvbuf
= malloc(nvsz
);
4722 if (nvbuf
== NULL
) {
4723 err
= (zfs_error(hdl
, EZFS_NOMEM
, strerror(errno
)));
4727 zc
.zc_nvlist_dst_size
= nvsz
;
4728 zc
.zc_nvlist_dst
= (uintptr_t)nvbuf
;
4730 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4732 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_GET_FSACL
, &zc
) != 0) {
4733 (void) snprintf(errbuf
, sizeof (errbuf
),
4734 dgettext(TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4739 nvsz
= zc
.zc_nvlist_dst_size
;
4743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4744 "pool must be upgraded"));
4745 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4748 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4751 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4754 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4759 int rc
= nvlist_unpack(nvbuf
, zc
.zc_nvlist_dst_size
, nvl
, 0);
4761 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(
4762 TEXT_DOMAIN
, "cannot get permissions on '%s'"),
4764 err
= zfs_standard_error_fmt(hdl
, rc
, errbuf
);
4774 zfs_set_fsacl(zfs_handle_t
*zhp
, boolean_t un
, nvlist_t
*nvl
)
4776 zfs_cmd_t zc
= {"\0"};
4777 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4783 assert(zhp
->zfs_type
== ZFS_TYPE_VOLUME
||
4784 zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
);
4786 err
= nvlist_size(nvl
, &nvsz
, NV_ENCODE_NATIVE
);
4789 nvbuf
= malloc(nvsz
);
4791 err
= nvlist_pack(nvl
, &nvbuf
, &nvsz
, NV_ENCODE_NATIVE
, 0);
4794 zc
.zc_nvlist_src_size
= nvsz
;
4795 zc
.zc_nvlist_src
= (uintptr_t)nvbuf
;
4796 zc
.zc_perm_action
= un
;
4798 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
4800 if (zfs_ioctl(hdl
, ZFS_IOC_SET_FSACL
, &zc
) != 0) {
4801 (void) snprintf(errbuf
, sizeof (errbuf
),
4802 dgettext(TEXT_DOMAIN
, "cannot set permissions on '%s'"),
4806 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4807 "pool must be upgraded"));
4808 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4811 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4814 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4817 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4828 zfs_get_holds(zfs_handle_t
*zhp
, nvlist_t
**nvl
)
4833 err
= lzc_get_holds(zhp
->zfs_name
, nvl
);
4836 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
4838 (void) snprintf(errbuf
, sizeof (errbuf
),
4839 dgettext(TEXT_DOMAIN
, "cannot get holds for '%s'"),
4843 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4844 "pool must be upgraded"));
4845 err
= zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4848 err
= zfs_error(hdl
, EZFS_BADTYPE
, errbuf
);
4851 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4854 err
= zfs_standard_error_fmt(hdl
, errno
, errbuf
);
4863 * Convert the zvol's volume size to an appropriate reservation.
4864 * Note: If this routine is updated, it is necessary to update the ZFS test
4865 * suite's shell version in reservation.kshlib.
4868 zvol_volsize_to_reservation(uint64_t volsize
, nvlist_t
*props
)
4871 uint64_t nblocks
, volblocksize
;
4875 if (nvlist_lookup_string(props
,
4876 zfs_prop_to_name(ZFS_PROP_COPIES
), &strval
) == 0)
4877 ncopies
= atoi(strval
);
4880 if (nvlist_lookup_uint64(props
,
4881 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE
),
4882 &volblocksize
) != 0)
4883 volblocksize
= ZVOL_DEFAULT_BLOCKSIZE
;
4884 nblocks
= volsize
/volblocksize
;
4885 /* start with metadnode L0-L6 */
4887 /* calculate number of indirects */
4888 while (nblocks
> 1) {
4889 nblocks
+= DNODES_PER_LEVEL
- 1;
4890 nblocks
/= DNODES_PER_LEVEL
;
4893 numdb
*= MIN(SPA_DVAS_PER_BP
, ncopies
+ 1);
4896 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4897 * compressed, but in practice they compress down to about
4900 numdb
*= 1ULL << DN_MAX_INDBLKSHIFT
;