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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
25 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
26 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
27 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Martin Matuska. All rights reserved.
29 * Copyright (c) 2013 Steven Hartland. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2017 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017 RackTop Systems.
34 */
35
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>
57
58 #include <sys/dnode.h>
59 #include <sys/spa.h>
60 #include <sys/zap.h>
61 #include <libzfs.h>
62
63 #include "zfs_namecheck.h"
64 #include "zfs_prop.h"
65 #include "libzfs_impl.h"
66 #include "zfs_deleg.h"
67
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);
70
71 /*
72 * Given a single type (not a mask of types), return the type in a human
73 * readable form.
74 */
75 const char *
76 zfs_type_to_name(zfs_type_t type)
77 {
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"));
425 * Makes a handle from the given dataset name. Used by zfs_open() and
426 * zfs_iter_* to create child handles on the fly.
427 */
428 static int
429 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
430 {
431 if (put_stats_zhdl(zhp, zc) != 0)
432 return (-1);
433
434 /*
435 * We've managed to open the dataset and gather statistics. Determine
436 * the high-level type.
437 */
438 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
439 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
440 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
441 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
442 else
443 abort();
444
445 if (zhp->zfs_dmustats.dds_is_snapshot)
446 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
447 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
448 zhp->zfs_type = ZFS_TYPE_VOLUME;
449 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
450 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
451 else
452 abort(); /* we should never see any other types */
453
454 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
455 return (-1);
456
457 return (0);
458 }
459
460 zfs_handle_t *
461 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
462 {
463 zfs_cmd_t zc = { 0 };
464
465 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
1245 break;
1246 }
1247 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1248 goto error;
1249 }
1250 }
1251
1252 /*FALLTHRU*/
1253
1254 case ZFS_PROP_SHARESMB:
1255 case ZFS_PROP_SHARENFS:
1256 /*
1257 * For the mountpoint and sharenfs or sharesmb
1258 * properties, check if it can be set in a
1259 * global/non-global zone based on
1260 * the zoned property value:
1261 *
1262 * global zone non-global zone
1263 * --------------------------------------------------
1264 * zoned=on mountpoint (no) mountpoint (yes)
1265 * sharenfs (no) sharenfs (no)
1266 * sharesmb (no) sharesmb (no)
1267 *
1268 * zoned=off mountpoint (yes) N/A
1269 * sharenfs (yes)
1270 * sharesmb (yes)
1271 */
1272 if (zoned) {
1273 if (getzoneid() == GLOBAL_ZONEID) {
1274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1275 "'%s' cannot be set on "
1276 "dataset in a non-global zone"),
1277 propname);
1278 (void) zfs_error(hdl, EZFS_ZONED,
1279 errbuf);
1280 goto error;
1281 } else if (prop == ZFS_PROP_SHARENFS ||
1282 prop == ZFS_PROP_SHARESMB) {
1283 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1284 "'%s' cannot be set in "
1285 "a non-global zone"), propname);
1286 (void) zfs_error(hdl, EZFS_ZONED,
1287 errbuf);
1288 goto error;
1289 }
1290 } else if (getzoneid() != GLOBAL_ZONEID) {
1291 /*
1292 * If zoned property is 'off', this must be in
1293 * a global zone. If not, something is wrong.
1294 */
1295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1296 "'%s' cannot be set while dataset "
1297 "'zoned' property is set"), propname);
1298 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1299 goto error;
1300 }
1301
1302 /*
1303 * At this point, it is legitimate to set the
1304 * property. Now we want to make sure that the
1305 * property value is valid if it is sharenfs.
1306 */
1307 if ((prop == ZFS_PROP_SHARENFS ||
1308 prop == ZFS_PROP_SHARESMB) &&
1540 case EROFS:
1541 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1542 break;
1543
1544 case E2BIG:
1545 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1546 "property value too long"));
1547 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1548 break;
1549
1550 case ENOTSUP:
1551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1552 "pool and or dataset must be upgraded to set this "
1553 "property or value"));
1554 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1555 break;
1556
1557 case ERANGE:
1558 if (prop == ZFS_PROP_COMPRESSION ||
1559 prop == ZFS_PROP_RECORDSIZE) {
1560 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1561 "property setting is not allowed on "
1562 "bootable datasets"));
1563 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1564 } else if (prop == ZFS_PROP_CHECKSUM ||
1565 prop == ZFS_PROP_DEDUP) {
1566 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1567 "property setting is not allowed on "
1568 "root pools"));
1569 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1570 } else {
1571 (void) zfs_standard_error(hdl, err, errbuf);
1572 }
1573 break;
1574
1575 case EINVAL:
1576 if (prop == ZPROP_INVAL) {
1577 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1578 } else {
1579 (void) zfs_standard_error(hdl, err, errbuf);
1580 }
1581 break;
1582
1583 case EOVERFLOW:
1584 /*
1585 * This platform can't address a volume this big.
1586 */
1587 #ifdef _ILP32
1588 if (prop == ZFS_PROP_VOLSIZE) {
1589 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1590 break;
1591 }
1592 #endif
1593 /* FALLTHROUGH */
1594 default:
1595 (void) zfs_standard_error(hdl, err, errbuf);
1596 }
1597 }
1598
1599 /*
1600 * Given a property name and value, set the property for the given dataset.
1601 */
1602 int
1603 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1604 {
1605 int ret = -1;
1606 char errbuf[1024];
1607 libzfs_handle_t *hdl = zhp->zfs_hdl;
1608 nvlist_t *nvl = NULL;
1609
1610 (void) snprintf(errbuf, sizeof (errbuf),
1611 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1612 zhp->zfs_name);
1613
1614 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1615 nvlist_add_string(nvl, propname, propval) != 0) {
1616 (void) no_memory(hdl);
1709 if (cls[cl_idx] != NULL &&
1710 (ret = changelist_prefix(cls[cl_idx])) != 0)
1711 goto error;
1712
1713 cl_idx++;
1714 }
1715 assert(cl_idx == nvl_len);
1716
1717 /*
1718 * Execute the corresponding ioctl() to set this list of properties.
1719 */
1720 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1721
1722 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1723 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1724 goto error;
1725
1726 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1727
1728 if (ret != 0) {
1729 /* Get the list of unset properties back and report them. */
1730 nvlist_t *errorprops = NULL;
1731 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1732 goto error;
1733 for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1734 elem != NULL;
1735 elem = nvlist_next_nvpair(nvl, elem)) {
1736 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1737 zfs_setprop_error(hdl, prop, errno, errbuf);
1738 }
1739 nvlist_free(errorprops);
1740
1741 if (added_resv && errno == ENOSPC) {
1742 /* clean up the volsize property we tried to set */
1743 uint64_t old_volsize = zfs_prop_get_int(zhp,
1744 ZFS_PROP_VOLSIZE);
1745 nvlist_free(nvl);
1746 nvl = NULL;
1747 zcmd_free_nvlists(&zc);
1748
1749 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1750 goto error;
1751 if (nvlist_add_uint64(nvl,
1752 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1753 old_volsize) != 0)
1754 goto error;
1755 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1817
1818 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1819 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1820
1821 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1822 return (zfs_standard_error(hdl, errno, errbuf));
1823
1824 return (0);
1825 }
1826
1827 /*
1828 * Verify that this property is inheritable.
1829 */
1830 if (zfs_prop_readonly(prop))
1831 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1832
1833 if (!zfs_prop_inheritable(prop) && !received)
1834 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1835
1836 /*
1837 * Check to see if the value applies to this type
1838 */
1839 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1840 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1841
1842 /*
1843 * Normalize the name, to get rid of shorthand abbreviations.
1844 */
1845 propname = zfs_prop_to_name(prop);
1846 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1847 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1848
1849 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1850 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1852 "dataset is used in a non-global zone"));
1853 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1854 }
1855
1856 /*
2058 *val = B_TRUE;
2059 if (src)
2060 *src = ZPROP_SRC_TEMPORARY;
2061 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2062 *val = B_FALSE;
2063 if (src)
2064 *src = ZPROP_SRC_TEMPORARY;
2065 }
2066 break;
2067
2068 case ZFS_PROP_CANMOUNT:
2069 case ZFS_PROP_VOLSIZE:
2070 case ZFS_PROP_QUOTA:
2071 case ZFS_PROP_REFQUOTA:
2072 case ZFS_PROP_RESERVATION:
2073 case ZFS_PROP_REFRESERVATION:
2074 case ZFS_PROP_FILESYSTEM_LIMIT:
2075 case ZFS_PROP_SNAPSHOT_LIMIT:
2076 case ZFS_PROP_FILESYSTEM_COUNT:
2077 case ZFS_PROP_SNAPSHOT_COUNT:
2078 *val = getprop_uint64(zhp, prop, source);
2079
2080 if (*source == NULL) {
2081 /* not default, must be local */
2082 *source = zhp->zfs_name;
2083 }
2084 break;
2085
2086 case ZFS_PROP_MOUNTED:
2087 *val = (zhp->zfs_mntopts != NULL);
2088 break;
2089
2090 case ZFS_PROP_NUMCLONES:
2091 *val = zhp->zfs_dmustats.dds_num_clones;
2092 break;
2093
2094 case ZFS_PROP_VERSION:
2095 case ZFS_PROP_NORMALIZE:
2096 case ZFS_PROP_UTF8ONLY:
2097 case ZFS_PROP_CASE:
2536 */
2537 if (val == 0) {
2538 if (literal)
2539 (void) strlcpy(propbuf, "0", proplen);
2540 else
2541 (void) strlcpy(propbuf, "none", proplen);
2542 } else {
2543 if (literal)
2544 (void) snprintf(propbuf, proplen, "%llu",
2545 (u_longlong_t)val);
2546 else
2547 zfs_nicenum(val, propbuf, proplen);
2548 }
2549 zcp_check(zhp, prop, val, NULL);
2550 break;
2551
2552 case ZFS_PROP_FILESYSTEM_LIMIT:
2553 case ZFS_PROP_SNAPSHOT_LIMIT:
2554 case ZFS_PROP_FILESYSTEM_COUNT:
2555 case ZFS_PROP_SNAPSHOT_COUNT:
2556
2557 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2558 return (-1);
2559
2560 /*
2561 * If limit is UINT64_MAX, we translate this into 'none' (unless
2562 * literal is set), and indicate that it's the default value.
2563 * Otherwise, we print the number nicely and indicate that it's
2564 * set locally.
2565 */
2566 if (literal) {
2567 (void) snprintf(propbuf, proplen, "%llu",
2568 (u_longlong_t)val);
2569 } else if (val == UINT64_MAX) {
2570 (void) strlcpy(propbuf, "none", proplen);
2571 } else {
2572 zfs_nicenum(val, propbuf, proplen);
2573 }
2574
2575 zcp_check(zhp, prop, val, NULL);
2579 case ZFS_PROP_COMPRESSRATIO:
2580 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2581 return (-1);
2582 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2583 (u_longlong_t)(val / 100),
2584 (u_longlong_t)(val % 100));
2585 zcp_check(zhp, prop, val, NULL);
2586 break;
2587
2588 case ZFS_PROP_TYPE:
2589 switch (zhp->zfs_type) {
2590 case ZFS_TYPE_FILESYSTEM:
2591 str = "filesystem";
2592 break;
2593 case ZFS_TYPE_VOLUME:
2594 str = "volume";
2595 break;
2596 case ZFS_TYPE_SNAPSHOT:
2597 str = "snapshot";
2598 break;
2599 case ZFS_TYPE_BOOKMARK:
2600 str = "bookmark";
2601 break;
2602 default:
2603 abort();
2604 }
2605 (void) snprintf(propbuf, proplen, "%s", str);
2606 zcp_check(zhp, prop, NULL, propbuf);
2607 break;
2608
2609 case ZFS_PROP_MOUNTED:
2610 /*
2611 * The 'mounted' property is a pseudo-property that described
2612 * whether the filesystem is currently mounted. Even though
2613 * it's a boolean value, the typical values of "on" and "off"
2614 * don't make sense, so we translate to "yes" and "no".
2615 */
2616 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2617 src, &source, &val) != 0)
2618 return (-1);
2665 }
2666 m_label_free(new_sl);
2667
2668 (void) strlcpy(propbuf, ascii, proplen);
2669 free(ascii);
2670 }
2671 break;
2672
2673 case ZFS_PROP_GUID:
2674 /*
2675 * GUIDs are stored as numbers, but they are identifiers.
2676 * We don't want them to be pretty printed, because pretty
2677 * printing mangles the ID into a truncated and useless value.
2678 */
2679 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2680 return (-1);
2681 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2682 zcp_check(zhp, prop, val, NULL);
2683 break;
2684
2685 default:
2686 switch (zfs_prop_get_type(prop)) {
2687 case PROP_TYPE_NUMBER:
2688 if (get_numeric_property(zhp, prop, src,
2689 &source, &val) != 0) {
2690 return (-1);
2691 }
2692
2693 if (literal) {
2694 (void) snprintf(propbuf, proplen, "%llu",
2695 (u_longlong_t)val);
2696 } else {
2697 zfs_nicenum(val, propbuf, proplen);
2698 }
2699 zcp_check(zhp, prop, val, NULL);
2700 break;
2701
2702 case PROP_TYPE_STRING:
2703 str = getprop_string(zhp, prop, &source);
2704 if (str == NULL)
3125 {
3126 char *slashp;
3127
3128 (void) strlcpy(buf, path, buflen);
3129
3130 if ((slashp = strrchr(buf, '/')) == NULL)
3131 return (-1);
3132 *slashp = '\0';
3133
3134 return (0);
3135 }
3136
3137 /*
3138 * If accept_ancestor is false, then check to make sure that the given path has
3139 * a parent, and that it exists. If accept_ancestor is true, then find the
3140 * closest existing ancestor for the given path. In prefixlen return the
3141 * length of already existing prefix of the given path. We also fetch the
3142 * 'zoned' property, which is used to validate property settings when creating
3143 * new datasets.
3144 */
3145 static int
3146 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3147 boolean_t accept_ancestor, int *prefixlen)
3148 {
3149 zfs_cmd_t zc = { 0 };
3150 char parent[ZFS_MAX_DATASET_NAME_LEN];
3151 char *slash;
3152 zfs_handle_t *zhp;
3153 char errbuf[1024];
3154 uint64_t is_zoned;
3155
3156 (void) snprintf(errbuf, sizeof (errbuf),
3157 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3158
3159 /* get parent, and check to see if this is just a pool */
3160 if (parent_name(path, parent, sizeof (parent)) != 0) {
3161 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3162 "missing dataset name"));
3163 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3164 }
3165
3437
3438 if (size == 0) {
3439 nvlist_free(props);
3440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3441 "volume size cannot be zero"));
3442 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3443 }
3444
3445 if (size % blocksize != 0) {
3446 nvlist_free(props);
3447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3448 "volume size must be a multiple of volume block "
3449 "size"));
3450 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3451 }
3452 }
3453
3454 /* create the dataset */
3455 ret = lzc_create(path, ost, props);
3456 nvlist_free(props);
3457
3458 /* check for failure */
3459 if (ret != 0) {
3460 char parent[ZFS_MAX_DATASET_NAME_LEN];
3461 (void) parent_name(path, parent, sizeof (parent));
3462
3463 switch (errno) {
3464 case ENOENT:
3465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3466 "no such parent '%s'"), parent);
3467 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3468
3469 case EINVAL:
3470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3471 "parent '%s' is not a filesystem"), parent);
3472 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3473
3474 case ENOTSUP:
3475 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3476 "pool must be upgraded to set this "
3481 "invalid property value(s) specified"));
3482 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3483 #ifdef _ILP32
3484 case EOVERFLOW:
3485 /*
3486 * This platform can't address a volume this big.
3487 */
3488 if (type == ZFS_TYPE_VOLUME)
3489 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3490 errbuf));
3491 #endif
3492 /* FALLTHROUGH */
3493 default:
3494 return (zfs_standard_error(hdl, errno, errbuf));
3495 }
3496 }
3497
3498 return (0);
3499 }
3500
3501 /*
3502 * Destroys the given dataset. The caller must make sure that the filesystem
3503 * isn't mounted, and that there are no active dependents. If the file system
3504 * does not exist this function does nothing.
3505 */
3506 int
3507 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3508 {
3509 zfs_cmd_t zc = { 0 };
3510
3511 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3512 nvlist_t *nv = fnvlist_alloc();
3513 fnvlist_add_boolean(nv, zhp->zfs_name);
3514 int error = lzc_destroy_bookmarks(nv, NULL);
3515 fnvlist_free(nv);
3516 if (error != 0) {
3517 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3518 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3519 zhp->zfs_name));
3520 }
3579 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3580
3581 if (nvlist_empty(dd.nvl)) {
3582 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3583 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3584 zhp->zfs_name, snapname);
3585 } else {
3586 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3587 }
3588 nvlist_free(dd.nvl);
3589 return (ret);
3590 }
3591
3592 /*
3593 * Destroys all the snapshots named in the nvlist.
3594 */
3595 int
3596 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3597 {
3598 int ret;
3599 nvlist_t *errlist = NULL;
3600
3601 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3602
3603 if (ret == 0) {
3604 nvlist_free(errlist);
3605 return (0);
3606 }
3607
3608 if (nvlist_empty(errlist)) {
3609 char errbuf[1024];
3610 (void) snprintf(errbuf, sizeof (errbuf),
3611 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3612
3613 ret = zfs_standard_error(hdl, ret, errbuf);
3614 }
3615 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3616 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3617 char errbuf[1024];
3618 (void) snprintf(errbuf, sizeof (errbuf),
3619 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3620 nvpair_name(pair));
3621
3661 return (-1);
3662
3663 (void) parent_name(target, parent, sizeof (parent));
3664
3665 /* do the clone */
3666
3667 if (props) {
3668 zfs_type_t type;
3669 if (ZFS_IS_VOLUME(zhp)) {
3670 type = ZFS_TYPE_VOLUME;
3671 } else {
3672 type = ZFS_TYPE_FILESYSTEM;
3673 }
3674 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3675 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3676 return (-1);
3677 }
3678
3679 ret = lzc_clone(target, zhp->zfs_name, props);
3680 nvlist_free(props);
3681
3682 if (ret != 0) {
3683 switch (errno) {
3684
3685 case ENOENT:
3686 /*
3687 * The parent doesn't exist. We should have caught this
3688 * above, but there may a race condition that has since
3689 * destroyed the parent.
3690 *
3691 * At this point, we don't know whether it's the source
3692 * that doesn't exist anymore, or whether the target
3693 * dataset doesn't exist.
3694 */
3695 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3696 "no such parent '%s'"), parent);
3697 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3698
3699 case EXDEV:
3700 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3720 libzfs_handle_t *hdl = zhp->zfs_hdl;
3721 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3722 int ret;
3723 char errbuf[1024];
3724
3725 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3726 "cannot promote '%s'"), zhp->zfs_name);
3727
3728 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3730 "snapshots can not be promoted"));
3731 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3732 }
3733
3734 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3736 "not a cloned filesystem"));
3737 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3738 }
3739
3740 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3741 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3742
3743 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3744
3745 if (ret != 0) {
3746 switch (ret) {
3747 case EEXIST:
3748 /* There is a conflicting snapshot name. */
3749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3750 "conflicting snapshot '%s' from parent '%s'"),
3751 snapname, zhp->zfs_dmustats.dds_origin);
3752 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3753
3754 default:
3755 return (zfs_standard_error(hdl, ret, errbuf));
3756 }
3757 }
3758 return (ret);
3759 }
3760
3761 typedef struct snapdata {
3762 nvlist_t *sd_nvl;
3766 static int
3767 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3768 {
3769 snapdata_t *sd = arg;
3770 char name[ZFS_MAX_DATASET_NAME_LEN];
3771 int rv = 0;
3772
3773 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3774 (void) snprintf(name, sizeof (name),
3775 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3776
3777 fnvlist_add_boolean(sd->sd_nvl, name);
3778
3779 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3780 }
3781 zfs_close(zhp);
3782
3783 return (rv);
3784 }
3785
3786 int
3787 zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs)
3788 {
3789 int err;
3790 char errbuf[1024];
3791
3792 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3793 "cannot remap filesystem '%s' "), fs);
3794
3795 err = lzc_remap(fs);
3796
3797 if (err != 0) {
3798 (void) zfs_standard_error(hdl, err, errbuf);
3799 }
3800
3801 return (err);
3802 }
3803
3804 /*
3805 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3806 * created.
3807 */
3808 int
3809 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3810 {
3811 int ret;
3812 char errbuf[1024];
3813 nvpair_t *elem;
3814 nvlist_t *errors;
3815
3816 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3817 "cannot create snapshots "));
3818
3819 elem = NULL;
3820 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3821 const char *snapname = nvpair_name(elem);
3822
3823 /* validate the target name */
3832
3833 /*
3834 * get pool handle for prop validation. assumes all snaps are in the
3835 * same pool, as does lzc_snapshot (below).
3836 */
3837 char pool[ZFS_MAX_DATASET_NAME_LEN];
3838 elem = nvlist_next_nvpair(snaps, NULL);
3839 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3840 pool[strcspn(pool, "/@")] = '\0';
3841 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3842
3843 if (props != NULL &&
3844 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3845 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3846 zpool_close(zpool_hdl);
3847 return (-1);
3848 }
3849 zpool_close(zpool_hdl);
3850
3851 ret = lzc_snapshot(snaps, props, &errors);
3852
3853 if (ret != 0) {
3854 boolean_t printed = B_FALSE;
3855 for (elem = nvlist_next_nvpair(errors, NULL);
3856 elem != NULL;
3857 elem = nvlist_next_nvpair(errors, elem)) {
3858 (void) snprintf(errbuf, sizeof (errbuf),
3859 dgettext(TEXT_DOMAIN,
3860 "cannot create snapshot '%s'"), nvpair_name(elem));
3861 (void) zfs_standard_error(hdl,
3862 fnvpair_value_int32(elem), errbuf);
3863 printed = B_TRUE;
3864 }
3865 if (!printed) {
3866 switch (ret) {
3867 case EXDEV:
3868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3869 "multiple snapshots of same "
3870 "fs not allowed"));
3871 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4080 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4081 boolean_t force_unmount)
4082 {
4083 int ret = 0;
4084 zfs_cmd_t zc = { 0 };
4085 char *delim;
4086 prop_changelist_t *cl = NULL;
4087 zfs_handle_t *zhrp = NULL;
4088 char *parentname = NULL;
4089 char parent[ZFS_MAX_DATASET_NAME_LEN];
4090 libzfs_handle_t *hdl = zhp->zfs_hdl;
4091 char errbuf[1024];
4092
4093 /* if we have the same exact name, just return success */
4094 if (strcmp(zhp->zfs_name, target) == 0)
4095 return (0);
4096
4097 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4098 "cannot rename to '%s'"), target);
4099
4100 /* make sure source name is valid */
4101 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4102 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4103
4104 /*
4105 * Make sure the target name is valid
4106 */
4107 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4108 if ((strchr(target, '@') == NULL) ||
4109 *target == '@') {
4110 /*
4111 * Snapshot target name is abbreviated,
4112 * reconstruct full dataset name
4113 */
4114 (void) strlcpy(parent, zhp->zfs_name,
4115 sizeof (parent));
4116 delim = strchr(parent, '@');
4117 if (strchr(target, '@') == NULL)
4118 *(++delim) = '\0';
4119 else
4120 *delim = '\0';
4121 (void) strlcat(parent, target, sizeof (parent));
4122 target = parent;
4692 {
4693 struct holdarg *ha = arg;
4694 char name[ZFS_MAX_DATASET_NAME_LEN];
4695 int rv = 0;
4696 nvlist_t *existing_holds;
4697
4698 (void) snprintf(name, sizeof (name),
4699 "%s@%s", zhp->zfs_name, ha->snapname);
4700
4701 if (lzc_get_holds(name, &existing_holds) != 0) {
4702 ha->error = ENOENT;
4703 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4704 ha->error = ESRCH;
4705 } else {
4706 nvlist_t *torelease = fnvlist_alloc();
4707 fnvlist_add_boolean(torelease, ha->tag);
4708 fnvlist_add_nvlist(ha->nvl, name, torelease);
4709 fnvlist_free(torelease);
4710 }
4711
4712 if (ha->recursive)
4713 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4714 zfs_close(zhp);
4715 return (rv);
4716 }
4717
4718 int
4719 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4720 boolean_t recursive)
4721 {
4722 int ret;
4723 struct holdarg ha;
4724 nvlist_t *errors = NULL;
4725 nvpair_t *elem;
4726 libzfs_handle_t *hdl = zhp->zfs_hdl;
4727 char errbuf[1024];
4728
4729 ha.nvl = fnvlist_alloc();
4730 ha.snapname = snapname;
4731 ha.tag = tag;
|
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 /*
27 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28 * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
29 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
30 * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
31 * Copyright (c) 2013 Martin Matuska. All rights reserved.
32 * Copyright (c) 2013 Steven Hartland. All rights reserved.
33 * Copyright (c) 2014 Integros [integros.com]
34 * Copyright 2018 Nexenta Systems, Inc.
35 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
36 * Copyright 2017-2018 RackTop Systems.
37 */
38
39 #include <ctype.h>
40 #include <errno.h>
41 #include <libintl.h>
42 #include <math.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <strings.h>
46 #include <unistd.h>
47 #include <stddef.h>
48 #include <zone.h>
49 #include <fcntl.h>
50 #include <sys/mntent.h>
51 #include <sys/mount.h>
52 #include <priv.h>
53 #include <pwd.h>
54 #include <grp.h>
55 #include <stddef.h>
56 #include <ucred.h>
57 #include <idmap.h>
58 #include <aclutils.h>
59 #include <directory.h>
60
61 #include <sys/dnode.h>
62 #include <sys/spa.h>
63 #include <sys/zap.h>
64 #include <libzfs.h>
65
66 #include "zfs_namecheck.h"
67 #include "zfs_prop.h"
68 #include "libzfs_impl.h"
69 #include "zfs_deleg.h"
70 #include "zfs_errno.h"
71
72 static int userquota_propname_decode(const char *propname, boolean_t zoned,
73 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
74
75 /*
76 * Given a single type (not a mask of types), return the type in a human
77 * readable form.
78 */
79 const char *
80 zfs_type_to_name(zfs_type_t type)
81 {
82 switch (type) {
83 case ZFS_TYPE_FILESYSTEM:
84 return (dgettext(TEXT_DOMAIN, "filesystem"));
85 case ZFS_TYPE_SNAPSHOT:
86 return (dgettext(TEXT_DOMAIN, "snapshot"));
87 case ZFS_TYPE_VOLUME:
88 return (dgettext(TEXT_DOMAIN, "volume"));
89 case ZFS_TYPE_POOL:
90 return (dgettext(TEXT_DOMAIN, "pool"));
429 * Makes a handle from the given dataset name. Used by zfs_open() and
430 * zfs_iter_* to create child handles on the fly.
431 */
432 static int
433 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
434 {
435 if (put_stats_zhdl(zhp, zc) != 0)
436 return (-1);
437
438 /*
439 * We've managed to open the dataset and gather statistics. Determine
440 * the high-level type.
441 */
442 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
443 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
444 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
445 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
446 else
447 abort();
448
449 if (zhp->zfs_dmustats.dds_is_autosnapshot)
450 zhp->zfs_type = ZFS_TYPE_AUTOSNAP;
451 else if (zhp->zfs_dmustats.dds_is_snapshot)
452 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
453 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
454 zhp->zfs_type = ZFS_TYPE_VOLUME;
455 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
456 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
457 else
458 abort(); /* we should never see any other types */
459
460 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
461 return (-1);
462
463 return (0);
464 }
465
466 zfs_handle_t *
467 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
468 {
469 zfs_cmd_t zc = { 0 };
470
471 zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
1251 break;
1252 }
1253 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1254 goto error;
1255 }
1256 }
1257
1258 /*FALLTHRU*/
1259
1260 case ZFS_PROP_SHARESMB:
1261 case ZFS_PROP_SHARENFS:
1262 /*
1263 * For the mountpoint and sharenfs or sharesmb
1264 * properties, check if it can be set in a
1265 * global/non-global zone based on
1266 * the zoned property value:
1267 *
1268 * global zone non-global zone
1269 * --------------------------------------------------
1270 * zoned=on mountpoint (no) mountpoint (yes)
1271 * sharenfs (no) sharenfs (yes)
1272 * sharesmb (no) sharesmb (no)
1273 *
1274 * zoned=off mountpoint (yes) N/A
1275 * sharenfs (yes)
1276 * sharesmb (yes)
1277 */
1278 if (zoned) {
1279 if (getzoneid() == GLOBAL_ZONEID) {
1280 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1281 "'%s' cannot be set on "
1282 "dataset in a non-global zone"),
1283 propname);
1284 (void) zfs_error(hdl, EZFS_ZONED,
1285 errbuf);
1286 goto error;
1287 }
1288 } else if (getzoneid() != GLOBAL_ZONEID) {
1289 /*
1290 * If zoned property is 'off', this must be in
1291 * a global zone. If not, something is wrong.
1292 */
1293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1294 "'%s' cannot be set while dataset "
1295 "'zoned' property is set"), propname);
1296 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1297 goto error;
1298 }
1299
1300 /*
1301 * At this point, it is legitimate to set the
1302 * property. Now we want to make sure that the
1303 * property value is valid if it is sharenfs.
1304 */
1305 if ((prop == ZFS_PROP_SHARENFS ||
1306 prop == ZFS_PROP_SHARESMB) &&
1538 case EROFS:
1539 (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1540 break;
1541
1542 case E2BIG:
1543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1544 "property value too long"));
1545 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1546 break;
1547
1548 case ENOTSUP:
1549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1550 "pool and or dataset must be upgraded to set this "
1551 "property or value"));
1552 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1553 break;
1554
1555 case ERANGE:
1556 if (prop == ZFS_PROP_COMPRESSION ||
1557 prop == ZFS_PROP_RECORDSIZE) {
1558 (void) zfs_error_aux(hdl,
1559 dgettext(TEXT_DOMAIN,
1560 "property setting is not allowed on "
1561 "bootable datasets"));
1562 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1563 } else if (prop == ZFS_PROP_CHECKSUM ||
1564 prop == ZFS_PROP_DEDUP) {
1565 (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1566 "property setting is not allowed on "
1567 "root pools"));
1568 (void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1569 } else {
1570 (void) zfs_standard_error(hdl, err, errbuf);
1571 }
1572 break;
1573
1574 case EINVAL:
1575 if (prop == ZPROP_INVAL) {
1576 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1577 } else {
1578 (void) zfs_standard_error(hdl, err, errbuf);
1579 }
1580 break;
1581
1582 case EKZFS_WBCCONFLICT:
1583 if (prop == ZFS_PROP_WBC_MODE || prop == ZFS_PROP_DEDUP) {
1584 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1585 "WBC and deduplication cannot be "
1586 "active simultaneously on the "
1587 "same dataset"));
1588 (void) zfs_error(hdl, EZFS_WBCNOTSUP, errbuf);
1589 } else {
1590 (void) zfs_standard_error(hdl, err, errbuf);
1591 }
1592
1593 break;
1594
1595 case EOVERFLOW:
1596 /*
1597 * This platform can't address a volume this big.
1598 */
1599 #ifdef _ILP32
1600 if (prop == ZFS_PROP_VOLSIZE) {
1601 (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1602 break;
1603 }
1604 #endif
1605 /* FALLTHROUGH */
1606 default:
1607 if (prop != ZFS_PROP_WBC_MODE) {
1608 (void) zfs_standard_error(hdl, err, errbuf);
1609 break;
1610 }
1611
1612 switch (err) {
1613 case EINPROGRESS:
1614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1615 "WBC cannot be enabled for the dataset since "
1616 "the latter is currently in transition to "
1617 "wbc_mode = off. Try again later"));
1618 (void) zfs_error(hdl, EZFS_WBCINPROGRESS, errbuf);
1619 break;
1620 case EALREADY:
1621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1622 "WBC is already in the requested (on/off) "
1623 "state, nothing to do"));
1624 (void) zfs_error(hdl, EZFS_WBCALREADY, errbuf);
1625 break;
1626 case EKZFS_WBCNOTSUP:
1627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1628 "feature@wbc is not enabled or "
1629 "special device (special vdev) is not present"));
1630 (void) zfs_error(hdl, EZFS_WBCNOTSUP, errbuf);
1631 break;
1632 case EKZFS_WBCCHILD:
1633 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1634 "a child dataset has this property enabled"));
1635 (void) zfs_error(hdl, EZFS_WBCCHILD, errbuf);
1636 break;
1637 case EKZFS_WBCPARENT:
1638 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1639 "a parent dataset has this property enabled"));
1640 (void) zfs_error(hdl, EZFS_WBCPARENT, errbuf);
1641 break;
1642 default:
1643 (void) zfs_standard_error(hdl, err, errbuf);
1644 break;
1645 }
1646 }
1647 }
1648
1649 /*
1650 * Given a property name and value, set the property for the given dataset.
1651 */
1652 int
1653 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1654 {
1655 int ret = -1;
1656 char errbuf[1024];
1657 libzfs_handle_t *hdl = zhp->zfs_hdl;
1658 nvlist_t *nvl = NULL;
1659
1660 (void) snprintf(errbuf, sizeof (errbuf),
1661 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1662 zhp->zfs_name);
1663
1664 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1665 nvlist_add_string(nvl, propname, propval) != 0) {
1666 (void) no_memory(hdl);
1759 if (cls[cl_idx] != NULL &&
1760 (ret = changelist_prefix(cls[cl_idx])) != 0)
1761 goto error;
1762
1763 cl_idx++;
1764 }
1765 assert(cl_idx == nvl_len);
1766
1767 /*
1768 * Execute the corresponding ioctl() to set this list of properties.
1769 */
1770 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1771
1772 if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1773 (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1774 goto error;
1775
1776 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1777
1778 if (ret != 0) {
1779 if (zc.zc_nvlist_dst_filled == B_FALSE) {
1780 (void) zfs_standard_error(hdl, errno, errbuf);
1781 goto error;
1782 }
1783
1784 /* Get the list of unset properties back and report them. */
1785 nvlist_t *errorprops = NULL;
1786 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1787 goto error;
1788 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1789 elem != NULL;
1790 elem = nvlist_next_nvpair(errorprops, elem)) {
1791 zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1792 zfs_setprop_error(hdl, prop, errno, errbuf);
1793 }
1794 nvlist_free(errorprops);
1795
1796 if (added_resv && errno == ENOSPC) {
1797 /* clean up the volsize property we tried to set */
1798 uint64_t old_volsize = zfs_prop_get_int(zhp,
1799 ZFS_PROP_VOLSIZE);
1800 nvlist_free(nvl);
1801 nvl = NULL;
1802 zcmd_free_nvlists(&zc);
1803
1804 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1805 goto error;
1806 if (nvlist_add_uint64(nvl,
1807 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1808 old_volsize) != 0)
1809 goto error;
1810 if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1872
1873 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1874 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1875
1876 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1877 return (zfs_standard_error(hdl, errno, errbuf));
1878
1879 return (0);
1880 }
1881
1882 /*
1883 * Verify that this property is inheritable.
1884 */
1885 if (zfs_prop_readonly(prop))
1886 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1887
1888 if (!zfs_prop_inheritable(prop) && !received)
1889 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1890
1891 /*
1892 * This property is inheritable by nature,
1893 * but user can do only "set" operation,
1894 * because "inherit" does not make sence
1895 */
1896 if (prop == ZFS_PROP_WBC_MODE)
1897 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1898
1899 /*
1900 * Check to see if the value applies to this type
1901 */
1902 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
1903 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1904
1905 /*
1906 * Normalize the name, to get rid of shorthand abbreviations.
1907 */
1908 propname = zfs_prop_to_name(prop);
1909 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1910 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1911
1912 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1913 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1915 "dataset is used in a non-global zone"));
1916 return (zfs_error(hdl, EZFS_ZONED, errbuf));
1917 }
1918
1919 /*
2121 *val = B_TRUE;
2122 if (src)
2123 *src = ZPROP_SRC_TEMPORARY;
2124 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2125 *val = B_FALSE;
2126 if (src)
2127 *src = ZPROP_SRC_TEMPORARY;
2128 }
2129 break;
2130
2131 case ZFS_PROP_CANMOUNT:
2132 case ZFS_PROP_VOLSIZE:
2133 case ZFS_PROP_QUOTA:
2134 case ZFS_PROP_REFQUOTA:
2135 case ZFS_PROP_RESERVATION:
2136 case ZFS_PROP_REFRESERVATION:
2137 case ZFS_PROP_FILESYSTEM_LIMIT:
2138 case ZFS_PROP_SNAPSHOT_LIMIT:
2139 case ZFS_PROP_FILESYSTEM_COUNT:
2140 case ZFS_PROP_SNAPSHOT_COUNT:
2141 case ZFS_PROP_RATE_LIMIT:
2142 *val = getprop_uint64(zhp, prop, source);
2143
2144 if (*source == NULL) {
2145 /* not default, must be local */
2146 *source = zhp->zfs_name;
2147 }
2148 break;
2149
2150 case ZFS_PROP_MOUNTED:
2151 *val = (zhp->zfs_mntopts != NULL);
2152 break;
2153
2154 case ZFS_PROP_NUMCLONES:
2155 *val = zhp->zfs_dmustats.dds_num_clones;
2156 break;
2157
2158 case ZFS_PROP_VERSION:
2159 case ZFS_PROP_NORMALIZE:
2160 case ZFS_PROP_UTF8ONLY:
2161 case ZFS_PROP_CASE:
2600 */
2601 if (val == 0) {
2602 if (literal)
2603 (void) strlcpy(propbuf, "0", proplen);
2604 else
2605 (void) strlcpy(propbuf, "none", proplen);
2606 } else {
2607 if (literal)
2608 (void) snprintf(propbuf, proplen, "%llu",
2609 (u_longlong_t)val);
2610 else
2611 zfs_nicenum(val, propbuf, proplen);
2612 }
2613 zcp_check(zhp, prop, val, NULL);
2614 break;
2615
2616 case ZFS_PROP_FILESYSTEM_LIMIT:
2617 case ZFS_PROP_SNAPSHOT_LIMIT:
2618 case ZFS_PROP_FILESYSTEM_COUNT:
2619 case ZFS_PROP_SNAPSHOT_COUNT:
2620 case ZFS_PROP_RATE_LIMIT:
2621
2622 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2623 return (-1);
2624
2625 /*
2626 * If limit is UINT64_MAX, we translate this into 'none' (unless
2627 * literal is set), and indicate that it's the default value.
2628 * Otherwise, we print the number nicely and indicate that it's
2629 * set locally.
2630 */
2631 if (literal) {
2632 (void) snprintf(propbuf, proplen, "%llu",
2633 (u_longlong_t)val);
2634 } else if (val == UINT64_MAX) {
2635 (void) strlcpy(propbuf, "none", proplen);
2636 } else {
2637 zfs_nicenum(val, propbuf, proplen);
2638 }
2639
2640 zcp_check(zhp, prop, val, NULL);
2644 case ZFS_PROP_COMPRESSRATIO:
2645 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2646 return (-1);
2647 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2648 (u_longlong_t)(val / 100),
2649 (u_longlong_t)(val % 100));
2650 zcp_check(zhp, prop, val, NULL);
2651 break;
2652
2653 case ZFS_PROP_TYPE:
2654 switch (zhp->zfs_type) {
2655 case ZFS_TYPE_FILESYSTEM:
2656 str = "filesystem";
2657 break;
2658 case ZFS_TYPE_VOLUME:
2659 str = "volume";
2660 break;
2661 case ZFS_TYPE_SNAPSHOT:
2662 str = "snapshot";
2663 break;
2664 case ZFS_TYPE_AUTOSNAP:
2665 str = "autosnap";
2666 break;
2667 case ZFS_TYPE_BOOKMARK:
2668 str = "bookmark";
2669 break;
2670 default:
2671 abort();
2672 }
2673 (void) snprintf(propbuf, proplen, "%s", str);
2674 zcp_check(zhp, prop, NULL, propbuf);
2675 break;
2676
2677 case ZFS_PROP_MOUNTED:
2678 /*
2679 * The 'mounted' property is a pseudo-property that described
2680 * whether the filesystem is currently mounted. Even though
2681 * it's a boolean value, the typical values of "on" and "off"
2682 * don't make sense, so we translate to "yes" and "no".
2683 */
2684 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2685 src, &source, &val) != 0)
2686 return (-1);
2733 }
2734 m_label_free(new_sl);
2735
2736 (void) strlcpy(propbuf, ascii, proplen);
2737 free(ascii);
2738 }
2739 break;
2740
2741 case ZFS_PROP_GUID:
2742 /*
2743 * GUIDs are stored as numbers, but they are identifiers.
2744 * We don't want them to be pretty printed, because pretty
2745 * printing mangles the ID into a truncated and useless value.
2746 */
2747 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2748 return (-1);
2749 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2750 zcp_check(zhp, prop, val, NULL);
2751 break;
2752
2753 case ZFS_PROP_MODIFIED:
2754 /*
2755 * For a snapshot the "modified" pseudo-property is set to
2756 * "yes" if parent filesystem/zvol differs from the snapshot.
2757 * Even though it's a boolean value, the typical
2758 * values of "on" and "off" don't make sense, so we translate
2759 * to "yes" and "no".
2760 */
2761 if (get_numeric_property(zhp, ZFS_PROP_MODIFIED,
2762 src, &source, &val) != 0)
2763 return (-1);
2764 if (val != 0)
2765 (void) strlcpy(propbuf, "yes", proplen);
2766 else
2767 (void) strlcpy(propbuf, "no", proplen);
2768 break;
2769
2770 default:
2771 switch (zfs_prop_get_type(prop)) {
2772 case PROP_TYPE_NUMBER:
2773 if (get_numeric_property(zhp, prop, src,
2774 &source, &val) != 0) {
2775 return (-1);
2776 }
2777
2778 if (literal) {
2779 (void) snprintf(propbuf, proplen, "%llu",
2780 (u_longlong_t)val);
2781 } else {
2782 zfs_nicenum(val, propbuf, proplen);
2783 }
2784 zcp_check(zhp, prop, val, NULL);
2785 break;
2786
2787 case PROP_TYPE_STRING:
2788 str = getprop_string(zhp, prop, &source);
2789 if (str == NULL)
3210 {
3211 char *slashp;
3212
3213 (void) strlcpy(buf, path, buflen);
3214
3215 if ((slashp = strrchr(buf, '/')) == NULL)
3216 return (-1);
3217 *slashp = '\0';
3218
3219 return (0);
3220 }
3221
3222 /*
3223 * If accept_ancestor is false, then check to make sure that the given path has
3224 * a parent, and that it exists. If accept_ancestor is true, then find the
3225 * closest existing ancestor for the given path. In prefixlen return the
3226 * length of already existing prefix of the given path. We also fetch the
3227 * 'zoned' property, which is used to validate property settings when creating
3228 * new datasets.
3229 */
3230 int
3231 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3232 boolean_t accept_ancestor, int *prefixlen)
3233 {
3234 zfs_cmd_t zc = { 0 };
3235 char parent[ZFS_MAX_DATASET_NAME_LEN];
3236 char *slash;
3237 zfs_handle_t *zhp;
3238 char errbuf[1024];
3239 uint64_t is_zoned;
3240
3241 (void) snprintf(errbuf, sizeof (errbuf),
3242 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3243
3244 /* get parent, and check to see if this is just a pool */
3245 if (parent_name(path, parent, sizeof (parent)) != 0) {
3246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3247 "missing dataset name"));
3248 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3249 }
3250
3522
3523 if (size == 0) {
3524 nvlist_free(props);
3525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3526 "volume size cannot be zero"));
3527 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3528 }
3529
3530 if (size % blocksize != 0) {
3531 nvlist_free(props);
3532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3533 "volume size must be a multiple of volume block "
3534 "size"));
3535 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3536 }
3537 }
3538
3539 /* create the dataset */
3540 ret = lzc_create(path, ost, props);
3541 nvlist_free(props);
3542 if (ret == 0)
3543 libzfs_log_event(hdl, path);
3544
3545 /* check for failure */
3546 if (ret != 0) {
3547 char parent[ZFS_MAX_DATASET_NAME_LEN];
3548 (void) parent_name(path, parent, sizeof (parent));
3549
3550 switch (errno) {
3551 case ENOENT:
3552 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3553 "no such parent '%s'"), parent);
3554 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3555
3556 case EINVAL:
3557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3558 "parent '%s' is not a filesystem"), parent);
3559 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3560
3561 case ENOTSUP:
3562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3563 "pool must be upgraded to set this "
3568 "invalid property value(s) specified"));
3569 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3570 #ifdef _ILP32
3571 case EOVERFLOW:
3572 /*
3573 * This platform can't address a volume this big.
3574 */
3575 if (type == ZFS_TYPE_VOLUME)
3576 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3577 errbuf));
3578 #endif
3579 /* FALLTHROUGH */
3580 default:
3581 return (zfs_standard_error(hdl, errno, errbuf));
3582 }
3583 }
3584
3585 return (0);
3586 }
3587
3588 int
3589 zfs_check_krrp(libzfs_handle_t *hdl, const char *name)
3590 {
3591 zfs_cmd_t zc = { 0 };
3592 int err;
3593
3594 (void) strncpy(zc.zc_name, name, ZFS_MAX_DATASET_NAME_LEN);
3595 zc.zc_name[ZFS_MAX_DATASET_NAME_LEN - 1] = '\0';
3596
3597 (void) zfs_ioctl(hdl, ZFS_IOC_CHECK_KRRP, &zc);
3598 err = errno;
3599
3600 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3601
3602 return (err);
3603 }
3604
3605 int
3606 zfs_destroy_atomically(zfs_handle_t *zhp, boolean_t defer)
3607 {
3608 zfs_cmd_t zc = { 0 };
3609
3610 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3611 nvlist_t *nv = fnvlist_alloc();
3612 fnvlist_add_boolean(nv, zhp->zfs_name);
3613 int error = lzc_destroy_bookmarks(nv, NULL);
3614 fnvlist_free(nv);
3615 if (error != 0) {
3616 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3617 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3618 zhp->zfs_name));
3619 }
3620 return (0);
3621 }
3622
3623 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3624
3625 if (ZFS_IS_VOLUME(zhp)) {
3626 zc.zc_objset_type = DMU_OST_ZVOL;
3627 } else {
3628 zc.zc_objset_type = DMU_OST_ZFS;
3629 }
3630
3631 zc.zc_defer_destroy = defer;
3632 zc.zc_guid = B_TRUE;
3633 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 &&
3634 errno != ENOENT) {
3635 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3636 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3637 zhp->zfs_name));
3638 }
3639
3640 remove_mountpoint(zhp);
3641
3642 return (0);
3643
3644 }
3645
3646 /*
3647 * Destroys the given dataset. The caller must make sure that the filesystem
3648 * isn't mounted, and that there are no active dependents. If the file system
3649 * does not exist this function does nothing.
3650 */
3651 int
3652 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3653 {
3654 zfs_cmd_t zc = { 0 };
3655
3656 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3657 nvlist_t *nv = fnvlist_alloc();
3658 fnvlist_add_boolean(nv, zhp->zfs_name);
3659 int error = lzc_destroy_bookmarks(nv, NULL);
3660 fnvlist_free(nv);
3661 if (error != 0) {
3662 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3663 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3664 zhp->zfs_name));
3665 }
3724 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3725
3726 if (nvlist_empty(dd.nvl)) {
3727 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3728 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3729 zhp->zfs_name, snapname);
3730 } else {
3731 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3732 }
3733 nvlist_free(dd.nvl);
3734 return (ret);
3735 }
3736
3737 /*
3738 * Destroys all the snapshots named in the nvlist.
3739 */
3740 int
3741 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3742 {
3743 int ret;
3744 nvpair_t *elem;
3745 nvlist_t *errlist = NULL;
3746
3747 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3748 if (ret == 0) {
3749 for (elem = nvlist_next_nvpair(snaps, NULL); elem != NULL;
3750 elem = nvlist_next_nvpair(snaps, elem))
3751 libzfs_log_event(hdl, nvpair_name(elem));
3752 }
3753
3754 if (ret == 0) {
3755 nvlist_free(errlist);
3756 return (0);
3757 }
3758
3759 if (nvlist_empty(errlist)) {
3760 char errbuf[1024];
3761 (void) snprintf(errbuf, sizeof (errbuf),
3762 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3763
3764 ret = zfs_standard_error(hdl, ret, errbuf);
3765 }
3766 for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL);
3767 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3768 char errbuf[1024];
3769 (void) snprintf(errbuf, sizeof (errbuf),
3770 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3771 nvpair_name(pair));
3772
3812 return (-1);
3813
3814 (void) parent_name(target, parent, sizeof (parent));
3815
3816 /* do the clone */
3817
3818 if (props) {
3819 zfs_type_t type;
3820 if (ZFS_IS_VOLUME(zhp)) {
3821 type = ZFS_TYPE_VOLUME;
3822 } else {
3823 type = ZFS_TYPE_FILESYSTEM;
3824 }
3825 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3826 zhp, zhp->zpool_hdl, errbuf)) == NULL)
3827 return (-1);
3828 }
3829
3830 ret = lzc_clone(target, zhp->zfs_name, props);
3831 nvlist_free(props);
3832 if (ret == 0)
3833 libzfs_log_event(hdl, target);
3834
3835 if (ret != 0) {
3836 switch (errno) {
3837
3838 case ENOENT:
3839 /*
3840 * The parent doesn't exist. We should have caught this
3841 * above, but there may a race condition that has since
3842 * destroyed the parent.
3843 *
3844 * At this point, we don't know whether it's the source
3845 * that doesn't exist anymore, or whether the target
3846 * dataset doesn't exist.
3847 */
3848 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3849 "no such parent '%s'"), parent);
3850 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3851
3852 case EXDEV:
3853 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3873 libzfs_handle_t *hdl = zhp->zfs_hdl;
3874 char snapname[ZFS_MAX_DATASET_NAME_LEN];
3875 int ret;
3876 char errbuf[1024];
3877
3878 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3879 "cannot promote '%s'"), zhp->zfs_name);
3880
3881 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3883 "snapshots can not be promoted"));
3884 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3885 }
3886
3887 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3888 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3889 "not a cloned filesystem"));
3890 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3891 }
3892
3893 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3894
3895 if (ret != 0) {
3896 switch (ret) {
3897 case EEXIST:
3898 /* There is a conflicting snapshot name. */
3899 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3900 "conflicting snapshot '%s' from parent '%s'"),
3901 snapname, zhp->zfs_dmustats.dds_origin);
3902 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3903
3904 default:
3905 return (zfs_standard_error(hdl, ret, errbuf));
3906 }
3907 }
3908 return (ret);
3909 }
3910
3911 typedef struct snapdata {
3912 nvlist_t *sd_nvl;
3916 static int
3917 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
3918 {
3919 snapdata_t *sd = arg;
3920 char name[ZFS_MAX_DATASET_NAME_LEN];
3921 int rv = 0;
3922
3923 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
3924 (void) snprintf(name, sizeof (name),
3925 "%s@%s", zfs_get_name(zhp), sd->sd_snapname);
3926
3927 fnvlist_add_boolean(sd->sd_nvl, name);
3928
3929 rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd);
3930 }
3931 zfs_close(zhp);
3932
3933 return (rv);
3934 }
3935
3936 /*
3937 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
3938 * created.
3939 */
3940 int
3941 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
3942 {
3943 int ret;
3944 char errbuf[1024];
3945 nvpair_t *elem;
3946 nvlist_t *errors;
3947
3948 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3949 "cannot create snapshots "));
3950
3951 elem = NULL;
3952 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
3953 const char *snapname = nvpair_name(elem);
3954
3955 /* validate the target name */
3964
3965 /*
3966 * get pool handle for prop validation. assumes all snaps are in the
3967 * same pool, as does lzc_snapshot (below).
3968 */
3969 char pool[ZFS_MAX_DATASET_NAME_LEN];
3970 elem = nvlist_next_nvpair(snaps, NULL);
3971 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
3972 pool[strcspn(pool, "/@")] = '\0';
3973 zpool_handle_t *zpool_hdl = zpool_open(hdl, pool);
3974
3975 if (props != NULL &&
3976 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
3977 props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) {
3978 zpool_close(zpool_hdl);
3979 return (-1);
3980 }
3981 zpool_close(zpool_hdl);
3982
3983 ret = lzc_snapshot(snaps, props, &errors);
3984 if (ret == 0) {
3985 for (elem = nvlist_next_nvpair(snaps, NULL); elem != NULL;
3986 elem = nvlist_next_nvpair(snaps, elem))
3987 libzfs_log_event(hdl, nvpair_name(elem));
3988 }
3989
3990 if (ret != 0) {
3991 boolean_t printed = B_FALSE;
3992 for (elem = nvlist_next_nvpair(errors, NULL);
3993 elem != NULL;
3994 elem = nvlist_next_nvpair(errors, elem)) {
3995 (void) snprintf(errbuf, sizeof (errbuf),
3996 dgettext(TEXT_DOMAIN,
3997 "cannot create snapshot '%s'"), nvpair_name(elem));
3998 (void) zfs_standard_error(hdl,
3999 fnvpair_value_int32(elem), errbuf);
4000 printed = B_TRUE;
4001 }
4002 if (!printed) {
4003 switch (ret) {
4004 case EXDEV:
4005 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4006 "multiple snapshots of same "
4007 "fs not allowed"));
4008 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4217 zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive,
4218 boolean_t force_unmount)
4219 {
4220 int ret = 0;
4221 zfs_cmd_t zc = { 0 };
4222 char *delim;
4223 prop_changelist_t *cl = NULL;
4224 zfs_handle_t *zhrp = NULL;
4225 char *parentname = NULL;
4226 char parent[ZFS_MAX_DATASET_NAME_LEN];
4227 libzfs_handle_t *hdl = zhp->zfs_hdl;
4228 char errbuf[1024];
4229
4230 /* if we have the same exact name, just return success */
4231 if (strcmp(zhp->zfs_name, target) == 0)
4232 return (0);
4233
4234 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4235 "cannot rename to '%s'"), target);
4236
4237 if (zhp->zfs_type == ZFS_TYPE_AUTOSNAP) {
4238 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4239 "autosnapshots cannot be renamed"));
4240 return (zfs_error(hdl, EZFS_PERM, errbuf));
4241 }
4242
4243 /*
4244 * Make sure the target name is valid
4245 */
4246 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4247 if ((strchr(target, '@') == NULL) ||
4248 *target == '@') {
4249 /*
4250 * Snapshot target name is abbreviated,
4251 * reconstruct full dataset name
4252 */
4253 (void) strlcpy(parent, zhp->zfs_name,
4254 sizeof (parent));
4255 delim = strchr(parent, '@');
4256 if (strchr(target, '@') == NULL)
4257 *(++delim) = '\0';
4258 else
4259 *delim = '\0';
4260 (void) strlcat(parent, target, sizeof (parent));
4261 target = parent;
4831 {
4832 struct holdarg *ha = arg;
4833 char name[ZFS_MAX_DATASET_NAME_LEN];
4834 int rv = 0;
4835 nvlist_t *existing_holds;
4836
4837 (void) snprintf(name, sizeof (name),
4838 "%s@%s", zhp->zfs_name, ha->snapname);
4839
4840 if (lzc_get_holds(name, &existing_holds) != 0) {
4841 ha->error = ENOENT;
4842 } else if (!nvlist_exists(existing_holds, ha->tag)) {
4843 ha->error = ESRCH;
4844 } else {
4845 nvlist_t *torelease = fnvlist_alloc();
4846 fnvlist_add_boolean(torelease, ha->tag);
4847 fnvlist_add_nvlist(ha->nvl, name, torelease);
4848 fnvlist_free(torelease);
4849 }
4850
4851 fnvlist_free(existing_holds);
4852
4853 if (ha->recursive)
4854 rv = zfs_iter_filesystems(zhp, zfs_release_one, ha);
4855 zfs_close(zhp);
4856 return (rv);
4857 }
4858
4859 int
4860 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4861 boolean_t recursive)
4862 {
4863 int ret;
4864 struct holdarg ha;
4865 nvlist_t *errors = NULL;
4866 nvpair_t *elem;
4867 libzfs_handle_t *hdl = zhp->zfs_hdl;
4868 char errbuf[1024];
4869
4870 ha.nvl = fnvlist_alloc();
4871 ha.snapname = snapname;
4872 ha.tag = tag;
|