Merge commit '5cabbc6b49070407fb9610cfe73d4c0e0dea3e77' into merges
[unleashed.git] / usr / src / lib / libzfs / common / libzfs_dataset.c
blobb83d8378395bab9027a2f2a6ea5cf3e1fb0d9ec2
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, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2017 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017 RackTop Systems.
36 #include <ctype.h>
37 #include <errno.h>
38 #include <libintl.h>
39 #include <math.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <strings.h>
43 #include <unistd.h>
44 #include <stddef.h>
45 #include <zone.h>
46 #include <fcntl.h>
47 #include <sys/mntent.h>
48 #include <sys/mount.h>
49 #include <priv.h>
50 #include <pwd.h>
51 #include <grp.h>
52 #include <stddef.h>
53 #include <ucred.h>
54 #include <idmap.h>
55 #include <aclutils.h>
56 #include <directory.h>
58 #include <sys/dnode.h>
59 #include <sys/spa.h>
60 #include <sys/zap.h>
61 #include <libzfs.h>
63 #include "zfs_namecheck.h"
64 #include "zfs_prop.h"
65 #include "libzfs_impl.h"
66 #include "zfs_deleg.h"
68 static int userquota_propname_decode(const char *propname, boolean_t zoned,
69 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
72 * Given a single type (not a mask of types), return the type in a human
73 * readable form.
75 const char *
76 zfs_type_to_name(zfs_type_t type)
78 switch (type) {
79 case ZFS_TYPE_FILESYSTEM:
80 return (dgettext(TEXT_DOMAIN, "filesystem"));
81 case ZFS_TYPE_SNAPSHOT:
82 return (dgettext(TEXT_DOMAIN, "snapshot"));
83 case ZFS_TYPE_VOLUME:
84 return (dgettext(TEXT_DOMAIN, "volume"));
85 case ZFS_TYPE_POOL:
86 return (dgettext(TEXT_DOMAIN, "pool"));
87 case ZFS_TYPE_BOOKMARK:
88 return (dgettext(TEXT_DOMAIN, "bookmark"));
89 default:
90 assert(!"unhandled zfs_type_t");
93 return (NULL);
97 * Validate a ZFS path. This is used even before trying to open the dataset, to
98 * provide a more meaningful error message. We call zfs_error_aux() to
99 * explain exactly why the name was not valid.
102 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
103 boolean_t modifying)
105 namecheck_err_t why;
106 char what;
108 if (entity_namecheck(path, &why, &what) != 0) {
109 if (hdl != NULL) {
110 switch (why) {
111 case NAME_ERR_TOOLONG:
112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
113 "name is too long"));
114 break;
116 case NAME_ERR_LEADING_SLASH:
117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 "leading slash in name"));
119 break;
121 case NAME_ERR_EMPTY_COMPONENT:
122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
123 "empty component in name"));
124 break;
126 case NAME_ERR_TRAILING_SLASH:
127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
128 "trailing slash in name"));
129 break;
131 case NAME_ERR_INVALCHAR:
132 zfs_error_aux(hdl,
133 dgettext(TEXT_DOMAIN, "invalid character "
134 "'%c' in name"), what);
135 break;
137 case NAME_ERR_MULTIPLE_DELIMITERS:
138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139 "multiple '@' and/or '#' delimiters in "
140 "name"));
141 break;
143 case NAME_ERR_NOLETTER:
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "pool doesn't begin with a letter"));
146 break;
148 case NAME_ERR_RESERVED:
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "name is reserved"));
151 break;
153 case NAME_ERR_DISKLIKE:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "reserved disk name"));
156 break;
158 default:
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "(%d) not defined"), why);
161 break;
165 return (0);
168 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
169 if (hdl != NULL)
170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 "snapshot delimiter '@' is not expected here"));
172 return (0);
175 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
176 if (hdl != NULL)
177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
178 "missing '@' delimiter in snapshot name"));
179 return (0);
182 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
183 if (hdl != NULL)
184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
185 "bookmark delimiter '#' is not expected here"));
186 return (0);
189 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
190 if (hdl != NULL)
191 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
192 "missing '#' delimiter in bookmark name"));
193 return (0);
196 if (modifying && strchr(path, '%') != NULL) {
197 if (hdl != NULL)
198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
199 "invalid character %c in name"), '%');
200 return (0);
203 return (-1);
207 zfs_name_valid(const char *name, zfs_type_t type)
209 if (type == ZFS_TYPE_POOL)
210 return (zpool_name_valid(NULL, B_FALSE, name));
211 return (zfs_validate_name(NULL, name, type, B_FALSE));
215 * This function takes the raw DSL properties, and filters out the user-defined
216 * properties into a separate nvlist.
218 static nvlist_t *
219 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
221 libzfs_handle_t *hdl = zhp->zfs_hdl;
222 nvpair_t *elem;
223 nvlist_t *propval;
224 nvlist_t *nvl;
226 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
227 (void) no_memory(hdl);
228 return (NULL);
231 elem = NULL;
232 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
233 if (!zfs_prop_user(nvpair_name(elem)))
234 continue;
236 verify(nvpair_value_nvlist(elem, &propval) == 0);
237 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
238 nvlist_free(nvl);
239 (void) no_memory(hdl);
240 return (NULL);
244 return (nvl);
247 static zpool_handle_t *
248 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
250 libzfs_handle_t *hdl = zhp->zfs_hdl;
251 zpool_handle_t *zph;
253 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
254 if (hdl->libzfs_pool_handles != NULL)
255 zph->zpool_next = hdl->libzfs_pool_handles;
256 hdl->libzfs_pool_handles = zph;
258 return (zph);
261 static zpool_handle_t *
262 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
264 libzfs_handle_t *hdl = zhp->zfs_hdl;
265 zpool_handle_t *zph = hdl->libzfs_pool_handles;
267 while ((zph != NULL) &&
268 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
269 zph = zph->zpool_next;
270 return (zph);
274 * Returns a handle to the pool that contains the provided dataset.
275 * If a handle to that pool already exists then that handle is returned.
276 * Otherwise, a new handle is created and added to the list of handles.
278 static zpool_handle_t *
279 zpool_handle(zfs_handle_t *zhp)
281 char *pool_name;
282 int len;
283 zpool_handle_t *zph;
285 len = strcspn(zhp->zfs_name, "/@#") + 1;
286 pool_name = zfs_alloc(zhp->zfs_hdl, len);
287 (void) strlcpy(pool_name, zhp->zfs_name, len);
289 zph = zpool_find_handle(zhp, pool_name, len);
290 if (zph == NULL)
291 zph = zpool_add_handle(zhp, pool_name);
293 free(pool_name);
294 return (zph);
297 void
298 zpool_free_handles(libzfs_handle_t *hdl)
300 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
302 while (zph != NULL) {
303 next = zph->zpool_next;
304 zpool_close(zph);
305 zph = next;
307 hdl->libzfs_pool_handles = NULL;
311 * Utility function to gather stats (objset and zpl) for the given object.
313 static int
314 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
316 libzfs_handle_t *hdl = zhp->zfs_hdl;
318 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
320 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
321 if (errno == ENOMEM) {
322 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
323 return (-1);
325 } else {
326 return (-1);
329 return (0);
333 * Utility function to get the received properties of the given object.
335 static int
336 get_recvd_props_ioctl(zfs_handle_t *zhp)
338 libzfs_handle_t *hdl = zhp->zfs_hdl;
339 nvlist_t *recvdprops;
340 zfs_cmd_t zc = { 0 };
341 int err;
343 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
344 return (-1);
346 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
348 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
349 if (errno == ENOMEM) {
350 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
351 return (-1);
353 } else {
354 zcmd_free_nvlists(&zc);
355 return (-1);
359 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
360 zcmd_free_nvlists(&zc);
361 if (err != 0)
362 return (-1);
364 nvlist_free(zhp->zfs_recvd_props);
365 zhp->zfs_recvd_props = recvdprops;
367 return (0);
370 static int
371 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
373 nvlist_t *allprops, *userprops;
375 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
377 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
378 return (-1);
382 * XXX Why do we store the user props separately, in addition to
383 * storing them in zfs_props?
385 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
386 nvlist_free(allprops);
387 return (-1);
390 nvlist_free(zhp->zfs_props);
391 nvlist_free(zhp->zfs_user_props);
393 zhp->zfs_props = allprops;
394 zhp->zfs_user_props = userprops;
396 return (0);
399 static int
400 get_stats(zfs_handle_t *zhp)
402 int rc = 0;
403 zfs_cmd_t zc = { 0 };
405 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
406 return (-1);
407 if (get_stats_ioctl(zhp, &zc) != 0)
408 rc = -1;
409 else if (put_stats_zhdl(zhp, &zc) != 0)
410 rc = -1;
411 zcmd_free_nvlists(&zc);
412 return (rc);
416 * Refresh the properties currently stored in the handle.
418 void
419 zfs_refresh_properties(zfs_handle_t *zhp)
421 (void) get_stats(zhp);
425 * Makes a handle from the given dataset name. Used by zfs_open() and
426 * zfs_iter_* to create child handles on the fly.
428 static int
429 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
431 if (put_stats_zhdl(zhp, zc) != 0)
432 return (-1);
435 * We've managed to open the dataset and gather statistics. Determine
436 * the high-level type.
438 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
439 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
440 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
441 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
442 else
443 abort();
445 if (zhp->zfs_dmustats.dds_is_snapshot)
446 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
447 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
448 zhp->zfs_type = ZFS_TYPE_VOLUME;
449 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
450 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
451 else
452 abort(); /* we should never see any other types */
454 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
455 return (-1);
457 return (0);
460 zfs_handle_t *
461 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
463 zfs_cmd_t zc = { 0 };
465 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
467 if (zhp == NULL)
468 return (NULL);
470 zhp->zfs_hdl = hdl;
471 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
472 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
473 free(zhp);
474 return (NULL);
476 if (get_stats_ioctl(zhp, &zc) == -1) {
477 zcmd_free_nvlists(&zc);
478 free(zhp);
479 return (NULL);
481 if (make_dataset_handle_common(zhp, &zc) == -1) {
482 free(zhp);
483 zhp = NULL;
485 zcmd_free_nvlists(&zc);
486 return (zhp);
489 zfs_handle_t *
490 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
492 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
494 if (zhp == NULL)
495 return (NULL);
497 zhp->zfs_hdl = hdl;
498 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
499 if (make_dataset_handle_common(zhp, zc) == -1) {
500 free(zhp);
501 return (NULL);
503 return (zhp);
506 zfs_handle_t *
507 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
509 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
511 if (zhp == NULL)
512 return (NULL);
514 zhp->zfs_hdl = pzhp->zfs_hdl;
515 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
516 zhp->zfs_head_type = pzhp->zfs_type;
517 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
518 zhp->zpool_hdl = zpool_handle(zhp);
519 return (zhp);
522 zfs_handle_t *
523 zfs_handle_dup(zfs_handle_t *zhp_orig)
525 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
527 if (zhp == NULL)
528 return (NULL);
530 zhp->zfs_hdl = zhp_orig->zfs_hdl;
531 zhp->zpool_hdl = zhp_orig->zpool_hdl;
532 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
533 sizeof (zhp->zfs_name));
534 zhp->zfs_type = zhp_orig->zfs_type;
535 zhp->zfs_head_type = zhp_orig->zfs_head_type;
536 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
537 if (zhp_orig->zfs_props != NULL) {
538 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
539 (void) no_memory(zhp->zfs_hdl);
540 zfs_close(zhp);
541 return (NULL);
544 if (zhp_orig->zfs_user_props != NULL) {
545 if (nvlist_dup(zhp_orig->zfs_user_props,
546 &zhp->zfs_user_props, 0) != 0) {
547 (void) no_memory(zhp->zfs_hdl);
548 zfs_close(zhp);
549 return (NULL);
552 if (zhp_orig->zfs_recvd_props != NULL) {
553 if (nvlist_dup(zhp_orig->zfs_recvd_props,
554 &zhp->zfs_recvd_props, 0)) {
555 (void) no_memory(zhp->zfs_hdl);
556 zfs_close(zhp);
557 return (NULL);
560 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
561 if (zhp_orig->zfs_mntopts != NULL) {
562 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
563 zhp_orig->zfs_mntopts);
565 zhp->zfs_props_table = zhp_orig->zfs_props_table;
566 return (zhp);
569 boolean_t
570 zfs_bookmark_exists(const char *path)
572 nvlist_t *bmarks;
573 nvlist_t *props;
574 char fsname[ZFS_MAX_DATASET_NAME_LEN];
575 char *bmark_name;
576 char *pound;
577 int err;
578 boolean_t rv;
581 (void) strlcpy(fsname, path, sizeof (fsname));
582 pound = strchr(fsname, '#');
583 if (pound == NULL)
584 return (B_FALSE);
586 *pound = '\0';
587 bmark_name = pound + 1;
588 props = fnvlist_alloc();
589 err = lzc_get_bookmarks(fsname, props, &bmarks);
590 nvlist_free(props);
591 if (err != 0) {
592 nvlist_free(bmarks);
593 return (B_FALSE);
596 rv = nvlist_exists(bmarks, bmark_name);
597 nvlist_free(bmarks);
598 return (rv);
601 zfs_handle_t *
602 make_bookmark_handle(zfs_handle_t *parent, const char *path,
603 nvlist_t *bmark_props)
605 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
607 if (zhp == NULL)
608 return (NULL);
610 /* Fill in the name. */
611 zhp->zfs_hdl = parent->zfs_hdl;
612 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
614 /* Set the property lists. */
615 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
616 free(zhp);
617 return (NULL);
620 /* Set the types. */
621 zhp->zfs_head_type = parent->zfs_head_type;
622 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
624 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
625 nvlist_free(zhp->zfs_props);
626 free(zhp);
627 return (NULL);
630 return (zhp);
633 struct zfs_open_bookmarks_cb_data {
634 const char *path;
635 zfs_handle_t *zhp;
638 static int
639 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
641 struct zfs_open_bookmarks_cb_data *dp = data;
644 * Is it the one we are looking for?
646 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
648 * We found it. Save it and let the caller know we are done.
650 dp->zhp = zhp;
651 return (EEXIST);
655 * Not found. Close the handle and ask for another one.
657 zfs_close(zhp);
658 return (0);
662 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
663 * argument is a mask of acceptable types. The function will print an
664 * appropriate error message and return NULL if it can't be opened.
666 zfs_handle_t *
667 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
669 zfs_handle_t *zhp;
670 char errbuf[1024];
671 char *bookp;
673 (void) snprintf(errbuf, sizeof (errbuf),
674 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
677 * Validate the name before we even try to open it.
679 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
680 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
681 return (NULL);
685 * Bookmarks needs to be handled separately.
687 bookp = strchr(path, '#');
688 if (bookp == NULL) {
690 * Try to get stats for the dataset, which will tell us if it
691 * exists.
693 errno = 0;
694 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
695 (void) zfs_standard_error(hdl, errno, errbuf);
696 return (NULL);
698 } else {
699 char dsname[ZFS_MAX_DATASET_NAME_LEN];
700 zfs_handle_t *pzhp;
701 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
704 * We need to cut out '#' and everything after '#'
705 * to get the parent dataset name only.
707 assert(bookp - path < sizeof (dsname));
708 (void) strncpy(dsname, path, bookp - path);
709 dsname[bookp - path] = '\0';
712 * Create handle for the parent dataset.
714 errno = 0;
715 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
716 (void) zfs_standard_error(hdl, errno, errbuf);
717 return (NULL);
721 * Iterate bookmarks to find the right one.
723 errno = 0;
724 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
725 &cb_data) == 0) && (cb_data.zhp == NULL)) {
726 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
727 zfs_close(pzhp);
728 return (NULL);
730 if (cb_data.zhp == NULL) {
731 (void) zfs_standard_error(hdl, errno, errbuf);
732 zfs_close(pzhp);
733 return (NULL);
735 zhp = cb_data.zhp;
738 * Cleanup.
740 zfs_close(pzhp);
743 if (!(types & zhp->zfs_type)) {
744 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
745 zfs_close(zhp);
746 return (NULL);
749 return (zhp);
753 * Release a ZFS handle. Nothing to do but free the associated memory.
755 void
756 zfs_close(zfs_handle_t *zhp)
758 free(zhp->zfs_mntopts);
759 nvlist_free(zhp->zfs_props);
760 nvlist_free(zhp->zfs_user_props);
761 nvlist_free(zhp->zfs_recvd_props);
762 free(zhp);
765 typedef struct mnttab_node {
766 struct mnttab mtn_mt;
767 avl_node_t mtn_node;
768 } mnttab_node_t;
770 static int
771 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
773 const mnttab_node_t *mtn1 = arg1;
774 const mnttab_node_t *mtn2 = arg2;
775 int rv;
777 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
779 if (rv == 0)
780 return (0);
781 return (rv > 0 ? 1 : -1);
784 void
785 libzfs_mnttab_init(libzfs_handle_t *hdl)
787 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
788 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
789 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
792 void
793 libzfs_mnttab_update(libzfs_handle_t *hdl)
795 struct mnttab entry;
797 rewind(hdl->libzfs_mnttab);
798 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
799 mnttab_node_t *mtn;
801 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
802 continue;
803 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
804 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
805 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
806 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
807 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
808 avl_add(&hdl->libzfs_mnttab_cache, mtn);
812 void
813 libzfs_mnttab_fini(libzfs_handle_t *hdl)
815 void *cookie = NULL;
816 mnttab_node_t *mtn;
818 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
819 != NULL) {
820 free(mtn->mtn_mt.mnt_special);
821 free(mtn->mtn_mt.mnt_mountp);
822 free(mtn->mtn_mt.mnt_fstype);
823 free(mtn->mtn_mt.mnt_mntopts);
824 free(mtn);
826 avl_destroy(&hdl->libzfs_mnttab_cache);
829 void
830 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
832 hdl->libzfs_mnttab_enable = enable;
836 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
837 struct mnttab *entry)
839 mnttab_node_t find;
840 mnttab_node_t *mtn;
842 if (!hdl->libzfs_mnttab_enable) {
843 struct mnttab srch = { 0 };
845 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
846 libzfs_mnttab_fini(hdl);
847 rewind(hdl->libzfs_mnttab);
848 srch.mnt_special = (char *)fsname;
849 srch.mnt_fstype = MNTTYPE_ZFS;
850 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
851 return (0);
852 else
853 return (ENOENT);
856 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
857 libzfs_mnttab_update(hdl);
859 find.mtn_mt.mnt_special = (char *)fsname;
860 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
861 if (mtn) {
862 *entry = mtn->mtn_mt;
863 return (0);
865 return (ENOENT);
868 void
869 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
870 const char *mountp, const char *mntopts)
872 mnttab_node_t *mtn;
874 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
875 return;
876 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
877 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
878 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
879 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
880 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
881 avl_add(&hdl->libzfs_mnttab_cache, mtn);
884 void
885 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
887 mnttab_node_t find;
888 mnttab_node_t *ret;
890 find.mtn_mt.mnt_special = (char *)fsname;
891 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
892 != NULL) {
893 avl_remove(&hdl->libzfs_mnttab_cache, ret);
894 free(ret->mtn_mt.mnt_special);
895 free(ret->mtn_mt.mnt_mountp);
896 free(ret->mtn_mt.mnt_fstype);
897 free(ret->mtn_mt.mnt_mntopts);
898 free(ret);
903 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
905 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
907 if (zpool_handle == NULL)
908 return (-1);
910 *spa_version = zpool_get_prop_int(zpool_handle,
911 ZPOOL_PROP_VERSION, NULL);
912 return (0);
916 * The choice of reservation property depends on the SPA version.
918 static int
919 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
921 int spa_version;
923 if (zfs_spa_version(zhp, &spa_version) < 0)
924 return (-1);
926 if (spa_version >= SPA_VERSION_REFRESERVATION)
927 *resv_prop = ZFS_PROP_REFRESERVATION;
928 else
929 *resv_prop = ZFS_PROP_RESERVATION;
931 return (0);
935 * Given an nvlist of properties to set, validates that they are correct, and
936 * parses any numeric properties (index, boolean, etc) if they are specified as
937 * strings.
939 nvlist_t *
940 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
941 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
942 const char *errbuf)
944 nvpair_t *elem;
945 uint64_t intval;
946 char *strval;
947 zfs_prop_t prop;
948 nvlist_t *ret;
949 int chosen_normal = -1;
950 int chosen_utf = -1;
952 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
953 (void) no_memory(hdl);
954 return (NULL);
958 * Make sure this property is valid and applies to this type.
961 elem = NULL;
962 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
963 const char *propname = nvpair_name(elem);
965 prop = zfs_name_to_prop(propname);
966 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
968 * This is a user property: make sure it's a
969 * string, and that it's less than ZAP_MAXNAMELEN.
971 if (nvpair_type(elem) != DATA_TYPE_STRING) {
972 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
973 "'%s' must be a string"), propname);
974 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
975 goto error;
978 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
980 "property name '%s' is too long"),
981 propname);
982 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
983 goto error;
986 (void) nvpair_value_string(elem, &strval);
987 if (nvlist_add_string(ret, propname, strval) != 0) {
988 (void) no_memory(hdl);
989 goto error;
991 continue;
995 * Currently, only user properties can be modified on
996 * snapshots.
998 if (type == ZFS_TYPE_SNAPSHOT) {
999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1000 "this property can not be modified for snapshots"));
1001 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1002 goto error;
1005 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1006 zfs_userquota_prop_t uqtype;
1007 char newpropname[128];
1008 char domain[128];
1009 uint64_t rid;
1010 uint64_t valary[3];
1012 if (userquota_propname_decode(propname, zoned,
1013 &uqtype, domain, sizeof (domain), &rid) != 0) {
1014 zfs_error_aux(hdl,
1015 dgettext(TEXT_DOMAIN,
1016 "'%s' has an invalid user/group name"),
1017 propname);
1018 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1019 goto error;
1022 if (uqtype != ZFS_PROP_USERQUOTA &&
1023 uqtype != ZFS_PROP_GROUPQUOTA) {
1024 zfs_error_aux(hdl,
1025 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1026 propname);
1027 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1028 errbuf);
1029 goto error;
1032 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1033 (void) nvpair_value_string(elem, &strval);
1034 if (strcmp(strval, "none") == 0) {
1035 intval = 0;
1036 } else if (zfs_nicestrtonum(hdl,
1037 strval, &intval) != 0) {
1038 (void) zfs_error(hdl,
1039 EZFS_BADPROP, errbuf);
1040 goto error;
1042 } else if (nvpair_type(elem) ==
1043 DATA_TYPE_UINT64) {
1044 (void) nvpair_value_uint64(elem, &intval);
1045 if (intval == 0) {
1046 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1047 "use 'none' to disable "
1048 "userquota/groupquota"));
1049 goto error;
1051 } else {
1052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1053 "'%s' must be a number"), propname);
1054 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1055 goto error;
1059 * Encode the prop name as
1060 * userquota@<hex-rid>-domain, to make it easy
1061 * for the kernel to decode.
1063 (void) snprintf(newpropname, sizeof (newpropname),
1064 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1065 (longlong_t)rid, domain);
1066 valary[0] = uqtype;
1067 valary[1] = rid;
1068 valary[2] = intval;
1069 if (nvlist_add_uint64_array(ret, newpropname,
1070 valary, 3) != 0) {
1071 (void) no_memory(hdl);
1072 goto error;
1074 continue;
1075 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1077 "'%s' is readonly"),
1078 propname);
1079 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1080 goto error;
1083 if (prop == ZPROP_INVAL) {
1084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1085 "invalid property '%s'"), propname);
1086 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1087 goto error;
1090 if (!zfs_prop_valid_for_type(prop, type)) {
1091 zfs_error_aux(hdl,
1092 dgettext(TEXT_DOMAIN, "'%s' does not "
1093 "apply to datasets of this type"), propname);
1094 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1095 goto error;
1098 if (zfs_prop_readonly(prop) &&
1099 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1100 zfs_error_aux(hdl,
1101 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1102 propname);
1103 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1104 goto error;
1107 if (zprop_parse_value(hdl, elem, prop, type, ret,
1108 &strval, &intval, errbuf) != 0)
1109 goto error;
1112 * Perform some additional checks for specific properties.
1114 switch (prop) {
1115 case ZFS_PROP_VERSION:
1117 int version;
1119 if (zhp == NULL)
1120 break;
1121 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1122 if (intval < version) {
1123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1124 "Can not downgrade; already at version %u"),
1125 version);
1126 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1127 goto error;
1129 break;
1132 case ZFS_PROP_VOLBLOCKSIZE:
1133 case ZFS_PROP_RECORDSIZE:
1135 int maxbs = SPA_MAXBLOCKSIZE;
1136 if (zpool_hdl != NULL) {
1137 maxbs = zpool_get_prop_int(zpool_hdl,
1138 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1141 * Volumes are limited to a volblocksize of 128KB,
1142 * because they typically service workloads with
1143 * small random writes, which incur a large performance
1144 * penalty with large blocks.
1146 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1147 maxbs = SPA_OLD_MAXBLOCKSIZE;
1149 * The value must be a power of two between
1150 * SPA_MINBLOCKSIZE and maxbs.
1152 if (intval < SPA_MINBLOCKSIZE ||
1153 intval > maxbs || !ISP2(intval)) {
1154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1155 "'%s' must be power of 2 from 512B "
1156 "to %uKB"), propname, maxbs >> 10);
1157 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1158 goto error;
1160 break;
1162 case ZFS_PROP_MOUNTPOINT:
1164 namecheck_err_t why;
1166 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1167 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1168 break;
1170 if (mountpoint_namecheck(strval, &why)) {
1171 switch (why) {
1172 case NAME_ERR_LEADING_SLASH:
1173 zfs_error_aux(hdl,
1174 dgettext(TEXT_DOMAIN,
1175 "'%s' must be an absolute path, "
1176 "'none', or 'legacy'"), propname);
1177 break;
1178 case NAME_ERR_TOOLONG:
1179 zfs_error_aux(hdl,
1180 dgettext(TEXT_DOMAIN,
1181 "component of '%s' is too long"),
1182 propname);
1183 break;
1185 default:
1186 zfs_error_aux(hdl,
1187 dgettext(TEXT_DOMAIN,
1188 "(%d) not defined"),
1189 why);
1190 break;
1192 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1193 goto error;
1197 /*FALLTHRU*/
1199 case ZFS_PROP_SHARESMB:
1200 case ZFS_PROP_SHARENFS:
1202 * For the mountpoint and sharenfs or sharesmb
1203 * properties, check if it can be set in a
1204 * global/non-global zone based on
1205 * the zoned property value:
1207 * global zone non-global zone
1208 * --------------------------------------------------
1209 * zoned=on mountpoint (no) mountpoint (yes)
1210 * sharenfs (no) sharenfs (no)
1211 * sharesmb (no) sharesmb (no)
1213 * zoned=off mountpoint (yes) N/A
1214 * sharenfs (yes)
1215 * sharesmb (yes)
1217 if (zoned) {
1218 if (getzoneid() == GLOBAL_ZONEID) {
1219 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1220 "'%s' cannot be set on "
1221 "dataset in a non-global zone"),
1222 propname);
1223 (void) zfs_error(hdl, EZFS_ZONED,
1224 errbuf);
1225 goto error;
1226 } else if (prop == ZFS_PROP_SHARENFS ||
1227 prop == ZFS_PROP_SHARESMB) {
1228 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1229 "'%s' cannot be set in "
1230 "a non-global zone"), propname);
1231 (void) zfs_error(hdl, EZFS_ZONED,
1232 errbuf);
1233 goto error;
1235 } else if (getzoneid() != GLOBAL_ZONEID) {
1237 * If zoned property is 'off', this must be in
1238 * a global zone. If not, something is wrong.
1240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1241 "'%s' cannot be set while dataset "
1242 "'zoned' property is set"), propname);
1243 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1244 goto error;
1248 * At this point, it is legitimate to set the
1249 * property. Now we want to make sure that the
1250 * property value is valid if it is sharenfs.
1252 if ((prop == ZFS_PROP_SHARENFS ||
1253 prop == ZFS_PROP_SHARESMB) &&
1254 strcmp(strval, "on") != 0 &&
1255 strcmp(strval, "off") != 0) {
1256 zfs_share_proto_t proto;
1258 if (prop == ZFS_PROP_SHARESMB)
1259 proto = PROTO_SMB;
1260 else
1261 proto = PROTO_NFS;
1264 * Must be an valid sharing protocol
1265 * option string so init the libshare
1266 * in order to enable the parser and
1267 * then parse the options. We use the
1268 * control API since we don't care about
1269 * the current configuration and don't
1270 * want the overhead of loading it
1271 * until we actually do something.
1274 if (zfs_init_libshare(hdl,
1275 SA_INIT_CONTROL_API) != SA_OK) {
1277 * An error occurred so we can't do
1278 * anything
1280 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1281 "'%s' cannot be set: problem "
1282 "in share initialization"),
1283 propname);
1284 (void) zfs_error(hdl, EZFS_BADPROP,
1285 errbuf);
1286 goto error;
1289 if (zfs_parse_options(strval, proto) != SA_OK) {
1291 * There was an error in parsing so
1292 * deal with it by issuing an error
1293 * message and leaving after
1294 * uninitializing the the libshare
1295 * interface.
1297 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1298 "'%s' cannot be set to invalid "
1299 "options"), propname);
1300 (void) zfs_error(hdl, EZFS_BADPROP,
1301 errbuf);
1302 zfs_uninit_libshare(hdl);
1303 goto error;
1305 zfs_uninit_libshare(hdl);
1308 break;
1310 case ZFS_PROP_UTF8ONLY:
1311 chosen_utf = (int)intval;
1312 break;
1314 case ZFS_PROP_NORMALIZE:
1315 chosen_normal = (int)intval;
1316 break;
1318 default:
1319 break;
1323 * For changes to existing volumes, we have some additional
1324 * checks to enforce.
1326 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1327 uint64_t volsize = zfs_prop_get_int(zhp,
1328 ZFS_PROP_VOLSIZE);
1329 uint64_t blocksize = zfs_prop_get_int(zhp,
1330 ZFS_PROP_VOLBLOCKSIZE);
1331 char buf[64];
1333 switch (prop) {
1334 case ZFS_PROP_RESERVATION:
1335 case ZFS_PROP_REFRESERVATION:
1336 if (intval > volsize) {
1337 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1338 "'%s' is greater than current "
1339 "volume size"), propname);
1340 (void) zfs_error(hdl, EZFS_BADPROP,
1341 errbuf);
1342 goto error;
1344 break;
1346 case ZFS_PROP_VOLSIZE:
1347 if (intval % blocksize != 0) {
1348 zfs_nicenum(blocksize, buf,
1349 sizeof (buf));
1350 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1351 "'%s' must be a multiple of "
1352 "volume block size (%s)"),
1353 propname, buf);
1354 (void) zfs_error(hdl, EZFS_BADPROP,
1355 errbuf);
1356 goto error;
1359 if (intval == 0) {
1360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1361 "'%s' cannot be zero"),
1362 propname);
1363 (void) zfs_error(hdl, EZFS_BADPROP,
1364 errbuf);
1365 goto error;
1367 break;
1369 default:
1370 break;
1376 * If normalization was chosen, but no UTF8 choice was made,
1377 * enforce rejection of non-UTF8 names.
1379 * If normalization was chosen, but rejecting non-UTF8 names
1380 * was explicitly not chosen, it is an error.
1382 if (chosen_normal > 0 && chosen_utf < 0) {
1383 if (nvlist_add_uint64(ret,
1384 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1385 (void) no_memory(hdl);
1386 goto error;
1388 } else if (chosen_normal > 0 && chosen_utf == 0) {
1389 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1390 "'%s' must be set 'on' if normalization chosen"),
1391 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1392 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1393 goto error;
1395 return (ret);
1397 error:
1398 nvlist_free(ret);
1399 return (NULL);
1403 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1405 uint64_t old_volsize;
1406 uint64_t new_volsize;
1407 uint64_t old_reservation;
1408 uint64_t new_reservation;
1409 zfs_prop_t resv_prop;
1410 nvlist_t *props;
1413 * If this is an existing volume, and someone is setting the volsize,
1414 * make sure that it matches the reservation, or add it if necessary.
1416 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1417 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1418 return (-1);
1419 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1421 props = fnvlist_alloc();
1422 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1423 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1425 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1426 old_reservation) || nvlist_exists(nvl,
1427 zfs_prop_to_name(resv_prop))) {
1428 fnvlist_free(props);
1429 return (0);
1431 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1432 &new_volsize) != 0) {
1433 fnvlist_free(props);
1434 return (-1);
1436 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1437 fnvlist_free(props);
1439 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1440 new_reservation) != 0) {
1441 (void) no_memory(zhp->zfs_hdl);
1442 return (-1);
1444 return (1);
1447 void
1448 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1449 char *errbuf)
1451 switch (err) {
1453 case ENOSPC:
1455 * For quotas and reservations, ENOSPC indicates
1456 * something different; setting a quota or reservation
1457 * doesn't use any disk space.
1459 switch (prop) {
1460 case ZFS_PROP_QUOTA:
1461 case ZFS_PROP_REFQUOTA:
1462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1463 "size is less than current used or "
1464 "reserved space"));
1465 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1466 break;
1468 case ZFS_PROP_RESERVATION:
1469 case ZFS_PROP_REFRESERVATION:
1470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1471 "size is greater than available space"));
1472 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1473 break;
1475 default:
1476 (void) zfs_standard_error(hdl, err, errbuf);
1477 break;
1479 break;
1481 case EBUSY:
1482 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1483 break;
1485 case EROFS:
1486 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1487 break;
1489 case E2BIG:
1490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1491 "property value too long"));
1492 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1493 break;
1495 case ENOTSUP:
1496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1497 "pool and or dataset must be upgraded to set this "
1498 "property or value"));
1499 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1500 break;
1502 case ERANGE:
1503 if (prop == ZFS_PROP_COMPRESSION ||
1504 prop == ZFS_PROP_RECORDSIZE) {
1505 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506 "property setting is not allowed on "
1507 "bootable datasets"));
1508 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1509 } else if (prop == ZFS_PROP_CHECKSUM ||
1510 prop == ZFS_PROP_DEDUP) {
1511 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1512 "property setting is not allowed on "
1513 "root pools"));
1514 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1515 } else {
1516 (void) zfs_standard_error(hdl, err, errbuf);
1518 break;
1520 case EINVAL:
1521 if (prop == ZPROP_INVAL) {
1522 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1523 } else {
1524 (void) zfs_standard_error(hdl, err, errbuf);
1526 break;
1528 case EOVERFLOW:
1530 * This platform can't address a volume this big.
1532 #ifdef _ILP32
1533 if (prop == ZFS_PROP_VOLSIZE) {
1534 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1535 break;
1537 #endif
1538 /* FALLTHROUGH */
1539 default:
1540 (void) zfs_standard_error(hdl, err, errbuf);
1545 * Given a property name and value, set the property for the given dataset.
1548 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1550 int ret = -1;
1551 char errbuf[1024];
1552 libzfs_handle_t *hdl = zhp->zfs_hdl;
1553 nvlist_t *nvl = NULL;
1555 (void) snprintf(errbuf, sizeof (errbuf),
1556 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1557 zhp->zfs_name);
1559 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1560 nvlist_add_string(nvl, propname, propval) != 0) {
1561 (void) no_memory(hdl);
1562 goto error;
1565 ret = zfs_prop_set_list(zhp, nvl);
1567 error:
1568 nvlist_free(nvl);
1569 return (ret);
1575 * Given an nvlist of property names and values, set the properties for the
1576 * given dataset.
1579 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1581 zfs_cmd_t zc = { 0 };
1582 int ret = -1;
1583 prop_changelist_t **cls = NULL;
1584 int cl_idx;
1585 char errbuf[1024];
1586 libzfs_handle_t *hdl = zhp->zfs_hdl;
1587 nvlist_t *nvl;
1588 int nvl_len;
1589 int added_resv = 0;
1591 (void) snprintf(errbuf, sizeof (errbuf),
1592 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1593 zhp->zfs_name);
1595 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1596 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1597 errbuf)) == NULL)
1598 goto error;
1601 * We have to check for any extra properties which need to be added
1602 * before computing the length of the nvlist.
1604 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1605 elem != NULL;
1606 elem = nvlist_next_nvpair(nvl, elem)) {
1607 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1608 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1609 goto error;
1613 * Check how many properties we're setting and allocate an array to
1614 * store changelist pointers for postfix().
1616 nvl_len = 0;
1617 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1618 elem != NULL;
1619 elem = nvlist_next_nvpair(nvl, elem))
1620 nvl_len++;
1621 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1622 goto error;
1624 cl_idx = 0;
1625 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1626 elem != NULL;
1627 elem = nvlist_next_nvpair(nvl, elem)) {
1629 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1631 assert(cl_idx < nvl_len);
1633 * We don't want to unmount & remount the dataset when changing
1634 * its canmount property to 'on' or 'noauto'. We only use
1635 * the changelist logic to unmount when setting canmount=off.
1637 if (prop != ZFS_PROP_CANMOUNT ||
1638 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1639 zfs_is_mounted(zhp, NULL))) {
1640 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1641 if (cls[cl_idx] == NULL)
1642 goto error;
1645 if (prop == ZFS_PROP_MOUNTPOINT &&
1646 changelist_haszonedchild(cls[cl_idx])) {
1647 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1648 "child dataset with inherited mountpoint is used "
1649 "in a non-global zone"));
1650 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1651 goto error;
1654 if (cls[cl_idx] != NULL &&
1655 (ret = changelist_prefix(cls[cl_idx])) != 0)
1656 goto error;
1658 cl_idx++;
1660 assert(cl_idx == nvl_len);
1663 * Execute the corresponding ioctl() to set this list of properties.
1665 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1667 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1668 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1669 goto error;
1671 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1673 if (ret != 0) {
1674 /* Get the list of unset properties back and report them. */
1675 nvlist_t *errorprops = NULL;
1676 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1677 goto error;
1678 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1679 elem != NULL;
1680 elem = nvlist_next_nvpair(nvl, elem)) {
1681 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1682 zfs_setprop_error(hdl, prop, errno, errbuf);
1684 nvlist_free(errorprops);
1686 if (added_resv && errno == ENOSPC) {
1687 /* clean up the volsize property we tried to set */
1688 uint64_t old_volsize = zfs_prop_get_int(zhp,
1689 ZFS_PROP_VOLSIZE);
1690 nvlist_free(nvl);
1691 nvl = NULL;
1692 zcmd_free_nvlists(&zc);
1694 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1695 goto error;
1696 if (nvlist_add_uint64(nvl,
1697 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1698 old_volsize) != 0)
1699 goto error;
1700 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1701 goto error;
1702 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1704 } else {
1705 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1706 if (cls[cl_idx] != NULL) {
1707 int clp_err = changelist_postfix(cls[cl_idx]);
1708 if (clp_err != 0)
1709 ret = clp_err;
1714 * Refresh the statistics so the new property value
1715 * is reflected.
1717 if (ret == 0)
1718 (void) get_stats(zhp);
1721 error:
1722 nvlist_free(nvl);
1723 zcmd_free_nvlists(&zc);
1724 if (cls != NULL) {
1725 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1726 if (cls[cl_idx] != NULL)
1727 changelist_free(cls[cl_idx]);
1729 free(cls);
1731 return (ret);
1735 * Given a property, inherit the value from the parent dataset, or if received
1736 * is TRUE, revert to the received value, if any.
1739 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1741 zfs_cmd_t zc = { 0 };
1742 int ret;
1743 prop_changelist_t *cl;
1744 libzfs_handle_t *hdl = zhp->zfs_hdl;
1745 char errbuf[1024];
1746 zfs_prop_t prop;
1748 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1749 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1751 zc.zc_cookie = received;
1752 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1754 * For user properties, the amount of work we have to do is very
1755 * small, so just do it here.
1757 if (!zfs_prop_user(propname)) {
1758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1759 "invalid property"));
1760 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1763 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1764 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1766 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1767 return (zfs_standard_error(hdl, errno, errbuf));
1769 return (0);
1773 * Verify that this property is inheritable.
1775 if (zfs_prop_readonly(prop))
1776 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1778 if (!zfs_prop_inheritable(prop) && !received)
1779 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1782 * Check to see if the value applies to this type
1784 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1785 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1788 * Normalize the name, to get rid of shorthand abbreviations.
1790 propname = zfs_prop_to_name(prop);
1791 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1792 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1794 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1795 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1797 "dataset is used in a non-global zone"));
1798 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1802 * Determine datasets which will be affected by this change, if any.
1804 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1805 return (-1);
1807 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1809 "child dataset with inherited mountpoint is used "
1810 "in a non-global zone"));
1811 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1812 goto error;
1815 if ((ret = changelist_prefix(cl)) != 0)
1816 goto error;
1818 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1819 return (zfs_standard_error(hdl, errno, errbuf));
1820 } else {
1822 if ((ret = changelist_postfix(cl)) != 0)
1823 goto error;
1826 * Refresh the statistics so the new property is reflected.
1828 (void) get_stats(zhp);
1831 error:
1832 changelist_free(cl);
1833 return (ret);
1837 * True DSL properties are stored in an nvlist. The following two functions
1838 * extract them appropriately.
1840 static uint64_t
1841 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1843 nvlist_t *nv;
1844 uint64_t value;
1846 *source = NULL;
1847 if (nvlist_lookup_nvlist(zhp->zfs_props,
1848 zfs_prop_to_name(prop), &nv) == 0) {
1849 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1850 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1851 } else {
1852 verify(!zhp->zfs_props_table ||
1853 zhp->zfs_props_table[prop] == B_TRUE);
1854 value = zfs_prop_default_numeric(prop);
1855 *source = "";
1858 return (value);
1861 static const char *
1862 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1864 nvlist_t *nv;
1865 const char *value;
1867 *source = NULL;
1868 if (nvlist_lookup_nvlist(zhp->zfs_props,
1869 zfs_prop_to_name(prop), &nv) == 0) {
1870 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1871 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1872 } else {
1873 verify(!zhp->zfs_props_table ||
1874 zhp->zfs_props_table[prop] == B_TRUE);
1875 value = zfs_prop_default_string(prop);
1876 *source = "";
1879 return (value);
1882 static boolean_t
1883 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1885 return (zhp->zfs_props == zhp->zfs_recvd_props);
1888 static void
1889 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1891 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1892 zhp->zfs_props = zhp->zfs_recvd_props;
1895 static void
1896 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1898 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1899 *cookie = 0;
1903 * Internal function for getting a numeric property. Both zfs_prop_get() and
1904 * zfs_prop_get_int() are built using this interface.
1906 * Certain properties can be overridden using 'mount -o'. In this case, scan
1907 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1908 * If they differ from the on-disk values, report the current values and mark
1909 * the source "temporary".
1911 static int
1912 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1913 char **source, uint64_t *val)
1915 zfs_cmd_t zc = { 0 };
1916 nvlist_t *zplprops = NULL;
1917 struct mnttab mnt;
1918 char *mntopt_on = NULL;
1919 char *mntopt_off = NULL;
1920 boolean_t received = zfs_is_recvd_props_mode(zhp);
1922 *source = NULL;
1924 switch (prop) {
1925 case ZFS_PROP_ATIME:
1926 mntopt_on = MNTOPT_ATIME;
1927 mntopt_off = MNTOPT_NOATIME;
1928 break;
1930 case ZFS_PROP_DEVICES:
1931 mntopt_on = MNTOPT_DEVICES;
1932 mntopt_off = MNTOPT_NODEVICES;
1933 break;
1935 case ZFS_PROP_EXEC:
1936 mntopt_on = MNTOPT_EXEC;
1937 mntopt_off = MNTOPT_NOEXEC;
1938 break;
1940 case ZFS_PROP_READONLY:
1941 mntopt_on = MNTOPT_RO;
1942 mntopt_off = MNTOPT_RW;
1943 break;
1945 case ZFS_PROP_SETUID:
1946 mntopt_on = MNTOPT_SETUID;
1947 mntopt_off = MNTOPT_NOSETUID;
1948 break;
1950 case ZFS_PROP_XATTR:
1951 mntopt_on = MNTOPT_XATTR;
1952 mntopt_off = MNTOPT_NOXATTR;
1953 break;
1955 case ZFS_PROP_NBMAND:
1956 mntopt_on = MNTOPT_NBMAND;
1957 mntopt_off = MNTOPT_NONBMAND;
1958 break;
1960 default:
1961 break;
1965 * Because looking up the mount options is potentially expensive
1966 * (iterating over all of /etc/mnttab), we defer its calculation until
1967 * we're looking up a property which requires its presence.
1969 if (!zhp->zfs_mntcheck &&
1970 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1971 libzfs_handle_t *hdl = zhp->zfs_hdl;
1972 struct mnttab entry;
1974 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1975 zhp->zfs_mntopts = zfs_strdup(hdl,
1976 entry.mnt_mntopts);
1977 if (zhp->zfs_mntopts == NULL)
1978 return (-1);
1981 zhp->zfs_mntcheck = B_TRUE;
1984 if (zhp->zfs_mntopts == NULL)
1985 mnt.mnt_mntopts = "";
1986 else
1987 mnt.mnt_mntopts = zhp->zfs_mntopts;
1989 switch (prop) {
1990 case ZFS_PROP_ATIME:
1991 case ZFS_PROP_DEVICES:
1992 case ZFS_PROP_EXEC:
1993 case ZFS_PROP_READONLY:
1994 case ZFS_PROP_SETUID:
1995 case ZFS_PROP_XATTR:
1996 case ZFS_PROP_NBMAND:
1997 *val = getprop_uint64(zhp, prop, source);
1999 if (received)
2000 break;
2002 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2003 *val = B_TRUE;
2004 if (src)
2005 *src = ZPROP_SRC_TEMPORARY;
2006 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2007 *val = B_FALSE;
2008 if (src)
2009 *src = ZPROP_SRC_TEMPORARY;
2011 break;
2013 case ZFS_PROP_CANMOUNT:
2014 case ZFS_PROP_VOLSIZE:
2015 case ZFS_PROP_QUOTA:
2016 case ZFS_PROP_REFQUOTA:
2017 case ZFS_PROP_RESERVATION:
2018 case ZFS_PROP_REFRESERVATION:
2019 case ZFS_PROP_FILESYSTEM_LIMIT:
2020 case ZFS_PROP_SNAPSHOT_LIMIT:
2021 case ZFS_PROP_FILESYSTEM_COUNT:
2022 case ZFS_PROP_SNAPSHOT_COUNT:
2023 *val = getprop_uint64(zhp, prop, source);
2025 if (*source == NULL) {
2026 /* not default, must be local */
2027 *source = zhp->zfs_name;
2029 break;
2031 case ZFS_PROP_MOUNTED:
2032 *val = (zhp->zfs_mntopts != NULL);
2033 break;
2035 case ZFS_PROP_NUMCLONES:
2036 *val = zhp->zfs_dmustats.dds_num_clones;
2037 break;
2039 case ZFS_PROP_VERSION:
2040 case ZFS_PROP_NORMALIZE:
2041 case ZFS_PROP_UTF8ONLY:
2042 case ZFS_PROP_CASE:
2043 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2044 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2045 return (-1);
2046 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2047 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2048 zcmd_free_nvlists(&zc);
2049 return (-1);
2051 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2052 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2053 val) != 0) {
2054 zcmd_free_nvlists(&zc);
2055 return (-1);
2057 nvlist_free(zplprops);
2058 zcmd_free_nvlists(&zc);
2059 break;
2061 case ZFS_PROP_INCONSISTENT:
2062 *val = zhp->zfs_dmustats.dds_inconsistent;
2063 break;
2065 default:
2066 switch (zfs_prop_get_type(prop)) {
2067 case PROP_TYPE_NUMBER:
2068 case PROP_TYPE_INDEX:
2069 *val = getprop_uint64(zhp, prop, source);
2071 * If we tried to use a default value for a
2072 * readonly property, it means that it was not
2073 * present. Note this only applies to "truly"
2074 * readonly properties, not set-once properties
2075 * like volblocksize.
2077 if (zfs_prop_readonly(prop) &&
2078 !zfs_prop_setonce(prop) &&
2079 *source != NULL && (*source)[0] == '\0') {
2080 *source = NULL;
2081 return (-1);
2083 break;
2085 case PROP_TYPE_STRING:
2086 default:
2087 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2088 "cannot get non-numeric property"));
2089 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2090 dgettext(TEXT_DOMAIN, "internal error")));
2094 return (0);
2098 * Calculate the source type, given the raw source string.
2100 static void
2101 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2102 char *statbuf, size_t statlen)
2104 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2105 return;
2107 if (source == NULL) {
2108 *srctype = ZPROP_SRC_NONE;
2109 } else if (source[0] == '\0') {
2110 *srctype = ZPROP_SRC_DEFAULT;
2111 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2112 *srctype = ZPROP_SRC_RECEIVED;
2113 } else {
2114 if (strcmp(source, zhp->zfs_name) == 0) {
2115 *srctype = ZPROP_SRC_LOCAL;
2116 } else {
2117 (void) strlcpy(statbuf, source, statlen);
2118 *srctype = ZPROP_SRC_INHERITED;
2125 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2126 size_t proplen, boolean_t literal)
2128 zfs_prop_t prop;
2129 int err = 0;
2131 if (zhp->zfs_recvd_props == NULL)
2132 if (get_recvd_props_ioctl(zhp) != 0)
2133 return (-1);
2135 prop = zfs_name_to_prop(propname);
2137 if (prop != ZPROP_INVAL) {
2138 uint64_t cookie;
2139 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2140 return (-1);
2141 zfs_set_recvd_props_mode(zhp, &cookie);
2142 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2143 NULL, NULL, 0, literal);
2144 zfs_unset_recvd_props_mode(zhp, &cookie);
2145 } else {
2146 nvlist_t *propval;
2147 char *recvdval;
2148 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2149 propname, &propval) != 0)
2150 return (-1);
2151 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2152 &recvdval) == 0);
2153 (void) strlcpy(propbuf, recvdval, proplen);
2156 return (err == 0 ? 0 : -1);
2159 static int
2160 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2162 nvlist_t *value;
2163 nvpair_t *pair;
2165 value = zfs_get_clones_nvl(zhp);
2166 if (value == NULL)
2167 return (-1);
2169 propbuf[0] = '\0';
2170 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2171 pair = nvlist_next_nvpair(value, pair)) {
2172 if (propbuf[0] != '\0')
2173 (void) strlcat(propbuf, ",", proplen);
2174 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2177 return (0);
2180 struct get_clones_arg {
2181 uint64_t numclones;
2182 nvlist_t *value;
2183 const char *origin;
2184 char buf[ZFS_MAX_DATASET_NAME_LEN];
2188 get_clones_cb(zfs_handle_t *zhp, void *arg)
2190 struct get_clones_arg *gca = arg;
2192 if (gca->numclones == 0) {
2193 zfs_close(zhp);
2194 return (0);
2197 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2198 NULL, NULL, 0, B_TRUE) != 0)
2199 goto out;
2200 if (strcmp(gca->buf, gca->origin) == 0) {
2201 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2202 gca->numclones--;
2205 out:
2206 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2207 zfs_close(zhp);
2208 return (0);
2211 nvlist_t *
2212 zfs_get_clones_nvl(zfs_handle_t *zhp)
2214 nvlist_t *nv, *value;
2216 if (nvlist_lookup_nvlist(zhp->zfs_props,
2217 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2218 struct get_clones_arg gca;
2221 * if this is a snapshot, then the kernel wasn't able
2222 * to get the clones. Do it by slowly iterating.
2224 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2225 return (NULL);
2226 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2227 return (NULL);
2228 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2229 nvlist_free(nv);
2230 return (NULL);
2233 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2234 gca.value = value;
2235 gca.origin = zhp->zfs_name;
2237 if (gca.numclones != 0) {
2238 zfs_handle_t *root;
2239 char pool[ZFS_MAX_DATASET_NAME_LEN];
2240 char *cp = pool;
2242 /* get the pool name */
2243 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2244 (void) strsep(&cp, "/@");
2245 root = zfs_open(zhp->zfs_hdl, pool,
2246 ZFS_TYPE_FILESYSTEM);
2248 (void) get_clones_cb(root, &gca);
2251 if (gca.numclones != 0 ||
2252 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2253 nvlist_add_nvlist(zhp->zfs_props,
2254 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2255 nvlist_free(nv);
2256 nvlist_free(value);
2257 return (NULL);
2259 nvlist_free(nv);
2260 nvlist_free(value);
2261 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2262 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2265 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2267 return (value);
2271 * Accepts a property and value and checks that the value
2272 * matches the one found by the channel program. If they are
2273 * not equal, print both of them.
2275 void
2276 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2277 const char *strval)
2279 if (!zhp->zfs_hdl->libzfs_prop_debug)
2280 return;
2281 int error;
2282 char *poolname = zhp->zpool_hdl->zpool_name;
2283 const char *program =
2284 "args = ...\n"
2285 "ds = args['dataset']\n"
2286 "prop = args['property']\n"
2287 "value, setpoint = zfs.get_prop(ds, prop)\n"
2288 "return {value=value, setpoint=setpoint}\n";
2289 nvlist_t *outnvl;
2290 nvlist_t *retnvl;
2291 nvlist_t *argnvl = fnvlist_alloc();
2293 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2294 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2296 error = lzc_channel_program_nosync(poolname, program,
2297 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2299 if (error == 0) {
2300 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2301 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2302 int64_t ans;
2303 error = nvlist_lookup_int64(retnvl, "value", &ans);
2304 if (error != 0) {
2305 (void) fprintf(stderr, "zcp check error: %u\n",
2306 error);
2307 return;
2309 if (ans != intval) {
2310 (void) fprintf(stderr,
2311 "%s: zfs found %lld, but zcp found %lld\n",
2312 zfs_prop_to_name(prop),
2313 (longlong_t)intval, (longlong_t)ans);
2315 } else {
2316 char *str_ans;
2317 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2318 if (error != 0) {
2319 (void) fprintf(stderr, "zcp check error: %u\n",
2320 error);
2321 return;
2323 if (strcmp(strval, str_ans) != 0) {
2324 (void) fprintf(stderr,
2325 "%s: zfs found %s, but zcp found %s\n",
2326 zfs_prop_to_name(prop),
2327 strval, str_ans);
2330 } else {
2331 (void) fprintf(stderr,
2332 "zcp check failed, channel program error: %u\n", error);
2334 nvlist_free(argnvl);
2335 nvlist_free(outnvl);
2339 * Retrieve a property from the given object. If 'literal' is specified, then
2340 * numbers are left as exact values. Otherwise, numbers are converted to a
2341 * human-readable form.
2343 * Returns 0 on success, or -1 on error.
2346 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2347 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2349 char *source = NULL;
2350 uint64_t val;
2351 const char *str;
2352 const char *strval;
2353 boolean_t received = zfs_is_recvd_props_mode(zhp);
2356 * Check to see if this property applies to our object
2358 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2359 return (-1);
2361 if (received && zfs_prop_readonly(prop))
2362 return (-1);
2364 if (src)
2365 *src = ZPROP_SRC_NONE;
2367 switch (prop) {
2368 case ZFS_PROP_CREATION:
2370 * 'creation' is a time_t stored in the statistics. We convert
2371 * this into a string unless 'literal' is specified.
2374 val = getprop_uint64(zhp, prop, &source);
2375 time_t time = (time_t)val;
2376 struct tm t;
2378 if (literal ||
2379 localtime_r(&time, &t) == NULL ||
2380 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2381 &t) == 0)
2382 (void) snprintf(propbuf, proplen, "%llu", val);
2384 zcp_check(zhp, prop, val, NULL);
2385 break;
2387 case ZFS_PROP_MOUNTPOINT:
2389 * Getting the precise mountpoint can be tricky.
2391 * - for 'none' or 'legacy', return those values.
2392 * - for inherited mountpoints, we want to take everything
2393 * after our ancestor and append it to the inherited value.
2395 * If the pool has an alternate root, we want to prepend that
2396 * root to any values we return.
2399 str = getprop_string(zhp, prop, &source);
2401 if (str[0] == '/') {
2402 char buf[MAXPATHLEN];
2403 char *root = buf;
2404 const char *relpath;
2407 * If we inherit the mountpoint, even from a dataset
2408 * with a received value, the source will be the path of
2409 * the dataset we inherit from. If source is
2410 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2411 * inherited.
2413 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2414 relpath = "";
2415 } else {
2416 relpath = zhp->zfs_name + strlen(source);
2417 if (relpath[0] == '/')
2418 relpath++;
2421 if ((zpool_get_prop(zhp->zpool_hdl,
2422 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2423 B_FALSE)) || (strcmp(root, "-") == 0))
2424 root[0] = '\0';
2426 * Special case an alternate root of '/'. This will
2427 * avoid having multiple leading slashes in the
2428 * mountpoint path.
2430 if (strcmp(root, "/") == 0)
2431 root++;
2434 * If the mountpoint is '/' then skip over this
2435 * if we are obtaining either an alternate root or
2436 * an inherited mountpoint.
2438 if (str[1] == '\0' && (root[0] != '\0' ||
2439 relpath[0] != '\0'))
2440 str++;
2442 if (relpath[0] == '\0')
2443 (void) snprintf(propbuf, proplen, "%s%s",
2444 root, str);
2445 else
2446 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2447 root, str, relpath[0] == '@' ? "" : "/",
2448 relpath);
2449 } else {
2450 /* 'legacy' or 'none' */
2451 (void) strlcpy(propbuf, str, proplen);
2453 zcp_check(zhp, prop, 0, propbuf);
2454 break;
2456 case ZFS_PROP_ORIGIN:
2457 str = getprop_string(zhp, prop, &source);
2458 if (str == NULL)
2459 return (-1);
2460 (void) strlcpy(propbuf, str, proplen);
2461 zcp_check(zhp, prop, 0, str);
2462 break;
2464 case ZFS_PROP_CLONES:
2465 if (get_clones_string(zhp, propbuf, proplen) != 0)
2466 return (-1);
2467 break;
2469 case ZFS_PROP_QUOTA:
2470 case ZFS_PROP_REFQUOTA:
2471 case ZFS_PROP_RESERVATION:
2472 case ZFS_PROP_REFRESERVATION:
2474 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2475 return (-1);
2477 * If quota or reservation is 0, we translate this into 'none'
2478 * (unless literal is set), and indicate that it's the default
2479 * value. Otherwise, we print the number nicely and indicate
2480 * that its set locally.
2482 if (val == 0) {
2483 if (literal)
2484 (void) strlcpy(propbuf, "0", proplen);
2485 else
2486 (void) strlcpy(propbuf, "none", proplen);
2487 } else {
2488 if (literal)
2489 (void) snprintf(propbuf, proplen, "%llu",
2490 (u_longlong_t)val);
2491 else
2492 zfs_nicenum(val, propbuf, proplen);
2494 zcp_check(zhp, prop, val, NULL);
2495 break;
2497 case ZFS_PROP_FILESYSTEM_LIMIT:
2498 case ZFS_PROP_SNAPSHOT_LIMIT:
2499 case ZFS_PROP_FILESYSTEM_COUNT:
2500 case ZFS_PROP_SNAPSHOT_COUNT:
2502 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2503 return (-1);
2506 * If limit is UINT64_MAX, we translate this into 'none' (unless
2507 * literal is set), and indicate that it's the default value.
2508 * Otherwise, we print the number nicely and indicate that it's
2509 * set locally.
2511 if (literal) {
2512 (void) snprintf(propbuf, proplen, "%llu",
2513 (u_longlong_t)val);
2514 } else if (val == UINT64_MAX) {
2515 (void) strlcpy(propbuf, "none", proplen);
2516 } else {
2517 zfs_nicenum(val, propbuf, proplen);
2520 zcp_check(zhp, prop, val, NULL);
2521 break;
2523 case ZFS_PROP_REFRATIO:
2524 case ZFS_PROP_COMPRESSRATIO:
2525 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2526 return (-1);
2527 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2528 (u_longlong_t)(val / 100),
2529 (u_longlong_t)(val % 100));
2530 zcp_check(zhp, prop, val, NULL);
2531 break;
2533 case ZFS_PROP_TYPE:
2534 switch (zhp->zfs_type) {
2535 case ZFS_TYPE_FILESYSTEM:
2536 str = "filesystem";
2537 break;
2538 case ZFS_TYPE_VOLUME:
2539 str = "volume";
2540 break;
2541 case ZFS_TYPE_SNAPSHOT:
2542 str = "snapshot";
2543 break;
2544 case ZFS_TYPE_BOOKMARK:
2545 str = "bookmark";
2546 break;
2547 default:
2548 abort();
2550 (void) snprintf(propbuf, proplen, "%s", str);
2551 zcp_check(zhp, prop, 0, propbuf);
2552 break;
2554 case ZFS_PROP_MOUNTED:
2556 * The 'mounted' property is a pseudo-property that described
2557 * whether the filesystem is currently mounted. Even though
2558 * it's a boolean value, the typical values of "on" and "off"
2559 * don't make sense, so we translate to "yes" and "no".
2561 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2562 src, &source, &val) != 0)
2563 return (-1);
2564 if (val)
2565 (void) strlcpy(propbuf, "yes", proplen);
2566 else
2567 (void) strlcpy(propbuf, "no", proplen);
2568 break;
2570 case ZFS_PROP_NAME:
2572 * The 'name' property is a pseudo-property derived from the
2573 * dataset name. It is presented as a real property to simplify
2574 * consumers.
2576 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2577 zcp_check(zhp, prop, 0, propbuf);
2578 break;
2580 case ZFS_PROP_GUID:
2582 * GUIDs are stored as numbers, but they are identifiers.
2583 * We don't want them to be pretty printed, because pretty
2584 * printing mangles the ID into a truncated and useless value.
2586 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2587 return (-1);
2588 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2589 zcp_check(zhp, prop, val, NULL);
2590 break;
2592 default:
2593 switch (zfs_prop_get_type(prop)) {
2594 case PROP_TYPE_NUMBER:
2595 if (get_numeric_property(zhp, prop, src,
2596 &source, &val) != 0) {
2597 return (-1);
2600 if (literal) {
2601 (void) snprintf(propbuf, proplen, "%llu",
2602 (u_longlong_t)val);
2603 } else {
2604 zfs_nicenum(val, propbuf, proplen);
2606 zcp_check(zhp, prop, val, NULL);
2607 break;
2609 case PROP_TYPE_STRING:
2610 str = getprop_string(zhp, prop, &source);
2611 if (str == NULL)
2612 return (-1);
2614 (void) strlcpy(propbuf, str, proplen);
2615 zcp_check(zhp, prop, 0, str);
2616 break;
2618 case PROP_TYPE_INDEX:
2619 if (get_numeric_property(zhp, prop, src,
2620 &source, &val) != 0)
2621 return (-1);
2622 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2623 return (-1);
2625 (void) strlcpy(propbuf, strval, proplen);
2626 zcp_check(zhp, prop, 0, strval);
2627 break;
2629 default:
2630 abort();
2634 get_source(zhp, src, source, statbuf, statlen);
2636 return (0);
2640 * Utility function to get the given numeric property. Does no validation that
2641 * the given property is the appropriate type; should only be used with
2642 * hard-coded property types.
2644 uint64_t
2645 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2647 char *source;
2648 uint64_t val;
2650 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2652 return (val);
2656 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2658 char buf[64];
2660 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2661 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2665 * Similar to zfs_prop_get(), but returns the value as an integer.
2668 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2669 zprop_source_t *src, char *statbuf, size_t statlen)
2671 char *source;
2674 * Check to see if this property applies to our object
2676 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2677 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2678 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2679 zfs_prop_to_name(prop)));
2682 if (src)
2683 *src = ZPROP_SRC_NONE;
2685 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2686 return (-1);
2688 get_source(zhp, src, source, statbuf, statlen);
2690 return (0);
2693 static int
2694 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2695 char **domainp, idmap_rid_t *ridp)
2697 idmap_get_handle_t *get_hdl = NULL;
2698 idmap_stat status;
2699 int err = EINVAL;
2701 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2702 goto out;
2704 if (isuser) {
2705 err = idmap_get_sidbyuid(get_hdl, id,
2706 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2707 } else {
2708 err = idmap_get_sidbygid(get_hdl, id,
2709 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2711 if (err == IDMAP_SUCCESS &&
2712 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2713 status == IDMAP_SUCCESS)
2714 err = 0;
2715 else
2716 err = EINVAL;
2717 out:
2718 if (get_hdl)
2719 idmap_get_destroy(get_hdl);
2720 return (err);
2724 * convert the propname into parameters needed by kernel
2725 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2726 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2728 static int
2729 userquota_propname_decode(const char *propname, boolean_t zoned,
2730 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2732 zfs_userquota_prop_t type;
2733 char *cp, *end;
2734 char *numericsid = NULL;
2735 boolean_t isuser;
2737 domain[0] = '\0';
2738 *ridp = 0;
2739 /* Figure out the property type ({user|group}{quota|space}) */
2740 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2741 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2742 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2743 break;
2745 if (type == ZFS_NUM_USERQUOTA_PROPS)
2746 return (EINVAL);
2747 *typep = type;
2749 isuser = (type == ZFS_PROP_USERQUOTA ||
2750 type == ZFS_PROP_USERUSED);
2752 cp = strchr(propname, '@') + 1;
2754 if (strchr(cp, '@')) {
2756 * It's a SID name (eg "user@domain") that needs to be
2757 * turned into S-1-domainID-RID.
2759 int flag = 0;
2760 idmap_stat stat, map_stat;
2761 uid_t pid;
2762 idmap_rid_t rid;
2763 idmap_get_handle_t *gh = NULL;
2765 stat = idmap_get_create(&gh);
2766 if (stat != IDMAP_SUCCESS) {
2767 idmap_get_destroy(gh);
2768 return (ENOMEM);
2770 if (zoned && getzoneid() == GLOBAL_ZONEID)
2771 return (ENOENT);
2772 if (isuser) {
2773 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2774 if (stat < 0)
2775 return (ENOENT);
2776 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2777 &rid, &map_stat);
2778 } else {
2779 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2780 if (stat < 0)
2781 return (ENOENT);
2782 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2783 &rid, &map_stat);
2785 if (stat < 0) {
2786 idmap_get_destroy(gh);
2787 return (ENOENT);
2789 stat = idmap_get_mappings(gh);
2790 idmap_get_destroy(gh);
2792 if (stat < 0) {
2793 return (ENOENT);
2795 if (numericsid == NULL)
2796 return (ENOENT);
2797 cp = numericsid;
2798 *ridp = rid;
2799 /* will be further decoded below */
2802 if (strncmp(cp, "S-1-", 4) == 0) {
2803 /* It's a numeric SID (eg "S-1-234-567-89") */
2804 (void) strlcpy(domain, cp, domainlen);
2805 errno = 0;
2806 if (*ridp == 0) {
2807 cp = strrchr(domain, '-');
2808 *cp = '\0';
2809 cp++;
2810 *ridp = strtoull(cp, &end, 10);
2811 } else {
2812 end = "";
2814 if (numericsid) {
2815 free(numericsid);
2816 numericsid = NULL;
2818 if (errno != 0 || *end != '\0')
2819 return (EINVAL);
2820 } else if (!isdigit(*cp)) {
2822 * It's a user/group name (eg "user") that needs to be
2823 * turned into a uid/gid
2825 if (zoned && getzoneid() == GLOBAL_ZONEID)
2826 return (ENOENT);
2827 if (isuser) {
2828 struct passwd *pw;
2829 pw = getpwnam(cp);
2830 if (pw == NULL)
2831 return (ENOENT);
2832 *ridp = pw->pw_uid;
2833 } else {
2834 struct group *gr;
2835 gr = getgrnam(cp);
2836 if (gr == NULL)
2837 return (ENOENT);
2838 *ridp = gr->gr_gid;
2840 } else {
2841 /* It's a user/group ID (eg "12345"). */
2842 uid_t id = strtoul(cp, &end, 10);
2843 idmap_rid_t rid;
2844 char *mapdomain;
2846 if (*end != '\0')
2847 return (EINVAL);
2848 if (id > MAXUID) {
2849 /* It's an ephemeral ID. */
2850 if (idmap_id_to_numeric_domain_rid(id, isuser,
2851 &mapdomain, &rid) != 0)
2852 return (ENOENT);
2853 (void) strlcpy(domain, mapdomain, domainlen);
2854 *ridp = rid;
2855 } else {
2856 *ridp = id;
2860 ASSERT3P(numericsid, ==, NULL);
2861 return (0);
2864 static int
2865 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2866 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2868 int err;
2869 zfs_cmd_t zc = { 0 };
2871 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2873 err = userquota_propname_decode(propname,
2874 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2875 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2876 zc.zc_objset_type = *typep;
2877 if (err)
2878 return (err);
2880 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2881 if (err)
2882 return (err);
2884 *propvalue = zc.zc_cookie;
2885 return (0);
2889 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2890 uint64_t *propvalue)
2892 zfs_userquota_prop_t type;
2894 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2895 &type));
2899 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2900 char *propbuf, int proplen, boolean_t literal)
2902 int err;
2903 uint64_t propvalue;
2904 zfs_userquota_prop_t type;
2906 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2907 &type);
2909 if (err)
2910 return (err);
2912 if (literal) {
2913 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2914 } else if (propvalue == 0 &&
2915 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2916 (void) strlcpy(propbuf, "none", proplen);
2917 } else {
2918 zfs_nicenum(propvalue, propbuf, proplen);
2920 return (0);
2924 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2925 uint64_t *propvalue)
2927 int err;
2928 zfs_cmd_t zc = { 0 };
2929 const char *snapname;
2931 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2933 snapname = strchr(propname, '@') + 1;
2934 if (strchr(snapname, '@')) {
2935 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2936 } else {
2937 /* snapname is the short name, append it to zhp's fsname */
2938 char *cp;
2940 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2941 sizeof (zc.zc_value));
2942 cp = strchr(zc.zc_value, '@');
2943 if (cp != NULL)
2944 *cp = '\0';
2945 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2946 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2949 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2950 if (err)
2951 return (err);
2953 *propvalue = zc.zc_cookie;
2954 return (0);
2958 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2959 char *propbuf, int proplen, boolean_t literal)
2961 int err;
2962 uint64_t propvalue;
2964 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2966 if (err)
2967 return (err);
2969 if (literal) {
2970 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2971 } else {
2972 zfs_nicenum(propvalue, propbuf, proplen);
2974 return (0);
2978 * Returns the name of the given zfs handle.
2980 const char *
2981 zfs_get_name(const zfs_handle_t *zhp)
2983 return (zhp->zfs_name);
2987 * Returns the name of the parent pool for the given zfs handle.
2989 const char *
2990 zfs_get_pool_name(const zfs_handle_t *zhp)
2992 return (zhp->zpool_hdl->zpool_name);
2996 * Returns the type of the given zfs handle.
2998 zfs_type_t
2999 zfs_get_type(const zfs_handle_t *zhp)
3001 return (zhp->zfs_type);
3005 * Is one dataset name a child dataset of another?
3007 * Needs to handle these cases:
3008 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3009 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3010 * Descendant? No. No. No. Yes.
3012 static boolean_t
3013 is_descendant(const char *ds1, const char *ds2)
3015 size_t d1len = strlen(ds1);
3017 /* ds2 can't be a descendant if it's smaller */
3018 if (strlen(ds2) < d1len)
3019 return (B_FALSE);
3021 /* otherwise, compare strings and verify that there's a '/' char */
3022 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3026 * Given a complete name, return just the portion that refers to the parent.
3027 * Will return -1 if there is no parent (path is just the name of the
3028 * pool).
3030 static int
3031 parent_name(const char *path, char *buf, size_t buflen)
3033 char *slashp;
3035 (void) strlcpy(buf, path, buflen);
3037 if ((slashp = strrchr(buf, '/')) == NULL)
3038 return (-1);
3039 *slashp = '\0';
3041 return (0);
3045 * If accept_ancestor is false, then check to make sure that the given path has
3046 * a parent, and that it exists. If accept_ancestor is true, then find the
3047 * closest existing ancestor for the given path. In prefixlen return the
3048 * length of already existing prefix of the given path. We also fetch the
3049 * 'zoned' property, which is used to validate property settings when creating
3050 * new datasets.
3052 static int
3053 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3054 boolean_t accept_ancestor, int *prefixlen)
3056 zfs_cmd_t zc = { 0 };
3057 char parent[ZFS_MAX_DATASET_NAME_LEN];
3058 char *slash;
3059 zfs_handle_t *zhp;
3060 char errbuf[1024];
3061 uint64_t is_zoned;
3063 (void) snprintf(errbuf, sizeof (errbuf),
3064 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3066 /* get parent, and check to see if this is just a pool */
3067 if (parent_name(path, parent, sizeof (parent)) != 0) {
3068 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3069 "missing dataset name"));
3070 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3073 /* check to see if the pool exists */
3074 if ((slash = strchr(parent, '/')) == NULL)
3075 slash = parent + strlen(parent);
3076 (void) strncpy(zc.zc_name, parent, slash - parent);
3077 zc.zc_name[slash - parent] = '\0';
3078 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3079 errno == ENOENT) {
3080 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3081 "no such pool '%s'"), zc.zc_name);
3082 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3085 /* check to see if the parent dataset exists */
3086 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3087 if (errno == ENOENT && accept_ancestor) {
3089 * Go deeper to find an ancestor, give up on top level.
3091 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3093 "no such pool '%s'"), zc.zc_name);
3094 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3096 } else if (errno == ENOENT) {
3097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3098 "parent does not exist"));
3099 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3100 } else
3101 return (zfs_standard_error(hdl, errno, errbuf));
3104 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3105 if (zoned != NULL)
3106 *zoned = is_zoned;
3108 /* we are in a non-global zone, but parent is in the global zone */
3109 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3110 (void) zfs_standard_error(hdl, EPERM, errbuf);
3111 zfs_close(zhp);
3112 return (-1);
3115 /* make sure parent is a filesystem */
3116 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3118 "parent is not a filesystem"));
3119 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3120 zfs_close(zhp);
3121 return (-1);
3124 zfs_close(zhp);
3125 if (prefixlen != NULL)
3126 *prefixlen = strlen(parent);
3127 return (0);
3131 * Finds whether the dataset of the given type(s) exists.
3133 boolean_t
3134 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3136 zfs_handle_t *zhp;
3138 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3139 return (B_FALSE);
3142 * Try to get stats for the dataset, which will tell us if it exists.
3144 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3145 int ds_type = zhp->zfs_type;
3147 zfs_close(zhp);
3148 if (types & ds_type)
3149 return (B_TRUE);
3151 return (B_FALSE);
3155 * Given a path to 'target', create all the ancestors between
3156 * the prefixlen portion of the path, and the target itself.
3157 * Fail if the initial prefixlen-ancestor does not already exist.
3160 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3162 zfs_handle_t *h;
3163 char *cp;
3164 const char *opname;
3166 /* make sure prefix exists */
3167 cp = target + prefixlen;
3168 if (*cp != '/') {
3169 assert(strchr(cp, '/') == NULL);
3170 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3171 } else {
3172 *cp = '\0';
3173 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3174 *cp = '/';
3176 if (h == NULL)
3177 return (-1);
3178 zfs_close(h);
3181 * Attempt to create, mount, and share any ancestor filesystems,
3182 * up to the prefixlen-long one.
3184 for (cp = target + prefixlen + 1;
3185 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3187 *cp = '\0';
3189 h = make_dataset_handle(hdl, target);
3190 if (h) {
3191 /* it already exists, nothing to do here */
3192 zfs_close(h);
3193 continue;
3196 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3197 NULL) != 0) {
3198 opname = dgettext(TEXT_DOMAIN, "create");
3199 goto ancestorerr;
3202 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3203 if (h == NULL) {
3204 opname = dgettext(TEXT_DOMAIN, "open");
3205 goto ancestorerr;
3208 if (zfs_mount(h, NULL, 0) != 0) {
3209 opname = dgettext(TEXT_DOMAIN, "mount");
3210 goto ancestorerr;
3213 if (zfs_share(h) != 0) {
3214 opname = dgettext(TEXT_DOMAIN, "share");
3215 goto ancestorerr;
3218 zfs_close(h);
3221 return (0);
3223 ancestorerr:
3224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3225 "failed to %s ancestor '%s'"), opname, target);
3226 return (-1);
3230 * Creates non-existing ancestors of the given path.
3233 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3235 int prefix;
3236 char *path_copy;
3237 int rc = 0;
3239 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3240 return (-1);
3242 if ((path_copy = strdup(path)) != NULL) {
3243 rc = create_parents(hdl, path_copy, prefix);
3244 free(path_copy);
3246 if (path_copy == NULL || rc != 0)
3247 return (-1);
3249 return (0);
3253 * Create a new filesystem or volume.
3256 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3257 nvlist_t *props)
3259 int ret;
3260 uint64_t size = 0;
3261 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3262 char errbuf[1024];
3263 uint64_t zoned;
3264 enum lzc_dataset_type ost;
3265 zpool_handle_t *zpool_handle;
3267 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3268 "cannot create '%s'"), path);
3270 /* validate the path, taking care to note the extended error message */
3271 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3272 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3274 /* validate parents exist */
3275 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3276 return (-1);
3279 * The failure modes when creating a dataset of a different type over
3280 * one that already exists is a little strange. In particular, if you
3281 * try to create a dataset on top of an existing dataset, the ioctl()
3282 * will return ENOENT, not EEXIST. To prevent this from happening, we
3283 * first try to see if the dataset exists.
3285 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3287 "dataset already exists"));
3288 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3291 if (type == ZFS_TYPE_VOLUME)
3292 ost = LZC_DATSET_TYPE_ZVOL;
3293 else
3294 ost = LZC_DATSET_TYPE_ZFS;
3296 /* open zpool handle for prop validation */
3297 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3298 (void) strlcpy(pool_path, path, sizeof (pool_path));
3300 /* truncate pool_path at first slash */
3301 char *p = strchr(pool_path, '/');
3302 if (p != NULL)
3303 *p = '\0';
3305 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3306 return (-1);
3308 if (props && (props = zfs_valid_proplist(hdl, type, props,
3309 zoned, NULL, zpool_handle, errbuf)) == 0) {
3310 zpool_close(zpool_handle);
3311 return (-1);
3313 zpool_close(zpool_handle);
3315 if (type == ZFS_TYPE_VOLUME) {
3317 * If we are creating a volume, the size and block size must
3318 * satisfy a few restraints. First, the blocksize must be a
3319 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3320 * volsize must be a multiple of the block size, and cannot be
3321 * zero.
3323 if (props == NULL || nvlist_lookup_uint64(props,
3324 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3325 nvlist_free(props);
3326 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3327 "missing volume size"));
3328 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3331 if ((ret = nvlist_lookup_uint64(props,
3332 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3333 &blocksize)) != 0) {
3334 if (ret == ENOENT) {
3335 blocksize = zfs_prop_default_numeric(
3336 ZFS_PROP_VOLBLOCKSIZE);
3337 } else {
3338 nvlist_free(props);
3339 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3340 "missing volume block size"));
3341 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3345 if (size == 0) {
3346 nvlist_free(props);
3347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3348 "volume size cannot be zero"));
3349 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3352 if (size % blocksize != 0) {
3353 nvlist_free(props);
3354 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3355 "volume size must be a multiple of volume block "
3356 "size"));
3357 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3361 /* create the dataset */
3362 ret = lzc_create(path, ost, props);
3363 nvlist_free(props);
3365 /* check for failure */
3366 if (ret != 0) {
3367 char parent[ZFS_MAX_DATASET_NAME_LEN];
3368 (void) parent_name(path, parent, sizeof (parent));
3370 switch (errno) {
3371 case ENOENT:
3372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3373 "no such parent '%s'"), parent);
3374 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3376 case EINVAL:
3377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3378 "parent '%s' is not a filesystem"), parent);
3379 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3381 case ENOTSUP:
3382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3383 "pool must be upgraded to set this "
3384 "property or value"));
3385 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3386 case ERANGE:
3387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3388 "invalid property value(s) specified"));
3389 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3390 #ifdef _ILP32
3391 case EOVERFLOW:
3393 * This platform can't address a volume this big.
3395 if (type == ZFS_TYPE_VOLUME)
3396 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3397 errbuf));
3398 #endif
3399 /* FALLTHROUGH */
3400 default:
3401 return (zfs_standard_error(hdl, errno, errbuf));
3405 return (0);
3409 * Destroys the given dataset. The caller must make sure that the filesystem
3410 * isn't mounted, and that there are no active dependents. If the file system
3411 * does not exist this function does nothing.
3414 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3416 zfs_cmd_t zc = { 0 };
3418 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3419 nvlist_t *nv = fnvlist_alloc();
3420 fnvlist_add_boolean(nv, zhp->zfs_name);
3421 int error = lzc_destroy_bookmarks(nv, NULL);
3422 fnvlist_free(nv);
3423 if (error != 0) {
3424 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3425 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3426 zhp->zfs_name));
3428 return (0);
3431 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3433 if (ZFS_IS_VOLUME(zhp)) {
3434 zc.zc_objset_type = DMU_OST_ZVOL;
3435 } else {
3436 zc.zc_objset_type = DMU_OST_ZFS;
3439 zc.zc_defer_destroy = defer;
3440 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3441 errno != ENOENT) {
3442 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3443 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3444 zhp->zfs_name));
3447 remove_mountpoint(zhp);
3449 return (0);
3452 struct destroydata {
3453 nvlist_t *nvl;
3454 const char *snapname;
3457 static int
3458 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3460 struct destroydata *dd = arg;
3461 char name[ZFS_MAX_DATASET_NAME_LEN];
3462 int rv = 0;
3464 (void) snprintf(name, sizeof (name),
3465 "%s@%s", zhp->zfs_name, dd->snapname);
3467 if (lzc_exists(name))
3468 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3470 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3471 zfs_close(zhp);
3472 return (rv);
3476 * Destroys all snapshots with the given name in zhp & descendants.
3479 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3481 int ret;
3482 struct destroydata dd = { 0 };
3484 dd.snapname = snapname;
3485 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3486 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3488 if (nvlist_empty(dd.nvl)) {
3489 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3490 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3491 zhp->zfs_name, snapname);
3492 } else {
3493 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3495 nvlist_free(dd.nvl);
3496 return (ret);
3500 * Destroys all the snapshots named in the nvlist.
3503 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3505 int ret;
3506 nvlist_t *errlist = NULL;
3508 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3510 if (ret == 0) {
3511 nvlist_free(errlist);
3512 return (0);
3515 if (nvlist_empty(errlist)) {
3516 char errbuf[1024];
3517 (void) snprintf(errbuf, sizeof (errbuf),
3518 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3520 ret = zfs_standard_error(hdl, ret, errbuf);
3522 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3523 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3524 char errbuf[1024];
3525 (void) snprintf(errbuf, sizeof (errbuf),
3526 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3527 nvpair_name(pair));
3529 switch (fnvpair_value_int32(pair)) {
3530 case EEXIST:
3531 zfs_error_aux(hdl,
3532 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3533 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3534 break;
3535 default:
3536 ret = zfs_standard_error(hdl, errno, errbuf);
3537 break;
3541 nvlist_free(errlist);
3542 return (ret);
3546 * Clones the given dataset. The target must be of the same type as the source.
3549 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3551 char parent[ZFS_MAX_DATASET_NAME_LEN];
3552 int ret;
3553 char errbuf[1024];
3554 libzfs_handle_t *hdl = zhp->zfs_hdl;
3555 uint64_t zoned;
3557 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3559 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3560 "cannot create '%s'"), target);
3562 /* validate the target/clone name */
3563 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3564 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3566 /* validate parents exist */
3567 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3568 return (-1);
3570 (void) parent_name(target, parent, sizeof (parent));
3572 /* do the clone */
3574 if (props) {
3575 zfs_type_t type;
3576 if (ZFS_IS_VOLUME(zhp)) {
3577 type = ZFS_TYPE_VOLUME;
3578 } else {
3579 type = ZFS_TYPE_FILESYSTEM;
3581 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3582 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3583 return (-1);
3586 ret = lzc_clone(target, zhp->zfs_name, props);
3587 nvlist_free(props);
3589 if (ret != 0) {
3590 switch (errno) {
3592 case ENOENT:
3594 * The parent doesn't exist. We should have caught this
3595 * above, but there may a race condition that has since
3596 * destroyed the parent.
3598 * At this point, we don't know whether it's the source
3599 * that doesn't exist anymore, or whether the target
3600 * dataset doesn't exist.
3602 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3603 "no such parent '%s'"), parent);
3604 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3606 case EXDEV:
3607 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3608 "source and target pools differ"));
3609 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3610 errbuf));
3612 default:
3613 return (zfs_standard_error(zhp->zfs_hdl, errno,
3614 errbuf));
3618 return (ret);
3622 * Promotes the given clone fs to be the clone parent.
3625 zfs_promote(zfs_handle_t *zhp)
3627 libzfs_handle_t *hdl = zhp->zfs_hdl;
3628 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3629 int ret;
3630 char errbuf[1024];
3632 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3633 "cannot promote '%s'"), zhp->zfs_name);
3635 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3637 "snapshots can not be promoted"));
3638 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3641 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3642 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3643 "not a cloned filesystem"));
3644 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3647 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3649 if (ret != 0) {
3650 switch (ret) {
3651 case EEXIST:
3652 /* There is a conflicting snapshot name. */
3653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3654 "conflicting snapshot '%s' from parent '%s'"),
3655 snapname, zhp->zfs_dmustats.dds_origin);
3656 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3658 default:
3659 return (zfs_standard_error(hdl, ret, errbuf));
3662 return (ret);
3665 typedef struct snapdata {
3666 nvlist_t *sd_nvl;
3667 const char *sd_snapname;
3668 } snapdata_t;
3670 static int
3671 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3673 snapdata_t *sd = arg;
3674 char name[ZFS_MAX_DATASET_NAME_LEN];
3675 int rv = 0;
3677 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3678 (void) snprintf(name, sizeof (name),
3679 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3681 fnvlist_add_boolean(sd->sd_nvl, name);
3683 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3685 zfs_close(zhp);
3687 return (rv);
3691 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3693 int err;
3694 char errbuf[1024];
3696 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3697 "cannot remap filesystem '%s' "), fs);
3699 err = lzc_remap(fs);
3701 if (err != 0) {
3702 (void) zfs_standard_error(hdl, err, errbuf);
3705 return (err);
3709 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3710 * created.
3713 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3715 int ret;
3716 char errbuf[1024];
3717 nvpair_t *elem;
3718 nvlist_t *errors;
3720 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3721 "cannot create snapshots "));
3723 elem = NULL;
3724 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3725 const char *snapname = nvpair_name(elem);
3727 /* validate the target name */
3728 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3729 B_TRUE)) {
3730 (void) snprintf(errbuf, sizeof (errbuf),
3731 dgettext(TEXT_DOMAIN,
3732 "cannot create snapshot '%s'"), snapname);
3733 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3738 * get pool handle for prop validation. assumes all snaps are in the
3739 * same pool, as does lzc_snapshot (below).
3741 char pool[ZFS_MAX_DATASET_NAME_LEN];
3742 elem = nvlist_next_nvpair(snaps, NULL);
3743 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3744 pool[strcspn(pool, "/@")] = '\0';
3745 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3747 if (props != NULL &&
3748 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3749 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3750 zpool_close(zpool_hdl);
3751 return (-1);
3753 zpool_close(zpool_hdl);
3755 ret = lzc_snapshot(snaps, props, &errors);
3757 if (ret != 0) {
3758 boolean_t printed = B_FALSE;
3759 for (elem = nvlist_next_nvpair(errors, NULL);
3760 elem != NULL;
3761 elem = nvlist_next_nvpair(errors, elem)) {
3762 (void) snprintf(errbuf, sizeof (errbuf),
3763 dgettext(TEXT_DOMAIN,
3764 "cannot create snapshot '%s'"), nvpair_name(elem));
3765 (void) zfs_standard_error(hdl,
3766 fnvpair_value_int32(elem), errbuf);
3767 printed = B_TRUE;
3769 if (!printed) {
3770 switch (ret) {
3771 case EXDEV:
3772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3773 "multiple snapshots of same "
3774 "fs not allowed"));
3775 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3777 break;
3778 default:
3779 (void) zfs_standard_error(hdl, ret, errbuf);
3784 nvlist_free(props);
3785 nvlist_free(errors);
3786 return (ret);
3790 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3791 nvlist_t *props)
3793 int ret;
3794 snapdata_t sd = { 0 };
3795 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3796 char *cp;
3797 zfs_handle_t *zhp;
3798 char errbuf[1024];
3800 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3801 "cannot snapshot %s"), path);
3803 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3804 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3806 (void) strlcpy(fsname, path, sizeof (fsname));
3807 cp = strchr(fsname, '@');
3808 *cp = '\0';
3809 sd.sd_snapname = cp + 1;
3811 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3812 ZFS_TYPE_VOLUME)) == NULL) {
3813 return (-1);
3816 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3817 if (recursive) {
3818 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3819 } else {
3820 fnvlist_add_boolean(sd.sd_nvl, path);
3823 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3824 nvlist_free(sd.sd_nvl);
3825 zfs_close(zhp);
3826 return (ret);
3830 * Destroy any more recent snapshots. We invoke this callback on any dependents
3831 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3832 * is a dependent and we should just destroy it without checking the transaction
3833 * group.
3835 typedef struct rollback_data {
3836 const char *cb_target; /* the snapshot */
3837 uint64_t cb_create; /* creation time reference */
3838 boolean_t cb_error;
3839 boolean_t cb_force;
3840 } rollback_data_t;
3842 static int
3843 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3845 rollback_data_t *cbp = data;
3846 prop_changelist_t *clp;
3848 /* We must destroy this clone; first unmount it */
3849 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3850 cbp->cb_force ? MS_FORCE: 0);
3851 if (clp == NULL || changelist_prefix(clp) != 0) {
3852 cbp->cb_error = B_TRUE;
3853 zfs_close(zhp);
3854 return (0);
3856 if (zfs_destroy(zhp, B_FALSE) != 0)
3857 cbp->cb_error = B_TRUE;
3858 else
3859 changelist_remove(clp, zhp->zfs_name);
3860 (void) changelist_postfix(clp);
3861 changelist_free(clp);
3863 zfs_close(zhp);
3864 return (0);
3867 static int
3868 rollback_destroy(zfs_handle_t *zhp, void *data)
3870 rollback_data_t *cbp = data;
3872 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3873 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3874 rollback_destroy_dependent, cbp);
3876 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3879 zfs_close(zhp);
3880 return (0);
3884 * Given a dataset, rollback to a specific snapshot, discarding any
3885 * data changes since then and making it the active dataset.
3887 * Any snapshots and bookmarks more recent than the target are
3888 * destroyed, along with their dependents (i.e. clones).
3891 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3893 rollback_data_t cb = { 0 };
3894 int err;
3895 boolean_t restore_resv = 0;
3896 uint64_t old_volsize = 0, new_volsize;
3897 zfs_prop_t resv_prop;
3899 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3900 zhp->zfs_type == ZFS_TYPE_VOLUME);
3903 * Destroy all recent snapshots and their dependents.
3905 cb.cb_force = force;
3906 cb.cb_target = snap->zfs_name;
3907 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3908 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3909 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3911 if (cb.cb_error)
3912 return (-1);
3915 * Now that we have verified that the snapshot is the latest,
3916 * rollback to the given snapshot.
3919 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3920 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3921 return (-1);
3922 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3923 restore_resv =
3924 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3928 * Pass both the filesystem and the wanted snapshot names,
3929 * we would get an error back if the snapshot is destroyed or
3930 * a new snapshot is created before this request is processed.
3932 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
3933 if (err == EXDEV) {
3934 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3935 "'%s' is not the latest snapshot"), snap->zfs_name);
3936 (void) zfs_error_fmt(zhp->zfs_hdl, EZFS_BUSY,
3937 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3938 zhp->zfs_name);
3939 return (err);
3940 } else if (err != 0) {
3941 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3942 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3943 zhp->zfs_name);
3944 return (err);
3948 * For volumes, if the pre-rollback volsize matched the pre-
3949 * rollback reservation and the volsize has changed then set
3950 * the reservation property to the post-rollback volsize.
3951 * Make a new handle since the rollback closed the dataset.
3953 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3954 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3955 if (restore_resv) {
3956 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3957 if (old_volsize != new_volsize)
3958 err = zfs_prop_set_int(zhp, resv_prop,
3959 new_volsize);
3961 zfs_close(zhp);
3963 return (err);
3967 * Renames the given dataset.
3970 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3971 boolean_t force_unmount)
3973 int ret = 0;
3974 zfs_cmd_t zc = { 0 };
3975 char *delim;
3976 prop_changelist_t *cl = NULL;
3977 zfs_handle_t *zhrp = NULL;
3978 char *parentname = NULL;
3979 char parent[ZFS_MAX_DATASET_NAME_LEN];
3980 libzfs_handle_t *hdl = zhp->zfs_hdl;
3981 char errbuf[1024];
3983 /* if we have the same exact name, just return success */
3984 if (strcmp(zhp->zfs_name, target) == 0)
3985 return (0);
3987 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3988 "cannot rename to '%s'"), target);
3991 * Make sure the target name is valid
3993 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3994 if ((strchr(target, '@') == NULL) ||
3995 *target == '@') {
3997 * Snapshot target name is abbreviated,
3998 * reconstruct full dataset name
4000 (void) strlcpy(parent, zhp->zfs_name,
4001 sizeof (parent));
4002 delim = strchr(parent, '@');
4003 if (strchr(target, '@') == NULL)
4004 *(++delim) = '\0';
4005 else
4006 *delim = '\0';
4007 (void) strlcat(parent, target, sizeof (parent));
4008 target = parent;
4009 } else {
4011 * Make sure we're renaming within the same dataset.
4013 delim = strchr(target, '@');
4014 if (strncmp(zhp->zfs_name, target, delim - target)
4015 != 0 || zhp->zfs_name[delim - target] != '@') {
4016 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4017 "snapshots must be part of same "
4018 "dataset"));
4019 return (zfs_error(hdl, EZFS_CROSSTARGET,
4020 errbuf));
4023 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4024 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4025 } else {
4026 if (recursive) {
4027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4028 "recursive rename must be a snapshot"));
4029 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4032 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4033 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4035 /* validate parents */
4036 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4037 return (-1);
4039 /* make sure we're in the same pool */
4040 verify((delim = strchr(target, '/')) != NULL);
4041 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4042 zhp->zfs_name[delim - target] != '/') {
4043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4044 "datasets must be within same pool"));
4045 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4048 /* new name cannot be a child of the current dataset name */
4049 if (is_descendant(zhp->zfs_name, target)) {
4050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4051 "New dataset name cannot be a descendant of "
4052 "current dataset name"));
4053 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4057 (void) snprintf(errbuf, sizeof (errbuf),
4058 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4060 if (getzoneid() == GLOBAL_ZONEID &&
4061 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4062 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4063 "dataset is used in a non-global zone"));
4064 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4067 if (recursive) {
4068 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4069 if (parentname == NULL) {
4070 ret = -1;
4071 goto error;
4073 delim = strchr(parentname, '@');
4074 *delim = '\0';
4075 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4076 if (zhrp == NULL) {
4077 ret = -1;
4078 goto error;
4080 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4081 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4082 force_unmount ? MS_FORCE : 0)) == NULL)
4083 return (-1);
4085 if (changelist_haszonedchild(cl)) {
4086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4087 "child dataset with inherited mountpoint is used "
4088 "in a non-global zone"));
4089 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4090 ret = -1;
4091 goto error;
4094 if ((ret = changelist_prefix(cl)) != 0)
4095 goto error;
4098 if (ZFS_IS_VOLUME(zhp))
4099 zc.zc_objset_type = DMU_OST_ZVOL;
4100 else
4101 zc.zc_objset_type = DMU_OST_ZFS;
4103 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4104 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4106 zc.zc_cookie = recursive;
4108 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4110 * if it was recursive, the one that actually failed will
4111 * be in zc.zc_name
4113 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4114 "cannot rename '%s'"), zc.zc_name);
4116 if (recursive && errno == EEXIST) {
4117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4118 "a child dataset already has a snapshot "
4119 "with the new name"));
4120 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4121 } else {
4122 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4126 * On failure, we still want to remount any filesystems that
4127 * were previously mounted, so we don't alter the system state.
4129 if (cl != NULL)
4130 (void) changelist_postfix(cl);
4131 } else {
4132 if (cl != NULL) {
4133 changelist_rename(cl, zfs_get_name(zhp), target);
4134 ret = changelist_postfix(cl);
4138 error:
4139 if (parentname != NULL) {
4140 free(parentname);
4142 if (zhrp != NULL) {
4143 zfs_close(zhrp);
4145 if (cl != NULL) {
4146 changelist_free(cl);
4148 return (ret);
4151 nvlist_t *
4152 zfs_get_user_props(zfs_handle_t *zhp)
4154 return (zhp->zfs_user_props);
4157 nvlist_t *
4158 zfs_get_recvd_props(zfs_handle_t *zhp)
4160 if (zhp->zfs_recvd_props == NULL)
4161 if (get_recvd_props_ioctl(zhp) != 0)
4162 return (NULL);
4163 return (zhp->zfs_recvd_props);
4167 * This function is used by 'zfs list' to determine the exact set of columns to
4168 * display, and their maximum widths. This does two main things:
4170 * - If this is a list of all properties, then expand the list to include
4171 * all native properties, and set a flag so that for each dataset we look
4172 * for new unique user properties and add them to the list.
4174 * - For non fixed-width properties, keep track of the maximum width seen
4175 * so that we can size the column appropriately. If the user has
4176 * requested received property values, we also need to compute the width
4177 * of the RECEIVED column.
4180 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4181 boolean_t literal)
4183 libzfs_handle_t *hdl = zhp->zfs_hdl;
4184 zprop_list_t *entry;
4185 zprop_list_t **last, **start;
4186 nvlist_t *userprops, *propval;
4187 nvpair_t *elem;
4188 char *strval;
4189 char buf[ZFS_MAXPROPLEN];
4191 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4192 return (-1);
4194 userprops = zfs_get_user_props(zhp);
4196 entry = *plp;
4197 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4199 * Go through and add any user properties as necessary. We
4200 * start by incrementing our list pointer to the first
4201 * non-native property.
4203 start = plp;
4204 while (*start != NULL) {
4205 if ((*start)->pl_prop == ZPROP_INVAL)
4206 break;
4207 start = &(*start)->pl_next;
4210 elem = NULL;
4211 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4213 * See if we've already found this property in our list.
4215 for (last = start; *last != NULL;
4216 last = &(*last)->pl_next) {
4217 if (strcmp((*last)->pl_user_prop,
4218 nvpair_name(elem)) == 0)
4219 break;
4222 if (*last == NULL) {
4223 if ((entry = zfs_alloc(hdl,
4224 sizeof (zprop_list_t))) == NULL ||
4225 ((entry->pl_user_prop = zfs_strdup(hdl,
4226 nvpair_name(elem)))) == NULL) {
4227 free(entry);
4228 return (-1);
4231 entry->pl_prop = ZPROP_INVAL;
4232 entry->pl_width = strlen(nvpair_name(elem));
4233 entry->pl_all = B_TRUE;
4234 *last = entry;
4240 * Now go through and check the width of any non-fixed columns
4242 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4243 if (entry->pl_fixed && !literal)
4244 continue;
4246 if (entry->pl_prop != ZPROP_INVAL) {
4247 if (zfs_prop_get(zhp, entry->pl_prop,
4248 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4249 if (strlen(buf) > entry->pl_width)
4250 entry->pl_width = strlen(buf);
4252 if (received && zfs_prop_get_recvd(zhp,
4253 zfs_prop_to_name(entry->pl_prop),
4254 buf, sizeof (buf), literal) == 0)
4255 if (strlen(buf) > entry->pl_recvd_width)
4256 entry->pl_recvd_width = strlen(buf);
4257 } else {
4258 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4259 &propval) == 0) {
4260 verify(nvlist_lookup_string(propval,
4261 ZPROP_VALUE, &strval) == 0);
4262 if (strlen(strval) > entry->pl_width)
4263 entry->pl_width = strlen(strval);
4265 if (received && zfs_prop_get_recvd(zhp,
4266 entry->pl_user_prop,
4267 buf, sizeof (buf), literal) == 0)
4268 if (strlen(buf) > entry->pl_recvd_width)
4269 entry->pl_recvd_width = strlen(buf);
4273 return (0);
4277 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4278 char *resource, void *export, void *sharetab,
4279 int sharemax, zfs_share_op_t operation)
4281 zfs_cmd_t zc = { 0 };
4282 int error;
4284 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4285 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4286 if (resource)
4287 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4288 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4289 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4290 zc.zc_share.z_sharetype = operation;
4291 zc.zc_share.z_sharemax = sharemax;
4292 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4293 return (error);
4296 void
4297 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4299 nvpair_t *curr;
4302 * Keep a reference to the props-table against which we prune the
4303 * properties.
4305 zhp->zfs_props_table = props;
4307 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4309 while (curr) {
4310 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4311 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4314 * User properties will result in ZPROP_INVAL, and since we
4315 * only know how to prune standard ZFS properties, we always
4316 * leave these in the list. This can also happen if we
4317 * encounter an unknown DSL property (when running older
4318 * software, for example).
4320 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4321 (void) nvlist_remove(zhp->zfs_props,
4322 nvpair_name(curr), nvpair_type(curr));
4323 curr = next;
4327 static int
4328 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4329 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4331 zfs_cmd_t zc = { 0 };
4332 nvlist_t *nvlist = NULL;
4333 int error;
4335 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4336 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4337 zc.zc_cookie = (uint64_t)cmd;
4339 if (cmd == ZFS_SMB_ACL_RENAME) {
4340 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4341 (void) no_memory(hdl);
4342 return (0);
4346 switch (cmd) {
4347 case ZFS_SMB_ACL_ADD:
4348 case ZFS_SMB_ACL_REMOVE:
4349 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4350 break;
4351 case ZFS_SMB_ACL_RENAME:
4352 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4353 resource1) != 0) {
4354 (void) no_memory(hdl);
4355 return (-1);
4357 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4358 resource2) != 0) {
4359 (void) no_memory(hdl);
4360 return (-1);
4362 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4363 nvlist_free(nvlist);
4364 return (-1);
4366 break;
4367 case ZFS_SMB_ACL_PURGE:
4368 break;
4369 default:
4370 return (-1);
4372 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4373 nvlist_free(nvlist);
4374 return (error);
4378 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4379 char *path, char *resource)
4381 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4382 resource, NULL));
4386 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4387 char *path, char *resource)
4389 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4390 resource, NULL));
4394 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4396 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4397 NULL, NULL));
4401 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4402 char *oldname, char *newname)
4404 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4405 oldname, newname));
4409 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4410 zfs_userspace_cb_t func, void *arg)
4412 zfs_cmd_t zc = { 0 };
4413 zfs_useracct_t buf[100];
4414 libzfs_handle_t *hdl = zhp->zfs_hdl;
4415 int ret;
4417 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4419 zc.zc_objset_type = type;
4420 zc.zc_nvlist_dst = (uintptr_t)buf;
4422 for (;;) {
4423 zfs_useracct_t *zua = buf;
4425 zc.zc_nvlist_dst_size = sizeof (buf);
4426 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4427 char errbuf[1024];
4429 (void) snprintf(errbuf, sizeof (errbuf),
4430 dgettext(TEXT_DOMAIN,
4431 "cannot get used/quota for %s"), zc.zc_name);
4432 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4434 if (zc.zc_nvlist_dst_size == 0)
4435 break;
4437 while (zc.zc_nvlist_dst_size > 0) {
4438 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4439 zua->zu_space)) != 0)
4440 return (ret);
4441 zua++;
4442 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4446 return (0);
4449 struct holdarg {
4450 nvlist_t *nvl;
4451 const char *snapname;
4452 const char *tag;
4453 boolean_t recursive;
4454 int error;
4457 static int
4458 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4460 struct holdarg *ha = arg;
4461 char name[ZFS_MAX_DATASET_NAME_LEN];
4462 int rv = 0;
4464 (void) snprintf(name, sizeof (name),
4465 "%s@%s", zhp->zfs_name, ha->snapname);
4467 if (lzc_exists(name))
4468 fnvlist_add_string(ha->nvl, name, ha->tag);
4470 if (ha->recursive)
4471 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4472 zfs_close(zhp);
4473 return (rv);
4477 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4478 boolean_t recursive, int cleanup_fd)
4480 int ret;
4481 struct holdarg ha;
4483 ha.nvl = fnvlist_alloc();
4484 ha.snapname = snapname;
4485 ha.tag = tag;
4486 ha.recursive = recursive;
4487 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4489 if (nvlist_empty(ha.nvl)) {
4490 char errbuf[1024];
4492 fnvlist_free(ha.nvl);
4493 ret = ENOENT;
4494 (void) snprintf(errbuf, sizeof (errbuf),
4495 dgettext(TEXT_DOMAIN,
4496 "cannot hold snapshot '%s@%s'"),
4497 zhp->zfs_name, snapname);
4498 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4499 return (ret);
4502 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4503 fnvlist_free(ha.nvl);
4505 return (ret);
4509 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4511 int ret;
4512 nvlist_t *errors;
4513 libzfs_handle_t *hdl = zhp->zfs_hdl;
4514 char errbuf[1024];
4515 nvpair_t *elem;
4517 errors = NULL;
4518 ret = lzc_hold(holds, cleanup_fd, &errors);
4520 if (ret == 0) {
4521 /* There may be errors even in the success case. */
4522 fnvlist_free(errors);
4523 return (0);
4526 if (nvlist_empty(errors)) {
4527 /* no hold-specific errors */
4528 (void) snprintf(errbuf, sizeof (errbuf),
4529 dgettext(TEXT_DOMAIN, "cannot hold"));
4530 switch (ret) {
4531 case ENOTSUP:
4532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4533 "pool must be upgraded"));
4534 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4535 break;
4536 case EINVAL:
4537 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4538 break;
4539 default:
4540 (void) zfs_standard_error(hdl, ret, errbuf);
4544 for (elem = nvlist_next_nvpair(errors, NULL);
4545 elem != NULL;
4546 elem = nvlist_next_nvpair(errors, elem)) {
4547 (void) snprintf(errbuf, sizeof (errbuf),
4548 dgettext(TEXT_DOMAIN,
4549 "cannot hold snapshot '%s'"), nvpair_name(elem));
4550 switch (fnvpair_value_int32(elem)) {
4551 case E2BIG:
4553 * Temporary tags wind up having the ds object id
4554 * prepended. So even if we passed the length check
4555 * above, it's still possible for the tag to wind
4556 * up being slightly too long.
4558 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4559 break;
4560 case EINVAL:
4561 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4562 break;
4563 case EEXIST:
4564 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4565 break;
4566 default:
4567 (void) zfs_standard_error(hdl,
4568 fnvpair_value_int32(elem), errbuf);
4572 fnvlist_free(errors);
4573 return (ret);
4576 static int
4577 zfs_release_one(zfs_handle_t *zhp, void *arg)
4579 struct holdarg *ha = arg;
4580 char name[ZFS_MAX_DATASET_NAME_LEN];
4581 int rv = 0;
4582 nvlist_t *existing_holds;
4584 (void) snprintf(name, sizeof (name),
4585 "%s@%s", zhp->zfs_name, ha->snapname);
4587 if (lzc_get_holds(name, &existing_holds) != 0) {
4588 ha->error = ENOENT;
4589 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4590 ha->error = ESRCH;
4591 } else {
4592 nvlist_t *torelease = fnvlist_alloc();
4593 fnvlist_add_boolean(torelease, ha->tag);
4594 fnvlist_add_nvlist(ha->nvl, name, torelease);
4595 fnvlist_free(torelease);
4598 if (ha->recursive)
4599 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4600 zfs_close(zhp);
4601 return (rv);
4605 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4606 boolean_t recursive)
4608 int ret;
4609 struct holdarg ha;
4610 nvlist_t *errors = NULL;
4611 nvpair_t *elem;
4612 libzfs_handle_t *hdl = zhp->zfs_hdl;
4613 char errbuf[1024];
4615 ha.nvl = fnvlist_alloc();
4616 ha.snapname = snapname;
4617 ha.tag = tag;
4618 ha.recursive = recursive;
4619 ha.error = 0;
4620 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4622 if (nvlist_empty(ha.nvl)) {
4623 fnvlist_free(ha.nvl);
4624 ret = ha.error;
4625 (void) snprintf(errbuf, sizeof (errbuf),
4626 dgettext(TEXT_DOMAIN,
4627 "cannot release hold from snapshot '%s@%s'"),
4628 zhp->zfs_name, snapname);
4629 if (ret == ESRCH) {
4630 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4631 } else {
4632 (void) zfs_standard_error(hdl, ret, errbuf);
4634 return (ret);
4637 ret = lzc_release(ha.nvl, &errors);
4638 fnvlist_free(ha.nvl);
4640 if (ret == 0) {
4641 /* There may be errors even in the success case. */
4642 fnvlist_free(errors);
4643 return (0);
4646 if (nvlist_empty(errors)) {
4647 /* no hold-specific errors */
4648 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4649 "cannot release"));
4650 switch (errno) {
4651 case ENOTSUP:
4652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4653 "pool must be upgraded"));
4654 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4655 break;
4656 default:
4657 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4661 for (elem = nvlist_next_nvpair(errors, NULL);
4662 elem != NULL;
4663 elem = nvlist_next_nvpair(errors, elem)) {
4664 (void) snprintf(errbuf, sizeof (errbuf),
4665 dgettext(TEXT_DOMAIN,
4666 "cannot release hold from snapshot '%s'"),
4667 nvpair_name(elem));
4668 switch (fnvpair_value_int32(elem)) {
4669 case ESRCH:
4670 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4671 break;
4672 case EINVAL:
4673 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4674 break;
4675 default:
4676 (void) zfs_standard_error_fmt(hdl,
4677 fnvpair_value_int32(elem), errbuf);
4681 fnvlist_free(errors);
4682 return (ret);
4686 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4688 zfs_cmd_t zc = { 0 };
4689 libzfs_handle_t *hdl = zhp->zfs_hdl;
4690 int nvsz = 2048;
4691 void *nvbuf;
4692 int err = 0;
4693 char errbuf[1024];
4695 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4696 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4698 tryagain:
4700 nvbuf = malloc(nvsz);
4701 if (nvbuf == NULL) {
4702 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4703 goto out;
4706 zc.zc_nvlist_dst_size = nvsz;
4707 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4709 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4711 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4712 (void) snprintf(errbuf, sizeof (errbuf),
4713 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4714 zc.zc_name);
4715 switch (errno) {
4716 case ENOMEM:
4717 free(nvbuf);
4718 nvsz = zc.zc_nvlist_dst_size;
4719 goto tryagain;
4721 case ENOTSUP:
4722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4723 "pool must be upgraded"));
4724 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4725 break;
4726 case EINVAL:
4727 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4728 break;
4729 case ENOENT:
4730 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4731 break;
4732 default:
4733 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4734 break;
4736 } else {
4737 /* success */
4738 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4739 if (rc) {
4740 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4741 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4742 zc.zc_name);
4743 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4747 free(nvbuf);
4748 out:
4749 return (err);
4753 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4755 zfs_cmd_t zc = { 0 };
4756 libzfs_handle_t *hdl = zhp->zfs_hdl;
4757 char *nvbuf;
4758 char errbuf[1024];
4759 size_t nvsz;
4760 int err;
4762 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4763 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4765 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4766 assert(err == 0);
4768 nvbuf = malloc(nvsz);
4770 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4771 assert(err == 0);
4773 zc.zc_nvlist_src_size = nvsz;
4774 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4775 zc.zc_perm_action = un;
4777 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4779 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4780 (void) snprintf(errbuf, sizeof (errbuf),
4781 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4782 zc.zc_name);
4783 switch (errno) {
4784 case ENOTSUP:
4785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4786 "pool must be upgraded"));
4787 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4788 break;
4789 case EINVAL:
4790 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4791 break;
4792 case ENOENT:
4793 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4794 break;
4795 default:
4796 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4797 break;
4801 free(nvbuf);
4803 return (err);
4807 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4809 int err;
4810 char errbuf[1024];
4812 err = lzc_get_holds(zhp->zfs_name, nvl);
4814 if (err != 0) {
4815 libzfs_handle_t *hdl = zhp->zfs_hdl;
4817 (void) snprintf(errbuf, sizeof (errbuf),
4818 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4819 zhp->zfs_name);
4820 switch (err) {
4821 case ENOTSUP:
4822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4823 "pool must be upgraded"));
4824 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4825 break;
4826 case EINVAL:
4827 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4828 break;
4829 case ENOENT:
4830 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4831 break;
4832 default:
4833 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4834 break;
4838 return (err);
4842 * Convert the zvol's volume size to an appropriate reservation.
4843 * Note: If this routine is updated, it is necessary to update the ZFS test
4844 * suite's shell version in reservation.kshlib.
4846 uint64_t
4847 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4849 uint64_t numdb;
4850 uint64_t nblocks, volblocksize;
4851 int ncopies;
4852 char *strval;
4854 if (nvlist_lookup_string(props,
4855 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4856 ncopies = atoi(strval);
4857 else
4858 ncopies = 1;
4859 if (nvlist_lookup_uint64(props,
4860 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4861 &volblocksize) != 0)
4862 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4863 nblocks = volsize/volblocksize;
4864 /* start with metadnode L0-L6 */
4865 numdb = 7;
4866 /* calculate number of indirects */
4867 while (nblocks > 1) {
4868 nblocks += DNODES_PER_LEVEL - 1;
4869 nblocks /= DNODES_PER_LEVEL;
4870 numdb += nblocks;
4872 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4873 volsize *= ncopies;
4875 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4876 * compressed, but in practice they compress down to about
4877 * 1100 bytes
4879 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4880 volsize += numdb;
4881 return (volsize);