Fedora 28: Fix misc bounds check compiler warnings
[zfs.git] / lib / libzfs / libzfs_dataset.c
blob8ec5b7af4b4a771d50af3e0f2479c4a2bfdd5d53
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) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright 2017 Nexenta Systems, Inc.
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 * Copyright 2017 RackTop Systems.
35 #include <ctype.h>
36 #include <errno.h>
37 #include <libintl.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <stddef.h>
44 #include <zone.h>
45 #include <fcntl.h>
46 #include <sys/mntent.h>
47 #include <sys/mount.h>
48 #include <pwd.h>
49 #include <grp.h>
50 #include <stddef.h>
51 #include <ucred.h>
52 #ifdef HAVE_IDMAP
53 #include <idmap.h>
54 #include <aclutils.h>
55 #include <directory.h>
56 #endif /* HAVE_IDMAP */
58 #include <sys/dnode.h>
59 #include <sys/spa.h>
60 #include <sys/zap.h>
61 #include <sys/dsl_crypt.h>
62 #include <libzfs.h>
64 #include "zfs_namecheck.h"
65 #include "zfs_prop.h"
66 #include "libzfs_impl.h"
67 #include "libzfs.h"
68 #include "zfs_deleg.h"
70 static int userquota_propname_decode(const char *propname, boolean_t zoned,
71 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
74 * Given a single type (not a mask of types), return the type in a human
75 * readable form.
77 const char *
78 zfs_type_to_name(zfs_type_t type)
80 switch (type) {
81 case ZFS_TYPE_FILESYSTEM:
82 return (dgettext(TEXT_DOMAIN, "filesystem"));
83 case ZFS_TYPE_SNAPSHOT:
84 return (dgettext(TEXT_DOMAIN, "snapshot"));
85 case ZFS_TYPE_VOLUME:
86 return (dgettext(TEXT_DOMAIN, "volume"));
87 case ZFS_TYPE_POOL:
88 return (dgettext(TEXT_DOMAIN, "pool"));
89 case ZFS_TYPE_BOOKMARK:
90 return (dgettext(TEXT_DOMAIN, "bookmark"));
91 default:
92 assert(!"unhandled zfs_type_t");
95 return (NULL);
99 * Validate a ZFS path. This is used even before trying to open the dataset, to
100 * provide a more meaningful error message. We call zfs_error_aux() to
101 * explain exactly why the name was not valid.
104 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
105 boolean_t modifying)
107 namecheck_err_t why;
108 char what;
110 if (entity_namecheck(path, &why, &what) != 0) {
111 if (hdl != NULL) {
112 switch (why) {
113 case NAME_ERR_TOOLONG:
114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
115 "name is too long"));
116 break;
118 case NAME_ERR_LEADING_SLASH:
119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
120 "leading slash in name"));
121 break;
123 case NAME_ERR_EMPTY_COMPONENT:
124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
125 "empty component in name"));
126 break;
128 case NAME_ERR_TRAILING_SLASH:
129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
130 "trailing slash in name"));
131 break;
133 case NAME_ERR_INVALCHAR:
134 zfs_error_aux(hdl,
135 dgettext(TEXT_DOMAIN, "invalid character "
136 "'%c' in name"), what);
137 break;
139 case NAME_ERR_MULTIPLE_DELIMITERS:
140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141 "multiple '@' and/or '#' delimiters in "
142 "name"));
143 break;
145 case NAME_ERR_NOLETTER:
146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
147 "pool doesn't begin with a letter"));
148 break;
150 case NAME_ERR_RESERVED:
151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
152 "name is reserved"));
153 break;
155 case NAME_ERR_DISKLIKE:
156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
157 "reserved disk name"));
158 break;
160 default:
161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
162 "(%d) not defined"), why);
163 break;
167 return (0);
170 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
171 if (hdl != NULL)
172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
173 "snapshot delimiter '@' is not expected here"));
174 return (0);
177 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
178 if (hdl != NULL)
179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
180 "missing '@' delimiter in snapshot name"));
181 return (0);
184 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
185 if (hdl != NULL)
186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
187 "bookmark delimiter '#' is not expected here"));
188 return (0);
191 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
192 if (hdl != NULL)
193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
194 "missing '#' delimiter in bookmark name"));
195 return (0);
198 if (modifying && strchr(path, '%') != NULL) {
199 if (hdl != NULL)
200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
201 "invalid character %c in name"), '%');
202 return (0);
205 return (-1);
209 zfs_name_valid(const char *name, zfs_type_t type)
211 if (type == ZFS_TYPE_POOL)
212 return (zpool_name_valid(NULL, B_FALSE, name));
213 return (zfs_validate_name(NULL, name, type, B_FALSE));
217 * This function takes the raw DSL properties, and filters out the user-defined
218 * properties into a separate nvlist.
220 static nvlist_t *
221 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
223 libzfs_handle_t *hdl = zhp->zfs_hdl;
224 nvpair_t *elem;
225 nvlist_t *propval;
226 nvlist_t *nvl;
228 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
229 (void) no_memory(hdl);
230 return (NULL);
233 elem = NULL;
234 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
235 if (!zfs_prop_user(nvpair_name(elem)))
236 continue;
238 verify(nvpair_value_nvlist(elem, &propval) == 0);
239 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
240 nvlist_free(nvl);
241 (void) no_memory(hdl);
242 return (NULL);
246 return (nvl);
249 static zpool_handle_t *
250 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
252 libzfs_handle_t *hdl = zhp->zfs_hdl;
253 zpool_handle_t *zph;
255 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
256 if (hdl->libzfs_pool_handles != NULL)
257 zph->zpool_next = hdl->libzfs_pool_handles;
258 hdl->libzfs_pool_handles = zph;
260 return (zph);
263 static zpool_handle_t *
264 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
266 libzfs_handle_t *hdl = zhp->zfs_hdl;
267 zpool_handle_t *zph = hdl->libzfs_pool_handles;
269 while ((zph != NULL) &&
270 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
271 zph = zph->zpool_next;
272 return (zph);
276 * Returns a handle to the pool that contains the provided dataset.
277 * If a handle to that pool already exists then that handle is returned.
278 * Otherwise, a new handle is created and added to the list of handles.
280 static zpool_handle_t *
281 zpool_handle(zfs_handle_t *zhp)
283 char *pool_name;
284 int len;
285 zpool_handle_t *zph;
287 len = strcspn(zhp->zfs_name, "/@#") + 1;
288 pool_name = zfs_alloc(zhp->zfs_hdl, len);
289 (void) strlcpy(pool_name, zhp->zfs_name, len);
291 zph = zpool_find_handle(zhp, pool_name, len);
292 if (zph == NULL)
293 zph = zpool_add_handle(zhp, pool_name);
295 free(pool_name);
296 return (zph);
299 void
300 zpool_free_handles(libzfs_handle_t *hdl)
302 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
304 while (zph != NULL) {
305 next = zph->zpool_next;
306 zpool_close(zph);
307 zph = next;
309 hdl->libzfs_pool_handles = NULL;
313 * Utility function to gather stats (objset and zpl) for the given object.
315 static int
316 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
318 libzfs_handle_t *hdl = zhp->zfs_hdl;
320 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
322 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
323 if (errno == ENOMEM) {
324 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
325 return (-1);
327 } else {
328 return (-1);
331 return (0);
335 * Utility function to get the received properties of the given object.
337 static int
338 get_recvd_props_ioctl(zfs_handle_t *zhp)
340 libzfs_handle_t *hdl = zhp->zfs_hdl;
341 nvlist_t *recvdprops;
342 zfs_cmd_t zc = {"\0"};
343 int err;
345 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
346 return (-1);
348 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
350 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
351 if (errno == ENOMEM) {
352 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
353 return (-1);
355 } else {
356 zcmd_free_nvlists(&zc);
357 return (-1);
361 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
362 zcmd_free_nvlists(&zc);
363 if (err != 0)
364 return (-1);
366 nvlist_free(zhp->zfs_recvd_props);
367 zhp->zfs_recvd_props = recvdprops;
369 return (0);
372 static int
373 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
375 nvlist_t *allprops, *userprops;
377 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
379 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
380 return (-1);
384 * XXX Why do we store the user props separately, in addition to
385 * storing them in zfs_props?
387 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
388 nvlist_free(allprops);
389 return (-1);
392 nvlist_free(zhp->zfs_props);
393 nvlist_free(zhp->zfs_user_props);
395 zhp->zfs_props = allprops;
396 zhp->zfs_user_props = userprops;
398 return (0);
401 static int
402 get_stats(zfs_handle_t *zhp)
404 int rc = 0;
405 zfs_cmd_t zc = {"\0"};
407 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
408 return (-1);
409 if (get_stats_ioctl(zhp, &zc) != 0)
410 rc = -1;
411 else if (put_stats_zhdl(zhp, &zc) != 0)
412 rc = -1;
413 zcmd_free_nvlists(&zc);
414 return (rc);
418 * Refresh the properties currently stored in the handle.
420 void
421 zfs_refresh_properties(zfs_handle_t *zhp)
423 (void) get_stats(zhp);
427 * Makes a handle from the given dataset name. Used by zfs_open() and
428 * zfs_iter_* to create child handles on the fly.
430 static int
431 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
433 if (put_stats_zhdl(zhp, zc) != 0)
434 return (-1);
437 * We've managed to open the dataset and gather statistics. Determine
438 * the high-level type.
440 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
441 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
442 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
443 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
444 else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
445 return (-1);
446 else
447 abort();
449 if (zhp->zfs_dmustats.dds_is_snapshot)
450 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
451 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
452 zhp->zfs_type = ZFS_TYPE_VOLUME;
453 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
454 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
455 else
456 abort(); /* we should never see any other types */
458 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
459 return (-1);
461 return (0);
464 zfs_handle_t *
465 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
467 zfs_cmd_t zc = {"\0"};
469 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
471 if (zhp == NULL)
472 return (NULL);
474 zhp->zfs_hdl = hdl;
475 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
476 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
477 free(zhp);
478 return (NULL);
480 if (get_stats_ioctl(zhp, &zc) == -1) {
481 zcmd_free_nvlists(&zc);
482 free(zhp);
483 return (NULL);
485 if (make_dataset_handle_common(zhp, &zc) == -1) {
486 free(zhp);
487 zhp = NULL;
489 zcmd_free_nvlists(&zc);
490 return (zhp);
493 zfs_handle_t *
494 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
496 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
498 if (zhp == NULL)
499 return (NULL);
501 zhp->zfs_hdl = hdl;
502 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
503 if (make_dataset_handle_common(zhp, zc) == -1) {
504 free(zhp);
505 return (NULL);
507 return (zhp);
510 zfs_handle_t *
511 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
513 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
515 if (zhp == NULL)
516 return (NULL);
518 zhp->zfs_hdl = pzhp->zfs_hdl;
519 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
520 zhp->zfs_head_type = pzhp->zfs_type;
521 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
522 zhp->zpool_hdl = zpool_handle(zhp);
524 return (zhp);
527 zfs_handle_t *
528 zfs_handle_dup(zfs_handle_t *zhp_orig)
530 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
532 if (zhp == NULL)
533 return (NULL);
535 zhp->zfs_hdl = zhp_orig->zfs_hdl;
536 zhp->zpool_hdl = zhp_orig->zpool_hdl;
537 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
538 sizeof (zhp->zfs_name));
539 zhp->zfs_type = zhp_orig->zfs_type;
540 zhp->zfs_head_type = zhp_orig->zfs_head_type;
541 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
542 if (zhp_orig->zfs_props != NULL) {
543 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
544 (void) no_memory(zhp->zfs_hdl);
545 zfs_close(zhp);
546 return (NULL);
549 if (zhp_orig->zfs_user_props != NULL) {
550 if (nvlist_dup(zhp_orig->zfs_user_props,
551 &zhp->zfs_user_props, 0) != 0) {
552 (void) no_memory(zhp->zfs_hdl);
553 zfs_close(zhp);
554 return (NULL);
557 if (zhp_orig->zfs_recvd_props != NULL) {
558 if (nvlist_dup(zhp_orig->zfs_recvd_props,
559 &zhp->zfs_recvd_props, 0)) {
560 (void) no_memory(zhp->zfs_hdl);
561 zfs_close(zhp);
562 return (NULL);
565 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
566 if (zhp_orig->zfs_mntopts != NULL) {
567 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
568 zhp_orig->zfs_mntopts);
570 zhp->zfs_props_table = zhp_orig->zfs_props_table;
571 return (zhp);
574 boolean_t
575 zfs_bookmark_exists(const char *path)
577 nvlist_t *bmarks;
578 nvlist_t *props;
579 char fsname[ZFS_MAX_DATASET_NAME_LEN];
580 char *bmark_name;
581 char *pound;
582 int err;
583 boolean_t rv;
586 (void) strlcpy(fsname, path, sizeof (fsname));
587 pound = strchr(fsname, '#');
588 if (pound == NULL)
589 return (B_FALSE);
591 *pound = '\0';
592 bmark_name = pound + 1;
593 props = fnvlist_alloc();
594 err = lzc_get_bookmarks(fsname, props, &bmarks);
595 nvlist_free(props);
596 if (err != 0) {
597 nvlist_free(bmarks);
598 return (B_FALSE);
601 rv = nvlist_exists(bmarks, bmark_name);
602 nvlist_free(bmarks);
603 return (rv);
606 zfs_handle_t *
607 make_bookmark_handle(zfs_handle_t *parent, const char *path,
608 nvlist_t *bmark_props)
610 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
612 if (zhp == NULL)
613 return (NULL);
615 /* Fill in the name. */
616 zhp->zfs_hdl = parent->zfs_hdl;
617 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
619 /* Set the property lists. */
620 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
621 free(zhp);
622 return (NULL);
625 /* Set the types. */
626 zhp->zfs_head_type = parent->zfs_head_type;
627 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
629 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
630 nvlist_free(zhp->zfs_props);
631 free(zhp);
632 return (NULL);
635 return (zhp);
638 struct zfs_open_bookmarks_cb_data {
639 const char *path;
640 zfs_handle_t *zhp;
643 static int
644 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
646 struct zfs_open_bookmarks_cb_data *dp = data;
649 * Is it the one we are looking for?
651 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
653 * We found it. Save it and let the caller know we are done.
655 dp->zhp = zhp;
656 return (EEXIST);
660 * Not found. Close the handle and ask for another one.
662 zfs_close(zhp);
663 return (0);
667 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
668 * argument is a mask of acceptable types. The function will print an
669 * appropriate error message and return NULL if it can't be opened.
671 zfs_handle_t *
672 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
674 zfs_handle_t *zhp;
675 char errbuf[1024];
676 char *bookp;
678 (void) snprintf(errbuf, sizeof (errbuf),
679 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
682 * Validate the name before we even try to open it.
684 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
685 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
686 return (NULL);
690 * Bookmarks needs to be handled separately.
692 bookp = strchr(path, '#');
693 if (bookp == NULL) {
695 * Try to get stats for the dataset, which will tell us if it
696 * exists.
698 errno = 0;
699 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
700 (void) zfs_standard_error(hdl, errno, errbuf);
701 return (NULL);
703 } else {
704 char dsname[ZFS_MAX_DATASET_NAME_LEN];
705 zfs_handle_t *pzhp;
706 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
709 * We need to cut out '#' and everything after '#'
710 * to get the parent dataset name only.
712 assert(bookp - path < sizeof (dsname));
713 (void) strncpy(dsname, path, bookp - path);
714 dsname[bookp - path] = '\0';
717 * Create handle for the parent dataset.
719 errno = 0;
720 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
721 (void) zfs_standard_error(hdl, errno, errbuf);
722 return (NULL);
726 * Iterate bookmarks to find the right one.
728 errno = 0;
729 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
730 &cb_data) == 0) && (cb_data.zhp == NULL)) {
731 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
732 zfs_close(pzhp);
733 return (NULL);
735 if (cb_data.zhp == NULL) {
736 (void) zfs_standard_error(hdl, errno, errbuf);
737 zfs_close(pzhp);
738 return (NULL);
740 zhp = cb_data.zhp;
743 * Cleanup.
745 zfs_close(pzhp);
748 if (!(types & zhp->zfs_type)) {
749 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
750 zfs_close(zhp);
751 return (NULL);
754 return (zhp);
758 * Release a ZFS handle. Nothing to do but free the associated memory.
760 void
761 zfs_close(zfs_handle_t *zhp)
763 if (zhp->zfs_mntopts)
764 free(zhp->zfs_mntopts);
765 nvlist_free(zhp->zfs_props);
766 nvlist_free(zhp->zfs_user_props);
767 nvlist_free(zhp->zfs_recvd_props);
768 free(zhp);
771 typedef struct mnttab_node {
772 struct mnttab mtn_mt;
773 avl_node_t mtn_node;
774 } mnttab_node_t;
776 static int
777 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
779 const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
780 const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
781 int rv;
783 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
785 return (AVL_ISIGN(rv));
788 void
789 libzfs_mnttab_init(libzfs_handle_t *hdl)
791 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
792 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
793 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
797 libzfs_mnttab_update(libzfs_handle_t *hdl)
799 struct mnttab entry;
801 /* Reopen MNTTAB to prevent reading stale data from open file */
802 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
803 return (ENOENT);
805 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
806 mnttab_node_t *mtn;
807 avl_index_t where;
809 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
810 continue;
812 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
813 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
814 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
815 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
816 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
818 /* Exclude duplicate mounts */
819 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
820 free(mtn->mtn_mt.mnt_special);
821 free(mtn->mtn_mt.mnt_mountp);
822 free(mtn->mtn_mt.mnt_fstype);
823 free(mtn->mtn_mt.mnt_mntopts);
824 free(mtn);
825 continue;
828 avl_add(&hdl->libzfs_mnttab_cache, mtn);
831 return (0);
834 void
835 libzfs_mnttab_fini(libzfs_handle_t *hdl)
837 void *cookie = NULL;
838 mnttab_node_t *mtn;
840 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
841 != NULL) {
842 free(mtn->mtn_mt.mnt_special);
843 free(mtn->mtn_mt.mnt_mountp);
844 free(mtn->mtn_mt.mnt_fstype);
845 free(mtn->mtn_mt.mnt_mntopts);
846 free(mtn);
848 avl_destroy(&hdl->libzfs_mnttab_cache);
851 void
852 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
854 hdl->libzfs_mnttab_enable = enable;
858 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
859 struct mnttab *entry)
861 mnttab_node_t find;
862 mnttab_node_t *mtn;
863 int error;
865 if (!hdl->libzfs_mnttab_enable) {
866 struct mnttab srch = { 0 };
868 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
869 libzfs_mnttab_fini(hdl);
871 /* Reopen MNTTAB to prevent reading stale data from open file */
872 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
873 return (ENOENT);
875 srch.mnt_special = (char *)fsname;
876 srch.mnt_fstype = MNTTYPE_ZFS;
877 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
878 return (0);
879 else
880 return (ENOENT);
883 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
884 if ((error = libzfs_mnttab_update(hdl)) != 0)
885 return (error);
887 find.mtn_mt.mnt_special = (char *)fsname;
888 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
889 if (mtn) {
890 *entry = mtn->mtn_mt;
891 return (0);
893 return (ENOENT);
896 void
897 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
898 const char *mountp, const char *mntopts)
900 mnttab_node_t *mtn;
902 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
903 return;
904 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
905 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
906 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
907 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
908 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
909 avl_add(&hdl->libzfs_mnttab_cache, mtn);
912 void
913 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
915 mnttab_node_t find;
916 mnttab_node_t *ret;
918 find.mtn_mt.mnt_special = (char *)fsname;
919 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
920 != NULL) {
921 avl_remove(&hdl->libzfs_mnttab_cache, ret);
922 free(ret->mtn_mt.mnt_special);
923 free(ret->mtn_mt.mnt_mountp);
924 free(ret->mtn_mt.mnt_fstype);
925 free(ret->mtn_mt.mnt_mntopts);
926 free(ret);
931 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
933 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
935 if (zpool_handle == NULL)
936 return (-1);
938 *spa_version = zpool_get_prop_int(zpool_handle,
939 ZPOOL_PROP_VERSION, NULL);
940 return (0);
944 * The choice of reservation property depends on the SPA version.
946 static int
947 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
949 int spa_version;
951 if (zfs_spa_version(zhp, &spa_version) < 0)
952 return (-1);
954 if (spa_version >= SPA_VERSION_REFRESERVATION)
955 *resv_prop = ZFS_PROP_REFRESERVATION;
956 else
957 *resv_prop = ZFS_PROP_RESERVATION;
959 return (0);
963 * Given an nvlist of properties to set, validates that they are correct, and
964 * parses any numeric properties (index, boolean, etc) if they are specified as
965 * strings.
967 nvlist_t *
968 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
969 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
970 boolean_t key_params_ok, const char *errbuf)
972 nvpair_t *elem;
973 uint64_t intval;
974 char *strval;
975 zfs_prop_t prop;
976 nvlist_t *ret;
977 int chosen_normal = -1;
978 int chosen_utf = -1;
980 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
981 (void) no_memory(hdl);
982 return (NULL);
986 * Make sure this property is valid and applies to this type.
989 elem = NULL;
990 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
991 const char *propname = nvpair_name(elem);
993 prop = zfs_name_to_prop(propname);
994 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
996 * This is a user property: make sure it's a
997 * string, and that it's less than ZAP_MAXNAMELEN.
999 if (nvpair_type(elem) != DATA_TYPE_STRING) {
1000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001 "'%s' must be a string"), propname);
1002 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1003 goto error;
1006 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
1007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1008 "property name '%s' is too long"),
1009 propname);
1010 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1011 goto error;
1014 (void) nvpair_value_string(elem, &strval);
1015 if (nvlist_add_string(ret, propname, strval) != 0) {
1016 (void) no_memory(hdl);
1017 goto error;
1019 continue;
1023 * Currently, only user properties can be modified on
1024 * snapshots.
1026 if (type == ZFS_TYPE_SNAPSHOT) {
1027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1028 "this property can not be modified for snapshots"));
1029 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1030 goto error;
1033 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1034 zfs_userquota_prop_t uqtype;
1035 char *newpropname = NULL;
1036 char domain[128];
1037 uint64_t rid;
1038 uint64_t valary[3];
1039 int rc;
1041 if (userquota_propname_decode(propname, zoned,
1042 &uqtype, domain, sizeof (domain), &rid) != 0) {
1043 zfs_error_aux(hdl,
1044 dgettext(TEXT_DOMAIN,
1045 "'%s' has an invalid user/group name"),
1046 propname);
1047 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1048 goto error;
1051 if (uqtype != ZFS_PROP_USERQUOTA &&
1052 uqtype != ZFS_PROP_GROUPQUOTA &&
1053 uqtype != ZFS_PROP_USEROBJQUOTA &&
1054 uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1055 uqtype != ZFS_PROP_PROJECTQUOTA &&
1056 uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
1057 zfs_error_aux(hdl,
1058 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1059 propname);
1060 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1061 errbuf);
1062 goto error;
1065 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1066 (void) nvpair_value_string(elem, &strval);
1067 if (strcmp(strval, "none") == 0) {
1068 intval = 0;
1069 } else if (zfs_nicestrtonum(hdl,
1070 strval, &intval) != 0) {
1071 (void) zfs_error(hdl,
1072 EZFS_BADPROP, errbuf);
1073 goto error;
1075 } else if (nvpair_type(elem) ==
1076 DATA_TYPE_UINT64) {
1077 (void) nvpair_value_uint64(elem, &intval);
1078 if (intval == 0) {
1079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1080 "use 'none' to disable "
1081 "{user|group|project}quota"));
1082 goto error;
1084 } else {
1085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1086 "'%s' must be a number"), propname);
1087 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1088 goto error;
1092 * Encode the prop name as
1093 * userquota@<hex-rid>-domain, to make it easy
1094 * for the kernel to decode.
1096 rc = asprintf(&newpropname, "%s%llx-%s",
1097 zfs_userquota_prop_prefixes[uqtype],
1098 (longlong_t)rid, domain);
1099 if (rc == -1 || newpropname == NULL) {
1100 (void) no_memory(hdl);
1101 goto error;
1104 valary[0] = uqtype;
1105 valary[1] = rid;
1106 valary[2] = intval;
1107 if (nvlist_add_uint64_array(ret, newpropname,
1108 valary, 3) != 0) {
1109 free(newpropname);
1110 (void) no_memory(hdl);
1111 goto error;
1113 free(newpropname);
1114 continue;
1115 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1117 "'%s' is readonly"),
1118 propname);
1119 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1120 goto error;
1123 if (prop == ZPROP_INVAL) {
1124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1125 "invalid property '%s'"), propname);
1126 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1127 goto error;
1130 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1131 zfs_error_aux(hdl,
1132 dgettext(TEXT_DOMAIN, "'%s' does not "
1133 "apply to datasets of this type"), propname);
1134 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1135 goto error;
1138 if (zfs_prop_readonly(prop) &&
1139 !(zfs_prop_setonce(prop) && zhp == NULL) &&
1140 !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1141 zfs_error_aux(hdl,
1142 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1143 propname);
1144 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1145 goto error;
1148 if (zprop_parse_value(hdl, elem, prop, type, ret,
1149 &strval, &intval, errbuf) != 0)
1150 goto error;
1153 * Perform some additional checks for specific properties.
1155 switch (prop) {
1156 case ZFS_PROP_VERSION:
1158 int version;
1160 if (zhp == NULL)
1161 break;
1162 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1163 if (intval < version) {
1164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1165 "Can not downgrade; already at version %u"),
1166 version);
1167 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1168 goto error;
1170 break;
1173 case ZFS_PROP_VOLBLOCKSIZE:
1174 case ZFS_PROP_RECORDSIZE:
1176 int maxbs = SPA_MAXBLOCKSIZE;
1177 char buf[64];
1179 if (zpool_hdl != NULL) {
1180 maxbs = zpool_get_prop_int(zpool_hdl,
1181 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1184 * The value must be a power of two between
1185 * SPA_MINBLOCKSIZE and maxbs.
1187 if (intval < SPA_MINBLOCKSIZE ||
1188 intval > maxbs || !ISP2(intval)) {
1189 zfs_nicebytes(maxbs, buf, sizeof (buf));
1190 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1191 "'%s' must be power of 2 from 512B "
1192 "to %s"), propname, buf);
1193 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1194 goto error;
1196 break;
1198 case ZFS_PROP_MLSLABEL:
1200 #ifdef HAVE_MLSLABEL
1202 * Verify the mlslabel string and convert to
1203 * internal hex label string.
1206 m_label_t *new_sl;
1207 char *hex = NULL; /* internal label string */
1209 /* Default value is already OK. */
1210 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1211 break;
1213 /* Verify the label can be converted to binary form */
1214 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1215 (str_to_label(strval, &new_sl, MAC_LABEL,
1216 L_NO_CORRECTION, NULL) == -1)) {
1217 goto badlabel;
1220 /* Now translate to hex internal label string */
1221 if (label_to_str(new_sl, &hex, M_INTERNAL,
1222 DEF_NAMES) != 0) {
1223 if (hex)
1224 free(hex);
1225 goto badlabel;
1227 m_label_free(new_sl);
1229 /* If string is already in internal form, we're done. */
1230 if (strcmp(strval, hex) == 0) {
1231 free(hex);
1232 break;
1235 /* Replace the label string with the internal form. */
1236 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1237 DATA_TYPE_STRING);
1238 verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1239 hex) == 0);
1240 free(hex);
1242 break;
1244 badlabel:
1245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1246 "invalid mlslabel '%s'"), strval);
1247 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1248 m_label_free(new_sl); /* OK if null */
1249 goto error;
1250 #else
1251 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1252 "mlslabels are unsupported"));
1253 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1254 goto error;
1255 #endif /* HAVE_MLSLABEL */
1258 case ZFS_PROP_MOUNTPOINT:
1260 namecheck_err_t why;
1262 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1263 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1264 break;
1266 if (mountpoint_namecheck(strval, &why)) {
1267 switch (why) {
1268 case NAME_ERR_LEADING_SLASH:
1269 zfs_error_aux(hdl,
1270 dgettext(TEXT_DOMAIN,
1271 "'%s' must be an absolute path, "
1272 "'none', or 'legacy'"), propname);
1273 break;
1274 case NAME_ERR_TOOLONG:
1275 zfs_error_aux(hdl,
1276 dgettext(TEXT_DOMAIN,
1277 "component of '%s' is too long"),
1278 propname);
1279 break;
1281 default:
1282 zfs_error_aux(hdl,
1283 dgettext(TEXT_DOMAIN,
1284 "(%d) not defined"),
1285 why);
1286 break;
1288 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1289 goto error;
1293 /*FALLTHRU*/
1295 case ZFS_PROP_SHARESMB:
1296 case ZFS_PROP_SHARENFS:
1298 * For the mountpoint and sharenfs or sharesmb
1299 * properties, check if it can be set in a
1300 * global/non-global zone based on
1301 * the zoned property value:
1303 * global zone non-global zone
1304 * --------------------------------------------------
1305 * zoned=on mountpoint (no) mountpoint (yes)
1306 * sharenfs (no) sharenfs (no)
1307 * sharesmb (no) sharesmb (no)
1309 * zoned=off mountpoint (yes) N/A
1310 * sharenfs (yes)
1311 * sharesmb (yes)
1313 if (zoned) {
1314 if (getzoneid() == GLOBAL_ZONEID) {
1315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1316 "'%s' cannot be set on "
1317 "dataset in a non-global zone"),
1318 propname);
1319 (void) zfs_error(hdl, EZFS_ZONED,
1320 errbuf);
1321 goto error;
1322 } else if (prop == ZFS_PROP_SHARENFS ||
1323 prop == ZFS_PROP_SHARESMB) {
1324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1325 "'%s' cannot be set in "
1326 "a non-global zone"), propname);
1327 (void) zfs_error(hdl, EZFS_ZONED,
1328 errbuf);
1329 goto error;
1331 } else if (getzoneid() != GLOBAL_ZONEID) {
1333 * If zoned property is 'off', this must be in
1334 * a global zone. If not, something is wrong.
1336 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1337 "'%s' cannot be set while dataset "
1338 "'zoned' property is set"), propname);
1339 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1340 goto error;
1344 * At this point, it is legitimate to set the
1345 * property. Now we want to make sure that the
1346 * property value is valid if it is sharenfs.
1348 if ((prop == ZFS_PROP_SHARENFS ||
1349 prop == ZFS_PROP_SHARESMB) &&
1350 strcmp(strval, "on") != 0 &&
1351 strcmp(strval, "off") != 0) {
1352 zfs_share_proto_t proto;
1354 if (prop == ZFS_PROP_SHARESMB)
1355 proto = PROTO_SMB;
1356 else
1357 proto = PROTO_NFS;
1360 * Must be an valid sharing protocol
1361 * option string so init the libshare
1362 * in order to enable the parser and
1363 * then parse the options. We use the
1364 * control API since we don't care about
1365 * the current configuration and don't
1366 * want the overhead of loading it
1367 * until we actually do something.
1370 if (zfs_init_libshare(hdl,
1371 SA_INIT_CONTROL_API) != SA_OK) {
1373 * An error occurred so we can't do
1374 * anything
1376 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1377 "'%s' cannot be set: problem "
1378 "in share initialization"),
1379 propname);
1380 (void) zfs_error(hdl, EZFS_BADPROP,
1381 errbuf);
1382 goto error;
1385 if (zfs_parse_options(strval, proto) != SA_OK) {
1387 * There was an error in parsing so
1388 * deal with it by issuing an error
1389 * message and leaving after
1390 * uninitializing the the libshare
1391 * interface.
1393 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1394 "'%s' cannot be set to invalid "
1395 "options"), propname);
1396 (void) zfs_error(hdl, EZFS_BADPROP,
1397 errbuf);
1398 zfs_uninit_libshare(hdl);
1399 goto error;
1401 zfs_uninit_libshare(hdl);
1404 break;
1406 case ZFS_PROP_KEYLOCATION:
1407 if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1409 "invalid keylocation"));
1410 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1411 goto error;
1414 if (zhp != NULL) {
1415 uint64_t crypt =
1416 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1418 if (crypt == ZIO_CRYPT_OFF &&
1419 strcmp(strval, "none") != 0) {
1420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1421 "keylocation must not be 'none' "
1422 "for encrypted datasets"));
1423 (void) zfs_error(hdl, EZFS_BADPROP,
1424 errbuf);
1425 goto error;
1426 } else if (crypt != ZIO_CRYPT_OFF &&
1427 strcmp(strval, "none") == 0) {
1428 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1429 "keylocation must be 'none' "
1430 "for unencrypted datasets"));
1431 (void) zfs_error(hdl, EZFS_BADPROP,
1432 errbuf);
1433 goto error;
1436 break;
1438 case ZFS_PROP_PBKDF2_ITERS:
1439 if (intval < MIN_PBKDF2_ITERATIONS) {
1440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1441 "minimum pbkdf2 iterations is %u"),
1442 MIN_PBKDF2_ITERATIONS);
1443 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1444 goto error;
1446 break;
1448 case ZFS_PROP_UTF8ONLY:
1449 chosen_utf = (int)intval;
1450 break;
1452 case ZFS_PROP_NORMALIZE:
1453 chosen_normal = (int)intval;
1454 break;
1456 default:
1457 break;
1461 * For changes to existing volumes, we have some additional
1462 * checks to enforce.
1464 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1465 uint64_t blocksize = zfs_prop_get_int(zhp,
1466 ZFS_PROP_VOLBLOCKSIZE);
1467 char buf[64];
1469 switch (prop) {
1470 case ZFS_PROP_VOLSIZE:
1471 if (intval % blocksize != 0) {
1472 zfs_nicebytes(blocksize, buf,
1473 sizeof (buf));
1474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475 "'%s' must be a multiple of "
1476 "volume block size (%s)"),
1477 propname, buf);
1478 (void) zfs_error(hdl, EZFS_BADPROP,
1479 errbuf);
1480 goto error;
1483 if (intval == 0) {
1484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1485 "'%s' cannot be zero"),
1486 propname);
1487 (void) zfs_error(hdl, EZFS_BADPROP,
1488 errbuf);
1489 goto error;
1491 break;
1493 default:
1494 break;
1498 /* check encryption properties */
1499 if (zhp != NULL) {
1500 int64_t crypt = zfs_prop_get_int(zhp,
1501 ZFS_PROP_ENCRYPTION);
1503 switch (prop) {
1504 case ZFS_PROP_COPIES:
1505 if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1507 "encrypted datasets cannot have "
1508 "3 copies"));
1509 (void) zfs_error(hdl, EZFS_BADPROP,
1510 errbuf);
1511 goto error;
1513 break;
1514 default:
1515 break;
1521 * If normalization was chosen, but no UTF8 choice was made,
1522 * enforce rejection of non-UTF8 names.
1524 * If normalization was chosen, but rejecting non-UTF8 names
1525 * was explicitly not chosen, it is an error.
1527 if (chosen_normal > 0 && chosen_utf < 0) {
1528 if (nvlist_add_uint64(ret,
1529 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1530 (void) no_memory(hdl);
1531 goto error;
1533 } else if (chosen_normal > 0 && chosen_utf == 0) {
1534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1535 "'%s' must be set 'on' if normalization chosen"),
1536 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1537 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1538 goto error;
1540 return (ret);
1542 error:
1543 nvlist_free(ret);
1544 return (NULL);
1548 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1550 uint64_t old_volsize;
1551 uint64_t new_volsize;
1552 uint64_t old_reservation;
1553 uint64_t new_reservation;
1554 zfs_prop_t resv_prop;
1555 nvlist_t *props;
1558 * If this is an existing volume, and someone is setting the volsize,
1559 * make sure that it matches the reservation, or add it if necessary.
1561 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1562 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1563 return (-1);
1564 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1566 props = fnvlist_alloc();
1567 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1568 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1570 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1571 old_reservation) || nvlist_exists(nvl,
1572 zfs_prop_to_name(resv_prop))) {
1573 fnvlist_free(props);
1574 return (0);
1576 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1577 &new_volsize) != 0) {
1578 fnvlist_free(props);
1579 return (-1);
1581 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1582 fnvlist_free(props);
1584 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1585 new_reservation) != 0) {
1586 (void) no_memory(zhp->zfs_hdl);
1587 return (-1);
1589 return (1);
1592 void
1593 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1594 char *errbuf)
1596 switch (err) {
1598 case ENOSPC:
1600 * For quotas and reservations, ENOSPC indicates
1601 * something different; setting a quota or reservation
1602 * doesn't use any disk space.
1604 switch (prop) {
1605 case ZFS_PROP_QUOTA:
1606 case ZFS_PROP_REFQUOTA:
1607 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1608 "size is less than current used or "
1609 "reserved space"));
1610 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1611 break;
1613 case ZFS_PROP_RESERVATION:
1614 case ZFS_PROP_REFRESERVATION:
1615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1616 "size is greater than available space"));
1617 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1618 break;
1620 default:
1621 (void) zfs_standard_error(hdl, err, errbuf);
1622 break;
1624 break;
1626 case EBUSY:
1627 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1628 break;
1630 case EROFS:
1631 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1632 break;
1634 case E2BIG:
1635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1636 "property value too long"));
1637 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1638 break;
1640 case ENOTSUP:
1641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1642 "pool and or dataset must be upgraded to set this "
1643 "property or value"));
1644 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1645 break;
1647 case ERANGE:
1648 if (prop == ZFS_PROP_COMPRESSION ||
1649 prop == ZFS_PROP_DNODESIZE ||
1650 prop == ZFS_PROP_RECORDSIZE) {
1651 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1652 "property setting is not allowed on "
1653 "bootable datasets"));
1654 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1655 } else if (prop == ZFS_PROP_CHECKSUM ||
1656 prop == ZFS_PROP_DEDUP) {
1657 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1658 "property setting is not allowed on "
1659 "root pools"));
1660 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1661 } else {
1662 (void) zfs_standard_error(hdl, err, errbuf);
1664 break;
1666 case EINVAL:
1667 if (prop == ZPROP_INVAL) {
1668 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1669 } else {
1670 (void) zfs_standard_error(hdl, err, errbuf);
1672 break;
1674 case EACCES:
1675 if (prop == ZFS_PROP_KEYLOCATION) {
1676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1677 "keylocation may only be set on encryption roots"));
1678 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1679 } else {
1680 (void) zfs_standard_error(hdl, err, errbuf);
1682 break;
1684 case EOVERFLOW:
1686 * This platform can't address a volume this big.
1688 #ifdef _ILP32
1689 if (prop == ZFS_PROP_VOLSIZE) {
1690 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1691 break;
1693 #endif
1694 /* FALLTHROUGH */
1695 default:
1696 (void) zfs_standard_error(hdl, err, errbuf);
1700 static boolean_t
1701 zfs_is_namespace_prop(zfs_prop_t prop)
1703 switch (prop) {
1705 case ZFS_PROP_ATIME:
1706 case ZFS_PROP_RELATIME:
1707 case ZFS_PROP_DEVICES:
1708 case ZFS_PROP_EXEC:
1709 case ZFS_PROP_SETUID:
1710 case ZFS_PROP_READONLY:
1711 case ZFS_PROP_XATTR:
1712 case ZFS_PROP_NBMAND:
1713 return (B_TRUE);
1715 default:
1716 return (B_FALSE);
1721 * Given a property name and value, set the property for the given dataset.
1724 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1726 int ret = -1;
1727 char errbuf[1024];
1728 libzfs_handle_t *hdl = zhp->zfs_hdl;
1729 nvlist_t *nvl = NULL;
1731 (void) snprintf(errbuf, sizeof (errbuf),
1732 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1733 zhp->zfs_name);
1735 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1736 nvlist_add_string(nvl, propname, propval) != 0) {
1737 (void) no_memory(hdl);
1738 goto error;
1741 ret = zfs_prop_set_list(zhp, nvl);
1743 error:
1744 nvlist_free(nvl);
1745 return (ret);
1751 * Given an nvlist of property names and values, set the properties for the
1752 * given dataset.
1755 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1757 zfs_cmd_t zc = {"\0"};
1758 int ret = -1;
1759 prop_changelist_t **cls = NULL;
1760 int cl_idx;
1761 char errbuf[1024];
1762 libzfs_handle_t *hdl = zhp->zfs_hdl;
1763 nvlist_t *nvl;
1764 int nvl_len = 0;
1765 int added_resv = 0;
1766 zfs_prop_t prop = 0;
1767 nvpair_t *elem;
1769 (void) snprintf(errbuf, sizeof (errbuf),
1770 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1771 zhp->zfs_name);
1773 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1774 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1775 B_FALSE, errbuf)) == NULL)
1776 goto error;
1779 * We have to check for any extra properties which need to be added
1780 * before computing the length of the nvlist.
1782 for (elem = nvlist_next_nvpair(nvl, NULL);
1783 elem != NULL;
1784 elem = nvlist_next_nvpair(nvl, elem)) {
1785 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1786 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1787 goto error;
1791 * Check how many properties we're setting and allocate an array to
1792 * store changelist pointers for postfix().
1794 for (elem = nvlist_next_nvpair(nvl, NULL);
1795 elem != NULL;
1796 elem = nvlist_next_nvpair(nvl, elem))
1797 nvl_len++;
1798 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1799 goto error;
1801 cl_idx = 0;
1802 for (elem = nvlist_next_nvpair(nvl, NULL);
1803 elem != NULL;
1804 elem = nvlist_next_nvpair(nvl, elem)) {
1806 prop = zfs_name_to_prop(nvpair_name(elem));
1808 assert(cl_idx < nvl_len);
1810 * We don't want to unmount & remount the dataset when changing
1811 * its canmount property to 'on' or 'noauto'. We only use
1812 * the changelist logic to unmount when setting canmount=off.
1814 if (prop != ZFS_PROP_CANMOUNT ||
1815 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1816 zfs_is_mounted(zhp, NULL))) {
1817 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1818 if (cls[cl_idx] == NULL)
1819 goto error;
1822 if (prop == ZFS_PROP_MOUNTPOINT &&
1823 changelist_haszonedchild(cls[cl_idx])) {
1824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825 "child dataset with inherited mountpoint is used "
1826 "in a non-global zone"));
1827 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1828 goto error;
1831 if (cls[cl_idx] != NULL &&
1832 (ret = changelist_prefix(cls[cl_idx])) != 0)
1833 goto error;
1835 cl_idx++;
1837 assert(cl_idx == nvl_len);
1840 * Execute the corresponding ioctl() to set this list of properties.
1842 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1844 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1845 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1846 goto error;
1848 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1850 if (ret != 0) {
1851 /* Get the list of unset properties back and report them. */
1852 nvlist_t *errorprops = NULL;
1853 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1854 goto error;
1855 for (elem = nvlist_next_nvpair(nvl, NULL);
1856 elem != NULL;
1857 elem = nvlist_next_nvpair(nvl, elem)) {
1858 prop = zfs_name_to_prop(nvpair_name(elem));
1859 zfs_setprop_error(hdl, prop, errno, errbuf);
1861 nvlist_free(errorprops);
1863 if (added_resv && errno == ENOSPC) {
1864 /* clean up the volsize property we tried to set */
1865 uint64_t old_volsize = zfs_prop_get_int(zhp,
1866 ZFS_PROP_VOLSIZE);
1867 nvlist_free(nvl);
1868 nvl = NULL;
1869 zcmd_free_nvlists(&zc);
1871 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1872 goto error;
1873 if (nvlist_add_uint64(nvl,
1874 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1875 old_volsize) != 0)
1876 goto error;
1877 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1878 goto error;
1879 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1881 } else {
1882 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1883 if (cls[cl_idx] != NULL) {
1884 int clp_err = changelist_postfix(cls[cl_idx]);
1885 if (clp_err != 0)
1886 ret = clp_err;
1890 if (ret == 0) {
1892 * Refresh the statistics so the new property
1893 * value is reflected.
1895 (void) get_stats(zhp);
1898 * Remount the filesystem to propagate the change
1899 * if one of the options handled by the generic
1900 * Linux namespace layer has been modified.
1902 if (zfs_is_namespace_prop(prop) &&
1903 zfs_is_mounted(zhp, NULL))
1904 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1908 error:
1909 nvlist_free(nvl);
1910 zcmd_free_nvlists(&zc);
1911 if (cls != NULL) {
1912 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1913 if (cls[cl_idx] != NULL)
1914 changelist_free(cls[cl_idx]);
1916 free(cls);
1918 return (ret);
1922 * Given a property, inherit the value from the parent dataset, or if received
1923 * is TRUE, revert to the received value, if any.
1926 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1928 zfs_cmd_t zc = {"\0"};
1929 int ret;
1930 prop_changelist_t *cl;
1931 libzfs_handle_t *hdl = zhp->zfs_hdl;
1932 char errbuf[1024];
1933 zfs_prop_t prop;
1935 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1936 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1938 zc.zc_cookie = received;
1939 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1941 * For user properties, the amount of work we have to do is very
1942 * small, so just do it here.
1944 if (!zfs_prop_user(propname)) {
1945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1946 "invalid property"));
1947 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1950 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1951 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1953 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1954 return (zfs_standard_error(hdl, errno, errbuf));
1956 return (0);
1960 * Verify that this property is inheritable.
1962 if (zfs_prop_readonly(prop))
1963 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1965 if (!zfs_prop_inheritable(prop) && !received)
1966 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1969 * Check to see if the value applies to this type
1971 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
1972 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1975 * Normalize the name, to get rid of shorthand abbreviations.
1977 propname = zfs_prop_to_name(prop);
1978 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1979 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1981 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1982 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1983 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1984 "dataset is used in a non-global zone"));
1985 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1989 * Determine datasets which will be affected by this change, if any.
1991 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1992 return (-1);
1994 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1996 "child dataset with inherited mountpoint is used "
1997 "in a non-global zone"));
1998 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1999 goto error;
2002 if ((ret = changelist_prefix(cl)) != 0)
2003 goto error;
2005 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
2006 return (zfs_standard_error(hdl, errno, errbuf));
2007 } else {
2009 if ((ret = changelist_postfix(cl)) != 0)
2010 goto error;
2013 * Refresh the statistics so the new property is reflected.
2015 (void) get_stats(zhp);
2018 * Remount the filesystem to propagate the change
2019 * if one of the options handled by the generic
2020 * Linux namespace layer has been modified.
2022 if (zfs_is_namespace_prop(prop) &&
2023 zfs_is_mounted(zhp, NULL))
2024 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
2027 error:
2028 changelist_free(cl);
2029 return (ret);
2033 * True DSL properties are stored in an nvlist. The following two functions
2034 * extract them appropriately.
2036 uint64_t
2037 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2039 nvlist_t *nv;
2040 uint64_t value;
2042 *source = NULL;
2043 if (nvlist_lookup_nvlist(zhp->zfs_props,
2044 zfs_prop_to_name(prop), &nv) == 0) {
2045 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2046 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2047 } else {
2048 verify(!zhp->zfs_props_table ||
2049 zhp->zfs_props_table[prop] == B_TRUE);
2050 value = zfs_prop_default_numeric(prop);
2051 *source = "";
2054 return (value);
2057 static const char *
2058 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2060 nvlist_t *nv;
2061 const char *value;
2063 *source = NULL;
2064 if (nvlist_lookup_nvlist(zhp->zfs_props,
2065 zfs_prop_to_name(prop), &nv) == 0) {
2066 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2067 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2068 } else {
2069 verify(!zhp->zfs_props_table ||
2070 zhp->zfs_props_table[prop] == B_TRUE);
2071 value = zfs_prop_default_string(prop);
2072 *source = "";
2075 return (value);
2078 static boolean_t
2079 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2081 return (zhp->zfs_props == zhp->zfs_recvd_props);
2084 static void
2085 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2087 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2088 zhp->zfs_props = zhp->zfs_recvd_props;
2091 static void
2092 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2094 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2095 *cookie = 0;
2099 * Internal function for getting a numeric property. Both zfs_prop_get() and
2100 * zfs_prop_get_int() are built using this interface.
2102 * Certain properties can be overridden using 'mount -o'. In this case, scan
2103 * the contents of the /proc/self/mounts entry, searching for the
2104 * appropriate options. If they differ from the on-disk values, report the
2105 * current values and mark the source "temporary".
2107 static int
2108 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2109 char **source, uint64_t *val)
2111 zfs_cmd_t zc = {"\0"};
2112 nvlist_t *zplprops = NULL;
2113 struct mnttab mnt;
2114 char *mntopt_on = NULL;
2115 char *mntopt_off = NULL;
2116 boolean_t received = zfs_is_recvd_props_mode(zhp);
2118 *source = NULL;
2121 * If the property is being fetched for a snapshot, check whether
2122 * the property is valid for the snapshot's head dataset type.
2124 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
2125 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
2126 *val = zfs_prop_default_numeric(prop);
2127 return (-1);
2130 switch (prop) {
2131 case ZFS_PROP_ATIME:
2132 mntopt_on = MNTOPT_ATIME;
2133 mntopt_off = MNTOPT_NOATIME;
2134 break;
2136 case ZFS_PROP_RELATIME:
2137 mntopt_on = MNTOPT_RELATIME;
2138 mntopt_off = MNTOPT_NORELATIME;
2139 break;
2141 case ZFS_PROP_DEVICES:
2142 mntopt_on = MNTOPT_DEVICES;
2143 mntopt_off = MNTOPT_NODEVICES;
2144 break;
2146 case ZFS_PROP_EXEC:
2147 mntopt_on = MNTOPT_EXEC;
2148 mntopt_off = MNTOPT_NOEXEC;
2149 break;
2151 case ZFS_PROP_READONLY:
2152 mntopt_on = MNTOPT_RO;
2153 mntopt_off = MNTOPT_RW;
2154 break;
2156 case ZFS_PROP_SETUID:
2157 mntopt_on = MNTOPT_SETUID;
2158 mntopt_off = MNTOPT_NOSETUID;
2159 break;
2161 case ZFS_PROP_XATTR:
2162 mntopt_on = MNTOPT_XATTR;
2163 mntopt_off = MNTOPT_NOXATTR;
2164 break;
2166 case ZFS_PROP_NBMAND:
2167 mntopt_on = MNTOPT_NBMAND;
2168 mntopt_off = MNTOPT_NONBMAND;
2169 break;
2171 default:
2172 break;
2176 * Because looking up the mount options is potentially expensive
2177 * (iterating over all of /proc/self/mounts), we defer its
2178 * calculation until we're looking up a property which requires
2179 * its presence.
2181 if (!zhp->zfs_mntcheck &&
2182 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2183 libzfs_handle_t *hdl = zhp->zfs_hdl;
2184 struct mnttab entry;
2186 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2187 zhp->zfs_mntopts = zfs_strdup(hdl,
2188 entry.mnt_mntopts);
2189 if (zhp->zfs_mntopts == NULL)
2190 return (-1);
2193 zhp->zfs_mntcheck = B_TRUE;
2196 if (zhp->zfs_mntopts == NULL)
2197 mnt.mnt_mntopts = "";
2198 else
2199 mnt.mnt_mntopts = zhp->zfs_mntopts;
2201 switch (prop) {
2202 case ZFS_PROP_ATIME:
2203 case ZFS_PROP_RELATIME:
2204 case ZFS_PROP_DEVICES:
2205 case ZFS_PROP_EXEC:
2206 case ZFS_PROP_READONLY:
2207 case ZFS_PROP_SETUID:
2208 case ZFS_PROP_XATTR:
2209 case ZFS_PROP_NBMAND:
2210 *val = getprop_uint64(zhp, prop, source);
2212 if (received)
2213 break;
2215 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2216 *val = B_TRUE;
2217 if (src)
2218 *src = ZPROP_SRC_TEMPORARY;
2219 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2220 *val = B_FALSE;
2221 if (src)
2222 *src = ZPROP_SRC_TEMPORARY;
2224 break;
2226 case ZFS_PROP_CANMOUNT:
2227 case ZFS_PROP_VOLSIZE:
2228 case ZFS_PROP_QUOTA:
2229 case ZFS_PROP_REFQUOTA:
2230 case ZFS_PROP_RESERVATION:
2231 case ZFS_PROP_REFRESERVATION:
2232 case ZFS_PROP_FILESYSTEM_LIMIT:
2233 case ZFS_PROP_SNAPSHOT_LIMIT:
2234 case ZFS_PROP_FILESYSTEM_COUNT:
2235 case ZFS_PROP_SNAPSHOT_COUNT:
2236 *val = getprop_uint64(zhp, prop, source);
2238 if (*source == NULL) {
2239 /* not default, must be local */
2240 *source = zhp->zfs_name;
2242 break;
2244 case ZFS_PROP_MOUNTED:
2245 *val = (zhp->zfs_mntopts != NULL);
2246 break;
2248 case ZFS_PROP_NUMCLONES:
2249 *val = zhp->zfs_dmustats.dds_num_clones;
2250 break;
2252 case ZFS_PROP_VERSION:
2253 case ZFS_PROP_NORMALIZE:
2254 case ZFS_PROP_UTF8ONLY:
2255 case ZFS_PROP_CASE:
2256 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2257 return (-1);
2258 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2259 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2260 zcmd_free_nvlists(&zc);
2261 if (prop == ZFS_PROP_VERSION &&
2262 zhp->zfs_type == ZFS_TYPE_VOLUME)
2263 *val = zfs_prop_default_numeric(prop);
2264 return (-1);
2266 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2267 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2268 val) != 0) {
2269 zcmd_free_nvlists(&zc);
2270 return (-1);
2272 nvlist_free(zplprops);
2273 zcmd_free_nvlists(&zc);
2274 break;
2276 case ZFS_PROP_INCONSISTENT:
2277 *val = zhp->zfs_dmustats.dds_inconsistent;
2278 break;
2280 default:
2281 switch (zfs_prop_get_type(prop)) {
2282 case PROP_TYPE_NUMBER:
2283 case PROP_TYPE_INDEX:
2284 *val = getprop_uint64(zhp, prop, source);
2286 * If we tried to use a default value for a
2287 * readonly property, it means that it was not
2288 * present. Note this only applies to "truly"
2289 * readonly properties, not set-once properties
2290 * like volblocksize.
2292 if (zfs_prop_readonly(prop) &&
2293 !zfs_prop_setonce(prop) &&
2294 *source != NULL && (*source)[0] == '\0') {
2295 *source = NULL;
2296 return (-1);
2298 break;
2300 case PROP_TYPE_STRING:
2301 default:
2302 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2303 "cannot get non-numeric property"));
2304 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2305 dgettext(TEXT_DOMAIN, "internal error")));
2309 return (0);
2313 * Calculate the source type, given the raw source string.
2315 static void
2316 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2317 char *statbuf, size_t statlen)
2319 if (statbuf == NULL ||
2320 srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) {
2321 return;
2324 if (source == NULL) {
2325 *srctype = ZPROP_SRC_NONE;
2326 } else if (source[0] == '\0') {
2327 *srctype = ZPROP_SRC_DEFAULT;
2328 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2329 *srctype = ZPROP_SRC_RECEIVED;
2330 } else {
2331 if (strcmp(source, zhp->zfs_name) == 0) {
2332 *srctype = ZPROP_SRC_LOCAL;
2333 } else {
2334 (void) strlcpy(statbuf, source, statlen);
2335 *srctype = ZPROP_SRC_INHERITED;
2342 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2343 size_t proplen, boolean_t literal)
2345 zfs_prop_t prop;
2346 int err = 0;
2348 if (zhp->zfs_recvd_props == NULL)
2349 if (get_recvd_props_ioctl(zhp) != 0)
2350 return (-1);
2352 prop = zfs_name_to_prop(propname);
2354 if (prop != ZPROP_INVAL) {
2355 uint64_t cookie;
2356 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2357 return (-1);
2358 zfs_set_recvd_props_mode(zhp, &cookie);
2359 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2360 NULL, NULL, 0, literal);
2361 zfs_unset_recvd_props_mode(zhp, &cookie);
2362 } else {
2363 nvlist_t *propval;
2364 char *recvdval;
2365 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2366 propname, &propval) != 0)
2367 return (-1);
2368 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2369 &recvdval) == 0);
2370 (void) strlcpy(propbuf, recvdval, proplen);
2373 return (err == 0 ? 0 : -1);
2376 static int
2377 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2379 nvlist_t *value;
2380 nvpair_t *pair;
2382 value = zfs_get_clones_nvl(zhp);
2383 if (value == NULL)
2384 return (-1);
2386 propbuf[0] = '\0';
2387 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2388 pair = nvlist_next_nvpair(value, pair)) {
2389 if (propbuf[0] != '\0')
2390 (void) strlcat(propbuf, ",", proplen);
2391 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2394 return (0);
2397 struct get_clones_arg {
2398 uint64_t numclones;
2399 nvlist_t *value;
2400 const char *origin;
2401 char buf[ZFS_MAX_DATASET_NAME_LEN];
2405 get_clones_cb(zfs_handle_t *zhp, void *arg)
2407 struct get_clones_arg *gca = arg;
2409 if (gca->numclones == 0) {
2410 zfs_close(zhp);
2411 return (0);
2414 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2415 NULL, NULL, 0, B_TRUE) != 0)
2416 goto out;
2417 if (strcmp(gca->buf, gca->origin) == 0) {
2418 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2419 gca->numclones--;
2422 out:
2423 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2424 zfs_close(zhp);
2425 return (0);
2428 nvlist_t *
2429 zfs_get_clones_nvl(zfs_handle_t *zhp)
2431 nvlist_t *nv, *value;
2433 if (nvlist_lookup_nvlist(zhp->zfs_props,
2434 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2435 struct get_clones_arg gca;
2438 * if this is a snapshot, then the kernel wasn't able
2439 * to get the clones. Do it by slowly iterating.
2441 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2442 return (NULL);
2443 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2444 return (NULL);
2445 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2446 nvlist_free(nv);
2447 return (NULL);
2450 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2451 gca.value = value;
2452 gca.origin = zhp->zfs_name;
2454 if (gca.numclones != 0) {
2455 zfs_handle_t *root;
2456 char pool[ZFS_MAX_DATASET_NAME_LEN];
2457 char *cp = pool;
2459 /* get the pool name */
2460 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2461 (void) strsep(&cp, "/@");
2462 root = zfs_open(zhp->zfs_hdl, pool,
2463 ZFS_TYPE_FILESYSTEM);
2464 if (root == NULL) {
2465 nvlist_free(nv);
2466 nvlist_free(value);
2467 return (NULL);
2470 (void) get_clones_cb(root, &gca);
2473 if (gca.numclones != 0 ||
2474 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2475 nvlist_add_nvlist(zhp->zfs_props,
2476 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2477 nvlist_free(nv);
2478 nvlist_free(value);
2479 return (NULL);
2481 nvlist_free(nv);
2482 nvlist_free(value);
2483 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2484 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2487 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2489 return (value);
2493 * Accepts a property and value and checks that the value
2494 * matches the one found by the channel program. If they are
2495 * not equal, print both of them.
2497 static void
2498 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2499 const char *strval)
2501 if (!zhp->zfs_hdl->libzfs_prop_debug)
2502 return;
2503 int error;
2504 char *poolname = zhp->zpool_hdl->zpool_name;
2505 const char *prop_name = zfs_prop_to_name(prop);
2506 const char *program =
2507 "args = ...\n"
2508 "ds = args['dataset']\n"
2509 "prop = args['property']\n"
2510 "value, setpoint = zfs.get_prop(ds, prop)\n"
2511 "return {value=value, setpoint=setpoint}\n";
2512 nvlist_t *outnvl;
2513 nvlist_t *retnvl;
2514 nvlist_t *argnvl = fnvlist_alloc();
2516 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2517 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2519 error = lzc_channel_program_nosync(poolname, program,
2520 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2522 if (error == 0) {
2523 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2524 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2525 int64_t ans;
2526 error = nvlist_lookup_int64(retnvl, "value", &ans);
2527 if (error != 0) {
2528 (void) fprintf(stderr, "%s: zcp check error: "
2529 "%u\n", prop_name, error);
2530 return;
2532 if (ans != intval) {
2533 (void) fprintf(stderr, "%s: zfs found %llu, "
2534 "but zcp found %llu\n", prop_name,
2535 (u_longlong_t)intval, (u_longlong_t)ans);
2537 } else {
2538 char *str_ans;
2539 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2540 if (error != 0) {
2541 (void) fprintf(stderr, "%s: zcp check error: "
2542 "%u\n", prop_name, error);
2543 return;
2545 if (strcmp(strval, str_ans) != 0) {
2546 (void) fprintf(stderr,
2547 "%s: zfs found '%s', but zcp found '%s'\n",
2548 prop_name, strval, str_ans);
2551 } else {
2552 (void) fprintf(stderr, "%s: zcp check failed, channel program "
2553 "error: %u\n", prop_name, error);
2555 nvlist_free(argnvl);
2556 nvlist_free(outnvl);
2560 * Retrieve a property from the given object. If 'literal' is specified, then
2561 * numbers are left as exact values. Otherwise, numbers are converted to a
2562 * human-readable form.
2564 * Returns 0 on success, or -1 on error.
2567 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2568 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2570 char *source = NULL;
2571 uint64_t val;
2572 const char *str;
2573 const char *strval;
2574 boolean_t received = zfs_is_recvd_props_mode(zhp);
2577 * Check to see if this property applies to our object
2579 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2580 return (-1);
2582 if (received && zfs_prop_readonly(prop))
2583 return (-1);
2585 if (src)
2586 *src = ZPROP_SRC_NONE;
2588 switch (prop) {
2589 case ZFS_PROP_CREATION:
2591 * 'creation' is a time_t stored in the statistics. We convert
2592 * this into a string unless 'literal' is specified.
2595 val = getprop_uint64(zhp, prop, &source);
2596 time_t time = (time_t)val;
2597 struct tm t;
2599 if (literal ||
2600 localtime_r(&time, &t) == NULL ||
2601 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2602 &t) == 0)
2603 (void) snprintf(propbuf, proplen, "%llu",
2604 (u_longlong_t)val);
2606 zcp_check(zhp, prop, val, NULL);
2607 break;
2609 case ZFS_PROP_MOUNTPOINT:
2611 * Getting the precise mountpoint can be tricky.
2613 * - for 'none' or 'legacy', return those values.
2614 * - for inherited mountpoints, we want to take everything
2615 * after our ancestor and append it to the inherited value.
2617 * If the pool has an alternate root, we want to prepend that
2618 * root to any values we return.
2621 str = getprop_string(zhp, prop, &source);
2623 if (str[0] == '/') {
2624 char buf[MAXPATHLEN];
2625 char *root = buf;
2626 const char *relpath;
2629 * If we inherit the mountpoint, even from a dataset
2630 * with a received value, the source will be the path of
2631 * the dataset we inherit from. If source is
2632 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2633 * inherited.
2635 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2636 relpath = "";
2637 } else {
2638 relpath = zhp->zfs_name + strlen(source);
2639 if (relpath[0] == '/')
2640 relpath++;
2643 if ((zpool_get_prop(zhp->zpool_hdl,
2644 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2645 B_FALSE)) || (strcmp(root, "-") == 0))
2646 root[0] = '\0';
2648 * Special case an alternate root of '/'. This will
2649 * avoid having multiple leading slashes in the
2650 * mountpoint path.
2652 if (strcmp(root, "/") == 0)
2653 root++;
2656 * If the mountpoint is '/' then skip over this
2657 * if we are obtaining either an alternate root or
2658 * an inherited mountpoint.
2660 if (str[1] == '\0' && (root[0] != '\0' ||
2661 relpath[0] != '\0'))
2662 str++;
2664 if (relpath[0] == '\0')
2665 (void) snprintf(propbuf, proplen, "%s%s",
2666 root, str);
2667 else
2668 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2669 root, str, relpath[0] == '@' ? "" : "/",
2670 relpath);
2671 } else {
2672 /* 'legacy' or 'none' */
2673 (void) strlcpy(propbuf, str, proplen);
2675 zcp_check(zhp, prop, 0, propbuf);
2676 break;
2678 case ZFS_PROP_ORIGIN:
2679 str = getprop_string(zhp, prop, &source);
2680 if (str == NULL)
2681 return (-1);
2682 (void) strlcpy(propbuf, str, proplen);
2683 zcp_check(zhp, prop, 0, str);
2684 break;
2686 case ZFS_PROP_CLONES:
2687 if (get_clones_string(zhp, propbuf, proplen) != 0)
2688 return (-1);
2689 break;
2691 case ZFS_PROP_QUOTA:
2692 case ZFS_PROP_REFQUOTA:
2693 case ZFS_PROP_RESERVATION:
2694 case ZFS_PROP_REFRESERVATION:
2696 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2697 return (-1);
2699 * If quota or reservation is 0, we translate this into 'none'
2700 * (unless literal is set), and indicate that it's the default
2701 * value. Otherwise, we print the number nicely and indicate
2702 * that its set locally.
2704 if (val == 0) {
2705 if (literal)
2706 (void) strlcpy(propbuf, "0", proplen);
2707 else
2708 (void) strlcpy(propbuf, "none", proplen);
2709 } else {
2710 if (literal)
2711 (void) snprintf(propbuf, proplen, "%llu",
2712 (u_longlong_t)val);
2713 else
2714 zfs_nicebytes(val, propbuf, proplen);
2716 zcp_check(zhp, prop, val, NULL);
2717 break;
2719 case ZFS_PROP_FILESYSTEM_LIMIT:
2720 case ZFS_PROP_SNAPSHOT_LIMIT:
2721 case ZFS_PROP_FILESYSTEM_COUNT:
2722 case ZFS_PROP_SNAPSHOT_COUNT:
2724 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2725 return (-1);
2728 * If limit is UINT64_MAX, we translate this into 'none' (unless
2729 * literal is set), and indicate that it's the default value.
2730 * Otherwise, we print the number nicely and indicate that it's
2731 * set locally.
2733 if (literal) {
2734 (void) snprintf(propbuf, proplen, "%llu",
2735 (u_longlong_t)val);
2736 } else if (val == UINT64_MAX) {
2737 (void) strlcpy(propbuf, "none", proplen);
2738 } else {
2739 zfs_nicenum(val, propbuf, proplen);
2742 zcp_check(zhp, prop, val, NULL);
2743 break;
2745 case ZFS_PROP_REFRATIO:
2746 case ZFS_PROP_COMPRESSRATIO:
2747 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2748 return (-1);
2749 if (literal)
2750 (void) snprintf(propbuf, proplen, "%llu.%02llu",
2751 (u_longlong_t)(val / 100),
2752 (u_longlong_t)(val % 100));
2753 else
2754 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2755 (u_longlong_t)(val / 100),
2756 (u_longlong_t)(val % 100));
2757 zcp_check(zhp, prop, val, NULL);
2758 break;
2760 case ZFS_PROP_TYPE:
2761 switch (zhp->zfs_type) {
2762 case ZFS_TYPE_FILESYSTEM:
2763 str = "filesystem";
2764 break;
2765 case ZFS_TYPE_VOLUME:
2766 str = "volume";
2767 break;
2768 case ZFS_TYPE_SNAPSHOT:
2769 str = "snapshot";
2770 break;
2771 case ZFS_TYPE_BOOKMARK:
2772 str = "bookmark";
2773 break;
2774 default:
2775 abort();
2777 (void) snprintf(propbuf, proplen, "%s", str);
2778 zcp_check(zhp, prop, 0, propbuf);
2779 break;
2781 case ZFS_PROP_MOUNTED:
2783 * The 'mounted' property is a pseudo-property that described
2784 * whether the filesystem is currently mounted. Even though
2785 * it's a boolean value, the typical values of "on" and "off"
2786 * don't make sense, so we translate to "yes" and "no".
2788 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2789 src, &source, &val) != 0)
2790 return (-1);
2791 if (val)
2792 (void) strlcpy(propbuf, "yes", proplen);
2793 else
2794 (void) strlcpy(propbuf, "no", proplen);
2795 break;
2797 case ZFS_PROP_NAME:
2799 * The 'name' property is a pseudo-property derived from the
2800 * dataset name. It is presented as a real property to simplify
2801 * consumers.
2803 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2804 zcp_check(zhp, prop, 0, propbuf);
2805 break;
2807 case ZFS_PROP_MLSLABEL:
2809 #ifdef HAVE_MLSLABEL
2810 m_label_t *new_sl = NULL;
2811 char *ascii = NULL; /* human readable label */
2813 (void) strlcpy(propbuf,
2814 getprop_string(zhp, prop, &source), proplen);
2816 if (literal || (strcasecmp(propbuf,
2817 ZFS_MLSLABEL_DEFAULT) == 0))
2818 break;
2821 * Try to translate the internal hex string to
2822 * human-readable output. If there are any
2823 * problems just use the hex string.
2826 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2827 L_NO_CORRECTION, NULL) == -1) {
2828 m_label_free(new_sl);
2829 break;
2832 if (label_to_str(new_sl, &ascii, M_LABEL,
2833 DEF_NAMES) != 0) {
2834 if (ascii)
2835 free(ascii);
2836 m_label_free(new_sl);
2837 break;
2839 m_label_free(new_sl);
2841 (void) strlcpy(propbuf, ascii, proplen);
2842 free(ascii);
2843 #else
2844 (void) strlcpy(propbuf,
2845 getprop_string(zhp, prop, &source), proplen);
2846 #endif /* HAVE_MLSLABEL */
2848 break;
2850 case ZFS_PROP_GUID:
2851 case ZFS_PROP_CREATETXG:
2853 * GUIDs are stored as numbers, but they are identifiers.
2854 * We don't want them to be pretty printed, because pretty
2855 * printing mangles the ID into a truncated and useless value.
2857 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2858 return (-1);
2859 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2860 zcp_check(zhp, prop, val, NULL);
2861 break;
2863 case ZFS_PROP_REFERENCED:
2864 case ZFS_PROP_AVAILABLE:
2865 case ZFS_PROP_USED:
2866 case ZFS_PROP_USEDSNAP:
2867 case ZFS_PROP_USEDDS:
2868 case ZFS_PROP_USEDREFRESERV:
2869 case ZFS_PROP_USEDCHILD:
2870 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2871 return (-1);
2872 if (literal) {
2873 (void) snprintf(propbuf, proplen, "%llu",
2874 (u_longlong_t)val);
2875 } else {
2876 zfs_nicebytes(val, propbuf, proplen);
2878 zcp_check(zhp, prop, val, NULL);
2879 break;
2881 default:
2882 switch (zfs_prop_get_type(prop)) {
2883 case PROP_TYPE_NUMBER:
2884 if (get_numeric_property(zhp, prop, src,
2885 &source, &val) != 0) {
2886 return (-1);
2889 if (literal) {
2890 (void) snprintf(propbuf, proplen, "%llu",
2891 (u_longlong_t)val);
2892 } else {
2893 zfs_nicenum(val, propbuf, proplen);
2895 zcp_check(zhp, prop, val, NULL);
2896 break;
2898 case PROP_TYPE_STRING:
2899 str = getprop_string(zhp, prop, &source);
2900 if (str == NULL)
2901 return (-1);
2903 (void) strlcpy(propbuf, str, proplen);
2904 zcp_check(zhp, prop, 0, str);
2905 break;
2907 case PROP_TYPE_INDEX:
2908 if (get_numeric_property(zhp, prop, src,
2909 &source, &val) != 0)
2910 return (-1);
2911 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2912 return (-1);
2914 (void) strlcpy(propbuf, strval, proplen);
2915 zcp_check(zhp, prop, 0, strval);
2916 break;
2918 default:
2919 abort();
2923 get_source(zhp, src, source, statbuf, statlen);
2925 return (0);
2929 * Utility function to get the given numeric property. Does no validation that
2930 * the given property is the appropriate type; should only be used with
2931 * hard-coded property types.
2933 uint64_t
2934 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2936 char *source;
2937 uint64_t val = 0;
2939 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2941 return (val);
2945 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2947 char buf[64];
2949 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2950 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2954 * Similar to zfs_prop_get(), but returns the value as an integer.
2957 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2958 zprop_source_t *src, char *statbuf, size_t statlen)
2960 char *source;
2963 * Check to see if this property applies to our object
2965 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
2966 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2967 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2968 zfs_prop_to_name(prop)));
2971 if (src)
2972 *src = ZPROP_SRC_NONE;
2974 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2975 return (-1);
2977 get_source(zhp, src, source, statbuf, statlen);
2979 return (0);
2982 #ifdef HAVE_IDMAP
2983 static int
2984 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2985 char **domainp, idmap_rid_t *ridp)
2987 idmap_get_handle_t *get_hdl = NULL;
2988 idmap_stat status;
2989 int err = EINVAL;
2991 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2992 goto out;
2994 if (isuser) {
2995 err = idmap_get_sidbyuid(get_hdl, id,
2996 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2997 } else {
2998 err = idmap_get_sidbygid(get_hdl, id,
2999 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3001 if (err == IDMAP_SUCCESS &&
3002 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
3003 status == IDMAP_SUCCESS)
3004 err = 0;
3005 else
3006 err = EINVAL;
3007 out:
3008 if (get_hdl)
3009 idmap_get_destroy(get_hdl);
3010 return (err);
3012 #endif /* HAVE_IDMAP */
3015 * convert the propname into parameters needed by kernel
3016 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3017 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3018 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3019 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3020 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3021 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3023 static int
3024 userquota_propname_decode(const char *propname, boolean_t zoned,
3025 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
3027 zfs_userquota_prop_t type;
3028 char *cp;
3029 boolean_t isuser;
3030 boolean_t isgroup;
3031 boolean_t isproject;
3032 struct passwd *pw;
3033 struct group *gr;
3035 domain[0] = '\0';
3037 /* Figure out the property type ({user|group|project}{quota|space}) */
3038 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
3039 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
3040 strlen(zfs_userquota_prop_prefixes[type])) == 0)
3041 break;
3043 if (type == ZFS_NUM_USERQUOTA_PROPS)
3044 return (EINVAL);
3045 *typep = type;
3047 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3048 type == ZFS_PROP_USEROBJQUOTA ||
3049 type == ZFS_PROP_USEROBJUSED);
3050 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3051 type == ZFS_PROP_GROUPOBJQUOTA ||
3052 type == ZFS_PROP_GROUPOBJUSED);
3053 isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3054 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3055 type == ZFS_PROP_PROJECTOBJUSED);
3057 cp = strchr(propname, '@') + 1;
3059 if (isuser && (pw = getpwnam(cp)) != NULL) {
3060 if (zoned && getzoneid() == GLOBAL_ZONEID)
3061 return (ENOENT);
3062 *ridp = pw->pw_uid;
3063 } else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3064 if (zoned && getzoneid() == GLOBAL_ZONEID)
3065 return (ENOENT);
3066 *ridp = gr->gr_gid;
3067 } else if (!isproject && strchr(cp, '@')) {
3068 #ifdef HAVE_IDMAP
3070 * It's a SID name (eg "user@domain") that needs to be
3071 * turned into S-1-domainID-RID.
3073 directory_error_t e;
3074 char *numericsid = NULL;
3075 char *end;
3077 if (zoned && getzoneid() == GLOBAL_ZONEID)
3078 return (ENOENT);
3079 if (isuser) {
3080 e = directory_sid_from_user_name(NULL,
3081 cp, &numericsid);
3082 } else {
3083 e = directory_sid_from_group_name(NULL,
3084 cp, &numericsid);
3086 if (e != NULL) {
3087 directory_error_free(e);
3088 return (ENOENT);
3090 if (numericsid == NULL)
3091 return (ENOENT);
3092 cp = numericsid;
3093 (void) strlcpy(domain, cp, domainlen);
3094 cp = strrchr(domain, '-');
3095 *cp = '\0';
3096 cp++;
3098 errno = 0;
3099 *ridp = strtoull(cp, &end, 10);
3100 free(numericsid);
3102 if (errno != 0 || *end != '\0')
3103 return (EINVAL);
3104 #else
3105 return (ENOSYS);
3106 #endif /* HAVE_IDMAP */
3107 } else {
3108 /* It's a user/group/project ID (eg "12345"). */
3109 uid_t id;
3110 char *end;
3111 id = strtoul(cp, &end, 10);
3112 if (*end != '\0')
3113 return (EINVAL);
3114 if (id > MAXUID && !isproject) {
3115 #ifdef HAVE_IDMAP
3116 /* It's an ephemeral ID. */
3117 idmap_rid_t rid;
3118 char *mapdomain;
3120 if (idmap_id_to_numeric_domain_rid(id, isuser,
3121 &mapdomain, &rid) != 0)
3122 return (ENOENT);
3123 (void) strlcpy(domain, mapdomain, domainlen);
3124 *ridp = rid;
3125 #else
3126 return (ENOSYS);
3127 #endif /* HAVE_IDMAP */
3128 } else {
3129 *ridp = id;
3133 return (0);
3136 static int
3137 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3138 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3140 int err;
3141 zfs_cmd_t zc = {"\0"};
3143 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3145 err = userquota_propname_decode(propname,
3146 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3147 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3148 zc.zc_objset_type = *typep;
3149 if (err)
3150 return (err);
3152 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3153 if (err)
3154 return (err);
3156 *propvalue = zc.zc_cookie;
3157 return (0);
3161 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3162 uint64_t *propvalue)
3164 zfs_userquota_prop_t type;
3166 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3167 &type));
3171 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3172 char *propbuf, int proplen, boolean_t literal)
3174 int err;
3175 uint64_t propvalue;
3176 zfs_userquota_prop_t type;
3178 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3179 &type);
3181 if (err)
3182 return (err);
3184 if (literal) {
3185 (void) snprintf(propbuf, proplen, "%llu",
3186 (u_longlong_t)propvalue);
3187 } else if (propvalue == 0 &&
3188 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3189 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3190 type == ZFS_PROP_PROJECTQUOTA ||
3191 type == ZFS_PROP_PROJECTOBJQUOTA)) {
3192 (void) strlcpy(propbuf, "none", proplen);
3193 } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3194 type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3195 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3196 zfs_nicebytes(propvalue, propbuf, proplen);
3197 } else {
3198 zfs_nicenum(propvalue, propbuf, proplen);
3200 return (0);
3204 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3205 uint64_t *propvalue)
3207 int err;
3208 zfs_cmd_t zc = {"\0"};
3209 const char *snapname;
3211 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3213 snapname = strchr(propname, '@') + 1;
3214 if (strchr(snapname, '@')) {
3215 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3216 } else {
3217 /* snapname is the short name, append it to zhp's fsname */
3218 char *cp;
3220 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3221 sizeof (zc.zc_value));
3222 cp = strchr(zc.zc_value, '@');
3223 if (cp != NULL)
3224 *cp = '\0';
3225 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3226 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3229 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3230 if (err)
3231 return (err);
3233 *propvalue = zc.zc_cookie;
3234 return (0);
3238 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3239 char *propbuf, int proplen, boolean_t literal)
3241 int err;
3242 uint64_t propvalue;
3244 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3246 if (err)
3247 return (err);
3249 if (literal) {
3250 (void) snprintf(propbuf, proplen, "%llu",
3251 (u_longlong_t)propvalue);
3252 } else {
3253 zfs_nicebytes(propvalue, propbuf, proplen);
3256 return (0);
3260 * Returns the name of the given zfs handle.
3262 const char *
3263 zfs_get_name(const zfs_handle_t *zhp)
3265 return (zhp->zfs_name);
3269 * Returns the name of the parent pool for the given zfs handle.
3271 const char *
3272 zfs_get_pool_name(const zfs_handle_t *zhp)
3274 return (zhp->zpool_hdl->zpool_name);
3278 * Returns the type of the given zfs handle.
3280 zfs_type_t
3281 zfs_get_type(const zfs_handle_t *zhp)
3283 return (zhp->zfs_type);
3287 * Is one dataset name a child dataset of another?
3289 * Needs to handle these cases:
3290 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3291 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3292 * Descendant? No. No. No. Yes.
3294 static boolean_t
3295 is_descendant(const char *ds1, const char *ds2)
3297 size_t d1len = strlen(ds1);
3299 /* ds2 can't be a descendant if it's smaller */
3300 if (strlen(ds2) < d1len)
3301 return (B_FALSE);
3303 /* otherwise, compare strings and verify that there's a '/' char */
3304 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3308 * Given a complete name, return just the portion that refers to the parent.
3309 * Will return -1 if there is no parent (path is just the name of the
3310 * pool).
3312 static int
3313 parent_name(const char *path, char *buf, size_t buflen)
3315 char *slashp;
3317 (void) strlcpy(buf, path, buflen);
3319 if ((slashp = strrchr(buf, '/')) == NULL)
3320 return (-1);
3321 *slashp = '\0';
3323 return (0);
3327 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3329 return (parent_name(zfs_get_name(zhp), buf, buflen));
3333 * If accept_ancestor is false, then check to make sure that the given path has
3334 * a parent, and that it exists. If accept_ancestor is true, then find the
3335 * closest existing ancestor for the given path. In prefixlen return the
3336 * length of already existing prefix of the given path. We also fetch the
3337 * 'zoned' property, which is used to validate property settings when creating
3338 * new datasets.
3340 static int
3341 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3342 boolean_t accept_ancestor, int *prefixlen)
3344 zfs_cmd_t zc = {"\0"};
3345 char parent[ZFS_MAX_DATASET_NAME_LEN];
3346 char *slash;
3347 zfs_handle_t *zhp;
3348 char errbuf[1024];
3349 uint64_t is_zoned;
3351 (void) snprintf(errbuf, sizeof (errbuf),
3352 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3354 /* get parent, and check to see if this is just a pool */
3355 if (parent_name(path, parent, sizeof (parent)) != 0) {
3356 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3357 "missing dataset name"));
3358 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3361 /* check to see if the pool exists */
3362 if ((slash = strchr(parent, '/')) == NULL)
3363 slash = parent + strlen(parent);
3364 (void) strncpy(zc.zc_name, parent, slash - parent);
3365 zc.zc_name[slash - parent] = '\0';
3366 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3367 errno == ENOENT) {
3368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3369 "no such pool '%s'"), zc.zc_name);
3370 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3373 /* check to see if the parent dataset exists */
3374 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3375 if (errno == ENOENT && accept_ancestor) {
3377 * Go deeper to find an ancestor, give up on top level.
3379 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3380 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3381 "no such pool '%s'"), zc.zc_name);
3382 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3384 } else if (errno == ENOENT) {
3385 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3386 "parent does not exist"));
3387 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3388 } else
3389 return (zfs_standard_error(hdl, errno, errbuf));
3392 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3393 if (zoned != NULL)
3394 *zoned = is_zoned;
3396 /* we are in a non-global zone, but parent is in the global zone */
3397 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3398 (void) zfs_standard_error(hdl, EPERM, errbuf);
3399 zfs_close(zhp);
3400 return (-1);
3403 /* make sure parent is a filesystem */
3404 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3406 "parent is not a filesystem"));
3407 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3408 zfs_close(zhp);
3409 return (-1);
3412 zfs_close(zhp);
3413 if (prefixlen != NULL)
3414 *prefixlen = strlen(parent);
3415 return (0);
3419 * Finds whether the dataset of the given type(s) exists.
3421 boolean_t
3422 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3424 zfs_handle_t *zhp;
3426 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3427 return (B_FALSE);
3430 * Try to get stats for the dataset, which will tell us if it exists.
3432 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3433 int ds_type = zhp->zfs_type;
3435 zfs_close(zhp);
3436 if (types & ds_type)
3437 return (B_TRUE);
3439 return (B_FALSE);
3443 * Given a path to 'target', create all the ancestors between
3444 * the prefixlen portion of the path, and the target itself.
3445 * Fail if the initial prefixlen-ancestor does not already exist.
3448 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3450 zfs_handle_t *h;
3451 char *cp;
3452 const char *opname;
3454 /* make sure prefix exists */
3455 cp = target + prefixlen;
3456 if (*cp != '/') {
3457 assert(strchr(cp, '/') == NULL);
3458 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3459 } else {
3460 *cp = '\0';
3461 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3462 *cp = '/';
3464 if (h == NULL)
3465 return (-1);
3466 zfs_close(h);
3469 * Attempt to create, mount, and share any ancestor filesystems,
3470 * up to the prefixlen-long one.
3472 for (cp = target + prefixlen + 1;
3473 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3475 *cp = '\0';
3477 h = make_dataset_handle(hdl, target);
3478 if (h) {
3479 /* it already exists, nothing to do here */
3480 zfs_close(h);
3481 continue;
3484 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3485 NULL) != 0) {
3486 opname = dgettext(TEXT_DOMAIN, "create");
3487 goto ancestorerr;
3490 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3491 if (h == NULL) {
3492 opname = dgettext(TEXT_DOMAIN, "open");
3493 goto ancestorerr;
3496 if (zfs_mount(h, NULL, 0) != 0) {
3497 opname = dgettext(TEXT_DOMAIN, "mount");
3498 goto ancestorerr;
3501 if (zfs_share(h) != 0) {
3502 opname = dgettext(TEXT_DOMAIN, "share");
3503 goto ancestorerr;
3506 zfs_close(h);
3509 return (0);
3511 ancestorerr:
3512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3513 "failed to %s ancestor '%s'"), opname, target);
3514 return (-1);
3518 * Creates non-existing ancestors of the given path.
3521 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3523 int prefix;
3524 char *path_copy;
3525 int rc = 0;
3527 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3528 return (-1);
3530 if ((path_copy = strdup(path)) != NULL) {
3531 rc = create_parents(hdl, path_copy, prefix);
3532 free(path_copy);
3534 if (path_copy == NULL || rc != 0)
3535 return (-1);
3537 return (0);
3541 * Create a new filesystem or volume.
3544 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3545 nvlist_t *props)
3547 int ret;
3548 uint64_t size = 0;
3549 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3550 uint64_t zoned;
3551 enum lzc_dataset_type ost;
3552 zpool_handle_t *zpool_handle;
3553 uint8_t *wkeydata = NULL;
3554 uint_t wkeylen = 0;
3555 char errbuf[1024];
3556 char parent[ZFS_MAX_DATASET_NAME_LEN];
3558 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3559 "cannot create '%s'"), path);
3561 /* validate the path, taking care to note the extended error message */
3562 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3563 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3565 /* validate parents exist */
3566 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3567 return (-1);
3570 * The failure modes when creating a dataset of a different type over
3571 * one that already exists is a little strange. In particular, if you
3572 * try to create a dataset on top of an existing dataset, the ioctl()
3573 * will return ENOENT, not EEXIST. To prevent this from happening, we
3574 * first try to see if the dataset exists.
3576 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3578 "dataset already exists"));
3579 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3582 if (type == ZFS_TYPE_VOLUME)
3583 ost = LZC_DATSET_TYPE_ZVOL;
3584 else
3585 ost = LZC_DATSET_TYPE_ZFS;
3587 /* open zpool handle for prop validation */
3588 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3589 (void) strlcpy(pool_path, path, sizeof (pool_path));
3591 /* truncate pool_path at first slash */
3592 char *p = strchr(pool_path, '/');
3593 if (p != NULL)
3594 *p = '\0';
3596 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3597 return (-1);
3599 if (props && (props = zfs_valid_proplist(hdl, type, props,
3600 zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3601 zpool_close(zpool_handle);
3602 return (-1);
3604 zpool_close(zpool_handle);
3606 if (type == ZFS_TYPE_VOLUME) {
3608 * If we are creating a volume, the size and block size must
3609 * satisfy a few restraints. First, the blocksize must be a
3610 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3611 * volsize must be a multiple of the block size, and cannot be
3612 * zero.
3614 if (props == NULL || nvlist_lookup_uint64(props,
3615 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3616 nvlist_free(props);
3617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3618 "missing volume size"));
3619 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3622 if ((ret = nvlist_lookup_uint64(props,
3623 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3624 &blocksize)) != 0) {
3625 if (ret == ENOENT) {
3626 blocksize = zfs_prop_default_numeric(
3627 ZFS_PROP_VOLBLOCKSIZE);
3628 } else {
3629 nvlist_free(props);
3630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3631 "missing volume block size"));
3632 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3636 if (size == 0) {
3637 nvlist_free(props);
3638 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3639 "volume size cannot be zero"));
3640 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3643 if (size % blocksize != 0) {
3644 nvlist_free(props);
3645 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3646 "volume size must be a multiple of volume block "
3647 "size"));
3648 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3652 (void) parent_name(path, parent, sizeof (parent));
3653 if (zfs_crypto_create(hdl, parent, props, NULL, &wkeydata,
3654 &wkeylen) != 0) {
3655 nvlist_free(props);
3656 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3659 /* create the dataset */
3660 ret = lzc_create(path, ost, props, wkeydata, wkeylen);
3661 nvlist_free(props);
3662 if (wkeydata != NULL)
3663 free(wkeydata);
3665 /* check for failure */
3666 if (ret != 0) {
3667 switch (errno) {
3668 case ENOENT:
3669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3670 "no such parent '%s'"), parent);
3671 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3673 case EINVAL:
3674 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3675 "parent '%s' is not a filesystem"), parent);
3676 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3678 case ENOTSUP:
3679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3680 "pool must be upgraded to set this "
3681 "property or value"));
3682 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3684 case EACCES:
3685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3686 "encryption root's key is not loaded "
3687 "or provided"));
3688 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3690 case ERANGE:
3691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3692 "invalid property value(s) specified"));
3693 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3694 #ifdef _ILP32
3695 case EOVERFLOW:
3697 * This platform can't address a volume this big.
3699 if (type == ZFS_TYPE_VOLUME)
3700 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3701 errbuf));
3702 #endif
3703 /* FALLTHROUGH */
3704 default:
3705 return (zfs_standard_error(hdl, errno, errbuf));
3709 return (0);
3713 * Destroys the given dataset. The caller must make sure that the filesystem
3714 * isn't mounted, and that there are no active dependents. If the file system
3715 * does not exist this function does nothing.
3718 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3720 zfs_cmd_t zc = {"\0"};
3722 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3723 nvlist_t *nv = fnvlist_alloc();
3724 fnvlist_add_boolean(nv, zhp->zfs_name);
3725 int error = lzc_destroy_bookmarks(nv, NULL);
3726 fnvlist_free(nv);
3727 if (error != 0) {
3728 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3729 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3730 zhp->zfs_name));
3732 return (0);
3735 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3737 if (ZFS_IS_VOLUME(zhp)) {
3738 zc.zc_objset_type = DMU_OST_ZVOL;
3739 } else {
3740 zc.zc_objset_type = DMU_OST_ZFS;
3743 zc.zc_defer_destroy = defer;
3744 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3745 errno != ENOENT) {
3746 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3747 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3748 zhp->zfs_name));
3751 remove_mountpoint(zhp);
3753 return (0);
3756 struct destroydata {
3757 nvlist_t *nvl;
3758 const char *snapname;
3761 static int
3762 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3764 struct destroydata *dd = arg;
3765 char name[ZFS_MAX_DATASET_NAME_LEN];
3766 int rv = 0;
3768 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
3769 dd->snapname) >= sizeof (name))
3770 return (EINVAL);
3772 if (lzc_exists(name))
3773 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3775 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3776 zfs_close(zhp);
3777 return (rv);
3781 * Destroys all snapshots with the given name in zhp & descendants.
3784 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3786 int ret;
3787 struct destroydata dd = { 0 };
3789 dd.snapname = snapname;
3790 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3791 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3793 if (nvlist_empty(dd.nvl)) {
3794 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3795 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3796 zhp->zfs_name, snapname);
3797 } else {
3798 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3800 nvlist_free(dd.nvl);
3801 return (ret);
3805 * Destroys all the snapshots named in the nvlist.
3808 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3810 int ret;
3811 nvlist_t *errlist = NULL;
3812 nvpair_t *pair;
3814 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3816 if (ret == 0) {
3817 nvlist_free(errlist);
3818 return (0);
3821 if (nvlist_empty(errlist)) {
3822 char errbuf[1024];
3823 (void) snprintf(errbuf, sizeof (errbuf),
3824 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3826 ret = zfs_standard_error(hdl, ret, errbuf);
3828 for (pair = nvlist_next_nvpair(errlist, NULL);
3829 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3830 char errbuf[1024];
3831 (void) snprintf(errbuf, sizeof (errbuf),
3832 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3833 nvpair_name(pair));
3835 switch (fnvpair_value_int32(pair)) {
3836 case EEXIST:
3837 zfs_error_aux(hdl,
3838 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3839 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3840 break;
3841 default:
3842 ret = zfs_standard_error(hdl, errno, errbuf);
3843 break;
3847 nvlist_free(errlist);
3848 return (ret);
3852 * Clones the given dataset. The target must be of the same type as the source.
3855 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3857 char parent[ZFS_MAX_DATASET_NAME_LEN];
3858 int ret;
3859 char errbuf[1024];
3860 libzfs_handle_t *hdl = zhp->zfs_hdl;
3861 uint64_t zoned;
3863 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3865 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3866 "cannot create '%s'"), target);
3868 /* validate the target/clone name */
3869 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3870 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3872 /* validate parents exist */
3873 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3874 return (-1);
3876 (void) parent_name(target, parent, sizeof (parent));
3878 /* do the clone */
3880 if (props) {
3881 zfs_type_t type;
3882 if (ZFS_IS_VOLUME(zhp)) {
3883 type = ZFS_TYPE_VOLUME;
3884 } else {
3885 type = ZFS_TYPE_FILESYSTEM;
3887 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3888 zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
3889 return (-1);
3892 if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
3893 nvlist_free(props);
3894 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3897 ret = lzc_clone(target, zhp->zfs_name, props);
3898 nvlist_free(props);
3900 if (ret != 0) {
3901 switch (errno) {
3903 case ENOENT:
3905 * The parent doesn't exist. We should have caught this
3906 * above, but there may a race condition that has since
3907 * destroyed the parent.
3909 * At this point, we don't know whether it's the source
3910 * that doesn't exist anymore, or whether the target
3911 * dataset doesn't exist.
3913 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3914 "no such parent '%s'"), parent);
3915 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3917 case EXDEV:
3918 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3919 "source and target pools differ"));
3920 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3921 errbuf));
3923 default:
3924 return (zfs_standard_error(zhp->zfs_hdl, errno,
3925 errbuf));
3929 return (ret);
3933 * Promotes the given clone fs to be the clone parent.
3936 zfs_promote(zfs_handle_t *zhp)
3938 libzfs_handle_t *hdl = zhp->zfs_hdl;
3939 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3940 int ret;
3941 char errbuf[1024];
3943 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3944 "cannot promote '%s'"), zhp->zfs_name);
3946 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3948 "snapshots can not be promoted"));
3949 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3952 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3954 "not a cloned filesystem"));
3955 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3958 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3959 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3961 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3963 if (ret != 0) {
3964 switch (ret) {
3965 case EEXIST:
3966 /* There is a conflicting snapshot name. */
3967 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3968 "conflicting snapshot '%s' from parent '%s'"),
3969 snapname, zhp->zfs_dmustats.dds_origin);
3970 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3972 default:
3973 return (zfs_standard_error(hdl, ret, errbuf));
3976 return (ret);
3979 typedef struct snapdata {
3980 nvlist_t *sd_nvl;
3981 const char *sd_snapname;
3982 } snapdata_t;
3984 static int
3985 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3987 snapdata_t *sd = arg;
3988 char name[ZFS_MAX_DATASET_NAME_LEN];
3989 int rv = 0;
3991 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3992 if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp),
3993 sd->sd_snapname) >= sizeof (name))
3994 return (EINVAL);
3996 fnvlist_add_boolean(sd->sd_nvl, name);
3998 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
4000 zfs_close(zhp);
4002 return (rv);
4006 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4007 * created.
4010 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4012 int ret;
4013 char errbuf[1024];
4014 nvpair_t *elem;
4015 nvlist_t *errors;
4016 zpool_handle_t *zpool_hdl;
4017 char pool[ZFS_MAX_DATASET_NAME_LEN];
4019 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4020 "cannot create snapshots "));
4022 elem = NULL;
4023 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
4024 const char *snapname = nvpair_name(elem);
4026 /* validate the target name */
4027 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
4028 B_TRUE)) {
4029 (void) snprintf(errbuf, sizeof (errbuf),
4030 dgettext(TEXT_DOMAIN,
4031 "cannot create snapshot '%s'"), snapname);
4032 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4037 * get pool handle for prop validation. assumes all snaps are in the
4038 * same pool, as does lzc_snapshot (below).
4040 elem = nvlist_next_nvpair(snaps, NULL);
4041 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4042 pool[strcspn(pool, "/@")] = '\0';
4043 zpool_hdl = zpool_open(hdl, pool);
4044 if (zpool_hdl == NULL)
4045 return (-1);
4047 if (props != NULL &&
4048 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4049 props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4050 zpool_close(zpool_hdl);
4051 return (-1);
4053 zpool_close(zpool_hdl);
4055 ret = lzc_snapshot(snaps, props, &errors);
4057 if (ret != 0) {
4058 boolean_t printed = B_FALSE;
4059 for (elem = nvlist_next_nvpair(errors, NULL);
4060 elem != NULL;
4061 elem = nvlist_next_nvpair(errors, elem)) {
4062 (void) snprintf(errbuf, sizeof (errbuf),
4063 dgettext(TEXT_DOMAIN,
4064 "cannot create snapshot '%s'"), nvpair_name(elem));
4065 (void) zfs_standard_error(hdl,
4066 fnvpair_value_int32(elem), errbuf);
4067 printed = B_TRUE;
4069 if (!printed) {
4070 switch (ret) {
4071 case EXDEV:
4072 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4073 "multiple snapshots of same "
4074 "fs not allowed"));
4075 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4077 break;
4078 default:
4079 (void) zfs_standard_error(hdl, ret, errbuf);
4084 nvlist_free(props);
4085 nvlist_free(errors);
4086 return (ret);
4090 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4091 nvlist_t *props)
4093 int ret;
4094 snapdata_t sd = { 0 };
4095 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4096 char *cp;
4097 zfs_handle_t *zhp;
4098 char errbuf[1024];
4100 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4101 "cannot snapshot %s"), path);
4103 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4104 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4106 (void) strlcpy(fsname, path, sizeof (fsname));
4107 cp = strchr(fsname, '@');
4108 *cp = '\0';
4109 sd.sd_snapname = cp + 1;
4111 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4112 ZFS_TYPE_VOLUME)) == NULL) {
4113 return (-1);
4116 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
4117 if (recursive) {
4118 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4119 } else {
4120 fnvlist_add_boolean(sd.sd_nvl, path);
4123 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4124 nvlist_free(sd.sd_nvl);
4125 zfs_close(zhp);
4126 return (ret);
4130 * Destroy any more recent snapshots. We invoke this callback on any dependents
4131 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4132 * is a dependent and we should just destroy it without checking the transaction
4133 * group.
4135 typedef struct rollback_data {
4136 const char *cb_target; /* the snapshot */
4137 uint64_t cb_create; /* creation time reference */
4138 boolean_t cb_error;
4139 boolean_t cb_force;
4140 } rollback_data_t;
4142 static int
4143 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4145 rollback_data_t *cbp = data;
4146 prop_changelist_t *clp;
4148 /* We must destroy this clone; first unmount it */
4149 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4150 cbp->cb_force ? MS_FORCE: 0);
4151 if (clp == NULL || changelist_prefix(clp) != 0) {
4152 cbp->cb_error = B_TRUE;
4153 zfs_close(zhp);
4154 return (0);
4156 if (zfs_destroy(zhp, B_FALSE) != 0)
4157 cbp->cb_error = B_TRUE;
4158 else
4159 changelist_remove(clp, zhp->zfs_name);
4160 (void) changelist_postfix(clp);
4161 changelist_free(clp);
4163 zfs_close(zhp);
4164 return (0);
4167 static int
4168 rollback_destroy(zfs_handle_t *zhp, void *data)
4170 rollback_data_t *cbp = data;
4172 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4173 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
4174 rollback_destroy_dependent, cbp);
4176 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4179 zfs_close(zhp);
4180 return (0);
4184 * Given a dataset, rollback to a specific snapshot, discarding any
4185 * data changes since then and making it the active dataset.
4187 * Any snapshots and bookmarks more recent than the target are
4188 * destroyed, along with their dependents (i.e. clones).
4191 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4193 rollback_data_t cb = { 0 };
4194 int err;
4195 boolean_t restore_resv = 0;
4196 uint64_t old_volsize = 0, new_volsize;
4197 zfs_prop_t resv_prop = { 0 };
4199 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4200 zhp->zfs_type == ZFS_TYPE_VOLUME);
4203 * Destroy all recent snapshots and their dependents.
4205 cb.cb_force = force;
4206 cb.cb_target = snap->zfs_name;
4207 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4208 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
4209 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
4211 if (cb.cb_error)
4212 return (-1);
4215 * Now that we have verified that the snapshot is the latest,
4216 * rollback to the given snapshot.
4219 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4220 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4221 return (-1);
4222 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4223 restore_resv =
4224 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4228 * Pass both the filesystem and the wanted snapshot names,
4229 * we would get an error back if the snapshot is destroyed or
4230 * a new snapshot is created before this request is processed.
4232 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4233 if (err != 0) {
4234 char errbuf[1024];
4236 (void) snprintf(errbuf, sizeof (errbuf),
4237 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4238 zhp->zfs_name);
4239 switch (err) {
4240 case EEXIST:
4241 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4242 "there is a snapshot or bookmark more recent "
4243 "than '%s'"), snap->zfs_name);
4244 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4245 break;
4246 case ESRCH:
4247 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4248 "'%s' is not found among snapshots of '%s'"),
4249 snap->zfs_name, zhp->zfs_name);
4250 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4251 break;
4252 case EINVAL:
4253 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4254 break;
4255 default:
4256 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4258 return (err);
4262 * For volumes, if the pre-rollback volsize matched the pre-
4263 * rollback reservation and the volsize has changed then set
4264 * the reservation property to the post-rollback volsize.
4265 * Make a new handle since the rollback closed the dataset.
4267 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4268 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4269 if (restore_resv) {
4270 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4271 if (old_volsize != new_volsize)
4272 err = zfs_prop_set_int(zhp, resv_prop,
4273 new_volsize);
4275 zfs_close(zhp);
4277 return (err);
4281 * Renames the given dataset.
4284 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4285 boolean_t force_unmount)
4287 int ret = 0;
4288 zfs_cmd_t zc = {"\0"};
4289 char *delim;
4290 prop_changelist_t *cl = NULL;
4291 zfs_handle_t *zhrp = NULL;
4292 char *parentname = NULL;
4293 char parent[ZFS_MAX_DATASET_NAME_LEN];
4294 libzfs_handle_t *hdl = zhp->zfs_hdl;
4295 char errbuf[1024];
4297 /* if we have the same exact name, just return success */
4298 if (strcmp(zhp->zfs_name, target) == 0)
4299 return (0);
4301 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4302 "cannot rename to '%s'"), target);
4304 /* make sure source name is valid */
4305 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4306 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4309 * Make sure the target name is valid
4311 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4312 if ((strchr(target, '@') == NULL) ||
4313 *target == '@') {
4315 * Snapshot target name is abbreviated,
4316 * reconstruct full dataset name
4318 (void) strlcpy(parent, zhp->zfs_name,
4319 sizeof (parent));
4320 delim = strchr(parent, '@');
4321 if (strchr(target, '@') == NULL)
4322 *(++delim) = '\0';
4323 else
4324 *delim = '\0';
4325 (void) strlcat(parent, target, sizeof (parent));
4326 target = parent;
4327 } else {
4329 * Make sure we're renaming within the same dataset.
4331 delim = strchr(target, '@');
4332 if (strncmp(zhp->zfs_name, target, delim - target)
4333 != 0 || zhp->zfs_name[delim - target] != '@') {
4334 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4335 "snapshots must be part of same "
4336 "dataset"));
4337 return (zfs_error(hdl, EZFS_CROSSTARGET,
4338 errbuf));
4341 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4342 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4343 } else {
4344 if (recursive) {
4345 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4346 "recursive rename must be a snapshot"));
4347 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4350 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4351 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4353 /* validate parents */
4354 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4355 return (-1);
4357 /* make sure we're in the same pool */
4358 verify((delim = strchr(target, '/')) != NULL);
4359 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4360 zhp->zfs_name[delim - target] != '/') {
4361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4362 "datasets must be within same pool"));
4363 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4366 /* new name cannot be a child of the current dataset name */
4367 if (is_descendant(zhp->zfs_name, target)) {
4368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4369 "New dataset name cannot be a descendant of "
4370 "current dataset name"));
4371 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4375 (void) snprintf(errbuf, sizeof (errbuf),
4376 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4378 if (getzoneid() == GLOBAL_ZONEID &&
4379 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4380 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4381 "dataset is used in a non-global zone"));
4382 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4385 if (recursive) {
4386 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4387 if (parentname == NULL) {
4388 ret = -1;
4389 goto error;
4391 delim = strchr(parentname, '@');
4392 *delim = '\0';
4393 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4394 if (zhrp == NULL) {
4395 ret = -1;
4396 goto error;
4398 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4399 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4400 force_unmount ? MS_FORCE : 0)) == NULL)
4401 return (-1);
4403 if (changelist_haszonedchild(cl)) {
4404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4405 "child dataset with inherited mountpoint is used "
4406 "in a non-global zone"));
4407 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4408 ret = -1;
4409 goto error;
4412 if ((ret = changelist_prefix(cl)) != 0)
4413 goto error;
4416 if (ZFS_IS_VOLUME(zhp))
4417 zc.zc_objset_type = DMU_OST_ZVOL;
4418 else
4419 zc.zc_objset_type = DMU_OST_ZFS;
4421 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4422 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4424 zc.zc_cookie = recursive;
4426 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4428 * if it was recursive, the one that actually failed will
4429 * be in zc.zc_name
4431 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4432 "cannot rename '%s'"), zc.zc_name);
4434 if (recursive && errno == EEXIST) {
4435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4436 "a child dataset already has a snapshot "
4437 "with the new name"));
4438 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4439 } else if (errno == EACCES) {
4440 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) ==
4441 ZIO_CRYPT_OFF) {
4442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4443 "cannot rename an unencrypted dataset to "
4444 "be a decendent of an encrypted one"));
4445 } else {
4446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4447 "cannot move encryption child outside of "
4448 "its encryption root"));
4450 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4451 } else {
4452 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4456 * On failure, we still want to remount any filesystems that
4457 * were previously mounted, so we don't alter the system state.
4459 if (cl != NULL)
4460 (void) changelist_postfix(cl);
4461 } else {
4462 if (cl != NULL) {
4463 changelist_rename(cl, zfs_get_name(zhp), target);
4464 ret = changelist_postfix(cl);
4468 error:
4469 if (parentname != NULL) {
4470 free(parentname);
4472 if (zhrp != NULL) {
4473 zfs_close(zhrp);
4475 if (cl != NULL) {
4476 changelist_free(cl);
4478 return (ret);
4481 nvlist_t *
4482 zfs_get_all_props(zfs_handle_t *zhp)
4484 return (zhp->zfs_props);
4487 nvlist_t *
4488 zfs_get_recvd_props(zfs_handle_t *zhp)
4490 if (zhp->zfs_recvd_props == NULL)
4491 if (get_recvd_props_ioctl(zhp) != 0)
4492 return (NULL);
4493 return (zhp->zfs_recvd_props);
4496 nvlist_t *
4497 zfs_get_user_props(zfs_handle_t *zhp)
4499 return (zhp->zfs_user_props);
4503 * This function is used by 'zfs list' to determine the exact set of columns to
4504 * display, and their maximum widths. This does two main things:
4506 * - If this is a list of all properties, then expand the list to include
4507 * all native properties, and set a flag so that for each dataset we look
4508 * for new unique user properties and add them to the list.
4510 * - For non fixed-width properties, keep track of the maximum width seen
4511 * so that we can size the column appropriately. If the user has
4512 * requested received property values, we also need to compute the width
4513 * of the RECEIVED column.
4516 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4517 boolean_t literal)
4519 libzfs_handle_t *hdl = zhp->zfs_hdl;
4520 zprop_list_t *entry;
4521 zprop_list_t **last, **start;
4522 nvlist_t *userprops, *propval;
4523 nvpair_t *elem;
4524 char *strval;
4525 char buf[ZFS_MAXPROPLEN];
4527 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4528 return (-1);
4530 userprops = zfs_get_user_props(zhp);
4532 entry = *plp;
4533 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4535 * Go through and add any user properties as necessary. We
4536 * start by incrementing our list pointer to the first
4537 * non-native property.
4539 start = plp;
4540 while (*start != NULL) {
4541 if ((*start)->pl_prop == ZPROP_INVAL)
4542 break;
4543 start = &(*start)->pl_next;
4546 elem = NULL;
4547 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4549 * See if we've already found this property in our list.
4551 for (last = start; *last != NULL;
4552 last = &(*last)->pl_next) {
4553 if (strcmp((*last)->pl_user_prop,
4554 nvpair_name(elem)) == 0)
4555 break;
4558 if (*last == NULL) {
4559 if ((entry = zfs_alloc(hdl,
4560 sizeof (zprop_list_t))) == NULL ||
4561 ((entry->pl_user_prop = zfs_strdup(hdl,
4562 nvpair_name(elem)))) == NULL) {
4563 free(entry);
4564 return (-1);
4567 entry->pl_prop = ZPROP_INVAL;
4568 entry->pl_width = strlen(nvpair_name(elem));
4569 entry->pl_all = B_TRUE;
4570 *last = entry;
4576 * Now go through and check the width of any non-fixed columns
4578 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4579 if (entry->pl_fixed && !literal)
4580 continue;
4582 if (entry->pl_prop != ZPROP_INVAL) {
4583 if (zfs_prop_get(zhp, entry->pl_prop,
4584 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4585 if (strlen(buf) > entry->pl_width)
4586 entry->pl_width = strlen(buf);
4588 if (received && zfs_prop_get_recvd(zhp,
4589 zfs_prop_to_name(entry->pl_prop),
4590 buf, sizeof (buf), literal) == 0)
4591 if (strlen(buf) > entry->pl_recvd_width)
4592 entry->pl_recvd_width = strlen(buf);
4593 } else {
4594 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4595 &propval) == 0) {
4596 verify(nvlist_lookup_string(propval,
4597 ZPROP_VALUE, &strval) == 0);
4598 if (strlen(strval) > entry->pl_width)
4599 entry->pl_width = strlen(strval);
4601 if (received && zfs_prop_get_recvd(zhp,
4602 entry->pl_user_prop,
4603 buf, sizeof (buf), literal) == 0)
4604 if (strlen(buf) > entry->pl_recvd_width)
4605 entry->pl_recvd_width = strlen(buf);
4609 return (0);
4612 void
4613 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4615 nvpair_t *curr;
4616 nvpair_t *next;
4619 * Keep a reference to the props-table against which we prune the
4620 * properties.
4622 zhp->zfs_props_table = props;
4624 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4626 while (curr) {
4627 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4628 next = nvlist_next_nvpair(zhp->zfs_props, curr);
4631 * User properties will result in ZPROP_INVAL, and since we
4632 * only know how to prune standard ZFS properties, we always
4633 * leave these in the list. This can also happen if we
4634 * encounter an unknown DSL property (when running older
4635 * software, for example).
4637 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4638 (void) nvlist_remove(zhp->zfs_props,
4639 nvpair_name(curr), nvpair_type(curr));
4640 curr = next;
4644 static int
4645 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4646 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4648 zfs_cmd_t zc = {"\0"};
4649 nvlist_t *nvlist = NULL;
4650 int error;
4652 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4653 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4654 zc.zc_cookie = (uint64_t)cmd;
4656 if (cmd == ZFS_SMB_ACL_RENAME) {
4657 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4658 (void) no_memory(hdl);
4659 return (0);
4663 switch (cmd) {
4664 case ZFS_SMB_ACL_ADD:
4665 case ZFS_SMB_ACL_REMOVE:
4666 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4667 break;
4668 case ZFS_SMB_ACL_RENAME:
4669 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4670 resource1) != 0) {
4671 (void) no_memory(hdl);
4672 return (-1);
4674 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4675 resource2) != 0) {
4676 (void) no_memory(hdl);
4677 return (-1);
4679 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4680 nvlist_free(nvlist);
4681 return (-1);
4683 break;
4684 case ZFS_SMB_ACL_PURGE:
4685 break;
4686 default:
4687 return (-1);
4689 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4690 nvlist_free(nvlist);
4691 return (error);
4695 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4696 char *path, char *resource)
4698 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4699 resource, NULL));
4703 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4704 char *path, char *resource)
4706 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4707 resource, NULL));
4711 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4713 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4714 NULL, NULL));
4718 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4719 char *oldname, char *newname)
4721 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4722 oldname, newname));
4726 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4727 zfs_userspace_cb_t func, void *arg)
4729 zfs_cmd_t zc = {"\0"};
4730 zfs_useracct_t buf[100];
4731 libzfs_handle_t *hdl = zhp->zfs_hdl;
4732 int ret;
4734 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4736 zc.zc_objset_type = type;
4737 zc.zc_nvlist_dst = (uintptr_t)buf;
4739 for (;;) {
4740 zfs_useracct_t *zua = buf;
4742 zc.zc_nvlist_dst_size = sizeof (buf);
4743 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4744 char errbuf[1024];
4746 if ((errno == ENOTSUP &&
4747 (type == ZFS_PROP_USEROBJUSED ||
4748 type == ZFS_PROP_GROUPOBJUSED ||
4749 type == ZFS_PROP_USEROBJQUOTA ||
4750 type == ZFS_PROP_GROUPOBJQUOTA ||
4751 type == ZFS_PROP_PROJECTOBJUSED ||
4752 type == ZFS_PROP_PROJECTOBJQUOTA ||
4753 type == ZFS_PROP_PROJECTUSED ||
4754 type == ZFS_PROP_PROJECTQUOTA)))
4755 break;
4757 (void) snprintf(errbuf, sizeof (errbuf),
4758 dgettext(TEXT_DOMAIN,
4759 "cannot get used/quota for %s"), zc.zc_name);
4760 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4762 if (zc.zc_nvlist_dst_size == 0)
4763 break;
4765 while (zc.zc_nvlist_dst_size > 0) {
4766 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4767 zua->zu_space)) != 0)
4768 return (ret);
4769 zua++;
4770 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4774 return (0);
4777 struct holdarg {
4778 nvlist_t *nvl;
4779 const char *snapname;
4780 const char *tag;
4781 boolean_t recursive;
4782 int error;
4785 static int
4786 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4788 struct holdarg *ha = arg;
4789 char name[ZFS_MAX_DATASET_NAME_LEN];
4790 int rv = 0;
4792 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
4793 ha->snapname) >= sizeof (name))
4794 return (EINVAL);
4796 if (lzc_exists(name))
4797 fnvlist_add_string(ha->nvl, name, ha->tag);
4799 if (ha->recursive)
4800 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4801 zfs_close(zhp);
4802 return (rv);
4806 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4807 boolean_t recursive, int cleanup_fd)
4809 int ret;
4810 struct holdarg ha;
4812 ha.nvl = fnvlist_alloc();
4813 ha.snapname = snapname;
4814 ha.tag = tag;
4815 ha.recursive = recursive;
4816 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4818 if (nvlist_empty(ha.nvl)) {
4819 char errbuf[1024];
4821 fnvlist_free(ha.nvl);
4822 ret = ENOENT;
4823 (void) snprintf(errbuf, sizeof (errbuf),
4824 dgettext(TEXT_DOMAIN,
4825 "cannot hold snapshot '%s@%s'"),
4826 zhp->zfs_name, snapname);
4827 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4828 return (ret);
4831 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4832 fnvlist_free(ha.nvl);
4834 return (ret);
4838 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4840 int ret;
4841 nvlist_t *errors;
4842 libzfs_handle_t *hdl = zhp->zfs_hdl;
4843 char errbuf[1024];
4844 nvpair_t *elem;
4846 errors = NULL;
4847 ret = lzc_hold(holds, cleanup_fd, &errors);
4849 if (ret == 0) {
4850 /* There may be errors even in the success case. */
4851 fnvlist_free(errors);
4852 return (0);
4855 if (nvlist_empty(errors)) {
4856 /* no hold-specific errors */
4857 (void) snprintf(errbuf, sizeof (errbuf),
4858 dgettext(TEXT_DOMAIN, "cannot hold"));
4859 switch (ret) {
4860 case ENOTSUP:
4861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4862 "pool must be upgraded"));
4863 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4864 break;
4865 case EINVAL:
4866 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4867 break;
4868 default:
4869 (void) zfs_standard_error(hdl, ret, errbuf);
4873 for (elem = nvlist_next_nvpair(errors, NULL);
4874 elem != NULL;
4875 elem = nvlist_next_nvpair(errors, elem)) {
4876 (void) snprintf(errbuf, sizeof (errbuf),
4877 dgettext(TEXT_DOMAIN,
4878 "cannot hold snapshot '%s'"), nvpair_name(elem));
4879 switch (fnvpair_value_int32(elem)) {
4880 case E2BIG:
4882 * Temporary tags wind up having the ds object id
4883 * prepended. So even if we passed the length check
4884 * above, it's still possible for the tag to wind
4885 * up being slightly too long.
4887 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4888 break;
4889 case EINVAL:
4890 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4891 break;
4892 case EEXIST:
4893 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4894 break;
4895 default:
4896 (void) zfs_standard_error(hdl,
4897 fnvpair_value_int32(elem), errbuf);
4901 fnvlist_free(errors);
4902 return (ret);
4905 static int
4906 zfs_release_one(zfs_handle_t *zhp, void *arg)
4908 struct holdarg *ha = arg;
4909 char name[ZFS_MAX_DATASET_NAME_LEN];
4910 int rv = 0;
4911 nvlist_t *existing_holds;
4913 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
4914 ha->snapname) >= sizeof (name)) {
4915 ha->error = EINVAL;
4916 rv = EINVAL;
4919 if (lzc_get_holds(name, &existing_holds) != 0) {
4920 ha->error = ENOENT;
4921 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4922 ha->error = ESRCH;
4923 } else {
4924 nvlist_t *torelease = fnvlist_alloc();
4925 fnvlist_add_boolean(torelease, ha->tag);
4926 fnvlist_add_nvlist(ha->nvl, name, torelease);
4927 fnvlist_free(torelease);
4930 if (ha->recursive)
4931 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4932 zfs_close(zhp);
4933 return (rv);
4937 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4938 boolean_t recursive)
4940 int ret;
4941 struct holdarg ha;
4942 nvlist_t *errors = NULL;
4943 nvpair_t *elem;
4944 libzfs_handle_t *hdl = zhp->zfs_hdl;
4945 char errbuf[1024];
4947 ha.nvl = fnvlist_alloc();
4948 ha.snapname = snapname;
4949 ha.tag = tag;
4950 ha.recursive = recursive;
4951 ha.error = 0;
4952 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4954 if (nvlist_empty(ha.nvl)) {
4955 fnvlist_free(ha.nvl);
4956 ret = ha.error;
4957 (void) snprintf(errbuf, sizeof (errbuf),
4958 dgettext(TEXT_DOMAIN,
4959 "cannot release hold from snapshot '%s@%s'"),
4960 zhp->zfs_name, snapname);
4961 if (ret == ESRCH) {
4962 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4963 } else {
4964 (void) zfs_standard_error(hdl, ret, errbuf);
4966 return (ret);
4969 ret = lzc_release(ha.nvl, &errors);
4970 fnvlist_free(ha.nvl);
4972 if (ret == 0) {
4973 /* There may be errors even in the success case. */
4974 fnvlist_free(errors);
4975 return (0);
4978 if (nvlist_empty(errors)) {
4979 /* no hold-specific errors */
4980 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4981 "cannot release"));
4982 switch (errno) {
4983 case ENOTSUP:
4984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4985 "pool must be upgraded"));
4986 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4987 break;
4988 default:
4989 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4993 for (elem = nvlist_next_nvpair(errors, NULL);
4994 elem != NULL;
4995 elem = nvlist_next_nvpair(errors, elem)) {
4996 (void) snprintf(errbuf, sizeof (errbuf),
4997 dgettext(TEXT_DOMAIN,
4998 "cannot release hold from snapshot '%s'"),
4999 nvpair_name(elem));
5000 switch (fnvpair_value_int32(elem)) {
5001 case ESRCH:
5002 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5003 break;
5004 case EINVAL:
5005 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5006 break;
5007 default:
5008 (void) zfs_standard_error_fmt(hdl,
5009 fnvpair_value_int32(elem), errbuf);
5013 fnvlist_free(errors);
5014 return (ret);
5018 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5020 zfs_cmd_t zc = {"\0"};
5021 libzfs_handle_t *hdl = zhp->zfs_hdl;
5022 int nvsz = 2048;
5023 void *nvbuf;
5024 int err = 0;
5025 char errbuf[1024];
5027 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5028 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5030 tryagain:
5032 nvbuf = malloc(nvsz);
5033 if (nvbuf == NULL) {
5034 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
5035 goto out;
5038 zc.zc_nvlist_dst_size = nvsz;
5039 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5041 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5043 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
5044 (void) snprintf(errbuf, sizeof (errbuf),
5045 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5046 zc.zc_name);
5047 switch (errno) {
5048 case ENOMEM:
5049 free(nvbuf);
5050 nvsz = zc.zc_nvlist_dst_size;
5051 goto tryagain;
5053 case ENOTSUP:
5054 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5055 "pool must be upgraded"));
5056 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5057 break;
5058 case EINVAL:
5059 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5060 break;
5061 case ENOENT:
5062 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5063 break;
5064 default:
5065 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5066 break;
5068 } else {
5069 /* success */
5070 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5071 if (rc) {
5072 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
5073 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5074 zc.zc_name);
5075 err = zfs_standard_error_fmt(hdl, rc, errbuf);
5079 free(nvbuf);
5080 out:
5081 return (err);
5085 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5087 zfs_cmd_t zc = {"\0"};
5088 libzfs_handle_t *hdl = zhp->zfs_hdl;
5089 char *nvbuf;
5090 char errbuf[1024];
5091 size_t nvsz;
5092 int err;
5094 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5095 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5097 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5098 assert(err == 0);
5100 nvbuf = malloc(nvsz);
5102 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5103 assert(err == 0);
5105 zc.zc_nvlist_src_size = nvsz;
5106 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5107 zc.zc_perm_action = un;
5109 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5111 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5112 (void) snprintf(errbuf, sizeof (errbuf),
5113 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5114 zc.zc_name);
5115 switch (errno) {
5116 case ENOTSUP:
5117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5118 "pool must be upgraded"));
5119 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5120 break;
5121 case EINVAL:
5122 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5123 break;
5124 case ENOENT:
5125 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5126 break;
5127 default:
5128 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5129 break;
5133 free(nvbuf);
5135 return (err);
5139 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5141 int err;
5142 char errbuf[1024];
5144 err = lzc_get_holds(zhp->zfs_name, nvl);
5146 if (err != 0) {
5147 libzfs_handle_t *hdl = zhp->zfs_hdl;
5149 (void) snprintf(errbuf, sizeof (errbuf),
5150 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5151 zhp->zfs_name);
5152 switch (err) {
5153 case ENOTSUP:
5154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5155 "pool must be upgraded"));
5156 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5157 break;
5158 case EINVAL:
5159 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5160 break;
5161 case ENOENT:
5162 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5163 break;
5164 default:
5165 err = zfs_standard_error_fmt(hdl, errno, errbuf);
5166 break;
5170 return (err);
5174 * Convert the zvol's volume size to an appropriate reservation.
5175 * Note: If this routine is updated, it is necessary to update the ZFS test
5176 * suite's shell version in reservation.kshlib.
5178 uint64_t
5179 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
5181 uint64_t numdb;
5182 uint64_t nblocks, volblocksize;
5183 int ncopies;
5184 char *strval;
5186 if (nvlist_lookup_string(props,
5187 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5188 ncopies = atoi(strval);
5189 else
5190 ncopies = 1;
5191 if (nvlist_lookup_uint64(props,
5192 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5193 &volblocksize) != 0)
5194 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5195 nblocks = volsize/volblocksize;
5196 /* start with metadnode L0-L6 */
5197 numdb = 7;
5198 /* calculate number of indirects */
5199 while (nblocks > 1) {
5200 nblocks += DNODES_PER_LEVEL - 1;
5201 nblocks /= DNODES_PER_LEVEL;
5202 numdb += nblocks;
5204 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5205 volsize *= ncopies;
5207 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5208 * compressed, but in practice they compress down to about
5209 * 1100 bytes
5211 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5212 volsize += numdb;
5213 return (volsize);