Merge commit 'dfc115332c94a2f62058ac7f2bce7631fbd20b3d'
[unleashed/tickless.git] / usr / src / lib / libzfs / common / libzfs_dataset.c
blobe28d615fd72e0022d89dc74b86628f33d997ece7
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 2016 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 (void) zfs_prop_get_table();
109 if (entity_namecheck(path, &why, &what) != 0) {
110 if (hdl != NULL) {
111 switch (why) {
112 case NAME_ERR_TOOLONG:
113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
114 "name is too long"));
115 break;
117 case NAME_ERR_LEADING_SLASH:
118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
119 "leading slash in name"));
120 break;
122 case NAME_ERR_EMPTY_COMPONENT:
123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
124 "empty component in name"));
125 break;
127 case NAME_ERR_TRAILING_SLASH:
128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
129 "trailing slash in name"));
130 break;
132 case NAME_ERR_INVALCHAR:
133 zfs_error_aux(hdl,
134 dgettext(TEXT_DOMAIN, "invalid character "
135 "'%c' in name"), what);
136 break;
138 case NAME_ERR_MULTIPLE_DELIMITERS:
139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
140 "multiple '@' and/or '#' delimiters in "
141 "name"));
142 break;
144 case NAME_ERR_NOLETTER:
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "pool doesn't begin with a letter"));
147 break;
149 case NAME_ERR_RESERVED:
150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
151 "name is reserved"));
152 break;
154 case NAME_ERR_DISKLIKE:
155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
156 "reserved disk name"));
157 break;
159 default:
160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
161 "(%d) not defined"), why);
162 break;
166 return (0);
169 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
170 if (hdl != NULL)
171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
172 "snapshot delimiter '@' is not expected here"));
173 return (0);
176 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
177 if (hdl != NULL)
178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
179 "missing '@' delimiter in snapshot name"));
180 return (0);
183 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
184 if (hdl != NULL)
185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
186 "bookmark delimiter '#' is not expected here"));
187 return (0);
190 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
191 if (hdl != NULL)
192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193 "missing '#' delimiter in bookmark name"));
194 return (0);
197 if (modifying && strchr(path, '%') != NULL) {
198 if (hdl != NULL)
199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
200 "invalid character %c in name"), '%');
201 return (0);
204 return (-1);
208 zfs_name_valid(const char *name, zfs_type_t type)
210 if (type == ZFS_TYPE_POOL)
211 return (zpool_name_valid(NULL, B_FALSE, name));
212 return (zfs_validate_name(NULL, name, type, B_FALSE));
216 * This function takes the raw DSL properties, and filters out the user-defined
217 * properties into a separate nvlist.
219 static nvlist_t *
220 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
222 libzfs_handle_t *hdl = zhp->zfs_hdl;
223 nvpair_t *elem;
224 nvlist_t *propval;
225 nvlist_t *nvl;
227 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
228 (void) no_memory(hdl);
229 return (NULL);
232 elem = NULL;
233 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
234 if (!zfs_prop_user(nvpair_name(elem)))
235 continue;
237 verify(nvpair_value_nvlist(elem, &propval) == 0);
238 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
239 nvlist_free(nvl);
240 (void) no_memory(hdl);
241 return (NULL);
245 return (nvl);
248 static zpool_handle_t *
249 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
251 libzfs_handle_t *hdl = zhp->zfs_hdl;
252 zpool_handle_t *zph;
254 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
255 if (hdl->libzfs_pool_handles != NULL)
256 zph->zpool_next = hdl->libzfs_pool_handles;
257 hdl->libzfs_pool_handles = zph;
259 return (zph);
262 static zpool_handle_t *
263 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
265 libzfs_handle_t *hdl = zhp->zfs_hdl;
266 zpool_handle_t *zph = hdl->libzfs_pool_handles;
268 while ((zph != NULL) &&
269 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
270 zph = zph->zpool_next;
271 return (zph);
275 * Returns a handle to the pool that contains the provided dataset.
276 * If a handle to that pool already exists then that handle is returned.
277 * Otherwise, a new handle is created and added to the list of handles.
279 static zpool_handle_t *
280 zpool_handle(zfs_handle_t *zhp)
282 char *pool_name;
283 int len;
284 zpool_handle_t *zph;
286 len = strcspn(zhp->zfs_name, "/@#") + 1;
287 pool_name = zfs_alloc(zhp->zfs_hdl, len);
288 (void) strlcpy(pool_name, zhp->zfs_name, len);
290 zph = zpool_find_handle(zhp, pool_name, len);
291 if (zph == NULL)
292 zph = zpool_add_handle(zhp, pool_name);
294 free(pool_name);
295 return (zph);
298 void
299 zpool_free_handles(libzfs_handle_t *hdl)
301 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
303 while (zph != NULL) {
304 next = zph->zpool_next;
305 zpool_close(zph);
306 zph = next;
308 hdl->libzfs_pool_handles = NULL;
312 * Utility function to gather stats (objset and zpl) for the given object.
314 static int
315 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
317 libzfs_handle_t *hdl = zhp->zfs_hdl;
319 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
321 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
322 if (errno == ENOMEM) {
323 if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
324 return (-1);
326 } else {
327 return (-1);
330 return (0);
334 * Utility function to get the received properties of the given object.
336 static int
337 get_recvd_props_ioctl(zfs_handle_t *zhp)
339 libzfs_handle_t *hdl = zhp->zfs_hdl;
340 nvlist_t *recvdprops;
341 zfs_cmd_t zc = { 0 };
342 int err;
344 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
345 return (-1);
347 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
349 while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
350 if (errno == ENOMEM) {
351 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
352 return (-1);
354 } else {
355 zcmd_free_nvlists(&zc);
356 return (-1);
360 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
361 zcmd_free_nvlists(&zc);
362 if (err != 0)
363 return (-1);
365 nvlist_free(zhp->zfs_recvd_props);
366 zhp->zfs_recvd_props = recvdprops;
368 return (0);
371 static int
372 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
374 nvlist_t *allprops, *userprops;
376 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
378 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
379 return (-1);
383 * XXX Why do we store the user props separately, in addition to
384 * storing them in zfs_props?
386 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
387 nvlist_free(allprops);
388 return (-1);
391 nvlist_free(zhp->zfs_props);
392 nvlist_free(zhp->zfs_user_props);
394 zhp->zfs_props = allprops;
395 zhp->zfs_user_props = userprops;
397 return (0);
400 static int
401 get_stats(zfs_handle_t *zhp)
403 int rc = 0;
404 zfs_cmd_t zc = { 0 };
406 if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
407 return (-1);
408 if (get_stats_ioctl(zhp, &zc) != 0)
409 rc = -1;
410 else if (put_stats_zhdl(zhp, &zc) != 0)
411 rc = -1;
412 zcmd_free_nvlists(&zc);
413 return (rc);
417 * Refresh the properties currently stored in the handle.
419 void
420 zfs_refresh_properties(zfs_handle_t *zhp)
422 (void) get_stats(zhp);
426 * Makes a handle from the given dataset name. Used by zfs_open() and
427 * zfs_iter_* to create child handles on the fly.
429 static int
430 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
432 if (put_stats_zhdl(zhp, zc) != 0)
433 return (-1);
436 * We've managed to open the dataset and gather statistics. Determine
437 * the high-level type.
439 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
440 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
441 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
442 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
443 else
444 abort();
446 if (zhp->zfs_dmustats.dds_is_snapshot)
447 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
448 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
449 zhp->zfs_type = ZFS_TYPE_VOLUME;
450 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
451 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
452 else
453 abort(); /* we should never see any other types */
455 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
456 return (-1);
458 return (0);
461 zfs_handle_t *
462 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
464 zfs_cmd_t zc = { 0 };
466 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
468 if (zhp == NULL)
469 return (NULL);
471 zhp->zfs_hdl = hdl;
472 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
473 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
474 free(zhp);
475 return (NULL);
477 if (get_stats_ioctl(zhp, &zc) == -1) {
478 zcmd_free_nvlists(&zc);
479 free(zhp);
480 return (NULL);
482 if (make_dataset_handle_common(zhp, &zc) == -1) {
483 free(zhp);
484 zhp = NULL;
486 zcmd_free_nvlists(&zc);
487 return (zhp);
490 zfs_handle_t *
491 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
493 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
495 if (zhp == NULL)
496 return (NULL);
498 zhp->zfs_hdl = hdl;
499 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
500 if (make_dataset_handle_common(zhp, zc) == -1) {
501 free(zhp);
502 return (NULL);
504 return (zhp);
507 zfs_handle_t *
508 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
510 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
512 if (zhp == NULL)
513 return (NULL);
515 zhp->zfs_hdl = pzhp->zfs_hdl;
516 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
517 zhp->zfs_head_type = pzhp->zfs_type;
518 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
519 zhp->zpool_hdl = zpool_handle(zhp);
520 return (zhp);
523 zfs_handle_t *
524 zfs_handle_dup(zfs_handle_t *zhp_orig)
526 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
528 if (zhp == NULL)
529 return (NULL);
531 zhp->zfs_hdl = zhp_orig->zfs_hdl;
532 zhp->zpool_hdl = zhp_orig->zpool_hdl;
533 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
534 sizeof (zhp->zfs_name));
535 zhp->zfs_type = zhp_orig->zfs_type;
536 zhp->zfs_head_type = zhp_orig->zfs_head_type;
537 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
538 if (zhp_orig->zfs_props != NULL) {
539 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
540 (void) no_memory(zhp->zfs_hdl);
541 zfs_close(zhp);
542 return (NULL);
545 if (zhp_orig->zfs_user_props != NULL) {
546 if (nvlist_dup(zhp_orig->zfs_user_props,
547 &zhp->zfs_user_props, 0) != 0) {
548 (void) no_memory(zhp->zfs_hdl);
549 zfs_close(zhp);
550 return (NULL);
553 if (zhp_orig->zfs_recvd_props != NULL) {
554 if (nvlist_dup(zhp_orig->zfs_recvd_props,
555 &zhp->zfs_recvd_props, 0)) {
556 (void) no_memory(zhp->zfs_hdl);
557 zfs_close(zhp);
558 return (NULL);
561 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
562 if (zhp_orig->zfs_mntopts != NULL) {
563 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
564 zhp_orig->zfs_mntopts);
566 zhp->zfs_props_table = zhp_orig->zfs_props_table;
567 return (zhp);
570 boolean_t
571 zfs_bookmark_exists(const char *path)
573 nvlist_t *bmarks;
574 nvlist_t *props;
575 char fsname[ZFS_MAX_DATASET_NAME_LEN];
576 char *bmark_name;
577 char *pound;
578 int err;
579 boolean_t rv;
582 (void) strlcpy(fsname, path, sizeof (fsname));
583 pound = strchr(fsname, '#');
584 if (pound == NULL)
585 return (B_FALSE);
587 *pound = '\0';
588 bmark_name = pound + 1;
589 props = fnvlist_alloc();
590 err = lzc_get_bookmarks(fsname, props, &bmarks);
591 nvlist_free(props);
592 if (err != 0) {
593 nvlist_free(bmarks);
594 return (B_FALSE);
597 rv = nvlist_exists(bmarks, bmark_name);
598 nvlist_free(bmarks);
599 return (rv);
602 zfs_handle_t *
603 make_bookmark_handle(zfs_handle_t *parent, const char *path,
604 nvlist_t *bmark_props)
606 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
608 if (zhp == NULL)
609 return (NULL);
611 /* Fill in the name. */
612 zhp->zfs_hdl = parent->zfs_hdl;
613 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
615 /* Set the property lists. */
616 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
617 free(zhp);
618 return (NULL);
621 /* Set the types. */
622 zhp->zfs_head_type = parent->zfs_head_type;
623 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
625 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
626 nvlist_free(zhp->zfs_props);
627 free(zhp);
628 return (NULL);
631 return (zhp);
634 struct zfs_open_bookmarks_cb_data {
635 const char *path;
636 zfs_handle_t *zhp;
639 static int
640 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
642 struct zfs_open_bookmarks_cb_data *dp = data;
645 * Is it the one we are looking for?
647 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
649 * We found it. Save it and let the caller know we are done.
651 dp->zhp = zhp;
652 return (EEXIST);
656 * Not found. Close the handle and ask for another one.
658 zfs_close(zhp);
659 return (0);
663 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
664 * argument is a mask of acceptable types. The function will print an
665 * appropriate error message and return NULL if it can't be opened.
667 zfs_handle_t *
668 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
670 zfs_handle_t *zhp;
671 char errbuf[1024];
672 char *bookp;
674 (void) snprintf(errbuf, sizeof (errbuf),
675 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
678 * Validate the name before we even try to open it.
680 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
681 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
682 return (NULL);
686 * Bookmarks needs to be handled separately.
688 bookp = strchr(path, '#');
689 if (bookp == NULL) {
691 * Try to get stats for the dataset, which will tell us if it
692 * exists.
694 errno = 0;
695 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
696 (void) zfs_standard_error(hdl, errno, errbuf);
697 return (NULL);
699 } else {
700 char dsname[ZFS_MAX_DATASET_NAME_LEN];
701 zfs_handle_t *pzhp;
702 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
705 * We need to cut out '#' and everything after '#'
706 * to get the parent dataset name only.
708 assert(bookp - path < sizeof (dsname));
709 (void) strncpy(dsname, path, bookp - path);
710 dsname[bookp - path] = '\0';
713 * Create handle for the parent dataset.
715 errno = 0;
716 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
717 (void) zfs_standard_error(hdl, errno, errbuf);
718 return (NULL);
722 * Iterate bookmarks to find the right one.
724 errno = 0;
725 if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
726 &cb_data) == 0) && (cb_data.zhp == NULL)) {
727 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
728 zfs_close(pzhp);
729 return (NULL);
731 if (cb_data.zhp == NULL) {
732 (void) zfs_standard_error(hdl, errno, errbuf);
733 zfs_close(pzhp);
734 return (NULL);
736 zhp = cb_data.zhp;
739 * Cleanup.
741 zfs_close(pzhp);
744 if (!(types & zhp->zfs_type)) {
745 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
746 zfs_close(zhp);
747 return (NULL);
750 return (zhp);
754 * Release a ZFS handle. Nothing to do but free the associated memory.
756 void
757 zfs_close(zfs_handle_t *zhp)
759 free(zhp->zfs_mntopts);
760 nvlist_free(zhp->zfs_props);
761 nvlist_free(zhp->zfs_user_props);
762 nvlist_free(zhp->zfs_recvd_props);
763 free(zhp);
766 typedef struct mnttab_node {
767 struct mnttab mtn_mt;
768 avl_node_t mtn_node;
769 } mnttab_node_t;
771 static int
772 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
774 const mnttab_node_t *mtn1 = arg1;
775 const mnttab_node_t *mtn2 = arg2;
776 int rv;
778 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
780 if (rv == 0)
781 return (0);
782 return (rv > 0 ? 1 : -1);
785 void
786 libzfs_mnttab_init(libzfs_handle_t *hdl)
788 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
789 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
790 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
793 void
794 libzfs_mnttab_update(libzfs_handle_t *hdl)
796 struct mnttab entry;
798 rewind(hdl->libzfs_mnttab);
799 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
800 mnttab_node_t *mtn;
802 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
803 continue;
804 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
805 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
806 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
807 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
808 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
809 avl_add(&hdl->libzfs_mnttab_cache, mtn);
813 void
814 libzfs_mnttab_fini(libzfs_handle_t *hdl)
816 void *cookie = NULL;
817 mnttab_node_t *mtn;
819 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
820 != NULL) {
821 free(mtn->mtn_mt.mnt_special);
822 free(mtn->mtn_mt.mnt_mountp);
823 free(mtn->mtn_mt.mnt_fstype);
824 free(mtn->mtn_mt.mnt_mntopts);
825 free(mtn);
827 avl_destroy(&hdl->libzfs_mnttab_cache);
830 void
831 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
833 hdl->libzfs_mnttab_enable = enable;
837 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
838 struct mnttab *entry)
840 mnttab_node_t find;
841 mnttab_node_t *mtn;
843 if (!hdl->libzfs_mnttab_enable) {
844 struct mnttab srch = { 0 };
846 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
847 libzfs_mnttab_fini(hdl);
848 rewind(hdl->libzfs_mnttab);
849 srch.mnt_special = (char *)fsname;
850 srch.mnt_fstype = MNTTYPE_ZFS;
851 if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
852 return (0);
853 else
854 return (ENOENT);
857 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
858 libzfs_mnttab_update(hdl);
860 find.mtn_mt.mnt_special = (char *)fsname;
861 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
862 if (mtn) {
863 *entry = mtn->mtn_mt;
864 return (0);
866 return (ENOENT);
869 void
870 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
871 const char *mountp, const char *mntopts)
873 mnttab_node_t *mtn;
875 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
876 return;
877 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
878 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
879 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
880 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
881 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
882 avl_add(&hdl->libzfs_mnttab_cache, mtn);
885 void
886 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
888 mnttab_node_t find;
889 mnttab_node_t *ret;
891 find.mtn_mt.mnt_special = (char *)fsname;
892 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
893 != NULL) {
894 avl_remove(&hdl->libzfs_mnttab_cache, ret);
895 free(ret->mtn_mt.mnt_special);
896 free(ret->mtn_mt.mnt_mountp);
897 free(ret->mtn_mt.mnt_fstype);
898 free(ret->mtn_mt.mnt_mntopts);
899 free(ret);
904 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
906 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
908 if (zpool_handle == NULL)
909 return (-1);
911 *spa_version = zpool_get_prop_int(zpool_handle,
912 ZPOOL_PROP_VERSION, NULL);
913 return (0);
917 * The choice of reservation property depends on the SPA version.
919 static int
920 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
922 int spa_version;
924 if (zfs_spa_version(zhp, &spa_version) < 0)
925 return (-1);
927 if (spa_version >= SPA_VERSION_REFRESERVATION)
928 *resv_prop = ZFS_PROP_REFRESERVATION;
929 else
930 *resv_prop = ZFS_PROP_RESERVATION;
932 return (0);
936 * Given an nvlist of properties to set, validates that they are correct, and
937 * parses any numeric properties (index, boolean, etc) if they are specified as
938 * strings.
940 nvlist_t *
941 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
942 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
943 const char *errbuf)
945 nvpair_t *elem;
946 uint64_t intval;
947 char *strval;
948 zfs_prop_t prop;
949 nvlist_t *ret;
950 int chosen_normal = -1;
951 int chosen_utf = -1;
953 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
954 (void) no_memory(hdl);
955 return (NULL);
959 * Make sure this property is valid and applies to this type.
962 elem = NULL;
963 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
964 const char *propname = nvpair_name(elem);
966 prop = zfs_name_to_prop(propname);
967 if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
969 * This is a user property: make sure it's a
970 * string, and that it's less than ZAP_MAXNAMELEN.
972 if (nvpair_type(elem) != DATA_TYPE_STRING) {
973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
974 "'%s' must be a string"), propname);
975 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
976 goto error;
979 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
981 "property name '%s' is too long"),
982 propname);
983 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
984 goto error;
987 (void) nvpair_value_string(elem, &strval);
988 if (nvlist_add_string(ret, propname, strval) != 0) {
989 (void) no_memory(hdl);
990 goto error;
992 continue;
996 * Currently, only user properties can be modified on
997 * snapshots.
999 if (type == ZFS_TYPE_SNAPSHOT) {
1000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001 "this property can not be modified for snapshots"));
1002 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1003 goto error;
1006 if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1007 zfs_userquota_prop_t uqtype;
1008 char newpropname[128];
1009 char domain[128];
1010 uint64_t rid;
1011 uint64_t valary[3];
1013 if (userquota_propname_decode(propname, zoned,
1014 &uqtype, domain, sizeof (domain), &rid) != 0) {
1015 zfs_error_aux(hdl,
1016 dgettext(TEXT_DOMAIN,
1017 "'%s' has an invalid user/group name"),
1018 propname);
1019 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1020 goto error;
1023 if (uqtype != ZFS_PROP_USERQUOTA &&
1024 uqtype != ZFS_PROP_GROUPQUOTA) {
1025 zfs_error_aux(hdl,
1026 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1027 propname);
1028 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1029 errbuf);
1030 goto error;
1033 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1034 (void) nvpair_value_string(elem, &strval);
1035 if (strcmp(strval, "none") == 0) {
1036 intval = 0;
1037 } else if (zfs_nicestrtonum(hdl,
1038 strval, &intval) != 0) {
1039 (void) zfs_error(hdl,
1040 EZFS_BADPROP, errbuf);
1041 goto error;
1043 } else if (nvpair_type(elem) ==
1044 DATA_TYPE_UINT64) {
1045 (void) nvpair_value_uint64(elem, &intval);
1046 if (intval == 0) {
1047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1048 "use 'none' to disable "
1049 "userquota/groupquota"));
1050 goto error;
1052 } else {
1053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1054 "'%s' must be a number"), propname);
1055 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1056 goto error;
1060 * Encode the prop name as
1061 * userquota@<hex-rid>-domain, to make it easy
1062 * for the kernel to decode.
1064 (void) snprintf(newpropname, sizeof (newpropname),
1065 "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1066 (longlong_t)rid, domain);
1067 valary[0] = uqtype;
1068 valary[1] = rid;
1069 valary[2] = intval;
1070 if (nvlist_add_uint64_array(ret, newpropname,
1071 valary, 3) != 0) {
1072 (void) no_memory(hdl);
1073 goto error;
1075 continue;
1076 } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1078 "'%s' is readonly"),
1079 propname);
1080 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1081 goto error;
1084 if (prop == ZPROP_INVAL) {
1085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1086 "invalid property '%s'"), propname);
1087 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1088 goto error;
1091 if (!zfs_prop_valid_for_type(prop, type)) {
1092 zfs_error_aux(hdl,
1093 dgettext(TEXT_DOMAIN, "'%s' does not "
1094 "apply to datasets of this type"), propname);
1095 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1096 goto error;
1099 if (zfs_prop_readonly(prop) &&
1100 (!zfs_prop_setonce(prop) || zhp != NULL)) {
1101 zfs_error_aux(hdl,
1102 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1103 propname);
1104 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1105 goto error;
1108 if (zprop_parse_value(hdl, elem, prop, type, ret,
1109 &strval, &intval, errbuf) != 0)
1110 goto error;
1113 * Perform some additional checks for specific properties.
1115 switch (prop) {
1116 case ZFS_PROP_VERSION:
1118 int version;
1120 if (zhp == NULL)
1121 break;
1122 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1123 if (intval < version) {
1124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1125 "Can not downgrade; already at version %u"),
1126 version);
1127 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1128 goto error;
1130 break;
1133 case ZFS_PROP_VOLBLOCKSIZE:
1134 case ZFS_PROP_RECORDSIZE:
1136 int maxbs = SPA_MAXBLOCKSIZE;
1137 if (zpool_hdl != NULL) {
1138 maxbs = zpool_get_prop_int(zpool_hdl,
1139 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1142 * Volumes are limited to a volblocksize of 128KB,
1143 * because they typically service workloads with
1144 * small random writes, which incur a large performance
1145 * penalty with large blocks.
1147 if (prop == ZFS_PROP_VOLBLOCKSIZE)
1148 maxbs = SPA_OLD_MAXBLOCKSIZE;
1150 * The value must be a power of two between
1151 * SPA_MINBLOCKSIZE and maxbs.
1153 if (intval < SPA_MINBLOCKSIZE ||
1154 intval > maxbs || !ISP2(intval)) {
1155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1156 "'%s' must be power of 2 from 512B "
1157 "to %uKB"), propname, maxbs >> 10);
1158 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1159 goto error;
1161 break;
1163 case ZFS_PROP_MOUNTPOINT:
1165 namecheck_err_t why;
1167 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1168 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1169 break;
1171 if (mountpoint_namecheck(strval, &why)) {
1172 switch (why) {
1173 case NAME_ERR_LEADING_SLASH:
1174 zfs_error_aux(hdl,
1175 dgettext(TEXT_DOMAIN,
1176 "'%s' must be an absolute path, "
1177 "'none', or 'legacy'"), propname);
1178 break;
1179 case NAME_ERR_TOOLONG:
1180 zfs_error_aux(hdl,
1181 dgettext(TEXT_DOMAIN,
1182 "component of '%s' is too long"),
1183 propname);
1184 break;
1186 default:
1187 zfs_error_aux(hdl,
1188 dgettext(TEXT_DOMAIN,
1189 "(%d) not defined"),
1190 why);
1191 break;
1193 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1194 goto error;
1198 /*FALLTHRU*/
1200 case ZFS_PROP_SHARESMB:
1201 case ZFS_PROP_SHARENFS:
1203 * For the mountpoint and sharenfs or sharesmb
1204 * properties, check if it can be set in a
1205 * global/non-global zone based on
1206 * the zoned property value:
1208 * global zone non-global zone
1209 * --------------------------------------------------
1210 * zoned=on mountpoint (no) mountpoint (yes)
1211 * sharenfs (no) sharenfs (no)
1212 * sharesmb (no) sharesmb (no)
1214 * zoned=off mountpoint (yes) N/A
1215 * sharenfs (yes)
1216 * sharesmb (yes)
1218 if (zoned) {
1219 if (getzoneid() == GLOBAL_ZONEID) {
1220 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1221 "'%s' cannot be set on "
1222 "dataset in a non-global zone"),
1223 propname);
1224 (void) zfs_error(hdl, EZFS_ZONED,
1225 errbuf);
1226 goto error;
1227 } else if (prop == ZFS_PROP_SHARENFS ||
1228 prop == ZFS_PROP_SHARESMB) {
1229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1230 "'%s' cannot be set in "
1231 "a non-global zone"), propname);
1232 (void) zfs_error(hdl, EZFS_ZONED,
1233 errbuf);
1234 goto error;
1236 } else if (getzoneid() != GLOBAL_ZONEID) {
1238 * If zoned property is 'off', this must be in
1239 * a global zone. If not, something is wrong.
1241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1242 "'%s' cannot be set while dataset "
1243 "'zoned' property is set"), propname);
1244 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1245 goto error;
1249 * At this point, it is legitimate to set the
1250 * property. Now we want to make sure that the
1251 * property value is valid if it is sharenfs.
1253 if ((prop == ZFS_PROP_SHARENFS ||
1254 prop == ZFS_PROP_SHARESMB) &&
1255 strcmp(strval, "on") != 0 &&
1256 strcmp(strval, "off") != 0) {
1257 zfs_share_proto_t proto;
1259 if (prop == ZFS_PROP_SHARESMB)
1260 proto = PROTO_SMB;
1261 else
1262 proto = PROTO_NFS;
1265 * Must be an valid sharing protocol
1266 * option string so init the libshare
1267 * in order to enable the parser and
1268 * then parse the options. We use the
1269 * control API since we don't care about
1270 * the current configuration and don't
1271 * want the overhead of loading it
1272 * until we actually do something.
1275 if (zfs_init_libshare(hdl,
1276 SA_INIT_CONTROL_API) != SA_OK) {
1278 * An error occurred so we can't do
1279 * anything
1281 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1282 "'%s' cannot be set: problem "
1283 "in share initialization"),
1284 propname);
1285 (void) zfs_error(hdl, EZFS_BADPROP,
1286 errbuf);
1287 goto error;
1290 if (zfs_parse_options(strval, proto) != SA_OK) {
1292 * There was an error in parsing so
1293 * deal with it by issuing an error
1294 * message and leaving after
1295 * uninitializing the the libshare
1296 * interface.
1298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1299 "'%s' cannot be set to invalid "
1300 "options"), propname);
1301 (void) zfs_error(hdl, EZFS_BADPROP,
1302 errbuf);
1303 zfs_uninit_libshare(hdl);
1304 goto error;
1306 zfs_uninit_libshare(hdl);
1309 break;
1311 case ZFS_PROP_UTF8ONLY:
1312 chosen_utf = (int)intval;
1313 break;
1315 case ZFS_PROP_NORMALIZE:
1316 chosen_normal = (int)intval;
1317 break;
1319 default:
1320 break;
1324 * For changes to existing volumes, we have some additional
1325 * checks to enforce.
1327 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1328 uint64_t volsize = zfs_prop_get_int(zhp,
1329 ZFS_PROP_VOLSIZE);
1330 uint64_t blocksize = zfs_prop_get_int(zhp,
1331 ZFS_PROP_VOLBLOCKSIZE);
1332 char buf[64];
1334 switch (prop) {
1335 case ZFS_PROP_RESERVATION:
1336 case ZFS_PROP_REFRESERVATION:
1337 if (intval > volsize) {
1338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1339 "'%s' is greater than current "
1340 "volume size"), propname);
1341 (void) zfs_error(hdl, EZFS_BADPROP,
1342 errbuf);
1343 goto error;
1345 break;
1347 case ZFS_PROP_VOLSIZE:
1348 if (intval % blocksize != 0) {
1349 zfs_nicenum(blocksize, buf,
1350 sizeof (buf));
1351 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352 "'%s' must be a multiple of "
1353 "volume block size (%s)"),
1354 propname, buf);
1355 (void) zfs_error(hdl, EZFS_BADPROP,
1356 errbuf);
1357 goto error;
1360 if (intval == 0) {
1361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1362 "'%s' cannot be zero"),
1363 propname);
1364 (void) zfs_error(hdl, EZFS_BADPROP,
1365 errbuf);
1366 goto error;
1368 break;
1370 default:
1371 break;
1377 * If normalization was chosen, but no UTF8 choice was made,
1378 * enforce rejection of non-UTF8 names.
1380 * If normalization was chosen, but rejecting non-UTF8 names
1381 * was explicitly not chosen, it is an error.
1383 if (chosen_normal > 0 && chosen_utf < 0) {
1384 if (nvlist_add_uint64(ret,
1385 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1386 (void) no_memory(hdl);
1387 goto error;
1389 } else if (chosen_normal > 0 && chosen_utf == 0) {
1390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1391 "'%s' must be set 'on' if normalization chosen"),
1392 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1393 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1394 goto error;
1396 return (ret);
1398 error:
1399 nvlist_free(ret);
1400 return (NULL);
1404 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1406 uint64_t old_volsize;
1407 uint64_t new_volsize;
1408 uint64_t old_reservation;
1409 uint64_t new_reservation;
1410 zfs_prop_t resv_prop;
1411 nvlist_t *props;
1414 * If this is an existing volume, and someone is setting the volsize,
1415 * make sure that it matches the reservation, or add it if necessary.
1417 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1418 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1419 return (-1);
1420 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1422 props = fnvlist_alloc();
1423 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1424 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1426 if ((zvol_volsize_to_reservation(old_volsize, props) !=
1427 old_reservation) || nvlist_exists(nvl,
1428 zfs_prop_to_name(resv_prop))) {
1429 fnvlist_free(props);
1430 return (0);
1432 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1433 &new_volsize) != 0) {
1434 fnvlist_free(props);
1435 return (-1);
1437 new_reservation = zvol_volsize_to_reservation(new_volsize, props);
1438 fnvlist_free(props);
1440 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1441 new_reservation) != 0) {
1442 (void) no_memory(zhp->zfs_hdl);
1443 return (-1);
1445 return (1);
1448 void
1449 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1450 char *errbuf)
1452 switch (err) {
1454 case ENOSPC:
1456 * For quotas and reservations, ENOSPC indicates
1457 * something different; setting a quota or reservation
1458 * doesn't use any disk space.
1460 switch (prop) {
1461 case ZFS_PROP_QUOTA:
1462 case ZFS_PROP_REFQUOTA:
1463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1464 "size is less than current used or "
1465 "reserved space"));
1466 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1467 break;
1469 case ZFS_PROP_RESERVATION:
1470 case ZFS_PROP_REFRESERVATION:
1471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1472 "size is greater than available space"));
1473 (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1474 break;
1476 default:
1477 (void) zfs_standard_error(hdl, err, errbuf);
1478 break;
1480 break;
1482 case EBUSY:
1483 (void) zfs_standard_error(hdl, EBUSY, errbuf);
1484 break;
1486 case EROFS:
1487 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1488 break;
1490 case E2BIG:
1491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1492 "property value too long"));
1493 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1494 break;
1496 case ENOTSUP:
1497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1498 "pool and or dataset must be upgraded to set this "
1499 "property or value"));
1500 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1501 break;
1503 case ERANGE:
1504 if (prop == ZFS_PROP_COMPRESSION ||
1505 prop == ZFS_PROP_RECORDSIZE) {
1506 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1507 "property setting is not allowed on "
1508 "bootable datasets"));
1509 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1510 } else if (prop == ZFS_PROP_CHECKSUM ||
1511 prop == ZFS_PROP_DEDUP) {
1512 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1513 "property setting is not allowed on "
1514 "root pools"));
1515 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1516 } else {
1517 (void) zfs_standard_error(hdl, err, errbuf);
1519 break;
1521 case EINVAL:
1522 if (prop == ZPROP_INVAL) {
1523 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1524 } else {
1525 (void) zfs_standard_error(hdl, err, errbuf);
1527 break;
1529 case EOVERFLOW:
1531 * This platform can't address a volume this big.
1533 #ifdef _ILP32
1534 if (prop == ZFS_PROP_VOLSIZE) {
1535 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1536 break;
1538 #endif
1539 /* FALLTHROUGH */
1540 default:
1541 (void) zfs_standard_error(hdl, err, errbuf);
1546 * Given a property name and value, set the property for the given dataset.
1549 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1551 int ret = -1;
1552 char errbuf[1024];
1553 libzfs_handle_t *hdl = zhp->zfs_hdl;
1554 nvlist_t *nvl = NULL;
1556 (void) snprintf(errbuf, sizeof (errbuf),
1557 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1558 zhp->zfs_name);
1560 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1561 nvlist_add_string(nvl, propname, propval) != 0) {
1562 (void) no_memory(hdl);
1563 goto error;
1566 ret = zfs_prop_set_list(zhp, nvl);
1568 error:
1569 nvlist_free(nvl);
1570 return (ret);
1576 * Given an nvlist of property names and values, set the properties for the
1577 * given dataset.
1580 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1582 zfs_cmd_t zc = { 0 };
1583 int ret = -1;
1584 prop_changelist_t **cls = NULL;
1585 int cl_idx;
1586 char errbuf[1024];
1587 libzfs_handle_t *hdl = zhp->zfs_hdl;
1588 nvlist_t *nvl;
1589 int nvl_len;
1590 int added_resv = 0;
1592 (void) snprintf(errbuf, sizeof (errbuf),
1593 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1594 zhp->zfs_name);
1596 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1597 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1598 errbuf)) == NULL)
1599 goto error;
1602 * We have to check for any extra properties which need to be added
1603 * before computing the length of the nvlist.
1605 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1606 elem != NULL;
1607 elem = nvlist_next_nvpair(nvl, elem)) {
1608 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1609 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1610 goto error;
1614 * Check how many properties we're setting and allocate an array to
1615 * store changelist pointers for postfix().
1617 nvl_len = 0;
1618 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1619 elem != NULL;
1620 elem = nvlist_next_nvpair(nvl, elem))
1621 nvl_len++;
1622 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1623 goto error;
1625 cl_idx = 0;
1626 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1627 elem != NULL;
1628 elem = nvlist_next_nvpair(nvl, elem)) {
1630 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1632 assert(cl_idx < nvl_len);
1634 * We don't want to unmount & remount the dataset when changing
1635 * its canmount property to 'on' or 'noauto'. We only use
1636 * the changelist logic to unmount when setting canmount=off.
1638 if (prop != ZFS_PROP_CANMOUNT ||
1639 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1640 zfs_is_mounted(zhp, NULL))) {
1641 cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1642 if (cls[cl_idx] == NULL)
1643 goto error;
1646 if (prop == ZFS_PROP_MOUNTPOINT &&
1647 changelist_haszonedchild(cls[cl_idx])) {
1648 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1649 "child dataset with inherited mountpoint is used "
1650 "in a non-global zone"));
1651 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1652 goto error;
1655 if (cls[cl_idx] != NULL &&
1656 (ret = changelist_prefix(cls[cl_idx])) != 0)
1657 goto error;
1659 cl_idx++;
1661 assert(cl_idx == nvl_len);
1664 * Execute the corresponding ioctl() to set this list of properties.
1666 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1668 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1669 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1670 goto error;
1672 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1674 if (ret != 0) {
1675 /* Get the list of unset properties back and report them. */
1676 nvlist_t *errorprops = NULL;
1677 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1678 goto error;
1679 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1680 elem != NULL;
1681 elem = nvlist_next_nvpair(nvl, elem)) {
1682 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1683 zfs_setprop_error(hdl, prop, errno, errbuf);
1685 nvlist_free(errorprops);
1687 if (added_resv && errno == ENOSPC) {
1688 /* clean up the volsize property we tried to set */
1689 uint64_t old_volsize = zfs_prop_get_int(zhp,
1690 ZFS_PROP_VOLSIZE);
1691 nvlist_free(nvl);
1692 nvl = NULL;
1693 zcmd_free_nvlists(&zc);
1695 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1696 goto error;
1697 if (nvlist_add_uint64(nvl,
1698 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1699 old_volsize) != 0)
1700 goto error;
1701 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1702 goto error;
1703 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1705 } else {
1706 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1707 if (cls[cl_idx] != NULL) {
1708 int clp_err = changelist_postfix(cls[cl_idx]);
1709 if (clp_err != 0)
1710 ret = clp_err;
1715 * Refresh the statistics so the new property value
1716 * is reflected.
1718 if (ret == 0)
1719 (void) get_stats(zhp);
1722 error:
1723 nvlist_free(nvl);
1724 zcmd_free_nvlists(&zc);
1725 if (cls != NULL) {
1726 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1727 if (cls[cl_idx] != NULL)
1728 changelist_free(cls[cl_idx]);
1730 free(cls);
1732 return (ret);
1736 * Given a property, inherit the value from the parent dataset, or if received
1737 * is TRUE, revert to the received value, if any.
1740 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1742 zfs_cmd_t zc = { 0 };
1743 int ret;
1744 prop_changelist_t *cl;
1745 libzfs_handle_t *hdl = zhp->zfs_hdl;
1746 char errbuf[1024];
1747 zfs_prop_t prop;
1749 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1750 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1752 zc.zc_cookie = received;
1753 if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
1755 * For user properties, the amount of work we have to do is very
1756 * small, so just do it here.
1758 if (!zfs_prop_user(propname)) {
1759 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1760 "invalid property"));
1761 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1764 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1765 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1767 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1768 return (zfs_standard_error(hdl, errno, errbuf));
1770 return (0);
1774 * Verify that this property is inheritable.
1776 if (zfs_prop_readonly(prop))
1777 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1779 if (!zfs_prop_inheritable(prop) && !received)
1780 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1783 * Check to see if the value applies to this type
1785 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1786 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1789 * Normalize the name, to get rid of shorthand abbreviations.
1791 propname = zfs_prop_to_name(prop);
1792 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1793 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1795 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1796 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1797 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1798 "dataset is used in a non-global zone"));
1799 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1803 * Determine datasets which will be affected by this change, if any.
1805 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1806 return (-1);
1808 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1809 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1810 "child dataset with inherited mountpoint is used "
1811 "in a non-global zone"));
1812 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1813 goto error;
1816 if ((ret = changelist_prefix(cl)) != 0)
1817 goto error;
1819 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
1820 return (zfs_standard_error(hdl, errno, errbuf));
1821 } else {
1823 if ((ret = changelist_postfix(cl)) != 0)
1824 goto error;
1827 * Refresh the statistics so the new property is reflected.
1829 (void) get_stats(zhp);
1832 error:
1833 changelist_free(cl);
1834 return (ret);
1838 * True DSL properties are stored in an nvlist. The following two functions
1839 * extract them appropriately.
1841 static uint64_t
1842 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1844 nvlist_t *nv;
1845 uint64_t value;
1847 *source = NULL;
1848 if (nvlist_lookup_nvlist(zhp->zfs_props,
1849 zfs_prop_to_name(prop), &nv) == 0) {
1850 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
1851 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1852 } else {
1853 verify(!zhp->zfs_props_table ||
1854 zhp->zfs_props_table[prop] == B_TRUE);
1855 value = zfs_prop_default_numeric(prop);
1856 *source = "";
1859 return (value);
1862 static const char *
1863 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
1865 nvlist_t *nv;
1866 const char *value;
1868 *source = NULL;
1869 if (nvlist_lookup_nvlist(zhp->zfs_props,
1870 zfs_prop_to_name(prop), &nv) == 0) {
1871 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1872 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1873 } else {
1874 verify(!zhp->zfs_props_table ||
1875 zhp->zfs_props_table[prop] == B_TRUE);
1876 value = zfs_prop_default_string(prop);
1877 *source = "";
1880 return (value);
1883 static boolean_t
1884 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1886 return (zhp->zfs_props == zhp->zfs_recvd_props);
1889 static void
1890 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1892 *cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
1893 zhp->zfs_props = zhp->zfs_recvd_props;
1896 static void
1897 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
1899 zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
1900 *cookie = 0;
1904 * Internal function for getting a numeric property. Both zfs_prop_get() and
1905 * zfs_prop_get_int() are built using this interface.
1907 * Certain properties can be overridden using 'mount -o'. In this case, scan
1908 * the contents of the /etc/mnttab entry, searching for the appropriate options.
1909 * If they differ from the on-disk values, report the current values and mark
1910 * the source "temporary".
1912 static int
1913 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1914 char **source, uint64_t *val)
1916 zfs_cmd_t zc = { 0 };
1917 nvlist_t *zplprops = NULL;
1918 struct mnttab mnt;
1919 char *mntopt_on = NULL;
1920 char *mntopt_off = NULL;
1921 boolean_t received = zfs_is_recvd_props_mode(zhp);
1923 *source = NULL;
1925 switch (prop) {
1926 case ZFS_PROP_ATIME:
1927 mntopt_on = MNTOPT_ATIME;
1928 mntopt_off = MNTOPT_NOATIME;
1929 break;
1931 case ZFS_PROP_DEVICES:
1932 mntopt_on = MNTOPT_DEVICES;
1933 mntopt_off = MNTOPT_NODEVICES;
1934 break;
1936 case ZFS_PROP_EXEC:
1937 mntopt_on = MNTOPT_EXEC;
1938 mntopt_off = MNTOPT_NOEXEC;
1939 break;
1941 case ZFS_PROP_READONLY:
1942 mntopt_on = MNTOPT_RO;
1943 mntopt_off = MNTOPT_RW;
1944 break;
1946 case ZFS_PROP_SETUID:
1947 mntopt_on = MNTOPT_SETUID;
1948 mntopt_off = MNTOPT_NOSETUID;
1949 break;
1951 case ZFS_PROP_XATTR:
1952 mntopt_on = MNTOPT_XATTR;
1953 mntopt_off = MNTOPT_NOXATTR;
1954 break;
1956 case ZFS_PROP_NBMAND:
1957 mntopt_on = MNTOPT_NBMAND;
1958 mntopt_off = MNTOPT_NONBMAND;
1959 break;
1961 default:
1962 break;
1966 * Because looking up the mount options is potentially expensive
1967 * (iterating over all of /etc/mnttab), we defer its calculation until
1968 * we're looking up a property which requires its presence.
1970 if (!zhp->zfs_mntcheck &&
1971 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
1972 libzfs_handle_t *hdl = zhp->zfs_hdl;
1973 struct mnttab entry;
1975 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
1976 zhp->zfs_mntopts = zfs_strdup(hdl,
1977 entry.mnt_mntopts);
1978 if (zhp->zfs_mntopts == NULL)
1979 return (-1);
1982 zhp->zfs_mntcheck = B_TRUE;
1985 if (zhp->zfs_mntopts == NULL)
1986 mnt.mnt_mntopts = "";
1987 else
1988 mnt.mnt_mntopts = zhp->zfs_mntopts;
1990 switch (prop) {
1991 case ZFS_PROP_ATIME:
1992 case ZFS_PROP_DEVICES:
1993 case ZFS_PROP_EXEC:
1994 case ZFS_PROP_READONLY:
1995 case ZFS_PROP_SETUID:
1996 case ZFS_PROP_XATTR:
1997 case ZFS_PROP_NBMAND:
1998 *val = getprop_uint64(zhp, prop, source);
2000 if (received)
2001 break;
2003 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2004 *val = B_TRUE;
2005 if (src)
2006 *src = ZPROP_SRC_TEMPORARY;
2007 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2008 *val = B_FALSE;
2009 if (src)
2010 *src = ZPROP_SRC_TEMPORARY;
2012 break;
2014 case ZFS_PROP_CANMOUNT:
2015 case ZFS_PROP_VOLSIZE:
2016 case ZFS_PROP_QUOTA:
2017 case ZFS_PROP_REFQUOTA:
2018 case ZFS_PROP_RESERVATION:
2019 case ZFS_PROP_REFRESERVATION:
2020 case ZFS_PROP_FILESYSTEM_LIMIT:
2021 case ZFS_PROP_SNAPSHOT_LIMIT:
2022 case ZFS_PROP_FILESYSTEM_COUNT:
2023 case ZFS_PROP_SNAPSHOT_COUNT:
2024 *val = getprop_uint64(zhp, prop, source);
2026 if (*source == NULL) {
2027 /* not default, must be local */
2028 *source = zhp->zfs_name;
2030 break;
2032 case ZFS_PROP_MOUNTED:
2033 *val = (zhp->zfs_mntopts != NULL);
2034 break;
2036 case ZFS_PROP_NUMCLONES:
2037 *val = zhp->zfs_dmustats.dds_num_clones;
2038 break;
2040 case ZFS_PROP_VERSION:
2041 case ZFS_PROP_NORMALIZE:
2042 case ZFS_PROP_UTF8ONLY:
2043 case ZFS_PROP_CASE:
2044 if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2045 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2046 return (-1);
2047 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2048 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2049 zcmd_free_nvlists(&zc);
2050 return (-1);
2052 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2053 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2054 val) != 0) {
2055 zcmd_free_nvlists(&zc);
2056 return (-1);
2058 nvlist_free(zplprops);
2059 zcmd_free_nvlists(&zc);
2060 break;
2062 case ZFS_PROP_INCONSISTENT:
2063 *val = zhp->zfs_dmustats.dds_inconsistent;
2064 break;
2066 default:
2067 switch (zfs_prop_get_type(prop)) {
2068 case PROP_TYPE_NUMBER:
2069 case PROP_TYPE_INDEX:
2070 *val = getprop_uint64(zhp, prop, source);
2072 * If we tried to use a default value for a
2073 * readonly property, it means that it was not
2074 * present. Note this only applies to "truly"
2075 * readonly properties, not set-once properties
2076 * like volblocksize.
2078 if (zfs_prop_readonly(prop) &&
2079 !zfs_prop_setonce(prop) &&
2080 *source != NULL && (*source)[0] == '\0') {
2081 *source = NULL;
2082 return (-1);
2084 break;
2086 case PROP_TYPE_STRING:
2087 default:
2088 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2089 "cannot get non-numeric property"));
2090 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2091 dgettext(TEXT_DOMAIN, "internal error")));
2095 return (0);
2099 * Calculate the source type, given the raw source string.
2101 static void
2102 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2103 char *statbuf, size_t statlen)
2105 if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2106 return;
2108 if (source == NULL) {
2109 *srctype = ZPROP_SRC_NONE;
2110 } else if (source[0] == '\0') {
2111 *srctype = ZPROP_SRC_DEFAULT;
2112 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2113 *srctype = ZPROP_SRC_RECEIVED;
2114 } else {
2115 if (strcmp(source, zhp->zfs_name) == 0) {
2116 *srctype = ZPROP_SRC_LOCAL;
2117 } else {
2118 (void) strlcpy(statbuf, source, statlen);
2119 *srctype = ZPROP_SRC_INHERITED;
2126 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2127 size_t proplen, boolean_t literal)
2129 zfs_prop_t prop;
2130 int err = 0;
2132 if (zhp->zfs_recvd_props == NULL)
2133 if (get_recvd_props_ioctl(zhp) != 0)
2134 return (-1);
2136 prop = zfs_name_to_prop(propname);
2138 if (prop != ZPROP_INVAL) {
2139 uint64_t cookie;
2140 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2141 return (-1);
2142 zfs_set_recvd_props_mode(zhp, &cookie);
2143 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2144 NULL, NULL, 0, literal);
2145 zfs_unset_recvd_props_mode(zhp, &cookie);
2146 } else {
2147 nvlist_t *propval;
2148 char *recvdval;
2149 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2150 propname, &propval) != 0)
2151 return (-1);
2152 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2153 &recvdval) == 0);
2154 (void) strlcpy(propbuf, recvdval, proplen);
2157 return (err == 0 ? 0 : -1);
2160 static int
2161 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2163 nvlist_t *value;
2164 nvpair_t *pair;
2166 value = zfs_get_clones_nvl(zhp);
2167 if (value == NULL)
2168 return (-1);
2170 propbuf[0] = '\0';
2171 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2172 pair = nvlist_next_nvpair(value, pair)) {
2173 if (propbuf[0] != '\0')
2174 (void) strlcat(propbuf, ",", proplen);
2175 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2178 return (0);
2181 struct get_clones_arg {
2182 uint64_t numclones;
2183 nvlist_t *value;
2184 const char *origin;
2185 char buf[ZFS_MAX_DATASET_NAME_LEN];
2189 get_clones_cb(zfs_handle_t *zhp, void *arg)
2191 struct get_clones_arg *gca = arg;
2193 if (gca->numclones == 0) {
2194 zfs_close(zhp);
2195 return (0);
2198 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2199 NULL, NULL, 0, B_TRUE) != 0)
2200 goto out;
2201 if (strcmp(gca->buf, gca->origin) == 0) {
2202 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2203 gca->numclones--;
2206 out:
2207 (void) zfs_iter_children(zhp, get_clones_cb, gca);
2208 zfs_close(zhp);
2209 return (0);
2212 nvlist_t *
2213 zfs_get_clones_nvl(zfs_handle_t *zhp)
2215 nvlist_t *nv, *value;
2217 if (nvlist_lookup_nvlist(zhp->zfs_props,
2218 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2219 struct get_clones_arg gca;
2222 * if this is a snapshot, then the kernel wasn't able
2223 * to get the clones. Do it by slowly iterating.
2225 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2226 return (NULL);
2227 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2228 return (NULL);
2229 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2230 nvlist_free(nv);
2231 return (NULL);
2234 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2235 gca.value = value;
2236 gca.origin = zhp->zfs_name;
2238 if (gca.numclones != 0) {
2239 zfs_handle_t *root;
2240 char pool[ZFS_MAX_DATASET_NAME_LEN];
2241 char *cp = pool;
2243 /* get the pool name */
2244 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2245 (void) strsep(&cp, "/@");
2246 root = zfs_open(zhp->zfs_hdl, pool,
2247 ZFS_TYPE_FILESYSTEM);
2249 (void) get_clones_cb(root, &gca);
2252 if (gca.numclones != 0 ||
2253 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2254 nvlist_add_nvlist(zhp->zfs_props,
2255 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2256 nvlist_free(nv);
2257 nvlist_free(value);
2258 return (NULL);
2260 nvlist_free(nv);
2261 nvlist_free(value);
2262 verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2263 zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2266 verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2268 return (value);
2272 * Accepts a property and value and checks that the value
2273 * matches the one found by the channel program. If they are
2274 * not equal, print both of them.
2276 void
2277 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2278 const char *strval)
2280 if (!zhp->zfs_hdl->libzfs_prop_debug)
2281 return;
2282 int error;
2283 char *poolname = zhp->zpool_hdl->zpool_name;
2284 const char *program =
2285 "args = ...\n"
2286 "ds = args['dataset']\n"
2287 "prop = args['property']\n"
2288 "value, setpoint = zfs.get_prop(ds, prop)\n"
2289 "return {value=value, setpoint=setpoint}\n";
2290 nvlist_t *outnvl;
2291 nvlist_t *retnvl;
2292 nvlist_t *argnvl = fnvlist_alloc();
2294 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2295 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2297 error = lzc_channel_program(poolname, program,
2298 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2300 if (error == 0) {
2301 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2302 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2303 int64_t ans;
2304 error = nvlist_lookup_int64(retnvl, "value", &ans);
2305 if (error != 0) {
2306 (void) fprintf(stderr, "zcp check error: %u\n",
2307 error);
2308 return;
2310 if (ans != intval) {
2311 (void) fprintf(stderr,
2312 "%s: zfs found %lld, but zcp found %lld\n",
2313 zfs_prop_to_name(prop),
2314 (longlong_t)intval, (longlong_t)ans);
2316 } else {
2317 char *str_ans;
2318 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2319 if (error != 0) {
2320 (void) fprintf(stderr, "zcp check error: %u\n",
2321 error);
2322 return;
2324 if (strcmp(strval, str_ans) != 0) {
2325 (void) fprintf(stderr,
2326 "%s: zfs found %s, but zcp found %s\n",
2327 zfs_prop_to_name(prop),
2328 strval, str_ans);
2331 } else {
2332 (void) fprintf(stderr,
2333 "zcp check failed, channel program error: %u\n", error);
2335 nvlist_free(argnvl);
2336 nvlist_free(outnvl);
2340 * Retrieve a property from the given object. If 'literal' is specified, then
2341 * numbers are left as exact values. Otherwise, numbers are converted to a
2342 * human-readable form.
2344 * Returns 0 on success, or -1 on error.
2347 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2348 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2350 char *source = NULL;
2351 uint64_t val;
2352 const char *str;
2353 const char *strval;
2354 boolean_t received = zfs_is_recvd_props_mode(zhp);
2357 * Check to see if this property applies to our object
2359 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2360 return (-1);
2362 if (received && zfs_prop_readonly(prop))
2363 return (-1);
2365 if (src)
2366 *src = ZPROP_SRC_NONE;
2368 switch (prop) {
2369 case ZFS_PROP_CREATION:
2371 * 'creation' is a time_t stored in the statistics. We convert
2372 * this into a string unless 'literal' is specified.
2375 val = getprop_uint64(zhp, prop, &source);
2376 time_t time = (time_t)val;
2377 struct tm t;
2379 if (literal ||
2380 localtime_r(&time, &t) == NULL ||
2381 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2382 &t) == 0)
2383 (void) snprintf(propbuf, proplen, "%llu", val);
2385 zcp_check(zhp, prop, val, NULL);
2386 break;
2388 case ZFS_PROP_MOUNTPOINT:
2390 * Getting the precise mountpoint can be tricky.
2392 * - for 'none' or 'legacy', return those values.
2393 * - for inherited mountpoints, we want to take everything
2394 * after our ancestor and append it to the inherited value.
2396 * If the pool has an alternate root, we want to prepend that
2397 * root to any values we return.
2400 str = getprop_string(zhp, prop, &source);
2402 if (str[0] == '/') {
2403 char buf[MAXPATHLEN];
2404 char *root = buf;
2405 const char *relpath;
2408 * If we inherit the mountpoint, even from a dataset
2409 * with a received value, the source will be the path of
2410 * the dataset we inherit from. If source is
2411 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2412 * inherited.
2414 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2415 relpath = "";
2416 } else {
2417 relpath = zhp->zfs_name + strlen(source);
2418 if (relpath[0] == '/')
2419 relpath++;
2422 if ((zpool_get_prop(zhp->zpool_hdl,
2423 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2424 B_FALSE)) || (strcmp(root, "-") == 0))
2425 root[0] = '\0';
2427 * Special case an alternate root of '/'. This will
2428 * avoid having multiple leading slashes in the
2429 * mountpoint path.
2431 if (strcmp(root, "/") == 0)
2432 root++;
2435 * If the mountpoint is '/' then skip over this
2436 * if we are obtaining either an alternate root or
2437 * an inherited mountpoint.
2439 if (str[1] == '\0' && (root[0] != '\0' ||
2440 relpath[0] != '\0'))
2441 str++;
2443 if (relpath[0] == '\0')
2444 (void) snprintf(propbuf, proplen, "%s%s",
2445 root, str);
2446 else
2447 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2448 root, str, relpath[0] == '@' ? "" : "/",
2449 relpath);
2450 } else {
2451 /* 'legacy' or 'none' */
2452 (void) strlcpy(propbuf, str, proplen);
2454 zcp_check(zhp, prop, 0, propbuf);
2455 break;
2457 case ZFS_PROP_ORIGIN:
2458 str = getprop_string(zhp, prop, &source);
2459 if (str == NULL)
2460 return (-1);
2461 (void) strlcpy(propbuf, str, proplen);
2462 zcp_check(zhp, prop, 0, str);
2463 break;
2465 case ZFS_PROP_CLONES:
2466 if (get_clones_string(zhp, propbuf, proplen) != 0)
2467 return (-1);
2468 break;
2470 case ZFS_PROP_QUOTA:
2471 case ZFS_PROP_REFQUOTA:
2472 case ZFS_PROP_RESERVATION:
2473 case ZFS_PROP_REFRESERVATION:
2475 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2476 return (-1);
2478 * If quota or reservation is 0, we translate this into 'none'
2479 * (unless literal is set), and indicate that it's the default
2480 * value. Otherwise, we print the number nicely and indicate
2481 * that its set locally.
2483 if (val == 0) {
2484 if (literal)
2485 (void) strlcpy(propbuf, "0", proplen);
2486 else
2487 (void) strlcpy(propbuf, "none", proplen);
2488 } else {
2489 if (literal)
2490 (void) snprintf(propbuf, proplen, "%llu",
2491 (u_longlong_t)val);
2492 else
2493 zfs_nicenum(val, propbuf, proplen);
2495 zcp_check(zhp, prop, val, NULL);
2496 break;
2498 case ZFS_PROP_FILESYSTEM_LIMIT:
2499 case ZFS_PROP_SNAPSHOT_LIMIT:
2500 case ZFS_PROP_FILESYSTEM_COUNT:
2501 case ZFS_PROP_SNAPSHOT_COUNT:
2503 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2504 return (-1);
2507 * If limit is UINT64_MAX, we translate this into 'none' (unless
2508 * literal is set), and indicate that it's the default value.
2509 * Otherwise, we print the number nicely and indicate that it's
2510 * set locally.
2512 if (literal) {
2513 (void) snprintf(propbuf, proplen, "%llu",
2514 (u_longlong_t)val);
2515 } else if (val == UINT64_MAX) {
2516 (void) strlcpy(propbuf, "none", proplen);
2517 } else {
2518 zfs_nicenum(val, propbuf, proplen);
2521 zcp_check(zhp, prop, val, NULL);
2522 break;
2524 case ZFS_PROP_REFRATIO:
2525 case ZFS_PROP_COMPRESSRATIO:
2526 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2527 return (-1);
2528 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2529 (u_longlong_t)(val / 100),
2530 (u_longlong_t)(val % 100));
2531 zcp_check(zhp, prop, val, NULL);
2532 break;
2534 case ZFS_PROP_TYPE:
2535 switch (zhp->zfs_type) {
2536 case ZFS_TYPE_FILESYSTEM:
2537 str = "filesystem";
2538 break;
2539 case ZFS_TYPE_VOLUME:
2540 str = "volume";
2541 break;
2542 case ZFS_TYPE_SNAPSHOT:
2543 str = "snapshot";
2544 break;
2545 case ZFS_TYPE_BOOKMARK:
2546 str = "bookmark";
2547 break;
2548 default:
2549 abort();
2551 (void) snprintf(propbuf, proplen, "%s", str);
2552 zcp_check(zhp, prop, 0, propbuf);
2553 break;
2555 case ZFS_PROP_MOUNTED:
2557 * The 'mounted' property is a pseudo-property that described
2558 * whether the filesystem is currently mounted. Even though
2559 * it's a boolean value, the typical values of "on" and "off"
2560 * don't make sense, so we translate to "yes" and "no".
2562 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2563 src, &source, &val) != 0)
2564 return (-1);
2565 if (val)
2566 (void) strlcpy(propbuf, "yes", proplen);
2567 else
2568 (void) strlcpy(propbuf, "no", proplen);
2569 break;
2571 case ZFS_PROP_NAME:
2573 * The 'name' property is a pseudo-property derived from the
2574 * dataset name. It is presented as a real property to simplify
2575 * consumers.
2577 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2578 zcp_check(zhp, prop, 0, propbuf);
2579 break;
2581 case ZFS_PROP_GUID:
2583 * GUIDs are stored as numbers, but they are identifiers.
2584 * We don't want them to be pretty printed, because pretty
2585 * printing mangles the ID into a truncated and useless value.
2587 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2588 return (-1);
2589 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2590 zcp_check(zhp, prop, val, NULL);
2591 break;
2593 default:
2594 switch (zfs_prop_get_type(prop)) {
2595 case PROP_TYPE_NUMBER:
2596 if (get_numeric_property(zhp, prop, src,
2597 &source, &val) != 0) {
2598 return (-1);
2601 if (literal) {
2602 (void) snprintf(propbuf, proplen, "%llu",
2603 (u_longlong_t)val);
2604 } else {
2605 zfs_nicenum(val, propbuf, proplen);
2607 zcp_check(zhp, prop, val, NULL);
2608 break;
2610 case PROP_TYPE_STRING:
2611 str = getprop_string(zhp, prop, &source);
2612 if (str == NULL)
2613 return (-1);
2615 (void) strlcpy(propbuf, str, proplen);
2616 zcp_check(zhp, prop, 0, str);
2617 break;
2619 case PROP_TYPE_INDEX:
2620 if (get_numeric_property(zhp, prop, src,
2621 &source, &val) != 0)
2622 return (-1);
2623 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2624 return (-1);
2626 (void) strlcpy(propbuf, strval, proplen);
2627 zcp_check(zhp, prop, 0, strval);
2628 break;
2630 default:
2631 abort();
2635 get_source(zhp, src, source, statbuf, statlen);
2637 return (0);
2641 * Utility function to get the given numeric property. Does no validation that
2642 * the given property is the appropriate type; should only be used with
2643 * hard-coded property types.
2645 uint64_t
2646 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2648 char *source;
2649 uint64_t val;
2651 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
2653 return (val);
2657 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2659 char buf[64];
2661 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2662 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2666 * Similar to zfs_prop_get(), but returns the value as an integer.
2669 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2670 zprop_source_t *src, char *statbuf, size_t statlen)
2672 char *source;
2675 * Check to see if this property applies to our object
2677 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2678 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2679 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2680 zfs_prop_to_name(prop)));
2683 if (src)
2684 *src = ZPROP_SRC_NONE;
2686 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2687 return (-1);
2689 get_source(zhp, src, source, statbuf, statlen);
2691 return (0);
2694 static int
2695 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2696 char **domainp, idmap_rid_t *ridp)
2698 idmap_get_handle_t *get_hdl = NULL;
2699 idmap_stat status;
2700 int err = EINVAL;
2702 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2703 goto out;
2705 if (isuser) {
2706 err = idmap_get_sidbyuid(get_hdl, id,
2707 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2708 } else {
2709 err = idmap_get_sidbygid(get_hdl, id,
2710 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2712 if (err == IDMAP_SUCCESS &&
2713 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2714 status == IDMAP_SUCCESS)
2715 err = 0;
2716 else
2717 err = EINVAL;
2718 out:
2719 if (get_hdl)
2720 idmap_get_destroy(get_hdl);
2721 return (err);
2725 * convert the propname into parameters needed by kernel
2726 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2727 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2729 static int
2730 userquota_propname_decode(const char *propname, boolean_t zoned,
2731 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2733 zfs_userquota_prop_t type;
2734 char *cp, *end;
2735 char *numericsid = NULL;
2736 boolean_t isuser;
2738 domain[0] = '\0';
2739 *ridp = 0;
2740 /* Figure out the property type ({user|group}{quota|space}) */
2741 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2742 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2743 strlen(zfs_userquota_prop_prefixes[type])) == 0)
2744 break;
2746 if (type == ZFS_NUM_USERQUOTA_PROPS)
2747 return (EINVAL);
2748 *typep = type;
2750 isuser = (type == ZFS_PROP_USERQUOTA ||
2751 type == ZFS_PROP_USERUSED);
2753 cp = strchr(propname, '@') + 1;
2755 if (strchr(cp, '@')) {
2757 * It's a SID name (eg "user@domain") that needs to be
2758 * turned into S-1-domainID-RID.
2760 int flag = 0;
2761 idmap_stat stat, map_stat;
2762 uid_t pid;
2763 idmap_rid_t rid;
2764 idmap_get_handle_t *gh = NULL;
2766 stat = idmap_get_create(&gh);
2767 if (stat != IDMAP_SUCCESS) {
2768 idmap_get_destroy(gh);
2769 return (ENOMEM);
2771 if (zoned && getzoneid() == GLOBAL_ZONEID)
2772 return (ENOENT);
2773 if (isuser) {
2774 stat = idmap_getuidbywinname(cp, NULL, flag, &pid);
2775 if (stat < 0)
2776 return (ENOENT);
2777 stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid,
2778 &rid, &map_stat);
2779 } else {
2780 stat = idmap_getgidbywinname(cp, NULL, flag, &pid);
2781 if (stat < 0)
2782 return (ENOENT);
2783 stat = idmap_get_sidbygid(gh, pid, flag, &numericsid,
2784 &rid, &map_stat);
2786 if (stat < 0) {
2787 idmap_get_destroy(gh);
2788 return (ENOENT);
2790 stat = idmap_get_mappings(gh);
2791 idmap_get_destroy(gh);
2793 if (stat < 0) {
2794 return (ENOENT);
2796 if (numericsid == NULL)
2797 return (ENOENT);
2798 cp = numericsid;
2799 *ridp = rid;
2800 /* will be further decoded below */
2803 if (strncmp(cp, "S-1-", 4) == 0) {
2804 /* It's a numeric SID (eg "S-1-234-567-89") */
2805 (void) strlcpy(domain, cp, domainlen);
2806 errno = 0;
2807 if (*ridp == 0) {
2808 cp = strrchr(domain, '-');
2809 *cp = '\0';
2810 cp++;
2811 *ridp = strtoull(cp, &end, 10);
2812 } else {
2813 end = "";
2815 if (numericsid) {
2816 free(numericsid);
2817 numericsid = NULL;
2819 if (errno != 0 || *end != '\0')
2820 return (EINVAL);
2821 } else if (!isdigit(*cp)) {
2823 * It's a user/group name (eg "user") that needs to be
2824 * turned into a uid/gid
2826 if (zoned && getzoneid() == GLOBAL_ZONEID)
2827 return (ENOENT);
2828 if (isuser) {
2829 struct passwd *pw;
2830 pw = getpwnam(cp);
2831 if (pw == NULL)
2832 return (ENOENT);
2833 *ridp = pw->pw_uid;
2834 } else {
2835 struct group *gr;
2836 gr = getgrnam(cp);
2837 if (gr == NULL)
2838 return (ENOENT);
2839 *ridp = gr->gr_gid;
2841 } else {
2842 /* It's a user/group ID (eg "12345"). */
2843 uid_t id = strtoul(cp, &end, 10);
2844 idmap_rid_t rid;
2845 char *mapdomain;
2847 if (*end != '\0')
2848 return (EINVAL);
2849 if (id > MAXUID) {
2850 /* It's an ephemeral ID. */
2851 if (idmap_id_to_numeric_domain_rid(id, isuser,
2852 &mapdomain, &rid) != 0)
2853 return (ENOENT);
2854 (void) strlcpy(domain, mapdomain, domainlen);
2855 *ridp = rid;
2856 } else {
2857 *ridp = id;
2861 ASSERT3P(numericsid, ==, NULL);
2862 return (0);
2865 static int
2866 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
2867 uint64_t *propvalue, zfs_userquota_prop_t *typep)
2869 int err;
2870 zfs_cmd_t zc = { 0 };
2872 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2874 err = userquota_propname_decode(propname,
2875 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
2876 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
2877 zc.zc_objset_type = *typep;
2878 if (err)
2879 return (err);
2881 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
2882 if (err)
2883 return (err);
2885 *propvalue = zc.zc_cookie;
2886 return (0);
2890 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
2891 uint64_t *propvalue)
2893 zfs_userquota_prop_t type;
2895 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
2896 &type));
2900 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
2901 char *propbuf, int proplen, boolean_t literal)
2903 int err;
2904 uint64_t propvalue;
2905 zfs_userquota_prop_t type;
2907 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
2908 &type);
2910 if (err)
2911 return (err);
2913 if (literal) {
2914 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2915 } else if (propvalue == 0 &&
2916 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) {
2917 (void) strlcpy(propbuf, "none", proplen);
2918 } else {
2919 zfs_nicenum(propvalue, propbuf, proplen);
2921 return (0);
2925 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
2926 uint64_t *propvalue)
2928 int err;
2929 zfs_cmd_t zc = { 0 };
2930 const char *snapname;
2932 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2934 snapname = strchr(propname, '@') + 1;
2935 if (strchr(snapname, '@')) {
2936 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
2937 } else {
2938 /* snapname is the short name, append it to zhp's fsname */
2939 char *cp;
2941 (void) strlcpy(zc.zc_value, zhp->zfs_name,
2942 sizeof (zc.zc_value));
2943 cp = strchr(zc.zc_value, '@');
2944 if (cp != NULL)
2945 *cp = '\0';
2946 (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
2947 (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
2950 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
2951 if (err)
2952 return (err);
2954 *propvalue = zc.zc_cookie;
2955 return (0);
2959 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
2960 char *propbuf, int proplen, boolean_t literal)
2962 int err;
2963 uint64_t propvalue;
2965 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
2967 if (err)
2968 return (err);
2970 if (literal) {
2971 (void) snprintf(propbuf, proplen, "%llu", propvalue);
2972 } else {
2973 zfs_nicenum(propvalue, propbuf, proplen);
2975 return (0);
2979 * Returns the name of the given zfs handle.
2981 const char *
2982 zfs_get_name(const zfs_handle_t *zhp)
2984 return (zhp->zfs_name);
2988 * Returns the name of the parent pool for the given zfs handle.
2990 const char *
2991 zfs_get_pool_name(const zfs_handle_t *zhp)
2993 return (zhp->zpool_hdl->zpool_name);
2997 * Returns the type of the given zfs handle.
2999 zfs_type_t
3000 zfs_get_type(const zfs_handle_t *zhp)
3002 return (zhp->zfs_type);
3006 * Is one dataset name a child dataset of another?
3008 * Needs to handle these cases:
3009 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3010 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3011 * Descendant? No. No. No. Yes.
3013 static boolean_t
3014 is_descendant(const char *ds1, const char *ds2)
3016 size_t d1len = strlen(ds1);
3018 /* ds2 can't be a descendant if it's smaller */
3019 if (strlen(ds2) < d1len)
3020 return (B_FALSE);
3022 /* otherwise, compare strings and verify that there's a '/' char */
3023 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3027 * Given a complete name, return just the portion that refers to the parent.
3028 * Will return -1 if there is no parent (path is just the name of the
3029 * pool).
3031 static int
3032 parent_name(const char *path, char *buf, size_t buflen)
3034 char *slashp;
3036 (void) strlcpy(buf, path, buflen);
3038 if ((slashp = strrchr(buf, '/')) == NULL)
3039 return (-1);
3040 *slashp = '\0';
3042 return (0);
3046 * If accept_ancestor is false, then check to make sure that the given path has
3047 * a parent, and that it exists. If accept_ancestor is true, then find the
3048 * closest existing ancestor for the given path. In prefixlen return the
3049 * length of already existing prefix of the given path. We also fetch the
3050 * 'zoned' property, which is used to validate property settings when creating
3051 * new datasets.
3053 static int
3054 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3055 boolean_t accept_ancestor, int *prefixlen)
3057 zfs_cmd_t zc = { 0 };
3058 char parent[ZFS_MAX_DATASET_NAME_LEN];
3059 char *slash;
3060 zfs_handle_t *zhp;
3061 char errbuf[1024];
3062 uint64_t is_zoned;
3064 (void) snprintf(errbuf, sizeof (errbuf),
3065 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3067 /* get parent, and check to see if this is just a pool */
3068 if (parent_name(path, parent, sizeof (parent)) != 0) {
3069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3070 "missing dataset name"));
3071 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3074 /* check to see if the pool exists */
3075 if ((slash = strchr(parent, '/')) == NULL)
3076 slash = parent + strlen(parent);
3077 (void) strncpy(zc.zc_name, parent, slash - parent);
3078 zc.zc_name[slash - parent] = '\0';
3079 if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3080 errno == ENOENT) {
3081 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3082 "no such pool '%s'"), zc.zc_name);
3083 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3086 /* check to see if the parent dataset exists */
3087 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3088 if (errno == ENOENT && accept_ancestor) {
3090 * Go deeper to find an ancestor, give up on top level.
3092 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3094 "no such pool '%s'"), zc.zc_name);
3095 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3097 } else if (errno == ENOENT) {
3098 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3099 "parent does not exist"));
3100 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3101 } else
3102 return (zfs_standard_error(hdl, errno, errbuf));
3105 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3106 if (zoned != NULL)
3107 *zoned = is_zoned;
3109 /* we are in a non-global zone, but parent is in the global zone */
3110 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3111 (void) zfs_standard_error(hdl, EPERM, errbuf);
3112 zfs_close(zhp);
3113 return (-1);
3116 /* make sure parent is a filesystem */
3117 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3119 "parent is not a filesystem"));
3120 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3121 zfs_close(zhp);
3122 return (-1);
3125 zfs_close(zhp);
3126 if (prefixlen != NULL)
3127 *prefixlen = strlen(parent);
3128 return (0);
3132 * Finds whether the dataset of the given type(s) exists.
3134 boolean_t
3135 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3137 zfs_handle_t *zhp;
3139 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3140 return (B_FALSE);
3143 * Try to get stats for the dataset, which will tell us if it exists.
3145 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3146 int ds_type = zhp->zfs_type;
3148 zfs_close(zhp);
3149 if (types & ds_type)
3150 return (B_TRUE);
3152 return (B_FALSE);
3156 * Given a path to 'target', create all the ancestors between
3157 * the prefixlen portion of the path, and the target itself.
3158 * Fail if the initial prefixlen-ancestor does not already exist.
3161 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3163 zfs_handle_t *h;
3164 char *cp;
3165 const char *opname;
3167 /* make sure prefix exists */
3168 cp = target + prefixlen;
3169 if (*cp != '/') {
3170 assert(strchr(cp, '/') == NULL);
3171 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3172 } else {
3173 *cp = '\0';
3174 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3175 *cp = '/';
3177 if (h == NULL)
3178 return (-1);
3179 zfs_close(h);
3182 * Attempt to create, mount, and share any ancestor filesystems,
3183 * up to the prefixlen-long one.
3185 for (cp = target + prefixlen + 1;
3186 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3188 *cp = '\0';
3190 h = make_dataset_handle(hdl, target);
3191 if (h) {
3192 /* it already exists, nothing to do here */
3193 zfs_close(h);
3194 continue;
3197 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3198 NULL) != 0) {
3199 opname = dgettext(TEXT_DOMAIN, "create");
3200 goto ancestorerr;
3203 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3204 if (h == NULL) {
3205 opname = dgettext(TEXT_DOMAIN, "open");
3206 goto ancestorerr;
3209 if (zfs_mount(h, NULL, 0) != 0) {
3210 opname = dgettext(TEXT_DOMAIN, "mount");
3211 goto ancestorerr;
3214 if (zfs_share(h) != 0) {
3215 opname = dgettext(TEXT_DOMAIN, "share");
3216 goto ancestorerr;
3219 zfs_close(h);
3222 return (0);
3224 ancestorerr:
3225 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3226 "failed to %s ancestor '%s'"), opname, target);
3227 return (-1);
3231 * Creates non-existing ancestors of the given path.
3234 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3236 int prefix;
3237 char *path_copy;
3238 int rc = 0;
3240 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3241 return (-1);
3243 if ((path_copy = strdup(path)) != NULL) {
3244 rc = create_parents(hdl, path_copy, prefix);
3245 free(path_copy);
3247 if (path_copy == NULL || rc != 0)
3248 return (-1);
3250 return (0);
3254 * Create a new filesystem or volume.
3257 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3258 nvlist_t *props)
3260 int ret;
3261 uint64_t size = 0;
3262 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3263 char errbuf[1024];
3264 uint64_t zoned;
3265 enum lzc_dataset_type ost;
3266 zpool_handle_t *zpool_handle;
3268 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3269 "cannot create '%s'"), path);
3271 /* validate the path, taking care to note the extended error message */
3272 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3273 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3275 /* validate parents exist */
3276 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3277 return (-1);
3280 * The failure modes when creating a dataset of a different type over
3281 * one that already exists is a little strange. In particular, if you
3282 * try to create a dataset on top of an existing dataset, the ioctl()
3283 * will return ENOENT, not EEXIST. To prevent this from happening, we
3284 * first try to see if the dataset exists.
3286 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3287 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3288 "dataset already exists"));
3289 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3292 if (type == ZFS_TYPE_VOLUME)
3293 ost = LZC_DATSET_TYPE_ZVOL;
3294 else
3295 ost = LZC_DATSET_TYPE_ZFS;
3297 /* open zpool handle for prop validation */
3298 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3299 (void) strlcpy(pool_path, path, sizeof (pool_path));
3301 /* truncate pool_path at first slash */
3302 char *p = strchr(pool_path, '/');
3303 if (p != NULL)
3304 *p = '\0';
3306 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3307 return (-1);
3309 if (props && (props = zfs_valid_proplist(hdl, type, props,
3310 zoned, NULL, zpool_handle, errbuf)) == 0) {
3311 zpool_close(zpool_handle);
3312 return (-1);
3314 zpool_close(zpool_handle);
3316 if (type == ZFS_TYPE_VOLUME) {
3318 * If we are creating a volume, the size and block size must
3319 * satisfy a few restraints. First, the blocksize must be a
3320 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3321 * volsize must be a multiple of the block size, and cannot be
3322 * zero.
3324 if (props == NULL || nvlist_lookup_uint64(props,
3325 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3326 nvlist_free(props);
3327 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3328 "missing volume size"));
3329 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3332 if ((ret = nvlist_lookup_uint64(props,
3333 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3334 &blocksize)) != 0) {
3335 if (ret == ENOENT) {
3336 blocksize = zfs_prop_default_numeric(
3337 ZFS_PROP_VOLBLOCKSIZE);
3338 } else {
3339 nvlist_free(props);
3340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3341 "missing volume block size"));
3342 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3346 if (size == 0) {
3347 nvlist_free(props);
3348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3349 "volume size cannot be zero"));
3350 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3353 if (size % blocksize != 0) {
3354 nvlist_free(props);
3355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3356 "volume size must be a multiple of volume block "
3357 "size"));
3358 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3362 /* create the dataset */
3363 ret = lzc_create(path, ost, props);
3364 nvlist_free(props);
3366 /* check for failure */
3367 if (ret != 0) {
3368 char parent[ZFS_MAX_DATASET_NAME_LEN];
3369 (void) parent_name(path, parent, sizeof (parent));
3371 switch (errno) {
3372 case ENOENT:
3373 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3374 "no such parent '%s'"), parent);
3375 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3377 case EINVAL:
3378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3379 "parent '%s' is not a filesystem"), parent);
3380 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3382 case ENOTSUP:
3383 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3384 "pool must be upgraded to set this "
3385 "property or value"));
3386 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3387 #ifdef _ILP32
3388 case EOVERFLOW:
3390 * This platform can't address a volume this big.
3392 if (type == ZFS_TYPE_VOLUME)
3393 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3394 errbuf));
3395 #endif
3396 /* FALLTHROUGH */
3397 default:
3398 return (zfs_standard_error(hdl, errno, errbuf));
3402 return (0);
3406 * Destroys the given dataset. The caller must make sure that the filesystem
3407 * isn't mounted, and that there are no active dependents. If the file system
3408 * does not exist this function does nothing.
3411 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3413 zfs_cmd_t zc = { 0 };
3415 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3416 nvlist_t *nv = fnvlist_alloc();
3417 fnvlist_add_boolean(nv, zhp->zfs_name);
3418 int error = lzc_destroy_bookmarks(nv, NULL);
3419 fnvlist_free(nv);
3420 if (error != 0) {
3421 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3422 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3423 zhp->zfs_name));
3425 return (0);
3428 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3430 if (ZFS_IS_VOLUME(zhp)) {
3431 zc.zc_objset_type = DMU_OST_ZVOL;
3432 } else {
3433 zc.zc_objset_type = DMU_OST_ZFS;
3436 zc.zc_defer_destroy = defer;
3437 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3438 errno != ENOENT) {
3439 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3440 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3441 zhp->zfs_name));
3444 remove_mountpoint(zhp);
3446 return (0);
3449 struct destroydata {
3450 nvlist_t *nvl;
3451 const char *snapname;
3454 static int
3455 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3457 struct destroydata *dd = arg;
3458 char name[ZFS_MAX_DATASET_NAME_LEN];
3459 int rv = 0;
3461 (void) snprintf(name, sizeof (name),
3462 "%s@%s", zhp->zfs_name, dd->snapname);
3464 if (lzc_exists(name))
3465 verify(nvlist_add_boolean(dd->nvl, name) == 0);
3467 rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd);
3468 zfs_close(zhp);
3469 return (rv);
3473 * Destroys all snapshots with the given name in zhp & descendants.
3476 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3478 int ret;
3479 struct destroydata dd = { 0 };
3481 dd.snapname = snapname;
3482 verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0);
3483 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3485 if (nvlist_empty(dd.nvl)) {
3486 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3487 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3488 zhp->zfs_name, snapname);
3489 } else {
3490 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3492 nvlist_free(dd.nvl);
3493 return (ret);
3497 * Destroys all the snapshots named in the nvlist.
3500 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3502 int ret;
3503 nvlist_t *errlist = NULL;
3505 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3507 if (ret == 0) {
3508 nvlist_free(errlist);
3509 return (0);
3512 if (nvlist_empty(errlist)) {
3513 char errbuf[1024];
3514 (void) snprintf(errbuf, sizeof (errbuf),
3515 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3517 ret = zfs_standard_error(hdl, ret, errbuf);
3519 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3520 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3521 char errbuf[1024];
3522 (void) snprintf(errbuf, sizeof (errbuf),
3523 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3524 nvpair_name(pair));
3526 switch (fnvpair_value_int32(pair)) {
3527 case EEXIST:
3528 zfs_error_aux(hdl,
3529 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3530 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3531 break;
3532 default:
3533 ret = zfs_standard_error(hdl, errno, errbuf);
3534 break;
3538 nvlist_free(errlist);
3539 return (ret);
3543 * Clones the given dataset. The target must be of the same type as the source.
3546 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3548 char parent[ZFS_MAX_DATASET_NAME_LEN];
3549 int ret;
3550 char errbuf[1024];
3551 libzfs_handle_t *hdl = zhp->zfs_hdl;
3552 uint64_t zoned;
3554 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3556 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3557 "cannot create '%s'"), target);
3559 /* validate the target/clone name */
3560 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3561 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3563 /* validate parents exist */
3564 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3565 return (-1);
3567 (void) parent_name(target, parent, sizeof (parent));
3569 /* do the clone */
3571 if (props) {
3572 zfs_type_t type;
3573 if (ZFS_IS_VOLUME(zhp)) {
3574 type = ZFS_TYPE_VOLUME;
3575 } else {
3576 type = ZFS_TYPE_FILESYSTEM;
3578 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3579 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3580 return (-1);
3583 ret = lzc_clone(target, zhp->zfs_name, props);
3584 nvlist_free(props);
3586 if (ret != 0) {
3587 switch (errno) {
3589 case ENOENT:
3591 * The parent doesn't exist. We should have caught this
3592 * above, but there may a race condition that has since
3593 * destroyed the parent.
3595 * At this point, we don't know whether it's the source
3596 * that doesn't exist anymore, or whether the target
3597 * dataset doesn't exist.
3599 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3600 "no such parent '%s'"), parent);
3601 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3603 case EXDEV:
3604 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3605 "source and target pools differ"));
3606 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3607 errbuf));
3609 default:
3610 return (zfs_standard_error(zhp->zfs_hdl, errno,
3611 errbuf));
3615 return (ret);
3619 * Promotes the given clone fs to be the clone parent.
3622 zfs_promote(zfs_handle_t *zhp)
3624 libzfs_handle_t *hdl = zhp->zfs_hdl;
3625 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3626 int ret;
3627 char errbuf[1024];
3629 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3630 "cannot promote '%s'"), zhp->zfs_name);
3632 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3633 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3634 "snapshots can not be promoted"));
3635 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3638 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3640 "not a cloned filesystem"));
3641 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3644 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3646 if (ret != 0) {
3647 switch (ret) {
3648 case EEXIST:
3649 /* There is a conflicting snapshot name. */
3650 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3651 "conflicting snapshot '%s' from parent '%s'"),
3652 snapname, zhp->zfs_dmustats.dds_origin);
3653 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3655 default:
3656 return (zfs_standard_error(hdl, ret, errbuf));
3659 return (ret);
3662 typedef struct snapdata {
3663 nvlist_t *sd_nvl;
3664 const char *sd_snapname;
3665 } snapdata_t;
3667 static int
3668 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3670 snapdata_t *sd = arg;
3671 char name[ZFS_MAX_DATASET_NAME_LEN];
3672 int rv = 0;
3674 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3675 (void) snprintf(name, sizeof (name),
3676 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3678 fnvlist_add_boolean(sd->sd_nvl, name);
3680 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3682 zfs_close(zhp);
3684 return (rv);
3688 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3689 * created.
3692 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3694 int ret;
3695 char errbuf[1024];
3696 nvpair_t *elem;
3697 nvlist_t *errors;
3699 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3700 "cannot create snapshots "));
3702 elem = NULL;
3703 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3704 const char *snapname = nvpair_name(elem);
3706 /* validate the target name */
3707 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
3708 B_TRUE)) {
3709 (void) snprintf(errbuf, sizeof (errbuf),
3710 dgettext(TEXT_DOMAIN,
3711 "cannot create snapshot '%s'"), snapname);
3712 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3717 * get pool handle for prop validation. assumes all snaps are in the
3718 * same pool, as does lzc_snapshot (below).
3720 char pool[ZFS_MAX_DATASET_NAME_LEN];
3721 elem = nvlist_next_nvpair(snaps, NULL);
3722 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3723 pool[strcspn(pool, "/@")] = '\0';
3724 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3726 if (props != NULL &&
3727 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3728 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3729 zpool_close(zpool_hdl);
3730 return (-1);
3732 zpool_close(zpool_hdl);
3734 ret = lzc_snapshot(snaps, props, &errors);
3736 if (ret != 0) {
3737 boolean_t printed = B_FALSE;
3738 for (elem = nvlist_next_nvpair(errors, NULL);
3739 elem != NULL;
3740 elem = nvlist_next_nvpair(errors, elem)) {
3741 (void) snprintf(errbuf, sizeof (errbuf),
3742 dgettext(TEXT_DOMAIN,
3743 "cannot create snapshot '%s'"), nvpair_name(elem));
3744 (void) zfs_standard_error(hdl,
3745 fnvpair_value_int32(elem), errbuf);
3746 printed = B_TRUE;
3748 if (!printed) {
3749 switch (ret) {
3750 case EXDEV:
3751 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3752 "multiple snapshots of same "
3753 "fs not allowed"));
3754 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
3756 break;
3757 default:
3758 (void) zfs_standard_error(hdl, ret, errbuf);
3763 nvlist_free(props);
3764 nvlist_free(errors);
3765 return (ret);
3769 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
3770 nvlist_t *props)
3772 int ret;
3773 snapdata_t sd = { 0 };
3774 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3775 char *cp;
3776 zfs_handle_t *zhp;
3777 char errbuf[1024];
3779 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3780 "cannot snapshot %s"), path);
3782 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
3783 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3785 (void) strlcpy(fsname, path, sizeof (fsname));
3786 cp = strchr(fsname, '@');
3787 *cp = '\0';
3788 sd.sd_snapname = cp + 1;
3790 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
3791 ZFS_TYPE_VOLUME)) == NULL) {
3792 return (-1);
3795 verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0);
3796 if (recursive) {
3797 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
3798 } else {
3799 fnvlist_add_boolean(sd.sd_nvl, path);
3802 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
3803 nvlist_free(sd.sd_nvl);
3804 zfs_close(zhp);
3805 return (ret);
3809 * Destroy any more recent snapshots. We invoke this callback on any dependents
3810 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
3811 * is a dependent and we should just destroy it without checking the transaction
3812 * group.
3814 typedef struct rollback_data {
3815 const char *cb_target; /* the snapshot */
3816 uint64_t cb_create; /* creation time reference */
3817 boolean_t cb_error;
3818 boolean_t cb_force;
3819 } rollback_data_t;
3821 static int
3822 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
3824 rollback_data_t *cbp = data;
3825 prop_changelist_t *clp;
3827 /* We must destroy this clone; first unmount it */
3828 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3829 cbp->cb_force ? MS_FORCE: 0);
3830 if (clp == NULL || changelist_prefix(clp) != 0) {
3831 cbp->cb_error = B_TRUE;
3832 zfs_close(zhp);
3833 return (0);
3835 if (zfs_destroy(zhp, B_FALSE) != 0)
3836 cbp->cb_error = B_TRUE;
3837 else
3838 changelist_remove(clp, zhp->zfs_name);
3839 (void) changelist_postfix(clp);
3840 changelist_free(clp);
3842 zfs_close(zhp);
3843 return (0);
3846 static int
3847 rollback_destroy(zfs_handle_t *zhp, void *data)
3849 rollback_data_t *cbp = data;
3851 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
3852 cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE,
3853 rollback_destroy_dependent, cbp);
3855 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
3858 zfs_close(zhp);
3859 return (0);
3863 * Given a dataset, rollback to a specific snapshot, discarding any
3864 * data changes since then and making it the active dataset.
3866 * Any snapshots and bookmarks more recent than the target are
3867 * destroyed, along with their dependents (i.e. clones).
3870 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
3872 rollback_data_t cb = { 0 };
3873 int err;
3874 boolean_t restore_resv = 0;
3875 uint64_t old_volsize = 0, new_volsize;
3876 zfs_prop_t resv_prop;
3878 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
3879 zhp->zfs_type == ZFS_TYPE_VOLUME);
3882 * Destroy all recent snapshots and their dependents.
3884 cb.cb_force = force;
3885 cb.cb_target = snap->zfs_name;
3886 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
3887 (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb);
3888 (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb);
3890 if (cb.cb_error)
3891 return (-1);
3894 * Now that we have verified that the snapshot is the latest,
3895 * rollback to the given snapshot.
3898 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
3899 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
3900 return (-1);
3901 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3902 restore_resv =
3903 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
3907 * We rely on zfs_iter_children() to verify that there are no
3908 * newer snapshots for the given dataset. Therefore, we can
3909 * simply pass the name on to the ioctl() call. There is still
3910 * an unlikely race condition where the user has taken a
3911 * snapshot since we verified that this was the most recent.
3913 err = lzc_rollback(zhp->zfs_name, NULL, 0);
3914 if (err != 0) {
3915 (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3916 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
3917 zhp->zfs_name);
3918 return (err);
3922 * For volumes, if the pre-rollback volsize matched the pre-
3923 * rollback reservation and the volsize has changed then set
3924 * the reservation property to the post-rollback volsize.
3925 * Make a new handle since the rollback closed the dataset.
3927 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
3928 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
3929 if (restore_resv) {
3930 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
3931 if (old_volsize != new_volsize)
3932 err = zfs_prop_set_int(zhp, resv_prop,
3933 new_volsize);
3935 zfs_close(zhp);
3937 return (err);
3941 * Renames the given dataset.
3944 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
3945 boolean_t force_unmount)
3947 int ret = 0;
3948 zfs_cmd_t zc = { 0 };
3949 char *delim;
3950 prop_changelist_t *cl = NULL;
3951 zfs_handle_t *zhrp = NULL;
3952 char *parentname = NULL;
3953 char parent[ZFS_MAX_DATASET_NAME_LEN];
3954 libzfs_handle_t *hdl = zhp->zfs_hdl;
3955 char errbuf[1024];
3957 /* if we have the same exact name, just return success */
3958 if (strcmp(zhp->zfs_name, target) == 0)
3959 return (0);
3961 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3962 "cannot rename to '%s'"), target);
3965 * Make sure the target name is valid
3967 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3968 if ((strchr(target, '@') == NULL) ||
3969 *target == '@') {
3971 * Snapshot target name is abbreviated,
3972 * reconstruct full dataset name
3974 (void) strlcpy(parent, zhp->zfs_name,
3975 sizeof (parent));
3976 delim = strchr(parent, '@');
3977 if (strchr(target, '@') == NULL)
3978 *(++delim) = '\0';
3979 else
3980 *delim = '\0';
3981 (void) strlcat(parent, target, sizeof (parent));
3982 target = parent;
3983 } else {
3985 * Make sure we're renaming within the same dataset.
3987 delim = strchr(target, '@');
3988 if (strncmp(zhp->zfs_name, target, delim - target)
3989 != 0 || zhp->zfs_name[delim - target] != '@') {
3990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3991 "snapshots must be part of same "
3992 "dataset"));
3993 return (zfs_error(hdl, EZFS_CROSSTARGET,
3994 errbuf));
3997 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
3998 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3999 } else {
4000 if (recursive) {
4001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4002 "recursive rename must be a snapshot"));
4003 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4006 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4007 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4009 /* validate parents */
4010 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4011 return (-1);
4013 /* make sure we're in the same pool */
4014 verify((delim = strchr(target, '/')) != NULL);
4015 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4016 zhp->zfs_name[delim - target] != '/') {
4017 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4018 "datasets must be within same pool"));
4019 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4022 /* new name cannot be a child of the current dataset name */
4023 if (is_descendant(zhp->zfs_name, target)) {
4024 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4025 "New dataset name cannot be a descendant of "
4026 "current dataset name"));
4027 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4031 (void) snprintf(errbuf, sizeof (errbuf),
4032 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4034 if (getzoneid() == GLOBAL_ZONEID &&
4035 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4036 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4037 "dataset is used in a non-global zone"));
4038 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4041 if (recursive) {
4042 parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4043 if (parentname == NULL) {
4044 ret = -1;
4045 goto error;
4047 delim = strchr(parentname, '@');
4048 *delim = '\0';
4049 zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET);
4050 if (zhrp == NULL) {
4051 ret = -1;
4052 goto error;
4054 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4055 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4056 force_unmount ? MS_FORCE : 0)) == NULL)
4057 return (-1);
4059 if (changelist_haszonedchild(cl)) {
4060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4061 "child dataset with inherited mountpoint is used "
4062 "in a non-global zone"));
4063 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4064 ret = -1;
4065 goto error;
4068 if ((ret = changelist_prefix(cl)) != 0)
4069 goto error;
4072 if (ZFS_IS_VOLUME(zhp))
4073 zc.zc_objset_type = DMU_OST_ZVOL;
4074 else
4075 zc.zc_objset_type = DMU_OST_ZFS;
4077 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4078 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4080 zc.zc_cookie = recursive;
4082 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4084 * if it was recursive, the one that actually failed will
4085 * be in zc.zc_name
4087 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4088 "cannot rename '%s'"), zc.zc_name);
4090 if (recursive && errno == EEXIST) {
4091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4092 "a child dataset already has a snapshot "
4093 "with the new name"));
4094 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4095 } else {
4096 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4100 * On failure, we still want to remount any filesystems that
4101 * were previously mounted, so we don't alter the system state.
4103 if (cl != NULL)
4104 (void) changelist_postfix(cl);
4105 } else {
4106 if (cl != NULL) {
4107 changelist_rename(cl, zfs_get_name(zhp), target);
4108 ret = changelist_postfix(cl);
4112 error:
4113 if (parentname != NULL) {
4114 free(parentname);
4116 if (zhrp != NULL) {
4117 zfs_close(zhrp);
4119 if (cl != NULL) {
4120 changelist_free(cl);
4122 return (ret);
4125 nvlist_t *
4126 zfs_get_user_props(zfs_handle_t *zhp)
4128 return (zhp->zfs_user_props);
4131 nvlist_t *
4132 zfs_get_recvd_props(zfs_handle_t *zhp)
4134 if (zhp->zfs_recvd_props == NULL)
4135 if (get_recvd_props_ioctl(zhp) != 0)
4136 return (NULL);
4137 return (zhp->zfs_recvd_props);
4141 * This function is used by 'zfs list' to determine the exact set of columns to
4142 * display, and their maximum widths. This does two main things:
4144 * - If this is a list of all properties, then expand the list to include
4145 * all native properties, and set a flag so that for each dataset we look
4146 * for new unique user properties and add them to the list.
4148 * - For non fixed-width properties, keep track of the maximum width seen
4149 * so that we can size the column appropriately. If the user has
4150 * requested received property values, we also need to compute the width
4151 * of the RECEIVED column.
4154 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4155 boolean_t literal)
4157 libzfs_handle_t *hdl = zhp->zfs_hdl;
4158 zprop_list_t *entry;
4159 zprop_list_t **last, **start;
4160 nvlist_t *userprops, *propval;
4161 nvpair_t *elem;
4162 char *strval;
4163 char buf[ZFS_MAXPROPLEN];
4165 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4166 return (-1);
4168 userprops = zfs_get_user_props(zhp);
4170 entry = *plp;
4171 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4173 * Go through and add any user properties as necessary. We
4174 * start by incrementing our list pointer to the first
4175 * non-native property.
4177 start = plp;
4178 while (*start != NULL) {
4179 if ((*start)->pl_prop == ZPROP_INVAL)
4180 break;
4181 start = &(*start)->pl_next;
4184 elem = NULL;
4185 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4187 * See if we've already found this property in our list.
4189 for (last = start; *last != NULL;
4190 last = &(*last)->pl_next) {
4191 if (strcmp((*last)->pl_user_prop,
4192 nvpair_name(elem)) == 0)
4193 break;
4196 if (*last == NULL) {
4197 if ((entry = zfs_alloc(hdl,
4198 sizeof (zprop_list_t))) == NULL ||
4199 ((entry->pl_user_prop = zfs_strdup(hdl,
4200 nvpair_name(elem)))) == NULL) {
4201 free(entry);
4202 return (-1);
4205 entry->pl_prop = ZPROP_INVAL;
4206 entry->pl_width = strlen(nvpair_name(elem));
4207 entry->pl_all = B_TRUE;
4208 *last = entry;
4214 * Now go through and check the width of any non-fixed columns
4216 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4217 if (entry->pl_fixed && !literal)
4218 continue;
4220 if (entry->pl_prop != ZPROP_INVAL) {
4221 if (zfs_prop_get(zhp, entry->pl_prop,
4222 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4223 if (strlen(buf) > entry->pl_width)
4224 entry->pl_width = strlen(buf);
4226 if (received && zfs_prop_get_recvd(zhp,
4227 zfs_prop_to_name(entry->pl_prop),
4228 buf, sizeof (buf), literal) == 0)
4229 if (strlen(buf) > entry->pl_recvd_width)
4230 entry->pl_recvd_width = strlen(buf);
4231 } else {
4232 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4233 &propval) == 0) {
4234 verify(nvlist_lookup_string(propval,
4235 ZPROP_VALUE, &strval) == 0);
4236 if (strlen(strval) > entry->pl_width)
4237 entry->pl_width = strlen(strval);
4239 if (received && zfs_prop_get_recvd(zhp,
4240 entry->pl_user_prop,
4241 buf, sizeof (buf), literal) == 0)
4242 if (strlen(buf) > entry->pl_recvd_width)
4243 entry->pl_recvd_width = strlen(buf);
4247 return (0);
4251 zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path,
4252 char *resource, void *export, void *sharetab,
4253 int sharemax, zfs_share_op_t operation)
4255 zfs_cmd_t zc = { 0 };
4256 int error;
4258 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4259 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4260 if (resource)
4261 (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string));
4262 zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab;
4263 zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export;
4264 zc.zc_share.z_sharetype = operation;
4265 zc.zc_share.z_sharemax = sharemax;
4266 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc);
4267 return (error);
4270 void
4271 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4273 nvpair_t *curr;
4276 * Keep a reference to the props-table against which we prune the
4277 * properties.
4279 zhp->zfs_props_table = props;
4281 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4283 while (curr) {
4284 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4285 nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr);
4288 * User properties will result in ZPROP_INVAL, and since we
4289 * only know how to prune standard ZFS properties, we always
4290 * leave these in the list. This can also happen if we
4291 * encounter an unknown DSL property (when running older
4292 * software, for example).
4294 if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE)
4295 (void) nvlist_remove(zhp->zfs_props,
4296 nvpair_name(curr), nvpair_type(curr));
4297 curr = next;
4301 static int
4302 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4303 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4305 zfs_cmd_t zc = { 0 };
4306 nvlist_t *nvlist = NULL;
4307 int error;
4309 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4310 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4311 zc.zc_cookie = (uint64_t)cmd;
4313 if (cmd == ZFS_SMB_ACL_RENAME) {
4314 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4315 (void) no_memory(hdl);
4316 return (0);
4320 switch (cmd) {
4321 case ZFS_SMB_ACL_ADD:
4322 case ZFS_SMB_ACL_REMOVE:
4323 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4324 break;
4325 case ZFS_SMB_ACL_RENAME:
4326 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4327 resource1) != 0) {
4328 (void) no_memory(hdl);
4329 return (-1);
4331 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4332 resource2) != 0) {
4333 (void) no_memory(hdl);
4334 return (-1);
4336 if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) {
4337 nvlist_free(nvlist);
4338 return (-1);
4340 break;
4341 case ZFS_SMB_ACL_PURGE:
4342 break;
4343 default:
4344 return (-1);
4346 error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4347 nvlist_free(nvlist);
4348 return (error);
4352 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4353 char *path, char *resource)
4355 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4356 resource, NULL));
4360 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4361 char *path, char *resource)
4363 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4364 resource, NULL));
4368 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4370 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4371 NULL, NULL));
4375 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4376 char *oldname, char *newname)
4378 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4379 oldname, newname));
4383 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4384 zfs_userspace_cb_t func, void *arg)
4386 zfs_cmd_t zc = { 0 };
4387 zfs_useracct_t buf[100];
4388 libzfs_handle_t *hdl = zhp->zfs_hdl;
4389 int ret;
4391 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4393 zc.zc_objset_type = type;
4394 zc.zc_nvlist_dst = (uintptr_t)buf;
4396 for (;;) {
4397 zfs_useracct_t *zua = buf;
4399 zc.zc_nvlist_dst_size = sizeof (buf);
4400 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4401 char errbuf[1024];
4403 (void) snprintf(errbuf, sizeof (errbuf),
4404 dgettext(TEXT_DOMAIN,
4405 "cannot get used/quota for %s"), zc.zc_name);
4406 return (zfs_standard_error_fmt(hdl, errno, errbuf));
4408 if (zc.zc_nvlist_dst_size == 0)
4409 break;
4411 while (zc.zc_nvlist_dst_size > 0) {
4412 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4413 zua->zu_space)) != 0)
4414 return (ret);
4415 zua++;
4416 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4420 return (0);
4423 struct holdarg {
4424 nvlist_t *nvl;
4425 const char *snapname;
4426 const char *tag;
4427 boolean_t recursive;
4428 int error;
4431 static int
4432 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4434 struct holdarg *ha = arg;
4435 char name[ZFS_MAX_DATASET_NAME_LEN];
4436 int rv = 0;
4438 (void) snprintf(name, sizeof (name),
4439 "%s@%s", zhp->zfs_name, ha->snapname);
4441 if (lzc_exists(name))
4442 fnvlist_add_string(ha->nvl, name, ha->tag);
4444 if (ha->recursive)
4445 rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
4446 zfs_close(zhp);
4447 return (rv);
4451 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4452 boolean_t recursive, int cleanup_fd)
4454 int ret;
4455 struct holdarg ha;
4457 ha.nvl = fnvlist_alloc();
4458 ha.snapname = snapname;
4459 ha.tag = tag;
4460 ha.recursive = recursive;
4461 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4463 if (nvlist_empty(ha.nvl)) {
4464 char errbuf[1024];
4466 fnvlist_free(ha.nvl);
4467 ret = ENOENT;
4468 (void) snprintf(errbuf, sizeof (errbuf),
4469 dgettext(TEXT_DOMAIN,
4470 "cannot hold snapshot '%s@%s'"),
4471 zhp->zfs_name, snapname);
4472 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4473 return (ret);
4476 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4477 fnvlist_free(ha.nvl);
4479 return (ret);
4483 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4485 int ret;
4486 nvlist_t *errors;
4487 libzfs_handle_t *hdl = zhp->zfs_hdl;
4488 char errbuf[1024];
4489 nvpair_t *elem;
4491 errors = NULL;
4492 ret = lzc_hold(holds, cleanup_fd, &errors);
4494 if (ret == 0) {
4495 /* There may be errors even in the success case. */
4496 fnvlist_free(errors);
4497 return (0);
4500 if (nvlist_empty(errors)) {
4501 /* no hold-specific errors */
4502 (void) snprintf(errbuf, sizeof (errbuf),
4503 dgettext(TEXT_DOMAIN, "cannot hold"));
4504 switch (ret) {
4505 case ENOTSUP:
4506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4507 "pool must be upgraded"));
4508 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4509 break;
4510 case EINVAL:
4511 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4512 break;
4513 default:
4514 (void) zfs_standard_error(hdl, ret, errbuf);
4518 for (elem = nvlist_next_nvpair(errors, NULL);
4519 elem != NULL;
4520 elem = nvlist_next_nvpair(errors, elem)) {
4521 (void) snprintf(errbuf, sizeof (errbuf),
4522 dgettext(TEXT_DOMAIN,
4523 "cannot hold snapshot '%s'"), nvpair_name(elem));
4524 switch (fnvpair_value_int32(elem)) {
4525 case E2BIG:
4527 * Temporary tags wind up having the ds object id
4528 * prepended. So even if we passed the length check
4529 * above, it's still possible for the tag to wind
4530 * up being slightly too long.
4532 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4533 break;
4534 case EINVAL:
4535 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4536 break;
4537 case EEXIST:
4538 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4539 break;
4540 default:
4541 (void) zfs_standard_error(hdl,
4542 fnvpair_value_int32(elem), errbuf);
4546 fnvlist_free(errors);
4547 return (ret);
4550 static int
4551 zfs_release_one(zfs_handle_t *zhp, void *arg)
4553 struct holdarg *ha = arg;
4554 char name[ZFS_MAX_DATASET_NAME_LEN];
4555 int rv = 0;
4556 nvlist_t *existing_holds;
4558 (void) snprintf(name, sizeof (name),
4559 "%s@%s", zhp->zfs_name, ha->snapname);
4561 if (lzc_get_holds(name, &existing_holds) != 0) {
4562 ha->error = ENOENT;
4563 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4564 ha->error = ESRCH;
4565 } else {
4566 nvlist_t *torelease = fnvlist_alloc();
4567 fnvlist_add_boolean(torelease, ha->tag);
4568 fnvlist_add_nvlist(ha->nvl, name, torelease);
4569 fnvlist_free(torelease);
4572 if (ha->recursive)
4573 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4574 zfs_close(zhp);
4575 return (rv);
4579 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4580 boolean_t recursive)
4582 int ret;
4583 struct holdarg ha;
4584 nvlist_t *errors = NULL;
4585 nvpair_t *elem;
4586 libzfs_handle_t *hdl = zhp->zfs_hdl;
4587 char errbuf[1024];
4589 ha.nvl = fnvlist_alloc();
4590 ha.snapname = snapname;
4591 ha.tag = tag;
4592 ha.recursive = recursive;
4593 ha.error = 0;
4594 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4596 if (nvlist_empty(ha.nvl)) {
4597 fnvlist_free(ha.nvl);
4598 ret = ha.error;
4599 (void) snprintf(errbuf, sizeof (errbuf),
4600 dgettext(TEXT_DOMAIN,
4601 "cannot release hold from snapshot '%s@%s'"),
4602 zhp->zfs_name, snapname);
4603 if (ret == ESRCH) {
4604 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4605 } else {
4606 (void) zfs_standard_error(hdl, ret, errbuf);
4608 return (ret);
4611 ret = lzc_release(ha.nvl, &errors);
4612 fnvlist_free(ha.nvl);
4614 if (ret == 0) {
4615 /* There may be errors even in the success case. */
4616 fnvlist_free(errors);
4617 return (0);
4620 if (nvlist_empty(errors)) {
4621 /* no hold-specific errors */
4622 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4623 "cannot release"));
4624 switch (errno) {
4625 case ENOTSUP:
4626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4627 "pool must be upgraded"));
4628 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4629 break;
4630 default:
4631 (void) zfs_standard_error_fmt(hdl, errno, errbuf);
4635 for (elem = nvlist_next_nvpair(errors, NULL);
4636 elem != NULL;
4637 elem = nvlist_next_nvpair(errors, elem)) {
4638 (void) snprintf(errbuf, sizeof (errbuf),
4639 dgettext(TEXT_DOMAIN,
4640 "cannot release hold from snapshot '%s'"),
4641 nvpair_name(elem));
4642 switch (fnvpair_value_int32(elem)) {
4643 case ESRCH:
4644 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4645 break;
4646 case EINVAL:
4647 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4648 break;
4649 default:
4650 (void) zfs_standard_error_fmt(hdl,
4651 fnvpair_value_int32(elem), errbuf);
4655 fnvlist_free(errors);
4656 return (ret);
4660 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
4662 zfs_cmd_t zc = { 0 };
4663 libzfs_handle_t *hdl = zhp->zfs_hdl;
4664 int nvsz = 2048;
4665 void *nvbuf;
4666 int err = 0;
4667 char errbuf[1024];
4669 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4670 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4672 tryagain:
4674 nvbuf = malloc(nvsz);
4675 if (nvbuf == NULL) {
4676 err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno)));
4677 goto out;
4680 zc.zc_nvlist_dst_size = nvsz;
4681 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
4683 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4685 if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) {
4686 (void) snprintf(errbuf, sizeof (errbuf),
4687 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
4688 zc.zc_name);
4689 switch (errno) {
4690 case ENOMEM:
4691 free(nvbuf);
4692 nvsz = zc.zc_nvlist_dst_size;
4693 goto tryagain;
4695 case ENOTSUP:
4696 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4697 "pool must be upgraded"));
4698 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4699 break;
4700 case EINVAL:
4701 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4702 break;
4703 case ENOENT:
4704 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4705 break;
4706 default:
4707 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4708 break;
4710 } else {
4711 /* success */
4712 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
4713 if (rc) {
4714 (void) snprintf(errbuf, sizeof (errbuf), dgettext(
4715 TEXT_DOMAIN, "cannot get permissions on '%s'"),
4716 zc.zc_name);
4717 err = zfs_standard_error_fmt(hdl, rc, errbuf);
4721 free(nvbuf);
4722 out:
4723 return (err);
4727 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
4729 zfs_cmd_t zc = { 0 };
4730 libzfs_handle_t *hdl = zhp->zfs_hdl;
4731 char *nvbuf;
4732 char errbuf[1024];
4733 size_t nvsz;
4734 int err;
4736 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
4737 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
4739 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
4740 assert(err == 0);
4742 nvbuf = malloc(nvsz);
4744 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
4745 assert(err == 0);
4747 zc.zc_nvlist_src_size = nvsz;
4748 zc.zc_nvlist_src = (uintptr_t)nvbuf;
4749 zc.zc_perm_action = un;
4751 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4753 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
4754 (void) snprintf(errbuf, sizeof (errbuf),
4755 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
4756 zc.zc_name);
4757 switch (errno) {
4758 case ENOTSUP:
4759 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4760 "pool must be upgraded"));
4761 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4762 break;
4763 case EINVAL:
4764 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4765 break;
4766 case ENOENT:
4767 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4768 break;
4769 default:
4770 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4771 break;
4775 free(nvbuf);
4777 return (err);
4781 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
4783 int err;
4784 char errbuf[1024];
4786 err = lzc_get_holds(zhp->zfs_name, nvl);
4788 if (err != 0) {
4789 libzfs_handle_t *hdl = zhp->zfs_hdl;
4791 (void) snprintf(errbuf, sizeof (errbuf),
4792 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
4793 zhp->zfs_name);
4794 switch (err) {
4795 case ENOTSUP:
4796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4797 "pool must be upgraded"));
4798 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
4799 break;
4800 case EINVAL:
4801 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
4802 break;
4803 case ENOENT:
4804 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4805 break;
4806 default:
4807 err = zfs_standard_error_fmt(hdl, errno, errbuf);
4808 break;
4812 return (err);
4816 * Convert the zvol's volume size to an appropriate reservation.
4817 * Note: If this routine is updated, it is necessary to update the ZFS test
4818 * suite's shell version in reservation.kshlib.
4820 uint64_t
4821 zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props)
4823 uint64_t numdb;
4824 uint64_t nblocks, volblocksize;
4825 int ncopies;
4826 char *strval;
4828 if (nvlist_lookup_string(props,
4829 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
4830 ncopies = atoi(strval);
4831 else
4832 ncopies = 1;
4833 if (nvlist_lookup_uint64(props,
4834 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
4835 &volblocksize) != 0)
4836 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
4837 nblocks = volsize/volblocksize;
4838 /* start with metadnode L0-L6 */
4839 numdb = 7;
4840 /* calculate number of indirects */
4841 while (nblocks > 1) {
4842 nblocks += DNODES_PER_LEVEL - 1;
4843 nblocks /= DNODES_PER_LEVEL;
4844 numdb += nblocks;
4846 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
4847 volsize *= ncopies;
4849 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
4850 * compressed, but in practice they compress down to about
4851 * 1100 bytes
4853 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
4854 volsize += numdb;
4855 return (volsize);