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. 24d7f601efSGeorge Wilson * Copyright (c) 2012 by Delphix. All rights reserved. 25f0f3ef5aSGarrett D'Amore * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. 26*70f56fa6SYuri Pankov * Copyright 2012 Nexenta Systems, Inc. All rights reserved. 27fa9e4066Sahrens */ 28fa9e4066Sahrens 29fa9e4066Sahrens #include <ctype.h> 30fa9e4066Sahrens #include <errno.h> 31fa9e4066Sahrens #include <libintl.h> 32fa9e4066Sahrens #include <math.h> 33fa9e4066Sahrens #include <stdio.h> 34fa9e4066Sahrens #include <stdlib.h> 35fa9e4066Sahrens #include <strings.h> 36fa9e4066Sahrens #include <unistd.h> 373cb34c60Sahrens #include <stddef.h> 38fa9e4066Sahrens #include <zone.h> 3999653d4eSeschrock #include <fcntl.h> 40fa9e4066Sahrens #include <sys/mntent.h> 41b12a1c38Slling #include <sys/mount.h> 42ecd6cf80Smarks #include <priv.h> 43ecd6cf80Smarks #include <pwd.h> 44ecd6cf80Smarks #include <grp.h> 45ecd6cf80Smarks #include <stddef.h> 46ecd6cf80Smarks #include <ucred.h> 4714843421SMatthew Ahrens #include <idmap.h> 4814843421SMatthew Ahrens #include <aclutils.h> 493b12c289SMatthew Ahrens #include <directory.h> 50fa9e4066Sahrens 51c1449561SEric Taylor #include <sys/dnode.h> 52fa9e4066Sahrens #include <sys/spa.h> 53e9dbad6fSeschrock #include <sys/zap.h> 54fa9e4066Sahrens #include <libzfs.h> 55fa9e4066Sahrens 56fa9e4066Sahrens #include "zfs_namecheck.h" 57fa9e4066Sahrens #include "zfs_prop.h" 58fa9e4066Sahrens #include "libzfs_impl.h" 59ecd6cf80Smarks #include "zfs_deleg.h" 60fa9e4066Sahrens 6114843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned, 6214843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 63cdf5b4caSmmusante 64fa9e4066Sahrens /* 65fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human 66fa9e4066Sahrens * readable form. 67fa9e4066Sahrens */ 68fa9e4066Sahrens const char * 69fa9e4066Sahrens zfs_type_to_name(zfs_type_t type) 70fa9e4066Sahrens { 71fa9e4066Sahrens switch (type) { 72fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 73fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 74fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 75fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 76fa9e4066Sahrens case ZFS_TYPE_VOLUME: 77fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 78fa9e4066Sahrens } 79fa9e4066Sahrens 80fa9e4066Sahrens return (NULL); 81fa9e4066Sahrens } 82fa9e4066Sahrens 83fa9e4066Sahrens /* 84fa9e4066Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 85fa9e4066Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 86fa9e4066Sahrens * We guess what the type would have been based on the path and the mask of 87fa9e4066Sahrens * acceptable types. 88fa9e4066Sahrens */ 89fa9e4066Sahrens static const char * 90fa9e4066Sahrens path_to_str(const char *path, int types) 91fa9e4066Sahrens { 92fa9e4066Sahrens /* 93fa9e4066Sahrens * When given a single type, always report the exact type. 94fa9e4066Sahrens */ 95fa9e4066Sahrens if (types == ZFS_TYPE_SNAPSHOT) 96fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 97fa9e4066Sahrens if (types == ZFS_TYPE_FILESYSTEM) 98fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 99fa9e4066Sahrens if (types == ZFS_TYPE_VOLUME) 100fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 101fa9e4066Sahrens 102fa9e4066Sahrens /* 103fa9e4066Sahrens * The user is requesting more than one type of dataset. If this is the 104fa9e4066Sahrens * case, consult the path itself. If we're looking for a snapshot, and 105fa9e4066Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 106fa9e4066Sahrens * snapshot attribute and try again. 107fa9e4066Sahrens */ 108fa9e4066Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 109fa9e4066Sahrens if (strchr(path, '@') != NULL) 110fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 111fa9e4066Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 112fa9e4066Sahrens } 113fa9e4066Sahrens 114fa9e4066Sahrens /* 115fa9e4066Sahrens * The user has requested either filesystems or volumes. 116fa9e4066Sahrens * We have no way of knowing a priori what type this would be, so always 117fa9e4066Sahrens * report it as "filesystem" or "volume", our two primitive types. 118fa9e4066Sahrens */ 119fa9e4066Sahrens if (types & ZFS_TYPE_FILESYSTEM) 120fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 121fa9e4066Sahrens 122fa9e4066Sahrens assert(types & ZFS_TYPE_VOLUME); 123fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 124fa9e4066Sahrens } 125fa9e4066Sahrens 126fa9e4066Sahrens /* 127fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 12814843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to 12914843421SMatthew Ahrens * explain exactly why the name was not valid. 130fa9e4066Sahrens */ 13199d5e173STim Haley int 132f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 133f18faf3fSek110237 boolean_t modifying) 134fa9e4066Sahrens { 135fa9e4066Sahrens namecheck_err_t why; 136fa9e4066Sahrens char what; 137fa9e4066Sahrens 1381af68beaSAlexander Stetsenko (void) zfs_prop_get_table(); 139fa9e4066Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 14099653d4eSeschrock if (hdl != NULL) { 141fa9e4066Sahrens switch (why) { 142b81d61a6Slling case NAME_ERR_TOOLONG: 14399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14499653d4eSeschrock "name is too long")); 145b81d61a6Slling break; 146b81d61a6Slling 147fa9e4066Sahrens case NAME_ERR_LEADING_SLASH: 14899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14999653d4eSeschrock "leading slash in name")); 150fa9e4066Sahrens break; 151fa9e4066Sahrens 152fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT: 15399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15499653d4eSeschrock "empty component in name")); 155fa9e4066Sahrens break; 156fa9e4066Sahrens 157fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH: 15899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15999653d4eSeschrock "trailing slash in name")); 160fa9e4066Sahrens break; 161fa9e4066Sahrens 162fa9e4066Sahrens case NAME_ERR_INVALCHAR: 16399653d4eSeschrock zfs_error_aux(hdl, 164fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 16599653d4eSeschrock "'%c' in name"), what); 166fa9e4066Sahrens break; 167fa9e4066Sahrens 168fa9e4066Sahrens case NAME_ERR_MULTIPLE_AT: 16999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17099653d4eSeschrock "multiple '@' delimiters in name")); 171fa9e4066Sahrens break; 1725ad82045Snd150628 1735ad82045Snd150628 case NAME_ERR_NOLETTER: 1745ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1755ad82045Snd150628 "pool doesn't begin with a letter")); 1765ad82045Snd150628 break; 1775ad82045Snd150628 1785ad82045Snd150628 case NAME_ERR_RESERVED: 1795ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1805ad82045Snd150628 "name is reserved")); 1815ad82045Snd150628 break; 1825ad82045Snd150628 1835ad82045Snd150628 case NAME_ERR_DISKLIKE: 1845ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1855ad82045Snd150628 "reserved disk name")); 1865ad82045Snd150628 break; 187fa9e4066Sahrens } 188fa9e4066Sahrens } 189fa9e4066Sahrens 190fa9e4066Sahrens return (0); 191fa9e4066Sahrens } 192fa9e4066Sahrens 193fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 19499653d4eSeschrock if (hdl != NULL) 19599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 19699653d4eSeschrock "snapshot delimiter '@' in filesystem name")); 197fa9e4066Sahrens return (0); 198fa9e4066Sahrens } 199fa9e4066Sahrens 2001d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2011d452cf5Sahrens if (hdl != NULL) 2021d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 203d7d4af51Smmusante "missing '@' delimiter in snapshot name")); 2041d452cf5Sahrens return (0); 2051d452cf5Sahrens } 2061d452cf5Sahrens 207f18faf3fSek110237 if (modifying && strchr(path, '%') != NULL) { 208f18faf3fSek110237 if (hdl != NULL) 209f18faf3fSek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 210f18faf3fSek110237 "invalid character %c in name"), '%'); 211f18faf3fSek110237 return (0); 212f18faf3fSek110237 } 213f18faf3fSek110237 21499653d4eSeschrock return (-1); 215fa9e4066Sahrens } 216fa9e4066Sahrens 217fa9e4066Sahrens int 218fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type) 219fa9e4066Sahrens { 220e7cbe64fSgw25295 if (type == ZFS_TYPE_POOL) 221e7cbe64fSgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 222f18faf3fSek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 223fa9e4066Sahrens } 224fa9e4066Sahrens 225fa9e4066Sahrens /* 226e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined 227e9dbad6fSeschrock * properties into a separate nvlist. 228e9dbad6fSeschrock */ 229fac3008cSeschrock static nvlist_t * 230fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 231e9dbad6fSeschrock { 232e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 233e9dbad6fSeschrock nvpair_t *elem; 234e9dbad6fSeschrock nvlist_t *propval; 235fac3008cSeschrock nvlist_t *nvl; 236e9dbad6fSeschrock 237fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 238fac3008cSeschrock (void) no_memory(hdl); 239fac3008cSeschrock return (NULL); 240fac3008cSeschrock } 241e9dbad6fSeschrock 242e9dbad6fSeschrock elem = NULL; 243fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 244e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem))) 245e9dbad6fSeschrock continue; 246e9dbad6fSeschrock 247e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 248fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 249fac3008cSeschrock nvlist_free(nvl); 250fac3008cSeschrock (void) no_memory(hdl); 251fac3008cSeschrock return (NULL); 252fac3008cSeschrock } 253e9dbad6fSeschrock } 254e9dbad6fSeschrock 255fac3008cSeschrock return (nvl); 256e9dbad6fSeschrock } 257e9dbad6fSeschrock 25829ab75c9Srm160521 static zpool_handle_t * 25929ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 26029ab75c9Srm160521 { 26129ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 26229ab75c9Srm160521 zpool_handle_t *zph; 26329ab75c9Srm160521 26429ab75c9Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 26529ab75c9Srm160521 if (hdl->libzfs_pool_handles != NULL) 26629ab75c9Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 26729ab75c9Srm160521 hdl->libzfs_pool_handles = zph; 26829ab75c9Srm160521 } 26929ab75c9Srm160521 return (zph); 27029ab75c9Srm160521 } 27129ab75c9Srm160521 27229ab75c9Srm160521 static zpool_handle_t * 27329ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 27429ab75c9Srm160521 { 27529ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 27629ab75c9Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 27729ab75c9Srm160521 27829ab75c9Srm160521 while ((zph != NULL) && 27929ab75c9Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 28029ab75c9Srm160521 zph = zph->zpool_next; 28129ab75c9Srm160521 return (zph); 28229ab75c9Srm160521 } 28329ab75c9Srm160521 28429ab75c9Srm160521 /* 28529ab75c9Srm160521 * Returns a handle to the pool that contains the provided dataset. 28629ab75c9Srm160521 * If a handle to that pool already exists then that handle is returned. 28729ab75c9Srm160521 * Otherwise, a new handle is created and added to the list of handles. 28829ab75c9Srm160521 */ 28929ab75c9Srm160521 static zpool_handle_t * 29029ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp) 29129ab75c9Srm160521 { 29229ab75c9Srm160521 char *pool_name; 29329ab75c9Srm160521 int len; 29429ab75c9Srm160521 zpool_handle_t *zph; 29529ab75c9Srm160521 29629ab75c9Srm160521 len = strcspn(zhp->zfs_name, "/@") + 1; 29729ab75c9Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 29829ab75c9Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 29929ab75c9Srm160521 30029ab75c9Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 30129ab75c9Srm160521 if (zph == NULL) 30229ab75c9Srm160521 zph = zpool_add_handle(zhp, pool_name); 30329ab75c9Srm160521 30429ab75c9Srm160521 free(pool_name); 30529ab75c9Srm160521 return (zph); 30629ab75c9Srm160521 } 30729ab75c9Srm160521 30829ab75c9Srm160521 void 30929ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 31029ab75c9Srm160521 { 31129ab75c9Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 31229ab75c9Srm160521 31329ab75c9Srm160521 while (zph != NULL) { 31429ab75c9Srm160521 next = zph->zpool_next; 31529ab75c9Srm160521 zpool_close(zph); 31629ab75c9Srm160521 zph = next; 31729ab75c9Srm160521 } 31829ab75c9Srm160521 hdl->libzfs_pool_handles = NULL; 31929ab75c9Srm160521 } 32029ab75c9Srm160521 321e9dbad6fSeschrock /* 322fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object. 323fa9e4066Sahrens */ 324fa9e4066Sahrens static int 325ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 326fa9e4066Sahrens { 327e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 328fa9e4066Sahrens 329ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 330fa9e4066Sahrens 331ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3327f7322feSeschrock if (errno == ENOMEM) { 333ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 33499653d4eSeschrock return (-1); 335e9dbad6fSeschrock } 3367f7322feSeschrock } else { 337fa9e4066Sahrens return (-1); 3387f7322feSeschrock } 3397f7322feSeschrock } 340ebedde84SEric Taylor return (0); 341fac3008cSeschrock } 342fac3008cSeschrock 34392241e0bSTom Erickson /* 34492241e0bSTom Erickson * Utility function to get the received properties of the given object. 34592241e0bSTom Erickson */ 34692241e0bSTom Erickson static int 34792241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp) 34892241e0bSTom Erickson { 34992241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl; 35092241e0bSTom Erickson nvlist_t *recvdprops; 35192241e0bSTom Erickson zfs_cmd_t zc = { 0 }; 35292241e0bSTom Erickson int err; 35392241e0bSTom Erickson 35492241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 35592241e0bSTom Erickson return (-1); 35692241e0bSTom Erickson 35792241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 35892241e0bSTom Erickson 35992241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 36092241e0bSTom Erickson if (errno == ENOMEM) { 36192241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 36292241e0bSTom Erickson return (-1); 36392241e0bSTom Erickson } 36492241e0bSTom Erickson } else { 36592241e0bSTom Erickson zcmd_free_nvlists(&zc); 36692241e0bSTom Erickson return (-1); 36792241e0bSTom Erickson } 36892241e0bSTom Erickson } 36992241e0bSTom Erickson 37092241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 37192241e0bSTom Erickson zcmd_free_nvlists(&zc); 37292241e0bSTom Erickson if (err != 0) 37392241e0bSTom Erickson return (-1); 37492241e0bSTom Erickson 37592241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 37692241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops; 37792241e0bSTom Erickson 37892241e0bSTom Erickson return (0); 37992241e0bSTom Erickson } 38092241e0bSTom Erickson 381ebedde84SEric Taylor static int 382ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 383ebedde84SEric Taylor { 384ebedde84SEric Taylor nvlist_t *allprops, *userprops; 385ebedde84SEric Taylor 386ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 387ebedde84SEric Taylor 388ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 389ebedde84SEric Taylor return (-1); 390ebedde84SEric Taylor } 391fac3008cSeschrock 39214843421SMatthew Ahrens /* 39314843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to 39414843421SMatthew Ahrens * storing them in zfs_props? 39514843421SMatthew Ahrens */ 396fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 397fac3008cSeschrock nvlist_free(allprops); 398fac3008cSeschrock return (-1); 399fac3008cSeschrock } 400fac3008cSeschrock 40199653d4eSeschrock nvlist_free(zhp->zfs_props); 402fac3008cSeschrock nvlist_free(zhp->zfs_user_props); 40399653d4eSeschrock 404fac3008cSeschrock zhp->zfs_props = allprops; 405fac3008cSeschrock zhp->zfs_user_props = userprops; 40699653d4eSeschrock 407fa9e4066Sahrens return (0); 408fa9e4066Sahrens } 409fa9e4066Sahrens 410ebedde84SEric Taylor static int 411ebedde84SEric Taylor get_stats(zfs_handle_t *zhp) 412ebedde84SEric Taylor { 413ebedde84SEric Taylor int rc = 0; 414ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 415ebedde84SEric Taylor 416ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 417ebedde84SEric Taylor return (-1); 418ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0) 419ebedde84SEric Taylor rc = -1; 420ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0) 421ebedde84SEric Taylor rc = -1; 422ebedde84SEric Taylor zcmd_free_nvlists(&zc); 423ebedde84SEric Taylor return (rc); 424ebedde84SEric Taylor } 425ebedde84SEric Taylor 426fa9e4066Sahrens /* 427fa9e4066Sahrens * Refresh the properties currently stored in the handle. 428fa9e4066Sahrens */ 429fa9e4066Sahrens void 430fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 431fa9e4066Sahrens { 432fa9e4066Sahrens (void) get_stats(zhp); 433fa9e4066Sahrens } 434fa9e4066Sahrens 435fa9e4066Sahrens /* 436fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 437fa9e4066Sahrens * zfs_iter_* to create child handles on the fly. 438fa9e4066Sahrens */ 439ebedde84SEric Taylor static int 440ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 441fa9e4066Sahrens { 442503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0) 443ebedde84SEric Taylor return (-1); 44431fd60d3Sahrens 445fa9e4066Sahrens /* 446fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine 447fa9e4066Sahrens * the high-level type. 448fa9e4066Sahrens */ 449a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 450a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 451a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 452a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 453a2eea2e1Sahrens else 454a2eea2e1Sahrens abort(); 455a2eea2e1Sahrens 456fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 457fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 458fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 459fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 460fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 461fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 462fa9e4066Sahrens else 46399653d4eSeschrock abort(); /* we should never see any other types */ 464fa9e4066Sahrens 4659fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 4669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1); 4679fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 468ebedde84SEric Taylor return (0); 469ebedde84SEric Taylor } 470ebedde84SEric Taylor 471ebedde84SEric Taylor zfs_handle_t * 472ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path) 473ebedde84SEric Taylor { 474ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 475ebedde84SEric Taylor 476ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 477ebedde84SEric Taylor 478ebedde84SEric Taylor if (zhp == NULL) 479ebedde84SEric Taylor return (NULL); 480ebedde84SEric Taylor 481ebedde84SEric Taylor zhp->zfs_hdl = hdl; 482ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 483ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 484ebedde84SEric Taylor free(zhp); 485ebedde84SEric Taylor return (NULL); 486ebedde84SEric Taylor } 487ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) { 488ebedde84SEric Taylor zcmd_free_nvlists(&zc); 489ebedde84SEric Taylor free(zhp); 490ebedde84SEric Taylor return (NULL); 491ebedde84SEric Taylor } 492ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) { 493ebedde84SEric Taylor free(zhp); 494ebedde84SEric Taylor zhp = NULL; 495ebedde84SEric Taylor } 496ebedde84SEric Taylor zcmd_free_nvlists(&zc); 497ebedde84SEric Taylor return (zhp); 498ebedde84SEric Taylor } 499ebedde84SEric Taylor 50019b94df9SMatthew Ahrens zfs_handle_t * 501ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 502ebedde84SEric Taylor { 503ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 504ebedde84SEric Taylor 505ebedde84SEric Taylor if (zhp == NULL) 506ebedde84SEric Taylor return (NULL); 507ebedde84SEric Taylor 508ebedde84SEric Taylor zhp->zfs_hdl = hdl; 509ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 510ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) { 511ebedde84SEric Taylor free(zhp); 512ebedde84SEric Taylor return (NULL); 513ebedde84SEric Taylor } 514fa9e4066Sahrens return (zhp); 515fa9e4066Sahrens } 516fa9e4066Sahrens 51719b94df9SMatthew Ahrens zfs_handle_t * 51819b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig) 51919b94df9SMatthew Ahrens { 52019b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 52119b94df9SMatthew Ahrens 52219b94df9SMatthew Ahrens if (zhp == NULL) 52319b94df9SMatthew Ahrens return (NULL); 52419b94df9SMatthew Ahrens 52519b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl; 52619b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl; 52719b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 52819b94df9SMatthew Ahrens sizeof (zhp->zfs_name)); 52919b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type; 53019b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type; 53119b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 53219b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) { 53319b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { 53419b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 53519b94df9SMatthew Ahrens zfs_close(zhp); 53619b94df9SMatthew Ahrens return (NULL); 53719b94df9SMatthew Ahrens } 53819b94df9SMatthew Ahrens } 53919b94df9SMatthew Ahrens if (zhp_orig->zfs_user_props != NULL) { 54019b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props, 54119b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) { 54219b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 54319b94df9SMatthew Ahrens zfs_close(zhp); 54419b94df9SMatthew Ahrens return (NULL); 54519b94df9SMatthew Ahrens } 54619b94df9SMatthew Ahrens } 54719b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) { 54819b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props, 54919b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) { 55019b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 55119b94df9SMatthew Ahrens zfs_close(zhp); 55219b94df9SMatthew Ahrens return (NULL); 55319b94df9SMatthew Ahrens } 55419b94df9SMatthew Ahrens } 55519b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 55619b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) { 55719b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 55819b94df9SMatthew Ahrens zhp_orig->zfs_mntopts); 55919b94df9SMatthew Ahrens } 56019b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table; 56119b94df9SMatthew Ahrens return (zhp); 56219b94df9SMatthew Ahrens } 56319b94df9SMatthew Ahrens 564fa9e4066Sahrens /* 565fa9e4066Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 566fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an 567fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened. 568fa9e4066Sahrens */ 569fa9e4066Sahrens zfs_handle_t * 57099653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 571fa9e4066Sahrens { 572fa9e4066Sahrens zfs_handle_t *zhp; 57399653d4eSeschrock char errbuf[1024]; 57499653d4eSeschrock 57599653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 57699653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 577fa9e4066Sahrens 578fa9e4066Sahrens /* 57999653d4eSeschrock * Validate the name before we even try to open it. 580fa9e4066Sahrens */ 581f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 58299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 58399653d4eSeschrock "invalid dataset name")); 58499653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 585fa9e4066Sahrens return (NULL); 586fa9e4066Sahrens } 587fa9e4066Sahrens 588fa9e4066Sahrens /* 589fa9e4066Sahrens * Try to get stats for the dataset, which will tell us if it exists. 590fa9e4066Sahrens */ 591fa9e4066Sahrens errno = 0; 59299653d4eSeschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 593ece3d9b3Slling (void) zfs_standard_error(hdl, errno, errbuf); 594fa9e4066Sahrens return (NULL); 595fa9e4066Sahrens } 596fa9e4066Sahrens 597fa9e4066Sahrens if (!(types & zhp->zfs_type)) { 59899653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 59994de1d4cSeschrock zfs_close(zhp); 600fa9e4066Sahrens return (NULL); 601fa9e4066Sahrens } 602fa9e4066Sahrens 603fa9e4066Sahrens return (zhp); 604fa9e4066Sahrens } 605fa9e4066Sahrens 606fa9e4066Sahrens /* 607fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 608fa9e4066Sahrens */ 609fa9e4066Sahrens void 610fa9e4066Sahrens zfs_close(zfs_handle_t *zhp) 611fa9e4066Sahrens { 612fa9e4066Sahrens if (zhp->zfs_mntopts) 613fa9e4066Sahrens free(zhp->zfs_mntopts); 61499653d4eSeschrock nvlist_free(zhp->zfs_props); 615e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props); 61692241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 617fa9e4066Sahrens free(zhp); 618fa9e4066Sahrens } 619fa9e4066Sahrens 620ebedde84SEric Taylor typedef struct mnttab_node { 621ebedde84SEric Taylor struct mnttab mtn_mt; 622ebedde84SEric Taylor avl_node_t mtn_node; 623ebedde84SEric Taylor } mnttab_node_t; 624ebedde84SEric Taylor 625ebedde84SEric Taylor static int 626ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 627ebedde84SEric Taylor { 628ebedde84SEric Taylor const mnttab_node_t *mtn1 = arg1; 629ebedde84SEric Taylor const mnttab_node_t *mtn2 = arg2; 630ebedde84SEric Taylor int rv; 631ebedde84SEric Taylor 632ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 633ebedde84SEric Taylor 634ebedde84SEric Taylor if (rv == 0) 635ebedde84SEric Taylor return (0); 636ebedde84SEric Taylor return (rv > 0 ? 1 : -1); 637ebedde84SEric Taylor } 638ebedde84SEric Taylor 639ebedde84SEric Taylor void 640ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl) 641ebedde84SEric Taylor { 642ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 643ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 644ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 645b2634b9cSEric Taylor } 646b2634b9cSEric Taylor 647b2634b9cSEric Taylor void 648b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl) 649b2634b9cSEric Taylor { 650b2634b9cSEric Taylor struct mnttab entry; 651ebedde84SEric Taylor 652ebedde84SEric Taylor rewind(hdl->libzfs_mnttab); 653ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 654ebedde84SEric Taylor mnttab_node_t *mtn; 655ebedde84SEric Taylor 656ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 657ebedde84SEric Taylor continue; 658ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 659ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 660ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 661ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 662ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 663ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 664ebedde84SEric Taylor } 665ebedde84SEric Taylor } 666ebedde84SEric Taylor 667ebedde84SEric Taylor void 668ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl) 669ebedde84SEric Taylor { 670ebedde84SEric Taylor void *cookie = NULL; 671ebedde84SEric Taylor mnttab_node_t *mtn; 672ebedde84SEric Taylor 673ebedde84SEric Taylor while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 674ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special); 675ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp); 676ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype); 677ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts); 678ebedde84SEric Taylor free(mtn); 679ebedde84SEric Taylor } 680ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache); 681ebedde84SEric Taylor } 682ebedde84SEric Taylor 683b2634b9cSEric Taylor void 684b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 685b2634b9cSEric Taylor { 686b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable; 687b2634b9cSEric Taylor } 688b2634b9cSEric Taylor 689ebedde84SEric Taylor int 690ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 691ebedde84SEric Taylor struct mnttab *entry) 692ebedde84SEric Taylor { 693ebedde84SEric Taylor mnttab_node_t find; 694ebedde84SEric Taylor mnttab_node_t *mtn; 695ebedde84SEric Taylor 696b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) { 697b2634b9cSEric Taylor struct mnttab srch = { 0 }; 698b2634b9cSEric Taylor 699b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 700b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 701b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab); 702b2634b9cSEric Taylor srch.mnt_special = (char *)fsname; 703b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS; 704b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 705b2634b9cSEric Taylor return (0); 706b2634b9cSEric Taylor else 707b2634b9cSEric Taylor return (ENOENT); 708b2634b9cSEric Taylor } 709b2634b9cSEric Taylor 710ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 711b2634b9cSEric Taylor libzfs_mnttab_update(hdl); 712ebedde84SEric Taylor 713ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 714ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 715ebedde84SEric Taylor if (mtn) { 716ebedde84SEric Taylor *entry = mtn->mtn_mt; 717ebedde84SEric Taylor return (0); 718ebedde84SEric Taylor } 719ebedde84SEric Taylor return (ENOENT); 720ebedde84SEric Taylor } 721ebedde84SEric Taylor 722ebedde84SEric Taylor void 723ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 724ebedde84SEric Taylor const char *mountp, const char *mntopts) 725ebedde84SEric Taylor { 726ebedde84SEric Taylor mnttab_node_t *mtn; 727ebedde84SEric Taylor 728ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 729ebedde84SEric Taylor return; 730ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 731ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 732ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 733ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 734ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 735ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 736ebedde84SEric Taylor } 737ebedde84SEric Taylor 738ebedde84SEric Taylor void 739ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 740ebedde84SEric Taylor { 741ebedde84SEric Taylor mnttab_node_t find; 742ebedde84SEric Taylor mnttab_node_t *ret; 743ebedde84SEric Taylor 744ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 745ebedde84SEric Taylor if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 746ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret); 747ebedde84SEric Taylor free(ret->mtn_mt.mnt_special); 748ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp); 749ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype); 750ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts); 751ebedde84SEric Taylor free(ret); 752ebedde84SEric Taylor } 753ebedde84SEric Taylor } 754ebedde84SEric Taylor 7557b97dc1aSrm160521 int 7567b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 7577b97dc1aSrm160521 { 75829ab75c9Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 7597b97dc1aSrm160521 7607b97dc1aSrm160521 if (zpool_handle == NULL) 7617b97dc1aSrm160521 return (-1); 7627b97dc1aSrm160521 7637b97dc1aSrm160521 *spa_version = zpool_get_prop_int(zpool_handle, 7647b97dc1aSrm160521 ZPOOL_PROP_VERSION, NULL); 7657b97dc1aSrm160521 return (0); 7667b97dc1aSrm160521 } 7677b97dc1aSrm160521 7687b97dc1aSrm160521 /* 7697b97dc1aSrm160521 * The choice of reservation property depends on the SPA version. 7707b97dc1aSrm160521 */ 7717b97dc1aSrm160521 static int 7727b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 7737b97dc1aSrm160521 { 7747b97dc1aSrm160521 int spa_version; 7757b97dc1aSrm160521 7767b97dc1aSrm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 7777b97dc1aSrm160521 return (-1); 7787b97dc1aSrm160521 7797b97dc1aSrm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 7807b97dc1aSrm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 7817b97dc1aSrm160521 else 7827b97dc1aSrm160521 *resv_prop = ZFS_PROP_RESERVATION; 7837b97dc1aSrm160521 7847b97dc1aSrm160521 return (0); 7857b97dc1aSrm160521 } 7867b97dc1aSrm160521 787b1b8ab34Slling /* 788e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and 789e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as 790e9dbad6fSeschrock * strings. 791fa9e4066Sahrens */ 7920a48a24eStimh nvlist_t * 7930a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 794990b4856Slling uint64_t zoned, zfs_handle_t *zhp, const char *errbuf) 795fa9e4066Sahrens { 796e9dbad6fSeschrock nvpair_t *elem; 797e9dbad6fSeschrock uint64_t intval; 798e9dbad6fSeschrock char *strval; 799990b4856Slling zfs_prop_t prop; 800e9dbad6fSeschrock nvlist_t *ret; 801da6c28aaSamw int chosen_normal = -1; 802da6c28aaSamw int chosen_utf = -1; 803990b4856Slling 804e9dbad6fSeschrock if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 805e9dbad6fSeschrock (void) no_memory(hdl); 806e9dbad6fSeschrock return (NULL); 807e9dbad6fSeschrock } 808fa9e4066Sahrens 80914843421SMatthew Ahrens /* 81014843421SMatthew Ahrens * Make sure this property is valid and applies to this type. 81114843421SMatthew Ahrens */ 81214843421SMatthew Ahrens 813e9dbad6fSeschrock elem = NULL; 814e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 815990b4856Slling const char *propname = nvpair_name(elem); 81699653d4eSeschrock 81714843421SMatthew Ahrens prop = zfs_name_to_prop(propname); 81814843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 819fa9e4066Sahrens /* 82014843421SMatthew Ahrens * This is a user property: make sure it's a 821990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN. 822990b4856Slling */ 823990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 824990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 825990b4856Slling "'%s' must be a string"), propname); 826990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 827990b4856Slling goto error; 828990b4856Slling } 829990b4856Slling 830990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 831e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 832e9dbad6fSeschrock "property name '%s' is too long"), 833e9dbad6fSeschrock propname); 834990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 835e9dbad6fSeschrock goto error; 836e9dbad6fSeschrock } 837e9dbad6fSeschrock 838e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval); 839e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) { 840e9dbad6fSeschrock (void) no_memory(hdl); 841e9dbad6fSeschrock goto error; 842e9dbad6fSeschrock } 843e9dbad6fSeschrock continue; 844e9dbad6fSeschrock } 845fa9e4066Sahrens 84614843421SMatthew Ahrens /* 84714843421SMatthew Ahrens * Currently, only user properties can be modified on 84814843421SMatthew Ahrens * snapshots. 84914843421SMatthew Ahrens */ 850bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 851bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 852bb0ade09Sahrens "this property can not be modified for snapshots")); 853bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 854bb0ade09Sahrens goto error; 855bb0ade09Sahrens } 856bb0ade09Sahrens 85714843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 85814843421SMatthew Ahrens zfs_userquota_prop_t uqtype; 85914843421SMatthew Ahrens char newpropname[128]; 86014843421SMatthew Ahrens char domain[128]; 86114843421SMatthew Ahrens uint64_t rid; 86214843421SMatthew Ahrens uint64_t valary[3]; 86314843421SMatthew Ahrens 86414843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned, 86514843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) { 86614843421SMatthew Ahrens zfs_error_aux(hdl, 86714843421SMatthew Ahrens dgettext(TEXT_DOMAIN, 86814843421SMatthew Ahrens "'%s' has an invalid user/group name"), 86914843421SMatthew Ahrens propname); 87014843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 87114843421SMatthew Ahrens goto error; 87214843421SMatthew Ahrens } 87314843421SMatthew Ahrens 87414843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA && 87514843421SMatthew Ahrens uqtype != ZFS_PROP_GROUPQUOTA) { 87614843421SMatthew Ahrens zfs_error_aux(hdl, 87714843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"), 87814843421SMatthew Ahrens propname); 87914843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, 88014843421SMatthew Ahrens errbuf); 88114843421SMatthew Ahrens goto error; 88214843421SMatthew Ahrens } 88314843421SMatthew Ahrens 88414843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) { 88514843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval); 88614843421SMatthew Ahrens if (strcmp(strval, "none") == 0) { 88714843421SMatthew Ahrens intval = 0; 88814843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl, 88914843421SMatthew Ahrens strval, &intval) != 0) { 89014843421SMatthew Ahrens (void) zfs_error(hdl, 89114843421SMatthew Ahrens EZFS_BADPROP, errbuf); 89214843421SMatthew Ahrens goto error; 89314843421SMatthew Ahrens } 89414843421SMatthew Ahrens } else if (nvpair_type(elem) == 89514843421SMatthew Ahrens DATA_TYPE_UINT64) { 89614843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval); 89714843421SMatthew Ahrens if (intval == 0) { 89814843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 89914843421SMatthew Ahrens "use 'none' to disable " 90014843421SMatthew Ahrens "userquota/groupquota")); 90114843421SMatthew Ahrens goto error; 90214843421SMatthew Ahrens } 90314843421SMatthew Ahrens } else { 90414843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 90514843421SMatthew Ahrens "'%s' must be a number"), propname); 90614843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 90714843421SMatthew Ahrens goto error; 90814843421SMatthew Ahrens } 90914843421SMatthew Ahrens 9102d5843dbSMatthew Ahrens /* 9112d5843dbSMatthew Ahrens * Encode the prop name as 9122d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy 9132d5843dbSMatthew Ahrens * for the kernel to decode. 9142d5843dbSMatthew Ahrens */ 91514843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname), 9162d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 9172d5843dbSMatthew Ahrens (longlong_t)rid, domain); 91814843421SMatthew Ahrens valary[0] = uqtype; 91914843421SMatthew Ahrens valary[1] = rid; 92014843421SMatthew Ahrens valary[2] = intval; 92114843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname, 92214843421SMatthew Ahrens valary, 3) != 0) { 92314843421SMatthew Ahrens (void) no_memory(hdl); 92414843421SMatthew Ahrens goto error; 92514843421SMatthew Ahrens } 92614843421SMatthew Ahrens continue; 92719b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { 92819b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 92919b94df9SMatthew Ahrens "'%s' is readonly"), 93019b94df9SMatthew Ahrens propname); 93119b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 93219b94df9SMatthew Ahrens goto error; 93314843421SMatthew Ahrens } 93414843421SMatthew Ahrens 93514843421SMatthew Ahrens if (prop == ZPROP_INVAL) { 93614843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 93714843421SMatthew Ahrens "invalid property '%s'"), propname); 93814843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 93914843421SMatthew Ahrens goto error; 94014843421SMatthew Ahrens } 94114843421SMatthew Ahrens 942e9dbad6fSeschrock if (!zfs_prop_valid_for_type(prop, type)) { 943e9dbad6fSeschrock zfs_error_aux(hdl, 944e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not " 945e9dbad6fSeschrock "apply to datasets of this type"), propname); 946e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 947e9dbad6fSeschrock goto error; 948e9dbad6fSeschrock } 949e9dbad6fSeschrock 950e9dbad6fSeschrock if (zfs_prop_readonly(prop) && 951da6c28aaSamw (!zfs_prop_setonce(prop) || zhp != NULL)) { 952e9dbad6fSeschrock zfs_error_aux(hdl, 953e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 954e9dbad6fSeschrock propname); 955e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 956e9dbad6fSeschrock goto error; 957e9dbad6fSeschrock } 958e9dbad6fSeschrock 959990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 960990b4856Slling &strval, &intval, errbuf) != 0) 961e9dbad6fSeschrock goto error; 962e9dbad6fSeschrock 963e9dbad6fSeschrock /* 964e9dbad6fSeschrock * Perform some additional checks for specific properties. 965e9dbad6fSeschrock */ 966e9dbad6fSeschrock switch (prop) { 967e7437265Sahrens case ZFS_PROP_VERSION: 968e7437265Sahrens { 969e7437265Sahrens int version; 970e7437265Sahrens 971e7437265Sahrens if (zhp == NULL) 972e7437265Sahrens break; 973e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 974e7437265Sahrens if (intval < version) { 975e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 976e7437265Sahrens "Can not downgrade; already at version %u"), 977e7437265Sahrens version); 978e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 979e7437265Sahrens goto error; 980e7437265Sahrens } 981e7437265Sahrens break; 982e7437265Sahrens } 983e7437265Sahrens 984e9dbad6fSeschrock case ZFS_PROP_RECORDSIZE: 985e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE: 986e9dbad6fSeschrock /* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ 987e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE || 988e9dbad6fSeschrock intval > SPA_MAXBLOCKSIZE || !ISP2(intval)) { 989e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 990e9dbad6fSeschrock "'%s' must be power of 2 from %u " 991e9dbad6fSeschrock "to %uk"), propname, 992e9dbad6fSeschrock (uint_t)SPA_MINBLOCKSIZE, 993e9dbad6fSeschrock (uint_t)SPA_MAXBLOCKSIZE >> 10); 994e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 995e9dbad6fSeschrock goto error; 996e9dbad6fSeschrock } 997e9dbad6fSeschrock break; 998e9dbad6fSeschrock 9994201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 10004201a95eSRic Aleshire { 10014201a95eSRic Aleshire /* 10024201a95eSRic Aleshire * Verify the mlslabel string and convert to 10034201a95eSRic Aleshire * internal hex label string. 10044201a95eSRic Aleshire */ 10054201a95eSRic Aleshire 10064201a95eSRic Aleshire m_label_t *new_sl; 10074201a95eSRic Aleshire char *hex = NULL; /* internal label string */ 10084201a95eSRic Aleshire 10094201a95eSRic Aleshire /* Default value is already OK. */ 10104201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 10114201a95eSRic Aleshire break; 10124201a95eSRic Aleshire 10134201a95eSRic Aleshire /* Verify the label can be converted to binary form */ 10144201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 10154201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL, 10164201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) { 10174201a95eSRic Aleshire goto badlabel; 10184201a95eSRic Aleshire } 10194201a95eSRic Aleshire 10204201a95eSRic Aleshire /* Now translate to hex internal label string */ 10214201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL, 10224201a95eSRic Aleshire DEF_NAMES) != 0) { 10234201a95eSRic Aleshire if (hex) 10244201a95eSRic Aleshire free(hex); 10254201a95eSRic Aleshire goto badlabel; 10264201a95eSRic Aleshire } 10274201a95eSRic Aleshire m_label_free(new_sl); 10284201a95eSRic Aleshire 10294201a95eSRic Aleshire /* If string is already in internal form, we're done. */ 10304201a95eSRic Aleshire if (strcmp(strval, hex) == 0) { 10314201a95eSRic Aleshire free(hex); 10324201a95eSRic Aleshire break; 10334201a95eSRic Aleshire } 10344201a95eSRic Aleshire 10354201a95eSRic Aleshire /* Replace the label string with the internal form. */ 1036569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop), 10374201a95eSRic Aleshire DATA_TYPE_STRING); 10384201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop), 10394201a95eSRic Aleshire hex) == 0); 10404201a95eSRic Aleshire free(hex); 10414201a95eSRic Aleshire 10424201a95eSRic Aleshire break; 10434201a95eSRic Aleshire 10444201a95eSRic Aleshire badlabel: 10454201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 10464201a95eSRic Aleshire "invalid mlslabel '%s'"), strval); 10474201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 10484201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */ 10494201a95eSRic Aleshire goto error; 10504201a95eSRic Aleshire 10514201a95eSRic Aleshire } 10524201a95eSRic Aleshire 1053e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT: 105489eef05eSrm160521 { 105589eef05eSrm160521 namecheck_err_t why; 105689eef05eSrm160521 1057e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1058e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1059e9dbad6fSeschrock break; 1060e9dbad6fSeschrock 106189eef05eSrm160521 if (mountpoint_namecheck(strval, &why)) { 106289eef05eSrm160521 switch (why) { 106389eef05eSrm160521 case NAME_ERR_LEADING_SLASH: 106489eef05eSrm160521 zfs_error_aux(hdl, 106589eef05eSrm160521 dgettext(TEXT_DOMAIN, 1066e9dbad6fSeschrock "'%s' must be an absolute path, " 1067e9dbad6fSeschrock "'none', or 'legacy'"), propname); 106889eef05eSrm160521 break; 106989eef05eSrm160521 case NAME_ERR_TOOLONG: 107089eef05eSrm160521 zfs_error_aux(hdl, 107189eef05eSrm160521 dgettext(TEXT_DOMAIN, 107289eef05eSrm160521 "component of '%s' is too long"), 107389eef05eSrm160521 propname); 107489eef05eSrm160521 break; 107589eef05eSrm160521 } 1076e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1077e9dbad6fSeschrock goto error; 1078e9dbad6fSeschrock } 107989eef05eSrm160521 } 108089eef05eSrm160521 1081f3861e1aSahl /*FALLTHRU*/ 1082e9dbad6fSeschrock 1083da6c28aaSamw case ZFS_PROP_SHARESMB: 1084f3861e1aSahl case ZFS_PROP_SHARENFS: 1085e9dbad6fSeschrock /* 1086da6c28aaSamw * For the mountpoint and sharenfs or sharesmb 1087da6c28aaSamw * properties, check if it can be set in a 1088da6c28aaSamw * global/non-global zone based on 1089f3861e1aSahl * the zoned property value: 1090fa9e4066Sahrens * 1091fa9e4066Sahrens * global zone non-global zone 1092f3861e1aSahl * -------------------------------------------------- 1093fa9e4066Sahrens * zoned=on mountpoint (no) mountpoint (yes) 1094fa9e4066Sahrens * sharenfs (no) sharenfs (no) 1095da6c28aaSamw * sharesmb (no) sharesmb (no) 1096fa9e4066Sahrens * 1097fa9e4066Sahrens * zoned=off mountpoint (yes) N/A 1098fa9e4066Sahrens * sharenfs (yes) 1099da6c28aaSamw * sharesmb (yes) 1100fa9e4066Sahrens */ 1101e9dbad6fSeschrock if (zoned) { 1102fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID) { 110399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1104e9dbad6fSeschrock "'%s' cannot be set on " 1105e9dbad6fSeschrock "dataset in a non-global zone"), 1106e9dbad6fSeschrock propname); 1107e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1108e9dbad6fSeschrock errbuf); 1109e9dbad6fSeschrock goto error; 1110da6c28aaSamw } else if (prop == ZFS_PROP_SHARENFS || 1111da6c28aaSamw prop == ZFS_PROP_SHARESMB) { 111299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1113e9dbad6fSeschrock "'%s' cannot be set in " 1114e9dbad6fSeschrock "a non-global zone"), propname); 1115e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1116e9dbad6fSeschrock errbuf); 1117e9dbad6fSeschrock goto error; 1118fa9e4066Sahrens } 1119fa9e4066Sahrens } else if (getzoneid() != GLOBAL_ZONEID) { 1120fa9e4066Sahrens /* 1121fa9e4066Sahrens * If zoned property is 'off', this must be in 112214843421SMatthew Ahrens * a global zone. If not, something is wrong. 1123fa9e4066Sahrens */ 112499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1125e9dbad6fSeschrock "'%s' cannot be set while dataset " 1126e9dbad6fSeschrock "'zoned' property is set"), propname); 1127e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1128e9dbad6fSeschrock goto error; 1129fa9e4066Sahrens } 1130f3861e1aSahl 113167331909Sdougm /* 113267331909Sdougm * At this point, it is legitimate to set the 113367331909Sdougm * property. Now we want to make sure that the 113467331909Sdougm * property value is valid if it is sharenfs. 113567331909Sdougm */ 1136da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS || 1137da6c28aaSamw prop == ZFS_PROP_SHARESMB) && 113867331909Sdougm strcmp(strval, "on") != 0 && 113967331909Sdougm strcmp(strval, "off") != 0) { 1140da6c28aaSamw zfs_share_proto_t proto; 1141da6c28aaSamw 1142da6c28aaSamw if (prop == ZFS_PROP_SHARESMB) 1143da6c28aaSamw proto = PROTO_SMB; 1144da6c28aaSamw else 1145da6c28aaSamw proto = PROTO_NFS; 114667331909Sdougm 114767331909Sdougm /* 1148da6c28aaSamw * Must be an valid sharing protocol 1149da6c28aaSamw * option string so init the libshare 1150da6c28aaSamw * in order to enable the parser and 1151da6c28aaSamw * then parse the options. We use the 1152da6c28aaSamw * control API since we don't care about 1153da6c28aaSamw * the current configuration and don't 115467331909Sdougm * want the overhead of loading it 115567331909Sdougm * until we actually do something. 115667331909Sdougm */ 115767331909Sdougm 115867331909Sdougm if (zfs_init_libshare(hdl, 115967331909Sdougm SA_INIT_CONTROL_API) != SA_OK) { 1160fac3008cSeschrock /* 1161fac3008cSeschrock * An error occurred so we can't do 1162fac3008cSeschrock * anything 1163fac3008cSeschrock */ 116467331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 116567331909Sdougm "'%s' cannot be set: problem " 116667331909Sdougm "in share initialization"), 116767331909Sdougm propname); 1168fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1169fac3008cSeschrock errbuf); 117067331909Sdougm goto error; 117167331909Sdougm } 117267331909Sdougm 1173da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) { 117467331909Sdougm /* 117567331909Sdougm * There was an error in parsing so 117667331909Sdougm * deal with it by issuing an error 117767331909Sdougm * message and leaving after 117867331909Sdougm * uninitializing the the libshare 117967331909Sdougm * interface. 118067331909Sdougm */ 118167331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 118267331909Sdougm "'%s' cannot be set to invalid " 118367331909Sdougm "options"), propname); 1184fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1185fac3008cSeschrock errbuf); 118667331909Sdougm zfs_uninit_libshare(hdl); 118767331909Sdougm goto error; 118867331909Sdougm } 118967331909Sdougm zfs_uninit_libshare(hdl); 119067331909Sdougm } 119167331909Sdougm 1192f3861e1aSahl break; 1193da6c28aaSamw case ZFS_PROP_UTF8ONLY: 1194da6c28aaSamw chosen_utf = (int)intval; 1195da6c28aaSamw break; 1196da6c28aaSamw case ZFS_PROP_NORMALIZE: 1197da6c28aaSamw chosen_normal = (int)intval; 1198da6c28aaSamw break; 1199fa9e4066Sahrens } 1200fa9e4066Sahrens 1201e9dbad6fSeschrock /* 1202e9dbad6fSeschrock * For changes to existing volumes, we have some additional 1203e9dbad6fSeschrock * checks to enforce. 1204e9dbad6fSeschrock */ 1205e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1206e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp, 1207e9dbad6fSeschrock ZFS_PROP_VOLSIZE); 1208e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp, 1209e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 1210e9dbad6fSeschrock char buf[64]; 1211e9dbad6fSeschrock 1212e9dbad6fSeschrock switch (prop) { 1213e9dbad6fSeschrock case ZFS_PROP_RESERVATION: 1214a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 1215e9dbad6fSeschrock if (intval > volsize) { 1216e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1217e9dbad6fSeschrock "'%s' is greater than current " 1218e9dbad6fSeschrock "volume size"), propname); 1219e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1220e9dbad6fSeschrock errbuf); 1221e9dbad6fSeschrock goto error; 1222e9dbad6fSeschrock } 1223e9dbad6fSeschrock break; 1224e9dbad6fSeschrock 1225e9dbad6fSeschrock case ZFS_PROP_VOLSIZE: 1226e9dbad6fSeschrock if (intval % blocksize != 0) { 1227e9dbad6fSeschrock zfs_nicenum(blocksize, buf, 1228e9dbad6fSeschrock sizeof (buf)); 1229e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1230e9dbad6fSeschrock "'%s' must be a multiple of " 1231e9dbad6fSeschrock "volume block size (%s)"), 1232e9dbad6fSeschrock propname, buf); 1233e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1234e9dbad6fSeschrock errbuf); 1235e9dbad6fSeschrock goto error; 1236e9dbad6fSeschrock } 1237e9dbad6fSeschrock 1238e9dbad6fSeschrock if (intval == 0) { 1239e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1240e9dbad6fSeschrock "'%s' cannot be zero"), 1241e9dbad6fSeschrock propname); 1242e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1243e9dbad6fSeschrock errbuf); 1244e9dbad6fSeschrock goto error; 1245e9dbad6fSeschrock } 1246f3861e1aSahl break; 1247e9dbad6fSeschrock } 1248e9dbad6fSeschrock } 1249e9dbad6fSeschrock } 1250e9dbad6fSeschrock 1251e9dbad6fSeschrock /* 1252da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made, 1253da6c28aaSamw * enforce rejection of non-UTF8 names. 1254da6c28aaSamw * 1255da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names 1256da6c28aaSamw * was explicitly not chosen, it is an error. 1257da6c28aaSamw */ 1258de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) { 1259da6c28aaSamw if (nvlist_add_uint64(ret, 1260da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1261da6c28aaSamw (void) no_memory(hdl); 1262da6c28aaSamw goto error; 1263da6c28aaSamw } 1264de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 1265da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1266da6c28aaSamw "'%s' must be set 'on' if normalization chosen"), 1267da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1268da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1269da6c28aaSamw goto error; 1270da6c28aaSamw } 1271e9dbad6fSeschrock return (ret); 1272e9dbad6fSeschrock 1273e9dbad6fSeschrock error: 1274e9dbad6fSeschrock nvlist_free(ret); 1275e9dbad6fSeschrock return (NULL); 1276e9dbad6fSeschrock } 1277e9dbad6fSeschrock 127836db6475SEric Taylor int 127936db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 128036db6475SEric Taylor { 128136db6475SEric Taylor uint64_t old_volsize; 128236db6475SEric Taylor uint64_t new_volsize; 128336db6475SEric Taylor uint64_t old_reservation; 128436db6475SEric Taylor uint64_t new_reservation; 128536db6475SEric Taylor zfs_prop_t resv_prop; 128636db6475SEric Taylor 128736db6475SEric Taylor /* 128836db6475SEric Taylor * If this is an existing volume, and someone is setting the volsize, 128936db6475SEric Taylor * make sure that it matches the reservation, or add it if necessary. 129036db6475SEric Taylor */ 129136db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 129236db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 129336db6475SEric Taylor return (-1); 129436db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop); 129536db6475SEric Taylor if ((zvol_volsize_to_reservation(old_volsize, zhp->zfs_props) != 129636db6475SEric Taylor old_reservation) || nvlist_lookup_uint64(nvl, 129736db6475SEric Taylor zfs_prop_to_name(resv_prop), &new_reservation) != ENOENT) { 129836db6475SEric Taylor return (0); 129936db6475SEric Taylor } 130036db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 130136db6475SEric Taylor &new_volsize) != 0) 130236db6475SEric Taylor return (-1); 130336db6475SEric Taylor new_reservation = zvol_volsize_to_reservation(new_volsize, 130436db6475SEric Taylor zhp->zfs_props); 130536db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 130636db6475SEric Taylor new_reservation) != 0) { 130736db6475SEric Taylor (void) no_memory(zhp->zfs_hdl); 130836db6475SEric Taylor return (-1); 130936db6475SEric Taylor } 131036db6475SEric Taylor return (1); 131136db6475SEric Taylor } 131236db6475SEric Taylor 131392241e0bSTom Erickson void 131492241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 131592241e0bSTom Erickson char *errbuf) 131692241e0bSTom Erickson { 131792241e0bSTom Erickson switch (err) { 131892241e0bSTom Erickson 131992241e0bSTom Erickson case ENOSPC: 132092241e0bSTom Erickson /* 132192241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates 132292241e0bSTom Erickson * something different; setting a quota or reservation 132392241e0bSTom Erickson * doesn't use any disk space. 132492241e0bSTom Erickson */ 132592241e0bSTom Erickson switch (prop) { 132692241e0bSTom Erickson case ZFS_PROP_QUOTA: 132792241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 132892241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 132992241e0bSTom Erickson "size is less than current used or " 133092241e0bSTom Erickson "reserved space")); 133192241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 133292241e0bSTom Erickson break; 133392241e0bSTom Erickson 133492241e0bSTom Erickson case ZFS_PROP_RESERVATION: 133592241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 133692241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 133792241e0bSTom Erickson "size is greater than available space")); 133892241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 133992241e0bSTom Erickson break; 134092241e0bSTom Erickson 134192241e0bSTom Erickson default: 134292241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 134392241e0bSTom Erickson break; 134492241e0bSTom Erickson } 134592241e0bSTom Erickson break; 134692241e0bSTom Erickson 134792241e0bSTom Erickson case EBUSY: 134892241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf); 134992241e0bSTom Erickson break; 135092241e0bSTom Erickson 135192241e0bSTom Erickson case EROFS: 135292241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 135392241e0bSTom Erickson break; 135492241e0bSTom Erickson 135592241e0bSTom Erickson case ENOTSUP: 135692241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 135792241e0bSTom Erickson "pool and or dataset must be upgraded to set this " 135892241e0bSTom Erickson "property or value")); 135992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 136092241e0bSTom Erickson break; 136192241e0bSTom Erickson 136292241e0bSTom Erickson case ERANGE: 136392241e0bSTom Erickson if (prop == ZFS_PROP_COMPRESSION) { 136492241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 136592241e0bSTom Erickson "property setting is not allowed on " 136692241e0bSTom Erickson "bootable datasets")); 136792241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 136892241e0bSTom Erickson } else { 136992241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 137092241e0bSTom Erickson } 137192241e0bSTom Erickson break; 137292241e0bSTom Erickson 1373ab003da8SJim Dunham case EINVAL: 1374ab003da8SJim Dunham if (prop == ZPROP_INVAL) { 1375ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1376ab003da8SJim Dunham } else { 1377ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf); 1378ab003da8SJim Dunham } 1379ab003da8SJim Dunham break; 1380ab003da8SJim Dunham 138192241e0bSTom Erickson case EOVERFLOW: 138292241e0bSTom Erickson /* 138392241e0bSTom Erickson * This platform can't address a volume this big. 138492241e0bSTom Erickson */ 138592241e0bSTom Erickson #ifdef _ILP32 138692241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) { 138792241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 138892241e0bSTom Erickson break; 138992241e0bSTom Erickson } 139092241e0bSTom Erickson #endif 139192241e0bSTom Erickson /* FALLTHROUGH */ 139292241e0bSTom Erickson default: 139392241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 139492241e0bSTom Erickson } 139592241e0bSTom Erickson } 139692241e0bSTom Erickson 1397e9dbad6fSeschrock /* 1398e9dbad6fSeschrock * Given a property name and value, set the property for the given dataset. 1399e9dbad6fSeschrock */ 1400e9dbad6fSeschrock int 1401e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1402e9dbad6fSeschrock { 1403e9dbad6fSeschrock zfs_cmd_t zc = { 0 }; 1404e9dbad6fSeschrock int ret = -1; 1405e9dbad6fSeschrock prop_changelist_t *cl = NULL; 1406e9dbad6fSeschrock char errbuf[1024]; 1407e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 1408e9dbad6fSeschrock nvlist_t *nvl = NULL, *realprops; 1409e9dbad6fSeschrock zfs_prop_t prop; 14104445fffbSMatthew Ahrens boolean_t do_prefix = B_TRUE; 141136db6475SEric Taylor int added_resv; 1412e9dbad6fSeschrock 1413e9dbad6fSeschrock (void) snprintf(errbuf, sizeof (errbuf), 1414e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1415e9dbad6fSeschrock zhp->zfs_name); 1416e9dbad6fSeschrock 1417e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1418e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) { 1419e9dbad6fSeschrock (void) no_memory(hdl); 1420e9dbad6fSeschrock goto error; 1421e9dbad6fSeschrock } 1422e9dbad6fSeschrock 14230a48a24eStimh if ((realprops = zfs_valid_proplist(hdl, zhp->zfs_type, nvl, 1424e9dbad6fSeschrock zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, errbuf)) == NULL) 1425e9dbad6fSeschrock goto error; 1426990b4856Slling 1427e9dbad6fSeschrock nvlist_free(nvl); 1428e9dbad6fSeschrock nvl = realprops; 1429e9dbad6fSeschrock 1430e9dbad6fSeschrock prop = zfs_name_to_prop(propname); 1431e9dbad6fSeschrock 143236db6475SEric Taylor if (prop == ZFS_PROP_VOLSIZE) { 143336db6475SEric Taylor if ((added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) 143436db6475SEric Taylor goto error; 143536db6475SEric Taylor } 143636db6475SEric Taylor 14370069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1438e9dbad6fSeschrock goto error; 1439fa9e4066Sahrens 1440fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 144199653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1442fa9e4066Sahrens "child dataset with inherited mountpoint is used " 144399653d4eSeschrock "in a non-global zone")); 144499653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1445fa9e4066Sahrens goto error; 1446fa9e4066Sahrens } 1447fa9e4066Sahrens 14480068372bSMark J Musante /* 14494445fffbSMatthew Ahrens * We don't want to unmount & remount the dataset when changing 14504445fffbSMatthew Ahrens * its canmount property to 'on' or 'noauto'. We only use 14514445fffbSMatthew Ahrens * the changelist logic to unmount when setting canmount=off. 14520068372bSMark J Musante */ 14534445fffbSMatthew Ahrens if (prop == ZFS_PROP_CANMOUNT) { 14544445fffbSMatthew Ahrens uint64_t idx; 14554445fffbSMatthew Ahrens int err = zprop_string_to_index(prop, propval, &idx, 14564445fffbSMatthew Ahrens ZFS_TYPE_DATASET); 14574445fffbSMatthew Ahrens if (err == 0 && idx != ZFS_CANMOUNT_OFF) 14584445fffbSMatthew Ahrens do_prefix = B_FALSE; 14594445fffbSMatthew Ahrens } 1460a227b7f4Shs24103 1461a227b7f4Shs24103 if (do_prefix && (ret = changelist_prefix(cl)) != 0) 1462fa9e4066Sahrens goto error; 1463fa9e4066Sahrens 1464fa9e4066Sahrens /* 1465fa9e4066Sahrens * Execute the corresponding ioctl() to set this property. 1466fa9e4066Sahrens */ 1467fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1468fa9e4066Sahrens 1469990b4856Slling if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 1470e9dbad6fSeschrock goto error; 1471e9dbad6fSeschrock 1472ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1473743a77edSAlan Wright 1474fa9e4066Sahrens if (ret != 0) { 147592241e0bSTom Erickson zfs_setprop_error(hdl, prop, errno, errbuf); 147636db6475SEric Taylor if (added_resv && errno == ENOSPC) { 147736db6475SEric Taylor /* clean up the volsize property we tried to set */ 147836db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp, 147936db6475SEric Taylor ZFS_PROP_VOLSIZE); 148036db6475SEric Taylor nvlist_free(nvl); 148136db6475SEric Taylor zcmd_free_nvlists(&zc); 148236db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 148336db6475SEric Taylor goto error; 148436db6475SEric Taylor if (nvlist_add_uint64(nvl, 148536db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE), 148636db6475SEric Taylor old_volsize) != 0) 148736db6475SEric Taylor goto error; 148836db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 148936db6475SEric Taylor goto error; 149036db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 149136db6475SEric Taylor } 1492fa9e4066Sahrens } else { 1493a227b7f4Shs24103 if (do_prefix) 1494a227b7f4Shs24103 ret = changelist_postfix(cl); 1495a227b7f4Shs24103 1496fa9e4066Sahrens /* 1497fa9e4066Sahrens * Refresh the statistics so the new property value 1498fa9e4066Sahrens * is reflected. 1499fa9e4066Sahrens */ 1500a227b7f4Shs24103 if (ret == 0) 1501fa9e4066Sahrens (void) get_stats(zhp); 1502fa9e4066Sahrens } 1503fa9e4066Sahrens 1504fa9e4066Sahrens error: 1505e9dbad6fSeschrock nvlist_free(nvl); 1506e9dbad6fSeschrock zcmd_free_nvlists(&zc); 1507e9dbad6fSeschrock if (cl) 1508fa9e4066Sahrens changelist_free(cl); 1509fa9e4066Sahrens return (ret); 1510fa9e4066Sahrens } 1511fa9e4066Sahrens 1512fa9e4066Sahrens /* 151392241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received 151492241e0bSTom Erickson * is TRUE, revert to the received value, if any. 1515fa9e4066Sahrens */ 1516fa9e4066Sahrens int 151792241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1518fa9e4066Sahrens { 1519fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1520fa9e4066Sahrens int ret; 1521fa9e4066Sahrens prop_changelist_t *cl; 152299653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 152399653d4eSeschrock char errbuf[1024]; 1524e9dbad6fSeschrock zfs_prop_t prop; 152599653d4eSeschrock 152699653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 152799653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1528fa9e4066Sahrens 152992241e0bSTom Erickson zc.zc_cookie = received; 1530990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1531e9dbad6fSeschrock /* 1532e9dbad6fSeschrock * For user properties, the amount of work we have to do is very 1533e9dbad6fSeschrock * small, so just do it here. 1534e9dbad6fSeschrock */ 1535e9dbad6fSeschrock if (!zfs_prop_user(propname)) { 1536e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1537e9dbad6fSeschrock "invalid property")); 1538e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1539e9dbad6fSeschrock } 1540e9dbad6fSeschrock 1541e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1542e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1543e9dbad6fSeschrock 1544e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1545e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1546e9dbad6fSeschrock 1547e9dbad6fSeschrock return (0); 1548e9dbad6fSeschrock } 1549e9dbad6fSeschrock 1550fa9e4066Sahrens /* 1551fa9e4066Sahrens * Verify that this property is inheritable. 1552fa9e4066Sahrens */ 155399653d4eSeschrock if (zfs_prop_readonly(prop)) 155499653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1555fa9e4066Sahrens 155692241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received) 155799653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1558fa9e4066Sahrens 1559fa9e4066Sahrens /* 1560fa9e4066Sahrens * Check to see if the value applies to this type 1561fa9e4066Sahrens */ 156299653d4eSeschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 156399653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1564fa9e4066Sahrens 1565bf7c2d40Srm160521 /* 156636db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations. 1567bf7c2d40Srm160521 */ 1568bf7c2d40Srm160521 propname = zfs_prop_to_name(prop); 1569fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1570e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1571fa9e4066Sahrens 1572fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1573fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 157499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 157599653d4eSeschrock "dataset is used in a non-global zone")); 157699653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1577fa9e4066Sahrens } 1578fa9e4066Sahrens 1579fa9e4066Sahrens /* 1580fa9e4066Sahrens * Determine datasets which will be affected by this change, if any. 1581fa9e4066Sahrens */ 15820069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1583fa9e4066Sahrens return (-1); 1584fa9e4066Sahrens 1585fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 158699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 158799653d4eSeschrock "child dataset with inherited mountpoint is used " 158899653d4eSeschrock "in a non-global zone")); 158999653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1590fa9e4066Sahrens goto error; 1591fa9e4066Sahrens } 1592fa9e4066Sahrens 1593fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 1594fa9e4066Sahrens goto error; 1595fa9e4066Sahrens 1596e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 159799653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1598fa9e4066Sahrens } else { 1599fa9e4066Sahrens 1600efc555ebSnd150628 if ((ret = changelist_postfix(cl)) != 0) 1601fa9e4066Sahrens goto error; 1602fa9e4066Sahrens 1603fa9e4066Sahrens /* 1604fa9e4066Sahrens * Refresh the statistics so the new property is reflected. 1605fa9e4066Sahrens */ 1606fa9e4066Sahrens (void) get_stats(zhp); 1607fa9e4066Sahrens } 1608fa9e4066Sahrens 1609fa9e4066Sahrens error: 1610fa9e4066Sahrens changelist_free(cl); 1611fa9e4066Sahrens return (ret); 1612fa9e4066Sahrens } 1613fa9e4066Sahrens 1614fa9e4066Sahrens /* 16157f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions 16167f7322feSeschrock * extract them appropriately. 16177f7322feSeschrock */ 16187f7322feSeschrock static uint64_t 16197f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 16207f7322feSeschrock { 16217f7322feSeschrock nvlist_t *nv; 16227f7322feSeschrock uint64_t value; 16237f7322feSeschrock 1624a2eea2e1Sahrens *source = NULL; 16257f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 16267f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1627990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1628990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 16297f7322feSeschrock } else { 16302e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 16312e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 16327f7322feSeschrock value = zfs_prop_default_numeric(prop); 16337f7322feSeschrock *source = ""; 16347f7322feSeschrock } 16357f7322feSeschrock 16367f7322feSeschrock return (value); 16377f7322feSeschrock } 16387f7322feSeschrock 16397f7322feSeschrock static char * 16407f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 16417f7322feSeschrock { 16427f7322feSeschrock nvlist_t *nv; 16437f7322feSeschrock char *value; 16447f7322feSeschrock 1645a2eea2e1Sahrens *source = NULL; 16467f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 16477f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1648990b4856Slling verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0); 1649990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 16507f7322feSeschrock } else { 16512e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 16522e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 16537f7322feSeschrock if ((value = (char *)zfs_prop_default_string(prop)) == NULL) 16547f7322feSeschrock value = ""; 16557f7322feSeschrock *source = ""; 16567f7322feSeschrock } 16577f7322feSeschrock 16587f7322feSeschrock return (value); 16597f7322feSeschrock } 16607f7322feSeschrock 166192241e0bSTom Erickson static boolean_t 166292241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp) 166392241e0bSTom Erickson { 166492241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props); 166592241e0bSTom Erickson } 166692241e0bSTom Erickson 166792241e0bSTom Erickson static void 166892241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 166992241e0bSTom Erickson { 167092241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 167192241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props; 167292241e0bSTom Erickson } 167392241e0bSTom Erickson 167492241e0bSTom Erickson static void 167592241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 167692241e0bSTom Erickson { 167792241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 167892241e0bSTom Erickson *cookie = 0; 167992241e0bSTom Erickson } 168092241e0bSTom Erickson 16817f7322feSeschrock /* 1682fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1683fa9e4066Sahrens * zfs_prop_get_int() are built using this interface. 1684fa9e4066Sahrens * 1685fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1686fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1687fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark 1688fa9e4066Sahrens * the source "temporary". 1689fa9e4066Sahrens */ 169099653d4eSeschrock static int 1691990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 169299653d4eSeschrock char **source, uint64_t *val) 1693fa9e4066Sahrens { 1694bd00f61bSrm160521 zfs_cmd_t zc = { 0 }; 169596510749Stimh nvlist_t *zplprops = NULL; 1696fa9e4066Sahrens struct mnttab mnt; 16973ccfa83cSahrens char *mntopt_on = NULL; 16983ccfa83cSahrens char *mntopt_off = NULL; 169992241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 1700fa9e4066Sahrens 1701fa9e4066Sahrens *source = NULL; 1702fa9e4066Sahrens 17033ccfa83cSahrens switch (prop) { 17043ccfa83cSahrens case ZFS_PROP_ATIME: 17053ccfa83cSahrens mntopt_on = MNTOPT_ATIME; 17063ccfa83cSahrens mntopt_off = MNTOPT_NOATIME; 17073ccfa83cSahrens break; 17083ccfa83cSahrens 17093ccfa83cSahrens case ZFS_PROP_DEVICES: 17103ccfa83cSahrens mntopt_on = MNTOPT_DEVICES; 17113ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES; 17123ccfa83cSahrens break; 17133ccfa83cSahrens 17143ccfa83cSahrens case ZFS_PROP_EXEC: 17153ccfa83cSahrens mntopt_on = MNTOPT_EXEC; 17163ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC; 17173ccfa83cSahrens break; 17183ccfa83cSahrens 17193ccfa83cSahrens case ZFS_PROP_READONLY: 17203ccfa83cSahrens mntopt_on = MNTOPT_RO; 17213ccfa83cSahrens mntopt_off = MNTOPT_RW; 17223ccfa83cSahrens break; 17233ccfa83cSahrens 17243ccfa83cSahrens case ZFS_PROP_SETUID: 17253ccfa83cSahrens mntopt_on = MNTOPT_SETUID; 17263ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID; 17273ccfa83cSahrens break; 17283ccfa83cSahrens 17293ccfa83cSahrens case ZFS_PROP_XATTR: 17303ccfa83cSahrens mntopt_on = MNTOPT_XATTR; 17313ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR; 17323ccfa83cSahrens break; 1733da6c28aaSamw 1734da6c28aaSamw case ZFS_PROP_NBMAND: 1735da6c28aaSamw mntopt_on = MNTOPT_NBMAND; 1736da6c28aaSamw mntopt_off = MNTOPT_NONBMAND; 1737da6c28aaSamw break; 17383ccfa83cSahrens } 17393ccfa83cSahrens 17403bb79becSeschrock /* 17413bb79becSeschrock * Because looking up the mount options is potentially expensive 17423bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until 17433bb79becSeschrock * we're looking up a property which requires its presence. 17443bb79becSeschrock */ 17453bb79becSeschrock if (!zhp->zfs_mntcheck && 17463ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 1747ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 1748ebedde84SEric Taylor struct mnttab entry; 17493bb79becSeschrock 1750ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 1751ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl, 17523ccfa83cSahrens entry.mnt_mntopts); 17533ccfa83cSahrens if (zhp->zfs_mntopts == NULL) 17543bb79becSeschrock return (-1); 17553ccfa83cSahrens } 17563bb79becSeschrock 17573bb79becSeschrock zhp->zfs_mntcheck = B_TRUE; 17583bb79becSeschrock } 17593bb79becSeschrock 1760fa9e4066Sahrens if (zhp->zfs_mntopts == NULL) 1761fa9e4066Sahrens mnt.mnt_mntopts = ""; 1762fa9e4066Sahrens else 1763fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1764fa9e4066Sahrens 1765fa9e4066Sahrens switch (prop) { 1766fa9e4066Sahrens case ZFS_PROP_ATIME: 1767fa9e4066Sahrens case ZFS_PROP_DEVICES: 1768fa9e4066Sahrens case ZFS_PROP_EXEC: 17693ccfa83cSahrens case ZFS_PROP_READONLY: 17703ccfa83cSahrens case ZFS_PROP_SETUID: 17713ccfa83cSahrens case ZFS_PROP_XATTR: 1772da6c28aaSamw case ZFS_PROP_NBMAND: 177399653d4eSeschrock *val = getprop_uint64(zhp, prop, source); 1774fa9e4066Sahrens 177592241e0bSTom Erickson if (received) 177692241e0bSTom Erickson break; 177792241e0bSTom Erickson 17783ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 177999653d4eSeschrock *val = B_TRUE; 1780fa9e4066Sahrens if (src) 1781990b4856Slling *src = ZPROP_SRC_TEMPORARY; 17823ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 178399653d4eSeschrock *val = B_FALSE; 1784fa9e4066Sahrens if (src) 1785990b4856Slling *src = ZPROP_SRC_TEMPORARY; 1786fa9e4066Sahrens } 178799653d4eSeschrock break; 1788fa9e4066Sahrens 17893ccfa83cSahrens case ZFS_PROP_CANMOUNT: 1790d41c4376SMark J Musante case ZFS_PROP_VOLSIZE: 1791fa9e4066Sahrens case ZFS_PROP_QUOTA: 1792a9799022Sck153898 case ZFS_PROP_REFQUOTA: 1793fa9e4066Sahrens case ZFS_PROP_RESERVATION: 1794a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 1795a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source); 179692241e0bSTom Erickson 179792241e0bSTom Erickson if (*source == NULL) { 179892241e0bSTom Erickson /* not default, must be local */ 1799fa9e4066Sahrens *source = zhp->zfs_name; 180092241e0bSTom Erickson } 180199653d4eSeschrock break; 1802fa9e4066Sahrens 1803fa9e4066Sahrens case ZFS_PROP_MOUNTED: 180499653d4eSeschrock *val = (zhp->zfs_mntopts != NULL); 180599653d4eSeschrock break; 1806fa9e4066Sahrens 180739c23413Seschrock case ZFS_PROP_NUMCLONES: 180839c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 180939c23413Seschrock break; 181039c23413Seschrock 1811bd00f61bSrm160521 case ZFS_PROP_VERSION: 1812de8267e0Stimh case ZFS_PROP_NORMALIZE: 1813de8267e0Stimh case ZFS_PROP_UTF8ONLY: 1814de8267e0Stimh case ZFS_PROP_CASE: 1815de8267e0Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 1816de8267e0Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 18173d7934e1Srm160521 return (-1); 1818bd00f61bSrm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1819de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 1820de8267e0Stimh zcmd_free_nvlists(&zc); 1821f4b94bdeSMatthew Ahrens return (-1); 1822bd00f61bSrm160521 } 1823de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 1824de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 1825de8267e0Stimh val) != 0) { 1826de8267e0Stimh zcmd_free_nvlists(&zc); 1827f4b94bdeSMatthew Ahrens return (-1); 1828de8267e0Stimh } 182996510749Stimh if (zplprops) 183096510749Stimh nvlist_free(zplprops); 1831de8267e0Stimh zcmd_free_nvlists(&zc); 1832bd00f61bSrm160521 break; 1833bd00f61bSrm160521 1834fa9e4066Sahrens default: 1835e7437265Sahrens switch (zfs_prop_get_type(prop)) { 183691ebeef5Sahrens case PROP_TYPE_NUMBER: 183791ebeef5Sahrens case PROP_TYPE_INDEX: 1838e7437265Sahrens *val = getprop_uint64(zhp, prop, source); 183974e7dc98SMatthew Ahrens /* 184014843421SMatthew Ahrens * If we tried to use a default value for a 184174e7dc98SMatthew Ahrens * readonly property, it means that it was not 184231f572c2STom Erickson * present. 184374e7dc98SMatthew Ahrens */ 184474e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) && 184531f572c2STom Erickson *source != NULL && (*source)[0] == '\0') { 184631f572c2STom Erickson *source = NULL; 184774e7dc98SMatthew Ahrens } 1848e7437265Sahrens break; 1849e7437265Sahrens 185091ebeef5Sahrens case PROP_TYPE_STRING: 1851e7437265Sahrens default: 185299653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 185399653d4eSeschrock "cannot get non-numeric property")); 185499653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 185599653d4eSeschrock dgettext(TEXT_DOMAIN, "internal error"))); 1856fa9e4066Sahrens } 1857e7437265Sahrens } 1858fa9e4066Sahrens 1859fa9e4066Sahrens return (0); 1860fa9e4066Sahrens } 1861fa9e4066Sahrens 1862fa9e4066Sahrens /* 1863fa9e4066Sahrens * Calculate the source type, given the raw source string. 1864fa9e4066Sahrens */ 1865fa9e4066Sahrens static void 1866990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 1867fa9e4066Sahrens char *statbuf, size_t statlen) 1868fa9e4066Sahrens { 1869990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 1870fa9e4066Sahrens return; 1871fa9e4066Sahrens 1872fa9e4066Sahrens if (source == NULL) { 1873990b4856Slling *srctype = ZPROP_SRC_NONE; 1874fa9e4066Sahrens } else if (source[0] == '\0') { 1875990b4856Slling *srctype = ZPROP_SRC_DEFAULT; 187692241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 187792241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED; 1878fa9e4066Sahrens } else { 1879fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 1880990b4856Slling *srctype = ZPROP_SRC_LOCAL; 1881fa9e4066Sahrens } else { 1882fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen); 1883990b4856Slling *srctype = ZPROP_SRC_INHERITED; 1884fa9e4066Sahrens } 1885fa9e4066Sahrens } 1886fa9e4066Sahrens 1887fa9e4066Sahrens } 1888fa9e4066Sahrens 188992241e0bSTom Erickson int 189092241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 189192241e0bSTom Erickson size_t proplen, boolean_t literal) 189292241e0bSTom Erickson { 189392241e0bSTom Erickson zfs_prop_t prop; 189492241e0bSTom Erickson int err = 0; 189592241e0bSTom Erickson 189692241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 189792241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 189892241e0bSTom Erickson return (-1); 189992241e0bSTom Erickson 190092241e0bSTom Erickson prop = zfs_name_to_prop(propname); 190192241e0bSTom Erickson 190292241e0bSTom Erickson if (prop != ZPROP_INVAL) { 190392241e0bSTom Erickson uint64_t cookie; 190492241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 190592241e0bSTom Erickson return (-1); 190692241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie); 190792241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen, 190892241e0bSTom Erickson NULL, NULL, 0, literal); 190992241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie); 191092241e0bSTom Erickson } else { 191192241e0bSTom Erickson nvlist_t *propval; 191292241e0bSTom Erickson char *recvdval; 191392241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 191492241e0bSTom Erickson propname, &propval) != 0) 191592241e0bSTom Erickson return (-1); 191692241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE, 191792241e0bSTom Erickson &recvdval) == 0); 191892241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen); 191992241e0bSTom Erickson } 192092241e0bSTom Erickson 192192241e0bSTom Erickson return (err == 0 ? 0 : -1); 192292241e0bSTom Erickson } 192392241e0bSTom Erickson 192419b94df9SMatthew Ahrens static int 192519b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 192619b94df9SMatthew Ahrens { 192719b94df9SMatthew Ahrens nvlist_t *value; 192819b94df9SMatthew Ahrens nvpair_t *pair; 192919b94df9SMatthew Ahrens 193019b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp); 193119b94df9SMatthew Ahrens if (value == NULL) 193219b94df9SMatthew Ahrens return (-1); 193319b94df9SMatthew Ahrens 193419b94df9SMatthew Ahrens propbuf[0] = '\0'; 193519b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 193619b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) { 193719b94df9SMatthew Ahrens if (propbuf[0] != '\0') 193819b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen); 193919b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen); 194019b94df9SMatthew Ahrens } 194119b94df9SMatthew Ahrens 194219b94df9SMatthew Ahrens return (0); 194319b94df9SMatthew Ahrens } 194419b94df9SMatthew Ahrens 194519b94df9SMatthew Ahrens struct get_clones_arg { 194619b94df9SMatthew Ahrens uint64_t numclones; 194719b94df9SMatthew Ahrens nvlist_t *value; 194819b94df9SMatthew Ahrens const char *origin; 194919b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 195019b94df9SMatthew Ahrens }; 195119b94df9SMatthew Ahrens 195219b94df9SMatthew Ahrens int 195319b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg) 195419b94df9SMatthew Ahrens { 195519b94df9SMatthew Ahrens struct get_clones_arg *gca = arg; 195619b94df9SMatthew Ahrens 195719b94df9SMatthew Ahrens if (gca->numclones == 0) { 195819b94df9SMatthew Ahrens zfs_close(zhp); 195919b94df9SMatthew Ahrens return (0); 196019b94df9SMatthew Ahrens } 196119b94df9SMatthew Ahrens 196219b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 196319b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0) 196419b94df9SMatthew Ahrens goto out; 196519b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) { 196619b94df9SMatthew Ahrens if (nvlist_add_boolean(gca->value, zfs_get_name(zhp)) != 0) { 196719b94df9SMatthew Ahrens zfs_close(zhp); 196819b94df9SMatthew Ahrens return (no_memory(zhp->zfs_hdl)); 196919b94df9SMatthew Ahrens } 197019b94df9SMatthew Ahrens gca->numclones--; 197119b94df9SMatthew Ahrens } 197219b94df9SMatthew Ahrens 197319b94df9SMatthew Ahrens out: 197419b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca); 197519b94df9SMatthew Ahrens zfs_close(zhp); 197619b94df9SMatthew Ahrens return (0); 197719b94df9SMatthew Ahrens } 197819b94df9SMatthew Ahrens 197919b94df9SMatthew Ahrens nvlist_t * 198019b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp) 198119b94df9SMatthew Ahrens { 198219b94df9SMatthew Ahrens nvlist_t *nv, *value; 198319b94df9SMatthew Ahrens 198419b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props, 198519b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 198619b94df9SMatthew Ahrens struct get_clones_arg gca; 198719b94df9SMatthew Ahrens 198819b94df9SMatthew Ahrens /* 198919b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able 199019b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating. 199119b94df9SMatthew Ahrens */ 199219b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 199319b94df9SMatthew Ahrens return (NULL); 199419b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 199519b94df9SMatthew Ahrens return (NULL); 199619b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 199719b94df9SMatthew Ahrens nvlist_free(nv); 199819b94df9SMatthew Ahrens return (NULL); 199919b94df9SMatthew Ahrens } 200019b94df9SMatthew Ahrens 200119b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 200219b94df9SMatthew Ahrens gca.value = value; 200319b94df9SMatthew Ahrens gca.origin = zhp->zfs_name; 200419b94df9SMatthew Ahrens 200519b94df9SMatthew Ahrens if (gca.numclones != 0) { 200619b94df9SMatthew Ahrens zfs_handle_t *root; 200719b94df9SMatthew Ahrens char pool[ZFS_MAXNAMELEN]; 200819b94df9SMatthew Ahrens char *cp = pool; 200919b94df9SMatthew Ahrens 201019b94df9SMatthew Ahrens /* get the pool name */ 201119b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 201219b94df9SMatthew Ahrens (void) strsep(&cp, "/@"); 201319b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool, 201419b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM); 201519b94df9SMatthew Ahrens 201619b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca); 201719b94df9SMatthew Ahrens } 201819b94df9SMatthew Ahrens 201919b94df9SMatthew Ahrens if (gca.numclones != 0 || 202019b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 202119b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props, 202219b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 202319b94df9SMatthew Ahrens nvlist_free(nv); 202419b94df9SMatthew Ahrens nvlist_free(value); 202519b94df9SMatthew Ahrens return (NULL); 202619b94df9SMatthew Ahrens } 202719b94df9SMatthew Ahrens nvlist_free(nv); 202819b94df9SMatthew Ahrens nvlist_free(value); 202919b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, 203019b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); 203119b94df9SMatthew Ahrens } 203219b94df9SMatthew Ahrens 203319b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); 203419b94df9SMatthew Ahrens 203519b94df9SMatthew Ahrens return (value); 203619b94df9SMatthew Ahrens } 203719b94df9SMatthew Ahrens 2038fa9e4066Sahrens /* 2039fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2040fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2041fa9e4066Sahrens * human-readable form. 2042fa9e4066Sahrens * 2043fa9e4066Sahrens * Returns 0 on success, or -1 on error. 2044fa9e4066Sahrens */ 2045fa9e4066Sahrens int 2046fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2047990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2048fa9e4066Sahrens { 2049fa9e4066Sahrens char *source = NULL; 2050fa9e4066Sahrens uint64_t val; 2051fa9e4066Sahrens char *str; 2052e9dbad6fSeschrock const char *strval; 205392241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2054fa9e4066Sahrens 2055fa9e4066Sahrens /* 2056fa9e4066Sahrens * Check to see if this property applies to our object 2057fa9e4066Sahrens */ 2058fa9e4066Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2059fa9e4066Sahrens return (-1); 2060fa9e4066Sahrens 206192241e0bSTom Erickson if (received && zfs_prop_readonly(prop)) 206292241e0bSTom Erickson return (-1); 206392241e0bSTom Erickson 2064fa9e4066Sahrens if (src) 2065990b4856Slling *src = ZPROP_SRC_NONE; 2066fa9e4066Sahrens 2067fa9e4066Sahrens switch (prop) { 2068fa9e4066Sahrens case ZFS_PROP_CREATION: 2069fa9e4066Sahrens /* 2070fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert 2071fa9e4066Sahrens * this into a string unless 'literal' is specified. 2072fa9e4066Sahrens */ 2073fa9e4066Sahrens { 2074a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source); 2075a2eea2e1Sahrens time_t time = (time_t)val; 2076fa9e4066Sahrens struct tm t; 2077fa9e4066Sahrens 2078fa9e4066Sahrens if (literal || 2079fa9e4066Sahrens localtime_r(&time, &t) == NULL || 2080fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2081fa9e4066Sahrens &t) == 0) 2082a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2083fa9e4066Sahrens } 2084fa9e4066Sahrens break; 2085fa9e4066Sahrens 2086fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT: 2087fa9e4066Sahrens /* 2088fa9e4066Sahrens * Getting the precise mountpoint can be tricky. 2089fa9e4066Sahrens * 2090fa9e4066Sahrens * - for 'none' or 'legacy', return those values. 2091fa9e4066Sahrens * - for inherited mountpoints, we want to take everything 2092fa9e4066Sahrens * after our ancestor and append it to the inherited value. 2093fa9e4066Sahrens * 2094fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that 2095fa9e4066Sahrens * root to any values we return. 2096fa9e4066Sahrens */ 209729ab75c9Srm160521 20987f7322feSeschrock str = getprop_string(zhp, prop, &source); 2099fa9e4066Sahrens 2100b21718f0Sgw25295 if (str[0] == '/') { 210129ab75c9Srm160521 char buf[MAXPATHLEN]; 210229ab75c9Srm160521 char *root = buf; 2103a79992aaSTom Erickson const char *relpath; 2104fa9e4066Sahrens 2105a79992aaSTom Erickson /* 2106a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset 2107a79992aaSTom Erickson * with a received value, the source will be the path of 2108a79992aaSTom Erickson * the dataset we inherit from. If source is 2109a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2110a79992aaSTom Erickson * inherited. 2111a79992aaSTom Erickson */ 2112a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2113a79992aaSTom Erickson relpath = ""; 2114a79992aaSTom Erickson } else { 2115a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source); 2116fa9e4066Sahrens if (relpath[0] == '/') 2117fa9e4066Sahrens relpath++; 2118a79992aaSTom Erickson } 2119b21718f0Sgw25295 212029ab75c9Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 212129ab75c9Srm160521 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL)) || 212229ab75c9Srm160521 (strcmp(root, "-") == 0)) 212329ab75c9Srm160521 root[0] = '\0'; 2124b21718f0Sgw25295 /* 2125b21718f0Sgw25295 * Special case an alternate root of '/'. This will 2126b21718f0Sgw25295 * avoid having multiple leading slashes in the 2127b21718f0Sgw25295 * mountpoint path. 2128b21718f0Sgw25295 */ 2129b21718f0Sgw25295 if (strcmp(root, "/") == 0) 2130b21718f0Sgw25295 root++; 2131b21718f0Sgw25295 2132b21718f0Sgw25295 /* 2133b21718f0Sgw25295 * If the mountpoint is '/' then skip over this 2134b21718f0Sgw25295 * if we are obtaining either an alternate root or 2135b21718f0Sgw25295 * an inherited mountpoint. 2136b21718f0Sgw25295 */ 2137b21718f0Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 2138b21718f0Sgw25295 relpath[0] != '\0')) 21397f7322feSeschrock str++; 2140fa9e4066Sahrens 2141fa9e4066Sahrens if (relpath[0] == '\0') 2142fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s", 21437f7322feSeschrock root, str); 2144fa9e4066Sahrens else 2145fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 21467f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/", 2147fa9e4066Sahrens relpath); 2148fa9e4066Sahrens } else { 2149fa9e4066Sahrens /* 'legacy' or 'none' */ 21507f7322feSeschrock (void) strlcpy(propbuf, str, proplen); 2151fa9e4066Sahrens } 2152fa9e4066Sahrens 2153fa9e4066Sahrens break; 2154fa9e4066Sahrens 2155fa9e4066Sahrens case ZFS_PROP_ORIGIN: 2156a2eea2e1Sahrens (void) strlcpy(propbuf, getprop_string(zhp, prop, &source), 2157fa9e4066Sahrens proplen); 2158fa9e4066Sahrens /* 2159fa9e4066Sahrens * If there is no parent at all, return failure to indicate that 2160fa9e4066Sahrens * it doesn't apply to this dataset. 2161fa9e4066Sahrens */ 2162fa9e4066Sahrens if (propbuf[0] == '\0') 2163fa9e4066Sahrens return (-1); 2164fa9e4066Sahrens break; 2165fa9e4066Sahrens 216619b94df9SMatthew Ahrens case ZFS_PROP_CLONES: 216719b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0) 216819b94df9SMatthew Ahrens return (-1); 216919b94df9SMatthew Ahrens break; 217019b94df9SMatthew Ahrens 2171fa9e4066Sahrens case ZFS_PROP_QUOTA: 2172a9799022Sck153898 case ZFS_PROP_REFQUOTA: 2173fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2174a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 2175a9799022Sck153898 217699653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 217799653d4eSeschrock return (-1); 2178fa9e4066Sahrens 2179fa9e4066Sahrens /* 2180fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none' 2181fa9e4066Sahrens * (unless literal is set), and indicate that it's the default 2182fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate 2183fa9e4066Sahrens * that its set locally. 2184fa9e4066Sahrens */ 2185fa9e4066Sahrens if (val == 0) { 2186fa9e4066Sahrens if (literal) 2187fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen); 2188fa9e4066Sahrens else 2189fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen); 2190fa9e4066Sahrens } else { 2191fa9e4066Sahrens if (literal) 21925ad82045Snd150628 (void) snprintf(propbuf, proplen, "%llu", 21935ad82045Snd150628 (u_longlong_t)val); 2194fa9e4066Sahrens else 2195fa9e4066Sahrens zfs_nicenum(val, propbuf, proplen); 2196fa9e4066Sahrens } 2197fa9e4066Sahrens break; 2198fa9e4066Sahrens 2199187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO: 2200fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO: 220199653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 220299653d4eSeschrock return (-1); 2203b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux", 2204b24ab676SJeff Bonwick (u_longlong_t)(val / 100), 2205b24ab676SJeff Bonwick (u_longlong_t)(val % 100)); 2206fa9e4066Sahrens break; 2207fa9e4066Sahrens 2208fa9e4066Sahrens case ZFS_PROP_TYPE: 2209fa9e4066Sahrens switch (zhp->zfs_type) { 2210fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 2211fa9e4066Sahrens str = "filesystem"; 2212fa9e4066Sahrens break; 2213fa9e4066Sahrens case ZFS_TYPE_VOLUME: 2214fa9e4066Sahrens str = "volume"; 2215fa9e4066Sahrens break; 2216fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 2217fa9e4066Sahrens str = "snapshot"; 2218fa9e4066Sahrens break; 2219fa9e4066Sahrens default: 222099653d4eSeschrock abort(); 2221fa9e4066Sahrens } 2222fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2223fa9e4066Sahrens break; 2224fa9e4066Sahrens 2225fa9e4066Sahrens case ZFS_PROP_MOUNTED: 2226fa9e4066Sahrens /* 2227fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described 2228fa9e4066Sahrens * whether the filesystem is currently mounted. Even though 2229fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off" 2230fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no". 2231fa9e4066Sahrens */ 223299653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 223399653d4eSeschrock src, &source, &val) != 0) 223499653d4eSeschrock return (-1); 223599653d4eSeschrock if (val) 2236fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen); 2237fa9e4066Sahrens else 2238fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen); 2239fa9e4066Sahrens break; 2240fa9e4066Sahrens 2241fa9e4066Sahrens case ZFS_PROP_NAME: 2242fa9e4066Sahrens /* 2243fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the 2244fa9e4066Sahrens * dataset name. It is presented as a real property to simplify 2245fa9e4066Sahrens * consumers. 2246fa9e4066Sahrens */ 2247fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2248fa9e4066Sahrens break; 2249fa9e4066Sahrens 22504201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 22514201a95eSRic Aleshire { 22524201a95eSRic Aleshire m_label_t *new_sl = NULL; 22534201a95eSRic Aleshire char *ascii = NULL; /* human readable label */ 22544201a95eSRic Aleshire 22554201a95eSRic Aleshire (void) strlcpy(propbuf, 22564201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen); 22574201a95eSRic Aleshire 22584201a95eSRic Aleshire if (literal || (strcasecmp(propbuf, 22594201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0)) 22604201a95eSRic Aleshire break; 22614201a95eSRic Aleshire 22624201a95eSRic Aleshire /* 22634201a95eSRic Aleshire * Try to translate the internal hex string to 22644201a95eSRic Aleshire * human-readable output. If there are any 22654201a95eSRic Aleshire * problems just use the hex string. 22664201a95eSRic Aleshire */ 22674201a95eSRic Aleshire 22684201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL, 22694201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) { 22704201a95eSRic Aleshire m_label_free(new_sl); 22714201a95eSRic Aleshire break; 22724201a95eSRic Aleshire } 22734201a95eSRic Aleshire 22744201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL, 22754201a95eSRic Aleshire DEF_NAMES) != 0) { 22764201a95eSRic Aleshire if (ascii) 22774201a95eSRic Aleshire free(ascii); 22784201a95eSRic Aleshire m_label_free(new_sl); 22794201a95eSRic Aleshire break; 22804201a95eSRic Aleshire } 22814201a95eSRic Aleshire m_label_free(new_sl); 22824201a95eSRic Aleshire 22834201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen); 22844201a95eSRic Aleshire free(ascii); 22854201a95eSRic Aleshire } 22864201a95eSRic Aleshire break; 22874201a95eSRic Aleshire 2288f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID: 2289f0f3ef5aSGarrett D'Amore /* 2290f0f3ef5aSGarrett D'Amore * GUIDs are stored as numbers, but they are identifiers. 2291f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty 2292f0f3ef5aSGarrett D'Amore * printing mangles the ID into a truncated and useless value. 2293f0f3ef5aSGarrett D'Amore */ 2294f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2295f0f3ef5aSGarrett D'Amore return (-1); 2296f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2297f0f3ef5aSGarrett D'Amore break; 2298f0f3ef5aSGarrett D'Amore 2299fa9e4066Sahrens default: 2300e7437265Sahrens switch (zfs_prop_get_type(prop)) { 230191ebeef5Sahrens case PROP_TYPE_NUMBER: 2302e7437265Sahrens if (get_numeric_property(zhp, prop, src, 2303e7437265Sahrens &source, &val) != 0) 2304e7437265Sahrens return (-1); 2305e7437265Sahrens if (literal) 2306e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu", 2307e7437265Sahrens (u_longlong_t)val); 2308e7437265Sahrens else 2309e7437265Sahrens zfs_nicenum(val, propbuf, proplen); 2310e7437265Sahrens break; 2311e7437265Sahrens 231291ebeef5Sahrens case PROP_TYPE_STRING: 2313e7437265Sahrens (void) strlcpy(propbuf, 2314e7437265Sahrens getprop_string(zhp, prop, &source), proplen); 2315e7437265Sahrens break; 2316e7437265Sahrens 231791ebeef5Sahrens case PROP_TYPE_INDEX: 2318fe192f76Sahrens if (get_numeric_property(zhp, prop, src, 2319fe192f76Sahrens &source, &val) != 0) 2320fe192f76Sahrens return (-1); 2321fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2322e7437265Sahrens return (-1); 2323e7437265Sahrens (void) strlcpy(propbuf, strval, proplen); 2324e7437265Sahrens break; 2325e7437265Sahrens 2326e7437265Sahrens default: 232799653d4eSeschrock abort(); 2328fa9e4066Sahrens } 2329e7437265Sahrens } 2330fa9e4066Sahrens 2331fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2332fa9e4066Sahrens 2333fa9e4066Sahrens return (0); 2334fa9e4066Sahrens } 2335fa9e4066Sahrens 2336fa9e4066Sahrens /* 2337fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that 2338fa9e4066Sahrens * the given property is the appropriate type; should only be used with 2339fa9e4066Sahrens * hard-coded property types. 2340fa9e4066Sahrens */ 2341fa9e4066Sahrens uint64_t 2342fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2343fa9e4066Sahrens { 2344fa9e4066Sahrens char *source; 234599653d4eSeschrock uint64_t val; 2346fa9e4066Sahrens 23473cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 234899653d4eSeschrock 234999653d4eSeschrock return (val); 2350fa9e4066Sahrens } 2351fa9e4066Sahrens 23527b97dc1aSrm160521 int 23537b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 23547b97dc1aSrm160521 { 23557b97dc1aSrm160521 char buf[64]; 23567b97dc1aSrm160521 235714843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 23587b97dc1aSrm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 23597b97dc1aSrm160521 } 23607b97dc1aSrm160521 2361fa9e4066Sahrens /* 2362fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2363fa9e4066Sahrens */ 2364fa9e4066Sahrens int 2365fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2366990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen) 2367fa9e4066Sahrens { 2368fa9e4066Sahrens char *source; 2369fa9e4066Sahrens 2370fa9e4066Sahrens /* 2371fa9e4066Sahrens * Check to see if this property applies to our object 2372fa9e4066Sahrens */ 2373e45ce728Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2374ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 237599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 237699653d4eSeschrock zfs_prop_to_name(prop))); 2377e45ce728Sahrens } 2378fa9e4066Sahrens 2379fa9e4066Sahrens if (src) 2380990b4856Slling *src = ZPROP_SRC_NONE; 2381fa9e4066Sahrens 238299653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 238399653d4eSeschrock return (-1); 2384fa9e4066Sahrens 2385fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2386fa9e4066Sahrens 2387fa9e4066Sahrens return (0); 2388fa9e4066Sahrens } 2389fa9e4066Sahrens 239014843421SMatthew Ahrens static int 239114843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 239214843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp) 239314843421SMatthew Ahrens { 239414843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL; 239514843421SMatthew Ahrens idmap_stat status; 239614843421SMatthew Ahrens int err = EINVAL; 239714843421SMatthew Ahrens 23981fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 239914843421SMatthew Ahrens goto out; 240014843421SMatthew Ahrens 240114843421SMatthew Ahrens if (isuser) { 240214843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id, 240314843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 240414843421SMatthew Ahrens } else { 240514843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id, 240614843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 240714843421SMatthew Ahrens } 240814843421SMatthew Ahrens if (err == IDMAP_SUCCESS && 240914843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 241014843421SMatthew Ahrens status == IDMAP_SUCCESS) 241114843421SMatthew Ahrens err = 0; 241214843421SMatthew Ahrens else 241314843421SMatthew Ahrens err = EINVAL; 241414843421SMatthew Ahrens out: 241514843421SMatthew Ahrens if (get_hdl) 241614843421SMatthew Ahrens idmap_get_destroy(get_hdl); 241714843421SMatthew Ahrens return (err); 241814843421SMatthew Ahrens } 241914843421SMatthew Ahrens 242014843421SMatthew Ahrens /* 242114843421SMatthew Ahrens * convert the propname into parameters needed by kernel 242214843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 242314843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 242414843421SMatthew Ahrens */ 242514843421SMatthew Ahrens static int 242614843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned, 242714843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 242814843421SMatthew Ahrens { 242914843421SMatthew Ahrens zfs_userquota_prop_t type; 243014843421SMatthew Ahrens char *cp, *end; 24313b12c289SMatthew Ahrens char *numericsid = NULL; 243214843421SMatthew Ahrens boolean_t isuser; 243314843421SMatthew Ahrens 243414843421SMatthew Ahrens domain[0] = '\0'; 243514843421SMatthew Ahrens 243614843421SMatthew Ahrens /* Figure out the property type ({user|group}{quota|space}) */ 243714843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 243814843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type], 243914843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0) 244014843421SMatthew Ahrens break; 244114843421SMatthew Ahrens } 244214843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS) 244314843421SMatthew Ahrens return (EINVAL); 244414843421SMatthew Ahrens *typep = type; 244514843421SMatthew Ahrens 244614843421SMatthew Ahrens isuser = (type == ZFS_PROP_USERQUOTA || 244714843421SMatthew Ahrens type == ZFS_PROP_USERUSED); 244814843421SMatthew Ahrens 244914843421SMatthew Ahrens cp = strchr(propname, '@') + 1; 245014843421SMatthew Ahrens 245114843421SMatthew Ahrens if (strchr(cp, '@')) { 245214843421SMatthew Ahrens /* 245314843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be 24543b12c289SMatthew Ahrens * turned into S-1-domainID-RID. 245514843421SMatthew Ahrens */ 24563b12c289SMatthew Ahrens directory_error_t e; 245714843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 245814843421SMatthew Ahrens return (ENOENT); 24593b12c289SMatthew Ahrens if (isuser) { 24603b12c289SMatthew Ahrens e = directory_sid_from_user_name(NULL, 24613b12c289SMatthew Ahrens cp, &numericsid); 24623b12c289SMatthew Ahrens } else { 24633b12c289SMatthew Ahrens e = directory_sid_from_group_name(NULL, 24643b12c289SMatthew Ahrens cp, &numericsid); 24653b12c289SMatthew Ahrens } 24663b12c289SMatthew Ahrens if (e != NULL) { 24673b12c289SMatthew Ahrens directory_error_free(e); 246814843421SMatthew Ahrens return (ENOENT); 24693b12c289SMatthew Ahrens } 24703b12c289SMatthew Ahrens if (numericsid == NULL) 247114843421SMatthew Ahrens return (ENOENT); 24723b12c289SMatthew Ahrens cp = numericsid; 24733b12c289SMatthew Ahrens /* will be further decoded below */ 24743b12c289SMatthew Ahrens } 24753b12c289SMatthew Ahrens 24763b12c289SMatthew Ahrens if (strncmp(cp, "S-1-", 4) == 0) { 247714843421SMatthew Ahrens /* It's a numeric SID (eg "S-1-234-567-89") */ 24783b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen); 247914843421SMatthew Ahrens cp = strrchr(domain, '-'); 248014843421SMatthew Ahrens *cp = '\0'; 248114843421SMatthew Ahrens cp++; 248214843421SMatthew Ahrens 248314843421SMatthew Ahrens errno = 0; 248414843421SMatthew Ahrens *ridp = strtoull(cp, &end, 10); 24853b12c289SMatthew Ahrens if (numericsid) { 24863b12c289SMatthew Ahrens free(numericsid); 24873b12c289SMatthew Ahrens numericsid = NULL; 24883b12c289SMatthew Ahrens } 2489777badbaSMatthew Ahrens if (errno != 0 || *end != '\0') 249014843421SMatthew Ahrens return (EINVAL); 249114843421SMatthew Ahrens } else if (!isdigit(*cp)) { 249214843421SMatthew Ahrens /* 249314843421SMatthew Ahrens * It's a user/group name (eg "user") that needs to be 249414843421SMatthew Ahrens * turned into a uid/gid 249514843421SMatthew Ahrens */ 249614843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 249714843421SMatthew Ahrens return (ENOENT); 249814843421SMatthew Ahrens if (isuser) { 249914843421SMatthew Ahrens struct passwd *pw; 250014843421SMatthew Ahrens pw = getpwnam(cp); 250114843421SMatthew Ahrens if (pw == NULL) 250214843421SMatthew Ahrens return (ENOENT); 250314843421SMatthew Ahrens *ridp = pw->pw_uid; 250414843421SMatthew Ahrens } else { 250514843421SMatthew Ahrens struct group *gr; 250614843421SMatthew Ahrens gr = getgrnam(cp); 250714843421SMatthew Ahrens if (gr == NULL) 250814843421SMatthew Ahrens return (ENOENT); 250914843421SMatthew Ahrens *ridp = gr->gr_gid; 251014843421SMatthew Ahrens } 251114843421SMatthew Ahrens } else { 251214843421SMatthew Ahrens /* It's a user/group ID (eg "12345"). */ 251314843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10); 251414843421SMatthew Ahrens idmap_rid_t rid; 251514843421SMatthew Ahrens char *mapdomain; 251614843421SMatthew Ahrens 251714843421SMatthew Ahrens if (*end != '\0') 251814843421SMatthew Ahrens return (EINVAL); 251914843421SMatthew Ahrens if (id > MAXUID) { 252014843421SMatthew Ahrens /* It's an ephemeral ID. */ 252114843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser, 252214843421SMatthew Ahrens &mapdomain, &rid) != 0) 252314843421SMatthew Ahrens return (ENOENT); 25243b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen); 252514843421SMatthew Ahrens *ridp = rid; 252614843421SMatthew Ahrens } else { 252714843421SMatthew Ahrens *ridp = id; 252814843421SMatthew Ahrens } 252914843421SMatthew Ahrens } 253014843421SMatthew Ahrens 25313b12c289SMatthew Ahrens ASSERT3P(numericsid, ==, NULL); 253214843421SMatthew Ahrens return (0); 253314843421SMatthew Ahrens } 253414843421SMatthew Ahrens 2535edea4b55SLin Ling static int 2536edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 2537edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep) 253814843421SMatthew Ahrens { 253914843421SMatthew Ahrens int err; 254014843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 254114843421SMatthew Ahrens 254219b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 254314843421SMatthew Ahrens 254414843421SMatthew Ahrens err = userquota_propname_decode(propname, 254514843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 2546edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 2547edea4b55SLin Ling zc.zc_objset_type = *typep; 254814843421SMatthew Ahrens if (err) 254914843421SMatthew Ahrens return (err); 255014843421SMatthew Ahrens 255114843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 255214843421SMatthew Ahrens if (err) 255314843421SMatthew Ahrens return (err); 255414843421SMatthew Ahrens 2555edea4b55SLin Ling *propvalue = zc.zc_cookie; 2556edea4b55SLin Ling return (0); 2557edea4b55SLin Ling } 2558edea4b55SLin Ling 2559edea4b55SLin Ling int 2560edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 2561edea4b55SLin Ling uint64_t *propvalue) 2562edea4b55SLin Ling { 2563edea4b55SLin Ling zfs_userquota_prop_t type; 2564edea4b55SLin Ling 2565edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 2566edea4b55SLin Ling &type)); 2567edea4b55SLin Ling } 2568edea4b55SLin Ling 2569edea4b55SLin Ling int 2570edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 2571edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal) 2572edea4b55SLin Ling { 2573edea4b55SLin Ling int err; 2574edea4b55SLin Ling uint64_t propvalue; 2575edea4b55SLin Ling zfs_userquota_prop_t type; 2576edea4b55SLin Ling 2577edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 2578edea4b55SLin Ling &type); 2579edea4b55SLin Ling 2580edea4b55SLin Ling if (err) 2581edea4b55SLin Ling return (err); 2582edea4b55SLin Ling 258314843421SMatthew Ahrens if (literal) { 2584edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue); 2585edea4b55SLin Ling } else if (propvalue == 0 && 258614843421SMatthew Ahrens (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 258714843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen); 258814843421SMatthew Ahrens } else { 2589edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen); 259014843421SMatthew Ahrens } 259114843421SMatthew Ahrens return (0); 259214843421SMatthew Ahrens } 259314843421SMatthew Ahrens 259419b94df9SMatthew Ahrens int 259519b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 259619b94df9SMatthew Ahrens uint64_t *propvalue) 259719b94df9SMatthew Ahrens { 259819b94df9SMatthew Ahrens int err; 259919b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 }; 260019b94df9SMatthew Ahrens const char *snapname; 260119b94df9SMatthew Ahrens 260219b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 260319b94df9SMatthew Ahrens 260419b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1; 260519b94df9SMatthew Ahrens if (strchr(snapname, '@')) { 260619b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 260719b94df9SMatthew Ahrens } else { 260819b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */ 260919b94df9SMatthew Ahrens char *cp; 261019b94df9SMatthew Ahrens 261119b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name, 261219b94df9SMatthew Ahrens sizeof (zc.zc_value)); 261319b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@'); 261419b94df9SMatthew Ahrens if (cp != NULL) 261519b94df9SMatthew Ahrens *cp = '\0'; 261619b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); 261719b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); 261819b94df9SMatthew Ahrens } 261919b94df9SMatthew Ahrens 262019b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); 262119b94df9SMatthew Ahrens if (err) 262219b94df9SMatthew Ahrens return (err); 262319b94df9SMatthew Ahrens 262419b94df9SMatthew Ahrens *propvalue = zc.zc_cookie; 262519b94df9SMatthew Ahrens return (0); 262619b94df9SMatthew Ahrens } 262719b94df9SMatthew Ahrens 262819b94df9SMatthew Ahrens int 262919b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 263019b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal) 263119b94df9SMatthew Ahrens { 263219b94df9SMatthew Ahrens int err; 263319b94df9SMatthew Ahrens uint64_t propvalue; 263419b94df9SMatthew Ahrens 263519b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue); 263619b94df9SMatthew Ahrens 263719b94df9SMatthew Ahrens if (err) 263819b94df9SMatthew Ahrens return (err); 263919b94df9SMatthew Ahrens 264019b94df9SMatthew Ahrens if (literal) { 264119b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue); 264219b94df9SMatthew Ahrens } else { 264319b94df9SMatthew Ahrens zfs_nicenum(propvalue, propbuf, proplen); 264419b94df9SMatthew Ahrens } 264519b94df9SMatthew Ahrens return (0); 264619b94df9SMatthew Ahrens } 264719b94df9SMatthew Ahrens 2648fa9e4066Sahrens /* 2649fa9e4066Sahrens * Returns the name of the given zfs handle. 2650fa9e4066Sahrens */ 2651fa9e4066Sahrens const char * 2652fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp) 2653fa9e4066Sahrens { 2654fa9e4066Sahrens return (zhp->zfs_name); 2655fa9e4066Sahrens } 2656fa9e4066Sahrens 2657fa9e4066Sahrens /* 2658fa9e4066Sahrens * Returns the type of the given zfs handle. 2659fa9e4066Sahrens */ 2660fa9e4066Sahrens zfs_type_t 2661fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp) 2662fa9e4066Sahrens { 2663fa9e4066Sahrens return (zhp->zfs_type); 2664fa9e4066Sahrens } 2665fa9e4066Sahrens 26667f7322feSeschrock /* 2667d41c4376SMark J Musante * Is one dataset name a child dataset of another? 2668d41c4376SMark J Musante * 2669d41c4376SMark J Musante * Needs to handle these cases: 2670d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 2671d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 2672d41c4376SMark J Musante * Descendant? No. No. No. Yes. 2673d41c4376SMark J Musante */ 2674d41c4376SMark J Musante static boolean_t 2675d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2) 2676d41c4376SMark J Musante { 2677d41c4376SMark J Musante size_t d1len = strlen(ds1); 2678d41c4376SMark J Musante 2679d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */ 2680d41c4376SMark J Musante if (strlen(ds2) < d1len) 2681d41c4376SMark J Musante return (B_FALSE); 2682d41c4376SMark J Musante 2683d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */ 2684d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 2685d41c4376SMark J Musante } 2686d41c4376SMark J Musante 2687d41c4376SMark J Musante /* 2688fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent. 268919b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the 269019b94df9SMatthew Ahrens * pool). 2691fa9e4066Sahrens */ 2692fa9e4066Sahrens static int 2693fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen) 2694fa9e4066Sahrens { 269519b94df9SMatthew Ahrens char *slashp; 2696fa9e4066Sahrens 269719b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen); 269819b94df9SMatthew Ahrens 269919b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL) 2700fa9e4066Sahrens return (-1); 270119b94df9SMatthew Ahrens *slashp = '\0'; 2702fa9e4066Sahrens 2703fa9e4066Sahrens return (0); 2704fa9e4066Sahrens } 2705fa9e4066Sahrens 2706fa9e4066Sahrens /* 27077f1f55eaSvb160487 * If accept_ancestor is false, then check to make sure that the given path has 27087f1f55eaSvb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 27097f1f55eaSvb160487 * closest existing ancestor for the given path. In prefixlen return the 27107f1f55eaSvb160487 * length of already existing prefix of the given path. We also fetch the 27117f1f55eaSvb160487 * 'zoned' property, which is used to validate property settings when creating 27127f1f55eaSvb160487 * new datasets. 2713fa9e4066Sahrens */ 2714fa9e4066Sahrens static int 27157f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 27167f1f55eaSvb160487 boolean_t accept_ancestor, int *prefixlen) 2717fa9e4066Sahrens { 2718fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2719fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 2720fa9e4066Sahrens char *slash; 27217f7322feSeschrock zfs_handle_t *zhp; 272299653d4eSeschrock char errbuf[1024]; 2723d41c4376SMark J Musante uint64_t is_zoned; 272499653d4eSeschrock 2725deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf), 2726deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2727fa9e4066Sahrens 2728fa9e4066Sahrens /* get parent, and check to see if this is just a pool */ 2729fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 273099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 273199653d4eSeschrock "missing dataset name")); 273299653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2733fa9e4066Sahrens } 2734fa9e4066Sahrens 2735fa9e4066Sahrens /* check to see if the pool exists */ 2736fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL) 2737fa9e4066Sahrens slash = parent + strlen(parent); 27388ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent); 2739fa9e4066Sahrens zc.zc_name[slash - parent] = '\0'; 274099653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2741fa9e4066Sahrens errno == ENOENT) { 274299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 274399653d4eSeschrock "no such pool '%s'"), zc.zc_name); 274499653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2745fa9e4066Sahrens } 2746fa9e4066Sahrens 2747fa9e4066Sahrens /* check to see if the parent dataset exists */ 27487f1f55eaSvb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 27497f1f55eaSvb160487 if (errno == ENOENT && accept_ancestor) { 27507f1f55eaSvb160487 /* 27517f1f55eaSvb160487 * Go deeper to find an ancestor, give up on top level. 27527f1f55eaSvb160487 */ 27537f1f55eaSvb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 27547f1f55eaSvb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 27557f1f55eaSvb160487 "no such pool '%s'"), zc.zc_name); 27567f1f55eaSvb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 27577f1f55eaSvb160487 } 27587f1f55eaSvb160487 } else if (errno == ENOENT) { 275999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 276099653d4eSeschrock "parent does not exist")); 276199653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 27627f1f55eaSvb160487 } else 276399653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 2764fa9e4066Sahrens } 2765fa9e4066Sahrens 2766d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 2767d41c4376SMark J Musante if (zoned != NULL) 2768d41c4376SMark J Musante *zoned = is_zoned; 2769d41c4376SMark J Musante 2770fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */ 2771d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 277299653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 27737f7322feSeschrock zfs_close(zhp); 2774fa9e4066Sahrens return (-1); 2775fa9e4066Sahrens } 2776fa9e4066Sahrens 2777fa9e4066Sahrens /* make sure parent is a filesystem */ 27787f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 277999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 278099653d4eSeschrock "parent is not a filesystem")); 278199653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 27827f7322feSeschrock zfs_close(zhp); 2783fa9e4066Sahrens return (-1); 2784fa9e4066Sahrens } 2785fa9e4066Sahrens 27867f7322feSeschrock zfs_close(zhp); 27877f1f55eaSvb160487 if (prefixlen != NULL) 27887f1f55eaSvb160487 *prefixlen = strlen(parent); 27897f1f55eaSvb160487 return (0); 27907f1f55eaSvb160487 } 27917f1f55eaSvb160487 27927f1f55eaSvb160487 /* 27937f1f55eaSvb160487 * Finds whether the dataset of the given type(s) exists. 27947f1f55eaSvb160487 */ 27957f1f55eaSvb160487 boolean_t 27967f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 27977f1f55eaSvb160487 { 27987f1f55eaSvb160487 zfs_handle_t *zhp; 27997f1f55eaSvb160487 2800f18faf3fSek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 28017f1f55eaSvb160487 return (B_FALSE); 28027f1f55eaSvb160487 28037f1f55eaSvb160487 /* 28047f1f55eaSvb160487 * Try to get stats for the dataset, which will tell us if it exists. 28057f1f55eaSvb160487 */ 28067f1f55eaSvb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 28077f1f55eaSvb160487 int ds_type = zhp->zfs_type; 28087f1f55eaSvb160487 28097f1f55eaSvb160487 zfs_close(zhp); 28107f1f55eaSvb160487 if (types & ds_type) 28117f1f55eaSvb160487 return (B_TRUE); 28127f1f55eaSvb160487 } 28137f1f55eaSvb160487 return (B_FALSE); 28147f1f55eaSvb160487 } 28157f1f55eaSvb160487 28167f1f55eaSvb160487 /* 28173cb34c60Sahrens * Given a path to 'target', create all the ancestors between 28183cb34c60Sahrens * the prefixlen portion of the path, and the target itself. 28193cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 28203cb34c60Sahrens */ 28213cb34c60Sahrens int 28223cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 28233cb34c60Sahrens { 28243cb34c60Sahrens zfs_handle_t *h; 28253cb34c60Sahrens char *cp; 28263cb34c60Sahrens const char *opname; 28273cb34c60Sahrens 28283cb34c60Sahrens /* make sure prefix exists */ 28293cb34c60Sahrens cp = target + prefixlen; 28303cb34c60Sahrens if (*cp != '/') { 28313cb34c60Sahrens assert(strchr(cp, '/') == NULL); 28323cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 28333cb34c60Sahrens } else { 28343cb34c60Sahrens *cp = '\0'; 28353cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 28363cb34c60Sahrens *cp = '/'; 28373cb34c60Sahrens } 28383cb34c60Sahrens if (h == NULL) 28393cb34c60Sahrens return (-1); 28403cb34c60Sahrens zfs_close(h); 28413cb34c60Sahrens 28423cb34c60Sahrens /* 28433cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems, 28443cb34c60Sahrens * up to the prefixlen-long one. 28453cb34c60Sahrens */ 28463cb34c60Sahrens for (cp = target + prefixlen + 1; 28473cb34c60Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 28483cb34c60Sahrens 28493cb34c60Sahrens *cp = '\0'; 28503cb34c60Sahrens 28513cb34c60Sahrens h = make_dataset_handle(hdl, target); 28523cb34c60Sahrens if (h) { 28533cb34c60Sahrens /* it already exists, nothing to do here */ 28543cb34c60Sahrens zfs_close(h); 28553cb34c60Sahrens continue; 28563cb34c60Sahrens } 28573cb34c60Sahrens 28583cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 28593cb34c60Sahrens NULL) != 0) { 28603cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 28613cb34c60Sahrens goto ancestorerr; 28623cb34c60Sahrens } 28633cb34c60Sahrens 28643cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 28653cb34c60Sahrens if (h == NULL) { 28663cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 28673cb34c60Sahrens goto ancestorerr; 28683cb34c60Sahrens } 28693cb34c60Sahrens 28703cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) { 28713cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 28723cb34c60Sahrens goto ancestorerr; 28733cb34c60Sahrens } 28743cb34c60Sahrens 28753cb34c60Sahrens if (zfs_share(h) != 0) { 28763cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 28773cb34c60Sahrens goto ancestorerr; 28783cb34c60Sahrens } 28793cb34c60Sahrens 28803cb34c60Sahrens zfs_close(h); 28813cb34c60Sahrens } 28823cb34c60Sahrens 28833cb34c60Sahrens return (0); 28843cb34c60Sahrens 28853cb34c60Sahrens ancestorerr: 28863cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 28873cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target); 28883cb34c60Sahrens return (-1); 28893cb34c60Sahrens } 28903cb34c60Sahrens 28913cb34c60Sahrens /* 28927f1f55eaSvb160487 * Creates non-existing ancestors of the given path. 28937f1f55eaSvb160487 */ 28947f1f55eaSvb160487 int 28957f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 28967f1f55eaSvb160487 { 28977f1f55eaSvb160487 int prefix; 28987f1f55eaSvb160487 char *path_copy; 28997f1f55eaSvb160487 int rc; 29007f1f55eaSvb160487 2901d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 29027f1f55eaSvb160487 return (-1); 29037f1f55eaSvb160487 29047f1f55eaSvb160487 if ((path_copy = strdup(path)) != NULL) { 29057f1f55eaSvb160487 rc = create_parents(hdl, path_copy, prefix); 29067f1f55eaSvb160487 free(path_copy); 29077f1f55eaSvb160487 } 29087f1f55eaSvb160487 if (path_copy == NULL || rc != 0) 29097f1f55eaSvb160487 return (-1); 29107f1f55eaSvb160487 2911fa9e4066Sahrens return (0); 2912fa9e4066Sahrens } 2913fa9e4066Sahrens 2914fa9e4066Sahrens /* 2915e9dbad6fSeschrock * Create a new filesystem or volume. 2916fa9e4066Sahrens */ 2917fa9e4066Sahrens int 291899653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 2919e9dbad6fSeschrock nvlist_t *props) 2920fa9e4066Sahrens { 2921fa9e4066Sahrens int ret; 2922fa9e4066Sahrens uint64_t size = 0; 2923fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 292499653d4eSeschrock char errbuf[1024]; 2925e9dbad6fSeschrock uint64_t zoned; 29264445fffbSMatthew Ahrens dmu_objset_type_t ost; 292799653d4eSeschrock 292899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 292999653d4eSeschrock "cannot create '%s'"), path); 2930fa9e4066Sahrens 2931fa9e4066Sahrens /* validate the path, taking care to note the extended error message */ 2932f18faf3fSek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 293399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2934fa9e4066Sahrens 2935fa9e4066Sahrens /* validate parents exist */ 29367f1f55eaSvb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 2937fa9e4066Sahrens return (-1); 2938fa9e4066Sahrens 2939fa9e4066Sahrens /* 2940fa9e4066Sahrens * The failure modes when creating a dataset of a different type over 2941fa9e4066Sahrens * one that already exists is a little strange. In particular, if you 2942fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 2943fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 2944fa9e4066Sahrens * first try to see if the dataset exists. 2945fa9e4066Sahrens */ 29464445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 294799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 294899653d4eSeschrock "dataset already exists")); 294999653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 2950fa9e4066Sahrens } 2951fa9e4066Sahrens 2952fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) 29534445fffbSMatthew Ahrens ost = DMU_OST_ZVOL; 2954fa9e4066Sahrens else 29554445fffbSMatthew Ahrens ost = DMU_OST_ZFS; 2956fa9e4066Sahrens 29570a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props, 2958b1b8ab34Slling zoned, NULL, errbuf)) == 0) 2959e9dbad6fSeschrock return (-1); 2960e9dbad6fSeschrock 2961fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) { 29625c5460e9Seschrock /* 29635c5460e9Seschrock * If we are creating a volume, the size and block size must 29645c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a 29655c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 29665c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be 29675c5460e9Seschrock * zero. 29685c5460e9Seschrock */ 2969e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props, 2970e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 2971e9dbad6fSeschrock nvlist_free(props); 297299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2973e9dbad6fSeschrock "missing volume size")); 2974e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2975fa9e4066Sahrens } 2976fa9e4066Sahrens 2977e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props, 2978e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 2979e9dbad6fSeschrock &blocksize)) != 0) { 2980e9dbad6fSeschrock if (ret == ENOENT) { 2981e9dbad6fSeschrock blocksize = zfs_prop_default_numeric( 2982e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 2983e9dbad6fSeschrock } else { 2984e9dbad6fSeschrock nvlist_free(props); 298599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2986e9dbad6fSeschrock "missing volume block size")); 2987e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 2988e9dbad6fSeschrock } 2989e9dbad6fSeschrock } 2990e9dbad6fSeschrock 2991e9dbad6fSeschrock if (size == 0) { 2992e9dbad6fSeschrock nvlist_free(props); 2993e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 2994e9dbad6fSeschrock "volume size cannot be zero")); 2995e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 29965c5460e9Seschrock } 29975c5460e9Seschrock 29985c5460e9Seschrock if (size % blocksize != 0) { 2999e9dbad6fSeschrock nvlist_free(props); 300099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3001e9dbad6fSeschrock "volume size must be a multiple of volume block " 3002e9dbad6fSeschrock "size")); 3003e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3004e9dbad6fSeschrock } 30055c5460e9Seschrock } 30065c5460e9Seschrock 3007fa9e4066Sahrens /* create the dataset */ 30084445fffbSMatthew Ahrens ret = lzc_create(path, ost, props); 30094445fffbSMatthew Ahrens nvlist_free(props); 3010e9dbad6fSeschrock 3011fa9e4066Sahrens /* check for failure */ 3012fa9e4066Sahrens if (ret != 0) { 3013fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 3014fa9e4066Sahrens (void) parent_name(path, parent, sizeof (parent)); 3015fa9e4066Sahrens 3016fa9e4066Sahrens switch (errno) { 3017fa9e4066Sahrens case ENOENT: 301899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 301999653d4eSeschrock "no such parent '%s'"), parent); 302099653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3021fa9e4066Sahrens 3022fa9e4066Sahrens case EINVAL: 302399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3024d7d4af51Smmusante "parent '%s' is not a filesystem"), parent); 302599653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3026fa9e4066Sahrens 3027fa9e4066Sahrens case EDOM: 302899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3029e9dbad6fSeschrock "volume block size must be power of 2 from " 3030e9dbad6fSeschrock "%u to %uk"), 3031fa9e4066Sahrens (uint_t)SPA_MINBLOCKSIZE, 3032fa9e4066Sahrens (uint_t)SPA_MAXBLOCKSIZE >> 10); 303399653d4eSeschrock 3034e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 303599653d4eSeschrock 303640feaa91Sahrens case ENOTSUP: 303740feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 303840feaa91Sahrens "pool must be upgraded to set this " 303940feaa91Sahrens "property or value")); 304040feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3041fa9e4066Sahrens #ifdef _ILP32 3042fa9e4066Sahrens case EOVERFLOW: 3043fa9e4066Sahrens /* 3044fa9e4066Sahrens * This platform can't address a volume this big. 3045fa9e4066Sahrens */ 304699653d4eSeschrock if (type == ZFS_TYPE_VOLUME) 304799653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 304899653d4eSeschrock errbuf)); 3049fa9e4066Sahrens #endif 305099653d4eSeschrock /* FALLTHROUGH */ 3051fa9e4066Sahrens default: 305299653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3053fa9e4066Sahrens } 3054fa9e4066Sahrens } 3055fa9e4066Sahrens 3056fa9e4066Sahrens return (0); 3057fa9e4066Sahrens } 3058fa9e4066Sahrens 3059fa9e4066Sahrens /* 3060fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 306165fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system 306265fec9f6SChristopher Siden * does not exist this function does nothing. 3063fa9e4066Sahrens */ 3064fa9e4066Sahrens int 3065842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3066fa9e4066Sahrens { 3067fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3068fa9e4066Sahrens 3069fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3070fa9e4066Sahrens 3071e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) { 3072fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3073fa9e4066Sahrens } else { 3074fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3075fa9e4066Sahrens } 3076fa9e4066Sahrens 3077842727c2SChris Kirby zc.zc_defer_destroy = defer; 307865fec9f6SChristopher Siden if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 && 307965fec9f6SChristopher Siden errno != ENOENT) { 3080ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 308199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 308299653d4eSeschrock zhp->zfs_name)); 30831d452cf5Sahrens } 3084fa9e4066Sahrens 3085fa9e4066Sahrens remove_mountpoint(zhp); 3086fa9e4066Sahrens 3087fa9e4066Sahrens return (0); 3088fa9e4066Sahrens } 3089fa9e4066Sahrens 30901d452cf5Sahrens struct destroydata { 309119b94df9SMatthew Ahrens nvlist_t *nvl; 309219b94df9SMatthew Ahrens const char *snapname; 30931d452cf5Sahrens }; 30941d452cf5Sahrens 30951d452cf5Sahrens static int 3096681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 30971d452cf5Sahrens { 30981d452cf5Sahrens struct destroydata *dd = arg; 30991d452cf5Sahrens zfs_handle_t *szhp; 31001d452cf5Sahrens char name[ZFS_MAXNAMELEN]; 3101681d9761SEric Taylor int rv = 0; 31021d452cf5Sahrens 310319b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name), 310419b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname); 31051d452cf5Sahrens 31061d452cf5Sahrens szhp = make_dataset_handle(zhp->zfs_hdl, name); 31071d452cf5Sahrens if (szhp) { 310819b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0); 31091d452cf5Sahrens zfs_close(szhp); 31101d452cf5Sahrens } 31111d452cf5Sahrens 311219b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 31133ccfa83cSahrens zfs_close(zhp); 31143ccfa83cSahrens return (rv); 31151d452cf5Sahrens } 31161d452cf5Sahrens 31171d452cf5Sahrens /* 31181d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants. 31191d452cf5Sahrens */ 31201d452cf5Sahrens int 3121842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 31221d452cf5Sahrens { 31231d452cf5Sahrens int ret; 31241d452cf5Sahrens struct destroydata dd = { 0 }; 31251d452cf5Sahrens 31261d452cf5Sahrens dd.snapname = snapname; 312719b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); 312819b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 31291d452cf5Sahrens 313019b94df9SMatthew Ahrens if (nvlist_next_nvpair(dd.nvl, NULL) == NULL) { 313119b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 31321d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 313319b94df9SMatthew Ahrens zhp->zfs_name, snapname); 313419b94df9SMatthew Ahrens } else { 313519b94df9SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp, dd.nvl, defer); 313619b94df9SMatthew Ahrens } 313719b94df9SMatthew Ahrens nvlist_free(dd.nvl); 313819b94df9SMatthew Ahrens return (ret); 3139e5351341SMatthew Ahrens } 3140e5351341SMatthew Ahrens 314119b94df9SMatthew Ahrens /* 314219b94df9SMatthew Ahrens * Destroys all the snapshots named in the nvlist. They must be underneath 314319b94df9SMatthew Ahrens * the zhp (either snapshots of it, or snapshots of its descendants). 314419b94df9SMatthew Ahrens */ 314519b94df9SMatthew Ahrens int 314619b94df9SMatthew Ahrens zfs_destroy_snaps_nvl(zfs_handle_t *zhp, nvlist_t *snaps, boolean_t defer) 314719b94df9SMatthew Ahrens { 314819b94df9SMatthew Ahrens int ret; 31494445fffbSMatthew Ahrens nvlist_t *errlist; 315019b94df9SMatthew Ahrens 31514445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist); 31521d452cf5Sahrens 31531d452cf5Sahrens if (ret != 0) { 31544445fffbSMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL); 31554445fffbSMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 31561d452cf5Sahrens char errbuf[1024]; 31574445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 31584445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 31594445fffbSMatthew Ahrens nvpair_name(pair)); 31601d452cf5Sahrens 31614445fffbSMatthew Ahrens switch (fnvpair_value_int32(pair)) { 31621d452cf5Sahrens case EEXIST: 31634445fffbSMatthew Ahrens zfs_error_aux(zhp->zfs_hdl, 31644445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 31651d452cf5Sahrens "snapshot is cloned")); 31664445fffbSMatthew Ahrens ret = zfs_error(zhp->zfs_hdl, EZFS_EXISTS, 31674445fffbSMatthew Ahrens errbuf); 31684445fffbSMatthew Ahrens break; 31691d452cf5Sahrens default: 31704445fffbSMatthew Ahrens ret = zfs_standard_error(zhp->zfs_hdl, errno, 31714445fffbSMatthew Ahrens errbuf); 31724445fffbSMatthew Ahrens break; 31734445fffbSMatthew Ahrens } 31741d452cf5Sahrens } 31751d452cf5Sahrens } 31761d452cf5Sahrens 31774445fffbSMatthew Ahrens return (ret); 31781d452cf5Sahrens } 31791d452cf5Sahrens 3180fa9e4066Sahrens /* 3181fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source. 3182fa9e4066Sahrens */ 3183fa9e4066Sahrens int 3184e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3185fa9e4066Sahrens { 3186fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 3187fa9e4066Sahrens int ret; 318899653d4eSeschrock char errbuf[1024]; 318999653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3190e9dbad6fSeschrock uint64_t zoned; 3191fa9e4066Sahrens 3192fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3193fa9e4066Sahrens 319499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 319599653d4eSeschrock "cannot create '%s'"), target); 319699653d4eSeschrock 319719b94df9SMatthew Ahrens /* validate the target/clone name */ 3198f18faf3fSek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 319999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3200fa9e4066Sahrens 3201fa9e4066Sahrens /* validate parents exist */ 32027f1f55eaSvb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3203fa9e4066Sahrens return (-1); 3204fa9e4066Sahrens 3205fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent)); 3206fa9e4066Sahrens 3207fa9e4066Sahrens /* do the clone */ 3208e9dbad6fSeschrock 3209e9dbad6fSeschrock if (props) { 32104445fffbSMatthew Ahrens zfs_type_t type; 32114445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) { 32124445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME; 32134445fffbSMatthew Ahrens } else { 32144445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM; 32154445fffbSMatthew Ahrens } 32160a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 32170a48a24eStimh zhp, errbuf)) == NULL) 3218e9dbad6fSeschrock return (-1); 3219e9dbad6fSeschrock } 3220e9dbad6fSeschrock 32214445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props); 3222e9dbad6fSeschrock nvlist_free(props); 3223e9dbad6fSeschrock 3224fa9e4066Sahrens if (ret != 0) { 3225fa9e4066Sahrens switch (errno) { 3226fa9e4066Sahrens 3227fa9e4066Sahrens case ENOENT: 3228fa9e4066Sahrens /* 3229fa9e4066Sahrens * The parent doesn't exist. We should have caught this 3230fa9e4066Sahrens * above, but there may a race condition that has since 3231fa9e4066Sahrens * destroyed the parent. 3232fa9e4066Sahrens * 3233fa9e4066Sahrens * At this point, we don't know whether it's the source 3234fa9e4066Sahrens * that doesn't exist anymore, or whether the target 3235fa9e4066Sahrens * dataset doesn't exist. 3236fa9e4066Sahrens */ 323799653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 323899653d4eSeschrock "no such parent '%s'"), parent); 323999653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3240fa9e4066Sahrens 324199653d4eSeschrock case EXDEV: 324299653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 324399653d4eSeschrock "source and target pools differ")); 324499653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 324599653d4eSeschrock errbuf)); 324699653d4eSeschrock 324799653d4eSeschrock default: 324899653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 324999653d4eSeschrock errbuf)); 325099653d4eSeschrock } 325199653d4eSeschrock } 325299653d4eSeschrock 325399653d4eSeschrock return (ret); 325499653d4eSeschrock } 325599653d4eSeschrock 325699653d4eSeschrock /* 325799653d4eSeschrock * Promotes the given clone fs to be the clone parent. 325899653d4eSeschrock */ 325999653d4eSeschrock int 326099653d4eSeschrock zfs_promote(zfs_handle_t *zhp) 326199653d4eSeschrock { 326299653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 326399653d4eSeschrock zfs_cmd_t zc = { 0 }; 326499653d4eSeschrock char parent[MAXPATHLEN]; 326599653d4eSeschrock int ret; 326699653d4eSeschrock char errbuf[1024]; 326799653d4eSeschrock 326899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 326999653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name); 327099653d4eSeschrock 327199653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 327299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 327399653d4eSeschrock "snapshots can not be promoted")); 327499653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 327599653d4eSeschrock } 327699653d4eSeschrock 32773cb34c60Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 327899653d4eSeschrock if (parent[0] == '\0') { 327999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 328099653d4eSeschrock "not a cloned filesystem")); 328199653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 328299653d4eSeschrock } 328399653d4eSeschrock 32843cb34c60Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 3285e9dbad6fSeschrock sizeof (zc.zc_value)); 328699653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3287ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 328899653d4eSeschrock 328999653d4eSeschrock if (ret != 0) { 32900b69c2f0Sahrens int save_errno = errno; 3291fa9e4066Sahrens 32920b69c2f0Sahrens switch (save_errno) { 3293fa9e4066Sahrens case EEXIST: 3294681d9761SEric Taylor /* There is a conflicting snapshot name. */ 329599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3296681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"), 3297681d9761SEric Taylor zc.zc_string, parent); 329899653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3299fa9e4066Sahrens 3300fa9e4066Sahrens default: 33010b69c2f0Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3302fa9e4066Sahrens } 3303fa9e4066Sahrens } 3304e9dbad6fSeschrock return (ret); 33051d452cf5Sahrens } 33061d452cf5Sahrens 33074445fffbSMatthew Ahrens typedef struct snapdata { 33084445fffbSMatthew Ahrens nvlist_t *sd_nvl; 33094445fffbSMatthew Ahrens const char *sd_snapname; 33104445fffbSMatthew Ahrens } snapdata_t; 33114445fffbSMatthew Ahrens 33124445fffbSMatthew Ahrens static int 33134445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 33144445fffbSMatthew Ahrens { 33154445fffbSMatthew Ahrens snapdata_t *sd = arg; 33164445fffbSMatthew Ahrens char name[ZFS_MAXNAMELEN]; 33174445fffbSMatthew Ahrens int rv = 0; 33184445fffbSMatthew Ahrens 33194445fffbSMatthew Ahrens (void) snprintf(name, sizeof (name), 33204445fffbSMatthew Ahrens "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 33214445fffbSMatthew Ahrens 33224445fffbSMatthew Ahrens fnvlist_add_boolean(sd->sd_nvl, name); 33234445fffbSMatthew Ahrens 33244445fffbSMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 33254445fffbSMatthew Ahrens zfs_close(zhp); 33264445fffbSMatthew Ahrens return (rv); 33274445fffbSMatthew Ahrens } 33284445fffbSMatthew Ahrens 3329fa9e4066Sahrens /* 33304445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 33314445fffbSMatthew Ahrens * created. 3332fa9e4066Sahrens */ 3333fa9e4066Sahrens int 33344445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 33354445fffbSMatthew Ahrens { 33364445fffbSMatthew Ahrens int ret; 33374445fffbSMatthew Ahrens char errbuf[1024]; 33384445fffbSMatthew Ahrens nvpair_t *elem; 33394445fffbSMatthew Ahrens nvlist_t *errors; 33404445fffbSMatthew Ahrens 33414445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 33424445fffbSMatthew Ahrens "cannot create snapshots ")); 33434445fffbSMatthew Ahrens 33444445fffbSMatthew Ahrens elem = NULL; 33454445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 33464445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem); 33474445fffbSMatthew Ahrens 33484445fffbSMatthew Ahrens /* validate the target name */ 33494445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 33504445fffbSMatthew Ahrens B_TRUE)) { 33514445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 33524445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 33534445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname); 33544445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 33554445fffbSMatthew Ahrens } 33564445fffbSMatthew Ahrens } 33574445fffbSMatthew Ahrens 33584445fffbSMatthew Ahrens if (props != NULL && 33594445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 33604445fffbSMatthew Ahrens props, B_FALSE, NULL, errbuf)) == NULL) { 33614445fffbSMatthew Ahrens return (-1); 33624445fffbSMatthew Ahrens } 33634445fffbSMatthew Ahrens 33644445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors); 33654445fffbSMatthew Ahrens 33664445fffbSMatthew Ahrens if (ret != 0) { 33674445fffbSMatthew Ahrens boolean_t printed = B_FALSE; 33684445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 33694445fffbSMatthew Ahrens elem != NULL; 33704445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 33714445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 33724445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 33734445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem)); 33744445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, 33754445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf); 33764445fffbSMatthew Ahrens printed = B_TRUE; 33774445fffbSMatthew Ahrens } 33784445fffbSMatthew Ahrens if (!printed) { 33794445fffbSMatthew Ahrens switch (ret) { 33804445fffbSMatthew Ahrens case EXDEV: 33814445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 33824445fffbSMatthew Ahrens "multiple snapshots of same " 33834445fffbSMatthew Ahrens "fs not allowed")); 33844445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 33854445fffbSMatthew Ahrens 33864445fffbSMatthew Ahrens break; 33874445fffbSMatthew Ahrens default: 33884445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 33894445fffbSMatthew Ahrens } 33904445fffbSMatthew Ahrens } 33914445fffbSMatthew Ahrens } 33924445fffbSMatthew Ahrens 33934445fffbSMatthew Ahrens nvlist_free(props); 33944445fffbSMatthew Ahrens nvlist_free(errors); 33954445fffbSMatthew Ahrens return (ret); 33964445fffbSMatthew Ahrens } 33974445fffbSMatthew Ahrens 33984445fffbSMatthew Ahrens int 3399bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 3400bb0ade09Sahrens nvlist_t *props) 3401fa9e4066Sahrens { 3402fa9e4066Sahrens int ret; 34034445fffbSMatthew Ahrens snapdata_t sd = { 0 }; 34044445fffbSMatthew Ahrens char fsname[ZFS_MAXNAMELEN]; 34054445fffbSMatthew Ahrens char *cp; 34064445fffbSMatthew Ahrens zfs_handle_t *zhp; 340799653d4eSeschrock char errbuf[1024]; 3408fa9e4066Sahrens 340999653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 34104445fffbSMatthew Ahrens "cannot snapshot %s"), path); 341199653d4eSeschrock 3412f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 341399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3414fa9e4066Sahrens 34154445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 34164445fffbSMatthew Ahrens cp = strchr(fsname, '@'); 34174445fffbSMatthew Ahrens *cp = '\0'; 34184445fffbSMatthew Ahrens sd.sd_snapname = cp + 1; 3419bb0ade09Sahrens 34204445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 3421fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3422fa9e4066Sahrens return (-1); 3423fa9e4066Sahrens } 3424fa9e4066Sahrens 34254445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); 34264445fffbSMatthew Ahrens if (recursive) { 34274445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 34284445fffbSMatthew Ahrens } else { 34294445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path); 3430681d9761SEric Taylor } 3431fa9e4066Sahrens 34324445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 34334445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl); 3434fa9e4066Sahrens zfs_close(zhp); 3435fa9e4066Sahrens return (ret); 3436fa9e4066Sahrens } 3437fa9e4066Sahrens 3438fa9e4066Sahrens /* 3439b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 3440b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3441b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction 3442b12a1c38Slling * group. 3443fa9e4066Sahrens */ 3444b12a1c38Slling typedef struct rollback_data { 3445b12a1c38Slling const char *cb_target; /* the snapshot */ 3446b12a1c38Slling uint64_t cb_create; /* creation time reference */ 3447c391e322Sahrens boolean_t cb_error; 344899653d4eSeschrock boolean_t cb_dependent; 3449c391e322Sahrens boolean_t cb_force; 3450b12a1c38Slling } rollback_data_t; 3451b12a1c38Slling 3452b12a1c38Slling static int 3453b12a1c38Slling rollback_destroy(zfs_handle_t *zhp, void *data) 3454b12a1c38Slling { 3455b12a1c38Slling rollback_data_t *cbp = data; 3456b12a1c38Slling 3457b12a1c38Slling if (!cbp->cb_dependent) { 3458b12a1c38Slling if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && 3459b12a1c38Slling zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && 3460b12a1c38Slling zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > 3461b12a1c38Slling cbp->cb_create) { 3462b12a1c38Slling 346399653d4eSeschrock cbp->cb_dependent = B_TRUE; 34644ccbb6e7Sahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 34654ccbb6e7Sahrens rollback_destroy, cbp); 346699653d4eSeschrock cbp->cb_dependent = B_FALSE; 3467b12a1c38Slling 3468842727c2SChris Kirby cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 3469b12a1c38Slling } 3470b12a1c38Slling } else { 3471c391e322Sahrens /* We must destroy this clone; first unmount it */ 3472c391e322Sahrens prop_changelist_t *clp; 3473c391e322Sahrens 34740069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 3475c391e322Sahrens cbp->cb_force ? MS_FORCE: 0); 3476c391e322Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 3477c391e322Sahrens cbp->cb_error = B_TRUE; 3478c391e322Sahrens zfs_close(zhp); 3479c391e322Sahrens return (0); 3480c391e322Sahrens } 3481842727c2SChris Kirby if (zfs_destroy(zhp, B_FALSE) != 0) 3482c391e322Sahrens cbp->cb_error = B_TRUE; 3483c391e322Sahrens else 3484c391e322Sahrens changelist_remove(clp, zhp->zfs_name); 3485ba7b046eSahrens (void) changelist_postfix(clp); 3486c391e322Sahrens changelist_free(clp); 3487b12a1c38Slling } 3488b12a1c38Slling 3489b12a1c38Slling zfs_close(zhp); 3490b12a1c38Slling return (0); 3491b12a1c38Slling } 3492b12a1c38Slling 3493b12a1c38Slling /* 34944ccbb6e7Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 34954ccbb6e7Sahrens * data changes since then and making it the active dataset. 34964ccbb6e7Sahrens * 34974ccbb6e7Sahrens * Any snapshots more recent than the target are destroyed, along with 34984ccbb6e7Sahrens * their dependents. 3499b12a1c38Slling */ 35004ccbb6e7Sahrens int 3501c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3502fa9e4066Sahrens { 35034ccbb6e7Sahrens rollback_data_t cb = { 0 }; 35044ccbb6e7Sahrens int err; 3505fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 35067b97dc1aSrm160521 boolean_t restore_resv = 0; 35077b97dc1aSrm160521 uint64_t old_volsize, new_volsize; 35087b97dc1aSrm160521 zfs_prop_t resv_prop; 3509fa9e4066Sahrens 3510fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3511fa9e4066Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3512fa9e4066Sahrens 35134ccbb6e7Sahrens /* 35142e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents. 35154ccbb6e7Sahrens */ 3516c391e322Sahrens cb.cb_force = force; 35174ccbb6e7Sahrens cb.cb_target = snap->zfs_name; 35184ccbb6e7Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 35194ccbb6e7Sahrens (void) zfs_iter_children(zhp, rollback_destroy, &cb); 35204ccbb6e7Sahrens 3521c391e322Sahrens if (cb.cb_error) 3522c391e322Sahrens return (-1); 35234ccbb6e7Sahrens 35244ccbb6e7Sahrens /* 35254ccbb6e7Sahrens * Now that we have verified that the snapshot is the latest, 35264ccbb6e7Sahrens * rollback to the given snapshot. 35274ccbb6e7Sahrens */ 35284ccbb6e7Sahrens 35297b97dc1aSrm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 35307b97dc1aSrm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 35317b97dc1aSrm160521 return (-1); 35327b97dc1aSrm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 35337b97dc1aSrm160521 restore_resv = 35347b97dc1aSrm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 35357b97dc1aSrm160521 } 3536fa9e4066Sahrens 3537fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3538fa9e4066Sahrens 3539e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 3540fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3541fa9e4066Sahrens else 3542fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3543fa9e4066Sahrens 3544fa9e4066Sahrens /* 35454ccbb6e7Sahrens * We rely on zfs_iter_children() to verify that there are no 35464ccbb6e7Sahrens * newer snapshots for the given dataset. Therefore, we can 35474ccbb6e7Sahrens * simply pass the name on to the ioctl() call. There is still 35484ccbb6e7Sahrens * an unlikely race condition where the user has taken a 35494ccbb6e7Sahrens * snapshot since we verified that this was the most recent. 35507b97dc1aSrm160521 * 3551fa9e4066Sahrens */ 35524ccbb6e7Sahrens if ((err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_ROLLBACK, &zc)) != 0) { 3553ece3d9b3Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 355499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 355599653d4eSeschrock zhp->zfs_name); 3556b9415e83Srm160521 return (err); 3557b9415e83Srm160521 } 3558fa9e4066Sahrens 35597b97dc1aSrm160521 /* 35607b97dc1aSrm160521 * For volumes, if the pre-rollback volsize matched the pre- 35617b97dc1aSrm160521 * rollback reservation and the volsize has changed then set 35627b97dc1aSrm160521 * the reservation property to the post-rollback volsize. 35637b97dc1aSrm160521 * Make a new handle since the rollback closed the dataset. 35647b97dc1aSrm160521 */ 3565b9415e83Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 3566b9415e83Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 35677b97dc1aSrm160521 if (restore_resv) { 35687b97dc1aSrm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 35697b97dc1aSrm160521 if (old_volsize != new_volsize) 3570b9415e83Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 3571b9415e83Srm160521 new_volsize); 35727b97dc1aSrm160521 } 35737b97dc1aSrm160521 zfs_close(zhp); 35747b97dc1aSrm160521 } 35754ccbb6e7Sahrens return (err); 3576b12a1c38Slling } 3577b12a1c38Slling 3578b12a1c38Slling /* 3579fa9e4066Sahrens * Renames the given dataset. 3580fa9e4066Sahrens */ 3581fa9e4066Sahrens int 35826a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, 35836a9cb0eaSEric Schrock boolean_t force_unmount) 3584fa9e4066Sahrens { 3585fa9e4066Sahrens int ret; 3586fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3587fa9e4066Sahrens char *delim; 3588cdf5b4caSmmusante prop_changelist_t *cl = NULL; 3589cdf5b4caSmmusante zfs_handle_t *zhrp = NULL; 3590cdf5b4caSmmusante char *parentname = NULL; 3591fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 359299653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 359399653d4eSeschrock char errbuf[1024]; 3594fa9e4066Sahrens 3595fa9e4066Sahrens /* if we have the same exact name, just return success */ 3596fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3597fa9e4066Sahrens return (0); 3598fa9e4066Sahrens 359999653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 360099653d4eSeschrock "cannot rename to '%s'"), target); 360199653d4eSeschrock 3602fa9e4066Sahrens /* 3603fa9e4066Sahrens * Make sure the target name is valid 3604fa9e4066Sahrens */ 3605fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 360698579b20Snd150628 if ((strchr(target, '@') == NULL) || 360798579b20Snd150628 *target == '@') { 360898579b20Snd150628 /* 360998579b20Snd150628 * Snapshot target name is abbreviated, 361098579b20Snd150628 * reconstruct full dataset name 361198579b20Snd150628 */ 361298579b20Snd150628 (void) strlcpy(parent, zhp->zfs_name, 361398579b20Snd150628 sizeof (parent)); 361498579b20Snd150628 delim = strchr(parent, '@'); 361598579b20Snd150628 if (strchr(target, '@') == NULL) 361698579b20Snd150628 *(++delim) = '\0'; 361798579b20Snd150628 else 361898579b20Snd150628 *delim = '\0'; 361998579b20Snd150628 (void) strlcat(parent, target, sizeof (parent)); 362098579b20Snd150628 target = parent; 362198579b20Snd150628 } else { 3622fa9e4066Sahrens /* 3623fa9e4066Sahrens * Make sure we're renaming within the same dataset. 3624fa9e4066Sahrens */ 362598579b20Snd150628 delim = strchr(target, '@'); 362698579b20Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 362798579b20Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 362899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 362998579b20Snd150628 "snapshots must be part of same " 363098579b20Snd150628 "dataset")); 363198579b20Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 363298579b20Snd150628 errbuf)); 3633fa9e4066Sahrens } 363498579b20Snd150628 } 3635f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 363698579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3637fa9e4066Sahrens } else { 3638cdf5b4caSmmusante if (recursive) { 3639cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3640cdf5b4caSmmusante "recursive rename must be a snapshot")); 3641cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3642cdf5b4caSmmusante } 3643cdf5b4caSmmusante 3644f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 364598579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3646e9dbad6fSeschrock 3647fa9e4066Sahrens /* validate parents */ 3648d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 3649fa9e4066Sahrens return (-1); 3650fa9e4066Sahrens 3651fa9e4066Sahrens /* make sure we're in the same pool */ 3652fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL); 3653fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3654fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') { 365599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 365699653d4eSeschrock "datasets must be within same pool")); 365799653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3658fa9e4066Sahrens } 3659f2fdf992Snd150628 3660f2fdf992Snd150628 /* new name cannot be a child of the current dataset name */ 3661d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) { 3662f2fdf992Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3663d41c4376SMark J Musante "New dataset name cannot be a descendant of " 3664f2fdf992Snd150628 "current dataset name")); 3665f2fdf992Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3666f2fdf992Snd150628 } 3667fa9e4066Sahrens } 3668fa9e4066Sahrens 366999653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 367099653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 367199653d4eSeschrock 3672fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID && 3673fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 367499653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 367599653d4eSeschrock "dataset is used in a non-global zone")); 367699653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3677fa9e4066Sahrens } 3678fa9e4066Sahrens 3679cdf5b4caSmmusante if (recursive) { 3680cdf5b4caSmmusante 3681f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 3682f0c5ee21Smmusante if (parentname == NULL) { 3683f0c5ee21Smmusante ret = -1; 3684f0c5ee21Smmusante goto error; 3685f0c5ee21Smmusante } 3686cdf5b4caSmmusante delim = strchr(parentname, '@'); 3687cdf5b4caSmmusante *delim = '\0'; 3688990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 3689cdf5b4caSmmusante if (zhrp == NULL) { 3690f0c5ee21Smmusante ret = -1; 3691f0c5ee21Smmusante goto error; 3692cdf5b4caSmmusante } 3693cdf5b4caSmmusante 3694cdf5b4caSmmusante } else { 36956a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 36966a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL) 369799653d4eSeschrock return (-1); 3698fa9e4066Sahrens 3699fa9e4066Sahrens if (changelist_haszonedchild(cl)) { 370099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 370199653d4eSeschrock "child dataset with inherited mountpoint is used " 370299653d4eSeschrock "in a non-global zone")); 3703e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 3704fa9e4066Sahrens goto error; 3705fa9e4066Sahrens } 3706fa9e4066Sahrens 3707fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 3708fa9e4066Sahrens goto error; 3709cdf5b4caSmmusante } 3710fa9e4066Sahrens 3711e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 3712fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3713fa9e4066Sahrens else 3714fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3715fa9e4066Sahrens 371698579b20Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3717e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 371898579b20Snd150628 3719cdf5b4caSmmusante zc.zc_cookie = recursive; 3720cdf5b4caSmmusante 3721ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 3722cdf5b4caSmmusante /* 3723cdf5b4caSmmusante * if it was recursive, the one that actually failed will 3724cdf5b4caSmmusante * be in zc.zc_name 3725cdf5b4caSmmusante */ 3726cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 37273cb34c60Sahrens "cannot rename '%s'"), zc.zc_name); 3728cdf5b4caSmmusante 3729cdf5b4caSmmusante if (recursive && errno == EEXIST) { 3730cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3731cdf5b4caSmmusante "a child dataset already has a snapshot " 3732cdf5b4caSmmusante "with the new name")); 3733a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 3734cdf5b4caSmmusante } else { 373599653d4eSeschrock (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 3736cdf5b4caSmmusante } 3737fa9e4066Sahrens 3738fa9e4066Sahrens /* 3739fa9e4066Sahrens * On failure, we still want to remount any filesystems that 3740fa9e4066Sahrens * were previously mounted, so we don't alter the system state. 3741fa9e4066Sahrens */ 3742681d9761SEric Taylor if (!recursive) 3743fa9e4066Sahrens (void) changelist_postfix(cl); 3744cdf5b4caSmmusante } else { 3745681d9761SEric Taylor if (!recursive) { 3746fa9e4066Sahrens changelist_rename(cl, zfs_get_name(zhp), target); 3747fa9e4066Sahrens ret = changelist_postfix(cl); 3748fa9e4066Sahrens } 3749cdf5b4caSmmusante } 3750fa9e4066Sahrens 3751fa9e4066Sahrens error: 3752cdf5b4caSmmusante if (parentname) { 3753cdf5b4caSmmusante free(parentname); 3754cdf5b4caSmmusante } 3755cdf5b4caSmmusante if (zhrp) { 3756cdf5b4caSmmusante zfs_close(zhrp); 3757cdf5b4caSmmusante } 3758cdf5b4caSmmusante if (cl) { 3759fa9e4066Sahrens changelist_free(cl); 3760cdf5b4caSmmusante } 3761fa9e4066Sahrens return (ret); 3762fa9e4066Sahrens } 3763fa9e4066Sahrens 3764e9dbad6fSeschrock nvlist_t * 3765e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp) 3766e9dbad6fSeschrock { 3767e9dbad6fSeschrock return (zhp->zfs_user_props); 3768e9dbad6fSeschrock } 3769e9dbad6fSeschrock 377092241e0bSTom Erickson nvlist_t * 377192241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp) 377292241e0bSTom Erickson { 377392241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 377492241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 377592241e0bSTom Erickson return (NULL); 377692241e0bSTom Erickson return (zhp->zfs_recvd_props); 377792241e0bSTom Erickson } 377892241e0bSTom Erickson 3779e9dbad6fSeschrock /* 3780e9dbad6fSeschrock * This function is used by 'zfs list' to determine the exact set of columns to 3781e9dbad6fSeschrock * display, and their maximum widths. This does two main things: 3782e9dbad6fSeschrock * 3783e9dbad6fSeschrock * - If this is a list of all properties, then expand the list to include 3784e9dbad6fSeschrock * all native properties, and set a flag so that for each dataset we look 3785e9dbad6fSeschrock * for new unique user properties and add them to the list. 3786e9dbad6fSeschrock * 3787e9dbad6fSeschrock * - For non fixed-width properties, keep track of the maximum width seen 378892241e0bSTom Erickson * so that we can size the column appropriately. If the user has 378992241e0bSTom Erickson * requested received property values, we also need to compute the width 379092241e0bSTom Erickson * of the RECEIVED column. 3791e9dbad6fSeschrock */ 3792e9dbad6fSeschrock int 379392241e0bSTom Erickson zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received) 3794e9dbad6fSeschrock { 3795e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3796990b4856Slling zprop_list_t *entry; 3797990b4856Slling zprop_list_t **last, **start; 3798e9dbad6fSeschrock nvlist_t *userprops, *propval; 3799e9dbad6fSeschrock nvpair_t *elem; 3800e9dbad6fSeschrock char *strval; 3801e9dbad6fSeschrock char buf[ZFS_MAXPROPLEN]; 3802e9dbad6fSeschrock 3803990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 3804e9dbad6fSeschrock return (-1); 3805e9dbad6fSeschrock 3806e9dbad6fSeschrock userprops = zfs_get_user_props(zhp); 3807e9dbad6fSeschrock 3808e9dbad6fSeschrock entry = *plp; 3809e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 3810e9dbad6fSeschrock /* 3811e9dbad6fSeschrock * Go through and add any user properties as necessary. We 3812e9dbad6fSeschrock * start by incrementing our list pointer to the first 3813e9dbad6fSeschrock * non-native property. 3814e9dbad6fSeschrock */ 3815e9dbad6fSeschrock start = plp; 3816e9dbad6fSeschrock while (*start != NULL) { 3817990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL) 3818e9dbad6fSeschrock break; 3819e9dbad6fSeschrock start = &(*start)->pl_next; 3820e9dbad6fSeschrock } 3821e9dbad6fSeschrock 3822e9dbad6fSeschrock elem = NULL; 3823e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 3824e9dbad6fSeschrock /* 3825e9dbad6fSeschrock * See if we've already found this property in our list. 3826e9dbad6fSeschrock */ 3827e9dbad6fSeschrock for (last = start; *last != NULL; 3828e9dbad6fSeschrock last = &(*last)->pl_next) { 3829e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop, 3830e9dbad6fSeschrock nvpair_name(elem)) == 0) 3831e9dbad6fSeschrock break; 3832e9dbad6fSeschrock } 3833e9dbad6fSeschrock 3834e9dbad6fSeschrock if (*last == NULL) { 3835e9dbad6fSeschrock if ((entry = zfs_alloc(hdl, 3836990b4856Slling sizeof (zprop_list_t))) == NULL || 3837e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl, 3838e9dbad6fSeschrock nvpair_name(elem)))) == NULL) { 3839e9dbad6fSeschrock free(entry); 3840e9dbad6fSeschrock return (-1); 3841e9dbad6fSeschrock } 3842e9dbad6fSeschrock 3843990b4856Slling entry->pl_prop = ZPROP_INVAL; 3844e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem)); 3845e9dbad6fSeschrock entry->pl_all = B_TRUE; 3846e9dbad6fSeschrock *last = entry; 3847e9dbad6fSeschrock } 3848e9dbad6fSeschrock } 3849e9dbad6fSeschrock } 3850e9dbad6fSeschrock 3851e9dbad6fSeschrock /* 3852e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns 3853e9dbad6fSeschrock */ 3854e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 3855e9dbad6fSeschrock if (entry->pl_fixed) 3856e9dbad6fSeschrock continue; 3857e9dbad6fSeschrock 3858990b4856Slling if (entry->pl_prop != ZPROP_INVAL) { 3859e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop, 3860e9dbad6fSeschrock buf, sizeof (buf), NULL, NULL, 0, B_FALSE) == 0) { 3861e9dbad6fSeschrock if (strlen(buf) > entry->pl_width) 3862e9dbad6fSeschrock entry->pl_width = strlen(buf); 3863e9dbad6fSeschrock } 386492241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 386592241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop), 386692241e0bSTom Erickson buf, sizeof (buf), B_FALSE) == 0) 386792241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 386892241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 386992241e0bSTom Erickson } else { 387092241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 387192241e0bSTom Erickson &propval) == 0) { 3872e9dbad6fSeschrock verify(nvlist_lookup_string(propval, 3873990b4856Slling ZPROP_VALUE, &strval) == 0); 3874e9dbad6fSeschrock if (strlen(strval) > entry->pl_width) 3875e9dbad6fSeschrock entry->pl_width = strlen(strval); 3876e9dbad6fSeschrock } 387792241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 387892241e0bSTom Erickson entry->pl_user_prop, 387992241e0bSTom Erickson buf, sizeof (buf), B_FALSE) == 0) 388092241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 388192241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 388292241e0bSTom Erickson } 3883e9dbad6fSeschrock } 3884e9dbad6fSeschrock 3885e9dbad6fSeschrock return (0); 3886e9dbad6fSeschrock } 3887ecd6cf80Smarks 3888ecd6cf80Smarks int 3889ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 3890743a77edSAlan Wright char *resource, void *export, void *sharetab, 3891743a77edSAlan Wright int sharemax, zfs_share_op_t operation) 3892ecd6cf80Smarks { 3893ecd6cf80Smarks zfs_cmd_t zc = { 0 }; 3894ecd6cf80Smarks int error; 3895ecd6cf80Smarks 3896ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3897ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 3898743a77edSAlan Wright if (resource) 3899743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 3900ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 3901ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 3902da6c28aaSamw zc.zc_share.z_sharetype = operation; 3903ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax; 3904ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 3905ecd6cf80Smarks return (error); 3906ecd6cf80Smarks } 39072e5e9e19SSanjeev Bagewadi 39082e5e9e19SSanjeev Bagewadi void 39092e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 39102e5e9e19SSanjeev Bagewadi { 39112e5e9e19SSanjeev Bagewadi nvpair_t *curr; 39122e5e9e19SSanjeev Bagewadi 39132e5e9e19SSanjeev Bagewadi /* 39142e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the 39152e5e9e19SSanjeev Bagewadi * properties. 39162e5e9e19SSanjeev Bagewadi */ 39172e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props; 39182e5e9e19SSanjeev Bagewadi 39192e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 39202e5e9e19SSanjeev Bagewadi 39212e5e9e19SSanjeev Bagewadi while (curr) { 39222e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 39232e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 39242e5e9e19SSanjeev Bagewadi 392514843421SMatthew Ahrens /* 3926faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we 3927faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always 3928faaa6415SEric Schrock * leave these in the list. This can also happen if we 3929faaa6415SEric Schrock * encounter an unknown DSL property (when running older 3930faaa6415SEric Schrock * software, for example). 393114843421SMatthew Ahrens */ 393214843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 39332e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props, 39342e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr)); 39352e5e9e19SSanjeev Bagewadi curr = next; 39362e5e9e19SSanjeev Bagewadi } 39372e5e9e19SSanjeev Bagewadi } 3938743a77edSAlan Wright 3939743a77edSAlan Wright static int 3940743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 3941743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 3942743a77edSAlan Wright { 3943743a77edSAlan Wright zfs_cmd_t zc = { 0 }; 3944743a77edSAlan Wright nvlist_t *nvlist = NULL; 3945743a77edSAlan Wright int error; 3946743a77edSAlan Wright 3947743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 3948743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 3949743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd; 3950743a77edSAlan Wright 3951743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) { 3952743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 3953743a77edSAlan Wright (void) no_memory(hdl); 3954743a77edSAlan Wright return (NULL); 3955743a77edSAlan Wright } 3956743a77edSAlan Wright } 3957743a77edSAlan Wright 3958743a77edSAlan Wright switch (cmd) { 3959743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 3960743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 3961743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 3962743a77edSAlan Wright break; 3963743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 3964743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 3965743a77edSAlan Wright resource1) != 0) { 3966743a77edSAlan Wright (void) no_memory(hdl); 3967743a77edSAlan Wright return (-1); 3968743a77edSAlan Wright } 3969743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 3970743a77edSAlan Wright resource2) != 0) { 3971743a77edSAlan Wright (void) no_memory(hdl); 3972743a77edSAlan Wright return (-1); 3973743a77edSAlan Wright } 3974743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 3975743a77edSAlan Wright nvlist_free(nvlist); 3976743a77edSAlan Wright return (-1); 3977743a77edSAlan Wright } 3978743a77edSAlan Wright break; 3979743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 3980743a77edSAlan Wright break; 3981743a77edSAlan Wright default: 3982743a77edSAlan Wright return (-1); 3983743a77edSAlan Wright } 3984743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 3985743a77edSAlan Wright if (nvlist) 3986743a77edSAlan Wright nvlist_free(nvlist); 3987743a77edSAlan Wright return (error); 3988743a77edSAlan Wright } 3989743a77edSAlan Wright 3990743a77edSAlan Wright int 3991743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 3992743a77edSAlan Wright char *path, char *resource) 3993743a77edSAlan Wright { 3994743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 3995743a77edSAlan Wright resource, NULL)); 3996743a77edSAlan Wright } 3997743a77edSAlan Wright 3998743a77edSAlan Wright int 3999743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4000743a77edSAlan Wright char *path, char *resource) 4001743a77edSAlan Wright { 4002743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4003743a77edSAlan Wright resource, NULL)); 4004743a77edSAlan Wright } 4005743a77edSAlan Wright 4006743a77edSAlan Wright int 4007743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4008743a77edSAlan Wright { 4009743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4010743a77edSAlan Wright NULL, NULL)); 4011743a77edSAlan Wright } 4012743a77edSAlan Wright 4013743a77edSAlan Wright int 4014743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4015743a77edSAlan Wright char *oldname, char *newname) 4016743a77edSAlan Wright { 4017743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4018743a77edSAlan Wright oldname, newname)); 4019743a77edSAlan Wright } 402014843421SMatthew Ahrens 402114843421SMatthew Ahrens int 402214843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 402314843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg) 402414843421SMatthew Ahrens { 402514843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 402614843421SMatthew Ahrens zfs_useracct_t buf[100]; 4027*70f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl; 4028*70f56fa6SYuri Pankov int ret; 402914843421SMatthew Ahrens 403019b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 403114843421SMatthew Ahrens 403214843421SMatthew Ahrens zc.zc_objset_type = type; 403314843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf; 403414843421SMatthew Ahrens 4035*70f56fa6SYuri Pankov for (;;) { 403614843421SMatthew Ahrens zfs_useracct_t *zua = buf; 403714843421SMatthew Ahrens 403814843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf); 4039*70f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 4040*70f56fa6SYuri Pankov char errbuf[ZFS_MAXNAMELEN + 32]; 4041*70f56fa6SYuri Pankov 4042*70f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf), 4043*70f56fa6SYuri Pankov dgettext(TEXT_DOMAIN, 4044*70f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name); 4045*70f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf)); 4046*70f56fa6SYuri Pankov } 4047*70f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0) 404814843421SMatthew Ahrens break; 404914843421SMatthew Ahrens 405014843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) { 4051*70f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 4052*70f56fa6SYuri Pankov zua->zu_space)) != 0) 4053*70f56fa6SYuri Pankov return (ret); 405414843421SMatthew Ahrens zua++; 405514843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 405614843421SMatthew Ahrens } 405714843421SMatthew Ahrens } 405814843421SMatthew Ahrens 4059*70f56fa6SYuri Pankov return (0); 406014843421SMatthew Ahrens } 4061842727c2SChris Kirby 4062842727c2SChris Kirby int 4063842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4064c99e4bdcSChris Kirby boolean_t recursive, boolean_t temphold, boolean_t enoent_ok, 4065a7f53a56SChris Kirby int cleanup_fd, uint64_t dsobj, uint64_t createtxg) 4066842727c2SChris Kirby { 4067842727c2SChris Kirby zfs_cmd_t zc = { 0 }; 4068842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4069842727c2SChris Kirby 4070a7f53a56SChris Kirby ASSERT(!recursive || dsobj == 0); 4071a7f53a56SChris Kirby 4072842727c2SChris Kirby (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4073842727c2SChris Kirby (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 4074ca45db41SChris Kirby if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string)) 4075ca45db41SChris Kirby >= sizeof (zc.zc_string)) 4076ca45db41SChris Kirby return (zfs_error(hdl, EZFS_TAGTOOLONG, tag)); 4077842727c2SChris Kirby zc.zc_cookie = recursive; 4078ca45db41SChris Kirby zc.zc_temphold = temphold; 4079c99e4bdcSChris Kirby zc.zc_cleanup_fd = cleanup_fd; 4080a7f53a56SChris Kirby zc.zc_sendobj = dsobj; 4081a7f53a56SChris Kirby zc.zc_createtxg = createtxg; 4082842727c2SChris Kirby 4083842727c2SChris Kirby if (zfs_ioctl(hdl, ZFS_IOC_HOLD, &zc) != 0) { 4084842727c2SChris Kirby char errbuf[ZFS_MAXNAMELEN+32]; 4085842727c2SChris Kirby 4086842727c2SChris Kirby /* 4087842727c2SChris Kirby * if it was recursive, the one that actually failed will be in 4088842727c2SChris Kirby * zc.zc_name. 4089842727c2SChris Kirby */ 4090842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4091842727c2SChris Kirby "cannot hold '%s@%s'"), zc.zc_name, snapname); 4092842727c2SChris Kirby switch (errno) { 409315508ac0SChris Kirby case E2BIG: 409415508ac0SChris Kirby /* 409515508ac0SChris Kirby * Temporary tags wind up having the ds object id 409615508ac0SChris Kirby * prepended. So even if we passed the length check 409715508ac0SChris Kirby * above, it's still possible for the tag to wind 409815508ac0SChris Kirby * up being slightly too long. 409915508ac0SChris Kirby */ 410015508ac0SChris Kirby return (zfs_error(hdl, EZFS_TAGTOOLONG, errbuf)); 4101842727c2SChris Kirby case ENOTSUP: 4102842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4103842727c2SChris Kirby "pool must be upgraded")); 4104842727c2SChris Kirby return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 4105842727c2SChris Kirby case EINVAL: 4106842727c2SChris Kirby return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4107842727c2SChris Kirby case EEXIST: 4108842727c2SChris Kirby return (zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf)); 4109818119b8SChris Kirby case ENOENT: 4110818119b8SChris Kirby if (enoent_ok) 4111a7f53a56SChris Kirby return (ENOENT); 4112818119b8SChris Kirby /* FALLTHROUGH */ 4113842727c2SChris Kirby default: 4114842727c2SChris Kirby return (zfs_standard_error_fmt(hdl, errno, errbuf)); 4115842727c2SChris Kirby } 4116842727c2SChris Kirby } 4117842727c2SChris Kirby 4118842727c2SChris Kirby return (0); 4119842727c2SChris Kirby } 4120842727c2SChris Kirby 4121842727c2SChris Kirby int 4122842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 4123842727c2SChris Kirby boolean_t recursive) 4124842727c2SChris Kirby { 4125842727c2SChris Kirby zfs_cmd_t zc = { 0 }; 4126842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4127842727c2SChris Kirby 4128842727c2SChris Kirby (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 4129842727c2SChris Kirby (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 4130ca45db41SChris Kirby if (strlcpy(zc.zc_string, tag, sizeof (zc.zc_string)) 4131ca45db41SChris Kirby >= sizeof (zc.zc_string)) 4132ca45db41SChris Kirby return (zfs_error(hdl, EZFS_TAGTOOLONG, tag)); 4133842727c2SChris Kirby zc.zc_cookie = recursive; 4134842727c2SChris Kirby 4135842727c2SChris Kirby if (zfs_ioctl(hdl, ZFS_IOC_RELEASE, &zc) != 0) { 4136842727c2SChris Kirby char errbuf[ZFS_MAXNAMELEN+32]; 4137842727c2SChris Kirby 4138842727c2SChris Kirby /* 4139842727c2SChris Kirby * if it was recursive, the one that actually failed will be in 4140842727c2SChris Kirby * zc.zc_name. 4141842727c2SChris Kirby */ 4142842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 4143620252bcSChris Kirby "cannot release '%s' from '%s@%s'"), tag, zc.zc_name, 4144620252bcSChris Kirby snapname); 4145842727c2SChris Kirby switch (errno) { 4146842727c2SChris Kirby case ESRCH: 4147842727c2SChris Kirby return (zfs_error(hdl, EZFS_REFTAG_RELE, errbuf)); 4148842727c2SChris Kirby case ENOTSUP: 4149842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4150842727c2SChris Kirby "pool must be upgraded")); 4151842727c2SChris Kirby return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 4152842727c2SChris Kirby case EINVAL: 4153842727c2SChris Kirby return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 4154842727c2SChris Kirby default: 4155842727c2SChris Kirby return (zfs_standard_error_fmt(hdl, errno, errbuf)); 4156842727c2SChris Kirby } 4157842727c2SChris Kirby } 4158842727c2SChris Kirby 4159842727c2SChris Kirby return (0); 4160842727c2SChris Kirby } 4161ca45db41SChris Kirby 41621af68beaSAlexander Stetsenko int 41631af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 41641af68beaSAlexander Stetsenko { 41651af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 41661af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 41671af68beaSAlexander Stetsenko int nvsz = 2048; 41681af68beaSAlexander Stetsenko void *nvbuf; 41691af68beaSAlexander Stetsenko int err = 0; 41701af68beaSAlexander Stetsenko char errbuf[ZFS_MAXNAMELEN+32]; 41711af68beaSAlexander Stetsenko 41721af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 41731af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 41741af68beaSAlexander Stetsenko 41751af68beaSAlexander Stetsenko tryagain: 41761af68beaSAlexander Stetsenko 41771af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 41781af68beaSAlexander Stetsenko if (nvbuf == NULL) { 41791af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 41801af68beaSAlexander Stetsenko goto out; 41811af68beaSAlexander Stetsenko } 41821af68beaSAlexander Stetsenko 41831af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 41841af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 41851af68beaSAlexander Stetsenko 41861af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN); 41871af68beaSAlexander Stetsenko 4188d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 41891af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 41901af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 41911af68beaSAlexander Stetsenko zc.zc_name); 41921af68beaSAlexander Stetsenko switch (errno) { 41931af68beaSAlexander Stetsenko case ENOMEM: 41941af68beaSAlexander Stetsenko free(nvbuf); 41951af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 41961af68beaSAlexander Stetsenko goto tryagain; 41971af68beaSAlexander Stetsenko 41981af68beaSAlexander Stetsenko case ENOTSUP: 41991af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42001af68beaSAlexander Stetsenko "pool must be upgraded")); 42011af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 42021af68beaSAlexander Stetsenko break; 42031af68beaSAlexander Stetsenko case EINVAL: 42041af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 42051af68beaSAlexander Stetsenko break; 42061af68beaSAlexander Stetsenko case ENOENT: 42071af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 42081af68beaSAlexander Stetsenko break; 42091af68beaSAlexander Stetsenko default: 42101af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 42111af68beaSAlexander Stetsenko break; 42121af68beaSAlexander Stetsenko } 42131af68beaSAlexander Stetsenko } else { 42141af68beaSAlexander Stetsenko /* success */ 42151af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 42161af68beaSAlexander Stetsenko if (rc) { 42171af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext( 42181af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"), 42191af68beaSAlexander Stetsenko zc.zc_name); 42201af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 42211af68beaSAlexander Stetsenko } 42221af68beaSAlexander Stetsenko } 42231af68beaSAlexander Stetsenko 42241af68beaSAlexander Stetsenko free(nvbuf); 42251af68beaSAlexander Stetsenko out: 42261af68beaSAlexander Stetsenko return (err); 42271af68beaSAlexander Stetsenko } 42281af68beaSAlexander Stetsenko 42291af68beaSAlexander Stetsenko int 42301af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 42311af68beaSAlexander Stetsenko { 42321af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 42331af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 42341af68beaSAlexander Stetsenko char *nvbuf; 42351af68beaSAlexander Stetsenko char errbuf[ZFS_MAXNAMELEN+32]; 42361af68beaSAlexander Stetsenko size_t nvsz; 42371af68beaSAlexander Stetsenko int err; 42381af68beaSAlexander Stetsenko 42391af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 42401af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 42411af68beaSAlexander Stetsenko 42421af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 42431af68beaSAlexander Stetsenko assert(err == 0); 42441af68beaSAlexander Stetsenko 42451af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 42461af68beaSAlexander Stetsenko 42471af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 42481af68beaSAlexander Stetsenko assert(err == 0); 42491af68beaSAlexander Stetsenko 42501af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz; 42511af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf; 42521af68beaSAlexander Stetsenko zc.zc_perm_action = un; 42531af68beaSAlexander Stetsenko 42541af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 42551af68beaSAlexander Stetsenko 42561af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 42571af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 42581af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 42591af68beaSAlexander Stetsenko zc.zc_name); 42601af68beaSAlexander Stetsenko switch (errno) { 42611af68beaSAlexander Stetsenko case ENOTSUP: 42621af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 42631af68beaSAlexander Stetsenko "pool must be upgraded")); 42641af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 42651af68beaSAlexander Stetsenko break; 42661af68beaSAlexander Stetsenko case EINVAL: 42671af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 42681af68beaSAlexander Stetsenko break; 42691af68beaSAlexander Stetsenko case ENOENT: 42701af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 42711af68beaSAlexander Stetsenko break; 42721af68beaSAlexander Stetsenko default: 42731af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 42741af68beaSAlexander Stetsenko break; 42751af68beaSAlexander Stetsenko } 42761af68beaSAlexander Stetsenko } 42771af68beaSAlexander Stetsenko 42781af68beaSAlexander Stetsenko free(nvbuf); 42791af68beaSAlexander Stetsenko 42801af68beaSAlexander Stetsenko return (err); 42811af68beaSAlexander Stetsenko } 42821af68beaSAlexander Stetsenko 42831af68beaSAlexander Stetsenko int 42841af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 42851af68beaSAlexander Stetsenko { 42861af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 42871af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 42881af68beaSAlexander Stetsenko int nvsz = 2048; 42891af68beaSAlexander Stetsenko void *nvbuf; 42901af68beaSAlexander Stetsenko int err = 0; 42911af68beaSAlexander Stetsenko char errbuf[ZFS_MAXNAMELEN+32]; 42921af68beaSAlexander Stetsenko 42931af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 42941af68beaSAlexander Stetsenko 42951af68beaSAlexander Stetsenko tryagain: 42961af68beaSAlexander Stetsenko 42971af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 42981af68beaSAlexander Stetsenko if (nvbuf == NULL) { 42991af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 43001af68beaSAlexander Stetsenko goto out; 43011af68beaSAlexander Stetsenko } 43021af68beaSAlexander Stetsenko 43031af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 43041af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 43051af68beaSAlexander Stetsenko 43061af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN); 43071af68beaSAlexander Stetsenko 43081af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_GET_HOLDS, &zc) != 0) { 43091af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 43101af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 43111af68beaSAlexander Stetsenko zc.zc_name); 43121af68beaSAlexander Stetsenko switch (errno) { 43131af68beaSAlexander Stetsenko case ENOMEM: 43141af68beaSAlexander Stetsenko free(nvbuf); 43151af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 43161af68beaSAlexander Stetsenko goto tryagain; 43171af68beaSAlexander Stetsenko 43181af68beaSAlexander Stetsenko case ENOTSUP: 43191af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 43201af68beaSAlexander Stetsenko "pool must be upgraded")); 43211af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 43221af68beaSAlexander Stetsenko break; 43231af68beaSAlexander Stetsenko case EINVAL: 43241af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 43251af68beaSAlexander Stetsenko break; 43261af68beaSAlexander Stetsenko case ENOENT: 43271af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 43281af68beaSAlexander Stetsenko break; 43291af68beaSAlexander Stetsenko default: 43301af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 43311af68beaSAlexander Stetsenko break; 43321af68beaSAlexander Stetsenko } 43331af68beaSAlexander Stetsenko } else { 43341af68beaSAlexander Stetsenko /* success */ 43351af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 43361af68beaSAlexander Stetsenko if (rc) { 43371af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 43381af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 43391af68beaSAlexander Stetsenko zc.zc_name); 43401af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 43411af68beaSAlexander Stetsenko } 43421af68beaSAlexander Stetsenko } 43431af68beaSAlexander Stetsenko 43441af68beaSAlexander Stetsenko free(nvbuf); 43451af68beaSAlexander Stetsenko out: 43461af68beaSAlexander Stetsenko return (err); 43471af68beaSAlexander Stetsenko } 43481af68beaSAlexander Stetsenko 4349c1449561SEric Taylor uint64_t 4350c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props) 4351c1449561SEric Taylor { 4352c1449561SEric Taylor uint64_t numdb; 4353c1449561SEric Taylor uint64_t nblocks, volblocksize; 4354c1449561SEric Taylor int ncopies; 4355c1449561SEric Taylor char *strval; 4356c1449561SEric Taylor 4357c1449561SEric Taylor if (nvlist_lookup_string(props, 4358c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 4359c1449561SEric Taylor ncopies = atoi(strval); 4360c1449561SEric Taylor else 4361c1449561SEric Taylor ncopies = 1; 4362c1449561SEric Taylor if (nvlist_lookup_uint64(props, 4363c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 4364c1449561SEric Taylor &volblocksize) != 0) 4365c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 4366c1449561SEric Taylor nblocks = volsize/volblocksize; 4367c1449561SEric Taylor /* start with metadnode L0-L6 */ 4368c1449561SEric Taylor numdb = 7; 4369c1449561SEric Taylor /* calculate number of indirects */ 4370c1449561SEric Taylor while (nblocks > 1) { 4371c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1; 4372c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL; 4373c1449561SEric Taylor numdb += nblocks; 4374c1449561SEric Taylor } 4375c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 4376c1449561SEric Taylor volsize *= ncopies; 4377c1449561SEric Taylor /* 4378c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 4379c1449561SEric Taylor * compressed, but in practice they compress down to about 4380c1449561SEric Taylor * 1100 bytes 4381c1449561SEric Taylor */ 4382c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 4383c1449561SEric Taylor volsize += numdb; 4384c1449561SEric Taylor return (volsize); 4385c1449561SEric Taylor } 4386