1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5ea8dc4b6Seschrock * Common Development and Distribution License (the "License"). 6ea8dc4b6Seschrock * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21f3861e1aSahl 22fa9e4066Sahrens /* 2336db6475SEric Taylor * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24a2afb611SJerry Jelinek * Copyright (c) 2013, Joyent, Inc. All rights reserved. 25e9316f76SJoe Stein * Copyright (c) 2011, 2015 by Delphix. All rights reserved. 26f0f3ef5aSGarrett D'Amore * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. 27013023d4SMartin Matuska * Copyright (c) 2013 Martin Matuska. All rights reserved. 28a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved. 2943d68d68SYuri Pankov * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 30*c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com] 31fa9e4066Sahrens */ 32fa9e4066Sahrens 33fa9e4066Sahrens #include <ctype.h> 34fa9e4066Sahrens #include <errno.h> 35fa9e4066Sahrens #include <libintl.h> 36fa9e4066Sahrens #include <math.h> 37fa9e4066Sahrens #include <stdio.h> 38fa9e4066Sahrens #include <stdlib.h> 39fa9e4066Sahrens #include <strings.h> 40fa9e4066Sahrens #include <unistd.h> 413cb34c60Sahrens #include <stddef.h> 42fa9e4066Sahrens #include <zone.h> 4399653d4eSeschrock #include <fcntl.h> 44fa9e4066Sahrens #include <sys/mntent.h> 45b12a1c38Slling #include <sys/mount.h> 46ecd6cf80Smarks #include <priv.h> 47ecd6cf80Smarks #include <pwd.h> 48ecd6cf80Smarks #include <grp.h> 49ecd6cf80Smarks #include <stddef.h> 50ecd6cf80Smarks #include <ucred.h> 5114843421SMatthew Ahrens #include <idmap.h> 5214843421SMatthew Ahrens #include <aclutils.h> 533b12c289SMatthew Ahrens #include <directory.h> 54fa9e4066Sahrens 55c1449561SEric Taylor #include <sys/dnode.h> 56fa9e4066Sahrens #include <sys/spa.h> 57e9dbad6fSeschrock #include <sys/zap.h> 58fa9e4066Sahrens #include <libzfs.h> 59fa9e4066Sahrens 60fa9e4066Sahrens #include "zfs_namecheck.h" 61fa9e4066Sahrens #include "zfs_prop.h" 62fa9e4066Sahrens #include "libzfs_impl.h" 63ecd6cf80Smarks #include "zfs_deleg.h" 64fa9e4066Sahrens 6514843421SMatthew Ahrens static int userquota_propname_decode(const char *propname, boolean_t zoned, 6614843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); 67cdf5b4caSmmusante 68fa9e4066Sahrens /* 69fa9e4066Sahrens * Given a single type (not a mask of types), return the type in a human 70fa9e4066Sahrens * readable form. 71fa9e4066Sahrens */ 72fa9e4066Sahrens const char * 73fa9e4066Sahrens zfs_type_to_name(zfs_type_t type) 74fa9e4066Sahrens { 75fa9e4066Sahrens switch (type) { 76fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 77fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 78fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 79fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 80fa9e4066Sahrens case ZFS_TYPE_VOLUME: 81fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 82fa9e4066Sahrens } 83fa9e4066Sahrens 84fa9e4066Sahrens return (NULL); 85fa9e4066Sahrens } 86fa9e4066Sahrens 87fa9e4066Sahrens /* 88fa9e4066Sahrens * Given a path and mask of ZFS types, return a string describing this dataset. 89fa9e4066Sahrens * This is used when we fail to open a dataset and we cannot get an exact type. 90fa9e4066Sahrens * We guess what the type would have been based on the path and the mask of 91fa9e4066Sahrens * acceptable types. 92fa9e4066Sahrens */ 93fa9e4066Sahrens static const char * 94fa9e4066Sahrens path_to_str(const char *path, int types) 95fa9e4066Sahrens { 96fa9e4066Sahrens /* 97fa9e4066Sahrens * When given a single type, always report the exact type. 98fa9e4066Sahrens */ 99fa9e4066Sahrens if (types == ZFS_TYPE_SNAPSHOT) 100fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 101fa9e4066Sahrens if (types == ZFS_TYPE_FILESYSTEM) 102fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 103fa9e4066Sahrens if (types == ZFS_TYPE_VOLUME) 104fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 105fa9e4066Sahrens 106fa9e4066Sahrens /* 107fa9e4066Sahrens * The user is requesting more than one type of dataset. If this is the 108fa9e4066Sahrens * case, consult the path itself. If we're looking for a snapshot, and 109fa9e4066Sahrens * a '@' is found, then report it as "snapshot". Otherwise, remove the 110fa9e4066Sahrens * snapshot attribute and try again. 111fa9e4066Sahrens */ 112fa9e4066Sahrens if (types & ZFS_TYPE_SNAPSHOT) { 113fa9e4066Sahrens if (strchr(path, '@') != NULL) 114fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "snapshot")); 115fa9e4066Sahrens return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); 116fa9e4066Sahrens } 117fa9e4066Sahrens 118fa9e4066Sahrens /* 119fa9e4066Sahrens * The user has requested either filesystems or volumes. 120fa9e4066Sahrens * We have no way of knowing a priori what type this would be, so always 121fa9e4066Sahrens * report it as "filesystem" or "volume", our two primitive types. 122fa9e4066Sahrens */ 123fa9e4066Sahrens if (types & ZFS_TYPE_FILESYSTEM) 124fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "filesystem")); 125fa9e4066Sahrens 126fa9e4066Sahrens assert(types & ZFS_TYPE_VOLUME); 127fa9e4066Sahrens return (dgettext(TEXT_DOMAIN, "volume")); 128fa9e4066Sahrens } 129fa9e4066Sahrens 130fa9e4066Sahrens /* 131fa9e4066Sahrens * Validate a ZFS path. This is used even before trying to open the dataset, to 13214843421SMatthew Ahrens * provide a more meaningful error message. We call zfs_error_aux() to 13314843421SMatthew Ahrens * explain exactly why the name was not valid. 134fa9e4066Sahrens */ 13599d5e173STim Haley int 136f18faf3fSek110237 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, 137f18faf3fSek110237 boolean_t modifying) 138fa9e4066Sahrens { 139fa9e4066Sahrens namecheck_err_t why; 140fa9e4066Sahrens char what; 141fa9e4066Sahrens 1421af68beaSAlexander Stetsenko (void) zfs_prop_get_table(); 143fa9e4066Sahrens if (dataset_namecheck(path, &why, &what) != 0) { 14499653d4eSeschrock if (hdl != NULL) { 145fa9e4066Sahrens switch (why) { 146b81d61a6Slling case NAME_ERR_TOOLONG: 14799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14899653d4eSeschrock "name is too long")); 149b81d61a6Slling break; 150b81d61a6Slling 151fa9e4066Sahrens case NAME_ERR_LEADING_SLASH: 15299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15399653d4eSeschrock "leading slash in name")); 154fa9e4066Sahrens break; 155fa9e4066Sahrens 156fa9e4066Sahrens case NAME_ERR_EMPTY_COMPONENT: 15799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 15899653d4eSeschrock "empty component in name")); 159fa9e4066Sahrens break; 160fa9e4066Sahrens 161fa9e4066Sahrens case NAME_ERR_TRAILING_SLASH: 16299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 16399653d4eSeschrock "trailing slash in name")); 164fa9e4066Sahrens break; 165fa9e4066Sahrens 166fa9e4066Sahrens case NAME_ERR_INVALCHAR: 16799653d4eSeschrock zfs_error_aux(hdl, 168fa9e4066Sahrens dgettext(TEXT_DOMAIN, "invalid character " 16999653d4eSeschrock "'%c' in name"), what); 170fa9e4066Sahrens break; 171fa9e4066Sahrens 172fa9e4066Sahrens case NAME_ERR_MULTIPLE_AT: 17399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 17499653d4eSeschrock "multiple '@' delimiters in name")); 175fa9e4066Sahrens break; 1765ad82045Snd150628 1775ad82045Snd150628 case NAME_ERR_NOLETTER: 1785ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1795ad82045Snd150628 "pool doesn't begin with a letter")); 1805ad82045Snd150628 break; 1815ad82045Snd150628 1825ad82045Snd150628 case NAME_ERR_RESERVED: 1835ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1845ad82045Snd150628 "name is reserved")); 1855ad82045Snd150628 break; 1865ad82045Snd150628 1875ad82045Snd150628 case NAME_ERR_DISKLIKE: 1885ad82045Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1895ad82045Snd150628 "reserved disk name")); 1905ad82045Snd150628 break; 191fa9e4066Sahrens } 192fa9e4066Sahrens } 193fa9e4066Sahrens 194fa9e4066Sahrens return (0); 195fa9e4066Sahrens } 196fa9e4066Sahrens 197fa9e4066Sahrens if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { 19899653d4eSeschrock if (hdl != NULL) 19999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 20099653d4eSeschrock "snapshot delimiter '@' in filesystem name")); 201fa9e4066Sahrens return (0); 202fa9e4066Sahrens } 203fa9e4066Sahrens 2041d452cf5Sahrens if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { 2051d452cf5Sahrens if (hdl != NULL) 2061d452cf5Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 207d7d4af51Smmusante "missing '@' delimiter in snapshot name")); 2081d452cf5Sahrens return (0); 2091d452cf5Sahrens } 2101d452cf5Sahrens 211f18faf3fSek110237 if (modifying && strchr(path, '%') != NULL) { 212f18faf3fSek110237 if (hdl != NULL) 213f18faf3fSek110237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 214f18faf3fSek110237 "invalid character %c in name"), '%'); 215f18faf3fSek110237 return (0); 216f18faf3fSek110237 } 217f18faf3fSek110237 21899653d4eSeschrock return (-1); 219fa9e4066Sahrens } 220fa9e4066Sahrens 221fa9e4066Sahrens int 222fa9e4066Sahrens zfs_name_valid(const char *name, zfs_type_t type) 223fa9e4066Sahrens { 224e7cbe64fSgw25295 if (type == ZFS_TYPE_POOL) 225e7cbe64fSgw25295 return (zpool_name_valid(NULL, B_FALSE, name)); 226f18faf3fSek110237 return (zfs_validate_name(NULL, name, type, B_FALSE)); 227fa9e4066Sahrens } 228fa9e4066Sahrens 229fa9e4066Sahrens /* 230e9dbad6fSeschrock * This function takes the raw DSL properties, and filters out the user-defined 231e9dbad6fSeschrock * properties into a separate nvlist. 232e9dbad6fSeschrock */ 233fac3008cSeschrock static nvlist_t * 234fac3008cSeschrock process_user_props(zfs_handle_t *zhp, nvlist_t *props) 235e9dbad6fSeschrock { 236e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 237e9dbad6fSeschrock nvpair_t *elem; 238e9dbad6fSeschrock nvlist_t *propval; 239fac3008cSeschrock nvlist_t *nvl; 240e9dbad6fSeschrock 241fac3008cSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { 242fac3008cSeschrock (void) no_memory(hdl); 243fac3008cSeschrock return (NULL); 244fac3008cSeschrock } 245e9dbad6fSeschrock 246e9dbad6fSeschrock elem = NULL; 247fac3008cSeschrock while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { 248e9dbad6fSeschrock if (!zfs_prop_user(nvpair_name(elem))) 249e9dbad6fSeschrock continue; 250e9dbad6fSeschrock 251e9dbad6fSeschrock verify(nvpair_value_nvlist(elem, &propval) == 0); 252fac3008cSeschrock if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { 253fac3008cSeschrock nvlist_free(nvl); 254fac3008cSeschrock (void) no_memory(hdl); 255fac3008cSeschrock return (NULL); 256fac3008cSeschrock } 257e9dbad6fSeschrock } 258e9dbad6fSeschrock 259fac3008cSeschrock return (nvl); 260e9dbad6fSeschrock } 261e9dbad6fSeschrock 26229ab75c9Srm160521 static zpool_handle_t * 26329ab75c9Srm160521 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) 26429ab75c9Srm160521 { 26529ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 26629ab75c9Srm160521 zpool_handle_t *zph; 26729ab75c9Srm160521 26829ab75c9Srm160521 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { 26929ab75c9Srm160521 if (hdl->libzfs_pool_handles != NULL) 27029ab75c9Srm160521 zph->zpool_next = hdl->libzfs_pool_handles; 27129ab75c9Srm160521 hdl->libzfs_pool_handles = zph; 27229ab75c9Srm160521 } 27329ab75c9Srm160521 return (zph); 27429ab75c9Srm160521 } 27529ab75c9Srm160521 27629ab75c9Srm160521 static zpool_handle_t * 27729ab75c9Srm160521 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) 27829ab75c9Srm160521 { 27929ab75c9Srm160521 libzfs_handle_t *hdl = zhp->zfs_hdl; 28029ab75c9Srm160521 zpool_handle_t *zph = hdl->libzfs_pool_handles; 28129ab75c9Srm160521 28229ab75c9Srm160521 while ((zph != NULL) && 28329ab75c9Srm160521 (strncmp(pool_name, zpool_get_name(zph), len) != 0)) 28429ab75c9Srm160521 zph = zph->zpool_next; 28529ab75c9Srm160521 return (zph); 28629ab75c9Srm160521 } 28729ab75c9Srm160521 28829ab75c9Srm160521 /* 28929ab75c9Srm160521 * Returns a handle to the pool that contains the provided dataset. 29029ab75c9Srm160521 * If a handle to that pool already exists then that handle is returned. 29129ab75c9Srm160521 * Otherwise, a new handle is created and added to the list of handles. 29229ab75c9Srm160521 */ 29329ab75c9Srm160521 static zpool_handle_t * 29429ab75c9Srm160521 zpool_handle(zfs_handle_t *zhp) 29529ab75c9Srm160521 { 29629ab75c9Srm160521 char *pool_name; 29729ab75c9Srm160521 int len; 29829ab75c9Srm160521 zpool_handle_t *zph; 29929ab75c9Srm160521 30078f17100SMatthew Ahrens len = strcspn(zhp->zfs_name, "/@#") + 1; 30129ab75c9Srm160521 pool_name = zfs_alloc(zhp->zfs_hdl, len); 30229ab75c9Srm160521 (void) strlcpy(pool_name, zhp->zfs_name, len); 30329ab75c9Srm160521 30429ab75c9Srm160521 zph = zpool_find_handle(zhp, pool_name, len); 30529ab75c9Srm160521 if (zph == NULL) 30629ab75c9Srm160521 zph = zpool_add_handle(zhp, pool_name); 30729ab75c9Srm160521 30829ab75c9Srm160521 free(pool_name); 30929ab75c9Srm160521 return (zph); 31029ab75c9Srm160521 } 31129ab75c9Srm160521 31229ab75c9Srm160521 void 31329ab75c9Srm160521 zpool_free_handles(libzfs_handle_t *hdl) 31429ab75c9Srm160521 { 31529ab75c9Srm160521 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; 31629ab75c9Srm160521 31729ab75c9Srm160521 while (zph != NULL) { 31829ab75c9Srm160521 next = zph->zpool_next; 31929ab75c9Srm160521 zpool_close(zph); 32029ab75c9Srm160521 zph = next; 32129ab75c9Srm160521 } 32229ab75c9Srm160521 hdl->libzfs_pool_handles = NULL; 32329ab75c9Srm160521 } 32429ab75c9Srm160521 325e9dbad6fSeschrock /* 326fa9e4066Sahrens * Utility function to gather stats (objset and zpl) for the given object. 327fa9e4066Sahrens */ 328fa9e4066Sahrens static int 329ebedde84SEric Taylor get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) 330fa9e4066Sahrens { 331e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 332fa9e4066Sahrens 333ebedde84SEric Taylor (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); 334fa9e4066Sahrens 335ebedde84SEric Taylor while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { 3367f7322feSeschrock if (errno == ENOMEM) { 337ebedde84SEric Taylor if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { 33899653d4eSeschrock return (-1); 339e9dbad6fSeschrock } 3407f7322feSeschrock } else { 341fa9e4066Sahrens return (-1); 3427f7322feSeschrock } 3437f7322feSeschrock } 344ebedde84SEric Taylor return (0); 345fac3008cSeschrock } 346fac3008cSeschrock 34792241e0bSTom Erickson /* 34892241e0bSTom Erickson * Utility function to get the received properties of the given object. 34992241e0bSTom Erickson */ 35092241e0bSTom Erickson static int 35192241e0bSTom Erickson get_recvd_props_ioctl(zfs_handle_t *zhp) 35292241e0bSTom Erickson { 35392241e0bSTom Erickson libzfs_handle_t *hdl = zhp->zfs_hdl; 35492241e0bSTom Erickson nvlist_t *recvdprops; 35592241e0bSTom Erickson zfs_cmd_t zc = { 0 }; 35692241e0bSTom Erickson int err; 35792241e0bSTom Erickson 35892241e0bSTom Erickson if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) 35992241e0bSTom Erickson return (-1); 36092241e0bSTom Erickson 36192241e0bSTom Erickson (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 36292241e0bSTom Erickson 36392241e0bSTom Erickson while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { 36492241e0bSTom Erickson if (errno == ENOMEM) { 36592241e0bSTom Erickson if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { 36692241e0bSTom Erickson return (-1); 36792241e0bSTom Erickson } 36892241e0bSTom Erickson } else { 36992241e0bSTom Erickson zcmd_free_nvlists(&zc); 37092241e0bSTom Erickson return (-1); 37192241e0bSTom Erickson } 37292241e0bSTom Erickson } 37392241e0bSTom Erickson 37492241e0bSTom Erickson err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); 37592241e0bSTom Erickson zcmd_free_nvlists(&zc); 37692241e0bSTom Erickson if (err != 0) 37792241e0bSTom Erickson return (-1); 37892241e0bSTom Erickson 37992241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 38092241e0bSTom Erickson zhp->zfs_recvd_props = recvdprops; 38192241e0bSTom Erickson 38292241e0bSTom Erickson return (0); 38392241e0bSTom Erickson } 38492241e0bSTom Erickson 385ebedde84SEric Taylor static int 386ebedde84SEric Taylor put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) 387ebedde84SEric Taylor { 388ebedde84SEric Taylor nvlist_t *allprops, *userprops; 389ebedde84SEric Taylor 390ebedde84SEric Taylor zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ 391ebedde84SEric Taylor 392ebedde84SEric Taylor if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { 393ebedde84SEric Taylor return (-1); 394ebedde84SEric Taylor } 395fac3008cSeschrock 39614843421SMatthew Ahrens /* 39714843421SMatthew Ahrens * XXX Why do we store the user props separately, in addition to 39814843421SMatthew Ahrens * storing them in zfs_props? 39914843421SMatthew Ahrens */ 400fac3008cSeschrock if ((userprops = process_user_props(zhp, allprops)) == NULL) { 401fac3008cSeschrock nvlist_free(allprops); 402fac3008cSeschrock return (-1); 403fac3008cSeschrock } 404fac3008cSeschrock 40599653d4eSeschrock nvlist_free(zhp->zfs_props); 406fac3008cSeschrock nvlist_free(zhp->zfs_user_props); 40799653d4eSeschrock 408fac3008cSeschrock zhp->zfs_props = allprops; 409fac3008cSeschrock zhp->zfs_user_props = userprops; 41099653d4eSeschrock 411fa9e4066Sahrens return (0); 412fa9e4066Sahrens } 413fa9e4066Sahrens 414ebedde84SEric Taylor static int 415ebedde84SEric Taylor get_stats(zfs_handle_t *zhp) 416ebedde84SEric Taylor { 417ebedde84SEric Taylor int rc = 0; 418ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 419ebedde84SEric Taylor 420ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 421ebedde84SEric Taylor return (-1); 422ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) != 0) 423ebedde84SEric Taylor rc = -1; 424ebedde84SEric Taylor else if (put_stats_zhdl(zhp, &zc) != 0) 425ebedde84SEric Taylor rc = -1; 426ebedde84SEric Taylor zcmd_free_nvlists(&zc); 427ebedde84SEric Taylor return (rc); 428ebedde84SEric Taylor } 429ebedde84SEric Taylor 430fa9e4066Sahrens /* 431fa9e4066Sahrens * Refresh the properties currently stored in the handle. 432fa9e4066Sahrens */ 433fa9e4066Sahrens void 434fa9e4066Sahrens zfs_refresh_properties(zfs_handle_t *zhp) 435fa9e4066Sahrens { 436fa9e4066Sahrens (void) get_stats(zhp); 437fa9e4066Sahrens } 438fa9e4066Sahrens 439fa9e4066Sahrens /* 440fa9e4066Sahrens * Makes a handle from the given dataset name. Used by zfs_open() and 441fa9e4066Sahrens * zfs_iter_* to create child handles on the fly. 442fa9e4066Sahrens */ 443ebedde84SEric Taylor static int 444ebedde84SEric Taylor make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) 445fa9e4066Sahrens { 446503ad85cSMatthew Ahrens if (put_stats_zhdl(zhp, zc) != 0) 447ebedde84SEric Taylor return (-1); 44831fd60d3Sahrens 449fa9e4066Sahrens /* 450fa9e4066Sahrens * We've managed to open the dataset and gather statistics. Determine 451fa9e4066Sahrens * the high-level type. 452fa9e4066Sahrens */ 453a2eea2e1Sahrens if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 454a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_VOLUME; 455a2eea2e1Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 456a2eea2e1Sahrens zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; 457a2eea2e1Sahrens else 458a2eea2e1Sahrens abort(); 459a2eea2e1Sahrens 460fa9e4066Sahrens if (zhp->zfs_dmustats.dds_is_snapshot) 461fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_SNAPSHOT; 462fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) 463fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_VOLUME; 464fa9e4066Sahrens else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) 465fa9e4066Sahrens zhp->zfs_type = ZFS_TYPE_FILESYSTEM; 466fa9e4066Sahrens else 46799653d4eSeschrock abort(); /* we should never see any other types */ 468fa9e4066Sahrens 4699fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) 4709fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States return (-1); 4719fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 472ebedde84SEric Taylor return (0); 473ebedde84SEric Taylor } 474ebedde84SEric Taylor 475ebedde84SEric Taylor zfs_handle_t * 476ebedde84SEric Taylor make_dataset_handle(libzfs_handle_t *hdl, const char *path) 477ebedde84SEric Taylor { 478ebedde84SEric Taylor zfs_cmd_t zc = { 0 }; 479ebedde84SEric Taylor 480ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 481ebedde84SEric Taylor 482ebedde84SEric Taylor if (zhp == NULL) 483ebedde84SEric Taylor return (NULL); 484ebedde84SEric Taylor 485ebedde84SEric Taylor zhp->zfs_hdl = hdl; 486ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 487ebedde84SEric Taylor if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { 488ebedde84SEric Taylor free(zhp); 489ebedde84SEric Taylor return (NULL); 490ebedde84SEric Taylor } 491ebedde84SEric Taylor if (get_stats_ioctl(zhp, &zc) == -1) { 492ebedde84SEric Taylor zcmd_free_nvlists(&zc); 493ebedde84SEric Taylor free(zhp); 494ebedde84SEric Taylor return (NULL); 495ebedde84SEric Taylor } 496ebedde84SEric Taylor if (make_dataset_handle_common(zhp, &zc) == -1) { 497ebedde84SEric Taylor free(zhp); 498ebedde84SEric Taylor zhp = NULL; 499ebedde84SEric Taylor } 500ebedde84SEric Taylor zcmd_free_nvlists(&zc); 501ebedde84SEric Taylor return (zhp); 502ebedde84SEric Taylor } 503ebedde84SEric Taylor 50419b94df9SMatthew Ahrens zfs_handle_t * 505ebedde84SEric Taylor make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) 506ebedde84SEric Taylor { 507ebedde84SEric Taylor zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 508ebedde84SEric Taylor 509ebedde84SEric Taylor if (zhp == NULL) 510ebedde84SEric Taylor return (NULL); 511ebedde84SEric Taylor 512ebedde84SEric Taylor zhp->zfs_hdl = hdl; 513ebedde84SEric Taylor (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); 514ebedde84SEric Taylor if (make_dataset_handle_common(zhp, zc) == -1) { 515ebedde84SEric Taylor free(zhp); 516ebedde84SEric Taylor return (NULL); 517ebedde84SEric Taylor } 518fa9e4066Sahrens return (zhp); 519fa9e4066Sahrens } 520fa9e4066Sahrens 52119b94df9SMatthew Ahrens zfs_handle_t * 52219b94df9SMatthew Ahrens zfs_handle_dup(zfs_handle_t *zhp_orig) 52319b94df9SMatthew Ahrens { 52419b94df9SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 52519b94df9SMatthew Ahrens 52619b94df9SMatthew Ahrens if (zhp == NULL) 52719b94df9SMatthew Ahrens return (NULL); 52819b94df9SMatthew Ahrens 52919b94df9SMatthew Ahrens zhp->zfs_hdl = zhp_orig->zfs_hdl; 53019b94df9SMatthew Ahrens zhp->zpool_hdl = zhp_orig->zpool_hdl; 53119b94df9SMatthew Ahrens (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, 53219b94df9SMatthew Ahrens sizeof (zhp->zfs_name)); 53319b94df9SMatthew Ahrens zhp->zfs_type = zhp_orig->zfs_type; 53419b94df9SMatthew Ahrens zhp->zfs_head_type = zhp_orig->zfs_head_type; 53519b94df9SMatthew Ahrens zhp->zfs_dmustats = zhp_orig->zfs_dmustats; 53619b94df9SMatthew Ahrens if (zhp_orig->zfs_props != NULL) { 53719b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { 53819b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 53919b94df9SMatthew Ahrens zfs_close(zhp); 54019b94df9SMatthew Ahrens return (NULL); 54119b94df9SMatthew Ahrens } 54219b94df9SMatthew Ahrens } 54319b94df9SMatthew Ahrens if (zhp_orig->zfs_user_props != NULL) { 54419b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_user_props, 54519b94df9SMatthew Ahrens &zhp->zfs_user_props, 0) != 0) { 54619b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 54719b94df9SMatthew Ahrens zfs_close(zhp); 54819b94df9SMatthew Ahrens return (NULL); 54919b94df9SMatthew Ahrens } 55019b94df9SMatthew Ahrens } 55119b94df9SMatthew Ahrens if (zhp_orig->zfs_recvd_props != NULL) { 55219b94df9SMatthew Ahrens if (nvlist_dup(zhp_orig->zfs_recvd_props, 55319b94df9SMatthew Ahrens &zhp->zfs_recvd_props, 0)) { 55419b94df9SMatthew Ahrens (void) no_memory(zhp->zfs_hdl); 55519b94df9SMatthew Ahrens zfs_close(zhp); 55619b94df9SMatthew Ahrens return (NULL); 55719b94df9SMatthew Ahrens } 55819b94df9SMatthew Ahrens } 55919b94df9SMatthew Ahrens zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; 56019b94df9SMatthew Ahrens if (zhp_orig->zfs_mntopts != NULL) { 56119b94df9SMatthew Ahrens zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, 56219b94df9SMatthew Ahrens zhp_orig->zfs_mntopts); 56319b94df9SMatthew Ahrens } 56419b94df9SMatthew Ahrens zhp->zfs_props_table = zhp_orig->zfs_props_table; 56519b94df9SMatthew Ahrens return (zhp); 56619b94df9SMatthew Ahrens } 56719b94df9SMatthew Ahrens 56878f17100SMatthew Ahrens boolean_t 56978f17100SMatthew Ahrens zfs_bookmark_exists(const char *path) 57078f17100SMatthew Ahrens { 57178f17100SMatthew Ahrens nvlist_t *bmarks; 57278f17100SMatthew Ahrens nvlist_t *props; 57378f17100SMatthew Ahrens char fsname[ZFS_MAXNAMELEN]; 57478f17100SMatthew Ahrens char *bmark_name; 57578f17100SMatthew Ahrens char *pound; 57678f17100SMatthew Ahrens int err; 57778f17100SMatthew Ahrens boolean_t rv; 57878f17100SMatthew Ahrens 57978f17100SMatthew Ahrens 58078f17100SMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 58178f17100SMatthew Ahrens pound = strchr(fsname, '#'); 58278f17100SMatthew Ahrens if (pound == NULL) 58378f17100SMatthew Ahrens return (B_FALSE); 58478f17100SMatthew Ahrens 58578f17100SMatthew Ahrens *pound = '\0'; 58678f17100SMatthew Ahrens bmark_name = pound + 1; 58778f17100SMatthew Ahrens props = fnvlist_alloc(); 58878f17100SMatthew Ahrens err = lzc_get_bookmarks(fsname, props, &bmarks); 58978f17100SMatthew Ahrens nvlist_free(props); 59078f17100SMatthew Ahrens if (err != 0) { 59178f17100SMatthew Ahrens nvlist_free(bmarks); 59278f17100SMatthew Ahrens return (B_FALSE); 59378f17100SMatthew Ahrens } 59478f17100SMatthew Ahrens 59578f17100SMatthew Ahrens rv = nvlist_exists(bmarks, bmark_name); 59678f17100SMatthew Ahrens nvlist_free(bmarks); 59778f17100SMatthew Ahrens return (rv); 59878f17100SMatthew Ahrens } 59978f17100SMatthew Ahrens 60078f17100SMatthew Ahrens zfs_handle_t * 60178f17100SMatthew Ahrens make_bookmark_handle(zfs_handle_t *parent, const char *path, 60278f17100SMatthew Ahrens nvlist_t *bmark_props) 60378f17100SMatthew Ahrens { 60478f17100SMatthew Ahrens zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); 60578f17100SMatthew Ahrens 60678f17100SMatthew Ahrens if (zhp == NULL) 60778f17100SMatthew Ahrens return (NULL); 60878f17100SMatthew Ahrens 60978f17100SMatthew Ahrens /* Fill in the name. */ 61078f17100SMatthew Ahrens zhp->zfs_hdl = parent->zfs_hdl; 61178f17100SMatthew Ahrens (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); 61278f17100SMatthew Ahrens 61378f17100SMatthew Ahrens /* Set the property lists. */ 61478f17100SMatthew Ahrens if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { 61578f17100SMatthew Ahrens free(zhp); 61678f17100SMatthew Ahrens return (NULL); 61778f17100SMatthew Ahrens } 61878f17100SMatthew Ahrens 61978f17100SMatthew Ahrens /* Set the types. */ 62078f17100SMatthew Ahrens zhp->zfs_head_type = parent->zfs_head_type; 62178f17100SMatthew Ahrens zhp->zfs_type = ZFS_TYPE_BOOKMARK; 62278f17100SMatthew Ahrens 62378f17100SMatthew Ahrens if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { 62478f17100SMatthew Ahrens nvlist_free(zhp->zfs_props); 62578f17100SMatthew Ahrens free(zhp); 62678f17100SMatthew Ahrens return (NULL); 62778f17100SMatthew Ahrens } 62878f17100SMatthew Ahrens 62978f17100SMatthew Ahrens return (zhp); 63078f17100SMatthew Ahrens } 63178f17100SMatthew Ahrens 632fa9e4066Sahrens /* 633fa9e4066Sahrens * Opens the given snapshot, filesystem, or volume. The 'types' 634fa9e4066Sahrens * argument is a mask of acceptable types. The function will print an 635fa9e4066Sahrens * appropriate error message and return NULL if it can't be opened. 636fa9e4066Sahrens */ 637fa9e4066Sahrens zfs_handle_t * 63899653d4eSeschrock zfs_open(libzfs_handle_t *hdl, const char *path, int types) 639fa9e4066Sahrens { 640fa9e4066Sahrens zfs_handle_t *zhp; 64199653d4eSeschrock char errbuf[1024]; 64299653d4eSeschrock 64399653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 64499653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); 645fa9e4066Sahrens 646fa9e4066Sahrens /* 64799653d4eSeschrock * Validate the name before we even try to open it. 648fa9e4066Sahrens */ 649f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_DATASET, B_FALSE)) { 65099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 65199653d4eSeschrock "invalid dataset name")); 65299653d4eSeschrock (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); 653fa9e4066Sahrens return (NULL); 654fa9e4066Sahrens } 655fa9e4066Sahrens 656fa9e4066Sahrens /* 657fa9e4066Sahrens * Try to get stats for the dataset, which will tell us if it exists. 658fa9e4066Sahrens */ 659fa9e4066Sahrens errno = 0; 66099653d4eSeschrock if ((zhp = make_dataset_handle(hdl, path)) == NULL) { 661ece3d9b3Slling (void) zfs_standard_error(hdl, errno, errbuf); 662fa9e4066Sahrens return (NULL); 663fa9e4066Sahrens } 664fa9e4066Sahrens 665fa9e4066Sahrens if (!(types & zhp->zfs_type)) { 66699653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 66794de1d4cSeschrock zfs_close(zhp); 668fa9e4066Sahrens return (NULL); 669fa9e4066Sahrens } 670fa9e4066Sahrens 671fa9e4066Sahrens return (zhp); 672fa9e4066Sahrens } 673fa9e4066Sahrens 674fa9e4066Sahrens /* 675fa9e4066Sahrens * Release a ZFS handle. Nothing to do but free the associated memory. 676fa9e4066Sahrens */ 677fa9e4066Sahrens void 678fa9e4066Sahrens zfs_close(zfs_handle_t *zhp) 679fa9e4066Sahrens { 680fa9e4066Sahrens if (zhp->zfs_mntopts) 681fa9e4066Sahrens free(zhp->zfs_mntopts); 68299653d4eSeschrock nvlist_free(zhp->zfs_props); 683e9dbad6fSeschrock nvlist_free(zhp->zfs_user_props); 68492241e0bSTom Erickson nvlist_free(zhp->zfs_recvd_props); 685fa9e4066Sahrens free(zhp); 686fa9e4066Sahrens } 687fa9e4066Sahrens 688ebedde84SEric Taylor typedef struct mnttab_node { 689ebedde84SEric Taylor struct mnttab mtn_mt; 690ebedde84SEric Taylor avl_node_t mtn_node; 691ebedde84SEric Taylor } mnttab_node_t; 692ebedde84SEric Taylor 693ebedde84SEric Taylor static int 694ebedde84SEric Taylor libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) 695ebedde84SEric Taylor { 696ebedde84SEric Taylor const mnttab_node_t *mtn1 = arg1; 697ebedde84SEric Taylor const mnttab_node_t *mtn2 = arg2; 698ebedde84SEric Taylor int rv; 699ebedde84SEric Taylor 700ebedde84SEric Taylor rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); 701ebedde84SEric Taylor 702ebedde84SEric Taylor if (rv == 0) 703ebedde84SEric Taylor return (0); 704ebedde84SEric Taylor return (rv > 0 ? 1 : -1); 705ebedde84SEric Taylor } 706ebedde84SEric Taylor 707ebedde84SEric Taylor void 708ebedde84SEric Taylor libzfs_mnttab_init(libzfs_handle_t *hdl) 709ebedde84SEric Taylor { 710ebedde84SEric Taylor assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); 711ebedde84SEric Taylor avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, 712ebedde84SEric Taylor sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); 713b2634b9cSEric Taylor } 714b2634b9cSEric Taylor 715b2634b9cSEric Taylor void 716b2634b9cSEric Taylor libzfs_mnttab_update(libzfs_handle_t *hdl) 717b2634b9cSEric Taylor { 718b2634b9cSEric Taylor struct mnttab entry; 719ebedde84SEric Taylor 720ebedde84SEric Taylor rewind(hdl->libzfs_mnttab); 721ebedde84SEric Taylor while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 722ebedde84SEric Taylor mnttab_node_t *mtn; 723ebedde84SEric Taylor 724ebedde84SEric Taylor if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 725ebedde84SEric Taylor continue; 726ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 727ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); 728ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); 729ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); 730ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); 731ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 732ebedde84SEric Taylor } 733ebedde84SEric Taylor } 734ebedde84SEric Taylor 735ebedde84SEric Taylor void 736ebedde84SEric Taylor libzfs_mnttab_fini(libzfs_handle_t *hdl) 737ebedde84SEric Taylor { 738ebedde84SEric Taylor void *cookie = NULL; 739ebedde84SEric Taylor mnttab_node_t *mtn; 740ebedde84SEric Taylor 741ebedde84SEric Taylor while (mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) { 742ebedde84SEric Taylor free(mtn->mtn_mt.mnt_special); 743ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mountp); 744ebedde84SEric Taylor free(mtn->mtn_mt.mnt_fstype); 745ebedde84SEric Taylor free(mtn->mtn_mt.mnt_mntopts); 746ebedde84SEric Taylor free(mtn); 747ebedde84SEric Taylor } 748ebedde84SEric Taylor avl_destroy(&hdl->libzfs_mnttab_cache); 749ebedde84SEric Taylor } 750ebedde84SEric Taylor 751b2634b9cSEric Taylor void 752b2634b9cSEric Taylor libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) 753b2634b9cSEric Taylor { 754b2634b9cSEric Taylor hdl->libzfs_mnttab_enable = enable; 755b2634b9cSEric Taylor } 756b2634b9cSEric Taylor 757ebedde84SEric Taylor int 758ebedde84SEric Taylor libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, 759ebedde84SEric Taylor struct mnttab *entry) 760ebedde84SEric Taylor { 761ebedde84SEric Taylor mnttab_node_t find; 762ebedde84SEric Taylor mnttab_node_t *mtn; 763ebedde84SEric Taylor 764b2634b9cSEric Taylor if (!hdl->libzfs_mnttab_enable) { 765b2634b9cSEric Taylor struct mnttab srch = { 0 }; 766b2634b9cSEric Taylor 767b2634b9cSEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache)) 768b2634b9cSEric Taylor libzfs_mnttab_fini(hdl); 769b2634b9cSEric Taylor rewind(hdl->libzfs_mnttab); 770b2634b9cSEric Taylor srch.mnt_special = (char *)fsname; 771b2634b9cSEric Taylor srch.mnt_fstype = MNTTYPE_ZFS; 772b2634b9cSEric Taylor if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) 773b2634b9cSEric Taylor return (0); 774b2634b9cSEric Taylor else 775b2634b9cSEric Taylor return (ENOENT); 776b2634b9cSEric Taylor } 777b2634b9cSEric Taylor 778ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 779b2634b9cSEric Taylor libzfs_mnttab_update(hdl); 780ebedde84SEric Taylor 781ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 782ebedde84SEric Taylor mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); 783ebedde84SEric Taylor if (mtn) { 784ebedde84SEric Taylor *entry = mtn->mtn_mt; 785ebedde84SEric Taylor return (0); 786ebedde84SEric Taylor } 787ebedde84SEric Taylor return (ENOENT); 788ebedde84SEric Taylor } 789ebedde84SEric Taylor 790ebedde84SEric Taylor void 791ebedde84SEric Taylor libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, 792ebedde84SEric Taylor const char *mountp, const char *mntopts) 793ebedde84SEric Taylor { 794ebedde84SEric Taylor mnttab_node_t *mtn; 795ebedde84SEric Taylor 796ebedde84SEric Taylor if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) 797ebedde84SEric Taylor return; 798ebedde84SEric Taylor mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); 799ebedde84SEric Taylor mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); 800ebedde84SEric Taylor mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); 801ebedde84SEric Taylor mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); 802ebedde84SEric Taylor mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); 803ebedde84SEric Taylor avl_add(&hdl->libzfs_mnttab_cache, mtn); 804ebedde84SEric Taylor } 805ebedde84SEric Taylor 806ebedde84SEric Taylor void 807ebedde84SEric Taylor libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) 808ebedde84SEric Taylor { 809ebedde84SEric Taylor mnttab_node_t find; 810ebedde84SEric Taylor mnttab_node_t *ret; 811ebedde84SEric Taylor 812ebedde84SEric Taylor find.mtn_mt.mnt_special = (char *)fsname; 813ebedde84SEric Taylor if (ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) { 814ebedde84SEric Taylor avl_remove(&hdl->libzfs_mnttab_cache, ret); 815ebedde84SEric Taylor free(ret->mtn_mt.mnt_special); 816ebedde84SEric Taylor free(ret->mtn_mt.mnt_mountp); 817ebedde84SEric Taylor free(ret->mtn_mt.mnt_fstype); 818ebedde84SEric Taylor free(ret->mtn_mt.mnt_mntopts); 819ebedde84SEric Taylor free(ret); 820ebedde84SEric Taylor } 821ebedde84SEric Taylor } 822ebedde84SEric Taylor 8237b97dc1aSrm160521 int 8247b97dc1aSrm160521 zfs_spa_version(zfs_handle_t *zhp, int *spa_version) 8257b97dc1aSrm160521 { 82629ab75c9Srm160521 zpool_handle_t *zpool_handle = zhp->zpool_hdl; 8277b97dc1aSrm160521 8287b97dc1aSrm160521 if (zpool_handle == NULL) 8297b97dc1aSrm160521 return (-1); 8307b97dc1aSrm160521 8317b97dc1aSrm160521 *spa_version = zpool_get_prop_int(zpool_handle, 8327b97dc1aSrm160521 ZPOOL_PROP_VERSION, NULL); 8337b97dc1aSrm160521 return (0); 8347b97dc1aSrm160521 } 8357b97dc1aSrm160521 8367b97dc1aSrm160521 /* 8377b97dc1aSrm160521 * The choice of reservation property depends on the SPA version. 8387b97dc1aSrm160521 */ 8397b97dc1aSrm160521 static int 8407b97dc1aSrm160521 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) 8417b97dc1aSrm160521 { 8427b97dc1aSrm160521 int spa_version; 8437b97dc1aSrm160521 8447b97dc1aSrm160521 if (zfs_spa_version(zhp, &spa_version) < 0) 8457b97dc1aSrm160521 return (-1); 8467b97dc1aSrm160521 8477b97dc1aSrm160521 if (spa_version >= SPA_VERSION_REFRESERVATION) 8487b97dc1aSrm160521 *resv_prop = ZFS_PROP_REFRESERVATION; 8497b97dc1aSrm160521 else 8507b97dc1aSrm160521 *resv_prop = ZFS_PROP_RESERVATION; 8517b97dc1aSrm160521 8527b97dc1aSrm160521 return (0); 8537b97dc1aSrm160521 } 8547b97dc1aSrm160521 855b1b8ab34Slling /* 856e9dbad6fSeschrock * Given an nvlist of properties to set, validates that they are correct, and 857e9dbad6fSeschrock * parses any numeric properties (index, boolean, etc) if they are specified as 858e9dbad6fSeschrock * strings. 859fa9e4066Sahrens */ 8600a48a24eStimh nvlist_t * 8610a48a24eStimh zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, 862e9316f76SJoe Stein uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl, 863e9316f76SJoe Stein const char *errbuf) 864fa9e4066Sahrens { 865e9dbad6fSeschrock nvpair_t *elem; 866e9dbad6fSeschrock uint64_t intval; 867e9dbad6fSeschrock char *strval; 868990b4856Slling zfs_prop_t prop; 869e9dbad6fSeschrock nvlist_t *ret; 870da6c28aaSamw int chosen_normal = -1; 871da6c28aaSamw int chosen_utf = -1; 872990b4856Slling 873e9dbad6fSeschrock if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { 874e9dbad6fSeschrock (void) no_memory(hdl); 875e9dbad6fSeschrock return (NULL); 876e9dbad6fSeschrock } 877fa9e4066Sahrens 87814843421SMatthew Ahrens /* 87914843421SMatthew Ahrens * Make sure this property is valid and applies to this type. 88014843421SMatthew Ahrens */ 88114843421SMatthew Ahrens 882e9dbad6fSeschrock elem = NULL; 883e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { 884990b4856Slling const char *propname = nvpair_name(elem); 88599653d4eSeschrock 88614843421SMatthew Ahrens prop = zfs_name_to_prop(propname); 88714843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { 888fa9e4066Sahrens /* 88914843421SMatthew Ahrens * This is a user property: make sure it's a 890990b4856Slling * string, and that it's less than ZAP_MAXNAMELEN. 891990b4856Slling */ 892990b4856Slling if (nvpair_type(elem) != DATA_TYPE_STRING) { 893990b4856Slling zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 894990b4856Slling "'%s' must be a string"), propname); 895990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 896990b4856Slling goto error; 897990b4856Slling } 898990b4856Slling 899990b4856Slling if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { 900e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 901e9dbad6fSeschrock "property name '%s' is too long"), 902e9dbad6fSeschrock propname); 903990b4856Slling (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 904e9dbad6fSeschrock goto error; 905e9dbad6fSeschrock } 906e9dbad6fSeschrock 907e9dbad6fSeschrock (void) nvpair_value_string(elem, &strval); 908e9dbad6fSeschrock if (nvlist_add_string(ret, propname, strval) != 0) { 909e9dbad6fSeschrock (void) no_memory(hdl); 910e9dbad6fSeschrock goto error; 911e9dbad6fSeschrock } 912e9dbad6fSeschrock continue; 913e9dbad6fSeschrock } 914fa9e4066Sahrens 91514843421SMatthew Ahrens /* 91614843421SMatthew Ahrens * Currently, only user properties can be modified on 91714843421SMatthew Ahrens * snapshots. 91814843421SMatthew Ahrens */ 919bb0ade09Sahrens if (type == ZFS_TYPE_SNAPSHOT) { 920bb0ade09Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 921bb0ade09Sahrens "this property can not be modified for snapshots")); 922bb0ade09Sahrens (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 923bb0ade09Sahrens goto error; 924bb0ade09Sahrens } 925bb0ade09Sahrens 92614843421SMatthew Ahrens if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { 92714843421SMatthew Ahrens zfs_userquota_prop_t uqtype; 92814843421SMatthew Ahrens char newpropname[128]; 92914843421SMatthew Ahrens char domain[128]; 93014843421SMatthew Ahrens uint64_t rid; 93114843421SMatthew Ahrens uint64_t valary[3]; 93214843421SMatthew Ahrens 93314843421SMatthew Ahrens if (userquota_propname_decode(propname, zoned, 93414843421SMatthew Ahrens &uqtype, domain, sizeof (domain), &rid) != 0) { 93514843421SMatthew Ahrens zfs_error_aux(hdl, 93614843421SMatthew Ahrens dgettext(TEXT_DOMAIN, 93714843421SMatthew Ahrens "'%s' has an invalid user/group name"), 93814843421SMatthew Ahrens propname); 93914843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 94014843421SMatthew Ahrens goto error; 94114843421SMatthew Ahrens } 94214843421SMatthew Ahrens 94314843421SMatthew Ahrens if (uqtype != ZFS_PROP_USERQUOTA && 94414843421SMatthew Ahrens uqtype != ZFS_PROP_GROUPQUOTA) { 94514843421SMatthew Ahrens zfs_error_aux(hdl, 94614843421SMatthew Ahrens dgettext(TEXT_DOMAIN, "'%s' is readonly"), 94714843421SMatthew Ahrens propname); 94814843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, 94914843421SMatthew Ahrens errbuf); 95014843421SMatthew Ahrens goto error; 95114843421SMatthew Ahrens } 95214843421SMatthew Ahrens 95314843421SMatthew Ahrens if (nvpair_type(elem) == DATA_TYPE_STRING) { 95414843421SMatthew Ahrens (void) nvpair_value_string(elem, &strval); 95514843421SMatthew Ahrens if (strcmp(strval, "none") == 0) { 95614843421SMatthew Ahrens intval = 0; 95714843421SMatthew Ahrens } else if (zfs_nicestrtonum(hdl, 95814843421SMatthew Ahrens strval, &intval) != 0) { 95914843421SMatthew Ahrens (void) zfs_error(hdl, 96014843421SMatthew Ahrens EZFS_BADPROP, errbuf); 96114843421SMatthew Ahrens goto error; 96214843421SMatthew Ahrens } 96314843421SMatthew Ahrens } else if (nvpair_type(elem) == 96414843421SMatthew Ahrens DATA_TYPE_UINT64) { 96514843421SMatthew Ahrens (void) nvpair_value_uint64(elem, &intval); 96614843421SMatthew Ahrens if (intval == 0) { 96714843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 96814843421SMatthew Ahrens "use 'none' to disable " 96914843421SMatthew Ahrens "userquota/groupquota")); 97014843421SMatthew Ahrens goto error; 97114843421SMatthew Ahrens } 97214843421SMatthew Ahrens } else { 97314843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 97414843421SMatthew Ahrens "'%s' must be a number"), propname); 97514843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 97614843421SMatthew Ahrens goto error; 97714843421SMatthew Ahrens } 97814843421SMatthew Ahrens 9792d5843dbSMatthew Ahrens /* 9802d5843dbSMatthew Ahrens * Encode the prop name as 9812d5843dbSMatthew Ahrens * userquota@<hex-rid>-domain, to make it easy 9822d5843dbSMatthew Ahrens * for the kernel to decode. 9832d5843dbSMatthew Ahrens */ 98414843421SMatthew Ahrens (void) snprintf(newpropname, sizeof (newpropname), 9852d5843dbSMatthew Ahrens "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype], 9862d5843dbSMatthew Ahrens (longlong_t)rid, domain); 98714843421SMatthew Ahrens valary[0] = uqtype; 98814843421SMatthew Ahrens valary[1] = rid; 98914843421SMatthew Ahrens valary[2] = intval; 99014843421SMatthew Ahrens if (nvlist_add_uint64_array(ret, newpropname, 99114843421SMatthew Ahrens valary, 3) != 0) { 99214843421SMatthew Ahrens (void) no_memory(hdl); 99314843421SMatthew Ahrens goto error; 99414843421SMatthew Ahrens } 99514843421SMatthew Ahrens continue; 99619b94df9SMatthew Ahrens } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { 99719b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 99819b94df9SMatthew Ahrens "'%s' is readonly"), 99919b94df9SMatthew Ahrens propname); 100019b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 100119b94df9SMatthew Ahrens goto error; 100214843421SMatthew Ahrens } 100314843421SMatthew Ahrens 100414843421SMatthew Ahrens if (prop == ZPROP_INVAL) { 100514843421SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 100614843421SMatthew Ahrens "invalid property '%s'"), propname); 100714843421SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 100814843421SMatthew Ahrens goto error; 100914843421SMatthew Ahrens } 101014843421SMatthew Ahrens 1011e9dbad6fSeschrock if (!zfs_prop_valid_for_type(prop, type)) { 1012e9dbad6fSeschrock zfs_error_aux(hdl, 1013e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' does not " 1014e9dbad6fSeschrock "apply to datasets of this type"), propname); 1015e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); 1016e9dbad6fSeschrock goto error; 1017e9dbad6fSeschrock } 1018e9dbad6fSeschrock 1019e9dbad6fSeschrock if (zfs_prop_readonly(prop) && 1020da6c28aaSamw (!zfs_prop_setonce(prop) || zhp != NULL)) { 1021e9dbad6fSeschrock zfs_error_aux(hdl, 1022e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "'%s' is readonly"), 1023e9dbad6fSeschrock propname); 1024e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); 1025e9dbad6fSeschrock goto error; 1026e9dbad6fSeschrock } 1027e9dbad6fSeschrock 1028990b4856Slling if (zprop_parse_value(hdl, elem, prop, type, ret, 1029990b4856Slling &strval, &intval, errbuf) != 0) 1030e9dbad6fSeschrock goto error; 1031e9dbad6fSeschrock 1032e9dbad6fSeschrock /* 1033e9dbad6fSeschrock * Perform some additional checks for specific properties. 1034e9dbad6fSeschrock */ 1035e9dbad6fSeschrock switch (prop) { 1036e7437265Sahrens case ZFS_PROP_VERSION: 1037e7437265Sahrens { 1038e7437265Sahrens int version; 1039e7437265Sahrens 1040e7437265Sahrens if (zhp == NULL) 1041e7437265Sahrens break; 1042e7437265Sahrens version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); 1043e7437265Sahrens if (intval < version) { 1044e7437265Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1045e7437265Sahrens "Can not downgrade; already at version %u"), 1046e7437265Sahrens version); 1047e7437265Sahrens (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1048e7437265Sahrens goto error; 1049e7437265Sahrens } 1050e7437265Sahrens break; 1051e7437265Sahrens } 1052e7437265Sahrens 1053e9dbad6fSeschrock case ZFS_PROP_VOLBLOCKSIZE: 1054b5152584SMatthew Ahrens case ZFS_PROP_RECORDSIZE: 1055b5152584SMatthew Ahrens { 1056b5152584SMatthew Ahrens int maxbs = SPA_MAXBLOCKSIZE; 1057e9316f76SJoe Stein if (zpool_hdl != NULL) { 1058e9316f76SJoe Stein maxbs = zpool_get_prop_int(zpool_hdl, 1059b5152584SMatthew Ahrens ZPOOL_PROP_MAXBLOCKSIZE, NULL); 1060b5152584SMatthew Ahrens } 1061b5152584SMatthew Ahrens /* 1062b5152584SMatthew Ahrens * Volumes are limited to a volblocksize of 128KB, 1063b5152584SMatthew Ahrens * because they typically service workloads with 1064b5152584SMatthew Ahrens * small random writes, which incur a large performance 1065b5152584SMatthew Ahrens * penalty with large blocks. 1066b5152584SMatthew Ahrens */ 1067b5152584SMatthew Ahrens if (prop == ZFS_PROP_VOLBLOCKSIZE) 1068b5152584SMatthew Ahrens maxbs = SPA_OLD_MAXBLOCKSIZE; 1069b5152584SMatthew Ahrens /* 1070b5152584SMatthew Ahrens * The value must be a power of two between 1071b5152584SMatthew Ahrens * SPA_MINBLOCKSIZE and maxbs. 1072b5152584SMatthew Ahrens */ 1073e9dbad6fSeschrock if (intval < SPA_MINBLOCKSIZE || 1074b5152584SMatthew Ahrens intval > maxbs || !ISP2(intval)) { 1075e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1076b5152584SMatthew Ahrens "'%s' must be power of 2 from 512B " 1077b5152584SMatthew Ahrens "to %uKB"), propname, maxbs >> 10); 1078e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1079e9dbad6fSeschrock goto error; 1080e9dbad6fSeschrock } 1081e9dbad6fSeschrock break; 1082b5152584SMatthew Ahrens } 10834201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 10844201a95eSRic Aleshire { 10854201a95eSRic Aleshire /* 10864201a95eSRic Aleshire * Verify the mlslabel string and convert to 10874201a95eSRic Aleshire * internal hex label string. 10884201a95eSRic Aleshire */ 10894201a95eSRic Aleshire 10904201a95eSRic Aleshire m_label_t *new_sl; 10914201a95eSRic Aleshire char *hex = NULL; /* internal label string */ 10924201a95eSRic Aleshire 10934201a95eSRic Aleshire /* Default value is already OK. */ 10944201a95eSRic Aleshire if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) 10954201a95eSRic Aleshire break; 10964201a95eSRic Aleshire 10974201a95eSRic Aleshire /* Verify the label can be converted to binary form */ 10984201a95eSRic Aleshire if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || 10994201a95eSRic Aleshire (str_to_label(strval, &new_sl, MAC_LABEL, 11004201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1)) { 11014201a95eSRic Aleshire goto badlabel; 11024201a95eSRic Aleshire } 11034201a95eSRic Aleshire 11044201a95eSRic Aleshire /* Now translate to hex internal label string */ 11054201a95eSRic Aleshire if (label_to_str(new_sl, &hex, M_INTERNAL, 11064201a95eSRic Aleshire DEF_NAMES) != 0) { 11074201a95eSRic Aleshire if (hex) 11084201a95eSRic Aleshire free(hex); 11094201a95eSRic Aleshire goto badlabel; 11104201a95eSRic Aleshire } 11114201a95eSRic Aleshire m_label_free(new_sl); 11124201a95eSRic Aleshire 11134201a95eSRic Aleshire /* If string is already in internal form, we're done. */ 11144201a95eSRic Aleshire if (strcmp(strval, hex) == 0) { 11154201a95eSRic Aleshire free(hex); 11164201a95eSRic Aleshire break; 11174201a95eSRic Aleshire } 11184201a95eSRic Aleshire 11194201a95eSRic Aleshire /* Replace the label string with the internal form. */ 1120569038c9SRic Aleshire (void) nvlist_remove(ret, zfs_prop_to_name(prop), 11214201a95eSRic Aleshire DATA_TYPE_STRING); 11224201a95eSRic Aleshire verify(nvlist_add_string(ret, zfs_prop_to_name(prop), 11234201a95eSRic Aleshire hex) == 0); 11244201a95eSRic Aleshire free(hex); 11254201a95eSRic Aleshire 11264201a95eSRic Aleshire break; 11274201a95eSRic Aleshire 11284201a95eSRic Aleshire badlabel: 11294201a95eSRic Aleshire zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 11304201a95eSRic Aleshire "invalid mlslabel '%s'"), strval); 11314201a95eSRic Aleshire (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 11324201a95eSRic Aleshire m_label_free(new_sl); /* OK if null */ 11334201a95eSRic Aleshire goto error; 11344201a95eSRic Aleshire 11354201a95eSRic Aleshire } 11364201a95eSRic Aleshire 1137e9dbad6fSeschrock case ZFS_PROP_MOUNTPOINT: 113889eef05eSrm160521 { 113989eef05eSrm160521 namecheck_err_t why; 114089eef05eSrm160521 1141e9dbad6fSeschrock if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || 1142e9dbad6fSeschrock strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) 1143e9dbad6fSeschrock break; 1144e9dbad6fSeschrock 114589eef05eSrm160521 if (mountpoint_namecheck(strval, &why)) { 114689eef05eSrm160521 switch (why) { 114789eef05eSrm160521 case NAME_ERR_LEADING_SLASH: 114889eef05eSrm160521 zfs_error_aux(hdl, 114989eef05eSrm160521 dgettext(TEXT_DOMAIN, 1150e9dbad6fSeschrock "'%s' must be an absolute path, " 1151e9dbad6fSeschrock "'none', or 'legacy'"), propname); 115289eef05eSrm160521 break; 115389eef05eSrm160521 case NAME_ERR_TOOLONG: 115489eef05eSrm160521 zfs_error_aux(hdl, 115589eef05eSrm160521 dgettext(TEXT_DOMAIN, 115689eef05eSrm160521 "component of '%s' is too long"), 115789eef05eSrm160521 propname); 115889eef05eSrm160521 break; 115989eef05eSrm160521 } 1160e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1161e9dbad6fSeschrock goto error; 1162e9dbad6fSeschrock } 116389eef05eSrm160521 } 116489eef05eSrm160521 1165f3861e1aSahl /*FALLTHRU*/ 1166e9dbad6fSeschrock 1167da6c28aaSamw case ZFS_PROP_SHARESMB: 1168f3861e1aSahl case ZFS_PROP_SHARENFS: 1169e9dbad6fSeschrock /* 1170da6c28aaSamw * For the mountpoint and sharenfs or sharesmb 1171da6c28aaSamw * properties, check if it can be set in a 1172da6c28aaSamw * global/non-global zone based on 1173f3861e1aSahl * the zoned property value: 1174fa9e4066Sahrens * 1175fa9e4066Sahrens * global zone non-global zone 1176f3861e1aSahl * -------------------------------------------------- 1177fa9e4066Sahrens * zoned=on mountpoint (no) mountpoint (yes) 1178fa9e4066Sahrens * sharenfs (no) sharenfs (no) 1179da6c28aaSamw * sharesmb (no) sharesmb (no) 1180fa9e4066Sahrens * 1181fa9e4066Sahrens * zoned=off mountpoint (yes) N/A 1182fa9e4066Sahrens * sharenfs (yes) 1183da6c28aaSamw * sharesmb (yes) 1184fa9e4066Sahrens */ 1185e9dbad6fSeschrock if (zoned) { 1186fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID) { 118799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1188e9dbad6fSeschrock "'%s' cannot be set on " 1189e9dbad6fSeschrock "dataset in a non-global zone"), 1190e9dbad6fSeschrock propname); 1191e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1192e9dbad6fSeschrock errbuf); 1193e9dbad6fSeschrock goto error; 1194da6c28aaSamw } else if (prop == ZFS_PROP_SHARENFS || 1195da6c28aaSamw prop == ZFS_PROP_SHARESMB) { 119699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1197e9dbad6fSeschrock "'%s' cannot be set in " 1198e9dbad6fSeschrock "a non-global zone"), propname); 1199e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, 1200e9dbad6fSeschrock errbuf); 1201e9dbad6fSeschrock goto error; 1202fa9e4066Sahrens } 1203fa9e4066Sahrens } else if (getzoneid() != GLOBAL_ZONEID) { 1204fa9e4066Sahrens /* 1205fa9e4066Sahrens * If zoned property is 'off', this must be in 120614843421SMatthew Ahrens * a global zone. If not, something is wrong. 1207fa9e4066Sahrens */ 120899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1209e9dbad6fSeschrock "'%s' cannot be set while dataset " 1210e9dbad6fSeschrock "'zoned' property is set"), propname); 1211e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 1212e9dbad6fSeschrock goto error; 1213fa9e4066Sahrens } 1214f3861e1aSahl 121567331909Sdougm /* 121667331909Sdougm * At this point, it is legitimate to set the 121767331909Sdougm * property. Now we want to make sure that the 121867331909Sdougm * property value is valid if it is sharenfs. 121967331909Sdougm */ 1220da6c28aaSamw if ((prop == ZFS_PROP_SHARENFS || 1221da6c28aaSamw prop == ZFS_PROP_SHARESMB) && 122267331909Sdougm strcmp(strval, "on") != 0 && 122367331909Sdougm strcmp(strval, "off") != 0) { 1224da6c28aaSamw zfs_share_proto_t proto; 1225da6c28aaSamw 1226da6c28aaSamw if (prop == ZFS_PROP_SHARESMB) 1227da6c28aaSamw proto = PROTO_SMB; 1228da6c28aaSamw else 1229da6c28aaSamw proto = PROTO_NFS; 123067331909Sdougm 123167331909Sdougm /* 1232da6c28aaSamw * Must be an valid sharing protocol 1233da6c28aaSamw * option string so init the libshare 1234da6c28aaSamw * in order to enable the parser and 1235da6c28aaSamw * then parse the options. We use the 1236da6c28aaSamw * control API since we don't care about 1237da6c28aaSamw * the current configuration and don't 123867331909Sdougm * want the overhead of loading it 123967331909Sdougm * until we actually do something. 124067331909Sdougm */ 124167331909Sdougm 124267331909Sdougm if (zfs_init_libshare(hdl, 124367331909Sdougm SA_INIT_CONTROL_API) != SA_OK) { 1244fac3008cSeschrock /* 1245fac3008cSeschrock * An error occurred so we can't do 1246fac3008cSeschrock * anything 1247fac3008cSeschrock */ 124867331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 124967331909Sdougm "'%s' cannot be set: problem " 125067331909Sdougm "in share initialization"), 125167331909Sdougm propname); 1252fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1253fac3008cSeschrock errbuf); 125467331909Sdougm goto error; 125567331909Sdougm } 125667331909Sdougm 1257da6c28aaSamw if (zfs_parse_options(strval, proto) != SA_OK) { 125867331909Sdougm /* 125967331909Sdougm * There was an error in parsing so 126067331909Sdougm * deal with it by issuing an error 126167331909Sdougm * message and leaving after 126267331909Sdougm * uninitializing the the libshare 126367331909Sdougm * interface. 126467331909Sdougm */ 126567331909Sdougm zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 126667331909Sdougm "'%s' cannot be set to invalid " 126767331909Sdougm "options"), propname); 1268fac3008cSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1269fac3008cSeschrock errbuf); 127067331909Sdougm zfs_uninit_libshare(hdl); 127167331909Sdougm goto error; 127267331909Sdougm } 127367331909Sdougm zfs_uninit_libshare(hdl); 127467331909Sdougm } 127567331909Sdougm 1276f3861e1aSahl break; 1277da6c28aaSamw case ZFS_PROP_UTF8ONLY: 1278da6c28aaSamw chosen_utf = (int)intval; 1279da6c28aaSamw break; 1280da6c28aaSamw case ZFS_PROP_NORMALIZE: 1281da6c28aaSamw chosen_normal = (int)intval; 1282da6c28aaSamw break; 1283fa9e4066Sahrens } 1284fa9e4066Sahrens 1285e9dbad6fSeschrock /* 1286e9dbad6fSeschrock * For changes to existing volumes, we have some additional 1287e9dbad6fSeschrock * checks to enforce. 1288e9dbad6fSeschrock */ 1289e9dbad6fSeschrock if (type == ZFS_TYPE_VOLUME && zhp != NULL) { 1290e9dbad6fSeschrock uint64_t volsize = zfs_prop_get_int(zhp, 1291e9dbad6fSeschrock ZFS_PROP_VOLSIZE); 1292e9dbad6fSeschrock uint64_t blocksize = zfs_prop_get_int(zhp, 1293e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 1294e9dbad6fSeschrock char buf[64]; 1295e9dbad6fSeschrock 1296e9dbad6fSeschrock switch (prop) { 1297e9dbad6fSeschrock case ZFS_PROP_RESERVATION: 1298a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 1299e9dbad6fSeschrock if (intval > volsize) { 1300e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1301e9dbad6fSeschrock "'%s' is greater than current " 1302e9dbad6fSeschrock "volume size"), propname); 1303e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1304e9dbad6fSeschrock errbuf); 1305e9dbad6fSeschrock goto error; 1306e9dbad6fSeschrock } 1307e9dbad6fSeschrock break; 1308e9dbad6fSeschrock 1309e9dbad6fSeschrock case ZFS_PROP_VOLSIZE: 1310e9dbad6fSeschrock if (intval % blocksize != 0) { 1311e9dbad6fSeschrock zfs_nicenum(blocksize, buf, 1312e9dbad6fSeschrock sizeof (buf)); 1313e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1314e9dbad6fSeschrock "'%s' must be a multiple of " 1315e9dbad6fSeschrock "volume block size (%s)"), 1316e9dbad6fSeschrock propname, buf); 1317e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1318e9dbad6fSeschrock errbuf); 1319e9dbad6fSeschrock goto error; 1320e9dbad6fSeschrock } 1321e9dbad6fSeschrock 1322e9dbad6fSeschrock if (intval == 0) { 1323e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1324e9dbad6fSeschrock "'%s' cannot be zero"), 1325e9dbad6fSeschrock propname); 1326e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_BADPROP, 1327e9dbad6fSeschrock errbuf); 1328e9dbad6fSeschrock goto error; 1329e9dbad6fSeschrock } 1330f3861e1aSahl break; 1331e9dbad6fSeschrock } 1332e9dbad6fSeschrock } 1333e9dbad6fSeschrock } 1334e9dbad6fSeschrock 1335e9dbad6fSeschrock /* 1336da6c28aaSamw * If normalization was chosen, but no UTF8 choice was made, 1337da6c28aaSamw * enforce rejection of non-UTF8 names. 1338da6c28aaSamw * 1339da6c28aaSamw * If normalization was chosen, but rejecting non-UTF8 names 1340da6c28aaSamw * was explicitly not chosen, it is an error. 1341da6c28aaSamw */ 1342de8267e0Stimh if (chosen_normal > 0 && chosen_utf < 0) { 1343da6c28aaSamw if (nvlist_add_uint64(ret, 1344da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { 1345da6c28aaSamw (void) no_memory(hdl); 1346da6c28aaSamw goto error; 1347da6c28aaSamw } 1348de8267e0Stimh } else if (chosen_normal > 0 && chosen_utf == 0) { 1349da6c28aaSamw zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1350da6c28aaSamw "'%s' must be set 'on' if normalization chosen"), 1351da6c28aaSamw zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); 1352da6c28aaSamw (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1353da6c28aaSamw goto error; 1354da6c28aaSamw } 1355e9dbad6fSeschrock return (ret); 1356e9dbad6fSeschrock 1357e9dbad6fSeschrock error: 1358e9dbad6fSeschrock nvlist_free(ret); 1359e9dbad6fSeschrock return (NULL); 1360e9dbad6fSeschrock } 1361e9dbad6fSeschrock 136236db6475SEric Taylor int 136336db6475SEric Taylor zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) 136436db6475SEric Taylor { 136536db6475SEric Taylor uint64_t old_volsize; 136636db6475SEric Taylor uint64_t new_volsize; 136736db6475SEric Taylor uint64_t old_reservation; 136836db6475SEric Taylor uint64_t new_reservation; 136936db6475SEric Taylor zfs_prop_t resv_prop; 1370c61ea566SGeorge Wilson nvlist_t *props; 137136db6475SEric Taylor 137236db6475SEric Taylor /* 137336db6475SEric Taylor * If this is an existing volume, and someone is setting the volsize, 137436db6475SEric Taylor * make sure that it matches the reservation, or add it if necessary. 137536db6475SEric Taylor */ 137636db6475SEric Taylor old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 137736db6475SEric Taylor if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 137836db6475SEric Taylor return (-1); 137936db6475SEric Taylor old_reservation = zfs_prop_get_int(zhp, resv_prop); 1380c61ea566SGeorge Wilson 1381c61ea566SGeorge Wilson props = fnvlist_alloc(); 1382c61ea566SGeorge Wilson fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 1383c61ea566SGeorge Wilson zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); 1384c61ea566SGeorge Wilson 1385c61ea566SGeorge Wilson if ((zvol_volsize_to_reservation(old_volsize, props) != 1386c61ea566SGeorge Wilson old_reservation) || nvlist_exists(nvl, 1387c61ea566SGeorge Wilson zfs_prop_to_name(resv_prop))) { 1388c61ea566SGeorge Wilson fnvlist_free(props); 138936db6475SEric Taylor return (0); 139036db6475SEric Taylor } 139136db6475SEric Taylor if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), 1392c61ea566SGeorge Wilson &new_volsize) != 0) { 1393c61ea566SGeorge Wilson fnvlist_free(props); 139436db6475SEric Taylor return (-1); 1395c61ea566SGeorge Wilson } 1396c61ea566SGeorge Wilson new_reservation = zvol_volsize_to_reservation(new_volsize, props); 1397c61ea566SGeorge Wilson fnvlist_free(props); 1398c61ea566SGeorge Wilson 139936db6475SEric Taylor if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), 140036db6475SEric Taylor new_reservation) != 0) { 140136db6475SEric Taylor (void) no_memory(zhp->zfs_hdl); 140236db6475SEric Taylor return (-1); 140336db6475SEric Taylor } 140436db6475SEric Taylor return (1); 140536db6475SEric Taylor } 140636db6475SEric Taylor 140792241e0bSTom Erickson void 140892241e0bSTom Erickson zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, 140992241e0bSTom Erickson char *errbuf) 141092241e0bSTom Erickson { 141192241e0bSTom Erickson switch (err) { 141292241e0bSTom Erickson 141392241e0bSTom Erickson case ENOSPC: 141492241e0bSTom Erickson /* 141592241e0bSTom Erickson * For quotas and reservations, ENOSPC indicates 141692241e0bSTom Erickson * something different; setting a quota or reservation 141792241e0bSTom Erickson * doesn't use any disk space. 141892241e0bSTom Erickson */ 141992241e0bSTom Erickson switch (prop) { 142092241e0bSTom Erickson case ZFS_PROP_QUOTA: 142192241e0bSTom Erickson case ZFS_PROP_REFQUOTA: 142292241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 142392241e0bSTom Erickson "size is less than current used or " 142492241e0bSTom Erickson "reserved space")); 142592241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 142692241e0bSTom Erickson break; 142792241e0bSTom Erickson 142892241e0bSTom Erickson case ZFS_PROP_RESERVATION: 142992241e0bSTom Erickson case ZFS_PROP_REFRESERVATION: 143092241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 143192241e0bSTom Erickson "size is greater than available space")); 143292241e0bSTom Erickson (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); 143392241e0bSTom Erickson break; 143492241e0bSTom Erickson 143592241e0bSTom Erickson default: 143692241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 143792241e0bSTom Erickson break; 143892241e0bSTom Erickson } 143992241e0bSTom Erickson break; 144092241e0bSTom Erickson 144192241e0bSTom Erickson case EBUSY: 144292241e0bSTom Erickson (void) zfs_standard_error(hdl, EBUSY, errbuf); 144392241e0bSTom Erickson break; 144492241e0bSTom Erickson 144592241e0bSTom Erickson case EROFS: 144692241e0bSTom Erickson (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); 144792241e0bSTom Erickson break; 144892241e0bSTom Erickson 14496fdcb3d1SWill Andrews case E2BIG: 14506fdcb3d1SWill Andrews zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 14516fdcb3d1SWill Andrews "property value too long")); 14526fdcb3d1SWill Andrews (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 14536fdcb3d1SWill Andrews break; 14546fdcb3d1SWill Andrews 145592241e0bSTom Erickson case ENOTSUP: 145692241e0bSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 145792241e0bSTom Erickson "pool and or dataset must be upgraded to set this " 145892241e0bSTom Erickson "property or value")); 145992241e0bSTom Erickson (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 146092241e0bSTom Erickson break; 146192241e0bSTom Erickson 146292241e0bSTom Erickson case ERANGE: 1463b5152584SMatthew Ahrens if (prop == ZFS_PROP_COMPRESSION || 1464b5152584SMatthew Ahrens prop == ZFS_PROP_RECORDSIZE) { 146592241e0bSTom Erickson (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 146692241e0bSTom Erickson "property setting is not allowed on " 146792241e0bSTom Erickson "bootable datasets")); 146892241e0bSTom Erickson (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 146945818ee1SMatthew Ahrens } else if (prop == ZFS_PROP_CHECKSUM || 147045818ee1SMatthew Ahrens prop == ZFS_PROP_DEDUP) { 147145818ee1SMatthew Ahrens (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 147245818ee1SMatthew Ahrens "property setting is not allowed on " 147345818ee1SMatthew Ahrens "root pools")); 147445818ee1SMatthew Ahrens (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); 147592241e0bSTom Erickson } else { 147692241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 147792241e0bSTom Erickson } 147892241e0bSTom Erickson break; 147992241e0bSTom Erickson 1480ab003da8SJim Dunham case EINVAL: 1481ab003da8SJim Dunham if (prop == ZPROP_INVAL) { 1482ab003da8SJim Dunham (void) zfs_error(hdl, EZFS_BADPROP, errbuf); 1483ab003da8SJim Dunham } else { 1484ab003da8SJim Dunham (void) zfs_standard_error(hdl, err, errbuf); 1485ab003da8SJim Dunham } 1486ab003da8SJim Dunham break; 1487ab003da8SJim Dunham 148892241e0bSTom Erickson case EOVERFLOW: 148992241e0bSTom Erickson /* 149092241e0bSTom Erickson * This platform can't address a volume this big. 149192241e0bSTom Erickson */ 149292241e0bSTom Erickson #ifdef _ILP32 149392241e0bSTom Erickson if (prop == ZFS_PROP_VOLSIZE) { 149492241e0bSTom Erickson (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); 149592241e0bSTom Erickson break; 149692241e0bSTom Erickson } 149792241e0bSTom Erickson #endif 149892241e0bSTom Erickson /* FALLTHROUGH */ 149992241e0bSTom Erickson default: 150092241e0bSTom Erickson (void) zfs_standard_error(hdl, err, errbuf); 150192241e0bSTom Erickson } 150292241e0bSTom Erickson } 150392241e0bSTom Erickson 1504e9dbad6fSeschrock /* 1505e9dbad6fSeschrock * Given a property name and value, set the property for the given dataset. 1506e9dbad6fSeschrock */ 1507e9dbad6fSeschrock int 1508e9dbad6fSeschrock zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) 1509e9dbad6fSeschrock { 1510e9dbad6fSeschrock int ret = -1; 1511e9dbad6fSeschrock char errbuf[1024]; 1512e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 151330925561SChris Williamson nvlist_t *nvl = NULL; 1514e9dbad6fSeschrock 1515e9dbad6fSeschrock (void) snprintf(errbuf, sizeof (errbuf), 1516e9dbad6fSeschrock dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 1517e9dbad6fSeschrock zhp->zfs_name); 1518e9dbad6fSeschrock 1519e9dbad6fSeschrock if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || 1520e9dbad6fSeschrock nvlist_add_string(nvl, propname, propval) != 0) { 1521e9dbad6fSeschrock (void) no_memory(hdl); 1522e9dbad6fSeschrock goto error; 1523e9dbad6fSeschrock } 1524e9dbad6fSeschrock 152530925561SChris Williamson ret = zfs_prop_set_list(zhp, nvl); 152630925561SChris Williamson 152730925561SChris Williamson error: 152830925561SChris Williamson nvlist_free(nvl); 152930925561SChris Williamson return (ret); 153030925561SChris Williamson } 153130925561SChris Williamson 153230925561SChris Williamson 153330925561SChris Williamson 153430925561SChris Williamson /* 153530925561SChris Williamson * Given an nvlist of property names and values, set the properties for the 153630925561SChris Williamson * given dataset. 153730925561SChris Williamson */ 153830925561SChris Williamson int 153930925561SChris Williamson zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props) 154030925561SChris Williamson { 154130925561SChris Williamson zfs_cmd_t zc = { 0 }; 154230925561SChris Williamson int ret = -1; 154330925561SChris Williamson prop_changelist_t **cls = NULL; 154430925561SChris Williamson int cl_idx; 154530925561SChris Williamson char errbuf[1024]; 154630925561SChris Williamson libzfs_handle_t *hdl = zhp->zfs_hdl; 154730925561SChris Williamson nvlist_t *nvl; 154830925561SChris Williamson int nvl_len; 154930925561SChris Williamson int added_resv; 155030925561SChris Williamson 155130925561SChris Williamson (void) snprintf(errbuf, sizeof (errbuf), 155230925561SChris Williamson dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), 155330925561SChris Williamson zhp->zfs_name); 155430925561SChris Williamson 155530925561SChris Williamson if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, 1556e9316f76SJoe Stein zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, 1557e9316f76SJoe Stein errbuf)) == NULL) 1558e9dbad6fSeschrock goto error; 1559990b4856Slling 156030925561SChris Williamson /* 156130925561SChris Williamson * We have to check for any extra properties which need to be added 156230925561SChris Williamson * before computing the length of the nvlist. 156330925561SChris Williamson */ 156430925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 156530925561SChris Williamson elem != NULL; 156630925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 156730925561SChris Williamson if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && 156830925561SChris Williamson (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { 156930925561SChris Williamson goto error; 157030925561SChris Williamson } 157130925561SChris Williamson } 157230925561SChris Williamson /* 157330925561SChris Williamson * Check how many properties we're setting and allocate an array to 157430925561SChris Williamson * store changelist pointers for postfix(). 157530925561SChris Williamson */ 157630925561SChris Williamson nvl_len = 0; 157730925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 157830925561SChris Williamson elem != NULL; 157930925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) 158030925561SChris Williamson nvl_len++; 158130925561SChris Williamson if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) 158230925561SChris Williamson goto error; 1583e9dbad6fSeschrock 158430925561SChris Williamson cl_idx = 0; 158530925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 158630925561SChris Williamson elem != NULL; 158730925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 1588e9dbad6fSeschrock 158930925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 159030925561SChris Williamson 159130925561SChris Williamson assert(cl_idx < nvl_len); 159230925561SChris Williamson /* 159330925561SChris Williamson * We don't want to unmount & remount the dataset when changing 159430925561SChris Williamson * its canmount property to 'on' or 'noauto'. We only use 159530925561SChris Williamson * the changelist logic to unmount when setting canmount=off. 159630925561SChris Williamson */ 159730925561SChris Williamson if (!(prop == ZFS_PROP_CANMOUNT && 159830925561SChris Williamson fnvpair_value_uint64(elem) != ZFS_CANMOUNT_OFF)) { 159930925561SChris Williamson cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); 160030925561SChris Williamson if (cls[cl_idx] == NULL) 160136db6475SEric Taylor goto error; 160236db6475SEric Taylor } 160336db6475SEric Taylor 160430925561SChris Williamson if (prop == ZFS_PROP_MOUNTPOINT && 160530925561SChris Williamson changelist_haszonedchild(cls[cl_idx])) { 160699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1607fa9e4066Sahrens "child dataset with inherited mountpoint is used " 160899653d4eSeschrock "in a non-global zone")); 160999653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1610fa9e4066Sahrens goto error; 1611fa9e4066Sahrens } 1612fa9e4066Sahrens 161330925561SChris Williamson if (cls[cl_idx] != NULL && 161430925561SChris Williamson (ret = changelist_prefix(cls[cl_idx])) != 0) 1615fa9e4066Sahrens goto error; 1616fa9e4066Sahrens 161730925561SChris Williamson cl_idx++; 161830925561SChris Williamson } 161930925561SChris Williamson assert(cl_idx == nvl_len); 162030925561SChris Williamson 1621fa9e4066Sahrens /* 162230925561SChris Williamson * Execute the corresponding ioctl() to set this list of properties. 1623fa9e4066Sahrens */ 1624fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1625fa9e4066Sahrens 162630925561SChris Williamson if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 || 162730925561SChris Williamson (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0) 1628e9dbad6fSeschrock goto error; 1629e9dbad6fSeschrock 1630ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 1631743a77edSAlan Wright 1632fa9e4066Sahrens if (ret != 0) { 163330925561SChris Williamson /* Get the list of unset properties back and report them. */ 163430925561SChris Williamson nvlist_t *errorprops = NULL; 163530925561SChris Williamson if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) 163630925561SChris Williamson goto error; 163730925561SChris Williamson for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL); 163830925561SChris Williamson elem != NULL; 163930925561SChris Williamson elem = nvlist_next_nvpair(nvl, elem)) { 164030925561SChris Williamson zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem)); 164192241e0bSTom Erickson zfs_setprop_error(hdl, prop, errno, errbuf); 164230925561SChris Williamson } 164330925561SChris Williamson nvlist_free(errorprops); 164430925561SChris Williamson 164536db6475SEric Taylor if (added_resv && errno == ENOSPC) { 164636db6475SEric Taylor /* clean up the volsize property we tried to set */ 164736db6475SEric Taylor uint64_t old_volsize = zfs_prop_get_int(zhp, 164836db6475SEric Taylor ZFS_PROP_VOLSIZE); 164936db6475SEric Taylor nvlist_free(nvl); 165030925561SChris Williamson nvl = NULL; 165136db6475SEric Taylor zcmd_free_nvlists(&zc); 165230925561SChris Williamson 165336db6475SEric Taylor if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) 165436db6475SEric Taylor goto error; 165536db6475SEric Taylor if (nvlist_add_uint64(nvl, 165636db6475SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLSIZE), 165736db6475SEric Taylor old_volsize) != 0) 165836db6475SEric Taylor goto error; 165936db6475SEric Taylor if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) 166036db6475SEric Taylor goto error; 166136db6475SEric Taylor (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); 166236db6475SEric Taylor } 1663fa9e4066Sahrens } else { 166430925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 166530925561SChris Williamson if (cls[cl_idx] != NULL) { 166630925561SChris Williamson int clp_err = changelist_postfix(cls[cl_idx]); 166730925561SChris Williamson if (clp_err != 0) 166830925561SChris Williamson ret = clp_err; 166930925561SChris Williamson } 167030925561SChris Williamson } 1671a227b7f4Shs24103 1672fa9e4066Sahrens /* 1673fa9e4066Sahrens * Refresh the statistics so the new property value 1674fa9e4066Sahrens * is reflected. 1675fa9e4066Sahrens */ 1676a227b7f4Shs24103 if (ret == 0) 1677fa9e4066Sahrens (void) get_stats(zhp); 1678fa9e4066Sahrens } 1679fa9e4066Sahrens 1680fa9e4066Sahrens error: 1681e9dbad6fSeschrock nvlist_free(nvl); 1682e9dbad6fSeschrock zcmd_free_nvlists(&zc); 168330925561SChris Williamson if (cls != NULL) { 168430925561SChris Williamson for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { 168530925561SChris Williamson if (cls[cl_idx] != NULL) 168630925561SChris Williamson changelist_free(cls[cl_idx]); 168730925561SChris Williamson } 168830925561SChris Williamson free(cls); 168930925561SChris Williamson } 1690fa9e4066Sahrens return (ret); 1691fa9e4066Sahrens } 1692fa9e4066Sahrens 1693fa9e4066Sahrens /* 169492241e0bSTom Erickson * Given a property, inherit the value from the parent dataset, or if received 169592241e0bSTom Erickson * is TRUE, revert to the received value, if any. 1696fa9e4066Sahrens */ 1697fa9e4066Sahrens int 169892241e0bSTom Erickson zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) 1699fa9e4066Sahrens { 1700fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 1701fa9e4066Sahrens int ret; 1702fa9e4066Sahrens prop_changelist_t *cl; 170399653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 170499653d4eSeschrock char errbuf[1024]; 1705e9dbad6fSeschrock zfs_prop_t prop; 170699653d4eSeschrock 170799653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 170899653d4eSeschrock "cannot inherit %s for '%s'"), propname, zhp->zfs_name); 1709fa9e4066Sahrens 171092241e0bSTom Erickson zc.zc_cookie = received; 1711990b4856Slling if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { 1712e9dbad6fSeschrock /* 1713e9dbad6fSeschrock * For user properties, the amount of work we have to do is very 1714e9dbad6fSeschrock * small, so just do it here. 1715e9dbad6fSeschrock */ 1716e9dbad6fSeschrock if (!zfs_prop_user(propname)) { 1717e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 1718e9dbad6fSeschrock "invalid property")); 1719e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 1720e9dbad6fSeschrock } 1721e9dbad6fSeschrock 1722e9dbad6fSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1723e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1724e9dbad6fSeschrock 1725e45ce728Sahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) 1726e9dbad6fSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1727e9dbad6fSeschrock 1728e9dbad6fSeschrock return (0); 1729e9dbad6fSeschrock } 1730e9dbad6fSeschrock 1731fa9e4066Sahrens /* 1732fa9e4066Sahrens * Verify that this property is inheritable. 1733fa9e4066Sahrens */ 173499653d4eSeschrock if (zfs_prop_readonly(prop)) 173599653d4eSeschrock return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); 1736fa9e4066Sahrens 173792241e0bSTom Erickson if (!zfs_prop_inheritable(prop) && !received) 173899653d4eSeschrock return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); 1739fa9e4066Sahrens 1740fa9e4066Sahrens /* 1741fa9e4066Sahrens * Check to see if the value applies to this type 1742fa9e4066Sahrens */ 174399653d4eSeschrock if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 174499653d4eSeschrock return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); 1745fa9e4066Sahrens 1746bf7c2d40Srm160521 /* 174736db6475SEric Taylor * Normalize the name, to get rid of shorthand abbreviations. 1748bf7c2d40Srm160521 */ 1749bf7c2d40Srm160521 propname = zfs_prop_to_name(prop); 1750fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 1751e9dbad6fSeschrock (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); 1752fa9e4066Sahrens 1753fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && 1754fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 175599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 175699653d4eSeschrock "dataset is used in a non-global zone")); 175799653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 1758fa9e4066Sahrens } 1759fa9e4066Sahrens 1760fa9e4066Sahrens /* 1761fa9e4066Sahrens * Determine datasets which will be affected by this change, if any. 1762fa9e4066Sahrens */ 17630069fd67STim Haley if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) 1764fa9e4066Sahrens return (-1); 1765fa9e4066Sahrens 1766fa9e4066Sahrens if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { 176799653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 176899653d4eSeschrock "child dataset with inherited mountpoint is used " 176999653d4eSeschrock "in a non-global zone")); 177099653d4eSeschrock ret = zfs_error(hdl, EZFS_ZONED, errbuf); 1771fa9e4066Sahrens goto error; 1772fa9e4066Sahrens } 1773fa9e4066Sahrens 1774fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 1775fa9e4066Sahrens goto error; 1776fa9e4066Sahrens 1777e45ce728Sahrens if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { 177899653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 1779fa9e4066Sahrens } else { 1780fa9e4066Sahrens 1781efc555ebSnd150628 if ((ret = changelist_postfix(cl)) != 0) 1782fa9e4066Sahrens goto error; 1783fa9e4066Sahrens 1784fa9e4066Sahrens /* 1785fa9e4066Sahrens * Refresh the statistics so the new property is reflected. 1786fa9e4066Sahrens */ 1787fa9e4066Sahrens (void) get_stats(zhp); 1788fa9e4066Sahrens } 1789fa9e4066Sahrens 1790fa9e4066Sahrens error: 1791fa9e4066Sahrens changelist_free(cl); 1792fa9e4066Sahrens return (ret); 1793fa9e4066Sahrens } 1794fa9e4066Sahrens 1795fa9e4066Sahrens /* 17967f7322feSeschrock * True DSL properties are stored in an nvlist. The following two functions 17977f7322feSeschrock * extract them appropriately. 17987f7322feSeschrock */ 17997f7322feSeschrock static uint64_t 18007f7322feSeschrock getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18017f7322feSeschrock { 18027f7322feSeschrock nvlist_t *nv; 18037f7322feSeschrock uint64_t value; 18047f7322feSeschrock 1805a2eea2e1Sahrens *source = NULL; 18067f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18077f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 1808990b4856Slling verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); 1809990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18107f7322feSeschrock } else { 18112e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 18122e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 18137f7322feSeschrock value = zfs_prop_default_numeric(prop); 18147f7322feSeschrock *source = ""; 18157f7322feSeschrock } 18167f7322feSeschrock 18177f7322feSeschrock return (value); 18187f7322feSeschrock } 18197f7322feSeschrock 18209c3fd121SMatthew Ahrens static const char * 18217f7322feSeschrock getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) 18227f7322feSeschrock { 18237f7322feSeschrock nvlist_t *nv; 18249c3fd121SMatthew Ahrens const char *value; 18257f7322feSeschrock 1826a2eea2e1Sahrens *source = NULL; 18277f7322feSeschrock if (nvlist_lookup_nvlist(zhp->zfs_props, 18287f7322feSeschrock zfs_prop_to_name(prop), &nv) == 0) { 18299c3fd121SMatthew Ahrens value = fnvlist_lookup_string(nv, ZPROP_VALUE); 1830990b4856Slling (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); 18317f7322feSeschrock } else { 18322e5e9e19SSanjeev Bagewadi verify(!zhp->zfs_props_table || 18332e5e9e19SSanjeev Bagewadi zhp->zfs_props_table[prop] == B_TRUE); 18349c3fd121SMatthew Ahrens value = zfs_prop_default_string(prop); 18357f7322feSeschrock *source = ""; 18367f7322feSeschrock } 18377f7322feSeschrock 18387f7322feSeschrock return (value); 18397f7322feSeschrock } 18407f7322feSeschrock 184192241e0bSTom Erickson static boolean_t 184292241e0bSTom Erickson zfs_is_recvd_props_mode(zfs_handle_t *zhp) 184392241e0bSTom Erickson { 184492241e0bSTom Erickson return (zhp->zfs_props == zhp->zfs_recvd_props); 184592241e0bSTom Erickson } 184692241e0bSTom Erickson 184792241e0bSTom Erickson static void 184892241e0bSTom Erickson zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 184992241e0bSTom Erickson { 185092241e0bSTom Erickson *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; 185192241e0bSTom Erickson zhp->zfs_props = zhp->zfs_recvd_props; 185292241e0bSTom Erickson } 185392241e0bSTom Erickson 185492241e0bSTom Erickson static void 185592241e0bSTom Erickson zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) 185692241e0bSTom Erickson { 185792241e0bSTom Erickson zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; 185892241e0bSTom Erickson *cookie = 0; 185992241e0bSTom Erickson } 186092241e0bSTom Erickson 18617f7322feSeschrock /* 1862fa9e4066Sahrens * Internal function for getting a numeric property. Both zfs_prop_get() and 1863fa9e4066Sahrens * zfs_prop_get_int() are built using this interface. 1864fa9e4066Sahrens * 1865fa9e4066Sahrens * Certain properties can be overridden using 'mount -o'. In this case, scan 1866fa9e4066Sahrens * the contents of the /etc/mnttab entry, searching for the appropriate options. 1867fa9e4066Sahrens * If they differ from the on-disk values, report the current values and mark 1868fa9e4066Sahrens * the source "temporary". 1869fa9e4066Sahrens */ 187099653d4eSeschrock static int 1871990b4856Slling get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, 187299653d4eSeschrock char **source, uint64_t *val) 1873fa9e4066Sahrens { 1874bd00f61bSrm160521 zfs_cmd_t zc = { 0 }; 187596510749Stimh nvlist_t *zplprops = NULL; 1876fa9e4066Sahrens struct mnttab mnt; 18773ccfa83cSahrens char *mntopt_on = NULL; 18783ccfa83cSahrens char *mntopt_off = NULL; 187992241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 1880fa9e4066Sahrens 1881fa9e4066Sahrens *source = NULL; 1882fa9e4066Sahrens 18833ccfa83cSahrens switch (prop) { 18843ccfa83cSahrens case ZFS_PROP_ATIME: 18853ccfa83cSahrens mntopt_on = MNTOPT_ATIME; 18863ccfa83cSahrens mntopt_off = MNTOPT_NOATIME; 18873ccfa83cSahrens break; 18883ccfa83cSahrens 18893ccfa83cSahrens case ZFS_PROP_DEVICES: 18903ccfa83cSahrens mntopt_on = MNTOPT_DEVICES; 18913ccfa83cSahrens mntopt_off = MNTOPT_NODEVICES; 18923ccfa83cSahrens break; 18933ccfa83cSahrens 18943ccfa83cSahrens case ZFS_PROP_EXEC: 18953ccfa83cSahrens mntopt_on = MNTOPT_EXEC; 18963ccfa83cSahrens mntopt_off = MNTOPT_NOEXEC; 18973ccfa83cSahrens break; 18983ccfa83cSahrens 18993ccfa83cSahrens case ZFS_PROP_READONLY: 19003ccfa83cSahrens mntopt_on = MNTOPT_RO; 19013ccfa83cSahrens mntopt_off = MNTOPT_RW; 19023ccfa83cSahrens break; 19033ccfa83cSahrens 19043ccfa83cSahrens case ZFS_PROP_SETUID: 19053ccfa83cSahrens mntopt_on = MNTOPT_SETUID; 19063ccfa83cSahrens mntopt_off = MNTOPT_NOSETUID; 19073ccfa83cSahrens break; 19083ccfa83cSahrens 19093ccfa83cSahrens case ZFS_PROP_XATTR: 19103ccfa83cSahrens mntopt_on = MNTOPT_XATTR; 19113ccfa83cSahrens mntopt_off = MNTOPT_NOXATTR; 19123ccfa83cSahrens break; 1913da6c28aaSamw 1914da6c28aaSamw case ZFS_PROP_NBMAND: 1915da6c28aaSamw mntopt_on = MNTOPT_NBMAND; 1916da6c28aaSamw mntopt_off = MNTOPT_NONBMAND; 1917da6c28aaSamw break; 19183ccfa83cSahrens } 19193ccfa83cSahrens 19203bb79becSeschrock /* 19213bb79becSeschrock * Because looking up the mount options is potentially expensive 19223bb79becSeschrock * (iterating over all of /etc/mnttab), we defer its calculation until 19233bb79becSeschrock * we're looking up a property which requires its presence. 19243bb79becSeschrock */ 19253bb79becSeschrock if (!zhp->zfs_mntcheck && 19263ccfa83cSahrens (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { 1927ebedde84SEric Taylor libzfs_handle_t *hdl = zhp->zfs_hdl; 1928ebedde84SEric Taylor struct mnttab entry; 19293bb79becSeschrock 1930ebedde84SEric Taylor if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { 1931ebedde84SEric Taylor zhp->zfs_mntopts = zfs_strdup(hdl, 19323ccfa83cSahrens entry.mnt_mntopts); 19333ccfa83cSahrens if (zhp->zfs_mntopts == NULL) 19343bb79becSeschrock return (-1); 19353ccfa83cSahrens } 19363bb79becSeschrock 19373bb79becSeschrock zhp->zfs_mntcheck = B_TRUE; 19383bb79becSeschrock } 19393bb79becSeschrock 1940fa9e4066Sahrens if (zhp->zfs_mntopts == NULL) 1941fa9e4066Sahrens mnt.mnt_mntopts = ""; 1942fa9e4066Sahrens else 1943fa9e4066Sahrens mnt.mnt_mntopts = zhp->zfs_mntopts; 1944fa9e4066Sahrens 1945fa9e4066Sahrens switch (prop) { 1946fa9e4066Sahrens case ZFS_PROP_ATIME: 1947fa9e4066Sahrens case ZFS_PROP_DEVICES: 1948fa9e4066Sahrens case ZFS_PROP_EXEC: 19493ccfa83cSahrens case ZFS_PROP_READONLY: 19503ccfa83cSahrens case ZFS_PROP_SETUID: 19513ccfa83cSahrens case ZFS_PROP_XATTR: 1952da6c28aaSamw case ZFS_PROP_NBMAND: 195399653d4eSeschrock *val = getprop_uint64(zhp, prop, source); 1954fa9e4066Sahrens 195592241e0bSTom Erickson if (received) 195692241e0bSTom Erickson break; 195792241e0bSTom Erickson 19583ccfa83cSahrens if (hasmntopt(&mnt, mntopt_on) && !*val) { 195999653d4eSeschrock *val = B_TRUE; 1960fa9e4066Sahrens if (src) 1961990b4856Slling *src = ZPROP_SRC_TEMPORARY; 19623ccfa83cSahrens } else if (hasmntopt(&mnt, mntopt_off) && *val) { 196399653d4eSeschrock *val = B_FALSE; 1964fa9e4066Sahrens if (src) 1965990b4856Slling *src = ZPROP_SRC_TEMPORARY; 1966fa9e4066Sahrens } 196799653d4eSeschrock break; 1968fa9e4066Sahrens 19693ccfa83cSahrens case ZFS_PROP_CANMOUNT: 1970d41c4376SMark J Musante case ZFS_PROP_VOLSIZE: 1971fa9e4066Sahrens case ZFS_PROP_QUOTA: 1972a9799022Sck153898 case ZFS_PROP_REFQUOTA: 1973fa9e4066Sahrens case ZFS_PROP_RESERVATION: 1974a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 1975a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 1976a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 1977a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 1978a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 1979a2eea2e1Sahrens *val = getprop_uint64(zhp, prop, source); 198092241e0bSTom Erickson 198192241e0bSTom Erickson if (*source == NULL) { 198292241e0bSTom Erickson /* not default, must be local */ 1983fa9e4066Sahrens *source = zhp->zfs_name; 198492241e0bSTom Erickson } 198599653d4eSeschrock break; 1986fa9e4066Sahrens 1987fa9e4066Sahrens case ZFS_PROP_MOUNTED: 198899653d4eSeschrock *val = (zhp->zfs_mntopts != NULL); 198999653d4eSeschrock break; 1990fa9e4066Sahrens 199139c23413Seschrock case ZFS_PROP_NUMCLONES: 199239c23413Seschrock *val = zhp->zfs_dmustats.dds_num_clones; 199339c23413Seschrock break; 199439c23413Seschrock 1995bd00f61bSrm160521 case ZFS_PROP_VERSION: 1996de8267e0Stimh case ZFS_PROP_NORMALIZE: 1997de8267e0Stimh case ZFS_PROP_UTF8ONLY: 1998de8267e0Stimh case ZFS_PROP_CASE: 1999de8267e0Stimh if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) || 2000de8267e0Stimh zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) 20013d7934e1Srm160521 return (-1); 2002bd00f61bSrm160521 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 2003de8267e0Stimh if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { 2004de8267e0Stimh zcmd_free_nvlists(&zc); 2005f4b94bdeSMatthew Ahrens return (-1); 2006bd00f61bSrm160521 } 2007de8267e0Stimh if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || 2008de8267e0Stimh nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), 2009de8267e0Stimh val) != 0) { 2010de8267e0Stimh zcmd_free_nvlists(&zc); 2011f4b94bdeSMatthew Ahrens return (-1); 2012de8267e0Stimh } 201396510749Stimh if (zplprops) 201496510749Stimh nvlist_free(zplprops); 2015de8267e0Stimh zcmd_free_nvlists(&zc); 2016bd00f61bSrm160521 break; 2017bd00f61bSrm160521 2018ca48f36fSKeith M Wesolowski case ZFS_PROP_INCONSISTENT: 2019ca48f36fSKeith M Wesolowski *val = zhp->zfs_dmustats.dds_inconsistent; 2020ca48f36fSKeith M Wesolowski break; 2021ca48f36fSKeith M Wesolowski 2022fa9e4066Sahrens default: 2023e7437265Sahrens switch (zfs_prop_get_type(prop)) { 202491ebeef5Sahrens case PROP_TYPE_NUMBER: 202591ebeef5Sahrens case PROP_TYPE_INDEX: 2026e7437265Sahrens *val = getprop_uint64(zhp, prop, source); 202774e7dc98SMatthew Ahrens /* 202814843421SMatthew Ahrens * If we tried to use a default value for a 202974e7dc98SMatthew Ahrens * readonly property, it means that it was not 203031f572c2STom Erickson * present. 203174e7dc98SMatthew Ahrens */ 203274e7dc98SMatthew Ahrens if (zfs_prop_readonly(prop) && 203331f572c2STom Erickson *source != NULL && (*source)[0] == '\0') { 203431f572c2STom Erickson *source = NULL; 203574e7dc98SMatthew Ahrens } 2036e7437265Sahrens break; 2037e7437265Sahrens 203891ebeef5Sahrens case PROP_TYPE_STRING: 2039e7437265Sahrens default: 204099653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 204199653d4eSeschrock "cannot get non-numeric property")); 204299653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, 204399653d4eSeschrock dgettext(TEXT_DOMAIN, "internal error"))); 2044fa9e4066Sahrens } 2045e7437265Sahrens } 2046fa9e4066Sahrens 2047fa9e4066Sahrens return (0); 2048fa9e4066Sahrens } 2049fa9e4066Sahrens 2050fa9e4066Sahrens /* 2051fa9e4066Sahrens * Calculate the source type, given the raw source string. 2052fa9e4066Sahrens */ 2053fa9e4066Sahrens static void 2054990b4856Slling get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, 2055fa9e4066Sahrens char *statbuf, size_t statlen) 2056fa9e4066Sahrens { 2057990b4856Slling if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY) 2058fa9e4066Sahrens return; 2059fa9e4066Sahrens 2060fa9e4066Sahrens if (source == NULL) { 2061990b4856Slling *srctype = ZPROP_SRC_NONE; 2062fa9e4066Sahrens } else if (source[0] == '\0') { 2063990b4856Slling *srctype = ZPROP_SRC_DEFAULT; 206492241e0bSTom Erickson } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { 206592241e0bSTom Erickson *srctype = ZPROP_SRC_RECEIVED; 2066fa9e4066Sahrens } else { 2067fa9e4066Sahrens if (strcmp(source, zhp->zfs_name) == 0) { 2068990b4856Slling *srctype = ZPROP_SRC_LOCAL; 2069fa9e4066Sahrens } else { 2070fa9e4066Sahrens (void) strlcpy(statbuf, source, statlen); 2071990b4856Slling *srctype = ZPROP_SRC_INHERITED; 2072fa9e4066Sahrens } 2073fa9e4066Sahrens } 2074fa9e4066Sahrens 2075fa9e4066Sahrens } 2076fa9e4066Sahrens 207792241e0bSTom Erickson int 207892241e0bSTom Erickson zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, 207992241e0bSTom Erickson size_t proplen, boolean_t literal) 208092241e0bSTom Erickson { 208192241e0bSTom Erickson zfs_prop_t prop; 208292241e0bSTom Erickson int err = 0; 208392241e0bSTom Erickson 208492241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 208592241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 208692241e0bSTom Erickson return (-1); 208792241e0bSTom Erickson 208892241e0bSTom Erickson prop = zfs_name_to_prop(propname); 208992241e0bSTom Erickson 209092241e0bSTom Erickson if (prop != ZPROP_INVAL) { 209192241e0bSTom Erickson uint64_t cookie; 209292241e0bSTom Erickson if (!nvlist_exists(zhp->zfs_recvd_props, propname)) 209392241e0bSTom Erickson return (-1); 209492241e0bSTom Erickson zfs_set_recvd_props_mode(zhp, &cookie); 209592241e0bSTom Erickson err = zfs_prop_get(zhp, prop, propbuf, proplen, 209692241e0bSTom Erickson NULL, NULL, 0, literal); 209792241e0bSTom Erickson zfs_unset_recvd_props_mode(zhp, &cookie); 209892241e0bSTom Erickson } else { 209992241e0bSTom Erickson nvlist_t *propval; 210092241e0bSTom Erickson char *recvdval; 210192241e0bSTom Erickson if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, 210292241e0bSTom Erickson propname, &propval) != 0) 210392241e0bSTom Erickson return (-1); 210492241e0bSTom Erickson verify(nvlist_lookup_string(propval, ZPROP_VALUE, 210592241e0bSTom Erickson &recvdval) == 0); 210692241e0bSTom Erickson (void) strlcpy(propbuf, recvdval, proplen); 210792241e0bSTom Erickson } 210892241e0bSTom Erickson 210992241e0bSTom Erickson return (err == 0 ? 0 : -1); 211092241e0bSTom Erickson } 211192241e0bSTom Erickson 211219b94df9SMatthew Ahrens static int 211319b94df9SMatthew Ahrens get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) 211419b94df9SMatthew Ahrens { 211519b94df9SMatthew Ahrens nvlist_t *value; 211619b94df9SMatthew Ahrens nvpair_t *pair; 211719b94df9SMatthew Ahrens 211819b94df9SMatthew Ahrens value = zfs_get_clones_nvl(zhp); 211919b94df9SMatthew Ahrens if (value == NULL) 212019b94df9SMatthew Ahrens return (-1); 212119b94df9SMatthew Ahrens 212219b94df9SMatthew Ahrens propbuf[0] = '\0'; 212319b94df9SMatthew Ahrens for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; 212419b94df9SMatthew Ahrens pair = nvlist_next_nvpair(value, pair)) { 212519b94df9SMatthew Ahrens if (propbuf[0] != '\0') 212619b94df9SMatthew Ahrens (void) strlcat(propbuf, ",", proplen); 212719b94df9SMatthew Ahrens (void) strlcat(propbuf, nvpair_name(pair), proplen); 212819b94df9SMatthew Ahrens } 212919b94df9SMatthew Ahrens 213019b94df9SMatthew Ahrens return (0); 213119b94df9SMatthew Ahrens } 213219b94df9SMatthew Ahrens 213319b94df9SMatthew Ahrens struct get_clones_arg { 213419b94df9SMatthew Ahrens uint64_t numclones; 213519b94df9SMatthew Ahrens nvlist_t *value; 213619b94df9SMatthew Ahrens const char *origin; 213719b94df9SMatthew Ahrens char buf[ZFS_MAXNAMELEN]; 213819b94df9SMatthew Ahrens }; 213919b94df9SMatthew Ahrens 214019b94df9SMatthew Ahrens int 214119b94df9SMatthew Ahrens get_clones_cb(zfs_handle_t *zhp, void *arg) 214219b94df9SMatthew Ahrens { 214319b94df9SMatthew Ahrens struct get_clones_arg *gca = arg; 214419b94df9SMatthew Ahrens 214519b94df9SMatthew Ahrens if (gca->numclones == 0) { 214619b94df9SMatthew Ahrens zfs_close(zhp); 214719b94df9SMatthew Ahrens return (0); 214819b94df9SMatthew Ahrens } 214919b94df9SMatthew Ahrens 215019b94df9SMatthew Ahrens if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), 215119b94df9SMatthew Ahrens NULL, NULL, 0, B_TRUE) != 0) 215219b94df9SMatthew Ahrens goto out; 215319b94df9SMatthew Ahrens if (strcmp(gca->buf, gca->origin) == 0) { 21543b2aab18SMatthew Ahrens fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); 215519b94df9SMatthew Ahrens gca->numclones--; 215619b94df9SMatthew Ahrens } 215719b94df9SMatthew Ahrens 215819b94df9SMatthew Ahrens out: 215919b94df9SMatthew Ahrens (void) zfs_iter_children(zhp, get_clones_cb, gca); 216019b94df9SMatthew Ahrens zfs_close(zhp); 216119b94df9SMatthew Ahrens return (0); 216219b94df9SMatthew Ahrens } 216319b94df9SMatthew Ahrens 216419b94df9SMatthew Ahrens nvlist_t * 216519b94df9SMatthew Ahrens zfs_get_clones_nvl(zfs_handle_t *zhp) 216619b94df9SMatthew Ahrens { 216719b94df9SMatthew Ahrens nvlist_t *nv, *value; 216819b94df9SMatthew Ahrens 216919b94df9SMatthew Ahrens if (nvlist_lookup_nvlist(zhp->zfs_props, 217019b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { 217119b94df9SMatthew Ahrens struct get_clones_arg gca; 217219b94df9SMatthew Ahrens 217319b94df9SMatthew Ahrens /* 217419b94df9SMatthew Ahrens * if this is a snapshot, then the kernel wasn't able 217519b94df9SMatthew Ahrens * to get the clones. Do it by slowly iterating. 217619b94df9SMatthew Ahrens */ 217719b94df9SMatthew Ahrens if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) 217819b94df9SMatthew Ahrens return (NULL); 217919b94df9SMatthew Ahrens if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) 218019b94df9SMatthew Ahrens return (NULL); 218119b94df9SMatthew Ahrens if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { 218219b94df9SMatthew Ahrens nvlist_free(nv); 218319b94df9SMatthew Ahrens return (NULL); 218419b94df9SMatthew Ahrens } 218519b94df9SMatthew Ahrens 218619b94df9SMatthew Ahrens gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); 218719b94df9SMatthew Ahrens gca.value = value; 218819b94df9SMatthew Ahrens gca.origin = zhp->zfs_name; 218919b94df9SMatthew Ahrens 219019b94df9SMatthew Ahrens if (gca.numclones != 0) { 219119b94df9SMatthew Ahrens zfs_handle_t *root; 219219b94df9SMatthew Ahrens char pool[ZFS_MAXNAMELEN]; 219319b94df9SMatthew Ahrens char *cp = pool; 219419b94df9SMatthew Ahrens 219519b94df9SMatthew Ahrens /* get the pool name */ 219619b94df9SMatthew Ahrens (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); 219719b94df9SMatthew Ahrens (void) strsep(&cp, "/@"); 219819b94df9SMatthew Ahrens root = zfs_open(zhp->zfs_hdl, pool, 219919b94df9SMatthew Ahrens ZFS_TYPE_FILESYSTEM); 220019b94df9SMatthew Ahrens 220119b94df9SMatthew Ahrens (void) get_clones_cb(root, &gca); 220219b94df9SMatthew Ahrens } 220319b94df9SMatthew Ahrens 220419b94df9SMatthew Ahrens if (gca.numclones != 0 || 220519b94df9SMatthew Ahrens nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || 220619b94df9SMatthew Ahrens nvlist_add_nvlist(zhp->zfs_props, 220719b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { 220819b94df9SMatthew Ahrens nvlist_free(nv); 220919b94df9SMatthew Ahrens nvlist_free(value); 221019b94df9SMatthew Ahrens return (NULL); 221119b94df9SMatthew Ahrens } 221219b94df9SMatthew Ahrens nvlist_free(nv); 221319b94df9SMatthew Ahrens nvlist_free(value); 221419b94df9SMatthew Ahrens verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, 221519b94df9SMatthew Ahrens zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); 221619b94df9SMatthew Ahrens } 221719b94df9SMatthew Ahrens 221819b94df9SMatthew Ahrens verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); 221919b94df9SMatthew Ahrens 222019b94df9SMatthew Ahrens return (value); 222119b94df9SMatthew Ahrens } 222219b94df9SMatthew Ahrens 2223fa9e4066Sahrens /* 2224fa9e4066Sahrens * Retrieve a property from the given object. If 'literal' is specified, then 2225fa9e4066Sahrens * numbers are left as exact values. Otherwise, numbers are converted to a 2226fa9e4066Sahrens * human-readable form. 2227fa9e4066Sahrens * 2228fa9e4066Sahrens * Returns 0 on success, or -1 on error. 2229fa9e4066Sahrens */ 2230fa9e4066Sahrens int 2231fa9e4066Sahrens zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, 2232990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) 2233fa9e4066Sahrens { 2234fa9e4066Sahrens char *source = NULL; 2235fa9e4066Sahrens uint64_t val; 22369c3fd121SMatthew Ahrens const char *str; 2237e9dbad6fSeschrock const char *strval; 223892241e0bSTom Erickson boolean_t received = zfs_is_recvd_props_mode(zhp); 2239fa9e4066Sahrens 2240fa9e4066Sahrens /* 2241fa9e4066Sahrens * Check to see if this property applies to our object 2242fa9e4066Sahrens */ 2243fa9e4066Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) 2244fa9e4066Sahrens return (-1); 2245fa9e4066Sahrens 224692241e0bSTom Erickson if (received && zfs_prop_readonly(prop)) 224792241e0bSTom Erickson return (-1); 224892241e0bSTom Erickson 2249fa9e4066Sahrens if (src) 2250990b4856Slling *src = ZPROP_SRC_NONE; 2251fa9e4066Sahrens 2252fa9e4066Sahrens switch (prop) { 2253fa9e4066Sahrens case ZFS_PROP_CREATION: 2254fa9e4066Sahrens /* 2255fa9e4066Sahrens * 'creation' is a time_t stored in the statistics. We convert 2256fa9e4066Sahrens * this into a string unless 'literal' is specified. 2257fa9e4066Sahrens */ 2258fa9e4066Sahrens { 2259a2eea2e1Sahrens val = getprop_uint64(zhp, prop, &source); 2260a2eea2e1Sahrens time_t time = (time_t)val; 2261fa9e4066Sahrens struct tm t; 2262fa9e4066Sahrens 2263fa9e4066Sahrens if (literal || 2264fa9e4066Sahrens localtime_r(&time, &t) == NULL || 2265fa9e4066Sahrens strftime(propbuf, proplen, "%a %b %e %k:%M %Y", 2266fa9e4066Sahrens &t) == 0) 2267a2eea2e1Sahrens (void) snprintf(propbuf, proplen, "%llu", val); 2268fa9e4066Sahrens } 2269fa9e4066Sahrens break; 2270fa9e4066Sahrens 2271fa9e4066Sahrens case ZFS_PROP_MOUNTPOINT: 2272fa9e4066Sahrens /* 2273fa9e4066Sahrens * Getting the precise mountpoint can be tricky. 2274fa9e4066Sahrens * 2275fa9e4066Sahrens * - for 'none' or 'legacy', return those values. 2276fa9e4066Sahrens * - for inherited mountpoints, we want to take everything 2277fa9e4066Sahrens * after our ancestor and append it to the inherited value. 2278fa9e4066Sahrens * 2279fa9e4066Sahrens * If the pool has an alternate root, we want to prepend that 2280fa9e4066Sahrens * root to any values we return. 2281fa9e4066Sahrens */ 228229ab75c9Srm160521 22837f7322feSeschrock str = getprop_string(zhp, prop, &source); 2284fa9e4066Sahrens 2285b21718f0Sgw25295 if (str[0] == '/') { 228629ab75c9Srm160521 char buf[MAXPATHLEN]; 228729ab75c9Srm160521 char *root = buf; 2288a79992aaSTom Erickson const char *relpath; 2289fa9e4066Sahrens 2290a79992aaSTom Erickson /* 2291a79992aaSTom Erickson * If we inherit the mountpoint, even from a dataset 2292a79992aaSTom Erickson * with a received value, the source will be the path of 2293a79992aaSTom Erickson * the dataset we inherit from. If source is 2294a79992aaSTom Erickson * ZPROP_SOURCE_VAL_RECVD, the received value is not 2295a79992aaSTom Erickson * inherited. 2296a79992aaSTom Erickson */ 2297a79992aaSTom Erickson if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { 2298a79992aaSTom Erickson relpath = ""; 2299a79992aaSTom Erickson } else { 2300a79992aaSTom Erickson relpath = zhp->zfs_name + strlen(source); 2301fa9e4066Sahrens if (relpath[0] == '/') 2302fa9e4066Sahrens relpath++; 2303a79992aaSTom Erickson } 2304b21718f0Sgw25295 230529ab75c9Srm160521 if ((zpool_get_prop(zhp->zpool_hdl, 2306c58b3526SAdam Stevko ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, 2307c58b3526SAdam Stevko B_FALSE)) || (strcmp(root, "-") == 0)) 230829ab75c9Srm160521 root[0] = '\0'; 2309b21718f0Sgw25295 /* 2310b21718f0Sgw25295 * Special case an alternate root of '/'. This will 2311b21718f0Sgw25295 * avoid having multiple leading slashes in the 2312b21718f0Sgw25295 * mountpoint path. 2313b21718f0Sgw25295 */ 2314b21718f0Sgw25295 if (strcmp(root, "/") == 0) 2315b21718f0Sgw25295 root++; 2316b21718f0Sgw25295 2317b21718f0Sgw25295 /* 2318b21718f0Sgw25295 * If the mountpoint is '/' then skip over this 2319b21718f0Sgw25295 * if we are obtaining either an alternate root or 2320b21718f0Sgw25295 * an inherited mountpoint. 2321b21718f0Sgw25295 */ 2322b21718f0Sgw25295 if (str[1] == '\0' && (root[0] != '\0' || 2323b21718f0Sgw25295 relpath[0] != '\0')) 23247f7322feSeschrock str++; 2325fa9e4066Sahrens 2326fa9e4066Sahrens if (relpath[0] == '\0') 2327fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s", 23287f7322feSeschrock root, str); 2329fa9e4066Sahrens else 2330fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s%s%s%s", 23317f7322feSeschrock root, str, relpath[0] == '@' ? "" : "/", 2332fa9e4066Sahrens relpath); 2333fa9e4066Sahrens } else { 2334fa9e4066Sahrens /* 'legacy' or 'none' */ 23357f7322feSeschrock (void) strlcpy(propbuf, str, proplen); 2336fa9e4066Sahrens } 2337fa9e4066Sahrens 2338fa9e4066Sahrens break; 2339fa9e4066Sahrens 2340fa9e4066Sahrens case ZFS_PROP_ORIGIN: 23419c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 23429c3fd121SMatthew Ahrens if (str == NULL) 2343fa9e4066Sahrens return (-1); 23449c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2345fa9e4066Sahrens break; 2346fa9e4066Sahrens 234719b94df9SMatthew Ahrens case ZFS_PROP_CLONES: 234819b94df9SMatthew Ahrens if (get_clones_string(zhp, propbuf, proplen) != 0) 234919b94df9SMatthew Ahrens return (-1); 235019b94df9SMatthew Ahrens break; 235119b94df9SMatthew Ahrens 2352fa9e4066Sahrens case ZFS_PROP_QUOTA: 2353a9799022Sck153898 case ZFS_PROP_REFQUOTA: 2354fa9e4066Sahrens case ZFS_PROP_RESERVATION: 2355a9799022Sck153898 case ZFS_PROP_REFRESERVATION: 2356a9799022Sck153898 235799653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 235899653d4eSeschrock return (-1); 2359fa9e4066Sahrens 2360fa9e4066Sahrens /* 2361fa9e4066Sahrens * If quota or reservation is 0, we translate this into 'none' 2362fa9e4066Sahrens * (unless literal is set), and indicate that it's the default 2363fa9e4066Sahrens * value. Otherwise, we print the number nicely and indicate 2364fa9e4066Sahrens * that its set locally. 2365fa9e4066Sahrens */ 2366fa9e4066Sahrens if (val == 0) { 2367fa9e4066Sahrens if (literal) 2368fa9e4066Sahrens (void) strlcpy(propbuf, "0", proplen); 2369fa9e4066Sahrens else 2370fa9e4066Sahrens (void) strlcpy(propbuf, "none", proplen); 2371fa9e4066Sahrens } else { 2372fa9e4066Sahrens if (literal) 23735ad82045Snd150628 (void) snprintf(propbuf, proplen, "%llu", 23745ad82045Snd150628 (u_longlong_t)val); 2375fa9e4066Sahrens else 2376fa9e4066Sahrens zfs_nicenum(val, propbuf, proplen); 2377fa9e4066Sahrens } 2378fa9e4066Sahrens break; 2379fa9e4066Sahrens 2380a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_LIMIT: 2381a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_LIMIT: 2382a2afb611SJerry Jelinek case ZFS_PROP_FILESYSTEM_COUNT: 2383a2afb611SJerry Jelinek case ZFS_PROP_SNAPSHOT_COUNT: 2384a2afb611SJerry Jelinek 2385a2afb611SJerry Jelinek if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2386a2afb611SJerry Jelinek return (-1); 2387a2afb611SJerry Jelinek 2388a2afb611SJerry Jelinek /* 2389a2afb611SJerry Jelinek * If limit is UINT64_MAX, we translate this into 'none' (unless 2390a2afb611SJerry Jelinek * literal is set), and indicate that it's the default value. 2391a2afb611SJerry Jelinek * Otherwise, we print the number nicely and indicate that it's 2392a2afb611SJerry Jelinek * set locally. 2393a2afb611SJerry Jelinek */ 2394a2afb611SJerry Jelinek if (literal) { 2395a2afb611SJerry Jelinek (void) snprintf(propbuf, proplen, "%llu", 2396a2afb611SJerry Jelinek (u_longlong_t)val); 2397a2afb611SJerry Jelinek } else if (val == UINT64_MAX) { 2398a2afb611SJerry Jelinek (void) strlcpy(propbuf, "none", proplen); 2399a2afb611SJerry Jelinek } else { 2400a2afb611SJerry Jelinek zfs_nicenum(val, propbuf, proplen); 2401a2afb611SJerry Jelinek } 2402a2afb611SJerry Jelinek break; 2403a2afb611SJerry Jelinek 2404187d6ac0SMatt Ahrens case ZFS_PROP_REFRATIO: 2405fa9e4066Sahrens case ZFS_PROP_COMPRESSRATIO: 240699653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 240799653d4eSeschrock return (-1); 2408b24ab676SJeff Bonwick (void) snprintf(propbuf, proplen, "%llu.%02llux", 2409b24ab676SJeff Bonwick (u_longlong_t)(val / 100), 2410b24ab676SJeff Bonwick (u_longlong_t)(val % 100)); 2411fa9e4066Sahrens break; 2412fa9e4066Sahrens 2413fa9e4066Sahrens case ZFS_PROP_TYPE: 2414fa9e4066Sahrens switch (zhp->zfs_type) { 2415fa9e4066Sahrens case ZFS_TYPE_FILESYSTEM: 2416fa9e4066Sahrens str = "filesystem"; 2417fa9e4066Sahrens break; 2418fa9e4066Sahrens case ZFS_TYPE_VOLUME: 2419fa9e4066Sahrens str = "volume"; 2420fa9e4066Sahrens break; 2421fa9e4066Sahrens case ZFS_TYPE_SNAPSHOT: 2422fa9e4066Sahrens str = "snapshot"; 2423fa9e4066Sahrens break; 242478f17100SMatthew Ahrens case ZFS_TYPE_BOOKMARK: 242578f17100SMatthew Ahrens str = "bookmark"; 242678f17100SMatthew Ahrens break; 2427fa9e4066Sahrens default: 242899653d4eSeschrock abort(); 2429fa9e4066Sahrens } 2430fa9e4066Sahrens (void) snprintf(propbuf, proplen, "%s", str); 2431fa9e4066Sahrens break; 2432fa9e4066Sahrens 2433fa9e4066Sahrens case ZFS_PROP_MOUNTED: 2434fa9e4066Sahrens /* 2435fa9e4066Sahrens * The 'mounted' property is a pseudo-property that described 2436fa9e4066Sahrens * whether the filesystem is currently mounted. Even though 2437fa9e4066Sahrens * it's a boolean value, the typical values of "on" and "off" 2438fa9e4066Sahrens * don't make sense, so we translate to "yes" and "no". 2439fa9e4066Sahrens */ 244099653d4eSeschrock if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, 244199653d4eSeschrock src, &source, &val) != 0) 244299653d4eSeschrock return (-1); 244399653d4eSeschrock if (val) 2444fa9e4066Sahrens (void) strlcpy(propbuf, "yes", proplen); 2445fa9e4066Sahrens else 2446fa9e4066Sahrens (void) strlcpy(propbuf, "no", proplen); 2447fa9e4066Sahrens break; 2448fa9e4066Sahrens 2449fa9e4066Sahrens case ZFS_PROP_NAME: 2450fa9e4066Sahrens /* 2451fa9e4066Sahrens * The 'name' property is a pseudo-property derived from the 2452fa9e4066Sahrens * dataset name. It is presented as a real property to simplify 2453fa9e4066Sahrens * consumers. 2454fa9e4066Sahrens */ 2455fa9e4066Sahrens (void) strlcpy(propbuf, zhp->zfs_name, proplen); 2456fa9e4066Sahrens break; 2457fa9e4066Sahrens 24584201a95eSRic Aleshire case ZFS_PROP_MLSLABEL: 24594201a95eSRic Aleshire { 24604201a95eSRic Aleshire m_label_t *new_sl = NULL; 24614201a95eSRic Aleshire char *ascii = NULL; /* human readable label */ 24624201a95eSRic Aleshire 24634201a95eSRic Aleshire (void) strlcpy(propbuf, 24644201a95eSRic Aleshire getprop_string(zhp, prop, &source), proplen); 24654201a95eSRic Aleshire 24664201a95eSRic Aleshire if (literal || (strcasecmp(propbuf, 24674201a95eSRic Aleshire ZFS_MLSLABEL_DEFAULT) == 0)) 24684201a95eSRic Aleshire break; 24694201a95eSRic Aleshire 24704201a95eSRic Aleshire /* 24714201a95eSRic Aleshire * Try to translate the internal hex string to 24724201a95eSRic Aleshire * human-readable output. If there are any 24734201a95eSRic Aleshire * problems just use the hex string. 24744201a95eSRic Aleshire */ 24754201a95eSRic Aleshire 24764201a95eSRic Aleshire if (str_to_label(propbuf, &new_sl, MAC_LABEL, 24774201a95eSRic Aleshire L_NO_CORRECTION, NULL) == -1) { 24784201a95eSRic Aleshire m_label_free(new_sl); 24794201a95eSRic Aleshire break; 24804201a95eSRic Aleshire } 24814201a95eSRic Aleshire 24824201a95eSRic Aleshire if (label_to_str(new_sl, &ascii, M_LABEL, 24834201a95eSRic Aleshire DEF_NAMES) != 0) { 24844201a95eSRic Aleshire if (ascii) 24854201a95eSRic Aleshire free(ascii); 24864201a95eSRic Aleshire m_label_free(new_sl); 24874201a95eSRic Aleshire break; 24884201a95eSRic Aleshire } 24894201a95eSRic Aleshire m_label_free(new_sl); 24904201a95eSRic Aleshire 24914201a95eSRic Aleshire (void) strlcpy(propbuf, ascii, proplen); 24924201a95eSRic Aleshire free(ascii); 24934201a95eSRic Aleshire } 24944201a95eSRic Aleshire break; 24954201a95eSRic Aleshire 2496f0f3ef5aSGarrett D'Amore case ZFS_PROP_GUID: 2497f0f3ef5aSGarrett D'Amore /* 2498f0f3ef5aSGarrett D'Amore * GUIDs are stored as numbers, but they are identifiers. 2499f0f3ef5aSGarrett D'Amore * We don't want them to be pretty printed, because pretty 2500f0f3ef5aSGarrett D'Amore * printing mangles the ID into a truncated and useless value. 2501f0f3ef5aSGarrett D'Amore */ 2502f0f3ef5aSGarrett D'Amore if (get_numeric_property(zhp, prop, src, &source, &val) != 0) 2503f0f3ef5aSGarrett D'Amore return (-1); 2504f0f3ef5aSGarrett D'Amore (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); 2505f0f3ef5aSGarrett D'Amore break; 2506f0f3ef5aSGarrett D'Amore 2507fa9e4066Sahrens default: 2508e7437265Sahrens switch (zfs_prop_get_type(prop)) { 250991ebeef5Sahrens case PROP_TYPE_NUMBER: 2510e7437265Sahrens if (get_numeric_property(zhp, prop, src, 2511e7437265Sahrens &source, &val) != 0) 2512e7437265Sahrens return (-1); 2513e7437265Sahrens if (literal) 2514e7437265Sahrens (void) snprintf(propbuf, proplen, "%llu", 2515e7437265Sahrens (u_longlong_t)val); 2516e7437265Sahrens else 2517e7437265Sahrens zfs_nicenum(val, propbuf, proplen); 2518e7437265Sahrens break; 2519e7437265Sahrens 252091ebeef5Sahrens case PROP_TYPE_STRING: 25219c3fd121SMatthew Ahrens str = getprop_string(zhp, prop, &source); 25229c3fd121SMatthew Ahrens if (str == NULL) 25239c3fd121SMatthew Ahrens return (-1); 25249c3fd121SMatthew Ahrens (void) strlcpy(propbuf, str, proplen); 2525e7437265Sahrens break; 2526e7437265Sahrens 252791ebeef5Sahrens case PROP_TYPE_INDEX: 2528fe192f76Sahrens if (get_numeric_property(zhp, prop, src, 2529fe192f76Sahrens &source, &val) != 0) 2530fe192f76Sahrens return (-1); 2531fe192f76Sahrens if (zfs_prop_index_to_string(prop, val, &strval) != 0) 2532e7437265Sahrens return (-1); 2533e7437265Sahrens (void) strlcpy(propbuf, strval, proplen); 2534e7437265Sahrens break; 2535e7437265Sahrens 2536e7437265Sahrens default: 253799653d4eSeschrock abort(); 2538fa9e4066Sahrens } 2539e7437265Sahrens } 2540fa9e4066Sahrens 2541fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2542fa9e4066Sahrens 2543fa9e4066Sahrens return (0); 2544fa9e4066Sahrens } 2545fa9e4066Sahrens 2546fa9e4066Sahrens /* 2547fa9e4066Sahrens * Utility function to get the given numeric property. Does no validation that 2548fa9e4066Sahrens * the given property is the appropriate type; should only be used with 2549fa9e4066Sahrens * hard-coded property types. 2550fa9e4066Sahrens */ 2551fa9e4066Sahrens uint64_t 2552fa9e4066Sahrens zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) 2553fa9e4066Sahrens { 2554fa9e4066Sahrens char *source; 255599653d4eSeschrock uint64_t val; 2556fa9e4066Sahrens 25573cb34c60Sahrens (void) get_numeric_property(zhp, prop, NULL, &source, &val); 255899653d4eSeschrock 255999653d4eSeschrock return (val); 2560fa9e4066Sahrens } 2561fa9e4066Sahrens 25627b97dc1aSrm160521 int 25637b97dc1aSrm160521 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) 25647b97dc1aSrm160521 { 25657b97dc1aSrm160521 char buf[64]; 25667b97dc1aSrm160521 256714843421SMatthew Ahrens (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); 25687b97dc1aSrm160521 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); 25697b97dc1aSrm160521 } 25707b97dc1aSrm160521 2571fa9e4066Sahrens /* 2572fa9e4066Sahrens * Similar to zfs_prop_get(), but returns the value as an integer. 2573fa9e4066Sahrens */ 2574fa9e4066Sahrens int 2575fa9e4066Sahrens zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, 2576990b4856Slling zprop_source_t *src, char *statbuf, size_t statlen) 2577fa9e4066Sahrens { 2578fa9e4066Sahrens char *source; 2579fa9e4066Sahrens 2580fa9e4066Sahrens /* 2581fa9e4066Sahrens * Check to see if this property applies to our object 2582fa9e4066Sahrens */ 2583e45ce728Sahrens if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) { 2584ece3d9b3Slling return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, 258599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot get property '%s'"), 258699653d4eSeschrock zfs_prop_to_name(prop))); 2587e45ce728Sahrens } 2588fa9e4066Sahrens 2589fa9e4066Sahrens if (src) 2590990b4856Slling *src = ZPROP_SRC_NONE; 2591fa9e4066Sahrens 259299653d4eSeschrock if (get_numeric_property(zhp, prop, src, &source, value) != 0) 259399653d4eSeschrock return (-1); 2594fa9e4066Sahrens 2595fa9e4066Sahrens get_source(zhp, src, source, statbuf, statlen); 2596fa9e4066Sahrens 2597fa9e4066Sahrens return (0); 2598fa9e4066Sahrens } 2599fa9e4066Sahrens 260014843421SMatthew Ahrens static int 260114843421SMatthew Ahrens idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, 260214843421SMatthew Ahrens char **domainp, idmap_rid_t *ridp) 260314843421SMatthew Ahrens { 260414843421SMatthew Ahrens idmap_get_handle_t *get_hdl = NULL; 260514843421SMatthew Ahrens idmap_stat status; 260614843421SMatthew Ahrens int err = EINVAL; 260714843421SMatthew Ahrens 26081fdeec65Sjoyce mcintosh if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) 260914843421SMatthew Ahrens goto out; 261014843421SMatthew Ahrens 261114843421SMatthew Ahrens if (isuser) { 261214843421SMatthew Ahrens err = idmap_get_sidbyuid(get_hdl, id, 261314843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 261414843421SMatthew Ahrens } else { 261514843421SMatthew Ahrens err = idmap_get_sidbygid(get_hdl, id, 261614843421SMatthew Ahrens IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); 261714843421SMatthew Ahrens } 261814843421SMatthew Ahrens if (err == IDMAP_SUCCESS && 261914843421SMatthew Ahrens idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && 262014843421SMatthew Ahrens status == IDMAP_SUCCESS) 262114843421SMatthew Ahrens err = 0; 262214843421SMatthew Ahrens else 262314843421SMatthew Ahrens err = EINVAL; 262414843421SMatthew Ahrens out: 262514843421SMatthew Ahrens if (get_hdl) 262614843421SMatthew Ahrens idmap_get_destroy(get_hdl); 262714843421SMatthew Ahrens return (err); 262814843421SMatthew Ahrens } 262914843421SMatthew Ahrens 263014843421SMatthew Ahrens /* 263114843421SMatthew Ahrens * convert the propname into parameters needed by kernel 263214843421SMatthew Ahrens * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 263314843421SMatthew Ahrens * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 263414843421SMatthew Ahrens */ 263514843421SMatthew Ahrens static int 263614843421SMatthew Ahrens userquota_propname_decode(const char *propname, boolean_t zoned, 263714843421SMatthew Ahrens zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) 263814843421SMatthew Ahrens { 263914843421SMatthew Ahrens zfs_userquota_prop_t type; 264014843421SMatthew Ahrens char *cp, *end; 26413b12c289SMatthew Ahrens char *numericsid = NULL; 264214843421SMatthew Ahrens boolean_t isuser; 264314843421SMatthew Ahrens 264414843421SMatthew Ahrens domain[0] = '\0'; 26451ed6b69aSGordon Ross *ridp = 0; 264614843421SMatthew Ahrens /* Figure out the property type ({user|group}{quota|space}) */ 264714843421SMatthew Ahrens for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { 264814843421SMatthew Ahrens if (strncmp(propname, zfs_userquota_prop_prefixes[type], 264914843421SMatthew Ahrens strlen(zfs_userquota_prop_prefixes[type])) == 0) 265014843421SMatthew Ahrens break; 265114843421SMatthew Ahrens } 265214843421SMatthew Ahrens if (type == ZFS_NUM_USERQUOTA_PROPS) 265314843421SMatthew Ahrens return (EINVAL); 265414843421SMatthew Ahrens *typep = type; 265514843421SMatthew Ahrens 265614843421SMatthew Ahrens isuser = (type == ZFS_PROP_USERQUOTA || 265714843421SMatthew Ahrens type == ZFS_PROP_USERUSED); 265814843421SMatthew Ahrens 265914843421SMatthew Ahrens cp = strchr(propname, '@') + 1; 266014843421SMatthew Ahrens 266114843421SMatthew Ahrens if (strchr(cp, '@')) { 266214843421SMatthew Ahrens /* 266314843421SMatthew Ahrens * It's a SID name (eg "user@domain") that needs to be 26643b12c289SMatthew Ahrens * turned into S-1-domainID-RID. 266514843421SMatthew Ahrens */ 26661ed6b69aSGordon Ross int flag = 0; 26671ed6b69aSGordon Ross idmap_stat stat, map_stat; 26681ed6b69aSGordon Ross uid_t pid; 26691ed6b69aSGordon Ross idmap_rid_t rid; 26701ed6b69aSGordon Ross idmap_get_handle_t *gh = NULL; 26711ed6b69aSGordon Ross 26721ed6b69aSGordon Ross stat = idmap_get_create(&gh); 26731ed6b69aSGordon Ross if (stat != IDMAP_SUCCESS) { 26741ed6b69aSGordon Ross idmap_get_destroy(gh); 26751ed6b69aSGordon Ross return (ENOMEM); 26761ed6b69aSGordon Ross } 267714843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 267814843421SMatthew Ahrens return (ENOENT); 26793b12c289SMatthew Ahrens if (isuser) { 26801ed6b69aSGordon Ross stat = idmap_getuidbywinname(cp, NULL, flag, &pid); 26811ed6b69aSGordon Ross if (stat < 0) 26821ed6b69aSGordon Ross return (ENOENT); 26831ed6b69aSGordon Ross stat = idmap_get_sidbyuid(gh, pid, flag, &numericsid, 26841ed6b69aSGordon Ross &rid, &map_stat); 26853b12c289SMatthew Ahrens } else { 26861ed6b69aSGordon Ross stat = idmap_getgidbywinname(cp, NULL, flag, &pid); 26871ed6b69aSGordon Ross if (stat < 0) 26881ed6b69aSGordon Ross return (ENOENT); 26891ed6b69aSGordon Ross stat = idmap_get_sidbygid(gh, pid, flag, &numericsid, 26901ed6b69aSGordon Ross &rid, &map_stat); 26913b12c289SMatthew Ahrens } 26921ed6b69aSGordon Ross if (stat < 0) { 26931ed6b69aSGordon Ross idmap_get_destroy(gh); 26941ed6b69aSGordon Ross return (ENOENT); 26951ed6b69aSGordon Ross } 26961ed6b69aSGordon Ross stat = idmap_get_mappings(gh); 26971ed6b69aSGordon Ross idmap_get_destroy(gh); 26981ed6b69aSGordon Ross 26991ed6b69aSGordon Ross if (stat < 0) { 270014843421SMatthew Ahrens return (ENOENT); 27013b12c289SMatthew Ahrens } 27023b12c289SMatthew Ahrens if (numericsid == NULL) 270314843421SMatthew Ahrens return (ENOENT); 27043b12c289SMatthew Ahrens cp = numericsid; 27051ed6b69aSGordon Ross *ridp = rid; 27063b12c289SMatthew Ahrens /* will be further decoded below */ 27073b12c289SMatthew Ahrens } 27083b12c289SMatthew Ahrens 27093b12c289SMatthew Ahrens if (strncmp(cp, "S-1-", 4) == 0) { 271014843421SMatthew Ahrens /* It's a numeric SID (eg "S-1-234-567-89") */ 27113b12c289SMatthew Ahrens (void) strlcpy(domain, cp, domainlen); 27121ed6b69aSGordon Ross errno = 0; 27131ed6b69aSGordon Ross if (*ridp == 0) { 271414843421SMatthew Ahrens cp = strrchr(domain, '-'); 271514843421SMatthew Ahrens *cp = '\0'; 271614843421SMatthew Ahrens cp++; 271714843421SMatthew Ahrens *ridp = strtoull(cp, &end, 10); 27181ed6b69aSGordon Ross } else { 27191ed6b69aSGordon Ross end = ""; 27201ed6b69aSGordon Ross } 27213b12c289SMatthew Ahrens if (numericsid) { 27223b12c289SMatthew Ahrens free(numericsid); 27233b12c289SMatthew Ahrens numericsid = NULL; 27243b12c289SMatthew Ahrens } 2725777badbaSMatthew Ahrens if (errno != 0 || *end != '\0') 272614843421SMatthew Ahrens return (EINVAL); 272714843421SMatthew Ahrens } else if (!isdigit(*cp)) { 272814843421SMatthew Ahrens /* 272914843421SMatthew Ahrens * It's a user/group name (eg "user") that needs to be 273014843421SMatthew Ahrens * turned into a uid/gid 273114843421SMatthew Ahrens */ 273214843421SMatthew Ahrens if (zoned && getzoneid() == GLOBAL_ZONEID) 273314843421SMatthew Ahrens return (ENOENT); 273414843421SMatthew Ahrens if (isuser) { 273514843421SMatthew Ahrens struct passwd *pw; 273614843421SMatthew Ahrens pw = getpwnam(cp); 273714843421SMatthew Ahrens if (pw == NULL) 273814843421SMatthew Ahrens return (ENOENT); 273914843421SMatthew Ahrens *ridp = pw->pw_uid; 274014843421SMatthew Ahrens } else { 274114843421SMatthew Ahrens struct group *gr; 274214843421SMatthew Ahrens gr = getgrnam(cp); 274314843421SMatthew Ahrens if (gr == NULL) 274414843421SMatthew Ahrens return (ENOENT); 274514843421SMatthew Ahrens *ridp = gr->gr_gid; 274614843421SMatthew Ahrens } 274714843421SMatthew Ahrens } else { 274814843421SMatthew Ahrens /* It's a user/group ID (eg "12345"). */ 274914843421SMatthew Ahrens uid_t id = strtoul(cp, &end, 10); 275014843421SMatthew Ahrens idmap_rid_t rid; 275114843421SMatthew Ahrens char *mapdomain; 275214843421SMatthew Ahrens 275314843421SMatthew Ahrens if (*end != '\0') 275414843421SMatthew Ahrens return (EINVAL); 275514843421SMatthew Ahrens if (id > MAXUID) { 275614843421SMatthew Ahrens /* It's an ephemeral ID. */ 275714843421SMatthew Ahrens if (idmap_id_to_numeric_domain_rid(id, isuser, 275814843421SMatthew Ahrens &mapdomain, &rid) != 0) 275914843421SMatthew Ahrens return (ENOENT); 27603b12c289SMatthew Ahrens (void) strlcpy(domain, mapdomain, domainlen); 276114843421SMatthew Ahrens *ridp = rid; 276214843421SMatthew Ahrens } else { 276314843421SMatthew Ahrens *ridp = id; 276414843421SMatthew Ahrens } 276514843421SMatthew Ahrens } 276614843421SMatthew Ahrens 27673b12c289SMatthew Ahrens ASSERT3P(numericsid, ==, NULL); 276814843421SMatthew Ahrens return (0); 276914843421SMatthew Ahrens } 277014843421SMatthew Ahrens 2771edea4b55SLin Ling static int 2772edea4b55SLin Ling zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, 2773edea4b55SLin Ling uint64_t *propvalue, zfs_userquota_prop_t *typep) 277414843421SMatthew Ahrens { 277514843421SMatthew Ahrens int err; 277614843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 277714843421SMatthew Ahrens 277819b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 277914843421SMatthew Ahrens 278014843421SMatthew Ahrens err = userquota_propname_decode(propname, 278114843421SMatthew Ahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED), 2782edea4b55SLin Ling typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); 2783edea4b55SLin Ling zc.zc_objset_type = *typep; 278414843421SMatthew Ahrens if (err) 278514843421SMatthew Ahrens return (err); 278614843421SMatthew Ahrens 278714843421SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); 278814843421SMatthew Ahrens if (err) 278914843421SMatthew Ahrens return (err); 279014843421SMatthew Ahrens 2791edea4b55SLin Ling *propvalue = zc.zc_cookie; 2792edea4b55SLin Ling return (0); 2793edea4b55SLin Ling } 2794edea4b55SLin Ling 2795edea4b55SLin Ling int 2796edea4b55SLin Ling zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, 2797edea4b55SLin Ling uint64_t *propvalue) 2798edea4b55SLin Ling { 2799edea4b55SLin Ling zfs_userquota_prop_t type; 2800edea4b55SLin Ling 2801edea4b55SLin Ling return (zfs_prop_get_userquota_common(zhp, propname, propvalue, 2802edea4b55SLin Ling &type)); 2803edea4b55SLin Ling } 2804edea4b55SLin Ling 2805edea4b55SLin Ling int 2806edea4b55SLin Ling zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, 2807edea4b55SLin Ling char *propbuf, int proplen, boolean_t literal) 2808edea4b55SLin Ling { 2809edea4b55SLin Ling int err; 2810edea4b55SLin Ling uint64_t propvalue; 2811edea4b55SLin Ling zfs_userquota_prop_t type; 2812edea4b55SLin Ling 2813edea4b55SLin Ling err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, 2814edea4b55SLin Ling &type); 2815edea4b55SLin Ling 2816edea4b55SLin Ling if (err) 2817edea4b55SLin Ling return (err); 2818edea4b55SLin Ling 281914843421SMatthew Ahrens if (literal) { 2820edea4b55SLin Ling (void) snprintf(propbuf, proplen, "%llu", propvalue); 2821edea4b55SLin Ling } else if (propvalue == 0 && 282214843421SMatthew Ahrens (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA)) { 282314843421SMatthew Ahrens (void) strlcpy(propbuf, "none", proplen); 282414843421SMatthew Ahrens } else { 2825edea4b55SLin Ling zfs_nicenum(propvalue, propbuf, proplen); 282614843421SMatthew Ahrens } 282714843421SMatthew Ahrens return (0); 282814843421SMatthew Ahrens } 282914843421SMatthew Ahrens 283019b94df9SMatthew Ahrens int 283119b94df9SMatthew Ahrens zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, 283219b94df9SMatthew Ahrens uint64_t *propvalue) 283319b94df9SMatthew Ahrens { 283419b94df9SMatthew Ahrens int err; 283519b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 }; 283619b94df9SMatthew Ahrens const char *snapname; 283719b94df9SMatthew Ahrens 283819b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 283919b94df9SMatthew Ahrens 284019b94df9SMatthew Ahrens snapname = strchr(propname, '@') + 1; 284119b94df9SMatthew Ahrens if (strchr(snapname, '@')) { 284219b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); 284319b94df9SMatthew Ahrens } else { 284419b94df9SMatthew Ahrens /* snapname is the short name, append it to zhp's fsname */ 284519b94df9SMatthew Ahrens char *cp; 284619b94df9SMatthew Ahrens 284719b94df9SMatthew Ahrens (void) strlcpy(zc.zc_value, zhp->zfs_name, 284819b94df9SMatthew Ahrens sizeof (zc.zc_value)); 284919b94df9SMatthew Ahrens cp = strchr(zc.zc_value, '@'); 285019b94df9SMatthew Ahrens if (cp != NULL) 285119b94df9SMatthew Ahrens *cp = '\0'; 285219b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); 285319b94df9SMatthew Ahrens (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); 285419b94df9SMatthew Ahrens } 285519b94df9SMatthew Ahrens 285619b94df9SMatthew Ahrens err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); 285719b94df9SMatthew Ahrens if (err) 285819b94df9SMatthew Ahrens return (err); 285919b94df9SMatthew Ahrens 286019b94df9SMatthew Ahrens *propvalue = zc.zc_cookie; 286119b94df9SMatthew Ahrens return (0); 286219b94df9SMatthew Ahrens } 286319b94df9SMatthew Ahrens 286419b94df9SMatthew Ahrens int 286519b94df9SMatthew Ahrens zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, 286619b94df9SMatthew Ahrens char *propbuf, int proplen, boolean_t literal) 286719b94df9SMatthew Ahrens { 286819b94df9SMatthew Ahrens int err; 286919b94df9SMatthew Ahrens uint64_t propvalue; 287019b94df9SMatthew Ahrens 287119b94df9SMatthew Ahrens err = zfs_prop_get_written_int(zhp, propname, &propvalue); 287219b94df9SMatthew Ahrens 287319b94df9SMatthew Ahrens if (err) 287419b94df9SMatthew Ahrens return (err); 287519b94df9SMatthew Ahrens 287619b94df9SMatthew Ahrens if (literal) { 287719b94df9SMatthew Ahrens (void) snprintf(propbuf, proplen, "%llu", propvalue); 287819b94df9SMatthew Ahrens } else { 287919b94df9SMatthew Ahrens zfs_nicenum(propvalue, propbuf, proplen); 288019b94df9SMatthew Ahrens } 288119b94df9SMatthew Ahrens return (0); 288219b94df9SMatthew Ahrens } 288319b94df9SMatthew Ahrens 2884fa9e4066Sahrens /* 2885fa9e4066Sahrens * Returns the name of the given zfs handle. 2886fa9e4066Sahrens */ 2887fa9e4066Sahrens const char * 2888fa9e4066Sahrens zfs_get_name(const zfs_handle_t *zhp) 2889fa9e4066Sahrens { 2890fa9e4066Sahrens return (zhp->zfs_name); 2891fa9e4066Sahrens } 2892fa9e4066Sahrens 2893fa9e4066Sahrens /* 2894fa9e4066Sahrens * Returns the type of the given zfs handle. 2895fa9e4066Sahrens */ 2896fa9e4066Sahrens zfs_type_t 2897fa9e4066Sahrens zfs_get_type(const zfs_handle_t *zhp) 2898fa9e4066Sahrens { 2899fa9e4066Sahrens return (zhp->zfs_type); 2900fa9e4066Sahrens } 2901fa9e4066Sahrens 29027f7322feSeschrock /* 2903d41c4376SMark J Musante * Is one dataset name a child dataset of another? 2904d41c4376SMark J Musante * 2905d41c4376SMark J Musante * Needs to handle these cases: 2906d41c4376SMark J Musante * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" 2907d41c4376SMark J Musante * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" 2908d41c4376SMark J Musante * Descendant? No. No. No. Yes. 2909d41c4376SMark J Musante */ 2910d41c4376SMark J Musante static boolean_t 2911d41c4376SMark J Musante is_descendant(const char *ds1, const char *ds2) 2912d41c4376SMark J Musante { 2913d41c4376SMark J Musante size_t d1len = strlen(ds1); 2914d41c4376SMark J Musante 2915d41c4376SMark J Musante /* ds2 can't be a descendant if it's smaller */ 2916d41c4376SMark J Musante if (strlen(ds2) < d1len) 2917d41c4376SMark J Musante return (B_FALSE); 2918d41c4376SMark J Musante 2919d41c4376SMark J Musante /* otherwise, compare strings and verify that there's a '/' char */ 2920d41c4376SMark J Musante return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); 2921d41c4376SMark J Musante } 2922d41c4376SMark J Musante 2923d41c4376SMark J Musante /* 2924fa9e4066Sahrens * Given a complete name, return just the portion that refers to the parent. 292519b94df9SMatthew Ahrens * Will return -1 if there is no parent (path is just the name of the 292619b94df9SMatthew Ahrens * pool). 2927fa9e4066Sahrens */ 2928fa9e4066Sahrens static int 2929fa9e4066Sahrens parent_name(const char *path, char *buf, size_t buflen) 2930fa9e4066Sahrens { 293119b94df9SMatthew Ahrens char *slashp; 2932fa9e4066Sahrens 293319b94df9SMatthew Ahrens (void) strlcpy(buf, path, buflen); 293419b94df9SMatthew Ahrens 293519b94df9SMatthew Ahrens if ((slashp = strrchr(buf, '/')) == NULL) 2936fa9e4066Sahrens return (-1); 293719b94df9SMatthew Ahrens *slashp = '\0'; 2938fa9e4066Sahrens 2939fa9e4066Sahrens return (0); 2940fa9e4066Sahrens } 2941fa9e4066Sahrens 2942fa9e4066Sahrens /* 29437f1f55eaSvb160487 * If accept_ancestor is false, then check to make sure that the given path has 29447f1f55eaSvb160487 * a parent, and that it exists. If accept_ancestor is true, then find the 29457f1f55eaSvb160487 * closest existing ancestor for the given path. In prefixlen return the 29467f1f55eaSvb160487 * length of already existing prefix of the given path. We also fetch the 29477f1f55eaSvb160487 * 'zoned' property, which is used to validate property settings when creating 29487f1f55eaSvb160487 * new datasets. 2949fa9e4066Sahrens */ 2950fa9e4066Sahrens static int 29517f1f55eaSvb160487 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, 29527f1f55eaSvb160487 boolean_t accept_ancestor, int *prefixlen) 2953fa9e4066Sahrens { 2954fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 2955fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 2956fa9e4066Sahrens char *slash; 29577f7322feSeschrock zfs_handle_t *zhp; 295899653d4eSeschrock char errbuf[1024]; 2959d41c4376SMark J Musante uint64_t is_zoned; 296099653d4eSeschrock 2961deb8317bSMark J Musante (void) snprintf(errbuf, sizeof (errbuf), 2962deb8317bSMark J Musante dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); 2963fa9e4066Sahrens 2964fa9e4066Sahrens /* get parent, and check to see if this is just a pool */ 2965fa9e4066Sahrens if (parent_name(path, parent, sizeof (parent)) != 0) { 296699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 296799653d4eSeschrock "missing dataset name")); 296899653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 2969fa9e4066Sahrens } 2970fa9e4066Sahrens 2971fa9e4066Sahrens /* check to see if the pool exists */ 2972fa9e4066Sahrens if ((slash = strchr(parent, '/')) == NULL) 2973fa9e4066Sahrens slash = parent + strlen(parent); 29748ac09fceSRichard Lowe (void) strncpy(zc.zc_name, parent, slash - parent); 2975fa9e4066Sahrens zc.zc_name[slash - parent] = '\0'; 297699653d4eSeschrock if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && 2977fa9e4066Sahrens errno == ENOENT) { 297899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 297999653d4eSeschrock "no such pool '%s'"), zc.zc_name); 298099653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 2981fa9e4066Sahrens } 2982fa9e4066Sahrens 2983fa9e4066Sahrens /* check to see if the parent dataset exists */ 29847f1f55eaSvb160487 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { 29857f1f55eaSvb160487 if (errno == ENOENT && accept_ancestor) { 29867f1f55eaSvb160487 /* 29877f1f55eaSvb160487 * Go deeper to find an ancestor, give up on top level. 29887f1f55eaSvb160487 */ 29897f1f55eaSvb160487 if (parent_name(parent, parent, sizeof (parent)) != 0) { 29907f1f55eaSvb160487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 29917f1f55eaSvb160487 "no such pool '%s'"), zc.zc_name); 29927f1f55eaSvb160487 return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29937f1f55eaSvb160487 } 29947f1f55eaSvb160487 } else if (errno == ENOENT) { 299599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 299699653d4eSeschrock "parent does not exist")); 299799653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 29987f1f55eaSvb160487 } else 299999653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3000fa9e4066Sahrens } 3001fa9e4066Sahrens 3002d41c4376SMark J Musante is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 3003d41c4376SMark J Musante if (zoned != NULL) 3004d41c4376SMark J Musante *zoned = is_zoned; 3005d41c4376SMark J Musante 3006fa9e4066Sahrens /* we are in a non-global zone, but parent is in the global zone */ 3007d41c4376SMark J Musante if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { 300899653d4eSeschrock (void) zfs_standard_error(hdl, EPERM, errbuf); 30097f7322feSeschrock zfs_close(zhp); 3010fa9e4066Sahrens return (-1); 3011fa9e4066Sahrens } 3012fa9e4066Sahrens 3013fa9e4066Sahrens /* make sure parent is a filesystem */ 30147f7322feSeschrock if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { 301599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 301699653d4eSeschrock "parent is not a filesystem")); 301799653d4eSeschrock (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 30187f7322feSeschrock zfs_close(zhp); 3019fa9e4066Sahrens return (-1); 3020fa9e4066Sahrens } 3021fa9e4066Sahrens 30227f7322feSeschrock zfs_close(zhp); 30237f1f55eaSvb160487 if (prefixlen != NULL) 30247f1f55eaSvb160487 *prefixlen = strlen(parent); 30257f1f55eaSvb160487 return (0); 30267f1f55eaSvb160487 } 30277f1f55eaSvb160487 30287f1f55eaSvb160487 /* 30297f1f55eaSvb160487 * Finds whether the dataset of the given type(s) exists. 30307f1f55eaSvb160487 */ 30317f1f55eaSvb160487 boolean_t 30327f1f55eaSvb160487 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) 30337f1f55eaSvb160487 { 30347f1f55eaSvb160487 zfs_handle_t *zhp; 30357f1f55eaSvb160487 3036f18faf3fSek110237 if (!zfs_validate_name(hdl, path, types, B_FALSE)) 30377f1f55eaSvb160487 return (B_FALSE); 30387f1f55eaSvb160487 30397f1f55eaSvb160487 /* 30407f1f55eaSvb160487 * Try to get stats for the dataset, which will tell us if it exists. 30417f1f55eaSvb160487 */ 30427f1f55eaSvb160487 if ((zhp = make_dataset_handle(hdl, path)) != NULL) { 30437f1f55eaSvb160487 int ds_type = zhp->zfs_type; 30447f1f55eaSvb160487 30457f1f55eaSvb160487 zfs_close(zhp); 30467f1f55eaSvb160487 if (types & ds_type) 30477f1f55eaSvb160487 return (B_TRUE); 30487f1f55eaSvb160487 } 30497f1f55eaSvb160487 return (B_FALSE); 30507f1f55eaSvb160487 } 30517f1f55eaSvb160487 30527f1f55eaSvb160487 /* 30533cb34c60Sahrens * Given a path to 'target', create all the ancestors between 30543cb34c60Sahrens * the prefixlen portion of the path, and the target itself. 30553cb34c60Sahrens * Fail if the initial prefixlen-ancestor does not already exist. 30563cb34c60Sahrens */ 30573cb34c60Sahrens int 30583cb34c60Sahrens create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) 30593cb34c60Sahrens { 30603cb34c60Sahrens zfs_handle_t *h; 30613cb34c60Sahrens char *cp; 30623cb34c60Sahrens const char *opname; 30633cb34c60Sahrens 30643cb34c60Sahrens /* make sure prefix exists */ 30653cb34c60Sahrens cp = target + prefixlen; 30663cb34c60Sahrens if (*cp != '/') { 30673cb34c60Sahrens assert(strchr(cp, '/') == NULL); 30683cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30693cb34c60Sahrens } else { 30703cb34c60Sahrens *cp = '\0'; 30713cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 30723cb34c60Sahrens *cp = '/'; 30733cb34c60Sahrens } 30743cb34c60Sahrens if (h == NULL) 30753cb34c60Sahrens return (-1); 30763cb34c60Sahrens zfs_close(h); 30773cb34c60Sahrens 30783cb34c60Sahrens /* 30793cb34c60Sahrens * Attempt to create, mount, and share any ancestor filesystems, 30803cb34c60Sahrens * up to the prefixlen-long one. 30813cb34c60Sahrens */ 30823cb34c60Sahrens for (cp = target + prefixlen + 1; 30833cb34c60Sahrens cp = strchr(cp, '/'); *cp = '/', cp++) { 30843cb34c60Sahrens 30853cb34c60Sahrens *cp = '\0'; 30863cb34c60Sahrens 30873cb34c60Sahrens h = make_dataset_handle(hdl, target); 30883cb34c60Sahrens if (h) { 30893cb34c60Sahrens /* it already exists, nothing to do here */ 30903cb34c60Sahrens zfs_close(h); 30913cb34c60Sahrens continue; 30923cb34c60Sahrens } 30933cb34c60Sahrens 30943cb34c60Sahrens if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, 30953cb34c60Sahrens NULL) != 0) { 30963cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "create"); 30973cb34c60Sahrens goto ancestorerr; 30983cb34c60Sahrens } 30993cb34c60Sahrens 31003cb34c60Sahrens h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); 31013cb34c60Sahrens if (h == NULL) { 31023cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "open"); 31033cb34c60Sahrens goto ancestorerr; 31043cb34c60Sahrens } 31053cb34c60Sahrens 31063cb34c60Sahrens if (zfs_mount(h, NULL, 0) != 0) { 31073cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "mount"); 31083cb34c60Sahrens goto ancestorerr; 31093cb34c60Sahrens } 31103cb34c60Sahrens 31113cb34c60Sahrens if (zfs_share(h) != 0) { 31123cb34c60Sahrens opname = dgettext(TEXT_DOMAIN, "share"); 31133cb34c60Sahrens goto ancestorerr; 31143cb34c60Sahrens } 31153cb34c60Sahrens 31163cb34c60Sahrens zfs_close(h); 31173cb34c60Sahrens } 31183cb34c60Sahrens 31193cb34c60Sahrens return (0); 31203cb34c60Sahrens 31213cb34c60Sahrens ancestorerr: 31223cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 31233cb34c60Sahrens "failed to %s ancestor '%s'"), opname, target); 31243cb34c60Sahrens return (-1); 31253cb34c60Sahrens } 31263cb34c60Sahrens 31273cb34c60Sahrens /* 31287f1f55eaSvb160487 * Creates non-existing ancestors of the given path. 31297f1f55eaSvb160487 */ 31307f1f55eaSvb160487 int 31317f1f55eaSvb160487 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) 31327f1f55eaSvb160487 { 31337f1f55eaSvb160487 int prefix; 31347f1f55eaSvb160487 char *path_copy; 31357f1f55eaSvb160487 int rc; 31367f1f55eaSvb160487 3137d41c4376SMark J Musante if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) 31387f1f55eaSvb160487 return (-1); 31397f1f55eaSvb160487 31407f1f55eaSvb160487 if ((path_copy = strdup(path)) != NULL) { 31417f1f55eaSvb160487 rc = create_parents(hdl, path_copy, prefix); 31427f1f55eaSvb160487 free(path_copy); 31437f1f55eaSvb160487 } 31447f1f55eaSvb160487 if (path_copy == NULL || rc != 0) 31457f1f55eaSvb160487 return (-1); 31467f1f55eaSvb160487 3147fa9e4066Sahrens return (0); 3148fa9e4066Sahrens } 3149fa9e4066Sahrens 3150fa9e4066Sahrens /* 3151e9dbad6fSeschrock * Create a new filesystem or volume. 3152fa9e4066Sahrens */ 3153fa9e4066Sahrens int 315499653d4eSeschrock zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, 3155e9dbad6fSeschrock nvlist_t *props) 3156fa9e4066Sahrens { 3157fa9e4066Sahrens int ret; 3158fa9e4066Sahrens uint64_t size = 0; 3159fa9e4066Sahrens uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); 316099653d4eSeschrock char errbuf[1024]; 3161e9dbad6fSeschrock uint64_t zoned; 31624445fffbSMatthew Ahrens dmu_objset_type_t ost; 316399653d4eSeschrock 316499653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 316599653d4eSeschrock "cannot create '%s'"), path); 3166fa9e4066Sahrens 3167fa9e4066Sahrens /* validate the path, taking care to note the extended error message */ 3168f18faf3fSek110237 if (!zfs_validate_name(hdl, path, type, B_TRUE)) 316999653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3170fa9e4066Sahrens 3171fa9e4066Sahrens /* validate parents exist */ 31727f1f55eaSvb160487 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) 3173fa9e4066Sahrens return (-1); 3174fa9e4066Sahrens 3175fa9e4066Sahrens /* 3176fa9e4066Sahrens * The failure modes when creating a dataset of a different type over 3177fa9e4066Sahrens * one that already exists is a little strange. In particular, if you 3178fa9e4066Sahrens * try to create a dataset on top of an existing dataset, the ioctl() 3179fa9e4066Sahrens * will return ENOENT, not EEXIST. To prevent this from happening, we 3180fa9e4066Sahrens * first try to see if the dataset exists. 3181fa9e4066Sahrens */ 31824445fffbSMatthew Ahrens if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { 318399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 318499653d4eSeschrock "dataset already exists")); 318599653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3186fa9e4066Sahrens } 3187fa9e4066Sahrens 3188fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) 31894445fffbSMatthew Ahrens ost = DMU_OST_ZVOL; 3190fa9e4066Sahrens else 31914445fffbSMatthew Ahrens ost = DMU_OST_ZFS; 3192fa9e4066Sahrens 3193e9316f76SJoe Stein /* open zpool handle for prop validation */ 3194e9316f76SJoe Stein char pool_path[MAXNAMELEN]; 3195e9316f76SJoe Stein (void) strlcpy(pool_path, path, sizeof (pool_path)); 3196e9316f76SJoe Stein 3197e9316f76SJoe Stein /* truncate pool_path at first slash */ 3198e9316f76SJoe Stein char *p = strchr(pool_path, '/'); 3199e9316f76SJoe Stein if (p != NULL) 3200e9316f76SJoe Stein *p = '\0'; 3201e9316f76SJoe Stein 3202e9316f76SJoe Stein zpool_handle_t *zpool_handle = zpool_open(hdl, pool_path); 3203e9316f76SJoe Stein 32040a48a24eStimh if (props && (props = zfs_valid_proplist(hdl, type, props, 3205e9316f76SJoe Stein zoned, NULL, zpool_handle, errbuf)) == 0) { 3206e9316f76SJoe Stein zpool_close(zpool_handle); 3207e9dbad6fSeschrock return (-1); 3208e9316f76SJoe Stein } 3209e9316f76SJoe Stein zpool_close(zpool_handle); 3210e9dbad6fSeschrock 3211fa9e4066Sahrens if (type == ZFS_TYPE_VOLUME) { 32125c5460e9Seschrock /* 32135c5460e9Seschrock * If we are creating a volume, the size and block size must 32145c5460e9Seschrock * satisfy a few restraints. First, the blocksize must be a 32155c5460e9Seschrock * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the 32165c5460e9Seschrock * volsize must be a multiple of the block size, and cannot be 32175c5460e9Seschrock * zero. 32185c5460e9Seschrock */ 3219e9dbad6fSeschrock if (props == NULL || nvlist_lookup_uint64(props, 3220e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { 3221e9dbad6fSeschrock nvlist_free(props); 322299653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3223e9dbad6fSeschrock "missing volume size")); 3224e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3225fa9e4066Sahrens } 3226fa9e4066Sahrens 3227e9dbad6fSeschrock if ((ret = nvlist_lookup_uint64(props, 3228e9dbad6fSeschrock zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 3229e9dbad6fSeschrock &blocksize)) != 0) { 3230e9dbad6fSeschrock if (ret == ENOENT) { 3231e9dbad6fSeschrock blocksize = zfs_prop_default_numeric( 3232e9dbad6fSeschrock ZFS_PROP_VOLBLOCKSIZE); 3233e9dbad6fSeschrock } else { 3234e9dbad6fSeschrock nvlist_free(props); 323599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3236e9dbad6fSeschrock "missing volume block size")); 3237e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3238e9dbad6fSeschrock } 3239e9dbad6fSeschrock } 3240e9dbad6fSeschrock 3241e9dbad6fSeschrock if (size == 0) { 3242e9dbad6fSeschrock nvlist_free(props); 3243e9dbad6fSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3244e9dbad6fSeschrock "volume size cannot be zero")); 3245e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 32465c5460e9Seschrock } 32475c5460e9Seschrock 32485c5460e9Seschrock if (size % blocksize != 0) { 3249e9dbad6fSeschrock nvlist_free(props); 325099653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3251e9dbad6fSeschrock "volume size must be a multiple of volume block " 3252e9dbad6fSeschrock "size")); 3253e9dbad6fSeschrock return (zfs_error(hdl, EZFS_BADPROP, errbuf)); 3254e9dbad6fSeschrock } 32555c5460e9Seschrock } 32565c5460e9Seschrock 3257fa9e4066Sahrens /* create the dataset */ 32584445fffbSMatthew Ahrens ret = lzc_create(path, ost, props); 32594445fffbSMatthew Ahrens nvlist_free(props); 3260e9dbad6fSeschrock 3261fa9e4066Sahrens /* check for failure */ 3262fa9e4066Sahrens if (ret != 0) { 3263fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 3264fa9e4066Sahrens (void) parent_name(path, parent, sizeof (parent)); 3265fa9e4066Sahrens 3266fa9e4066Sahrens switch (errno) { 3267fa9e4066Sahrens case ENOENT: 326899653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 326999653d4eSeschrock "no such parent '%s'"), parent); 327099653d4eSeschrock return (zfs_error(hdl, EZFS_NOENT, errbuf)); 3271fa9e4066Sahrens 3272fa9e4066Sahrens case EINVAL: 327399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3274d7d4af51Smmusante "parent '%s' is not a filesystem"), parent); 327599653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3276fa9e4066Sahrens 327740feaa91Sahrens case ENOTSUP: 327840feaa91Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 327940feaa91Sahrens "pool must be upgraded to set this " 328040feaa91Sahrens "property or value")); 328140feaa91Sahrens return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); 3282fa9e4066Sahrens #ifdef _ILP32 3283fa9e4066Sahrens case EOVERFLOW: 3284fa9e4066Sahrens /* 3285fa9e4066Sahrens * This platform can't address a volume this big. 3286fa9e4066Sahrens */ 328799653d4eSeschrock if (type == ZFS_TYPE_VOLUME) 328899653d4eSeschrock return (zfs_error(hdl, EZFS_VOLTOOBIG, 328999653d4eSeschrock errbuf)); 3290fa9e4066Sahrens #endif 329199653d4eSeschrock /* FALLTHROUGH */ 3292fa9e4066Sahrens default: 329399653d4eSeschrock return (zfs_standard_error(hdl, errno, errbuf)); 3294fa9e4066Sahrens } 3295fa9e4066Sahrens } 3296fa9e4066Sahrens 3297fa9e4066Sahrens return (0); 3298fa9e4066Sahrens } 3299fa9e4066Sahrens 3300fa9e4066Sahrens /* 3301fa9e4066Sahrens * Destroys the given dataset. The caller must make sure that the filesystem 330265fec9f6SChristopher Siden * isn't mounted, and that there are no active dependents. If the file system 330365fec9f6SChristopher Siden * does not exist this function does nothing. 3304fa9e4066Sahrens */ 3305fa9e4066Sahrens int 3306842727c2SChris Kirby zfs_destroy(zfs_handle_t *zhp, boolean_t defer) 3307fa9e4066Sahrens { 3308fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3309fa9e4066Sahrens 331078f17100SMatthew Ahrens if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { 331178f17100SMatthew Ahrens nvlist_t *nv = fnvlist_alloc(); 331278f17100SMatthew Ahrens fnvlist_add_boolean(nv, zhp->zfs_name); 331378f17100SMatthew Ahrens int error = lzc_destroy_bookmarks(nv, NULL); 331478f17100SMatthew Ahrens fnvlist_free(nv); 331578f17100SMatthew Ahrens if (error != 0) { 331678f17100SMatthew Ahrens return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 331778f17100SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 331878f17100SMatthew Ahrens zhp->zfs_name)); 331978f17100SMatthew Ahrens } 332078f17100SMatthew Ahrens return (0); 332178f17100SMatthew Ahrens } 332278f17100SMatthew Ahrens 3323fa9e4066Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3324fa9e4066Sahrens 3325e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) { 3326fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3327fa9e4066Sahrens } else { 3328fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3329fa9e4066Sahrens } 3330fa9e4066Sahrens 3331842727c2SChris Kirby zc.zc_defer_destroy = defer; 333265fec9f6SChristopher Siden if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 && 333365fec9f6SChristopher Siden errno != ENOENT) { 3334ece3d9b3Slling return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, 333599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), 333699653d4eSeschrock zhp->zfs_name)); 33371d452cf5Sahrens } 3338fa9e4066Sahrens 3339fa9e4066Sahrens remove_mountpoint(zhp); 3340fa9e4066Sahrens 3341fa9e4066Sahrens return (0); 3342fa9e4066Sahrens } 3343fa9e4066Sahrens 33441d452cf5Sahrens struct destroydata { 334519b94df9SMatthew Ahrens nvlist_t *nvl; 334619b94df9SMatthew Ahrens const char *snapname; 33471d452cf5Sahrens }; 33481d452cf5Sahrens 33491d452cf5Sahrens static int 3350681d9761SEric Taylor zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) 33511d452cf5Sahrens { 33521d452cf5Sahrens struct destroydata *dd = arg; 33531d452cf5Sahrens char name[ZFS_MAXNAMELEN]; 3354681d9761SEric Taylor int rv = 0; 33551d452cf5Sahrens 335619b94df9SMatthew Ahrens (void) snprintf(name, sizeof (name), 335719b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, dd->snapname); 33581d452cf5Sahrens 3359a7a845e4SSteven Hartland if (lzc_exists(name)) 336019b94df9SMatthew Ahrens verify(nvlist_add_boolean(dd->nvl, name) == 0); 33611d452cf5Sahrens 336219b94df9SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); 33633ccfa83cSahrens zfs_close(zhp); 33643ccfa83cSahrens return (rv); 33651d452cf5Sahrens } 33661d452cf5Sahrens 33671d452cf5Sahrens /* 33681d452cf5Sahrens * Destroys all snapshots with the given name in zhp & descendants. 33691d452cf5Sahrens */ 33701d452cf5Sahrens int 3371842727c2SChris Kirby zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) 33721d452cf5Sahrens { 33731d452cf5Sahrens int ret; 33741d452cf5Sahrens struct destroydata dd = { 0 }; 33751d452cf5Sahrens 33761d452cf5Sahrens dd.snapname = snapname; 337719b94df9SMatthew Ahrens verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); 337819b94df9SMatthew Ahrens (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); 33791d452cf5Sahrens 3380a7a845e4SSteven Hartland if (nvlist_empty(dd.nvl)) { 338119b94df9SMatthew Ahrens ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, 33821d452cf5Sahrens dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), 338319b94df9SMatthew Ahrens zhp->zfs_name, snapname); 338419b94df9SMatthew Ahrens } else { 33853b2aab18SMatthew Ahrens ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); 338619b94df9SMatthew Ahrens } 338719b94df9SMatthew Ahrens nvlist_free(dd.nvl); 338819b94df9SMatthew Ahrens return (ret); 3389e5351341SMatthew Ahrens } 3390e5351341SMatthew Ahrens 339119b94df9SMatthew Ahrens /* 33923b2aab18SMatthew Ahrens * Destroys all the snapshots named in the nvlist. 339319b94df9SMatthew Ahrens */ 339419b94df9SMatthew Ahrens int 33953b2aab18SMatthew Ahrens zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) 339619b94df9SMatthew Ahrens { 339719b94df9SMatthew Ahrens int ret; 33984445fffbSMatthew Ahrens nvlist_t *errlist; 339919b94df9SMatthew Ahrens 34004445fffbSMatthew Ahrens ret = lzc_destroy_snaps(snaps, defer, &errlist); 34011d452cf5Sahrens 34023b2aab18SMatthew Ahrens if (ret == 0) 34033b2aab18SMatthew Ahrens return (0); 34043b2aab18SMatthew Ahrens 3405a7a845e4SSteven Hartland if (nvlist_empty(errlist)) { 34063b2aab18SMatthew Ahrens char errbuf[1024]; 34073b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 34083b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); 34093b2aab18SMatthew Ahrens 34103b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, ret, errbuf); 34113b2aab18SMatthew Ahrens } 34124445fffbSMatthew Ahrens for (nvpair_t *pair = nvlist_next_nvpair(errlist, NULL); 34134445fffbSMatthew Ahrens pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { 34141d452cf5Sahrens char errbuf[1024]; 34154445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 34164445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), 34174445fffbSMatthew Ahrens nvpair_name(pair)); 34181d452cf5Sahrens 34194445fffbSMatthew Ahrens switch (fnvpair_value_int32(pair)) { 34201d452cf5Sahrens case EEXIST: 34213b2aab18SMatthew Ahrens zfs_error_aux(hdl, 34223b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "snapshot is cloned")); 34233b2aab18SMatthew Ahrens ret = zfs_error(hdl, EZFS_EXISTS, errbuf); 34244445fffbSMatthew Ahrens break; 34251d452cf5Sahrens default: 34263b2aab18SMatthew Ahrens ret = zfs_standard_error(hdl, errno, errbuf); 34274445fffbSMatthew Ahrens break; 34284445fffbSMatthew Ahrens } 34291d452cf5Sahrens } 34301d452cf5Sahrens 34314445fffbSMatthew Ahrens return (ret); 34321d452cf5Sahrens } 34331d452cf5Sahrens 3434fa9e4066Sahrens /* 3435fa9e4066Sahrens * Clones the given dataset. The target must be of the same type as the source. 3436fa9e4066Sahrens */ 3437fa9e4066Sahrens int 3438e9dbad6fSeschrock zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) 3439fa9e4066Sahrens { 3440fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 3441fa9e4066Sahrens int ret; 344299653d4eSeschrock char errbuf[1024]; 344399653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 3444e9dbad6fSeschrock uint64_t zoned; 3445fa9e4066Sahrens 3446fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); 3447fa9e4066Sahrens 344899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 344999653d4eSeschrock "cannot create '%s'"), target); 345099653d4eSeschrock 345119b94df9SMatthew Ahrens /* validate the target/clone name */ 3452f18faf3fSek110237 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) 345399653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3454fa9e4066Sahrens 3455fa9e4066Sahrens /* validate parents exist */ 34567f1f55eaSvb160487 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) 3457fa9e4066Sahrens return (-1); 3458fa9e4066Sahrens 3459fa9e4066Sahrens (void) parent_name(target, parent, sizeof (parent)); 3460fa9e4066Sahrens 3461fa9e4066Sahrens /* do the clone */ 3462e9dbad6fSeschrock 3463e9dbad6fSeschrock if (props) { 34644445fffbSMatthew Ahrens zfs_type_t type; 34654445fffbSMatthew Ahrens if (ZFS_IS_VOLUME(zhp)) { 34664445fffbSMatthew Ahrens type = ZFS_TYPE_VOLUME; 34674445fffbSMatthew Ahrens } else { 34684445fffbSMatthew Ahrens type = ZFS_TYPE_FILESYSTEM; 34694445fffbSMatthew Ahrens } 34700a48a24eStimh if ((props = zfs_valid_proplist(hdl, type, props, zoned, 3471e9316f76SJoe Stein zhp, zhp->zpool_hdl, errbuf)) == NULL) 3472e9dbad6fSeschrock return (-1); 3473e9dbad6fSeschrock } 3474e9dbad6fSeschrock 34754445fffbSMatthew Ahrens ret = lzc_clone(target, zhp->zfs_name, props); 3476e9dbad6fSeschrock nvlist_free(props); 3477e9dbad6fSeschrock 3478fa9e4066Sahrens if (ret != 0) { 3479fa9e4066Sahrens switch (errno) { 3480fa9e4066Sahrens 3481fa9e4066Sahrens case ENOENT: 3482fa9e4066Sahrens /* 3483fa9e4066Sahrens * The parent doesn't exist. We should have caught this 3484fa9e4066Sahrens * above, but there may a race condition that has since 3485fa9e4066Sahrens * destroyed the parent. 3486fa9e4066Sahrens * 3487fa9e4066Sahrens * At this point, we don't know whether it's the source 3488fa9e4066Sahrens * that doesn't exist anymore, or whether the target 3489fa9e4066Sahrens * dataset doesn't exist. 3490fa9e4066Sahrens */ 349199653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 349299653d4eSeschrock "no such parent '%s'"), parent); 349399653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); 3494fa9e4066Sahrens 349599653d4eSeschrock case EXDEV: 349699653d4eSeschrock zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, 349799653d4eSeschrock "source and target pools differ")); 349899653d4eSeschrock return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, 349999653d4eSeschrock errbuf)); 350099653d4eSeschrock 350199653d4eSeschrock default: 350299653d4eSeschrock return (zfs_standard_error(zhp->zfs_hdl, errno, 350399653d4eSeschrock errbuf)); 350499653d4eSeschrock } 350599653d4eSeschrock } 350699653d4eSeschrock 350799653d4eSeschrock return (ret); 350899653d4eSeschrock } 350999653d4eSeschrock 351099653d4eSeschrock /* 351199653d4eSeschrock * Promotes the given clone fs to be the clone parent. 351299653d4eSeschrock */ 351399653d4eSeschrock int 351499653d4eSeschrock zfs_promote(zfs_handle_t *zhp) 351599653d4eSeschrock { 351699653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 351799653d4eSeschrock zfs_cmd_t zc = { 0 }; 351899653d4eSeschrock char parent[MAXPATHLEN]; 351999653d4eSeschrock int ret; 352099653d4eSeschrock char errbuf[1024]; 352199653d4eSeschrock 352299653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 352399653d4eSeschrock "cannot promote '%s'"), zhp->zfs_name); 352499653d4eSeschrock 352599653d4eSeschrock if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 352699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 352799653d4eSeschrock "snapshots can not be promoted")); 352899653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 352999653d4eSeschrock } 353099653d4eSeschrock 35313cb34c60Sahrens (void) strlcpy(parent, zhp->zfs_dmustats.dds_origin, sizeof (parent)); 353299653d4eSeschrock if (parent[0] == '\0') { 353399653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 353499653d4eSeschrock "not a cloned filesystem")); 353599653d4eSeschrock return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 353699653d4eSeschrock } 353799653d4eSeschrock 35383cb34c60Sahrens (void) strlcpy(zc.zc_value, zhp->zfs_dmustats.dds_origin, 3539e9dbad6fSeschrock sizeof (zc.zc_value)); 354099653d4eSeschrock (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3541ecd6cf80Smarks ret = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc); 354299653d4eSeschrock 354399653d4eSeschrock if (ret != 0) { 35440b69c2f0Sahrens int save_errno = errno; 3545fa9e4066Sahrens 35460b69c2f0Sahrens switch (save_errno) { 3547fa9e4066Sahrens case EEXIST: 3548681d9761SEric Taylor /* There is a conflicting snapshot name. */ 354999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3550681d9761SEric Taylor "conflicting snapshot '%s' from parent '%s'"), 3551681d9761SEric Taylor zc.zc_string, parent); 355299653d4eSeschrock return (zfs_error(hdl, EZFS_EXISTS, errbuf)); 3553fa9e4066Sahrens 3554fa9e4066Sahrens default: 35550b69c2f0Sahrens return (zfs_standard_error(hdl, save_errno, errbuf)); 3556fa9e4066Sahrens } 3557fa9e4066Sahrens } 3558e9dbad6fSeschrock return (ret); 35591d452cf5Sahrens } 35601d452cf5Sahrens 35614445fffbSMatthew Ahrens typedef struct snapdata { 35624445fffbSMatthew Ahrens nvlist_t *sd_nvl; 35634445fffbSMatthew Ahrens const char *sd_snapname; 35644445fffbSMatthew Ahrens } snapdata_t; 35654445fffbSMatthew Ahrens 35664445fffbSMatthew Ahrens static int 35674445fffbSMatthew Ahrens zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) 35684445fffbSMatthew Ahrens { 35694445fffbSMatthew Ahrens snapdata_t *sd = arg; 35704445fffbSMatthew Ahrens char name[ZFS_MAXNAMELEN]; 35714445fffbSMatthew Ahrens int rv = 0; 35724445fffbSMatthew Ahrens 3573ca48f36fSKeith M Wesolowski if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { 35744445fffbSMatthew Ahrens (void) snprintf(name, sizeof (name), 35754445fffbSMatthew Ahrens "%s@%s", zfs_get_name(zhp), sd->sd_snapname); 35764445fffbSMatthew Ahrens 35774445fffbSMatthew Ahrens fnvlist_add_boolean(sd->sd_nvl, name); 35784445fffbSMatthew Ahrens 35794445fffbSMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); 3580ca48f36fSKeith M Wesolowski } 35814445fffbSMatthew Ahrens zfs_close(zhp); 3582ca48f36fSKeith M Wesolowski 35834445fffbSMatthew Ahrens return (rv); 35844445fffbSMatthew Ahrens } 35854445fffbSMatthew Ahrens 3586fa9e4066Sahrens /* 35874445fffbSMatthew Ahrens * Creates snapshots. The keys in the snaps nvlist are the snapshots to be 35884445fffbSMatthew Ahrens * created. 3589fa9e4066Sahrens */ 3590fa9e4066Sahrens int 35914445fffbSMatthew Ahrens zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) 35924445fffbSMatthew Ahrens { 35934445fffbSMatthew Ahrens int ret; 35944445fffbSMatthew Ahrens char errbuf[1024]; 35954445fffbSMatthew Ahrens nvpair_t *elem; 35964445fffbSMatthew Ahrens nvlist_t *errors; 35974445fffbSMatthew Ahrens 35984445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 35994445fffbSMatthew Ahrens "cannot create snapshots ")); 36004445fffbSMatthew Ahrens 36014445fffbSMatthew Ahrens elem = NULL; 36024445fffbSMatthew Ahrens while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { 36034445fffbSMatthew Ahrens const char *snapname = nvpair_name(elem); 36044445fffbSMatthew Ahrens 36054445fffbSMatthew Ahrens /* validate the target name */ 36064445fffbSMatthew Ahrens if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, 36074445fffbSMatthew Ahrens B_TRUE)) { 36084445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 36094445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 36104445fffbSMatthew Ahrens "cannot create snapshot '%s'"), snapname); 36114445fffbSMatthew Ahrens return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 36124445fffbSMatthew Ahrens } 36134445fffbSMatthew Ahrens } 36144445fffbSMatthew Ahrens 3615e9316f76SJoe Stein /* 3616e9316f76SJoe Stein * get pool handle for prop validation. assumes all snaps are in the 3617e9316f76SJoe Stein * same pool, as does lzc_snapshot (below). 3618e9316f76SJoe Stein */ 3619e9316f76SJoe Stein char pool[MAXNAMELEN]; 3620e9316f76SJoe Stein elem = nvlist_next_nvpair(snaps, NULL); 3621e9316f76SJoe Stein (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); 3622e9316f76SJoe Stein pool[strcspn(pool, "/@")] = '\0'; 3623e9316f76SJoe Stein zpool_handle_t *zpool_hdl = zpool_open(hdl, pool); 3624e9316f76SJoe Stein 36254445fffbSMatthew Ahrens if (props != NULL && 36264445fffbSMatthew Ahrens (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, 3627e9316f76SJoe Stein props, B_FALSE, NULL, zpool_hdl, errbuf)) == NULL) { 3628e9316f76SJoe Stein zpool_close(zpool_hdl); 36294445fffbSMatthew Ahrens return (-1); 36304445fffbSMatthew Ahrens } 3631e9316f76SJoe Stein zpool_close(zpool_hdl); 36324445fffbSMatthew Ahrens 36334445fffbSMatthew Ahrens ret = lzc_snapshot(snaps, props, &errors); 36344445fffbSMatthew Ahrens 36354445fffbSMatthew Ahrens if (ret != 0) { 36364445fffbSMatthew Ahrens boolean_t printed = B_FALSE; 36374445fffbSMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 36384445fffbSMatthew Ahrens elem != NULL; 36394445fffbSMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 36404445fffbSMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 36414445fffbSMatthew Ahrens dgettext(TEXT_DOMAIN, 36424445fffbSMatthew Ahrens "cannot create snapshot '%s'"), nvpair_name(elem)); 36434445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, 36444445fffbSMatthew Ahrens fnvpair_value_int32(elem), errbuf); 36454445fffbSMatthew Ahrens printed = B_TRUE; 36464445fffbSMatthew Ahrens } 36474445fffbSMatthew Ahrens if (!printed) { 36484445fffbSMatthew Ahrens switch (ret) { 36494445fffbSMatthew Ahrens case EXDEV: 36504445fffbSMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 36514445fffbSMatthew Ahrens "multiple snapshots of same " 36524445fffbSMatthew Ahrens "fs not allowed")); 36534445fffbSMatthew Ahrens (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 36544445fffbSMatthew Ahrens 36554445fffbSMatthew Ahrens break; 36564445fffbSMatthew Ahrens default: 36574445fffbSMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 36584445fffbSMatthew Ahrens } 36594445fffbSMatthew Ahrens } 36604445fffbSMatthew Ahrens } 36614445fffbSMatthew Ahrens 36624445fffbSMatthew Ahrens nvlist_free(props); 36634445fffbSMatthew Ahrens nvlist_free(errors); 36644445fffbSMatthew Ahrens return (ret); 36654445fffbSMatthew Ahrens } 36664445fffbSMatthew Ahrens 36674445fffbSMatthew Ahrens int 3668bb0ade09Sahrens zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, 3669bb0ade09Sahrens nvlist_t *props) 3670fa9e4066Sahrens { 3671fa9e4066Sahrens int ret; 36724445fffbSMatthew Ahrens snapdata_t sd = { 0 }; 36734445fffbSMatthew Ahrens char fsname[ZFS_MAXNAMELEN]; 36744445fffbSMatthew Ahrens char *cp; 36754445fffbSMatthew Ahrens zfs_handle_t *zhp; 367699653d4eSeschrock char errbuf[1024]; 3677fa9e4066Sahrens 367899653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 36794445fffbSMatthew Ahrens "cannot snapshot %s"), path); 368099653d4eSeschrock 3681f18faf3fSek110237 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) 368299653d4eSeschrock return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3683fa9e4066Sahrens 36844445fffbSMatthew Ahrens (void) strlcpy(fsname, path, sizeof (fsname)); 36854445fffbSMatthew Ahrens cp = strchr(fsname, '@'); 36864445fffbSMatthew Ahrens *cp = '\0'; 36874445fffbSMatthew Ahrens sd.sd_snapname = cp + 1; 3688bb0ade09Sahrens 36894445fffbSMatthew Ahrens if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | 3690fa9e4066Sahrens ZFS_TYPE_VOLUME)) == NULL) { 3691fa9e4066Sahrens return (-1); 3692fa9e4066Sahrens } 3693fa9e4066Sahrens 36944445fffbSMatthew Ahrens verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); 36954445fffbSMatthew Ahrens if (recursive) { 36964445fffbSMatthew Ahrens (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); 36974445fffbSMatthew Ahrens } else { 36984445fffbSMatthew Ahrens fnvlist_add_boolean(sd.sd_nvl, path); 3699681d9761SEric Taylor } 3700fa9e4066Sahrens 37014445fffbSMatthew Ahrens ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); 37024445fffbSMatthew Ahrens nvlist_free(sd.sd_nvl); 3703fa9e4066Sahrens zfs_close(zhp); 3704fa9e4066Sahrens return (ret); 3705fa9e4066Sahrens } 3706fa9e4066Sahrens 3707fa9e4066Sahrens /* 3708b12a1c38Slling * Destroy any more recent snapshots. We invoke this callback on any dependents 3709b12a1c38Slling * of the snapshot first. If the 'cb_dependent' member is non-zero, then this 3710b12a1c38Slling * is a dependent and we should just destroy it without checking the transaction 3711b12a1c38Slling * group. 3712fa9e4066Sahrens */ 3713b12a1c38Slling typedef struct rollback_data { 3714b12a1c38Slling const char *cb_target; /* the snapshot */ 3715b12a1c38Slling uint64_t cb_create; /* creation time reference */ 3716c391e322Sahrens boolean_t cb_error; 3717c391e322Sahrens boolean_t cb_force; 3718b12a1c38Slling } rollback_data_t; 3719b12a1c38Slling 3720b12a1c38Slling static int 372178f17100SMatthew Ahrens rollback_destroy_dependent(zfs_handle_t *zhp, void *data) 3722b12a1c38Slling { 3723b12a1c38Slling rollback_data_t *cbp = data; 3724c391e322Sahrens prop_changelist_t *clp; 3725c391e322Sahrens 372678f17100SMatthew Ahrens /* We must destroy this clone; first unmount it */ 37270069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 3728c391e322Sahrens cbp->cb_force ? MS_FORCE: 0); 3729c391e322Sahrens if (clp == NULL || changelist_prefix(clp) != 0) { 3730c391e322Sahrens cbp->cb_error = B_TRUE; 3731c391e322Sahrens zfs_close(zhp); 3732c391e322Sahrens return (0); 3733c391e322Sahrens } 3734842727c2SChris Kirby if (zfs_destroy(zhp, B_FALSE) != 0) 3735c391e322Sahrens cbp->cb_error = B_TRUE; 3736c391e322Sahrens else 3737c391e322Sahrens changelist_remove(clp, zhp->zfs_name); 3738ba7b046eSahrens (void) changelist_postfix(clp); 3739c391e322Sahrens changelist_free(clp); 374078f17100SMatthew Ahrens 374178f17100SMatthew Ahrens zfs_close(zhp); 374278f17100SMatthew Ahrens return (0); 374378f17100SMatthew Ahrens } 374478f17100SMatthew Ahrens 374578f17100SMatthew Ahrens static int 374678f17100SMatthew Ahrens rollback_destroy(zfs_handle_t *zhp, void *data) 374778f17100SMatthew Ahrens { 374878f17100SMatthew Ahrens rollback_data_t *cbp = data; 374978f17100SMatthew Ahrens 375078f17100SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { 375178f17100SMatthew Ahrens cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, 375278f17100SMatthew Ahrens rollback_destroy_dependent, cbp); 375378f17100SMatthew Ahrens 375478f17100SMatthew Ahrens cbp->cb_error |= zfs_destroy(zhp, B_FALSE); 3755b12a1c38Slling } 3756b12a1c38Slling 3757b12a1c38Slling zfs_close(zhp); 3758b12a1c38Slling return (0); 3759b12a1c38Slling } 3760b12a1c38Slling 3761b12a1c38Slling /* 37624ccbb6e7Sahrens * Given a dataset, rollback to a specific snapshot, discarding any 37634ccbb6e7Sahrens * data changes since then and making it the active dataset. 37644ccbb6e7Sahrens * 376578f17100SMatthew Ahrens * Any snapshots and bookmarks more recent than the target are 376678f17100SMatthew Ahrens * destroyed, along with their dependents (i.e. clones). 3767b12a1c38Slling */ 37684ccbb6e7Sahrens int 3769c391e322Sahrens zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) 3770fa9e4066Sahrens { 37714ccbb6e7Sahrens rollback_data_t cb = { 0 }; 37724ccbb6e7Sahrens int err; 37737b97dc1aSrm160521 boolean_t restore_resv = 0; 37747b97dc1aSrm160521 uint64_t old_volsize, new_volsize; 37757b97dc1aSrm160521 zfs_prop_t resv_prop; 3776fa9e4066Sahrens 3777fa9e4066Sahrens assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || 3778fa9e4066Sahrens zhp->zfs_type == ZFS_TYPE_VOLUME); 3779fa9e4066Sahrens 37804ccbb6e7Sahrens /* 37812e2c1355SMatthew Ahrens * Destroy all recent snapshots and their dependents. 37824ccbb6e7Sahrens */ 3783c391e322Sahrens cb.cb_force = force; 37844ccbb6e7Sahrens cb.cb_target = snap->zfs_name; 37854ccbb6e7Sahrens cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); 378678f17100SMatthew Ahrens (void) zfs_iter_snapshots(zhp, rollback_destroy, &cb); 378778f17100SMatthew Ahrens (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); 37884ccbb6e7Sahrens 3789c391e322Sahrens if (cb.cb_error) 3790c391e322Sahrens return (-1); 37914ccbb6e7Sahrens 37924ccbb6e7Sahrens /* 37934ccbb6e7Sahrens * Now that we have verified that the snapshot is the latest, 37944ccbb6e7Sahrens * rollback to the given snapshot. 37954ccbb6e7Sahrens */ 37964ccbb6e7Sahrens 37977b97dc1aSrm160521 if (zhp->zfs_type == ZFS_TYPE_VOLUME) { 37987b97dc1aSrm160521 if (zfs_which_resv_prop(zhp, &resv_prop) < 0) 37997b97dc1aSrm160521 return (-1); 38007b97dc1aSrm160521 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38017b97dc1aSrm160521 restore_resv = 38027b97dc1aSrm160521 (old_volsize == zfs_prop_get_int(zhp, resv_prop)); 38037b97dc1aSrm160521 } 3804fa9e4066Sahrens 3805fa9e4066Sahrens /* 38064ccbb6e7Sahrens * We rely on zfs_iter_children() to verify that there are no 38074ccbb6e7Sahrens * newer snapshots for the given dataset. Therefore, we can 38084ccbb6e7Sahrens * simply pass the name on to the ioctl() call. There is still 38094ccbb6e7Sahrens * an unlikely race condition where the user has taken a 38104ccbb6e7Sahrens * snapshot since we verified that this was the most recent. 3811fa9e4066Sahrens */ 3812a7027df1SMatthew Ahrens err = lzc_rollback(zhp->zfs_name, NULL, 0); 3813a7027df1SMatthew Ahrens if (err != 0) { 3814ece3d9b3Slling (void) zfs_standard_error_fmt(zhp->zfs_hdl, errno, 381599653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), 381699653d4eSeschrock zhp->zfs_name); 3817b9415e83Srm160521 return (err); 3818b9415e83Srm160521 } 3819fa9e4066Sahrens 38207b97dc1aSrm160521 /* 38217b97dc1aSrm160521 * For volumes, if the pre-rollback volsize matched the pre- 38227b97dc1aSrm160521 * rollback reservation and the volsize has changed then set 38237b97dc1aSrm160521 * the reservation property to the post-rollback volsize. 38247b97dc1aSrm160521 * Make a new handle since the rollback closed the dataset. 38257b97dc1aSrm160521 */ 3826b9415e83Srm160521 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && 3827b9415e83Srm160521 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { 38287b97dc1aSrm160521 if (restore_resv) { 38297b97dc1aSrm160521 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); 38307b97dc1aSrm160521 if (old_volsize != new_volsize) 3831b9415e83Srm160521 err = zfs_prop_set_int(zhp, resv_prop, 3832b9415e83Srm160521 new_volsize); 38337b97dc1aSrm160521 } 38347b97dc1aSrm160521 zfs_close(zhp); 38357b97dc1aSrm160521 } 38364ccbb6e7Sahrens return (err); 3837b12a1c38Slling } 3838b12a1c38Slling 3839b12a1c38Slling /* 3840fa9e4066Sahrens * Renames the given dataset. 3841fa9e4066Sahrens */ 3842fa9e4066Sahrens int 38436a9cb0eaSEric Schrock zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, 38446a9cb0eaSEric Schrock boolean_t force_unmount) 3845fa9e4066Sahrens { 3846fa9e4066Sahrens int ret; 3847fa9e4066Sahrens zfs_cmd_t zc = { 0 }; 3848fa9e4066Sahrens char *delim; 3849cdf5b4caSmmusante prop_changelist_t *cl = NULL; 3850cdf5b4caSmmusante zfs_handle_t *zhrp = NULL; 3851cdf5b4caSmmusante char *parentname = NULL; 3852fa9e4066Sahrens char parent[ZFS_MAXNAMELEN]; 385399653d4eSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 385499653d4eSeschrock char errbuf[1024]; 3855fa9e4066Sahrens 3856fa9e4066Sahrens /* if we have the same exact name, just return success */ 3857fa9e4066Sahrens if (strcmp(zhp->zfs_name, target) == 0) 3858fa9e4066Sahrens return (0); 3859fa9e4066Sahrens 386099653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 386199653d4eSeschrock "cannot rename to '%s'"), target); 386299653d4eSeschrock 3863fa9e4066Sahrens /* 3864fa9e4066Sahrens * Make sure the target name is valid 3865fa9e4066Sahrens */ 3866fa9e4066Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { 386798579b20Snd150628 if ((strchr(target, '@') == NULL) || 386898579b20Snd150628 *target == '@') { 386998579b20Snd150628 /* 387098579b20Snd150628 * Snapshot target name is abbreviated, 387198579b20Snd150628 * reconstruct full dataset name 387298579b20Snd150628 */ 387398579b20Snd150628 (void) strlcpy(parent, zhp->zfs_name, 387498579b20Snd150628 sizeof (parent)); 387598579b20Snd150628 delim = strchr(parent, '@'); 387698579b20Snd150628 if (strchr(target, '@') == NULL) 387798579b20Snd150628 *(++delim) = '\0'; 387898579b20Snd150628 else 387998579b20Snd150628 *delim = '\0'; 388098579b20Snd150628 (void) strlcat(parent, target, sizeof (parent)); 388198579b20Snd150628 target = parent; 388298579b20Snd150628 } else { 3883fa9e4066Sahrens /* 3884fa9e4066Sahrens * Make sure we're renaming within the same dataset. 3885fa9e4066Sahrens */ 388698579b20Snd150628 delim = strchr(target, '@'); 388798579b20Snd150628 if (strncmp(zhp->zfs_name, target, delim - target) 388898579b20Snd150628 != 0 || zhp->zfs_name[delim - target] != '@') { 388999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 389098579b20Snd150628 "snapshots must be part of same " 389198579b20Snd150628 "dataset")); 389298579b20Snd150628 return (zfs_error(hdl, EZFS_CROSSTARGET, 389398579b20Snd150628 errbuf)); 3894fa9e4066Sahrens } 389598579b20Snd150628 } 3896f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 389798579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3898fa9e4066Sahrens } else { 3899cdf5b4caSmmusante if (recursive) { 3900cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3901cdf5b4caSmmusante "recursive rename must be a snapshot")); 3902cdf5b4caSmmusante return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); 3903cdf5b4caSmmusante } 3904cdf5b4caSmmusante 3905f18faf3fSek110237 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) 390698579b20Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3907e9dbad6fSeschrock 3908fa9e4066Sahrens /* validate parents */ 3909d41c4376SMark J Musante if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) 3910fa9e4066Sahrens return (-1); 3911fa9e4066Sahrens 3912fa9e4066Sahrens /* make sure we're in the same pool */ 3913fa9e4066Sahrens verify((delim = strchr(target, '/')) != NULL); 3914fa9e4066Sahrens if (strncmp(zhp->zfs_name, target, delim - target) != 0 || 3915fa9e4066Sahrens zhp->zfs_name[delim - target] != '/') { 391699653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 391799653d4eSeschrock "datasets must be within same pool")); 391899653d4eSeschrock return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); 3919fa9e4066Sahrens } 3920f2fdf992Snd150628 3921f2fdf992Snd150628 /* new name cannot be a child of the current dataset name */ 3922d41c4376SMark J Musante if (is_descendant(zhp->zfs_name, target)) { 3923f2fdf992Snd150628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3924d41c4376SMark J Musante "New dataset name cannot be a descendant of " 3925f2fdf992Snd150628 "current dataset name")); 3926f2fdf992Snd150628 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); 3927f2fdf992Snd150628 } 3928fa9e4066Sahrens } 3929fa9e4066Sahrens 393099653d4eSeschrock (void) snprintf(errbuf, sizeof (errbuf), 393199653d4eSeschrock dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); 393299653d4eSeschrock 3933fa9e4066Sahrens if (getzoneid() == GLOBAL_ZONEID && 3934fa9e4066Sahrens zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { 393599653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 393699653d4eSeschrock "dataset is used in a non-global zone")); 393799653d4eSeschrock return (zfs_error(hdl, EZFS_ZONED, errbuf)); 3938fa9e4066Sahrens } 3939fa9e4066Sahrens 3940cdf5b4caSmmusante if (recursive) { 3941f0c5ee21Smmusante parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); 3942f0c5ee21Smmusante if (parentname == NULL) { 3943f0c5ee21Smmusante ret = -1; 3944f0c5ee21Smmusante goto error; 3945f0c5ee21Smmusante } 3946cdf5b4caSmmusante delim = strchr(parentname, '@'); 3947cdf5b4caSmmusante *delim = '\0'; 3948990b4856Slling zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); 3949cdf5b4caSmmusante if (zhrp == NULL) { 3950f0c5ee21Smmusante ret = -1; 3951f0c5ee21Smmusante goto error; 3952cdf5b4caSmmusante } 395333cde0d0SMatthew Ahrens } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { 39546a9cb0eaSEric Schrock if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0, 39556a9cb0eaSEric Schrock force_unmount ? MS_FORCE : 0)) == NULL) 395699653d4eSeschrock return (-1); 3957fa9e4066Sahrens 3958fa9e4066Sahrens if (changelist_haszonedchild(cl)) { 395999653d4eSeschrock zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 396099653d4eSeschrock "child dataset with inherited mountpoint is used " 396199653d4eSeschrock "in a non-global zone")); 3962e9dbad6fSeschrock (void) zfs_error(hdl, EZFS_ZONED, errbuf); 3963fa9e4066Sahrens goto error; 3964fa9e4066Sahrens } 3965fa9e4066Sahrens 3966fa9e4066Sahrens if ((ret = changelist_prefix(cl)) != 0) 3967fa9e4066Sahrens goto error; 3968cdf5b4caSmmusante } 3969fa9e4066Sahrens 3970e9dbad6fSeschrock if (ZFS_IS_VOLUME(zhp)) 3971fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZVOL; 3972fa9e4066Sahrens else 3973fa9e4066Sahrens zc.zc_objset_type = DMU_OST_ZFS; 3974fa9e4066Sahrens 397598579b20Snd150628 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 3976e9dbad6fSeschrock (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); 397798579b20Snd150628 3978cdf5b4caSmmusante zc.zc_cookie = recursive; 3979cdf5b4caSmmusante 3980ecd6cf80Smarks if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { 3981cdf5b4caSmmusante /* 3982cdf5b4caSmmusante * if it was recursive, the one that actually failed will 3983cdf5b4caSmmusante * be in zc.zc_name 3984cdf5b4caSmmusante */ 3985cdf5b4caSmmusante (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 39863cb34c60Sahrens "cannot rename '%s'"), zc.zc_name); 3987cdf5b4caSmmusante 3988cdf5b4caSmmusante if (recursive && errno == EEXIST) { 3989cdf5b4caSmmusante zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 3990cdf5b4caSmmusante "a child dataset already has a snapshot " 3991cdf5b4caSmmusante "with the new name")); 3992a10acbd6Seschrock (void) zfs_error(hdl, EZFS_EXISTS, errbuf); 3993cdf5b4caSmmusante } else { 399499653d4eSeschrock (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); 3995cdf5b4caSmmusante } 3996fa9e4066Sahrens 3997fa9e4066Sahrens /* 3998fa9e4066Sahrens * On failure, we still want to remount any filesystems that 3999fa9e4066Sahrens * were previously mounted, so we don't alter the system state. 4000fa9e4066Sahrens */ 400133cde0d0SMatthew Ahrens if (cl != NULL) 4002fa9e4066Sahrens (void) changelist_postfix(cl); 4003cdf5b4caSmmusante } else { 400433cde0d0SMatthew Ahrens if (cl != NULL) { 4005fa9e4066Sahrens changelist_rename(cl, zfs_get_name(zhp), target); 4006fa9e4066Sahrens ret = changelist_postfix(cl); 4007fa9e4066Sahrens } 4008cdf5b4caSmmusante } 4009fa9e4066Sahrens 4010fa9e4066Sahrens error: 401133cde0d0SMatthew Ahrens if (parentname != NULL) { 4012cdf5b4caSmmusante free(parentname); 4013cdf5b4caSmmusante } 401433cde0d0SMatthew Ahrens if (zhrp != NULL) { 4015cdf5b4caSmmusante zfs_close(zhrp); 4016cdf5b4caSmmusante } 401733cde0d0SMatthew Ahrens if (cl != NULL) { 4018fa9e4066Sahrens changelist_free(cl); 4019cdf5b4caSmmusante } 4020fa9e4066Sahrens return (ret); 4021fa9e4066Sahrens } 4022fa9e4066Sahrens 4023e9dbad6fSeschrock nvlist_t * 4024e9dbad6fSeschrock zfs_get_user_props(zfs_handle_t *zhp) 4025e9dbad6fSeschrock { 4026e9dbad6fSeschrock return (zhp->zfs_user_props); 4027e9dbad6fSeschrock } 4028e9dbad6fSeschrock 402992241e0bSTom Erickson nvlist_t * 403092241e0bSTom Erickson zfs_get_recvd_props(zfs_handle_t *zhp) 403192241e0bSTom Erickson { 403292241e0bSTom Erickson if (zhp->zfs_recvd_props == NULL) 403392241e0bSTom Erickson if (get_recvd_props_ioctl(zhp) != 0) 403492241e0bSTom Erickson return (NULL); 403592241e0bSTom Erickson return (zhp->zfs_recvd_props); 403692241e0bSTom Erickson } 403792241e0bSTom Erickson 4038e9dbad6fSeschrock /* 4039e9dbad6fSeschrock * This function is used by 'zfs list' to determine the exact set of columns to 4040e9dbad6fSeschrock * display, and their maximum widths. This does two main things: 4041e9dbad6fSeschrock * 4042e9dbad6fSeschrock * - If this is a list of all properties, then expand the list to include 4043e9dbad6fSeschrock * all native properties, and set a flag so that for each dataset we look 4044e9dbad6fSeschrock * for new unique user properties and add them to the list. 4045e9dbad6fSeschrock * 4046e9dbad6fSeschrock * - For non fixed-width properties, keep track of the maximum width seen 404792241e0bSTom Erickson * so that we can size the column appropriately. If the user has 404892241e0bSTom Erickson * requested received property values, we also need to compute the width 404992241e0bSTom Erickson * of the RECEIVED column. 4050e9dbad6fSeschrock */ 4051e9dbad6fSeschrock int 405243d68d68SYuri Pankov zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, 405343d68d68SYuri Pankov boolean_t literal) 4054e9dbad6fSeschrock { 4055e9dbad6fSeschrock libzfs_handle_t *hdl = zhp->zfs_hdl; 4056990b4856Slling zprop_list_t *entry; 4057990b4856Slling zprop_list_t **last, **start; 4058e9dbad6fSeschrock nvlist_t *userprops, *propval; 4059e9dbad6fSeschrock nvpair_t *elem; 4060e9dbad6fSeschrock char *strval; 4061e9dbad6fSeschrock char buf[ZFS_MAXPROPLEN]; 4062e9dbad6fSeschrock 4063990b4856Slling if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) 4064e9dbad6fSeschrock return (-1); 4065e9dbad6fSeschrock 4066e9dbad6fSeschrock userprops = zfs_get_user_props(zhp); 4067e9dbad6fSeschrock 4068e9dbad6fSeschrock entry = *plp; 4069e9dbad6fSeschrock if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { 4070e9dbad6fSeschrock /* 4071e9dbad6fSeschrock * Go through and add any user properties as necessary. We 4072e9dbad6fSeschrock * start by incrementing our list pointer to the first 4073e9dbad6fSeschrock * non-native property. 4074e9dbad6fSeschrock */ 4075e9dbad6fSeschrock start = plp; 4076e9dbad6fSeschrock while (*start != NULL) { 4077990b4856Slling if ((*start)->pl_prop == ZPROP_INVAL) 4078e9dbad6fSeschrock break; 4079e9dbad6fSeschrock start = &(*start)->pl_next; 4080e9dbad6fSeschrock } 4081e9dbad6fSeschrock 4082e9dbad6fSeschrock elem = NULL; 4083e9dbad6fSeschrock while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { 4084e9dbad6fSeschrock /* 4085e9dbad6fSeschrock * See if we've already found this property in our list. 4086e9dbad6fSeschrock */ 4087e9dbad6fSeschrock for (last = start; *last != NULL; 4088e9dbad6fSeschrock last = &(*last)->pl_next) { 4089e9dbad6fSeschrock if (strcmp((*last)->pl_user_prop, 4090e9dbad6fSeschrock nvpair_name(elem)) == 0) 4091e9dbad6fSeschrock break; 4092e9dbad6fSeschrock } 4093e9dbad6fSeschrock 4094e9dbad6fSeschrock if (*last == NULL) { 4095e9dbad6fSeschrock if ((entry = zfs_alloc(hdl, 4096990b4856Slling sizeof (zprop_list_t))) == NULL || 4097e9dbad6fSeschrock ((entry->pl_user_prop = zfs_strdup(hdl, 4098e9dbad6fSeschrock nvpair_name(elem)))) == NULL) { 4099e9dbad6fSeschrock free(entry); 4100e9dbad6fSeschrock return (-1); 4101e9dbad6fSeschrock } 4102e9dbad6fSeschrock 4103990b4856Slling entry->pl_prop = ZPROP_INVAL; 4104e9dbad6fSeschrock entry->pl_width = strlen(nvpair_name(elem)); 4105e9dbad6fSeschrock entry->pl_all = B_TRUE; 4106e9dbad6fSeschrock *last = entry; 4107e9dbad6fSeschrock } 4108e9dbad6fSeschrock } 4109e9dbad6fSeschrock } 4110e9dbad6fSeschrock 4111e9dbad6fSeschrock /* 4112e9dbad6fSeschrock * Now go through and check the width of any non-fixed columns 4113e9dbad6fSeschrock */ 4114e9dbad6fSeschrock for (entry = *plp; entry != NULL; entry = entry->pl_next) { 411543d68d68SYuri Pankov if (entry->pl_fixed && !literal) 4116e9dbad6fSeschrock continue; 4117e9dbad6fSeschrock 4118990b4856Slling if (entry->pl_prop != ZPROP_INVAL) { 4119e9dbad6fSeschrock if (zfs_prop_get(zhp, entry->pl_prop, 412043d68d68SYuri Pankov buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { 4121e9dbad6fSeschrock if (strlen(buf) > entry->pl_width) 4122e9dbad6fSeschrock entry->pl_width = strlen(buf); 4123e9dbad6fSeschrock } 412492241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 412592241e0bSTom Erickson zfs_prop_to_name(entry->pl_prop), 412643d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 412792241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 412892241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 412992241e0bSTom Erickson } else { 413092241e0bSTom Erickson if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, 413192241e0bSTom Erickson &propval) == 0) { 4132e9dbad6fSeschrock verify(nvlist_lookup_string(propval, 4133990b4856Slling ZPROP_VALUE, &strval) == 0); 4134e9dbad6fSeschrock if (strlen(strval) > entry->pl_width) 4135e9dbad6fSeschrock entry->pl_width = strlen(strval); 4136e9dbad6fSeschrock } 413792241e0bSTom Erickson if (received && zfs_prop_get_recvd(zhp, 413892241e0bSTom Erickson entry->pl_user_prop, 413943d68d68SYuri Pankov buf, sizeof (buf), literal) == 0) 414092241e0bSTom Erickson if (strlen(buf) > entry->pl_recvd_width) 414192241e0bSTom Erickson entry->pl_recvd_width = strlen(buf); 414292241e0bSTom Erickson } 4143e9dbad6fSeschrock } 4144e9dbad6fSeschrock 4145e9dbad6fSeschrock return (0); 4146e9dbad6fSeschrock } 4147ecd6cf80Smarks 4148ecd6cf80Smarks int 4149ecd6cf80Smarks zfs_deleg_share_nfs(libzfs_handle_t *hdl, char *dataset, char *path, 4150743a77edSAlan Wright char *resource, void *export, void *sharetab, 4151743a77edSAlan Wright int sharemax, zfs_share_op_t operation) 4152ecd6cf80Smarks { 4153ecd6cf80Smarks zfs_cmd_t zc = { 0 }; 4154ecd6cf80Smarks int error; 4155ecd6cf80Smarks 4156ecd6cf80Smarks (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4157ecd6cf80Smarks (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4158743a77edSAlan Wright if (resource) 4159743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource, sizeof (zc.zc_string)); 4160ecd6cf80Smarks zc.zc_share.z_sharedata = (uint64_t)(uintptr_t)sharetab; 4161ecd6cf80Smarks zc.zc_share.z_exportdata = (uint64_t)(uintptr_t)export; 4162da6c28aaSamw zc.zc_share.z_sharetype = operation; 4163ecd6cf80Smarks zc.zc_share.z_sharemax = sharemax; 4164ecd6cf80Smarks error = ioctl(hdl->libzfs_fd, ZFS_IOC_SHARE, &zc); 4165ecd6cf80Smarks return (error); 4166ecd6cf80Smarks } 41672e5e9e19SSanjeev Bagewadi 41682e5e9e19SSanjeev Bagewadi void 41692e5e9e19SSanjeev Bagewadi zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) 41702e5e9e19SSanjeev Bagewadi { 41712e5e9e19SSanjeev Bagewadi nvpair_t *curr; 41722e5e9e19SSanjeev Bagewadi 41732e5e9e19SSanjeev Bagewadi /* 41742e5e9e19SSanjeev Bagewadi * Keep a reference to the props-table against which we prune the 41752e5e9e19SSanjeev Bagewadi * properties. 41762e5e9e19SSanjeev Bagewadi */ 41772e5e9e19SSanjeev Bagewadi zhp->zfs_props_table = props; 41782e5e9e19SSanjeev Bagewadi 41792e5e9e19SSanjeev Bagewadi curr = nvlist_next_nvpair(zhp->zfs_props, NULL); 41802e5e9e19SSanjeev Bagewadi 41812e5e9e19SSanjeev Bagewadi while (curr) { 41822e5e9e19SSanjeev Bagewadi zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); 41832e5e9e19SSanjeev Bagewadi nvpair_t *next = nvlist_next_nvpair(zhp->zfs_props, curr); 41842e5e9e19SSanjeev Bagewadi 418514843421SMatthew Ahrens /* 4186faaa6415SEric Schrock * User properties will result in ZPROP_INVAL, and since we 4187faaa6415SEric Schrock * only know how to prune standard ZFS properties, we always 4188faaa6415SEric Schrock * leave these in the list. This can also happen if we 4189faaa6415SEric Schrock * encounter an unknown DSL property (when running older 4190faaa6415SEric Schrock * software, for example). 419114843421SMatthew Ahrens */ 419214843421SMatthew Ahrens if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) 41932e5e9e19SSanjeev Bagewadi (void) nvlist_remove(zhp->zfs_props, 41942e5e9e19SSanjeev Bagewadi nvpair_name(curr), nvpair_type(curr)); 41952e5e9e19SSanjeev Bagewadi curr = next; 41962e5e9e19SSanjeev Bagewadi } 41972e5e9e19SSanjeev Bagewadi } 4198743a77edSAlan Wright 4199743a77edSAlan Wright static int 4200743a77edSAlan Wright zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, 4201743a77edSAlan Wright zfs_smb_acl_op_t cmd, char *resource1, char *resource2) 4202743a77edSAlan Wright { 4203743a77edSAlan Wright zfs_cmd_t zc = { 0 }; 4204743a77edSAlan Wright nvlist_t *nvlist = NULL; 4205743a77edSAlan Wright int error; 4206743a77edSAlan Wright 4207743a77edSAlan Wright (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); 4208743a77edSAlan Wright (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); 4209743a77edSAlan Wright zc.zc_cookie = (uint64_t)cmd; 4210743a77edSAlan Wright 4211743a77edSAlan Wright if (cmd == ZFS_SMB_ACL_RENAME) { 4212743a77edSAlan Wright if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { 4213743a77edSAlan Wright (void) no_memory(hdl); 421430925561SChris Williamson return (0); 4215743a77edSAlan Wright } 4216743a77edSAlan Wright } 4217743a77edSAlan Wright 4218743a77edSAlan Wright switch (cmd) { 4219743a77edSAlan Wright case ZFS_SMB_ACL_ADD: 4220743a77edSAlan Wright case ZFS_SMB_ACL_REMOVE: 4221743a77edSAlan Wright (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); 4222743a77edSAlan Wright break; 4223743a77edSAlan Wright case ZFS_SMB_ACL_RENAME: 4224743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, 4225743a77edSAlan Wright resource1) != 0) { 4226743a77edSAlan Wright (void) no_memory(hdl); 4227743a77edSAlan Wright return (-1); 4228743a77edSAlan Wright } 4229743a77edSAlan Wright if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, 4230743a77edSAlan Wright resource2) != 0) { 4231743a77edSAlan Wright (void) no_memory(hdl); 4232743a77edSAlan Wright return (-1); 4233743a77edSAlan Wright } 4234743a77edSAlan Wright if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { 4235743a77edSAlan Wright nvlist_free(nvlist); 4236743a77edSAlan Wright return (-1); 4237743a77edSAlan Wright } 4238743a77edSAlan Wright break; 4239743a77edSAlan Wright case ZFS_SMB_ACL_PURGE: 4240743a77edSAlan Wright break; 4241743a77edSAlan Wright default: 4242743a77edSAlan Wright return (-1); 4243743a77edSAlan Wright } 4244743a77edSAlan Wright error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); 4245743a77edSAlan Wright if (nvlist) 4246743a77edSAlan Wright nvlist_free(nvlist); 4247743a77edSAlan Wright return (error); 4248743a77edSAlan Wright } 4249743a77edSAlan Wright 4250743a77edSAlan Wright int 4251743a77edSAlan Wright zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, 4252743a77edSAlan Wright char *path, char *resource) 4253743a77edSAlan Wright { 4254743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, 4255743a77edSAlan Wright resource, NULL)); 4256743a77edSAlan Wright } 4257743a77edSAlan Wright 4258743a77edSAlan Wright int 4259743a77edSAlan Wright zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, 4260743a77edSAlan Wright char *path, char *resource) 4261743a77edSAlan Wright { 4262743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, 4263743a77edSAlan Wright resource, NULL)); 4264743a77edSAlan Wright } 4265743a77edSAlan Wright 4266743a77edSAlan Wright int 4267743a77edSAlan Wright zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) 4268743a77edSAlan Wright { 4269743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, 4270743a77edSAlan Wright NULL, NULL)); 4271743a77edSAlan Wright } 4272743a77edSAlan Wright 4273743a77edSAlan Wright int 4274743a77edSAlan Wright zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, 4275743a77edSAlan Wright char *oldname, char *newname) 4276743a77edSAlan Wright { 4277743a77edSAlan Wright return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, 4278743a77edSAlan Wright oldname, newname)); 4279743a77edSAlan Wright } 428014843421SMatthew Ahrens 428114843421SMatthew Ahrens int 428214843421SMatthew Ahrens zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, 428314843421SMatthew Ahrens zfs_userspace_cb_t func, void *arg) 428414843421SMatthew Ahrens { 428514843421SMatthew Ahrens zfs_cmd_t zc = { 0 }; 428614843421SMatthew Ahrens zfs_useracct_t buf[100]; 428770f56fa6SYuri Pankov libzfs_handle_t *hdl = zhp->zfs_hdl; 428870f56fa6SYuri Pankov int ret; 428914843421SMatthew Ahrens 429019b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 429114843421SMatthew Ahrens 429214843421SMatthew Ahrens zc.zc_objset_type = type; 429314843421SMatthew Ahrens zc.zc_nvlist_dst = (uintptr_t)buf; 429414843421SMatthew Ahrens 429570f56fa6SYuri Pankov for (;;) { 429614843421SMatthew Ahrens zfs_useracct_t *zua = buf; 429714843421SMatthew Ahrens 429814843421SMatthew Ahrens zc.zc_nvlist_dst_size = sizeof (buf); 429970f56fa6SYuri Pankov if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { 43003b2aab18SMatthew Ahrens char errbuf[1024]; 430170f56fa6SYuri Pankov 430270f56fa6SYuri Pankov (void) snprintf(errbuf, sizeof (errbuf), 430370f56fa6SYuri Pankov dgettext(TEXT_DOMAIN, 430470f56fa6SYuri Pankov "cannot get used/quota for %s"), zc.zc_name); 430570f56fa6SYuri Pankov return (zfs_standard_error_fmt(hdl, errno, errbuf)); 430670f56fa6SYuri Pankov } 430770f56fa6SYuri Pankov if (zc.zc_nvlist_dst_size == 0) 430814843421SMatthew Ahrens break; 430914843421SMatthew Ahrens 431014843421SMatthew Ahrens while (zc.zc_nvlist_dst_size > 0) { 431170f56fa6SYuri Pankov if ((ret = func(arg, zua->zu_domain, zua->zu_rid, 431270f56fa6SYuri Pankov zua->zu_space)) != 0) 431370f56fa6SYuri Pankov return (ret); 431414843421SMatthew Ahrens zua++; 431514843421SMatthew Ahrens zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); 431614843421SMatthew Ahrens } 431714843421SMatthew Ahrens } 431814843421SMatthew Ahrens 431970f56fa6SYuri Pankov return (0); 432014843421SMatthew Ahrens } 4321842727c2SChris Kirby 43223b2aab18SMatthew Ahrens struct holdarg { 43233b2aab18SMatthew Ahrens nvlist_t *nvl; 43243b2aab18SMatthew Ahrens const char *snapname; 43253b2aab18SMatthew Ahrens const char *tag; 43263b2aab18SMatthew Ahrens boolean_t recursive; 4327bb6e7075SMatthew Ahrens int error; 43283b2aab18SMatthew Ahrens }; 43293b2aab18SMatthew Ahrens 43303b2aab18SMatthew Ahrens static int 43313b2aab18SMatthew Ahrens zfs_hold_one(zfs_handle_t *zhp, void *arg) 43323b2aab18SMatthew Ahrens { 43333b2aab18SMatthew Ahrens struct holdarg *ha = arg; 43343b2aab18SMatthew Ahrens char name[ZFS_MAXNAMELEN]; 43353b2aab18SMatthew Ahrens int rv = 0; 43363b2aab18SMatthew Ahrens 43373b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 43383b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 43393b2aab18SMatthew Ahrens 4340a7a845e4SSteven Hartland if (lzc_exists(name)) 43413b2aab18SMatthew Ahrens fnvlist_add_string(ha->nvl, name, ha->tag); 43423b2aab18SMatthew Ahrens 43433b2aab18SMatthew Ahrens if (ha->recursive) 43443b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); 43453b2aab18SMatthew Ahrens zfs_close(zhp); 43463b2aab18SMatthew Ahrens return (rv); 43473b2aab18SMatthew Ahrens } 43483b2aab18SMatthew Ahrens 4349842727c2SChris Kirby int 4350842727c2SChris Kirby zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, 4351a7a845e4SSteven Hartland boolean_t recursive, int cleanup_fd) 4352842727c2SChris Kirby { 43533b2aab18SMatthew Ahrens int ret; 43543b2aab18SMatthew Ahrens struct holdarg ha; 4355842727c2SChris Kirby 43563b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 43573b2aab18SMatthew Ahrens ha.snapname = snapname; 43583b2aab18SMatthew Ahrens ha.tag = tag; 43593b2aab18SMatthew Ahrens ha.recursive = recursive; 43603b2aab18SMatthew Ahrens (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); 4361013023d4SMartin Matuska 4362a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4363a7a845e4SSteven Hartland char errbuf[1024]; 4364a7a845e4SSteven Hartland 4365013023d4SMartin Matuska fnvlist_free(ha.nvl); 4366013023d4SMartin Matuska ret = ENOENT; 4367013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4368013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4369013023d4SMartin Matuska "cannot hold snapshot '%s@%s'"), 4370013023d4SMartin Matuska zhp->zfs_name, snapname); 4371a7a845e4SSteven Hartland (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); 4372013023d4SMartin Matuska return (ret); 4373013023d4SMartin Matuska } 4374013023d4SMartin Matuska 4375a7a845e4SSteven Hartland ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); 43763b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4377a7f53a56SChris Kirby 4378a7a845e4SSteven Hartland return (ret); 4379a7a845e4SSteven Hartland } 4380842727c2SChris Kirby 4381a7a845e4SSteven Hartland int 4382a7a845e4SSteven Hartland zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) 4383a7a845e4SSteven Hartland { 4384a7a845e4SSteven Hartland int ret; 4385a7a845e4SSteven Hartland nvlist_t *errors; 4386a7a845e4SSteven Hartland libzfs_handle_t *hdl = zhp->zfs_hdl; 4387a7a845e4SSteven Hartland char errbuf[1024]; 4388a7a845e4SSteven Hartland nvpair_t *elem; 4389a7a845e4SSteven Hartland 4390a7a845e4SSteven Hartland errors = NULL; 4391a7a845e4SSteven Hartland ret = lzc_hold(holds, cleanup_fd, &errors); 4392a7a845e4SSteven Hartland 4393a7a845e4SSteven Hartland if (ret == 0) { 4394a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4395a7a845e4SSteven Hartland fnvlist_free(errors); 4396a7a845e4SSteven Hartland return (0); 4397a7a845e4SSteven Hartland } 4398a7a845e4SSteven Hartland 4399a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 44003b2aab18SMatthew Ahrens /* no hold-specific errors */ 44013b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 44023b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, "cannot hold")); 44033b2aab18SMatthew Ahrens switch (ret) { 44043b2aab18SMatthew Ahrens case ENOTSUP: 44053b2aab18SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 44063b2aab18SMatthew Ahrens "pool must be upgraded")); 44073b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 44083b2aab18SMatthew Ahrens break; 44093b2aab18SMatthew Ahrens case EINVAL: 44103b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 44113b2aab18SMatthew Ahrens break; 44123b2aab18SMatthew Ahrens default: 44133b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, ret, errbuf); 44143b2aab18SMatthew Ahrens } 44153b2aab18SMatthew Ahrens } 4416842727c2SChris Kirby 44173b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 44183b2aab18SMatthew Ahrens elem != NULL; 44193b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 44203b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 44213b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 44223b2aab18SMatthew Ahrens "cannot hold snapshot '%s'"), nvpair_name(elem)); 44233b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 442415508ac0SChris Kirby case E2BIG: 442515508ac0SChris Kirby /* 442615508ac0SChris Kirby * Temporary tags wind up having the ds object id 442715508ac0SChris Kirby * prepended. So even if we passed the length check 442815508ac0SChris Kirby * above, it's still possible for the tag to wind 442915508ac0SChris Kirby * up being slightly too long. 443015508ac0SChris Kirby */ 44313b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); 44323b2aab18SMatthew Ahrens break; 4433842727c2SChris Kirby case EINVAL: 44343b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 44353b2aab18SMatthew Ahrens break; 4436842727c2SChris Kirby case EEXIST: 44373b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); 44383b2aab18SMatthew Ahrens break; 4439842727c2SChris Kirby default: 44403b2aab18SMatthew Ahrens (void) zfs_standard_error(hdl, 44413b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 4442842727c2SChris Kirby } 4443842727c2SChris Kirby } 4444842727c2SChris Kirby 44453b2aab18SMatthew Ahrens fnvlist_free(errors); 44463b2aab18SMatthew Ahrens return (ret); 44473b2aab18SMatthew Ahrens } 44483b2aab18SMatthew Ahrens 44493b2aab18SMatthew Ahrens static int 44503b2aab18SMatthew Ahrens zfs_release_one(zfs_handle_t *zhp, void *arg) 44513b2aab18SMatthew Ahrens { 44523b2aab18SMatthew Ahrens struct holdarg *ha = arg; 44533b2aab18SMatthew Ahrens char name[ZFS_MAXNAMELEN]; 44543b2aab18SMatthew Ahrens int rv = 0; 4455bb6e7075SMatthew Ahrens nvlist_t *existing_holds; 44563b2aab18SMatthew Ahrens 44573b2aab18SMatthew Ahrens (void) snprintf(name, sizeof (name), 44583b2aab18SMatthew Ahrens "%s@%s", zhp->zfs_name, ha->snapname); 44593b2aab18SMatthew Ahrens 4460bb6e7075SMatthew Ahrens if (lzc_get_holds(name, &existing_holds) != 0) { 4461bb6e7075SMatthew Ahrens ha->error = ENOENT; 4462bb6e7075SMatthew Ahrens } else if (!nvlist_exists(existing_holds, ha->tag)) { 4463bb6e7075SMatthew Ahrens ha->error = ESRCH; 4464bb6e7075SMatthew Ahrens } else { 4465bb6e7075SMatthew Ahrens nvlist_t *torelease = fnvlist_alloc(); 4466bb6e7075SMatthew Ahrens fnvlist_add_boolean(torelease, ha->tag); 4467bb6e7075SMatthew Ahrens fnvlist_add_nvlist(ha->nvl, name, torelease); 4468bb6e7075SMatthew Ahrens fnvlist_free(torelease); 44693b2aab18SMatthew Ahrens } 44703b2aab18SMatthew Ahrens 44713b2aab18SMatthew Ahrens if (ha->recursive) 44723b2aab18SMatthew Ahrens rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); 44733b2aab18SMatthew Ahrens zfs_close(zhp); 44743b2aab18SMatthew Ahrens return (rv); 4475842727c2SChris Kirby } 4476842727c2SChris Kirby 4477842727c2SChris Kirby int 4478842727c2SChris Kirby zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, 4479842727c2SChris Kirby boolean_t recursive) 4480842727c2SChris Kirby { 44813b2aab18SMatthew Ahrens int ret; 44823b2aab18SMatthew Ahrens struct holdarg ha; 4483a7a845e4SSteven Hartland nvlist_t *errors = NULL; 44843b2aab18SMatthew Ahrens nvpair_t *elem; 4485842727c2SChris Kirby libzfs_handle_t *hdl = zhp->zfs_hdl; 4486013023d4SMartin Matuska char errbuf[1024]; 4487842727c2SChris Kirby 44883b2aab18SMatthew Ahrens ha.nvl = fnvlist_alloc(); 44893b2aab18SMatthew Ahrens ha.snapname = snapname; 44903b2aab18SMatthew Ahrens ha.tag = tag; 44913b2aab18SMatthew Ahrens ha.recursive = recursive; 4492bb6e7075SMatthew Ahrens ha.error = 0; 44933b2aab18SMatthew Ahrens (void) zfs_release_one(zfs_handle_dup(zhp), &ha); 4494013023d4SMartin Matuska 4495a7a845e4SSteven Hartland if (nvlist_empty(ha.nvl)) { 4496013023d4SMartin Matuska fnvlist_free(ha.nvl); 4497bb6e7075SMatthew Ahrens ret = ha.error; 4498013023d4SMartin Matuska (void) snprintf(errbuf, sizeof (errbuf), 4499013023d4SMartin Matuska dgettext(TEXT_DOMAIN, 4500013023d4SMartin Matuska "cannot release hold from snapshot '%s@%s'"), 4501013023d4SMartin Matuska zhp->zfs_name, snapname); 4502bb6e7075SMatthew Ahrens if (ret == ESRCH) { 4503bb6e7075SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 4504bb6e7075SMatthew Ahrens } else { 4505013023d4SMartin Matuska (void) zfs_standard_error(hdl, ret, errbuf); 4506bb6e7075SMatthew Ahrens } 4507013023d4SMartin Matuska return (ret); 4508013023d4SMartin Matuska } 4509013023d4SMartin Matuska 45103b2aab18SMatthew Ahrens ret = lzc_release(ha.nvl, &errors); 45113b2aab18SMatthew Ahrens fnvlist_free(ha.nvl); 4512842727c2SChris Kirby 4513a7a845e4SSteven Hartland if (ret == 0) { 4514a7a845e4SSteven Hartland /* There may be errors even in the success case. */ 4515a7a845e4SSteven Hartland fnvlist_free(errors); 45163b2aab18SMatthew Ahrens return (0); 4517a7a845e4SSteven Hartland } 4518842727c2SChris Kirby 4519a7a845e4SSteven Hartland if (nvlist_empty(errors)) { 45203b2aab18SMatthew Ahrens /* no hold-specific errors */ 4521842727c2SChris Kirby (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, 45223b2aab18SMatthew Ahrens "cannot release")); 4523842727c2SChris Kirby switch (errno) { 4524842727c2SChris Kirby case ENOTSUP: 4525842727c2SChris Kirby zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 4526842727c2SChris Kirby "pool must be upgraded")); 45273b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); 45283b2aab18SMatthew Ahrens break; 4529842727c2SChris Kirby default: 45303b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, errno, errbuf); 4531842727c2SChris Kirby } 4532842727c2SChris Kirby } 4533842727c2SChris Kirby 45343b2aab18SMatthew Ahrens for (elem = nvlist_next_nvpair(errors, NULL); 45353b2aab18SMatthew Ahrens elem != NULL; 45363b2aab18SMatthew Ahrens elem = nvlist_next_nvpair(errors, elem)) { 45373b2aab18SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), 45383b2aab18SMatthew Ahrens dgettext(TEXT_DOMAIN, 45393b2aab18SMatthew Ahrens "cannot release hold from snapshot '%s'"), 45403b2aab18SMatthew Ahrens nvpair_name(elem)); 45413b2aab18SMatthew Ahrens switch (fnvpair_value_int32(elem)) { 45423b2aab18SMatthew Ahrens case ESRCH: 45433b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); 45443b2aab18SMatthew Ahrens break; 45453b2aab18SMatthew Ahrens case EINVAL: 45463b2aab18SMatthew Ahrens (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); 45473b2aab18SMatthew Ahrens break; 45483b2aab18SMatthew Ahrens default: 45493b2aab18SMatthew Ahrens (void) zfs_standard_error_fmt(hdl, 45503b2aab18SMatthew Ahrens fnvpair_value_int32(elem), errbuf); 45513b2aab18SMatthew Ahrens } 45523b2aab18SMatthew Ahrens } 45533b2aab18SMatthew Ahrens 45543b2aab18SMatthew Ahrens fnvlist_free(errors); 45553b2aab18SMatthew Ahrens return (ret); 4556842727c2SChris Kirby } 4557ca45db41SChris Kirby 45581af68beaSAlexander Stetsenko int 45591af68beaSAlexander Stetsenko zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) 45601af68beaSAlexander Stetsenko { 45611af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 45621af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 45631af68beaSAlexander Stetsenko int nvsz = 2048; 45641af68beaSAlexander Stetsenko void *nvbuf; 45651af68beaSAlexander Stetsenko int err = 0; 45663b2aab18SMatthew Ahrens char errbuf[1024]; 45671af68beaSAlexander Stetsenko 45681af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 45691af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 45701af68beaSAlexander Stetsenko 45711af68beaSAlexander Stetsenko tryagain: 45721af68beaSAlexander Stetsenko 45731af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 45741af68beaSAlexander Stetsenko if (nvbuf == NULL) { 45751af68beaSAlexander Stetsenko err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); 45761af68beaSAlexander Stetsenko goto out; 45771af68beaSAlexander Stetsenko } 45781af68beaSAlexander Stetsenko 45791af68beaSAlexander Stetsenko zc.zc_nvlist_dst_size = nvsz; 45801af68beaSAlexander Stetsenko zc.zc_nvlist_dst = (uintptr_t)nvbuf; 45811af68beaSAlexander Stetsenko 45821af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, ZFS_MAXNAMELEN); 45831af68beaSAlexander Stetsenko 4584d7f601efSGeorge Wilson if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { 45851af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 45861af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), 45871af68beaSAlexander Stetsenko zc.zc_name); 45881af68beaSAlexander Stetsenko switch (errno) { 45891af68beaSAlexander Stetsenko case ENOMEM: 45901af68beaSAlexander Stetsenko free(nvbuf); 45911af68beaSAlexander Stetsenko nvsz = zc.zc_nvlist_dst_size; 45921af68beaSAlexander Stetsenko goto tryagain; 45931af68beaSAlexander Stetsenko 45941af68beaSAlexander Stetsenko case ENOTSUP: 45951af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 45961af68beaSAlexander Stetsenko "pool must be upgraded")); 45971af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 45981af68beaSAlexander Stetsenko break; 45991af68beaSAlexander Stetsenko case EINVAL: 46001af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 46011af68beaSAlexander Stetsenko break; 46021af68beaSAlexander Stetsenko case ENOENT: 46031af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 46041af68beaSAlexander Stetsenko break; 46051af68beaSAlexander Stetsenko default: 46061af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 46071af68beaSAlexander Stetsenko break; 46081af68beaSAlexander Stetsenko } 46091af68beaSAlexander Stetsenko } else { 46101af68beaSAlexander Stetsenko /* success */ 46111af68beaSAlexander Stetsenko int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); 46121af68beaSAlexander Stetsenko if (rc) { 46131af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), dgettext( 46141af68beaSAlexander Stetsenko TEXT_DOMAIN, "cannot get permissions on '%s'"), 46151af68beaSAlexander Stetsenko zc.zc_name); 46161af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, rc, errbuf); 46171af68beaSAlexander Stetsenko } 46181af68beaSAlexander Stetsenko } 46191af68beaSAlexander Stetsenko 46201af68beaSAlexander Stetsenko free(nvbuf); 46211af68beaSAlexander Stetsenko out: 46221af68beaSAlexander Stetsenko return (err); 46231af68beaSAlexander Stetsenko } 46241af68beaSAlexander Stetsenko 46251af68beaSAlexander Stetsenko int 46261af68beaSAlexander Stetsenko zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) 46271af68beaSAlexander Stetsenko { 46281af68beaSAlexander Stetsenko zfs_cmd_t zc = { 0 }; 46291af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 46301af68beaSAlexander Stetsenko char *nvbuf; 46313b2aab18SMatthew Ahrens char errbuf[1024]; 46321af68beaSAlexander Stetsenko size_t nvsz; 46331af68beaSAlexander Stetsenko int err; 46341af68beaSAlexander Stetsenko 46351af68beaSAlexander Stetsenko assert(zhp->zfs_type == ZFS_TYPE_VOLUME || 46361af68beaSAlexander Stetsenko zhp->zfs_type == ZFS_TYPE_FILESYSTEM); 46371af68beaSAlexander Stetsenko 46381af68beaSAlexander Stetsenko err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); 46391af68beaSAlexander Stetsenko assert(err == 0); 46401af68beaSAlexander Stetsenko 46411af68beaSAlexander Stetsenko nvbuf = malloc(nvsz); 46421af68beaSAlexander Stetsenko 46431af68beaSAlexander Stetsenko err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); 46441af68beaSAlexander Stetsenko assert(err == 0); 46451af68beaSAlexander Stetsenko 46461af68beaSAlexander Stetsenko zc.zc_nvlist_src_size = nvsz; 46471af68beaSAlexander Stetsenko zc.zc_nvlist_src = (uintptr_t)nvbuf; 46481af68beaSAlexander Stetsenko zc.zc_perm_action = un; 46491af68beaSAlexander Stetsenko 46501af68beaSAlexander Stetsenko (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); 46511af68beaSAlexander Stetsenko 46521af68beaSAlexander Stetsenko if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { 46531af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 46541af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), 46551af68beaSAlexander Stetsenko zc.zc_name); 46561af68beaSAlexander Stetsenko switch (errno) { 46571af68beaSAlexander Stetsenko case ENOTSUP: 46581af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46591af68beaSAlexander Stetsenko "pool must be upgraded")); 46601af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 46611af68beaSAlexander Stetsenko break; 46621af68beaSAlexander Stetsenko case EINVAL: 46631af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 46641af68beaSAlexander Stetsenko break; 46651af68beaSAlexander Stetsenko case ENOENT: 46661af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 46671af68beaSAlexander Stetsenko break; 46681af68beaSAlexander Stetsenko default: 46691af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 46701af68beaSAlexander Stetsenko break; 46711af68beaSAlexander Stetsenko } 46721af68beaSAlexander Stetsenko } 46731af68beaSAlexander Stetsenko 46741af68beaSAlexander Stetsenko free(nvbuf); 46751af68beaSAlexander Stetsenko 46761af68beaSAlexander Stetsenko return (err); 46771af68beaSAlexander Stetsenko } 46781af68beaSAlexander Stetsenko 46791af68beaSAlexander Stetsenko int 46801af68beaSAlexander Stetsenko zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) 46811af68beaSAlexander Stetsenko { 46823b2aab18SMatthew Ahrens int err; 46833b2aab18SMatthew Ahrens char errbuf[1024]; 46843b2aab18SMatthew Ahrens 46853b2aab18SMatthew Ahrens err = lzc_get_holds(zhp->zfs_name, nvl); 46863b2aab18SMatthew Ahrens 46873b2aab18SMatthew Ahrens if (err != 0) { 46881af68beaSAlexander Stetsenko libzfs_handle_t *hdl = zhp->zfs_hdl; 46891af68beaSAlexander Stetsenko 46901af68beaSAlexander Stetsenko (void) snprintf(errbuf, sizeof (errbuf), 46911af68beaSAlexander Stetsenko dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), 46923b2aab18SMatthew Ahrens zhp->zfs_name); 46933b2aab18SMatthew Ahrens switch (err) { 46941af68beaSAlexander Stetsenko case ENOTSUP: 46951af68beaSAlexander Stetsenko zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 46961af68beaSAlexander Stetsenko "pool must be upgraded")); 46971af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADVERSION, errbuf); 46981af68beaSAlexander Stetsenko break; 46991af68beaSAlexander Stetsenko case EINVAL: 47001af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_BADTYPE, errbuf); 47011af68beaSAlexander Stetsenko break; 47021af68beaSAlexander Stetsenko case ENOENT: 47031af68beaSAlexander Stetsenko err = zfs_error(hdl, EZFS_NOENT, errbuf); 47041af68beaSAlexander Stetsenko break; 47051af68beaSAlexander Stetsenko default: 47061af68beaSAlexander Stetsenko err = zfs_standard_error_fmt(hdl, errno, errbuf); 47071af68beaSAlexander Stetsenko break; 47081af68beaSAlexander Stetsenko } 47091af68beaSAlexander Stetsenko } 47101af68beaSAlexander Stetsenko 47111af68beaSAlexander Stetsenko return (err); 47121af68beaSAlexander Stetsenko } 47131af68beaSAlexander Stetsenko 47143e30c24aSWill Andrews /* 47153e30c24aSWill Andrews * Convert the zvol's volume size to an appropriate reservation. 47163e30c24aSWill Andrews * Note: If this routine is updated, it is necessary to update the ZFS test 47173e30c24aSWill Andrews * suite's shell version in reservation.kshlib. 47183e30c24aSWill Andrews */ 4719c1449561SEric Taylor uint64_t 4720c1449561SEric Taylor zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props) 4721c1449561SEric Taylor { 4722c1449561SEric Taylor uint64_t numdb; 4723c1449561SEric Taylor uint64_t nblocks, volblocksize; 4724c1449561SEric Taylor int ncopies; 4725c1449561SEric Taylor char *strval; 4726c1449561SEric Taylor 4727c1449561SEric Taylor if (nvlist_lookup_string(props, 4728c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) 4729c1449561SEric Taylor ncopies = atoi(strval); 4730c1449561SEric Taylor else 4731c1449561SEric Taylor ncopies = 1; 4732c1449561SEric Taylor if (nvlist_lookup_uint64(props, 4733c1449561SEric Taylor zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), 4734c1449561SEric Taylor &volblocksize) != 0) 4735c1449561SEric Taylor volblocksize = ZVOL_DEFAULT_BLOCKSIZE; 4736c1449561SEric Taylor nblocks = volsize/volblocksize; 4737c1449561SEric Taylor /* start with metadnode L0-L6 */ 4738c1449561SEric Taylor numdb = 7; 4739c1449561SEric Taylor /* calculate number of indirects */ 4740c1449561SEric Taylor while (nblocks > 1) { 4741c1449561SEric Taylor nblocks += DNODES_PER_LEVEL - 1; 4742c1449561SEric Taylor nblocks /= DNODES_PER_LEVEL; 4743c1449561SEric Taylor numdb += nblocks; 4744c1449561SEric Taylor } 4745c1449561SEric Taylor numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); 4746c1449561SEric Taylor volsize *= ncopies; 4747c1449561SEric Taylor /* 4748c1449561SEric Taylor * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't 4749c1449561SEric Taylor * compressed, but in practice they compress down to about 4750c1449561SEric Taylor * 1100 bytes 4751c1449561SEric Taylor */ 4752c1449561SEric Taylor numdb *= 1ULL << DN_MAX_INDBLKSHIFT; 4753c1449561SEric Taylor volsize += numdb; 4754c1449561SEric Taylor return (volsize); 4755c1449561SEric Taylor } 4756