1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21f3861e1aSahl 22fa9e4066Sahrens /* 2336db6475SEric Taylor * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24a2afb611SJerry Jelinek * Copyright (c) 2013, Joyent, Inc. All rights reserved. 25e9316f76SJoe Stein * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 26f0f3ef5aSGarrett D'Amore * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. 270d8fa8f8SMartin Matuska * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved. 28013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved. 29a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved. 30c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 318808ac5dSYuri Pankov * Copyright 2016 Nexenta Systems, Inc. 3288f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> 33fa9e4066Sahrens */ 34fa9e4066Sahrens 35fa9e4066Sahrens #include <ctype.h> 36fa9e4066Sahrens #include <errno.h> 37fa9e4066Sahrens #include <libintl.h> 38fa9e4066Sahrens #include <math.h> 39fa9e4066Sahrens #include <stdio.h> 40fa9e4066Sahrens #include <stdlib.h> 41fa9e4066Sahrens #include <strings.h> 42fa9e4066Sahrens #include <unistd.h> 433cb34c60Sahrens #include <stddef.h> 44fa9e4066Sahrens #include <zone.h> 4599653d4eSeschrock #include <fcntl.h> 46fa9e4066Sahrens #include <sys/mntent.h> 47b12a1c38Slling #include <sys/mount.h> 48ecd6cf80Smarks #include <priv.h> 49ecd6cf80Smarks #include <pwd.h> 50ecd6cf80Smarks #include <grp.h> 51ecd6cf80Smarks #include <stddef.h> 52ecd6cf80Smarks #include <ucred.h> 5314843421SMatthew Ahrens #include <idmap.h> 5414843421SMatthew Ahrens #include <aclutils.h> 553b12c289SMatthew Ahrens #include <directory.h> 56fa9e4066Sahrens 57c1449561SEric Taylor #include <sys/dnode.h> 58fa9e4066Sahrens #include <sys/spa.h> 59e9dbad6fSeschrock #include <sys/zap.h> 60fa9e4066Sahrens #include <libzfs.h> 61fa9e4066Sahrens 62fa9e4066Sahrens #include "zfs_namecheck.h" 63fa9e4066Sahrens #include "zfs_prop.h" 64fa9e4066Sahrens #include "libzfs_impl.h" 65ecd6cf80Smarks #include "zfs_deleg.h" 66fa9e4066Sahrens 6714843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned, 6814843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 69cdf5b4caSmmusante 70fa9e4066Sahrens /* 71fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human 72fa9e4066Sahrens * readable form. 73fa9e4066Sahrens */ 74fa9e4066Sahrens const char * 75fa9e4066Sahrens zfs_type_to_name(zfs_type_t type) 76fa9e4066Sahrens { 77fa9e4066Sahrens switch (type) { 78fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 79fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 80fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 81fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 82fa9e4066Sahrens case ZFS_TYPE_VOLUME: 83fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 8488f61deeSIgor Kozhukhov case ZFS_TYPE_POOL: 8588f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "pool")); 8688f61deeSIgor Kozhukhov case ZFS_TYPE_BOOKMARK: 8788f61deeSIgor Kozhukhov return (dgettext(TEXT_DOMAIN, "bookmark")); 8888f61deeSIgor Kozhukhov default: 8988f61deeSIgor Kozhukhov assert(!"unhandled zfs_type_t"); 90fa9e4066Sahrens } 91fa9e4066Sahrens 92fa9e4066Sahrens return (NULL); 93fa9e4066Sahrens } 94fa9e4066Sahrens 95fa9e4066Sahrens /* 96fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 9714843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to 9814843421SMatthew Ahrens * explain exactly why the name was not valid. 99fa9e4066Sahrens */ 10099d5e173STim Haley int 101f18faf3fSek zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 102f18faf3fSek boolean_t modifying) 103fa9e4066Sahrens { 104fa9e4066Sahrens namecheck_err_t why; 105fa9e4066Sahrens char what; 106fa9e4066Sahrens 1071af68beaSAlexander Stetsenko (void) zfs_prop_get_table(); 108fa9e4066Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 10999653d4eSeschrock if (hdl != NULL) { 110fa9e4066Sahrens switch (why) { 111b81d61a6Slling case NAME_ERR_TOOLONG: 11299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11399653d4eSeschrock "name is too long")); 114b81d61a6Slling break; 115b81d61a6Slling 116fa9e4066Sahrens case NAME_ERR_LEADING_SLASH: 11799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11899653d4eSeschrock "leading slash in name")); 119fa9e4066Sahrens break; 120fa9e4066Sahrens 121fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT: 12299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12399653d4eSeschrock "empty component in name")); 124fa9e4066Sahrens break; 125fa9e4066Sahrens 126fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH: 12799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 12899653d4eSeschrock "trailing slash in name")); 129fa9e4066Sahrens break; 130fa9e4066Sahrens 131fa9e4066Sahrens case NAME_ERR_INVALCHAR: 13299653d4eSeschrock zfs_error_aux(hdl, 133fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 13499653d4eSeschrock "'%c' in name"), what); 135fa9e4066Sahrens break; 136fa9e4066Sahrens 137fa9e4066Sahrens case NAME_ERR_MULTIPLE_AT: 13899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 13999653d4eSeschrock "multiple '@' delimiters in name")); 140fa9e4066Sahrens break; 1415ad82045Snd 1425ad82045Snd case NAME_ERR_NOLETTER: 1435ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1445ad82045Snd "pool doesn't begin with a letter")); 1455ad82045Snd break; 1465ad82045Snd 1475ad82045Snd case NAME_ERR_RESERVED: 1485ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1495ad82045Snd "name is reserved")); 1505ad82045Snd break; 1515ad82045Snd 1525ad82045Snd case NAME_ERR_DISKLIKE: 1535ad82045Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1545ad82045Snd "reserved disk name")); 1555ad82045Snd break; 15688f61deeSIgor Kozhukhov 15788f61deeSIgor Kozhukhov default: 15888f61deeSIgor Kozhukhov zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15988f61deeSIgor Kozhukhov "(%d) not defined"), why); 16088f61deeSIgor Kozhukhov break; 161fa9e4066Sahrens } 162fa9e4066Sahrens } 163fa9e4066Sahrens 164fa9e4066Sahrens return (0); 165fa9e4066Sahrens } 166fa9e4066Sahrens 167fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 16899653d4eSeschrock if (hdl != NULL) 16999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17099653d4eSeschrock "snapshot delimiter '@' in filesystem name")); 171fa9e4066Sahrens return (0); 172fa9e4066Sahrens } 173fa9e4066Sahrens 1741d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 1751d452cf5Sahrens if (hdl != NULL) 1761d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 177d7d4af51Smmusante "missing '@' delimiter in snapshot name")); 1781d452cf5Sahrens return (0); 1791d452cf5Sahrens } 1801d452cf5Sahrens 181f18faf3fSek if (modifying && strchr(path, '%') != NULL) { 182f18faf3fSek if (hdl != NULL) 183f18faf3fSek zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 184f18faf3fSek "invalid character %c in name"), '%'); 185f18faf3fSek return (0); 186f18faf3fSek } 187f18faf3fSek 18899653d4eSeschrock return (-1); 189fa9e4066Sahrens } 190fa9e4066Sahrens 191fa9e4066Sahrens int 192fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type) 193fa9e4066Sahrens { 194e7cbe64fSgw if (type == ZFS_TYPE_POOL) 195e7cbe64fSgw return (zpool_name_valid(NULL, B_FALSE, name)); 196f18faf3fSek return (zfs_validate_name(NULL, name, type, B_FALSE)); 197fa9e4066Sahrens } 198fa9e4066Sahrens 199e9dbad6fSeschrock /* 200e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined 201e9dbad6fSeschrock * properties into a separate nvlist. 202e9dbad6fSeschrock */ 203fac3008cSeschrock static nvlist_t * 204fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 205e9dbad6fSeschrock { 206e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 207e9dbad6fSeschrock nvpair_t *elem; 208e9dbad6fSeschrock nvlist_t *propval; 209fac3008cSeschrock nvlist_t *nvl; 210e9dbad6fSeschrock 211fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 212fac3008cSeschrock (void) no_memory(hdl); 213fac3008cSeschrock return (NULL); 214fac3008cSeschrock } 215e9dbad6fSeschrock 216e9dbad6fSeschrock elem = NULL; 217fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 218e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem))) 219e9dbad6fSeschrock continue; 220e9dbad6fSeschrock 221e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 222fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 223fac3008cSeschrock nvlist_free(nvl); 224fac3008cSeschrock (void) no_memory(hdl); 225fac3008cSeschrock return (NULL); 226fac3008cSeschrock } 227e9dbad6fSeschrock } 228e9dbad6fSeschrock 229fac3008cSeschrock return (nvl); 230e9dbad6fSeschrock } 231e9dbad6fSeschrock 23229ab75c9Srm static zpool_handle_t * 23329ab75c9Srm zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 23429ab75c9Srm { 23529ab75c9Srm libzfs_handle_t *hdl = zhp->zfs_hdl; 23629ab75c9Srm zpool_handle_t *zph; 23729ab75c9Srm 23829ab75c9Srm if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 23929ab75c9Srm if (hdl->libzfs_pool_handles != NULL) 24029ab75c9Srm zph->zpool_next = hdl->libzfs_pool_handles; 24129ab75c9Srm hdl->libzfs_pool_handles = zph; 24229ab75c9Srm } 24329ab75c9Srm return (zph); 24429ab75c9Srm } 24529ab75c9Srm 24629ab75c9Srm static zpool_handle_t * 24729ab75c9Srm zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 24829ab75c9Srm { 24929ab75c9Srm libzfs_handle_t *hdl = zhp->zfs_hdl; 25029ab75c9Srm zpool_handle_t *zph = hdl->libzfs_pool_handles; 25129ab75c9Srm 25229ab75c9Srm while ((zph != NULL) && 25329ab75c9Srm (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 25429ab75c9Srm zph = zph->zpool_next; 25529ab75c9Srm return (zph); 25629ab75c9Srm } 25729ab75c9Srm 25829ab75c9Srm /* 25929ab75c9Srm * Returns a handle to the pool that contains the provided dataset. 26029ab75c9Srm * If a handle to that pool already exists then that handle is returned. 26129ab75c9Srm * Otherwise, a new handle is created and added to the list of handles. 26229ab75c9Srm */ 26329ab75c9Srm static zpool_handle_t * 26429ab75c9Srm zpool_handle(zfs_handle_t *zhp) 26529ab75c9Srm { 26629ab75c9Srm char *pool_name; 26729ab75c9Srm int len; 26829ab75c9Srm zpool_handle_t *zph; 26929ab75c9Srm 27078f17100SMatthew Ahrens len = strcspn(zhp->zfs_name, "/@#") + 1; 27129ab75c9Srm pool_name = zfs_alloc(zhp->zfs_hdl, len); 27229ab75c9Srm (void) strlcpy(pool_name, zhp->zfs_name, len); 27329ab75c9Srm 27429ab75c9Srm zph = zpool_find_handle(zhp, pool_name, len); 27529ab75c9Srm if (zph == NULL) 27629ab75c9Srm zph = zpool_add_handle(zhp, pool_name); 27729ab75c9Srm 27829ab75c9Srm free(pool_name); 27929ab75c9Srm return (zph); 28029ab75c9Srm } 28129ab75c9Srm 28229ab75c9Srm void 28329ab75c9Srm zpool_free_handles(libzfs_handle_t *hdl) 28429ab75c9Srm { 28529ab75c9Srm zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 28629ab75c9Srm 28729ab75c9Srm while (zph != NULL) { 28829ab75c9Srm next = zph->zpool_next; 28929ab75c9Srm zpool_close(zph); 29029ab75c9Srm zph = next; 29129ab75c9Srm } 29229ab75c9Srm hdl->libzfs_pool_handles = NULL; 29329ab75c9Srm } 29429ab75c9Srm 295fa9e4066Sahrens /* 296fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object. 297fa9e4066Sahrens */ 298fa9e4066Sahrens static int 299ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 300fa9e4066Sahrens { 301e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 302fa9e4066Sahrens 303ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 304fa9e4066Sahrens 305ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3067f7322feSeschrock if (errno == ENOMEM) { 307ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 30899653d4eSeschrock return (-1); 309e9dbad6fSeschrock } 3107f7322feSeschrock } else { 3117f7322feSeschrock return (-1); 3127f7322feSeschrock } 3137f7322feSeschrock } 314ebedde84SEric Taylor return (0); 315ebedde84SEric Taylor } 316fa9e4066Sahrens 31792241e0bSTom Erickson /* 31892241e0bSTom Erickson * Utility function to get the received properties of the given object. 31992241e0bSTom Erickson */ 32092241e0bSTom Erickson static int 32192241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp) 32292241e0bSTom Erickson { 32392241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl; 32492241e0bSTom Erickson nvlist_t *recvdprops; 32592241e0bSTom Erickson zfs_cmd_t zc = { 0 }; 32692241e0bSTom Erickson int err; 32792241e0bSTom Erickson 32892241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 32992241e0bSTom Erickson return (-1); 33092241e0bSTom Erickson 33192241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 33292241e0bSTom Erickson 33392241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 33492241e0bSTom Erickson if (errno == ENOMEM) { 33592241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 33692241e0bSTom Erickson return (-1); 33792241e0bSTom Erickson } 33892241e0bSTom Erickson } else { 33992241e0bSTom Erickson zcmd_free_nvlists(&zc); 34092241e0bSTom Erickson return (-1); 34192241e0bSTom Erickson } 34292241e0bSTom Erickson } 34392241e0bSTom Erickson 34492241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 34592241e0bSTom Erickson zcmd_free_nvlists(&zc); 34692241e0bSTom Erickson if (err != 0) 34792241e0bSTom Erickson return (-1); 34892241e0bSTom Erickson 34992241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 35092241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops; 35192241e0bSTom Erickson 35292241e0bSTom Erickson return (0); 35392241e0bSTom Erickson } 35492241e0bSTom Erickson 355ebedde84SEric Taylor static int 356ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 357ebedde84SEric Taylor { 358ebedde84SEric Taylor nvlist_t *allprops, *userprops; 359fa9e4066Sahrens 360ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 361ebedde84SEric Taylor 362ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 36399653d4eSeschrock return (-1); 36499653d4eSeschrock } 365fa9e4066Sahrens 36614843421SMatthew Ahrens /* 36714843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to 36814843421SMatthew Ahrens * storing them in zfs_props? 36914843421SMatthew Ahrens */ 370fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 371fac3008cSeschrock nvlist_free(allprops); 372e9dbad6fSeschrock return (-1); 373fac3008cSeschrock } 374fac3008cSeschrock 375fac3008cSeschrock nvlist_free(zhp->zfs_props); 376fac3008cSeschrock nvlist_free(zhp->zfs_user_props); 377fac3008cSeschrock 378fac3008cSeschrock zhp->zfs_props = allprops; 379fac3008cSeschrock zhp->zfs_user_props = userprops; 38099653d4eSeschrock 381fa9e4066Sahrens return (0); 382fa9e4066Sahrens } 383fa9e4066Sahrens 384ebedde84SEric Taylor static int 385ebedde84SEric Taylor get_stats(zfs_handle_t *zhp) 386ebedde84SEric Taylor { 387ebedde84SEric Taylor int rc = 0; 388ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 389ebedde84SEric Taylor 390ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 391ebedde84SEric Taylor return (-1); 392ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0) 393ebedde84SEric Taylor rc = -1; 394ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0) 395ebedde84SEric Taylor rc = -1; 396ebedde84SEric Taylor zcmd_free_nvlists(&zc); 397ebedde84SEric Taylor return (rc); 398ebedde84SEric Taylor } 399ebedde84SEric Taylor 400fa9e4066Sahrens /* 401fa9e4066Sahrens * Refresh the properties currently stored in the handle. 402fa9e4066Sahrens */ 403fa9e4066Sahrens void 404fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 405fa9e4066Sahrens { 406fa9e4066Sahrens (void) get_stats(zhp); 407fa9e4066Sahrens } 408fa9e4066Sahrens 409fa9e4066Sahrens /* 410fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 411fa9e4066Sahrens * zfs_iter_* to create child handles on the fly. 412fa9e4066Sahrens */ 413ebedde84SEric Taylor static int 414ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 415fa9e4066Sahrens { 416503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0) 417ebedde84SEric Taylor return (-1); 41831fd60d3Sahrens 419fa9e4066Sahrens /* 420fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine 421fa9e4066Sahrens * the high-level type. 422fa9e4066Sahrens */ 423a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 424a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 425a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 426a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 427a2eea2e1Sahrens else 428a2eea2e1Sahrens abort(); 429a2eea2e1Sahrens 430fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 431fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 432fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 433fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 434fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 435fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 436fa9e4066Sahrens else 43799653d4eSeschrock abort(); /* we should never see any other types */ 438fa9e4066Sahrens 4399fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 4409fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1); 4419fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 442ebedde84SEric Taylor return (0); 443ebedde84SEric Taylor } 444ebedde84SEric Taylor 445ebedde84SEric Taylor zfs_handle_t * 446ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path) 447ebedde84SEric Taylor { 448ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 449ebedde84SEric Taylor 450ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 451ebedde84SEric Taylor 452ebedde84SEric Taylor if (zhp == NULL) 453ebedde84SEric Taylor return (NULL); 454ebedde84SEric Taylor 455ebedde84SEric Taylor zhp->zfs_hdl = hdl; 456ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 457ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 458ebedde84SEric Taylor free(zhp); 459ebedde84SEric Taylor return (NULL); 460ebedde84SEric Taylor } 461ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) { 462ebedde84SEric Taylor zcmd_free_nvlists(&zc); 463ebedde84SEric Taylor free(zhp); 464ebedde84SEric Taylor return (NULL); 465ebedde84SEric Taylor } 466ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) { 467ebedde84SEric Taylor free(zhp); 468ebedde84SEric Taylor zhp = NULL; 469ebedde84SEric Taylor } 470ebedde84SEric Taylor zcmd_free_nvlists(&zc); 471ebedde84SEric Taylor return (zhp); 472ebedde84SEric Taylor } 473ebedde84SEric Taylor 47419b94df9SMatthew Ahrens zfs_handle_t * 475ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 476ebedde84SEric Taylor { 477ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 478ebedde84SEric Taylor 479ebedde84SEric Taylor if (zhp == NULL) 480ebedde84SEric Taylor return (NULL); 481ebedde84SEric Taylor 482ebedde84SEric Taylor zhp->zfs_hdl = hdl; 483ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 484ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) { 485ebedde84SEric Taylor free(zhp); 486ebedde84SEric Taylor return (NULL); 487ebedde84SEric Taylor } 488fa9e4066Sahrens return (zhp); 489fa9e4066Sahrens } 490fa9e4066Sahrens 4910d8fa8f8SMartin Matuska zfs_handle_t * 4920d8fa8f8SMartin Matuska make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc) 4930d8fa8f8SMartin Matuska { 4940d8fa8f8SMartin Matuska zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 4950d8fa8f8SMartin Matuska 4960d8fa8f8SMartin Matuska if (zhp == NULL) 4970d8fa8f8SMartin Matuska return (NULL); 4980d8fa8f8SMartin Matuska 4990d8fa8f8SMartin Matuska zhp->zfs_hdl = pzhp->zfs_hdl; 5000d8fa8f8SMartin Matuska (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 5010d8fa8f8SMartin Matuska zhp->zfs_head_type = pzhp->zfs_type; 5020d8fa8f8SMartin Matuska zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 5030d8fa8f8SMartin Matuska zhp->zpool_hdl = zpool_handle(zhp); 5040d8fa8f8SMartin Matuska return (zhp); 5050d8fa8f8SMartin Matuska } 5060d8fa8f8SMartin Matuska 50719b94df9SMatthew Ahrens zfs_handle_t * 50819b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig) 50919b94df9SMatthew Ahrens { 51019b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 51119b94df9SMatthew Ahrens 51219b94df9SMatthew Ahrens if (zhp == NULL) 51319b94df9SMatthew Ahrens return (NULL); 51419b94df9SMatthew Ahrens 51519b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl; 51619b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl; 51719b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 51819b94df9SMatthew Ahrens sizeof (zhp->zfs_name)); 51919b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type; 52019b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type; 52119b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 52219b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) { 52319b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { 52419b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 52519b94df9SMatthew Ahrens zfs_close(zhp); 52619b94df9SMatthew Ahrens return (NULL); 52719b94df9SMatthew Ahrens } 52819b94df9SMatthew Ahrens } 52919b94df9SMatthew Ahrens if (zhp_orig->zfs_user_props != NULL) { 53019b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props, 53119b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) { 53219b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 53319b94df9SMatthew Ahrens zfs_close(zhp); 53419b94df9SMatthew Ahrens return (NULL); 53519b94df9SMatthew Ahrens } 53619b94df9SMatthew Ahrens } 53719b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) { 53819b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props, 53919b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) { 54019b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 54119b94df9SMatthew Ahrens zfs_close(zhp); 54219b94df9SMatthew Ahrens return (NULL); 54319b94df9SMatthew Ahrens } 54419b94df9SMatthew Ahrens } 54519b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 54619b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) { 54719b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 54819b94df9SMatthew Ahrens zhp_orig->zfs_mntopts); 54919b94df9SMatthew Ahrens } 55019b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table; 55119b94df9SMatthew Ahrens return (zhp); 55219b94df9SMatthew Ahrens } 55319b94df9SMatthew Ahrens 55478f17100SMatthew Ahrens boolean_t 55578f17100SMatthew Ahrens zfs_bookmark_exists(const char *path) 55678f17100SMatthew Ahrens { 55778f17100SMatthew Ahrens nvlist_t *bmarks; 55878f17100SMatthew Ahrens nvlist_t *props; 559*9adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 56078f17100SMatthew Ahrens char *bmark_name; 56178f17100SMatthew Ahrens char *pound; 56278f17100SMatthew Ahrens int err; 56378f17100SMatthew Ahrens boolean_t rv; 56478f17100SMatthew Ahrens 56578f17100SMatthew Ahrens 56678f17100SMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 56778f17100SMatthew Ahrens pound = strchr(fsname, '#'); 56878f17100SMatthew Ahrens if (pound == NULL) 56978f17100SMatthew Ahrens return (B_FALSE); 57078f17100SMatthew Ahrens 57178f17100SMatthew Ahrens *pound = '\0'; 57278f17100SMatthew Ahrens bmark_name = pound + 1; 57378f17100SMatthew Ahrens props = fnvlist_alloc(); 57478f17100SMatthew Ahrens err = lzc_get_bookmarks(fsname, props, &bmarks); 57578f17100SMatthew Ahrens nvlist_free(props); 57678f17100SMatthew Ahrens if (err != 0) { 57778f17100SMatthew Ahrens nvlist_free(bmarks); 57878f17100SMatthew Ahrens return (B_FALSE); 57978f17100SMatthew Ahrens } 58078f17100SMatthew Ahrens 58178f17100SMatthew Ahrens rv = nvlist_exists(bmarks, bmark_name); 58278f17100SMatthew Ahrens nvlist_free(bmarks); 58378f17100SMatthew Ahrens return (rv); 58478f17100SMatthew Ahrens } 58578f17100SMatthew Ahrens 58678f17100SMatthew Ahrens zfs_handle_t * 58778f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path, 58878f17100SMatthew Ahrens nvlist_t *bmark_props) 58978f17100SMatthew Ahrens { 59078f17100SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 59178f17100SMatthew Ahrens 59278f17100SMatthew Ahrens if (zhp == NULL) 59378f17100SMatthew Ahrens return (NULL); 59478f17100SMatthew Ahrens 59578f17100SMatthew Ahrens /* Fill in the name. */ 59678f17100SMatthew Ahrens zhp->zfs_hdl = parent->zfs_hdl; 59778f17100SMatthew Ahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 59878f17100SMatthew Ahrens 59978f17100SMatthew Ahrens /* Set the property lists. */ 60078f17100SMatthew Ahrens if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { 60178f17100SMatthew Ahrens free(zhp); 60278f17100SMatthew Ahrens return (NULL); 60378f17100SMatthew Ahrens } 60478f17100SMatthew Ahrens 60578f17100SMatthew Ahrens /* Set the types. */ 60678f17100SMatthew Ahrens zhp->zfs_head_type = parent->zfs_head_type; 60778f17100SMatthew Ahrens zhp->zfs_type = ZFS_TYPE_BOOKMARK; 60878f17100SMatthew Ahrens 60978f17100SMatthew Ahrens if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { 61078f17100SMatthew Ahrens nvlist_free(zhp->zfs_props); 61178f17100SMatthew Ahrens free(zhp); 61278f17100SMatthew Ahrens return (NULL); 61378f17100SMatthew Ahrens } 61478f17100SMatthew Ahrens 61578f17100SMatthew Ahrens return (zhp); 61678f17100SMatthew Ahrens } 61778f17100SMatthew Ahrens 618fa9e4066Sahrens /* 619fa9e4066Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 620fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an 621fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened. 622fa9e4066Sahrens */ 623fa9e4066Sahrens zfs_handle_t * 62499653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 625fa9e4066Sahrens { 626fa9e4066Sahrens zfs_handle_t *zhp; 62799653d4eSeschrock char errbuf[1024]; 62899653d4eSeschrock 62999653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 63099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 631fa9e4066Sahrens 632fa9e4066Sahrens /* 63399653d4eSeschrock * Validate the name before we even try to open it. 634fa9e4066Sahrens */ 635f18faf3fSek if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 63699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 63799653d4eSeschrock "invalid dataset name")); 63899653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 639fa9e4066Sahrens return (NULL); 640fa9e4066Sahrens } 641fa9e4066Sahrens 642fa9e4066Sahrens /* 643fa9e4066Sahrens * Try to get stats for the dataset, which will tell us if it exists. 644fa9e4066Sahrens */ 645fa9e4066Sahrens errno = 0; 64699653d4eSeschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 647ece3d9b3Slling (void) zfs_standard_error(hdl, errno, errbuf); 648fa9e4066Sahrens return (NULL); 649fa9e4066Sahrens } 650fa9e4066Sahrens 651fa9e4066Sahrens if (!(types & zhp->zfs_type)) { 65299653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 65394de1d4cSeschrock zfs_close(zhp); 654fa9e4066Sahrens return (NULL); 655fa9e4066Sahrens } 656fa9e4066Sahrens 657fa9e4066Sahrens return (zhp); 658fa9e4066Sahrens } 659fa9e4066Sahrens 660fa9e4066Sahrens /* 661fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 662fa9e4066Sahrens */ 663fa9e4066Sahrens void 664fa9e4066Sahrens zfs_close(zfs_handle_t *zhp) 665fa9e4066Sahrens { 666fa9e4066Sahrens if (zhp->zfs_mntopts) 667fa9e4066Sahrens free(zhp->zfs_mntopts); 668e9dbad6fSeschrock nvlist_free(zhp->zfs_props); 669e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props); 67092241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 671fa9e4066Sahrens free(zhp); 672fa9e4066Sahrens } 673fa9e4066Sahrens 674ebedde84SEric Taylor typedef struct mnttab_node { 675ebedde84SEric Taylor struct mnttab mtn_mt; 676ebedde84SEric Taylor avl_node_t mtn_node; 677ebedde84SEric Taylor } mnttab_node_t; 678ebedde84SEric Taylor 679ebedde84SEric Taylor static int 680ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 681ebedde84SEric Taylor { 682ebedde84SEric Taylor const mnttab_node_t *mtn1 = arg1; 683ebedde84SEric Taylor const mnttab_node_t *mtn2 = arg2; 684ebedde84SEric Taylor int rv; 685ebedde84SEric Taylor 686ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 687ebedde84SEric Taylor 688ebedde84SEric Taylor if (rv == 0) 689ebedde84SEric Taylor return (0); 690ebedde84SEric Taylor return (rv > 0 ? 1 : -1); 691ebedde84SEric Taylor } 692ebedde84SEric Taylor 693ebedde84SEric Taylor void 694ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl) 695ebedde84SEric Taylor { 696ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 697ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 698ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 699b2634b9cSEric Taylor } 700b2634b9cSEric Taylor 701b2634b9cSEric Taylor void 702b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl) 703b2634b9cSEric Taylor { 704b2634b9cSEric Taylor struct mnttab entry; 705ebedde84SEric Taylor 706ebedde84SEric Taylor rewind(hdl->libzfs_mnttab); 707ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 708ebedde84SEric Taylor mnttab_node_t *mtn; 709ebedde84SEric Taylor 710ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 711ebedde84SEric Taylor continue; 712ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 713ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 714ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 715ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 716ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 717ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 718ebedde84SEric Taylor } 719ebedde84SEric Taylor } 720ebedde84SEric Taylor 721ebedde84SEric Taylor void 722ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl) 723ebedde84SEric Taylor { 724ebedde84SEric Taylor void *cookie = NULL; 725ebedde84SEric Taylor mnttab_node_t *mtn; 726ebedde84SEric Taylor 72788f61deeSIgor Kozhukhov while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) 72888f61deeSIgor Kozhukhov != NULL) { 729ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special); 730ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp); 731ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype); 732ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts); 733ebedde84SEric Taylor free(mtn); 734ebedde84SEric Taylor } 735ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache); 736ebedde84SEric Taylor } 737ebedde84SEric Taylor 738b2634b9cSEric Taylor void 739b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 740b2634b9cSEric Taylor { 741b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable; 742b2634b9cSEric Taylor } 743b2634b9cSEric Taylor 744ebedde84SEric Taylor int 745ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 746ebedde84SEric Taylor struct mnttab *entry) 747ebedde84SEric Taylor { 748ebedde84SEric Taylor mnttab_node_t find; 749ebedde84SEric Taylor mnttab_node_t *mtn; 750ebedde84SEric Taylor 751b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) { 752b2634b9cSEric Taylor struct mnttab srch = { 0 }; 753b2634b9cSEric Taylor 754b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 755b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 756b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab); 757b2634b9cSEric Taylor srch.mnt_special = (char *)fsname; 758b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS; 759b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 760b2634b9cSEric Taylor return (0); 761b2634b9cSEric Taylor else 762b2634b9cSEric Taylor return (ENOENT); 763b2634b9cSEric Taylor } 764b2634b9cSEric Taylor 765ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 766b2634b9cSEric Taylor libzfs_mnttab_update(hdl); 767ebedde84SEric Taylor 768ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 769ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 770ebedde84SEric Taylor if (mtn) { 771ebedde84SEric Taylor *entry = mtn->mtn_mt; 772ebedde84SEric Taylor return (0); 773ebedde84SEric Taylor } 774ebedde84SEric Taylor return (ENOENT); 775ebedde84SEric Taylor } 776ebedde84SEric Taylor 777ebedde84SEric Taylor void 778ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 779ebedde84SEric Taylor const char *mountp, const char *mntopts) 780ebedde84SEric Taylor { 781ebedde84SEric Taylor mnttab_node_t *mtn; 782ebedde84SEric Taylor 783ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 784ebedde84SEric Taylor return; 785ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 786ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 787ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 788ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 789ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 790ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 791ebedde84SEric Taylor } 792ebedde84SEric Taylor 793ebedde84SEric Taylor void 794ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 795ebedde84SEric Taylor { 796ebedde84SEric Taylor mnttab_node_t find; 797ebedde84SEric Taylor mnttab_node_t *ret; 798ebedde84SEric Taylor 799ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 80088f61deeSIgor Kozhukhov if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) 80188f61deeSIgor Kozhukhov != NULL) { 802ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret); 803ebedde84SEric Taylor free(ret->mtn_mt.mnt_special); 804ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp); 805ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype); 806ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts); 807ebedde84SEric Taylor free(ret); 808ebedde84SEric Taylor } 809ebedde84SEric Taylor } 810ebedde84SEric Taylor 8117b97dc1aSrm int 8127b97dc1aSrm zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 8137b97dc1aSrm { 81429ab75c9Srm zpool_handle_t *zpool_handle = zhp->zpool_hdl; 8157b97dc1aSrm 8167b97dc1aSrm if (zpool_handle == NULL) 8177b97dc1aSrm return (-1); 8187b97dc1aSrm 8197b97dc1aSrm *spa_version = zpool_get_prop_int(zpool_handle, 8207b97dc1aSrm ZPOOL_PROP_VERSION, NULL); 8217b97dc1aSrm return (0); 8227b97dc1aSrm } 8237b97dc1aSrm 8247b97dc1aSrm /* 8257b97dc1aSrm * The choice of reservation property depends on the SPA version. 8267b97dc1aSrm */ 8277b97dc1aSrm static int 8287b97dc1aSrm zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 8297b97dc1aSrm { 8307b97dc1aSrm int spa_version; 8317b97dc1aSrm 8327b97dc1aSrm if (zfs_spa_version(zhp, &spa_version) < 0) 8337b97dc1aSrm return (-1); 8347b97dc1aSrm 8357b97dc1aSrm if (spa_version >= SPA_VERSION_REFRESERVATION) 8367b97dc1aSrm *resv_prop = ZFS_PROP_REFRESERVATION; 8377b97dc1aSrm else 8387b97dc1aSrm *resv_prop = ZFS_PROP_RESERVATION; 8397b97dc1aSrm 8407b97dc1aSrm return (0); 8417b97dc1aSrm } 8427b97dc1aSrm 843e9dbad6fSeschrock /* 844e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and 845e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as 846e9dbad6fSeschrock * strings. 847e9dbad6fSeschrock */ 8480a48a24eStimh nvlist_t * 8490a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 850e9316f76SJoe Stein uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl, 851e9316f76SJoe Stein const char *errbuf) 852e9dbad6fSeschrock { 853e9dbad6fSeschrock nvpair_t *elem; 854e9dbad6fSeschrock uint64_t intval; 855e9dbad6fSeschrock char *strval; 856990b4856Slling zfs_prop_t prop; 857e9dbad6fSeschrock nvlist_t *ret; 858da6c28aaSamw int chosen_normal = -1; 859da6c28aaSamw int chosen_utf = -1; 860e9dbad6fSeschrock 861990b4856Slling if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 862990b4856Slling (void) no_memory(hdl); 863990b4856Slling return (NULL); 864e9dbad6fSeschrock } 865e9dbad6fSeschrock 86614843421SMatthew Ahrens /* 86714843421SMatthew Ahrens * Make sure this property is valid and applies to this type. 86814843421SMatthew Ahrens */ 86914843421SMatthew Ahrens 870e9dbad6fSeschrock elem = NULL; 871e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 872990b4856Slling const char *propname = nvpair_name(elem); 873e9dbad6fSeschrock 87414843421SMatthew Ahrens prop = zfs_name_to_prop(propname); 87514843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 876990b4856Slling /* 87714843421SMatthew Ahrens * This is a user property: make sure it's a 878990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN. 879990b4856Slling */ 880990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 881990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 882990b4856Slling "'%s' must be a string"), propname); 883990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 884990b4856Slling goto error; 885990b4856Slling } 886990b4856Slling 887990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 888990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 889990b4856Slling "property name '%s' is too long"), 890990b4856Slling propname); 891990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 892990b4856Slling goto error; 893fa9e4066Sahrens } 894fa9e4066Sahrens 895e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval); 896e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) { 897e9dbad6fSeschrock (void) no_memory(hdl); 898e9dbad6fSeschrock goto error; 899e9dbad6fSeschrock } 900e9dbad6fSeschrock continue; 901e9dbad6fSeschrock } 902e9dbad6fSeschrock 90314843421SMatthew Ahrens /* 90414843421SMatthew Ahrens * Currently, only user properties can be modified on 90514843421SMatthew Ahrens * snapshots. 90614843421SMatthew Ahrens */ 907bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 908bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 909bb0ade09Sahrens "this property can not be modified for snapshots")); 910bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 911bb0ade09Sahrens goto error; 912bb0ade09Sahrens } 913bb0ade09Sahrens 91414843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 91514843421SMatthew Ahrens zfs_userquota_prop_t uqtype; 91614843421SMatthew Ahrens char newpropname[128]; 91714843421SMatthew Ahrens char domain[128]; 91814843421SMatthew Ahrens uint64_t rid; 91914843421SMatthew Ahrens uint64_t valary[3]; 92014843421SMatthew Ahrens 92114843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned, 92214843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) { 92314843421SMatthew Ahrens zfs_error_aux(hdl, 92414843421SMatthew Ahrens dgettext(TEXT_DOMAIN, 92514843421SMatthew Ahrens "'%s' has an invalid user/group name"), 92614843421SMatthew Ahrens propname); 92714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 92814843421SMatthew Ahrens goto error; 92914843421SMatthew Ahrens } 93014843421SMatthew Ahrens 93114843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA && 93214843421SMatthew Ahrens uqtype != ZFS_PROP_GROUPQUOTA) { 93314843421SMatthew Ahrens zfs_error_aux(hdl, 93414843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"), 93514843421SMatthew Ahrens propname); 93614843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, 93714843421SMatthew Ahrens errbuf); 93814843421SMatthew Ahrens goto error; 93914843421SMatthew Ahrens } 94014843421SMatthew Ahrens 94114843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) { 94214843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval); 94314843421SMatthew Ahrens if (strcmp(strval, "none") == 0) { 94414843421SMatthew Ahrens intval = 0; 94514843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl, 94614843421SMatthew Ahrens strval, &intval) != 0) { 94714843421SMatthew Ahrens (void) zfs_error(hdl, 94814843421SMatthew Ahrens EZFS_BADPROP, errbuf); 94914843421SMatthew Ahrens goto error; 95014843421SMatthew Ahrens } 95114843421SMatthew Ahrens } else if (nvpair_type(elem) == 95214843421SMatthew Ahrens DATA_TYPE_UINT64) { 95314843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval); 95414843421SMatthew Ahrens if (intval == 0) { 95514843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 95614843421SMatthew Ahrens "use 'none' to disable " 95714843421SMatthew Ahrens "userquota/groupquota")); 95814843421SMatthew Ahrens goto error; 95914843421SMatthew Ahrens } 96014843421SMatthew Ahrens } else { 96114843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96214843421SMatthew Ahrens "'%s' must be a number"), propname); 96314843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 96414843421SMatthew Ahrens goto error; 96514843421SMatthew Ahrens } 96614843421SMatthew Ahrens 9672d5843dbSMatthew Ahrens /* 9682d5843dbSMatthew Ahrens * Encode the prop name as 9692d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy 9702d5843dbSMatthew Ahrens * for the kernel to decode. 9712d5843dbSMatthew Ahrens */ 97214843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname), 9732d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 9742d5843dbSMatthew Ahrens (longlong_t)rid, domain); 97514843421SMatthew Ahrens valary[0] = uqtype; 97614843421SMatthew Ahrens valary[1] = rid; 97714843421SMatthew Ahrens valary[2] = intval; 97814843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname, 97914843421SMatthew Ahrens valary, 3) != 0) { 98014843421SMatthew Ahrens (void) no_memory(hdl); 98114843421SMatthew Ahrens goto error; 98214843421SMatthew Ahrens } 98314843421SMatthew Ahrens continue; 98419b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { 98519b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 98619b94df9SMatthew Ahrens "'%s' is readonly"), 98719b94df9SMatthew Ahrens propname); 98819b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 98919b94df9SMatthew Ahrens goto error; 99014843421SMatthew Ahrens } 99114843421SMatthew Ahrens 99214843421SMatthew Ahrens if (prop == ZPROP_INVAL) { 99314843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 99414843421SMatthew Ahrens "invalid property '%s'"), propname); 99514843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 99614843421SMatthew Ahrens goto error; 99714843421SMatthew Ahrens } 99814843421SMatthew Ahrens 999e9dbad6fSeschrock if (!zfs_prop_valid_for_type(prop, type)) { 1000e9dbad6fSeschrock zfs_error_aux(hdl, 1001e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not " 1002e9dbad6fSeschrock "apply to datasets of this type"), propname); 1003e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1004e9dbad6fSeschrock goto error; 1005e9dbad6fSeschrock } 1006e9dbad6fSeschrock 1007e9dbad6fSeschrock if (zfs_prop_readonly(prop) && 1008da6c28aaSamw (!zfs_prop_setonce(prop) || zhp != NULL)) { 1009e9dbad6fSeschrock zfs_error_aux(hdl, 1010e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1011e9dbad6fSeschrock propname); 1012e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1013e9dbad6fSeschrock goto error; 1014e9dbad6fSeschrock } 1015e9dbad6fSeschrock 1016990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 1017990b4856Slling &strval, &intval, errbuf) != 0) 1018e9dbad6fSeschrock goto error; 1019fa9e4066Sahrens 1020e9dbad6fSeschrock /* 1021e9dbad6fSeschrock * Perform some additional checks for specific properties. 1022e9dbad6fSeschrock */ 1023e9dbad6fSeschrock switch (prop) { 1024e7437265Sahrens case ZFS_PROP_VERSION: 1025e7437265Sahrens { 1026e7437265Sahrens int version; 1027e7437265Sahrens 1028e7437265Sahrens if (zhp == NULL) 1029e7437265Sahrens break; 1030e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1031e7437265Sahrens if (intval < version) { 1032e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1033e7437265Sahrens "Can not downgrade; already at version %u"), 1034e7437265Sahrens version); 1035e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1036e7437265Sahrens goto error; 1037e7437265Sahrens } 1038e7437265Sahrens break; 1039e7437265Sahrens } 1040e7437265Sahrens 1041e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE: 1042b5152584SMatthew Ahrens case ZFS_PROP_RECORDSIZE: 1043b5152584SMatthew Ahrens { 1044b5152584SMatthew Ahrens int maxbs = SPA_MAXBLOCKSIZE; 1045e9316f76SJoe Stein if (zpool_hdl != NULL) { 1046e9316f76SJoe Stein maxbs = zpool_get_prop_int(zpool_hdl, 1047b5152584SMatthew Ahrens ZPOOL_PROP_MAXBLOCKSIZE, NULL); 1048b5152584SMatthew Ahrens } 1049b5152584SMatthew Ahrens /* 1050b5152584SMatthew Ahrens * Volumes are limited to a volblocksize of 128KB, 1051b5152584SMatthew Ahrens * because they typically service workloads with 1052b5152584SMatthew Ahrens * small random writes, which incur a large performance 1053b5152584SMatthew Ahrens * penalty with large blocks. 1054b5152584SMatthew Ahrens */ 1055b5152584SMatthew Ahrens if (prop == ZFS_PROP_VOLBLOCKSIZE) 1056b5152584SMatthew Ahrens maxbs = SPA_OLD_MAXBLOCKSIZE; 1057b5152584SMatthew Ahrens /* 1058b5152584SMatthew Ahrens * The value must be a power of two between 1059b5152584SMatthew Ahrens * SPA_MINBLOCKSIZE and maxbs. 1060b5152584SMatthew Ahrens */ 1061e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE || 1062b5152584SMatthew Ahrens intval > maxbs || !ISP2(intval)) { 106399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1064b5152584SMatthew Ahrens "'%s' must be power of 2 from 512B " 1065b5152584SMatthew Ahrens "to %uKB"), propname, maxbs >> 10); 1066e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1067e9dbad6fSeschrock goto error; 1068fa9e4066Sahrens } 1069fa9e4066Sahrens break; 1070b5152584SMatthew Ahrens } 10714201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 10724201a95eSRic Aleshire { 10734201a95eSRic Aleshire /* 10744201a95eSRic Aleshire * Verify the mlslabel string and convert to 10754201a95eSRic Aleshire * internal hex label string. 10764201a95eSRic Aleshire */ 10774201a95eSRic Aleshire 10784201a95eSRic Aleshire m_label_t *new_sl; 10794201a95eSRic Aleshire char *hex = NULL; /* internal label string */ 10804201a95eSRic Aleshire 10814201a95eSRic Aleshire /* Default value is already OK. */ 10824201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 10834201a95eSRic Aleshire break; 10844201a95eSRic Aleshire 10854201a95eSRic Aleshire /* Verify the label can be converted to binary form */ 10864201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 10874201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL, 10884201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) { 10894201a95eSRic Aleshire goto badlabel; 10904201a95eSRic Aleshire } 10914201a95eSRic Aleshire 10924201a95eSRic Aleshire /* Now translate to hex internal label string */ 10934201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL, 10944201a95eSRic Aleshire DEF_NAMES) != 0) { 10954201a95eSRic Aleshire if (hex) 10964201a95eSRic Aleshire free(hex); 10974201a95eSRic Aleshire goto badlabel; 10984201a95eSRic Aleshire } 10994201a95eSRic Aleshire m_label_free(new_sl); 11004201a95eSRic Aleshire 11014201a95eSRic Aleshire /* If string is already in internal form, we're done. */ 11024201a95eSRic Aleshire if (strcmp(strval, hex) == 0) { 11034201a95eSRic Aleshire free(hex); 11044201a95eSRic Aleshire break; 11054201a95eSRic Aleshire } 11064201a95eSRic Aleshire 11074201a95eSRic Aleshire /* Replace the label string with the internal form. */ 1108569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop), 11094201a95eSRic Aleshire DATA_TYPE_STRING); 11104201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop), 11114201a95eSRic Aleshire hex) == 0); 11124201a95eSRic Aleshire free(hex); 11134201a95eSRic Aleshire 11144201a95eSRic Aleshire break; 11154201a95eSRic Aleshire 11164201a95eSRic Aleshire badlabel: 11174201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11184201a95eSRic Aleshire "invalid mlslabel '%s'"), strval); 11194201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11204201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */ 11214201a95eSRic Aleshire goto error; 11224201a95eSRic Aleshire 11234201a95eSRic Aleshire } 11244201a95eSRic Aleshire 1125e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT: 112689eef05eSrm { 112789eef05eSrm namecheck_err_t why; 112889eef05eSrm 1129e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1130e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1131e9dbad6fSeschrock break; 1132fa9e4066Sahrens 113389eef05eSrm if (mountpoint_namecheck(strval, &why)) { 113489eef05eSrm switch (why) { 113589eef05eSrm case NAME_ERR_LEADING_SLASH: 113689eef05eSrm zfs_error_aux(hdl, 113789eef05eSrm dgettext(TEXT_DOMAIN, 113889eef05eSrm "'%s' must be an absolute path, " 113989eef05eSrm "'none', or 'legacy'"), propname); 114089eef05eSrm break; 114189eef05eSrm case NAME_ERR_TOOLONG: 114289eef05eSrm zfs_error_aux(hdl, 114389eef05eSrm dgettext(TEXT_DOMAIN, 114489eef05eSrm "component of '%s' is too long"), 114589eef05eSrm propname); 114689eef05eSrm break; 114788f61deeSIgor Kozhukhov 114888f61deeSIgor Kozhukhov default: 114988f61deeSIgor Kozhukhov zfs_error_aux(hdl, 115088f61deeSIgor Kozhukhov dgettext(TEXT_DOMAIN, 115188f61deeSIgor Kozhukhov "(%d) not defined"), 115288f61deeSIgor Kozhukhov why); 115388f61deeSIgor Kozhukhov break; 115489eef05eSrm } 1155e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1156e9dbad6fSeschrock goto error; 1157fa9e4066Sahrens } 115889eef05eSrm } 115989eef05eSrm 1160f3861e1aSahl /*FALLTHRU*/ 1161fa9e4066Sahrens 1162da6c28aaSamw case ZFS_PROP_SHARESMB: 1163f3861e1aSahl case ZFS_PROP_SHARENFS: 1164f3861e1aSahl /* 1165da6c28aaSamw * For the mountpoint and sharenfs or sharesmb 1166da6c28aaSamw * properties, check if it can be set in a 1167da6c28aaSamw * global/non-global zone based on 1168f3861e1aSahl * the zoned property value: 1169f3861e1aSahl * 1170f3861e1aSahl * global zone non-global zone 1171f3861e1aSahl * -------------------------------------------------- 1172f3861e1aSahl * zoned=on mountpoint (no) mountpoint (yes) 1173f3861e1aSahl * sharenfs (no) sharenfs (no) 1174da6c28aaSamw * sharesmb (no) sharesmb (no) 1175f3861e1aSahl * 1176f3861e1aSahl * zoned=off mountpoint (yes) N/A 1177f3861e1aSahl * sharenfs (yes) 1178da6c28aaSamw * sharesmb (yes) 1179f3861e1aSahl */ 1180e9dbad6fSeschrock if (zoned) { 1181e9dbad6fSeschrock if (getzoneid() == GLOBAL_ZONEID) { 1182e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1183e9dbad6fSeschrock "'%s' cannot be set on " 1184e9dbad6fSeschrock "dataset in a non-global zone"), 1185e9dbad6fSeschrock propname); 1186e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1187e9dbad6fSeschrock errbuf); 1188e9dbad6fSeschrock goto error; 1189da6c28aaSamw } else if (prop == ZFS_PROP_SHARENFS || 1190da6c28aaSamw prop == ZFS_PROP_SHARESMB) { 1191e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1192e9dbad6fSeschrock "'%s' cannot be set in " 1193e9dbad6fSeschrock "a non-global zone"), propname); 1194e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1195e9dbad6fSeschrock errbuf); 1196e9dbad6fSeschrock goto error; 1197fa9e4066Sahrens } 1198e9dbad6fSeschrock } else if (getzoneid() != GLOBAL_ZONEID) { 1199e9dbad6fSeschrock /* 1200e9dbad6fSeschrock * If zoned property is 'off', this must be in 120114843421SMatthew Ahrens * a global zone. If not, something is wrong. 1202e9dbad6fSeschrock */ 1203e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1204e9dbad6fSeschrock "'%s' cannot be set while dataset " 1205e9dbad6fSeschrock "'zoned' property is set"), propname); 1206e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1207e9dbad6fSeschrock goto error; 1208fa9e4066Sahrens } 1209f3861e1aSahl 121067331909Sdougm /* 121167331909Sdougm * At this point, it is legitimate to set the 121267331909Sdougm * property. Now we want to make sure that the 121367331909Sdougm * property value is valid if it is sharenfs. 121467331909Sdougm */ 1215da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS || 1216da6c28aaSamw prop == ZFS_PROP_SHARESMB) && 1217fac3008cSeschrock strcmp(strval, "on") != 0 && 1218fac3008cSeschrock strcmp(strval, "off") != 0) { 1219da6c28aaSamw zfs_share_proto_t proto; 1220da6c28aaSamw 1221da6c28aaSamw if (prop == ZFS_PROP_SHARESMB) 1222da6c28aaSamw proto = PROTO_SMB; 1223da6c28aaSamw else 1224da6c28aaSamw proto = PROTO_NFS; 122567331909Sdougm 122667331909Sdougm /* 1227da6c28aaSamw * Must be an valid sharing protocol 1228da6c28aaSamw * option string so init the libshare 1229da6c28aaSamw * in order to enable the parser and 1230da6c28aaSamw * then parse the options. We use the 1231da6c28aaSamw * control API since we don't care about 1232da6c28aaSamw * the current configuration and don't 123367331909Sdougm * want the overhead of loading it 123467331909Sdougm * until we actually do something. 123567331909Sdougm */ 123667331909Sdougm 1237fac3008cSeschrock if (zfs_init_libshare(hdl, 1238fac3008cSeschrock SA_INIT_CONTROL_API) != SA_OK) { 1239fac3008cSeschrock /* 1240fac3008cSeschrock * An error occurred so we can't do 1241fac3008cSeschrock * anything 1242fac3008cSeschrock */ 1243fac3008cSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1244fac3008cSeschrock "'%s' cannot be set: problem " 1245fac3008cSeschrock "in share initialization"), 1246fac3008cSeschrock propname); 1247fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1248fac3008cSeschrock errbuf); 1249fac3008cSeschrock goto error; 1250fac3008cSeschrock } 125167331909Sdougm 1252da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) { 1253fac3008cSeschrock /* 1254fac3008cSeschrock * There was an error in parsing so 1255fac3008cSeschrock * deal with it by issuing an error 1256fac3008cSeschrock * message and leaving after 1257fac3008cSeschrock * uninitializing the the libshare 1258fac3008cSeschrock * interface. 1259fac3008cSeschrock */ 1260fac3008cSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1261fac3008cSeschrock "'%s' cannot be set to invalid " 1262fac3008cSeschrock "options"), propname); 1263fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1264fac3008cSeschrock errbuf); 1265fac3008cSeschrock zfs_uninit_libshare(hdl); 1266fac3008cSeschrock goto error; 1267fac3008cSeschrock } 126867331909Sdougm zfs_uninit_libshare(hdl); 126967331909Sdougm } 127067331909Sdougm 1271da6c28aaSamw break; 127288f61deeSIgor Kozhukhov 1273da6c28aaSamw case ZFS_PROP_UTF8ONLY: 1274da6c28aaSamw chosen_utf = (int)intval; 1275da6c28aaSamw break; 127688f61deeSIgor Kozhukhov 1277da6c28aaSamw case ZFS_PROP_NORMALIZE: 1278da6c28aaSamw chosen_normal = (int)intval; 1279f3861e1aSahl break; 128088f61deeSIgor Kozhukhov 128188f61deeSIgor Kozhukhov default: 128288f61deeSIgor Kozhukhov break; 1283e9dbad6fSeschrock } 1284fa9e4066Sahrens 1285e9dbad6fSeschrock /* 1286e9dbad6fSeschrock * For changes to existing volumes, we have some additional 1287e9dbad6fSeschrock * checks to enforce. 1288e9dbad6fSeschrock */ 1289e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1290e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp, 1291e9dbad6fSeschrock ZFS_PROP_VOLSIZE); 1292e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp, 1293e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 1294e9dbad6fSeschrock char buf[64]; 1295e9dbad6fSeschrock 1296e9dbad6fSeschrock switch (prop) { 1297e9dbad6fSeschrock case ZFS_PROP_RESERVATION: 1298a9799022Sck case ZFS_PROP_REFRESERVATION: 1299e9dbad6fSeschrock if (intval > volsize) { 1300e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1301e9dbad6fSeschrock "'%s' is greater than current " 1302e9dbad6fSeschrock "volume size"), propname); 1303e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1304e9dbad6fSeschrock errbuf); 1305e9dbad6fSeschrock goto error; 1306e9dbad6fSeschrock } 1307e9dbad6fSeschrock break; 1308e9dbad6fSeschrock 1309e9dbad6fSeschrock case ZFS_PROP_VOLSIZE: 1310e9dbad6fSeschrock if (intval % blocksize != 0) { 1311e9dbad6fSeschrock zfs_nicenum(blocksize, buf, 1312e9dbad6fSeschrock sizeof (buf)); 1313e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1314e9dbad6fSeschrock "'%s' must be a multiple of " 1315e9dbad6fSeschrock "volume block size (%s)"), 1316e9dbad6fSeschrock propname, buf); 1317e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1318e9dbad6fSeschrock errbuf); 1319e9dbad6fSeschrock goto error; 1320e9dbad6fSeschrock } 1321e9dbad6fSeschrock 1322e9dbad6fSeschrock if (intval == 0) { 1323e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1324e9dbad6fSeschrock "'%s' cannot be zero"), 1325e9dbad6fSeschrock propname); 1326e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1327e9dbad6fSeschrock errbuf); 1328e9dbad6fSeschrock goto error; 1329e9dbad6fSeschrock } 1330f3861e1aSahl break; 133188f61deeSIgor Kozhukhov 133288f61deeSIgor Kozhukhov default: 133388f61deeSIgor Kozhukhov break; 1334fa9e4066Sahrens } 1335e9dbad6fSeschrock } 1336e9dbad6fSeschrock } 1337fa9e4066Sahrens 1338da6c28aaSamw /* 1339da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made, 1340da6c28aaSamw * enforce rejection of non-UTF8 names. 1341da6c28aaSamw * 1342da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names 1343da6c28aaSamw * was explicitly not chosen, it is an error. 1344da6c28aaSamw */ 1345de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) { 1346da6c28aaSamw if (nvlist_add_uint64(ret, 1347da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1348da6c28aaSamw (void) no_memory(hdl); 1349da6c28aaSamw goto error; 1350da6c28aaSamw } 1351de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 1352da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1353da6c28aaSamw "'%s' must be set 'on' if normalization chosen"), 1354da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1355da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1356da6c28aaSamw goto error; 1357da6c28aaSamw } 135836db6475SEric Taylor return (ret); 135936db6475SEric Taylor 136036db6475SEric Taylor error: 136136db6475SEric Taylor nvlist_free(ret); 136236db6475SEric Taylor return (NULL); 136336db6475SEric Taylor } 136436db6475SEric Taylor 136536db6475SEric Taylor int 136636db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 136736db6475SEric Taylor { 136836db6475SEric Taylor uint64_t old_volsize; 136936db6475SEric Taylor uint64_t new_volsize; 137036db6475SEric Taylor uint64_t old_reservation; 137136db6475SEric Taylor uint64_t new_reservation; 137236db6475SEric Taylor zfs_prop_t resv_prop; 1373c61ea566SGeorge Wilson nvlist_t *props; 1374da6c28aaSamw 1375e9dbad6fSeschrock /* 1376e9dbad6fSeschrock * If this is an existing volume, and someone is setting the volsize, 1377e9dbad6fSeschrock * make sure that it matches the reservation, or add it if necessary. 1378e9dbad6fSeschrock */ 137936db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 138036db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 138136db6475SEric Taylor return (-1); 138236db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop); 1383c61ea566SGeorge Wilson 1384c61ea566SGeorge Wilson props = fnvlist_alloc(); 1385c61ea566SGeorge Wilson fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1386c61ea566SGeorge Wilson zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1387c61ea566SGeorge Wilson 1388c61ea566SGeorge Wilson if ((zvol_volsize_to_reservation(old_volsize, props) != 1389c61ea566SGeorge Wilson old_reservation) || nvlist_exists(nvl, 1390c61ea566SGeorge Wilson zfs_prop_to_name(resv_prop))) { 1391c61ea566SGeorge Wilson fnvlist_free(props); 139236db6475SEric Taylor return (0); 1393fa9e4066Sahrens } 139436db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1395c61ea566SGeorge Wilson &new_volsize) != 0) { 1396c61ea566SGeorge Wilson fnvlist_free(props); 139736db6475SEric Taylor return (-1); 1398c61ea566SGeorge Wilson } 1399c61ea566SGeorge Wilson new_reservation = zvol_volsize_to_reservation(new_volsize, props); 1400c61ea566SGeorge Wilson fnvlist_free(props); 1401c61ea566SGeorge Wilson 140236db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 140336db6475SEric Taylor new_reservation) != 0) { 140436db6475SEric Taylor (void) no_memory(zhp->zfs_hdl); 140536db6475SEric Taylor return (-1); 140636db6475SEric Taylor } 140736db6475SEric Taylor return (1); 1408fa9e4066Sahrens } 1409fa9e4066Sahrens 141092241e0bSTom Erickson void 141192241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 141292241e0bSTom Erickson char *errbuf) 141392241e0bSTom Erickson { 141492241e0bSTom Erickson switch (err) { 141592241e0bSTom Erickson 141692241e0bSTom Erickson case ENOSPC: 141792241e0bSTom Erickson /* 141892241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates 141992241e0bSTom Erickson * something different; setting a quota or reservation 142092241e0bSTom Erickson * doesn't use any disk space. 142192241e0bSTom Erickson */ 142292241e0bSTom Erickson switch (prop) { 142392241e0bSTom Erickson case ZFS_PROP_QUOTA: 142492241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 142592241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 142692241e0bSTom Erickson "size is less than current used or " 142792241e0bSTom Erickson "reserved space")); 142892241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 142992241e0bSTom Erickson break; 143092241e0bSTom Erickson 143192241e0bSTom Erickson case ZFS_PROP_RESERVATION: 143292241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 143392241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 143492241e0bSTom Erickson "size is greater than available space")); 143592241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 143692241e0bSTom Erickson break; 143792241e0bSTom Erickson 143892241e0bSTom Erickson default: 143992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 144092241e0bSTom Erickson break; 144192241e0bSTom Erickson } 144292241e0bSTom Erickson break; 144392241e0bSTom Erickson 144492241e0bSTom Erickson case EBUSY: 144592241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf); 144692241e0bSTom Erickson break; 144792241e0bSTom Erickson 144892241e0bSTom Erickson case EROFS: 144992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 145092241e0bSTom Erickson break; 145192241e0bSTom Erickson 14526fdcb3d1SWill Andrews case E2BIG: 14536fdcb3d1SWill Andrews zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14546fdcb3d1SWill Andrews "property value too long")); 14556fdcb3d1SWill Andrews (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 14566fdcb3d1SWill Andrews break; 14576fdcb3d1SWill Andrews 145892241e0bSTom Erickson case ENOTSUP: 145992241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 146092241e0bSTom Erickson "pool and or dataset must be upgraded to set this " 146192241e0bSTom Erickson "property or value")); 146292241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 146392241e0bSTom Erickson break; 146492241e0bSTom Erickson 146592241e0bSTom Erickson case ERANGE: 1466b5152584SMatthew Ahrens if (prop == ZFS_PROP_COMPRESSION || 1467b5152584SMatthew Ahrens prop == ZFS_PROP_RECORDSIZE) { 146892241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 146992241e0bSTom Erickson "property setting is not allowed on " 147092241e0bSTom Erickson "bootable datasets")); 147192241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 147245818ee1SMatthew Ahrens } else if (prop == ZFS_PROP_CHECKSUM || 147345818ee1SMatthew Ahrens prop == ZFS_PROP_DEDUP) { 147445818ee1SMatthew Ahrens (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 147545818ee1SMatthew Ahrens "property setting is not allowed on " 147645818ee1SMatthew Ahrens "root pools")); 147745818ee1SMatthew Ahrens (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 147892241e0bSTom Erickson } else { 147992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 148092241e0bSTom Erickson } 148192241e0bSTom Erickson break; 148292241e0bSTom Erickson 1483ab003da8SJim Dunham case EINVAL: 1484ab003da8SJim Dunham if (prop == ZPROP_INVAL) { 1485ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1486ab003da8SJim Dunham } else { 1487ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf); 1488ab003da8SJim Dunham } 1489ab003da8SJim Dunham break; 1490ab003da8SJim Dunham 149192241e0bSTom Erickson case EOVERFLOW: 149292241e0bSTom Erickson /* 149392241e0bSTom Erickson * This platform can't address a volume this big. 149492241e0bSTom Erickson */ 149592241e0bSTom Erickson #ifdef _ILP32 149692241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) { 149792241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 149892241e0bSTom Erickson break; 149992241e0bSTom Erickson } 150092241e0bSTom Erickson #endif 150192241e0bSTom Erickson /* FALLTHROUGH */ 150292241e0bSTom Erickson default: 150392241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 150492241e0bSTom Erickson } 150592241e0bSTom Erickson } 150692241e0bSTom Erickson 1507fa9e4066Sahrens /* 1508fa9e4066Sahrens * Given a property name and value, set the property for the given dataset. 1509fa9e4066Sahrens */ 1510fa9e4066Sahrens int 1511e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1512fa9e4066Sahrens { 1513e9dbad6fSeschrock int ret = -1; 151499653d4eSeschrock char errbuf[1024]; 151599653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 151630925561SChris Williamson nvlist_t *nvl = NULL; 151799653d4eSeschrock 151899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 1519e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 152099653d4eSeschrock zhp->zfs_name); 152199653d4eSeschrock 1522e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1523e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) { 1524e9dbad6fSeschrock (void) no_memory(hdl); 1525e9dbad6fSeschrock goto error; 1526fa9e4066Sahrens } 1527fa9e4066Sahrens 152830925561SChris Williamson ret = zfs_prop_set_list(zhp, nvl); 1529990b4856Slling 153030925561SChris Williamson error: 1531e9dbad6fSeschrock nvlist_free(nvl); 153230925561SChris Williamson return (ret); 153330925561SChris Williamson } 1534e9dbad6fSeschrock 1535e9dbad6fSeschrock 153636db6475SEric Taylor 153730925561SChris Williamson /* 153830925561SChris Williamson * Given an nvlist of property names and values, set the properties for the 153930925561SChris Williamson * given dataset. 154030925561SChris Williamson */ 154130925561SChris Williamson int 154230925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props) 154330925561SChris Williamson { 154430925561SChris Williamson zfs_cmd_t zc = { 0 }; 154530925561SChris Williamson int ret = -1; 154630925561SChris Williamson prop_changelist_t **cls = NULL; 154730925561SChris Williamson int cl_idx; 154830925561SChris Williamson char errbuf[1024]; 154930925561SChris Williamson libzfs_handle_t *hdl = zhp->zfs_hdl; 155030925561SChris Williamson nvlist_t *nvl; 155130925561SChris Williamson int nvl_len; 1552f83b46baSPaul Dagnelie int added_resv = 0; 1553fa9e4066Sahrens 155430925561SChris Williamson (void) snprintf(errbuf, sizeof (errbuf), 155530925561SChris Williamson dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 155630925561SChris Williamson zhp->zfs_name); 155730925561SChris Williamson 155830925561SChris Williamson if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, 1559e9316f76SJoe Stein zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, 1560e9316f76SJoe Stein errbuf)) == NULL) 1561fa9e4066Sahrens goto error; 1562fa9e4066Sahrens 15630068372bSMark J Musante /* 156430925561SChris Williamson * We have to check for any extra properties which need to be added 156530925561SChris Williamson * before computing the length of the nvlist. 15660068372bSMark J Musante */ 156730925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 156830925561SChris Williamson elem != NULL; 156930925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 157030925561SChris Williamson if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && 157130925561SChris Williamson (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { 157230925561SChris Williamson goto error; 157330925561SChris Williamson } 15744445fffbSMatthew Ahrens } 157530925561SChris Williamson /* 157630925561SChris Williamson * Check how many properties we're setting and allocate an array to 157730925561SChris Williamson * store changelist pointers for postfix(). 157830925561SChris Williamson */ 157930925561SChris Williamson nvl_len = 0; 158030925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 158130925561SChris Williamson elem != NULL; 158230925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) 158330925561SChris Williamson nvl_len++; 158430925561SChris Williamson if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) 15850068372bSMark J Musante goto error; 1586fa9e4066Sahrens 158730925561SChris Williamson cl_idx = 0; 158830925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 158930925561SChris Williamson elem != NULL; 159030925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 159130925561SChris Williamson 159230925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 159330925561SChris Williamson 159430925561SChris Williamson assert(cl_idx < nvl_len); 159530925561SChris Williamson /* 159630925561SChris Williamson * We don't want to unmount & remount the dataset when changing 159730925561SChris Williamson * its canmount property to 'on' or 'noauto'. We only use 159830925561SChris Williamson * the changelist logic to unmount when setting canmount=off. 159930925561SChris Williamson */ 160030925561SChris Williamson if (!(prop == ZFS_PROP_CANMOUNT && 160130925561SChris Williamson fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) { 160230925561SChris Williamson cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); 160330925561SChris Williamson if (cls[cl_idx] == NULL) 160430925561SChris Williamson goto error; 160530925561SChris Williamson } 160630925561SChris Williamson 160730925561SChris Williamson if (prop == ZFS_PROP_MOUNTPOINT && 160830925561SChris Williamson changelist_haszonedchild(cls[cl_idx])) { 160930925561SChris Williamson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 161030925561SChris Williamson "child dataset with inherited mountpoint is used " 161130925561SChris Williamson "in a non-global zone")); 161230925561SChris Williamson ret = zfs_error(hdl, EZFS_ZONED, errbuf); 161330925561SChris Williamson goto error; 161430925561SChris Williamson } 161530925561SChris Williamson 161630925561SChris Williamson if (cls[cl_idx] != NULL && 161730925561SChris Williamson (ret = changelist_prefix(cls[cl_idx])) != 0) 161830925561SChris Williamson goto error; 161930925561SChris Williamson 162030925561SChris Williamson cl_idx++; 162130925561SChris Williamson } 162230925561SChris Williamson assert(cl_idx == nvl_len); 162330925561SChris Williamson 1624fa9e4066Sahrens /* 162530925561SChris Williamson * Execute the corresponding ioctl() to set this list of properties. 1626fa9e4066Sahrens */ 1627fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1628fa9e4066Sahrens 162930925561SChris Williamson if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 || 163030925561SChris Williamson (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0) 1631e9dbad6fSeschrock goto error; 1632e9dbad6fSeschrock 1633ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1634743a77edSAlan Wright 1635fa9e4066Sahrens if (ret != 0) { 163630925561SChris Williamson /* Get the list of unset properties back and report them. */ 163730925561SChris Williamson nvlist_t *errorprops = NULL; 163830925561SChris Williamson if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) 163930925561SChris Williamson goto error; 164030925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 164130925561SChris Williamson elem != NULL; 164230925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 164330925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 164430925561SChris Williamson zfs_setprop_error(hdl, prop, errno, errbuf); 164530925561SChris Williamson } 164630925561SChris Williamson nvlist_free(errorprops); 164730925561SChris Williamson 164836db6475SEric Taylor if (added_resv && errno == ENOSPC) { 164936db6475SEric Taylor /* clean up the volsize property we tried to set */ 165036db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp, 165136db6475SEric Taylor ZFS_PROP_VOLSIZE); 165236db6475SEric Taylor nvlist_free(nvl); 165330925561SChris Williamson nvl = NULL; 165436db6475SEric Taylor zcmd_free_nvlists(&zc); 165530925561SChris Williamson 165636db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 165736db6475SEric Taylor goto error; 165836db6475SEric Taylor if (nvlist_add_uint64(nvl, 165936db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE), 166036db6475SEric Taylor old_volsize) != 0) 166136db6475SEric Taylor goto error; 166236db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 166336db6475SEric Taylor goto error; 166436db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 166536db6475SEric Taylor } 1666fa9e4066Sahrens } else { 166730925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 166830925561SChris Williamson if (cls[cl_idx] != NULL) { 166930925561SChris Williamson int clp_err = changelist_postfix(cls[cl_idx]); 167030925561SChris Williamson if (clp_err != 0) 167130925561SChris Williamson ret = clp_err; 167230925561SChris Williamson } 167330925561SChris Williamson } 1674a227b7f4Shs 1675fa9e4066Sahrens /* 1676fa9e4066Sahrens * Refresh the statistics so the new property value 1677fa9e4066Sahrens * is reflected. 1678fa9e4066Sahrens */ 1679a227b7f4Shs if (ret == 0) 1680e9dbad6fSeschrock (void) get_stats(zhp); 1681fa9e4066Sahrens } 1682fa9e4066Sahrens 1683fa9e4066Sahrens error: 1684e9dbad6fSeschrock nvlist_free(nvl); 1685e9dbad6fSeschrock zcmd_free_nvlists(&zc); 168630925561SChris Williamson if (cls != NULL) { 168730925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 168830925561SChris Williamson if (cls[cl_idx] != NULL) 168930925561SChris Williamson changelist_free(cls[cl_idx]); 169030925561SChris Williamson } 169130925561SChris Williamson free(cls); 169230925561SChris Williamson } 1693fa9e4066Sahrens return (ret); 1694fa9e4066Sahrens } 1695fa9e4066Sahrens 1696fa9e4066Sahrens /* 169792241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received 169892241e0bSTom Erickson * is TRUE, revert to the received value, if any. 1699fa9e4066Sahrens */ 1700fa9e4066Sahrens int 170192241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1702fa9e4066Sahrens { 1703fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1704fa9e4066Sahrens int ret; 1705fa9e4066Sahrens prop_changelist_t *cl; 170699653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 170799653d4eSeschrock char errbuf[1024]; 1708e9dbad6fSeschrock zfs_prop_t prop; 170999653d4eSeschrock 171099653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 171199653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1712fa9e4066Sahrens 171392241e0bSTom Erickson zc.zc_cookie = received; 1714990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1715e9dbad6fSeschrock /* 1716e9dbad6fSeschrock * For user properties, the amount of work we have to do is very 1717e9dbad6fSeschrock * small, so just do it here. 1718e9dbad6fSeschrock */ 1719e9dbad6fSeschrock if (!zfs_prop_user(propname)) { 1720e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1721e9dbad6fSeschrock "invalid property")); 1722e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1723e9dbad6fSeschrock } 1724e9dbad6fSeschrock 1725e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1726e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1727e9dbad6fSeschrock 1728e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1729e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1730e9dbad6fSeschrock 1731e9dbad6fSeschrock return (0); 1732e9dbad6fSeschrock } 1733e9dbad6fSeschrock 1734fa9e4066Sahrens /* 1735fa9e4066Sahrens * Verify that this property is inheritable. 1736fa9e4066Sahrens */ 173799653d4eSeschrock if (zfs_prop_readonly(prop)) 173899653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1739fa9e4066Sahrens 174092241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received) 174199653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1742fa9e4066Sahrens 1743fa9e4066Sahrens /* 1744fa9e4066Sahrens * Check to see if the value applies to this type 1745fa9e4066Sahrens */ 174699653d4eSeschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 174799653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1748fa9e4066Sahrens 1749bf7c2d40Srm /* 175036db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations. 1751bf7c2d40Srm */ 1752bf7c2d40Srm propname = zfs_prop_to_name(prop); 1753fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1754e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1755fa9e4066Sahrens 1756fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1757fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 175899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 175999653d4eSeschrock "dataset is used in a non-global zone")); 176099653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1761fa9e4066Sahrens } 1762fa9e4066Sahrens 1763fa9e4066Sahrens /* 1764fa9e4066Sahrens * Determine datasets which will be affected by this change, if any. 1765fa9e4066Sahrens */ 17660069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1767fa9e4066Sahrens return (-1); 1768fa9e4066Sahrens 1769fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 177099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 177199653d4eSeschrock "child dataset with inherited mountpoint is used " 177299653d4eSeschrock "in a non-global zone")); 177399653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1774fa9e4066Sahrens goto error; 1775fa9e4066Sahrens } 1776fa9e4066Sahrens 1777fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 1778fa9e4066Sahrens goto error; 1779fa9e4066Sahrens 1780e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 178199653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1782fa9e4066Sahrens } else { 1783fa9e4066Sahrens 1784efc555ebSnd if ((ret = changelist_postfix(cl)) != 0) 1785fa9e4066Sahrens goto error; 1786fa9e4066Sahrens 1787fa9e4066Sahrens /* 1788fa9e4066Sahrens * Refresh the statistics so the new property is reflected. 1789fa9e4066Sahrens */ 1790fa9e4066Sahrens (void) get_stats(zhp); 1791fa9e4066Sahrens } 1792fa9e4066Sahrens 1793fa9e4066Sahrens error: 1794fa9e4066Sahrens changelist_free(cl); 1795fa9e4066Sahrens return (ret); 1796fa9e4066Sahrens } 1797fa9e4066Sahrens 17987f7322feSeschrock /* 17997f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions 18007f7322feSeschrock * extract them appropriately. 18017f7322feSeschrock */ 18027f7322feSeschrock static uint64_t 18037f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18047f7322feSeschrock { 18057f7322feSeschrock nvlist_t *nv; 18067f7322feSeschrock uint64_t value; 18077f7322feSeschrock 1808a2eea2e1Sahrens *source = NULL; 18097f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18107f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1811990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1812990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18137f7322feSeschrock } else { 18142e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 18152e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 18167f7322feSeschrock value = zfs_prop_default_numeric(prop); 18177f7322feSeschrock *source = ""; 18187f7322feSeschrock } 18197f7322feSeschrock 18207f7322feSeschrock return (value); 18217f7322feSeschrock } 18227f7322feSeschrock 18239c3fd121SMatthew Ahrens static const char * 18247f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18257f7322feSeschrock { 18267f7322feSeschrock nvlist_t *nv; 18279c3fd121SMatthew Ahrens const char *value; 18287f7322feSeschrock 1829a2eea2e1Sahrens *source = NULL; 18307f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18317f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 18329c3fd121SMatthew Ahrens value = fnvlist_lookup_string(nv, ZPROP_VALUE); 1833990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18347f7322feSeschrock } else { 18352e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 18362e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 18379c3fd121SMatthew Ahrens value = zfs_prop_default_string(prop); 18387f7322feSeschrock *source = ""; 18397f7322feSeschrock } 18407f7322feSeschrock 18417f7322feSeschrock return (value); 18427f7322feSeschrock } 18437f7322feSeschrock 184492241e0bSTom Erickson static boolean_t 184592241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp) 184692241e0bSTom Erickson { 184792241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props); 184892241e0bSTom Erickson } 184992241e0bSTom Erickson 185092241e0bSTom Erickson static void 185192241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 185292241e0bSTom Erickson { 185392241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 185492241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props; 185592241e0bSTom Erickson } 185692241e0bSTom Erickson 185792241e0bSTom Erickson static void 185892241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 185992241e0bSTom Erickson { 186092241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 186192241e0bSTom Erickson *cookie = 0; 186292241e0bSTom Erickson } 186392241e0bSTom Erickson 1864fa9e4066Sahrens /* 1865fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1866fa9e4066Sahrens * zfs_prop_get_int() are built using this interface. 1867fa9e4066Sahrens * 1868fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1869fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1870fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark 1871fa9e4066Sahrens * the source "temporary". 1872fa9e4066Sahrens */ 187399653d4eSeschrock static int 1874990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 187599653d4eSeschrock char **source, uint64_t *val) 1876fa9e4066Sahrens { 1877bd00f61bSrm zfs_cmd_t zc = { 0 }; 187896510749Stimh nvlist_t *zplprops = NULL; 1879fa9e4066Sahrens struct mnttab mnt; 18803ccfa83cSahrens char *mntopt_on = NULL; 18813ccfa83cSahrens char *mntopt_off = NULL; 188292241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 1883fa9e4066Sahrens 1884fa9e4066Sahrens *source = NULL; 1885fa9e4066Sahrens 18863ccfa83cSahrens switch (prop) { 18873ccfa83cSahrens case ZFS_PROP_ATIME: 18883ccfa83cSahrens mntopt_on = MNTOPT_ATIME; 18893ccfa83cSahrens mntopt_off = MNTOPT_NOATIME; 18903ccfa83cSahrens break; 18913ccfa83cSahrens 18923ccfa83cSahrens case ZFS_PROP_DEVICES: 18933ccfa83cSahrens mntopt_on = MNTOPT_DEVICES; 18943ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES; 18953ccfa83cSahrens break; 18963ccfa83cSahrens 18973ccfa83cSahrens case ZFS_PROP_EXEC: 18983ccfa83cSahrens mntopt_on = MNTOPT_EXEC; 18993ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC; 19003ccfa83cSahrens break; 19013ccfa83cSahrens 19023ccfa83cSahrens case ZFS_PROP_READONLY: 19033ccfa83cSahrens mntopt_on = MNTOPT_RO; 19043ccfa83cSahrens mntopt_off = MNTOPT_RW; 19053ccfa83cSahrens break; 19063ccfa83cSahrens 19073ccfa83cSahrens case ZFS_PROP_SETUID: 19083ccfa83cSahrens mntopt_on = MNTOPT_SETUID; 19093ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID; 19103ccfa83cSahrens break; 19113ccfa83cSahrens 19123ccfa83cSahrens case ZFS_PROP_XATTR: 19133ccfa83cSahrens mntopt_on = MNTOPT_XATTR; 19143ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR; 19153ccfa83cSahrens break; 1916da6c28aaSamw 1917da6c28aaSamw case ZFS_PROP_NBMAND: 1918da6c28aaSamw mntopt_on = MNTOPT_NBMAND; 1919da6c28aaSamw mntopt_off = MNTOPT_NONBMAND; 1920da6c28aaSamw break; 192188f61deeSIgor Kozhukhov 192288f61deeSIgor Kozhukhov default: 192388f61deeSIgor Kozhukhov break; 19243ccfa83cSahrens } 19253ccfa83cSahrens 19263bb79becSeschrock /* 19273bb79becSeschrock * Because looking up the mount options is potentially expensive 19283bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until 19293bb79becSeschrock * we're looking up a property which requires its presence. 19303bb79becSeschrock */ 19313bb79becSeschrock if (!zhp->zfs_mntcheck && 19323ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 1933ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 1934ebedde84SEric Taylor struct mnttab entry; 19353bb79becSeschrock 1936ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 1937ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl, 19383ccfa83cSahrens entry.mnt_mntopts); 19393ccfa83cSahrens if (zhp->zfs_mntopts == NULL) 19403ccfa83cSahrens return (-1); 19413ccfa83cSahrens } 19423bb79becSeschrock 19433bb79becSeschrock zhp->zfs_mntcheck = B_TRUE; 19443bb79becSeschrock } 19453bb79becSeschrock 1946fa9e4066Sahrens if (zhp->zfs_mntopts == NULL) 1947fa9e4066Sahrens mnt.mnt_mntopts = ""; 1948fa9e4066Sahrens else 1949fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1950fa9e4066Sahrens 1951fa9e4066Sahrens switch (prop) { 1952fa9e4066Sahrens case ZFS_PROP_ATIME: 1953fa9e4066Sahrens case ZFS_PROP_DEVICES: 1954fa9e4066Sahrens case ZFS_PROP_EXEC: 19553ccfa83cSahrens case ZFS_PROP_READONLY: 19563ccfa83cSahrens case ZFS_PROP_SETUID: 19573ccfa83cSahrens case ZFS_PROP_XATTR: 1958da6c28aaSamw case ZFS_PROP_NBMAND: 195999653d4eSeschrock *val = getprop_uint64(zhp, prop, source); 1960fa9e4066Sahrens 196192241e0bSTom Erickson if (received) 196292241e0bSTom Erickson break; 196392241e0bSTom Erickson 19643ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 196599653d4eSeschrock *val = B_TRUE; 1966fa9e4066Sahrens if (src) 1967990b4856Slling *src = ZPROP_SRC_TEMPORARY; 19683ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 196999653d4eSeschrock *val = B_FALSE; 1970fa9e4066Sahrens if (src) 1971990b4856Slling *src = ZPROP_SRC_TEMPORARY; 1972fa9e4066Sahrens } 197399653d4eSeschrock break; 1974fa9e4066Sahrens 19753ccfa83cSahrens case ZFS_PROP_CANMOUNT: 1976d41c4376SMark J Musante case ZFS_PROP_VOLSIZE: 1977fa9e4066Sahrens case ZFS_PROP_QUOTA: 1978a9799022Sck case ZFS_PROP_REFQUOTA: 1979fa9e4066Sahrens case ZFS_PROP_RESERVATION: 1980a9799022Sck case ZFS_PROP_REFRESERVATION: 1981a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 1982a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 1983a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 1984a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 1985a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source); 198692241e0bSTom Erickson 198792241e0bSTom Erickson if (*source == NULL) { 198892241e0bSTom Erickson /* not default, must be local */ 1989fa9e4066Sahrens *source = zhp->zfs_name; 199092241e0bSTom Erickson } 199199653d4eSeschrock break; 1992fa9e4066Sahrens 1993fa9e4066Sahrens case ZFS_PROP_MOUNTED: 199499653d4eSeschrock *val = (zhp->zfs_mntopts != NULL); 199599653d4eSeschrock break; 1996fa9e4066Sahrens 199739c23413Seschrock case ZFS_PROP_NUMCLONES: 199839c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 199939c23413Seschrock break; 200039c23413Seschrock 2001bd00f61bSrm case ZFS_PROP_VERSION: 2002de8267e0Stimh case ZFS_PROP_NORMALIZE: 2003de8267e0Stimh case ZFS_PROP_UTF8ONLY: 2004de8267e0Stimh case ZFS_PROP_CASE: 2005de8267e0Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 2006de8267e0Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 20073d7934e1Srm return (-1); 2008bd00f61bSrm (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2009de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2010de8267e0Stimh zcmd_free_nvlists(&zc); 2011f4b94bdeSMatthew Ahrens return (-1); 2012bd00f61bSrm } 2013de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2014de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2015de8267e0Stimh val) != 0) { 2016de8267e0Stimh zcmd_free_nvlists(&zc); 2017f4b94bdeSMatthew Ahrens return (-1); 2018de8267e0Stimh } 2019aab83bb8SJosef 'Jeff' Sipek nvlist_free(zplprops); 2020de8267e0Stimh zcmd_free_nvlists(&zc); 2021bd00f61bSrm break; 2022bd00f61bSrm 2023ca48f36fSKeith M Wesolowski case ZFS_PROP_INCONSISTENT: 2024ca48f36fSKeith M Wesolowski *val = zhp->zfs_dmustats.dds_inconsistent; 2025ca48f36fSKeith M Wesolowski break; 2026ca48f36fSKeith M Wesolowski 2027fa9e4066Sahrens default: 2028e7437265Sahrens switch (zfs_prop_get_type(prop)) { 202991ebeef5Sahrens case PROP_TYPE_NUMBER: 203091ebeef5Sahrens case PROP_TYPE_INDEX: 2031e7437265Sahrens *val = getprop_uint64(zhp, prop, source); 203274e7dc98SMatthew Ahrens /* 203314843421SMatthew Ahrens * If we tried to use a default value for a 203474e7dc98SMatthew Ahrens * readonly property, it means that it was not 203531f572c2STom Erickson * present. 203674e7dc98SMatthew Ahrens */ 203774e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) && 203831f572c2STom Erickson *source != NULL && (*source)[0] == '\0') { 203931f572c2STom Erickson *source = NULL; 204074e7dc98SMatthew Ahrens } 2041e7437265Sahrens break; 2042e7437265Sahrens 204391ebeef5Sahrens case PROP_TYPE_STRING: 2044e7437265Sahrens default: 2045e7437265Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 2046e7437265Sahrens "cannot get non-numeric property")); 2047e7437265Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 2048e7437265Sahrens dgettext(TEXT_DOMAIN, "internal error"))); 2049e7437265Sahrens } 2050fa9e4066Sahrens } 2051fa9e4066Sahrens 2052fa9e4066Sahrens return (0); 2053fa9e4066Sahrens } 2054fa9e4066Sahrens 2055fa9e4066Sahrens /* 2056fa9e4066Sahrens * Calculate the source type, given the raw source string. 2057fa9e4066Sahrens */ 2058fa9e4066Sahrens static void 2059990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2060fa9e4066Sahrens char *statbuf, size_t statlen) 2061fa9e4066Sahrens { 2062990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2063fa9e4066Sahrens return; 2064fa9e4066Sahrens 2065fa9e4066Sahrens if (source == NULL) { 2066990b4856Slling *srctype = ZPROP_SRC_NONE; 2067fa9e4066Sahrens } else if (source[0] == '\0') { 2068990b4856Slling *srctype = ZPROP_SRC_DEFAULT; 206992241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 207092241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED; 2071fa9e4066Sahrens } else { 2072fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2073990b4856Slling *srctype = ZPROP_SRC_LOCAL; 2074fa9e4066Sahrens } else { 2075fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen); 2076990b4856Slling *srctype = ZPROP_SRC_INHERITED; 2077fa9e4066Sahrens } 2078fa9e4066Sahrens } 2079fa9e4066Sahrens 2080fa9e4066Sahrens } 2081fa9e4066Sahrens 208292241e0bSTom Erickson int 208392241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 208492241e0bSTom Erickson size_t proplen, boolean_t literal) 208592241e0bSTom Erickson { 208692241e0bSTom Erickson zfs_prop_t prop; 208792241e0bSTom Erickson int err = 0; 208892241e0bSTom Erickson 208992241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 209092241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 209192241e0bSTom Erickson return (-1); 209292241e0bSTom Erickson 209392241e0bSTom Erickson prop = zfs_name_to_prop(propname); 209492241e0bSTom Erickson 209592241e0bSTom Erickson if (prop != ZPROP_INVAL) { 209692241e0bSTom Erickson uint64_t cookie; 209792241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 209892241e0bSTom Erickson return (-1); 209992241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie); 210092241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen, 210192241e0bSTom Erickson NULL, NULL, 0, literal); 210292241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie); 210392241e0bSTom Erickson } else { 210492241e0bSTom Erickson nvlist_t *propval; 210592241e0bSTom Erickson char *recvdval; 210692241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 210792241e0bSTom Erickson propname, &propval) != 0) 210892241e0bSTom Erickson return (-1); 210992241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE, 211092241e0bSTom Erickson &recvdval) == 0); 211192241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen); 211292241e0bSTom Erickson } 211392241e0bSTom Erickson 211492241e0bSTom Erickson return (err == 0 ? 0 : -1); 211592241e0bSTom Erickson } 211692241e0bSTom Erickson 211719b94df9SMatthew Ahrens static int 211819b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 211919b94df9SMatthew Ahrens { 212019b94df9SMatthew Ahrens nvlist_t *value; 212119b94df9SMatthew Ahrens nvpair_t *pair; 212219b94df9SMatthew Ahrens 212319b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp); 212419b94df9SMatthew Ahrens if (value == NULL) 212519b94df9SMatthew Ahrens return (-1); 212619b94df9SMatthew Ahrens 212719b94df9SMatthew Ahrens propbuf[0] = '\0'; 212819b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 212919b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) { 213019b94df9SMatthew Ahrens if (propbuf[0] != '\0') 213119b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen); 213219b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen); 213319b94df9SMatthew Ahrens } 213419b94df9SMatthew Ahrens 213519b94df9SMatthew Ahrens return (0); 213619b94df9SMatthew Ahrens } 213719b94df9SMatthew Ahrens 213819b94df9SMatthew Ahrens struct get_clones_arg { 213919b94df9SMatthew Ahrens uint64_t numclones; 214019b94df9SMatthew Ahrens nvlist_t *value; 214119b94df9SMatthew Ahrens const char *origin; 2142*9adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN]; 214319b94df9SMatthew Ahrens }; 214419b94df9SMatthew Ahrens 214519b94df9SMatthew Ahrens int 214619b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg) 214719b94df9SMatthew Ahrens { 214819b94df9SMatthew Ahrens struct get_clones_arg *gca = arg; 214919b94df9SMatthew Ahrens 215019b94df9SMatthew Ahrens if (gca->numclones == 0) { 215119b94df9SMatthew Ahrens zfs_close(zhp); 215219b94df9SMatthew Ahrens return (0); 215319b94df9SMatthew Ahrens } 215419b94df9SMatthew Ahrens 215519b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 215619b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0) 215719b94df9SMatthew Ahrens goto out; 215819b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) { 21593b2aab18SMatthew Ahrens fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 216019b94df9SMatthew Ahrens gca->numclones--; 216119b94df9SMatthew Ahrens } 216219b94df9SMatthew Ahrens 216319b94df9SMatthew Ahrens out: 216419b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca); 216519b94df9SMatthew Ahrens zfs_close(zhp); 216619b94df9SMatthew Ahrens return (0); 216719b94df9SMatthew Ahrens } 216819b94df9SMatthew Ahrens 216919b94df9SMatthew Ahrens nvlist_t * 217019b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp) 217119b94df9SMatthew Ahrens { 217219b94df9SMatthew Ahrens nvlist_t *nv, *value; 217319b94df9SMatthew Ahrens 217419b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props, 217519b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 217619b94df9SMatthew Ahrens struct get_clones_arg gca; 217719b94df9SMatthew Ahrens 217819b94df9SMatthew Ahrens /* 217919b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able 218019b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating. 218119b94df9SMatthew Ahrens */ 218219b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 218319b94df9SMatthew Ahrens return (NULL); 218419b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 218519b94df9SMatthew Ahrens return (NULL); 218619b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 218719b94df9SMatthew Ahrens nvlist_free(nv); 218819b94df9SMatthew Ahrens return (NULL); 218919b94df9SMatthew Ahrens } 219019b94df9SMatthew Ahrens 219119b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 219219b94df9SMatthew Ahrens gca.value = value; 219319b94df9SMatthew Ahrens gca.origin = zhp->zfs_name; 219419b94df9SMatthew Ahrens 219519b94df9SMatthew Ahrens if (gca.numclones != 0) { 219619b94df9SMatthew Ahrens zfs_handle_t *root; 2197*9adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 219819b94df9SMatthew Ahrens char *cp = pool; 219919b94df9SMatthew Ahrens 220019b94df9SMatthew Ahrens /* get the pool name */ 220119b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 220219b94df9SMatthew Ahrens (void) strsep(&cp, "/@"); 220319b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool, 220419b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM); 220519b94df9SMatthew Ahrens 220619b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca); 220719b94df9SMatthew Ahrens } 220819b94df9SMatthew Ahrens 220919b94df9SMatthew Ahrens if (gca.numclones != 0 || 221019b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 221119b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props, 221219b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 221319b94df9SMatthew Ahrens nvlist_free(nv); 221419b94df9SMatthew Ahrens nvlist_free(value); 221519b94df9SMatthew Ahrens return (NULL); 221619b94df9SMatthew Ahrens } 221719b94df9SMatthew Ahrens nvlist_free(nv); 221819b94df9SMatthew Ahrens nvlist_free(value); 221919b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, 222019b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); 222119b94df9SMatthew Ahrens } 222219b94df9SMatthew Ahrens 222319b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); 222419b94df9SMatthew Ahrens 222519b94df9SMatthew Ahrens return (value); 222619b94df9SMatthew Ahrens } 222719b94df9SMatthew Ahrens 2228fa9e4066Sahrens /* 2229fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2230fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2231fa9e4066Sahrens * human-readable form. 2232fa9e4066Sahrens * 2233fa9e4066Sahrens * Returns 0 on success, or -1 on error. 2234fa9e4066Sahrens */ 2235fa9e4066Sahrens int 2236fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2237990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2238fa9e4066Sahrens { 2239fa9e4066Sahrens char *source = NULL; 2240fa9e4066Sahrens uint64_t val; 22419c3fd121SMatthew Ahrens const char *str; 2242e9dbad6fSeschrock const char *strval; 224392241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2244fa9e4066Sahrens 2245fa9e4066Sahrens /* 2246fa9e4066Sahrens * Check to see if this property applies to our object 2247fa9e4066Sahrens */ 2248fa9e4066Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2249fa9e4066Sahrens return (-1); 2250fa9e4066Sahrens 225192241e0bSTom Erickson if (received && zfs_prop_readonly(prop)) 225292241e0bSTom Erickson return (-1); 225392241e0bSTom Erickson 2254fa9e4066Sahrens if (src) 2255990b4856Slling *src = ZPROP_SRC_NONE; 2256fa9e4066Sahrens 2257fa9e4066Sahrens switch (prop) { 2258fa9e4066Sahrens case ZFS_PROP_CREATION: 2259fa9e4066Sahrens /* 2260fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert 2261fa9e4066Sahrens * this into a string unless 'literal' is specified. 2262fa9e4066Sahrens */ 2263fa9e4066Sahrens { 2264a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source); 2265a2eea2e1Sahrens time_t time = (time_t)val; 2266fa9e4066Sahrens struct tm t; 2267fa9e4066Sahrens 2268fa9e4066Sahrens if (literal || 2269fa9e4066Sahrens localtime_r(&time, &t) == NULL || 2270fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2271fa9e4066Sahrens &t) == 0) 2272a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2273fa9e4066Sahrens } 2274fa9e4066Sahrens break; 2275fa9e4066Sahrens 2276fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT: 2277fa9e4066Sahrens /* 2278fa9e4066Sahrens * Getting the precise mountpoint can be tricky. 2279fa9e4066Sahrens * 2280fa9e4066Sahrens * - for 'none' or 'legacy', return those values. 2281fa9e4066Sahrens * - for inherited mountpoints, we want to take everything 2282fa9e4066Sahrens * after our ancestor and append it to the inherited value. 2283fa9e4066Sahrens * 2284fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that 2285fa9e4066Sahrens * root to any values we return. 2286fa9e4066Sahrens */ 228729ab75c9Srm 22887f7322feSeschrock str = getprop_string(zhp, prop, &source); 2289fa9e4066Sahrens 2290b21718f0Sgw if (str[0] == '/') { 229129ab75c9Srm char buf[MAXPATHLEN]; 229229ab75c9Srm char *root = buf; 2293a79992aaSTom Erickson const char *relpath; 2294fa9e4066Sahrens 2295a79992aaSTom Erickson /* 2296a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset 2297a79992aaSTom Erickson * with a received value, the source will be the path of 2298a79992aaSTom Erickson * the dataset we inherit from. If source is 2299a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2300a79992aaSTom Erickson * inherited. 2301a79992aaSTom Erickson */ 2302a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2303a79992aaSTom Erickson relpath = ""; 2304a79992aaSTom Erickson } else { 2305a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source); 2306a79992aaSTom Erickson if (relpath[0] == '/') 2307a79992aaSTom Erickson relpath++; 2308a79992aaSTom Erickson } 2309b21718f0Sgw 231029ab75c9Srm if ((zpool_get_prop(zhp->zpool_hdl, 2311c58b3526SAdam Stevko ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2312c58b3526SAdam Stevko B_FALSE)) || (strcmp(root, "-") == 0)) 231329ab75c9Srm root[0] = '\0'; 2314b21718f0Sgw /* 2315b21718f0Sgw * Special case an alternate root of '/'. This will 2316b21718f0Sgw * avoid having multiple leading slashes in the 2317b21718f0Sgw * mountpoint path. 2318b21718f0Sgw */ 2319b21718f0Sgw if (strcmp(root, "/") == 0) 2320b21718f0Sgw root++; 2321b21718f0Sgw 2322b21718f0Sgw /* 2323b21718f0Sgw * If the mountpoint is '/' then skip over this 2324b21718f0Sgw * if we are obtaining either an alternate root or 2325b21718f0Sgw * an inherited mountpoint. 2326b21718f0Sgw */ 2327b21718f0Sgw if (str[1] == '\0' && (root[0] != '\0' || 2328b21718f0Sgw relpath[0] != '\0')) 23297f7322feSeschrock str++; 2330fa9e4066Sahrens 2331fa9e4066Sahrens if (relpath[0] == '\0') 2332fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s", 23337f7322feSeschrock root, str); 2334fa9e4066Sahrens else 2335fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 23367f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/", 2337fa9e4066Sahrens relpath); 2338fa9e4066Sahrens } else { 2339fa9e4066Sahrens /* 'legacy' or 'none' */ 23407f7322feSeschrock (void) strlcpy(propbuf, str, proplen); 2341fa9e4066Sahrens } 2342fa9e4066Sahrens 2343fa9e4066Sahrens break; 2344fa9e4066Sahrens 2345fa9e4066Sahrens case ZFS_PROP_ORIGIN: 23469c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 23479c3fd121SMatthew Ahrens if (str == NULL) 2348fa9e4066Sahrens return (-1); 23499c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2350fa9e4066Sahrens break; 2351fa9e4066Sahrens 235219b94df9SMatthew Ahrens case ZFS_PROP_CLONES: 235319b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0) 235419b94df9SMatthew Ahrens return (-1); 235519b94df9SMatthew Ahrens break; 235619b94df9SMatthew Ahrens 2357fa9e4066Sahrens case ZFS_PROP_QUOTA: 2358a9799022Sck case ZFS_PROP_REFQUOTA: 2359fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2360a9799022Sck case ZFS_PROP_REFRESERVATION: 2361a9799022Sck 236299653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 236399653d4eSeschrock return (-1); 2364fa9e4066Sahrens 2365fa9e4066Sahrens /* 2366fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none' 2367fa9e4066Sahrens * (unless literal is set), and indicate that it's the default 2368fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate 2369fa9e4066Sahrens * that its set locally. 2370fa9e4066Sahrens */ 2371fa9e4066Sahrens if (val == 0) { 2372fa9e4066Sahrens if (literal) 2373fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen); 2374fa9e4066Sahrens else 2375fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen); 2376fa9e4066Sahrens } else { 2377fa9e4066Sahrens if (literal) 23785ad82045Snd (void) snprintf(propbuf, proplen, "%llu", 2379b1b8ab34Slling (u_longlong_t)val); 2380fa9e4066Sahrens else 2381fa9e4066Sahrens zfs_nicenum(val, propbuf, proplen); 2382fa9e4066Sahrens } 2383fa9e4066Sahrens break; 2384fa9e4066Sahrens 2385a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2386a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2387a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2388a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2389a2afb611SJerry Jelinek 2390a2afb611SJerry Jelinek if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2391a2afb611SJerry Jelinek return (-1); 2392a2afb611SJerry Jelinek 2393a2afb611SJerry Jelinek /* 2394a2afb611SJerry Jelinek * If limit is UINT64_MAX, we translate this into 'none' (unless 2395a2afb611SJerry Jelinek * literal is set), and indicate that it's the default value. 2396a2afb611SJerry Jelinek * Otherwise, we print the number nicely and indicate that it's 2397a2afb611SJerry Jelinek * set locally. 2398a2afb611SJerry Jelinek */ 2399a2afb611SJerry Jelinek if (literal) { 2400a2afb611SJerry Jelinek (void) snprintf(propbuf, proplen, "%llu", 2401a2afb611SJerry Jelinek (u_longlong_t)val); 2402a2afb611SJerry Jelinek } else if (val == UINT64_MAX) { 2403a2afb611SJerry Jelinek (void) strlcpy(propbuf, "none", proplen); 2404a2afb611SJerry Jelinek } else { 2405a2afb611SJerry Jelinek zfs_nicenum(val, propbuf, proplen); 2406a2afb611SJerry Jelinek } 2407a2afb611SJerry Jelinek break; 2408a2afb611SJerry Jelinek 2409187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO: 2410fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO: 241199653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 241299653d4eSeschrock return (-1); 2413b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux", 2414b24ab676SJeff Bonwick (u_longlong_t)(val / 100), 2415b24ab676SJeff Bonwick (u_longlong_t)(val % 100)); 2416fa9e4066Sahrens break; 2417fa9e4066Sahrens 2418fa9e4066Sahrens case ZFS_PROP_TYPE: 2419fa9e4066Sahrens switch (zhp->zfs_type) { 2420fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 2421fa9e4066Sahrens str = "filesystem"; 2422fa9e4066Sahrens break; 2423fa9e4066Sahrens case ZFS_TYPE_VOLUME: 2424fa9e4066Sahrens str = "volume"; 2425fa9e4066Sahrens break; 2426fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 2427fa9e4066Sahrens str = "snapshot"; 2428fa9e4066Sahrens break; 242978f17100SMatthew Ahrens case ZFS_TYPE_BOOKMARK: 243078f17100SMatthew Ahrens str = "bookmark"; 243178f17100SMatthew Ahrens break; 2432fa9e4066Sahrens default: 243399653d4eSeschrock abort(); 2434fa9e4066Sahrens } 2435fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2436fa9e4066Sahrens break; 2437fa9e4066Sahrens 2438fa9e4066Sahrens case ZFS_PROP_MOUNTED: 2439fa9e4066Sahrens /* 2440fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described 2441fa9e4066Sahrens * whether the filesystem is currently mounted. Even though 2442fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off" 2443fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no". 2444fa9e4066Sahrens */ 244599653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 244699653d4eSeschrock src, &source, &val) != 0) 244799653d4eSeschrock return (-1); 244899653d4eSeschrock if (val) 2449fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen); 2450fa9e4066Sahrens else 2451fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen); 2452fa9e4066Sahrens break; 2453fa9e4066Sahrens 2454fa9e4066Sahrens case ZFS_PROP_NAME: 2455fa9e4066Sahrens /* 2456fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the 2457fa9e4066Sahrens * dataset name. It is presented as a real property to simplify 2458fa9e4066Sahrens * consumers. 2459fa9e4066Sahrens */ 2460fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2461fa9e4066Sahrens break; 2462fa9e4066Sahrens 24634201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 24644201a95eSRic Aleshire { 24654201a95eSRic Aleshire m_label_t *new_sl = NULL; 24664201a95eSRic Aleshire char *ascii = NULL; /* human readable label */ 24674201a95eSRic Aleshire 24684201a95eSRic Aleshire (void) strlcpy(propbuf, 24694201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen); 24704201a95eSRic Aleshire 24714201a95eSRic Aleshire if (literal || (strcasecmp(propbuf, 24724201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0)) 24734201a95eSRic Aleshire break; 24744201a95eSRic Aleshire 24754201a95eSRic Aleshire /* 24764201a95eSRic Aleshire * Try to translate the internal hex string to 24774201a95eSRic Aleshire * human-readable output. If there are any 24784201a95eSRic Aleshire * problems just use the hex string. 24794201a95eSRic Aleshire */ 24804201a95eSRic Aleshire 24814201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL, 24824201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) { 24834201a95eSRic Aleshire m_label_free(new_sl); 24844201a95eSRic Aleshire break; 24854201a95eSRic Aleshire } 24864201a95eSRic Aleshire 24874201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL, 24884201a95eSRic Aleshire DEF_NAMES) != 0) { 24894201a95eSRic Aleshire if (ascii) 24904201a95eSRic Aleshire free(ascii); 24914201a95eSRic Aleshire m_label_free(new_sl); 24924201a95eSRic Aleshire break; 24934201a95eSRic Aleshire } 24944201a95eSRic Aleshire m_label_free(new_sl); 24954201a95eSRic Aleshire 24964201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen); 24974201a95eSRic Aleshire free(ascii); 24984201a95eSRic Aleshire } 24994201a95eSRic Aleshire break; 25004201a95eSRic Aleshire 2501f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID: 2502f0f3ef5aSGarrett D'Amore /* 2503f0f3ef5aSGarrett D'Amore * GUIDs are stored as numbers, but they are identifiers. 2504f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty 2505f0f3ef5aSGarrett D'Amore * printing mangles the ID into a truncated and useless value. 2506f0f3ef5aSGarrett D'Amore */ 2507f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2508f0f3ef5aSGarrett D'Amore return (-1); 2509f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2510f0f3ef5aSGarrett D'Amore break; 2511f0f3ef5aSGarrett D'Amore 2512fa9e4066Sahrens default: 2513e7437265Sahrens switch (zfs_prop_get_type(prop)) { 251491ebeef5Sahrens case PROP_TYPE_NUMBER: 2515e7437265Sahrens if (get_numeric_property(zhp, prop, src, 2516e7437265Sahrens &source, &val) != 0) 2517e7437265Sahrens return (-1); 2518e7437265Sahrens if (literal) 2519e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu", 2520e7437265Sahrens (u_longlong_t)val); 2521e7437265Sahrens else 2522e7437265Sahrens zfs_nicenum(val, propbuf, proplen); 2523e7437265Sahrens break; 2524e7437265Sahrens 252591ebeef5Sahrens case PROP_TYPE_STRING: 25269c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 25279c3fd121SMatthew Ahrens if (str == NULL) 25289c3fd121SMatthew Ahrens return (-1); 25299c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2530e7437265Sahrens break; 2531e7437265Sahrens 253291ebeef5Sahrens case PROP_TYPE_INDEX: 2533fe192f76Sahrens if (get_numeric_property(zhp, prop, src, 2534fe192f76Sahrens &source, &val) != 0) 2535fe192f76Sahrens return (-1); 2536fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2537e7437265Sahrens return (-1); 2538e7437265Sahrens (void) strlcpy(propbuf, strval, proplen); 2539e7437265Sahrens break; 2540e7437265Sahrens 2541e7437265Sahrens default: 2542e7437265Sahrens abort(); 2543e7437265Sahrens } 2544fa9e4066Sahrens } 2545fa9e4066Sahrens 2546fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2547fa9e4066Sahrens 2548fa9e4066Sahrens return (0); 2549fa9e4066Sahrens } 2550fa9e4066Sahrens 2551fa9e4066Sahrens /* 2552fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that 2553fa9e4066Sahrens * the given property is the appropriate type; should only be used with 2554fa9e4066Sahrens * hard-coded property types. 2555fa9e4066Sahrens */ 2556fa9e4066Sahrens uint64_t 2557fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2558fa9e4066Sahrens { 2559fa9e4066Sahrens char *source; 256099653d4eSeschrock uint64_t val; 256199653d4eSeschrock 25623cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 2563fa9e4066Sahrens 256499653d4eSeschrock return (val); 2565fa9e4066Sahrens } 2566fa9e4066Sahrens 25677b97dc1aSrm int 25687b97dc1aSrm zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 25697b97dc1aSrm { 25707b97dc1aSrm char buf[64]; 25717b97dc1aSrm 257214843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 25737b97dc1aSrm return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 25747b97dc1aSrm } 25757b97dc1aSrm 2576fa9e4066Sahrens /* 2577fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2578fa9e4066Sahrens */ 2579fa9e4066Sahrens int 2580fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2581990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen) 2582fa9e4066Sahrens { 2583fa9e4066Sahrens char *source; 2584fa9e4066Sahrens 2585fa9e4066Sahrens /* 2586fa9e4066Sahrens * Check to see if this property applies to our object 2587fa9e4066Sahrens */ 2588e45ce728Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2589ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 259099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 259199653d4eSeschrock zfs_prop_to_name(prop))); 2592e45ce728Sahrens } 2593fa9e4066Sahrens 2594fa9e4066Sahrens if (src) 2595990b4856Slling *src = ZPROP_SRC_NONE; 2596fa9e4066Sahrens 259799653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 259899653d4eSeschrock return (-1); 2599fa9e4066Sahrens 2600fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2601fa9e4066Sahrens 2602fa9e4066Sahrens return (0); 2603fa9e4066Sahrens } 2604fa9e4066Sahrens 260514843421SMatthew Ahrens static int 260614843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 260714843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp) 260814843421SMatthew Ahrens { 260914843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL; 261014843421SMatthew Ahrens idmap_stat status; 261114843421SMatthew Ahrens int err = EINVAL; 261214843421SMatthew Ahrens 26131fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 261414843421SMatthew Ahrens goto out; 261514843421SMatthew Ahrens 261614843421SMatthew Ahrens if (isuser) { 261714843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id, 261814843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 261914843421SMatthew Ahrens } else { 262014843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id, 262114843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 262214843421SMatthew Ahrens } 262314843421SMatthew Ahrens if (err == IDMAP_SUCCESS && 262414843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 262514843421SMatthew Ahrens status == IDMAP_SUCCESS) 262614843421SMatthew Ahrens err = 0; 262714843421SMatthew Ahrens else 262814843421SMatthew Ahrens err = EINVAL; 262914843421SMatthew Ahrens out: 263014843421SMatthew Ahrens if (get_hdl) 263114843421SMatthew Ahrens idmap_get_destroy(get_hdl); 263214843421SMatthew Ahrens return (err); 263314843421SMatthew Ahrens } 263414843421SMatthew Ahrens 263514843421SMatthew Ahrens /* 263614843421SMatthew Ahrens * convert the propname into parameters needed by kernel 263714843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 263814843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 263914843421SMatthew Ahrens */ 264014843421SMatthew Ahrens static int 264114843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned, 264214843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 264314843421SMatthew Ahrens { 264414843421SMatthew Ahrens zfs_userquota_prop_t type; 264514843421SMatthew Ahrens char *cp, *end; 26463b12c289SMatthew Ahrens char *numericsid = NULL; 264714843421SMatthew Ahrens boolean_t isuser; 264814843421SMatthew Ahrens 264914843421SMatthew Ahrens domain[0] = '\0'; 26501ed6b69aSGordon Ross *ridp = 0; 265114843421SMatthew Ahrens /* Figure out the property type ({user|group}{quota|space}) */ 265214843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 265314843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type], 265414843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0) 265514843421SMatthew Ahrens break; 265614843421SMatthew Ahrens } 265714843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS) 265814843421SMatthew Ahrens return (EINVAL); 265914843421SMatthew Ahrens *typep = type; 266014843421SMatthew Ahrens 266114843421SMatthew Ahrens isuser = (type == ZFS_PROP_USERQUOTA || 266214843421SMatthew Ahrens type == ZFS_PROP_USERUSED); 266314843421SMatthew Ahrens 266414843421SMatthew Ahrens cp = strchr(propname, '@') + 1; 266514843421SMatthew Ahrens 266614843421SMatthew Ahrens if (strchr(cp, '@')) { 266714843421SMatthew Ahrens /* 266814843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be 26693b12c289SMatthew Ahrens * turned into S-1-domainID-RID. 267014843421SMatthew Ahrens */ 26711ed6b69aSGordon Ross int flag = 0; 26721ed6b69aSGordon Ross idmap_stat stat, map_stat; 26731ed6b69aSGordon Ross uid_t pid; 26741ed6b69aSGordon Ross idmap_rid_t rid; 26751ed6b69aSGordon Ross idmap_get_handle_t *gh = NULL; 26761ed6b69aSGordon Ross 26771ed6b69aSGordon Ross stat = idmap_get_create(&gh); 26781ed6b69aSGordon Ross if (stat != IDMAP_SUCCESS) { 26791ed6b69aSGordon Ross idmap_get_destroy(gh); 26801ed6b69aSGordon Ross return (ENOMEM); 26811ed6b69aSGordon Ross } 268214843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 268314843421SMatthew Ahrens return (ENOENT); 26843b12c289SMatthew Ahrens if (isuser) { 26851ed6b69aSGordon Ross stat = idmap_getuidbywinname(cp, NULL, flag, &pid); 26861ed6b69aSGordon Ross if (stat < 0) 26871ed6b69aSGordon Ross return (ENOENT); 26881ed6b69aSGordon Ross stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid, 26891ed6b69aSGordon Ross &rid, &map_stat); 26903b12c289SMatthew Ahrens } else { 26911ed6b69aSGordon Ross stat = idmap_getgidbywinname(cp, NULL, flag, &pid); 26921ed6b69aSGordon Ross if (stat < 0) 26931ed6b69aSGordon Ross return (ENOENT); 26941ed6b69aSGordon Ross stat = idmap_get_sidbygid(gh, pid, flag, &numericsid, 26951ed6b69aSGordon Ross &rid, &map_stat); 26961ed6b69aSGordon Ross } 26971ed6b69aSGordon Ross if (stat < 0) { 26981ed6b69aSGordon Ross idmap_get_destroy(gh); 26991ed6b69aSGordon Ross return (ENOENT); 27003b12c289SMatthew Ahrens } 27011ed6b69aSGordon Ross stat = idmap_get_mappings(gh); 27021ed6b69aSGordon Ross idmap_get_destroy(gh); 27031ed6b69aSGordon Ross 27041ed6b69aSGordon Ross if (stat < 0) { 270514843421SMatthew Ahrens return (ENOENT); 27063b12c289SMatthew Ahrens } 27073b12c289SMatthew Ahrens if (numericsid == NULL) 270814843421SMatthew Ahrens return (ENOENT); 27093b12c289SMatthew Ahrens cp = numericsid; 27101ed6b69aSGordon Ross *ridp = rid; 27113b12c289SMatthew Ahrens /* will be further decoded below */ 27123b12c289SMatthew Ahrens } 27133b12c289SMatthew Ahrens 27143b12c289SMatthew Ahrens if (strncmp(cp, "S-1-", 4) == 0) { 271514843421SMatthew Ahrens /* It's a numeric SID (eg "S-1-234-567-89") */ 27163b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen); 271714843421SMatthew Ahrens errno = 0; 27181ed6b69aSGordon Ross if (*ridp == 0) { 27191ed6b69aSGordon Ross cp = strrchr(domain, '-'); 27201ed6b69aSGordon Ross *cp = '\0'; 27211ed6b69aSGordon Ross cp++; 27221ed6b69aSGordon Ross *ridp = strtoull(cp, &end, 10); 27231ed6b69aSGordon Ross } else { 27241ed6b69aSGordon Ross end = ""; 27251ed6b69aSGordon Ross } 27263b12c289SMatthew Ahrens if (numericsid) { 27273b12c289SMatthew Ahrens free(numericsid); 27283b12c289SMatthew Ahrens numericsid = NULL; 27293b12c289SMatthew Ahrens } 2730777badbaSMatthew Ahrens if (errno != 0 || *end != '\0') 273114843421SMatthew Ahrens return (EINVAL); 273214843421SMatthew Ahrens } else if (!isdigit(*cp)) { 273314843421SMatthew Ahrens /* 273414843421SMatthew Ahrens * It's a user/group name (eg "user") that needs to be 273514843421SMatthew Ahrens * turned into a uid/gid 273614843421SMatthew Ahrens */ 273714843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 273814843421SMatthew Ahrens return (ENOENT); 273914843421SMatthew Ahrens if (isuser) { 274014843421SMatthew Ahrens struct passwd *pw; 274114843421SMatthew Ahrens pw = getpwnam(cp); 274214843421SMatthew Ahrens if (pw == NULL) 274314843421SMatthew Ahrens return (ENOENT); 274414843421SMatthew Ahrens *ridp = pw->pw_uid; 274514843421SMatthew Ahrens } else { 274614843421SMatthew Ahrens struct group *gr; 274714843421SMatthew Ahrens gr = getgrnam(cp); 274814843421SMatthew Ahrens if (gr == NULL) 274914843421SMatthew Ahrens return (ENOENT); 275014843421SMatthew Ahrens *ridp = gr->gr_gid; 275114843421SMatthew Ahrens } 275214843421SMatthew Ahrens } else { 275314843421SMatthew Ahrens /* It's a user/group ID (eg "12345"). */ 275414843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10); 275514843421SMatthew Ahrens idmap_rid_t rid; 275614843421SMatthew Ahrens char *mapdomain; 275714843421SMatthew Ahrens 275814843421SMatthew Ahrens if (*end != '\0') 275914843421SMatthew Ahrens return (EINVAL); 276014843421SMatthew Ahrens if (id > MAXUID) { 276114843421SMatthew Ahrens /* It's an ephemeral ID. */ 276214843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser, 276314843421SMatthew Ahrens &mapdomain, &rid) != 0) 276414843421SMatthew Ahrens return (ENOENT); 27653b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen); 276614843421SMatthew Ahrens *ridp = rid; 276714843421SMatthew Ahrens } else { 276814843421SMatthew Ahrens *ridp = id; 276914843421SMatthew Ahrens } 277014843421SMatthew Ahrens } 277114843421SMatthew Ahrens 27723b12c289SMatthew Ahrens ASSERT3P(numericsid, ==, NULL); 277314843421SMatthew Ahrens return (0); 277414843421SMatthew Ahrens } 277514843421SMatthew Ahrens 2776edea4b55SLin Ling static int 2777edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 2778edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep) 277914843421SMatthew Ahrens { 278014843421SMatthew Ahrens int err; 278114843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 278214843421SMatthew Ahrens 278319b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 278414843421SMatthew Ahrens 278514843421SMatthew Ahrens err = userquota_propname_decode(propname, 278614843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 2787edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 2788edea4b55SLin Ling zc.zc_objset_type = *typep; 278914843421SMatthew Ahrens if (err) 279014843421SMatthew Ahrens return (err); 279114843421SMatthew Ahrens 279214843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 279314843421SMatthew Ahrens if (err) 279414843421SMatthew Ahrens return (err); 279514843421SMatthew Ahrens 2796edea4b55SLin Ling *propvalue = zc.zc_cookie; 2797edea4b55SLin Ling return (0); 2798edea4b55SLin Ling } 2799edea4b55SLin Ling 2800edea4b55SLin Ling int 2801edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 2802edea4b55SLin Ling uint64_t *propvalue) 2803edea4b55SLin Ling { 2804edea4b55SLin Ling zfs_userquota_prop_t type; 2805edea4b55SLin Ling 2806edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 2807edea4b55SLin Ling &type)); 2808edea4b55SLin Ling } 2809edea4b55SLin Ling 2810edea4b55SLin Ling int 2811edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 2812edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal) 2813edea4b55SLin Ling { 2814edea4b55SLin Ling int err; 2815edea4b55SLin Ling uint64_t propvalue; 2816edea4b55SLin Ling zfs_userquota_prop_t type; 2817edea4b55SLin Ling 2818edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 2819edea4b55SLin Ling &type); 2820edea4b55SLin Ling 2821edea4b55SLin Ling if (err) 2822edea4b55SLin Ling return (err); 2823edea4b55SLin Ling 282414843421SMatthew Ahrens if (literal) { 2825edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue); 2826edea4b55SLin Ling } else if (propvalue == 0 && 282714843421SMatthew Ahrens (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 282814843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen); 282914843421SMatthew Ahrens } else { 2830edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen); 283114843421SMatthew Ahrens } 283214843421SMatthew Ahrens return (0); 283314843421SMatthew Ahrens } 283414843421SMatthew Ahrens 283519b94df9SMatthew Ahrens int 283619b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 283719b94df9SMatthew Ahrens uint64_t *propvalue) 28388ac09fceSRichard Lowe { 283919b94df9SMatthew Ahrens int err; 284019b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 }; 284119b94df9SMatthew Ahrens const char *snapname; 2842ebedde84SEric Taylor 284319b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2844e5351341SMatthew Ahrens 284519b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1; 284619b94df9SMatthew Ahrens if (strchr(snapname, '@')) { 284719b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 284819b94df9SMatthew Ahrens } else { 284919b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */ 285019b94df9SMatthew Ahrens char *cp; 2851e5351341SMatthew Ahrens 285219b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name, 285319b94df9SMatthew Ahrens sizeof (zc.zc_value)); 285419b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@'); 285519b94df9SMatthew Ahrens if (cp != NULL) 285619b94df9SMatthew Ahrens *cp = '\0'; 285719b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); 285819b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); 28598ac09fceSRichard Lowe } 286019b94df9SMatthew Ahrens 286119b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); 286219b94df9SMatthew Ahrens if (err) 286319b94df9SMatthew Ahrens return (err); 286419b94df9SMatthew Ahrens 286519b94df9SMatthew Ahrens *propvalue = zc.zc_cookie; 286619b94df9SMatthew Ahrens return (0); 2867ebedde84SEric Taylor } 2868ebedde84SEric Taylor 2869fa9e4066Sahrens int 287019b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 287119b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal) 2872fa9e4066Sahrens { 287319b94df9SMatthew Ahrens int err; 287419b94df9SMatthew Ahrens uint64_t propvalue; 28753cb34c60Sahrens 287619b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue); 2877ebedde84SEric Taylor 287819b94df9SMatthew Ahrens if (err) 287919b94df9SMatthew Ahrens return (err); 28808ac09fceSRichard Lowe 288119b94df9SMatthew Ahrens if (literal) { 288219b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue); 288319b94df9SMatthew Ahrens } else { 288419b94df9SMatthew Ahrens zfs_nicenum(propvalue, propbuf, proplen); 2885fa9e4066Sahrens } 288619b94df9SMatthew Ahrens return (0); 28877f7322feSeschrock } 28887f7322feSeschrock 28897f7322feSeschrock /* 289019b94df9SMatthew Ahrens * Returns the name of the given zfs handle. 28917f7322feSeschrock */ 289219b94df9SMatthew Ahrens const char * 289319b94df9SMatthew Ahrens zfs_get_name(const zfs_handle_t *zhp) 28947f7322feSeschrock { 289519b94df9SMatthew Ahrens return (zhp->zfs_name); 289619b94df9SMatthew Ahrens } 28978ac09fceSRichard Lowe 28988808ac5dSYuri Pankov /* 28998808ac5dSYuri Pankov * Returns the name of the parent pool for the given zfs handle. 29008808ac5dSYuri Pankov */ 29018808ac5dSYuri Pankov const char * 29028808ac5dSYuri Pankov zfs_get_pool_name(const zfs_handle_t *zhp) 29038808ac5dSYuri Pankov { 29048808ac5dSYuri Pankov return (zhp->zpool_hdl->zpool_name); 29058808ac5dSYuri Pankov } 29068808ac5dSYuri Pankov 290719b94df9SMatthew Ahrens /* 290819b94df9SMatthew Ahrens * Returns the type of the given zfs handle. 290919b94df9SMatthew Ahrens */ 291019b94df9SMatthew Ahrens zfs_type_t 291119b94df9SMatthew Ahrens zfs_get_type(const zfs_handle_t *zhp) 291219b94df9SMatthew Ahrens { 291319b94df9SMatthew Ahrens return (zhp->zfs_type); 29147f7322feSeschrock } 29157f7322feSeschrock 2916d41c4376SMark J Musante /* 2917d41c4376SMark J Musante * Is one dataset name a child dataset of another? 2918d41c4376SMark J Musante * 2919d41c4376SMark J Musante * Needs to handle these cases: 2920d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 2921d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 2922d41c4376SMark J Musante * Descendant? No. No. No. Yes. 2923d41c4376SMark J Musante */ 2924d41c4376SMark J Musante static boolean_t 2925d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2) 2926d41c4376SMark J Musante { 2927d41c4376SMark J Musante size_t d1len = strlen(ds1); 2928d41c4376SMark J Musante 2929d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */ 2930d41c4376SMark J Musante if (strlen(ds2) < d1len) 2931d41c4376SMark J Musante return (B_FALSE); 2932d41c4376SMark J Musante 2933d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */ 2934d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 2935d41c4376SMark J Musante } 2936d41c4376SMark J Musante 2937fa9e4066Sahrens /* 2938fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent. 293919b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the 294019b94df9SMatthew Ahrens * pool). 2941fa9e4066Sahrens */ 2942fa9e4066Sahrens static int 2943fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen) 2944fa9e4066Sahrens { 294519b94df9SMatthew Ahrens char *slashp; 2946fa9e4066Sahrens 294719b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen); 2948fa9e4066Sahrens 294919b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL) 295019b94df9SMatthew Ahrens return (-1); 295119b94df9SMatthew Ahrens *slashp = '\0'; 2952fa9e4066Sahrens 2953fa9e4066Sahrens return (0); 2954fa9e4066Sahrens } 2955fa9e4066Sahrens 2956fa9e4066Sahrens /* 29577f1f55eaSvb * If accept_ancestor is false, then check to make sure that the given path has 29587f1f55eaSvb * a parent, and that it exists. If accept_ancestor is true, then find the 29597f1f55eaSvb * closest existing ancestor for the given path. In prefixlen return the 29607f1f55eaSvb * length of already existing prefix of the given path. We also fetch the 29617f1f55eaSvb * 'zoned' property, which is used to validate property settings when creating 29627f1f55eaSvb * new datasets. 2963fa9e4066Sahrens */ 2964fa9e4066Sahrens static int 29657f1f55eaSvb check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 29667f1f55eaSvb boolean_t accept_ancestor, int *prefixlen) 2967fa9e4066Sahrens { 2968fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2969*9adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 2970fa9e4066Sahrens char *slash; 29717f7322feSeschrock zfs_handle_t *zhp; 297299653d4eSeschrock char errbuf[1024]; 2973d41c4376SMark J Musante uint64_t is_zoned; 297499653d4eSeschrock 2975deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf), 2976deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2977fa9e4066Sahrens 2978fa9e4066Sahrens /* get parent, and check to see if this is just a pool */ 2979fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 298099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 298199653d4eSeschrock "missing dataset name")); 298299653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2983fa9e4066Sahrens } 2984fa9e4066Sahrens 2985fa9e4066Sahrens /* check to see if the pool exists */ 2986fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL) 2987fa9e4066Sahrens slash = parent + strlen(parent); 29888ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent); 2989fa9e4066Sahrens zc.zc_name[slash - parent] = '\0'; 299099653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2991fa9e4066Sahrens errno == ENOENT) { 299299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 299399653d4eSeschrock "no such pool '%s'"), zc.zc_name); 299499653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2995fa9e4066Sahrens } 2996fa9e4066Sahrens 2997fa9e4066Sahrens /* check to see if the parent dataset exists */ 29987f1f55eaSvb while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 29997f1f55eaSvb if (errno == ENOENT && accept_ancestor) { 30007f1f55eaSvb /* 30017f1f55eaSvb * Go deeper to find an ancestor, give up on top level. 30027f1f55eaSvb */ 30037f1f55eaSvb if (parent_name(parent, parent, sizeof (parent)) != 0) { 30047f1f55eaSvb zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 30057f1f55eaSvb "no such pool '%s'"), zc.zc_name); 30067f1f55eaSvb return (zfs_error(hdl, EZFS_NOENT, errbuf)); 30077f1f55eaSvb } 30087f1f55eaSvb } else if (errno == ENOENT) { 300999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 301099653d4eSeschrock "parent does not exist")); 301199653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 30127f1f55eaSvb } else 301399653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3014fa9e4066Sahrens } 3015fa9e4066Sahrens 3016d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 3017d41c4376SMark J Musante if (zoned != NULL) 3018d41c4376SMark J Musante *zoned = is_zoned; 3019d41c4376SMark J Musante 3020fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */ 3021d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 302299653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 30237f7322feSeschrock zfs_close(zhp); 3024fa9e4066Sahrens return (-1); 3025fa9e4066Sahrens } 3026fa9e4066Sahrens 3027fa9e4066Sahrens /* make sure parent is a filesystem */ 30287f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 302999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 303099653d4eSeschrock "parent is not a filesystem")); 303199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 30327f7322feSeschrock zfs_close(zhp); 3033fa9e4066Sahrens return (-1); 3034fa9e4066Sahrens } 3035fa9e4066Sahrens 30367f7322feSeschrock zfs_close(zhp); 30377f1f55eaSvb if (prefixlen != NULL) 30387f1f55eaSvb *prefixlen = strlen(parent); 30397f1f55eaSvb return (0); 30407f1f55eaSvb } 30417f1f55eaSvb 30427f1f55eaSvb /* 30437f1f55eaSvb * Finds whether the dataset of the given type(s) exists. 30447f1f55eaSvb */ 30457f1f55eaSvb boolean_t 30467f1f55eaSvb zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 30477f1f55eaSvb { 30487f1f55eaSvb zfs_handle_t *zhp; 30497f1f55eaSvb 3050f18faf3fSek if (!zfs_validate_name(hdl, path, types, B_FALSE)) 30517f1f55eaSvb return (B_FALSE); 30527f1f55eaSvb 30537f1f55eaSvb /* 30547f1f55eaSvb * Try to get stats for the dataset, which will tell us if it exists. 30557f1f55eaSvb */ 30567f1f55eaSvb if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 30577f1f55eaSvb int ds_type = zhp->zfs_type; 30587f1f55eaSvb 30597f1f55eaSvb zfs_close(zhp); 30607f1f55eaSvb if (types & ds_type) 30617f1f55eaSvb return (B_TRUE); 30627f1f55eaSvb } 30637f1f55eaSvb return (B_FALSE); 30647f1f55eaSvb } 30657f1f55eaSvb 30663cb34c60Sahrens /* 30673cb34c60Sahrens * Given a path to 'target', create all the ancestors between 30683cb34c60Sahrens * the prefixlen portion of the path, and the target itself. 30693cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 30703cb34c60Sahrens */ 30713cb34c60Sahrens int 30723cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 30733cb34c60Sahrens { 30743cb34c60Sahrens zfs_handle_t *h; 30753cb34c60Sahrens char *cp; 30763cb34c60Sahrens const char *opname; 30773cb34c60Sahrens 30783cb34c60Sahrens /* make sure prefix exists */ 30793cb34c60Sahrens cp = target + prefixlen; 30803cb34c60Sahrens if (*cp != '/') { 30813cb34c60Sahrens assert(strchr(cp, '/') == NULL); 30823cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30833cb34c60Sahrens } else { 30843cb34c60Sahrens *cp = '\0'; 30853cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30863cb34c60Sahrens *cp = '/'; 30873cb34c60Sahrens } 30883cb34c60Sahrens if (h == NULL) 30893cb34c60Sahrens return (-1); 30903cb34c60Sahrens zfs_close(h); 30913cb34c60Sahrens 30923cb34c60Sahrens /* 30933cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems, 30943cb34c60Sahrens * up to the prefixlen-long one. 30953cb34c60Sahrens */ 30963cb34c60Sahrens for (cp = target + prefixlen + 1; 309788f61deeSIgor Kozhukhov (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) { 30983cb34c60Sahrens 30993cb34c60Sahrens *cp = '\0'; 31003cb34c60Sahrens 31013cb34c60Sahrens h = make_dataset_handle(hdl, target); 31023cb34c60Sahrens if (h) { 31033cb34c60Sahrens /* it already exists, nothing to do here */ 31043cb34c60Sahrens zfs_close(h); 31053cb34c60Sahrens continue; 31063cb34c60Sahrens } 31073cb34c60Sahrens 31083cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 31093cb34c60Sahrens NULL) != 0) { 31103cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 31113cb34c60Sahrens goto ancestorerr; 31123cb34c60Sahrens } 31133cb34c60Sahrens 31143cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31153cb34c60Sahrens if (h == NULL) { 31163cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 31173cb34c60Sahrens goto ancestorerr; 31183cb34c60Sahrens } 31193cb34c60Sahrens 31203cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) { 31213cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 31223cb34c60Sahrens goto ancestorerr; 31233cb34c60Sahrens } 31243cb34c60Sahrens 31253cb34c60Sahrens if (zfs_share(h) != 0) { 31263cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 31273cb34c60Sahrens goto ancestorerr; 31283cb34c60Sahrens } 31293cb34c60Sahrens 31303cb34c60Sahrens zfs_close(h); 31313cb34c60Sahrens } 31323cb34c60Sahrens 31333cb34c60Sahrens return (0); 31343cb34c60Sahrens 31353cb34c60Sahrens ancestorerr: 31363cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31373cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target); 31383cb34c60Sahrens return (-1); 31393cb34c60Sahrens } 31403cb34c60Sahrens 31417f1f55eaSvb /* 31427f1f55eaSvb * Creates non-existing ancestors of the given path. 31437f1f55eaSvb */ 31447f1f55eaSvb int 31457f1f55eaSvb zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 31467f1f55eaSvb { 31477f1f55eaSvb int prefix; 31487f1f55eaSvb char *path_copy; 3149f83b46baSPaul Dagnelie int rc = 0; 31507f1f55eaSvb 3151d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 31527f1f55eaSvb return (-1); 31537f1f55eaSvb 31547f1f55eaSvb if ((path_copy = strdup(path)) != NULL) { 31557f1f55eaSvb rc = create_parents(hdl, path_copy, prefix); 31567f1f55eaSvb free(path_copy); 31577f1f55eaSvb } 31587f1f55eaSvb if (path_copy == NULL || rc != 0) 31597f1f55eaSvb return (-1); 31607f1f55eaSvb 3161fa9e4066Sahrens return (0); 3162fa9e4066Sahrens } 3163fa9e4066Sahrens 3164fa9e4066Sahrens /* 3165e9dbad6fSeschrock * Create a new filesystem or volume. 3166fa9e4066Sahrens */ 3167fa9e4066Sahrens int 316899653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3169e9dbad6fSeschrock nvlist_t *props) 3170fa9e4066Sahrens { 3171fa9e4066Sahrens int ret; 3172fa9e4066Sahrens uint64_t size = 0; 3173fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 317499653d4eSeschrock char errbuf[1024]; 3175e9dbad6fSeschrock uint64_t zoned; 317626455f9eSAndriy Gapon enum lzc_dataset_type ost; 317799653d4eSeschrock 317899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 317999653d4eSeschrock "cannot create '%s'"), path); 3180fa9e4066Sahrens 3181fa9e4066Sahrens /* validate the path, taking care to note the extended error message */ 3182f18faf3fSek if (!zfs_validate_name(hdl, path, type, B_TRUE)) 318399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3184fa9e4066Sahrens 3185fa9e4066Sahrens /* validate parents exist */ 31867f1f55eaSvb if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3187fa9e4066Sahrens return (-1); 3188fa9e4066Sahrens 3189fa9e4066Sahrens /* 3190fa9e4066Sahrens * The failure modes when creating a dataset of a different type over 3191fa9e4066Sahrens * one that already exists is a little strange. In particular, if you 3192fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3193fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3194fa9e4066Sahrens * first try to see if the dataset exists. 3195fa9e4066Sahrens */ 31964445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 319799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 319899653d4eSeschrock "dataset already exists")); 319999653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3200fa9e4066Sahrens } 3201fa9e4066Sahrens 3202fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) 320326455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZVOL; 3204fa9e4066Sahrens else 320526455f9eSAndriy Gapon ost = LZC_DATSET_TYPE_ZFS; 3206fa9e4066Sahrens 3207e9316f76SJoe Stein /* open zpool handle for prop validation */ 3208*9adfa60dSMatthew Ahrens char pool_path[ZFS_MAX_DATASET_NAME_LEN]; 3209e9316f76SJoe Stein (void) strlcpy(pool_path, path, sizeof (pool_path)); 3210e9316f76SJoe Stein 3211e9316f76SJoe Stein /* truncate pool_path at first slash */ 3212e9316f76SJoe Stein char *p = strchr(pool_path, '/'); 3213e9316f76SJoe Stein if (p != NULL) 3214e9316f76SJoe Stein *p = '\0'; 3215e9316f76SJoe Stein 3216e9316f76SJoe Stein zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path); 3217e9316f76SJoe Stein 32180a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props, 3219e9316f76SJoe Stein zoned, NULL, zpool_handle, errbuf)) == 0) { 3220e9316f76SJoe Stein zpool_close(zpool_handle); 3221e9dbad6fSeschrock return (-1); 3222e9316f76SJoe Stein } 3223e9316f76SJoe Stein zpool_close(zpool_handle); 3224e9dbad6fSeschrock 3225fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) { 32265c5460e9Seschrock /* 32275c5460e9Seschrock * If we are creating a volume, the size and block size must 32285c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a 32295c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 32305c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be 32315c5460e9Seschrock * zero. 32325c5460e9Seschrock */ 3233e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props, 3234e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3235e9dbad6fSeschrock nvlist_free(props); 323699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3237e9dbad6fSeschrock "missing volume size")); 3238e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3239e9dbad6fSeschrock } 3240e9dbad6fSeschrock 3241e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props, 3242e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3243e9dbad6fSeschrock &blocksize)) != 0) { 3244e9dbad6fSeschrock if (ret == ENOENT) { 3245e9dbad6fSeschrock blocksize = zfs_prop_default_numeric( 3246e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3247e9dbad6fSeschrock } else { 3248e9dbad6fSeschrock nvlist_free(props); 3249e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3250e9dbad6fSeschrock "missing volume block size")); 3251e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3252e9dbad6fSeschrock } 3253fa9e4066Sahrens } 3254fa9e4066Sahrens 3255e9dbad6fSeschrock if (size == 0) { 3256e9dbad6fSeschrock nvlist_free(props); 325799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3258e9dbad6fSeschrock "volume size cannot be zero")); 3259e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32605c5460e9Seschrock } 32615c5460e9Seschrock 32625c5460e9Seschrock if (size % blocksize != 0) { 3263e9dbad6fSeschrock nvlist_free(props); 326499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3265e9dbad6fSeschrock "volume size must be a multiple of volume block " 3266e9dbad6fSeschrock "size")); 3267e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32685c5460e9Seschrock } 3269fa9e4066Sahrens } 3270fa9e4066Sahrens 3271fa9e4066Sahrens /* create the dataset */ 32724445fffbSMatthew Ahrens ret = lzc_create(path, ost, props); 32734445fffbSMatthew Ahrens nvlist_free(props); 3274e9dbad6fSeschrock 3275fa9e4066Sahrens /* check for failure */ 3276fa9e4066Sahrens if (ret != 0) { 3277*9adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3278fa9e4066Sahrens (void) parent_name(path, parent, sizeof (parent)); 3279fa9e4066Sahrens 3280fa9e4066Sahrens switch (errno) { 3281fa9e4066Sahrens case ENOENT: 328299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 328399653d4eSeschrock "no such parent '%s'"), parent); 328499653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3285fa9e4066Sahrens 3286fa9e4066Sahrens case EINVAL: 328799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3288d7d4af51Smmusante "parent '%s' is not a filesystem"), parent); 328999653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3290fa9e4066Sahrens 329140feaa91Sahrens case ENOTSUP: 329240feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 329340feaa91Sahrens "pool must be upgraded to set this " 329440feaa91Sahrens "property or value")); 329540feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3296fa9e4066Sahrens #ifdef _ILP32 3297fa9e4066Sahrens case EOVERFLOW: 3298fa9e4066Sahrens /* 3299fa9e4066Sahrens * This platform can't address a volume this big. 3300fa9e4066Sahrens */ 330199653d4eSeschrock if (type == ZFS_TYPE_VOLUME) 330299653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 330399653d4eSeschrock errbuf)); 3304fa9e4066Sahrens #endif 330599653d4eSeschrock /* FALLTHROUGH */ 3306fa9e4066Sahrens default: 330799653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3308fa9e4066Sahrens } 3309fa9e4066Sahrens } 3310fa9e4066Sahrens 3311fa9e4066Sahrens return (0); 3312fa9e4066Sahrens } 3313fa9e4066Sahrens 3314fa9e4066Sahrens /* 3315fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 331665fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system 331765fec9f6SChristopher Siden * does not exist this function does nothing. 3318fa9e4066Sahrens */ 3319fa9e4066Sahrens int 3320842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3321fa9e4066Sahrens { 3322fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3323fa9e4066Sahrens 332478f17100SMatthew Ahrens if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 332578f17100SMatthew Ahrens nvlist_t *nv = fnvlist_alloc(); 332678f17100SMatthew Ahrens fnvlist_add_boolean(nv, zhp->zfs_name); 332778f17100SMatthew Ahrens int error = lzc_destroy_bookmarks(nv, NULL); 332878f17100SMatthew Ahrens fnvlist_free(nv); 332978f17100SMatthew Ahrens if (error != 0) { 333078f17100SMatthew Ahrens return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 333178f17100SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 333278f17100SMatthew Ahrens zhp->zfs_name)); 333378f17100SMatthew Ahrens } 333478f17100SMatthew Ahrens return (0); 333578f17100SMatthew Ahrens } 333678f17100SMatthew Ahrens 3337fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3338fa9e4066Sahrens 3339e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) { 3340fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3341fa9e4066Sahrens } else { 3342fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3343fa9e4066Sahrens } 3344fa9e4066Sahrens 3345842727c2SChris Kirby zc.zc_defer_destroy = defer; 334665fec9f6SChristopher Siden if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 && 334765fec9f6SChristopher Siden errno != ENOENT) { 3348ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 334999653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 335099653d4eSeschrock zhp->zfs_name)); 33511d452cf5Sahrens } 3352fa9e4066Sahrens 3353fa9e4066Sahrens remove_mountpoint(zhp); 3354fa9e4066Sahrens 3355fa9e4066Sahrens return (0); 3356fa9e4066Sahrens } 3357fa9e4066Sahrens 33581d452cf5Sahrens struct destroydata { 335919b94df9SMatthew Ahrens nvlist_t *nvl; 336019b94df9SMatthew Ahrens const char *snapname; 33611d452cf5Sahrens }; 33621d452cf5Sahrens 33631d452cf5Sahrens static int 3364681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 33651d452cf5Sahrens { 33661d452cf5Sahrens struct destroydata *dd = arg; 3367*9adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 3368681d9761SEric Taylor int rv = 0; 33691d452cf5Sahrens 337019b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name), 337119b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname); 33721d452cf5Sahrens 3373a7a845e4SSteven Hartland if (lzc_exists(name)) 337419b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0); 33751d452cf5Sahrens 337619b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 337719b94df9SMatthew Ahrens zfs_close(zhp); 33783ccfa83cSahrens return (rv); 33791d452cf5Sahrens } 33801d452cf5Sahrens 33811d452cf5Sahrens /* 33821d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants. 33831d452cf5Sahrens */ 33841d452cf5Sahrens int 3385842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 33861d452cf5Sahrens { 33871d452cf5Sahrens int ret; 33881d452cf5Sahrens struct destroydata dd = { 0 }; 33891d452cf5Sahrens 33901d452cf5Sahrens dd.snapname = snapname; 339119b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); 339219b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 33931d452cf5Sahrens 3394a7a845e4SSteven Hartland if (nvlist_empty(dd.nvl)) { 339519b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 33961d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 339719b94df9SMatthew Ahrens zhp->zfs_name, snapname); 339819b94df9SMatthew Ahrens } else { 33993b2aab18SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 34001d452cf5Sahrens } 340119b94df9SMatthew Ahrens nvlist_free(dd.nvl); 340219b94df9SMatthew Ahrens return (ret); 340319b94df9SMatthew Ahrens } 340419b94df9SMatthew Ahrens 340519b94df9SMatthew Ahrens /* 34063b2aab18SMatthew Ahrens * Destroys all the snapshots named in the nvlist. 340719b94df9SMatthew Ahrens */ 340819b94df9SMatthew Ahrens int 34093b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 341019b94df9SMatthew Ahrens { 341119b94df9SMatthew Ahrens int ret; 34124cde22c2SChris Williamson nvlist_t *errlist = NULL; 34131d452cf5Sahrens 34144445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist); 34151d452cf5Sahrens 34164cde22c2SChris Williamson if (ret == 0) { 34174cde22c2SChris Williamson nvlist_free(errlist); 34183b2aab18SMatthew Ahrens return (0); 34194cde22c2SChris Williamson } 34204445fffbSMatthew Ahrens 3421a7a845e4SSteven Hartland if (nvlist_empty(errlist)) { 34223b2aab18SMatthew Ahrens char errbuf[1024]; 34233b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 34243b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 34253b2aab18SMatthew Ahrens 34263b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, ret, errbuf); 34273b2aab18SMatthew Ahrens } 34283b2aab18SMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL); 34293b2aab18SMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 34303b2aab18SMatthew Ahrens char errbuf[1024]; 34313b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 34323b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 34333b2aab18SMatthew Ahrens nvpair_name(pair)); 34343b2aab18SMatthew Ahrens 34353b2aab18SMatthew Ahrens switch (fnvpair_value_int32(pair)) { 34363b2aab18SMatthew Ahrens case EEXIST: 34373b2aab18SMatthew Ahrens zfs_error_aux(hdl, 34383b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "snapshot is cloned")); 34393b2aab18SMatthew Ahrens ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 34403b2aab18SMatthew Ahrens break; 34413b2aab18SMatthew Ahrens default: 34423b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, errno, errbuf); 34433b2aab18SMatthew Ahrens break; 34441d452cf5Sahrens } 34451d452cf5Sahrens } 34461d452cf5Sahrens 34474cde22c2SChris Williamson nvlist_free(errlist); 34484445fffbSMatthew Ahrens return (ret); 34491d452cf5Sahrens } 34501d452cf5Sahrens 3451fa9e4066Sahrens /* 3452fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source. 3453fa9e4066Sahrens */ 3454fa9e4066Sahrens int 3455e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3456fa9e4066Sahrens { 3457*9adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 3458fa9e4066Sahrens int ret; 345999653d4eSeschrock char errbuf[1024]; 346099653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3461e9dbad6fSeschrock uint64_t zoned; 3462fa9e4066Sahrens 3463fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3464fa9e4066Sahrens 346599653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 346699653d4eSeschrock "cannot create '%s'"), target); 346799653d4eSeschrock 346819b94df9SMatthew Ahrens /* validate the target/clone name */ 3469f18faf3fSek if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 347099653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3471fa9e4066Sahrens 3472fa9e4066Sahrens /* validate parents exist */ 34737f1f55eaSvb if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3474fa9e4066Sahrens return (-1); 3475fa9e4066Sahrens 3476fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent)); 3477fa9e4066Sahrens 3478fa9e4066Sahrens /* do the clone */ 3479e9dbad6fSeschrock 3480e9dbad6fSeschrock if (props) { 34814445fffbSMatthew Ahrens zfs_type_t type; 34824445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) { 34834445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME; 34844445fffbSMatthew Ahrens } else { 34854445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM; 34864445fffbSMatthew Ahrens } 34870a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 3488e9316f76SJoe Stein zhp, zhp->zpool_hdl, errbuf)) == NULL) 3489e9dbad6fSeschrock return (-1); 3490e9dbad6fSeschrock } 3491fa9e4066Sahrens 34924445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props); 34934445fffbSMatthew Ahrens nvlist_free(props); 3494e9dbad6fSeschrock 3495fa9e4066Sahrens if (ret != 0) { 3496fa9e4066Sahrens switch (errno) { 3497fa9e4066Sahrens 3498fa9e4066Sahrens case ENOENT: 3499fa9e4066Sahrens /* 3500fa9e4066Sahrens * The parent doesn't exist. We should have caught this 3501fa9e4066Sahrens * above, but there may a race condition that has since 3502fa9e4066Sahrens * destroyed the parent. 3503fa9e4066Sahrens * 3504fa9e4066Sahrens * At this point, we don't know whether it's the source 3505fa9e4066Sahrens * that doesn't exist anymore, or whether the target 3506fa9e4066Sahrens * dataset doesn't exist. 3507fa9e4066Sahrens */ 350899653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 350999653d4eSeschrock "no such parent '%s'"), parent); 351099653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3511fa9e4066Sahrens 351299653d4eSeschrock case EXDEV: 351399653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 351499653d4eSeschrock "source and target pools differ")); 351599653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 351699653d4eSeschrock errbuf)); 3517fa9e4066Sahrens 351899653d4eSeschrock default: 351999653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 352099653d4eSeschrock errbuf)); 352199653d4eSeschrock } 352299653d4eSeschrock } 3523fa9e4066Sahrens 352499653d4eSeschrock return (ret); 352599653d4eSeschrock } 352699653d4eSeschrock 352799653d4eSeschrock /* 352899653d4eSeschrock * Promotes the given clone fs to be the clone parent. 352999653d4eSeschrock */ 353099653d4eSeschrock int 353199653d4eSeschrock zfs_promote(zfs_handle_t *zhp) 353299653d4eSeschrock { 353399653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 353499653d4eSeschrock zfs_cmd_t zc = { 0 }; 353599653d4eSeschrock char parent[MAXPATHLEN]; 353699653d4eSeschrock int ret; 353799653d4eSeschrock char errbuf[1024]; 353899653d4eSeschrock 353999653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 354099653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name); 354199653d4eSeschrock 354299653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 354399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 354499653d4eSeschrock "snapshots can not be promoted")); 354599653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 354699653d4eSeschrock } 354799653d4eSeschrock 35483cb34c60Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 354999653d4eSeschrock if (parent[0] == '\0') { 355099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 355199653d4eSeschrock "not a cloned filesystem")); 355299653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 355399653d4eSeschrock } 355499653d4eSeschrock 35553cb34c60Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 3556e9dbad6fSeschrock sizeof (zc.zc_value)); 355799653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3558ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 355999653d4eSeschrock 356099653d4eSeschrock if (ret != 0) { 35610b69c2f0Sahrens int save_errno = errno; 35620b69c2f0Sahrens 35630b69c2f0Sahrens switch (save_errno) { 356499653d4eSeschrock case EEXIST: 3565681d9761SEric Taylor /* There is a conflicting snapshot name. */ 356699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3567681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"), 3568681d9761SEric Taylor zc.zc_string, parent); 356999653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3570fa9e4066Sahrens 3571fa9e4066Sahrens default: 35720b69c2f0Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3573fa9e4066Sahrens } 3574fa9e4066Sahrens } 3575e9dbad6fSeschrock return (ret); 35761d452cf5Sahrens } 35771d452cf5Sahrens 35784445fffbSMatthew Ahrens typedef struct snapdata { 35794445fffbSMatthew Ahrens nvlist_t *sd_nvl; 35804445fffbSMatthew Ahrens const char *sd_snapname; 35814445fffbSMatthew Ahrens } snapdata_t; 35824445fffbSMatthew Ahrens 35834445fffbSMatthew Ahrens static int 35844445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 35854445fffbSMatthew Ahrens { 35864445fffbSMatthew Ahrens snapdata_t *sd = arg; 3587*9adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 35884445fffbSMatthew Ahrens int rv = 0; 35894445fffbSMatthew Ahrens 3590ca48f36fSKeith M Wesolowski if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 3591ca48f36fSKeith M Wesolowski (void) snprintf(name, sizeof (name), 3592ca48f36fSKeith M Wesolowski "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 35934445fffbSMatthew Ahrens 3594ca48f36fSKeith M Wesolowski fnvlist_add_boolean(sd->sd_nvl, name); 35954445fffbSMatthew Ahrens 3596ca48f36fSKeith M Wesolowski rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3597ca48f36fSKeith M Wesolowski } 35984445fffbSMatthew Ahrens zfs_close(zhp); 3599ca48f36fSKeith M Wesolowski 36004445fffbSMatthew Ahrens return (rv); 36014445fffbSMatthew Ahrens } 36024445fffbSMatthew Ahrens 3603fa9e4066Sahrens /* 36044445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 36054445fffbSMatthew Ahrens * created. 3606fa9e4066Sahrens */ 3607fa9e4066Sahrens int 36084445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 3609fa9e4066Sahrens { 3610fa9e4066Sahrens int ret; 361199653d4eSeschrock char errbuf[1024]; 36124445fffbSMatthew Ahrens nvpair_t *elem; 36134445fffbSMatthew Ahrens nvlist_t *errors; 3614fa9e4066Sahrens 361599653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36164445fffbSMatthew Ahrens "cannot create snapshots ")); 361799653d4eSeschrock 36184445fffbSMatthew Ahrens elem = NULL; 36194445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 36204445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem); 36214445fffbSMatthew Ahrens 36224445fffbSMatthew Ahrens /* validate the target name */ 36234445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 36244445fffbSMatthew Ahrens B_TRUE)) { 36254445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 36264445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 36274445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname); 36284445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 36294445fffbSMatthew Ahrens } 36304445fffbSMatthew Ahrens } 3631fa9e4066Sahrens 3632e9316f76SJoe Stein /* 3633e9316f76SJoe Stein * get pool handle for prop validation. assumes all snaps are in the 3634e9316f76SJoe Stein * same pool, as does lzc_snapshot (below). 3635e9316f76SJoe Stein */ 3636*9adfa60dSMatthew Ahrens char pool[ZFS_MAX_DATASET_NAME_LEN]; 3637e9316f76SJoe Stein elem = nvlist_next_nvpair(snaps, NULL); 3638e9316f76SJoe Stein (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); 3639e9316f76SJoe Stein pool[strcspn(pool, "/@")] = '\0'; 3640e9316f76SJoe Stein zpool_handle_t *zpool_hdl = zpool_open(hdl, pool); 3641e9316f76SJoe Stein 36424445fffbSMatthew Ahrens if (props != NULL && 36434445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 3644e9316f76SJoe Stein props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) { 3645e9316f76SJoe Stein zpool_close(zpool_hdl); 36464445fffbSMatthew Ahrens return (-1); 36474445fffbSMatthew Ahrens } 3648e9316f76SJoe Stein zpool_close(zpool_hdl); 3649bb0ade09Sahrens 36504445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors); 36514445fffbSMatthew Ahrens 36524445fffbSMatthew Ahrens if (ret != 0) { 36534445fffbSMatthew Ahrens boolean_t printed = B_FALSE; 36544445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 36554445fffbSMatthew Ahrens elem != NULL; 36564445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 36574445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 36584445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 36594445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem)); 36604445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, 36614445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf); 36624445fffbSMatthew Ahrens printed = B_TRUE; 3663bb0ade09Sahrens } 36644445fffbSMatthew Ahrens if (!printed) { 36654445fffbSMatthew Ahrens switch (ret) { 36664445fffbSMatthew Ahrens case EXDEV: 36674445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36684445fffbSMatthew Ahrens "multiple snapshots of same " 36694445fffbSMatthew Ahrens "fs not allowed")); 36704445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 3671bb0ade09Sahrens 36724445fffbSMatthew Ahrens break; 36734445fffbSMatthew Ahrens default: 36744445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 36754445fffbSMatthew Ahrens } 36764445fffbSMatthew Ahrens } 3677bb0ade09Sahrens } 3678bb0ade09Sahrens 36794445fffbSMatthew Ahrens nvlist_free(props); 36804445fffbSMatthew Ahrens nvlist_free(errors); 36814445fffbSMatthew Ahrens return (ret); 36824445fffbSMatthew Ahrens } 36834445fffbSMatthew Ahrens 36844445fffbSMatthew Ahrens int 36854445fffbSMatthew Ahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 36864445fffbSMatthew Ahrens nvlist_t *props) 36874445fffbSMatthew Ahrens { 36884445fffbSMatthew Ahrens int ret; 36894445fffbSMatthew Ahrens snapdata_t sd = { 0 }; 3690*9adfa60dSMatthew Ahrens char fsname[ZFS_MAX_DATASET_NAME_LEN]; 36914445fffbSMatthew Ahrens char *cp; 36924445fffbSMatthew Ahrens zfs_handle_t *zhp; 36934445fffbSMatthew Ahrens char errbuf[1024]; 36944445fffbSMatthew Ahrens 36954445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36964445fffbSMatthew Ahrens "cannot snapshot %s"), path); 36974445fffbSMatthew Ahrens 36984445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 36994445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 37004445fffbSMatthew Ahrens 37014445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 37024445fffbSMatthew Ahrens cp = strchr(fsname, '@'); 37034445fffbSMatthew Ahrens *cp = '\0'; 37044445fffbSMatthew Ahrens sd.sd_snapname = cp + 1; 3705fa9e4066Sahrens 37064445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 3707fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3708fa9e4066Sahrens return (-1); 3709fa9e4066Sahrens } 3710fa9e4066Sahrens 37114445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); 37124445fffbSMatthew Ahrens if (recursive) { 37134445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 37144445fffbSMatthew Ahrens } else { 37154445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path); 3716681d9761SEric Taylor } 3717fa9e4066Sahrens 37184445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 37194445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl); 3720fa9e4066Sahrens zfs_close(zhp); 3721fa9e4066Sahrens return (ret); 3722fa9e4066Sahrens } 3723fa9e4066Sahrens 3724fa9e4066Sahrens /* 3725b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 3726b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3727b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction 3728b12a1c38Slling * group. 3729fa9e4066Sahrens */ 3730b12a1c38Slling typedef struct rollback_data { 3731b12a1c38Slling const char *cb_target; /* the snapshot */ 3732b12a1c38Slling uint64_t cb_create; /* creation time reference */ 3733c391e322Sahrens boolean_t cb_error; 3734c391e322Sahrens boolean_t cb_force; 3735b12a1c38Slling } rollback_data_t; 3736b12a1c38Slling 3737b12a1c38Slling static int 373878f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 3739b12a1c38Slling { 3740b12a1c38Slling rollback_data_t *cbp = data; 374178f17100SMatthew Ahrens prop_changelist_t *clp; 374278f17100SMatthew Ahrens 374378f17100SMatthew Ahrens /* We must destroy this clone; first unmount it */ 374478f17100SMatthew Ahrens clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 374578f17100SMatthew Ahrens cbp->cb_force ? MS_FORCE: 0); 374678f17100SMatthew Ahrens if (clp == NULL || changelist_prefix(clp) != 0) { 374778f17100SMatthew Ahrens cbp->cb_error = B_TRUE; 374878f17100SMatthew Ahrens zfs_close(zhp); 374978f17100SMatthew Ahrens return (0); 375078f17100SMatthew Ahrens } 375178f17100SMatthew Ahrens if (zfs_destroy(zhp, B_FALSE) != 0) 375278f17100SMatthew Ahrens cbp->cb_error = B_TRUE; 375378f17100SMatthew Ahrens else 375478f17100SMatthew Ahrens changelist_remove(clp, zhp->zfs_name); 375578f17100SMatthew Ahrens (void) changelist_postfix(clp); 375678f17100SMatthew Ahrens changelist_free(clp); 3757b12a1c38Slling 375878f17100SMatthew Ahrens zfs_close(zhp); 375978f17100SMatthew Ahrens return (0); 376078f17100SMatthew Ahrens } 3761b12a1c38Slling 376278f17100SMatthew Ahrens static int 376378f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data) 376478f17100SMatthew Ahrens { 376578f17100SMatthew Ahrens rollback_data_t *cbp = data; 3766b12a1c38Slling 376778f17100SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 376878f17100SMatthew Ahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 376978f17100SMatthew Ahrens rollback_destroy_dependent, cbp); 3770c391e322Sahrens 377178f17100SMatthew Ahrens cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 3772b12a1c38Slling } 3773b12a1c38Slling 3774b12a1c38Slling zfs_close(zhp); 3775b12a1c38Slling return (0); 3776b12a1c38Slling } 3777b12a1c38Slling 3778b12a1c38Slling /* 3779b12a1c38Slling * Given a dataset, rollback to a specific snapshot, discarding any 3780b12a1c38Slling * data changes since then and making it the active dataset. 3781b12a1c38Slling * 378278f17100SMatthew Ahrens * Any snapshots and bookmarks more recent than the target are 378378f17100SMatthew Ahrens * destroyed, along with their dependents (i.e. clones). 3784b12a1c38Slling */ 3785b12a1c38Slling int 3786c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3787b12a1c38Slling { 3788b12a1c38Slling rollback_data_t cb = { 0 }; 37894ccbb6e7Sahrens int err; 37907b97dc1aSrm boolean_t restore_resv = 0; 3791f83b46baSPaul Dagnelie uint64_t old_volsize = 0, new_volsize; 37927b97dc1aSrm zfs_prop_t resv_prop; 3793b12a1c38Slling 37944ccbb6e7Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 37954ccbb6e7Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3796b12a1c38Slling 3797b12a1c38Slling /* 37982e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents. 3799b12a1c38Slling */ 3800c391e322Sahrens cb.cb_force = force; 3801b12a1c38Slling cb.cb_target = snap->zfs_name; 3802b12a1c38Slling cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 38030d8fa8f8SMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb); 380478f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); 3805b12a1c38Slling 3806c391e322Sahrens if (cb.cb_error) 3807c391e322Sahrens return (-1); 3808b12a1c38Slling 3809b12a1c38Slling /* 3810b12a1c38Slling * Now that we have verified that the snapshot is the latest, 3811b12a1c38Slling * rollback to the given snapshot. 3812b12a1c38Slling */ 3813b12a1c38Slling 38147b97dc1aSrm if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 38157b97dc1aSrm if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 38167b97dc1aSrm return (-1); 38177b97dc1aSrm old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38187b97dc1aSrm restore_resv = 38197b97dc1aSrm (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 38207b97dc1aSrm } 38214ccbb6e7Sahrens 3822b12a1c38Slling /* 38234ccbb6e7Sahrens * We rely on zfs_iter_children() to verify that there are no 38244ccbb6e7Sahrens * newer snapshots for the given dataset. Therefore, we can 38254ccbb6e7Sahrens * simply pass the name on to the ioctl() call. There is still 38264ccbb6e7Sahrens * an unlikely race condition where the user has taken a 38274ccbb6e7Sahrens * snapshot since we verified that this was the most recent. 3828b12a1c38Slling */ 3829a7027df1SMatthew Ahrens err = lzc_rollback(zhp->zfs_name, NULL, 0); 3830a7027df1SMatthew Ahrens if (err != 0) { 38314ccbb6e7Sahrens (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 38324ccbb6e7Sahrens dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 38334ccbb6e7Sahrens zhp->zfs_name); 3834b9415e83Srm return (err); 3835b9415e83Srm } 38367b97dc1aSrm 38377b97dc1aSrm /* 38387b97dc1aSrm * For volumes, if the pre-rollback volsize matched the pre- 38397b97dc1aSrm * rollback reservation and the volsize has changed then set 38407b97dc1aSrm * the reservation property to the post-rollback volsize. 38417b97dc1aSrm * Make a new handle since the rollback closed the dataset. 38427b97dc1aSrm */ 3843b9415e83Srm if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 3844b9415e83Srm (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 38457b97dc1aSrm if (restore_resv) { 38467b97dc1aSrm new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38477b97dc1aSrm if (old_volsize != new_volsize) 3848b9415e83Srm err = zfs_prop_set_int(zhp, resv_prop, 3849b9415e83Srm new_volsize); 38507b97dc1aSrm } 38517b97dc1aSrm zfs_close(zhp); 38524ccbb6e7Sahrens } 38534ccbb6e7Sahrens return (err); 3854b12a1c38Slling } 3855b12a1c38Slling 3856fa9e4066Sahrens /* 3857fa9e4066Sahrens * Renames the given dataset. 3858fa9e4066Sahrens */ 3859fa9e4066Sahrens int 38606a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, 38616a9cb0eaSEric Schrock boolean_t force_unmount) 3862fa9e4066Sahrens { 386388f61deeSIgor Kozhukhov int ret = 0; 3864fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3865fa9e4066Sahrens char *delim; 3866cdf5b4caSmmusante prop_changelist_t *cl = NULL; 3867cdf5b4caSmmusante zfs_handle_t *zhrp = NULL; 3868cdf5b4caSmmusante char *parentname = NULL; 3869*9adfa60dSMatthew Ahrens char parent[ZFS_MAX_DATASET_NAME_LEN]; 387099653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 387199653d4eSeschrock char errbuf[1024]; 3872fa9e4066Sahrens 3873fa9e4066Sahrens /* if we have the same exact name, just return success */ 3874fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3875fa9e4066Sahrens return (0); 3876fa9e4066Sahrens 387799653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 387899653d4eSeschrock "cannot rename to '%s'"), target); 387999653d4eSeschrock 3880fa9e4066Sahrens /* 3881fa9e4066Sahrens * Make sure the target name is valid 3882fa9e4066Sahrens */ 3883fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 388498579b20Snd if ((strchr(target, '@') == NULL) || 388598579b20Snd *target == '@') { 388698579b20Snd /* 388798579b20Snd * Snapshot target name is abbreviated, 388898579b20Snd * reconstruct full dataset name 388998579b20Snd */ 389098579b20Snd (void) strlcpy(parent, zhp->zfs_name, 389198579b20Snd sizeof (parent)); 389298579b20Snd delim = strchr(parent, '@'); 389398579b20Snd if (strchr(target, '@') == NULL) 389498579b20Snd *(++delim) = '\0'; 389598579b20Snd else 389698579b20Snd *delim = '\0'; 389798579b20Snd (void) strlcat(parent, target, sizeof (parent)); 389898579b20Snd target = parent; 389998579b20Snd } else { 390098579b20Snd /* 390198579b20Snd * Make sure we're renaming within the same dataset. 390298579b20Snd */ 390398579b20Snd delim = strchr(target, '@'); 390498579b20Snd if (strncmp(zhp->zfs_name, target, delim - target) 390598579b20Snd != 0 || zhp->zfs_name[delim - target] != '@') { 390698579b20Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 390798579b20Snd "snapshots must be part of same " 390898579b20Snd "dataset")); 390998579b20Snd return (zfs_error(hdl, EZFS_CROSSTARGET, 3910b1b8ab34Slling errbuf)); 391198579b20Snd } 3912fa9e4066Sahrens } 3913f18faf3fSek if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 391498579b20Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3915fa9e4066Sahrens } else { 3916cdf5b4caSmmusante if (recursive) { 3917cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3918cdf5b4caSmmusante "recursive rename must be a snapshot")); 3919cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3920cdf5b4caSmmusante } 3921cdf5b4caSmmusante 3922f18faf3fSek if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 392398579b20Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3924e9dbad6fSeschrock 3925fa9e4066Sahrens /* validate parents */ 3926d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 3927fa9e4066Sahrens return (-1); 3928fa9e4066Sahrens 3929fa9e4066Sahrens /* make sure we're in the same pool */ 3930fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL); 3931fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3932fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') { 393399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 393499653d4eSeschrock "datasets must be within same pool")); 393599653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3936fa9e4066Sahrens } 3937f2fdf992Snd 3938f2fdf992Snd /* new name cannot be a child of the current dataset name */ 3939d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) { 3940f2fdf992Snd zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3941d41c4376SMark J Musante "New dataset name cannot be a descendant of " 3942f2fdf992Snd "current dataset name")); 3943f2fdf992Snd return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3944f2fdf992Snd } 3945fa9e4066Sahrens } 3946fa9e4066Sahrens 394799653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 394899653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 394999653d4eSeschrock 3950fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID && 3951fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 395299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 395399653d4eSeschrock "dataset is used in a non-global zone")); 395499653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3955fa9e4066Sahrens } 3956fa9e4066Sahrens 3957cdf5b4caSmmusante if (recursive) { 3958f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 3959f0c5ee21Smmusante if (parentname == NULL) { 3960f0c5ee21Smmusante ret = -1; 3961f0c5ee21Smmusante goto error; 3962f0c5ee21Smmusante } 3963cdf5b4caSmmusante delim = strchr(parentname, '@'); 3964cdf5b4caSmmusante *delim = '\0'; 3965990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 3966cdf5b4caSmmusante if (zhrp == NULL) { 3967f0c5ee21Smmusante ret = -1; 3968f0c5ee21Smmusante goto error; 3969cdf5b4caSmmusante } 397033cde0d0SMatthew Ahrens } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 39716a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 39726a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL) 3973cdf5b4caSmmusante return (-1); 3974cdf5b4caSmmusante 3975cdf5b4caSmmusante if (changelist_haszonedchild(cl)) { 3976cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3977cdf5b4caSmmusante "child dataset with inherited mountpoint is used " 3978cdf5b4caSmmusante "in a non-global zone")); 3979cdf5b4caSmmusante (void) zfs_error(hdl, EZFS_ZONED, errbuf); 3980f83b46baSPaul Dagnelie ret = -1; 3981cdf5b4caSmmusante goto error; 3982cdf5b4caSmmusante } 3983cdf5b4caSmmusante 3984cdf5b4caSmmusante if ((ret = changelist_prefix(cl)) != 0) 3985cdf5b4caSmmusante goto error; 3986cdf5b4caSmmusante } 3987fa9e4066Sahrens 3988e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 3989fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3990fa9e4066Sahrens else 3991fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3992fa9e4066Sahrens 399398579b20Snd (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3994e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 399598579b20Snd 3996cdf5b4caSmmusante zc.zc_cookie = recursive; 3997cdf5b4caSmmusante 3998ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 3999cdf5b4caSmmusante /* 4000cdf5b4caSmmusante * if it was recursive, the one that actually failed will 4001cdf5b4caSmmusante * be in zc.zc_name 4002cdf5b4caSmmusante */ 4003cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 40043cb34c60Sahrens "cannot rename '%s'"), zc.zc_name); 4005cdf5b4caSmmusante 4006cdf5b4caSmmusante if (recursive && errno == EEXIST) { 4007cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4008cdf5b4caSmmusante "a child dataset already has a snapshot " 4009cdf5b4caSmmusante "with the new name")); 4010a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 4011cdf5b4caSmmusante } else { 4012cdf5b4caSmmusante (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 4013cdf5b4caSmmusante } 4014fa9e4066Sahrens 4015fa9e4066Sahrens /* 4016fa9e4066Sahrens * On failure, we still want to remount any filesystems that 4017fa9e4066Sahrens * were previously mounted, so we don't alter the system state. 4018fa9e4066Sahrens */ 401933cde0d0SMatthew Ahrens if (cl != NULL) 4020cdf5b4caSmmusante (void) changelist_postfix(cl); 4021fa9e4066Sahrens } else { 402233cde0d0SMatthew Ahrens if (cl != NULL) { 4023cdf5b4caSmmusante changelist_rename(cl, zfs_get_name(zhp), target); 4024cdf5b4caSmmusante ret = changelist_postfix(cl); 4025cdf5b4caSmmusante } 4026fa9e4066Sahrens } 4027fa9e4066Sahrens 4028fa9e4066Sahrens error: 402933cde0d0SMatthew Ahrens if (parentname != NULL) { 4030cdf5b4caSmmusante free(parentname); 4031cdf5b4caSmmusante } 403233cde0d0SMatthew Ahrens if (zhrp != NULL) { 4033cdf5b4caSmmusante zfs_close(zhrp); 4034cdf5b4caSmmusante } 403533cde0d0SMatthew Ahrens if (cl != NULL) { 4036cdf5b4caSmmusante changelist_free(cl); 4037cdf5b4caSmmusante } 4038fa9e4066Sahrens return (ret); 4039fa9e4066Sahrens } 4040fa9e4066Sahrens 4041e9dbad6fSeschrock nvlist_t * 4042e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp) 4043e9dbad6fSeschrock { 4044e9dbad6fSeschrock return (zhp->zfs_user_props); 4045e9dbad6fSeschrock } 4046e9dbad6fSeschrock 404792241e0bSTom Erickson nvlist_t * 404892241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp) 404992241e0bSTom Erickson { 405092241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 405192241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 405292241e0bSTom Erickson return (NULL); 405392241e0bSTom Erickson return (zhp->zfs_recvd_props); 405492241e0bSTom Erickson } 405592241e0bSTom Erickson 4056b1b8ab34Slling /* 4057b1b8ab34Slling * This function is used by 'zfs list' to determine the exact set of columns to 4058b1b8ab34Slling * display, and their maximum widths. This does two main things: 4059b1b8ab34Slling * 4060b1b8ab34Slling * - If this is a list of all properties, then expand the list to include 4061b1b8ab34Slling * all native properties, and set a flag so that for each dataset we look 4062b1b8ab34Slling * for new unique user properties and add them to the list. 4063b1b8ab34Slling * 4064b1b8ab34Slling * - For non fixed-width properties, keep track of the maximum width seen 406592241e0bSTom Erickson * so that we can size the column appropriately. If the user has 406692241e0bSTom Erickson * requested received property values, we also need to compute the width 406792241e0bSTom Erickson * of the RECEIVED column. 4068b1b8ab34Slling */ 4069b1b8ab34Slling int 407043d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 407143d68d68SYuri Pankov boolean_t literal) 4072b1b8ab34Slling { 4073b1b8ab34Slling libzfs_handle_t *hdl = zhp->zfs_hdl; 4074990b4856Slling zprop_list_t *entry; 4075990b4856Slling zprop_list_t **last, **start; 4076b1b8ab34Slling nvlist_t *userprops, *propval; 4077b1b8ab34Slling nvpair_t *elem; 4078b1b8ab34Slling char *strval; 4079b1b8ab34Slling char buf[ZFS_MAXPROPLEN]; 4080b1b8ab34Slling 4081990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 4082b1b8ab34Slling return (-1); 4083e9dbad6fSeschrock 4084e9dbad6fSeschrock userprops = zfs_get_user_props(zhp); 4085e9dbad6fSeschrock 4086e9dbad6fSeschrock entry = *plp; 4087e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4088e9dbad6fSeschrock /* 4089e9dbad6fSeschrock * Go through and add any user properties as necessary. We 4090e9dbad6fSeschrock * start by incrementing our list pointer to the first 4091e9dbad6fSeschrock * non-native property. 4092e9dbad6fSeschrock */ 4093e9dbad6fSeschrock start = plp; 4094e9dbad6fSeschrock while (*start != NULL) { 4095990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL) 4096e9dbad6fSeschrock break; 4097e9dbad6fSeschrock start = &(*start)->pl_next; 4098e9dbad6fSeschrock } 4099e9dbad6fSeschrock 4100e9dbad6fSeschrock elem = NULL; 4101e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4102e9dbad6fSeschrock /* 4103e9dbad6fSeschrock * See if we've already found this property in our list. 4104e9dbad6fSeschrock */ 4105e9dbad6fSeschrock for (last = start; *last != NULL; 4106e9dbad6fSeschrock last = &(*last)->pl_next) { 4107e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop, 4108e9dbad6fSeschrock nvpair_name(elem)) == 0) 4109e9dbad6fSeschrock break; 4110e9dbad6fSeschrock } 4111e9dbad6fSeschrock 4112e9dbad6fSeschrock if (*last == NULL) { 4113e9dbad6fSeschrock if ((entry = zfs_alloc(hdl, 4114990b4856Slling sizeof (zprop_list_t))) == NULL || 4115e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl, 4116e9dbad6fSeschrock nvpair_name(elem)))) == NULL) { 4117e9dbad6fSeschrock free(entry); 4118e9dbad6fSeschrock return (-1); 4119e9dbad6fSeschrock } 4120e9dbad6fSeschrock 4121990b4856Slling entry->pl_prop = ZPROP_INVAL; 4122e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem)); 4123e9dbad6fSeschrock entry->pl_all = B_TRUE; 4124e9dbad6fSeschrock *last = entry; 4125e9dbad6fSeschrock } 4126e9dbad6fSeschrock } 4127e9dbad6fSeschrock } 4128e9dbad6fSeschrock 4129e9dbad6fSeschrock /* 4130e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns 4131e9dbad6fSeschrock */ 4132e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 413343d68d68SYuri Pankov if (entry->pl_fixed && !literal) 4134e9dbad6fSeschrock continue; 4135e9dbad6fSeschrock 4136990b4856Slling if (entry->pl_prop != ZPROP_INVAL) { 4137e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop, 413843d68d68SYuri Pankov buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 4139e9dbad6fSeschrock if (strlen(buf) > entry->pl_width) 4140e9dbad6fSeschrock entry->pl_width = strlen(buf); 4141e9dbad6fSeschrock } 414292241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 414392241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop), 414443d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 414592241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 414692241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 414792241e0bSTom Erickson } else { 414892241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 414992241e0bSTom Erickson &propval) == 0) { 415092241e0bSTom Erickson verify(nvlist_lookup_string(propval, 415192241e0bSTom Erickson ZPROP_VALUE, &strval) == 0); 415292241e0bSTom Erickson if (strlen(strval) > entry->pl_width) 415392241e0bSTom Erickson entry->pl_width = strlen(strval); 415492241e0bSTom Erickson } 415592241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 415692241e0bSTom Erickson entry->pl_user_prop, 415743d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 415892241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 415992241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 4160e9dbad6fSeschrock } 4161e9dbad6fSeschrock } 4162e9dbad6fSeschrock 4163e9dbad6fSeschrock return (0); 4164e9dbad6fSeschrock } 4165ecd6cf80Smarks 4166ecd6cf80Smarks int 4167ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4168743a77edSAlan Wright char *resource, void *export, void *sharetab, 4169743a77edSAlan Wright int sharemax, zfs_share_op_t operation) 4170ecd6cf80Smarks { 4171ecd6cf80Smarks zfs_cmd_t zc = { 0 }; 4172ecd6cf80Smarks int error; 4173ecd6cf80Smarks 4174ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4175ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4176743a77edSAlan Wright if (resource) 4177743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 4178ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4179ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4180da6c28aaSamw zc.zc_share.z_sharetype = operation; 4181ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax; 4182ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4183ecd6cf80Smarks return (error); 4184ecd6cf80Smarks } 41852e5e9e19SSanjeev Bagewadi 41862e5e9e19SSanjeev Bagewadi void 41872e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 41882e5e9e19SSanjeev Bagewadi { 41892e5e9e19SSanjeev Bagewadi nvpair_t *curr; 41902e5e9e19SSanjeev Bagewadi 41912e5e9e19SSanjeev Bagewadi /* 41922e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the 41932e5e9e19SSanjeev Bagewadi * properties. 41942e5e9e19SSanjeev Bagewadi */ 41952e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props; 41962e5e9e19SSanjeev Bagewadi 41972e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 41982e5e9e19SSanjeev Bagewadi 41992e5e9e19SSanjeev Bagewadi while (curr) { 42002e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 42012e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 42022e5e9e19SSanjeev Bagewadi 420314843421SMatthew Ahrens /* 4204faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we 4205faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always 4206faaa6415SEric Schrock * leave these in the list. This can also happen if we 4207faaa6415SEric Schrock * encounter an unknown DSL property (when running older 4208faaa6415SEric Schrock * software, for example). 420914843421SMatthew Ahrens */ 421014843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 42112e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props, 42122e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr)); 42132e5e9e19SSanjeev Bagewadi curr = next; 42142e5e9e19SSanjeev Bagewadi } 42152e5e9e19SSanjeev Bagewadi } 4216743a77edSAlan Wright 4217743a77edSAlan Wright static int 4218743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4219743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4220743a77edSAlan Wright { 4221743a77edSAlan Wright zfs_cmd_t zc = { 0 }; 4222743a77edSAlan Wright nvlist_t *nvlist = NULL; 4223743a77edSAlan Wright int error; 4224743a77edSAlan Wright 4225743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4226743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4227743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd; 4228743a77edSAlan Wright 4229743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) { 4230743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4231743a77edSAlan Wright (void) no_memory(hdl); 423230925561SChris Williamson return (0); 4233743a77edSAlan Wright } 4234743a77edSAlan Wright } 4235743a77edSAlan Wright 4236743a77edSAlan Wright switch (cmd) { 4237743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4238743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4239743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4240743a77edSAlan Wright break; 4241743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4242743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4243743a77edSAlan Wright resource1) != 0) { 4244743a77edSAlan Wright (void) no_memory(hdl); 4245743a77edSAlan Wright return (-1); 4246743a77edSAlan Wright } 4247743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4248743a77edSAlan Wright resource2) != 0) { 4249743a77edSAlan Wright (void) no_memory(hdl); 4250743a77edSAlan Wright return (-1); 4251743a77edSAlan Wright } 4252743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 4253743a77edSAlan Wright nvlist_free(nvlist); 4254743a77edSAlan Wright return (-1); 4255743a77edSAlan Wright } 4256743a77edSAlan Wright break; 4257743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4258743a77edSAlan Wright break; 4259743a77edSAlan Wright default: 4260743a77edSAlan Wright return (-1); 4261743a77edSAlan Wright } 4262743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4263aab83bb8SJosef 'Jeff' Sipek nvlist_free(nvlist); 4264743a77edSAlan Wright return (error); 4265743a77edSAlan Wright } 4266743a77edSAlan Wright 4267743a77edSAlan Wright int 4268743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4269743a77edSAlan Wright char *path, char *resource) 4270743a77edSAlan Wright { 4271743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4272743a77edSAlan Wright resource, NULL)); 4273743a77edSAlan Wright } 4274743a77edSAlan Wright 4275743a77edSAlan Wright int 4276743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4277743a77edSAlan Wright char *path, char *resource) 4278743a77edSAlan Wright { 4279743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4280743a77edSAlan Wright resource, NULL)); 4281743a77edSAlan Wright } 4282743a77edSAlan Wright 4283743a77edSAlan Wright int 4284743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4285743a77edSAlan Wright { 4286743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4287743a77edSAlan Wright NULL, NULL)); 4288743a77edSAlan Wright } 4289743a77edSAlan Wright 4290743a77edSAlan Wright int 4291743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4292743a77edSAlan Wright char *oldname, char *newname) 4293743a77edSAlan Wright { 4294743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4295743a77edSAlan Wright oldname, newname)); 4296743a77edSAlan Wright } 429714843421SMatthew Ahrens 429814843421SMatthew Ahrens int 429914843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 430014843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg) 430114843421SMatthew Ahrens { 430214843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 430314843421SMatthew Ahrens zfs_useracct_t buf[100]; 430470f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl; 430570f56fa6SYuri Pankov int ret; 430614843421SMatthew Ahrens 430719b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 430814843421SMatthew Ahrens 430914843421SMatthew Ahrens zc.zc_objset_type = type; 431014843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf; 431114843421SMatthew Ahrens 431270f56fa6SYuri Pankov for (;;) { 431314843421SMatthew Ahrens zfs_useracct_t *zua = buf; 431414843421SMatthew Ahrens 431514843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf); 431670f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 43173b2aab18SMatthew Ahrens char errbuf[1024]; 431870f56fa6SYuri Pankov 431970f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf), 432070f56fa6SYuri Pankov dgettext(TEXT_DOMAIN, 432170f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name); 432270f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf)); 432370f56fa6SYuri Pankov } 432470f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0) 432514843421SMatthew Ahrens break; 432614843421SMatthew Ahrens 432714843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) { 432870f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 432970f56fa6SYuri Pankov zua->zu_space)) != 0) 433070f56fa6SYuri Pankov return (ret); 433114843421SMatthew Ahrens zua++; 433214843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 433314843421SMatthew Ahrens } 433414843421SMatthew Ahrens } 433514843421SMatthew Ahrens 433670f56fa6SYuri Pankov return (0); 433714843421SMatthew Ahrens } 4338842727c2SChris Kirby 43393b2aab18SMatthew Ahrens struct holdarg { 43403b2aab18SMatthew Ahrens nvlist_t *nvl; 43413b2aab18SMatthew Ahrens const char *snapname; 43423b2aab18SMatthew Ahrens const char *tag; 43433b2aab18SMatthew Ahrens boolean_t recursive; 4344bb6e7075SMatthew Ahrens int error; 43453b2aab18SMatthew Ahrens }; 43463b2aab18SMatthew Ahrens 43473b2aab18SMatthew Ahrens static int 43483b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg) 43493b2aab18SMatthew Ahrens { 43503b2aab18SMatthew Ahrens struct holdarg *ha = arg; 4351*9adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 43523b2aab18SMatthew Ahrens int rv = 0; 43533b2aab18SMatthew Ahrens 43543b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 43553b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 43563b2aab18SMatthew Ahrens 4357a7a845e4SSteven Hartland if (lzc_exists(name)) 43583b2aab18SMatthew Ahrens fnvlist_add_string(ha->nvl, name, ha->tag); 43593b2aab18SMatthew Ahrens 43603b2aab18SMatthew Ahrens if (ha->recursive) 43613b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); 43623b2aab18SMatthew Ahrens zfs_close(zhp); 43633b2aab18SMatthew Ahrens return (rv); 43643b2aab18SMatthew Ahrens } 43653b2aab18SMatthew Ahrens 4366842727c2SChris Kirby int 4367842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4368a7a845e4SSteven Hartland boolean_t recursive, int cleanup_fd) 4369842727c2SChris Kirby { 43703b2aab18SMatthew Ahrens int ret; 43713b2aab18SMatthew Ahrens struct holdarg ha; 4372842727c2SChris Kirby 43733b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 43743b2aab18SMatthew Ahrens ha.snapname = snapname; 43753b2aab18SMatthew Ahrens ha.tag = tag; 43763b2aab18SMatthew Ahrens ha.recursive = recursive; 43773b2aab18SMatthew Ahrens (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4378013023d4SMartin Matuska 4379a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4380a7a845e4SSteven Hartland char errbuf[1024]; 4381a7a845e4SSteven Hartland 4382013023d4SMartin Matuska fnvlist_free(ha.nvl); 4383013023d4SMartin Matuska ret = ENOENT; 4384a7a845e4SSteven Hartland (void) snprintf(errbuf, sizeof (errbuf), 4385a7a845e4SSteven Hartland dgettext(TEXT_DOMAIN, 4386a7a845e4SSteven Hartland "cannot hold snapshot '%s@%s'"), 4387a7a845e4SSteven Hartland zhp->zfs_name, snapname); 4388a7a845e4SSteven Hartland (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 4389013023d4SMartin Matuska return (ret); 4390013023d4SMartin Matuska } 4391013023d4SMartin Matuska 4392a7a845e4SSteven Hartland ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 43933b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4394a7f53a56SChris Kirby 4395a7a845e4SSteven Hartland return (ret); 4396a7a845e4SSteven Hartland } 4397a7a845e4SSteven Hartland 4398a7a845e4SSteven Hartland int 4399a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 4400a7a845e4SSteven Hartland { 4401a7a845e4SSteven Hartland int ret; 4402a7a845e4SSteven Hartland nvlist_t *errors; 4403a7a845e4SSteven Hartland libzfs_handle_t *hdl = zhp->zfs_hdl; 4404a7a845e4SSteven Hartland char errbuf[1024]; 4405a7a845e4SSteven Hartland nvpair_t *elem; 4406a7a845e4SSteven Hartland 4407a7a845e4SSteven Hartland errors = NULL; 4408a7a845e4SSteven Hartland ret = lzc_hold(holds, cleanup_fd, &errors); 4409a7a845e4SSteven Hartland 4410a7a845e4SSteven Hartland if (ret == 0) { 4411a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4412a7a845e4SSteven Hartland fnvlist_free(errors); 44133b2aab18SMatthew Ahrens return (0); 4414a7a845e4SSteven Hartland } 4415842727c2SChris Kirby 4416a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 44173b2aab18SMatthew Ahrens /* no hold-specific errors */ 44183b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 44193b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot hold")); 44203b2aab18SMatthew Ahrens switch (ret) { 44213b2aab18SMatthew Ahrens case ENOTSUP: 44223b2aab18SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44233b2aab18SMatthew Ahrens "pool must be upgraded")); 44243b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 44253b2aab18SMatthew Ahrens break; 44263b2aab18SMatthew Ahrens case EINVAL: 44273b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 44283b2aab18SMatthew Ahrens break; 44293b2aab18SMatthew Ahrens default: 44303b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 44313b2aab18SMatthew Ahrens } 44323b2aab18SMatthew Ahrens } 4433842727c2SChris Kirby 44343b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 44353b2aab18SMatthew Ahrens elem != NULL; 44363b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 44373b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 44383b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 44393b2aab18SMatthew Ahrens "cannot hold snapshot '%s'"), nvpair_name(elem)); 44403b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 444115508ac0SChris Kirby case E2BIG: 444215508ac0SChris Kirby /* 444315508ac0SChris Kirby * Temporary tags wind up having the ds object id 444415508ac0SChris Kirby * prepended. So even if we passed the length check 444515508ac0SChris Kirby * above, it's still possible for the tag to wind 444615508ac0SChris Kirby * up being slightly too long. 444715508ac0SChris Kirby */ 44483b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 44493b2aab18SMatthew Ahrens break; 4450842727c2SChris Kirby case EINVAL: 44513b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 44523b2aab18SMatthew Ahrens break; 4453842727c2SChris Kirby case EEXIST: 44543b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 44553b2aab18SMatthew Ahrens break; 4456842727c2SChris Kirby default: 44573b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, 44583b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4459842727c2SChris Kirby } 4460842727c2SChris Kirby } 4461842727c2SChris Kirby 44623b2aab18SMatthew Ahrens fnvlist_free(errors); 44633b2aab18SMatthew Ahrens return (ret); 44643b2aab18SMatthew Ahrens } 44653b2aab18SMatthew Ahrens 44663b2aab18SMatthew Ahrens static int 44673b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg) 44683b2aab18SMatthew Ahrens { 44693b2aab18SMatthew Ahrens struct holdarg *ha = arg; 4470*9adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN]; 44713b2aab18SMatthew Ahrens int rv = 0; 4472bb6e7075SMatthew Ahrens nvlist_t *existing_holds; 44733b2aab18SMatthew Ahrens 44743b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 44753b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 44763b2aab18SMatthew Ahrens 4477bb6e7075SMatthew Ahrens if (lzc_get_holds(name, &existing_holds) != 0) { 4478bb6e7075SMatthew Ahrens ha->error = ENOENT; 4479bb6e7075SMatthew Ahrens } else if (!nvlist_exists(existing_holds, ha->tag)) { 4480bb6e7075SMatthew Ahrens ha->error = ESRCH; 4481bb6e7075SMatthew Ahrens } else { 4482bb6e7075SMatthew Ahrens nvlist_t *torelease = fnvlist_alloc(); 4483bb6e7075SMatthew Ahrens fnvlist_add_boolean(torelease, ha->tag); 4484bb6e7075SMatthew Ahrens fnvlist_add_nvlist(ha->nvl, name, torelease); 4485bb6e7075SMatthew Ahrens fnvlist_free(torelease); 44863b2aab18SMatthew Ahrens } 44873b2aab18SMatthew Ahrens 44883b2aab18SMatthew Ahrens if (ha->recursive) 44893b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); 44903b2aab18SMatthew Ahrens zfs_close(zhp); 44913b2aab18SMatthew Ahrens return (rv); 4492842727c2SChris Kirby } 4493842727c2SChris Kirby 4494842727c2SChris Kirby int 4495842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 4496842727c2SChris Kirby boolean_t recursive) 4497842727c2SChris Kirby { 44983b2aab18SMatthew Ahrens int ret; 44993b2aab18SMatthew Ahrens struct holdarg ha; 4500a7a845e4SSteven Hartland nvlist_t *errors = NULL; 45013b2aab18SMatthew Ahrens nvpair_t *elem; 4502842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4503013023d4SMartin Matuska char errbuf[1024]; 4504842727c2SChris Kirby 45053b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 45063b2aab18SMatthew Ahrens ha.snapname = snapname; 45073b2aab18SMatthew Ahrens ha.tag = tag; 45083b2aab18SMatthew Ahrens ha.recursive = recursive; 4509bb6e7075SMatthew Ahrens ha.error = 0; 45103b2aab18SMatthew Ahrens (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 4511013023d4SMartin Matuska 4512a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4513013023d4SMartin Matuska fnvlist_free(ha.nvl); 4514bb6e7075SMatthew Ahrens ret = ha.error; 4515013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4516013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4517013023d4SMartin Matuska "cannot release hold from snapshot '%s@%s'"), 4518013023d4SMartin Matuska zhp->zfs_name, snapname); 4519bb6e7075SMatthew Ahrens if (ret == ESRCH) { 4520bb6e7075SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 4521bb6e7075SMatthew Ahrens } else { 4522bb6e7075SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 4523bb6e7075SMatthew Ahrens } 4524013023d4SMartin Matuska return (ret); 4525013023d4SMartin Matuska } 4526013023d4SMartin Matuska 45273b2aab18SMatthew Ahrens ret = lzc_release(ha.nvl, &errors); 45283b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4529842727c2SChris Kirby 4530a7a845e4SSteven Hartland if (ret == 0) { 4531a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4532a7a845e4SSteven Hartland fnvlist_free(errors); 45333b2aab18SMatthew Ahrens return (0); 4534a7a845e4SSteven Hartland } 45353b2aab18SMatthew Ahrens 4536a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 45373b2aab18SMatthew Ahrens /* no hold-specific errors */ 4538842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 45393b2aab18SMatthew Ahrens "cannot release")); 4540842727c2SChris Kirby switch (errno) { 4541842727c2SChris Kirby case ENOTSUP: 4542842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4543842727c2SChris Kirby "pool must be upgraded")); 45443b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 45453b2aab18SMatthew Ahrens break; 45463b2aab18SMatthew Ahrens default: 45473b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, errno, errbuf); 45483b2aab18SMatthew Ahrens } 45493b2aab18SMatthew Ahrens } 45503b2aab18SMatthew Ahrens 45513b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 45523b2aab18SMatthew Ahrens elem != NULL; 45533b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 45543b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 45553b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 45563b2aab18SMatthew Ahrens "cannot release hold from snapshot '%s'"), 45573b2aab18SMatthew Ahrens nvpair_name(elem)); 45583b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 45593b2aab18SMatthew Ahrens case ESRCH: 45603b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 45613b2aab18SMatthew Ahrens break; 4562842727c2SChris Kirby case EINVAL: 45633b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 45643b2aab18SMatthew Ahrens break; 4565842727c2SChris Kirby default: 45663b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, 45673b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4568842727c2SChris Kirby } 4569842727c2SChris Kirby } 4570842727c2SChris Kirby 45713b2aab18SMatthew Ahrens fnvlist_free(errors); 45723b2aab18SMatthew Ahrens return (ret); 4573842727c2SChris Kirby } 4574ca45db41SChris Kirby 45751af68beaSAlexander Stetsenko int 45761af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 45771af68beaSAlexander Stetsenko { 45781af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 45791af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 45801af68beaSAlexander Stetsenko int nvsz = 2048; 45811af68beaSAlexander Stetsenko void *nvbuf; 45821af68beaSAlexander Stetsenko int err = 0; 45833b2aab18SMatthew Ahrens char errbuf[1024]; 45841af68beaSAlexander Stetsenko 45851af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 45861af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 45871af68beaSAlexander Stetsenko 45881af68beaSAlexander Stetsenko tryagain: 45891af68beaSAlexander Stetsenko 45901af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 45911af68beaSAlexander Stetsenko if (nvbuf == NULL) { 45921af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 45931af68beaSAlexander Stetsenko goto out; 45941af68beaSAlexander Stetsenko } 45951af68beaSAlexander Stetsenko 45961af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 45971af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 45981af68beaSAlexander Stetsenko 4599*9adfa60dSMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 46001af68beaSAlexander Stetsenko 4601d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 46021af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 46031af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 46041af68beaSAlexander Stetsenko zc.zc_name); 46051af68beaSAlexander Stetsenko switch (errno) { 46061af68beaSAlexander Stetsenko case ENOMEM: 46071af68beaSAlexander Stetsenko free(nvbuf); 46081af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 46091af68beaSAlexander Stetsenko goto tryagain; 46101af68beaSAlexander Stetsenko 46111af68beaSAlexander Stetsenko case ENOTSUP: 46121af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46131af68beaSAlexander Stetsenko "pool must be upgraded")); 46141af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 46151af68beaSAlexander Stetsenko break; 46161af68beaSAlexander Stetsenko case EINVAL: 46171af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 46181af68beaSAlexander Stetsenko break; 46191af68beaSAlexander Stetsenko case ENOENT: 46201af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 46211af68beaSAlexander Stetsenko break; 46221af68beaSAlexander Stetsenko default: 46231af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 46241af68beaSAlexander Stetsenko break; 46251af68beaSAlexander Stetsenko } 46261af68beaSAlexander Stetsenko } else { 46271af68beaSAlexander Stetsenko /* success */ 46281af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 46291af68beaSAlexander Stetsenko if (rc) { 46301af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext( 46311af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"), 46321af68beaSAlexander Stetsenko zc.zc_name); 46331af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 46341af68beaSAlexander Stetsenko } 46351af68beaSAlexander Stetsenko } 46361af68beaSAlexander Stetsenko 46371af68beaSAlexander Stetsenko free(nvbuf); 46381af68beaSAlexander Stetsenko out: 46391af68beaSAlexander Stetsenko return (err); 46401af68beaSAlexander Stetsenko } 46411af68beaSAlexander Stetsenko 46421af68beaSAlexander Stetsenko int 46431af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 46441af68beaSAlexander Stetsenko { 46451af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 46461af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 46471af68beaSAlexander Stetsenko char *nvbuf; 46483b2aab18SMatthew Ahrens char errbuf[1024]; 46491af68beaSAlexander Stetsenko size_t nvsz; 46501af68beaSAlexander Stetsenko int err; 46511af68beaSAlexander Stetsenko 46521af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 46531af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 46541af68beaSAlexander Stetsenko 46551af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 46561af68beaSAlexander Stetsenko assert(err == 0); 46571af68beaSAlexander Stetsenko 46581af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 46591af68beaSAlexander Stetsenko 46601af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 46611af68beaSAlexander Stetsenko assert(err == 0); 46621af68beaSAlexander Stetsenko 46631af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz; 46641af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf; 46651af68beaSAlexander Stetsenko zc.zc_perm_action = un; 46661af68beaSAlexander Stetsenko 46671af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 46681af68beaSAlexander Stetsenko 46691af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 46701af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 46711af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 46721af68beaSAlexander Stetsenko zc.zc_name); 46731af68beaSAlexander Stetsenko switch (errno) { 46741af68beaSAlexander Stetsenko case ENOTSUP: 46751af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46761af68beaSAlexander Stetsenko "pool must be upgraded")); 46771af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 46781af68beaSAlexander Stetsenko break; 46791af68beaSAlexander Stetsenko case EINVAL: 46801af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 46811af68beaSAlexander Stetsenko break; 46821af68beaSAlexander Stetsenko case ENOENT: 46831af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 46841af68beaSAlexander Stetsenko break; 46851af68beaSAlexander Stetsenko default: 46861af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 46871af68beaSAlexander Stetsenko break; 46881af68beaSAlexander Stetsenko } 46891af68beaSAlexander Stetsenko } 46901af68beaSAlexander Stetsenko 46911af68beaSAlexander Stetsenko free(nvbuf); 46921af68beaSAlexander Stetsenko 46931af68beaSAlexander Stetsenko return (err); 46941af68beaSAlexander Stetsenko } 46951af68beaSAlexander Stetsenko 46961af68beaSAlexander Stetsenko int 46971af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 46981af68beaSAlexander Stetsenko { 46993b2aab18SMatthew Ahrens int err; 47003b2aab18SMatthew Ahrens char errbuf[1024]; 47011af68beaSAlexander Stetsenko 47023b2aab18SMatthew Ahrens err = lzc_get_holds(zhp->zfs_name, nvl); 47031af68beaSAlexander Stetsenko 47043b2aab18SMatthew Ahrens if (err != 0) { 47053b2aab18SMatthew Ahrens libzfs_handle_t *hdl = zhp->zfs_hdl; 47061af68beaSAlexander Stetsenko 47071af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 47081af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 47093b2aab18SMatthew Ahrens zhp->zfs_name); 47103b2aab18SMatthew Ahrens switch (err) { 47111af68beaSAlexander Stetsenko case ENOTSUP: 47121af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 47131af68beaSAlexander Stetsenko "pool must be upgraded")); 47141af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 47151af68beaSAlexander Stetsenko break; 47161af68beaSAlexander Stetsenko case EINVAL: 47171af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 47181af68beaSAlexander Stetsenko break; 47191af68beaSAlexander Stetsenko case ENOENT: 47201af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 47211af68beaSAlexander Stetsenko break; 47221af68beaSAlexander Stetsenko default: 47231af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 47241af68beaSAlexander Stetsenko break; 47251af68beaSAlexander Stetsenko } 47261af68beaSAlexander Stetsenko } 47271af68beaSAlexander Stetsenko 47281af68beaSAlexander Stetsenko return (err); 47291af68beaSAlexander Stetsenko } 47301af68beaSAlexander Stetsenko 47313e30c24aSWill Andrews /* 47323e30c24aSWill Andrews * Convert the zvol's volume size to an appropriate reservation. 47333e30c24aSWill Andrews * Note: If this routine is updated, it is necessary to update the ZFS test 47343e30c24aSWill Andrews * suite's shell version in reservation.kshlib. 47353e30c24aSWill Andrews */ 4736c1449561SEric Taylor uint64_t 4737c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props) 4738c1449561SEric Taylor { 4739c1449561SEric Taylor uint64_t numdb; 4740c1449561SEric Taylor uint64_t nblocks, volblocksize; 4741c1449561SEric Taylor int ncopies; 4742c1449561SEric Taylor char *strval; 4743c1449561SEric Taylor 4744c1449561SEric Taylor if (nvlist_lookup_string(props, 4745c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 4746c1449561SEric Taylor ncopies = atoi(strval); 4747c1449561SEric Taylor else 4748c1449561SEric Taylor ncopies = 1; 4749c1449561SEric Taylor if (nvlist_lookup_uint64(props, 4750c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 4751c1449561SEric Taylor &volblocksize) != 0) 4752c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 4753c1449561SEric Taylor nblocks = volsize/volblocksize; 4754c1449561SEric Taylor /* start with metadnode L0-L6 */ 4755c1449561SEric Taylor numdb = 7; 4756c1449561SEric Taylor /* calculate number of indirects */ 4757c1449561SEric Taylor while (nblocks > 1) { 4758c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1; 4759c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL; 4760c1449561SEric Taylor numdb += nblocks; 4761c1449561SEric Taylor } 4762c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 4763c1449561SEric Taylor volsize *= ncopies; 4764c1449561SEric Taylor /* 4765c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 4766c1449561SEric Taylor * compressed, but in practice they compress down to about 4767c1449561SEric Taylor * 1100 bytes 4768c1449561SEric Taylor */ 4769c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 4770c1449561SEric Taylor volsize += numdb; 4771c1449561SEric Taylor return (volsize); 4772c1449561SEric Taylor } 4773