Merge commit '9bba04fed2291aa884d1da64bc62150d5562cc5b'
[unleashed.git] / usr / src / lib / libzfs / common / libzfs_dataset.c
blob88c3b857ee740905dea41de86af411c0179e4412
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2018, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2017 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017 RackTop Systems.
36 #include <ctype.h>
37 #include <errno.h>
38 #include <libintl.h>
39 #include <math.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <strings.h>
43 #include <unistd.h>
44 #include <stddef.h>
45 #include <zone.h>
46 #include <fcntl.h>
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
49 #include <priv.h>
50 #include <pwd.h>
51 #include <grp.h>
52 #include <stddef.h>
53 #include <ucred.h>
54 #include <idmap.h>
55 #include <aclutils.h>
56 #include <directory.h>
58 #include <sys/dnode.h>
59 #include <sys/spa.h>
60 #include <sys/zap.h>
61 #include <libzfs.h>
63 #include "zfs_namecheck.h"
64 #include "zfs_prop.h"
65 #include "libzfs_impl.h"
66 #include "zfs_deleg.h"
68 static int userquota_propname_decode(const char *propname, boolean_t zoned,
69 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
72 * Given a single type (not a mask of types), return the type in a human
73 * readable form.
75 const char *
76 zfs_type_to_name(zfs_type_t type)
78 switch (type) {
79 case ZFS_TYPE_FILESYSTEM:
80 return (dgettext(TEXT_DOMAIN, "filesystem"));
81 case ZFS_TYPE_SNAPSHOT:
82 return (dgettext(TEXT_DOMAIN, "snapshot"));
83 case ZFS_TYPE_VOLUME:
84 return (dgettext(TEXT_DOMAIN, "volume"));
85 case ZFS_TYPE_POOL:
86 return (dgettext(TEXT_DOMAIN, "pool"));
87 case ZFS_TYPE_BOOKMARK:
88 return (dgettext(TEXT_DOMAIN, "bookmark"));
89 default:
90 assert(!"unhandled zfs_type_t");
93 return (NULL);
97 * Validate a ZFS path. This is used even before trying to open the dataset, to
98 * provide a more meaningful error message. We call zfs_error_aux() to
99 * explain exactly why the name was not valid.
102 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
103 boolean_t modifying)
105 namecheck_err_t why;
106 char what;
108 if (entity_namecheck(path, &why, &what) != 0) {
109 if (hdl != NULL) {
110 switch (why) {
111 case NAME_ERR_TOOLONG:
112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
113 "name is too long"));
114 break;
116 case NAME_ERR_LEADING_SLASH:
117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 "leading slash in name"));
119 break;
121 case NAME_ERR_EMPTY_COMPONENT:
122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
123 "empty component in name"));
124 break;
126 case NAME_ERR_TRAILING_SLASH:
127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
128 "trailing slash in name"));
129 break;
131 case NAME_ERR_INVALCHAR:
132 zfs_error_aux(hdl,
133 dgettext(TEXT_DOMAIN, "invalid character "
134 "'%c' in name"), what);
135 break;
137 case NAME_ERR_MULTIPLE_DELIMITERS:
138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139 "multiple '@' and/or '#' delimiters in "
140 "name"));
141 break;
143 case NAME_ERR_NOLETTER:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "pool doesn't begin with a letter"));
146 break;
148 case NAME_ERR_RESERVED:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "name is reserved"));
151 break;
153 case NAME_ERR_DISKLIKE:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "reserved disk name"));
156 break;
158 default:
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "(%d) not defined"), why);
161 break;
165 return (0);
168 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
169 if (hdl != NULL)
170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 "snapshot delimiter '@' is not expected here"));
172 return (0);
175 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
176 if (hdl != NULL)
177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178 "missing '@' delimiter in snapshot name"));
179 return (0);
182 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
183 if (hdl != NULL)
184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185 "bookmark delimiter '#' is not expected here"));
186 return (0);
189 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
190 if (hdl != NULL)
191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
192 "missing '#' delimiter in bookmark name"));
193 return (0);
196 if (modifying && strchr(path, '%') != NULL) {
197 if (hdl != NULL)
198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
199 "invalid character %c in name"), '%');
200 return (0);
203 return (-1);
207 zfs_name_valid(const char *name, zfs_type_t type)
209 if (type == ZFS_TYPE_POOL)
210 return (zpool_name_valid(NULL, B_FALSE, name));
211 return (zfs_validate_name(NULL, name, type, B_FALSE));
215 * This function takes the raw DSL properties, and filters out the user-defined
216 * properties into a separate nvlist.
218 static nvlist_t *
219 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
221 libzfs_handle_t *hdl = zhp->zfs_hdl;
222 nvpair_t *elem;
223 nvlist_t *propval;
224 nvlist_t *nvl;
226 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
227 (void) no_memory(hdl);
228 return (NULL);
231 elem = NULL;
232 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
233 if (!zfs_prop_user(nvpair_name(elem)))
234 continue;
236 verify(nvpair_value_nvlist(elem, &propval) == 0);
237 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
238 nvlist_free(nvl);
239 (void) no_memory(hdl);
240 return (NULL);
244 return (nvl);
247 static zpool_handle_t *
248 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
250 libzfs_handle_t *hdl = zhp->zfs_hdl;
251 zpool_handle_t *zph;
253 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
254 if (hdl->libzfs_pool_handles != NULL)
255 zph->zpool_next = hdl->libzfs_pool_handles;
256 hdl->libzfs_pool_handles = zph;
258 return (zph);
261 static zpool_handle_t *
262 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
264 libzfs_handle_t *hdl = zhp->zfs_hdl;
265 zpool_handle_t *zph = hdl->libzfs_pool_handles;
267 while ((zph != NULL) &&
268 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
269 zph = zph->zpool_next;
270 return (zph);
274 * Returns a handle to the pool that contains the provided dataset.
275 * If a handle to that pool already exists then that handle is returned.
276 * Otherwise, a new handle is created and added to the list of handles.
278 static zpool_handle_t *
279 zpool_handle(zfs_handle_t *zhp)
281 char *pool_name;
282 int len;
283 zpool_handle_t *zph;
285 len = strcspn(zhp->zfs_name, "/@#") + 1;
286 pool_name = zfs_alloc(zhp->zfs_hdl, len);
287 (void) strlcpy(pool_name, zhp->zfs_name, len);
289 zph = zpool_find_handle(zhp, pool_name, len);
290 if (zph == NULL)
291 zph = zpool_add_handle(zhp, pool_name);
293 free(pool_name);
294 return (zph);
297 void
298 zpool_free_handles(libzfs_handle_t *hdl)
300 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
302 while (zph != NULL) {
303 next = zph->zpool_next;
304 zpool_close(zph);
305 zph = next;
307 hdl->libzfs_pool_handles = NULL;
311 * Utility function to gather stats (objset and zpl) for the given object.
313 static int
314 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
316 libzfs_handle_t *hdl = zhp->zfs_hdl;
318 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
320 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
321 if (errno == ENOMEM) {
322 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
323 return (-1);
325 } else {
326 return (-1);
329 return (0);
333 * Utility function to get the received properties of the given object.
335 static int
336 get_recvd_props_ioctl(zfs_handle_t *zhp)
338 libzfs_handle_t *hdl = zhp->zfs_hdl;
339 nvlist_t *recvdprops;
340 zfs_cmd_t zc = { 0 };
341 int err;
343 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
344 return (-1);
346 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
348 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
349 if (errno == ENOMEM) {
350 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
351 return (-1);
353 } else {
354 zcmd_free_nvlists(&zc);
355 return (-1);
359 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
360 zcmd_free_nvlists(&zc);
361 if (err != 0)
362 return (-1);
364 nvlist_free(zhp->zfs_recvd_props);
365 zhp->zfs_recvd_props = recvdprops;
367 return (0);
370 static int
371 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
373 nvlist_t *allprops, *userprops;
375 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
377 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
378 return (-1);
382 * XXX Why do we store the user props separately, in addition to
383 * storing them in zfs_props?
385 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
386 nvlist_free(allprops);
387 return (-1);
390 nvlist_free(zhp->zfs_props);
391 nvlist_free(zhp->zfs_user_props);
393 zhp->zfs_props = allprops;
394 zhp->zfs_user_props = userprops;
396 return (0);
399 static int
400 get_stats(zfs_handle_t *zhp)
402 int rc = 0;
403 zfs_cmd_t zc = { 0 };
405 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
406 return (-1);
407 if (get_stats_ioctl(zhp, &zc) != 0)
408 rc = -1;
409 else if (put_stats_zhdl(zhp, &zc) != 0)
410 rc = -1;
411 zcmd_free_nvlists(&zc);
412 return (rc);
416 * Refresh the properties currently stored in the handle.
418 void
419 zfs_refresh_properties(zfs_handle_t *zhp)
421 (void) get_stats(zhp);
425 * Makes a handle from the given dataset name. Used by zfs_open() and
426 * zfs_iter_* to create child handles on the fly.
428 static int
429 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
431 if (put_stats_zhdl(zhp, zc) != 0)
432 return (-1);
435 * We've managed to open the dataset and gather statistics. Determine
436 * the high-level type.
438 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
439 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
440 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
441 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
442 else
443 abort();
445 if (zhp->zfs_dmustats.dds_is_snapshot)
446 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
447 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
448 zhp->zfs_type = ZFS_TYPE_VOLUME;
449 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
450 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
451 else
452 abort(); /* we should never see any other types */
454 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
455 return (-1);
457 return (0);
460 zfs_handle_t *
461 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
463 zfs_cmd_t zc = { 0 };
465 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
467 if (zhp == NULL)
468 return (NULL);
470 zhp->zfs_hdl = hdl;
471 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
472 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
473 free(zhp);
474 return (NULL);
476 if (get_stats_ioctl(zhp, &zc) == -1) {
477 zcmd_free_nvlists(&zc);
478 free(zhp);
479 return (NULL);
481 if (make_dataset_handle_common(zhp, &zc) == -1) {
482 free(zhp);
483 zhp = NULL;
485 zcmd_free_nvlists(&zc);
486 return (zhp);
489 zfs_handle_t *
490 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
492 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
494 if (zhp == NULL)
495 return (NULL);
497 zhp->zfs_hdl = hdl;
498 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
499 if (make_dataset_handle_common(zhp, zc) == -1) {
500 free(zhp);
501 return (NULL);
503 return (zhp);
506 zfs_handle_t *
507 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
509 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
511 if (zhp == NULL)
512 return (NULL);
514 zhp->zfs_hdl = pzhp->zfs_hdl;
515 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
516 zhp->zfs_head_type = pzhp->zfs_type;
517 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
518 zhp->zpool_hdl = zpool_handle(zhp);
519 return (zhp);
522 zfs_handle_t *
523 zfs_handle_dup(zfs_handle_t *zhp_orig)
525 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
527 if (zhp == NULL)
528 return (NULL);
530 zhp->zfs_hdl = zhp_orig->zfs_hdl;
531 zhp->zpool_hdl = zhp_orig->zpool_hdl;
532 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
533 sizeof (zhp->zfs_name));
534 zhp->zfs_type = zhp_orig->zfs_type;
535 zhp->zfs_head_type = zhp_orig->zfs_head_type;
536 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
537 if (zhp_orig->zfs_props != NULL) {
538 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
539 (void) no_memory(zhp->zfs_hdl);
540 zfs_close(zhp);
541 return (NULL);
544 if (zhp_orig->zfs_user_props != NULL) {
545 if (nvlist_dup(zhp_orig->zfs_user_props,
546 &zhp->zfs_user_props, 0) != 0) {
547 (void) no_memory(zhp->zfs_hdl);
548 zfs_close(zhp);
549 return (NULL);
552 if (zhp_orig->zfs_recvd_props != NULL) {
553 if (nvlist_dup(zhp_orig->zfs_recvd_props,
554 &zhp->zfs_recvd_props, 0)) {
555 (void) no_memory(zhp->zfs_hdl);
556 zfs_close(zhp);
557 return (NULL);
560 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
561 if (zhp_orig->zfs_mntopts != NULL) {
562 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
563 zhp_orig->zfs_mntopts);
565 zhp->zfs_props_table = zhp_orig->zfs_props_table;
566 return (zhp);
569 boolean_t
570 zfs_bookmark_exists(const char *path)
572 nvlist_t *bmarks;
573 nvlist_t *props;
574 char fsname[ZFS_MAX_DATASET_NAME_LEN];
575 char *bmark_name;
576 char *pound;
577 int err;
578 boolean_t rv;
581 (void) strlcpy(fsname, path, sizeof (fsname));
582 pound = strchr(fsname, '#');
583 if (pound == NULL)
584 return (B_FALSE);
586 *pound = '\0';
587 bmark_name = pound + 1;
588 props = fnvlist_alloc();
589 err = lzc_get_bookmarks(fsname, props, &bmarks);
590 nvlist_free(props);
591 if (err != 0) {
592 nvlist_free(bmarks);
593 return (B_FALSE);
596 rv = nvlist_exists(bmarks, bmark_name);
597 nvlist_free(bmarks);
598 return (rv);
601 zfs_handle_t *
602 make_bookmark_handle(zfs_handle_t *parent, const char *path,
603 nvlist_t *bmark_props)
605 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
607 if (zhp == NULL)
608 return (NULL);
610 /* Fill in the name. */
611 zhp->zfs_hdl = parent->zfs_hdl;
612 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
614 /* Set the property lists. */
615 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
616 free(zhp);
617 return (NULL);
620 /* Set the types. */
621 zhp->zfs_head_type = parent->zfs_head_type;
622 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
624 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
625 nvlist_free(zhp->zfs_props);
626 free(zhp);
627 return (NULL);
630 return (zhp);
633 struct zfs_open_bookmarks_cb_data {
634 const char *path;
635 zfs_handle_t *zhp;
638 static int
639 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
641 struct zfs_open_bookmarks_cb_data *dp = data;
644 * Is it the one we are looking for?
646 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
648 * We found it. Save it and let the caller know we are done.
650 dp->zhp = zhp;
651 return (EEXIST);
655 * Not found. Close the handle and ask for another one.
657 zfs_close(zhp);
658 return (0);
662 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
663 * argument is a mask of acceptable types. The function will print an
664 * appropriate error message and return NULL if it can't be opened.
666 zfs_handle_t *
667 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
669 zfs_handle_t *zhp;
670 char errbuf[1024];
671 char *bookp;
673 (void) snprintf(errbuf, sizeof (errbuf),
674 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
677 * Validate the name before we even try to open it.
679 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
680 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
681 return (NULL);
685 * Bookmarks needs to be handled separately.
687 bookp = strchr(path, '#');
688 if (bookp == NULL) {
690 * Try to get stats for the dataset, which will tell us if it
691 * exists.
693 errno = 0;
694 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
695 (void) zfs_standard_error(hdl, errno, errbuf);
696 return (NULL);
698 } else {
699 char dsname[ZFS_MAX_DATASET_NAME_LEN];
700 zfs_handle_t *pzhp;
701 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
704 * We need to cut out '#' and everything after '#'
705 * to get the parent dataset name only.
707 assert(bookp - path < sizeof (dsname));
708 (void) strncpy(dsname, path, bookp - path);
709 dsname[bookp - path] = '\0';
712 * Create handle for the parent dataset.
714 errno = 0;
715 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
716 (void) zfs_standard_error(hdl, errno, errbuf);
717 return (NULL);
721 * Iterate bookmarks to find the right one.
723 errno = 0;
724 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
725 &cb_data) == 0) && (cb_data.zhp == NULL)) {
726 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
727 zfs_close(pzhp);
728 return (NULL);
730 if (cb_data.zhp == NULL) {
731 (void) zfs_standard_error(hdl, errno, errbuf);
732 zfs_close(pzhp);
733 return (NULL);
735 zhp = cb_data.zhp;
738 * Cleanup.
740 zfs_close(pzhp);
743 if (!(types & zhp->zfs_type)) {
744 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
745 zfs_close(zhp);
746 return (NULL);
749 return (zhp);
753 * Release a ZFS handle. Nothing to do but free the associated memory.
755 void
756 zfs_close(zfs_handle_t *zhp)
758 free(zhp->zfs_mntopts);
759 nvlist_free(zhp->zfs_props);
760 nvlist_free(zhp->zfs_user_props);
761 nvlist_free(zhp->zfs_recvd_props);
762 free(zhp);
765 typedef struct mnttab_node {
766 struct mnttab mtn_mt;
767 avl_node_t mtn_node;
768 } mnttab_node_t;
770 static int
771 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
773 const mnttab_node_t *mtn1 = arg1;
774 const mnttab_node_t *mtn2 = arg2;
775 int rv;
777 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
779 if (rv == 0)
780 return (0);
781 return (rv > 0 ? 1 : -1);
784 void
785 libzfs_mnttab_init(libzfs_handle_t *hdl)
787 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
788 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
789 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
792 void
793 libzfs_mnttab_update(libzfs_handle_t *hdl)
795 struct mnttab entry;
797 rewind(hdl->libzfs_mnttab);
798 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
799 mnttab_node_t *mtn;
801 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
802 continue;
803 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
804 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
805 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
806 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
807 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
808 avl_add(&hdl->libzfs_mnttab_cache, mtn);
812 void
813 libzfs_mnttab_fini(libzfs_handle_t *hdl)
815 void *cookie = NULL;
816 mnttab_node_t *mtn;
818 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
819 != NULL) {
820 free(mtn->mtn_mt.mnt_special);
821 free(mtn->mtn_mt.mnt_mountp);
822 free(mtn->mtn_mt.mnt_fstype);
823 free(mtn->mtn_mt.mnt_mntopts);
824 free(mtn);
826 avl_destroy(&hdl->libzfs_mnttab_cache);
829 void
830 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
832 hdl->libzfs_mnttab_enable = enable;
836 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
837 struct mnttab *entry)
839 mnttab_node_t find;
840 mnttab_node_t *mtn;
842 if (!hdl->libzfs_mnttab_enable) {
843 struct mnttab srch = { 0 };
845 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
846 libzfs_mnttab_fini(hdl);
847 rewind(hdl->libzfs_mnttab);
848 srch.mnt_special = (char *)fsname;
849 srch.mnt_fstype = MNTTYPE_ZFS;
850 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
851 return (0);
852 else
853 return (ENOENT);
856 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
857 libzfs_mnttab_update(hdl);
859 find.mtn_mt.mnt_special = (char *)fsname;
860 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
861 if (mtn) {
862 *entry = mtn->mtn_mt;
863 return (0);
865 return (ENOENT);
868 void
869 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
870 const char *mountp, const char *mntopts)
872 mnttab_node_t *mtn;
874 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
875 return;
876 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
877 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
878 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
879 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
880 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
881 avl_add(&hdl->libzfs_mnttab_cache, mtn);
884 void
885 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
887 mnttab_node_t find;
888 mnttab_node_t *ret;
890 find.mtn_mt.mnt_special = (char *)fsname;
891 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
892 != NULL) {
893 avl_remove(&hdl->libzfs_mnttab_cache, ret);
894 free(ret->mtn_mt.mnt_special);
895 free(ret->mtn_mt.mnt_mountp);
896 free(ret->mtn_mt.mnt_fstype);
897 free(ret->mtn_mt.mnt_mntopts);
898 free(ret);
903 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
905 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
907 if (zpool_handle == NULL)
908 return (-1);
910 *spa_version = zpool_get_prop_int(zpool_handle,
911 ZPOOL_PROP_VERSION, NULL);
912 return (0);
916 * The choice of reservation property depends on the SPA version.
918 static int
919 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
921 int spa_version;
923 if (zfs_spa_version(zhp, &spa_version) < 0)
924 return (-1);
926 if (spa_version >= SPA_VERSION_REFRESERVATION)
927 *resv_prop = ZFS_PROP_REFRESERVATION;
928 else
929 *resv_prop = ZFS_PROP_RESERVATION;
931 return (0);
935 * Given an nvlist of properties to set, validates that they are correct, and
936 * parses any numeric properties (index, boolean, etc) if they are specified as
937 * strings.
939 nvlist_t *
940 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
941 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
942 const char *errbuf)
944 nvpair_t *elem;
945 uint64_t intval;
946 char *strval;
947 zfs_prop_t prop;
948 nvlist_t *ret;
949 int chosen_normal = -1;
950 int chosen_utf = -1;
952 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
953 (void) no_memory(hdl);
954 return (NULL);
958 * Make sure this property is valid and applies to this type.
961 elem = NULL;
962 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
963 const char *propname = nvpair_name(elem);
965 prop = zfs_name_to_prop(propname);
966 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
968 * This is a user property: make sure it's a
969 * string, and that it's less than ZAP_MAXNAMELEN.
971 if (nvpair_type(elem) != DATA_TYPE_STRING) {
972 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
973 "'%s' must be a string"), propname);
974 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
975 goto error;
978 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
980 "property name '%s' is too long"),
981 propname);
982 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
983 goto error;
986 (void) nvpair_value_string(elem, &strval);
987 if (nvlist_add_string(ret, propname, strval) != 0) {
988 (void) no_memory(hdl);
989 goto error;
991 continue;
995 * Currently, only user properties can be modified on
996 * snapshots.
998 if (type == ZFS_TYPE_SNAPSHOT) {
999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1000 "this property can not be modified for snapshots"));
1001 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1002 goto error;
1005 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1006 zfs_userquota_prop_t uqtype;
1007 char newpropname[128];
1008 char domain[128];
1009 uint64_t rid;
1010 uint64_t valary[3];
1012 if (userquota_propname_decode(propname, zoned,
1013 &uqtype, domain, sizeof (domain), &rid) != 0) {
1014 zfs_error_aux(hdl,
1015 dgettext(TEXT_DOMAIN,
1016 "'%s' has an invalid user/group name"),
1017 propname);
1018 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1019 goto error;
1022 if (uqtype != ZFS_PROP_USERQUOTA &&
1023 uqtype != ZFS_PROP_GROUPQUOTA) {
1024 zfs_error_aux(hdl,
1025 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1026 propname);
1027 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1028 errbuf);
1029 goto error;
1032 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1033 (void) nvpair_value_string(elem, &strval);
1034 if (strcmp(strval, "none") == 0) {
1035 intval = 0;
1036 } else if (zfs_nicestrtonum(hdl,
1037 strval, &intval) != 0) {
1038 (void) zfs_error(hdl,
1039 EZFS_BADPROP, errbuf);
1040 goto error;
1042 } else if (nvpair_type(elem) ==
1043 DATA_TYPE_UINT64) {
1044 (void) nvpair_value_uint64(elem, &intval);
1045 if (intval == 0) {
1046 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1047 "use 'none' to disable "
1048 "userquota/groupquota"));
1049 goto error;
1051 } else {
1052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1053 "'%s' must be a number"), propname);
1054 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1055 goto error;
1059 * Encode the prop name as
1060 * userquota@<hex-rid>-domain, to make it easy
1061 * for the kernel to decode.
1063 (void) snprintf(newpropname, sizeof (newpropname),
1064 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1065 (longlong_t)rid, domain);
1066 valary[0] = uqtype;
1067 valary[1] = rid;
1068 valary[2] = intval;
1069 if (nvlist_add_uint64_array(ret, newpropname,
1070 valary, 3) != 0) {
1071 (void) no_memory(hdl);
1072 goto error;
1074 continue;
1075 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1077 "'%s' is readonly"),
1078 propname);
1079 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1080 goto error;
1083 if (prop == ZPROP_INVAL) {
1084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1085 "invalid property '%s'"), propname);
1086 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1087 goto error;
1090 if (!zfs_prop_valid_for_type(prop, type)) {
1091 zfs_error_aux(hdl,
1092 dgettext(TEXT_DOMAIN, "'%s' does not "
1093 "apply to datasets of this type"), propname);
1094 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1095 goto error;
1098 if (zfs_prop_readonly(prop) &&
1099 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1100 zfs_error_aux(hdl,
1101 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1102 propname);
1103 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1104 goto error;
1107 if (zprop_parse_value(hdl, elem, prop, type, ret,
1108 &strval, &intval, errbuf) != 0)
1109 goto error;
1112 * Perform some additional checks for specific properties.
1114 switch (prop) {
1115 case ZFS_PROP_VERSION:
1117 int version;
1119 if (zhp == NULL)
1120 break;
1121 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1122 if (intval < version) {
1123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1124 "Can not downgrade; already at version %u"),
1125 version);
1126 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1127 goto error;
1129 break;
1132 case ZFS_PROP_VOLBLOCKSIZE:
1133 case ZFS_PROP_RECORDSIZE:
1135 int maxbs = SPA_MAXBLOCKSIZE;
1136 if (zpool_hdl != NULL) {
1137 maxbs = zpool_get_prop_int(zpool_hdl,
1138 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1141 * Volumes are limited to a volblocksize of 128KB,
1142 * because they typically service workloads with
1143 * small random writes, which incur a large performance
1144 * penalty with large blocks.
1146 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1147 maxbs = SPA_OLD_MAXBLOCKSIZE;
1149 * The value must be a power of two between
1150 * SPA_MINBLOCKSIZE and maxbs.
1152 if (intval < SPA_MINBLOCKSIZE ||
1153 intval > maxbs || !ISP2(intval)) {
1154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1155 "'%s' must be power of 2 from 512B "
1156 "to %uKB"), propname, maxbs >> 10);
1157 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1158 goto error;
1160 break;
1162 case ZFS_PROP_MOUNTPOINT:
1164 namecheck_err_t why;
1166 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1167 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1168 break;
1170 if (mountpoint_namecheck(strval, &why)) {
1171 switch (why) {
1172 case NAME_ERR_LEADING_SLASH:
1173 zfs_error_aux(hdl,
1174 dgettext(TEXT_DOMAIN,
1175 "'%s' must be an absolute path, "
1176 "'none', or 'legacy'"), propname);
1177 break;
1178 case NAME_ERR_TOOLONG:
1179 zfs_error_aux(hdl,
1180 dgettext(TEXT_DOMAIN,
1181 "component of '%s' is too long"),
1182 propname);
1183 break;
1185 default:
1186 zfs_error_aux(hdl,
1187 dgettext(TEXT_DOMAIN,
1188 "(%d) not defined"),
1189 why);
1190 break;
1192 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1193 goto error;
1197 /*FALLTHRU*/
1199 case ZFS_PROP_SHARESMB:
1200 case ZFS_PROP_SHARENFS:
1202 * For the mountpoint and sharenfs or sharesmb
1203 * properties, check if it can be set in a
1204 * global/non-global zone based on
1205 * the zoned property value:
1207 * global zone non-global zone
1208 * --------------------------------------------------
1209 * zoned=on mountpoint (no) mountpoint (yes)
1210 * sharenfs (no) sharenfs (no)
1211 * sharesmb (no) sharesmb (no)
1213 * zoned=off mountpoint (yes) N/A
1214 * sharenfs (yes)
1215 * sharesmb (yes)
1217 if (zoned) {
1218 if (getzoneid() == GLOBAL_ZONEID) {
1219 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1220 "'%s' cannot be set on "
1221 "dataset in a non-global zone"),
1222 propname);
1223 (void) zfs_error(hdl, EZFS_ZONED,
1224 errbuf);
1225 goto error;
1226 } else if (prop == ZFS_PROP_SHARENFS ||
1227 prop == ZFS_PROP_SHARESMB) {
1228 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1229 "'%s' cannot be set in "
1230 "a non-global zone"), propname);
1231 (void) zfs_error(hdl, EZFS_ZONED,
1232 errbuf);
1233 goto error;
1235 } else if (getzoneid() != GLOBAL_ZONEID) {
1237 * If zoned property is 'off', this must be in
1238 * a global zone. If not, something is wrong.
1240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1241 "'%s' cannot be set while dataset "
1242 "'zoned' property is set"), propname);
1243 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1244 goto error;
1248 * At this point, it is legitimate to set the
1249 * property. Now we want to make sure that the
1250 * property value is valid if it is sharenfs.
1252 if ((prop == ZFS_PROP_SHARENFS ||
1253 prop == ZFS_PROP_SHARESMB) &&
1254 strcmp(strval, "on") != 0 &&
1255 strcmp(strval, "off") != 0) {
1256 zfs_share_proto_t proto;
1258 if (prop == ZFS_PROP_SHARESMB)
1259 proto = PROTO_SMB;
1260 else
1261 proto = PROTO_NFS;
1264 * Must be an valid sharing protocol
1265 * option string so init the libshare
1266 * in order to enable the parser and
1267 * then parse the options. We use the
1268 * control API since we don't care about
1269 * the current configuration and don't
1270 * want the overhead of loading it
1271 * until we actually do something.
1274 if (zfs_init_libshare(hdl,
1275 SA_INIT_CONTROL_API) != SA_OK) {
1277 * An error occurred so we can't do
1278 * anything
1280 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1281 "'%s' cannot be set: problem "
1282 "in share initialization"),
1283 propname);
1284 (void) zfs_error(hdl, EZFS_BADPROP,
1285 errbuf);
1286 goto error;
1289 if (zfs_parse_options(strval, proto) != SA_OK) {
1291 * There was an error in parsing so
1292 * deal with it by issuing an error
1293 * message and leaving after
1294 * uninitializing the the libshare
1295 * interface.
1297 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1298 "'%s' cannot be set to invalid "
1299 "options"), propname);
1300 (void) zfs_error(hdl, EZFS_BADPROP,
1301 errbuf);
1302 zfs_uninit_libshare(hdl);
1303 goto error;
1305 zfs_uninit_libshare(hdl);
1308 break;
1310 case ZFS_PROP_UTF8ONLY:
1311 chosen_utf = (int)intval;
1312 break;
1314 case ZFS_PROP_NORMALIZE:
1315 chosen_normal = (int)intval;
1316 break;
1318 default:
1319 break;
1323 * For changes to existing volumes, we have some additional
1324 * checks to enforce.
1326 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1327 uint64_t volsize = zfs_prop_get_int(zhp,
1328 ZFS_PROP_VOLSIZE);
1329 uint64_t blocksize = zfs_prop_get_int(zhp,
1330 ZFS_PROP_VOLBLOCKSIZE);
1331 char buf[64];
1333 switch (prop) {
1334 case ZFS_PROP_RESERVATION:
1335 if (intval > volsize) {
1336 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1337 "'%s' is greater than current "
1338 "volume size"), propname);
1339 (void) zfs_error(hdl, EZFS_BADPROP,
1340 errbuf);
1341 goto error;
1343 break;
1345 case ZFS_PROP_REFRESERVATION:
1346 if (intval > volsize && intval != UINT64_MAX) {
1347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1348 "'%s' is greater than current "
1349 "volume size"), propname);
1350 (void) zfs_error(hdl, EZFS_BADPROP,
1351 errbuf);
1352 goto error;
1354 break;
1356 case ZFS_PROP_VOLSIZE:
1357 if (intval % blocksize != 0) {
1358 zfs_nicenum(blocksize, buf,
1359 sizeof (buf));
1360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1361 "'%s' must be a multiple of "
1362 "volume block size (%s)"),
1363 propname, buf);
1364 (void) zfs_error(hdl, EZFS_BADPROP,
1365 errbuf);
1366 goto error;
1369 if (intval == 0) {
1370 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1371 "'%s' cannot be zero"),
1372 propname);
1373 (void) zfs_error(hdl, EZFS_BADPROP,
1374 errbuf);
1375 goto error;
1377 break;
1379 default:
1380 break;
1386 * If normalization was chosen, but no UTF8 choice was made,
1387 * enforce rejection of non-UTF8 names.
1389 * If normalization was chosen, but rejecting non-UTF8 names
1390 * was explicitly not chosen, it is an error.
1392 if (chosen_normal > 0 && chosen_utf < 0) {
1393 if (nvlist_add_uint64(ret,
1394 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1395 (void) no_memory(hdl);
1396 goto error;
1398 } else if (chosen_normal > 0 && chosen_utf == 0) {
1399 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1400 "'%s' must be set 'on' if normalization chosen"),
1401 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1402 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1403 goto error;
1405 return (ret);
1407 error:
1408 nvlist_free(ret);
1409 return (NULL);
1413 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1415 uint64_t old_volsize;
1416 uint64_t new_volsize;
1417 uint64_t old_reservation;
1418 uint64_t new_reservation;
1419 zfs_prop_t resv_prop;
1420 nvlist_t *props;
1423 * If this is an existing volume, and someone is setting the volsize,
1424 * make sure that it matches the reservation, or add it if necessary.
1426 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1427 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1428 return (-1);
1429 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1431 props = fnvlist_alloc();
1432 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1433 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1435 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1436 old_reservation) || nvlist_exists(nvl,
1437 zfs_prop_to_name(resv_prop))) {
1438 fnvlist_free(props);
1439 return (0);
1441 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1442 &new_volsize) != 0) {
1443 fnvlist_free(props);
1444 return (-1);
1446 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1447 fnvlist_free(props);
1449 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1450 new_reservation) != 0) {
1451 (void) no_memory(zhp->zfs_hdl);
1452 return (-1);
1454 return (1);
1458 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1459 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1460 * Return codes must match zfs_add_synthetic_resv().
1462 static int
1463 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1465 uint64_t volsize;
1466 uint64_t resvsize;
1467 zfs_prop_t prop;
1468 nvlist_t *props;
1470 if (!ZFS_IS_VOLUME(zhp)) {
1471 return (0);
1474 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1475 return (-1);
1478 if (prop != ZFS_PROP_REFRESERVATION) {
1479 return (0);
1482 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1483 /* No value being set, so it can't be "auto" */
1484 return (0);
1486 if (resvsize != UINT64_MAX) {
1487 /* Being set to a value other than "auto" */
1488 return (0);
1491 props = fnvlist_alloc();
1493 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1494 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1496 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1497 &volsize) != 0) {
1498 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1501 resvsize = zvol_volsize_to_reservation(volsize, props);
1502 fnvlist_free(props);
1504 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1505 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1506 (void) no_memory(zhp->zfs_hdl);
1507 return (-1);
1509 return (1);
1512 void
1513 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1514 char *errbuf)
1516 switch (err) {
1518 case ENOSPC:
1520 * For quotas and reservations, ENOSPC indicates
1521 * something different; setting a quota or reservation
1522 * doesn't use any disk space.
1524 switch (prop) {
1525 case ZFS_PROP_QUOTA:
1526 case ZFS_PROP_REFQUOTA:
1527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1528 "size is less than current used or "
1529 "reserved space"));
1530 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1531 break;
1533 case ZFS_PROP_RESERVATION:
1534 case ZFS_PROP_REFRESERVATION:
1535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1536 "size is greater than available space"));
1537 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1538 break;
1540 default:
1541 (void) zfs_standard_error(hdl, err, errbuf);
1542 break;
1544 break;
1546 case EBUSY:
1547 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1548 break;
1550 case EROFS:
1551 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1552 break;
1554 case E2BIG:
1555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1556 "property value too long"));
1557 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1558 break;
1560 case ENOTSUP:
1561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562 "pool and or dataset must be upgraded to set this "
1563 "property or value"));
1564 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1565 break;
1567 case ERANGE:
1568 if (prop == ZFS_PROP_COMPRESSION ||
1569 prop == ZFS_PROP_RECORDSIZE) {
1570 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1571 "property setting is not allowed on "
1572 "bootable datasets"));
1573 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1574 } else if (prop == ZFS_PROP_CHECKSUM ||
1575 prop == ZFS_PROP_DEDUP) {
1576 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1577 "property setting is not allowed on "
1578 "root pools"));
1579 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1580 } else {
1581 (void) zfs_standard_error(hdl, err, errbuf);
1583 break;
1585 case EINVAL:
1586 if (prop == ZPROP_INVAL) {
1587 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1588 } else {
1589 (void) zfs_standard_error(hdl, err, errbuf);
1591 break;
1593 case EOVERFLOW:
1595 * This platform can't address a volume this big.
1597 #ifdef _ILP32
1598 if (prop == ZFS_PROP_VOLSIZE) {
1599 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1600 break;
1602 #endif
1603 /* FALLTHROUGH */
1604 default:
1605 (void) zfs_standard_error(hdl, err, errbuf);
1610 * Given a property name and value, set the property for the given dataset.
1613 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1615 int ret = -1;
1616 char errbuf[1024];
1617 libzfs_handle_t *hdl = zhp->zfs_hdl;
1618 nvlist_t *nvl = NULL;
1620 (void) snprintf(errbuf, sizeof (errbuf),
1621 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1622 zhp->zfs_name);
1624 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1625 nvlist_add_string(nvl, propname, propval) != 0) {
1626 (void) no_memory(hdl);
1627 goto error;
1630 ret = zfs_prop_set_list(zhp, nvl);
1632 error:
1633 nvlist_free(nvl);
1634 return (ret);
1640 * Given an nvlist of property names and values, set the properties for the
1641 * given dataset.
1644 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1646 zfs_cmd_t zc = { 0 };
1647 int ret = -1;
1648 prop_changelist_t **cls = NULL;
1649 int cl_idx;
1650 char errbuf[1024];
1651 libzfs_handle_t *hdl = zhp->zfs_hdl;
1652 nvlist_t *nvl;
1653 int nvl_len;
1654 int added_resv = 0;
1656 (void) snprintf(errbuf, sizeof (errbuf),
1657 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1658 zhp->zfs_name);
1660 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1661 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1662 errbuf)) == NULL)
1663 goto error;
1666 * We have to check for any extra properties which need to be added
1667 * before computing the length of the nvlist.
1669 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1670 elem != NULL;
1671 elem = nvlist_next_nvpair(nvl, elem)) {
1672 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1673 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1674 goto error;
1678 if (added_resv != 1 &&
1679 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1680 goto error;
1684 * Check how many properties we're setting and allocate an array to
1685 * store changelist pointers for postfix().
1687 nvl_len = 0;
1688 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1689 elem != NULL;
1690 elem = nvlist_next_nvpair(nvl, elem))
1691 nvl_len++;
1692 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1693 goto error;
1695 cl_idx = 0;
1696 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1697 elem != NULL;
1698 elem = nvlist_next_nvpair(nvl, elem)) {
1700 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1702 assert(cl_idx < nvl_len);
1704 * We don't want to unmount & remount the dataset when changing
1705 * its canmount property to 'on' or 'noauto'. We only use
1706 * the changelist logic to unmount when setting canmount=off.
1708 if (prop != ZFS_PROP_CANMOUNT ||
1709 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1710 zfs_is_mounted(zhp, NULL))) {
1711 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1712 if (cls[cl_idx] == NULL)
1713 goto error;
1716 if (prop == ZFS_PROP_MOUNTPOINT &&
1717 changelist_haszonedchild(cls[cl_idx])) {
1718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1719 "child dataset with inherited mountpoint is used "
1720 "in a non-global zone"));
1721 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1722 goto error;
1725 if (cls[cl_idx] != NULL &&
1726 (ret = changelist_prefix(cls[cl_idx])) != 0)
1727 goto error;
1729 cl_idx++;
1731 assert(cl_idx == nvl_len);
1734 * Execute the corresponding ioctl() to set this list of properties.
1736 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1738 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1739 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1740 goto error;
1742 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1744 if (ret != 0) {
1745 /* Get the list of unset properties back and report them. */
1746 nvlist_t *errorprops = NULL;
1747 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1748 goto error;
1749 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1750 elem != NULL;
1751 elem = nvlist_next_nvpair(nvl, elem)) {
1752 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1753 zfs_setprop_error(hdl, prop, errno, errbuf);
1755 nvlist_free(errorprops);
1757 if (added_resv && errno == ENOSPC) {
1758 /* clean up the volsize property we tried to set */
1759 uint64_t old_volsize = zfs_prop_get_int(zhp,
1760 ZFS_PROP_VOLSIZE);
1761 nvlist_free(nvl);
1762 nvl = NULL;
1763 zcmd_free_nvlists(&zc);
1765 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1766 goto error;
1767 if (nvlist_add_uint64(nvl,
1768 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1769 old_volsize) != 0)
1770 goto error;
1771 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1772 goto error;
1773 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1775 } else {
1776 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1777 if (cls[cl_idx] != NULL) {
1778 int clp_err = changelist_postfix(cls[cl_idx]);
1779 if (clp_err != 0)
1780 ret = clp_err;
1785 * Refresh the statistics so the new property value
1786 * is reflected.
1788 if (ret == 0)
1789 (void) get_stats(zhp);
1792 error:
1793 nvlist_free(nvl);
1794 zcmd_free_nvlists(&zc);
1795 if (cls != NULL) {
1796 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1797 if (cls[cl_idx] != NULL)
1798 changelist_free(cls[cl_idx]);
1800 free(cls);
1802 return (ret);
1806 * Given a property, inherit the value from the parent dataset, or if received
1807 * is TRUE, revert to the received value, if any.
1810 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1812 zfs_cmd_t zc = { 0 };
1813 int ret;
1814 prop_changelist_t *cl;
1815 libzfs_handle_t *hdl = zhp->zfs_hdl;
1816 char errbuf[1024];
1817 zfs_prop_t prop;
1819 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1820 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1822 zc.zc_cookie = received;
1823 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1825 * For user properties, the amount of work we have to do is very
1826 * small, so just do it here.
1828 if (!zfs_prop_user(propname)) {
1829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1830 "invalid property"));
1831 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1834 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1835 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1837 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1838 return (zfs_standard_error(hdl, errno, errbuf));
1840 return (0);
1844 * Verify that this property is inheritable.
1846 if (zfs_prop_readonly(prop))
1847 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1849 if (!zfs_prop_inheritable(prop) && !received)
1850 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1853 * Check to see if the value applies to this type
1855 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1856 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1859 * Normalize the name, to get rid of shorthand abbreviations.
1861 propname = zfs_prop_to_name(prop);
1862 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1863 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1865 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1866 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1867 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1868 "dataset is used in a non-global zone"));
1869 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1873 * Determine datasets which will be affected by this change, if any.
1875 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1876 return (-1);
1878 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1879 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1880 "child dataset with inherited mountpoint is used "
1881 "in a non-global zone"));
1882 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1883 goto error;
1886 if ((ret = changelist_prefix(cl)) != 0)
1887 goto error;
1889 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1890 return (zfs_standard_error(hdl, errno, errbuf));
1891 } else {
1893 if ((ret = changelist_postfix(cl)) != 0)
1894 goto error;
1897 * Refresh the statistics so the new property is reflected.
1899 (void) get_stats(zhp);
1902 error:
1903 changelist_free(cl);
1904 return (ret);
1908 * True DSL properties are stored in an nvlist. The following two functions
1909 * extract them appropriately.
1911 static uint64_t
1912 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1914 nvlist_t *nv;
1915 uint64_t value;
1917 *source = NULL;
1918 if (nvlist_lookup_nvlist(zhp->zfs_props,
1919 zfs_prop_to_name(prop), &nv) == 0) {
1920 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1921 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1922 } else {
1923 verify(!zhp->zfs_props_table ||
1924 zhp->zfs_props_table[prop] == B_TRUE);
1925 value = zfs_prop_default_numeric(prop);
1926 *source = "";
1929 return (value);
1932 static const char *
1933 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1935 nvlist_t *nv;
1936 const char *value;
1938 *source = NULL;
1939 if (nvlist_lookup_nvlist(zhp->zfs_props,
1940 zfs_prop_to_name(prop), &nv) == 0) {
1941 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1942 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1943 } else {
1944 verify(!zhp->zfs_props_table ||
1945 zhp->zfs_props_table[prop] == B_TRUE);
1946 value = zfs_prop_default_string(prop);
1947 *source = "";
1950 return (value);
1953 static boolean_t
1954 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1956 return (zhp->zfs_props == zhp->zfs_recvd_props);
1959 static void
1960 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1962 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1963 zhp->zfs_props = zhp->zfs_recvd_props;
1966 static void
1967 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1969 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1970 *cookie = 0;
1974 * Internal function for getting a numeric property. Both zfs_prop_get() and
1975 * zfs_prop_get_int() are built using this interface.
1977 * Certain properties can be overridden using 'mount -o'. In this case, scan
1978 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1979 * If they differ from the on-disk values, report the current values and mark
1980 * the source "temporary".
1982 static int
1983 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1984 char **source, uint64_t *val)
1986 zfs_cmd_t zc = { 0 };
1987 nvlist_t *zplprops = NULL;
1988 struct mnttab mnt;
1989 char *mntopt_on = NULL;
1990 char *mntopt_off = NULL;
1991 boolean_t received = zfs_is_recvd_props_mode(zhp);
1993 *source = NULL;
1995 switch (prop) {
1996 case ZFS_PROP_ATIME:
1997 mntopt_on = MNTOPT_ATIME;
1998 mntopt_off = MNTOPT_NOATIME;
1999 break;
2001 case ZFS_PROP_DEVICES:
2002 mntopt_on = MNTOPT_DEVICES;
2003 mntopt_off = MNTOPT_NODEVICES;
2004 break;
2006 case ZFS_PROP_EXEC:
2007 mntopt_on = MNTOPT_EXEC;
2008 mntopt_off = MNTOPT_NOEXEC;
2009 break;
2011 case ZFS_PROP_READONLY:
2012 mntopt_on = MNTOPT_RO;
2013 mntopt_off = MNTOPT_RW;
2014 break;
2016 case ZFS_PROP_SETUID:
2017 mntopt_on = MNTOPT_SETUID;
2018 mntopt_off = MNTOPT_NOSETUID;
2019 break;
2021 case ZFS_PROP_XATTR:
2022 mntopt_on = MNTOPT_XATTR;
2023 mntopt_off = MNTOPT_NOXATTR;
2024 break;
2026 case ZFS_PROP_NBMAND:
2027 mntopt_on = MNTOPT_NBMAND;
2028 mntopt_off = MNTOPT_NONBMAND;
2029 break;
2031 default:
2032 break;
2036 * Because looking up the mount options is potentially expensive
2037 * (iterating over all of /etc/mnttab), we defer its calculation until
2038 * we're looking up a property which requires its presence.
2040 if (!zhp->zfs_mntcheck &&
2041 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2042 libzfs_handle_t *hdl = zhp->zfs_hdl;
2043 struct mnttab entry;
2045 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2046 zhp->zfs_mntopts = zfs_strdup(hdl,
2047 entry.mnt_mntopts);
2048 if (zhp->zfs_mntopts == NULL)
2049 return (-1);
2052 zhp->zfs_mntcheck = B_TRUE;
2055 if (zhp->zfs_mntopts == NULL)
2056 mnt.mnt_mntopts = "";
2057 else
2058 mnt.mnt_mntopts = zhp->zfs_mntopts;
2060 switch (prop) {
2061 case ZFS_PROP_ATIME:
2062 case ZFS_PROP_DEVICES:
2063 case ZFS_PROP_EXEC:
2064 case ZFS_PROP_READONLY:
2065 case ZFS_PROP_SETUID:
2066 case ZFS_PROP_XATTR:
2067 case ZFS_PROP_NBMAND:
2068 *val = getprop_uint64(zhp, prop, source);
2070 if (received)
2071 break;
2073 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2074 *val = B_TRUE;
2075 if (src)
2076 *src = ZPROP_SRC_TEMPORARY;
2077 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2078 *val = B_FALSE;
2079 if (src)
2080 *src = ZPROP_SRC_TEMPORARY;
2082 break;
2084 case ZFS_PROP_CANMOUNT:
2085 case ZFS_PROP_VOLSIZE:
2086 case ZFS_PROP_QUOTA:
2087 case ZFS_PROP_REFQUOTA:
2088 case ZFS_PROP_RESERVATION:
2089 case ZFS_PROP_REFRESERVATION:
2090 case ZFS_PROP_FILESYSTEM_LIMIT:
2091 case ZFS_PROP_SNAPSHOT_LIMIT:
2092 case ZFS_PROP_FILESYSTEM_COUNT:
2093 case ZFS_PROP_SNAPSHOT_COUNT:
2094 *val = getprop_uint64(zhp, prop, source);
2096 if (*source == NULL) {
2097 /* not default, must be local */
2098 *source = zhp->zfs_name;
2100 break;
2102 case ZFS_PROP_MOUNTED:
2103 *val = (zhp->zfs_mntopts != NULL);
2104 break;
2106 case ZFS_PROP_NUMCLONES:
2107 *val = zhp->zfs_dmustats.dds_num_clones;
2108 break;
2110 case ZFS_PROP_VERSION:
2111 case ZFS_PROP_NORMALIZE:
2112 case ZFS_PROP_UTF8ONLY:
2113 case ZFS_PROP_CASE:
2114 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2115 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2116 return (-1);
2117 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2118 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2119 zcmd_free_nvlists(&zc);
2120 return (-1);
2122 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2123 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2124 val) != 0) {
2125 zcmd_free_nvlists(&zc);
2126 return (-1);
2128 nvlist_free(zplprops);
2129 zcmd_free_nvlists(&zc);
2130 break;
2132 case ZFS_PROP_INCONSISTENT:
2133 *val = zhp->zfs_dmustats.dds_inconsistent;
2134 break;
2136 default:
2137 switch (zfs_prop_get_type(prop)) {
2138 case PROP_TYPE_NUMBER:
2139 case PROP_TYPE_INDEX:
2140 *val = getprop_uint64(zhp, prop, source);
2142 * If we tried to use a default value for a
2143 * readonly property, it means that it was not
2144 * present. Note this only applies to "truly"
2145 * readonly properties, not set-once properties
2146 * like volblocksize.
2148 if (zfs_prop_readonly(prop) &&
2149 !zfs_prop_setonce(prop) &&
2150 *source != NULL && (*source)[0] == '\0') {
2151 *source = NULL;
2152 return (-1);
2154 break;
2156 case PROP_TYPE_STRING:
2157 default:
2158 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2159 "cannot get non-numeric property"));
2160 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2161 dgettext(TEXT_DOMAIN, "internal error")));
2165 return (0);
2169 * Calculate the source type, given the raw source string.
2171 static void
2172 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2173 char *statbuf, size_t statlen)
2175 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2176 return;
2178 if (source == NULL) {
2179 *srctype = ZPROP_SRC_NONE;
2180 } else if (source[0] == '\0') {
2181 *srctype = ZPROP_SRC_DEFAULT;
2182 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2183 *srctype = ZPROP_SRC_RECEIVED;
2184 } else {
2185 if (strcmp(source, zhp->zfs_name) == 0) {
2186 *srctype = ZPROP_SRC_LOCAL;
2187 } else {
2188 (void) strlcpy(statbuf, source, statlen);
2189 *srctype = ZPROP_SRC_INHERITED;
2196 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2197 size_t proplen, boolean_t literal)
2199 zfs_prop_t prop;
2200 int err = 0;
2202 if (zhp->zfs_recvd_props == NULL)
2203 if (get_recvd_props_ioctl(zhp) != 0)
2204 return (-1);
2206 prop = zfs_name_to_prop(propname);
2208 if (prop != ZPROP_INVAL) {
2209 uint64_t cookie;
2210 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2211 return (-1);
2212 zfs_set_recvd_props_mode(zhp, &cookie);
2213 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2214 NULL, NULL, 0, literal);
2215 zfs_unset_recvd_props_mode(zhp, &cookie);
2216 } else {
2217 nvlist_t *propval;
2218 char *recvdval;
2219 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2220 propname, &propval) != 0)
2221 return (-1);
2222 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2223 &recvdval) == 0);
2224 (void) strlcpy(propbuf, recvdval, proplen);
2227 return (err == 0 ? 0 : -1);
2230 static int
2231 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2233 nvlist_t *value;
2234 nvpair_t *pair;
2236 value = zfs_get_clones_nvl(zhp);
2237 if (value == NULL)
2238 return (-1);
2240 propbuf[0] = '\0';
2241 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2242 pair = nvlist_next_nvpair(value, pair)) {
2243 if (propbuf[0] != '\0')
2244 (void) strlcat(propbuf, ",", proplen);
2245 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2248 return (0);
2251 struct get_clones_arg {
2252 uint64_t numclones;
2253 nvlist_t *value;
2254 const char *origin;
2255 char buf[ZFS_MAX_DATASET_NAME_LEN];
2259 get_clones_cb(zfs_handle_t *zhp, void *arg)
2261 struct get_clones_arg *gca = arg;
2263 if (gca->numclones == 0) {
2264 zfs_close(zhp);
2265 return (0);
2268 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2269 NULL, NULL, 0, B_TRUE) != 0)
2270 goto out;
2271 if (strcmp(gca->buf, gca->origin) == 0) {
2272 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2273 gca->numclones--;
2276 out:
2277 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2278 zfs_close(zhp);
2279 return (0);
2282 nvlist_t *
2283 zfs_get_clones_nvl(zfs_handle_t *zhp)
2285 nvlist_t *nv, *value;
2287 if (nvlist_lookup_nvlist(zhp->zfs_props,
2288 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2289 struct get_clones_arg gca;
2292 * if this is a snapshot, then the kernel wasn't able
2293 * to get the clones. Do it by slowly iterating.
2295 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2296 return (NULL);
2297 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2298 return (NULL);
2299 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2300 nvlist_free(nv);
2301 return (NULL);
2304 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2305 gca.value = value;
2306 gca.origin = zhp->zfs_name;
2308 if (gca.numclones != 0) {
2309 zfs_handle_t *root;
2310 char pool[ZFS_MAX_DATASET_NAME_LEN];
2311 char *cp = pool;
2313 /* get the pool name */
2314 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2315 (void) strsep(&cp, "/@");
2316 root = zfs_open(zhp->zfs_hdl, pool,
2317 ZFS_TYPE_FILESYSTEM);
2319 (void) get_clones_cb(root, &gca);
2322 if (gca.numclones != 0 ||
2323 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2324 nvlist_add_nvlist(zhp->zfs_props,
2325 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2326 nvlist_free(nv);
2327 nvlist_free(value);
2328 return (NULL);
2330 nvlist_free(nv);
2331 nvlist_free(value);
2332 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2333 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2336 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2338 return (value);
2342 * Accepts a property and value and checks that the value
2343 * matches the one found by the channel program. If they are
2344 * not equal, print both of them.
2346 void
2347 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2348 const char *strval)
2350 if (!zhp->zfs_hdl->libzfs_prop_debug)
2351 return;
2352 int error;
2353 char *poolname = zhp->zpool_hdl->zpool_name;
2354 const char *program =
2355 "args = ...\n"
2356 "ds = args['dataset']\n"
2357 "prop = args['property']\n"
2358 "value, setpoint = zfs.get_prop(ds, prop)\n"
2359 "return {value=value, setpoint=setpoint}\n";
2360 nvlist_t *outnvl;
2361 nvlist_t *retnvl;
2362 nvlist_t *argnvl = fnvlist_alloc();
2364 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2365 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2367 error = lzc_channel_program_nosync(poolname, program,
2368 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2370 if (error == 0) {
2371 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2372 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2373 int64_t ans;
2374 error = nvlist_lookup_int64(retnvl, "value", &ans);
2375 if (error != 0) {
2376 (void) fprintf(stderr, "zcp check error: %u\n",
2377 error);
2378 return;
2380 if (ans != intval) {
2381 (void) fprintf(stderr,
2382 "%s: zfs found %lld, but zcp found %lld\n",
2383 zfs_prop_to_name(prop),
2384 (longlong_t)intval, (longlong_t)ans);
2386 } else {
2387 char *str_ans;
2388 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2389 if (error != 0) {
2390 (void) fprintf(stderr, "zcp check error: %u\n",
2391 error);
2392 return;
2394 if (strcmp(strval, str_ans) != 0) {
2395 (void) fprintf(stderr,
2396 "%s: zfs found %s, but zcp found %s\n",
2397 zfs_prop_to_name(prop),
2398 strval, str_ans);
2401 } else {
2402 (void) fprintf(stderr,
2403 "zcp check failed, channel program error: %u\n", error);
2405 nvlist_free(argnvl);
2406 nvlist_free(outnvl);
2410 * Retrieve a property from the given object. If 'literal' is specified, then
2411 * numbers are left as exact values. Otherwise, numbers are converted to a
2412 * human-readable form.
2414 * Returns 0 on success, or -1 on error.
2417 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2418 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2420 char *source = NULL;
2421 uint64_t val;
2422 const char *str;
2423 const char *strval;
2424 boolean_t received = zfs_is_recvd_props_mode(zhp);
2427 * Check to see if this property applies to our object
2429 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2430 return (-1);
2432 if (received && zfs_prop_readonly(prop))
2433 return (-1);
2435 if (src)
2436 *src = ZPROP_SRC_NONE;
2438 switch (prop) {
2439 case ZFS_PROP_CREATION:
2441 * 'creation' is a time_t stored in the statistics. We convert
2442 * this into a string unless 'literal' is specified.
2445 val = getprop_uint64(zhp, prop, &source);
2446 time_t time = (time_t)val;
2447 struct tm t;
2449 if (literal ||
2450 localtime_r(&time, &t) == NULL ||
2451 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2452 &t) == 0)
2453 (void) snprintf(propbuf, proplen, "%llu", val);
2455 zcp_check(zhp, prop, val, NULL);
2456 break;
2458 case ZFS_PROP_MOUNTPOINT:
2460 * Getting the precise mountpoint can be tricky.
2462 * - for 'none' or 'legacy', return those values.
2463 * - for inherited mountpoints, we want to take everything
2464 * after our ancestor and append it to the inherited value.
2466 * If the pool has an alternate root, we want to prepend that
2467 * root to any values we return.
2470 str = getprop_string(zhp, prop, &source);
2472 if (str[0] == '/') {
2473 char buf[MAXPATHLEN];
2474 char *root = buf;
2475 const char *relpath;
2478 * If we inherit the mountpoint, even from a dataset
2479 * with a received value, the source will be the path of
2480 * the dataset we inherit from. If source is
2481 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2482 * inherited.
2484 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2485 relpath = "";
2486 } else {
2487 relpath = zhp->zfs_name + strlen(source);
2488 if (relpath[0] == '/')
2489 relpath++;
2492 if ((zpool_get_prop(zhp->zpool_hdl,
2493 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2494 B_FALSE)) || (strcmp(root, "-") == 0))
2495 root[0] = '\0';
2497 * Special case an alternate root of '/'. This will
2498 * avoid having multiple leading slashes in the
2499 * mountpoint path.
2501 if (strcmp(root, "/") == 0)
2502 root++;
2505 * If the mountpoint is '/' then skip over this
2506 * if we are obtaining either an alternate root or
2507 * an inherited mountpoint.
2509 if (str[1] == '\0' && (root[0] != '\0' ||
2510 relpath[0] != '\0'))
2511 str++;
2513 if (relpath[0] == '\0')
2514 (void) snprintf(propbuf, proplen, "%s%s",
2515 root, str);
2516 else
2517 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2518 root, str, relpath[0] == '@' ? "" : "/",
2519 relpath);
2520 } else {
2521 /* 'legacy' or 'none' */
2522 (void) strlcpy(propbuf, str, proplen);
2524 zcp_check(zhp, prop, 0, propbuf);
2525 break;
2527 case ZFS_PROP_ORIGIN:
2528 str = getprop_string(zhp, prop, &source);
2529 if (str == NULL)
2530 return (-1);
2531 (void) strlcpy(propbuf, str, proplen);
2532 zcp_check(zhp, prop, 0, str);
2533 break;
2535 case ZFS_PROP_CLONES:
2536 if (get_clones_string(zhp, propbuf, proplen) != 0)
2537 return (-1);
2538 break;
2540 case ZFS_PROP_QUOTA:
2541 case ZFS_PROP_REFQUOTA:
2542 case ZFS_PROP_RESERVATION:
2543 case ZFS_PROP_REFRESERVATION:
2545 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2546 return (-1);
2548 * If quota or reservation is 0, we translate this into 'none'
2549 * (unless literal is set), and indicate that it's the default
2550 * value. Otherwise, we print the number nicely and indicate
2551 * that its set locally.
2553 if (val == 0) {
2554 if (literal)
2555 (void) strlcpy(propbuf, "0", proplen);
2556 else
2557 (void) strlcpy(propbuf, "none", proplen);
2558 } else {
2559 if (literal)
2560 (void) snprintf(propbuf, proplen, "%llu",
2561 (u_longlong_t)val);
2562 else
2563 zfs_nicenum(val, propbuf, proplen);
2565 zcp_check(zhp, prop, val, NULL);
2566 break;
2568 case ZFS_PROP_FILESYSTEM_LIMIT:
2569 case ZFS_PROP_SNAPSHOT_LIMIT:
2570 case ZFS_PROP_FILESYSTEM_COUNT:
2571 case ZFS_PROP_SNAPSHOT_COUNT:
2573 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2574 return (-1);
2577 * If limit is UINT64_MAX, we translate this into 'none' (unless
2578 * literal is set), and indicate that it's the default value.
2579 * Otherwise, we print the number nicely and indicate that it's
2580 * set locally.
2582 if (literal) {
2583 (void) snprintf(propbuf, proplen, "%llu",
2584 (u_longlong_t)val);
2585 } else if (val == UINT64_MAX) {
2586 (void) strlcpy(propbuf, "none", proplen);
2587 } else {
2588 zfs_nicenum(val, propbuf, proplen);
2591 zcp_check(zhp, prop, val, NULL);
2592 break;
2594 case ZFS_PROP_REFRATIO:
2595 case ZFS_PROP_COMPRESSRATIO:
2596 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2597 return (-1);
2598 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2599 (u_longlong_t)(val / 100),
2600 (u_longlong_t)(val % 100));
2601 zcp_check(zhp, prop, val, NULL);
2602 break;
2604 case ZFS_PROP_TYPE:
2605 switch (zhp->zfs_type) {
2606 case ZFS_TYPE_FILESYSTEM:
2607 str = "filesystem";
2608 break;
2609 case ZFS_TYPE_VOLUME:
2610 str = "volume";
2611 break;
2612 case ZFS_TYPE_SNAPSHOT:
2613 str = "snapshot";
2614 break;
2615 case ZFS_TYPE_BOOKMARK:
2616 str = "bookmark";
2617 break;
2618 default:
2619 abort();
2621 (void) snprintf(propbuf, proplen, "%s", str);
2622 zcp_check(zhp, prop, 0, propbuf);
2623 break;
2625 case ZFS_PROP_MOUNTED:
2627 * The 'mounted' property is a pseudo-property that described
2628 * whether the filesystem is currently mounted. Even though
2629 * it's a boolean value, the typical values of "on" and "off"
2630 * don't make sense, so we translate to "yes" and "no".
2632 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2633 src, &source, &val) != 0)
2634 return (-1);
2635 if (val)
2636 (void) strlcpy(propbuf, "yes", proplen);
2637 else
2638 (void) strlcpy(propbuf, "no", proplen);
2639 break;
2641 case ZFS_PROP_NAME:
2643 * The 'name' property is a pseudo-property derived from the
2644 * dataset name. It is presented as a real property to simplify
2645 * consumers.
2647 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2648 zcp_check(zhp, prop, 0, propbuf);
2649 break;
2651 case ZFS_PROP_GUID:
2653 * GUIDs are stored as numbers, but they are identifiers.
2654 * We don't want them to be pretty printed, because pretty
2655 * printing mangles the ID into a truncated and useless value.
2657 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2658 return (-1);
2659 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2660 zcp_check(zhp, prop, val, NULL);
2661 break;
2663 default:
2664 switch (zfs_prop_get_type(prop)) {
2665 case PROP_TYPE_NUMBER:
2666 if (get_numeric_property(zhp, prop, src,
2667 &source, &val) != 0) {
2668 return (-1);
2671 if (literal) {
2672 (void) snprintf(propbuf, proplen, "%llu",
2673 (u_longlong_t)val);
2674 } else {
2675 zfs_nicenum(val, propbuf, proplen);
2677 zcp_check(zhp, prop, val, NULL);
2678 break;
2680 case PROP_TYPE_STRING:
2681 str = getprop_string(zhp, prop, &source);
2682 if (str == NULL)
2683 return (-1);
2685 (void) strlcpy(propbuf, str, proplen);
2686 zcp_check(zhp, prop, 0, str);
2687 break;
2689 case PROP_TYPE_INDEX:
2690 if (get_numeric_property(zhp, prop, src,
2691 &source, &val) != 0)
2692 return (-1);
2693 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2694 return (-1);
2696 (void) strlcpy(propbuf, strval, proplen);
2697 zcp_check(zhp, prop, 0, strval);
2698 break;
2700 default:
2701 abort();
2705 get_source(zhp, src, source, statbuf, statlen);
2707 return (0);
2711 * Utility function to get the given numeric property. Does no validation that
2712 * the given property is the appropriate type; should only be used with
2713 * hard-coded property types.
2715 uint64_t
2716 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2718 char *source;
2719 uint64_t val;
2721 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2723 return (val);
2727 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2729 char buf[64];
2731 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2732 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2736 * Similar to zfs_prop_get(), but returns the value as an integer.
2739 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2740 zprop_source_t *src, char *statbuf, size_t statlen)
2742 char *source;
2745 * Check to see if this property applies to our object
2747 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2748 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2749 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2750 zfs_prop_to_name(prop)));
2753 if (src)
2754 *src = ZPROP_SRC_NONE;
2756 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2757 return (-1);
2759 get_source(zhp, src, source, statbuf, statlen);
2761 return (0);
2764 static int
2765 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2766 char **domainp, idmap_rid_t *ridp)
2768 idmap_get_handle_t *get_hdl = NULL;
2769 idmap_stat status;
2770 int err = EINVAL;
2772 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2773 goto out;
2775 if (isuser) {
2776 err = idmap_get_sidbyuid(get_hdl, id,
2777 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2778 } else {
2779 err = idmap_get_sidbygid(get_hdl, id,
2780 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2782 if (err == IDMAP_SUCCESS &&
2783 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2784 status == IDMAP_SUCCESS)
2785 err = 0;
2786 else
2787 err = EINVAL;
2788 out:
2789 if (get_hdl)
2790 idmap_get_destroy(get_hdl);
2791 return (err);
2795 * convert the propname into parameters needed by kernel
2796 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2797 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2799 static int
2800 userquota_propname_decode(const char *propname, boolean_t zoned,
2801 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2803 zfs_userquota_prop_t type;
2804 char *cp, *end;
2805 char *numericsid = NULL;
2806 boolean_t isuser;
2808 domain[0] = '\0';
2809 *ridp = 0;
2810 /* Figure out the property type ({user|group}{quota|space}) */
2811 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2812 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2813 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2814 break;
2816 if (type == ZFS_NUM_USERQUOTA_PROPS)
2817 return (EINVAL);
2818 *typep = type;
2820 isuser = (type == ZFS_PROP_USERQUOTA ||
2821 type == ZFS_PROP_USERUSED);
2823 cp = strchr(propname, '@') + 1;
2825 if (strchr(cp, '@')) {
2827 * It's a SID name (eg "user@domain") that needs to be
2828 * turned into S-1-domainID-RID.
2830 int flag = 0;
2831 idmap_stat stat, map_stat;
2832 uid_t pid;
2833 idmap_rid_t rid;
2834 idmap_get_handle_t *gh = NULL;
2836 stat = idmap_get_create(&gh);
2837 if (stat != IDMAP_SUCCESS) {
2838 idmap_get_destroy(gh);
2839 return (ENOMEM);
2841 if (zoned && getzoneid() == GLOBAL_ZONEID)
2842 return (ENOENT);
2843 if (isuser) {
2844 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2845 if (stat < 0)
2846 return (ENOENT);
2847 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2848 &rid, &map_stat);
2849 } else {
2850 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2851 if (stat < 0)
2852 return (ENOENT);
2853 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2854 &rid, &map_stat);
2856 if (stat < 0) {
2857 idmap_get_destroy(gh);
2858 return (ENOENT);
2860 stat = idmap_get_mappings(gh);
2861 idmap_get_destroy(gh);
2863 if (stat < 0) {
2864 return (ENOENT);
2866 if (numericsid == NULL)
2867 return (ENOENT);
2868 cp = numericsid;
2869 *ridp = rid;
2870 /* will be further decoded below */
2873 if (strncmp(cp, "S-1-", 4) == 0) {
2874 /* It's a numeric SID (eg "S-1-234-567-89") */
2875 (void) strlcpy(domain, cp, domainlen);
2876 errno = 0;
2877 if (*ridp == 0) {
2878 cp = strrchr(domain, '-');
2879 *cp = '\0';
2880 cp++;
2881 *ridp = strtoull(cp, &end, 10);
2882 } else {
2883 end = "";
2885 if (numericsid) {
2886 free(numericsid);
2887 numericsid = NULL;
2889 if (errno != 0 || *end != '\0')
2890 return (EINVAL);
2891 } else if (!isdigit(*cp)) {
2893 * It's a user/group name (eg "user") that needs to be
2894 * turned into a uid/gid
2896 if (zoned && getzoneid() == GLOBAL_ZONEID)
2897 return (ENOENT);
2898 if (isuser) {
2899 struct passwd *pw;
2900 pw = getpwnam(cp);
2901 if (pw == NULL)
2902 return (ENOENT);
2903 *ridp = pw->pw_uid;
2904 } else {
2905 struct group *gr;
2906 gr = getgrnam(cp);
2907 if (gr == NULL)
2908 return (ENOENT);
2909 *ridp = gr->gr_gid;
2911 } else {
2912 /* It's a user/group ID (eg "12345"). */
2913 uid_t id = strtoul(cp, &end, 10);
2914 idmap_rid_t rid;
2915 char *mapdomain;
2917 if (*end != '\0')
2918 return (EINVAL);
2919 if (id > MAXUID) {
2920 /* It's an ephemeral ID. */
2921 if (idmap_id_to_numeric_domain_rid(id, isuser,
2922 &mapdomain, &rid) != 0)
2923 return (ENOENT);
2924 (void) strlcpy(domain, mapdomain, domainlen);
2925 *ridp = rid;
2926 } else {
2927 *ridp = id;
2931 ASSERT3P(numericsid, ==, NULL);
2932 return (0);
2935 static int
2936 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2937 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2939 int err;
2940 zfs_cmd_t zc = { 0 };
2942 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2944 err = userquota_propname_decode(propname,
2945 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2946 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2947 zc.zc_objset_type = *typep;
2948 if (err)
2949 return (err);
2951 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2952 if (err)
2953 return (err);
2955 *propvalue = zc.zc_cookie;
2956 return (0);
2960 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2961 uint64_t *propvalue)
2963 zfs_userquota_prop_t type;
2965 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2966 &type));
2970 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2971 char *propbuf, int proplen, boolean_t literal)
2973 int err;
2974 uint64_t propvalue;
2975 zfs_userquota_prop_t type;
2977 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2978 &type);
2980 if (err)
2981 return (err);
2983 if (literal) {
2984 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2985 } else if (propvalue == 0 &&
2986 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2987 (void) strlcpy(propbuf, "none", proplen);
2988 } else {
2989 zfs_nicenum(propvalue, propbuf, proplen);
2991 return (0);
2995 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2996 uint64_t *propvalue)
2998 int err;
2999 zfs_cmd_t zc = { 0 };
3000 const char *snapname;
3002 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3004 snapname = strchr(propname, '@') + 1;
3005 if (strchr(snapname, '@')) {
3006 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3007 } else {
3008 /* snapname is the short name, append it to zhp's fsname */
3009 char *cp;
3011 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3012 sizeof (zc.zc_value));
3013 cp = strchr(zc.zc_value, '@');
3014 if (cp != NULL)
3015 *cp = '\0';
3016 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3017 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3020 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3021 if (err)
3022 return (err);
3024 *propvalue = zc.zc_cookie;
3025 return (0);
3029 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3030 char *propbuf, int proplen, boolean_t literal)
3032 int err;
3033 uint64_t propvalue;
3035 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3037 if (err)
3038 return (err);
3040 if (literal) {
3041 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3042 } else {
3043 zfs_nicenum(propvalue, propbuf, proplen);
3045 return (0);
3049 * Returns the name of the given zfs handle.
3051 const char *
3052 zfs_get_name(const zfs_handle_t *zhp)
3054 return (zhp->zfs_name);
3058 * Returns the name of the parent pool for the given zfs handle.
3060 const char *
3061 zfs_get_pool_name(const zfs_handle_t *zhp)
3063 return (zhp->zpool_hdl->zpool_name);
3067 * Returns the type of the given zfs handle.
3069 zfs_type_t
3070 zfs_get_type(const zfs_handle_t *zhp)
3072 return (zhp->zfs_type);
3076 * Is one dataset name a child dataset of another?
3078 * Needs to handle these cases:
3079 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3080 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3081 * Descendant? No. No. No. Yes.
3083 static boolean_t
3084 is_descendant(const char *ds1, const char *ds2)
3086 size_t d1len = strlen(ds1);
3088 /* ds2 can't be a descendant if it's smaller */
3089 if (strlen(ds2) < d1len)
3090 return (B_FALSE);
3092 /* otherwise, compare strings and verify that there's a '/' char */
3093 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3097 * Given a complete name, return just the portion that refers to the parent.
3098 * Will return -1 if there is no parent (path is just the name of the
3099 * pool).
3101 static int
3102 parent_name(const char *path, char *buf, size_t buflen)
3104 char *slashp;
3106 (void) strlcpy(buf, path, buflen);
3108 if ((slashp = strrchr(buf, '/')) == NULL)
3109 return (-1);
3110 *slashp = '\0';
3112 return (0);
3116 * If accept_ancestor is false, then check to make sure that the given path has
3117 * a parent, and that it exists. If accept_ancestor is true, then find the
3118 * closest existing ancestor for the given path. In prefixlen return the
3119 * length of already existing prefix of the given path. We also fetch the
3120 * 'zoned' property, which is used to validate property settings when creating
3121 * new datasets.
3123 static int
3124 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3125 boolean_t accept_ancestor, int *prefixlen)
3127 zfs_cmd_t zc = { 0 };
3128 char parent[ZFS_MAX_DATASET_NAME_LEN];
3129 char *slash;
3130 zfs_handle_t *zhp;
3131 char errbuf[1024];
3132 uint64_t is_zoned;
3134 (void) snprintf(errbuf, sizeof (errbuf),
3135 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3137 /* get parent, and check to see if this is just a pool */
3138 if (parent_name(path, parent, sizeof (parent)) != 0) {
3139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3140 "missing dataset name"));
3141 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3144 /* check to see if the pool exists */
3145 if ((slash = strchr(parent, '/')) == NULL)
3146 slash = parent + strlen(parent);
3147 (void) strncpy(zc.zc_name, parent, slash - parent);
3148 zc.zc_name[slash - parent] = '\0';
3149 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3150 errno == ENOENT) {
3151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3152 "no such pool '%s'"), zc.zc_name);
3153 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3156 /* check to see if the parent dataset exists */
3157 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3158 if (errno == ENOENT && accept_ancestor) {
3160 * Go deeper to find an ancestor, give up on top level.
3162 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3163 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3164 "no such pool '%s'"), zc.zc_name);
3165 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3167 } else if (errno == ENOENT) {
3168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3169 "parent does not exist"));
3170 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3171 } else
3172 return (zfs_standard_error(hdl, errno, errbuf));
3175 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3176 if (zoned != NULL)
3177 *zoned = is_zoned;
3179 /* we are in a non-global zone, but parent is in the global zone */
3180 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3181 (void) zfs_standard_error(hdl, EPERM, errbuf);
3182 zfs_close(zhp);
3183 return (-1);
3186 /* make sure parent is a filesystem */
3187 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3189 "parent is not a filesystem"));
3190 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3191 zfs_close(zhp);
3192 return (-1);
3195 zfs_close(zhp);
3196 if (prefixlen != NULL)
3197 *prefixlen = strlen(parent);
3198 return (0);
3202 * Finds whether the dataset of the given type(s) exists.
3204 boolean_t
3205 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3207 zfs_handle_t *zhp;
3209 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3210 return (B_FALSE);
3213 * Try to get stats for the dataset, which will tell us if it exists.
3215 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3216 int ds_type = zhp->zfs_type;
3218 zfs_close(zhp);
3219 if (types & ds_type)
3220 return (B_TRUE);
3222 return (B_FALSE);
3226 * Given a path to 'target', create all the ancestors between
3227 * the prefixlen portion of the path, and the target itself.
3228 * Fail if the initial prefixlen-ancestor does not already exist.
3231 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3233 zfs_handle_t *h;
3234 char *cp;
3235 const char *opname;
3237 /* make sure prefix exists */
3238 cp = target + prefixlen;
3239 if (*cp != '/') {
3240 assert(strchr(cp, '/') == NULL);
3241 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3242 } else {
3243 *cp = '\0';
3244 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3245 *cp = '/';
3247 if (h == NULL)
3248 return (-1);
3249 zfs_close(h);
3252 * Attempt to create, mount, and share any ancestor filesystems,
3253 * up to the prefixlen-long one.
3255 for (cp = target + prefixlen + 1;
3256 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3258 *cp = '\0';
3260 h = make_dataset_handle(hdl, target);
3261 if (h) {
3262 /* it already exists, nothing to do here */
3263 zfs_close(h);
3264 continue;
3267 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3268 NULL) != 0) {
3269 opname = dgettext(TEXT_DOMAIN, "create");
3270 goto ancestorerr;
3273 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3274 if (h == NULL) {
3275 opname = dgettext(TEXT_DOMAIN, "open");
3276 goto ancestorerr;
3279 if (zfs_mount(h, NULL, 0) != 0) {
3280 opname = dgettext(TEXT_DOMAIN, "mount");
3281 goto ancestorerr;
3284 if (zfs_share(h) != 0) {
3285 opname = dgettext(TEXT_DOMAIN, "share");
3286 goto ancestorerr;
3289 zfs_close(h);
3292 return (0);
3294 ancestorerr:
3295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3296 "failed to %s ancestor '%s'"), opname, target);
3297 return (-1);
3301 * Creates non-existing ancestors of the given path.
3304 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3306 int prefix;
3307 char *path_copy;
3308 int rc = 0;
3310 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3311 return (-1);
3313 if ((path_copy = strdup(path)) != NULL) {
3314 rc = create_parents(hdl, path_copy, prefix);
3315 free(path_copy);
3317 if (path_copy == NULL || rc != 0)
3318 return (-1);
3320 return (0);
3324 * Create a new filesystem or volume.
3327 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3328 nvlist_t *props)
3330 int ret;
3331 uint64_t size = 0;
3332 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3333 char errbuf[1024];
3334 uint64_t zoned;
3335 enum lzc_dataset_type ost;
3336 zpool_handle_t *zpool_handle;
3338 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3339 "cannot create '%s'"), path);
3341 /* validate the path, taking care to note the extended error message */
3342 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3343 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3345 /* validate parents exist */
3346 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3347 return (-1);
3350 * The failure modes when creating a dataset of a different type over
3351 * one that already exists is a little strange. In particular, if you
3352 * try to create a dataset on top of an existing dataset, the ioctl()
3353 * will return ENOENT, not EEXIST. To prevent this from happening, we
3354 * first try to see if the dataset exists.
3356 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3358 "dataset already exists"));
3359 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3362 if (type == ZFS_TYPE_VOLUME)
3363 ost = LZC_DATSET_TYPE_ZVOL;
3364 else
3365 ost = LZC_DATSET_TYPE_ZFS;
3367 /* open zpool handle for prop validation */
3368 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3369 (void) strlcpy(pool_path, path, sizeof (pool_path));
3371 /* truncate pool_path at first slash */
3372 char *p = strchr(pool_path, '/');
3373 if (p != NULL)
3374 *p = '\0';
3376 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3377 return (-1);
3379 if (props && (props = zfs_valid_proplist(hdl, type, props,
3380 zoned, NULL, zpool_handle, errbuf)) == 0) {
3381 zpool_close(zpool_handle);
3382 return (-1);
3384 zpool_close(zpool_handle);
3386 if (type == ZFS_TYPE_VOLUME) {
3388 * If we are creating a volume, the size and block size must
3389 * satisfy a few restraints. First, the blocksize must be a
3390 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3391 * volsize must be a multiple of the block size, and cannot be
3392 * zero.
3394 if (props == NULL || nvlist_lookup_uint64(props,
3395 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3396 nvlist_free(props);
3397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3398 "missing volume size"));
3399 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3402 if ((ret = nvlist_lookup_uint64(props,
3403 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3404 &blocksize)) != 0) {
3405 if (ret == ENOENT) {
3406 blocksize = zfs_prop_default_numeric(
3407 ZFS_PROP_VOLBLOCKSIZE);
3408 } else {
3409 nvlist_free(props);
3410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3411 "missing volume block size"));
3412 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3416 if (size == 0) {
3417 nvlist_free(props);
3418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3419 "volume size cannot be zero"));
3420 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3423 if (size % blocksize != 0) {
3424 nvlist_free(props);
3425 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3426 "volume size must be a multiple of volume block "
3427 "size"));
3428 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3432 /* create the dataset */
3433 ret = lzc_create(path, ost, props);
3434 nvlist_free(props);
3436 /* check for failure */
3437 if (ret != 0) {
3438 char parent[ZFS_MAX_DATASET_NAME_LEN];
3439 (void) parent_name(path, parent, sizeof (parent));
3441 switch (errno) {
3442 case ENOENT:
3443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3444 "no such parent '%s'"), parent);
3445 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3447 case EINVAL:
3448 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3449 "parent '%s' is not a filesystem"), parent);
3450 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3452 case ENOTSUP:
3453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3454 "pool must be upgraded to set this "
3455 "property or value"));
3456 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3457 case ERANGE:
3458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3459 "invalid property value(s) specified"));
3460 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3461 #ifdef _ILP32
3462 case EOVERFLOW:
3464 * This platform can't address a volume this big.
3466 if (type == ZFS_TYPE_VOLUME)
3467 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3468 errbuf));
3469 #endif
3470 /* FALLTHROUGH */
3471 default:
3472 return (zfs_standard_error(hdl, errno, errbuf));
3476 return (0);
3480 * Destroys the given dataset. The caller must make sure that the filesystem
3481 * isn't mounted, and that there are no active dependents. If the file system
3482 * does not exist this function does nothing.
3485 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3487 zfs_cmd_t zc = { 0 };
3489 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3490 nvlist_t *nv = fnvlist_alloc();
3491 fnvlist_add_boolean(nv, zhp->zfs_name);
3492 int error = lzc_destroy_bookmarks(nv, NULL);
3493 fnvlist_free(nv);
3494 if (error != 0) {
3495 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3496 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3497 zhp->zfs_name));
3499 return (0);
3502 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3504 if (ZFS_IS_VOLUME(zhp)) {
3505 zc.zc_objset_type = DMU_OST_ZVOL;
3506 } else {
3507 zc.zc_objset_type = DMU_OST_ZFS;
3510 zc.zc_defer_destroy = defer;
3511 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3512 errno != ENOENT) {
3513 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3514 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3515 zhp->zfs_name));
3518 remove_mountpoint(zhp);
3520 return (0);
3523 struct destroydata {
3524 nvlist_t *nvl;
3525 const char *snapname;
3528 static int
3529 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3531 struct destroydata *dd = arg;
3532 char name[ZFS_MAX_DATASET_NAME_LEN];
3533 int rv = 0;
3535 (void) snprintf(name, sizeof (name),
3536 "%s@%s", zhp->zfs_name, dd->snapname);
3538 if (lzc_exists(name))
3539 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3541 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3542 zfs_close(zhp);
3543 return (rv);
3547 * Destroys all snapshots with the given name in zhp & descendants.
3550 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3552 int ret;
3553 struct destroydata dd = { 0 };
3555 dd.snapname = snapname;
3556 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3557 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3559 if (nvlist_empty(dd.nvl)) {
3560 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3561 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3562 zhp->zfs_name, snapname);
3563 } else {
3564 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3566 nvlist_free(dd.nvl);
3567 return (ret);
3571 * Destroys all the snapshots named in the nvlist.
3574 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3576 int ret;
3577 nvlist_t *errlist = NULL;
3579 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3581 if (ret == 0) {
3582 nvlist_free(errlist);
3583 return (0);
3586 if (nvlist_empty(errlist)) {
3587 char errbuf[1024];
3588 (void) snprintf(errbuf, sizeof (errbuf),
3589 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3591 ret = zfs_standard_error(hdl, ret, errbuf);
3593 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3594 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3595 char errbuf[1024];
3596 (void) snprintf(errbuf, sizeof (errbuf),
3597 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3598 nvpair_name(pair));
3600 switch (fnvpair_value_int32(pair)) {
3601 case EEXIST:
3602 zfs_error_aux(hdl,
3603 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3604 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3605 break;
3606 default:
3607 ret = zfs_standard_error(hdl, errno, errbuf);
3608 break;
3612 nvlist_free(errlist);
3613 return (ret);
3617 * Clones the given dataset. The target must be of the same type as the source.
3620 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3622 char parent[ZFS_MAX_DATASET_NAME_LEN];
3623 int ret;
3624 char errbuf[1024];
3625 libzfs_handle_t *hdl = zhp->zfs_hdl;
3626 uint64_t zoned;
3628 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3630 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3631 "cannot create '%s'"), target);
3633 /* validate the target/clone name */
3634 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3635 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3637 /* validate parents exist */
3638 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3639 return (-1);
3641 (void) parent_name(target, parent, sizeof (parent));
3643 /* do the clone */
3645 if (props) {
3646 zfs_type_t type;
3648 if (ZFS_IS_VOLUME(zhp)) {
3649 type = ZFS_TYPE_VOLUME;
3650 } else {
3651 type = ZFS_TYPE_FILESYSTEM;
3653 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3654 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3655 return (-1);
3656 if (zfs_fix_auto_resv(zhp, props) == -1) {
3657 nvlist_free(props);
3658 return (-1);
3662 ret = lzc_clone(target, zhp->zfs_name, props);
3663 nvlist_free(props);
3665 if (ret != 0) {
3666 switch (errno) {
3668 case ENOENT:
3670 * The parent doesn't exist. We should have caught this
3671 * above, but there may a race condition that has since
3672 * destroyed the parent.
3674 * At this point, we don't know whether it's the source
3675 * that doesn't exist anymore, or whether the target
3676 * dataset doesn't exist.
3678 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3679 "no such parent '%s'"), parent);
3680 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3682 case EXDEV:
3683 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3684 "source and target pools differ"));
3685 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3686 errbuf));
3688 default:
3689 return (zfs_standard_error(zhp->zfs_hdl, errno,
3690 errbuf));
3694 return (ret);
3698 * Promotes the given clone fs to be the clone parent.
3701 zfs_promote(zfs_handle_t *zhp)
3703 libzfs_handle_t *hdl = zhp->zfs_hdl;
3704 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3705 int ret;
3706 char errbuf[1024];
3708 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3709 "cannot promote '%s'"), zhp->zfs_name);
3711 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3712 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3713 "snapshots can not be promoted"));
3714 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3717 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3719 "not a cloned filesystem"));
3720 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3723 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3724 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3726 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3728 if (ret != 0) {
3729 switch (ret) {
3730 case EEXIST:
3731 /* There is a conflicting snapshot name. */
3732 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3733 "conflicting snapshot '%s' from parent '%s'"),
3734 snapname, zhp->zfs_dmustats.dds_origin);
3735 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3737 default:
3738 return (zfs_standard_error(hdl, ret, errbuf));
3741 return (ret);
3744 typedef struct snapdata {
3745 nvlist_t *sd_nvl;
3746 const char *sd_snapname;
3747 } snapdata_t;
3749 static int
3750 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3752 snapdata_t *sd = arg;
3753 char name[ZFS_MAX_DATASET_NAME_LEN];
3754 int rv = 0;
3756 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3757 (void) snprintf(name, sizeof (name),
3758 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3760 fnvlist_add_boolean(sd->sd_nvl, name);
3762 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3764 zfs_close(zhp);
3766 return (rv);
3770 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3772 int err;
3773 char errbuf[1024];
3775 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3776 "cannot remap filesystem '%s' "), fs);
3778 err = lzc_remap(fs);
3780 if (err != 0) {
3781 (void) zfs_standard_error(hdl, err, errbuf);
3784 return (err);
3788 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3789 * created.
3792 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3794 int ret;
3795 char errbuf[1024];
3796 nvpair_t *elem;
3797 nvlist_t *errors;
3799 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3800 "cannot create snapshots "));
3802 elem = NULL;
3803 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3804 const char *snapname = nvpair_name(elem);
3806 /* validate the target name */
3807 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3808 B_TRUE)) {
3809 (void) snprintf(errbuf, sizeof (errbuf),
3810 dgettext(TEXT_DOMAIN,
3811 "cannot create snapshot '%s'"), snapname);
3812 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3817 * get pool handle for prop validation. assumes all snaps are in the
3818 * same pool, as does lzc_snapshot (below).
3820 char pool[ZFS_MAX_DATASET_NAME_LEN];
3821 elem = nvlist_next_nvpair(snaps, NULL);
3822 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3823 pool[strcspn(pool, "/@")] = '\0';
3824 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3826 if (props != NULL &&
3827 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3828 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3829 zpool_close(zpool_hdl);
3830 return (-1);
3832 zpool_close(zpool_hdl);
3834 ret = lzc_snapshot(snaps, props, &errors);
3836 if (ret != 0) {
3837 boolean_t printed = B_FALSE;
3838 for (elem = nvlist_next_nvpair(errors, NULL);
3839 elem != NULL;
3840 elem = nvlist_next_nvpair(errors, elem)) {
3841 (void) snprintf(errbuf, sizeof (errbuf),
3842 dgettext(TEXT_DOMAIN,
3843 "cannot create snapshot '%s'"), nvpair_name(elem));
3844 (void) zfs_standard_error(hdl,
3845 fnvpair_value_int32(elem), errbuf);
3846 printed = B_TRUE;
3848 if (!printed) {
3849 switch (ret) {
3850 case EXDEV:
3851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3852 "multiple snapshots of same "
3853 "fs not allowed"));
3854 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3856 break;
3857 default:
3858 (void) zfs_standard_error(hdl, ret, errbuf);
3863 nvlist_free(props);
3864 nvlist_free(errors);
3865 return (ret);
3869 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3870 nvlist_t *props)
3872 int ret;
3873 snapdata_t sd = { 0 };
3874 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3875 char *cp;
3876 zfs_handle_t *zhp;
3877 char errbuf[1024];
3879 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3880 "cannot snapshot %s"), path);
3882 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3883 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3885 (void) strlcpy(fsname, path, sizeof (fsname));
3886 cp = strchr(fsname, '@');
3887 *cp = '\0';
3888 sd.sd_snapname = cp + 1;
3890 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3891 ZFS_TYPE_VOLUME)) == NULL) {
3892 return (-1);
3895 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3896 if (recursive) {
3897 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3898 } else {
3899 fnvlist_add_boolean(sd.sd_nvl, path);
3902 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3903 nvlist_free(sd.sd_nvl);
3904 zfs_close(zhp);
3905 return (ret);
3909 * Destroy any more recent snapshots. We invoke this callback on any dependents
3910 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3911 * is a dependent and we should just destroy it without checking the transaction
3912 * group.
3914 typedef struct rollback_data {
3915 const char *cb_target; /* the snapshot */
3916 uint64_t cb_create; /* creation time reference */
3917 boolean_t cb_error;
3918 boolean_t cb_force;
3919 } rollback_data_t;
3921 static int
3922 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3924 rollback_data_t *cbp = data;
3925 prop_changelist_t *clp;
3927 /* We must destroy this clone; first unmount it */
3928 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3929 cbp->cb_force ? MS_FORCE: 0);
3930 if (clp == NULL || changelist_prefix(clp) != 0) {
3931 cbp->cb_error = B_TRUE;
3932 zfs_close(zhp);
3933 return (0);
3935 if (zfs_destroy(zhp, B_FALSE) != 0)
3936 cbp->cb_error = B_TRUE;
3937 else
3938 changelist_remove(clp, zhp->zfs_name);
3939 (void) changelist_postfix(clp);
3940 changelist_free(clp);
3942 zfs_close(zhp);
3943 return (0);
3946 static int
3947 rollback_destroy(zfs_handle_t *zhp, void *data)
3949 rollback_data_t *cbp = data;
3951 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3952 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3953 rollback_destroy_dependent, cbp);
3955 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3958 zfs_close(zhp);
3959 return (0);
3963 * Given a dataset, rollback to a specific snapshot, discarding any
3964 * data changes since then and making it the active dataset.
3966 * Any snapshots and bookmarks more recent than the target are
3967 * destroyed, along with their dependents (i.e. clones).
3970 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3972 rollback_data_t cb = { 0 };
3973 int err;
3974 boolean_t restore_resv = 0;
3975 uint64_t old_volsize = 0, new_volsize;
3976 zfs_prop_t resv_prop;
3978 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3979 zhp->zfs_type == ZFS_TYPE_VOLUME);
3982 * Destroy all recent snapshots and their dependents.
3984 cb.cb_force = force;
3985 cb.cb_target = snap->zfs_name;
3986 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3987 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3988 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3990 if (cb.cb_error)
3991 return (-1);
3994 * Now that we have verified that the snapshot is the latest,
3995 * rollback to the given snapshot.
3998 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3999 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4000 return (-1);
4001 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4002 restore_resv =
4003 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4007 * Pass both the filesystem and the wanted snapshot names,
4008 * we would get an error back if the snapshot is destroyed or
4009 * a new snapshot is created before this request is processed.
4011 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4012 if (err != 0) {
4013 char errbuf[1024];
4015 (void) snprintf(errbuf, sizeof (errbuf),
4016 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4017 zhp->zfs_name);
4018 switch (err) {
4019 case EEXIST:
4020 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4021 "there is a snapshot or bookmark more recent "
4022 "than '%s'"), snap->zfs_name);
4023 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4024 break;
4025 case ESRCH:
4026 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4027 "'%s' is not found among snapshots of '%s'"),
4028 snap->zfs_name, zhp->zfs_name);
4029 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4030 break;
4031 case EINVAL:
4032 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4033 break;
4034 default:
4035 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4037 return (err);
4041 * For volumes, if the pre-rollback volsize matched the pre-
4042 * rollback reservation and the volsize has changed then set
4043 * the reservation property to the post-rollback volsize.
4044 * Make a new handle since the rollback closed the dataset.
4046 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4047 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4048 if (restore_resv) {
4049 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4050 if (old_volsize != new_volsize)
4051 err = zfs_prop_set_int(zhp, resv_prop,
4052 new_volsize);
4054 zfs_close(zhp);
4056 return (err);
4060 * Renames the given dataset.
4063 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4064 boolean_t force_unmount)
4066 int ret = 0;
4067 zfs_cmd_t zc = { 0 };
4068 char *delim;
4069 prop_changelist_t *cl = NULL;
4070 zfs_handle_t *zhrp = NULL;
4071 char *parentname = NULL;
4072 char parent[ZFS_MAX_DATASET_NAME_LEN];
4073 libzfs_handle_t *hdl = zhp->zfs_hdl;
4074 char errbuf[1024];
4076 /* if we have the same exact name, just return success */
4077 if (strcmp(zhp->zfs_name, target) == 0)
4078 return (0);
4080 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4081 "cannot rename to '%s'"), target);
4083 /* make sure source name is valid */
4084 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4085 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4088 * Make sure the target name is valid
4090 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4091 if ((strchr(target, '@') == NULL) ||
4092 *target == '@') {
4094 * Snapshot target name is abbreviated,
4095 * reconstruct full dataset name
4097 (void) strlcpy(parent, zhp->zfs_name,
4098 sizeof (parent));
4099 delim = strchr(parent, '@');
4100 if (strchr(target, '@') == NULL)
4101 *(++delim) = '\0';
4102 else
4103 *delim = '\0';
4104 (void) strlcat(parent, target, sizeof (parent));
4105 target = parent;
4106 } else {
4108 * Make sure we're renaming within the same dataset.
4110 delim = strchr(target, '@');
4111 if (strncmp(zhp->zfs_name, target, delim - target)
4112 != 0 || zhp->zfs_name[delim - target] != '@') {
4113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4114 "snapshots must be part of same "
4115 "dataset"));
4116 return (zfs_error(hdl, EZFS_CROSSTARGET,
4117 errbuf));
4120 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4121 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4122 } else {
4123 if (recursive) {
4124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4125 "recursive rename must be a snapshot"));
4126 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4129 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4130 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4132 /* validate parents */
4133 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4134 return (-1);
4136 /* make sure we're in the same pool */
4137 verify((delim = strchr(target, '/')) != NULL);
4138 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4139 zhp->zfs_name[delim - target] != '/') {
4140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4141 "datasets must be within same pool"));
4142 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4145 /* new name cannot be a child of the current dataset name */
4146 if (is_descendant(zhp->zfs_name, target)) {
4147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4148 "New dataset name cannot be a descendant of "
4149 "current dataset name"));
4150 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4154 (void) snprintf(errbuf, sizeof (errbuf),
4155 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4157 if (getzoneid() == GLOBAL_ZONEID &&
4158 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4160 "dataset is used in a non-global zone"));
4161 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4164 if (recursive) {
4165 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4166 if (parentname == NULL) {
4167 ret = -1;
4168 goto error;
4170 delim = strchr(parentname, '@');
4171 *delim = '\0';
4172 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4173 if (zhrp == NULL) {
4174 ret = -1;
4175 goto error;
4177 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4178 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4179 force_unmount ? MS_FORCE : 0)) == NULL)
4180 return (-1);
4182 if (changelist_haszonedchild(cl)) {
4183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4184 "child dataset with inherited mountpoint is used "
4185 "in a non-global zone"));
4186 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4187 ret = -1;
4188 goto error;
4191 if ((ret = changelist_prefix(cl)) != 0)
4192 goto error;
4195 if (ZFS_IS_VOLUME(zhp))
4196 zc.zc_objset_type = DMU_OST_ZVOL;
4197 else
4198 zc.zc_objset_type = DMU_OST_ZFS;
4200 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4201 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4203 zc.zc_cookie = recursive;
4205 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4207 * if it was recursive, the one that actually failed will
4208 * be in zc.zc_name
4210 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4211 "cannot rename '%s'"), zc.zc_name);
4213 if (recursive && errno == EEXIST) {
4214 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4215 "a child dataset already has a snapshot "
4216 "with the new name"));
4217 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4218 } else {
4219 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4223 * On failure, we still want to remount any filesystems that
4224 * were previously mounted, so we don't alter the system state.
4226 if (cl != NULL)
4227 (void) changelist_postfix(cl);
4228 } else {
4229 if (cl != NULL) {
4230 changelist_rename(cl, zfs_get_name(zhp), target);
4231 ret = changelist_postfix(cl);
4235 error:
4236 if (parentname != NULL) {
4237 free(parentname);
4239 if (zhrp != NULL) {
4240 zfs_close(zhrp);
4242 if (cl != NULL) {
4243 changelist_free(cl);
4245 return (ret);
4248 nvlist_t *
4249 zfs_get_user_props(zfs_handle_t *zhp)
4251 return (zhp->zfs_user_props);
4254 nvlist_t *
4255 zfs_get_recvd_props(zfs_handle_t *zhp)
4257 if (zhp->zfs_recvd_props == NULL)
4258 if (get_recvd_props_ioctl(zhp) != 0)
4259 return (NULL);
4260 return (zhp->zfs_recvd_props);
4264 * This function is used by 'zfs list' to determine the exact set of columns to
4265 * display, and their maximum widths. This does two main things:
4267 * - If this is a list of all properties, then expand the list to include
4268 * all native properties, and set a flag so that for each dataset we look
4269 * for new unique user properties and add them to the list.
4271 * - For non fixed-width properties, keep track of the maximum width seen
4272 * so that we can size the column appropriately. If the user has
4273 * requested received property values, we also need to compute the width
4274 * of the RECEIVED column.
4277 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4278 boolean_t literal)
4280 libzfs_handle_t *hdl = zhp->zfs_hdl;
4281 zprop_list_t *entry;
4282 zprop_list_t **last, **start;
4283 nvlist_t *userprops, *propval;
4284 nvpair_t *elem;
4285 char *strval;
4286 char buf[ZFS_MAXPROPLEN];
4288 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4289 return (-1);
4291 userprops = zfs_get_user_props(zhp);
4293 entry = *plp;
4294 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4296 * Go through and add any user properties as necessary. We
4297 * start by incrementing our list pointer to the first
4298 * non-native property.
4300 start = plp;
4301 while (*start != NULL) {
4302 if ((*start)->pl_prop == ZPROP_INVAL)
4303 break;
4304 start = &(*start)->pl_next;
4307 elem = NULL;
4308 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4310 * See if we've already found this property in our list.
4312 for (last = start; *last != NULL;
4313 last = &(*last)->pl_next) {
4314 if (strcmp((*last)->pl_user_prop,
4315 nvpair_name(elem)) == 0)
4316 break;
4319 if (*last == NULL) {
4320 if ((entry = zfs_alloc(hdl,
4321 sizeof (zprop_list_t))) == NULL ||
4322 ((entry->pl_user_prop = zfs_strdup(hdl,
4323 nvpair_name(elem)))) == NULL) {
4324 free(entry);
4325 return (-1);
4328 entry->pl_prop = ZPROP_INVAL;
4329 entry->pl_width = strlen(nvpair_name(elem));
4330 entry->pl_all = B_TRUE;
4331 *last = entry;
4337 * Now go through and check the width of any non-fixed columns
4339 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4340 if (entry->pl_fixed && !literal)
4341 continue;
4343 if (entry->pl_prop != ZPROP_INVAL) {
4344 if (zfs_prop_get(zhp, entry->pl_prop,
4345 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4346 if (strlen(buf) > entry->pl_width)
4347 entry->pl_width = strlen(buf);
4349 if (received && zfs_prop_get_recvd(zhp,
4350 zfs_prop_to_name(entry->pl_prop),
4351 buf, sizeof (buf), literal) == 0)
4352 if (strlen(buf) > entry->pl_recvd_width)
4353 entry->pl_recvd_width = strlen(buf);
4354 } else {
4355 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4356 &propval) == 0) {
4357 verify(nvlist_lookup_string(propval,
4358 ZPROP_VALUE, &strval) == 0);
4359 if (strlen(strval) > entry->pl_width)
4360 entry->pl_width = strlen(strval);
4362 if (received && zfs_prop_get_recvd(zhp,
4363 entry->pl_user_prop,
4364 buf, sizeof (buf), literal) == 0)
4365 if (strlen(buf) > entry->pl_recvd_width)
4366 entry->pl_recvd_width = strlen(buf);
4370 return (0);
4374 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4375 char *resource, void *export, void *sharetab,
4376 int sharemax, zfs_share_op_t operation)
4378 zfs_cmd_t zc = { 0 };
4379 int error;
4381 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4382 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4383 if (resource)
4384 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4385 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4386 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4387 zc.zc_share.z_sharetype = operation;
4388 zc.zc_share.z_sharemax = sharemax;
4389 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4390 return (error);
4393 void
4394 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4396 nvpair_t *curr;
4399 * Keep a reference to the props-table against which we prune the
4400 * properties.
4402 zhp->zfs_props_table = props;
4404 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4406 while (curr) {
4407 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4408 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4411 * User properties will result in ZPROP_INVAL, and since we
4412 * only know how to prune standard ZFS properties, we always
4413 * leave these in the list. This can also happen if we
4414 * encounter an unknown DSL property (when running older
4415 * software, for example).
4417 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4418 (void) nvlist_remove(zhp->zfs_props,
4419 nvpair_name(curr), nvpair_type(curr));
4420 curr = next;
4424 static int
4425 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4426 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4428 zfs_cmd_t zc = { 0 };
4429 nvlist_t *nvlist = NULL;
4430 int error;
4432 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4433 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4434 zc.zc_cookie = (uint64_t)cmd;
4436 if (cmd == ZFS_SMB_ACL_RENAME) {
4437 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4438 (void) no_memory(hdl);
4439 return (0);
4443 switch (cmd) {
4444 case ZFS_SMB_ACL_ADD:
4445 case ZFS_SMB_ACL_REMOVE:
4446 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4447 break;
4448 case ZFS_SMB_ACL_RENAME:
4449 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4450 resource1) != 0) {
4451 (void) no_memory(hdl);
4452 return (-1);
4454 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4455 resource2) != 0) {
4456 (void) no_memory(hdl);
4457 return (-1);
4459 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4460 nvlist_free(nvlist);
4461 return (-1);
4463 break;
4464 case ZFS_SMB_ACL_PURGE:
4465 break;
4466 default:
4467 return (-1);
4469 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4470 nvlist_free(nvlist);
4471 return (error);
4475 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4476 char *path, char *resource)
4478 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4479 resource, NULL));
4483 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4484 char *path, char *resource)
4486 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4487 resource, NULL));
4491 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4493 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4494 NULL, NULL));
4498 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4499 char *oldname, char *newname)
4501 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4502 oldname, newname));
4506 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4507 zfs_userspace_cb_t func, void *arg)
4509 zfs_cmd_t zc = { 0 };
4510 zfs_useracct_t buf[100];
4511 libzfs_handle_t *hdl = zhp->zfs_hdl;
4512 int ret;
4514 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4516 zc.zc_objset_type = type;
4517 zc.zc_nvlist_dst = (uintptr_t)buf;
4519 for (;;) {
4520 zfs_useracct_t *zua = buf;
4522 zc.zc_nvlist_dst_size = sizeof (buf);
4523 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4524 char errbuf[1024];
4526 (void) snprintf(errbuf, sizeof (errbuf),
4527 dgettext(TEXT_DOMAIN,
4528 "cannot get used/quota for %s"), zc.zc_name);
4529 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4531 if (zc.zc_nvlist_dst_size == 0)
4532 break;
4534 while (zc.zc_nvlist_dst_size > 0) {
4535 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4536 zua->zu_space)) != 0)
4537 return (ret);
4538 zua++;
4539 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4543 return (0);
4546 struct holdarg {
4547 nvlist_t *nvl;
4548 const char *snapname;
4549 const char *tag;
4550 boolean_t recursive;
4551 int error;
4554 static int
4555 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4557 struct holdarg *ha = arg;
4558 char name[ZFS_MAX_DATASET_NAME_LEN];
4559 int rv = 0;
4561 (void) snprintf(name, sizeof (name),
4562 "%s@%s", zhp->zfs_name, ha->snapname);
4564 if (lzc_exists(name))
4565 fnvlist_add_string(ha->nvl, name, ha->tag);
4567 if (ha->recursive)
4568 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4569 zfs_close(zhp);
4570 return (rv);
4574 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4575 boolean_t recursive, int cleanup_fd)
4577 int ret;
4578 struct holdarg ha;
4580 ha.nvl = fnvlist_alloc();
4581 ha.snapname = snapname;
4582 ha.tag = tag;
4583 ha.recursive = recursive;
4584 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4586 if (nvlist_empty(ha.nvl)) {
4587 char errbuf[1024];
4589 fnvlist_free(ha.nvl);
4590 ret = ENOENT;
4591 (void) snprintf(errbuf, sizeof (errbuf),
4592 dgettext(TEXT_DOMAIN,
4593 "cannot hold snapshot '%s@%s'"),
4594 zhp->zfs_name, snapname);
4595 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4596 return (ret);
4599 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4600 fnvlist_free(ha.nvl);
4602 return (ret);
4606 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4608 int ret;
4609 nvlist_t *errors;
4610 libzfs_handle_t *hdl = zhp->zfs_hdl;
4611 char errbuf[1024];
4612 nvpair_t *elem;
4614 errors = NULL;
4615 ret = lzc_hold(holds, cleanup_fd, &errors);
4617 if (ret == 0) {
4618 /* There may be errors even in the success case. */
4619 fnvlist_free(errors);
4620 return (0);
4623 if (nvlist_empty(errors)) {
4624 /* no hold-specific errors */
4625 (void) snprintf(errbuf, sizeof (errbuf),
4626 dgettext(TEXT_DOMAIN, "cannot hold"));
4627 switch (ret) {
4628 case ENOTSUP:
4629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4630 "pool must be upgraded"));
4631 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4632 break;
4633 case EINVAL:
4634 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4635 break;
4636 default:
4637 (void) zfs_standard_error(hdl, ret, errbuf);
4641 for (elem = nvlist_next_nvpair(errors, NULL);
4642 elem != NULL;
4643 elem = nvlist_next_nvpair(errors, elem)) {
4644 (void) snprintf(errbuf, sizeof (errbuf),
4645 dgettext(TEXT_DOMAIN,
4646 "cannot hold snapshot '%s'"), nvpair_name(elem));
4647 switch (fnvpair_value_int32(elem)) {
4648 case E2BIG:
4650 * Temporary tags wind up having the ds object id
4651 * prepended. So even if we passed the length check
4652 * above, it's still possible for the tag to wind
4653 * up being slightly too long.
4655 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4656 break;
4657 case EINVAL:
4658 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4659 break;
4660 case EEXIST:
4661 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4662 break;
4663 default:
4664 (void) zfs_standard_error(hdl,
4665 fnvpair_value_int32(elem), errbuf);
4669 fnvlist_free(errors);
4670 return (ret);
4673 static int
4674 zfs_release_one(zfs_handle_t *zhp, void *arg)
4676 struct holdarg *ha = arg;
4677 char name[ZFS_MAX_DATASET_NAME_LEN];
4678 int rv = 0;
4679 nvlist_t *existing_holds;
4681 (void) snprintf(name, sizeof (name),
4682 "%s@%s", zhp->zfs_name, ha->snapname);
4684 if (lzc_get_holds(name, &existing_holds) != 0) {
4685 ha->error = ENOENT;
4686 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4687 ha->error = ESRCH;
4688 } else {
4689 nvlist_t *torelease = fnvlist_alloc();
4690 fnvlist_add_boolean(torelease, ha->tag);
4691 fnvlist_add_nvlist(ha->nvl, name, torelease);
4692 fnvlist_free(torelease);
4695 if (ha->recursive)
4696 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4697 zfs_close(zhp);
4698 return (rv);
4702 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4703 boolean_t recursive)
4705 int ret;
4706 struct holdarg ha;
4707 nvlist_t *errors = NULL;
4708 nvpair_t *elem;
4709 libzfs_handle_t *hdl = zhp->zfs_hdl;
4710 char errbuf[1024];
4712 ha.nvl = fnvlist_alloc();
4713 ha.snapname = snapname;
4714 ha.tag = tag;
4715 ha.recursive = recursive;
4716 ha.error = 0;
4717 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4719 if (nvlist_empty(ha.nvl)) {
4720 fnvlist_free(ha.nvl);
4721 ret = ha.error;
4722 (void) snprintf(errbuf, sizeof (errbuf),
4723 dgettext(TEXT_DOMAIN,
4724 "cannot release hold from snapshot '%s@%s'"),
4725 zhp->zfs_name, snapname);
4726 if (ret == ESRCH) {
4727 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4728 } else {
4729 (void) zfs_standard_error(hdl, ret, errbuf);
4731 return (ret);
4734 ret = lzc_release(ha.nvl, &errors);
4735 fnvlist_free(ha.nvl);
4737 if (ret == 0) {
4738 /* There may be errors even in the success case. */
4739 fnvlist_free(errors);
4740 return (0);
4743 if (nvlist_empty(errors)) {
4744 /* no hold-specific errors */
4745 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4746 "cannot release"));
4747 switch (errno) {
4748 case ENOTSUP:
4749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4750 "pool must be upgraded"));
4751 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4752 break;
4753 default:
4754 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4758 for (elem = nvlist_next_nvpair(errors, NULL);
4759 elem != NULL;
4760 elem = nvlist_next_nvpair(errors, elem)) {
4761 (void) snprintf(errbuf, sizeof (errbuf),
4762 dgettext(TEXT_DOMAIN,
4763 "cannot release hold from snapshot '%s'"),
4764 nvpair_name(elem));
4765 switch (fnvpair_value_int32(elem)) {
4766 case ESRCH:
4767 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4768 break;
4769 case EINVAL:
4770 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4771 break;
4772 default:
4773 (void) zfs_standard_error_fmt(hdl,
4774 fnvpair_value_int32(elem), errbuf);
4778 fnvlist_free(errors);
4779 return (ret);
4783 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4785 zfs_cmd_t zc = { 0 };
4786 libzfs_handle_t *hdl = zhp->zfs_hdl;
4787 int nvsz = 2048;
4788 void *nvbuf;
4789 int err = 0;
4790 char errbuf[1024];
4792 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4793 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4795 tryagain:
4797 nvbuf = malloc(nvsz);
4798 if (nvbuf == NULL) {
4799 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4800 goto out;
4803 zc.zc_nvlist_dst_size = nvsz;
4804 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4806 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4808 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4809 (void) snprintf(errbuf, sizeof (errbuf),
4810 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4811 zc.zc_name);
4812 switch (errno) {
4813 case ENOMEM:
4814 free(nvbuf);
4815 nvsz = zc.zc_nvlist_dst_size;
4816 goto tryagain;
4818 case ENOTSUP:
4819 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4820 "pool must be upgraded"));
4821 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4822 break;
4823 case EINVAL:
4824 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4825 break;
4826 case ENOENT:
4827 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4828 break;
4829 default:
4830 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4831 break;
4833 } else {
4834 /* success */
4835 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4836 if (rc) {
4837 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4838 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4839 zc.zc_name);
4840 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4844 free(nvbuf);
4845 out:
4846 return (err);
4850 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4852 zfs_cmd_t zc = { 0 };
4853 libzfs_handle_t *hdl = zhp->zfs_hdl;
4854 char *nvbuf;
4855 char errbuf[1024];
4856 size_t nvsz;
4857 int err;
4859 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4860 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4862 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4863 assert(err == 0);
4865 nvbuf = malloc(nvsz);
4867 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4868 assert(err == 0);
4870 zc.zc_nvlist_src_size = nvsz;
4871 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4872 zc.zc_perm_action = un;
4874 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4876 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4877 (void) snprintf(errbuf, sizeof (errbuf),
4878 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4879 zc.zc_name);
4880 switch (errno) {
4881 case ENOTSUP:
4882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4883 "pool must be upgraded"));
4884 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4885 break;
4886 case EINVAL:
4887 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4888 break;
4889 case ENOENT:
4890 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4891 break;
4892 default:
4893 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4894 break;
4898 free(nvbuf);
4900 return (err);
4904 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4906 int err;
4907 char errbuf[1024];
4909 err = lzc_get_holds(zhp->zfs_name, nvl);
4911 if (err != 0) {
4912 libzfs_handle_t *hdl = zhp->zfs_hdl;
4914 (void) snprintf(errbuf, sizeof (errbuf),
4915 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4916 zhp->zfs_name);
4917 switch (err) {
4918 case ENOTSUP:
4919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4920 "pool must be upgraded"));
4921 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4922 break;
4923 case EINVAL:
4924 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4925 break;
4926 case ENOENT:
4927 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4928 break;
4929 default:
4930 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4931 break;
4935 return (err);
4939 * Convert the zvol's volume size to an appropriate reservation.
4940 * Note: If this routine is updated, it is necessary to update the ZFS test
4941 * suite's shell version in reservation.kshlib.
4943 uint64_t
4944 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4946 uint64_t numdb;
4947 uint64_t nblocks, volblocksize;
4948 int ncopies;
4949 char *strval;
4951 if (nvlist_lookup_string(props,
4952 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4953 ncopies = atoi(strval);
4954 else
4955 ncopies = 1;
4956 if (nvlist_lookup_uint64(props,
4957 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4958 &volblocksize) != 0)
4959 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4960 nblocks = volsize/volblocksize;
4961 /* start with metadnode L0-L6 */
4962 numdb = 7;
4963 /* calculate number of indirects */
4964 while (nblocks > 1) {
4965 nblocks += DNODES_PER_LEVEL - 1;
4966 nblocks /= DNODES_PER_LEVEL;
4967 numdb += nblocks;
4969 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4970 volsize *= ncopies;
4972 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4973 * compressed, but in practice they compress down to about
4974 * 1100 bytes
4976 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4977 volsize += numdb;
4978 return (volsize);