9286 want refreservation=auto
[unleashed.git] / usr / src / lib / libzfs / common / libzfs_dataset.c
blob011c2653a152d8f1862d29c5a1792de37c64e5ff
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 if (zhp->zfs_mntopts)
759 free(zhp->zfs_mntopts);
760 nvlist_free(zhp->zfs_props);
761 nvlist_free(zhp->zfs_user_props);
762 nvlist_free(zhp->zfs_recvd_props);
763 free(zhp);
766 typedef struct mnttab_node {
767 struct mnttab mtn_mt;
768 avl_node_t mtn_node;
769 } mnttab_node_t;
771 static int
772 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
774 const mnttab_node_t *mtn1 = arg1;
775 const mnttab_node_t *mtn2 = arg2;
776 int rv;
778 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
780 if (rv == 0)
781 return (0);
782 return (rv > 0 ? 1 : -1);
785 void
786 libzfs_mnttab_init(libzfs_handle_t *hdl)
788 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
789 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
790 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
793 void
794 libzfs_mnttab_update(libzfs_handle_t *hdl)
796 struct mnttab entry;
798 rewind(hdl->libzfs_mnttab);
799 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
800 mnttab_node_t *mtn;
802 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
803 continue;
804 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
805 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
806 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
807 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
808 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
809 avl_add(&hdl->libzfs_mnttab_cache, mtn);
813 void
814 libzfs_mnttab_fini(libzfs_handle_t *hdl)
816 void *cookie = NULL;
817 mnttab_node_t *mtn;
819 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
820 != NULL) {
821 free(mtn->mtn_mt.mnt_special);
822 free(mtn->mtn_mt.mnt_mountp);
823 free(mtn->mtn_mt.mnt_fstype);
824 free(mtn->mtn_mt.mnt_mntopts);
825 free(mtn);
827 avl_destroy(&hdl->libzfs_mnttab_cache);
830 void
831 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
833 hdl->libzfs_mnttab_enable = enable;
837 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
838 struct mnttab *entry)
840 mnttab_node_t find;
841 mnttab_node_t *mtn;
843 if (!hdl->libzfs_mnttab_enable) {
844 struct mnttab srch = { 0 };
846 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
847 libzfs_mnttab_fini(hdl);
848 rewind(hdl->libzfs_mnttab);
849 srch.mnt_special = (char *)fsname;
850 srch.mnt_fstype = MNTTYPE_ZFS;
851 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
852 return (0);
853 else
854 return (ENOENT);
857 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
858 libzfs_mnttab_update(hdl);
860 find.mtn_mt.mnt_special = (char *)fsname;
861 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
862 if (mtn) {
863 *entry = mtn->mtn_mt;
864 return (0);
866 return (ENOENT);
869 void
870 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
871 const char *mountp, const char *mntopts)
873 mnttab_node_t *mtn;
875 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
876 return;
877 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
878 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
879 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
880 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
881 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
882 avl_add(&hdl->libzfs_mnttab_cache, mtn);
885 void
886 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
888 mnttab_node_t find;
889 mnttab_node_t *ret;
891 find.mtn_mt.mnt_special = (char *)fsname;
892 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
893 != NULL) {
894 avl_remove(&hdl->libzfs_mnttab_cache, ret);
895 free(ret->mtn_mt.mnt_special);
896 free(ret->mtn_mt.mnt_mountp);
897 free(ret->mtn_mt.mnt_fstype);
898 free(ret->mtn_mt.mnt_mntopts);
899 free(ret);
904 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
906 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
908 if (zpool_handle == NULL)
909 return (-1);
911 *spa_version = zpool_get_prop_int(zpool_handle,
912 ZPOOL_PROP_VERSION, NULL);
913 return (0);
917 * The choice of reservation property depends on the SPA version.
919 static int
920 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
922 int spa_version;
924 if (zfs_spa_version(zhp, &spa_version) < 0)
925 return (-1);
927 if (spa_version >= SPA_VERSION_REFRESERVATION)
928 *resv_prop = ZFS_PROP_REFRESERVATION;
929 else
930 *resv_prop = ZFS_PROP_RESERVATION;
932 return (0);
936 * Given an nvlist of properties to set, validates that they are correct, and
937 * parses any numeric properties (index, boolean, etc) if they are specified as
938 * strings.
940 nvlist_t *
941 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
942 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
943 const char *errbuf)
945 nvpair_t *elem;
946 uint64_t intval;
947 char *strval;
948 zfs_prop_t prop;
949 nvlist_t *ret;
950 int chosen_normal = -1;
951 int chosen_utf = -1;
953 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
954 (void) no_memory(hdl);
955 return (NULL);
959 * Make sure this property is valid and applies to this type.
962 elem = NULL;
963 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
964 const char *propname = nvpair_name(elem);
966 prop = zfs_name_to_prop(propname);
967 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
969 * This is a user property: make sure it's a
970 * string, and that it's less than ZAP_MAXNAMELEN.
972 if (nvpair_type(elem) != DATA_TYPE_STRING) {
973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
974 "'%s' must be a string"), propname);
975 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
976 goto error;
979 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
981 "property name '%s' is too long"),
982 propname);
983 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
984 goto error;
987 (void) nvpair_value_string(elem, &strval);
988 if (nvlist_add_string(ret, propname, strval) != 0) {
989 (void) no_memory(hdl);
990 goto error;
992 continue;
996 * Currently, only user properties can be modified on
997 * snapshots.
999 if (type == ZFS_TYPE_SNAPSHOT) {
1000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001 "this property can not be modified for snapshots"));
1002 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1003 goto error;
1006 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1007 zfs_userquota_prop_t uqtype;
1008 char newpropname[128];
1009 char domain[128];
1010 uint64_t rid;
1011 uint64_t valary[3];
1013 if (userquota_propname_decode(propname, zoned,
1014 &uqtype, domain, sizeof (domain), &rid) != 0) {
1015 zfs_error_aux(hdl,
1016 dgettext(TEXT_DOMAIN,
1017 "'%s' has an invalid user/group name"),
1018 propname);
1019 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1020 goto error;
1023 if (uqtype != ZFS_PROP_USERQUOTA &&
1024 uqtype != ZFS_PROP_GROUPQUOTA) {
1025 zfs_error_aux(hdl,
1026 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1027 propname);
1028 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1029 errbuf);
1030 goto error;
1033 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1034 (void) nvpair_value_string(elem, &strval);
1035 if (strcmp(strval, "none") == 0) {
1036 intval = 0;
1037 } else if (zfs_nicestrtonum(hdl,
1038 strval, &intval) != 0) {
1039 (void) zfs_error(hdl,
1040 EZFS_BADPROP, errbuf);
1041 goto error;
1043 } else if (nvpair_type(elem) ==
1044 DATA_TYPE_UINT64) {
1045 (void) nvpair_value_uint64(elem, &intval);
1046 if (intval == 0) {
1047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1048 "use 'none' to disable "
1049 "userquota/groupquota"));
1050 goto error;
1052 } else {
1053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1054 "'%s' must be a number"), propname);
1055 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1056 goto error;
1060 * Encode the prop name as
1061 * userquota@<hex-rid>-domain, to make it easy
1062 * for the kernel to decode.
1064 (void) snprintf(newpropname, sizeof (newpropname),
1065 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1066 (longlong_t)rid, domain);
1067 valary[0] = uqtype;
1068 valary[1] = rid;
1069 valary[2] = intval;
1070 if (nvlist_add_uint64_array(ret, newpropname,
1071 valary, 3) != 0) {
1072 (void) no_memory(hdl);
1073 goto error;
1075 continue;
1076 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1078 "'%s' is readonly"),
1079 propname);
1080 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1081 goto error;
1084 if (prop == ZPROP_INVAL) {
1085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1086 "invalid property '%s'"), propname);
1087 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1088 goto error;
1091 if (!zfs_prop_valid_for_type(prop, type)) {
1092 zfs_error_aux(hdl,
1093 dgettext(TEXT_DOMAIN, "'%s' does not "
1094 "apply to datasets of this type"), propname);
1095 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1096 goto error;
1099 if (zfs_prop_readonly(prop) &&
1100 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1101 zfs_error_aux(hdl,
1102 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1103 propname);
1104 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1105 goto error;
1108 if (zprop_parse_value(hdl, elem, prop, type, ret,
1109 &strval, &intval, errbuf) != 0)
1110 goto error;
1113 * Perform some additional checks for specific properties.
1115 switch (prop) {
1116 case ZFS_PROP_VERSION:
1118 int version;
1120 if (zhp == NULL)
1121 break;
1122 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1123 if (intval < version) {
1124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1125 "Can not downgrade; already at version %u"),
1126 version);
1127 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1128 goto error;
1130 break;
1133 case ZFS_PROP_VOLBLOCKSIZE:
1134 case ZFS_PROP_RECORDSIZE:
1136 int maxbs = SPA_MAXBLOCKSIZE;
1137 if (zpool_hdl != NULL) {
1138 maxbs = zpool_get_prop_int(zpool_hdl,
1139 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1142 * Volumes are limited to a volblocksize of 128KB,
1143 * because they typically service workloads with
1144 * small random writes, which incur a large performance
1145 * penalty with large blocks.
1147 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1148 maxbs = SPA_OLD_MAXBLOCKSIZE;
1150 * The value must be a power of two between
1151 * SPA_MINBLOCKSIZE and maxbs.
1153 if (intval < SPA_MINBLOCKSIZE ||
1154 intval > maxbs || !ISP2(intval)) {
1155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1156 "'%s' must be power of 2 from 512B "
1157 "to %uKB"), propname, maxbs >> 10);
1158 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1159 goto error;
1161 break;
1163 case ZFS_PROP_MLSLABEL:
1166 * Verify the mlslabel string and convert to
1167 * internal hex label string.
1170 m_label_t *new_sl;
1171 char *hex = NULL; /* internal label string */
1173 /* Default value is already OK. */
1174 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1175 break;
1177 /* Verify the label can be converted to binary form */
1178 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1179 (str_to_label(strval, &new_sl, MAC_LABEL,
1180 L_NO_CORRECTION, NULL) == -1)) {
1181 goto badlabel;
1184 /* Now translate to hex internal label string */
1185 if (label_to_str(new_sl, &hex, M_INTERNAL,
1186 DEF_NAMES) != 0) {
1187 if (hex)
1188 free(hex);
1189 goto badlabel;
1191 m_label_free(new_sl);
1193 /* If string is already in internal form, we're done. */
1194 if (strcmp(strval, hex) == 0) {
1195 free(hex);
1196 break;
1199 /* Replace the label string with the internal form. */
1200 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1201 DATA_TYPE_STRING);
1202 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1203 hex) == 0);
1204 free(hex);
1206 break;
1208 badlabel:
1209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1210 "invalid mlslabel '%s'"), strval);
1211 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1212 m_label_free(new_sl); /* OK if null */
1213 goto error;
1217 case ZFS_PROP_MOUNTPOINT:
1219 namecheck_err_t why;
1221 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1222 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1223 break;
1225 if (mountpoint_namecheck(strval, &why)) {
1226 switch (why) {
1227 case NAME_ERR_LEADING_SLASH:
1228 zfs_error_aux(hdl,
1229 dgettext(TEXT_DOMAIN,
1230 "'%s' must be an absolute path, "
1231 "'none', or 'legacy'"), propname);
1232 break;
1233 case NAME_ERR_TOOLONG:
1234 zfs_error_aux(hdl,
1235 dgettext(TEXT_DOMAIN,
1236 "component of '%s' is too long"),
1237 propname);
1238 break;
1240 default:
1241 zfs_error_aux(hdl,
1242 dgettext(TEXT_DOMAIN,
1243 "(%d) not defined"),
1244 why);
1245 break;
1247 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1248 goto error;
1252 /*FALLTHRU*/
1254 case ZFS_PROP_SHARESMB:
1255 case ZFS_PROP_SHARENFS:
1257 * For the mountpoint and sharenfs or sharesmb
1258 * properties, check if it can be set in a
1259 * global/non-global zone based on
1260 * the zoned property value:
1262 * global zone non-global zone
1263 * --------------------------------------------------
1264 * zoned=on mountpoint (no) mountpoint (yes)
1265 * sharenfs (no) sharenfs (no)
1266 * sharesmb (no) sharesmb (no)
1268 * zoned=off mountpoint (yes) N/A
1269 * sharenfs (yes)
1270 * sharesmb (yes)
1272 if (zoned) {
1273 if (getzoneid() == GLOBAL_ZONEID) {
1274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1275 "'%s' cannot be set on "
1276 "dataset in a non-global zone"),
1277 propname);
1278 (void) zfs_error(hdl, EZFS_ZONED,
1279 errbuf);
1280 goto error;
1281 } else if (prop == ZFS_PROP_SHARENFS ||
1282 prop == ZFS_PROP_SHARESMB) {
1283 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1284 "'%s' cannot be set in "
1285 "a non-global zone"), propname);
1286 (void) zfs_error(hdl, EZFS_ZONED,
1287 errbuf);
1288 goto error;
1290 } else if (getzoneid() != GLOBAL_ZONEID) {
1292 * If zoned property is 'off', this must be in
1293 * a global zone. If not, something is wrong.
1295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1296 "'%s' cannot be set while dataset "
1297 "'zoned' property is set"), propname);
1298 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1299 goto error;
1303 * At this point, it is legitimate to set the
1304 * property. Now we want to make sure that the
1305 * property value is valid if it is sharenfs.
1307 if ((prop == ZFS_PROP_SHARENFS ||
1308 prop == ZFS_PROP_SHARESMB) &&
1309 strcmp(strval, "on") != 0 &&
1310 strcmp(strval, "off") != 0) {
1311 zfs_share_proto_t proto;
1313 if (prop == ZFS_PROP_SHARESMB)
1314 proto = PROTO_SMB;
1315 else
1316 proto = PROTO_NFS;
1319 * Must be an valid sharing protocol
1320 * option string so init the libshare
1321 * in order to enable the parser and
1322 * then parse the options. We use the
1323 * control API since we don't care about
1324 * the current configuration and don't
1325 * want the overhead of loading it
1326 * until we actually do something.
1329 if (zfs_init_libshare(hdl,
1330 SA_INIT_CONTROL_API) != SA_OK) {
1332 * An error occurred so we can't do
1333 * anything
1335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1336 "'%s' cannot be set: problem "
1337 "in share initialization"),
1338 propname);
1339 (void) zfs_error(hdl, EZFS_BADPROP,
1340 errbuf);
1341 goto error;
1344 if (zfs_parse_options(strval, proto) != SA_OK) {
1346 * There was an error in parsing so
1347 * deal with it by issuing an error
1348 * message and leaving after
1349 * uninitializing the the libshare
1350 * interface.
1352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1353 "'%s' cannot be set to invalid "
1354 "options"), propname);
1355 (void) zfs_error(hdl, EZFS_BADPROP,
1356 errbuf);
1357 zfs_uninit_libshare(hdl);
1358 goto error;
1360 zfs_uninit_libshare(hdl);
1363 break;
1365 case ZFS_PROP_UTF8ONLY:
1366 chosen_utf = (int)intval;
1367 break;
1369 case ZFS_PROP_NORMALIZE:
1370 chosen_normal = (int)intval;
1371 break;
1373 default:
1374 break;
1378 * For changes to existing volumes, we have some additional
1379 * checks to enforce.
1381 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1382 uint64_t volsize = zfs_prop_get_int(zhp,
1383 ZFS_PROP_VOLSIZE);
1384 uint64_t blocksize = zfs_prop_get_int(zhp,
1385 ZFS_PROP_VOLBLOCKSIZE);
1386 char buf[64];
1388 switch (prop) {
1389 case ZFS_PROP_RESERVATION:
1390 if (intval > volsize) {
1391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1392 "'%s' is greater than current "
1393 "volume size"), propname);
1394 (void) zfs_error(hdl, EZFS_BADPROP,
1395 errbuf);
1396 goto error;
1398 break;
1400 case ZFS_PROP_REFRESERVATION:
1401 if (intval > volsize && intval != UINT64_MAX) {
1402 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1403 "'%s' is greater than current "
1404 "volume size"), propname);
1405 (void) zfs_error(hdl, EZFS_BADPROP,
1406 errbuf);
1407 goto error;
1409 break;
1411 case ZFS_PROP_VOLSIZE:
1412 if (intval % blocksize != 0) {
1413 zfs_nicenum(blocksize, buf,
1414 sizeof (buf));
1415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1416 "'%s' must be a multiple of "
1417 "volume block size (%s)"),
1418 propname, buf);
1419 (void) zfs_error(hdl, EZFS_BADPROP,
1420 errbuf);
1421 goto error;
1424 if (intval == 0) {
1425 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1426 "'%s' cannot be zero"),
1427 propname);
1428 (void) zfs_error(hdl, EZFS_BADPROP,
1429 errbuf);
1430 goto error;
1432 break;
1434 default:
1435 break;
1441 * If normalization was chosen, but no UTF8 choice was made,
1442 * enforce rejection of non-UTF8 names.
1444 * If normalization was chosen, but rejecting non-UTF8 names
1445 * was explicitly not chosen, it is an error.
1447 if (chosen_normal > 0 && chosen_utf < 0) {
1448 if (nvlist_add_uint64(ret,
1449 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1450 (void) no_memory(hdl);
1451 goto error;
1453 } else if (chosen_normal > 0 && chosen_utf == 0) {
1454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1455 "'%s' must be set 'on' if normalization chosen"),
1456 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1457 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1458 goto error;
1460 return (ret);
1462 error:
1463 nvlist_free(ret);
1464 return (NULL);
1468 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1470 uint64_t old_volsize;
1471 uint64_t new_volsize;
1472 uint64_t old_reservation;
1473 uint64_t new_reservation;
1474 zfs_prop_t resv_prop;
1475 nvlist_t *props;
1478 * If this is an existing volume, and someone is setting the volsize,
1479 * make sure that it matches the reservation, or add it if necessary.
1481 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1482 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1483 return (-1);
1484 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1486 props = fnvlist_alloc();
1487 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1488 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1490 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1491 old_reservation) || nvlist_exists(nvl,
1492 zfs_prop_to_name(resv_prop))) {
1493 fnvlist_free(props);
1494 return (0);
1496 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1497 &new_volsize) != 0) {
1498 fnvlist_free(props);
1499 return (-1);
1501 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1502 fnvlist_free(props);
1504 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1505 new_reservation) != 0) {
1506 (void) no_memory(zhp->zfs_hdl);
1507 return (-1);
1509 return (1);
1513 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1514 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1515 * Return codes must match zfs_add_synthetic_resv().
1517 static int
1518 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1520 uint64_t volsize;
1521 uint64_t resvsize;
1522 zfs_prop_t prop;
1523 nvlist_t *props;
1525 if (!ZFS_IS_VOLUME(zhp)) {
1526 return (0);
1529 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1530 return (-1);
1533 if (prop != ZFS_PROP_REFRESERVATION) {
1534 return (0);
1537 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1538 /* No value being set, so it can't be "auto" */
1539 return (0);
1541 if (resvsize != UINT64_MAX) {
1542 /* Being set to a value other than "auto" */
1543 return (0);
1546 props = fnvlist_alloc();
1548 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1549 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1551 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1552 &volsize) != 0) {
1553 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1556 resvsize = zvol_volsize_to_reservation(volsize, props);
1557 fnvlist_free(props);
1559 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1560 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1561 (void) no_memory(zhp->zfs_hdl);
1562 return (-1);
1564 return (1);
1567 void
1568 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1569 char *errbuf)
1571 switch (err) {
1573 case ENOSPC:
1575 * For quotas and reservations, ENOSPC indicates
1576 * something different; setting a quota or reservation
1577 * doesn't use any disk space.
1579 switch (prop) {
1580 case ZFS_PROP_QUOTA:
1581 case ZFS_PROP_REFQUOTA:
1582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1583 "size is less than current used or "
1584 "reserved space"));
1585 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1586 break;
1588 case ZFS_PROP_RESERVATION:
1589 case ZFS_PROP_REFRESERVATION:
1590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1591 "size is greater than available space"));
1592 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1593 break;
1595 default:
1596 (void) zfs_standard_error(hdl, err, errbuf);
1597 break;
1599 break;
1601 case EBUSY:
1602 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1603 break;
1605 case EROFS:
1606 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1607 break;
1609 case E2BIG:
1610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1611 "property value too long"));
1612 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1613 break;
1615 case ENOTSUP:
1616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1617 "pool and or dataset must be upgraded to set this "
1618 "property or value"));
1619 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1620 break;
1622 case ERANGE:
1623 if (prop == ZFS_PROP_COMPRESSION ||
1624 prop == ZFS_PROP_RECORDSIZE) {
1625 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1626 "property setting is not allowed on "
1627 "bootable datasets"));
1628 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1629 } else if (prop == ZFS_PROP_CHECKSUM ||
1630 prop == ZFS_PROP_DEDUP) {
1631 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1632 "property setting is not allowed on "
1633 "root pools"));
1634 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1635 } else {
1636 (void) zfs_standard_error(hdl, err, errbuf);
1638 break;
1640 case EINVAL:
1641 if (prop == ZPROP_INVAL) {
1642 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1643 } else {
1644 (void) zfs_standard_error(hdl, err, errbuf);
1646 break;
1648 case EOVERFLOW:
1650 * This platform can't address a volume this big.
1652 #ifdef _ILP32
1653 if (prop == ZFS_PROP_VOLSIZE) {
1654 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1655 break;
1657 #endif
1658 /* FALLTHROUGH */
1659 default:
1660 (void) zfs_standard_error(hdl, err, errbuf);
1665 * Given a property name and value, set the property for the given dataset.
1668 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1670 int ret = -1;
1671 char errbuf[1024];
1672 libzfs_handle_t *hdl = zhp->zfs_hdl;
1673 nvlist_t *nvl = NULL;
1675 (void) snprintf(errbuf, sizeof (errbuf),
1676 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1677 zhp->zfs_name);
1679 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1680 nvlist_add_string(nvl, propname, propval) != 0) {
1681 (void) no_memory(hdl);
1682 goto error;
1685 ret = zfs_prop_set_list(zhp, nvl);
1687 error:
1688 nvlist_free(nvl);
1689 return (ret);
1695 * Given an nvlist of property names and values, set the properties for the
1696 * given dataset.
1699 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1701 zfs_cmd_t zc = { 0 };
1702 int ret = -1;
1703 prop_changelist_t **cls = NULL;
1704 int cl_idx;
1705 char errbuf[1024];
1706 libzfs_handle_t *hdl = zhp->zfs_hdl;
1707 nvlist_t *nvl;
1708 int nvl_len;
1709 int added_resv = 0;
1711 (void) snprintf(errbuf, sizeof (errbuf),
1712 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1713 zhp->zfs_name);
1715 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1716 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1717 errbuf)) == NULL)
1718 goto error;
1721 * We have to check for any extra properties which need to be added
1722 * before computing the length of the nvlist.
1724 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1725 elem != NULL;
1726 elem = nvlist_next_nvpair(nvl, elem)) {
1727 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1728 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1729 goto error;
1733 if (added_resv != 1 &&
1734 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1735 goto error;
1739 * Check how many properties we're setting and allocate an array to
1740 * store changelist pointers for postfix().
1742 nvl_len = 0;
1743 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1744 elem != NULL;
1745 elem = nvlist_next_nvpair(nvl, elem))
1746 nvl_len++;
1747 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1748 goto error;
1750 cl_idx = 0;
1751 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1752 elem != NULL;
1753 elem = nvlist_next_nvpair(nvl, elem)) {
1755 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1757 assert(cl_idx < nvl_len);
1759 * We don't want to unmount & remount the dataset when changing
1760 * its canmount property to 'on' or 'noauto'. We only use
1761 * the changelist logic to unmount when setting canmount=off.
1763 if (prop != ZFS_PROP_CANMOUNT ||
1764 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1765 zfs_is_mounted(zhp, NULL))) {
1766 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1767 if (cls[cl_idx] == NULL)
1768 goto error;
1771 if (prop == ZFS_PROP_MOUNTPOINT &&
1772 changelist_haszonedchild(cls[cl_idx])) {
1773 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1774 "child dataset with inherited mountpoint is used "
1775 "in a non-global zone"));
1776 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1777 goto error;
1780 if (cls[cl_idx] != NULL &&
1781 (ret = changelist_prefix(cls[cl_idx])) != 0)
1782 goto error;
1784 cl_idx++;
1786 assert(cl_idx == nvl_len);
1789 * Execute the corresponding ioctl() to set this list of properties.
1791 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1793 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1794 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1795 goto error;
1797 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1799 if (ret != 0) {
1800 /* Get the list of unset properties back and report them. */
1801 nvlist_t *errorprops = NULL;
1802 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1803 goto error;
1804 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1805 elem != NULL;
1806 elem = nvlist_next_nvpair(nvl, elem)) {
1807 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1808 zfs_setprop_error(hdl, prop, errno, errbuf);
1810 nvlist_free(errorprops);
1812 if (added_resv && errno == ENOSPC) {
1813 /* clean up the volsize property we tried to set */
1814 uint64_t old_volsize = zfs_prop_get_int(zhp,
1815 ZFS_PROP_VOLSIZE);
1816 nvlist_free(nvl);
1817 nvl = NULL;
1818 zcmd_free_nvlists(&zc);
1820 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1821 goto error;
1822 if (nvlist_add_uint64(nvl,
1823 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1824 old_volsize) != 0)
1825 goto error;
1826 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1827 goto error;
1828 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1830 } else {
1831 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1832 if (cls[cl_idx] != NULL) {
1833 int clp_err = changelist_postfix(cls[cl_idx]);
1834 if (clp_err != 0)
1835 ret = clp_err;
1840 * Refresh the statistics so the new property value
1841 * is reflected.
1843 if (ret == 0)
1844 (void) get_stats(zhp);
1847 error:
1848 nvlist_free(nvl);
1849 zcmd_free_nvlists(&zc);
1850 if (cls != NULL) {
1851 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1852 if (cls[cl_idx] != NULL)
1853 changelist_free(cls[cl_idx]);
1855 free(cls);
1857 return (ret);
1861 * Given a property, inherit the value from the parent dataset, or if received
1862 * is TRUE, revert to the received value, if any.
1865 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1867 zfs_cmd_t zc = { 0 };
1868 int ret;
1869 prop_changelist_t *cl;
1870 libzfs_handle_t *hdl = zhp->zfs_hdl;
1871 char errbuf[1024];
1872 zfs_prop_t prop;
1874 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1875 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1877 zc.zc_cookie = received;
1878 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1880 * For user properties, the amount of work we have to do is very
1881 * small, so just do it here.
1883 if (!zfs_prop_user(propname)) {
1884 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1885 "invalid property"));
1886 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1889 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1890 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1892 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1893 return (zfs_standard_error(hdl, errno, errbuf));
1895 return (0);
1899 * Verify that this property is inheritable.
1901 if (zfs_prop_readonly(prop))
1902 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1904 if (!zfs_prop_inheritable(prop) && !received)
1905 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1908 * Check to see if the value applies to this type
1910 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1911 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1914 * Normalize the name, to get rid of shorthand abbreviations.
1916 propname = zfs_prop_to_name(prop);
1917 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1918 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1920 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1921 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1922 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1923 "dataset is used in a non-global zone"));
1924 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1928 * Determine datasets which will be affected by this change, if any.
1930 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1931 return (-1);
1933 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1935 "child dataset with inherited mountpoint is used "
1936 "in a non-global zone"));
1937 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1938 goto error;
1941 if ((ret = changelist_prefix(cl)) != 0)
1942 goto error;
1944 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1945 return (zfs_standard_error(hdl, errno, errbuf));
1946 } else {
1948 if ((ret = changelist_postfix(cl)) != 0)
1949 goto error;
1952 * Refresh the statistics so the new property is reflected.
1954 (void) get_stats(zhp);
1957 error:
1958 changelist_free(cl);
1959 return (ret);
1963 * True DSL properties are stored in an nvlist. The following two functions
1964 * extract them appropriately.
1966 static uint64_t
1967 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1969 nvlist_t *nv;
1970 uint64_t value;
1972 *source = NULL;
1973 if (nvlist_lookup_nvlist(zhp->zfs_props,
1974 zfs_prop_to_name(prop), &nv) == 0) {
1975 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1976 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1977 } else {
1978 verify(!zhp->zfs_props_table ||
1979 zhp->zfs_props_table[prop] == B_TRUE);
1980 value = zfs_prop_default_numeric(prop);
1981 *source = "";
1984 return (value);
1987 static const char *
1988 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1990 nvlist_t *nv;
1991 const char *value;
1993 *source = NULL;
1994 if (nvlist_lookup_nvlist(zhp->zfs_props,
1995 zfs_prop_to_name(prop), &nv) == 0) {
1996 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1997 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1998 } else {
1999 verify(!zhp->zfs_props_table ||
2000 zhp->zfs_props_table[prop] == B_TRUE);
2001 value = zfs_prop_default_string(prop);
2002 *source = "";
2005 return (value);
2008 static boolean_t
2009 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2011 return (zhp->zfs_props == zhp->zfs_recvd_props);
2014 static void
2015 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2017 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2018 zhp->zfs_props = zhp->zfs_recvd_props;
2021 static void
2022 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2024 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2025 *cookie = 0;
2029 * Internal function for getting a numeric property. Both zfs_prop_get() and
2030 * zfs_prop_get_int() are built using this interface.
2032 * Certain properties can be overridden using 'mount -o'. In this case, scan
2033 * the contents of the /etc/mnttab entry, searching for the appropriate options.
2034 * If they differ from the on-disk values, report the current values and mark
2035 * the source "temporary".
2037 static int
2038 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2039 char **source, uint64_t *val)
2041 zfs_cmd_t zc = { 0 };
2042 nvlist_t *zplprops = NULL;
2043 struct mnttab mnt;
2044 char *mntopt_on = NULL;
2045 char *mntopt_off = NULL;
2046 boolean_t received = zfs_is_recvd_props_mode(zhp);
2048 *source = NULL;
2050 switch (prop) {
2051 case ZFS_PROP_ATIME:
2052 mntopt_on = MNTOPT_ATIME;
2053 mntopt_off = MNTOPT_NOATIME;
2054 break;
2056 case ZFS_PROP_DEVICES:
2057 mntopt_on = MNTOPT_DEVICES;
2058 mntopt_off = MNTOPT_NODEVICES;
2059 break;
2061 case ZFS_PROP_EXEC:
2062 mntopt_on = MNTOPT_EXEC;
2063 mntopt_off = MNTOPT_NOEXEC;
2064 break;
2066 case ZFS_PROP_READONLY:
2067 mntopt_on = MNTOPT_RO;
2068 mntopt_off = MNTOPT_RW;
2069 break;
2071 case ZFS_PROP_SETUID:
2072 mntopt_on = MNTOPT_SETUID;
2073 mntopt_off = MNTOPT_NOSETUID;
2074 break;
2076 case ZFS_PROP_XATTR:
2077 mntopt_on = MNTOPT_XATTR;
2078 mntopt_off = MNTOPT_NOXATTR;
2079 break;
2081 case ZFS_PROP_NBMAND:
2082 mntopt_on = MNTOPT_NBMAND;
2083 mntopt_off = MNTOPT_NONBMAND;
2084 break;
2086 default:
2087 break;
2091 * Because looking up the mount options is potentially expensive
2092 * (iterating over all of /etc/mnttab), we defer its calculation until
2093 * we're looking up a property which requires its presence.
2095 if (!zhp->zfs_mntcheck &&
2096 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2097 libzfs_handle_t *hdl = zhp->zfs_hdl;
2098 struct mnttab entry;
2100 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2101 zhp->zfs_mntopts = zfs_strdup(hdl,
2102 entry.mnt_mntopts);
2103 if (zhp->zfs_mntopts == NULL)
2104 return (-1);
2107 zhp->zfs_mntcheck = B_TRUE;
2110 if (zhp->zfs_mntopts == NULL)
2111 mnt.mnt_mntopts = "";
2112 else
2113 mnt.mnt_mntopts = zhp->zfs_mntopts;
2115 switch (prop) {
2116 case ZFS_PROP_ATIME:
2117 case ZFS_PROP_DEVICES:
2118 case ZFS_PROP_EXEC:
2119 case ZFS_PROP_READONLY:
2120 case ZFS_PROP_SETUID:
2121 case ZFS_PROP_XATTR:
2122 case ZFS_PROP_NBMAND:
2123 *val = getprop_uint64(zhp, prop, source);
2125 if (received)
2126 break;
2128 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2129 *val = B_TRUE;
2130 if (src)
2131 *src = ZPROP_SRC_TEMPORARY;
2132 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2133 *val = B_FALSE;
2134 if (src)
2135 *src = ZPROP_SRC_TEMPORARY;
2137 break;
2139 case ZFS_PROP_CANMOUNT:
2140 case ZFS_PROP_VOLSIZE:
2141 case ZFS_PROP_QUOTA:
2142 case ZFS_PROP_REFQUOTA:
2143 case ZFS_PROP_RESERVATION:
2144 case ZFS_PROP_REFRESERVATION:
2145 case ZFS_PROP_FILESYSTEM_LIMIT:
2146 case ZFS_PROP_SNAPSHOT_LIMIT:
2147 case ZFS_PROP_FILESYSTEM_COUNT:
2148 case ZFS_PROP_SNAPSHOT_COUNT:
2149 *val = getprop_uint64(zhp, prop, source);
2151 if (*source == NULL) {
2152 /* not default, must be local */
2153 *source = zhp->zfs_name;
2155 break;
2157 case ZFS_PROP_MOUNTED:
2158 *val = (zhp->zfs_mntopts != NULL);
2159 break;
2161 case ZFS_PROP_NUMCLONES:
2162 *val = zhp->zfs_dmustats.dds_num_clones;
2163 break;
2165 case ZFS_PROP_VERSION:
2166 case ZFS_PROP_NORMALIZE:
2167 case ZFS_PROP_UTF8ONLY:
2168 case ZFS_PROP_CASE:
2169 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2170 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2171 return (-1);
2172 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2173 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2174 zcmd_free_nvlists(&zc);
2175 return (-1);
2177 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2178 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2179 val) != 0) {
2180 zcmd_free_nvlists(&zc);
2181 return (-1);
2183 nvlist_free(zplprops);
2184 zcmd_free_nvlists(&zc);
2185 break;
2187 case ZFS_PROP_INCONSISTENT:
2188 *val = zhp->zfs_dmustats.dds_inconsistent;
2189 break;
2191 default:
2192 switch (zfs_prop_get_type(prop)) {
2193 case PROP_TYPE_NUMBER:
2194 case PROP_TYPE_INDEX:
2195 *val = getprop_uint64(zhp, prop, source);
2197 * If we tried to use a default value for a
2198 * readonly property, it means that it was not
2199 * present. Note this only applies to "truly"
2200 * readonly properties, not set-once properties
2201 * like volblocksize.
2203 if (zfs_prop_readonly(prop) &&
2204 !zfs_prop_setonce(prop) &&
2205 *source != NULL && (*source)[0] == '\0') {
2206 *source = NULL;
2207 return (-1);
2209 break;
2211 case PROP_TYPE_STRING:
2212 default:
2213 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2214 "cannot get non-numeric property"));
2215 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2216 dgettext(TEXT_DOMAIN, "internal error")));
2220 return (0);
2224 * Calculate the source type, given the raw source string.
2226 static void
2227 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2228 char *statbuf, size_t statlen)
2230 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2231 return;
2233 if (source == NULL) {
2234 *srctype = ZPROP_SRC_NONE;
2235 } else if (source[0] == '\0') {
2236 *srctype = ZPROP_SRC_DEFAULT;
2237 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2238 *srctype = ZPROP_SRC_RECEIVED;
2239 } else {
2240 if (strcmp(source, zhp->zfs_name) == 0) {
2241 *srctype = ZPROP_SRC_LOCAL;
2242 } else {
2243 (void) strlcpy(statbuf, source, statlen);
2244 *srctype = ZPROP_SRC_INHERITED;
2251 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2252 size_t proplen, boolean_t literal)
2254 zfs_prop_t prop;
2255 int err = 0;
2257 if (zhp->zfs_recvd_props == NULL)
2258 if (get_recvd_props_ioctl(zhp) != 0)
2259 return (-1);
2261 prop = zfs_name_to_prop(propname);
2263 if (prop != ZPROP_INVAL) {
2264 uint64_t cookie;
2265 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2266 return (-1);
2267 zfs_set_recvd_props_mode(zhp, &cookie);
2268 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2269 NULL, NULL, 0, literal);
2270 zfs_unset_recvd_props_mode(zhp, &cookie);
2271 } else {
2272 nvlist_t *propval;
2273 char *recvdval;
2274 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2275 propname, &propval) != 0)
2276 return (-1);
2277 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2278 &recvdval) == 0);
2279 (void) strlcpy(propbuf, recvdval, proplen);
2282 return (err == 0 ? 0 : -1);
2285 static int
2286 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2288 nvlist_t *value;
2289 nvpair_t *pair;
2291 value = zfs_get_clones_nvl(zhp);
2292 if (value == NULL)
2293 return (-1);
2295 propbuf[0] = '\0';
2296 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2297 pair = nvlist_next_nvpair(value, pair)) {
2298 if (propbuf[0] != '\0')
2299 (void) strlcat(propbuf, ",", proplen);
2300 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2303 return (0);
2306 struct get_clones_arg {
2307 uint64_t numclones;
2308 nvlist_t *value;
2309 const char *origin;
2310 char buf[ZFS_MAX_DATASET_NAME_LEN];
2314 get_clones_cb(zfs_handle_t *zhp, void *arg)
2316 struct get_clones_arg *gca = arg;
2318 if (gca->numclones == 0) {
2319 zfs_close(zhp);
2320 return (0);
2323 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2324 NULL, NULL, 0, B_TRUE) != 0)
2325 goto out;
2326 if (strcmp(gca->buf, gca->origin) == 0) {
2327 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2328 gca->numclones--;
2331 out:
2332 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2333 zfs_close(zhp);
2334 return (0);
2337 nvlist_t *
2338 zfs_get_clones_nvl(zfs_handle_t *zhp)
2340 nvlist_t *nv, *value;
2342 if (nvlist_lookup_nvlist(zhp->zfs_props,
2343 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2344 struct get_clones_arg gca;
2347 * if this is a snapshot, then the kernel wasn't able
2348 * to get the clones. Do it by slowly iterating.
2350 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2351 return (NULL);
2352 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2353 return (NULL);
2354 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2355 nvlist_free(nv);
2356 return (NULL);
2359 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2360 gca.value = value;
2361 gca.origin = zhp->zfs_name;
2363 if (gca.numclones != 0) {
2364 zfs_handle_t *root;
2365 char pool[ZFS_MAX_DATASET_NAME_LEN];
2366 char *cp = pool;
2368 /* get the pool name */
2369 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2370 (void) strsep(&cp, "/@");
2371 root = zfs_open(zhp->zfs_hdl, pool,
2372 ZFS_TYPE_FILESYSTEM);
2374 (void) get_clones_cb(root, &gca);
2377 if (gca.numclones != 0 ||
2378 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2379 nvlist_add_nvlist(zhp->zfs_props,
2380 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2381 nvlist_free(nv);
2382 nvlist_free(value);
2383 return (NULL);
2385 nvlist_free(nv);
2386 nvlist_free(value);
2387 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2388 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2391 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2393 return (value);
2397 * Accepts a property and value and checks that the value
2398 * matches the one found by the channel program. If they are
2399 * not equal, print both of them.
2401 void
2402 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2403 const char *strval)
2405 if (!zhp->zfs_hdl->libzfs_prop_debug)
2406 return;
2407 int error;
2408 char *poolname = zhp->zpool_hdl->zpool_name;
2409 const char *program =
2410 "args = ...\n"
2411 "ds = args['dataset']\n"
2412 "prop = args['property']\n"
2413 "value, setpoint = zfs.get_prop(ds, prop)\n"
2414 "return {value=value, setpoint=setpoint}\n";
2415 nvlist_t *outnvl;
2416 nvlist_t *retnvl;
2417 nvlist_t *argnvl = fnvlist_alloc();
2419 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2420 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2422 error = lzc_channel_program_nosync(poolname, program,
2423 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2425 if (error == 0) {
2426 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2427 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2428 int64_t ans;
2429 error = nvlist_lookup_int64(retnvl, "value", &ans);
2430 if (error != 0) {
2431 (void) fprintf(stderr, "zcp check error: %u\n",
2432 error);
2433 return;
2435 if (ans != intval) {
2436 (void) fprintf(stderr,
2437 "%s: zfs found %lld, but zcp found %lld\n",
2438 zfs_prop_to_name(prop),
2439 (longlong_t)intval, (longlong_t)ans);
2441 } else {
2442 char *str_ans;
2443 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2444 if (error != 0) {
2445 (void) fprintf(stderr, "zcp check error: %u\n",
2446 error);
2447 return;
2449 if (strcmp(strval, str_ans) != 0) {
2450 (void) fprintf(stderr,
2451 "%s: zfs found %s, but zcp found %s\n",
2452 zfs_prop_to_name(prop),
2453 strval, str_ans);
2456 } else {
2457 (void) fprintf(stderr,
2458 "zcp check failed, channel program error: %u\n", error);
2460 nvlist_free(argnvl);
2461 nvlist_free(outnvl);
2465 * Retrieve a property from the given object. If 'literal' is specified, then
2466 * numbers are left as exact values. Otherwise, numbers are converted to a
2467 * human-readable form.
2469 * Returns 0 on success, or -1 on error.
2472 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2473 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2475 char *source = NULL;
2476 uint64_t val;
2477 const char *str;
2478 const char *strval;
2479 boolean_t received = zfs_is_recvd_props_mode(zhp);
2482 * Check to see if this property applies to our object
2484 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2485 return (-1);
2487 if (received && zfs_prop_readonly(prop))
2488 return (-1);
2490 if (src)
2491 *src = ZPROP_SRC_NONE;
2493 switch (prop) {
2494 case ZFS_PROP_CREATION:
2496 * 'creation' is a time_t stored in the statistics. We convert
2497 * this into a string unless 'literal' is specified.
2500 val = getprop_uint64(zhp, prop, &source);
2501 time_t time = (time_t)val;
2502 struct tm t;
2504 if (literal ||
2505 localtime_r(&time, &t) == NULL ||
2506 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2507 &t) == 0)
2508 (void) snprintf(propbuf, proplen, "%llu", val);
2510 zcp_check(zhp, prop, val, NULL);
2511 break;
2513 case ZFS_PROP_MOUNTPOINT:
2515 * Getting the precise mountpoint can be tricky.
2517 * - for 'none' or 'legacy', return those values.
2518 * - for inherited mountpoints, we want to take everything
2519 * after our ancestor and append it to the inherited value.
2521 * If the pool has an alternate root, we want to prepend that
2522 * root to any values we return.
2525 str = getprop_string(zhp, prop, &source);
2527 if (str[0] == '/') {
2528 char buf[MAXPATHLEN];
2529 char *root = buf;
2530 const char *relpath;
2533 * If we inherit the mountpoint, even from a dataset
2534 * with a received value, the source will be the path of
2535 * the dataset we inherit from. If source is
2536 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2537 * inherited.
2539 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2540 relpath = "";
2541 } else {
2542 relpath = zhp->zfs_name + strlen(source);
2543 if (relpath[0] == '/')
2544 relpath++;
2547 if ((zpool_get_prop(zhp->zpool_hdl,
2548 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2549 B_FALSE)) || (strcmp(root, "-") == 0))
2550 root[0] = '\0';
2552 * Special case an alternate root of '/'. This will
2553 * avoid having multiple leading slashes in the
2554 * mountpoint path.
2556 if (strcmp(root, "/") == 0)
2557 root++;
2560 * If the mountpoint is '/' then skip over this
2561 * if we are obtaining either an alternate root or
2562 * an inherited mountpoint.
2564 if (str[1] == '\0' && (root[0] != '\0' ||
2565 relpath[0] != '\0'))
2566 str++;
2568 if (relpath[0] == '\0')
2569 (void) snprintf(propbuf, proplen, "%s%s",
2570 root, str);
2571 else
2572 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2573 root, str, relpath[0] == '@' ? "" : "/",
2574 relpath);
2575 } else {
2576 /* 'legacy' or 'none' */
2577 (void) strlcpy(propbuf, str, proplen);
2579 zcp_check(zhp, prop, NULL, propbuf);
2580 break;
2582 case ZFS_PROP_ORIGIN:
2583 str = getprop_string(zhp, prop, &source);
2584 if (str == NULL)
2585 return (-1);
2586 (void) strlcpy(propbuf, str, proplen);
2587 zcp_check(zhp, prop, NULL, str);
2588 break;
2590 case ZFS_PROP_CLONES:
2591 if (get_clones_string(zhp, propbuf, proplen) != 0)
2592 return (-1);
2593 break;
2595 case ZFS_PROP_QUOTA:
2596 case ZFS_PROP_REFQUOTA:
2597 case ZFS_PROP_RESERVATION:
2598 case ZFS_PROP_REFRESERVATION:
2600 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2601 return (-1);
2603 * If quota or reservation is 0, we translate this into 'none'
2604 * (unless literal is set), and indicate that it's the default
2605 * value. Otherwise, we print the number nicely and indicate
2606 * that its set locally.
2608 if (val == 0) {
2609 if (literal)
2610 (void) strlcpy(propbuf, "0", proplen);
2611 else
2612 (void) strlcpy(propbuf, "none", proplen);
2613 } else {
2614 if (literal)
2615 (void) snprintf(propbuf, proplen, "%llu",
2616 (u_longlong_t)val);
2617 else
2618 zfs_nicenum(val, propbuf, proplen);
2620 zcp_check(zhp, prop, val, NULL);
2621 break;
2623 case ZFS_PROP_FILESYSTEM_LIMIT:
2624 case ZFS_PROP_SNAPSHOT_LIMIT:
2625 case ZFS_PROP_FILESYSTEM_COUNT:
2626 case ZFS_PROP_SNAPSHOT_COUNT:
2628 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2629 return (-1);
2632 * If limit is UINT64_MAX, we translate this into 'none' (unless
2633 * literal is set), and indicate that it's the default value.
2634 * Otherwise, we print the number nicely and indicate that it's
2635 * set locally.
2637 if (literal) {
2638 (void) snprintf(propbuf, proplen, "%llu",
2639 (u_longlong_t)val);
2640 } else if (val == UINT64_MAX) {
2641 (void) strlcpy(propbuf, "none", proplen);
2642 } else {
2643 zfs_nicenum(val, propbuf, proplen);
2646 zcp_check(zhp, prop, val, NULL);
2647 break;
2649 case ZFS_PROP_REFRATIO:
2650 case ZFS_PROP_COMPRESSRATIO:
2651 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2652 return (-1);
2653 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2654 (u_longlong_t)(val / 100),
2655 (u_longlong_t)(val % 100));
2656 zcp_check(zhp, prop, val, NULL);
2657 break;
2659 case ZFS_PROP_TYPE:
2660 switch (zhp->zfs_type) {
2661 case ZFS_TYPE_FILESYSTEM:
2662 str = "filesystem";
2663 break;
2664 case ZFS_TYPE_VOLUME:
2665 str = "volume";
2666 break;
2667 case ZFS_TYPE_SNAPSHOT:
2668 str = "snapshot";
2669 break;
2670 case ZFS_TYPE_BOOKMARK:
2671 str = "bookmark";
2672 break;
2673 default:
2674 abort();
2676 (void) snprintf(propbuf, proplen, "%s", str);
2677 zcp_check(zhp, prop, NULL, propbuf);
2678 break;
2680 case ZFS_PROP_MOUNTED:
2682 * The 'mounted' property is a pseudo-property that described
2683 * whether the filesystem is currently mounted. Even though
2684 * it's a boolean value, the typical values of "on" and "off"
2685 * don't make sense, so we translate to "yes" and "no".
2687 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2688 src, &source, &val) != 0)
2689 return (-1);
2690 if (val)
2691 (void) strlcpy(propbuf, "yes", proplen);
2692 else
2693 (void) strlcpy(propbuf, "no", proplen);
2694 break;
2696 case ZFS_PROP_NAME:
2698 * The 'name' property is a pseudo-property derived from the
2699 * dataset name. It is presented as a real property to simplify
2700 * consumers.
2702 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2703 zcp_check(zhp, prop, NULL, propbuf);
2704 break;
2706 case ZFS_PROP_MLSLABEL:
2708 m_label_t *new_sl = NULL;
2709 char *ascii = NULL; /* human readable label */
2711 (void) strlcpy(propbuf,
2712 getprop_string(zhp, prop, &source), proplen);
2714 if (literal || (strcasecmp(propbuf,
2715 ZFS_MLSLABEL_DEFAULT) == 0))
2716 break;
2719 * Try to translate the internal hex string to
2720 * human-readable output. If there are any
2721 * problems just use the hex string.
2724 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2725 L_NO_CORRECTION, NULL) == -1) {
2726 m_label_free(new_sl);
2727 break;
2730 if (label_to_str(new_sl, &ascii, M_LABEL,
2731 DEF_NAMES) != 0) {
2732 if (ascii)
2733 free(ascii);
2734 m_label_free(new_sl);
2735 break;
2737 m_label_free(new_sl);
2739 (void) strlcpy(propbuf, ascii, proplen);
2740 free(ascii);
2742 break;
2744 case ZFS_PROP_GUID:
2746 * GUIDs are stored as numbers, but they are identifiers.
2747 * We don't want them to be pretty printed, because pretty
2748 * printing mangles the ID into a truncated and useless value.
2750 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2751 return (-1);
2752 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2753 zcp_check(zhp, prop, val, NULL);
2754 break;
2756 default:
2757 switch (zfs_prop_get_type(prop)) {
2758 case PROP_TYPE_NUMBER:
2759 if (get_numeric_property(zhp, prop, src,
2760 &source, &val) != 0) {
2761 return (-1);
2764 if (literal) {
2765 (void) snprintf(propbuf, proplen, "%llu",
2766 (u_longlong_t)val);
2767 } else {
2768 zfs_nicenum(val, propbuf, proplen);
2770 zcp_check(zhp, prop, val, NULL);
2771 break;
2773 case PROP_TYPE_STRING:
2774 str = getprop_string(zhp, prop, &source);
2775 if (str == NULL)
2776 return (-1);
2778 (void) strlcpy(propbuf, str, proplen);
2779 zcp_check(zhp, prop, NULL, str);
2780 break;
2782 case PROP_TYPE_INDEX:
2783 if (get_numeric_property(zhp, prop, src,
2784 &source, &val) != 0)
2785 return (-1);
2786 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2787 return (-1);
2789 (void) strlcpy(propbuf, strval, proplen);
2790 zcp_check(zhp, prop, NULL, strval);
2791 break;
2793 default:
2794 abort();
2798 get_source(zhp, src, source, statbuf, statlen);
2800 return (0);
2804 * Utility function to get the given numeric property. Does no validation that
2805 * the given property is the appropriate type; should only be used with
2806 * hard-coded property types.
2808 uint64_t
2809 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2811 char *source;
2812 uint64_t val;
2814 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2816 return (val);
2820 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2822 char buf[64];
2824 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2825 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2829 * Similar to zfs_prop_get(), but returns the value as an integer.
2832 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2833 zprop_source_t *src, char *statbuf, size_t statlen)
2835 char *source;
2838 * Check to see if this property applies to our object
2840 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2841 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2842 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2843 zfs_prop_to_name(prop)));
2846 if (src)
2847 *src = ZPROP_SRC_NONE;
2849 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2850 return (-1);
2852 get_source(zhp, src, source, statbuf, statlen);
2854 return (0);
2857 static int
2858 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2859 char **domainp, idmap_rid_t *ridp)
2861 idmap_get_handle_t *get_hdl = NULL;
2862 idmap_stat status;
2863 int err = EINVAL;
2865 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2866 goto out;
2868 if (isuser) {
2869 err = idmap_get_sidbyuid(get_hdl, id,
2870 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2871 } else {
2872 err = idmap_get_sidbygid(get_hdl, id,
2873 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2875 if (err == IDMAP_SUCCESS &&
2876 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2877 status == IDMAP_SUCCESS)
2878 err = 0;
2879 else
2880 err = EINVAL;
2881 out:
2882 if (get_hdl)
2883 idmap_get_destroy(get_hdl);
2884 return (err);
2888 * convert the propname into parameters needed by kernel
2889 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2890 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2892 static int
2893 userquota_propname_decode(const char *propname, boolean_t zoned,
2894 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2896 zfs_userquota_prop_t type;
2897 char *cp, *end;
2898 char *numericsid = NULL;
2899 boolean_t isuser;
2901 domain[0] = '\0';
2902 *ridp = 0;
2903 /* Figure out the property type ({user|group}{quota|space}) */
2904 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2905 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2906 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2907 break;
2909 if (type == ZFS_NUM_USERQUOTA_PROPS)
2910 return (EINVAL);
2911 *typep = type;
2913 isuser = (type == ZFS_PROP_USERQUOTA ||
2914 type == ZFS_PROP_USERUSED);
2916 cp = strchr(propname, '@') + 1;
2918 if (strchr(cp, '@')) {
2920 * It's a SID name (eg "user@domain") that needs to be
2921 * turned into S-1-domainID-RID.
2923 int flag = 0;
2924 idmap_stat stat, map_stat;
2925 uid_t pid;
2926 idmap_rid_t rid;
2927 idmap_get_handle_t *gh = NULL;
2929 stat = idmap_get_create(&gh);
2930 if (stat != IDMAP_SUCCESS) {
2931 idmap_get_destroy(gh);
2932 return (ENOMEM);
2934 if (zoned && getzoneid() == GLOBAL_ZONEID)
2935 return (ENOENT);
2936 if (isuser) {
2937 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2938 if (stat < 0)
2939 return (ENOENT);
2940 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2941 &rid, &map_stat);
2942 } else {
2943 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2944 if (stat < 0)
2945 return (ENOENT);
2946 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2947 &rid, &map_stat);
2949 if (stat < 0) {
2950 idmap_get_destroy(gh);
2951 return (ENOENT);
2953 stat = idmap_get_mappings(gh);
2954 idmap_get_destroy(gh);
2956 if (stat < 0) {
2957 return (ENOENT);
2959 if (numericsid == NULL)
2960 return (ENOENT);
2961 cp = numericsid;
2962 *ridp = rid;
2963 /* will be further decoded below */
2966 if (strncmp(cp, "S-1-", 4) == 0) {
2967 /* It's a numeric SID (eg "S-1-234-567-89") */
2968 (void) strlcpy(domain, cp, domainlen);
2969 errno = 0;
2970 if (*ridp == 0) {
2971 cp = strrchr(domain, '-');
2972 *cp = '\0';
2973 cp++;
2974 *ridp = strtoull(cp, &end, 10);
2975 } else {
2976 end = "";
2978 if (numericsid) {
2979 free(numericsid);
2980 numericsid = NULL;
2982 if (errno != 0 || *end != '\0')
2983 return (EINVAL);
2984 } else if (!isdigit(*cp)) {
2986 * It's a user/group name (eg "user") that needs to be
2987 * turned into a uid/gid
2989 if (zoned && getzoneid() == GLOBAL_ZONEID)
2990 return (ENOENT);
2991 if (isuser) {
2992 struct passwd *pw;
2993 pw = getpwnam(cp);
2994 if (pw == NULL)
2995 return (ENOENT);
2996 *ridp = pw->pw_uid;
2997 } else {
2998 struct group *gr;
2999 gr = getgrnam(cp);
3000 if (gr == NULL)
3001 return (ENOENT);
3002 *ridp = gr->gr_gid;
3004 } else {
3005 /* It's a user/group ID (eg "12345"). */
3006 uid_t id = strtoul(cp, &end, 10);
3007 idmap_rid_t rid;
3008 char *mapdomain;
3010 if (*end != '\0')
3011 return (EINVAL);
3012 if (id > MAXUID) {
3013 /* It's an ephemeral ID. */
3014 if (idmap_id_to_numeric_domain_rid(id, isuser,
3015 &mapdomain, &rid) != 0)
3016 return (ENOENT);
3017 (void) strlcpy(domain, mapdomain, domainlen);
3018 *ridp = rid;
3019 } else {
3020 *ridp = id;
3024 ASSERT3P(numericsid, ==, NULL);
3025 return (0);
3028 static int
3029 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3030 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3032 int err;
3033 zfs_cmd_t zc = { 0 };
3035 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3037 err = userquota_propname_decode(propname,
3038 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3039 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3040 zc.zc_objset_type = *typep;
3041 if (err)
3042 return (err);
3044 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3045 if (err)
3046 return (err);
3048 *propvalue = zc.zc_cookie;
3049 return (0);
3053 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3054 uint64_t *propvalue)
3056 zfs_userquota_prop_t type;
3058 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3059 &type));
3063 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3064 char *propbuf, int proplen, boolean_t literal)
3066 int err;
3067 uint64_t propvalue;
3068 zfs_userquota_prop_t type;
3070 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3071 &type);
3073 if (err)
3074 return (err);
3076 if (literal) {
3077 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3078 } else if (propvalue == 0 &&
3079 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
3080 (void) strlcpy(propbuf, "none", proplen);
3081 } else {
3082 zfs_nicenum(propvalue, propbuf, proplen);
3084 return (0);
3088 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3089 uint64_t *propvalue)
3091 int err;
3092 zfs_cmd_t zc = { 0 };
3093 const char *snapname;
3095 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3097 snapname = strchr(propname, '@') + 1;
3098 if (strchr(snapname, '@')) {
3099 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3100 } else {
3101 /* snapname is the short name, append it to zhp's fsname */
3102 char *cp;
3104 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3105 sizeof (zc.zc_value));
3106 cp = strchr(zc.zc_value, '@');
3107 if (cp != NULL)
3108 *cp = '\0';
3109 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3110 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3113 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3114 if (err)
3115 return (err);
3117 *propvalue = zc.zc_cookie;
3118 return (0);
3122 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3123 char *propbuf, int proplen, boolean_t literal)
3125 int err;
3126 uint64_t propvalue;
3128 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3130 if (err)
3131 return (err);
3133 if (literal) {
3134 (void) snprintf(propbuf, proplen, "%llu", propvalue);
3135 } else {
3136 zfs_nicenum(propvalue, propbuf, proplen);
3138 return (0);
3142 * Returns the name of the given zfs handle.
3144 const char *
3145 zfs_get_name(const zfs_handle_t *zhp)
3147 return (zhp->zfs_name);
3151 * Returns the name of the parent pool for the given zfs handle.
3153 const char *
3154 zfs_get_pool_name(const zfs_handle_t *zhp)
3156 return (zhp->zpool_hdl->zpool_name);
3160 * Returns the type of the given zfs handle.
3162 zfs_type_t
3163 zfs_get_type(const zfs_handle_t *zhp)
3165 return (zhp->zfs_type);
3169 * Is one dataset name a child dataset of another?
3171 * Needs to handle these cases:
3172 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3173 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3174 * Descendant? No. No. No. Yes.
3176 static boolean_t
3177 is_descendant(const char *ds1, const char *ds2)
3179 size_t d1len = strlen(ds1);
3181 /* ds2 can't be a descendant if it's smaller */
3182 if (strlen(ds2) < d1len)
3183 return (B_FALSE);
3185 /* otherwise, compare strings and verify that there's a '/' char */
3186 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3190 * Given a complete name, return just the portion that refers to the parent.
3191 * Will return -1 if there is no parent (path is just the name of the
3192 * pool).
3194 static int
3195 parent_name(const char *path, char *buf, size_t buflen)
3197 char *slashp;
3199 (void) strlcpy(buf, path, buflen);
3201 if ((slashp = strrchr(buf, '/')) == NULL)
3202 return (-1);
3203 *slashp = '\0';
3205 return (0);
3209 * If accept_ancestor is false, then check to make sure that the given path has
3210 * a parent, and that it exists. If accept_ancestor is true, then find the
3211 * closest existing ancestor for the given path. In prefixlen return the
3212 * length of already existing prefix of the given path. We also fetch the
3213 * 'zoned' property, which is used to validate property settings when creating
3214 * new datasets.
3216 static int
3217 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3218 boolean_t accept_ancestor, int *prefixlen)
3220 zfs_cmd_t zc = { 0 };
3221 char parent[ZFS_MAX_DATASET_NAME_LEN];
3222 char *slash;
3223 zfs_handle_t *zhp;
3224 char errbuf[1024];
3225 uint64_t is_zoned;
3227 (void) snprintf(errbuf, sizeof (errbuf),
3228 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3230 /* get parent, and check to see if this is just a pool */
3231 if (parent_name(path, parent, sizeof (parent)) != 0) {
3232 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3233 "missing dataset name"));
3234 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3237 /* check to see if the pool exists */
3238 if ((slash = strchr(parent, '/')) == NULL)
3239 slash = parent + strlen(parent);
3240 (void) strncpy(zc.zc_name, parent, slash - parent);
3241 zc.zc_name[slash - parent] = '\0';
3242 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3243 errno == ENOENT) {
3244 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3245 "no such pool '%s'"), zc.zc_name);
3246 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3249 /* check to see if the parent dataset exists */
3250 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3251 if (errno == ENOENT && accept_ancestor) {
3253 * Go deeper to find an ancestor, give up on top level.
3255 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3256 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3257 "no such pool '%s'"), zc.zc_name);
3258 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3260 } else if (errno == ENOENT) {
3261 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3262 "parent does not exist"));
3263 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3264 } else
3265 return (zfs_standard_error(hdl, errno, errbuf));
3268 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3269 if (zoned != NULL)
3270 *zoned = is_zoned;
3272 /* we are in a non-global zone, but parent is in the global zone */
3273 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3274 (void) zfs_standard_error(hdl, EPERM, errbuf);
3275 zfs_close(zhp);
3276 return (-1);
3279 /* make sure parent is a filesystem */
3280 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3281 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3282 "parent is not a filesystem"));
3283 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3284 zfs_close(zhp);
3285 return (-1);
3288 zfs_close(zhp);
3289 if (prefixlen != NULL)
3290 *prefixlen = strlen(parent);
3291 return (0);
3295 * Finds whether the dataset of the given type(s) exists.
3297 boolean_t
3298 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3300 zfs_handle_t *zhp;
3302 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3303 return (B_FALSE);
3306 * Try to get stats for the dataset, which will tell us if it exists.
3308 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3309 int ds_type = zhp->zfs_type;
3311 zfs_close(zhp);
3312 if (types & ds_type)
3313 return (B_TRUE);
3315 return (B_FALSE);
3319 * Given a path to 'target', create all the ancestors between
3320 * the prefixlen portion of the path, and the target itself.
3321 * Fail if the initial prefixlen-ancestor does not already exist.
3324 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3326 zfs_handle_t *h;
3327 char *cp;
3328 const char *opname;
3330 /* make sure prefix exists */
3331 cp = target + prefixlen;
3332 if (*cp != '/') {
3333 assert(strchr(cp, '/') == NULL);
3334 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3335 } else {
3336 *cp = '\0';
3337 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3338 *cp = '/';
3340 if (h == NULL)
3341 return (-1);
3342 zfs_close(h);
3345 * Attempt to create, mount, and share any ancestor filesystems,
3346 * up to the prefixlen-long one.
3348 for (cp = target + prefixlen + 1;
3349 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3351 *cp = '\0';
3353 h = make_dataset_handle(hdl, target);
3354 if (h) {
3355 /* it already exists, nothing to do here */
3356 zfs_close(h);
3357 continue;
3360 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3361 NULL) != 0) {
3362 opname = dgettext(TEXT_DOMAIN, "create");
3363 goto ancestorerr;
3366 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3367 if (h == NULL) {
3368 opname = dgettext(TEXT_DOMAIN, "open");
3369 goto ancestorerr;
3372 if (zfs_mount(h, NULL, 0) != 0) {
3373 opname = dgettext(TEXT_DOMAIN, "mount");
3374 goto ancestorerr;
3377 if (zfs_share(h) != 0) {
3378 opname = dgettext(TEXT_DOMAIN, "share");
3379 goto ancestorerr;
3382 zfs_close(h);
3385 return (0);
3387 ancestorerr:
3388 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3389 "failed to %s ancestor '%s'"), opname, target);
3390 return (-1);
3394 * Creates non-existing ancestors of the given path.
3397 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3399 int prefix;
3400 char *path_copy;
3401 int rc = 0;
3403 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3404 return (-1);
3406 if ((path_copy = strdup(path)) != NULL) {
3407 rc = create_parents(hdl, path_copy, prefix);
3408 free(path_copy);
3410 if (path_copy == NULL || rc != 0)
3411 return (-1);
3413 return (0);
3417 * Create a new filesystem or volume.
3420 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3421 nvlist_t *props)
3423 int ret;
3424 uint64_t size = 0;
3425 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3426 char errbuf[1024];
3427 uint64_t zoned;
3428 enum lzc_dataset_type ost;
3429 zpool_handle_t *zpool_handle;
3431 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3432 "cannot create '%s'"), path);
3434 /* validate the path, taking care to note the extended error message */
3435 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3436 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3438 /* validate parents exist */
3439 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3440 return (-1);
3443 * The failure modes when creating a dataset of a different type over
3444 * one that already exists is a little strange. In particular, if you
3445 * try to create a dataset on top of an existing dataset, the ioctl()
3446 * will return ENOENT, not EEXIST. To prevent this from happening, we
3447 * first try to see if the dataset exists.
3449 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3451 "dataset already exists"));
3452 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3455 if (type == ZFS_TYPE_VOLUME)
3456 ost = LZC_DATSET_TYPE_ZVOL;
3457 else
3458 ost = LZC_DATSET_TYPE_ZFS;
3460 /* open zpool handle for prop validation */
3461 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3462 (void) strlcpy(pool_path, path, sizeof (pool_path));
3464 /* truncate pool_path at first slash */
3465 char *p = strchr(pool_path, '/');
3466 if (p != NULL)
3467 *p = '\0';
3469 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3470 return (-1);
3472 if (props && (props = zfs_valid_proplist(hdl, type, props,
3473 zoned, NULL, zpool_handle, errbuf)) == 0) {
3474 zpool_close(zpool_handle);
3475 return (-1);
3477 zpool_close(zpool_handle);
3479 if (type == ZFS_TYPE_VOLUME) {
3481 * If we are creating a volume, the size and block size must
3482 * satisfy a few restraints. First, the blocksize must be a
3483 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3484 * volsize must be a multiple of the block size, and cannot be
3485 * zero.
3487 if (props == NULL || nvlist_lookup_uint64(props,
3488 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3489 nvlist_free(props);
3490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3491 "missing volume size"));
3492 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3495 if ((ret = nvlist_lookup_uint64(props,
3496 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3497 &blocksize)) != 0) {
3498 if (ret == ENOENT) {
3499 blocksize = zfs_prop_default_numeric(
3500 ZFS_PROP_VOLBLOCKSIZE);
3501 } else {
3502 nvlist_free(props);
3503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3504 "missing volume block size"));
3505 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3509 if (size == 0) {
3510 nvlist_free(props);
3511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3512 "volume size cannot be zero"));
3513 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3516 if (size % blocksize != 0) {
3517 nvlist_free(props);
3518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3519 "volume size must be a multiple of volume block "
3520 "size"));
3521 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3525 /* create the dataset */
3526 ret = lzc_create(path, ost, props);
3527 nvlist_free(props);
3529 /* check for failure */
3530 if (ret != 0) {
3531 char parent[ZFS_MAX_DATASET_NAME_LEN];
3532 (void) parent_name(path, parent, sizeof (parent));
3534 switch (errno) {
3535 case ENOENT:
3536 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3537 "no such parent '%s'"), parent);
3538 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3540 case EINVAL:
3541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3542 "parent '%s' is not a filesystem"), parent);
3543 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3545 case ENOTSUP:
3546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3547 "pool must be upgraded to set this "
3548 "property or value"));
3549 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3550 case ERANGE:
3551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3552 "invalid property value(s) specified"));
3553 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3554 #ifdef _ILP32
3555 case EOVERFLOW:
3557 * This platform can't address a volume this big.
3559 if (type == ZFS_TYPE_VOLUME)
3560 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3561 errbuf));
3562 #endif
3563 /* FALLTHROUGH */
3564 default:
3565 return (zfs_standard_error(hdl, errno, errbuf));
3569 return (0);
3573 * Destroys the given dataset. The caller must make sure that the filesystem
3574 * isn't mounted, and that there are no active dependents. If the file system
3575 * does not exist this function does nothing.
3578 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3580 zfs_cmd_t zc = { 0 };
3582 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3583 nvlist_t *nv = fnvlist_alloc();
3584 fnvlist_add_boolean(nv, zhp->zfs_name);
3585 int error = lzc_destroy_bookmarks(nv, NULL);
3586 fnvlist_free(nv);
3587 if (error != 0) {
3588 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3589 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3590 zhp->zfs_name));
3592 return (0);
3595 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3597 if (ZFS_IS_VOLUME(zhp)) {
3598 zc.zc_objset_type = DMU_OST_ZVOL;
3599 } else {
3600 zc.zc_objset_type = DMU_OST_ZFS;
3603 zc.zc_defer_destroy = defer;
3604 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3605 errno != ENOENT) {
3606 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3607 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3608 zhp->zfs_name));
3611 remove_mountpoint(zhp);
3613 return (0);
3616 struct destroydata {
3617 nvlist_t *nvl;
3618 const char *snapname;
3621 static int
3622 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3624 struct destroydata *dd = arg;
3625 char name[ZFS_MAX_DATASET_NAME_LEN];
3626 int rv = 0;
3628 (void) snprintf(name, sizeof (name),
3629 "%s@%s", zhp->zfs_name, dd->snapname);
3631 if (lzc_exists(name))
3632 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3634 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3635 zfs_close(zhp);
3636 return (rv);
3640 * Destroys all snapshots with the given name in zhp & descendants.
3643 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3645 int ret;
3646 struct destroydata dd = { 0 };
3648 dd.snapname = snapname;
3649 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3650 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3652 if (nvlist_empty(dd.nvl)) {
3653 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3654 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3655 zhp->zfs_name, snapname);
3656 } else {
3657 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3659 nvlist_free(dd.nvl);
3660 return (ret);
3664 * Destroys all the snapshots named in the nvlist.
3667 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3669 int ret;
3670 nvlist_t *errlist = NULL;
3672 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3674 if (ret == 0) {
3675 nvlist_free(errlist);
3676 return (0);
3679 if (nvlist_empty(errlist)) {
3680 char errbuf[1024];
3681 (void) snprintf(errbuf, sizeof (errbuf),
3682 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3684 ret = zfs_standard_error(hdl, ret, errbuf);
3686 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3687 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3688 char errbuf[1024];
3689 (void) snprintf(errbuf, sizeof (errbuf),
3690 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3691 nvpair_name(pair));
3693 switch (fnvpair_value_int32(pair)) {
3694 case EEXIST:
3695 zfs_error_aux(hdl,
3696 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3697 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3698 break;
3699 default:
3700 ret = zfs_standard_error(hdl, errno, errbuf);
3701 break;
3705 nvlist_free(errlist);
3706 return (ret);
3710 * Clones the given dataset. The target must be of the same type as the source.
3713 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3715 char parent[ZFS_MAX_DATASET_NAME_LEN];
3716 int ret;
3717 char errbuf[1024];
3718 libzfs_handle_t *hdl = zhp->zfs_hdl;
3719 uint64_t zoned;
3721 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3723 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3724 "cannot create '%s'"), target);
3726 /* validate the target/clone name */
3727 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3728 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3730 /* validate parents exist */
3731 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3732 return (-1);
3734 (void) parent_name(target, parent, sizeof (parent));
3736 /* do the clone */
3738 if (props) {
3739 zfs_type_t type;
3741 if (ZFS_IS_VOLUME(zhp)) {
3742 type = ZFS_TYPE_VOLUME;
3743 } else {
3744 type = ZFS_TYPE_FILESYSTEM;
3746 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3747 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3748 return (-1);
3749 if (zfs_fix_auto_resv(zhp, props) == -1) {
3750 nvlist_free(props);
3751 return (-1);
3755 ret = lzc_clone(target, zhp->zfs_name, props);
3756 nvlist_free(props);
3758 if (ret != 0) {
3759 switch (errno) {
3761 case ENOENT:
3763 * The parent doesn't exist. We should have caught this
3764 * above, but there may a race condition that has since
3765 * destroyed the parent.
3767 * At this point, we don't know whether it's the source
3768 * that doesn't exist anymore, or whether the target
3769 * dataset doesn't exist.
3771 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3772 "no such parent '%s'"), parent);
3773 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3775 case EXDEV:
3776 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3777 "source and target pools differ"));
3778 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3779 errbuf));
3781 default:
3782 return (zfs_standard_error(zhp->zfs_hdl, errno,
3783 errbuf));
3787 return (ret);
3791 * Promotes the given clone fs to be the clone parent.
3794 zfs_promote(zfs_handle_t *zhp)
3796 libzfs_handle_t *hdl = zhp->zfs_hdl;
3797 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3798 int ret;
3799 char errbuf[1024];
3801 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3802 "cannot promote '%s'"), zhp->zfs_name);
3804 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3806 "snapshots can not be promoted"));
3807 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3810 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3812 "not a cloned filesystem"));
3813 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3816 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3817 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3819 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3821 if (ret != 0) {
3822 switch (ret) {
3823 case EEXIST:
3824 /* There is a conflicting snapshot name. */
3825 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3826 "conflicting snapshot '%s' from parent '%s'"),
3827 snapname, zhp->zfs_dmustats.dds_origin);
3828 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3830 default:
3831 return (zfs_standard_error(hdl, ret, errbuf));
3834 return (ret);
3837 typedef struct snapdata {
3838 nvlist_t *sd_nvl;
3839 const char *sd_snapname;
3840 } snapdata_t;
3842 static int
3843 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3845 snapdata_t *sd = arg;
3846 char name[ZFS_MAX_DATASET_NAME_LEN];
3847 int rv = 0;
3849 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3850 (void) snprintf(name, sizeof (name),
3851 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3853 fnvlist_add_boolean(sd->sd_nvl, name);
3855 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3857 zfs_close(zhp);
3859 return (rv);
3863 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3865 int err;
3866 char errbuf[1024];
3868 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3869 "cannot remap filesystem '%s' "), fs);
3871 err = lzc_remap(fs);
3873 if (err != 0) {
3874 (void) zfs_standard_error(hdl, err, errbuf);
3877 return (err);
3881 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3882 * created.
3885 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3887 int ret;
3888 char errbuf[1024];
3889 nvpair_t *elem;
3890 nvlist_t *errors;
3892 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3893 "cannot create snapshots "));
3895 elem = NULL;
3896 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3897 const char *snapname = nvpair_name(elem);
3899 /* validate the target name */
3900 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3901 B_TRUE)) {
3902 (void) snprintf(errbuf, sizeof (errbuf),
3903 dgettext(TEXT_DOMAIN,
3904 "cannot create snapshot '%s'"), snapname);
3905 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3910 * get pool handle for prop validation. assumes all snaps are in the
3911 * same pool, as does lzc_snapshot (below).
3913 char pool[ZFS_MAX_DATASET_NAME_LEN];
3914 elem = nvlist_next_nvpair(snaps, NULL);
3915 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3916 pool[strcspn(pool, "/@")] = '\0';
3917 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3919 if (props != NULL &&
3920 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3921 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3922 zpool_close(zpool_hdl);
3923 return (-1);
3925 zpool_close(zpool_hdl);
3927 ret = lzc_snapshot(snaps, props, &errors);
3929 if (ret != 0) {
3930 boolean_t printed = B_FALSE;
3931 for (elem = nvlist_next_nvpair(errors, NULL);
3932 elem != NULL;
3933 elem = nvlist_next_nvpair(errors, elem)) {
3934 (void) snprintf(errbuf, sizeof (errbuf),
3935 dgettext(TEXT_DOMAIN,
3936 "cannot create snapshot '%s'"), nvpair_name(elem));
3937 (void) zfs_standard_error(hdl,
3938 fnvpair_value_int32(elem), errbuf);
3939 printed = B_TRUE;
3941 if (!printed) {
3942 switch (ret) {
3943 case EXDEV:
3944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3945 "multiple snapshots of same "
3946 "fs not allowed"));
3947 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3949 break;
3950 default:
3951 (void) zfs_standard_error(hdl, ret, errbuf);
3956 nvlist_free(props);
3957 nvlist_free(errors);
3958 return (ret);
3962 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3963 nvlist_t *props)
3965 int ret;
3966 snapdata_t sd = { 0 };
3967 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3968 char *cp;
3969 zfs_handle_t *zhp;
3970 char errbuf[1024];
3972 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3973 "cannot snapshot %s"), path);
3975 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3976 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3978 (void) strlcpy(fsname, path, sizeof (fsname));
3979 cp = strchr(fsname, '@');
3980 *cp = '\0';
3981 sd.sd_snapname = cp + 1;
3983 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3984 ZFS_TYPE_VOLUME)) == NULL) {
3985 return (-1);
3988 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3989 if (recursive) {
3990 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3991 } else {
3992 fnvlist_add_boolean(sd.sd_nvl, path);
3995 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3996 nvlist_free(sd.sd_nvl);
3997 zfs_close(zhp);
3998 return (ret);
4002 * Destroy any more recent snapshots. We invoke this callback on any dependents
4003 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4004 * is a dependent and we should just destroy it without checking the transaction
4005 * group.
4007 typedef struct rollback_data {
4008 const char *cb_target; /* the snapshot */
4009 uint64_t cb_create; /* creation time reference */
4010 boolean_t cb_error;
4011 boolean_t cb_force;
4012 } rollback_data_t;
4014 static int
4015 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4017 rollback_data_t *cbp = data;
4018 prop_changelist_t *clp;
4020 /* We must destroy this clone; first unmount it */
4021 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4022 cbp->cb_force ? MS_FORCE: 0);
4023 if (clp == NULL || changelist_prefix(clp) != 0) {
4024 cbp->cb_error = B_TRUE;
4025 zfs_close(zhp);
4026 return (0);
4028 if (zfs_destroy(zhp, B_FALSE) != 0)
4029 cbp->cb_error = B_TRUE;
4030 else
4031 changelist_remove(clp, zhp->zfs_name);
4032 (void) changelist_postfix(clp);
4033 changelist_free(clp);
4035 zfs_close(zhp);
4036 return (0);
4039 static int
4040 rollback_destroy(zfs_handle_t *zhp, void *data)
4042 rollback_data_t *cbp = data;
4044 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4045 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4046 rollback_destroy_dependent, cbp);
4048 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4051 zfs_close(zhp);
4052 return (0);
4056 * Given a dataset, rollback to a specific snapshot, discarding any
4057 * data changes since then and making it the active dataset.
4059 * Any snapshots and bookmarks more recent than the target are
4060 * destroyed, along with their dependents (i.e. clones).
4063 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4065 rollback_data_t cb = { 0 };
4066 int err;
4067 boolean_t restore_resv = 0;
4068 uint64_t old_volsize = 0, new_volsize;
4069 zfs_prop_t resv_prop;
4071 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4072 zhp->zfs_type == ZFS_TYPE_VOLUME);
4075 * Destroy all recent snapshots and their dependents.
4077 cb.cb_force = force;
4078 cb.cb_target = snap->zfs_name;
4079 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4080 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4081 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4083 if (cb.cb_error)
4084 return (-1);
4087 * Now that we have verified that the snapshot is the latest,
4088 * rollback to the given snapshot.
4091 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4092 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4093 return (-1);
4094 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4095 restore_resv =
4096 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4100 * Pass both the filesystem and the wanted snapshot names,
4101 * we would get an error back if the snapshot is destroyed or
4102 * a new snapshot is created before this request is processed.
4104 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4105 if (err != 0) {
4106 char errbuf[1024];
4108 (void) snprintf(errbuf, sizeof (errbuf),
4109 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4110 zhp->zfs_name);
4111 switch (err) {
4112 case EEXIST:
4113 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4114 "there is a snapshot or bookmark more recent "
4115 "than '%s'"), snap->zfs_name);
4116 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4117 break;
4118 case ESRCH:
4119 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4120 "'%s' is not found among snapshots of '%s'"),
4121 snap->zfs_name, zhp->zfs_name);
4122 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4123 break;
4124 case EINVAL:
4125 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4126 break;
4127 default:
4128 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4130 return (err);
4134 * For volumes, if the pre-rollback volsize matched the pre-
4135 * rollback reservation and the volsize has changed then set
4136 * the reservation property to the post-rollback volsize.
4137 * Make a new handle since the rollback closed the dataset.
4139 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4140 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4141 if (restore_resv) {
4142 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4143 if (old_volsize != new_volsize)
4144 err = zfs_prop_set_int(zhp, resv_prop,
4145 new_volsize);
4147 zfs_close(zhp);
4149 return (err);
4153 * Renames the given dataset.
4156 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4157 boolean_t force_unmount)
4159 int ret = 0;
4160 zfs_cmd_t zc = { 0 };
4161 char *delim;
4162 prop_changelist_t *cl = NULL;
4163 zfs_handle_t *zhrp = NULL;
4164 char *parentname = NULL;
4165 char parent[ZFS_MAX_DATASET_NAME_LEN];
4166 libzfs_handle_t *hdl = zhp->zfs_hdl;
4167 char errbuf[1024];
4169 /* if we have the same exact name, just return success */
4170 if (strcmp(zhp->zfs_name, target) == 0)
4171 return (0);
4173 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4174 "cannot rename to '%s'"), target);
4176 /* make sure source name is valid */
4177 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4178 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4181 * Make sure the target name is valid
4183 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4184 if ((strchr(target, '@') == NULL) ||
4185 *target == '@') {
4187 * Snapshot target name is abbreviated,
4188 * reconstruct full dataset name
4190 (void) strlcpy(parent, zhp->zfs_name,
4191 sizeof (parent));
4192 delim = strchr(parent, '@');
4193 if (strchr(target, '@') == NULL)
4194 *(++delim) = '\0';
4195 else
4196 *delim = '\0';
4197 (void) strlcat(parent, target, sizeof (parent));
4198 target = parent;
4199 } else {
4201 * Make sure we're renaming within the same dataset.
4203 delim = strchr(target, '@');
4204 if (strncmp(zhp->zfs_name, target, delim - target)
4205 != 0 || zhp->zfs_name[delim - target] != '@') {
4206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4207 "snapshots must be part of same "
4208 "dataset"));
4209 return (zfs_error(hdl, EZFS_CROSSTARGET,
4210 errbuf));
4213 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4214 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4215 } else {
4216 if (recursive) {
4217 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4218 "recursive rename must be a snapshot"));
4219 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4222 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4223 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4225 /* validate parents */
4226 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4227 return (-1);
4229 /* make sure we're in the same pool */
4230 verify((delim = strchr(target, '/')) != NULL);
4231 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4232 zhp->zfs_name[delim - target] != '/') {
4233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4234 "datasets must be within same pool"));
4235 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4238 /* new name cannot be a child of the current dataset name */
4239 if (is_descendant(zhp->zfs_name, target)) {
4240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4241 "New dataset name cannot be a descendant of "
4242 "current dataset name"));
4243 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4247 (void) snprintf(errbuf, sizeof (errbuf),
4248 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4250 if (getzoneid() == GLOBAL_ZONEID &&
4251 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4253 "dataset is used in a non-global zone"));
4254 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4257 if (recursive) {
4258 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4259 if (parentname == NULL) {
4260 ret = -1;
4261 goto error;
4263 delim = strchr(parentname, '@');
4264 *delim = '\0';
4265 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4266 if (zhrp == NULL) {
4267 ret = -1;
4268 goto error;
4270 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4271 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4272 force_unmount ? MS_FORCE : 0)) == NULL)
4273 return (-1);
4275 if (changelist_haszonedchild(cl)) {
4276 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4277 "child dataset with inherited mountpoint is used "
4278 "in a non-global zone"));
4279 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4280 ret = -1;
4281 goto error;
4284 if ((ret = changelist_prefix(cl)) != 0)
4285 goto error;
4288 if (ZFS_IS_VOLUME(zhp))
4289 zc.zc_objset_type = DMU_OST_ZVOL;
4290 else
4291 zc.zc_objset_type = DMU_OST_ZFS;
4293 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4294 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4296 zc.zc_cookie = recursive;
4298 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4300 * if it was recursive, the one that actually failed will
4301 * be in zc.zc_name
4303 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4304 "cannot rename '%s'"), zc.zc_name);
4306 if (recursive && errno == EEXIST) {
4307 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4308 "a child dataset already has a snapshot "
4309 "with the new name"));
4310 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4311 } else {
4312 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4316 * On failure, we still want to remount any filesystems that
4317 * were previously mounted, so we don't alter the system state.
4319 if (cl != NULL)
4320 (void) changelist_postfix(cl);
4321 } else {
4322 if (cl != NULL) {
4323 changelist_rename(cl, zfs_get_name(zhp), target);
4324 ret = changelist_postfix(cl);
4328 error:
4329 if (parentname != NULL) {
4330 free(parentname);
4332 if (zhrp != NULL) {
4333 zfs_close(zhrp);
4335 if (cl != NULL) {
4336 changelist_free(cl);
4338 return (ret);
4341 nvlist_t *
4342 zfs_get_user_props(zfs_handle_t *zhp)
4344 return (zhp->zfs_user_props);
4347 nvlist_t *
4348 zfs_get_recvd_props(zfs_handle_t *zhp)
4350 if (zhp->zfs_recvd_props == NULL)
4351 if (get_recvd_props_ioctl(zhp) != 0)
4352 return (NULL);
4353 return (zhp->zfs_recvd_props);
4357 * This function is used by 'zfs list' to determine the exact set of columns to
4358 * display, and their maximum widths. This does two main things:
4360 * - If this is a list of all properties, then expand the list to include
4361 * all native properties, and set a flag so that for each dataset we look
4362 * for new unique user properties and add them to the list.
4364 * - For non fixed-width properties, keep track of the maximum width seen
4365 * so that we can size the column appropriately. If the user has
4366 * requested received property values, we also need to compute the width
4367 * of the RECEIVED column.
4370 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4371 boolean_t literal)
4373 libzfs_handle_t *hdl = zhp->zfs_hdl;
4374 zprop_list_t *entry;
4375 zprop_list_t **last, **start;
4376 nvlist_t *userprops, *propval;
4377 nvpair_t *elem;
4378 char *strval;
4379 char buf[ZFS_MAXPROPLEN];
4381 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4382 return (-1);
4384 userprops = zfs_get_user_props(zhp);
4386 entry = *plp;
4387 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4389 * Go through and add any user properties as necessary. We
4390 * start by incrementing our list pointer to the first
4391 * non-native property.
4393 start = plp;
4394 while (*start != NULL) {
4395 if ((*start)->pl_prop == ZPROP_INVAL)
4396 break;
4397 start = &(*start)->pl_next;
4400 elem = NULL;
4401 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4403 * See if we've already found this property in our list.
4405 for (last = start; *last != NULL;
4406 last = &(*last)->pl_next) {
4407 if (strcmp((*last)->pl_user_prop,
4408 nvpair_name(elem)) == 0)
4409 break;
4412 if (*last == NULL) {
4413 if ((entry = zfs_alloc(hdl,
4414 sizeof (zprop_list_t))) == NULL ||
4415 ((entry->pl_user_prop = zfs_strdup(hdl,
4416 nvpair_name(elem)))) == NULL) {
4417 free(entry);
4418 return (-1);
4421 entry->pl_prop = ZPROP_INVAL;
4422 entry->pl_width = strlen(nvpair_name(elem));
4423 entry->pl_all = B_TRUE;
4424 *last = entry;
4430 * Now go through and check the width of any non-fixed columns
4432 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4433 if (entry->pl_fixed && !literal)
4434 continue;
4436 if (entry->pl_prop != ZPROP_INVAL) {
4437 if (zfs_prop_get(zhp, entry->pl_prop,
4438 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4439 if (strlen(buf) > entry->pl_width)
4440 entry->pl_width = strlen(buf);
4442 if (received && zfs_prop_get_recvd(zhp,
4443 zfs_prop_to_name(entry->pl_prop),
4444 buf, sizeof (buf), literal) == 0)
4445 if (strlen(buf) > entry->pl_recvd_width)
4446 entry->pl_recvd_width = strlen(buf);
4447 } else {
4448 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4449 &propval) == 0) {
4450 verify(nvlist_lookup_string(propval,
4451 ZPROP_VALUE, &strval) == 0);
4452 if (strlen(strval) > entry->pl_width)
4453 entry->pl_width = strlen(strval);
4455 if (received && zfs_prop_get_recvd(zhp,
4456 entry->pl_user_prop,
4457 buf, sizeof (buf), literal) == 0)
4458 if (strlen(buf) > entry->pl_recvd_width)
4459 entry->pl_recvd_width = strlen(buf);
4463 return (0);
4467 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4468 char *resource, void *export, void *sharetab,
4469 int sharemax, zfs_share_op_t operation)
4471 zfs_cmd_t zc = { 0 };
4472 int error;
4474 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4475 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4476 if (resource)
4477 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4478 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4479 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4480 zc.zc_share.z_sharetype = operation;
4481 zc.zc_share.z_sharemax = sharemax;
4482 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4483 return (error);
4486 void
4487 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4489 nvpair_t *curr;
4492 * Keep a reference to the props-table against which we prune the
4493 * properties.
4495 zhp->zfs_props_table = props;
4497 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4499 while (curr) {
4500 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4501 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4504 * User properties will result in ZPROP_INVAL, and since we
4505 * only know how to prune standard ZFS properties, we always
4506 * leave these in the list. This can also happen if we
4507 * encounter an unknown DSL property (when running older
4508 * software, for example).
4510 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4511 (void) nvlist_remove(zhp->zfs_props,
4512 nvpair_name(curr), nvpair_type(curr));
4513 curr = next;
4517 static int
4518 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4519 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4521 zfs_cmd_t zc = { 0 };
4522 nvlist_t *nvlist = NULL;
4523 int error;
4525 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4526 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4527 zc.zc_cookie = (uint64_t)cmd;
4529 if (cmd == ZFS_SMB_ACL_RENAME) {
4530 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4531 (void) no_memory(hdl);
4532 return (0);
4536 switch (cmd) {
4537 case ZFS_SMB_ACL_ADD:
4538 case ZFS_SMB_ACL_REMOVE:
4539 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4540 break;
4541 case ZFS_SMB_ACL_RENAME:
4542 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4543 resource1) != 0) {
4544 (void) no_memory(hdl);
4545 return (-1);
4547 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4548 resource2) != 0) {
4549 (void) no_memory(hdl);
4550 return (-1);
4552 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4553 nvlist_free(nvlist);
4554 return (-1);
4556 break;
4557 case ZFS_SMB_ACL_PURGE:
4558 break;
4559 default:
4560 return (-1);
4562 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4563 nvlist_free(nvlist);
4564 return (error);
4568 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4569 char *path, char *resource)
4571 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4572 resource, NULL));
4576 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4577 char *path, char *resource)
4579 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4580 resource, NULL));
4584 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4586 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4587 NULL, NULL));
4591 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4592 char *oldname, char *newname)
4594 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4595 oldname, newname));
4599 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4600 zfs_userspace_cb_t func, void *arg)
4602 zfs_cmd_t zc = { 0 };
4603 zfs_useracct_t buf[100];
4604 libzfs_handle_t *hdl = zhp->zfs_hdl;
4605 int ret;
4607 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4609 zc.zc_objset_type = type;
4610 zc.zc_nvlist_dst = (uintptr_t)buf;
4612 for (;;) {
4613 zfs_useracct_t *zua = buf;
4615 zc.zc_nvlist_dst_size = sizeof (buf);
4616 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4617 char errbuf[1024];
4619 (void) snprintf(errbuf, sizeof (errbuf),
4620 dgettext(TEXT_DOMAIN,
4621 "cannot get used/quota for %s"), zc.zc_name);
4622 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4624 if (zc.zc_nvlist_dst_size == 0)
4625 break;
4627 while (zc.zc_nvlist_dst_size > 0) {
4628 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4629 zua->zu_space)) != 0)
4630 return (ret);
4631 zua++;
4632 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4636 return (0);
4639 struct holdarg {
4640 nvlist_t *nvl;
4641 const char *snapname;
4642 const char *tag;
4643 boolean_t recursive;
4644 int error;
4647 static int
4648 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4650 struct holdarg *ha = arg;
4651 char name[ZFS_MAX_DATASET_NAME_LEN];
4652 int rv = 0;
4654 (void) snprintf(name, sizeof (name),
4655 "%s@%s", zhp->zfs_name, ha->snapname);
4657 if (lzc_exists(name))
4658 fnvlist_add_string(ha->nvl, name, ha->tag);
4660 if (ha->recursive)
4661 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4662 zfs_close(zhp);
4663 return (rv);
4667 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4668 boolean_t recursive, int cleanup_fd)
4670 int ret;
4671 struct holdarg ha;
4673 ha.nvl = fnvlist_alloc();
4674 ha.snapname = snapname;
4675 ha.tag = tag;
4676 ha.recursive = recursive;
4677 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4679 if (nvlist_empty(ha.nvl)) {
4680 char errbuf[1024];
4682 fnvlist_free(ha.nvl);
4683 ret = ENOENT;
4684 (void) snprintf(errbuf, sizeof (errbuf),
4685 dgettext(TEXT_DOMAIN,
4686 "cannot hold snapshot '%s@%s'"),
4687 zhp->zfs_name, snapname);
4688 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4689 return (ret);
4692 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4693 fnvlist_free(ha.nvl);
4695 return (ret);
4699 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4701 int ret;
4702 nvlist_t *errors;
4703 libzfs_handle_t *hdl = zhp->zfs_hdl;
4704 char errbuf[1024];
4705 nvpair_t *elem;
4707 errors = NULL;
4708 ret = lzc_hold(holds, cleanup_fd, &errors);
4710 if (ret == 0) {
4711 /* There may be errors even in the success case. */
4712 fnvlist_free(errors);
4713 return (0);
4716 if (nvlist_empty(errors)) {
4717 /* no hold-specific errors */
4718 (void) snprintf(errbuf, sizeof (errbuf),
4719 dgettext(TEXT_DOMAIN, "cannot hold"));
4720 switch (ret) {
4721 case ENOTSUP:
4722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4723 "pool must be upgraded"));
4724 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4725 break;
4726 case EINVAL:
4727 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4728 break;
4729 default:
4730 (void) zfs_standard_error(hdl, ret, errbuf);
4734 for (elem = nvlist_next_nvpair(errors, NULL);
4735 elem != NULL;
4736 elem = nvlist_next_nvpair(errors, elem)) {
4737 (void) snprintf(errbuf, sizeof (errbuf),
4738 dgettext(TEXT_DOMAIN,
4739 "cannot hold snapshot '%s'"), nvpair_name(elem));
4740 switch (fnvpair_value_int32(elem)) {
4741 case E2BIG:
4743 * Temporary tags wind up having the ds object id
4744 * prepended. So even if we passed the length check
4745 * above, it's still possible for the tag to wind
4746 * up being slightly too long.
4748 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4749 break;
4750 case EINVAL:
4751 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4752 break;
4753 case EEXIST:
4754 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4755 break;
4756 default:
4757 (void) zfs_standard_error(hdl,
4758 fnvpair_value_int32(elem), errbuf);
4762 fnvlist_free(errors);
4763 return (ret);
4766 static int
4767 zfs_release_one(zfs_handle_t *zhp, void *arg)
4769 struct holdarg *ha = arg;
4770 char name[ZFS_MAX_DATASET_NAME_LEN];
4771 int rv = 0;
4772 nvlist_t *existing_holds;
4774 (void) snprintf(name, sizeof (name),
4775 "%s@%s", zhp->zfs_name, ha->snapname);
4777 if (lzc_get_holds(name, &existing_holds) != 0) {
4778 ha->error = ENOENT;
4779 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4780 ha->error = ESRCH;
4781 } else {
4782 nvlist_t *torelease = fnvlist_alloc();
4783 fnvlist_add_boolean(torelease, ha->tag);
4784 fnvlist_add_nvlist(ha->nvl, name, torelease);
4785 fnvlist_free(torelease);
4788 if (ha->recursive)
4789 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4790 zfs_close(zhp);
4791 return (rv);
4795 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4796 boolean_t recursive)
4798 int ret;
4799 struct holdarg ha;
4800 nvlist_t *errors = NULL;
4801 nvpair_t *elem;
4802 libzfs_handle_t *hdl = zhp->zfs_hdl;
4803 char errbuf[1024];
4805 ha.nvl = fnvlist_alloc();
4806 ha.snapname = snapname;
4807 ha.tag = tag;
4808 ha.recursive = recursive;
4809 ha.error = 0;
4810 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4812 if (nvlist_empty(ha.nvl)) {
4813 fnvlist_free(ha.nvl);
4814 ret = ha.error;
4815 (void) snprintf(errbuf, sizeof (errbuf),
4816 dgettext(TEXT_DOMAIN,
4817 "cannot release hold from snapshot '%s@%s'"),
4818 zhp->zfs_name, snapname);
4819 if (ret == ESRCH) {
4820 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4821 } else {
4822 (void) zfs_standard_error(hdl, ret, errbuf);
4824 return (ret);
4827 ret = lzc_release(ha.nvl, &errors);
4828 fnvlist_free(ha.nvl);
4830 if (ret == 0) {
4831 /* There may be errors even in the success case. */
4832 fnvlist_free(errors);
4833 return (0);
4836 if (nvlist_empty(errors)) {
4837 /* no hold-specific errors */
4838 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4839 "cannot release"));
4840 switch (errno) {
4841 case ENOTSUP:
4842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4843 "pool must be upgraded"));
4844 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4845 break;
4846 default:
4847 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4851 for (elem = nvlist_next_nvpair(errors, NULL);
4852 elem != NULL;
4853 elem = nvlist_next_nvpair(errors, elem)) {
4854 (void) snprintf(errbuf, sizeof (errbuf),
4855 dgettext(TEXT_DOMAIN,
4856 "cannot release hold from snapshot '%s'"),
4857 nvpair_name(elem));
4858 switch (fnvpair_value_int32(elem)) {
4859 case ESRCH:
4860 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4861 break;
4862 case EINVAL:
4863 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4864 break;
4865 default:
4866 (void) zfs_standard_error_fmt(hdl,
4867 fnvpair_value_int32(elem), errbuf);
4871 fnvlist_free(errors);
4872 return (ret);
4876 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4878 zfs_cmd_t zc = { 0 };
4879 libzfs_handle_t *hdl = zhp->zfs_hdl;
4880 int nvsz = 2048;
4881 void *nvbuf;
4882 int err = 0;
4883 char errbuf[1024];
4885 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4886 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4888 tryagain:
4890 nvbuf = malloc(nvsz);
4891 if (nvbuf == NULL) {
4892 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4893 goto out;
4896 zc.zc_nvlist_dst_size = nvsz;
4897 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4899 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4901 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4902 (void) snprintf(errbuf, sizeof (errbuf),
4903 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4904 zc.zc_name);
4905 switch (errno) {
4906 case ENOMEM:
4907 free(nvbuf);
4908 nvsz = zc.zc_nvlist_dst_size;
4909 goto tryagain;
4911 case ENOTSUP:
4912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4913 "pool must be upgraded"));
4914 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4915 break;
4916 case EINVAL:
4917 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4918 break;
4919 case ENOENT:
4920 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4921 break;
4922 default:
4923 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4924 break;
4926 } else {
4927 /* success */
4928 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4929 if (rc) {
4930 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4931 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4932 zc.zc_name);
4933 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4937 free(nvbuf);
4938 out:
4939 return (err);
4943 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4945 zfs_cmd_t zc = { 0 };
4946 libzfs_handle_t *hdl = zhp->zfs_hdl;
4947 char *nvbuf;
4948 char errbuf[1024];
4949 size_t nvsz;
4950 int err;
4952 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4953 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4955 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4956 assert(err == 0);
4958 nvbuf = malloc(nvsz);
4960 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4961 assert(err == 0);
4963 zc.zc_nvlist_src_size = nvsz;
4964 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4965 zc.zc_perm_action = un;
4967 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4969 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4970 (void) snprintf(errbuf, sizeof (errbuf),
4971 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4972 zc.zc_name);
4973 switch (errno) {
4974 case ENOTSUP:
4975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4976 "pool must be upgraded"));
4977 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4978 break;
4979 case EINVAL:
4980 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4981 break;
4982 case ENOENT:
4983 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4984 break;
4985 default:
4986 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4987 break;
4991 free(nvbuf);
4993 return (err);
4997 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4999 int err;
5000 char errbuf[1024];
5002 err = lzc_get_holds(zhp->zfs_name, nvl);
5004 if (err != 0) {
5005 libzfs_handle_t *hdl = zhp->zfs_hdl;
5007 (void) snprintf(errbuf, sizeof (errbuf),
5008 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5009 zhp->zfs_name);
5010 switch (err) {
5011 case ENOTSUP:
5012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5013 "pool must be upgraded"));
5014 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5015 break;
5016 case EINVAL:
5017 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5018 break;
5019 case ENOENT:
5020 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5021 break;
5022 default:
5023 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5024 break;
5028 return (err);
5032 * Convert the zvol's volume size to an appropriate reservation.
5033 * Note: If this routine is updated, it is necessary to update the ZFS test
5034 * suite's shell version in reservation.kshlib.
5036 uint64_t
5037 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5039 uint64_t numdb;
5040 uint64_t nblocks, volblocksize;
5041 int ncopies;
5042 char *strval;
5044 if (nvlist_lookup_string(props,
5045 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5046 ncopies = atoi(strval);
5047 else
5048 ncopies = 1;
5049 if (nvlist_lookup_uint64(props,
5050 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5051 &volblocksize) != 0)
5052 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5053 nblocks = volsize/volblocksize;
5054 /* start with metadnode L0-L6 */
5055 numdb = 7;
5056 /* calculate number of indirects */
5057 while (nblocks > 1) {
5058 nblocks += DNODES_PER_LEVEL - 1;
5059 nblocks /= DNODES_PER_LEVEL;
5060 numdb += nblocks;
5062 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5063 volsize *= ncopies;
5065 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5066 * compressed, but in practice they compress down to about
5067 * 1100 bytes
5069 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5070 volsize += numdb;
5071 return (volsize);